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