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