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