]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
eeeb1461c6ed3f54e64ffa6f6b5928e9166c03a9
[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 <h/fmt_scan.h>
10 #include <h/fmt_compile.h>
11 #include <h/scansbr.h>
12 #include <h/utils.h>
13
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) \
19
20 #define X(sw, minchars, id) id,
21 DEFINE_SWITCH_ENUM(FMTDUMP);
22 #undef X
23
24 #define X(sw, minchars, id) { sw, minchars, id },
25 DEFINE_SWITCH_ARRAY(FMTDUMP, switches);
26 #undef X
27
28 /* for assignlabel */
29 static struct format *lvec[128];
30 static int lused = 0;
31
32 /*
33 * static prototypes
34 */
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);
44
45
46 int
47 main (int argc, char **argv)
48 {
49 char *cp, *form = NULL, *format = NULL;
50 char buf[BUFSIZ], *nfs, **argp, **arguments;
51 struct format *fmt;
52
53 if (nmh_init(argv[0], 2)) { return 1; }
54
55 arguments = getarguments (invo_name, argc, argv, 1);
56 argp = arguments;
57
58 while ((cp = *argp++)) {
59 if (*cp == '-') {
60 switch (smatch (++cp, switches)) {
61 case AMBIGSW:
62 ambigsw (cp, switches);
63 done (1);
64 case UNKWNSW:
65 adios (NULL, "-%s unknown", cp);
66
67 case HELPSW:
68 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
69 print_help (buf, switches, 1);
70 done (0);
71 case VERSIONSW:
72 print_version(invo_name);
73 done (0);
74
75 case FORMSW:
76 if (!(form = *argp++) || *form == '-')
77 adios (NULL, "missing argument to %s", argp[-2]);
78 format = NULL;
79 continue;
80 case FMTSW:
81 if (!(format = *argp++) || *format == '-')
82 adios (NULL, "missing argument to %s", argp[-2]);
83 form = NULL;
84 continue;
85
86 }
87 }
88 if (form)
89 adios (NULL, "only one form at a time!");
90 else
91 form = cp;
92 }
93
94 /*
95 * Get new format string. Must be before chdir().
96 */
97 nfs = new_fs (form, format, FORMAT);
98 (void) fmt_compile(nfs, &fmt, 1);
99
100 fmt_dump(fmt);
101
102 fmt_free(fmt, 1);
103
104 done(0);
105 return 1;
106 }
107
108 static void
109 fmt_dump (struct format *fmth)
110 {
111 int i;
112 struct format *fmt, *addr;
113
114 /* Assign labels */
115 for (fmt = fmth; fmt; ++fmt) {
116 i = fmt->f_type;
117 if (i == FT_IF_S ||
118 i == FT_IF_S_NULL ||
119 i == FT_IF_V_EQ ||
120 i == FT_IF_V_NE ||
121 i == FT_IF_V_GT ||
122 i == FT_IF_MATCH ||
123 i == FT_IF_AMATCH ||
124 i == FT_GOTO) {
125 addr = fmt + fmt->f_skip;
126 if (findlabel(addr) < 0)
127 assignlabel(addr);
128 }
129 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
130 break;
131 }
132
133 /* Dump them out! */
134 for (fmt = fmth; fmt; ++fmt) {
135 dumpone(fmt);
136 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
137 break;
138 }
139 }
140
141 static void
142 dumpone(struct format *fmt)
143 {
144 int i;
145
146 if ((i = findlabel(fmt)) >= 0)
147 printf("L%d:", i);
148 putchar('\t');
149
150 fputs(f_typestr((int)fmt->f_type), stdout);
151
152 switch (fmt->f_type) {
153
154 case FT_COMP:
155 case FT_LS_COMP:
156 case FT_LV_COMPFLAG:
157 case FT_LV_COMP:
158 printf(", comp ");
159 litputs(fmt->f_comp->c_name);
160 if (fmt->f_comp->c_type)
161 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
162 if (fmt->f_comp->c_flags)
163 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
164 break;
165
166 case FT_LV_SEC:
167 case FT_LV_MIN:
168 case FT_LV_HOUR:
169 case FT_LV_MDAY:
170 case FT_LV_MON:
171 case FT_LS_MONTH:
172 case FT_LS_LMONTH:
173 case FT_LS_ZONE:
174 case FT_LV_YEAR:
175 case FT_LV_WDAY:
176 case FT_LS_DAY:
177 case FT_LS_WEEKDAY:
178 case FT_LV_YDAY:
179 case FT_LV_ZONE:
180 case FT_LV_CLOCK:
181 case FT_LV_RCLOCK:
182 case FT_LV_DAYF:
183 case FT_LV_ZONEF:
184 case FT_LV_DST:
185 case FT_LS_822DATE:
186 case FT_LS_PRETTY:
187 case FT_LOCALDATE:
188 case FT_GMTDATE:
189 case FT_PARSEDATE:
190 printf(", c_name ");
191 litputs(fmt->f_comp->c_name);
192 if (fmt->f_comp->c_type)
193 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
194 if (fmt->f_comp->c_flags)
195 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
196 break;
197
198 case FT_LS_ADDR:
199 case FT_LS_PERS:
200 case FT_LS_MBOX:
201 case FT_LS_HOST:
202 case FT_LS_PATH:
203 case FT_LS_GNAME:
204 case FT_LS_NOTE:
205 case FT_LS_822ADDR:
206 case FT_LV_HOSTTYPE:
207 case FT_LV_INGRPF:
208 case FT_LV_NOHOSTF:
209 case FT_LS_FRIENDLY:
210 case FT_PARSEADDR:
211 case FT_MYMBOX:
212 case FT_GETMYMBOX:
213 case FT_GETMYADDR:
214 printf(", c_name ");
215 litputs(fmt->f_comp->c_name);
216 if (fmt->f_comp->c_type)
217 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
218 if (fmt->f_comp->c_flags)
219 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
220 break;
221
222 case FT_COMPF:
223 printf(", width %d, fill '", fmt->f_width);
224 litputc(fmt->f_fill);
225 printf("' name ");
226 litputs(fmt->f_comp->c_name);
227 if (fmt->f_comp->c_type)
228 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
229 if (fmt->f_comp->c_flags)
230 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
231 break;
232
233 case FT_STRF:
234 case FT_NUMF:
235 printf(", width %d, fill '", fmt->f_width);
236 litputc(fmt->f_fill);
237 putchar('\'');
238 break;
239
240 case FT_LIT:
241 putchar(' ');
242 litputs(fmt->f_text);
243 break;
244
245 case FT_LITF:
246 printf(", width %d, fill '", fmt->f_width);
247 litputc(fmt->f_fill);
248 printf("' ");
249 litputs(fmt->f_text);
250 break;
251
252 case FT_CHAR:
253 putchar(' ');
254 putchar('\'');
255 litputc(fmt->f_char);
256 putchar('\'');
257 break;
258
259
260 case FT_IF_S:
261 case FT_IF_S_NULL:
262 case FT_IF_MATCH:
263 case FT_IF_AMATCH:
264 printf(" continue else goto");
265 /* FALLTHRU */
266 case FT_GOTO:
267 i = findlabel(fmt + fmt->f_skip);
268 printf(" L%d", i);
269 break;
270
271 case FT_IF_V_EQ:
272 case FT_IF_V_NE:
273 case FT_IF_V_GT:
274 i = findlabel(fmt + fmt->f_skip);
275 printf(" %d continue else goto L%d", fmt->f_value, i);
276 break;
277
278 case FT_V_EQ:
279 case FT_V_NE:
280 case FT_V_GT:
281 case FT_LV_LIT:
282 case FT_LV_PLUS_L:
283 case FT_LV_MINUS_L:
284 case FT_LV_MULTIPLY_L:
285 case FT_LV_DIVIDE_L:
286 case FT_LV_MODULO_L:
287 printf(" value %d", fmt->f_value);
288 break;
289
290 case FT_LS_LIT:
291 printf(" str ");
292 litputs(fmt->f_text);
293 break;
294
295 case FT_LS_GETENV:
296 printf(" getenv ");
297 litputs(fmt->f_text);
298 break;
299
300 case FT_LS_DECODECOMP:
301 printf(", comp ");
302 litputs(fmt->f_comp->c_name);
303 break;
304
305 case FT_LS_DECODE:
306 break;
307
308 case FT_LS_TRIM:
309 printf(", width %d", fmt->f_width);
310 break;
311
312 case FT_LV_DAT:
313 printf(", value dat[%d]", fmt->f_value);
314 break;
315 }
316 putchar('\n');
317 }
318
319 static int
320 findlabel(struct format *addr)
321 {
322 int i;
323
324 for (i = 0; i < lused; ++i)
325 if (addr == lvec[i])
326 return(i);
327 return(-1);
328 }
329
330 static void
331 assignlabel(struct format *addr)
332 {
333 lvec[lused++] = addr;
334 }
335
336 static char *
337 f_typestr(int t)
338 {
339 static char buf[32];
340
341 switch (t) {
342 case FT_COMP: return("COMP");
343 case FT_COMPF: return("COMPF");
344 case FT_LIT: return("LIT");
345 case FT_LITF: return("LITF");
346 case FT_CHAR: return("CHAR");
347 case FT_NUM: return("NUM");
348 case FT_NUMF: return("NUMF");
349 case FT_STR: return("STR");
350 case FT_STRF: return("STRF");
351 case FT_STRFW: return("STRFW");
352 case FT_PUTADDR: return("PUTADDR");
353 case FT_STRLIT: return("STRLIT");
354 case FT_STRLITZ: return("STRLITZ");
355 case FT_LS_COMP: return("LS_COMP");
356 case FT_LS_LIT: return("LS_LIT");
357 case FT_LS_GETENV: return("LS_GETENV");
358 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
359 case FT_LS_DECODE: return("FT_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_KILO: return("LS_KILO");
373 case FT_LS_KIBI: return("LS_KIBI");
374 case FT_LS_MONTH: return("LS_MONTH");
375 case FT_LS_LMONTH: return("LS_LMONTH");
376 case FT_LS_ZONE: return("LS_ZONE");
377 case FT_LS_DAY: return("LS_DAY");
378 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
379 case FT_LS_822DATE: return("LS_822DATE");
380 case FT_LS_PRETTY: return("LS_PRETTY");
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_ADDR: return("LS_ADDR");
396 case FT_LS_PERS: return("LS_PERS");
397 case FT_LS_MBOX: return("LS_MBOX");
398 case FT_LS_HOST: return("LS_HOST");
399 case FT_LS_PATH: return("LS_PATH");
400 case FT_LS_GNAME: return("LS_GNAME");
401 case FT_LS_NOTE: return("LS_NOTE");
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_PAUSE: return ("PAUSE");
419 case FT_DONE: return("DONE");
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");
436 default:
437 snprintf(buf, sizeof buf, "/* ??? #%d */", t);
438 return(buf);
439 }
440 }
441
442 #define FNORD(v, s) if (t & (v)) { \
443 if (i++ > 0) \
444 strcat(buf, "|"); \
445 strcat(buf, s); }
446
447 static char *
448 c_typestr(int t)
449 {
450 int i;
451 static char buf[64];
452
453 buf[0] = '\0';
454 if (t & ~(CT_ADDR|CT_DATE))
455 snprintf(buf, sizeof buf, "0x%x ", t);
456 strcat(buf, "<");
457 i = 0;
458 FNORD(CT_ADDR, "ADDR");
459 FNORD(CT_DATE, "DATE");
460 strcat(buf, ">");
461 return(buf);
462 }
463
464 static char *
465 c_flagsstr(int t)
466 {
467 int i;
468 static char buf[64];
469
470 buf[0] = '\0';
471 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED))
472 snprintf(buf, sizeof buf, "0x%x ", t);
473 strcat(buf, "<");
474 i = 0;
475 FNORD(CF_TRUE, "TRUE");
476 FNORD(CF_PARSED, "PARSED");
477 FNORD(CF_DATEFAB, "DATEFAB");
478 FNORD(CF_TRIMMED, "TRIMMED");
479 strcat(buf, ">");
480 return(buf);
481 }
482 #undef FNORD
483
484 static void
485 litputs(char *s)
486 {
487 if (s) {
488 putchar('"');
489 while (*s)
490 litputc(*s++);
491 putchar('"');
492 } else
493 fputs("<nil>", stdout);
494 }
495
496 static void
497 litputc(char c)
498 {
499 if (c & ~ 0177) {
500 putchar('M');
501 putchar('-');
502 c &= 0177;
503 }
504 if (c < 0x20 || c == 0177) {
505 if (c == '\b') {
506 putchar('\\');
507 putchar('b');
508 } else if (c == '\f') {
509 putchar('\\');
510 putchar('f');
511 } else if (c == '\n') {
512 putchar('\\');
513 putchar('n');
514 } else if (c == '\r') {
515 putchar('\\');
516 putchar('r');
517 } else if (c == '\t') {
518 putchar('\\');
519 putchar('t');
520 } else {
521 putchar('^');
522 putchar(c ^ 0x40); /* DEL to ?, others to alpha */
523 }
524 } else
525 putchar(c);
526 }