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