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