Remove RCS keywords, since they no longer work after git migration.
[mmh] / uip / fmtdump.c
1
2 /*
3  * fmtdump.c -- compile format file and dump out instructions
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 #include <h/mh.h>
11 #include <h/fmt_scan.h>
12 #include <h/fmt_compile.h>
13 #include <h/scansbr.h>
14
15 static struct swit switches[] = {
16 #define FORMSW              0
17     { "form formatfile", 0 },
18 #define FMTSW               1
19     { "format string", 5 },
20 #define VERSIONSW           2
21     { "version", 0 },
22 #define HELPSW              3
23     { "help", 0 },
24     { NULL, 0 }
25 };
26
27 /* for assignlabel */
28 static struct format *lvec[128];
29 static int lused = 0;
30
31 /*
32  * static prototypes
33  */
34 static void fmt_dump (struct format *);
35 static void dumpone(struct format *);
36 static int findlabel(struct format *);
37 static void assignlabel(struct format *);
38 static char *f_typestr(int);
39 static char *c_typestr(int);
40 static char *c_flagsstr(int);
41 static void litputs(char *);
42 static void litputc(char);
43
44
45 int
46 main (int argc, char **argv)
47 {
48     int ncomps;
49     char *cp, *form = NULL, *format = NULL;
50     char buf[BUFSIZ], *nfs, **argp, **arguments;
51     struct format *fmt;
52
53 #ifdef LOCALE
54     setlocale(LC_ALL, "");
55 #endif
56     invo_name = r1bindex (argv[0], '/');
57
58     /* read user profile/context */
59     context_read();
60
61     arguments = getarguments (invo_name, argc, argv, 1);
62     argp = arguments;
63
64     while ((cp = *argp++)) {
65         if (*cp == '-') {
66             switch (smatch (++cp, switches)) {
67                 case AMBIGSW: 
68                     ambigsw (cp, switches);
69                     done (1);
70                 case UNKWNSW: 
71                     adios (NULL, "-%s unknown", cp);
72
73                 case HELPSW: 
74                     snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
75                     print_help (buf, switches, 1);
76                     done (1);
77                 case VERSIONSW:
78                     print_version(invo_name);
79                     done (1);
80
81                 case FORMSW: 
82                     if (!(form = *argp++) || *form == '-')
83                         adios (NULL, "missing argument to %s", argp[-2]);
84                     format = NULL;
85                     continue;
86                 case FMTSW: 
87                     if (!(format = *argp++) || *format == '-')
88                         adios (NULL, "missing argument to %s", argp[-2]);
89                     form = NULL;
90                     continue;
91
92             }
93         }
94         if (form)
95             adios (NULL, "only one form at a time!");
96         else
97             form = cp;
98     }
99
100     /*
101      * Get new format string.  Must be before chdir().
102      */
103     nfs = new_fs (form, format, FORMAT);
104     ncomps = fmt_compile(nfs, &fmt);
105
106     fmt_dump(fmt);
107     done(0);
108     return 1;
109 }
110
111 static void
112 fmt_dump (struct format *fmth)
113 {
114         int i;
115         register struct format *fmt, *addr;
116
117         /* Assign labels */
118         for (fmt = fmth; fmt; ++fmt) {
119                 i = fmt->f_type;
120                 if (i == FT_IF_S ||
121                     i == FT_IF_S_NULL ||
122                     i == FT_IF_V_EQ ||
123                     i == FT_IF_V_NE ||
124                     i == FT_IF_V_GT ||
125                     i == FT_IF_MATCH ||
126                     i == FT_IF_AMATCH ||
127                     i == FT_GOTO) {
128                         addr = fmt + fmt->f_skip;
129                         if (findlabel(addr) < 0)
130                                 assignlabel(addr);
131                 }
132                 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
133                         break;
134         }
135
136         /* Dump them out! */
137         for (fmt = fmth; fmt; ++fmt) {
138                 dumpone(fmt);
139                 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
140                         break;
141         }
142 }
143
144 static void
145 dumpone(struct format *fmt)
146 {
147         register int i;
148
149         if ((i = findlabel(fmt)) >= 0)
150                 printf("L%d:", i);
151         putchar('\t');
152
153         fputs(f_typestr((int)fmt->f_type), stdout);
154
155         switch (fmt->f_type) {
156
157         case FT_COMP:
158         case FT_LS_COMP:
159         case FT_LV_COMPFLAG:
160         case FT_LV_COMP:
161                 printf(", comp ");
162                 litputs(fmt->f_comp->c_name);
163                 if (fmt->f_comp->c_type)
164                         printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
165                 if (fmt->f_comp->c_flags)
166                         printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
167                 break;
168
169         case FT_LV_SEC:
170         case FT_LV_MIN:
171         case FT_LV_HOUR:
172         case FT_LV_MDAY:
173         case FT_LV_MON:
174         case FT_LS_MONTH:
175         case FT_LS_LMONTH:
176         case FT_LS_ZONE:
177         case FT_LV_YEAR:
178         case FT_LV_WDAY:
179         case FT_LS_DAY:
180         case FT_LS_WEEKDAY:
181         case FT_LV_YDAY:
182         case FT_LV_ZONE:
183         case FT_LV_CLOCK:
184         case FT_LV_RCLOCK:
185         case FT_LV_DAYF:
186         case FT_LV_ZONEF:
187         case FT_LV_DST:
188         case FT_LS_822DATE:
189         case FT_LS_PRETTY:
190         case FT_LOCALDATE:
191         case FT_GMTDATE:
192         case FT_PARSEDATE:
193                 printf(", c_name ");
194                 litputs(fmt->f_comp->c_name);
195                 if (fmt->f_comp->c_type)
196                         printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
197                 if (fmt->f_comp->c_flags)
198                         printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
199                 break;
200
201         case FT_LS_ADDR:
202         case FT_LS_PERS:
203         case FT_LS_MBOX:
204         case FT_LS_HOST:
205         case FT_LS_PATH:
206         case FT_LS_GNAME:
207         case FT_LS_NOTE:
208         case FT_LS_822ADDR:
209         case FT_LV_HOSTTYPE:
210         case FT_LV_INGRPF:
211         case FT_LV_NOHOSTF:
212         case FT_LS_FRIENDLY:
213         case FT_PARSEADDR:
214         case FT_MYMBOX:
215                 printf(", c_name ");
216                 litputs(fmt->f_comp->c_name);
217                 if (fmt->f_comp->c_type)
218                         printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
219                 if (fmt->f_comp->c_flags)
220                         printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
221                 break;
222
223         case FT_COMPF:
224                 printf(", width %d, fill '", fmt->f_width);
225                 litputc(fmt->f_fill);
226                 printf("' name ");
227                 litputs(fmt->f_comp->c_name);
228                 if (fmt->f_comp->c_type)
229                         printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
230                 if (fmt->f_comp->c_flags)
231                         printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
232                 break;
233
234         case FT_STRF:
235         case FT_NUMF:
236                 printf(", width %d, fill '", fmt->f_width);
237                 litputc(fmt->f_fill);
238                 putchar('\'');
239                 break;
240
241         case FT_LIT:
242 #ifdef FT_LIT_FORCE
243         case FT_LIT_FORCE:
244 #endif
245                 putchar(' ');
246                 litputs(fmt->f_text);
247                 break;
248
249         case FT_LITF:
250                 printf(", width %d, fill '", fmt->f_width);
251                 litputc(fmt->f_fill);
252                 printf("' ");
253                 litputs(fmt->f_text);
254                 break;
255
256         case FT_CHAR:
257                 putchar(' ');
258                 putchar('\'');
259                 litputc(fmt->f_char);
260                 putchar('\'');
261                 break;
262
263
264         case FT_IF_S:
265         case FT_IF_S_NULL:
266         case FT_IF_MATCH:
267         case FT_IF_AMATCH:
268                 printf(" continue else goto");
269         case FT_GOTO:
270                 i = findlabel(fmt + fmt->f_skip);
271                 printf(" L%d", i);
272                 break;
273
274         case FT_IF_V_EQ:
275         case FT_IF_V_NE:
276         case FT_IF_V_GT:
277                 i = findlabel(fmt + fmt->f_skip);
278                 printf(" %d continue else goto L%d", fmt->f_value, i);
279                 break;
280
281         case FT_V_EQ:
282         case FT_V_NE:
283         case FT_V_GT:
284         case FT_LV_LIT:
285         case FT_LV_PLUS_L:
286         case FT_LV_MINUS_L:
287         case FT_LV_DIVIDE_L:
288         case FT_LV_MODULO_L:
289                 printf(" value %d", fmt->f_value);
290                 break;
291
292         case FT_LS_LIT:
293                 printf(" str ");
294                 litputs(fmt->f_text);
295                 break;
296
297         case FT_LS_GETENV:
298                 printf(" getenv ");
299                 litputs(fmt->f_text);
300                 break;
301
302         case FT_LS_DECODECOMP:
303                 printf(", comp ");
304                 litputs(fmt->f_comp->c_name);
305                 break;
306
307         case FT_LS_DECODE:
308                 break;
309
310         case FT_LS_TRIM:
311                 printf(", width %d", fmt->f_width);
312                 break;
313
314         case FT_LV_DAT:
315                 printf(", value dat[%d]", fmt->f_value);
316                 break;
317         }
318         putchar('\n');
319 }
320
321 static int
322 findlabel(struct format *addr)
323 {
324         register int i;
325
326         for (i = 0; i < lused; ++i)
327                 if (addr == lvec[i])
328                         return(i);
329         return(-1);
330 }
331
332 static void
333 assignlabel(struct format *addr)
334 {
335         lvec[lused++] = addr;
336 }
337
338 static char *
339 f_typestr(int t)
340 {
341         static char buf[32];
342
343         switch (t) {
344         case FT_COMP: return("COMP");
345         case FT_COMPF: return("COMPF");
346         case FT_LIT: return("LIT");
347         case FT_LITF: return("LITF");
348 #ifdef  FT_LIT_FORCE
349         case FT_LIT_FORCE: return("LIT_FORCE");
350 #endif
351         case FT_CHAR: return("CHAR");
352         case FT_NUM: return("NUM");
353         case FT_NUMF: return("NUMF");
354         case FT_STR: return("STR");
355         case FT_STRF: return("STRF");
356         case FT_STRFW: return("STRFW");
357         case FT_PUTADDR: return("PUTADDR");
358         case FT_LS_COMP: return("LS_COMP");
359         case FT_LS_LIT: return("LS_LIT");
360         case FT_LS_GETENV: return("LS_GETENV");
361         case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
362         case FT_LS_DECODE: return("FT_LS_DECODE");
363         case FT_LS_TRIM: return("LS_TRIM");
364         case FT_LV_COMP: return("LV_COMP");
365         case FT_LV_COMPFLAG: return("LV_COMPFLAG");
366         case FT_LV_LIT: return("LV_LIT");
367         case FT_LV_DAT: return("LV_DAT");
368         case FT_LV_STRLEN: return("LV_STRLEN");
369         case FT_LV_PLUS_L: return("LV_PLUS_L");
370         case FT_LV_MINUS_L: return("LV_MINUS_L");
371         case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
372         case FT_LV_MODULO_L: return("LV_MODULO_L");
373         case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
374         case FT_LS_MONTH: return("LS_MONTH");
375         case FT_LS_LMONTH: return("LS_LMONTH");
376         case FT_LS_ZONE: return("LS_ZONE");
377         case FT_LS_DAY: return("LS_DAY");
378         case FT_LS_WEEKDAY: return("LS_WEEKDAY");
379         case FT_LS_822DATE: return("LS_822DATE");
380         case FT_LS_PRETTY: return("LS_PRETTY");
381         case FT_LV_SEC: return("LV_SEC");
382         case FT_LV_MIN: return("LV_MIN");
383         case FT_LV_HOUR: return("LV_HOUR");
384         case FT_LV_MDAY: return("LV_MDAY");
385         case FT_LV_MON: return("LV_MON");
386         case FT_LV_YEAR: return("LV_YEAR");
387         case FT_LV_YDAY: return("LV_YDAY");
388         case FT_LV_WDAY: return("LV_WDAY");
389         case FT_LV_ZONE: return("LV_ZONE");
390         case FT_LV_CLOCK: return("LV_CLOCK");
391         case FT_LV_RCLOCK: return("LV_RCLOCK");
392         case FT_LV_DAYF: return("LV_DAYF");
393         case FT_LV_DST: return("LV_DST");
394         case FT_LV_ZONEF: return("LV_ZONEF");
395         case FT_LS_ADDR: return("LS_ADDR");
396         case FT_LS_PERS: return("LS_PERS");
397         case FT_LS_MBOX: return("LS_MBOX");
398         case FT_LS_HOST: return("LS_HOST");
399         case FT_LS_PATH: return("LS_PATH");
400         case FT_LS_GNAME: return("LS_GNAME");
401         case FT_LS_NOTE: return("LS_NOTE");
402         case FT_LS_822ADDR: return("LS_822ADDR");
403         case FT_LS_FRIENDLY: return("LS_FRIENDLY");
404         case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
405         case FT_LV_INGRPF: return("LV_INGRPF");
406         case FT_LV_NOHOSTF: return("LV_NOHOSTF");
407         case FT_LOCALDATE: return("LOCALDATE");
408         case FT_GMTDATE: return("GMTDATE");
409         case FT_PARSEDATE: return("PARSEDATE");
410         case FT_PARSEADDR: return("PARSEADDR");
411         case FT_FORMATADDR: return("FORMATADDR");
412         case FT_MYMBOX: return("MYMBOX");
413 #ifdef  FT_ADDTOSEQ
414         case FT_ADDTOSEQ: return("ADDTOSEQ");
415 #endif
416         case FT_SAVESTR: return("SAVESTR");
417 #ifdef  FT_PAUSE
418         case FT_PAUSE: return ("PAUSE");
419 #endif
420         case FT_DONE: return("DONE");
421         case FT_NOP: return("NOP");
422         case FT_GOTO: return("GOTO");
423         case FT_IF_S_NULL: return("IF_S_NULL");
424         case FT_IF_S: return("IF_S");
425         case FT_IF_V_EQ: return("IF_V_EQ");
426         case FT_IF_V_NE: return("IF_V_NE");
427         case FT_IF_V_GT: return("IF_V_GT");
428         case FT_IF_MATCH: return("IF_MATCH");
429         case FT_IF_AMATCH: return("IF_AMATCH");
430         case FT_S_NULL: return("S_NULL");
431         case FT_S_NONNULL: return("S_NONNULL");
432         case FT_V_EQ: return("V_EQ");
433         case FT_V_NE: return("V_NE");
434         case FT_V_GT: return("V_GT");
435         case FT_V_MATCH: return("V_MATCH");
436         case FT_V_AMATCH: return("V_AMATCH");
437         default:
438                 printf(buf, "/* ??? #%d */", t);
439                 return(buf);
440         }
441 }
442
443 #define FNORD(v, s) if (t & (v)) { \
444         if (i++ > 0) \
445                 strcat(buf, "|"); \
446         strcat(buf, s); }
447
448 static char *
449 c_typestr(int t)
450 {
451         register int i;
452         static char buf[64];
453
454         buf[0] = '\0';
455         if (t & ~(CT_ADDR|CT_DATE))
456                 printf(buf, "0x%x ", t);
457         strcat(buf, "<");
458         i = 0;
459         FNORD(CT_ADDR, "ADDR");
460         FNORD(CT_DATE, "DATE");
461         strcat(buf, ">");
462         return(buf);
463 }
464
465 static char *
466 c_flagsstr(int t)
467 {
468         register int i;
469         static char buf[64];
470
471         buf[0] = '\0';
472         if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB))
473                 printf(buf, "0x%x ", t);
474         strcat(buf, "<");
475         i = 0;
476         FNORD(CF_TRUE, "TRUE");
477         FNORD(CF_PARSED, "PARSED");
478         FNORD(CF_DATEFAB, "DATEFAB");
479         strcat(buf, ">");
480         return(buf);
481 }
482 #undef FNORD
483
484 static void
485 litputs(char *s)
486 {
487         if (s) {
488                 putc('"', stdout);
489                 while (*s)
490                         litputc(*s++);
491                 putc('"', stdout);
492         } else
493                 fputs("<nil>", stdout);
494 }
495
496 static void
497 litputc(char c)
498 {
499         if (c & ~ 0177) {
500                 putc('M', stdout);
501                 putc('-', stdout);
502                 c &= 0177;
503         }
504         if (c < 0x20 || c == 0177) {
505                 if (c == '\b') {
506                         putc('\\', stdout);
507                         putc('b', stdout);
508                 } else if (c == '\f') {
509                         putc('\\', stdout);
510                         putc('f', stdout);
511                 } else if (c == '\n') {
512                         putc('\\', stdout);
513                         putc('n', stdout);
514                 } else if (c == '\r') {
515                         putc('\\', stdout);
516                         putc('r', stdout);
517                 } else if (c == '\t') {
518                         putc('\\', stdout);
519                         putc('t', stdout);
520                 } else {
521                         putc('^', stdout);
522                         putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
523                 }
524         } else
525                 putc(c, stdout);
526 }