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