]> diplodocus.org Git - nmh/blob - uip/fmttest.c
ecbc4481b60c611f8436d0c73d80b57a15a5ec00
[nmh] / uip / fmttest.c
1
2 /*
3 * fmttest.c -- A program to help test and debug format instructions
4 *
5 * This code is Copyright (c) 2012, 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/utils.h>
14 #include <h/scansbr.h>
15
16 #define FMTTEST_SWITCHES \
17 X("form formatfile", 0, FORMSW) \
18 X("format string", 5, FMTSW) \
19 X("dump", 0, DUMPSW) \
20 X("address", 0, ADDRSW) \
21 X("date", 0, DATESW) \
22 X("width", 0, WIDTHSW) \
23 X("bufsize size-in-bytes", 0, BUFSZSW) \
24 X("version", 0, VERSIONSW) \
25 X("-component-name component-text", 0, OTHERSW) \
26 X("help", 0, HELPSW) \
27
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(FMTTEST);
30 #undef X
31
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(FMTTEST, switches);
34 #undef X
35
36 /*
37 * An array containing labels used for branch instructions
38 */
39
40 static struct format **lvec = NULL;
41 static int lused = 0;
42 static int lallocated = 0;
43
44 /*
45 * static prototypes
46 */
47 static void fmt_dump (char *, struct format *);
48 static void dumpone(struct format *);
49 static int findlabel(struct format *);
50 static void assignlabel(struct format *);
51 static char *f_typestr(int);
52 static char *c_typestr(int);
53 static char *c_flagsstr(int);
54 static void litputs(char *);
55 static void litputc(char);
56
57
58 int
59 main (int argc, char **argv)
60 {
61 char *cp, *form = NULL, *format = NULL, *folder = NULL;
62 char buf[BUFSIZ], *nfs, **argp, **arguments;
63 char **compargs = NULL;
64 struct format *fmt;
65 struct comp *cptr;
66 struct msgs_array msgs = { 0, 0, NULL };
67 int dump = 0, compp = 0, comppalloc = 0, i;
68 int width = 0, bufsize = 0;
69 int dat[5];
70
71 #ifdef LOCALE
72 setlocale(LC_ALL, "");
73 #endif
74 invo_name = r1bindex (argv[0], '/');
75
76 /* read user profile/context */
77 context_read();
78
79 arguments = getarguments (invo_name, argc, argv, 1);
80 argp = arguments;
81
82 while ((cp = *argp++)) {
83 if (*cp == '-') {
84 /*
85 * A -- means that we have a component name (like pick);
86 * save the component name and the next argument for the text.
87 */
88 if (*++cp == '-') {
89 /* it's -3 because of this ptr, next ptr, and trailing NULL */
90 if (compp > comppalloc - 3) {
91 comppalloc += 32;
92 compargs = (char **) mh_xrealloc(compargs, sizeof(char *) *
93 comppalloc);
94 }
95 compargs[compp++] = --cp;
96 /* Grab next argument for component text */
97 if (!(cp = *argp++))
98 adios(NULL, "missing argument to %s", argp[-2]);
99 compargs[compp++] = cp;
100 continue;
101 }
102 switch (smatch (cp, switches)) {
103 case AMBIGSW:
104 ambigsw (cp, switches);
105 done (1);
106 case UNKWNSW:
107 adios (NULL, "-%s unknown", cp);
108
109 case HELPSW:
110 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
111 print_help (buf, switches, 1);
112 done (0);
113 case VERSIONSW:
114 print_version(invo_name);
115 done (0);
116 case OTHERSW:
117 adios(NULL, "internal argument error!");
118 continue;
119
120 case WIDTHSW:
121 if (!(cp = *argp++) || *cp == '-')
122 adios(NULL, "missing argument to %s", argp[-2]);
123 width = atoi(cp);
124 continue;
125 case BUFSZSW:
126 if (!(cp = *argp++) || *cp == '-')
127 adios(NULL, "missing argument to %s", argp[-2]);
128 bufsize = atoi(cp);
129 continue;
130
131 case FORMSW:
132 if (!(form = *argp++) || *form == '-')
133 adios (NULL, "missing argument to %s", argp[-2]);
134 format = NULL;
135 continue;
136 case FMTSW:
137 if (!(format = *argp++) || *format == '-')
138 adios (NULL, "missing argument to %s", argp[-2]);
139 form = NULL;
140 continue;
141
142 case DUMPSW:
143 dump++;
144 continue;
145
146 }
147 }
148 if (*cp == '+' || *cp == '@') {
149 if (folder)
150 adios (NULL, "only one folder at a time!");
151 else
152 folder = pluspath (cp);
153 } else
154 app_msgarg(&msgs, cp);
155 }
156
157 if (compargs)
158 compargs[compp] = NULL;
159
160 /*
161 * Here's our weird heuristic:
162 *
163 * - We allow -dump without any other arguments.
164 * - If you've given any component arguments, we don't require any
165 * other arguments.
166 * - The arguments are interpreted as folders/messages.
167 */
168
169 if (!dump && !compargs && msgs.size == 0) {
170 adios (NULL, "usage: [switches] [+folder] msgs...]", invo_name);
171 }
172
173 /*
174 * Get new format string. Must be before chdir().
175 */
176 nfs = new_fs (form, format, FORMAT);
177 (void) fmt_compile(nfs, &fmt, 1);
178
179 if (dump)
180 fmt_dump(nfs, fmt);
181
182 if (compargs) {
183 for (i = 0; compargs[i] != NULL; i += 2) {
184 cptr = fmt_findcomp(compargs[i]);
185 if (cptr)
186 cptr->c_text = getcpy(compargs[i + 1]);
187 }
188 }
189
190 if (width == 0)
191 width = sc_width();
192
193 /*
194 * If we don't specify a buffer size, allocate a default one.
195 */
196
197 if (bufsize == 0) {
198 }
199
200 fmt_free(fmt, 1);
201
202 done(0);
203 return 1;
204 }
205
206 static void
207 fmt_dump (char *nfs, struct format *fmth)
208 {
209 int i;
210 register struct format *fmt, *addr;
211
212 printf("Instruction dump of format string: \n%s\n", nfs);
213
214 /* Assign labels */
215 for (fmt = fmth; fmt; ++fmt) {
216 i = fmt->f_type;
217 if (i == FT_IF_S ||
218 i == FT_IF_S_NULL ||
219 i == FT_IF_V_EQ ||
220 i == FT_IF_V_NE ||
221 i == FT_IF_V_GT ||
222 i == FT_IF_MATCH ||
223 i == FT_IF_AMATCH ||
224 i == FT_GOTO) {
225 addr = fmt + fmt->f_skip;
226 if (findlabel(addr) < 0)
227 assignlabel(addr);
228 }
229 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
230 break;
231 }
232
233 /* Dump them out! */
234 for (fmt = fmth; fmt; ++fmt) {
235 dumpone(fmt);
236 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
237 break;
238 }
239 }
240
241 static void
242 dumpone(struct format *fmt)
243 {
244 register int i;
245
246 if ((i = findlabel(fmt)) >= 0)
247 printf("L%d:", i);
248 putchar('\t');
249
250 fputs(f_typestr((int)fmt->f_type), stdout);
251
252 switch (fmt->f_type) {
253
254 case FT_COMP:
255 case FT_LS_COMP:
256 case FT_LV_COMPFLAG:
257 case FT_LV_COMP:
258 printf(", comp ");
259 litputs(fmt->f_comp->c_name);
260 if (fmt->f_comp->c_type)
261 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
262 if (fmt->f_comp->c_flags)
263 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
264 break;
265
266 case FT_LV_SEC:
267 case FT_LV_MIN:
268 case FT_LV_HOUR:
269 case FT_LV_MDAY:
270 case FT_LV_MON:
271 case FT_LS_MONTH:
272 case FT_LS_LMONTH:
273 case FT_LS_ZONE:
274 case FT_LV_YEAR:
275 case FT_LV_WDAY:
276 case FT_LS_DAY:
277 case FT_LS_WEEKDAY:
278 case FT_LV_YDAY:
279 case FT_LV_ZONE:
280 case FT_LV_CLOCK:
281 case FT_LV_RCLOCK:
282 case FT_LV_DAYF:
283 case FT_LV_ZONEF:
284 case FT_LV_DST:
285 case FT_LS_822DATE:
286 case FT_LS_PRETTY:
287 case FT_LOCALDATE:
288 case FT_GMTDATE:
289 case FT_PARSEDATE:
290 printf(", c_name ");
291 litputs(fmt->f_comp->c_name);
292 if (fmt->f_comp->c_type)
293 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
294 if (fmt->f_comp->c_flags)
295 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
296 break;
297
298 case FT_LS_ADDR:
299 case FT_LS_PERS:
300 case FT_LS_MBOX:
301 case FT_LS_HOST:
302 case FT_LS_PATH:
303 case FT_LS_GNAME:
304 case FT_LS_NOTE:
305 case FT_LS_822ADDR:
306 case FT_LV_HOSTTYPE:
307 case FT_LV_INGRPF:
308 case FT_LV_NOHOSTF:
309 case FT_LS_FRIENDLY:
310 case FT_PARSEADDR:
311 case FT_MYMBOX:
312 printf(", c_name ");
313 litputs(fmt->f_comp->c_name);
314 if (fmt->f_comp->c_type)
315 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
316 if (fmt->f_comp->c_flags)
317 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
318 break;
319
320 case FT_COMPF:
321 printf(", width %d, fill '", fmt->f_width);
322 litputc(fmt->f_fill);
323 printf("' name ");
324 litputs(fmt->f_comp->c_name);
325 if (fmt->f_comp->c_type)
326 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
327 if (fmt->f_comp->c_flags)
328 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
329 break;
330
331 case FT_STRF:
332 case FT_NUMF:
333 printf(", width %d, fill '", fmt->f_width);
334 litputc(fmt->f_fill);
335 putchar('\'');
336 break;
337
338 case FT_LIT:
339 #ifdef FT_LIT_FORCE
340 case FT_LIT_FORCE:
341 #endif
342 putchar(' ');
343 litputs(fmt->f_text);
344 break;
345
346 case FT_LITF:
347 printf(", width %d, fill '", fmt->f_width);
348 litputc(fmt->f_fill);
349 printf("' ");
350 litputs(fmt->f_text);
351 break;
352
353 case FT_CHAR:
354 putchar(' ');
355 putchar('\'');
356 litputc(fmt->f_char);
357 putchar('\'');
358 break;
359
360
361 case FT_IF_S:
362 case FT_IF_S_NULL:
363 case FT_IF_MATCH:
364 case FT_IF_AMATCH:
365 printf(" continue else goto");
366 case FT_GOTO:
367 i = findlabel(fmt + fmt->f_skip);
368 printf(" L%d", i);
369 break;
370
371 case FT_IF_V_EQ:
372 case FT_IF_V_NE:
373 case FT_IF_V_GT:
374 i = findlabel(fmt + fmt->f_skip);
375 printf(" %d continue else goto L%d", fmt->f_value, i);
376 break;
377
378 case FT_V_EQ:
379 case FT_V_NE:
380 case FT_V_GT:
381 case FT_LV_LIT:
382 case FT_LV_PLUS_L:
383 case FT_LV_MINUS_L:
384 case FT_LV_DIVIDE_L:
385 case FT_LV_MODULO_L:
386 printf(" value %d", fmt->f_value);
387 break;
388
389 case FT_LS_LIT:
390 printf(" str ");
391 litputs(fmt->f_text);
392 break;
393
394 case FT_LS_GETENV:
395 printf(" getenv ");
396 litputs(fmt->f_text);
397 break;
398
399 case FT_LS_DECODECOMP:
400 printf(", comp ");
401 litputs(fmt->f_comp->c_name);
402 break;
403
404 case FT_LS_DECODE:
405 break;
406
407 case FT_LS_TRIM:
408 printf(", width %d", fmt->f_width);
409 break;
410
411 case FT_LV_DAT:
412 printf(", value dat[%d]", fmt->f_value);
413 break;
414 }
415 putchar('\n');
416 }
417
418 static int
419 findlabel(struct format *addr)
420 {
421 register int i;
422
423 for (i = 0; i < lused; ++i)
424 if (addr == lvec[i])
425 return(i);
426 return(-1);
427 }
428
429 static void
430 assignlabel(struct format *addr)
431 {
432 if (lused >= lallocated) {
433 lallocated += 64;
434 lvec = (struct format **)
435 mh_xrealloc(lvec, sizeof(struct format *) * lallocated);
436 }
437
438 lvec[lused++] = addr;
439 }
440
441 static char *
442 f_typestr(int t)
443 {
444 static char buf[32];
445
446 switch (t) {
447 case FT_COMP: return("COMP");
448 case FT_COMPF: return("COMPF");
449 case FT_LIT: return("LIT");
450 case FT_LITF: return("LITF");
451 #ifdef FT_LIT_FORCE
452 case FT_LIT_FORCE: return("LIT_FORCE");
453 #endif
454 case FT_CHAR: return("CHAR");
455 case FT_NUM: return("NUM");
456 case FT_NUMF: return("NUMF");
457 case FT_STR: return("STR");
458 case FT_STRF: return("STRF");
459 case FT_STRFW: return("STRFW");
460 case FT_PUTADDR: return("PUTADDR");
461 case FT_STRLIT: return("STRLIT");
462 case FT_STRLITZ: return("STRLITZ");
463 case FT_LS_COMP: return("LS_COMP");
464 case FT_LS_LIT: return("LS_LIT");
465 case FT_LS_GETENV: return("LS_GETENV");
466 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
467 case FT_LS_DECODE: return("FT_LS_DECODE");
468 case FT_LS_TRIM: return("LS_TRIM");
469 case FT_LV_COMP: return("LV_COMP");
470 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
471 case FT_LV_LIT: return("LV_LIT");
472 case FT_LV_DAT: return("LV_DAT");
473 case FT_LV_STRLEN: return("LV_STRLEN");
474 case FT_LV_PLUS_L: return("LV_PLUS_L");
475 case FT_LV_MINUS_L: return("LV_MINUS_L");
476 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
477 case FT_LV_MODULO_L: return("LV_MODULO_L");
478 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
479 case FT_LS_MONTH: return("LS_MONTH");
480 case FT_LS_LMONTH: return("LS_LMONTH");
481 case FT_LS_ZONE: return("LS_ZONE");
482 case FT_LS_DAY: return("LS_DAY");
483 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
484 case FT_LS_822DATE: return("LS_822DATE");
485 case FT_LS_PRETTY: return("LS_PRETTY");
486 case FT_LV_SEC: return("LV_SEC");
487 case FT_LV_MIN: return("LV_MIN");
488 case FT_LV_HOUR: return("LV_HOUR");
489 case FT_LV_MDAY: return("LV_MDAY");
490 case FT_LV_MON: return("LV_MON");
491 case FT_LV_YEAR: return("LV_YEAR");
492 case FT_LV_YDAY: return("LV_YDAY");
493 case FT_LV_WDAY: return("LV_WDAY");
494 case FT_LV_ZONE: return("LV_ZONE");
495 case FT_LV_CLOCK: return("LV_CLOCK");
496 case FT_LV_RCLOCK: return("LV_RCLOCK");
497 case FT_LV_DAYF: return("LV_DAYF");
498 case FT_LV_DST: return("LV_DST");
499 case FT_LV_ZONEF: return("LV_ZONEF");
500 case FT_LS_ADDR: return("LS_ADDR");
501 case FT_LS_PERS: return("LS_PERS");
502 case FT_LS_MBOX: return("LS_MBOX");
503 case FT_LS_HOST: return("LS_HOST");
504 case FT_LS_PATH: return("LS_PATH");
505 case FT_LS_GNAME: return("LS_GNAME");
506 case FT_LS_NOTE: return("LS_NOTE");
507 case FT_LS_822ADDR: return("LS_822ADDR");
508 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
509 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
510 case FT_LV_INGRPF: return("LV_INGRPF");
511 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
512 case FT_LOCALDATE: return("LOCALDATE");
513 case FT_GMTDATE: return("GMTDATE");
514 case FT_PARSEDATE: return("PARSEDATE");
515 case FT_PARSEADDR: return("PARSEADDR");
516 case FT_FORMATADDR: return("FORMATADDR");
517 case FT_CONCATADDR: return("CONCATADDR");
518 case FT_MYMBOX: return("MYMBOX");
519 #ifdef FT_ADDTOSEQ
520 case FT_ADDTOSEQ: return("ADDTOSEQ");
521 #endif
522 case FT_SAVESTR: return("SAVESTR");
523 #ifdef FT_PAUSE
524 case FT_PAUSE: return ("PAUSE");
525 #endif
526 case FT_DONE: return("DONE");
527 case FT_NOP: return("NOP");
528 case FT_GOTO: return("GOTO");
529 case FT_IF_S_NULL: return("IF_S_NULL");
530 case FT_IF_S: return("IF_S");
531 case FT_IF_V_EQ: return("IF_V_EQ");
532 case FT_IF_V_NE: return("IF_V_NE");
533 case FT_IF_V_GT: return("IF_V_GT");
534 case FT_IF_MATCH: return("IF_MATCH");
535 case FT_IF_AMATCH: return("IF_AMATCH");
536 case FT_S_NULL: return("S_NULL");
537 case FT_S_NONNULL: return("S_NONNULL");
538 case FT_V_EQ: return("V_EQ");
539 case FT_V_NE: return("V_NE");
540 case FT_V_GT: return("V_GT");
541 case FT_V_MATCH: return("V_MATCH");
542 case FT_V_AMATCH: return("V_AMATCH");
543 default:
544 printf(buf, "/* ??? #%d */", t);
545 return(buf);
546 }
547 }
548
549 #define FNORD(v, s) if (t & (v)) { \
550 if (i++ > 0) \
551 strcat(buf, "|"); \
552 strcat(buf, s); }
553
554 static char *
555 c_typestr(int t)
556 {
557 register int i;
558 static char buf[64];
559
560 buf[0] = '\0';
561 if (t & ~(CT_ADDR|CT_DATE))
562 printf(buf, "0x%x ", t);
563 strcat(buf, "<");
564 i = 0;
565 FNORD(CT_ADDR, "ADDR");
566 FNORD(CT_DATE, "DATE");
567 strcat(buf, ">");
568 return(buf);
569 }
570
571 static char *
572 c_flagsstr(int t)
573 {
574 register int i;
575 static char buf[64];
576
577 buf[0] = '\0';
578 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED))
579 printf(buf, "0x%x ", t);
580 strcat(buf, "<");
581 i = 0;
582 FNORD(CF_TRUE, "TRUE");
583 FNORD(CF_PARSED, "PARSED");
584 FNORD(CF_DATEFAB, "DATEFAB");
585 FNORD(CF_TRIMMED, "TRIMMED");
586 strcat(buf, ">");
587 return(buf);
588 }
589 #undef FNORD
590
591 static void
592 litputs(char *s)
593 {
594 if (s) {
595 putc('"', stdout);
596 while (*s)
597 litputc(*s++);
598 putc('"', stdout);
599 } else
600 fputs("<nil>", stdout);
601 }
602
603 static void
604 litputc(char c)
605 {
606 if (c & ~ 0177) {
607 putc('M', stdout);
608 putc('-', stdout);
609 c &= 0177;
610 }
611 if (c < 0x20 || c == 0177) {
612 if (c == '\b') {
613 putc('\\', stdout);
614 putc('b', stdout);
615 } else if (c == '\f') {
616 putc('\\', stdout);
617 putc('f', stdout);
618 } else if (c == '\n') {
619 putc('\\', stdout);
620 putc('n', stdout);
621 } else if (c == '\r') {
622 putc('\\', stdout);
623 putc('r', stdout);
624 } else if (c == '\t') {
625 putc('\\', stdout);
626 putc('t', stdout);
627 } else {
628 putc('^', stdout);
629 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
630 }
631 } else
632 putc(c, stdout);
633 }