]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
b5a4660c94da3c91e08fccf9ccbeedc3b5d83487
[nmh] / uip / fmtdump.c
1 /* fmtdump.c -- compile format file and dump out instructions
2 *
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.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/fmt_new.h"
10 #include "scansbr.h"
11 #include "sbr/getarguments.h"
12 #include "sbr/smatch.h"
13 #include "sbr/ambigsw.h"
14 #include "sbr/print_version.h"
15 #include "sbr/print_help.h"
16 #include "sbr/error.h"
17 #include "h/fmt_scan.h"
18 #include "h/fmt_compile.h"
19 #include "h/done.h"
20 #include "h/utils.h"
21
22 #define FMTDUMP_SWITCHES \
23 X("form formatfile", 0, FORMSW) \
24 X("format string", 5, FMTSW) \
25 X("version", 0, VERSIONSW) \
26 X("help", 0, HELPSW) \
27
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(FMTDUMP);
30 #undef X
31
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(FMTDUMP, switches);
34 #undef X
35
36 /* for assignlabel */
37 static struct format *lvec[128];
38 static int lused = 0;
39
40 /*
41 * static prototypes
42 */
43 static void fmt_dump (struct format *);
44 static void dumpone(struct format *);
45 static int findlabel(struct format *);
46 static void assignlabel(struct format *);
47 static char *f_typestr(int);
48 static char *c_typestr(int);
49 static char *c_flagsstr(int);
50 static void litputs(char *);
51 static void litputc(char);
52
53
54 int
55 main (int argc, char **argv)
56 {
57 char *cp, *form = NULL, *format = NULL;
58 char buf[BUFSIZ], *nfs, **argp, **arguments;
59 struct format *fmt;
60
61 if (nmh_init(argv[0], true, false)) { return 1; }
62
63 arguments = getarguments (invo_name, argc, argv, 1);
64 argp = arguments;
65
66 while ((cp = *argp++)) {
67 if (*cp == '-') {
68 switch (smatch (++cp, switches)) {
69 case AMBIGSW:
70 ambigsw (cp, switches);
71 done (1);
72 case UNKWNSW:
73 die("-%s unknown", cp);
74
75 case HELPSW:
76 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
77 print_help (buf, switches, 1);
78 done (0);
79 case VERSIONSW:
80 print_version(invo_name);
81 done (0);
82
83 case FORMSW:
84 if (!(form = *argp++) || *form == '-')
85 die("missing argument to %s", argp[-2]);
86 format = NULL;
87 continue;
88 case FMTSW:
89 if (!(format = *argp++) || *format == '-')
90 die("missing argument to %s", argp[-2]);
91 form = NULL;
92 continue;
93
94 }
95 }
96 if (form)
97 die("only one form at a time!");
98 form = cp;
99 }
100
101 /*
102 * Get new format string. Must be before chdir().
103 */
104 nfs = new_fs (form, format, FORMAT);
105 (void) fmt_compile(nfs, &fmt, 1);
106
107 fmt_dump(fmt);
108
109 fmt_free(fmt, 1);
110
111 done(0);
112 return 1;
113 }
114
115 static void
116 fmt_dump (struct format *fmth)
117 {
118 int i;
119 struct format *fmt, *addr;
120
121 /* Assign labels */
122 for (fmt = fmth; fmt; ++fmt) {
123 i = fmt->f_type;
124 if (i == FT_IF_S ||
125 i == FT_IF_S_NULL ||
126 i == FT_IF_V_EQ ||
127 i == FT_IF_V_NE ||
128 i == FT_IF_V_GT ||
129 i == FT_IF_MATCH ||
130 i == FT_IF_AMATCH ||
131 i == FT_GOTO) {
132 addr = fmt + fmt->f_skip;
133 if (findlabel(addr) < 0)
134 assignlabel(addr);
135 }
136 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
137 break;
138 }
139
140 /* Dump them out! */
141 for (fmt = fmth; fmt; ++fmt) {
142 dumpone(fmt);
143 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
144 break;
145 }
146 }
147
148 static void
149 dumpone(struct format *fmt)
150 {
151 int i;
152
153 if ((i = findlabel(fmt)) >= 0)
154 printf("L%d:", i);
155 putchar('\t');
156
157 fputs(f_typestr((int)fmt->f_type), stdout);
158
159 switch (fmt->f_type) {
160
161 case FT_COMP:
162 case FT_LS_COMP:
163 case FT_LV_COMPFLAG:
164 case FT_LV_COMP:
165 fputs(", comp ", stdout);
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));
171 break;
172
173 case FT_LV_SEC:
174 case FT_LV_MIN:
175 case FT_LV_HOUR:
176 case FT_LV_MDAY:
177 case FT_LV_MON:
178 case FT_LS_MONTH:
179 case FT_LS_LMONTH:
180 case FT_LS_ZONE:
181 case FT_LV_YEAR:
182 case FT_LV_WDAY:
183 case FT_LS_DAY:
184 case FT_LS_WEEKDAY:
185 case FT_LV_YDAY:
186 case FT_LV_ZONE:
187 case FT_LV_CLOCK:
188 case FT_LV_RCLOCK:
189 case FT_LV_DAYF:
190 case FT_LV_ZONEF:
191 case FT_LV_DST:
192 case FT_LS_822DATE:
193 case FT_LS_PRETTY:
194 case FT_LOCALDATE:
195 case FT_GMTDATE:
196 case FT_PARSEDATE:
197 fputs(", c_name ", stdout);
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));
203 break;
204
205 case FT_LS_ADDR:
206 case FT_LS_PERS:
207 case FT_LS_MBOX:
208 case FT_LS_HOST:
209 case FT_LS_PATH:
210 case FT_LS_GNAME:
211 case FT_LS_NOTE:
212 case FT_LS_822ADDR:
213 case FT_LV_HOSTTYPE:
214 case FT_LV_INGRPF:
215 case FT_LV_NOHOSTF:
216 case FT_LS_FRIENDLY:
217 case FT_PARSEADDR:
218 case FT_MYMBOX:
219 case FT_GETMYMBOX:
220 case FT_GETMYADDR:
221 fputs(", c_name ", stdout);
222 litputs(fmt->f_comp->c_name);
223 if (fmt->f_comp->c_type)
224 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
225 if (fmt->f_comp->c_flags)
226 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
227 break;
228
229 case FT_COMPF:
230 printf(", width %d, fill '", fmt->f_width);
231 litputc(fmt->f_fill);
232 fputs("' name ", stdout);
233 litputs(fmt->f_comp->c_name);
234 if (fmt->f_comp->c_type)
235 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
236 if (fmt->f_comp->c_flags)
237 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
238 break;
239
240 case FT_STRF:
241 case FT_NUMF:
242 printf(", width %d, fill '", fmt->f_width);
243 litputc(fmt->f_fill);
244 putchar('\'');
245 break;
246
247 case FT_LIT:
248 putchar(' ');
249 litputs(fmt->f_text);
250 break;
251
252 case FT_LITF:
253 printf(", width %d, fill '", fmt->f_width);
254 litputc(fmt->f_fill);
255 fputs("' ", stdout);
256 litputs(fmt->f_text);
257 break;
258
259 case FT_CHAR:
260 putchar(' ');
261 putchar('\'');
262 litputc(fmt->f_char);
263 putchar('\'');
264 break;
265
266
267 case FT_IF_S:
268 case FT_IF_S_NULL:
269 case FT_IF_MATCH:
270 case FT_IF_AMATCH:
271 fputs(" continue else goto", stdout);
272 /* FALLTHRU */
273 case FT_GOTO:
274 i = findlabel(fmt + fmt->f_skip);
275 printf(" L%d", i);
276 break;
277
278 case FT_IF_V_EQ:
279 case FT_IF_V_NE:
280 case FT_IF_V_GT:
281 i = findlabel(fmt + fmt->f_skip);
282 printf(" %d continue else goto L%d", fmt->f_value, i);
283 break;
284
285 case FT_V_EQ:
286 case FT_V_NE:
287 case FT_V_GT:
288 case FT_LV_LIT:
289 case FT_LV_PLUS_L:
290 case FT_LV_MINUS_L:
291 case FT_LV_MULTIPLY_L:
292 case FT_LV_DIVIDE_L:
293 case FT_LV_MODULO_L:
294 printf(" value %d", fmt->f_value);
295 break;
296
297 case FT_LS_LIT:
298 fputs(" str ", stdout);
299 litputs(fmt->f_text);
300 break;
301
302 case FT_LS_GETENV:
303 fputs(" getenv ", stdout);
304 litputs(fmt->f_text);
305 break;
306
307 case FT_LS_DECODECOMP:
308 fputs(", comp ", stdout);
309 litputs(fmt->f_comp->c_name);
310 break;
311
312 case FT_LS_DECODE:
313 break;
314
315 case FT_LS_TRIM:
316 printf(", width %d", fmt->f_width);
317 break;
318
319 case FT_LV_DAT:
320 printf(", value dat[%d]", fmt->f_value);
321 break;
322 }
323 putchar('\n');
324 }
325
326 static int
327 findlabel(struct format *addr)
328 {
329 int i;
330
331 for (i = 0; i < lused; ++i)
332 if (addr == lvec[i])
333 return i;
334 return -1;
335 }
336
337 static void
338 assignlabel(struct format *addr)
339 {
340 lvec[lused++] = addr;
341 }
342
343 static char *
344 f_typestr(int t)
345 {
346 static char buf[32];
347
348 switch (t) {
349 case FT_COMP: return "COMP";
350 case FT_COMPF: return "COMPF";
351 case FT_LIT: return "LIT";
352 case FT_LITF: return "LITF";
353 case FT_CHAR: return "CHAR";
354 case FT_NUM: return "NUM";
355 case FT_NUMF: return "NUMF";
356 case FT_STR: return "STR";
357 case FT_STRF: return "STRF";
358 case FT_STRFW: return "STRFW";
359 case FT_STRLIT: return "STRLIT";
360 case FT_STRLITZ: return "STRLITZ";
361 case FT_PUTADDR: return "PUTADDR";
362 case FT_LS_COMP: return "LS_COMP";
363 case FT_LS_LIT: return "LS_LIT";
364 case FT_LS_GETENV: return "LS_GETENV";
365 case FT_LS_CFIND: return "LS_CFIND";
366 case FT_LS_DECODECOMP: return "LS_DECODECOMP";
367 case FT_LS_DECODE: return "LS_DECODE";
368 case FT_LS_TRIM: return "LS_TRIM";
369 case FT_LV_COMP: return "LV_COMP";
370 case FT_LV_COMPFLAG: return "LV_COMPFLAG";
371 case FT_LV_LIT: return "LV_LIT";
372 case FT_LV_DAT: return "LV_DAT";
373 case FT_LV_STRLEN: return "LV_STRLEN";
374 case FT_LV_PLUS_L: return "LV_PLUS_L";
375 case FT_LV_MINUS_L: return "LV_MINUS_L";
376 case FT_LV_MULTIPLY_L: return "LV_MULTIPLY_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_LS_KILO: return "LS_KILO";
388 case FT_LS_KIBI: return "LS_KIBI";
389 case FT_LV_SEC: return "LV_SEC";
390 case FT_LV_MIN: return "LV_MIN";
391 case FT_LV_HOUR: return "LV_HOUR";
392 case FT_LV_MDAY: return "LV_MDAY";
393 case FT_LV_MON: return "LV_MON";
394 case FT_LV_YEAR: return "LV_YEAR";
395 case FT_LV_YDAY: return "LV_YDAY";
396 case FT_LV_WDAY: return "LV_WDAY";
397 case FT_LV_ZONE: return "LV_ZONE";
398 case FT_LV_CLOCK: return "LV_CLOCK";
399 case FT_LV_RCLOCK: return "LV_RCLOCK";
400 case FT_LV_DAYF: return "LV_DAYF";
401 case FT_LV_DST: return "LV_DST";
402 case FT_LV_ZONEF: return "LV_ZONEF";
403 case FT_LS_PERS: return "LS_PERS";
404 case FT_LS_MBOX: return "LS_MBOX";
405 case FT_LS_HOST: return "LS_HOST";
406 case FT_LS_PATH: return "LS_PATH";
407 case FT_LS_GNAME: return "LS_GNAME";
408 case FT_LS_NOTE: return "LS_NOTE";
409 case FT_LS_ADDR: return "LS_ADDR";
410 case FT_LS_822ADDR: return "LS_822ADDR";
411 case FT_LS_FRIENDLY: return "LS_FRIENDLY";
412 case FT_LV_HOSTTYPE: return "LV_HOSTTYPE";
413 case FT_LV_INGRPF: return "LV_INGRPF";
414 case FT_LS_UNQUOTE: return "LS_UNQUOTE";
415 case FT_LV_NOHOSTF: return "LV_NOHOSTF";
416 case FT_LOCALDATE: return "LOCALDATE";
417 case FT_GMTDATE: return "GMTDATE";
418 case FT_PARSEDATE: return "PARSEDATE";
419 case FT_PARSEADDR: return "PARSEADDR";
420 case FT_FORMATADDR: return "FORMATADDR";
421 case FT_CONCATADDR: return "CONCATADDR";
422 case FT_MYMBOX: return "MYMBOX";
423 case FT_GETMYMBOX: return "GETMYMBOX";
424 case FT_GETMYADDR: return "GETMYADDR";
425 case FT_SAVESTR: return "SAVESTR";
426 case FT_DONE: return "DONE";
427 case FT_PAUSE: return "PAUSE";
428 case FT_NOP: return "NOP";
429 case FT_GOTO: return "GOTO";
430 case FT_IF_S_NULL: return "IF_S_NULL";
431 case FT_IF_S: return "IF_S";
432 case FT_IF_V_EQ: return "IF_V_EQ";
433 case FT_IF_V_NE: return "IF_V_NE";
434 case FT_IF_V_GT: return "IF_V_GT";
435 case FT_IF_MATCH: return "IF_MATCH";
436 case FT_IF_AMATCH: return "IF_AMATCH";
437 case FT_S_NULL: return "S_NULL";
438 case FT_S_NONNULL: return "S_NONNULL";
439 case FT_V_EQ: return "V_EQ";
440 case FT_V_NE: return "V_NE";
441 case FT_V_GT: return "V_GT";
442 case FT_V_MATCH: return "V_MATCH";
443 case FT_V_AMATCH: return "V_AMATCH";
444 default:
445 snprintf(buf, sizeof buf, "/* ??? #%d */", t);
446 return buf;
447 }
448 }
449
450 #define FNORD(v, s) if (t & (v)) { \
451 if (i++ > 0) \
452 strcat(buf, "|"); \
453 strcat(buf, s); }
454
455 static char *
456 c_typestr(int t)
457 {
458 int i;
459 static char buf[64];
460
461 buf[0] = '\0';
462 if (t & ~(CT_ADDR|CT_DATE))
463 snprintf(buf, sizeof buf, "0x%x ", t);
464 strcat(buf, "<");
465 i = 0;
466 FNORD(CT_ADDR, "ADDR");
467 FNORD(CT_DATE, "DATE");
468 strcat(buf, ">");
469 return buf;
470 }
471
472 static char *
473 c_flagsstr(int t)
474 {
475 int i;
476 static char buf[64];
477
478 buf[0] = '\0';
479 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED))
480 snprintf(buf, sizeof buf, "0x%x ", t);
481 strcat(buf, "<");
482 i = 0;
483 FNORD(CF_TRUE, "TRUE");
484 FNORD(CF_PARSED, "PARSED");
485 FNORD(CF_DATEFAB, "DATEFAB");
486 FNORD(CF_TRIMMED, "TRIMMED");
487 strcat(buf, ">");
488 return buf;
489 }
490 #undef FNORD
491
492 static void
493 litputs(char *s)
494 {
495 if (s) {
496 putchar('"');
497 while (*s)
498 litputc(*s++);
499 putchar('"');
500 } else
501 fputs("<nil>", stdout);
502 }
503
504 static void
505 litputc(char c)
506 {
507 if (c & ~ 0177) {
508 putchar('M');
509 putchar('-');
510 c &= 0177;
511 }
512 if (c < 0x20 || c == 0177) {
513 if (c == '\b') {
514 putchar('\\');
515 putchar('b');
516 } else if (c == '\f') {
517 putchar('\\');
518 putchar('f');
519 } else if (c == '\n') {
520 putchar('\\');
521 putchar('n');
522 } else if (c == '\r') {
523 putchar('\\');
524 putchar('r');
525 } else if (c == '\t') {
526 putchar('\\');
527 putchar('t');
528 } else {
529 putchar('^');
530 putchar(c ^ 0x40); /* DEL to ?, others to alpha */
531 }
532 } else
533 putchar(c);
534 }