]> diplodocus.org Git - nmh/blob - uip/fmttest.c
Use va_copy() to get a copy of va_list, instead of using original.
[nmh] / uip / fmttest.c
1 /* fmttest.c -- A program to help test and debug format instructions
2 *
3 * This code is Copyright (c) 2012, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/fmt_scan.h>
10 #include <h/fmt_compile.h>
11 #include <h/utils.h>
12 #include <h/scansbr.h>
13 #include <h/addrsbr.h>
14 #include "h/done.h"
15 #include "sbr/m_maildir.h"
16 #include "sbr/terminal.h"
17
18 #define FMTTEST_SWITCHES \
19 X("form formatfile", 0, FORMSW) \
20 X("format string", 5, FMTSW) \
21 X("address", 0, ADDRSW) \
22 X("raw", 0, RAWSW) \
23 X("date", 0, DATESW) \
24 X("message", 0, MESSAGESW) \
25 X("file", 0, FILESW) \
26 X("nofile", 0, NFILESW) \
27 X("-component-name component-text", 0, OTHERSW) \
28 X("dupaddrs", 0, DUPADDRSW) \
29 X("nodupaddrs", 0, NDUPADDRSW) \
30 X("ccme", 0, CCMESW) \
31 X("noccme", 0, NCCMESW) \
32 X("outsize size-in-characters", 0, OUTSIZESW) \
33 X("width column-width", 0, WIDTHSW) \
34 X("msgnum number", 0, MSGNUMSW) \
35 X("msgcur flag", 0, MSGCURSW) \
36 X("msgsize size", 0, MSGSIZESW) \
37 X("unseen flag", 0, UNSEENSW) \
38 X("dump", 0, DUMPSW) \
39 X("nodump", 0, NDUMPSW) \
40 X("trace", 0, TRACESW) \
41 X("notrace", 0, NTRACESW) \
42 X("version", 0, VERSIONSW) \
43 X("help", 0, HELPSW) \
44
45 #define X(sw, minchars, id) id,
46 DEFINE_SWITCH_ENUM(FMTTEST);
47 #undef X
48
49 #define X(sw, minchars, id) { sw, minchars, id },
50 DEFINE_SWITCH_ARRAY(FMTTEST, switches);
51 #undef X
52
53 /*
54 * An array containing labels used for branch instructions
55 */
56
57 static struct format **lvec = NULL;
58 static int lused = 0;
59 static int lallocated = 0;
60
61 enum mode_t { MESSAGE, ADDRESS, DATE, RAW };
62 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
63 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
64
65 /*
66 * Context structure used by the tracing routines
67 */
68
69 struct trace_context {
70 int num;
71 char *str;
72 char *outbuf;
73 };
74
75 /*
76 * static prototypes
77 */
78 static void fmt_dump (char *, struct format *);
79 static void dumpone(struct format *);
80 static void initlabels(struct format *);
81 static int findlabel(struct format *);
82 static void assignlabel(struct format *);
83 static char *f_typestr(int);
84 static char *c_typestr(int);
85 static char *c_flagsstr(int);
86 static void litputs(const char *);
87 static void litputc(char);
88 static void process_addresses(struct format *, struct msgs_array *,
89 charstring_t, int, int *,
90 struct fmt_callbacks *);
91 static void process_raw(struct format *, struct msgs_array *, charstring_t,
92 int, int *, struct fmt_callbacks *);
93 static void process_messages(struct format *, struct msgs_array *,
94 struct msgs_array *, charstring_t, char *, int,
95 int, int *, struct fmt_callbacks *);
96 static void process_single_file(FILE *, struct msgs_array *, int *, int,
97 struct format *, charstring_t, int,
98 struct fmt_callbacks *);
99 static void test_trace(void *, struct format *, int, char *, const char *);
100 static char *test_formataddr(char *, char *);
101 static char *test_concataddr(char *, char *);
102 static int insert(struct mailname *);
103 static void mlistfree(void);
104
105 static bool nodupcheck; /* If set, no check for duplicates */
106 static bool ccme; /* Should I cc myself? */
107 static struct mailname mq; /* Mail addresses to check for duplicates */
108 static char *dummy = "dummy";
109
110 int
111 main (int argc, char **argv)
112 {
113 char *cp, *form = NULL, *format = NULL, *defformat = FORMAT, *folder = NULL;
114 char buf[BUFSIZ], *nfs, **argp, **arguments;
115 charstring_t buffer;
116 struct format *fmt;
117 struct comp *cptr;
118 struct msgs_array msgs = { 0, 0, NULL }, compargs = { 0, 0, NULL};
119 bool dump = false;
120 int i;
121 int outputsize = 0;
122 bool dupaddrs = true;
123 bool trace = false;
124 int files = 0;
125 int colwidth = -1, msgnum = -1, msgcur = -1, msgsize = -1, msgunseen = -1;
126 enum mode_t mode = MESSAGE;
127 int dat[5];
128 struct fmt_callbacks cb, *cbp = NULL;
129
130 if (nmh_init(argv[0], true, true)) { return 1; }
131
132 arguments = getarguments (invo_name, argc, argv, 1);
133 argp = arguments;
134
135 while ((cp = *argp++)) {
136 if (*cp == '-') {
137 /*
138 * A -- means that we have a component name (like pick);
139 * save the component name and the next argument for the text.
140 */
141 if (*++cp == '-') {
142 if (*++cp == '\0')
143 die("missing component name after --");
144 app_msgarg(&compargs, cp);
145 /* Grab next argument for component text */
146 if (!(cp = *argp++))
147 die("missing argument to %s", argp[-2]);
148 app_msgarg(&compargs, cp);
149 continue;
150 }
151 switch (smatch (cp, switches)) {
152 case AMBIGSW:
153 ambigsw (cp, switches);
154 done (1);
155 case UNKWNSW:
156 die("-%s unknown", cp);
157
158 case HELPSW:
159 snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
160 print_help (buf, switches, 1);
161 done (0);
162 case VERSIONSW:
163 print_version(invo_name);
164 done (0);
165 case OTHERSW:
166 die("internal argument error!");
167 continue;
168
169 case OUTSIZESW:
170 if (!(cp = *argp++) || *cp == '-')
171 die("missing argument to %s", argp[-2]);
172 if (strcmp(cp, "max") == 0)
173 outputsize = INT_MAX;
174 else if (strcmp(cp, "width") == 0)
175 outputsize = sc_width();
176 else
177 outputsize = atoi(cp);
178 continue;
179
180 case FORMSW:
181 if (!(form = *argp++) || *form == '-')
182 die("missing argument to %s", argp[-2]);
183 format = NULL;
184 continue;
185 case FMTSW:
186 if (!(format = *argp++) || *format == '-')
187 die("missing argument to %s", argp[-2]);
188 form = NULL;
189 continue;
190
191 case TRACESW:
192 trace = true;
193 continue;
194 case NTRACESW:
195 trace = false;
196 continue;
197
198 case ADDRSW:
199 mode = ADDRESS;
200 defformat = DEFADDRFORMAT;
201 continue;
202 case RAWSW:
203 mode = RAW;
204 continue;
205 case MESSAGESW:
206 mode = MESSAGE;
207 defformat = FORMAT;
208 dupaddrs = false;
209 continue;
210 case DATESW:
211 mode = DATE;
212 defformat = DEFDATEFORMAT;
213 continue;
214
215 case FILESW:
216 files++;
217 continue;
218 case NFILESW:
219 files = 0;
220 continue;
221
222 case DUPADDRSW:
223 dupaddrs = true;
224 continue;
225 case NDUPADDRSW:
226 dupaddrs = false;
227 continue;
228
229 case CCMESW:
230 ccme = true;
231 continue;
232 case NCCMESW:
233 ccme = false;
234 continue;
235
236 case WIDTHSW:
237 if (!(cp = *argp++) || *cp == '-')
238 die("missing argument to %s", argp[-2]);
239 colwidth = atoi(cp);
240 continue;
241 case MSGNUMSW:
242 if (!(cp = *argp++) || *cp == '-')
243 die("missing argument to %s", argp[-2]);
244 msgnum = atoi(cp);
245 continue;
246 case MSGCURSW:
247 if (!(cp = *argp++) || *cp == '-')
248 die("missing argument to %s", argp[-2]);
249 msgcur = atoi(cp);
250 continue;
251 case MSGSIZESW:
252 if (!(cp = *argp++) || *cp == '-')
253 die("missing argument to %s", argp[-2]);
254 msgsize = atoi(cp);
255 continue;
256 case UNSEENSW:
257 if (!(cp = *argp++) || *cp == '-')
258 die("missing argument to %s", argp[-2]);
259 msgunseen = atoi(cp);
260 continue;
261
262 case DUMPSW:
263 dump = true;
264 continue;
265 case NDUMPSW:
266 dump = false;
267 continue;
268
269 }
270 }
271
272 /*
273 * Only interpret as a folder if we're in message mode
274 */
275
276 if (mode == MESSAGE && !files && (*cp == '+' || *cp == '@')) {
277 if (folder)
278 die("only one folder at a time!");
279 folder = pluspath (cp);
280 } else
281 app_msgarg(&msgs, cp);
282 }
283
284 /*
285 * Here's our weird heuristic:
286 *
287 * - We allow -dump without any other arguments.
288 * - If you've given any component arguments, we don't require any
289 * other arguments.
290 * - The arguments are interpreted as folders/messages _if_ we're in
291 * message mode, otherwise pass as strings in the text component.
292 */
293
294 if (!dump && compargs.size == 0 && msgs.size == 0) {
295 die("usage: [switches] [+folder] msgs | strings...");
296 }
297
298 /*
299 * If you're picking "raw" as a mode, then you have to select
300 * a format.
301 */
302
303 if (mode == RAW && form == NULL && format == NULL) {
304 die("You must specify a format with -form or -format when "
305 "using -raw");
306 }
307
308 /*
309 * Get new format string. Must be before chdir().
310 */
311 nfs = new_fs (form, format, defformat);
312 (void) fmt_compile(nfs, &fmt, 1);
313
314 if (dump || trace) {
315 initlabels(fmt);
316 if (dump) {
317 fmt_dump(nfs, fmt);
318 if (compargs.size == 0 && msgs.size == 0)
319 done(0);
320 }
321 }
322
323 buffer = charstring_create(BUFSIZ);
324
325 if (outputsize == 0) {
326 if (mode == MESSAGE)
327 outputsize = sc_width();
328 else
329 outputsize = INT_MAX;
330 }
331
332 dat[0] = msgnum;
333 dat[1] = msgcur;
334 dat[2] = msgsize;
335 dat[3] = colwidth == -1 ? outputsize : colwidth;
336 dat[4] = msgunseen;
337
338 /*
339 * If we want to provide our own formataddr, concactaddr, or tracing
340 * callback, do that now. Also, prime ismymbox if we use it.
341 */
342
343 if (!dupaddrs || trace) {
344 ZERO(&cb);
345 cbp = &cb;
346
347 if (!dupaddrs) {
348 cb.formataddr = test_formataddr;
349 cb.concataddr = test_concataddr;
350 if (!ccme)
351 ismymbox(NULL);
352 }
353
354 if (trace) {
355 struct trace_context *ctx;
356
357 NEW(ctx);
358 ctx->num = -1;
359 ctx->str = dummy;
360 ctx->outbuf = mh_xstrdup("");
361
362 cb.trace_func = test_trace;
363 cb.trace_context = ctx;
364 }
365 }
366
367 if (mode == MESSAGE) {
368 process_messages(fmt, &compargs, &msgs, buffer, folder, outputsize,
369 files, dat, cbp);
370 } else {
371 if (compargs.size) {
372 for (i = 0; i < compargs.size; i += 2) {
373 cptr = fmt_findcomp(compargs.msgs[i]);
374 if (cptr)
375 cptr->c_text = getcpy(compargs.msgs[i + 1]);
376 }
377 }
378
379 if (mode == ADDRESS) {
380 process_addresses(fmt, &msgs, buffer, outputsize, dat, cbp);
381 } else /* Fall-through for RAW or DATE */
382 process_raw(fmt, &msgs, buffer, outputsize, dat, cbp);
383 }
384
385 charstring_free(buffer);
386 fmt_free(fmt, 1);
387
388 done(0);
389 return 1;
390 }
391
392 /*
393 * Process each address with fmt_scan().
394 */
395
396 struct pqpair {
397 char *pq_text;
398 char *pq_error;
399 struct pqpair *pq_next;
400 };
401
402 static void
403 process_addresses(struct format *fmt, struct msgs_array *addrs,
404 charstring_t buffer, int outwidth, int *dat,
405 struct fmt_callbacks *cb)
406 {
407 int i;
408 char *cp, error[BUFSIZ];
409 struct mailname *mp;
410 struct pqpair *p, *q;
411 struct pqpair pq;
412 struct comp *c;
413
414 if (dat[0] == -1)
415 dat[0] = 0;
416 if (dat[1] == -1)
417 dat[1] = 0;
418 if (dat[2] == -1)
419 dat[2] = 0;
420 if (dat[4] == -1)
421 dat[4] = 0;
422
423 for (i = 0; i < addrs->size; i++) {
424 (q = &pq)->pq_next = NULL;
425 while ((cp = getname(addrs->msgs[i]))) {
426 NEW0(p);
427 if ((mp = getm(cp, NULL, 0, error, sizeof(error))) == NULL) {
428 p->pq_text = mh_xstrdup(cp);
429 p->pq_error = mh_xstrdup(error);
430 } else {
431 p->pq_text = getcpy(mp->m_text);
432 mnfree(mp);
433 }
434 q = (q->pq_next = p);
435 }
436
437 for (p = pq.pq_next; p; p = q) {
438 c = fmt_findcomp("text");
439 if (c) {
440 free(c->c_text);
441 c->c_text = p->pq_text;
442 p->pq_text = NULL;
443 }
444 c = fmt_findcomp("error");
445 if (c) {
446 free(c->c_text);
447 c->c_text = p->pq_error;
448 p->pq_error = NULL;
449 }
450
451 fmt_scan(fmt, buffer, outwidth, dat, cb);
452 fputs(charstring_buffer(buffer), stdout);
453 mlistfree();
454
455 free(p->pq_text);
456 free(p->pq_error);
457 q = p->pq_next;
458 free(p);
459 }
460 }
461 }
462
463 /*
464 * Process messages and run them through the format engine. A lot taken
465 * from scan.c.
466 */
467
468 static void
469 process_messages(struct format *fmt, struct msgs_array *comps,
470 struct msgs_array *msgs, charstring_t buffer, char *folder,
471 int outwidth, int files, int *dat,
472 struct fmt_callbacks *cb)
473 {
474 int i, msgnum, msgsize = dat[2], num = dat[0], cur = dat[1];
475 int num_unseen_seq = 0;
476 ivector_t seqnum = ivector_create (0);
477 char *maildir, *cp;
478 struct msgs *mp;
479 FILE *in;
480
481 /*
482 * If 'files' is set, short-circuit everything else and just process
483 * everything now.
484 */
485
486 if (files) {
487 for (i = 0; i < msgs->size; i++) {
488 if ((in = fopen(cp = msgs->msgs[i], "r")) == NULL) {
489 admonish(cp, "unable to open file");
490 continue;
491 }
492 process_single_file(in, comps, dat, msgsize, fmt, buffer,
493 outwidth, cb);
494 }
495
496 return;
497 }
498
499 if (! folder)
500 folder = getfolder(1);
501
502 maildir = m_maildir(folder);
503
504 if (chdir(maildir) < 0)
505 adios(maildir, "unable to change directory to");
506
507 if (!(mp = folder_read(folder, 1)))
508 die("unable to read folder %s", folder);
509
510 if (mp->nummsg == 0)
511 die("no messages in %s", folder);
512
513 for (i = 0; i < msgs->size; i++)
514 if (!m_convert(mp, msgs->msgs[i]))
515 done(1);
516 seq_setprev(mp); /* set the Previous-Sequence */
517
518 context_replace(pfolder, folder); /* update current folder */
519 seq_save(mp); /* synchronize message sequences */
520 context_save(); /* save the context file */
521
522 /*
523 * We want to set the unseen flag if requested, so we have to check
524 * the unseen sequence as well.
525 */
526
527 if (dat[4] == -1) {
528 if ((cp = context_find(usequence)) && *cp) {
529 char **ap, *dp;
530
531 dp = mh_xstrdup(cp);
532 ap = brkstring(dp, " ", "\n");
533 for (i = 0; ap && *ap; i++, ap++)
534 ivector_push_back (seqnum, seq_getnum(mp, *ap));
535
536 num_unseen_seq = i;
537 free(dp);
538 }
539 }
540
541 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
542 if (is_selected(mp, msgnum)) {
543 if ((in = fopen(cp = m_name(msgnum), "r")) == NULL) {
544 admonish(cp, "unable to open message");
545 continue;
546 }
547
548 fmt_freecomptext();
549
550 if (num == -1)
551 dat[0] = msgnum;
552
553 if (cur == -1)
554 dat[1] = msgnum == mp->curmsg;
555
556 /*
557 * Check to see if this is in the unseen sequence
558 */
559
560 dat[4] = 0;
561 for (i = 0; i < num_unseen_seq; i++) {
562 if (in_sequence(mp, ivector_at (seqnum, i), msgnum)) {
563 dat[4] = 1;
564 break;
565 }
566 }
567
568 /*
569 * Read in the message and process the components
570 */
571
572 process_single_file(in, comps, dat, msgsize, fmt, buffer,
573 outwidth, cb);
574 }
575 }
576
577 ivector_free (seqnum);
578 folder_free(mp);
579 }
580
581 /*
582 * Process a single file in message mode
583 */
584
585 static void
586 process_single_file(FILE *in, struct msgs_array *comps, int *dat, int msgsize,
587 struct format *fmt, charstring_t buffer, int outwidth,
588 struct fmt_callbacks *cb)
589 {
590 int i, state;
591 char name[NAMESZ], rbuf[NMH_BUFSIZ];
592 m_getfld_state_t gstate;
593 struct comp *c;
594 int bufsz;
595
596 /*
597 * Get our size if we didn't include one
598 */
599
600 if (msgsize == -1) {
601 struct stat st;
602
603 if (fstat(fileno(in), &st) < 0)
604 dat[2] = 0;
605 else
606 dat[2] = st.st_size;
607 }
608
609 /*
610 * Initialize everything else
611 */
612
613 if (dat[0] == -1)
614 dat[0] = 0;
615 if (dat[1] == -1)
616 dat[1] = 0;
617 if (dat[4] == -1)
618 dat[4] = 0;
619
620 /*
621 * Read in the message and process the components
622 */
623
624 gstate = m_getfld_state_init(in);
625 for (;;) {
626 bufsz = sizeof(rbuf);
627 state = m_getfld2(&gstate, name, rbuf, &bufsz);
628 switch (state) {
629 case FLD:
630 case FLDPLUS:
631 i = fmt_addcomptext(name, rbuf);
632 if (i != -1) {
633 while (state == FLDPLUS) {
634 bufsz = sizeof(rbuf);
635 state = m_getfld2(&gstate, name, rbuf, &bufsz);
636 fmt_appendcomp(i, name, rbuf);
637 }
638 }
639
640 while (state == FLDPLUS) {
641 bufsz = sizeof(rbuf);
642 state = m_getfld2(&gstate, name, rbuf, &bufsz);
643 }
644 break;
645
646 case BODY:
647 if (fmt_findcomp("body")) {
648 if ((i = strlen(rbuf)) < outwidth) {
649 bufsz = min (outwidth, (int) sizeof rbuf - i);
650 m_getfld2(&gstate, name, rbuf + i, &bufsz);
651 }
652
653 fmt_addcomptext("body", rbuf);
654 }
655 goto finished;
656
657 default:
658 goto finished;
659 }
660 }
661 finished:
662 fclose(in);
663 m_getfld_state_destroy(&gstate);
664
665 /*
666 * Do this now to override any components in the original message
667 */
668 if (comps->size) {
669 for (i = 0; i < comps->size; i += 2) {
670 c = fmt_findcomp(comps->msgs[i]);
671 if (c) {
672 free(c->c_text);
673 c->c_text = getcpy(comps->msgs[i + 1]);
674 }
675 }
676 }
677 fmt_scan(fmt, buffer, outwidth, dat, cb);
678 fputs(charstring_buffer (buffer), stdout);
679 mlistfree();
680 }
681
682 /*
683 * Run text through the format engine with no special processing
684 */
685
686 static void
687 process_raw(struct format *fmt, struct msgs_array *text, charstring_t buffer,
688 int outwidth, int *dat, struct fmt_callbacks *cb)
689 {
690 int i;
691 struct comp *c;
692
693 if (dat[0] == -1)
694 dat[0] = 0;
695 if (dat[1] == -1)
696 dat[1] = 0;
697 if (dat[2] == -1)
698 dat[2] = 0;
699 if (dat[4] == -1)
700 dat[4] = 0;
701
702 c = fmt_findcomp("text");
703
704 for (i = 0; i < text->size; i++) {
705 if (c != NULL) {
706 free(c->c_text);
707 c->c_text = getcpy(text->msgs[i]);
708 }
709
710 fmt_scan(fmt, buffer, outwidth, dat, cb);
711 fputs(charstring_buffer (buffer), stdout);
712 mlistfree();
713 }
714 }
715
716 /*
717 * Our basic tracing support callback.
718 *
719 * Print out each instruction as it's executed, including the values of
720 * the num and str registers if they've changed.
721 */
722
723 static void
724 test_trace(void *context, struct format *fmt, int num, char *str,
725 const char *outbuf)
726 {
727 struct trace_context *ctx = (struct trace_context *) context;
728 bool changed = false;
729
730 dumpone(fmt);
731
732 if (num != ctx->num) {
733 printf("num=%d", num);
734 ctx->num = num;
735 changed = true;
736 }
737
738 if (str != ctx->str) {
739 if (changed)
740 putchar(' ');
741 changed = true;
742 fputs("str=", stdout);
743 litputs(str);
744 ctx->str = str;
745 }
746
747 if (changed)
748 putchar('\n');
749
750 if (strcmp(outbuf, ctx->outbuf) != 0) {
751 fputs("outbuf=", stdout);
752 litputs(outbuf);
753 putchar('\n');
754 free(ctx->outbuf);
755 ctx->outbuf = mh_xstrdup(outbuf);
756 }
757 }
758
759 static void
760 fmt_dump (char *nfs, struct format *fmth)
761 {
762 struct format *fmt;
763
764 printf("Instruction dump of format string: \n%s\n", nfs);
765
766 /* Dump them out! */
767 for (fmt = fmth; fmt; ++fmt) {
768 dumpone(fmt);
769 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
770 break;
771 }
772 }
773
774 static void
775 dumpone(struct format *fmt)
776 {
777 int i;
778
779 if ((i = findlabel(fmt)) >= 0)
780 printf("L%d:", i);
781 putchar('\t');
782
783 fputs(f_typestr((int)fmt->f_type), stdout);
784
785 switch (fmt->f_type) {
786
787 case FT_COMP:
788 case FT_LS_COMP:
789 case FT_LV_COMPFLAG:
790 case FT_LV_COMP:
791 fputs(", comp ", stdout);
792 litputs(fmt->f_comp->c_name);
793 if (fmt->f_comp->c_type)
794 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
795 if (fmt->f_comp->c_flags)
796 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
797 break;
798
799 case FT_LV_SEC:
800 case FT_LV_MIN:
801 case FT_LV_HOUR:
802 case FT_LV_MDAY:
803 case FT_LV_MON:
804 case FT_LS_MONTH:
805 case FT_LS_LMONTH:
806 case FT_LS_ZONE:
807 case FT_LV_YEAR:
808 case FT_LV_WDAY:
809 case FT_LS_DAY:
810 case FT_LS_WEEKDAY:
811 case FT_LV_YDAY:
812 case FT_LV_ZONE:
813 case FT_LV_CLOCK:
814 case FT_LV_RCLOCK:
815 case FT_LV_DAYF:
816 case FT_LV_ZONEF:
817 case FT_LV_DST:
818 case FT_LS_822DATE:
819 case FT_LS_PRETTY:
820 case FT_LOCALDATE:
821 case FT_GMTDATE:
822 case FT_PARSEDATE:
823 fputs(", c_name ", stdout);
824 litputs(fmt->f_comp->c_name);
825 if (fmt->f_comp->c_type)
826 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
827 if (fmt->f_comp->c_flags)
828 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
829 break;
830
831 case FT_LS_ADDR:
832 case FT_LS_PERS:
833 case FT_LS_MBOX:
834 case FT_LS_HOST:
835 case FT_LS_PATH:
836 case FT_LS_GNAME:
837 case FT_LS_NOTE:
838 case FT_LS_822ADDR:
839 case FT_LV_HOSTTYPE:
840 case FT_LV_INGRPF:
841 case FT_LV_NOHOSTF:
842 case FT_LS_FRIENDLY:
843 case FT_PARSEADDR:
844 case FT_MYMBOX:
845 case FT_GETMYMBOX:
846 case FT_GETMYADDR:
847 fputs(", c_name ", stdout);
848 litputs(fmt->f_comp->c_name);
849 if (fmt->f_comp->c_type)
850 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
851 if (fmt->f_comp->c_flags)
852 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
853 break;
854
855 case FT_COMPF:
856 printf(", width %d, fill '", fmt->f_width);
857 litputc(fmt->f_fill);
858 fputs("' name ", stdout);
859 litputs(fmt->f_comp->c_name);
860 if (fmt->f_comp->c_type)
861 printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
862 if (fmt->f_comp->c_flags)
863 printf(", c_flags %s", c_flagsstr(fmt->f_comp->c_flags));
864 break;
865
866 case FT_STRF:
867 case FT_NUMF:
868 printf(", width %d, fill '", fmt->f_width);
869 litputc(fmt->f_fill);
870 putchar('\'');
871 break;
872
873 case FT_LIT:
874 putchar(' ');
875 litputs(fmt->f_text);
876 break;
877
878 case FT_LITF:
879 printf(", width %d, fill '", fmt->f_width);
880 litputc(fmt->f_fill);
881 fputs("' ", stdout);
882 litputs(fmt->f_text);
883 break;
884
885 case FT_CHAR:
886 putchar(' ');
887 putchar('\'');
888 litputc(fmt->f_char);
889 putchar('\'');
890 break;
891
892
893 case FT_IF_S:
894 case FT_IF_S_NULL:
895 case FT_IF_MATCH:
896 case FT_IF_AMATCH:
897 fputs(" continue else goto", stdout);
898 /* FALLTHRU */
899 case FT_GOTO:
900 i = findlabel(fmt + fmt->f_skip);
901 printf(" L%d", i);
902 break;
903
904 case FT_IF_V_EQ:
905 case FT_IF_V_NE:
906 case FT_IF_V_GT:
907 i = findlabel(fmt + fmt->f_skip);
908 printf(" %d continue else goto L%d", fmt->f_value, i);
909 break;
910
911 case FT_V_EQ:
912 case FT_V_NE:
913 case FT_V_GT:
914 case FT_LV_LIT:
915 case FT_LV_PLUS_L:
916 case FT_LV_MINUS_L:
917 case FT_LV_MULTIPLY_L:
918 case FT_LV_DIVIDE_L:
919 case FT_LV_MODULO_L:
920 printf(" value %d", fmt->f_value);
921 break;
922
923 case FT_LS_LIT:
924 fputs(" str ", stdout);
925 litputs(fmt->f_text);
926 break;
927
928 case FT_LS_GETENV:
929 fputs(" getenv ", stdout);
930 litputs(fmt->f_text);
931 break;
932
933 case FT_LS_DECODECOMP:
934 fputs(", comp ", stdout);
935 litputs(fmt->f_comp->c_name);
936 break;
937
938 case FT_LS_DECODE:
939 break;
940
941 case FT_LS_TRIM:
942 printf(", width %d", fmt->f_width);
943 break;
944
945 case FT_LV_DAT:
946 printf(", value dat[%d]", fmt->f_value);
947 break;
948 }
949 putchar('\n');
950 }
951
952 /*
953 * Iterate over all instructions and assign labels to the targets of
954 * branch statements
955 */
956
957 static void
958 initlabels(struct format *fmth)
959 {
960 struct format *fmt, *addr;
961 int i;
962
963 /* Assign labels */
964 for (fmt = fmth; fmt; ++fmt) {
965 i = fmt->f_type;
966 if (i == FT_IF_S ||
967 i == FT_IF_S_NULL ||
968 i == FT_IF_V_EQ ||
969 i == FT_IF_V_NE ||
970 i == FT_IF_V_GT ||
971 i == FT_IF_MATCH ||
972 i == FT_IF_AMATCH ||
973 i == FT_GOTO) {
974 addr = fmt + fmt->f_skip;
975 if (findlabel(addr) < 0)
976 assignlabel(addr);
977 }
978 if (fmt->f_type == FT_DONE && fmt->f_value == 0)
979 break;
980 }
981 }
982
983
984 static int
985 findlabel(struct format *addr)
986 {
987 int i;
988
989 for (i = 0; i < lused; ++i)
990 if (addr == lvec[i])
991 return i;
992 return -1;
993 }
994
995 static void
996 assignlabel(struct format *addr)
997 {
998 if (lused >= lallocated) {
999 lallocated += 64;
1000 lvec = (struct format **)
1001 mh_xrealloc(lvec, sizeof(struct format *) * lallocated);
1002 }
1003
1004 lvec[lused++] = addr;
1005 }
1006
1007 static char *
1008 f_typestr(int t)
1009 {
1010 static char buf[32];
1011
1012 switch (t) {
1013 case FT_COMP: return "COMP";
1014 case FT_COMPF: return "COMPF";
1015 case FT_LIT: return "LIT";
1016 case FT_LITF: return "LITF";
1017 case FT_CHAR: return "CHAR";
1018 case FT_NUM: return "NUM";
1019 case FT_NUMF: return "NUMF";
1020 case FT_STR: return "STR";
1021 case FT_STRF: return "STRF";
1022 case FT_STRFW: return "STRFW";
1023 case FT_STRLIT: return "STRLIT";
1024 case FT_STRLITZ: return "STRLITZ";
1025 case FT_PUTADDR: return "PUTADDR";
1026 case FT_LS_COMP: return "LS_COMP";
1027 case FT_LS_LIT: return "LS_LIT";
1028 case FT_LS_GETENV: return "LS_GETENV";
1029 case FT_LS_CFIND: return "LS_CFIND";
1030 case FT_LS_DECODECOMP: return "LS_DECODECOMP";
1031 case FT_LS_DECODE: return "LS_DECODE";
1032 case FT_LS_TRIM: return "LS_TRIM";
1033 case FT_LV_COMP: return "LV_COMP";
1034 case FT_LV_COMPFLAG: return "LV_COMPFLAG";
1035 case FT_LV_LIT: return "LV_LIT";
1036 case FT_LV_DAT: return "LV_DAT";
1037 case FT_LV_STRLEN: return "LV_STRLEN";
1038 case FT_LV_PLUS_L: return "LV_PLUS_L";
1039 case FT_LV_MINUS_L: return "LV_MINUS_L";
1040 case FT_LV_MULTIPLY_L: return "LV_MULTIPLY_L";
1041 case FT_LV_DIVIDE_L: return "LV_DIVIDE_L";
1042 case FT_LV_MODULO_L: return "LV_MODULO_L";
1043 case FT_LV_CHAR_LEFT: return "LV_CHAR_LEFT";
1044 case FT_LS_MONTH: return "LS_MONTH";
1045 case FT_LS_LMONTH: return "LS_LMONTH";
1046 case FT_LS_ZONE: return "LS_ZONE";
1047 case FT_LS_DAY: return "LS_DAY";
1048 case FT_LS_WEEKDAY: return "LS_WEEKDAY";
1049 case FT_LS_822DATE: return "LS_822DATE";
1050 case FT_LS_PRETTY: return "LS_PRETTY";
1051 case FT_LS_KILO: return "LS_KILO";
1052 case FT_LS_KIBI: return "LS_KIBI";
1053 case FT_LV_SEC: return "LV_SEC";
1054 case FT_LV_MIN: return "LV_MIN";
1055 case FT_LV_HOUR: return "LV_HOUR";
1056 case FT_LV_MDAY: return "LV_MDAY";
1057 case FT_LV_MON: return "LV_MON";
1058 case FT_LV_YEAR: return "LV_YEAR";
1059 case FT_LV_YDAY: return "LV_YDAY";
1060 case FT_LV_WDAY: return "LV_WDAY";
1061 case FT_LV_ZONE: return "LV_ZONE";
1062 case FT_LV_CLOCK: return "LV_CLOCK";
1063 case FT_LV_RCLOCK: return "LV_RCLOCK";
1064 case FT_LV_DAYF: return "LV_DAYF";
1065 case FT_LV_DST: return "LV_DST";
1066 case FT_LV_ZONEF: return "LV_ZONEF";
1067 case FT_LS_PERS: return "LS_PERS";
1068 case FT_LS_MBOX: return "LS_MBOX";
1069 case FT_LS_HOST: return "LS_HOST";
1070 case FT_LS_PATH: return "LS_PATH";
1071 case FT_LS_GNAME: return "LS_GNAME";
1072 case FT_LS_NOTE: return "LS_NOTE";
1073 case FT_LS_ADDR: return "LS_ADDR";
1074 case FT_LS_822ADDR: return "LS_822ADDR";
1075 case FT_LS_FRIENDLY: return "LS_FRIENDLY";
1076 case FT_LV_HOSTTYPE: return "LV_HOSTTYPE";
1077 case FT_LV_INGRPF: return "LV_INGRPF";
1078 case FT_LS_UNQUOTE: return "LS_UNQUOTE";
1079 case FT_LV_NOHOSTF: return "LV_NOHOSTF";
1080 case FT_LOCALDATE: return "LOCALDATE";
1081 case FT_GMTDATE: return "GMTDATE";
1082 case FT_PARSEDATE: return "PARSEDATE";
1083 case FT_PARSEADDR: return "PARSEADDR";
1084 case FT_FORMATADDR: return "FORMATADDR";
1085 case FT_CONCATADDR: return "CONCATADDR";
1086 case FT_MYMBOX: return "MYMBOX";
1087 case FT_GETMYMBOX: return "GETMYMBOX";
1088 case FT_GETMYADDR: return "GETMYADDR";
1089 case FT_SAVESTR: return "SAVESTR";
1090 case FT_DONE: return "DONE";
1091 case FT_PAUSE: return "PAUSE";
1092 case FT_NOP: return "NOP";
1093 case FT_GOTO: return "GOTO";
1094 case FT_IF_S_NULL: return "IF_S_NULL";
1095 case FT_IF_S: return "IF_S";
1096 case FT_IF_V_EQ: return "IF_V_EQ";
1097 case FT_IF_V_NE: return "IF_V_NE";
1098 case FT_IF_V_GT: return "IF_V_GT";
1099 case FT_IF_MATCH: return "IF_MATCH";
1100 case FT_IF_AMATCH: return "IF_AMATCH";
1101 case FT_S_NULL: return "S_NULL";
1102 case FT_S_NONNULL: return "S_NONNULL";
1103 case FT_V_EQ: return "V_EQ";
1104 case FT_V_NE: return "V_NE";
1105 case FT_V_GT: return "V_GT";
1106 case FT_V_MATCH: return "V_MATCH";
1107 case FT_V_AMATCH: return "V_AMATCH";
1108 default:
1109 snprintf(buf, sizeof(buf), "/* ??? #%d */", t);
1110 return buf;
1111 }
1112 }
1113
1114 static char *
1115 c_typestr(int t)
1116 {
1117 static char buf[64];
1118
1119 snprintb(buf, sizeof(buf), t, CT_BITS);
1120 return buf;
1121 }
1122
1123 static char *
1124 c_flagsstr(int t)
1125 {
1126 static char buf[64];
1127
1128 snprintb(buf, sizeof(buf), t, CF_BITS);
1129 return buf;
1130 }
1131
1132 static void
1133 litputs(const char *s)
1134 {
1135 if (s) {
1136 putchar('"');
1137 while (*s)
1138 litputc(*s++);
1139 putchar('"');
1140 } else
1141 fputs("<nil>", stdout);
1142 }
1143
1144 static void
1145 litputc(char c)
1146 {
1147 if (c & ~ 0177) {
1148 printf("\\x%02x", (unsigned char) c);
1149 } else if (c < 0x20 || c == 0177) {
1150 if (c == '\b') {
1151 putchar('\\');
1152 putchar('b');
1153 } else if (c == '\f') {
1154 putchar('\\');
1155 putchar('f');
1156 } else if (c == '\n') {
1157 putchar('\\');
1158 putchar('n');
1159 } else if (c == '\r') {
1160 putchar('\\');
1161 putchar('r');
1162 } else if (c == '\t') {
1163 putchar('\\');
1164 putchar('t');
1165 } else {
1166 putchar('^');
1167 putchar(c ^ 0x40); /* DEL to ?, others to alpha */
1168 }
1169 } else
1170 putchar(c);
1171 }
1172
1173 /*
1174 * Routines/code to support the duplicate address suppression code, adapted
1175 * from replsbr.c
1176 */
1177
1178 static char *buf; /* our current working buffer */
1179 static char *bufend; /* end of working buffer */
1180 static char *last_dst; /* buf ptr at end of last call */
1181 static unsigned int bufsiz=0; /* current size of buf */
1182
1183 #define BUFINCR 512 /* how much to expand buf when if fills */
1184
1185 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1186
1187 /*
1188 * check if there's enough room in buf for str.
1189 * add more mem if needed
1190 */
1191 #define CHECKMEM(str) \
1192 if ((len = strlen (str)) >= bufend - dst) {\
1193 int i = dst - buf;\
1194 int n = last_dst - buf;\
1195 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1196 buf = mh_xrealloc (buf, bufsiz);\
1197 dst = buf + i;\
1198 last_dst = buf + n;\
1199 bufend = buf + bufsiz;\
1200 }
1201
1202
1203 /*
1204 * These are versions of similar routines from replsbr.c; the purpose is
1205 * to suppress duplicate addresses from being added to a list when building
1206 * up addresses for the %(formataddr) format function. This is used by
1207 * repl to prevent duplicate addresses from being added to the "to" line.
1208 * See replsbr.c for more information.
1209 *
1210 * We can't use the functions in replsbr.c directly because they are slightly
1211 * different and depend on the rest of replsbr.c
1212 */
1213 static char *
1214 test_formataddr (char *orig, char *str)
1215 {
1216 int len;
1217 char error[BUFSIZ];
1218 bool isgroup;
1219 char *dst;
1220 char *cp;
1221 char *sp;
1222 struct mailname *mp = NULL;
1223
1224 /* if we don't have a buffer yet, get one */
1225 if (bufsiz == 0) {
1226 buf = mh_xmalloc (BUFINCR);
1227 last_dst = buf; /* XXX */
1228 bufsiz = BUFINCR - 6; /* leave some slop */
1229 bufend = buf + bufsiz;
1230 }
1231 /*
1232 * If "orig" points to our buffer we can just pick up where we
1233 * left off. Otherwise we have to copy orig into our buffer.
1234 */
1235 if (orig == buf)
1236 dst = last_dst;
1237 else if (!orig || !*orig) {
1238 dst = buf;
1239 *dst = '\0';
1240 } else {
1241 dst = last_dst; /* XXX */
1242 CHECKMEM (orig);
1243 CPY (orig);
1244 }
1245
1246 /* concatenate all the new addresses onto 'buf' */
1247 for (isgroup = false; (cp = getname (str)); ) {
1248 if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
1249 fprintf(stderr, "bad address \"%s\" -- %s\n", cp, error);
1250 continue;
1251 }
1252 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
1253 *dst++ = ';';
1254 isgroup = false;
1255 }
1256 if (insert (mp)) {
1257 /* if we get here we're going to add an address */
1258 if (dst != buf) {
1259 *dst++ = ',';
1260 *dst++ = ' ';
1261 }
1262 if (mp->m_gname) {
1263 CHECKMEM (mp->m_gname);
1264 CPY (mp->m_gname);
1265 isgroup = true;
1266 }
1267 sp = adrformat (mp);
1268 CHECKMEM (sp);
1269 CPY (sp);
1270 }
1271 }
1272
1273 if (isgroup)
1274 *dst++ = ';';
1275
1276 *dst = '\0';
1277 last_dst = dst;
1278 return buf;
1279 }
1280
1281
1282 /*
1283 * The companion to test_formataddr(); it behaves the same way, except doesn't
1284 * do duplicate address detection.
1285 */
1286 static char *
1287 test_concataddr(char *orig, char *str)
1288 {
1289 char *cp;
1290
1291 nodupcheck = true;
1292 cp = test_formataddr(orig, str);
1293 nodupcheck = false;
1294 return cp;
1295 }
1296
1297 static int
1298 insert (struct mailname *np)
1299 {
1300 struct mailname *mp;
1301
1302 if (nodupcheck)
1303 return 1;
1304
1305 if (np->m_mbox == NULL)
1306 return 0;
1307
1308 for (mp = &mq; mp->m_next; mp = mp->m_next) {
1309 if (!strcasecmp (FENDNULL(np->m_host),
1310 FENDNULL(mp->m_next->m_host)) &&
1311 !strcasecmp (FENDNULL(np->m_mbox),
1312 FENDNULL(mp->m_next->m_mbox)))
1313 return 0;
1314 }
1315 if (!ccme && ismymbox (np))
1316 return 0;
1317
1318 mp->m_next = np;
1319
1320 return 1;
1321 }
1322
1323 /*
1324 * Reset our duplicate address list
1325 */
1326
1327 void
1328 mlistfree(void)
1329 {
1330 struct mailname *mp, *mp2;
1331
1332 for (mp = mq.m_next; mp; mp = mp2) {
1333 mp2 = mp->m_next;
1334 mnfree(mp);
1335 }
1336 }