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