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