2 ** fmtdump.c -- compile format file and dump out instructions
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.
10 #include <h/fmt_scan.h>
11 #include <h/fmt_compile.h>
12 #include <h/scansbr.h>
16 static struct swit switches[] = {
18 { "form formatfile", 0 },
27 static struct format *lvec[128];
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);
45 main(int argc, char **argv)
47 char *cp, *form = NULL;
48 char buf[BUFSIZ], *fmtstr, **argp, **arguments;
51 setlocale(LC_ALL, "");
52 invo_name = mhbasename(argv[0]);
54 /* read user profile/context */
57 arguments = getarguments(invo_name, argc, argv, 1);
60 while ((cp = *argp++)) {
62 switch (smatch(++cp, switches)) {
64 ambigsw(cp, switches);
67 adios(EX_USAGE, NULL, "-%s unknown", cp);
70 snprintf(buf, sizeof(buf), "%s [switches]",
72 print_help(buf, switches, 1);
73 exit(argc == 2 ? EX_OK : EX_USAGE);
75 print_version(invo_name);
76 exit(argc == 2 ? EX_OK : EX_USAGE);
79 if (!(form = *argp++) || *form == '-')
80 adios(EX_USAGE, NULL, "missing argument to %s",
87 adios(EX_USAGE, NULL, "only one form at a time!");
92 /* Set format string. Must be before chdir(). */
93 fmtstr = new_fs(form, scanformat);
94 fmt_compile(fmtstr, &fmt);
101 fmt_dump(struct format *fmth)
104 struct format *fmt, *addr;
107 for (fmt = fmth; fmt; ++fmt) {
109 if (i == FT_IF_S || i == FT_IF_S_NULL || i == FT_IF_V_EQ ||
110 i == FT_IF_V_NE || i == FT_IF_V_GT ||
111 i == FT_IF_MATCH || i == FT_IF_AMATCH ||
113 addr = fmt + fmt->f_skip;
114 if (findlabel(addr) < 0)
117 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
122 for (fmt = fmth; fmt; ++fmt) {
124 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
130 dumpone(struct format *fmt)
134 if ((i = findlabel(fmt)) >= 0)
138 fputs(f_typestr((int)fmt->f_type), stdout);
140 switch (fmt->f_type) {
147 litputs(fmt->f_comp->c_name);
148 if (fmt->f_comp->c_type)
149 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
150 if (fmt->f_comp->c_flags)
151 printf(", c_flags %s",
152 c_flagsstr(fmt->f_comp->c_flags));
180 litputs(fmt->f_comp->c_name);
181 if (fmt->f_comp->c_type)
182 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
183 if (fmt->f_comp->c_flags)
184 printf(", c_flags %s",
185 c_flagsstr(fmt->f_comp->c_flags));
203 litputs(fmt->f_comp->c_name);
204 if (fmt->f_comp->c_type)
205 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
206 if (fmt->f_comp->c_flags)
207 printf(", c_flags %s",
208 c_flagsstr(fmt->f_comp->c_flags));
212 printf(", width %d, fill '", fmt->f_width);
213 litputc(fmt->f_fill);
215 litputs(fmt->f_comp->c_name);
216 if (fmt->f_comp->c_type)
217 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
218 if (fmt->f_comp->c_flags)
219 printf(", c_flags %s",
220 c_flagsstr(fmt->f_comp->c_flags));
225 printf(", width %d, fill '", fmt->f_width);
226 litputc(fmt->f_fill);
235 litputs(fmt->f_text);
239 printf(", width %d, fill '", fmt->f_width);
240 litputc(fmt->f_fill);
242 litputs(fmt->f_text);
248 litputc(fmt->f_char);
257 printf(" continue else goto");
259 i = findlabel(fmt + fmt->f_skip);
266 i = findlabel(fmt + fmt->f_skip);
267 printf(" %d continue else goto L%d", fmt->f_value, i);
278 printf(" value %d", fmt->f_value);
283 litputs(fmt->f_text);
288 litputs(fmt->f_text);
291 case FT_LS_DECODECOMP:
293 litputs(fmt->f_comp->c_name);
300 printf(", width %d", fmt->f_width);
304 printf(", value dat[%d]", fmt->f_value);
311 findlabel(struct format *addr)
315 for (i = 0; i < lused; ++i)
322 assignlabel(struct format *addr)
324 lvec[lused++] = addr;
333 case FT_COMP: return("COMP");
334 case FT_COMPF: return("COMPF");
335 case FT_LIT: return("LIT");
336 case FT_LITF: return("LITF");
338 case FT_LIT_FORCE: return("LIT_FORCE");
340 case FT_CHAR: return("CHAR");
341 case FT_NUM: return("NUM");
342 case FT_NUMF: return("NUMF");
343 case FT_STR: return("STR");
344 case FT_STRF: return("STRF");
345 case FT_STRFW: return("STRFW");
346 case FT_PUTADDR: return("PUTADDR");
347 case FT_LS_COMP: return("LS_COMP");
348 case FT_LS_LIT: return("LS_LIT");
349 case FT_LS_GETENV: return("LS_GETENV");
350 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
351 case FT_LS_DECODE: return("FT_LS_DECODE");
352 case FT_LS_TRIM: return("LS_TRIM");
353 case FT_LV_COMP: return("LV_COMP");
354 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
355 case FT_LV_LIT: return("LV_LIT");
356 case FT_LV_DAT: return("LV_DAT");
357 case FT_LV_STRLEN: return("LV_STRLEN");
358 case FT_LV_PLUS_L: return("LV_PLUS_L");
359 case FT_LV_MINUS_L: return("LV_MINUS_L");
360 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
361 case FT_LV_MODULO_L: return("LV_MODULO_L");
362 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
363 case FT_LS_MONTH: return("LS_MONTH");
364 case FT_LS_LMONTH: return("LS_LMONTH");
365 case FT_LS_ZONE: return("LS_ZONE");
366 case FT_LS_DAY: return("LS_DAY");
367 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
368 case FT_LS_822DATE: return("LS_822DATE");
369 case FT_LS_PRETTY: return("LS_PRETTY");
370 case FT_LV_SEC: return("LV_SEC");
371 case FT_LV_MIN: return("LV_MIN");
372 case FT_LV_HOUR: return("LV_HOUR");
373 case FT_LV_MDAY: return("LV_MDAY");
374 case FT_LV_MON: return("LV_MON");
375 case FT_LV_YEAR: return("LV_YEAR");
376 case FT_LV_YDAY: return("LV_YDAY");
377 case FT_LV_WDAY: return("LV_WDAY");
378 case FT_LV_ZONE: return("LV_ZONE");
379 case FT_LV_CLOCK: return("LV_CLOCK");
380 case FT_LV_RCLOCK: return("LV_RCLOCK");
381 case FT_LV_DAYF: return("LV_DAYF");
382 case FT_LV_DST: return("LV_DST");
383 case FT_LV_ZONEF: return("LV_ZONEF");
384 case FT_LS_ADDR: return("LS_ADDR");
385 case FT_LS_PERS: return("LS_PERS");
386 case FT_LS_MBOX: return("LS_MBOX");
387 case FT_LS_HOST: return("LS_HOST");
388 case FT_LS_PATH: return("LS_PATH");
389 case FT_LS_GNAME: return("LS_GNAME");
390 case FT_LS_NOTE: return("LS_NOTE");
391 case FT_LS_822ADDR: return("LS_822ADDR");
392 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
393 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
394 case FT_LV_INGRPF: return("LV_INGRPF");
395 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
396 case FT_LOCALDATE: return("LOCALDATE");
397 case FT_GMTDATE: return("GMTDATE");
398 case FT_PARSEDATE: return("PARSEDATE");
399 case FT_PARSEADDR: return("PARSEADDR");
400 case FT_FORMATADDR: return("FORMATADDR");
401 case FT_MYMBOX: return("MYMBOX");
402 case FT_SAVESTR: return("SAVESTR");
404 case FT_PAUSE: return ("PAUSE");
406 case FT_DONE: return("DONE");
407 case FT_NOP: return("NOP");
408 case FT_GOTO: return("GOTO");
409 case FT_IF_S_NULL: return("IF_S_NULL");
410 case FT_IF_S: return("IF_S");
411 case FT_IF_V_EQ: return("IF_V_EQ");
412 case FT_IF_V_NE: return("IF_V_NE");
413 case FT_IF_V_GT: return("IF_V_GT");
414 case FT_IF_MATCH: return("IF_MATCH");
415 case FT_IF_AMATCH: return("IF_AMATCH");
416 case FT_S_NULL: return("S_NULL");
417 case FT_S_NONNULL: return("S_NONNULL");
418 case FT_V_EQ: return("V_EQ");
419 case FT_V_NE: return("V_NE");
420 case FT_V_GT: return("V_GT");
421 case FT_V_MATCH: return("V_MATCH");
422 case FT_V_AMATCH: return("V_AMATCH");
424 printf(buf, "/* ??? #%d */", t);
429 #define FNORD(v, s) if (t & (v)) { \
441 if (t & ~(CT_ADDR|CT_DATE))
442 printf(buf, "0x%x ", t);
445 FNORD(CT_ADDR, "ADDR");
446 FNORD(CT_DATE, "DATE");
458 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB))
459 printf(buf, "0x%x ", t);
462 FNORD(CF_TRUE, "TRUE");
463 FNORD(CF_PARSED, "PARSED");
464 FNORD(CF_DATEFAB, "DATEFAB");
479 fputs("<nil>", stdout);
490 if (c < 0x20 || c == 0177) {
494 } else if (c == '\f') {
497 } else if (c == '\n') {
500 } else if (c == '\r') {
503 } else if (c == '\t') {
508 putc(c ^ 0x40, stdout);
509 /* DEL to ?, others to alpha */