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