Added all of the MH sources, including RCS files, in
[mmh] / docs / historical / mh-6.8.5 / uip / fmtdump.c
1 /* fmtdump.c - compile format file and dump out instructions */
2 #ifndef lint
3 static char ident[] = "@(#)$Id: fmtdump.c,v 1.10 1992/11/04 00:42:31 jromine Exp $";
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 /* \f */
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     invo_name = r1bindex (argv[0], '/');
60     if ((cp = m_find (invo_name)) != NULL) {
61         ap = brkstring (cp = getcpy (cp), " ", "\n");
62         ap = copyip (ap, arguments);
63     }
64     else
65         ap = arguments;
66     (void) copyip (argv + 1, ap);
67     argp = arguments;
68
69 /* \f */
70
71     while (cp = *argp++) {
72         if (*cp == '-')
73             switch (smatch (++cp, switches)) {
74                 case AMBIGSW: 
75                     ambigsw (cp, switches);
76                     done (1);
77                 case UNKWNSW: 
78                     adios (NULLCP, "-%s unknown", cp);
79                 case HELPSW: 
80                     (void) sprintf (buf, "%s [switches]",
81                             invo_name);
82                     help (buf, switches);
83                     done (1);
84
85                 case FORMSW: 
86                     if (!(form = *argp++) || *form == '-')
87                         adios (NULLCP, "missing argument to %s", argp[-2]);
88                     format = NULL;
89                     continue;
90                 case FMTSW: 
91                     if (!(format = *argp++) || *format == '-')
92                         adios (NULLCP, "missing argument to %s", argp[-2]);
93                     form = NULL;
94                     continue;
95
96             }
97         if (form)
98             adios (NULLCP, "only one form at a time!");
99         else
100             form = cp;
101     }
102
103     nfs = new_fs (form, format, FORMAT);        /* must be before chdir() */
104     ncomps = fmt_compile(nfs, &fmt);
105
106     fmt_dump(fmt);
107     done(0);
108 }
109
110 static void
111 fmt_dump (fmth)
112 register struct format *fmth;
113 {
114         int i;
115         register struct format *fmt, *addr;
116
117         /* Assign labels */
118         for (fmt = fmth; fmt; ++fmt) {
119                 i = fmt->f_type;
120                 if (i == FT_IF_S ||
121                     i == FT_IF_S_NULL ||
122                     i == FT_IF_V_EQ ||
123                     i == FT_IF_V_NE ||
124                     i == FT_IF_V_GT ||
125                     i == FT_IF_MATCH ||
126                     i == FT_IF_AMATCH ||
127                     i == FT_GOTO) {
128                         addr = fmt + fmt->f_skip;
129                         if (findlabel(addr) < 0)
130                                 assignlabel(addr);
131                 }
132                 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
133                         break;
134         }
135
136         /* Dump them out! */
137         for (fmt = fmth; fmt; ++fmt) {
138                 dumpone(fmt);
139                 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
140                         break;
141         }
142 }
143
144 static void
145 dumpone(fmt)
146         register 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 %d", 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 %d", 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 %d", 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 %d", 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_TRIM:
304                 printf(", width %d", fmt->f_width);
305                 break;
306
307         case FT_LV_DAT:
308                 printf(", value dat[%d]", fmt->f_value);
309                 break;
310         }
311         putchar('\n');
312 }
313
314 static int
315 findlabel(addr)
316         register struct format *addr;
317 {
318         register int i;
319
320         for (i = 0; i < lused; ++i)
321                 if (addr == lvec[i])
322                         return(i);
323         return(-1);
324 }
325
326 static void
327 assignlabel(addr)
328         register struct format *addr;
329 {
330         lvec[lused++] = addr;
331 }
332
333 static char *
334 f_typestr(t)
335         int t;
336 {
337         static char buf[32];
338
339         switch (t) {
340         case FT_COMP: return("COMP");
341         case FT_COMPF: return("COMPF");
342         case FT_LIT: return("LIT");
343         case FT_LITF: return("LITF");
344 #ifdef  FT_LIT_FORCE
345         case FT_LIT_FORCE: return("LIT_FORCE");
346 #endif
347         case FT_CHAR: return("CHAR");
348         case FT_NUM: return("NUM");
349         case FT_NUMF: return("NUMF");
350         case FT_STR: return("STR");
351         case FT_STRF: return("STRF");
352         case FT_STRFW: return("STRFW");
353         case FT_PUTADDR: return("PUTADDR");
354         case FT_LS_COMP: return("LS_COMP");
355         case FT_LS_LIT: return("LS_LIT");
356         case FT_LS_GETENV: return("LS_GETENV");
357         case FT_LS_TRIM: return("LS_TRIM");
358         case FT_LV_COMP: return("LV_COMP");
359         case FT_LV_COMPFLAG: return("LV_COMPFLAG");
360         case FT_LV_LIT: return("LV_LIT");
361         case FT_LV_DAT: return("LV_DAT");
362         case FT_LV_STRLEN: return("LV_STRLEN");
363         case FT_LV_PLUS_L: return("LV_PLUS_L");
364         case FT_LV_MINUS_L: return("LV_MINUS_L");
365         case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
366         case FT_LV_MODULO_L: return("LV_MODULO_L");
367         case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
368         case FT_LS_MONTH: return("LS_MONTH");
369         case FT_LS_LMONTH: return("LS_LMONTH");
370         case FT_LS_ZONE: return("LS_ZONE");
371         case FT_LS_DAY: return("LS_DAY");
372         case FT_LS_WEEKDAY: return("LS_WEEKDAY");
373         case FT_LS_822DATE: return("LS_822DATE");
374         case FT_LS_PRETTY: return("LS_PRETTY");
375         case FT_LV_SEC: return("LV_SEC");
376         case FT_LV_MIN: return("LV_MIN");
377         case FT_LV_HOUR: return("LV_HOUR");
378         case FT_LV_MDAY: return("LV_MDAY");
379         case FT_LV_MON: return("LV_MON");
380         case FT_LV_YEAR: return("LV_YEAR");
381         case FT_LV_YDAY: return("LV_YDAY");
382         case FT_LV_WDAY: return("LV_WDAY");
383         case FT_LV_ZONE: return("LV_ZONE");
384         case FT_LV_CLOCK: return("LV_CLOCK");
385         case FT_LV_RCLOCK: return("LV_RCLOCK");
386         case FT_LV_DAYF: return("LV_DAYF");
387         case FT_LV_DST: return("LV_DST");
388         case FT_LV_ZONEF: return("LV_ZONEF");
389         case FT_LS_ADDR: return("LS_ADDR");
390         case FT_LS_PERS: return("LS_PERS");
391         case FT_LS_MBOX: return("LS_MBOX");
392         case FT_LS_HOST: return("LS_HOST");
393         case FT_LS_PATH: return("LS_PATH");
394         case FT_LS_GNAME: return("LS_GNAME");
395         case FT_LS_NOTE: return("LS_NOTE");
396         case FT_LS_822ADDR: return("LS_822ADDR");
397         case FT_LS_FRIENDLY: return("LS_FRIENDLY");
398         case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
399         case FT_LV_INGRPF: return("LV_INGRPF");
400         case FT_LV_NOHOSTF: return("LV_NOHOSTF");
401         case FT_LOCALDATE: return("LOCALDATE");
402         case FT_GMTDATE: return("GMTDATE");
403         case FT_PARSEDATE: return("PARSEDATE");
404         case FT_PARSEADDR: return("PARSEADDR");
405         case FT_FORMATADDR: return("FORMATADDR");
406         case FT_MYMBOX: return("MYMBOX");
407 #ifdef  FT_ADDTOSEQ
408         case FT_ADDTOSEQ: return("ADDTOSEQ");
409 #endif
410         case FT_SAVESTR: return("SAVESTR");
411 #ifdef  FT_PAUSE
412         case FT_PAUSE: return ("PAUSE");
413 #endif
414         case FT_DONE: return("DONE");
415         case FT_NOP: return("NOP");
416         case FT_GOTO: return("GOTO");
417         case FT_IF_S_NULL: return("IF_S_NULL");
418         case FT_IF_S: return("IF_S");
419         case FT_IF_V_EQ: return("IF_V_EQ");
420         case FT_IF_V_NE: return("IF_V_NE");
421         case FT_IF_V_GT: return("IF_V_GT");
422         case FT_IF_MATCH: return("IF_MATCH");
423         case FT_IF_AMATCH: return("IF_AMATCH");
424         case FT_S_NULL: return("S_NULL");
425         case FT_S_NONNULL: return("S_NONNULL");
426         case FT_V_EQ: return("V_EQ");
427         case FT_V_NE: return("V_NE");
428         case FT_V_GT: return("V_GT");
429         case FT_V_MATCH: return("V_MATCH");
430         case FT_V_AMATCH: return("V_AMATCH");
431         default:
432                 (void)sprintf(buf, "/* ??? #%d */", t);
433                 return(buf);
434         }
435 }
436
437 #define FNORD(v, s) if (t & (v)) { \
438         if (i++ > 0) \
439                 strcat(buf, "|"); \
440         strcat(buf, s); }
441
442 static char *
443 c_typestr(t)
444         int t;
445 {
446         register int i;
447         static char buf[64];
448
449         buf[0] = '\0';
450         if (t & ~(CT_ADDR|CT_DATE|CT_MYMBOX|CT_ADDRPARSE))
451                 (void)sprintf(buf, "0x%x ", t);
452         strcat(buf, "<");
453         i = 0;
454         FNORD(CT_ADDR, "ADDR");
455         FNORD(CT_DATE, "DATE");
456         FNORD(CT_MYMBOX, "MYMBOX");
457         FNORD(CT_ADDRPARSE, "ADDRPARSE");
458         strcat(buf, ">");
459         return(buf);
460 #undef FNORD
461 }
462
463 static void
464 litputs(s)
465         register char *s;
466 {
467         if (s) {
468                 putc('"', stdout);
469                 while (*s)
470                         litputc(*s++);
471                 putc('"', stdout);
472         } else
473                 fputs("<nil>", stdout);
474 }
475
476 static void
477 litputc(c)
478         char c;
479 {
480         if (c & ~ 0177) {
481                 putc('M', stdout);
482                 putc('-', stdout);
483                 c &= 0177;
484         }
485         if (c < 0x20 || c == 0177) {
486                 if (c == '\b') {
487                         putc('\\', stdout);
488                         putc('b', stdout);
489                 } else if (c == '\f') {
490                         putc('\\', stdout);
491                         putc('f', stdout);
492                 } else if (c == '\n') {
493                         putc('\\', stdout);
494                         putc('n', stdout);
495                 } else if (c == '\r') {
496                         putc('\\', stdout);
497                         putc('r', stdout);
498                 } else if (c == '\t') {
499                         putc('\\', stdout);
500                         putc('t', stdout);
501                 } else {
502                         putc('^', stdout);
503                         putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
504                 }
505         } else
506                 putc(c, stdout);
507 }