]>
diplodocus.org Git - nmh/blob - uip/fmtdump.c
808d97ba3a5a6cd47378a62d31dd1ff58b541cf2
1 /* fmtdump.c -- compile format file and dump out instructions
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
10 #include <h/fmt_scan.h>
11 #include <h/fmt_compile.h>
12 #include <h/scansbr.h>
16 #define FMTDUMP_SWITCHES \
17 X("form formatfile", 0, FORMSW) \
18 X("format string", 5, FMTSW) \
19 X("version", 0, VERSIONSW) \
20 X("help", 0, HELPSW) \
22 #define X(sw, minchars, id) id,
23 DEFINE_SWITCH_ENUM(FMTDUMP
);
26 #define X(sw, minchars, id) { sw, minchars, id },
27 DEFINE_SWITCH_ARRAY(FMTDUMP
, switches
);
31 static struct format
*lvec
[128];
37 static void fmt_dump (struct format
*);
38 static void dumpone(struct format
*);
39 static int findlabel(struct format
*);
40 static void assignlabel(struct format
*);
41 static char *f_typestr(int);
42 static char *c_typestr(int);
43 static char *c_flagsstr(int);
44 static void litputs(char *);
45 static void litputc(char);
49 main (int argc
, char **argv
)
51 char *cp
, *form
= NULL
, *format
= NULL
;
52 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
55 if (nmh_init(argv
[0], true, false)) { return 1; }
57 arguments
= getarguments (invo_name
, argc
, argv
, 1);
60 while ((cp
= *argp
++)) {
62 switch (smatch (++cp
, switches
)) {
64 ambigsw (cp
, switches
);
67 die("-%s unknown", cp
);
70 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
71 print_help (buf
, switches
, 1);
74 print_version(invo_name
);
78 if (!(form
= *argp
++) || *form
== '-')
79 die("missing argument to %s", argp
[-2]);
83 if (!(format
= *argp
++) || *format
== '-')
84 die("missing argument to %s", argp
[-2]);
91 die("only one form at a time!");
96 * Get new format string. Must be before chdir().
98 nfs
= new_fs (form
, format
, FORMAT
);
99 (void) fmt_compile(nfs
, &fmt
, 1);
110 fmt_dump (struct format
*fmth
)
113 struct format
*fmt
, *addr
;
116 for (fmt
= fmth
; fmt
; ++fmt
) {
126 addr
= fmt
+ fmt
->f_skip
;
127 if (findlabel(addr
) < 0)
130 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
135 for (fmt
= fmth
; fmt
; ++fmt
) {
137 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
143 dumpone(struct format
*fmt
)
147 if ((i
= findlabel(fmt
)) >= 0)
151 fputs(f_typestr((int)fmt
->f_type
), stdout
);
153 switch (fmt
->f_type
) {
159 fputs(", comp ", stdout
);
160 litputs(fmt
->f_comp
->c_name
);
161 if (fmt
->f_comp
->c_type
)
162 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
163 if (fmt
->f_comp
->c_flags
)
164 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
191 fputs(", c_name ", stdout
);
192 litputs(fmt
->f_comp
->c_name
);
193 if (fmt
->f_comp
->c_type
)
194 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
195 if (fmt
->f_comp
->c_flags
)
196 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
215 fputs(", c_name ", stdout
);
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", c_flagsstr(fmt
->f_comp
->c_flags
));
224 printf(", width %d, fill '", fmt
->f_width
);
225 litputc(fmt
->f_fill
);
226 fputs("' name ", stdout
);
227 litputs(fmt
->f_comp
->c_name
);
228 if (fmt
->f_comp
->c_type
)
229 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
230 if (fmt
->f_comp
->c_flags
)
231 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
236 printf(", width %d, fill '", fmt
->f_width
);
237 litputc(fmt
->f_fill
);
243 litputs(fmt
->f_text
);
247 printf(", width %d, fill '", fmt
->f_width
);
248 litputc(fmt
->f_fill
);
250 litputs(fmt
->f_text
);
256 litputc(fmt
->f_char
);
265 fputs(" continue else goto", stdout
);
268 i
= findlabel(fmt
+ fmt
->f_skip
);
275 i
= findlabel(fmt
+ fmt
->f_skip
);
276 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
285 case FT_LV_MULTIPLY_L
:
288 printf(" value %d", fmt
->f_value
);
292 fputs(" str ", stdout
);
293 litputs(fmt
->f_text
);
297 fputs(" getenv ", stdout
);
298 litputs(fmt
->f_text
);
301 case FT_LS_DECODECOMP
:
302 fputs(", comp ", stdout
);
303 litputs(fmt
->f_comp
->c_name
);
310 printf(", width %d", fmt
->f_width
);
314 printf(", value dat[%d]", fmt
->f_value
);
321 findlabel(struct format
*addr
)
325 for (i
= 0; i
< lused
; ++i
)
332 assignlabel(struct format
*addr
)
334 lvec
[lused
++] = addr
;
343 case FT_COMP
: return "COMP";
344 case FT_COMPF
: return "COMPF";
345 case FT_LIT
: return "LIT";
346 case FT_LITF
: return "LITF";
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_STRLIT
: return "STRLIT";
354 case FT_STRLITZ
: return "STRLITZ";
355 case FT_PUTADDR
: return "PUTADDR";
356 case FT_LS_COMP
: return "LS_COMP";
357 case FT_LS_LIT
: return "LS_LIT";
358 case FT_LS_GETENV
: return "LS_GETENV";
359 case FT_LS_CFIND
: return "LS_CFIND";
360 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
361 case FT_LS_DECODE
: return "LS_DECODE";
362 case FT_LS_TRIM
: return "LS_TRIM";
363 case FT_LV_COMP
: return "LV_COMP";
364 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
365 case FT_LV_LIT
: return "LV_LIT";
366 case FT_LV_DAT
: return "LV_DAT";
367 case FT_LV_STRLEN
: return "LV_STRLEN";
368 case FT_LV_PLUS_L
: return "LV_PLUS_L";
369 case FT_LV_MINUS_L
: return "LV_MINUS_L";
370 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
371 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
372 case FT_LV_MODULO_L
: return "LV_MODULO_L";
373 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
374 case FT_LS_MONTH
: return "LS_MONTH";
375 case FT_LS_LMONTH
: return "LS_LMONTH";
376 case FT_LS_ZONE
: return "LS_ZONE";
377 case FT_LS_DAY
: return "LS_DAY";
378 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
379 case FT_LS_822DATE
: return "LS_822DATE";
380 case FT_LS_PRETTY
: return "LS_PRETTY";
381 case FT_LS_KILO
: return "LS_KILO";
382 case FT_LS_KIBI
: return "LS_KIBI";
383 case FT_LV_SEC
: return "LV_SEC";
384 case FT_LV_MIN
: return "LV_MIN";
385 case FT_LV_HOUR
: return "LV_HOUR";
386 case FT_LV_MDAY
: return "LV_MDAY";
387 case FT_LV_MON
: return "LV_MON";
388 case FT_LV_YEAR
: return "LV_YEAR";
389 case FT_LV_YDAY
: return "LV_YDAY";
390 case FT_LV_WDAY
: return "LV_WDAY";
391 case FT_LV_ZONE
: return "LV_ZONE";
392 case FT_LV_CLOCK
: return "LV_CLOCK";
393 case FT_LV_RCLOCK
: return "LV_RCLOCK";
394 case FT_LV_DAYF
: return "LV_DAYF";
395 case FT_LV_DST
: return "LV_DST";
396 case FT_LV_ZONEF
: return "LV_ZONEF";
397 case FT_LS_PERS
: return "LS_PERS";
398 case FT_LS_MBOX
: return "LS_MBOX";
399 case FT_LS_HOST
: return "LS_HOST";
400 case FT_LS_PATH
: return "LS_PATH";
401 case FT_LS_GNAME
: return "LS_GNAME";
402 case FT_LS_NOTE
: return "LS_NOTE";
403 case FT_LS_ADDR
: return "LS_ADDR";
404 case FT_LS_822ADDR
: return "LS_822ADDR";
405 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
406 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
407 case FT_LV_INGRPF
: return "LV_INGRPF";
408 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
409 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
410 case FT_LOCALDATE
: return "LOCALDATE";
411 case FT_GMTDATE
: return "GMTDATE";
412 case FT_PARSEDATE
: return "PARSEDATE";
413 case FT_PARSEADDR
: return "PARSEADDR";
414 case FT_FORMATADDR
: return "FORMATADDR";
415 case FT_CONCATADDR
: return "CONCATADDR";
416 case FT_MYMBOX
: return "MYMBOX";
417 case FT_GETMYMBOX
: return "GETMYMBOX";
418 case FT_GETMYADDR
: return "GETMYADDR";
419 case FT_SAVESTR
: return "SAVESTR";
420 case FT_DONE
: return "DONE";
421 case FT_PAUSE
: return "PAUSE";
422 case FT_NOP
: return "NOP";
423 case FT_GOTO
: return "GOTO";
424 case FT_IF_S_NULL
: return "IF_S_NULL";
425 case FT_IF_S
: return "IF_S";
426 case FT_IF_V_EQ
: return "IF_V_EQ";
427 case FT_IF_V_NE
: return "IF_V_NE";
428 case FT_IF_V_GT
: return "IF_V_GT";
429 case FT_IF_MATCH
: return "IF_MATCH";
430 case FT_IF_AMATCH
: return "IF_AMATCH";
431 case FT_S_NULL
: return "S_NULL";
432 case FT_S_NONNULL
: return "S_NONNULL";
433 case FT_V_EQ
: return "V_EQ";
434 case FT_V_NE
: return "V_NE";
435 case FT_V_GT
: return "V_GT";
436 case FT_V_MATCH
: return "V_MATCH";
437 case FT_V_AMATCH
: return "V_AMATCH";
439 snprintf(buf
, sizeof buf
, "/* ??? #%d */", t
);
444 #define FNORD(v, s) if (t & (v)) { \
456 if (t
& ~(CT_ADDR
|CT_DATE
))
457 snprintf(buf
, sizeof buf
, "0x%x ", t
);
460 FNORD(CT_ADDR
, "ADDR");
461 FNORD(CT_DATE
, "DATE");
473 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
474 snprintf(buf
, sizeof buf
, "0x%x ", t
);
477 FNORD(CF_TRUE
, "TRUE");
478 FNORD(CF_PARSED
, "PARSED");
479 FNORD(CF_DATEFAB
, "DATEFAB");
480 FNORD(CF_TRIMMED
, "TRIMMED");
495 fputs("<nil>", stdout
);
506 if (c
< 0x20 || c
== 0177) {
510 } else if (c
== '\f') {
513 } else if (c
== '\n') {
516 } else if (c
== '\r') {
519 } else if (c
== '\t') {
524 putchar(c
^ 0x40); /* DEL to ?, others to alpha */