0
|
1 /* fmtdump.c - compile format file and dump out instructions */
|
|
2 #ifndef lint
|
|
3 static char ident[] = "@(#)$Id$";
|
|
4 #endif
|
|
5
|
|
6 #include "../h/mh.h"
|
|
7 #include "../h/formatsbr.h"
|
|
8 #include "../h/fmtcompile.h"
|
|
9 #include "../h/scansbr.h"
|
|
10 #include <stdio.h>
|
|
11 #ifdef LOCALE
|
|
12 #include <locale.h>
|
|
13 #endif
|
|
14
|
|
15
|
|
16 static struct swit switches[] = {
|
|
17 #define FORMSW 0
|
|
18 "form formatfile", 0,
|
|
19 #define FMTSW 1
|
|
20 "format string", 5,
|
|
21
|
|
22 #define HELPSW 2
|
|
23 "help", 4,
|
|
24
|
|
25 NULL, 0
|
|
26 };
|
|
27
|
|
28
|
|
29 /* for assignlabel */
|
|
30 static struct format *lvec[128];
|
|
31 static lused = 0;
|
|
32
|
|
33 static char *f_typestr(), *c_typestr();
|
|
34 static void fmt_dump(), dumpone(), assignlabel(), litputs(), litputc();
|
|
35 static int findlabel();
|
|
36
|
|
37 /* */
|
|
38
|
|
39 /* ARGSUSED */
|
|
40
|
|
41 main (argc, argv)
|
|
42 int argc;
|
|
43 char *argv[];
|
|
44 {
|
|
45 int ncomps;
|
|
46 char *cp,
|
|
47 *form = NULL,
|
|
48 *format = NULL,
|
|
49 buf[100],
|
|
50 **ap,
|
|
51 **argp,
|
|
52 *nfs,
|
|
53 *arguments[MAXARGS];
|
|
54 struct format *fmt;
|
|
55
|
|
56 #ifdef LOCALE
|
|
57 setlocale(LC_ALL, "");
|
|
58 #endif
|
|
59 #ifdef JAPAN
|
|
60 ml_init();
|
|
61 #endif /* JAPAN */
|
|
62 invo_name = r1bindex (argv[0], '/');
|
|
63 if ((cp = m_find (invo_name)) != NULL) {
|
|
64 ap = brkstring (cp = getcpy (cp), " ", "\n");
|
|
65 ap = copyip (ap, arguments);
|
|
66 }
|
|
67 else
|
|
68 ap = arguments;
|
|
69 (void) copyip (argv + 1, ap);
|
|
70 argp = arguments;
|
|
71
|
|
72 /* */
|
|
73
|
|
74 while (cp = *argp++) {
|
|
75 if (*cp == '-')
|
|
76 switch (smatch (++cp, switches)) {
|
|
77 case AMBIGSW:
|
|
78 ambigsw (cp, switches);
|
|
79 done (1);
|
|
80 case UNKWNSW:
|
|
81 adios (NULLCP, "-%s unknown", cp);
|
|
82 case HELPSW:
|
|
83 (void) sprintf (buf, "%s [switches]",
|
|
84 invo_name);
|
|
85 help (buf, switches);
|
|
86 done (1);
|
|
87
|
|
88 case FORMSW:
|
|
89 if (!(form = *argp++) || *form == '-')
|
|
90 adios (NULLCP, "missing argument to %s", argp[-2]);
|
|
91 format = NULL;
|
|
92 continue;
|
|
93 case FMTSW:
|
|
94 if (!(format = *argp++) || *format == '-')
|
|
95 adios (NULLCP, "missing argument to %s", argp[-2]);
|
|
96 form = NULL;
|
|
97 continue;
|
|
98
|
|
99 }
|
|
100 if (form)
|
|
101 adios (NULLCP, "only one form at a time!");
|
|
102 else
|
|
103 form = cp;
|
|
104 }
|
|
105
|
|
106 nfs = new_fs (form, format, FORMAT); /* must be before chdir() */
|
|
107 ncomps = fmt_compile(nfs, &fmt);
|
|
108
|
|
109 fmt_dump(fmt);
|
|
110 done(0);
|
|
111 }
|
|
112
|
|
113 static void
|
|
114 fmt_dump (fmth)
|
|
115 register struct format *fmth;
|
|
116 {
|
|
117 int i;
|
|
118 register struct format *fmt, *addr;
|
|
119
|
|
120 /* Assign labels */
|
|
121 for (fmt = fmth; fmt; ++fmt) {
|
|
122 i = fmt->f_type;
|
|
123 if (i == FT_IF_S ||
|
|
124 i == FT_IF_S_NULL ||
|
|
125 i == FT_IF_V_EQ ||
|
|
126 i == FT_IF_V_NE ||
|
|
127 i == FT_IF_V_GT ||
|
|
128 i == FT_IF_MATCH ||
|
|
129 i == FT_IF_AMATCH ||
|
|
130 i == FT_GOTO) {
|
|
131 addr = fmt + fmt->f_skip;
|
|
132 if (findlabel(addr) < 0)
|
|
133 assignlabel(addr);
|
|
134 }
|
|
135 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
|
|
136 break;
|
|
137 }
|
|
138
|
|
139 /* Dump them out! */
|
|
140 for (fmt = fmth; fmt; ++fmt) {
|
|
141 dumpone(fmt);
|
|
142 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
|
|
143 break;
|
|
144 }
|
|
145 }
|
|
146
|
|
147 static void
|
|
148 dumpone(fmt)
|
|
149 register 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 %d", 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 %d", 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 %d", 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 %d", 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_TRIM:
|
|
307 printf(", width %d", fmt->f_width);
|
|
308 break;
|
|
309
|
|
310 case FT_LV_DAT:
|
|
311 printf(", value dat[%d]", fmt->f_value);
|
|
312 break;
|
|
313 }
|
|
314 putchar('\n');
|
|
315 }
|
|
316
|
|
317 static int
|
|
318 findlabel(addr)
|
|
319 register struct format *addr;
|
|
320 {
|
|
321 register int i;
|
|
322
|
|
323 for (i = 0; i < lused; ++i)
|
|
324 if (addr == lvec[i])
|
|
325 return(i);
|
|
326 return(-1);
|
|
327 }
|
|
328
|
|
329 static void
|
|
330 assignlabel(addr)
|
|
331 register struct format *addr;
|
|
332 {
|
|
333 lvec[lused++] = addr;
|
|
334 }
|
|
335
|
|
336 static char *
|
|
337 f_typestr(t)
|
|
338 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_TRIM: return("LS_TRIM");
|
|
361 case FT_LV_COMP: return("LV_COMP");
|
|
362 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
|
|
363 case FT_LV_LIT: return("LV_LIT");
|
|
364 case FT_LV_DAT: return("LV_DAT");
|
|
365 case FT_LV_STRLEN: return("LV_STRLEN");
|
|
366 case FT_LV_PLUS_L: return("LV_PLUS_L");
|
|
367 case FT_LV_MINUS_L: return("LV_MINUS_L");
|
|
368 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
|
|
369 case FT_LV_MODULO_L: return("LV_MODULO_L");
|
|
370 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
|
|
371 case FT_LS_MONTH: return("LS_MONTH");
|
|
372 case FT_LS_LMONTH: return("LS_LMONTH");
|
|
373 case FT_LS_ZONE: return("LS_ZONE");
|
|
374 case FT_LS_DAY: return("LS_DAY");
|
|
375 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
|
|
376 case FT_LS_822DATE: return("LS_822DATE");
|
|
377 case FT_LS_PRETTY: return("LS_PRETTY");
|
|
378 case FT_LV_SEC: return("LV_SEC");
|
|
379 case FT_LV_MIN: return("LV_MIN");
|
|
380 case FT_LV_HOUR: return("LV_HOUR");
|
|
381 case FT_LV_MDAY: return("LV_MDAY");
|
|
382 case FT_LV_MON: return("LV_MON");
|
|
383 case FT_LV_YEAR: return("LV_YEAR");
|
|
384 case FT_LV_YDAY: return("LV_YDAY");
|
|
385 case FT_LV_WDAY: return("LV_WDAY");
|
|
386 case FT_LV_ZONE: return("LV_ZONE");
|
|
387 case FT_LV_CLOCK: return("LV_CLOCK");
|
|
388 case FT_LV_RCLOCK: return("LV_RCLOCK");
|
|
389 case FT_LV_DAYF: return("LV_DAYF");
|
|
390 case FT_LV_DST: return("LV_DST");
|
|
391 case FT_LV_ZONEF: return("LV_ZONEF");
|
|
392 case FT_LS_ADDR: return("LS_ADDR");
|
|
393 case FT_LS_PERS: return("LS_PERS");
|
|
394 case FT_LS_MBOX: return("LS_MBOX");
|
|
395 case FT_LS_HOST: return("LS_HOST");
|
|
396 case FT_LS_PATH: return("LS_PATH");
|
|
397 case FT_LS_GNAME: return("LS_GNAME");
|
|
398 case FT_LS_NOTE: return("LS_NOTE");
|
|
399 case FT_LS_822ADDR: return("LS_822ADDR");
|
|
400 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
|
|
401 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
|
|
402 case FT_LV_INGRPF: return("LV_INGRPF");
|
|
403 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
|
|
404 case FT_LOCALDATE: return("LOCALDATE");
|
|
405 case FT_GMTDATE: return("GMTDATE");
|
|
406 case FT_PARSEDATE: return("PARSEDATE");
|
|
407 case FT_PARSEADDR: return("PARSEADDR");
|
|
408 case FT_FORMATADDR: return("FORMATADDR");
|
|
409 case FT_MYMBOX: return("MYMBOX");
|
|
410 #ifdef FT_ADDTOSEQ
|
|
411 case FT_ADDTOSEQ: return("ADDTOSEQ");
|
|
412 #endif
|
|
413 case FT_SAVESTR: return("SAVESTR");
|
|
414 #ifdef FT_PAUSE
|
|
415 case FT_PAUSE: return ("PAUSE");
|
|
416 #endif
|
|
417 case FT_DONE: return("DONE");
|
|
418 case FT_NOP: return("NOP");
|
|
419 case FT_GOTO: return("GOTO");
|
|
420 case FT_IF_S_NULL: return("IF_S_NULL");
|
|
421 case FT_IF_S: return("IF_S");
|
|
422 case FT_IF_V_EQ: return("IF_V_EQ");
|
|
423 case FT_IF_V_NE: return("IF_V_NE");
|
|
424 case FT_IF_V_GT: return("IF_V_GT");
|
|
425 case FT_IF_MATCH: return("IF_MATCH");
|
|
426 case FT_IF_AMATCH: return("IF_AMATCH");
|
|
427 case FT_S_NULL: return("S_NULL");
|
|
428 case FT_S_NONNULL: return("S_NONNULL");
|
|
429 case FT_V_EQ: return("V_EQ");
|
|
430 case FT_V_NE: return("V_NE");
|
|
431 case FT_V_GT: return("V_GT");
|
|
432 case FT_V_MATCH: return("V_MATCH");
|
|
433 case FT_V_AMATCH: return("V_AMATCH");
|
|
434 #ifdef MIME_HEADERS
|
|
435 case FT_LS_HENCODE: return("LS_HENCODE");
|
|
436 case FT_LS_HDECODE: return("LS_HDECODE");
|
|
437 #endif /* MIME_HEADERS */
|
|
438 default:
|
|
439 (void)sprintf(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(t)
|
|
451 int t;
|
|
452 {
|
|
453 register int i;
|
|
454 static char buf[64];
|
|
455
|
|
456 buf[0] = '\0';
|
|
457 if (t & ~(CT_ADDR|CT_DATE|CT_MYMBOX|CT_ADDRPARSE))
|
|
458 (void)sprintf(buf, "0x%x ", t);
|
|
459 strcat(buf, "<");
|
|
460 i = 0;
|
|
461 FNORD(CT_ADDR, "ADDR");
|
|
462 FNORD(CT_DATE, "DATE");
|
|
463 FNORD(CT_MYMBOX, "MYMBOX");
|
|
464 FNORD(CT_ADDRPARSE, "ADDRPARSE");
|
|
465 strcat(buf, ">");
|
|
466 return(buf);
|
|
467 #undef FNORD
|
|
468 }
|
|
469
|
|
470 static void
|
|
471 litputs(s)
|
|
472 register char *s;
|
|
473 {
|
|
474 if (s) {
|
|
475 putc('"', stdout);
|
|
476 #ifdef JAPAN
|
|
477 ml_pretty_fputs(s, stdout);
|
|
478 #else /* JAPAN */
|
|
479 while (*s)
|
|
480 litputc(*s++);
|
|
481 #endif /* JAPAN */
|
|
482 putc('"', stdout);
|
|
483 } else
|
|
484 fputs("<nil>", stdout);
|
|
485 }
|
|
486
|
|
487 static void
|
|
488 litputc(c)
|
|
489 char c;
|
|
490 {
|
|
491 if (c & ~ 0177) {
|
|
492 putc('M', stdout);
|
|
493 putc('-', stdout);
|
|
494 c &= 0177;
|
|
495 }
|
|
496 if (c < 0x20 || c == 0177) {
|
|
497 if (c == '\b') {
|
|
498 putc('\\', stdout);
|
|
499 putc('b', stdout);
|
|
500 } else if (c == '\f') {
|
|
501 putc('\\', stdout);
|
|
502 putc('f', stdout);
|
|
503 } else if (c == '\n') {
|
|
504 putc('\\', stdout);
|
|
505 putc('n', stdout);
|
|
506 } else if (c == '\r') {
|
|
507 putc('\\', stdout);
|
|
508 putc('r', stdout);
|
|
509 } else if (c == '\t') {
|
|
510 putc('\\', stdout);
|
|
511 putc('t', stdout);
|
|
512 } else {
|
|
513 putc('^', stdout);
|
|
514 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
|
|
515 }
|
|
516 } else
|
|
517 putc(c, stdout);
|
|
518 }
|