]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
Add support for a -nosasl switch.
[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 (0);
76 case VERSIONSW:
77 print_version(invo_name);
78 done (0);
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, 1);
104
105 fmt_dump(fmt);
106
107 fmt_free(fmt, 1);
108
109 done(0);
110 return 1;
111 }
112
113 static void
114 fmt_dump (struct format *fmth)
115 {
116 int i;
117 register struct format *fmt, *addr;
118
119 /* Assign labels */
120 for (fmt = fmth; fmt; ++fmt) {
121 i = fmt->f_type;
122 if (i == FT_IF_S ||
123 i == FT_IF_S_NULL ||
124 i == FT_IF_V_EQ ||
125 i == FT_IF_V_NE ||
126 i == FT_IF_V_GT ||
127 i == FT_IF_MATCH ||
128 i == FT_IF_AMATCH ||
129 i == FT_GOTO) {
130 addr = fmt + fmt->f_skip;
131 if (findlabel(addr) < 0)
132 assignlabel(addr);
133 }
134 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
135 break;
136 }
137
138 /* Dump them out! */
139 for (fmt = fmth; fmt; ++fmt) {
140 dumpone(fmt);
141 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
142 break;
143 }
144 }
145
146 static void
147 dumpone(struct format *fmt)
148 {
149 register int i;
150
151 if ((i = findlabel(fmt)) >= 0)
152 printf("L%d:", i);
153 putchar('\t');
154
155 fputs(f_typestr((int)fmt->f_type), stdout);
156
157 switch (fmt->f_type) {
158
159 case FT_COMP:
160 case FT_LS_COMP:
161 case FT_LV_COMPFLAG:
162 case FT_LV_COMP:
163 printf(", comp ");
164 litputs(fmt->f_comp->c_name);
165 if (fmt->f_comp->c_type)
166 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
167 if (fmt->f_comp->c_flags)
168 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
169 break;
170
171 case FT_LV_SEC:
172 case FT_LV_MIN:
173 case FT_LV_HOUR:
174 case FT_LV_MDAY:
175 case FT_LV_MON:
176 case FT_LS_MONTH:
177 case FT_LS_LMONTH:
178 case FT_LS_ZONE:
179 case FT_LV_YEAR:
180 case FT_LV_WDAY:
181 case FT_LS_DAY:
182 case FT_LS_WEEKDAY:
183 case FT_LV_YDAY:
184 case FT_LV_ZONE:
185 case FT_LV_CLOCK:
186 case FT_LV_RCLOCK:
187 case FT_LV_DAYF:
188 case FT_LV_ZONEF:
189 case FT_LV_DST:
190 case FT_LS_822DATE:
191 case FT_LS_PRETTY:
192 case FT_LOCALDATE:
193 case FT_GMTDATE:
194 case FT_PARSEDATE:
195 printf(", c_name ");
196 litputs(fmt->f_comp->c_name);
197 if (fmt->f_comp->c_type)
198 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
199 if (fmt->f_comp->c_flags)
200 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
201 break;
202
203 case FT_LS_ADDR:
204 case FT_LS_PERS:
205 case FT_LS_MBOX:
206 case FT_LS_HOST:
207 case FT_LS_PATH:
208 case FT_LS_GNAME:
209 case FT_LS_NOTE:
210 case FT_LS_822ADDR:
211 case FT_LV_HOSTTYPE:
212 case FT_LV_INGRPF:
213 case FT_LV_NOHOSTF:
214 case FT_LS_FRIENDLY:
215 case FT_PARSEADDR:
216 case FT_MYMBOX:
217 printf(", c_name ");
218 litputs(fmt->f_comp->c_name);
219 if (fmt->f_comp->c_type)
220 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
221 if (fmt->f_comp->c_flags)
222 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
223 break;
224
225 case FT_COMPF:
226 printf(", width %d, fill '", fmt->f_width);
227 litputc(fmt->f_fill);
228 printf("' name ");
229 litputs(fmt->f_comp->c_name);
230 if (fmt->f_comp->c_type)
231 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
232 if (fmt->f_comp->c_flags)
233 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
234 break;
235
236 case FT_STRF:
237 case FT_NUMF:
238 printf(", width %d, fill '", fmt->f_width);
239 litputc(fmt->f_fill);
240 putchar('\'');
241 break;
242
243 case FT_LIT:
244 #ifdef FT_LIT_FORCE
245 case FT_LIT_FORCE:
246 #endif
247 putchar(' ');
248 litputs(fmt->f_text);
249 break;
250
251 case FT_LITF:
252 printf(", width %d, fill '", fmt->f_width);
253 litputc(fmt->f_fill);
254 printf("' ");
255 litputs(fmt->f_text);
256 break;
257
258 case FT_CHAR:
259 putchar(' ');
260 putchar('\'');
261 litputc(fmt->f_char);
262 putchar('\'');
263 break;
264
265
266 case FT_IF_S:
267 case FT_IF_S_NULL:
268 case FT_IF_MATCH:
269 case FT_IF_AMATCH:
270 printf(" continue else goto");
271 case FT_GOTO:
272 i = findlabel(fmt + fmt->f_skip);
273 printf(" L%d", i);
274 break;
275
276 case FT_IF_V_EQ:
277 case FT_IF_V_NE:
278 case FT_IF_V_GT:
279 i = findlabel(fmt + fmt->f_skip);
280 printf(" %d continue else goto L%d", fmt->f_value, i);
281 break;
282
283 case FT_V_EQ:
284 case FT_V_NE:
285 case FT_V_GT:
286 case FT_LV_LIT:
287 case FT_LV_PLUS_L:
288 case FT_LV_MINUS_L:
289 case FT_LV_DIVIDE_L:
290 case FT_LV_MODULO_L:
291 printf(" value %d", fmt->f_value);
292 break;
293
294 case FT_LS_LIT:
295 printf(" str ");
296 litputs(fmt->f_text);
297 break;
298
299 case FT_LS_GETENV:
300 printf(" getenv ");
301 litputs(fmt->f_text);
302 break;
303
304 case FT_LS_DECODECOMP:
305 printf(", comp ");
306 litputs(fmt->f_comp->c_name);
307 break;
308
309 case FT_LS_DECODE:
310 break;
311
312 case FT_LS_TRIM:
313 printf(", width %d", fmt->f_width);
314 break;
315
316 case FT_LV_DAT:
317 printf(", value dat[%d]", fmt->f_value);
318 break;
319 }
320 putchar('\n');
321 }
322
323 static int
324 findlabel(struct format *addr)
325 {
326 register int i;
327
328 for (i = 0; i < lused; ++i)
329 if (addr == lvec[i])
330 return(i);
331 return(-1);
332 }
333
334 static void
335 assignlabel(struct format *addr)
336 {
337 lvec[lused++] = addr;
338 }
339
340 static char *
341 f_typestr(int t)
342 {
343 static char buf[32];
344
345 switch (t) {
346 case FT_COMP: return("COMP");
347 case FT_COMPF: return("COMPF");
348 case FT_LIT: return("LIT");
349 case FT_LITF: return("LITF");
350 #ifdef FT_LIT_FORCE
351 case FT_LIT_FORCE: return("LIT_FORCE");
352 #endif
353 case FT_CHAR: return("CHAR");
354 case FT_NUM: return("NUM");
355 case FT_NUMF: return("NUMF");
356 case FT_STR: return("STR");
357 case FT_STRF: return("STRF");
358 case FT_STRFW: return("STRFW");
359 case FT_PUTADDR: return("PUTADDR");
360 case FT_STRLIT: return("STRLIT");
361 case FT_STRLITZ: return("STRLITZ");
362 case FT_LS_COMP: return("LS_COMP");
363 case FT_LS_LIT: return("LS_LIT");
364 case FT_LS_GETENV: return("LS_GETENV");
365 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
366 case FT_LS_DECODE: return("FT_LS_DECODE");
367 case FT_LS_TRIM: return("LS_TRIM");
368 case FT_LV_COMP: return("LV_COMP");
369 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
370 case FT_LV_LIT: return("LV_LIT");
371 case FT_LV_DAT: return("LV_DAT");
372 case FT_LV_STRLEN: return("LV_STRLEN");
373 case FT_LV_PLUS_L: return("LV_PLUS_L");
374 case FT_LV_MINUS_L: return("LV_MINUS_L");
375 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
376 case FT_LV_MODULO_L: return("LV_MODULO_L");
377 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
378 case FT_LS_MONTH: return("LS_MONTH");
379 case FT_LS_LMONTH: return("LS_LMONTH");
380 case FT_LS_ZONE: return("LS_ZONE");
381 case FT_LS_DAY: return("LS_DAY");
382 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
383 case FT_LS_822DATE: return("LS_822DATE");
384 case FT_LS_PRETTY: return("LS_PRETTY");
385 case FT_LV_SEC: return("LV_SEC");
386 case FT_LV_MIN: return("LV_MIN");
387 case FT_LV_HOUR: return("LV_HOUR");
388 case FT_LV_MDAY: return("LV_MDAY");
389 case FT_LV_MON: return("LV_MON");
390 case FT_LV_YEAR: return("LV_YEAR");
391 case FT_LV_YDAY: return("LV_YDAY");
392 case FT_LV_WDAY: return("LV_WDAY");
393 case FT_LV_ZONE: return("LV_ZONE");
394 case FT_LV_CLOCK: return("LV_CLOCK");
395 case FT_LV_RCLOCK: return("LV_RCLOCK");
396 case FT_LV_DAYF: return("LV_DAYF");
397 case FT_LV_DST: return("LV_DST");
398 case FT_LV_ZONEF: return("LV_ZONEF");
399 case FT_LS_ADDR: return("LS_ADDR");
400 case FT_LS_PERS: return("LS_PERS");
401 case FT_LS_MBOX: return("LS_MBOX");
402 case FT_LS_HOST: return("LS_HOST");
403 case FT_LS_PATH: return("LS_PATH");
404 case FT_LS_GNAME: return("LS_GNAME");
405 case FT_LS_NOTE: return("LS_NOTE");
406 case FT_LS_822ADDR: return("LS_822ADDR");
407 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
408 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
409 case FT_LV_INGRPF: return("LV_INGRPF");
410 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
411 case FT_LOCALDATE: return("LOCALDATE");
412 case FT_GMTDATE: return("GMTDATE");
413 case FT_PARSEDATE: return("PARSEDATE");
414 case FT_PARSEADDR: return("PARSEADDR");
415 case FT_FORMATADDR: return("FORMATADDR");
416 case FT_CONCATADDR: return("CONCATADDR");
417 case FT_MYMBOX: return("MYMBOX");
418 #ifdef FT_ADDTOSEQ
419 case FT_ADDTOSEQ: return("ADDTOSEQ");
420 #endif
421 case FT_SAVESTR: return("SAVESTR");
422 #ifdef FT_PAUSE
423 case FT_PAUSE: return ("PAUSE");
424 #endif
425 case FT_DONE: return("DONE");
426 case FT_NOP: return("NOP");
427 case FT_GOTO: return("GOTO");
428 case FT_IF_S_NULL: return("IF_S_NULL");
429 case FT_IF_S: return("IF_S");
430 case FT_IF_V_EQ: return("IF_V_EQ");
431 case FT_IF_V_NE: return("IF_V_NE");
432 case FT_IF_V_GT: return("IF_V_GT");
433 case FT_IF_MATCH: return("IF_MATCH");
434 case FT_IF_AMATCH: return("IF_AMATCH");
435 case FT_S_NULL: return("S_NULL");
436 case FT_S_NONNULL: return("S_NONNULL");
437 case FT_V_EQ: return("V_EQ");
438 case FT_V_NE: return("V_NE");
439 case FT_V_GT: return("V_GT");
440 case FT_V_MATCH: return("V_MATCH");
441 case FT_V_AMATCH: return("V_AMATCH");
442 default:
443 printf(buf, "/* ??? #%d */", t);
444 return(buf);
445 }
446 }
447
448 #define FNORD(v, s) if (t & (v)) { \
449 if (i++ > 0) \
450 strcat(buf, "|"); \
451 strcat(buf, s); }
452
453 static char *
454 c_typestr(int t)
455 {
456 register int i;
457 static char buf[64];
458
459 buf[0] = '\0';
460 if (t & ~(CT_ADDR|CT_DATE))
461 printf(buf, "0x%x ", t);
462 strcat(buf, "<");
463 i = 0;
464 FNORD(CT_ADDR, "ADDR");
465 FNORD(CT_DATE, "DATE");
466 strcat(buf, ">");
467 return(buf);
468 }
469
470 static char *
471 c_flagsstr(int t)
472 {
473 register int i;
474 static char buf[64];
475
476 buf[0] = '\0';
477 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED))
478 printf(buf, "0x%x ", t);
479 strcat(buf, "<");
480 i = 0;
481 FNORD(CF_TRUE, "TRUE");
482 FNORD(CF_PARSED, "PARSED");
483 FNORD(CF_DATEFAB, "DATEFAB");
484 FNORD(CF_TRIMMED, "TRIMMED");
485 strcat(buf, ">");
486 return(buf);
487 }
488 #undef FNORD
489
490 static void
491 litputs(char *s)
492 {
493 if (s) {
494 putc('"', stdout);
495 while (*s)
496 litputc(*s++);
497 putc('"', stdout);
498 } else
499 fputs("<nil>", stdout);
500 }
501
502 static void
503 litputc(char c)
504 {
505 if (c & ~ 0177) {
506 putc('M', stdout);
507 putc('-', stdout);
508 c &= 0177;
509 }
510 if (c < 0x20 || c == 0177) {
511 if (c == '\b') {
512 putc('\\', stdout);
513 putc('b', stdout);
514 } else if (c == '\f') {
515 putc('\\', stdout);
516 putc('f', stdout);
517 } else if (c == '\n') {
518 putc('\\', stdout);
519 putc('n', stdout);
520 } else if (c == '\r') {
521 putc('\\', stdout);
522 putc('r', stdout);
523 } else if (c == '\t') {
524 putc('\\', stdout);
525 putc('t', stdout);
526 } else {
527 putc('^', stdout);
528 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
529 }
530 } else
531 putc(c, stdout);
532 }