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