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