]>
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>
14 #define FMTDUMP_SWITCHES \
15 X("form formatfile", 0, FORMSW) \
16 X("format string", 5, FMTSW) \
17 X("version", 0, VERSIONSW) \
18 X("help", 0, HELPSW) \
20 #define X(sw, minchars, id) id,
21 DEFINE_SWITCH_ENUM(FMTDUMP
);
24 #define X(sw, minchars, id) { sw, minchars, id },
25 DEFINE_SWITCH_ARRAY(FMTDUMP
, switches
);
29 static struct format
*lvec
[128];
35 static void fmt_dump (struct format
*);
36 static void dumpone(struct format
*);
37 static int findlabel(struct format
*);
38 static void assignlabel(struct format
*);
39 static char *f_typestr(int);
40 static char *c_typestr(int);
41 static char *c_flagsstr(int);
42 static void litputs(char *);
43 static void litputc(char);
47 main (int argc
, char **argv
)
49 char *cp
, *form
= NULL
, *format
= NULL
;
50 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
53 if (nmh_init(argv
[0], 2)) { return 1; }
55 arguments
= getarguments (invo_name
, argc
, argv
, 1);
58 while ((cp
= *argp
++)) {
60 switch (smatch (++cp
, switches
)) {
62 ambigsw (cp
, switches
);
65 adios (NULL
, "-%s unknown", cp
);
68 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
69 print_help (buf
, switches
, 1);
72 print_version(invo_name
);
76 if (!(form
= *argp
++) || *form
== '-')
77 adios (NULL
, "missing argument to %s", argp
[-2]);
81 if (!(format
= *argp
++) || *format
== '-')
82 adios (NULL
, "missing argument to %s", argp
[-2]);
89 adios (NULL
, "only one form at a time!");
94 * Get new format string. Must be before chdir().
96 nfs
= new_fs (form
, format
, FORMAT
);
97 (void) fmt_compile(nfs
, &fmt
, 1);
108 fmt_dump (struct format
*fmth
)
111 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 %s", c_flagsstr(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 %s", c_flagsstr(fmt
->f_comp
->c_flags
));
214 litputs(fmt
->f_comp
->c_name
);
215 if (fmt
->f_comp
->c_type
)
216 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
217 if (fmt
->f_comp
->c_flags
)
218 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
222 printf(", width %d, fill '", fmt
->f_width
);
223 litputc(fmt
->f_fill
);
225 litputs(fmt
->f_comp
->c_name
);
226 if (fmt
->f_comp
->c_type
)
227 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
228 if (fmt
->f_comp
->c_flags
)
229 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
234 printf(", width %d, fill '", fmt
->f_width
);
235 litputc(fmt
->f_fill
);
241 litputs(fmt
->f_text
);
245 printf(", width %d, fill '", fmt
->f_width
);
246 litputc(fmt
->f_fill
);
248 litputs(fmt
->f_text
);
254 litputc(fmt
->f_char
);
263 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
);
283 case FT_LV_MULTIPLY_L
:
286 printf(" value %d", fmt
->f_value
);
291 litputs(fmt
->f_text
);
296 litputs(fmt
->f_text
);
299 case FT_LS_DECODECOMP
:
301 litputs(fmt
->f_comp
->c_name
);
308 printf(", width %d", fmt
->f_width
);
312 printf(", value dat[%d]", fmt
->f_value
);
319 findlabel(struct format
*addr
)
323 for (i
= 0; i
< lused
; ++i
)
330 assignlabel(struct format
*addr
)
332 lvec
[lused
++] = addr
;
341 case FT_COMP
: return "COMP";
342 case FT_COMPF
: return "COMPF";
343 case FT_LIT
: return "LIT";
344 case FT_LITF
: return "LITF";
345 case FT_CHAR
: return "CHAR";
346 case FT_NUM
: return "NUM";
347 case FT_NUMF
: return "NUMF";
348 case FT_STR
: return "STR";
349 case FT_STRF
: return "STRF";
350 case FT_STRFW
: return "STRFW";
351 case FT_STRLIT
: return "STRLIT";
352 case FT_STRLITZ
: return "STRLITZ";
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_CFIND
: return "LS_CFIND";
358 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
359 case FT_LS_DECODE
: return "LS_DECODE";
360 case FT_LS_TRIM
: return "LS_TRIM";
361 case FT_LV_COMP
: return "LV_COMP";
362 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
363 case FT_LV_LIT
: return "LV_LIT";
364 case FT_LV_DAT
: return "LV_DAT";
365 case FT_LV_STRLEN
: return "LV_STRLEN";
366 case FT_LV_PLUS_L
: return "LV_PLUS_L";
367 case FT_LV_MINUS_L
: return "LV_MINUS_L";
368 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
369 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
370 case FT_LV_MODULO_L
: return "LV_MODULO_L";
371 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
372 case FT_LS_MONTH
: return "LS_MONTH";
373 case FT_LS_LMONTH
: return "LS_LMONTH";
374 case FT_LS_ZONE
: return "LS_ZONE";
375 case FT_LS_DAY
: return "LS_DAY";
376 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
377 case FT_LS_822DATE
: return "LS_822DATE";
378 case FT_LS_PRETTY
: return "LS_PRETTY";
379 case FT_LS_KILO
: return "LS_KILO";
380 case FT_LS_KIBI
: return "LS_KIBI";
381 case FT_LV_SEC
: return "LV_SEC";
382 case FT_LV_MIN
: return "LV_MIN";
383 case FT_LV_HOUR
: return "LV_HOUR";
384 case FT_LV_MDAY
: return "LV_MDAY";
385 case FT_LV_MON
: return "LV_MON";
386 case FT_LV_YEAR
: return "LV_YEAR";
387 case FT_LV_YDAY
: return "LV_YDAY";
388 case FT_LV_WDAY
: return "LV_WDAY";
389 case FT_LV_ZONE
: return "LV_ZONE";
390 case FT_LV_CLOCK
: return "LV_CLOCK";
391 case FT_LV_RCLOCK
: return "LV_RCLOCK";
392 case FT_LV_DAYF
: return "LV_DAYF";
393 case FT_LV_DST
: return "LV_DST";
394 case FT_LV_ZONEF
: return "LV_ZONEF";
395 case FT_LS_PERS
: return "LS_PERS";
396 case FT_LS_MBOX
: return "LS_MBOX";
397 case FT_LS_HOST
: return "LS_HOST";
398 case FT_LS_PATH
: return "LS_PATH";
399 case FT_LS_GNAME
: return "LS_GNAME";
400 case FT_LS_NOTE
: return "LS_NOTE";
401 case FT_LS_ADDR
: return "LS_ADDR";
402 case FT_LS_822ADDR
: return "LS_822ADDR";
403 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
404 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
405 case FT_LV_INGRPF
: return "LV_INGRPF";
406 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
407 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
408 case FT_LOCALDATE
: return "LOCALDATE";
409 case FT_GMTDATE
: return "GMTDATE";
410 case FT_PARSEDATE
: return "PARSEDATE";
411 case FT_PARSEADDR
: return "PARSEADDR";
412 case FT_FORMATADDR
: return "FORMATADDR";
413 case FT_CONCATADDR
: return "CONCATADDR";
414 case FT_MYMBOX
: return "MYMBOX";
415 case FT_GETMYMBOX
: return "GETMYMBOX";
416 case FT_GETMYADDR
: return "GETMYADDR";
417 case FT_SAVESTR
: return "SAVESTR";
418 case FT_DONE
: return "DONE";
419 case FT_PAUSE
: return "PAUSE";
420 case FT_NOP
: return "NOP";
421 case FT_GOTO
: return "GOTO";
422 case FT_IF_S_NULL
: return "IF_S_NULL";
423 case FT_IF_S
: return "IF_S";
424 case FT_IF_V_EQ
: return "IF_V_EQ";
425 case FT_IF_V_NE
: return "IF_V_NE";
426 case FT_IF_V_GT
: return "IF_V_GT";
427 case FT_IF_MATCH
: return "IF_MATCH";
428 case FT_IF_AMATCH
: return "IF_AMATCH";
429 case FT_S_NULL
: return "S_NULL";
430 case FT_S_NONNULL
: return "S_NONNULL";
431 case FT_V_EQ
: return "V_EQ";
432 case FT_V_NE
: return "V_NE";
433 case FT_V_GT
: return "V_GT";
434 case FT_V_MATCH
: return "V_MATCH";
435 case FT_V_AMATCH
: return "V_AMATCH";
437 snprintf(buf
, sizeof buf
, "/* ??? #%d */", t
);
442 #define FNORD(v, s) if (t & (v)) { \
454 if (t
& ~(CT_ADDR
|CT_DATE
))
455 snprintf(buf
, sizeof buf
, "0x%x ", t
);
458 FNORD(CT_ADDR
, "ADDR");
459 FNORD(CT_DATE
, "DATE");
471 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
472 snprintf(buf
, sizeof buf
, "0x%x ", t
);
475 FNORD(CF_TRUE
, "TRUE");
476 FNORD(CF_PARSED
, "PARSED");
477 FNORD(CF_DATEFAB
, "DATEFAB");
478 FNORD(CF_TRIMMED
, "TRIMMED");
493 fputs("<nil>", stdout
);
504 if (c
< 0x20 || c
== 0177) {
508 } else if (c
== '\f') {
511 } else if (c
== '\n') {
514 } else if (c
== '\r') {
517 } else if (c
== '\t') {
522 putchar(c
^ 0x40); /* DEL to ?, others to alpha */