]>
diplodocus.org Git - nmh/blob - uip/fmtdump.c
c3302669c3644c63792fc9d1953524f86cd4cd1f
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.
9 #include "sbr/print_help.h"
10 #include "sbr/error.h"
11 #include "h/fmt_scan.h"
12 #include "h/fmt_compile.h"
13 #include "h/scansbr.h"
17 #define FMTDUMP_SWITCHES \
18 X("form formatfile", 0, FORMSW) \
19 X("format string", 5, FMTSW) \
20 X("version", 0, VERSIONSW) \
21 X("help", 0, HELPSW) \
23 #define X(sw, minchars, id) id,
24 DEFINE_SWITCH_ENUM(FMTDUMP
);
27 #define X(sw, minchars, id) { sw, minchars, id },
28 DEFINE_SWITCH_ARRAY(FMTDUMP
, switches
);
32 static struct format
*lvec
[128];
38 static void fmt_dump (struct format
*);
39 static void dumpone(struct format
*);
40 static int findlabel(struct format
*);
41 static void assignlabel(struct format
*);
42 static char *f_typestr(int);
43 static char *c_typestr(int);
44 static char *c_flagsstr(int);
45 static void litputs(char *);
46 static void litputc(char);
50 main (int argc
, char **argv
)
52 char *cp
, *form
= NULL
, *format
= NULL
;
53 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
56 if (nmh_init(argv
[0], true, false)) { return 1; }
58 arguments
= getarguments (invo_name
, argc
, argv
, 1);
61 while ((cp
= *argp
++)) {
63 switch (smatch (++cp
, switches
)) {
65 ambigsw (cp
, switches
);
68 die("-%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 die("missing argument to %s", argp
[-2]);
84 if (!(format
= *argp
++) || *format
== '-')
85 die("missing argument to %s", argp
[-2]);
92 die("only one form at a time!");
97 * Get new format string. Must be before chdir().
99 nfs
= new_fs (form
, format
, FORMAT
);
100 (void) fmt_compile(nfs
, &fmt
, 1);
111 fmt_dump (struct format
*fmth
)
114 struct format
*fmt
, *addr
;
117 for (fmt
= fmth
; fmt
; ++fmt
) {
127 addr
= fmt
+ fmt
->f_skip
;
128 if (findlabel(addr
) < 0)
131 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
136 for (fmt
= fmth
; fmt
; ++fmt
) {
138 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
144 dumpone(struct format
*fmt
)
148 if ((i
= findlabel(fmt
)) >= 0)
152 fputs(f_typestr((int)fmt
->f_type
), stdout
);
154 switch (fmt
->f_type
) {
160 fputs(", comp ", stdout
);
161 litputs(fmt
->f_comp
->c_name
);
162 if (fmt
->f_comp
->c_type
)
163 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
164 if (fmt
->f_comp
->c_flags
)
165 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
192 fputs(", c_name ", stdout
);
193 litputs(fmt
->f_comp
->c_name
);
194 if (fmt
->f_comp
->c_type
)
195 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
196 if (fmt
->f_comp
->c_flags
)
197 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
216 fputs(", c_name ", stdout
);
217 litputs(fmt
->f_comp
->c_name
);
218 if (fmt
->f_comp
->c_type
)
219 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
220 if (fmt
->f_comp
->c_flags
)
221 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
225 printf(", width %d, fill '", fmt
->f_width
);
226 litputc(fmt
->f_fill
);
227 fputs("' name ", stdout
);
228 litputs(fmt
->f_comp
->c_name
);
229 if (fmt
->f_comp
->c_type
)
230 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
231 if (fmt
->f_comp
->c_flags
)
232 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
237 printf(", width %d, fill '", fmt
->f_width
);
238 litputc(fmt
->f_fill
);
244 litputs(fmt
->f_text
);
248 printf(", width %d, fill '", fmt
->f_width
);
249 litputc(fmt
->f_fill
);
251 litputs(fmt
->f_text
);
257 litputc(fmt
->f_char
);
266 fputs(" continue else goto", stdout
);
269 i
= findlabel(fmt
+ fmt
->f_skip
);
276 i
= findlabel(fmt
+ fmt
->f_skip
);
277 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
286 case FT_LV_MULTIPLY_L
:
289 printf(" value %d", fmt
->f_value
);
293 fputs(" str ", stdout
);
294 litputs(fmt
->f_text
);
298 fputs(" getenv ", stdout
);
299 litputs(fmt
->f_text
);
302 case FT_LS_DECODECOMP
:
303 fputs(", comp ", stdout
);
304 litputs(fmt
->f_comp
->c_name
);
311 printf(", width %d", fmt
->f_width
);
315 printf(", value dat[%d]", fmt
->f_value
);
322 findlabel(struct format
*addr
)
326 for (i
= 0; i
< lused
; ++i
)
333 assignlabel(struct format
*addr
)
335 lvec
[lused
++] = addr
;
344 case FT_COMP
: return "COMP";
345 case FT_COMPF
: return "COMPF";
346 case FT_LIT
: return "LIT";
347 case FT_LITF
: return "LITF";
348 case FT_CHAR
: return "CHAR";
349 case FT_NUM
: return "NUM";
350 case FT_NUMF
: return "NUMF";
351 case FT_STR
: return "STR";
352 case FT_STRF
: return "STRF";
353 case FT_STRFW
: return "STRFW";
354 case FT_STRLIT
: return "STRLIT";
355 case FT_STRLITZ
: return "STRLITZ";
356 case FT_PUTADDR
: return "PUTADDR";
357 case FT_LS_COMP
: return "LS_COMP";
358 case FT_LS_LIT
: return "LS_LIT";
359 case FT_LS_GETENV
: return "LS_GETENV";
360 case FT_LS_CFIND
: return "LS_CFIND";
361 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
362 case FT_LS_DECODE
: return "LS_DECODE";
363 case FT_LS_TRIM
: return "LS_TRIM";
364 case FT_LV_COMP
: return "LV_COMP";
365 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
366 case FT_LV_LIT
: return "LV_LIT";
367 case FT_LV_DAT
: return "LV_DAT";
368 case FT_LV_STRLEN
: return "LV_STRLEN";
369 case FT_LV_PLUS_L
: return "LV_PLUS_L";
370 case FT_LV_MINUS_L
: return "LV_MINUS_L";
371 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
372 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
373 case FT_LV_MODULO_L
: return "LV_MODULO_L";
374 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
375 case FT_LS_MONTH
: return "LS_MONTH";
376 case FT_LS_LMONTH
: return "LS_LMONTH";
377 case FT_LS_ZONE
: return "LS_ZONE";
378 case FT_LS_DAY
: return "LS_DAY";
379 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
380 case FT_LS_822DATE
: return "LS_822DATE";
381 case FT_LS_PRETTY
: return "LS_PRETTY";
382 case FT_LS_KILO
: return "LS_KILO";
383 case FT_LS_KIBI
: return "LS_KIBI";
384 case FT_LV_SEC
: return "LV_SEC";
385 case FT_LV_MIN
: return "LV_MIN";
386 case FT_LV_HOUR
: return "LV_HOUR";
387 case FT_LV_MDAY
: return "LV_MDAY";
388 case FT_LV_MON
: return "LV_MON";
389 case FT_LV_YEAR
: return "LV_YEAR";
390 case FT_LV_YDAY
: return "LV_YDAY";
391 case FT_LV_WDAY
: return "LV_WDAY";
392 case FT_LV_ZONE
: return "LV_ZONE";
393 case FT_LV_CLOCK
: return "LV_CLOCK";
394 case FT_LV_RCLOCK
: return "LV_RCLOCK";
395 case FT_LV_DAYF
: return "LV_DAYF";
396 case FT_LV_DST
: return "LV_DST";
397 case FT_LV_ZONEF
: return "LV_ZONEF";
398 case FT_LS_PERS
: return "LS_PERS";
399 case FT_LS_MBOX
: return "LS_MBOX";
400 case FT_LS_HOST
: return "LS_HOST";
401 case FT_LS_PATH
: return "LS_PATH";
402 case FT_LS_GNAME
: return "LS_GNAME";
403 case FT_LS_NOTE
: return "LS_NOTE";
404 case FT_LS_ADDR
: return "LS_ADDR";
405 case FT_LS_822ADDR
: return "LS_822ADDR";
406 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
407 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
408 case FT_LV_INGRPF
: return "LV_INGRPF";
409 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
410 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
411 case FT_LOCALDATE
: return "LOCALDATE";
412 case FT_GMTDATE
: return "GMTDATE";
413 case FT_PARSEDATE
: return "PARSEDATE";
414 case FT_PARSEADDR
: return "PARSEADDR";
415 case FT_FORMATADDR
: return "FORMATADDR";
416 case FT_CONCATADDR
: return "CONCATADDR";
417 case FT_MYMBOX
: return "MYMBOX";
418 case FT_GETMYMBOX
: return "GETMYMBOX";
419 case FT_GETMYADDR
: return "GETMYADDR";
420 case FT_SAVESTR
: return "SAVESTR";
421 case FT_DONE
: return "DONE";
422 case FT_PAUSE
: return "PAUSE";
423 case FT_NOP
: return "NOP";
424 case FT_GOTO
: return "GOTO";
425 case FT_IF_S_NULL
: return "IF_S_NULL";
426 case FT_IF_S
: return "IF_S";
427 case FT_IF_V_EQ
: return "IF_V_EQ";
428 case FT_IF_V_NE
: return "IF_V_NE";
429 case FT_IF_V_GT
: return "IF_V_GT";
430 case FT_IF_MATCH
: return "IF_MATCH";
431 case FT_IF_AMATCH
: return "IF_AMATCH";
432 case FT_S_NULL
: return "S_NULL";
433 case FT_S_NONNULL
: return "S_NONNULL";
434 case FT_V_EQ
: return "V_EQ";
435 case FT_V_NE
: return "V_NE";
436 case FT_V_GT
: return "V_GT";
437 case FT_V_MATCH
: return "V_MATCH";
438 case FT_V_AMATCH
: return "V_AMATCH";
440 snprintf(buf
, sizeof buf
, "/* ??? #%d */", t
);
445 #define FNORD(v, s) if (t & (v)) { \
457 if (t
& ~(CT_ADDR
|CT_DATE
))
458 snprintf(buf
, sizeof buf
, "0x%x ", t
);
461 FNORD(CT_ADDR
, "ADDR");
462 FNORD(CT_DATE
, "DATE");
474 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
475 snprintf(buf
, sizeof buf
, "0x%x ", t
);
478 FNORD(CF_TRUE
, "TRUE");
479 FNORD(CF_PARSED
, "PARSED");
480 FNORD(CF_DATEFAB
, "DATEFAB");
481 FNORD(CF_TRIMMED
, "TRIMMED");
496 fputs("<nil>", stdout
);
507 if (c
< 0x20 || c
== 0177) {
511 } else if (c
== '\f') {
514 } else if (c
== '\n') {
517 } else if (c
== '\r') {
520 } else if (c
== '\t') {
525 putchar(c
^ 0x40); /* DEL to ?, others to alpha */