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