]>
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/smatch.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/print_version.h"
12 #include "sbr/print_help.h"
13 #include "sbr/error.h"
14 #include "h/fmt_scan.h"
15 #include "h/fmt_compile.h"
16 #include "h/scansbr.h"
20 #define FMTDUMP_SWITCHES \
21 X("form formatfile", 0, FORMSW) \
22 X("format string", 5, FMTSW) \
23 X("version", 0, VERSIONSW) \
24 X("help", 0, HELPSW) \
26 #define X(sw, minchars, id) id,
27 DEFINE_SWITCH_ENUM(FMTDUMP
);
30 #define X(sw, minchars, id) { sw, minchars, id },
31 DEFINE_SWITCH_ARRAY(FMTDUMP
, switches
);
35 static struct format
*lvec
[128];
41 static void fmt_dump (struct format
*);
42 static void dumpone(struct format
*);
43 static int findlabel(struct format
*);
44 static void assignlabel(struct format
*);
45 static char *f_typestr(int);
46 static char *c_typestr(int);
47 static char *c_flagsstr(int);
48 static void litputs(char *);
49 static void litputc(char);
53 main (int argc
, char **argv
)
55 char *cp
, *form
= NULL
, *format
= NULL
;
56 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
59 if (nmh_init(argv
[0], true, false)) { return 1; }
61 arguments
= getarguments (invo_name
, argc
, argv
, 1);
64 while ((cp
= *argp
++)) {
66 switch (smatch (++cp
, switches
)) {
68 ambigsw (cp
, switches
);
71 die("-%s unknown", cp
);
74 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
75 print_help (buf
, switches
, 1);
78 print_version(invo_name
);
82 if (!(form
= *argp
++) || *form
== '-')
83 die("missing argument to %s", argp
[-2]);
87 if (!(format
= *argp
++) || *format
== '-')
88 die("missing argument to %s", argp
[-2]);
95 die("only one form at a time!");
100 * Get new format string. Must be before chdir().
102 nfs
= new_fs (form
, format
, FORMAT
);
103 (void) fmt_compile(nfs
, &fmt
, 1);
114 fmt_dump (struct format
*fmth
)
117 struct format
*fmt
, *addr
;
120 for (fmt
= fmth
; fmt
; ++fmt
) {
130 addr
= fmt
+ fmt
->f_skip
;
131 if (findlabel(addr
) < 0)
134 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
139 for (fmt
= fmth
; fmt
; ++fmt
) {
141 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
147 dumpone(struct format
*fmt
)
151 if ((i
= findlabel(fmt
)) >= 0)
155 fputs(f_typestr((int)fmt
->f_type
), stdout
);
157 switch (fmt
->f_type
) {
163 fputs(", comp ", stdout
);
164 litputs(fmt
->f_comp
->c_name
);
165 if (fmt
->f_comp
->c_type
)
166 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
167 if (fmt
->f_comp
->c_flags
)
168 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
195 fputs(", c_name ", stdout
);
196 litputs(fmt
->f_comp
->c_name
);
197 if (fmt
->f_comp
->c_type
)
198 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
199 if (fmt
->f_comp
->c_flags
)
200 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
219 fputs(", c_name ", stdout
);
220 litputs(fmt
->f_comp
->c_name
);
221 if (fmt
->f_comp
->c_type
)
222 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
223 if (fmt
->f_comp
->c_flags
)
224 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
228 printf(", width %d, fill '", fmt
->f_width
);
229 litputc(fmt
->f_fill
);
230 fputs("' name ", stdout
);
231 litputs(fmt
->f_comp
->c_name
);
232 if (fmt
->f_comp
->c_type
)
233 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
234 if (fmt
->f_comp
->c_flags
)
235 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
240 printf(", width %d, fill '", fmt
->f_width
);
241 litputc(fmt
->f_fill
);
247 litputs(fmt
->f_text
);
251 printf(", width %d, fill '", fmt
->f_width
);
252 litputc(fmt
->f_fill
);
254 litputs(fmt
->f_text
);
260 litputc(fmt
->f_char
);
269 fputs(" continue else goto", stdout
);
272 i
= findlabel(fmt
+ fmt
->f_skip
);
279 i
= findlabel(fmt
+ fmt
->f_skip
);
280 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
289 case FT_LV_MULTIPLY_L
:
292 printf(" value %d", fmt
->f_value
);
296 fputs(" str ", stdout
);
297 litputs(fmt
->f_text
);
301 fputs(" getenv ", stdout
);
302 litputs(fmt
->f_text
);
305 case FT_LS_DECODECOMP
:
306 fputs(", comp ", stdout
);
307 litputs(fmt
->f_comp
->c_name
);
314 printf(", width %d", fmt
->f_width
);
318 printf(", value dat[%d]", fmt
->f_value
);
325 findlabel(struct format
*addr
)
329 for (i
= 0; i
< lused
; ++i
)
336 assignlabel(struct format
*addr
)
338 lvec
[lused
++] = addr
;
347 case FT_COMP
: return "COMP";
348 case FT_COMPF
: return "COMPF";
349 case FT_LIT
: return "LIT";
350 case FT_LITF
: return "LITF";
351 case FT_CHAR
: return "CHAR";
352 case FT_NUM
: return "NUM";
353 case FT_NUMF
: return "NUMF";
354 case FT_STR
: return "STR";
355 case FT_STRF
: return "STRF";
356 case FT_STRFW
: return "STRFW";
357 case FT_STRLIT
: return "STRLIT";
358 case FT_STRLITZ
: return "STRLITZ";
359 case FT_PUTADDR
: return "PUTADDR";
360 case FT_LS_COMP
: return "LS_COMP";
361 case FT_LS_LIT
: return "LS_LIT";
362 case FT_LS_GETENV
: return "LS_GETENV";
363 case FT_LS_CFIND
: return "LS_CFIND";
364 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
365 case FT_LS_DECODE
: return "LS_DECODE";
366 case FT_LS_TRIM
: return "LS_TRIM";
367 case FT_LV_COMP
: return "LV_COMP";
368 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
369 case FT_LV_LIT
: return "LV_LIT";
370 case FT_LV_DAT
: return "LV_DAT";
371 case FT_LV_STRLEN
: return "LV_STRLEN";
372 case FT_LV_PLUS_L
: return "LV_PLUS_L";
373 case FT_LV_MINUS_L
: return "LV_MINUS_L";
374 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
375 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
376 case FT_LV_MODULO_L
: return "LV_MODULO_L";
377 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
378 case FT_LS_MONTH
: return "LS_MONTH";
379 case FT_LS_LMONTH
: return "LS_LMONTH";
380 case FT_LS_ZONE
: return "LS_ZONE";
381 case FT_LS_DAY
: return "LS_DAY";
382 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
383 case FT_LS_822DATE
: return "LS_822DATE";
384 case FT_LS_PRETTY
: return "LS_PRETTY";
385 case FT_LS_KILO
: return "LS_KILO";
386 case FT_LS_KIBI
: return "LS_KIBI";
387 case FT_LV_SEC
: return "LV_SEC";
388 case FT_LV_MIN
: return "LV_MIN";
389 case FT_LV_HOUR
: return "LV_HOUR";
390 case FT_LV_MDAY
: return "LV_MDAY";
391 case FT_LV_MON
: return "LV_MON";
392 case FT_LV_YEAR
: return "LV_YEAR";
393 case FT_LV_YDAY
: return "LV_YDAY";
394 case FT_LV_WDAY
: return "LV_WDAY";
395 case FT_LV_ZONE
: return "LV_ZONE";
396 case FT_LV_CLOCK
: return "LV_CLOCK";
397 case FT_LV_RCLOCK
: return "LV_RCLOCK";
398 case FT_LV_DAYF
: return "LV_DAYF";
399 case FT_LV_DST
: return "LV_DST";
400 case FT_LV_ZONEF
: return "LV_ZONEF";
401 case FT_LS_PERS
: return "LS_PERS";
402 case FT_LS_MBOX
: return "LS_MBOX";
403 case FT_LS_HOST
: return "LS_HOST";
404 case FT_LS_PATH
: return "LS_PATH";
405 case FT_LS_GNAME
: return "LS_GNAME";
406 case FT_LS_NOTE
: return "LS_NOTE";
407 case FT_LS_ADDR
: return "LS_ADDR";
408 case FT_LS_822ADDR
: return "LS_822ADDR";
409 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
410 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
411 case FT_LV_INGRPF
: return "LV_INGRPF";
412 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
413 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
414 case FT_LOCALDATE
: return "LOCALDATE";
415 case FT_GMTDATE
: return "GMTDATE";
416 case FT_PARSEDATE
: return "PARSEDATE";
417 case FT_PARSEADDR
: return "PARSEADDR";
418 case FT_FORMATADDR
: return "FORMATADDR";
419 case FT_CONCATADDR
: return "CONCATADDR";
420 case FT_MYMBOX
: return "MYMBOX";
421 case FT_GETMYMBOX
: return "GETMYMBOX";
422 case FT_GETMYADDR
: return "GETMYADDR";
423 case FT_SAVESTR
: return "SAVESTR";
424 case FT_DONE
: return "DONE";
425 case FT_PAUSE
: return "PAUSE";
426 case FT_NOP
: return "NOP";
427 case FT_GOTO
: return "GOTO";
428 case FT_IF_S_NULL
: return "IF_S_NULL";
429 case FT_IF_S
: return "IF_S";
430 case FT_IF_V_EQ
: return "IF_V_EQ";
431 case FT_IF_V_NE
: return "IF_V_NE";
432 case FT_IF_V_GT
: return "IF_V_GT";
433 case FT_IF_MATCH
: return "IF_MATCH";
434 case FT_IF_AMATCH
: return "IF_AMATCH";
435 case FT_S_NULL
: return "S_NULL";
436 case FT_S_NONNULL
: return "S_NONNULL";
437 case FT_V_EQ
: return "V_EQ";
438 case FT_V_NE
: return "V_NE";
439 case FT_V_GT
: return "V_GT";
440 case FT_V_MATCH
: return "V_MATCH";
441 case FT_V_AMATCH
: return "V_AMATCH";
443 snprintf(buf
, sizeof buf
, "/* ??? #%d */", t
);
448 #define FNORD(v, s) if (t & (v)) { \
460 if (t
& ~(CT_ADDR
|CT_DATE
))
461 snprintf(buf
, sizeof buf
, "0x%x ", t
);
464 FNORD(CT_ADDR
, "ADDR");
465 FNORD(CT_DATE
, "DATE");
477 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
478 snprintf(buf
, sizeof buf
, "0x%x ", t
);
481 FNORD(CF_TRUE
, "TRUE");
482 FNORD(CF_PARSED
, "PARSED");
483 FNORD(CF_DATEFAB
, "DATEFAB");
484 FNORD(CF_TRIMMED
, "TRIMMED");
499 fputs("<nil>", stdout
);
510 if (c
< 0x20 || c
== 0177) {
514 } else if (c
== '\f') {
517 } else if (c
== '\n') {
520 } else if (c
== '\r') {
523 } else if (c
== '\t') {
528 putchar(c
^ 0x40); /* DEL to ?, others to alpha */