]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
change mhlist to use decimal math when abbreviating sizes
[nmh] / uip / fmtdump.c
1
2 /*
3 * fmtdump.c -- compile format file and dump out instructions
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/fmt_scan.h>
12 #include <h/fmt_compile.h>
13 #include <h/scansbr.h>
14
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) \
20
21 #define X(sw, minchars, id) id,
22 DEFINE_SWITCH_ENUM(FMTDUMP);
23 #undef X
24
25 #define X(sw, minchars, id) { sw, minchars, id },
26 DEFINE_SWITCH_ARRAY(FMTDUMP, switches);
27 #undef X
28
29 /* for assignlabel */
30 static struct format *lvec[128];
31 static int lused = 0;
32
33 /*
34 * static prototypes
35 */
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);
45
46
47 int
48 main (int argc, char **argv)
49 {
50 char *cp, *form = NULL, *format = NULL;
51 char buf[BUFSIZ], *nfs, **argp, **arguments;
52 struct format *fmt;
53
54 if (nmh_init(argv[0], 1)) { return 1; }
55
56 arguments = getarguments (invo_name, argc, argv, 1);
57 argp = arguments;
58
59 while ((cp = *argp++)) {
60 if (*cp == '-') {
61 switch (smatch (++cp, switches)) {
62 case AMBIGSW:
63 ambigsw (cp, switches);
64 done (1);
65 case UNKWNSW:
66 adios (NULL, "-%s unknown", cp);
67
68 case HELPSW:
69 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
70 print_help (buf, switches, 1);
71 done (0);
72 case VERSIONSW:
73 print_version(invo_name);
74 done (0);
75
76 case FORMSW:
77 if (!(form = *argp++) || *form == '-')
78 adios (NULL, "missing argument to %s", argp[-2]);
79 format = NULL;
80 continue;
81 case FMTSW:
82 if (!(format = *argp++) || *format == '-')
83 adios (NULL, "missing argument to %s", argp[-2]);
84 form = NULL;
85 continue;
86
87 }
88 }
89 if (form)
90 adios (NULL, "only one form at a time!");
91 else
92 form = cp;
93 }
94
95 /*
96 * Get new format string. Must be before chdir().
97 */
98 nfs = new_fs (form, format, FORMAT);
99 (void) fmt_compile(nfs, &fmt, 1);
100
101 fmt_dump(fmt);
102
103 fmt_free(fmt, 1);
104
105 done(0);
106 return 1;
107 }
108
109 static void
110 fmt_dump (struct format *fmth)
111 {
112 int i;
113 register struct format *fmt, *addr;
114
115 /* Assign labels */
116 for (fmt = fmth; fmt; ++fmt) {
117 i = fmt->f_type;
118 if (i == FT_IF_S ||
119 i == FT_IF_S_NULL ||
120 i == FT_IF_V_EQ ||
121 i == FT_IF_V_NE ||
122 i == FT_IF_V_GT ||
123 i == FT_IF_MATCH ||
124 i == FT_IF_AMATCH ||
125 i == FT_GOTO) {
126 addr = fmt + fmt->f_skip;
127 if (findlabel(addr) < 0)
128 assignlabel(addr);
129 }
130 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
131 break;
132 }
133
134 /* Dump them out! */
135 for (fmt = fmth; fmt; ++fmt) {
136 dumpone(fmt);
137 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
138 break;
139 }
140 }
141
142 static void
143 dumpone(struct format *fmt)
144 {
145 register int i;
146
147 if ((i = findlabel(fmt)) >= 0)
148 printf("L%d:", i);
149 putchar('\t');
150
151 fputs(f_typestr((int)fmt->f_type), stdout);
152
153 switch (fmt->f_type) {
154
155 case FT_COMP:
156 case FT_LS_COMP:
157 case FT_LV_COMPFLAG:
158 case FT_LV_COMP:
159 printf(", comp ");
160 litputs(fmt->f_comp->c_name);
161 if (fmt->f_comp->c_type)
162 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
163 if (fmt->f_comp->c_flags)
164 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
165 break;
166
167 case FT_LV_SEC:
168 case FT_LV_MIN:
169 case FT_LV_HOUR:
170 case FT_LV_MDAY:
171 case FT_LV_MON:
172 case FT_LS_MONTH:
173 case FT_LS_LMONTH:
174 case FT_LS_ZONE:
175 case FT_LV_YEAR:
176 case FT_LV_WDAY:
177 case FT_LS_DAY:
178 case FT_LS_WEEKDAY:
179 case FT_LV_YDAY:
180 case FT_LV_ZONE:
181 case FT_LV_CLOCK:
182 case FT_LV_RCLOCK:
183 case FT_LV_DAYF:
184 case FT_LV_ZONEF:
185 case FT_LV_DST:
186 case FT_LS_822DATE:
187 case FT_LS_PRETTY:
188 case FT_LOCALDATE:
189 case FT_GMTDATE:
190 case FT_PARSEDATE:
191 printf(", c_name ");
192 litputs(fmt->f_comp->c_name);
193 if (fmt->f_comp->c_type)
194 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
195 if (fmt->f_comp->c_flags)
196 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
197 break;
198
199 case FT_LS_ADDR:
200 case FT_LS_PERS:
201 case FT_LS_MBOX:
202 case FT_LS_HOST:
203 case FT_LS_PATH:
204 case FT_LS_GNAME:
205 case FT_LS_NOTE:
206 case FT_LS_822ADDR:
207 case FT_LV_HOSTTYPE:
208 case FT_LV_INGRPF:
209 case FT_LV_NOHOSTF:
210 case FT_LS_FRIENDLY:
211 case FT_PARSEADDR:
212 case FT_MYMBOX:
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 case FT_GOTO:
268 i = findlabel(fmt + fmt->f_skip);
269 printf(" L%d", i);
270 break;
271
272 case FT_IF_V_EQ:
273 case FT_IF_V_NE:
274 case FT_IF_V_GT:
275 i = findlabel(fmt + fmt->f_skip);
276 printf(" %d continue else goto L%d", fmt->f_value, i);
277 break;
278
279 case FT_V_EQ:
280 case FT_V_NE:
281 case FT_V_GT:
282 case FT_LV_LIT:
283 case FT_LV_PLUS_L:
284 case FT_LV_MINUS_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 register 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 #ifdef FT_LIT_FORCE
347 case FT_LIT_FORCE: return("LIT_FORCE");
348 #endif
349 case FT_CHAR: return("CHAR");
350 case FT_NUM: return("NUM");
351 case FT_NUMF: return("NUMF");
352 case FT_STR: return("STR");
353 case FT_STRF: return("STRF");
354 case FT_STRFW: return("STRFW");
355 case FT_PUTADDR: return("PUTADDR");
356 case FT_STRLIT: return("STRLIT");
357 case FT_STRLITZ: return("STRLITZ");
358 case FT_LS_COMP: return("LS_COMP");
359 case FT_LS_LIT: return("LS_LIT");
360 case FT_LS_GETENV: return("LS_GETENV");
361 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
362 case FT_LS_DECODE: return("FT_LS_DECODE");
363 case FT_LS_TRIM: return("LS_TRIM");
364 case FT_LV_COMP: return("LV_COMP");
365 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
366 case FT_LV_LIT: return("LV_LIT");
367 case FT_LV_DAT: return("LV_DAT");
368 case FT_LV_STRLEN: return("LV_STRLEN");
369 case FT_LV_PLUS_L: return("LV_PLUS_L");
370 case FT_LV_MINUS_L: return("LV_MINUS_L");
371 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
372 case FT_LV_MODULO_L: return("LV_MODULO_L");
373 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
374 case FT_LS_KILO: return("LS_KILO");
375 case FT_LS_KIBI: return("LS_KIBI");
376 case FT_LS_MONTH: return("LS_MONTH");
377 case FT_LS_LMONTH: return("LS_LMONTH");
378 case FT_LS_ZONE: return("LS_ZONE");
379 case FT_LS_DAY: return("LS_DAY");
380 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
381 case FT_LS_822DATE: return("LS_822DATE");
382 case FT_LS_PRETTY: return("LS_PRETTY");
383 case FT_LV_SEC: return("LV_SEC");
384 case FT_LV_MIN: return("LV_MIN");
385 case FT_LV_HOUR: return("LV_HOUR");
386 case FT_LV_MDAY: return("LV_MDAY");
387 case FT_LV_MON: return("LV_MON");
388 case FT_LV_YEAR: return("LV_YEAR");
389 case FT_LV_YDAY: return("LV_YDAY");
390 case FT_LV_WDAY: return("LV_WDAY");
391 case FT_LV_ZONE: return("LV_ZONE");
392 case FT_LV_CLOCK: return("LV_CLOCK");
393 case FT_LV_RCLOCK: return("LV_RCLOCK");
394 case FT_LV_DAYF: return("LV_DAYF");
395 case FT_LV_DST: return("LV_DST");
396 case FT_LV_ZONEF: return("LV_ZONEF");
397 case FT_LS_ADDR: return("LS_ADDR");
398 case FT_LS_PERS: return("LS_PERS");
399 case FT_LS_MBOX: return("LS_MBOX");
400 case FT_LS_HOST: return("LS_HOST");
401 case FT_LS_PATH: return("LS_PATH");
402 case FT_LS_GNAME: return("LS_GNAME");
403 case FT_LS_NOTE: return("LS_NOTE");
404 case FT_LS_822ADDR: return("LS_822ADDR");
405 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
406 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
407 case FT_LV_INGRPF: return("LV_INGRPF");
408 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
409 case FT_LOCALDATE: return("LOCALDATE");
410 case FT_GMTDATE: return("GMTDATE");
411 case FT_PARSEDATE: return("PARSEDATE");
412 case FT_PARSEADDR: return("PARSEADDR");
413 case FT_FORMATADDR: return("FORMATADDR");
414 case FT_CONCATADDR: return("CONCATADDR");
415 case FT_MYMBOX: return("MYMBOX");
416 case FT_SAVESTR: return("SAVESTR");
417 #ifdef FT_PAUSE
418 case FT_PAUSE: return ("PAUSE");
419 #endif
420 case FT_DONE: return("DONE");
421 case FT_NOP: return("NOP");
422 case FT_GOTO: return("GOTO");
423 case FT_IF_S_NULL: return("IF_S_NULL");
424 case FT_IF_S: return("IF_S");
425 case FT_IF_V_EQ: return("IF_V_EQ");
426 case FT_IF_V_NE: return("IF_V_NE");
427 case FT_IF_V_GT: return("IF_V_GT");
428 case FT_IF_MATCH: return("IF_MATCH");
429 case FT_IF_AMATCH: return("IF_AMATCH");
430 case FT_S_NULL: return("S_NULL");
431 case FT_S_NONNULL: return("S_NONNULL");
432 case FT_V_EQ: return("V_EQ");
433 case FT_V_NE: return("V_NE");
434 case FT_V_GT: return("V_GT");
435 case FT_V_MATCH: return("V_MATCH");
436 case FT_V_AMATCH: return("V_AMATCH");
437 default:
438 printf(buf, "/* ??? #%d */", t);
439 return(buf);
440 }
441 }
442
443 #define FNORD(v, s) if (t & (v)) { \
444 if (i++ > 0) \
445 strcat(buf, "|"); \
446 strcat(buf, s); }
447
448 static char *
449 c_typestr(int t)
450 {
451 register int i;
452 static char buf[64];
453
454 buf[0] = '\0';
455 if (t & ~(CT_ADDR|CT_DATE))
456 printf(buf, "0x%x ", t);
457 strcat(buf, "<");
458 i = 0;
459 FNORD(CT_ADDR, "ADDR");
460 FNORD(CT_DATE, "DATE");
461 strcat(buf, ">");
462 return(buf);
463 }
464
465 static char *
466 c_flagsstr(int t)
467 {
468 register int i;
469 static char buf[64];
470
471 buf[0] = '\0';
472 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED))
473 printf(buf, "0x%x ", t);
474 strcat(buf, "<");
475 i = 0;
476 FNORD(CF_TRUE, "TRUE");
477 FNORD(CF_PARSED, "PARSED");
478 FNORD(CF_DATEFAB, "DATEFAB");
479 FNORD(CF_TRIMMED, "TRIMMED");
480 strcat(buf, ">");
481 return(buf);
482 }
483 #undef FNORD
484
485 static void
486 litputs(char *s)
487 {
488 if (s) {
489 putc('"', stdout);
490 while (*s)
491 litputc(*s++);
492 putc('"', stdout);
493 } else
494 fputs("<nil>", stdout);
495 }
496
497 static void
498 litputc(char c)
499 {
500 if (c & ~ 0177) {
501 putc('M', stdout);
502 putc('-', stdout);
503 c &= 0177;
504 }
505 if (c < 0x20 || c == 0177) {
506 if (c == '\b') {
507 putc('\\', stdout);
508 putc('b', stdout);
509 } else if (c == '\f') {
510 putc('\\', stdout);
511 putc('f', stdout);
512 } else if (c == '\n') {
513 putc('\\', stdout);
514 putc('n', stdout);
515 } else if (c == '\r') {
516 putc('\\', stdout);
517 putc('r', stdout);
518 } else if (c == '\t') {
519 putc('\\', stdout);
520 putc('t', stdout);
521 } else {
522 putc('^', stdout);
523 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
524 }
525 } else
526 putc(c, stdout);
527 }