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