]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
We're not using the .Bu macro anymore.
[nmh] / uip / fmtdump.c
1
2 /*
3 * fmtdump.c -- compile format file and dump out instructions
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/fmt_scan.h>
14 #include <h/fmt_compile.h>
15 #include <h/scansbr.h>
16
17 static struct swit switches[] = {
18 #define FORMSW 0
19 { "form formatfile", 0 },
20 #define FMTSW 1
21 { "format string", 5 },
22 #define VERSIONSW 2
23 { "version", 0 },
24 #define HELPSW 3
25 { "help", 0 },
26 { NULL, 0 }
27 };
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 int ncomps;
51 char *cp, *form = NULL, *format = NULL;
52 char buf[BUFSIZ], *nfs, **argp, **arguments;
53 struct format *fmt;
54
55 #ifdef LOCALE
56 setlocale(LC_ALL, "");
57 #endif
58 invo_name = r1bindex (argv[0], '/');
59
60 /* read user profile/context */
61 context_read();
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 adios (NULL, "-%s unknown", cp);
74
75 case HELPSW:
76 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
77 print_help (buf, switches, 1);
78 done (1);
79 case VERSIONSW:
80 print_version(invo_name);
81 done (1);
82
83 case FORMSW:
84 if (!(form = *argp++) || *form == '-')
85 adios (NULL, "missing argument to %s", argp[-2]);
86 format = NULL;
87 continue;
88 case FMTSW:
89 if (!(format = *argp++) || *format == '-')
90 adios (NULL, "missing argument to %s", argp[-2]);
91 form = NULL;
92 continue;
93
94 }
95 }
96 if (form)
97 adios (NULL, "only one form at a time!");
98 else
99 form = cp;
100 }
101
102 /*
103 * Get new format string. Must be before chdir().
104 */
105 nfs = new_fs (form, format, FORMAT);
106 ncomps = fmt_compile(nfs, &fmt);
107
108 fmt_dump(fmt);
109 return done(0);
110 }
111
112 static void
113 fmt_dump (struct format *fmth)
114 {
115 int i;
116 register struct format *fmt, *addr;
117
118 /* Assign labels */
119 for (fmt = fmth; fmt; ++fmt) {
120 i = fmt->f_type;
121 if (i == FT_IF_S ||
122 i == FT_IF_S_NULL ||
123 i == FT_IF_V_EQ ||
124 i == FT_IF_V_NE ||
125 i == FT_IF_V_GT ||
126 i == FT_IF_MATCH ||
127 i == FT_IF_AMATCH ||
128 i == FT_GOTO) {
129 addr = fmt + fmt->f_skip;
130 if (findlabel(addr) < 0)
131 assignlabel(addr);
132 }
133 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
134 break;
135 }
136
137 /* Dump them out! */
138 for (fmt = fmth; fmt; ++fmt) {
139 dumpone(fmt);
140 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
141 break;
142 }
143 }
144
145 static void
146 dumpone(struct format *fmt)
147 {
148 register int i;
149
150 if ((i = findlabel(fmt)) >= 0)
151 printf("L%d:", i);
152 putchar('\t');
153
154 fputs(f_typestr((int)fmt->f_type), stdout);
155
156 switch (fmt->f_type) {
157
158 case FT_COMP:
159 case FT_LS_COMP:
160 case FT_LV_COMPFLAG:
161 case FT_LV_COMP:
162 printf(", comp ");
163 litputs(fmt->f_comp->c_name);
164 if (fmt->f_comp->c_type)
165 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
166 if (fmt->f_comp->c_flags)
167 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
168 break;
169
170 case FT_LV_SEC:
171 case FT_LV_MIN:
172 case FT_LV_HOUR:
173 case FT_LV_MDAY:
174 case FT_LV_MON:
175 case FT_LS_MONTH:
176 case FT_LS_LMONTH:
177 case FT_LS_ZONE:
178 case FT_LV_YEAR:
179 case FT_LV_WDAY:
180 case FT_LS_DAY:
181 case FT_LS_WEEKDAY:
182 case FT_LV_YDAY:
183 case FT_LV_ZONE:
184 case FT_LV_CLOCK:
185 case FT_LV_RCLOCK:
186 case FT_LV_DAYF:
187 case FT_LV_ZONEF:
188 case FT_LV_DST:
189 case FT_LS_822DATE:
190 case FT_LS_PRETTY:
191 case FT_LOCALDATE:
192 case FT_GMTDATE:
193 case FT_PARSEDATE:
194 printf(", c_name ");
195 litputs(fmt->f_comp->c_name);
196 if (fmt->f_comp->c_type)
197 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
198 if (fmt->f_comp->c_flags)
199 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
200 break;
201
202 case FT_LS_ADDR:
203 case FT_LS_PERS:
204 case FT_LS_MBOX:
205 case FT_LS_HOST:
206 case FT_LS_PATH:
207 case FT_LS_GNAME:
208 case FT_LS_NOTE:
209 case FT_LS_822ADDR:
210 case FT_LV_HOSTTYPE:
211 case FT_LV_INGRPF:
212 case FT_LV_NOHOSTF:
213 case FT_LS_FRIENDLY:
214 case FT_PARSEADDR:
215 case FT_MYMBOX:
216 printf(", c_name ");
217 litputs(fmt->f_comp->c_name);
218 if (fmt->f_comp->c_type)
219 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
220 if (fmt->f_comp->c_flags)
221 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
222 break;
223
224 case FT_COMPF:
225 printf(", width %d, fill '", fmt->f_width);
226 litputc(fmt->f_fill);
227 printf("' name ");
228 litputs(fmt->f_comp->c_name);
229 if (fmt->f_comp->c_type)
230 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
231 if (fmt->f_comp->c_flags)
232 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
233 break;
234
235 case FT_STRF:
236 case FT_NUMF:
237 printf(", width %d, fill '", fmt->f_width);
238 litputc(fmt->f_fill);
239 putchar('\'');
240 break;
241
242 case FT_LIT:
243 #ifdef FT_LIT_FORCE
244 case FT_LIT_FORCE:
245 #endif
246 putchar(' ');
247 litputs(fmt->f_text);
248 break;
249
250 case FT_LITF:
251 printf(", width %d, fill '", fmt->f_width);
252 litputc(fmt->f_fill);
253 printf("' ");
254 litputs(fmt->f_text);
255 break;
256
257 case FT_CHAR:
258 putchar(' ');
259 putchar('\'');
260 litputc(fmt->f_char);
261 putchar('\'');
262 break;
263
264
265 case FT_IF_S:
266 case FT_IF_S_NULL:
267 case FT_IF_MATCH:
268 case FT_IF_AMATCH:
269 printf(" continue else goto");
270 case FT_GOTO:
271 i = findlabel(fmt + fmt->f_skip);
272 printf(" L%d", i);
273 break;
274
275 case FT_IF_V_EQ:
276 case FT_IF_V_NE:
277 case FT_IF_V_GT:
278 i = findlabel(fmt + fmt->f_skip);
279 printf(" %d continue else goto L%d", fmt->f_value, i);
280 break;
281
282 case FT_V_EQ:
283 case FT_V_NE:
284 case FT_V_GT:
285 case FT_LV_LIT:
286 case FT_LV_PLUS_L:
287 case FT_LV_MINUS_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 register 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_LS_COMP: return("LS_COMP");
360 case FT_LS_LIT: return("LS_LIT");
361 case FT_LS_GETENV: return("LS_GETENV");
362 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
363 case FT_LS_DECODE: return("FT_LS_DECODE");
364 case FT_LS_TRIM: return("LS_TRIM");
365 case FT_LV_COMP: return("LV_COMP");
366 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
367 case FT_LV_LIT: return("LV_LIT");
368 case FT_LV_DAT: return("LV_DAT");
369 case FT_LV_STRLEN: return("LV_STRLEN");
370 case FT_LV_PLUS_L: return("LV_PLUS_L");
371 case FT_LV_MINUS_L: return("LV_MINUS_L");
372 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
373 case FT_LV_MODULO_L: return("LV_MODULO_L");
374 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
375 case FT_LS_MONTH: return("LS_MONTH");
376 case FT_LS_LMONTH: return("LS_LMONTH");
377 case FT_LS_ZONE: return("LS_ZONE");
378 case FT_LS_DAY: return("LS_DAY");
379 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
380 case FT_LS_822DATE: return("LS_822DATE");
381 case FT_LS_PRETTY: return("LS_PRETTY");
382 case FT_LV_SEC: return("LV_SEC");
383 case FT_LV_MIN: return("LV_MIN");
384 case FT_LV_HOUR: return("LV_HOUR");
385 case FT_LV_MDAY: return("LV_MDAY");
386 case FT_LV_MON: return("LV_MON");
387 case FT_LV_YEAR: return("LV_YEAR");
388 case FT_LV_YDAY: return("LV_YDAY");
389 case FT_LV_WDAY: return("LV_WDAY");
390 case FT_LV_ZONE: return("LV_ZONE");
391 case FT_LV_CLOCK: return("LV_CLOCK");
392 case FT_LV_RCLOCK: return("LV_RCLOCK");
393 case FT_LV_DAYF: return("LV_DAYF");
394 case FT_LV_DST: return("LV_DST");
395 case FT_LV_ZONEF: return("LV_ZONEF");
396 case FT_LS_ADDR: return("LS_ADDR");
397 case FT_LS_PERS: return("LS_PERS");
398 case FT_LS_MBOX: return("LS_MBOX");
399 case FT_LS_HOST: return("LS_HOST");
400 case FT_LS_PATH: return("LS_PATH");
401 case FT_LS_GNAME: return("LS_GNAME");
402 case FT_LS_NOTE: return("LS_NOTE");
403 case FT_LS_822ADDR: return("LS_822ADDR");
404 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
405 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
406 case FT_LV_INGRPF: return("LV_INGRPF");
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_MYMBOX: return("MYMBOX");
414 #ifdef FT_ADDTOSEQ
415 case FT_ADDTOSEQ: return("ADDTOSEQ");
416 #endif
417 case FT_SAVESTR: return("SAVESTR");
418 #ifdef FT_PAUSE
419 case FT_PAUSE: return ("PAUSE");
420 #endif
421 case FT_DONE: return("DONE");
422 case FT_NOP: return("NOP");
423 case FT_GOTO: return("GOTO");
424 case FT_IF_S_NULL: return("IF_S_NULL");
425 case FT_IF_S: return("IF_S");
426 case FT_IF_V_EQ: return("IF_V_EQ");
427 case FT_IF_V_NE: return("IF_V_NE");
428 case FT_IF_V_GT: return("IF_V_GT");
429 case FT_IF_MATCH: return("IF_MATCH");
430 case FT_IF_AMATCH: return("IF_AMATCH");
431 case FT_S_NULL: return("S_NULL");
432 case FT_S_NONNULL: return("S_NONNULL");
433 case FT_V_EQ: return("V_EQ");
434 case FT_V_NE: return("V_NE");
435 case FT_V_GT: return("V_GT");
436 case FT_V_MATCH: return("V_MATCH");
437 case FT_V_AMATCH: return("V_AMATCH");
438 default:
439 printf(buf, "/* ??? #%d */", t);
440 return(buf);
441 }
442 }
443
444 #define FNORD(v, s) if (t & (v)) { \
445 if (i++ > 0) \
446 strcat(buf, "|"); \
447 strcat(buf, s); }
448
449 static char *
450 c_typestr(int t)
451 {
452 register int i;
453 static char buf[64];
454
455 buf[0] = '\0';
456 if (t & ~(CT_ADDR|CT_DATE))
457 printf(buf, "0x%x ", t);
458 strcat(buf, "<");
459 i = 0;
460 FNORD(CT_ADDR, "ADDR");
461 FNORD(CT_DATE, "DATE");
462 strcat(buf, ">");
463 return(buf);
464 }
465
466 static char *
467 c_flagsstr(int t)
468 {
469 register int i;
470 static char buf[64];
471
472 buf[0] = '\0';
473 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB))
474 printf(buf, "0x%x ", t);
475 strcat(buf, "<");
476 i = 0;
477 FNORD(CF_TRUE, "TRUE");
478 FNORD(CF_PARSED, "PARSED");
479 FNORD(CF_DATEFAB, "DATEFAB");
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 }