]> diplodocus.org Git - nmh/blob - uip/fmtdump.c
Cope with sasl_decode64() returning SASL_CONTINUE as well as SASL_OK.
[nmh] / uip / fmtdump.c
1
2 /*
3 * fmtdump.c -- compile format file and dump out instructions
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/fmt_scan.h>
14 #include <h/fmt_compile.h>
15 #include <h/scansbr.h>
16
17 static struct swit switches[] = {
18 #define FORMSW 0
19 { "form formatfile", 0 },
20 #define FMTSW 1
21 { "format string", 5 },
22 #define VERSIONSW 2
23 { "version", 0 },
24 #define HELPSW 3
25 { "help", 0 },
26 { NULL, 0 }
27 };
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 int ncomps;
51 char *cp, *form = NULL, *format = NULL;
52 char buf[BUFSIZ], *nfs, **argp, **arguments;
53 struct format *fmt;
54
55 #ifdef LOCALE
56 setlocale(LC_ALL, "");
57 #endif
58 invo_name = r1bindex (argv[0], '/');
59
60 /* read user profile/context */
61 context_read();
62
63 arguments = getarguments (invo_name, argc, argv, 1);
64 argp = arguments;
65
66 while ((cp = *argp++)) {
67 if (*cp == '-') {
68 switch (smatch (++cp, switches)) {
69 case AMBIGSW:
70 ambigsw (cp, switches);
71 done (1);
72 case UNKWNSW:
73 adios (NULL, "-%s unknown", cp);
74
75 case HELPSW:
76 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
77 print_help (buf, switches, 1);
78 done (1);
79 case VERSIONSW:
80 print_version(invo_name);
81 done (1);
82
83 case FORMSW:
84 if (!(form = *argp++) || *form == '-')
85 adios (NULL, "missing argument to %s", argp[-2]);
86 format = NULL;
87 continue;
88 case FMTSW:
89 if (!(format = *argp++) || *format == '-')
90 adios (NULL, "missing argument to %s", argp[-2]);
91 form = NULL;
92 continue;
93
94 }
95 }
96 if (form)
97 adios (NULL, "only one form at a time!");
98 else
99 form = cp;
100 }
101
102 /*
103 * Get new format string. Must be before chdir().
104 */
105 nfs = new_fs (form, format, FORMAT);
106 ncomps = fmt_compile(nfs, &fmt);
107
108 fmt_dump(fmt);
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_LS_COMP: return("LS_COMP");
361 case FT_LS_LIT: return("LS_LIT");
362 case FT_LS_GETENV: return("LS_GETENV");
363 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
364 case FT_LS_DECODE: return("FT_LS_DECODE");
365 case FT_LS_TRIM: return("LS_TRIM");
366 case FT_LV_COMP: return("LV_COMP");
367 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
368 case FT_LV_LIT: return("LV_LIT");
369 case FT_LV_DAT: return("LV_DAT");
370 case FT_LV_STRLEN: return("LV_STRLEN");
371 case FT_LV_PLUS_L: return("LV_PLUS_L");
372 case FT_LV_MINUS_L: return("LV_MINUS_L");
373 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
374 case FT_LV_MODULO_L: return("LV_MODULO_L");
375 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
376 case FT_LS_MONTH: return("LS_MONTH");
377 case FT_LS_LMONTH: return("LS_LMONTH");
378 case FT_LS_ZONE: return("LS_ZONE");
379 case FT_LS_DAY: return("LS_DAY");
380 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
381 case FT_LS_822DATE: return("LS_822DATE");
382 case FT_LS_PRETTY: return("LS_PRETTY");
383 case FT_LV_SEC: return("LV_SEC");
384 case FT_LV_MIN: return("LV_MIN");
385 case FT_LV_HOUR: return("LV_HOUR");
386 case FT_LV_MDAY: return("LV_MDAY");
387 case FT_LV_MON: return("LV_MON");
388 case FT_LV_YEAR: return("LV_YEAR");
389 case FT_LV_YDAY: return("LV_YDAY");
390 case FT_LV_WDAY: return("LV_WDAY");
391 case FT_LV_ZONE: return("LV_ZONE");
392 case FT_LV_CLOCK: return("LV_CLOCK");
393 case FT_LV_RCLOCK: return("LV_RCLOCK");
394 case FT_LV_DAYF: return("LV_DAYF");
395 case FT_LV_DST: return("LV_DST");
396 case FT_LV_ZONEF: return("LV_ZONEF");
397 case FT_LS_ADDR: return("LS_ADDR");
398 case FT_LS_PERS: return("LS_PERS");
399 case FT_LS_MBOX: return("LS_MBOX");
400 case FT_LS_HOST: return("LS_HOST");
401 case FT_LS_PATH: return("LS_PATH");
402 case FT_LS_GNAME: return("LS_GNAME");
403 case FT_LS_NOTE: return("LS_NOTE");
404 case FT_LS_822ADDR: return("LS_822ADDR");
405 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
406 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
407 case FT_LV_INGRPF: return("LV_INGRPF");
408 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
409 case FT_LOCALDATE: return("LOCALDATE");
410 case FT_GMTDATE: return("GMTDATE");
411 case FT_PARSEDATE: return("PARSEDATE");
412 case FT_PARSEADDR: return("PARSEADDR");
413 case FT_FORMATADDR: return("FORMATADDR");
414 case FT_MYMBOX: return("MYMBOX");
415 #ifdef FT_ADDTOSEQ
416 case FT_ADDTOSEQ: return("ADDTOSEQ");
417 #endif
418 case FT_SAVESTR: return("SAVESTR");
419 #ifdef FT_PAUSE
420 case FT_PAUSE: return ("PAUSE");
421 #endif
422 case FT_DONE: return("DONE");
423 case FT_NOP: return("NOP");
424 case FT_GOTO: return("GOTO");
425 case FT_IF_S_NULL: return("IF_S_NULL");
426 case FT_IF_S: return("IF_S");
427 case FT_IF_V_EQ: return("IF_V_EQ");
428 case FT_IF_V_NE: return("IF_V_NE");
429 case FT_IF_V_GT: return("IF_V_GT");
430 case FT_IF_MATCH: return("IF_MATCH");
431 case FT_IF_AMATCH: return("IF_AMATCH");
432 case FT_S_NULL: return("S_NULL");
433 case FT_S_NONNULL: return("S_NONNULL");
434 case FT_V_EQ: return("V_EQ");
435 case FT_V_NE: return("V_NE");
436 case FT_V_GT: return("V_GT");
437 case FT_V_MATCH: return("V_MATCH");
438 case FT_V_AMATCH: return("V_AMATCH");
439 default:
440 printf(buf, "/* ??? #%d */", t);
441 return(buf);
442 }
443 }
444
445 #define FNORD(v, s) if (t & (v)) { \
446 if (i++ > 0) \
447 strcat(buf, "|"); \
448 strcat(buf, s); }
449
450 static char *
451 c_typestr(int t)
452 {
453 register int i;
454 static char buf[64];
455
456 buf[0] = '\0';
457 if (t & ~(CT_ADDR|CT_DATE))
458 printf(buf, "0x%x ", t);
459 strcat(buf, "<");
460 i = 0;
461 FNORD(CT_ADDR, "ADDR");
462 FNORD(CT_DATE, "DATE");
463 strcat(buf, ">");
464 return(buf);
465 }
466
467 static char *
468 c_flagsstr(int t)
469 {
470 register int i;
471 static char buf[64];
472
473 buf[0] = '\0';
474 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB))
475 printf(buf, "0x%x ", t);
476 strcat(buf, "<");
477 i = 0;
478 FNORD(CF_TRUE, "TRUE");
479 FNORD(CF_PARSED, "PARSED");
480 FNORD(CF_DATEFAB, "DATEFAB");
481 strcat(buf, ">");
482 return(buf);
483 }
484 #undef FNORD
485
486 static void
487 litputs(char *s)
488 {
489 if (s) {
490 putc('"', stdout);
491 while (*s)
492 litputc(*s++);
493 putc('"', stdout);
494 } else
495 fputs("<nil>", stdout);
496 }
497
498 static void
499 litputc(char c)
500 {
501 if (c & ~ 0177) {
502 putc('M', stdout);
503 putc('-', stdout);
504 c &= 0177;
505 }
506 if (c < 0x20 || c == 0177) {
507 if (c == '\b') {
508 putc('\\', stdout);
509 putc('b', stdout);
510 } else if (c == '\f') {
511 putc('\\', stdout);
512 putc('f', stdout);
513 } else if (c == '\n') {
514 putc('\\', stdout);
515 putc('n', stdout);
516 } else if (c == '\r') {
517 putc('\\', stdout);
518 putc('r', stdout);
519 } else if (c == '\t') {
520 putc('\\', stdout);
521 putc('t', stdout);
522 } else {
523 putc('^', stdout);
524 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
525 }
526 } else
527 putc(c, stdout);
528 }