2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: dttypes.c /main/6 1998/04/20 13:01:30 mgreess $ */
25 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
26 * (c) Copyright 1993, 1994 International Business Machines Corp. *
27 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
28 * (c) Copyright 1993, 1994 Novell, Inc. *
30 #include <sys/types.h>
36 #include <X11/Shell.h>
37 #define INIT char *sp = instring;
38 #define GETC() (*sp++)
40 #define UNGETC(c) (--sp)
41 #define RETURN(c) return(c)
42 #define ERROR(c) {rexp_errno = c; return((char *)0);}
43 static int rexp_errno = 0;
44 #if defined(CSRG_BASED) || defined(__linux__)
53 #include <Dt/EnvControlP.h>
55 #if !defined(NL_CAT_LOCALE)
56 #define NL_CAT_LOCALE 0
59 extern char ** _DtsMMListDb(void);
69 static int read_errors = 0;
70 static char *error_str[] =
72 "Invalid DataBase\n", /* BAD_DB */
73 "Not a valid argument\n", /* NO_ARGUMENT */
74 "invalid rec_info field in -w option\n", /* REC_INFO_IN_STATE */
75 "Not in -w or -l option\n", /* NO_STATE */
76 "Unknown option\n" /* INVALIDE_ARG */
100 static nl_catd dtcatd = 0;
103 init(int *argc, char **argv)
106 XtAppContext appContext;
110 toplevel = XtInitialize(argv[0], "Dttype", NULL, 0,
113 XtSetArg(args[0], XmNallowShellResize, 1);
114 XtSetArg(args[1], XmNmappedWhenManaged, 0);
115 XtSetArg(args[2], XmNheight, 1);
116 XtSetArg(args[3], XmNwidth, 1);
117 XtSetValues(toplevel, args, 4);
118 XtRealizeWidget(toplevel);
120 if (DtInitialize(XtDisplay(toplevel), toplevel, argv[0],
123 fprintf(stderr, "couldn't initialize everything\n");
127 /* Load the filetype/action dbs; DtActionInvoke() requires this */
133 add_rec(int rec, List *l)
137 if(l->rec_total == 0 || l->rec_count >= l->rec_total-2)
140 l->rec_list = (int *)realloc(l->rec_list, (l->rec_total)*sizeof(int));
142 l->rec_list[l->rec_count] = rec;
144 l->rec_list[l->rec_count] = -1;
154 DtDtsMMDatabase *db_ptr;
155 DtDtsMMDatabase *db_ptr_list;
156 DtDtsMMRecord *rec_ptr;
157 DtDtsMMRecord *rec_ptr_list;
158 DtDtsMMField *fld_ptr;
159 DtDtsMMField *fld_ptr_list;
161 #if defined(CSRG_BASED) || defined(__linux__)
167 char expbuf_rn[2000];
168 char expbuf_fn[2000];
169 char expbuf_fv[2000];
170 char expbuf_df[2000];
172 memset(expbuf_rn, '\0', sizeof(expbuf_rn));
173 memset(expbuf_fn, '\0', sizeof(expbuf_fn));
174 memset(expbuf_fv, '\0', sizeof(expbuf_fv));
175 memset(expbuf_df, '\0', sizeof(expbuf_df));
178 #if defined(CSRG_BASED) || defined(__linux__)
179 if(regcomp(®ex_rn, l->rec_name?l->rec_name:"^.*", 0) != 0)
181 if((compile(l->rec_name?l->rec_name:"^.*",
183 &expbuf_rn[sizeof(expbuf_rn)],
188 fprintf(stderr, catgets(dtcatd, 1, 36, "error in regular expression %s\n"), l->rec_name?l->rec_name:"(NULL)");
192 #if defined(CSRG_BASED) || defined(__linux__)
193 if(regcomp(®ex_fn, l->fld_name?l->fld_name:"^.*", 0) != 0)
195 if((compile(l->fld_name?l->fld_name:"^.*",
197 &expbuf_fn[sizeof(expbuf_fn)],
202 fprintf(stderr, catgets(dtcatd, 1, 36, "error in regular expression %s\n"), l->fld_name?l->fld_name:"(NULL)");
206 #if defined(CSRG_BASED) || defined(__linux__)
207 if(regcomp(®ex_fv, l->fld_value?l->fld_value:"^.*", 0) != 0)
209 if((compile(l->fld_value?l->fld_value:"^.*",
211 &expbuf_fv[sizeof(expbuf_fv)],
216 fprintf(stderr, catgets(dtcatd, 1, 36, "error in regular expression %s\n"), l->fld_value?l->fld_value:"(NULL)");
220 #if defined(CSRG_BASED) || defined(__linux__)
221 if(regcomp(®ex_df, l->display_fld?l->display_fld:"^.*", 0) != 0)
223 if((compile(l->display_fld?l->display_fld:"^.*",
225 &expbuf_df[sizeof(expbuf_df)],
230 fprintf(stderr, catgets(dtcatd, 1, 36, "error in regular expression %s\n"), l->display_fld?l->display_fld:"(NULL)");
234 db_ptr = _DtDtsMMGet(l->db_name);
235 rec_ptr_list = _DtDtsMMGetPtr(db_ptr->recordList);
236 for(rec = 0; rec < db_ptr->recordCount; rec++)
238 rec_ptr = &rec_ptr_list[rec];
239 fld_ptr_list = _DtDtsMMGetPtr(rec_ptr->fieldList);
240 #if defined(CSRG_BASED) || defined(__linux__)
241 if(regexec(®ex_rn,
242 (char *)_DtDtsMMBosonToString(rec_ptr->recordName),
245 if(advance((char *)_DtDtsMMBosonToString(rec_ptr->recordName), expbuf_rn) != 0)
248 for(fld = 0; fld < rec_ptr->fieldCount; fld++)
253 fld_ptr = &fld_ptr_list[fld];
255 fn = _DtDtsMMExpandValue(_DtDtsMMBosonToString(fld_ptr->fieldName));
256 fv = _DtDtsMMExpandValue(_DtDtsMMBosonToString(fld_ptr->fieldValue));
258 #if defined(CSRG_BASED) || defined(__linux__)
259 if((regexec(®ex_fn, fn, 0, NULL, 0) == 0) &&
260 ((fld_ptr->fieldValue==0?
262 catgets(dtcatd, 1, 4, "NULL"),
268 if((advance(fn, expbuf_fn) != 0) &&
269 ((fld_ptr->fieldValue==0?advance(catgets(dtcatd, 1, 4, "NULL"), expbuf_fv):advance(fv, expbuf_fv)) != 0))
273 _DtDtsMMSafeFree(fn);
274 _DtDtsMMSafeFree(fv);
277 _DtDtsMMSafeFree(fn);
278 _DtDtsMMSafeFree(fv);
283 for(i = 0; l->rec_list && l->rec_list[i] != -1; i++)
285 rec = l->rec_list[i];
286 rec_ptr = &rec_ptr_list[rec];
287 if(l->display_list&(1<<r_info) ||
288 l->display_list&(1<<r_name) &&
289 (l->display_list&(1<<f_name) ||
290 l->display_list&(1<<f_value)))
292 printf(catgets(dtcatd, 1, 5, "=============== %s ===============\n"),
293 rec_ptr->recordName?(char *)_DtDtsMMBosonToString(rec_ptr->recordName):catgets(dtcatd, 1, 6, ""));
295 else if (l->display_list&(1<<r_name))
297 printf(catgets(dtcatd, 1, 7, "%s\n"),
298 rec_ptr->recordName?(char *)_DtDtsMMBosonToString(rec_ptr->recordName):catgets(dtcatd, 1, 8, ""));
300 if(l->display_list&(1<<r_info))
302 char *path = (char *)_DtDtsMMBosonToString(rec_ptr->pathId);
304 printf(catgets(dtcatd, 1, 9, "loaded from %s\n"), path?path:catgets(dtcatd, 1, 10, "Unknown"));
307 fld_ptr_list = _DtDtsMMGetPtr(rec_ptr->fieldList);
308 for(fld = 0; fld < rec_ptr->fieldCount; fld++)
314 fld_ptr = &fld_ptr_list[fld];
316 fn = _DtDtsMMExpandValue(_DtDtsMMBosonToString(fld_ptr->fieldName));
317 fv = _DtDtsMMExpandValue(_DtDtsMMBosonToString(fld_ptr->fieldValue));
321 #if defined(CSRG_BASED) || defined(__linux__)
322 if(regexec(®ex_df, fn, 0, NULL, 0) == 0)
324 if(advance(fn, expbuf_df) !=0)
327 printf(catgets(dtcatd, 1, 11, "\t%s"),
328 fn?fn:catgets(dtcatd, 1, 12, ""));
329 printf(catgets(dtcatd, 1, 13, " :\t%s"),
330 fld_ptr->fieldValue?fv:
331 catgets(dtcatd, 1, 14, ""));
332 printf("%s", catgets(dtcatd, 1, 15, "\n"));
338 if(l->display_list&(1<<f_name))
340 printf(catgets(dtcatd, 1, 11, "\t%s"), fld_ptr->fieldName?fn:catgets(dtcatd, 1, 12, ""));
343 if(l->display_list&(1<<f_value))
345 printf(catgets(dtcatd, 1, 13, " :\t%s"), fld_ptr->fieldValue?fv:catgets(dtcatd, 1, 14, ""));
348 if(term)printf("%s", catgets(dtcatd, 1, 15, "\n"));
350 _DtDtsMMSafeFree(fn);
351 _DtDtsMMSafeFree(fv);
357 parse_args(List *l, int argc, char **argv)
369 enum st state = none;
379 for(i = 1; i < argc; i++)
381 if(strcmp(argv[i], catgets(dtcatd, 1, 16, "-db")) == 0)
385 if(!_DtDtsMMGet(argv[i+1]))
389 l->db_name = argv[++i];
393 error |= 1<<NO_ARGUMENT;
396 else if(strcmp(argv[i], catgets(dtcatd, 1, 17, "-w")) == 0)
400 else if(strcmp(argv[i], catgets(dtcatd, 1, 18, "-l")) == 0)
404 else if(strcmp(argv[i], catgets(dtcatd, 1, 19, "rec_info")) == 0)
408 error |= 1<<REC_INFO_IN_STATE;
410 else if(state == list)
412 l->display_list|= 1<<r_info;
416 error |= 1<<NO_STATE;
419 else if(strcmp(argv[i], catgets(dtcatd, 1, 20, "rec_name")) == 0)
425 error |= 1<<NO_ARGUMENT;
429 l->rec_name = argv[++i];
432 else if(state == list)
434 l->display_list |= 1<<r_name;
438 error |= 1<<NO_STATE;
441 else if(strcmp(argv[i], catgets(dtcatd, 1, 21, "fld_name")) == 0)
447 error |= 1<<NO_ARGUMENT;
451 l->fld_name = argv[++i];
454 else if(state == list)
458 error |= 1<<NO_ARGUMENT;
462 l->display_list |= 1<<f_name;
463 if(strcmp(argv[i+1], catgets(dtcatd, 1, 22, "fld_value")) &&
464 strcmp(argv[i+1], catgets(dtcatd, 1, 21, "fld_name")) &&
465 strcmp(argv[i+1], catgets(dtcatd, 1, 20, "rec_name")) &&
466 strcmp(argv[i+1], catgets(dtcatd, 1, 19, "rec_info")) )
468 l->display_fld = argv[++i];
474 error |= 1<<NO_STATE;
477 else if(strcmp(argv[i], catgets(dtcatd, 1, 22, "fld_value")) == 0)
483 error |= 1<<NO_ARGUMENT;
487 l->fld_value = argv[++i];
490 else if(state == list)
492 l->display_list |= 1<<f_value;
496 error |= 1<<NO_STATE;
499 else if(strcmp(argv[i], catgets(dtcatd, 1, 23, "all")) == 0)
505 error |= 1<<NO_ARGUMENT;
509 l->fld_value = argv[++i];
512 else if(state == list)
514 l->display_list|= 1<<r_info;
515 l->display_list |= 1<<r_name;
516 l->display_list |= 1<<f_name;
517 l->display_list |= 1<<f_value;
521 error |= 1<<NO_STATE;
524 else if(strcmp(argv[i], catgets(dtcatd, 1, 26, "-type")) == 0)
530 error |= 1<<NO_ARGUMENT;
534 printf(catgets(dtcatd, 1, 27, "%s is of type %s\n"),
536 type = DtDtsFileToDataType(argv[i+1]));
537 new = (char *)malloc(strlen(type)+5);
539 strcat(new, catgets(dtcatd, 1, 28, "$"));
541 l->db_name = "DATA_ATTRIBUTES";
542 l->display_list|= 1<<r_info;
543 l->display_list |= 1<<r_name;
544 l->display_list |= 1<<f_name;
545 l->display_list |= 1<<f_value;
549 else if(strcmp(argv[i], catgets(dtcatd, 1, 35, "-help")) == 0)
555 error |= 1<<INVALID_ARG;
563 for(j = 0; j < XtNumber(error_str); j++)
565 error_str[j] = catgets(dtcatd, 1, 30+j, error_str[j]);
569 fprintf(stderr, catgets(dtcatd, 1, 24, "Arg = %s\n"), argv[i]);
570 for(j = 0; error; j++)
574 fprintf(stderr, "%s", error_str[j]);
575 error = error & ~(1<<j);
591 fprintf(stderr, "%s", catgets(dtcatd, 1, 25, "usage: dttypes [-help]\n\tdttypes [-type filename]\n\tdttypes [-db database] [-w [rec_name regexp] [fld_name regexp]\n\t\t\t[fld_value regexp]]\n\t\t[-l [rec_name] [rec_info] [fld_name regexp] [fld_value]]\n"));
596 main(int argc, char **argv)
611 enum st state = none;
613 locale = setlocale(LC_ALL, "");
618 _DtEnvControl(DT_ENV_SET);
620 dtcatd = catopen("dttypes", NL_CAT_LOCALE);
621 if(dtcatd == (nl_catd)-1)
626 memset(&l, '\0', sizeof(l));
628 if(!init(&argc, argv))
633 if(!parse_args(&l, argc, argv))
637 if(!(l.display_list&(1<<r_info) ||
638 l.display_list&(1<<f_name) ||
639 l.display_list&(1<<f_value) ||
640 l.display_list&(1<<r_name) ))
642 l.display_list |= 1<<r_name;
643 l.display_list |= 1<<r_info;
644 l.display_list |= 1<<f_name;
645 l.display_list |= 1<<f_value;
647 if(l.display_list&(1<<r_info)) l.display_list |= 1<<r_name;
648 if(l.display_list&(1<<f_name)) l.display_list |= 1<<r_name;
649 if(l.display_list&(1<<f_value)) l.display_list |= 1<<r_name;
650 if(l.display_list == 0) l.display_list |= 1<<r_name;
658 dbs = _DtsMMListDb();
659 for(i = 0; dbs[i]; i++)
661 printf(catgets(dtcatd, 1, 29, "--------------------- %s ----------------\n"),