]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
Feed fileproc and mhlproc from rcvdist, send, and whatnow to post.
[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 static struct swit switches[] = {
16 #define FORMSW 0
17 { "form formatfile", 0 },
18 #define FMTSW 1
19 { "format string", 5 },
20 #define VERSIONSW 2
21 { "version", 0 },
22 #define HELPSW 3
23 { "help", 0 },
24 { NULL, 0 }
25 };
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 #ifdef LOCALE
53 setlocale(LC_ALL, "");
54 #endif
55 invo_name = r1bindex (argv[0], '/');
56
57 /* read user profile/context */
58 context_read();
59
60 arguments = getarguments (invo_name, argc, argv, 1);
61 argp = arguments;
62
63 while ((cp = *argp++)) {
64 if (*cp == '-') {
65 switch (smatch (++cp, switches)) {
66 case AMBIGSW:
67 ambigsw (cp, switches);
68 done (1);
69 case UNKWNSW:
70 adios (NULL, "-%s unknown", cp);
71
72 case HELPSW:
73 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
74 print_help (buf, switches, 1);
75 done (1);
76 case VERSIONSW:
77 print_version(invo_name);
78 done (1);
79
80 case FORMSW:
81 if (!(form = *argp++) || *form == '-')
82 adios (NULL, "missing argument to %s", argp[-2]);
83 format = NULL;
84 continue;
85 case FMTSW:
86 if (!(format = *argp++) || *format == '-')
87 adios (NULL, "missing argument to %s", argp[-2]);
88 form = NULL;
89 continue;
90
91 }
92 }
93 if (form)
94 adios (NULL, "only one form at a time!");
95 else
96 form = cp;
97 }
98
99 /*
100 * Get new format string. Must be before chdir().
101 */
102 nfs = new_fs (form, format, FORMAT);
103 (void) fmt_compile(nfs, &fmt);
104
105 fmt_dump(fmt);
106 done(0);
107 return 1;
108 }
109
110 static void
111 fmt_dump (struct format *fmth)
112 {
113 int i;
114 register struct format *fmt, *addr;
115
116 /* Assign labels */
117 for (fmt = fmth; fmt; ++fmt) {
118 i = fmt->f_type;
119 if (i == FT_IF_S ||
120 i == FT_IF_S_NULL ||
121 i == FT_IF_V_EQ ||
122 i == FT_IF_V_NE ||
123 i == FT_IF_V_GT ||
124 i == FT_IF_MATCH ||
125 i == FT_IF_AMATCH ||
126 i == FT_GOTO) {
127 addr = fmt + fmt->f_skip;
128 if (findlabel(addr) < 0)
129 assignlabel(addr);
130 }
131 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
132 break;
133 }
134
135 /* Dump them out! */
136 for (fmt = fmth; fmt; ++fmt) {
137 dumpone(fmt);
138 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
139 break;
140 }
141 }
142
143 static void
144 dumpone(struct format *fmt)
145 {
146 register int i;
147
148 if ((i = findlabel(fmt)) >= 0)
149 printf("L%d:", i);
150 putchar('\t');
151
152 fputs(f_typestr((int)fmt->f_type), stdout);
153
154 switch (fmt->f_type) {
155
156 case FT_COMP:
157 case FT_LS_COMP:
158 case FT_LV_COMPFLAG:
159 case FT_LV_COMP:
160 printf(", comp ");
161 litputs(fmt->f_comp->c_name);
162 if (fmt->f_comp->c_type)
163 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
164 if (fmt->f_comp->c_flags)
165 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
166 break;
167
168 case FT_LV_SEC:
169 case FT_LV_MIN:
170 case FT_LV_HOUR:
171 case FT_LV_MDAY:
172 case FT_LV_MON:
173 case FT_LS_MONTH:
174 case FT_LS_LMONTH:
175 case FT_LS_ZONE:
176 case FT_LV_YEAR:
177 case FT_LV_WDAY:
178 case FT_LS_DAY:
179 case FT_LS_WEEKDAY:
180 case FT_LV_YDAY:
181 case FT_LV_ZONE:
182 case FT_LV_CLOCK:
183 case FT_LV_RCLOCK:
184 case FT_LV_DAYF:
185 case FT_LV_ZONEF:
186 case FT_LV_DST:
187 case FT_LS_822DATE:
188 case FT_LS_PRETTY:
189 case FT_LOCALDATE:
190 case FT_GMTDATE:
191 case FT_PARSEDATE:
192 printf(", c_name ");
193 litputs(fmt->f_comp->c_name);
194 if (fmt->f_comp->c_type)
195 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
196 if (fmt->f_comp->c_flags)
197 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
198 break;
199
200 case FT_LS_ADDR:
201 case FT_LS_PERS:
202 case FT_LS_MBOX:
203 case FT_LS_HOST:
204 case FT_LS_PATH:
205 case FT_LS_GNAME:
206 case FT_LS_NOTE:
207 case FT_LS_822ADDR:
208 case FT_LV_HOSTTYPE:
209 case FT_LV_INGRPF:
210 case FT_LV_NOHOSTF:
211 case FT_LS_FRIENDLY:
212 case FT_PARSEADDR:
213 case FT_MYMBOX:
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 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_DIVIDE_L:
287 case FT_LV_MODULO_L:
288 printf(" value %d", fmt->f_value);
289 break;
290
291 case FT_LS_LIT:
292 printf(" str ");
293 litputs(fmt->f_text);
294 break;
295
296 case FT_LS_GETENV:
297 printf(" getenv ");
298 litputs(fmt->f_text);
299 break;
300
301 case FT_LS_DECODECOMP:
302 printf(", comp ");
303 litputs(fmt->f_comp->c_name);
304 break;
305
306 case FT_LS_DECODE:
307 break;
308
309 case FT_LS_TRIM:
310 printf(", width %d", fmt->f_width);
311 break;
312
313 case FT_LV_DAT:
314 printf(", value dat[%d]", fmt->f_value);
315 break;
316 }
317 putchar('\n');
318 }
319
320 static int
321 findlabel(struct format *addr)
322 {
323 register int i;
324
325 for (i = 0; i < lused; ++i)
326 if (addr == lvec[i])
327 return(i);
328 return(-1);
329 }
330
331 static void
332 assignlabel(struct format *addr)
333 {
334 lvec[lused++] = addr;
335 }
336
337 static char *
338 f_typestr(int t)
339 {
340 static char buf[32];
341
342 switch (t) {
343 case FT_COMP: return("COMP");
344 case FT_COMPF: return("COMPF");
345 case FT_LIT: return("LIT");
346 case FT_LITF: return("LITF");
347 #ifdef FT_LIT_FORCE
348 case FT_LIT_FORCE: return("LIT_FORCE");
349 #endif
350 case FT_CHAR: return("CHAR");
351 case FT_NUM: return("NUM");
352 case FT_NUMF: return("NUMF");
353 case FT_STR: return("STR");
354 case FT_STRF: return("STRF");
355 case FT_STRFW: return("STRFW");
356 case FT_PUTADDR: return("PUTADDR");
357 case FT_LS_COMP: return("LS_COMP");
358 case FT_LS_LIT: return("LS_LIT");
359 case FT_LS_GETENV: return("LS_GETENV");
360 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
361 case FT_LS_DECODE: return("FT_LS_DECODE");
362 case FT_LS_TRIM: return("LS_TRIM");
363 case FT_LV_COMP: return("LV_COMP");
364 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
365 case FT_LV_LIT: return("LV_LIT");
366 case FT_LV_DAT: return("LV_DAT");
367 case FT_LV_STRLEN: return("LV_STRLEN");
368 case FT_LV_PLUS_L: return("LV_PLUS_L");
369 case FT_LV_MINUS_L: return("LV_MINUS_L");
370 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
371 case FT_LV_MODULO_L: return("LV_MODULO_L");
372 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
373 case FT_LS_MONTH: return("LS_MONTH");
374 case FT_LS_LMONTH: return("LS_LMONTH");
375 case FT_LS_ZONE: return("LS_ZONE");
376 case FT_LS_DAY: return("LS_DAY");
377 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
378 case FT_LS_822DATE: return("LS_822DATE");
379 case FT_LS_PRETTY: return("LS_PRETTY");
380 case FT_LV_SEC: return("LV_SEC");
381 case FT_LV_MIN: return("LV_MIN");
382 case FT_LV_HOUR: return("LV_HOUR");
383 case FT_LV_MDAY: return("LV_MDAY");
384 case FT_LV_MON: return("LV_MON");
385 case FT_LV_YEAR: return("LV_YEAR");
386 case FT_LV_YDAY: return("LV_YDAY");
387 case FT_LV_WDAY: return("LV_WDAY");
388 case FT_LV_ZONE: return("LV_ZONE");
389 case FT_LV_CLOCK: return("LV_CLOCK");
390 case FT_LV_RCLOCK: return("LV_RCLOCK");
391 case FT_LV_DAYF: return("LV_DAYF");
392 case FT_LV_DST: return("LV_DST");
393 case FT_LV_ZONEF: return("LV_ZONEF");
394 case FT_LS_ADDR: return("LS_ADDR");
395 case FT_LS_PERS: return("LS_PERS");
396 case FT_LS_MBOX: return("LS_MBOX");
397 case FT_LS_HOST: return("LS_HOST");
398 case FT_LS_PATH: return("LS_PATH");
399 case FT_LS_GNAME: return("LS_GNAME");
400 case FT_LS_NOTE: return("LS_NOTE");
401 case FT_LS_822ADDR: return("LS_822ADDR");
402 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
403 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
404 case FT_LV_INGRPF: return("LV_INGRPF");
405 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
406 case FT_LOCALDATE: return("LOCALDATE");
407 case FT_GMTDATE: return("GMTDATE");
408 case FT_PARSEDATE: return("PARSEDATE");
409 case FT_PARSEADDR: return("PARSEADDR");
410 case FT_FORMATADDR: return("FORMATADDR");
411 case FT_MYMBOX: return("MYMBOX");
412 #ifdef FT_ADDTOSEQ
413 case FT_ADDTOSEQ: return("ADDTOSEQ");
414 #endif
415 case FT_SAVESTR: return("SAVESTR");
416 #ifdef FT_PAUSE
417 case FT_PAUSE: return ("PAUSE");
418 #endif
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 printf(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 register int i;
451 static char buf[64];
452
453 buf[0] = '\0';
454 if (t & ~(CT_ADDR|CT_DATE))
455 printf(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 register int i;
468 static char buf[64];
469
470 buf[0] = '\0';
471 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB))
472 printf(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 strcat(buf, ">");
479 return(buf);
480 }
481 #undef FNORD
482
483 static void
484 litputs(char *s)
485 {
486 if (s) {
487 putc('"', stdout);
488 while (*s)
489 litputc(*s++);
490 putc('"', stdout);
491 } else
492 fputs("<nil>", stdout);
493 }
494
495 static void
496 litputc(char c)
497 {
498 if (c & ~ 0177) {
499 putc('M', stdout);
500 putc('-', stdout);
501 c &= 0177;
502 }
503 if (c < 0x20 || c == 0177) {
504 if (c == '\b') {
505 putc('\\', stdout);
506 putc('b', stdout);
507 } else if (c == '\f') {
508 putc('\\', stdout);
509 putc('f', stdout);
510 } else if (c == '\n') {
511 putc('\\', stdout);
512 putc('n', stdout);
513 } else if (c == '\r') {
514 putc('\\', stdout);
515 putc('r', stdout);
516 } else if (c == '\t') {
517 putc('\\', stdout);
518 putc('t', stdout);
519 } else {
520 putc('^', stdout);
521 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
522 }
523 } else
524 putc(c, stdout);
525 }