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