]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
Apply David Levine's fix from whomfile() to sendfile() as well.
[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 #ifdef LOCALE
55 setlocale(LC_ALL, "");
56 #endif
57 invo_name = r1bindex (argv[0], '/');
58
59 /* read user profile/context */
60 context_read();
61
62 arguments = getarguments (invo_name, argc, argv, 1);
63 argp = arguments;
64
65 while ((cp = *argp++)) {
66 if (*cp == '-') {
67 switch (smatch (++cp, switches)) {
68 case AMBIGSW:
69 ambigsw (cp, switches);
70 done (1);
71 case UNKWNSW:
72 adios (NULL, "-%s unknown", cp);
73
74 case HELPSW:
75 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
76 print_help (buf, switches, 1);
77 done (0);
78 case VERSIONSW:
79 print_version(invo_name);
80 done (0);
81
82 case FORMSW:
83 if (!(form = *argp++) || *form == '-')
84 adios (NULL, "missing argument to %s", argp[-2]);
85 format = NULL;
86 continue;
87 case FMTSW:
88 if (!(format = *argp++) || *format == '-')
89 adios (NULL, "missing argument to %s", argp[-2]);
90 form = NULL;
91 continue;
92
93 }
94 }
95 if (form)
96 adios (NULL, "only one form at a time!");
97 else
98 form = cp;
99 }
100
101 /*
102 * Get new format string. Must be before chdir().
103 */
104 nfs = new_fs (form, format, FORMAT);
105 (void) fmt_compile(nfs, &fmt, 1);
106
107 fmt_dump(fmt);
108
109 fmt_free(fmt, 1);
110
111 done(0);
112 return 1;
113 }
114
115 static void
116 fmt_dump (struct format *fmth)
117 {
118 int i;
119 register struct format *fmt, *addr;
120
121 /* Assign labels */
122 for (fmt = fmth; fmt; ++fmt) {
123 i = fmt->f_type;
124 if (i == FT_IF_S ||
125 i == FT_IF_S_NULL ||
126 i == FT_IF_V_EQ ||
127 i == FT_IF_V_NE ||
128 i == FT_IF_V_GT ||
129 i == FT_IF_MATCH ||
130 i == FT_IF_AMATCH ||
131 i == FT_GOTO) {
132 addr = fmt + fmt->f_skip;
133 if (findlabel(addr) < 0)
134 assignlabel(addr);
135 }
136 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
137 break;
138 }
139
140 /* Dump them out! */
141 for (fmt = fmth; fmt; ++fmt) {
142 dumpone(fmt);
143 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
144 break;
145 }
146 }
147
148 static void
149 dumpone(struct format *fmt)
150 {
151 register int i;
152
153 if ((i = findlabel(fmt)) >= 0)
154 printf("L%d:", i);
155 putchar('\t');
156
157 fputs(f_typestr((int)fmt->f_type), stdout);
158
159 switch (fmt->f_type) {
160
161 case FT_COMP:
162 case FT_LS_COMP:
163 case FT_LV_COMPFLAG:
164 case FT_LV_COMP:
165 printf(", comp ");
166 litputs(fmt->f_comp->c_name);
167 if (fmt->f_comp->c_type)
168 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
169 if (fmt->f_comp->c_flags)
170 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
171 break;
172
173 case FT_LV_SEC:
174 case FT_LV_MIN:
175 case FT_LV_HOUR:
176 case FT_LV_MDAY:
177 case FT_LV_MON:
178 case FT_LS_MONTH:
179 case FT_LS_LMONTH:
180 case FT_LS_ZONE:
181 case FT_LV_YEAR:
182 case FT_LV_WDAY:
183 case FT_LS_DAY:
184 case FT_LS_WEEKDAY:
185 case FT_LV_YDAY:
186 case FT_LV_ZONE:
187 case FT_LV_CLOCK:
188 case FT_LV_RCLOCK:
189 case FT_LV_DAYF:
190 case FT_LV_ZONEF:
191 case FT_LV_DST:
192 case FT_LS_822DATE:
193 case FT_LS_PRETTY:
194 case FT_LOCALDATE:
195 case FT_GMTDATE:
196 case FT_PARSEDATE:
197 printf(", c_name ");
198 litputs(fmt->f_comp->c_name);
199 if (fmt->f_comp->c_type)
200 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
201 if (fmt->f_comp->c_flags)
202 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
203 break;
204
205 case FT_LS_ADDR:
206 case FT_LS_PERS:
207 case FT_LS_MBOX:
208 case FT_LS_HOST:
209 case FT_LS_PATH:
210 case FT_LS_GNAME:
211 case FT_LS_NOTE:
212 case FT_LS_822ADDR:
213 case FT_LV_HOSTTYPE:
214 case FT_LV_INGRPF:
215 case FT_LV_NOHOSTF:
216 case FT_LS_FRIENDLY:
217 case FT_PARSEADDR:
218 case FT_MYMBOX:
219 printf(", c_name ");
220 litputs(fmt->f_comp->c_name);
221 if (fmt->f_comp->c_type)
222 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
223 if (fmt->f_comp->c_flags)
224 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
225 break;
226
227 case FT_COMPF:
228 printf(", width %d, fill '", fmt->f_width);
229 litputc(fmt->f_fill);
230 printf("' name ");
231 litputs(fmt->f_comp->c_name);
232 if (fmt->f_comp->c_type)
233 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
234 if (fmt->f_comp->c_flags)
235 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
236 break;
237
238 case FT_STRF:
239 case FT_NUMF:
240 printf(", width %d, fill '", fmt->f_width);
241 litputc(fmt->f_fill);
242 putchar('\'');
243 break;
244
245 case FT_LIT:
246 #ifdef FT_LIT_FORCE
247 case FT_LIT_FORCE:
248 #endif
249 putchar(' ');
250 litputs(fmt->f_text);
251 break;
252
253 case FT_LITF:
254 printf(", width %d, fill '", fmt->f_width);
255 litputc(fmt->f_fill);
256 printf("' ");
257 litputs(fmt->f_text);
258 break;
259
260 case FT_CHAR:
261 putchar(' ');
262 putchar('\'');
263 litputc(fmt->f_char);
264 putchar('\'');
265 break;
266
267
268 case FT_IF_S:
269 case FT_IF_S_NULL:
270 case FT_IF_MATCH:
271 case FT_IF_AMATCH:
272 printf(" continue else goto");
273 case FT_GOTO:
274 i = findlabel(fmt + fmt->f_skip);
275 printf(" L%d", i);
276 break;
277
278 case FT_IF_V_EQ:
279 case FT_IF_V_NE:
280 case FT_IF_V_GT:
281 i = findlabel(fmt + fmt->f_skip);
282 printf(" %d continue else goto L%d", fmt->f_value, i);
283 break;
284
285 case FT_V_EQ:
286 case FT_V_NE:
287 case FT_V_GT:
288 case FT_LV_LIT:
289 case FT_LV_PLUS_L:
290 case FT_LV_MINUS_L:
291 case FT_LV_DIVIDE_L:
292 case FT_LV_MODULO_L:
293 printf(" value %d", fmt->f_value);
294 break;
295
296 case FT_LS_LIT:
297 printf(" str ");
298 litputs(fmt->f_text);
299 break;
300
301 case FT_LS_GETENV:
302 printf(" getenv ");
303 litputs(fmt->f_text);
304 break;
305
306 case FT_LS_DECODECOMP:
307 printf(", comp ");
308 litputs(fmt->f_comp->c_name);
309 break;
310
311 case FT_LS_DECODE:
312 break;
313
314 case FT_LS_TRIM:
315 printf(", width %d", fmt->f_width);
316 break;
317
318 case FT_LV_DAT:
319 printf(", value dat[%d]", fmt->f_value);
320 break;
321 }
322 putchar('\n');
323 }
324
325 static int
326 findlabel(struct format *addr)
327 {
328 register int i;
329
330 for (i = 0; i < lused; ++i)
331 if (addr == lvec[i])
332 return(i);
333 return(-1);
334 }
335
336 static void
337 assignlabel(struct format *addr)
338 {
339 lvec[lused++] = addr;
340 }
341
342 static char *
343 f_typestr(int t)
344 {
345 static char buf[32];
346
347 switch (t) {
348 case FT_COMP: return("COMP");
349 case FT_COMPF: return("COMPF");
350 case FT_LIT: return("LIT");
351 case FT_LITF: return("LITF");
352 #ifdef FT_LIT_FORCE
353 case FT_LIT_FORCE: return("LIT_FORCE");
354 #endif
355 case FT_CHAR: return("CHAR");
356 case FT_NUM: return("NUM");
357 case FT_NUMF: return("NUMF");
358 case FT_STR: return("STR");
359 case FT_STRF: return("STRF");
360 case FT_STRFW: return("STRFW");
361 case FT_PUTADDR: return("PUTADDR");
362 case FT_STRLIT: return("STRLIT");
363 case FT_STRLITZ: return("STRLITZ");
364 case FT_LS_COMP: return("LS_COMP");
365 case FT_LS_LIT: return("LS_LIT");
366 case FT_LS_GETENV: return("LS_GETENV");
367 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
368 case FT_LS_DECODE: return("FT_LS_DECODE");
369 case FT_LS_TRIM: return("LS_TRIM");
370 case FT_LV_COMP: return("LV_COMP");
371 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
372 case FT_LV_LIT: return("LV_LIT");
373 case FT_LV_DAT: return("LV_DAT");
374 case FT_LV_STRLEN: return("LV_STRLEN");
375 case FT_LV_PLUS_L: return("LV_PLUS_L");
376 case FT_LV_MINUS_L: return("LV_MINUS_L");
377 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
378 case FT_LV_MODULO_L: return("LV_MODULO_L");
379 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
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_SAVESTR: return("SAVESTR");
421 #ifdef FT_PAUSE
422 case FT_PAUSE: return ("PAUSE");
423 #endif
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 register 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 register 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 putc('"', stdout);
494 while (*s)
495 litputc(*s++);
496 putc('"', stdout);
497 } else
498 fputs("<nil>", stdout);
499 }
500
501 static void
502 litputc(char c)
503 {
504 if (c & ~ 0177) {
505 putc('M', stdout);
506 putc('-', stdout);
507 c &= 0177;
508 }
509 if (c < 0x20 || c == 0177) {
510 if (c == '\b') {
511 putc('\\', stdout);
512 putc('b', stdout);
513 } else if (c == '\f') {
514 putc('\\', stdout);
515 putc('f', stdout);
516 } else if (c == '\n') {
517 putc('\\', stdout);
518 putc('n', stdout);
519 } else if (c == '\r') {
520 putc('\\', stdout);
521 putc('r', stdout);
522 } else if (c == '\t') {
523 putc('\\', stdout);
524 putc('t', stdout);
525 } else {
526 putc('^', stdout);
527 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
528 }
529 } else
530 putc(c, stdout);
531 }