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