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