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