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