util/dttypes: remove register keyword
[oweals/cde.git] / cde / programs / util / dttypes / dttypes.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $TOG: dttypes.c /main/6 1998/04/20 13:01:30 mgreess $ */
24 /*                                                                      *
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.                                *
29  */
30 #include        <sys/types.h>
31 #include        <locale.h>
32 #include        <stdlib.h>
33 #include        <unistd.h>
34 #include        <stdio.h>
35 #include        <Xm/Xm.h>
36 #include        <X11/Shell.h>
37 #define INIT          char *sp = instring;
38 #define GETC()        (*sp++)
39 #define PEEKC()       (*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__)
45 #include        <regex.h>
46 #else
47 #include        <regexp.h>
48 #endif
49 #include        <nl_types.h>
50 #include        <Dt/Dt.h>
51 #include        <Dt/DtsMM.h>
52 #include        <Dt/Dts.h>
53 #include        <Dt/EnvControlP.h>
54
55 #if !defined(NL_CAT_LOCALE)
56 #define NL_CAT_LOCALE  0
57 #endif 
58
59 extern  char ** _DtsMMListDb(void);
60 static  enum    errors
61 {
62         BAD_DB,
63         NO_ARGUMENT,
64         REC_INFO_IN_STATE,
65         NO_STATE,
66         INVALID_ARG
67 } MyErrors;
68
69 static  int     read_errors = 0;
70 static  char    *error_str[] =
71 {
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 */
77 };
78
79 static  enum
80 {
81         r_info,
82         r_name,
83         f_name,
84         f_value
85 } list_shift;
86
87 typedef struct
88 {
89         int     display_list;
90         int     *rec_list;
91         int     rec_count;
92         int     rec_total;
93         char    *db_name;
94         char    *rec_name;
95         char    *fld_name;
96         char    *fld_value;
97         char    *display_fld;
98 } List;
99
100 static  nl_catd dtcatd = 0;
101
102 static int
103 init(int *argc, char **argv)
104 {
105         Widget          toplevel;
106         XtAppContext    appContext;
107         Arg             args[20];
108
109
110         toplevel = XtInitialize(argv[0], "Dttype", NULL, 0,
111                 (int *) argc, argv);
112
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);
119
120         if (DtInitialize(XtDisplay(toplevel), toplevel, argv[0],
121                           "Dttype") == False)
122         {
123                 fprintf(stderr, "couldn't initialize everything\n");
124                 return(0);
125         }
126
127         /* Load the filetype/action dbs; DtActionInvoke() requires this */
128         _DtDtsMMInit(0);
129         return(1);
130 }
131
132 void
133 add_rec(int rec, List *l)
134 {
135         int     i;
136
137         if(l->rec_total == 0 || l->rec_count >= l->rec_total-2)
138         {
139                 l->rec_total += 10;
140                 l->rec_list = (int *)realloc(l->rec_list, (l->rec_total)*sizeof(int));
141         }
142         l->rec_list[l->rec_count] = rec;
143         l->rec_count++;
144         l->rec_list[l->rec_count] = -1;
145 }
146
147 void
148 rec_list(List *l)
149 {
150         int             i;
151         int             db;
152         int             rec;
153         int             fld;
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;
160
161 #if defined(CSRG_BASED) || defined(__linux__)
162         regex_t         regex_rn;
163         regex_t         regex_fn;
164         regex_t         regex_fv;
165         regex_t         regex_df;
166 #else
167         char            expbuf_rn[2000];
168         char            expbuf_fn[2000];
169         char            expbuf_fv[2000];
170         char            expbuf_df[2000];
171
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));
176 #endif
177
178 #if defined(CSRG_BASED) || defined(__linux__)
179         if(regcomp(&regex_rn, l->rec_name?l->rec_name:"^.*", 0) != 0)
180 #else
181         if((compile(l->rec_name?l->rec_name:"^.*",
182                         expbuf_rn,
183                         &expbuf_rn[sizeof(expbuf_rn)],
184                         0)) == (char *)0)
185 #endif
186         {
187                 /* error */
188                 fprintf(stderr, catgets(dtcatd, 1, 36, "error in regular expression %s\n"), l->rec_name?l->rec_name:"(NULL)");
189                 exit(1);
190         }
191
192 #if defined(CSRG_BASED) || defined(__linux__)
193         if(regcomp(&regex_fn, l->fld_name?l->fld_name:"^.*", 0) != 0)
194 #else
195         if((compile(l->fld_name?l->fld_name:"^.*",
196                         expbuf_fn,
197                         &expbuf_fn[sizeof(expbuf_fn)],
198                         0)) == (char *)0)
199 #endif
200         {
201                 /* error */
202                 fprintf(stderr, catgets(dtcatd, 1, 36, "error in regular expression %s\n"), l->fld_name?l->fld_name:"(NULL)");
203                 exit(1);
204         }
205
206 #if defined(CSRG_BASED) || defined(__linux__)
207         if(regcomp(&regex_fv, l->fld_value?l->fld_value:"^.*", 0) != 0)
208 #else
209         if((compile(l->fld_value?l->fld_value:"^.*",
210                         expbuf_fv,
211                         &expbuf_fv[sizeof(expbuf_fv)],
212                         0)) == (char *)0)
213 #endif
214         {
215                 /* error */
216                 fprintf(stderr, catgets(dtcatd, 1, 36, "error in regular expression %s\n"), l->fld_value?l->fld_value:"(NULL)");
217                 exit(1);
218         }
219
220 #if defined(CSRG_BASED) || defined(__linux__)
221         if(regcomp(&regex_df, l->display_fld?l->display_fld:"^.*", 0) != 0)
222 #else
223         if((compile(l->display_fld?l->display_fld:"^.*",
224                         expbuf_df,
225                         &expbuf_df[sizeof(expbuf_df)],
226                         0)) == (char *)0)
227 #endif
228         {
229                 /* error */
230                 fprintf(stderr, catgets(dtcatd, 1, 36, "error in regular expression %s\n"), l->display_fld?l->display_fld:"(NULL)");
231                 exit(1);
232         }
233
234         db_ptr = _DtDtsMMGet(l->db_name);
235         rec_ptr_list = _DtDtsMMGetPtr(db_ptr->recordList);
236         for(rec = 0; rec < db_ptr->recordCount; rec++)
237         {
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(&regex_rn,
242                           (char *)_DtDtsMMBosonToString(rec_ptr->recordName),
243                           0, NULL, 0) == 0)
244 #else
245                 if(advance((char *)_DtDtsMMBosonToString(rec_ptr->recordName), expbuf_rn) != 0)
246 #endif
247                 {
248                         for(fld = 0; fld < rec_ptr->fieldCount; fld++)
249                         {
250                                 char *fn;
251                                 char *fv;
252
253                                 fld_ptr = &fld_ptr_list[fld];
254
255                                 fn = _DtDtsMMExpandValue(_DtDtsMMBosonToString(fld_ptr->fieldName));
256                                 fv = _DtDtsMMExpandValue(_DtDtsMMBosonToString(fld_ptr->fieldValue));
257
258 #if defined(CSRG_BASED) || defined(__linux__)
259                                 if((regexec(&regex_fn, fn, 0, NULL, 0) == 0) &&
260                                    ((fld_ptr->fieldValue==0?
261                                         regexec(&regex_fv,
262                                                 catgets(dtcatd, 1, 4, "NULL"),
263                                                 0, NULL, 0):
264                                         regexec(&regex_fv,
265                                                 fv,
266                                                 0, NULL, 0)) == 0))
267 #else
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))
270 #endif
271                                 {
272                                         add_rec(rec, l);
273                                         _DtDtsMMSafeFree(fn);
274                                         _DtDtsMMSafeFree(fv);
275                                         break;
276                                 }
277                                 _DtDtsMMSafeFree(fn);
278                                 _DtDtsMMSafeFree(fv);
279                         }
280                 }
281         }
282
283         for(i = 0; l->rec_list && l->rec_list[i] != -1; i++)
284         {
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)))
291                 {
292                         printf(catgets(dtcatd, 1, 5, "=============== %s ===============\n"), 
293                                 rec_ptr->recordName?(char *)_DtDtsMMBosonToString(rec_ptr->recordName):catgets(dtcatd, 1, 6, ""));
294                 }
295                 else if (l->display_list&(1<<r_name))
296                 {
297                         printf(catgets(dtcatd, 1, 7, "%s\n"), 
298                                 rec_ptr->recordName?(char *)_DtDtsMMBosonToString(rec_ptr->recordName):catgets(dtcatd, 1, 8, ""));
299                 }
300                 if(l->display_list&(1<<r_info))
301                 {
302                         char    *path = (char *)_DtDtsMMBosonToString(rec_ptr->pathId);
303
304                         printf(catgets(dtcatd, 1, 9, "loaded from %s\n"), path?path:catgets(dtcatd, 1, 10, "Unknown"));
305                 }
306
307                 fld_ptr_list = _DtDtsMMGetPtr(rec_ptr->fieldList);
308                 for(fld = 0; fld < rec_ptr->fieldCount; fld++)
309                 {
310                         int     term = 0;
311                         char *fn;
312                         char *fv;
313
314                         fld_ptr = &fld_ptr_list[fld];
315
316                         fn = _DtDtsMMExpandValue(_DtDtsMMBosonToString(fld_ptr->fieldName));
317                         fv = _DtDtsMMExpandValue(_DtDtsMMBosonToString(fld_ptr->fieldValue));
318
319                         if(l->display_fld)
320                         {
321 #if defined(CSRG_BASED) || defined(__linux__)
322                                 if(regexec(&regex_df, fn, 0, NULL, 0) == 0)
323 #else
324                                 if(advance(fn, expbuf_df) !=0)
325 #endif
326                                 {
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"));
333                                 }
334
335                         }
336                         else
337                         {
338                                 if(l->display_list&(1<<f_name))
339                                 {
340                                         printf(catgets(dtcatd, 1, 11, "\t%s"), fld_ptr->fieldName?fn:catgets(dtcatd, 1, 12, ""));
341                                         term++;
342                                 }
343                                 if(l->display_list&(1<<f_value))
344                                 {
345                                         printf(catgets(dtcatd, 1, 13, " :\t%s"), fld_ptr->fieldValue?fv:catgets(dtcatd, 1, 14, ""));
346                                         term++;
347                                 }
348                                 if(term)printf("%s", catgets(dtcatd, 1, 15, "\n"));
349                         }
350                         _DtDtsMMSafeFree(fn);
351                         _DtDtsMMSafeFree(fv);
352                 }
353         }
354 }
355
356 int
357 parse_args(List *l, int argc, char **argv)
358 {
359         int     *rl;
360         int     df = 0;
361         int     i;
362         int     error = 0;
363         enum    st
364         {
365                 none,
366                 where,
367                 list
368         };
369         enum    st      state = none;
370
371         l->display_list = 0;
372         l->rec_list = 0;
373         l->rec_count = 0;
374         l->db_name = 0;
375         l->rec_name = 0;
376         l->fld_name = 0;
377         l->fld_value = 0;
378
379         for(i = 1; i < argc; i++)
380         {
381                 if(strcmp(argv[i], catgets(dtcatd, 1, 16, "-db")) == 0)
382                 {
383                         if(argv[i+1])
384                         {
385                                 if(!_DtDtsMMGet(argv[i+1]))
386                                 {
387                                         error |= 1<<BAD_DB;
388                                 }
389                                 l->db_name = argv[++i];
390                         }
391                         else
392                         {
393                                 error |= 1<<NO_ARGUMENT;
394                         }
395                 }
396                 else if(strcmp(argv[i], catgets(dtcatd, 1, 17, "-w")) == 0)
397                 {
398                         state = where;
399                 }
400                 else if(strcmp(argv[i], catgets(dtcatd, 1, 18, "-l")) == 0)
401                 {
402                         state = list;
403                 }
404                 else if(strcmp(argv[i], catgets(dtcatd, 1, 19, "rec_info")) == 0)
405                 {
406                         if(state == where)
407                         {
408                                 error |= 1<<REC_INFO_IN_STATE;
409                         }
410                         else if(state == list)
411                         {
412                                 l->display_list|= 1<<r_info;
413                         }
414                         else
415                         {
416                                 error |= 1<<NO_STATE;
417                         }
418                 }
419                 else if(strcmp(argv[i], catgets(dtcatd, 1, 20, "rec_name")) == 0)
420                 {
421                         if(state == where)
422                         {
423                                 if(!argv[i+1])
424                                 {
425                                         error |= 1<<NO_ARGUMENT;
426                                 }
427                                 else
428                                 {
429                                         l->rec_name = argv[++i];
430                                 }
431                         }
432                         else if(state == list)
433                         {
434                                 l->display_list |= 1<<r_name;
435                         }
436                         else
437                         {
438                                 error |= 1<<NO_STATE;
439                         }
440                 }
441                 else if(strcmp(argv[i], catgets(dtcatd, 1, 21, "fld_name")) == 0)
442                 {
443                         if(state == where)
444                         {
445                                 if(!argv[i+1])
446                                 {
447                                         error |= 1<<NO_ARGUMENT;
448                                 }
449                                 else
450                                 {
451                                         l->fld_name = argv[++i];
452                                 }
453                         }
454                         else if(state == list)
455                         {
456                                 if(!argv[i+1])
457                                 {
458                                         error |= 1<<NO_ARGUMENT;
459                                 }
460                                 else
461                                 {
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"))  )
467                                         {
468                                                 l->display_fld = argv[++i];
469                                         }
470                                 }
471                         }
472                         else
473                         {
474                                 error |= 1<<NO_STATE;
475                         }
476                 }
477                 else if(strcmp(argv[i], catgets(dtcatd, 1, 22, "fld_value")) == 0)
478                 {
479                         if(state == where)
480                         {
481                                 if(!argv[i+1])
482                                 {
483                                         error |= 1<<NO_ARGUMENT;
484                                 }
485                                 else
486                                 {
487                                         l->fld_value = argv[++i];
488                                 }
489                         }
490                         else if(state == list)
491                         {
492                                 l->display_list |= 1<<f_value;
493                         }
494                         else
495                         {
496                                 error |= 1<<NO_STATE;
497                         }
498                 }
499                 else if(strcmp(argv[i], catgets(dtcatd, 1, 23, "all")) == 0)
500                 {
501                         if(state == where)
502                         {
503                                 if(!argv[i+1])
504                                 {
505                                         error |= 1<<NO_ARGUMENT;
506                                 }
507                                 else
508                                 {
509                                         l->fld_value = argv[++i];
510                                 }
511                         }
512                         else if(state == list)
513                         {
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;
518                         }
519                         else
520                         {
521                                 error |= 1<<NO_STATE;
522                         }
523                 }
524                 else if(strcmp(argv[i], catgets(dtcatd, 1, 26, "-type")) == 0)
525                 {
526                         char    *type, *new;
527
528                         if(!argv[i+1])
529                         {
530                                 error |= 1<<NO_ARGUMENT;
531                         }
532                         else
533                         {
534                                 printf(catgets(dtcatd, 1, 27, "%s is of type %s\n"),
535                                         argv[i+1],
536                                         type = DtDtsFileToDataType(argv[i+1]));
537                                 new = (char *)malloc(strlen(type)+5);
538                                 strcpy(new, type);
539                                 strcat(new, catgets(dtcatd, 1, 28, "$"));
540                                 l->rec_name = new;
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;
546                         }
547                         i++;
548                 }
549                 else if(strcmp(argv[i], catgets(dtcatd, 1, 35, "-help")) == 0)
550                 {
551                         usage();
552                 }
553                 else
554                 {
555                         error |= 1<<INVALID_ARG;
556                 }
557                 if(error)
558                 {
559                         int     j;
560
561                         if(!read_errors)
562                         {
563                                 for(j = 0; j < XtNumber(error_str); j++)
564                                 {
565                                         error_str[j] = catgets(dtcatd, 1, 30+j, error_str[j]);
566                                 }
567                                 read_errors = ~0;
568                         } 
569                         fprintf(stderr, catgets(dtcatd, 1, 24, "Arg = %s\n"), argv[i]);
570                         for(j = 0; error; j++)
571                         {
572                                 if(error & 1<<j)
573                                 {
574                                         fprintf(stderr, "%s", error_str[j]);
575                                         error = error & ~(1<<j);
576                                 }
577                         }
578                         usage();
579                         break;
580                 }
581         }
582         if(error)
583         {
584                 return(0);
585         }
586         return(1);
587 }
588
589 int usage(void)
590 {
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"));
592         exit(1);
593 }
594
595 int
596 main(int argc, char **argv)
597 {
598         List    l;
599         char    **dbs;
600         int     *rl;
601         int     df = 0;
602         int     i;
603         int     error = 0;
604         char    *locale;
605         enum    st
606         {
607                 none,
608                 where,
609                 list
610         };
611         enum    st      state = none;
612
613         locale = setlocale(LC_ALL, "");
614         if(!locale)
615         {
616                 perror("setlocale");
617         }
618         _DtEnvControl(DT_ENV_SET);
619
620         dtcatd = catopen("dttypes", NL_CAT_LOCALE);
621         if(dtcatd == (nl_catd)-1)
622         {
623                 perror("catopen");
624         }
625
626         memset(&l, '\0', sizeof(l));
627
628         if(!init(&argc, argv))
629         {
630                 exit(1);
631         }
632
633         if(!parse_args(&l, argc, argv))
634         {
635                 exit(1);
636         }
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)  ))
641         {
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;
646         }
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;
651
652         if(l.db_name)
653         {
654                 rec_list(&l);
655         }
656         else
657         {
658                 dbs = _DtsMMListDb();
659                 for(i = 0; dbs[i]; i++)
660                 {
661                         printf(catgets(dtcatd, 1, 29, "--------------------- %s ----------------\n"),
662                                 dbs[i]);
663                         l.db_name = dbs[i];
664                         rec_list(&l);
665                         free(l.rec_list);
666                         l.rec_list = 0;
667                         l.rec_count = 0;
668                         l.rec_total = 0;
669                 }
670         }
671
672
673         return(0);
674 }