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