3 * fmtdump.c -- compile format file and dump out instructions
9 #include <h/fmt_scan.h>
10 #include <h/fmt_compile.h>
11 #include <h/scansbr.h>
13 static struct swit switches[] = {
15 { "form formatfile", 0 },
17 { "format string", 5 },
26 static struct format *lvec[128];
32 static void fmt_dump (struct format *);
33 static void dumpone(struct format *);
34 static int findlabel(struct format *);
35 static void assignlabel(struct format *);
36 static char *f_typestr(int);
37 static char *c_typestr(int);
38 static void litputs(char *);
39 static void litputc(char);
43 main (int argc, char **argv)
46 char *cp, *form = NULL, *format = NULL;
47 char buf[BUFSIZ], *nfs, **argp, **arguments;
51 setlocale(LC_ALL, "");
53 invo_name = r1bindex (argv[0], '/');
55 /* read user profile/context */
58 arguments = getarguments (invo_name, argc, argv, 1);
61 while ((cp = *argp++)) {
63 switch (smatch (++cp, switches)) {
65 ambigsw (cp, switches);
68 adios (NULL, "-%s unknown", cp);
71 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
72 print_help (buf, switches, 1);
75 print_version(invo_name);
79 if (!(form = *argp++) || *form == '-')
80 adios (NULL, "missing argument to %s", argp[-2]);
84 if (!(format = *argp++) || *format == '-')
85 adios (NULL, "missing argument to %s", argp[-2]);
92 adios (NULL, "only one form at a time!");
98 * Get new format string. Must be before chdir().
100 nfs = new_fs (form, format, FORMAT);
101 ncomps = fmt_compile(nfs, &fmt);
108 fmt_dump (struct format *fmth)
111 register struct format *fmt, *addr;
114 for (fmt = fmth; fmt; ++fmt) {
124 addr = fmt + fmt->f_skip;
125 if (findlabel(addr) < 0)
128 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
133 for (fmt = fmth; fmt; ++fmt) {
135 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
141 dumpone(struct format *fmt)
145 if ((i = findlabel(fmt)) >= 0)
149 fputs(f_typestr((int)fmt->f_type), stdout);
151 switch (fmt->f_type) {
158 litputs(fmt->f_comp->c_name);
159 if (fmt->f_comp->c_type)
160 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
161 if (fmt->f_comp->c_flags)
162 printf(", c_flags %d", fmt->f_comp->c_flags);
190 litputs(fmt->f_comp->c_name);
191 if (fmt->f_comp->c_type)
192 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
193 if (fmt->f_comp->c_flags)
194 printf(", c_flags %d", fmt->f_comp->c_flags);
212 litputs(fmt->f_comp->c_name);
213 if (fmt->f_comp->c_type)
214 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
215 if (fmt->f_comp->c_flags)
216 printf(", c_flags %d", fmt->f_comp->c_flags);
220 printf(", width %d, fill '", fmt->f_width);
221 litputc(fmt->f_fill);
223 litputs(fmt->f_comp->c_name);
224 if (fmt->f_comp->c_type)
225 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
226 if (fmt->f_comp->c_flags)
227 printf(", c_flags %d", fmt->f_comp->c_flags);
232 printf(", width %d, fill '", fmt->f_width);
233 litputc(fmt->f_fill);
242 litputs(fmt->f_text);
246 printf(", width %d, fill '", fmt->f_width);
247 litputc(fmt->f_fill);
249 litputs(fmt->f_text);
255 litputc(fmt->f_char);
264 printf(" continue else goto");
266 i = findlabel(fmt + fmt->f_skip);
273 i = findlabel(fmt + fmt->f_skip);
274 printf(" %d continue else goto L%d", fmt->f_value, i);
285 printf(" value %d", fmt->f_value);
290 litputs(fmt->f_text);
295 litputs(fmt->f_text);
298 case FT_LS_DECODECOMP:
300 litputs(fmt->f_comp->c_name);
307 printf(", width %d", fmt->f_width);
311 printf(", value dat[%d]", fmt->f_value);
318 findlabel(struct format *addr)
322 for (i = 0; i < lused; ++i)
329 assignlabel(struct format *addr)
331 lvec[lused++] = addr;
340 case FT_COMP: return("COMP");
341 case FT_COMPF: return("COMPF");
342 case FT_LIT: return("LIT");
343 case FT_LITF: return("LITF");
345 case FT_LIT_FORCE: return("LIT_FORCE");
347 case FT_CHAR: return("CHAR");
348 case FT_NUM: return("NUM");
349 case FT_NUMF: return("NUMF");
350 case FT_STR: return("STR");
351 case FT_STRF: return("STRF");
352 case FT_STRFW: return("STRFW");
353 case FT_PUTADDR: return("PUTADDR");
354 case FT_LS_COMP: return("LS_COMP");
355 case FT_LS_LIT: return("LS_LIT");
356 case FT_LS_GETENV: return("LS_GETENV");
357 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
358 case FT_LS_DECODE: return("FT_LS_DECODE");
359 case FT_LS_TRIM: return("LS_TRIM");
360 case FT_LV_COMP: return("LV_COMP");
361 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
362 case FT_LV_LIT: return("LV_LIT");
363 case FT_LV_DAT: return("LV_DAT");
364 case FT_LV_STRLEN: return("LV_STRLEN");
365 case FT_LV_PLUS_L: return("LV_PLUS_L");
366 case FT_LV_MINUS_L: return("LV_MINUS_L");
367 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
368 case FT_LV_MODULO_L: return("LV_MODULO_L");
369 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
370 case FT_LS_MONTH: return("LS_MONTH");
371 case FT_LS_LMONTH: return("LS_LMONTH");
372 case FT_LS_ZONE: return("LS_ZONE");
373 case FT_LS_DAY: return("LS_DAY");
374 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
375 case FT_LS_822DATE: return("LS_822DATE");
376 case FT_LS_PRETTY: return("LS_PRETTY");
377 case FT_LV_SEC: return("LV_SEC");
378 case FT_LV_MIN: return("LV_MIN");
379 case FT_LV_HOUR: return("LV_HOUR");
380 case FT_LV_MDAY: return("LV_MDAY");
381 case FT_LV_MON: return("LV_MON");
382 case FT_LV_YEAR: return("LV_YEAR");
383 case FT_LV_YDAY: return("LV_YDAY");
384 case FT_LV_WDAY: return("LV_WDAY");
385 case FT_LV_ZONE: return("LV_ZONE");
386 case FT_LV_CLOCK: return("LV_CLOCK");
387 case FT_LV_RCLOCK: return("LV_RCLOCK");
388 case FT_LV_DAYF: return("LV_DAYF");
389 case FT_LV_DST: return("LV_DST");
390 case FT_LV_ZONEF: return("LV_ZONEF");
391 case FT_LS_ADDR: return("LS_ADDR");
392 case FT_LS_PERS: return("LS_PERS");
393 case FT_LS_MBOX: return("LS_MBOX");
394 case FT_LS_HOST: return("LS_HOST");
395 case FT_LS_PATH: return("LS_PATH");
396 case FT_LS_GNAME: return("LS_GNAME");
397 case FT_LS_NOTE: return("LS_NOTE");
398 case FT_LS_822ADDR: return("LS_822ADDR");
399 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
400 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
401 case FT_LV_INGRPF: return("LV_INGRPF");
402 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
403 case FT_LOCALDATE: return("LOCALDATE");
404 case FT_GMTDATE: return("GMTDATE");
405 case FT_PARSEDATE: return("PARSEDATE");
406 case FT_PARSEADDR: return("PARSEADDR");
407 case FT_FORMATADDR: return("FORMATADDR");
408 case FT_MYMBOX: return("MYMBOX");
410 case FT_ADDTOSEQ: return("ADDTOSEQ");
412 case FT_SAVESTR: return("SAVESTR");
414 case FT_PAUSE: return ("PAUSE");
416 case FT_DONE: return("DONE");
417 case FT_NOP: return("NOP");
418 case FT_GOTO: return("GOTO");
419 case FT_IF_S_NULL: return("IF_S_NULL");
420 case FT_IF_S: return("IF_S");
421 case FT_IF_V_EQ: return("IF_V_EQ");
422 case FT_IF_V_NE: return("IF_V_NE");
423 case FT_IF_V_GT: return("IF_V_GT");
424 case FT_IF_MATCH: return("IF_MATCH");
425 case FT_IF_AMATCH: return("IF_AMATCH");
426 case FT_S_NULL: return("S_NULL");
427 case FT_S_NONNULL: return("S_NONNULL");
428 case FT_V_EQ: return("V_EQ");
429 case FT_V_NE: return("V_NE");
430 case FT_V_GT: return("V_GT");
431 case FT_V_MATCH: return("V_MATCH");
432 case FT_V_AMATCH: return("V_AMATCH");
434 printf(buf, "/* ??? #%d */", t);
439 #define FNORD(v, s) if (t & (v)) { \
451 if (t & ~(CT_ADDR|CT_DATE|CT_MYMBOX|CT_ADDRPARSE))
452 printf(buf, "0x%x ", t);
455 FNORD(CT_ADDR, "ADDR");
456 FNORD(CT_DATE, "DATE");
457 FNORD(CT_MYMBOX, "MYMBOX");
458 FNORD(CT_ADDRPARSE, "ADDRPARSE");
473 fputs("<nil>", stdout);
484 if (c < 0x20 || c == 0177) {
488 } else if (c == '\f') {
491 } else if (c == '\n') {
494 } else if (c == '\r') {
497 } else if (c == '\t') {
502 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */