]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
Makefile.am: Add test/inc/test-eom-align to XFAIL_TESTS.
[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 #ifdef FT_LIT_FORCE
242 case FT_LIT_FORCE:
243 #endif
244 putchar(' ');
245 litputs(fmt->f_text);
246 break;
247
248 case FT_LITF:
249 printf(", width %d, fill '", fmt->f_width);
250 litputc(fmt->f_fill);
251 printf("' ");
252 litputs(fmt->f_text);
253 break;
254
255 case FT_CHAR:
256 putchar(' ');
257 putchar('\'');
258 litputc(fmt->f_char);
259 putchar('\'');
260 break;
261
262
263 case FT_IF_S:
264 case FT_IF_S_NULL:
265 case FT_IF_MATCH:
266 case FT_IF_AMATCH:
267 printf(" continue else goto");
268 /* FALLTHRU */
269 case FT_GOTO:
270 i = findlabel(fmt + fmt->f_skip);
271 printf(" L%d", i);
272 break;
273
274 case FT_IF_V_EQ:
275 case FT_IF_V_NE:
276 case FT_IF_V_GT:
277 i = findlabel(fmt + fmt->f_skip);
278 printf(" %d continue else goto L%d", fmt->f_value, i);
279 break;
280
281 case FT_V_EQ:
282 case FT_V_NE:
283 case FT_V_GT:
284 case FT_LV_LIT:
285 case FT_LV_PLUS_L:
286 case FT_LV_MINUS_L:
287 case FT_LV_MULTIPLY_L:
288 case FT_LV_DIVIDE_L:
289 case FT_LV_MODULO_L:
290 printf(" value %d", fmt->f_value);
291 break;
292
293 case FT_LS_LIT:
294 printf(" str ");
295 litputs(fmt->f_text);
296 break;
297
298 case FT_LS_GETENV:
299 printf(" getenv ");
300 litputs(fmt->f_text);
301 break;
302
303 case FT_LS_DECODECOMP:
304 printf(", comp ");
305 litputs(fmt->f_comp->c_name);
306 break;
307
308 case FT_LS_DECODE:
309 break;
310
311 case FT_LS_TRIM:
312 printf(", width %d", fmt->f_width);
313 break;
314
315 case FT_LV_DAT:
316 printf(", value dat[%d]", fmt->f_value);
317 break;
318 }
319 putchar('\n');
320 }
321
322 static int
323 findlabel(struct format *addr)
324 {
325 int i;
326
327 for (i = 0; i < lused; ++i)
328 if (addr == lvec[i])
329 return(i);
330 return(-1);
331 }
332
333 static void
334 assignlabel(struct format *addr)
335 {
336 lvec[lused++] = addr;
337 }
338
339 static char *
340 f_typestr(int t)
341 {
342 static char buf[32];
343
344 switch (t) {
345 case FT_COMP: return("COMP");
346 case FT_COMPF: return("COMPF");
347 case FT_LIT: return("LIT");
348 case FT_LITF: return("LITF");
349 #ifdef FT_LIT_FORCE
350 case FT_LIT_FORCE: return("LIT_FORCE");
351 #endif
352 case FT_CHAR: return("CHAR");
353 case FT_NUM: return("NUM");
354 case FT_NUMF: return("NUMF");
355 case FT_STR: return("STR");
356 case FT_STRF: return("STRF");
357 case FT_STRFW: return("STRFW");
358 case FT_PUTADDR: return("PUTADDR");
359 case FT_STRLIT: return("STRLIT");
360 case FT_STRLITZ: return("STRLITZ");
361 case FT_LS_COMP: return("LS_COMP");
362 case FT_LS_LIT: return("LS_LIT");
363 case FT_LS_GETENV: return("LS_GETENV");
364 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
365 case FT_LS_DECODE: return("FT_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_KILO: return("LS_KILO");
379 case FT_LS_KIBI: return("LS_KIBI");
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_GETMYMBOX: return("GETMYMBOX");
421 case FT_GETMYADDR: return("GETMYADDR");
422 case FT_SAVESTR: return("SAVESTR");
423 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");
441 default:
442 printf(buf, "/* ??? #%d */", t);
443 return(buf);
444 }
445 }
446
447 #define FNORD(v, s) if (t & (v)) { \
448 if (i++ > 0) \
449 strcat(buf, "|"); \
450 strcat(buf, s); }
451
452 static char *
453 c_typestr(int t)
454 {
455 int i;
456 static char buf[64];
457
458 buf[0] = '\0';
459 if (t & ~(CT_ADDR|CT_DATE))
460 printf(buf, "0x%x ", t);
461 strcat(buf, "<");
462 i = 0;
463 FNORD(CT_ADDR, "ADDR");
464 FNORD(CT_DATE, "DATE");
465 strcat(buf, ">");
466 return(buf);
467 }
468
469 static char *
470 c_flagsstr(int t)
471 {
472 int i;
473 static char buf[64];
474
475 buf[0] = '\0';
476 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED))
477 printf(buf, "0x%x ", t);
478 strcat(buf, "<");
479 i = 0;
480 FNORD(CF_TRUE, "TRUE");
481 FNORD(CF_PARSED, "PARSED");
482 FNORD(CF_DATEFAB, "DATEFAB");
483 FNORD(CF_TRIMMED, "TRIMMED");
484 strcat(buf, ">");
485 return(buf);
486 }
487 #undef FNORD
488
489 static void
490 litputs(char *s)
491 {
492 if (s) {
493 putchar('"');
494 while (*s)
495 litputc(*s++);
496 putchar('"');
497 } else
498 fputs("<nil>", stdout);
499 }
500
501 static void
502 litputc(char c)
503 {
504 if (c & ~ 0177) {
505 putchar('M');
506 putchar('-');
507 c &= 0177;
508 }
509 if (c < 0x20 || c == 0177) {
510 if (c == '\b') {
511 putchar('\\');
512 putchar('b');
513 } else if (c == '\f') {
514 putchar('\\');
515 putchar('f');
516 } else if (c == '\n') {
517 putchar('\\');
518 putchar('n');
519 } else if (c == '\r') {
520 putchar('\\');
521 putchar('r');
522 } else if (c == '\t') {
523 putchar('\\');
524 putchar('t');
525 } else {
526 putchar('^');
527 putchar(c ^ 0x40); /* DEL to ?, others to alpha */
528 }
529 } else
530 putchar(c);
531 }