]> diplodocus.org Git - nmh/blob - uip/fmttest.c
7e71572196178fc2d448e5c3b4825c80c19cdd67
[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 #include <h/addrsbr.h>
16
17 #define FMTTEST_SWITCHES \
18 X("form formatfile", 0, FORMSW) \
19 X("format string", 5, FMTSW) \
20 X("dump", 0, DUMPSW) \
21 X("address", 0, ADDRSW) \
22 X("raw", 0, RAWSW) \
23 X("date", 0, DATESW) \
24 X("message", 0, MESSAGESW) \
25 X("normalize", 0, NORMSW) \
26 X("nonormalize", 0, NNORMSW) \
27 X("outsize size-in-characters", 0, OUTSIZESW) \
28 X("bufsize size-in-bytes", 0, BUFSZSW) \
29 X("width column-width", 0, WIDTHSW) \
30 X("msgnum number", 0, MSGNUMSW) \
31 X("msgcur flag", 0, MSGCURSW) \
32 X("msgsize size", 0, MSGSIZESW) \
33 X("unseen flag", 0, UNSEENSW) \
34 X("version", 0, VERSIONSW) \
35 X("-component-name component-text", 0, OTHERSW) \
36 X("help", 0, HELPSW) \
37
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(FMTTEST);
40 #undef X
41
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(FMTTEST, switches);
44 #undef X
45
46 /*
47 * An array containing labels used for branch instructions
48 */
49
50 static struct format **lvec = NULL;
51 static int lused = 0;
52 static int lallocated = 0;
53
54 enum mode_t { MESSAGE, ADDRESS, RAW };
55 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
56 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
57
58 /*
59 * static prototypes
60 */
61 static void fmt_dump (char *, struct format *);
62 static void dumpone(struct format *);
63 static void initlabels(struct format *);
64 static int findlabel(struct format *);
65 static void assignlabel(struct format *);
66 static char *f_typestr(int);
67 static char *c_typestr(int);
68 static char *c_flagsstr(int);
69 static void litputs(char *);
70 static void litputc(char);
71 static void process_addresses(struct format *, struct msgs_array *, char *,
72 int, int, int *, int);
73 static void process_raw(struct format *, struct msgs_array *, char *,
74 int, int, int *);
75 static void process_messages(struct format *, struct msgs_array *,
76 struct msgs_array *, char *, char *, int,
77 int, int *);
78
79
80 int
81 main (int argc, char **argv)
82 {
83 char *cp, *form = NULL, *format = NULL, *defformat = FORMAT, *folder = NULL;
84 char buf[BUFSIZ], *nfs, **argp, **arguments, *buffer;
85 struct format *fmt;
86 struct comp *cptr;
87 struct msgs_array msgs = { 0, 0, NULL }, compargs = { 0, 0, NULL};
88 int dump = 0, i;
89 int outputsize = 0, bufsize = 0;
90 int colwidth = -1, msgnum = -1, msgcur = -1, msgsize = -1, msgunseen = -1;
91 int normalize = AD_HOST;
92 enum mode_t mode = MESSAGE;
93 int dat[5];
94
95 #ifdef LOCALE
96 setlocale(LC_ALL, "");
97 #endif
98 invo_name = r1bindex (argv[0], '/');
99
100 /* read user profile/context */
101 context_read();
102
103 arguments = getarguments (invo_name, argc, argv, 1);
104 argp = arguments;
105
106 while ((cp = *argp++)) {
107 if (*cp == '-') {
108 /*
109 * A -- means that we have a component name (like pick);
110 * save the component name and the next argument for the text.
111 */
112 if (*++cp == '-') {
113 app_msgarg(&compargs, --cp);
114 /* Grab next argument for component text */
115 if (!(cp = *argp++))
116 adios(NULL, "missing argument to %s", argp[-2]);
117 app_msgarg(&compargs, cp);
118 continue;
119 }
120 switch (smatch (cp, switches)) {
121 case AMBIGSW:
122 ambigsw (cp, switches);
123 done (1);
124 case UNKWNSW:
125 adios (NULL, "-%s unknown", cp);
126
127 case HELPSW:
128 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
129 print_help (buf, switches, 1);
130 done (0);
131 case VERSIONSW:
132 print_version(invo_name);
133 done (0);
134 case OTHERSW:
135 adios(NULL, "internal argument error!");
136 continue;
137
138 case OUTSIZESW:
139 if (!(cp = *argp++) || *cp == '-')
140 adios(NULL, "missing argument to %s", argp[-2]);
141 if (strcmp(cp, "max") == 0)
142 outputsize = -1;
143 else if (strcmp(cp, "width") == 0)
144 outputsize = sc_width();
145 else
146 outputsize = atoi(cp);
147 continue;
148 case BUFSZSW:
149 if (!(cp = *argp++) || *cp == '-')
150 adios(NULL, "missing argument to %s", argp[-2]);
151 bufsize = atoi(cp);
152 continue;
153
154 case FORMSW:
155 if (!(form = *argp++) || *form == '-')
156 adios (NULL, "missing argument to %s", argp[-2]);
157 format = NULL;
158 continue;
159 case FMTSW:
160 if (!(format = *argp++) || *format == '-')
161 adios (NULL, "missing argument to %s", argp[-2]);
162 form = NULL;
163 continue;
164
165 case NORMSW:
166 normalize = AD_HOST;
167 continue;
168 case NNORMSW:
169 normalize = AD_NHST;
170 continue;
171
172 case ADDRSW:
173 mode = ADDRESS;
174 defformat = DEFADDRFORMAT;
175 continue;
176 case RAWSW:
177 mode = RAW;
178 continue;
179 case MESSAGESW:
180 mode = MESSAGE;
181 defformat = FORMAT;
182 continue;
183 case DATESW:
184 mode = RAW;
185 defformat = DEFDATEFORMAT;
186 continue;
187
188 case WIDTHSW:
189 if (!(cp = *argp++) || *cp == '-')
190 adios(NULL, "missing argument to %s", argp[-2]);
191 colwidth = atoi(cp);
192 continue;
193 case MSGNUMSW:
194 if (!(cp = *argp++) || *cp == '-')
195 adios(NULL, "missing argument to %s", argp[-2]);
196 msgnum = atoi(cp);
197 continue;
198 case MSGCURSW:
199 if (!(cp = *argp++) || *cp == '-')
200 adios(NULL, "missing argument to %s", argp[-2]);
201 msgcur = atoi(cp);
202 continue;
203 case MSGSIZESW:
204 if (!(cp = *argp++) || *cp == '-')
205 adios(NULL, "missing argument to %s", argp[-2]);
206 msgsize = atoi(cp);
207 continue;
208 case UNSEENSW:
209 if (!(cp = *argp++) || *cp == '-')
210 adios(NULL, "missing argument to %s", argp[-2]);
211 msgunseen = atoi(cp);
212 continue;
213
214 case DUMPSW:
215 dump++;
216 continue;
217
218 }
219 }
220
221 /*
222 * Only interpret as a folder if we're in message mode
223 */
224
225 if (mode == MESSAGE && (*cp == '+' || *cp == '@')) {
226 if (folder)
227 adios (NULL, "only one folder at a time!");
228 else
229 folder = pluspath (cp);
230 } else
231 app_msgarg(&msgs, cp);
232 }
233
234 /*
235 * Here's our weird heuristic:
236 *
237 * - We allow -dump without any other arguments.
238 * - If you've given any component arguments, we don't require any
239 * other arguments.
240 * - The arguments are interpreted as folders/messages _if_ we're in
241 * message mode, otherwise pass as strings in the text component.
242 */
243
244 if (!dump && compargs.size == 0 && msgs.size == 0) {
245 adios (NULL, "usage: [switches] [+folder] msgs | strings...",
246 invo_name);
247 }
248
249 /*
250 * Get new format string. Must be before chdir().
251 */
252 nfs = new_fs (form, format, defformat);
253 (void) fmt_compile(nfs, &fmt, 1);
254
255 if (dump) {
256 fmt_dump(nfs, fmt);
257 if (compargs.size == 0 && msgs.size == 0)
258 done(0);
259 }
260
261 /*
262 * If we don't specify a buffer size, allocate a default one.
263 */
264
265 if (bufsize == 0)
266 bufsize = BUFSIZ;
267
268 buffer = mh_xmalloc(bufsize);
269
270 if (outputsize < 0)
271 outputsize = bufsize - 1; /* For the trailing NUL */
272 else if (outputsize == 0) {
273 if (mode == ADDRESS)
274 outputsize = sc_width();
275 else
276 outputsize = bufsize - 1;
277 }
278
279 dat[0] = msgnum;
280 dat[1] = msgcur;
281 dat[2] = msgsize;
282 dat[3] = colwidth == -1 ? outputsize : colwidth;
283 dat[4] = msgunseen;
284
285 if (mode == MESSAGE) {
286 process_messages(fmt, &compargs, &msgs, buffer, folder, bufsize,
287 outputsize, dat);
288 } else {
289 if (compargs.size) {
290 for (i = 0; i < compargs.size; i += 2) {
291 cptr = fmt_findcomp(compargs.msgs[i]);
292 if (cptr)
293 cptr->c_text = getcpy(compargs.msgs[i + 1]);
294 }
295 }
296
297 if (mode == ADDRESS) {
298 fmt_norm = normalize;
299 process_addresses(fmt, &msgs, buffer, bufsize, outputsize,
300 dat, normalize);
301 } else
302 process_raw(fmt, &msgs, buffer, bufsize, outputsize, dat);
303 }
304
305 fmt_free(fmt, 1);
306
307 done(0);
308 return 1;
309 }
310
311 /*
312 * Process each address with fmt_scan().
313 */
314
315 struct pqpair {
316 char *pq_text;
317 char *pq_error;
318 struct pqpair *pq_next;
319 };
320
321 static void
322 process_addresses(struct format *fmt, struct msgs_array *addrs, char *buffer,
323 int bufsize, int outwidth, int *dat, int norm)
324 {
325 int i;
326 char *cp, error[BUFSIZ];
327 struct mailname *mp;
328 struct pqpair *p, *q;
329 struct pqpair pq;
330 struct comp *c;
331
332 if (dat[0] == -1)
333 dat[0] = 0;
334 if (dat[1] == -1)
335 dat[1] = 0;
336 if (dat[2] == -1)
337 dat[2] = 0;
338 if (dat[4] == -1)
339 dat[4] = 0;
340
341 for (i = 0; i < addrs->size; i++) {
342 (q = &pq)->pq_next = NULL;
343 while ((cp = getname(addrs->msgs[i]))) {
344 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
345 adios (NULL, "unable to allocate pqpair memory");
346 if ((mp = getm(cp, NULL, 0, norm, error)) == NULL) {
347 p->pq_text = getcpy(cp);
348 p->pq_error = getcpy(error);
349 } else {
350 p->pq_text = getcpy(mp->m_text);
351 mnfree(mp);
352 }
353 q = (q->pq_next = p);
354 }
355
356 for (p = pq.pq_next; p; p = q) {
357 c = fmt_findcomp("text");
358 if (c) {
359 if (c->c_text)
360 free(c->c_text);
361 c->c_text = p->pq_text;
362 p->pq_text = NULL;
363 }
364 c = fmt_findcomp("error");
365 if (c) {
366 if (c->c_text)
367 free(c->c_text);
368 c->c_text = p->pq_error;
369 p->pq_error = NULL;
370 }
371
372 fmt_scan(fmt, buffer, bufsize, outwidth, dat);
373 fputs(buffer, stdout);
374
375 if (p->pq_text)
376 free(p->pq_text);
377 if (p->pq_error)
378 free(p->pq_error);
379 q = p->pq_next;
380 free(p);
381 }
382 }
383 }
384
385 /*
386 * Process messages and run them through the format engine. A lot taken
387 * from scan.c.
388 */
389
390 static void
391 process_messages(struct format *fmt, struct msgs_array *comps,
392 struct msgs_array *msgs, char *buffer, char *folder,
393 int bufsize, int outwidth, int *dat)
394 {
395 int i, state, msgnum, msgsize = dat[2], num = dat[0], cur = dat[1];
396 int num_unseen_seq = 0, seqnum[NUMATTRS];
397 char *maildir, *cp, name[NAMESZ], rbuf[BUFSIZ];
398 struct msgs *mp;
399 FILE *in;
400
401 if (! folder)
402 folder = getfolder(1);
403
404 maildir = m_maildir(folder);
405
406 if (chdir(maildir) < 0)
407 adios(maildir, "unable to change directory to");
408
409 if (!(mp = folder_read(folder)))
410 adios(NULL, "unable to read folder %s", folder);
411
412 if (mp->nummsg == 0)
413 adios(NULL, "no messages in %s", folder);
414
415 for (i = 0; i < msgs->size; i++)
416 if (!m_convert(mp, msgs->msgs[i]))
417 done(1);
418 seq_setprev(mp); /* set the Previous-Sequence */
419
420 context_replace(pfolder, folder); /* update curren folder */
421 seq_save(mp); /* synchronize message sequences */
422 context_save(); /* save the context file */
423
424 /*
425 * We want to set the unseen flag if requested, so we have to check
426 * the unseen sequence as well.
427 */
428
429 if (dat[4] == -1) {
430 if ((cp = context_find(usequence)) && *cp) {
431 char **ap, *dp;
432
433 dp = getcpy(cp);
434 ap = brkstring(dp, " ", "\n");
435 for (i = 0; ap && *ap; i++, ap++)
436 seqnum[i] = seq_getnum(mp, *ap);
437
438 num_unseen_seq = i;
439 if (dp)
440 free(dp);
441 }
442 }
443
444 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
445 if (is_selected(mp, msgnum)) {
446 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
447 admonish(cp, "unable to open message");
448 continue;
449 }
450
451 fmt_freecomptext();
452
453 if (num == -1)
454 dat[0] = msgnum;
455
456 if (cur == -1)
457 dat[1] = msgnum == mp->curmsg;
458
459 /*
460 * Get our size if we didn't include one
461 */
462
463 if (msgsize == -1) {
464 struct stat st;
465
466 if (fstat(fileno(in), &st) < 0)
467 dat[2] = 0;
468 else
469 dat[2] = st.st_size;
470 }
471
472 /*
473 * Check to see if this is in the unseen sequence
474 */
475
476 dat[4] = 0;
477 for (i = 0; i < num_unseen_seq; i++) {
478 if (in_sequence(mp, seqnum[i], msgnum)) {
479 dat[4] = 1;
480 break;
481 }
482 }
483
484 /*
485 * Read in the message and process the components
486 */
487
488 for (state = FLD;;) {
489 state = m_getfld(state, name, rbuf, sizeof(rbuf), in);
490 switch (state) {
491 case FLD:
492 case FLDPLUS:
493 i = fmt_addcomptext(name, rbuf);
494 if (i != -1) {
495 while (state == FLDPLUS) {
496 state = m_getfld(state, name, rbuf,
497 sizeof(rbuf), in);
498 fmt_appendcomp(i, name, rbuf);
499 }
500 }
501
502 while (state == FLDPLUS)
503 state = m_getfld(state, name, rbuf,
504 sizeof(rbuf), in);
505 break;
506
507 case BODY:
508 if (fmt_findcomp("body")) {
509 if ((i = strlen(rbuf)) < outwidth)
510 state = m_getfld(state, name, rbuf + i,
511 outwidth - 1, in);
512
513 fmt_addcomptext("body", rbuf);
514 }
515 /* fall through */
516
517 default:
518 goto finished;
519 }
520 }
521 finished:
522 fclose(in);
523 fmt_scan(fmt, buffer, bufsize, outwidth, dat);
524 fputs(buffer, stdout);
525 }
526 }
527
528 folder_free(mp);
529 return;
530 }
531
532 /*
533 * Run text through the format engine with no special processing
534 */
535
536 static void
537 process_raw(struct format *fmt, struct msgs_array *text, char *buffer,
538 int bufsize, int outwidth, int *dat)
539 {
540 int i;
541 struct comp *c;
542
543 if (dat[0] == -1)
544 dat[0] = 0;
545 if (dat[1] == -1)
546 dat[1] = 0;
547 if (dat[2] == -1)
548 dat[2] = 0;
549 if (dat[4] == -1)
550 dat[4] = 0;
551
552 c = fmt_findcomp("text");
553
554 for (i = 0; i < text->size; i++) {
555 if (c != NULL) {
556 if (c->c_text != NULL)
557 free(c->c_text);
558 c->c_text = getcpy(text->msgs[i]);
559 }
560
561 fmt_scan(fmt, buffer, bufsize, outwidth, dat);
562 fputs(buffer, stdout);
563 }
564 }
565
566 static void
567 fmt_dump (char *nfs, struct format *fmth)
568 {
569 struct format *fmt;
570
571 printf("Instruction dump of format string: \n%s\n", nfs);
572
573 initlabels(fmth);
574
575 /* Dump them out! */
576 for (fmt = fmth; fmt; ++fmt) {
577 dumpone(fmt);
578 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
579 break;
580 }
581 }
582
583 static void
584 dumpone(struct format *fmt)
585 {
586 register int i;
587
588 if ((i = findlabel(fmt)) >= 0)
589 printf("L%d:", i);
590 putchar('\t');
591
592 fputs(f_typestr((int)fmt->f_type), stdout);
593
594 switch (fmt->f_type) {
595
596 case FT_COMP:
597 case FT_LS_COMP:
598 case FT_LV_COMPFLAG:
599 case FT_LV_COMP:
600 printf(", comp ");
601 litputs(fmt->f_comp->c_name);
602 if (fmt->f_comp->c_type)
603 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
604 if (fmt->f_comp->c_flags)
605 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
606 break;
607
608 case FT_LV_SEC:
609 case FT_LV_MIN:
610 case FT_LV_HOUR:
611 case FT_LV_MDAY:
612 case FT_LV_MON:
613 case FT_LS_MONTH:
614 case FT_LS_LMONTH:
615 case FT_LS_ZONE:
616 case FT_LV_YEAR:
617 case FT_LV_WDAY:
618 case FT_LS_DAY:
619 case FT_LS_WEEKDAY:
620 case FT_LV_YDAY:
621 case FT_LV_ZONE:
622 case FT_LV_CLOCK:
623 case FT_LV_RCLOCK:
624 case FT_LV_DAYF:
625 case FT_LV_ZONEF:
626 case FT_LV_DST:
627 case FT_LS_822DATE:
628 case FT_LS_PRETTY:
629 case FT_LOCALDATE:
630 case FT_GMTDATE:
631 case FT_PARSEDATE:
632 printf(", c_name ");
633 litputs(fmt->f_comp->c_name);
634 if (fmt->f_comp->c_type)
635 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
636 if (fmt->f_comp->c_flags)
637 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
638 break;
639
640 case FT_LS_ADDR:
641 case FT_LS_PERS:
642 case FT_LS_MBOX:
643 case FT_LS_HOST:
644 case FT_LS_PATH:
645 case FT_LS_GNAME:
646 case FT_LS_NOTE:
647 case FT_LS_822ADDR:
648 case FT_LV_HOSTTYPE:
649 case FT_LV_INGRPF:
650 case FT_LV_NOHOSTF:
651 case FT_LS_FRIENDLY:
652 case FT_PARSEADDR:
653 case FT_MYMBOX:
654 printf(", c_name ");
655 litputs(fmt->f_comp->c_name);
656 if (fmt->f_comp->c_type)
657 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
658 if (fmt->f_comp->c_flags)
659 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
660 break;
661
662 case FT_COMPF:
663 printf(", width %d, fill '", fmt->f_width);
664 litputc(fmt->f_fill);
665 printf("' name ");
666 litputs(fmt->f_comp->c_name);
667 if (fmt->f_comp->c_type)
668 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
669 if (fmt->f_comp->c_flags)
670 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
671 break;
672
673 case FT_STRF:
674 case FT_NUMF:
675 printf(", width %d, fill '", fmt->f_width);
676 litputc(fmt->f_fill);
677 putchar('\'');
678 break;
679
680 case FT_LIT:
681 #ifdef FT_LIT_FORCE
682 case FT_LIT_FORCE:
683 #endif
684 putchar(' ');
685 litputs(fmt->f_text);
686 break;
687
688 case FT_LITF:
689 printf(", width %d, fill '", fmt->f_width);
690 litputc(fmt->f_fill);
691 printf("' ");
692 litputs(fmt->f_text);
693 break;
694
695 case FT_CHAR:
696 putchar(' ');
697 putchar('\'');
698 litputc(fmt->f_char);
699 putchar('\'');
700 break;
701
702
703 case FT_IF_S:
704 case FT_IF_S_NULL:
705 case FT_IF_MATCH:
706 case FT_IF_AMATCH:
707 printf(" continue else goto");
708 case FT_GOTO:
709 i = findlabel(fmt + fmt->f_skip);
710 printf(" L%d", i);
711 break;
712
713 case FT_IF_V_EQ:
714 case FT_IF_V_NE:
715 case FT_IF_V_GT:
716 i = findlabel(fmt + fmt->f_skip);
717 printf(" %d continue else goto L%d", fmt->f_value, i);
718 break;
719
720 case FT_V_EQ:
721 case FT_V_NE:
722 case FT_V_GT:
723 case FT_LV_LIT:
724 case FT_LV_PLUS_L:
725 case FT_LV_MINUS_L:
726 case FT_LV_DIVIDE_L:
727 case FT_LV_MODULO_L:
728 printf(" value %d", fmt->f_value);
729 break;
730
731 case FT_LS_LIT:
732 printf(" str ");
733 litputs(fmt->f_text);
734 break;
735
736 case FT_LS_GETENV:
737 printf(" getenv ");
738 litputs(fmt->f_text);
739 break;
740
741 case FT_LS_DECODECOMP:
742 printf(", comp ");
743 litputs(fmt->f_comp->c_name);
744 break;
745
746 case FT_LS_DECODE:
747 break;
748
749 case FT_LS_TRIM:
750 printf(", width %d", fmt->f_width);
751 break;
752
753 case FT_LV_DAT:
754 printf(", value dat[%d]", fmt->f_value);
755 break;
756 }
757 putchar('\n');
758 }
759
760 /*
761 * Iterate over all instructions and assign labels to the targets of
762 * branch statements
763 */
764
765 static void
766 initlabels(struct format *fmth)
767 {
768 struct format *fmt, *addr;
769 int i;
770
771 /* Assign labels */
772 for (fmt = fmth; fmt; ++fmt) {
773 i = fmt->f_type;
774 if (i == FT_IF_S ||
775 i == FT_IF_S_NULL ||
776 i == FT_IF_V_EQ ||
777 i == FT_IF_V_NE ||
778 i == FT_IF_V_GT ||
779 i == FT_IF_MATCH ||
780 i == FT_IF_AMATCH ||
781 i == FT_GOTO) {
782 addr = fmt + fmt->f_skip;
783 if (findlabel(addr) < 0)
784 assignlabel(addr);
785 }
786 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
787 break;
788 }
789 }
790
791
792 static int
793 findlabel(struct format *addr)
794 {
795 register int i;
796
797 for (i = 0; i < lused; ++i)
798 if (addr == lvec[i])
799 return(i);
800 return(-1);
801 }
802
803 static void
804 assignlabel(struct format *addr)
805 {
806 if (lused >= lallocated) {
807 lallocated += 64;
808 lvec = (struct format **)
809 mh_xrealloc(lvec, sizeof(struct format *) * lallocated);
810 }
811
812 lvec[lused++] = addr;
813 }
814
815 static char *
816 f_typestr(int t)
817 {
818 static char buf[32];
819
820 switch (t) {
821 case FT_COMP: return("COMP");
822 case FT_COMPF: return("COMPF");
823 case FT_LIT: return("LIT");
824 case FT_LITF: return("LITF");
825 #ifdef FT_LIT_FORCE
826 case FT_LIT_FORCE: return("LIT_FORCE");
827 #endif
828 case FT_CHAR: return("CHAR");
829 case FT_NUM: return("NUM");
830 case FT_NUMF: return("NUMF");
831 case FT_STR: return("STR");
832 case FT_STRF: return("STRF");
833 case FT_STRFW: return("STRFW");
834 case FT_PUTADDR: return("PUTADDR");
835 case FT_STRLIT: return("STRLIT");
836 case FT_STRLITZ: return("STRLITZ");
837 case FT_LS_COMP: return("LS_COMP");
838 case FT_LS_LIT: return("LS_LIT");
839 case FT_LS_GETENV: return("LS_GETENV");
840 case FT_LS_DECODECOMP: return("FT_LS_DECODECOMP");
841 case FT_LS_DECODE: return("FT_LS_DECODE");
842 case FT_LS_TRIM: return("LS_TRIM");
843 case FT_LV_COMP: return("LV_COMP");
844 case FT_LV_COMPFLAG: return("LV_COMPFLAG");
845 case FT_LV_LIT: return("LV_LIT");
846 case FT_LV_DAT: return("LV_DAT");
847 case FT_LV_STRLEN: return("LV_STRLEN");
848 case FT_LV_PLUS_L: return("LV_PLUS_L");
849 case FT_LV_MINUS_L: return("LV_MINUS_L");
850 case FT_LV_DIVIDE_L: return("LV_DIVIDE_L");
851 case FT_LV_MODULO_L: return("LV_MODULO_L");
852 case FT_LV_CHAR_LEFT: return("LV_CHAR_LEFT");
853 case FT_LS_MONTH: return("LS_MONTH");
854 case FT_LS_LMONTH: return("LS_LMONTH");
855 case FT_LS_ZONE: return("LS_ZONE");
856 case FT_LS_DAY: return("LS_DAY");
857 case FT_LS_WEEKDAY: return("LS_WEEKDAY");
858 case FT_LS_822DATE: return("LS_822DATE");
859 case FT_LS_PRETTY: return("LS_PRETTY");
860 case FT_LV_SEC: return("LV_SEC");
861 case FT_LV_MIN: return("LV_MIN");
862 case FT_LV_HOUR: return("LV_HOUR");
863 case FT_LV_MDAY: return("LV_MDAY");
864 case FT_LV_MON: return("LV_MON");
865 case FT_LV_YEAR: return("LV_YEAR");
866 case FT_LV_YDAY: return("LV_YDAY");
867 case FT_LV_WDAY: return("LV_WDAY");
868 case FT_LV_ZONE: return("LV_ZONE");
869 case FT_LV_CLOCK: return("LV_CLOCK");
870 case FT_LV_RCLOCK: return("LV_RCLOCK");
871 case FT_LV_DAYF: return("LV_DAYF");
872 case FT_LV_DST: return("LV_DST");
873 case FT_LV_ZONEF: return("LV_ZONEF");
874 case FT_LS_ADDR: return("LS_ADDR");
875 case FT_LS_PERS: return("LS_PERS");
876 case FT_LS_MBOX: return("LS_MBOX");
877 case FT_LS_HOST: return("LS_HOST");
878 case FT_LS_PATH: return("LS_PATH");
879 case FT_LS_GNAME: return("LS_GNAME");
880 case FT_LS_NOTE: return("LS_NOTE");
881 case FT_LS_822ADDR: return("LS_822ADDR");
882 case FT_LS_FRIENDLY: return("LS_FRIENDLY");
883 case FT_LV_HOSTTYPE: return("LV_HOSTTYPE");
884 case FT_LV_INGRPF: return("LV_INGRPF");
885 case FT_LV_NOHOSTF: return("LV_NOHOSTF");
886 case FT_LOCALDATE: return("LOCALDATE");
887 case FT_GMTDATE: return("GMTDATE");
888 case FT_PARSEDATE: return("PARSEDATE");
889 case FT_PARSEADDR: return("PARSEADDR");
890 case FT_FORMATADDR: return("FORMATADDR");
891 case FT_CONCATADDR: return("CONCATADDR");
892 case FT_MYMBOX: return("MYMBOX");
893 #ifdef FT_ADDTOSEQ
894 case FT_ADDTOSEQ: return("ADDTOSEQ");
895 #endif
896 case FT_SAVESTR: return("SAVESTR");
897 #ifdef FT_PAUSE
898 case FT_PAUSE: return ("PAUSE");
899 #endif
900 case FT_DONE: return("DONE");
901 case FT_NOP: return("NOP");
902 case FT_GOTO: return("GOTO");
903 case FT_IF_S_NULL: return("IF_S_NULL");
904 case FT_IF_S: return("IF_S");
905 case FT_IF_V_EQ: return("IF_V_EQ");
906 case FT_IF_V_NE: return("IF_V_NE");
907 case FT_IF_V_GT: return("IF_V_GT");
908 case FT_IF_MATCH: return("IF_MATCH");
909 case FT_IF_AMATCH: return("IF_AMATCH");
910 case FT_S_NULL: return("S_NULL");
911 case FT_S_NONNULL: return("S_NONNULL");
912 case FT_V_EQ: return("V_EQ");
913 case FT_V_NE: return("V_NE");
914 case FT_V_GT: return("V_GT");
915 case FT_V_MATCH: return("V_MATCH");
916 case FT_V_AMATCH: return("V_AMATCH");
917 default:
918 printf(buf, "/* ??? #%d */", t);
919 return(buf);
920 }
921 }
922
923 #define FNORD(v, s) if (t & (v)) { \
924 if (i++ > 0) \
925 strcat(buf, "|"); \
926 strcat(buf, s); }
927
928 static char *
929 c_typestr(int t)
930 {
931 register int i;
932 static char buf[64];
933
934 buf[0] = '\0';
935 if (t & ~(CT_ADDR|CT_DATE))
936 printf(buf, "0x%x ", t);
937 strcat(buf, "<");
938 i = 0;
939 FNORD(CT_ADDR, "ADDR");
940 FNORD(CT_DATE, "DATE");
941 strcat(buf, ">");
942 return(buf);
943 }
944
945 static char *
946 c_flagsstr(int t)
947 {
948 register int i;
949 static char buf[64];
950
951 buf[0] = '\0';
952 if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB|CF_TRIMMED))
953 printf(buf, "0x%x ", t);
954 strcat(buf, "<");
955 i = 0;
956 FNORD(CF_TRUE, "TRUE");
957 FNORD(CF_PARSED, "PARSED");
958 FNORD(CF_DATEFAB, "DATEFAB");
959 FNORD(CF_TRIMMED, "TRIMMED");
960 strcat(buf, ">");
961 return(buf);
962 }
963 #undef FNORD
964
965 static void
966 litputs(char *s)
967 {
968 if (s) {
969 putc('"', stdout);
970 while (*s)
971 litputc(*s++);
972 putc('"', stdout);
973 } else
974 fputs("<nil>", stdout);
975 }
976
977 static void
978 litputc(char c)
979 {
980 if (c & ~ 0177) {
981 putc('M', stdout);
982 putc('-', stdout);
983 c &= 0177;
984 }
985 if (c < 0x20 || c == 0177) {
986 if (c == '\b') {
987 putc('\\', stdout);
988 putc('b', stdout);
989 } else if (c == '\f') {
990 putc('\\', stdout);
991 putc('f', stdout);
992 } else if (c == '\n') {
993 putc('\\', stdout);
994 putc('n', stdout);
995 } else if (c == '\r') {
996 putc('\\', stdout);
997 putc('r', stdout);
998 } else if (c == '\t') {
999 putc('\\', stdout);
1000 putc('t', stdout);
1001 } else {
1002 putc('^', stdout);
1003 putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
1004 }
1005 } else
1006 putc(c, stdout);
1007 }