]>
diplodocus.org Git - nmh/blob - uip/fmtdump.c
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 <h/fmt_scan.h>
10 #include <h/fmt_compile.h>
11 #include <h/scansbr.h>
15 #define FMTDUMP_SWITCHES \
16 X("form formatfile", 0, FORMSW) \
17 X("format string", 5, FMTSW) \
18 X("version", 0, VERSIONSW) \
19 X("help", 0, HELPSW) \
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(FMTDUMP
);
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(FMTDUMP
, switches
);
30 static struct format
*lvec
[128];
36 static void fmt_dump (struct format
*);
37 static void dumpone(struct format
*);
38 static int findlabel(struct format
*);
39 static void assignlabel(struct format
*);
40 static char *f_typestr(int);
41 static char *c_typestr(int);
42 static char *c_flagsstr(int);
43 static void litputs(char *);
44 static void litputc(char);
48 main (int argc
, char **argv
)
50 char *cp
, *form
= NULL
, *format
= NULL
;
51 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
54 if (nmh_init(argv
[0], true, false)) { return 1; }
56 arguments
= getarguments (invo_name
, argc
, argv
, 1);
59 while ((cp
= *argp
++)) {
61 switch (smatch (++cp
, switches
)) {
63 ambigsw (cp
, switches
);
66 die("-%s unknown", cp
);
69 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
70 print_help (buf
, switches
, 1);
73 print_version(invo_name
);
77 if (!(form
= *argp
++) || *form
== '-')
78 die("missing argument to %s", argp
[-2]);
82 if (!(format
= *argp
++) || *format
== '-')
83 die("missing argument to %s", argp
[-2]);
90 die("only one form at a time!");
95 * Get new format string. Must be before chdir().
97 nfs
= new_fs (form
, format
, FORMAT
);
98 (void) fmt_compile(nfs
, &fmt
, 1);
109 fmt_dump (struct format
*fmth
)
112 struct format
*fmt
, *addr
;
115 for (fmt
= fmth
; fmt
; ++fmt
) {
125 addr
= fmt
+ fmt
->f_skip
;
126 if (findlabel(addr
) < 0)
129 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
134 for (fmt
= fmth
; fmt
; ++fmt
) {
136 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
142 dumpone(struct format
*fmt
)
146 if ((i
= findlabel(fmt
)) >= 0)
150 fputs(f_typestr((int)fmt
->f_type
), stdout
);
152 switch (fmt
->f_type
) {
158 fputs(", comp ", stdout
);
159 litputs(fmt
->f_comp
->c_name
);
160 if (fmt
->f_comp
->c_type
)
161 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
162 if (fmt
->f_comp
->c_flags
)
163 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
190 fputs(", c_name ", stdout
);
191 litputs(fmt
->f_comp
->c_name
);
192 if (fmt
->f_comp
->c_type
)
193 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
194 if (fmt
->f_comp
->c_flags
)
195 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
214 fputs(", c_name ", stdout
);
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", c_flagsstr(fmt
->f_comp
->c_flags
));
223 printf(", width %d, fill '", fmt
->f_width
);
224 litputc(fmt
->f_fill
);
225 fputs("' name ", stdout
);
226 litputs(fmt
->f_comp
->c_name
);
227 if (fmt
->f_comp
->c_type
)
228 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
229 if (fmt
->f_comp
->c_flags
)
230 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
235 printf(", width %d, fill '", fmt
->f_width
);
236 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 fputs(" continue else goto", stdout
);
267 i
= findlabel(fmt
+ fmt
->f_skip
);
274 i
= findlabel(fmt
+ fmt
->f_skip
);
275 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
284 case FT_LV_MULTIPLY_L
:
287 printf(" value %d", fmt
->f_value
);
291 fputs(" str ", stdout
);
292 litputs(fmt
->f_text
);
296 fputs(" getenv ", stdout
);
297 litputs(fmt
->f_text
);
300 case FT_LS_DECODECOMP
:
301 fputs(", comp ", stdout
);
302 litputs(fmt
->f_comp
->c_name
);
309 printf(", width %d", fmt
->f_width
);
313 printf(", value dat[%d]", fmt
->f_value
);
320 findlabel(struct format
*addr
)
324 for (i
= 0; i
< lused
; ++i
)
331 assignlabel(struct format
*addr
)
333 lvec
[lused
++] = addr
;
342 case FT_COMP
: return "COMP";
343 case FT_COMPF
: return "COMPF";
344 case FT_LIT
: return "LIT";
345 case FT_LITF
: return "LITF";
346 case FT_CHAR
: return "CHAR";
347 case FT_NUM
: return "NUM";
348 case FT_NUMF
: return "NUMF";
349 case FT_STR
: return "STR";
350 case FT_STRF
: return "STRF";
351 case FT_STRFW
: return "STRFW";
352 case FT_STRLIT
: return "STRLIT";
353 case FT_STRLITZ
: return "STRLITZ";
354 case FT_PUTADDR
: return "PUTADDR";
355 case FT_LS_COMP
: return "LS_COMP";
356 case FT_LS_LIT
: return "LS_LIT";
357 case FT_LS_GETENV
: return "LS_GETENV";
358 case FT_LS_CFIND
: return "LS_CFIND";
359 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
360 case FT_LS_DECODE
: return "LS_DECODE";
361 case FT_LS_TRIM
: return "LS_TRIM";
362 case FT_LV_COMP
: return "LV_COMP";
363 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
364 case FT_LV_LIT
: return "LV_LIT";
365 case FT_LV_DAT
: return "LV_DAT";
366 case FT_LV_STRLEN
: return "LV_STRLEN";
367 case FT_LV_PLUS_L
: return "LV_PLUS_L";
368 case FT_LV_MINUS_L
: return "LV_MINUS_L";
369 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
370 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
371 case FT_LV_MODULO_L
: return "LV_MODULO_L";
372 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
373 case FT_LS_MONTH
: return "LS_MONTH";
374 case FT_LS_LMONTH
: return "LS_LMONTH";
375 case FT_LS_ZONE
: return "LS_ZONE";
376 case FT_LS_DAY
: return "LS_DAY";
377 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
378 case FT_LS_822DATE
: return "LS_822DATE";
379 case FT_LS_PRETTY
: return "LS_PRETTY";
380 case FT_LS_KILO
: return "LS_KILO";
381 case FT_LS_KIBI
: return "LS_KIBI";
382 case FT_LV_SEC
: return "LV_SEC";
383 case FT_LV_MIN
: return "LV_MIN";
384 case FT_LV_HOUR
: return "LV_HOUR";
385 case FT_LV_MDAY
: return "LV_MDAY";
386 case FT_LV_MON
: return "LV_MON";
387 case FT_LV_YEAR
: return "LV_YEAR";
388 case FT_LV_YDAY
: return "LV_YDAY";
389 case FT_LV_WDAY
: return "LV_WDAY";
390 case FT_LV_ZONE
: return "LV_ZONE";
391 case FT_LV_CLOCK
: return "LV_CLOCK";
392 case FT_LV_RCLOCK
: return "LV_RCLOCK";
393 case FT_LV_DAYF
: return "LV_DAYF";
394 case FT_LV_DST
: return "LV_DST";
395 case FT_LV_ZONEF
: return "LV_ZONEF";
396 case FT_LS_PERS
: return "LS_PERS";
397 case FT_LS_MBOX
: return "LS_MBOX";
398 case FT_LS_HOST
: return "LS_HOST";
399 case FT_LS_PATH
: return "LS_PATH";
400 case FT_LS_GNAME
: return "LS_GNAME";
401 case FT_LS_NOTE
: return "LS_NOTE";
402 case FT_LS_ADDR
: return "LS_ADDR";
403 case FT_LS_822ADDR
: return "LS_822ADDR";
404 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
405 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
406 case FT_LV_INGRPF
: return "LV_INGRPF";
407 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
408 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
409 case FT_LOCALDATE
: return "LOCALDATE";
410 case FT_GMTDATE
: return "GMTDATE";
411 case FT_PARSEDATE
: return "PARSEDATE";
412 case FT_PARSEADDR
: return "PARSEADDR";
413 case FT_FORMATADDR
: return "FORMATADDR";
414 case FT_CONCATADDR
: return "CONCATADDR";
415 case FT_MYMBOX
: return "MYMBOX";
416 case FT_GETMYMBOX
: return "GETMYMBOX";
417 case FT_GETMYADDR
: return "GETMYADDR";
418 case FT_SAVESTR
: return "SAVESTR";
419 case FT_DONE
: return "DONE";
420 case FT_PAUSE
: return "PAUSE";
421 case FT_NOP
: return "NOP";
422 case FT_GOTO
: return "GOTO";
423 case FT_IF_S_NULL
: return "IF_S_NULL";
424 case FT_IF_S
: return "IF_S";
425 case FT_IF_V_EQ
: return "IF_V_EQ";
426 case FT_IF_V_NE
: return "IF_V_NE";
427 case FT_IF_V_GT
: return "IF_V_GT";
428 case FT_IF_MATCH
: return "IF_MATCH";
429 case FT_IF_AMATCH
: return "IF_AMATCH";
430 case FT_S_NULL
: return "S_NULL";
431 case FT_S_NONNULL
: return "S_NONNULL";
432 case FT_V_EQ
: return "V_EQ";
433 case FT_V_NE
: return "V_NE";
434 case FT_V_GT
: return "V_GT";
435 case FT_V_MATCH
: return "V_MATCH";
436 case FT_V_AMATCH
: return "V_AMATCH";
438 snprintf(buf
, sizeof buf
, "/* ??? #%d */", t
);
443 #define FNORD(v, s) if (t & (v)) { \
455 if (t
& ~(CT_ADDR
|CT_DATE
))
456 snprintf(buf
, sizeof buf
, "0x%x ", t
);
459 FNORD(CT_ADDR
, "ADDR");
460 FNORD(CT_DATE
, "DATE");
472 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
473 snprintf(buf
, sizeof buf
, "0x%x ", t
);
476 FNORD(CF_TRUE
, "TRUE");
477 FNORD(CF_PARSED
, "PARSED");
478 FNORD(CF_DATEFAB
, "DATEFAB");
479 FNORD(CF_TRIMMED
, "TRIMMED");
494 fputs("<nil>", stdout
);
505 if (c
< 0x20 || c
== 0177) {
509 } else if (c
== '\f') {
512 } else if (c
== '\n') {
515 } else if (c
== '\r') {
518 } else if (c
== '\t') {
523 putchar(c
^ 0x40); /* DEL to ?, others to alpha */