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