]>
diplodocus.org Git - nmh/blob - uip/fmtdump.c
3 * fmtdump.c -- compile format file and dump out instructions
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/fmt_scan.h>
12 #include <h/fmt_compile.h>
13 #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
;
55 setlocale(LC_ALL
, "");
57 invo_name
= r1bindex (argv
[0], '/');
59 /* read user profile/context */
62 arguments
= getarguments (invo_name
, argc
, argv
, 1);
65 while ((cp
= *argp
++)) {
67 switch (smatch (++cp
, switches
)) {
69 ambigsw (cp
, switches
);
72 adios (NULL
, "-%s unknown", cp
);
75 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
76 print_help (buf
, switches
, 1);
79 print_version(invo_name
);
83 if (!(form
= *argp
++) || *form
== '-')
84 adios (NULL
, "missing argument to %s", argp
[-2]);
88 if (!(format
= *argp
++) || *format
== '-')
89 adios (NULL
, "missing argument to %s", argp
[-2]);
96 adios (NULL
, "only one form at a time!");
102 * Get new format string. Must be before chdir().
104 nfs
= new_fs (form
, format
, FORMAT
);
105 (void) fmt_compile(nfs
, &fmt
, 1);
116 fmt_dump (struct format
*fmth
)
119 register struct format
*fmt
, *addr
;
122 for (fmt
= fmth
; fmt
; ++fmt
) {
132 addr
= fmt
+ fmt
->f_skip
;
133 if (findlabel(addr
) < 0)
136 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
141 for (fmt
= fmth
; fmt
; ++fmt
) {
143 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
149 dumpone(struct format
*fmt
)
153 if ((i
= findlabel(fmt
)) >= 0)
157 fputs(f_typestr((int)fmt
->f_type
), stdout
);
159 switch (fmt
->f_type
) {
166 litputs(fmt
->f_comp
->c_name
);
167 if (fmt
->f_comp
->c_type
)
168 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
169 if (fmt
->f_comp
->c_flags
)
170 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
198 litputs(fmt
->f_comp
->c_name
);
199 if (fmt
->f_comp
->c_type
)
200 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
201 if (fmt
->f_comp
->c_flags
)
202 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
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
);
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
);
250 litputs(fmt
->f_text
);
254 printf(", width %d, fill '", fmt
->f_width
);
255 litputc(fmt
->f_fill
);
257 litputs(fmt
->f_text
);
263 litputc(fmt
->f_char
);
272 printf(" continue else goto");
274 i
= findlabel(fmt
+ fmt
->f_skip
);
281 i
= findlabel(fmt
+ fmt
->f_skip
);
282 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
293 printf(" value %d", fmt
->f_value
);
298 litputs(fmt
->f_text
);
303 litputs(fmt
->f_text
);
306 case FT_LS_DECODECOMP
:
308 litputs(fmt
->f_comp
->c_name
);
315 printf(", width %d", fmt
->f_width
);
319 printf(", value dat[%d]", fmt
->f_value
);
326 findlabel(struct format
*addr
)
330 for (i
= 0; i
< lused
; ++i
)
337 assignlabel(struct format
*addr
)
339 lvec
[lused
++] = addr
;
348 case FT_COMP
: return("COMP");
349 case FT_COMPF
: return("COMPF");
350 case FT_LIT
: return("LIT");
351 case FT_LITF
: return("LITF");
353 case FT_LIT_FORCE
: return("LIT_FORCE");
355 case FT_CHAR
: return("CHAR");
356 case FT_NUM
: return("NUM");
357 case FT_NUMF
: return("NUMF");
358 case FT_STR
: return("STR");
359 case FT_STRF
: return("STRF");
360 case FT_STRFW
: return("STRFW");
361 case FT_PUTADDR
: return("PUTADDR");
362 case FT_STRLIT
: return("STRLIT");
363 case FT_STRLITZ
: return("STRLITZ");
364 case FT_LS_COMP
: return("LS_COMP");
365 case FT_LS_LIT
: return("LS_LIT");
366 case FT_LS_GETENV
: return("LS_GETENV");
367 case FT_LS_DECODECOMP
: return("FT_LS_DECODECOMP");
368 case FT_LS_DECODE
: return("FT_LS_DECODE");
369 case FT_LS_TRIM
: return("LS_TRIM");
370 case FT_LV_COMP
: return("LV_COMP");
371 case FT_LV_COMPFLAG
: return("LV_COMPFLAG");
372 case FT_LV_LIT
: return("LV_LIT");
373 case FT_LV_DAT
: return("LV_DAT");
374 case FT_LV_STRLEN
: return("LV_STRLEN");
375 case FT_LV_PLUS_L
: return("LV_PLUS_L");
376 case FT_LV_MINUS_L
: return("LV_MINUS_L");
377 case FT_LV_DIVIDE_L
: return("LV_DIVIDE_L");
378 case FT_LV_MODULO_L
: return("LV_MODULO_L");
379 case FT_LV_CHAR_LEFT
: return("LV_CHAR_LEFT");
380 case FT_LS_MONTH
: return("LS_MONTH");
381 case FT_LS_LMONTH
: return("LS_LMONTH");
382 case FT_LS_ZONE
: return("LS_ZONE");
383 case FT_LS_DAY
: return("LS_DAY");
384 case FT_LS_WEEKDAY
: return("LS_WEEKDAY");
385 case FT_LS_822DATE
: return("LS_822DATE");
386 case FT_LS_PRETTY
: return("LS_PRETTY");
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_ADDR
: return("LS_ADDR");
402 case FT_LS_PERS
: return("LS_PERS");
403 case FT_LS_MBOX
: return("LS_MBOX");
404 case FT_LS_HOST
: return("LS_HOST");
405 case FT_LS_PATH
: return("LS_PATH");
406 case FT_LS_GNAME
: return("LS_GNAME");
407 case FT_LS_NOTE
: return("LS_NOTE");
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_LV_NOHOSTF
: return("LV_NOHOSTF");
413 case FT_LOCALDATE
: return("LOCALDATE");
414 case FT_GMTDATE
: return("GMTDATE");
415 case FT_PARSEDATE
: return("PARSEDATE");
416 case FT_PARSEADDR
: return("PARSEADDR");
417 case FT_FORMATADDR
: return("FORMATADDR");
418 case FT_CONCATADDR
: return("CONCATADDR");
419 case FT_MYMBOX
: return("MYMBOX");
420 case FT_SAVESTR
: return("SAVESTR");
422 case FT_PAUSE
: return ("PAUSE");
424 case FT_DONE
: return("DONE");
425 case FT_NOP
: return("NOP");
426 case FT_GOTO
: return("GOTO");
427 case FT_IF_S_NULL
: return("IF_S_NULL");
428 case FT_IF_S
: return("IF_S");
429 case FT_IF_V_EQ
: return("IF_V_EQ");
430 case FT_IF_V_NE
: return("IF_V_NE");
431 case FT_IF_V_GT
: return("IF_V_GT");
432 case FT_IF_MATCH
: return("IF_MATCH");
433 case FT_IF_AMATCH
: return("IF_AMATCH");
434 case FT_S_NULL
: return("S_NULL");
435 case FT_S_NONNULL
: return("S_NONNULL");
436 case FT_V_EQ
: return("V_EQ");
437 case FT_V_NE
: return("V_NE");
438 case FT_V_GT
: return("V_GT");
439 case FT_V_MATCH
: return("V_MATCH");
440 case FT_V_AMATCH
: return("V_AMATCH");
442 printf(buf
, "/* ??? #%d */", t
);
447 #define FNORD(v, s) if (t & (v)) { \
459 if (t
& ~(CT_ADDR
|CT_DATE
))
460 printf(buf
, "0x%x ", t
);
463 FNORD(CT_ADDR
, "ADDR");
464 FNORD(CT_DATE
, "DATE");
476 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
477 printf(buf
, "0x%x ", t
);
480 FNORD(CF_TRUE
, "TRUE");
481 FNORD(CF_PARSED
, "PARSED");
482 FNORD(CF_DATEFAB
, "DATEFAB");
483 FNORD(CF_TRIMMED
, "TRIMMED");
498 fputs("<nil>", stdout
);
509 if (c
< 0x20 || c
== 0177) {
513 } else if (c
== '\f') {
516 } else if (c
== '\n') {
519 } else if (c
== '\r') {
522 } else if (c
== '\t') {
527 putc(c
^ 0x40, stdout
); /* DEL to ?, others to alpha */