]>
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 "sbr/print_version.h"
10 #include "sbr/print_help.h"
11 #include "sbr/error.h"
12 #include "h/fmt_scan.h"
13 #include "h/fmt_compile.h"
14 #include "h/scansbr.h"
18 #define FMTDUMP_SWITCHES \
19 X("form formatfile", 0, FORMSW) \
20 X("format string", 5, FMTSW) \
21 X("version", 0, VERSIONSW) \
22 X("help", 0, HELPSW) \
24 #define X(sw, minchars, id) id,
25 DEFINE_SWITCH_ENUM(FMTDUMP
);
28 #define X(sw, minchars, id) { sw, minchars, id },
29 DEFINE_SWITCH_ARRAY(FMTDUMP
, switches
);
33 static struct format
*lvec
[128];
39 static void fmt_dump (struct format
*);
40 static void dumpone(struct format
*);
41 static int findlabel(struct format
*);
42 static void assignlabel(struct format
*);
43 static char *f_typestr(int);
44 static char *c_typestr(int);
45 static char *c_flagsstr(int);
46 static void litputs(char *);
47 static void litputc(char);
51 main (int argc
, char **argv
)
53 char *cp
, *form
= NULL
, *format
= NULL
;
54 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
57 if (nmh_init(argv
[0], true, false)) { return 1; }
59 arguments
= getarguments (invo_name
, argc
, argv
, 1);
62 while ((cp
= *argp
++)) {
64 switch (smatch (++cp
, switches
)) {
66 ambigsw (cp
, switches
);
69 die("-%s unknown", cp
);
72 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
73 print_help (buf
, switches
, 1);
76 print_version(invo_name
);
80 if (!(form
= *argp
++) || *form
== '-')
81 die("missing argument to %s", argp
[-2]);
85 if (!(format
= *argp
++) || *format
== '-')
86 die("missing argument to %s", argp
[-2]);
93 die("only one form at a time!");
98 * Get new format string. Must be before chdir().
100 nfs
= new_fs (form
, format
, FORMAT
);
101 (void) fmt_compile(nfs
, &fmt
, 1);
112 fmt_dump (struct format
*fmth
)
115 struct format
*fmt
, *addr
;
118 for (fmt
= fmth
; fmt
; ++fmt
) {
128 addr
= fmt
+ fmt
->f_skip
;
129 if (findlabel(addr
) < 0)
132 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
137 for (fmt
= fmth
; fmt
; ++fmt
) {
139 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
145 dumpone(struct format
*fmt
)
149 if ((i
= findlabel(fmt
)) >= 0)
153 fputs(f_typestr((int)fmt
->f_type
), stdout
);
155 switch (fmt
->f_type
) {
161 fputs(", comp ", stdout
);
162 litputs(fmt
->f_comp
->c_name
);
163 if (fmt
->f_comp
->c_type
)
164 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
165 if (fmt
->f_comp
->c_flags
)
166 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
193 fputs(", c_name ", stdout
);
194 litputs(fmt
->f_comp
->c_name
);
195 if (fmt
->f_comp
->c_type
)
196 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
197 if (fmt
->f_comp
->c_flags
)
198 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
217 fputs(", c_name ", stdout
);
218 litputs(fmt
->f_comp
->c_name
);
219 if (fmt
->f_comp
->c_type
)
220 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
221 if (fmt
->f_comp
->c_flags
)
222 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
226 printf(", width %d, fill '", fmt
->f_width
);
227 litputc(fmt
->f_fill
);
228 fputs("' name ", stdout
);
229 litputs(fmt
->f_comp
->c_name
);
230 if (fmt
->f_comp
->c_type
)
231 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
232 if (fmt
->f_comp
->c_flags
)
233 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
238 printf(", width %d, fill '", fmt
->f_width
);
239 litputc(fmt
->f_fill
);
245 litputs(fmt
->f_text
);
249 printf(", width %d, fill '", fmt
->f_width
);
250 litputc(fmt
->f_fill
);
252 litputs(fmt
->f_text
);
258 litputc(fmt
->f_char
);
267 fputs(" continue else goto", stdout
);
270 i
= findlabel(fmt
+ fmt
->f_skip
);
277 i
= findlabel(fmt
+ fmt
->f_skip
);
278 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
287 case FT_LV_MULTIPLY_L
:
290 printf(" value %d", fmt
->f_value
);
294 fputs(" str ", stdout
);
295 litputs(fmt
->f_text
);
299 fputs(" getenv ", stdout
);
300 litputs(fmt
->f_text
);
303 case FT_LS_DECODECOMP
:
304 fputs(", comp ", stdout
);
305 litputs(fmt
->f_comp
->c_name
);
312 printf(", width %d", fmt
->f_width
);
316 printf(", value dat[%d]", fmt
->f_value
);
323 findlabel(struct format
*addr
)
327 for (i
= 0; i
< lused
; ++i
)
334 assignlabel(struct format
*addr
)
336 lvec
[lused
++] = addr
;
345 case FT_COMP
: return "COMP";
346 case FT_COMPF
: return "COMPF";
347 case FT_LIT
: return "LIT";
348 case FT_LITF
: return "LITF";
349 case FT_CHAR
: return "CHAR";
350 case FT_NUM
: return "NUM";
351 case FT_NUMF
: return "NUMF";
352 case FT_STR
: return "STR";
353 case FT_STRF
: return "STRF";
354 case FT_STRFW
: return "STRFW";
355 case FT_STRLIT
: return "STRLIT";
356 case FT_STRLITZ
: return "STRLITZ";
357 case FT_PUTADDR
: return "PUTADDR";
358 case FT_LS_COMP
: return "LS_COMP";
359 case FT_LS_LIT
: return "LS_LIT";
360 case FT_LS_GETENV
: return "LS_GETENV";
361 case FT_LS_CFIND
: return "LS_CFIND";
362 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
363 case FT_LS_DECODE
: return "LS_DECODE";
364 case FT_LS_TRIM
: return "LS_TRIM";
365 case FT_LV_COMP
: return "LV_COMP";
366 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
367 case FT_LV_LIT
: return "LV_LIT";
368 case FT_LV_DAT
: return "LV_DAT";
369 case FT_LV_STRLEN
: return "LV_STRLEN";
370 case FT_LV_PLUS_L
: return "LV_PLUS_L";
371 case FT_LV_MINUS_L
: return "LV_MINUS_L";
372 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
373 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
374 case FT_LV_MODULO_L
: return "LV_MODULO_L";
375 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
376 case FT_LS_MONTH
: return "LS_MONTH";
377 case FT_LS_LMONTH
: return "LS_LMONTH";
378 case FT_LS_ZONE
: return "LS_ZONE";
379 case FT_LS_DAY
: return "LS_DAY";
380 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
381 case FT_LS_822DATE
: return "LS_822DATE";
382 case FT_LS_PRETTY
: return "LS_PRETTY";
383 case FT_LS_KILO
: return "LS_KILO";
384 case FT_LS_KIBI
: return "LS_KIBI";
385 case FT_LV_SEC
: return "LV_SEC";
386 case FT_LV_MIN
: return "LV_MIN";
387 case FT_LV_HOUR
: return "LV_HOUR";
388 case FT_LV_MDAY
: return "LV_MDAY";
389 case FT_LV_MON
: return "LV_MON";
390 case FT_LV_YEAR
: return "LV_YEAR";
391 case FT_LV_YDAY
: return "LV_YDAY";
392 case FT_LV_WDAY
: return "LV_WDAY";
393 case FT_LV_ZONE
: return "LV_ZONE";
394 case FT_LV_CLOCK
: return "LV_CLOCK";
395 case FT_LV_RCLOCK
: return "LV_RCLOCK";
396 case FT_LV_DAYF
: return "LV_DAYF";
397 case FT_LV_DST
: return "LV_DST";
398 case FT_LV_ZONEF
: return "LV_ZONEF";
399 case FT_LS_PERS
: return "LS_PERS";
400 case FT_LS_MBOX
: return "LS_MBOX";
401 case FT_LS_HOST
: return "LS_HOST";
402 case FT_LS_PATH
: return "LS_PATH";
403 case FT_LS_GNAME
: return "LS_GNAME";
404 case FT_LS_NOTE
: return "LS_NOTE";
405 case FT_LS_ADDR
: return "LS_ADDR";
406 case FT_LS_822ADDR
: return "LS_822ADDR";
407 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
408 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
409 case FT_LV_INGRPF
: return "LV_INGRPF";
410 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
411 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
412 case FT_LOCALDATE
: return "LOCALDATE";
413 case FT_GMTDATE
: return "GMTDATE";
414 case FT_PARSEDATE
: return "PARSEDATE";
415 case FT_PARSEADDR
: return "PARSEADDR";
416 case FT_FORMATADDR
: return "FORMATADDR";
417 case FT_CONCATADDR
: return "CONCATADDR";
418 case FT_MYMBOX
: return "MYMBOX";
419 case FT_GETMYMBOX
: return "GETMYMBOX";
420 case FT_GETMYADDR
: return "GETMYADDR";
421 case FT_SAVESTR
: return "SAVESTR";
422 case FT_DONE
: return "DONE";
423 case FT_PAUSE
: return "PAUSE";
424 case FT_NOP
: return "NOP";
425 case FT_GOTO
: return "GOTO";
426 case FT_IF_S_NULL
: return "IF_S_NULL";
427 case FT_IF_S
: return "IF_S";
428 case FT_IF_V_EQ
: return "IF_V_EQ";
429 case FT_IF_V_NE
: return "IF_V_NE";
430 case FT_IF_V_GT
: return "IF_V_GT";
431 case FT_IF_MATCH
: return "IF_MATCH";
432 case FT_IF_AMATCH
: return "IF_AMATCH";
433 case FT_S_NULL
: return "S_NULL";
434 case FT_S_NONNULL
: return "S_NONNULL";
435 case FT_V_EQ
: return "V_EQ";
436 case FT_V_NE
: return "V_NE";
437 case FT_V_GT
: return "V_GT";
438 case FT_V_MATCH
: return "V_MATCH";
439 case FT_V_AMATCH
: return "V_AMATCH";
441 snprintf(buf
, sizeof buf
, "/* ??? #%d */", t
);
446 #define FNORD(v, s) if (t & (v)) { \
458 if (t
& ~(CT_ADDR
|CT_DATE
))
459 snprintf(buf
, sizeof buf
, "0x%x ", t
);
462 FNORD(CT_ADDR
, "ADDR");
463 FNORD(CT_DATE
, "DATE");
475 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
476 snprintf(buf
, sizeof buf
, "0x%x ", t
);
479 FNORD(CF_TRUE
, "TRUE");
480 FNORD(CF_PARSED
, "PARSED");
481 FNORD(CF_DATEFAB
, "DATEFAB");
482 FNORD(CF_TRIMMED
, "TRIMMED");
497 fputs("<nil>", stdout
);
508 if (c
< 0x20 || c
== 0177) {
512 } else if (c
== '\f') {
515 } else if (c
== '\n') {
518 } else if (c
== '\r') {
521 } else if (c
== '\t') {
526 putchar(c
^ 0x40); /* DEL to ?, others to alpha */