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