Fix mhsign for gpg2: Expiry date format
[mmh] / uip / fmtdump.c
1 /*
2 ** fmtdump.c -- compile format file and dump out instructions
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/fmt_scan.h>
11 #include <h/fmt_compile.h>
12 #include <h/scansbr.h>
13 #include <locale.h>
14 #include <sysexits.h>
15
16 static struct swit switches[] = {
17 #define FORMSW  0
18         { "form formatfile", 0 },
19 #define VERSIONSW  1
20         { "Version", 0 },
21 #define HELPSW  2
22         { "help", 0 },
23         { NULL, 0 }
24 };
25
26 char *version=VERSION;
27
28 /* for assignlabel */
29 static struct format *lvec[128];
30 static int lused = 0;
31
32 /*
33 ** static prototypes
34 */
35 static void fmt_dump(struct format *);
36 static void dumpone(struct format *);
37 static int findlabel(struct format *);
38 static void assignlabel(struct format *);
39 static char *f_typestr(int);
40 static char *c_typestr(int);
41 static char *c_flagsstr(int);
42 static void litputs(char *);
43 static void litputc(char);
44
45
46 int
47 main(int argc, char **argv)
48 {
49         char *cp, *form = NULL;
50         char buf[BUFSIZ], *fmtstr, **argp, **arguments;
51         struct format *fmt;
52
53         setlocale(LC_ALL, "");
54         invo_name = mhbasename(argv[0]);
55
56         /* read user profile/context */
57         context_read();
58
59         arguments = getarguments(invo_name, argc, argv, 1);
60         argp = arguments;
61
62         while ((cp = *argp++)) {
63                 if (*cp == '-') {
64                         switch (smatch(++cp, switches)) {
65                         case AMBIGSW:
66                                 ambigsw(cp, switches);
67                                 exit(EX_USAGE);
68                         case UNKWNSW:
69                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
70
71                         case HELPSW:
72                                 snprintf(buf, sizeof(buf), "%s [switches]",
73                                                 invo_name);
74                                 print_help(buf, switches, 1);
75                                 exit(argc == 2 ? EX_OK : EX_USAGE);
76                         case VERSIONSW:
77                                 print_version(invo_name);
78                                 exit(argc == 2 ? EX_OK : EX_USAGE);
79
80                         case FORMSW:
81                                 if (!(form = *argp++) || *form == '-')
82                                         adios(EX_USAGE, NULL, "missing argument to %s",
83                                                         argp[-2]);
84                                 continue;
85
86                         }
87                 }
88                 if (form)
89                         adios(EX_USAGE, NULL, "only one form at a time!");
90                 else
91                         form = cp;
92         }
93
94         /* Set format string.  Must be before chdir(). */
95         fmtstr = new_fs(form, scanformat);
96         fmt_compile(fmtstr, &fmt);
97
98         fmt_dump(fmt);
99         return EX_OK;
100 }
101
102 static void
103 fmt_dump(struct format *fmth)
104 {
105         int i;
106         struct format *fmt, *addr;
107
108         /* Assign labels */
109         for (fmt = fmth; fmt; ++fmt) {
110                 i = fmt->f_type;
111                 if (i == FT_IF_S || i == FT_IF_S_NULL || i == FT_IF_V_EQ ||
112                                 i == FT_IF_V_NE || i == FT_IF_V_GT ||
113                                 i == FT_IF_MATCH || i == FT_IF_AMATCH ||
114                                 i == FT_GOTO) {
115                         addr = fmt + fmt->f_skip;
116                         if (findlabel(addr) < 0)
117                                 assignlabel(addr);
118                 }
119                 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
120                         break;
121         }
122
123         /* Dump them out! */
124         for (fmt = fmth; fmt; ++fmt) {
125                 dumpone(fmt);
126                 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
127                         break;
128         }
129 }
130
131 static void
132 dumpone(struct format *fmt)
133 {
134         int i;
135
136         if ((i = findlabel(fmt)) >= 0)
137                 printf("L%d:", i);
138         putchar('\t');
139
140         fputs(f_typestr((int)fmt->f_type), stdout);
141
142         switch (fmt->f_type) {
143
144         case FT_COMP:
145         case FT_LS_COMP:
146         case FT_LV_COMPFLAG:
147         case FT_LV_COMP:
148                 printf(", comp ");
149                 litputs(fmt->f_comp->c_name);
150                 if (fmt->f_comp->c_type)
151                         printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
152                 if (fmt->f_comp->c_flags)
153                         printf(", c_flags %s",
154                                         c_flagsstr(fmt->f_comp->c_flags));
155                 break;
156
157         case FT_LV_SEC:
158         case FT_LV_MIN:
159         case FT_LV_HOUR:
160         case FT_LV_MDAY:
161         case FT_LV_MON:
162         case FT_LS_MONTH:
163         case FT_LS_LMONTH:
164         case FT_LS_ZONE:
165         case FT_LV_YEAR:
166         case FT_LV_WDAY:
167         case FT_LS_DAY:
168         case FT_LS_WEEKDAY:
169         case FT_LV_YDAY:
170         case FT_LV_ZONE:
171         case FT_LV_CLOCK:
172         case FT_LV_RCLOCK:
173         case FT_LV_DAYF:
174         case FT_LV_ZONEF:
175         case FT_LV_DST:
176         case FT_LS_822DATE:
177         case FT_LS_PRETTY:
178         case FT_LOCALDATE:
179         case FT_GMTDATE:
180         case FT_PARSEDATE:
181                 printf(", c_name ");
182                 litputs(fmt->f_comp->c_name);
183                 if (fmt->f_comp->c_type)
184                         printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
185                 if (fmt->f_comp->c_flags)
186                         printf(", c_flags %s",
187                                         c_flagsstr(fmt->f_comp->c_flags));
188                 break;
189
190         case FT_LS_ADDR:
191         case FT_LS_PERS:
192         case FT_LS_MBOX:
193         case FT_LS_HOST:
194         case FT_LS_PATH:
195         case FT_LS_GNAME:
196         case FT_LS_NOTE:
197         case FT_LS_822ADDR:
198         case FT_LV_HOSTTYPE:
199         case FT_LV_INGRPF:
200         case FT_LV_NOHOSTF:
201         case FT_LS_FRIENDLY:
202         case FT_PARSEADDR:
203         case FT_MYMBOX:
204                 printf(", c_name ");
205                 litputs(fmt->f_comp->c_name);
206                 if (fmt->f_comp->c_type)
207                         printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
208                 if (fmt->f_comp->c_flags)
209                         printf(", c_flags %s",
210                                         c_flagsstr(fmt->f_comp->c_flags));
211                 break;
212
213         case FT_COMPF:
214                 printf(", width %d, fill '", fmt->f_width);
215                 litputc(fmt->f_fill);
216                 printf("' 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 %s",
222                                         c_flagsstr(fmt->f_comp->c_flags));
223                 break;
224
225         case FT_STRF:
226         case FT_NUMF:
227                 printf(", width %d, fill '", fmt->f_width);
228                 litputc(fmt->f_fill);
229                 putchar('\'');
230                 break;
231
232         case FT_LIT:
233 #ifdef FT_LIT_FORCE
234         case FT_LIT_FORCE:
235 #endif
236                 putchar(' ');
237                 litputs(fmt->f_text);
238                 break;
239
240         case FT_LITF:
241                 printf(", width %d, fill '", fmt->f_width);
242                 litputc(fmt->f_fill);
243                 printf("' ");
244                 litputs(fmt->f_text);
245                 break;
246
247         case FT_CHAR:
248                 putchar(' ');
249                 putchar('\'');
250                 litputc(fmt->f_char);
251                 putchar('\'');
252                 break;
253
254
255         case FT_IF_S:
256         case FT_IF_S_NULL:
257         case FT_IF_MATCH:
258         case FT_IF_AMATCH:
259                 printf(" continue else goto");
260         case FT_GOTO:
261                 i = findlabel(fmt + fmt->f_skip);
262                 printf(" L%d", i);
263                 break;
264
265         case FT_IF_V_EQ:
266         case FT_IF_V_NE:
267         case FT_IF_V_GT:
268                 i = findlabel(fmt + fmt->f_skip);
269                 printf(" %d continue else goto L%d", fmt->f_value, i);
270                 break;
271
272         case FT_V_EQ:
273         case FT_V_NE:
274         case FT_V_GT:
275         case FT_LV_LIT:
276         case FT_LV_PLUS_L:
277         case FT_LV_MINUS_L:
278         case FT_LV_DIVIDE_L:
279         case FT_LV_MODULO_L:
280                 printf(" value %d", fmt->f_value);
281                 break;
282
283         case FT_LS_LIT:
284                 printf(" str ");
285                 litputs(fmt->f_text);
286                 break;
287
288         case FT_LS_GETENV:
289                 printf(" getenv ");
290                 litputs(fmt->f_text);
291                 break;
292
293         case FT_LS_DECODECOMP:
294                 printf(", comp ");
295                 litputs(fmt->f_comp->c_name);
296                 break;
297
298         case FT_LS_DECODE:
299                 break;
300
301         case FT_LS_TRIM:
302                 printf(", width %d", fmt->f_width);
303                 break;
304
305         case FT_LV_DAT:
306                 printf(", value dat[%d]", fmt->f_value);
307                 break;
308
309         case FT_LS_UNMAILTO:
310                 break;
311         }
312         putchar('\n');
313 }
314
315 static int
316 findlabel(struct format *addr)
317 {
318         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(struct format *addr)
328 {
329         lvec[lused++] = addr;
330 }
331
332 static char *
333 f_typestr(int t)
334 {
335         static char buf[32];
336
337         switch (t) {
338         case FT_COMP: return("COMP");
339         case FT_COMPF: return("COMPF");
340         case FT_LIT: return("LIT");
341         case FT_LITF: return("LITF");
342 #ifdef FT_LIT_FORCE
343         case FT_LIT_FORCE: return("LIT_FORCE");
344 #endif
345         case FT_CHAR: return("CHAR");
346         case FT_NUM: return("NUM");
347         case FT_NUMF: return("NUMF");
348         case FT_STR: return("STR");
349         case FT_STRF: return("STRF");
350         case FT_STRFW: return("STRFW");
351         case FT_PUTADDR: return("PUTADDR");
352         case FT_LS_COMP: return("LS_COMP");
353         case FT_LS_LIT: return("LS_LIT");
354         case FT_LS_GETENV: return("LS_GETENV");
355         case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
356         case FT_LS_DECODE: return("FT_LS_DECODE");
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         case FT_SAVESTR: return("SAVESTR");
408 #ifdef FT_PAUSE
409         case FT_PAUSE: return ("PAUSE");
410 #endif
411         case FT_DONE: return("DONE");
412         case FT_NOP: return("NOP");
413         case FT_GOTO: return("GOTO");
414         case FT_IF_S_NULL: return("IF_S_NULL");
415         case FT_IF_S: return("IF_S");
416         case FT_IF_V_EQ: return("IF_V_EQ");
417         case FT_IF_V_NE: return("IF_V_NE");
418         case FT_IF_V_GT: return("IF_V_GT");
419         case FT_IF_MATCH: return("IF_MATCH");
420         case FT_IF_AMATCH: return("IF_AMATCH");
421         case FT_S_NULL: return("S_NULL");
422         case FT_S_NONNULL: return("S_NONNULL");
423         case FT_V_EQ: return("V_EQ");
424         case FT_V_NE: return("V_NE");
425         case FT_V_GT: return("V_GT");
426         case FT_V_MATCH: return("V_MATCH");
427         case FT_V_AMATCH: return("V_AMATCH");
428         case FT_LS_UNMAILTO: return("LS_UNMAILTO");
429         default:
430                 printf(buf, "/* ??? #%d */", t);
431                 return(buf);
432         }
433 }
434
435 #define FNORD(v, s) if (t & (v)) { \
436                 if (i++ > 0) \
437                         strcat(buf, "|"); \
438                 strcat(buf, s); }
439
440 static char *
441 c_typestr(int t)
442 {
443         int i;
444         static char buf[64];
445
446         buf[0] = '\0';
447         if (t & ~(CT_ADDR|CT_DATE))
448                 printf(buf, "0x%x ", t);
449         strcat(buf, "<");
450         i = 0;
451         FNORD(CT_ADDR, "ADDR");
452         FNORD(CT_DATE, "DATE");
453         strcat(buf, ">");
454         return(buf);
455 }
456
457 static char *
458 c_flagsstr(int t)
459 {
460         int i;
461         static char buf[64];
462
463         buf[0] = '\0';
464         if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB))
465                 printf(buf, "0x%x ", t);
466         strcat(buf, "<");
467         i = 0;
468         FNORD(CF_TRUE, "TRUE");
469         FNORD(CF_PARSED, "PARSED");
470         FNORD(CF_DATEFAB, "DATEFAB");
471         strcat(buf, ">");
472         return(buf);
473 }
474 #undef FNORD
475
476 static void
477 litputs(char *s)
478 {
479         if (s) {
480                 putc('"', stdout);
481                 while (*s)
482                         litputc(*s++);
483                 putc('"', stdout);
484         } else
485                 fputs("<nil>", stdout);
486 }
487
488 static void
489 litputc(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);
515                         /* DEL to ?, others to alpha */
516                 }
517         } else
518                 putc(c, stdout);
519 }