]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
ee8790a4572815836afe96461b942dbd2ec4146e
1 /* fmttest.c -- A program to help test and debug format instructions
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.
9 #include "sbr/brkstring.h"
10 #include "sbr/ambigsw.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/seq_getnum.h"
15 #include "sbr/error.h"
16 #include "h/fmt_scan.h"
17 #include "h/fmt_compile.h"
19 #include "h/scansbr.h"
20 #include "h/addrsbr.h"
22 #include "sbr/m_maildir.h"
23 #include "sbr/terminal.h"
25 #define FMTTEST_SWITCHES \
26 X("form formatfile", 0, FORMSW) \
27 X("format string", 5, FMTSW) \
28 X("address", 0, ADDRSW) \
30 X("date", 0, DATESW) \
31 X("message", 0, MESSAGESW) \
32 X("file", 0, FILESW) \
33 X("nofile", 0, NFILESW) \
34 X("-component-name component-text", 0, OTHERSW) \
35 X("dupaddrs", 0, DUPADDRSW) \
36 X("nodupaddrs", 0, NDUPADDRSW) \
37 X("ccme", 0, CCMESW) \
38 X("noccme", 0, NCCMESW) \
39 X("outsize size-in-characters", 0, OUTSIZESW) \
40 X("width column-width", 0, WIDTHSW) \
41 X("msgnum number", 0, MSGNUMSW) \
42 X("msgcur flag", 0, MSGCURSW) \
43 X("msgsize size", 0, MSGSIZESW) \
44 X("unseen flag", 0, UNSEENSW) \
45 X("dump", 0, DUMPSW) \
46 X("nodump", 0, NDUMPSW) \
47 X("trace", 0, TRACESW) \
48 X("notrace", 0, NTRACESW) \
49 X("version", 0, VERSIONSW) \
50 X("help", 0, HELPSW) \
52 #define X(sw, minchars, id) id,
53 DEFINE_SWITCH_ENUM(FMTTEST
);
56 #define X(sw, minchars, id) { sw, minchars, id },
57 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
61 * An array containing labels used for branch instructions
64 static struct format
**lvec
= NULL
;
66 static int lallocated
= 0;
68 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
69 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
70 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
73 * Context structure used by the tracing routines
76 struct trace_context
{
85 static void fmt_dump (char *, struct format
*);
86 static void dumpone(struct format
*);
87 static void initlabels(struct format
*);
88 static int findlabel(struct format
*);
89 static void assignlabel(struct format
*);
90 static char *f_typestr(int);
91 static char *c_typestr(int);
92 static char *c_flagsstr(int);
93 static void litputs(const char *);
94 static void litputc(char);
95 static void process_addresses(struct format
*, struct msgs_array
*,
96 charstring_t
, int, int *,
97 struct fmt_callbacks
*);
98 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
99 int, int *, struct fmt_callbacks
*);
100 static void process_messages(struct format
*, struct msgs_array
*,
101 struct msgs_array
*, charstring_t
, char *, int,
102 int, int *, struct fmt_callbacks
*);
103 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
104 struct format
*, charstring_t
, int,
105 struct fmt_callbacks
*);
106 static void test_trace(void *, struct format
*, int, char *, const char *);
107 static char *test_formataddr(char *, char *);
108 static char *test_concataddr(char *, char *);
109 static int insert(struct mailname
*);
110 static void mlistfree(void);
112 static bool nodupcheck
; /* If set, no check for duplicates */
113 static bool ccme
; /* Should I cc myself? */
114 static struct mailname mq
; /* Mail addresses to check for duplicates */
115 static char *dummy
= "dummy";
118 main (int argc
, char **argv
)
120 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
121 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
125 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
129 bool dupaddrs
= true;
132 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
133 enum mode_t mode
= MESSAGE
;
135 struct fmt_callbacks cb
, *cbp
= NULL
;
137 if (nmh_init(argv
[0], true, true)) { return 1; }
139 arguments
= getarguments (invo_name
, argc
, argv
, 1);
142 while ((cp
= *argp
++)) {
145 * A -- means that we have a component name (like pick);
146 * save the component name and the next argument for the text.
150 die("missing component name after --");
151 app_msgarg(&compargs
, cp
);
152 /* Grab next argument for component text */
154 die("missing argument to %s", argp
[-2]);
155 app_msgarg(&compargs
, cp
);
158 switch (smatch (cp
, switches
)) {
160 ambigsw (cp
, switches
);
163 die("-%s unknown", cp
);
166 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
167 print_help (buf
, switches
, 1);
170 print_version(invo_name
);
173 die("internal argument error!");
177 if (!(cp
= *argp
++) || *cp
== '-')
178 die("missing argument to %s", argp
[-2]);
179 if (strcmp(cp
, "max") == 0)
180 outputsize
= INT_MAX
;
181 else if (strcmp(cp
, "width") == 0)
182 outputsize
= sc_width();
184 outputsize
= atoi(cp
);
188 if (!(form
= *argp
++) || *form
== '-')
189 die("missing argument to %s", argp
[-2]);
193 if (!(format
= *argp
++) || *format
== '-')
194 die("missing argument to %s", argp
[-2]);
207 defformat
= DEFADDRFORMAT
;
219 defformat
= DEFDATEFORMAT
;
244 if (!(cp
= *argp
++) || *cp
== '-')
245 die("missing argument to %s", argp
[-2]);
249 if (!(cp
= *argp
++) || *cp
== '-')
250 die("missing argument to %s", argp
[-2]);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 die("missing argument to %s", argp
[-2]);
259 if (!(cp
= *argp
++) || *cp
== '-')
260 die("missing argument to %s", argp
[-2]);
264 if (!(cp
= *argp
++) || *cp
== '-')
265 die("missing argument to %s", argp
[-2]);
266 msgunseen
= atoi(cp
);
280 * Only interpret as a folder if we're in message mode
283 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
285 die("only one folder at a time!");
286 folder
= pluspath (cp
);
288 app_msgarg(&msgs
, cp
);
292 * Here's our weird heuristic:
294 * - We allow -dump without any other arguments.
295 * - If you've given any component arguments, we don't require any
297 * - The arguments are interpreted as folders/messages _if_ we're in
298 * message mode, otherwise pass as strings in the text component.
301 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
302 die("usage: [switches] [+folder] msgs | strings...");
306 * If you're picking "raw" as a mode, then you have to select
310 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
311 die("You must specify a format with -form or -format when "
316 * Get new format string. Must be before chdir().
318 nfs
= new_fs (form
, format
, defformat
);
319 (void) fmt_compile(nfs
, &fmt
, 1);
325 if (compargs
.size
== 0 && msgs
.size
== 0)
330 buffer
= charstring_create(BUFSIZ
);
332 if (outputsize
== 0) {
334 outputsize
= sc_width();
336 outputsize
= INT_MAX
;
342 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
346 * If we want to provide our own formataddr, concactaddr, or tracing
347 * callback, do that now. Also, prime ismymbox if we use it.
350 if (!dupaddrs
|| trace
) {
355 cb
.formataddr
= test_formataddr
;
356 cb
.concataddr
= test_concataddr
;
362 struct trace_context
*ctx
;
367 ctx
->outbuf
= mh_xstrdup("");
369 cb
.trace_func
= test_trace
;
370 cb
.trace_context
= ctx
;
374 if (mode
== MESSAGE
) {
375 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
379 for (i
= 0; i
< compargs
.size
; i
+= 2) {
380 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
382 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
386 if (mode
== ADDRESS
) {
387 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
388 } else /* Fall-through for RAW or DATE */
389 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
392 charstring_free(buffer
);
400 * Process each address with fmt_scan().
406 struct pqpair
*pq_next
;
410 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
411 charstring_t buffer
, int outwidth
, int *dat
,
412 struct fmt_callbacks
*cb
)
415 char *cp
, error
[BUFSIZ
];
417 struct pqpair
*p
, *q
;
430 for (i
= 0; i
< addrs
->size
; i
++) {
431 (q
= &pq
)->pq_next
= NULL
;
432 while ((cp
= getname(addrs
->msgs
[i
]))) {
434 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
435 p
->pq_text
= mh_xstrdup(cp
);
436 p
->pq_error
= mh_xstrdup(error
);
438 p
->pq_text
= getcpy(mp
->m_text
);
441 q
= (q
->pq_next
= p
);
444 for (p
= pq
.pq_next
; p
; p
= q
) {
445 c
= fmt_findcomp("text");
448 c
->c_text
= p
->pq_text
;
451 c
= fmt_findcomp("error");
454 c
->c_text
= p
->pq_error
;
458 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
459 fputs(charstring_buffer(buffer
), stdout
);
471 * Process messages and run them through the format engine. A lot taken
476 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
477 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
478 int outwidth
, int files
, int *dat
,
479 struct fmt_callbacks
*cb
)
481 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
482 int num_unseen_seq
= 0;
483 ivector_t seqnum
= ivector_create (0);
489 * If 'files' is set, short-circuit everything else and just process
494 for (i
= 0; i
< msgs
->size
; i
++) {
495 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
496 admonish(cp
, "unable to open file");
499 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
507 folder
= getfolder(1);
509 maildir
= m_maildir(folder
);
511 if (chdir(maildir
) < 0)
512 adios(maildir
, "unable to change directory to");
514 if (!(mp
= folder_read(folder
, 1)))
515 die("unable to read folder %s", folder
);
518 die("no messages in %s", folder
);
520 for (i
= 0; i
< msgs
->size
; i
++)
521 if (!m_convert(mp
, msgs
->msgs
[i
]))
523 seq_setprev(mp
); /* set the Previous-Sequence */
525 context_replace(pfolder
, folder
); /* update current folder */
526 seq_save(mp
); /* synchronize message sequences */
527 context_save(); /* save the context file */
530 * We want to set the unseen flag if requested, so we have to check
531 * the unseen sequence as well.
535 if ((cp
= context_find(usequence
)) && *cp
) {
539 ap
= brkstring(dp
, " ", "\n");
540 for (i
= 0; ap
&& *ap
; i
++, ap
++)
541 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
548 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
549 if (is_selected(mp
, msgnum
)) {
550 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
551 admonish(cp
, "unable to open message");
561 dat
[1] = msgnum
== mp
->curmsg
;
564 * Check to see if this is in the unseen sequence
568 for (i
= 0; i
< num_unseen_seq
; i
++) {
569 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
576 * Read in the message and process the components
579 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
584 ivector_free (seqnum
);
589 * Process a single file in message mode
593 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
594 struct format
*fmt
, charstring_t buffer
, int outwidth
,
595 struct fmt_callbacks
*cb
)
598 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
599 m_getfld_state_t gstate
;
604 * Get our size if we didn't include one
610 if (fstat(fileno(in
), &st
) < 0)
617 * Initialize everything else
628 * Read in the message and process the components
631 gstate
= m_getfld_state_init(in
);
633 bufsz
= sizeof(rbuf
);
634 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
638 i
= fmt_addcomptext(name
, rbuf
);
640 while (state
== FLDPLUS
) {
641 bufsz
= sizeof(rbuf
);
642 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
643 fmt_appendcomp(i
, name
, rbuf
);
647 while (state
== FLDPLUS
) {
648 bufsz
= sizeof(rbuf
);
649 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
654 if (fmt_findcomp("body")) {
655 if ((i
= strlen(rbuf
)) < outwidth
) {
656 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
657 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
660 fmt_addcomptext("body", rbuf
);
670 m_getfld_state_destroy(&gstate
);
673 * Do this now to override any components in the original message
676 for (i
= 0; i
< comps
->size
; i
+= 2) {
677 c
= fmt_findcomp(comps
->msgs
[i
]);
680 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
684 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
685 fputs(charstring_buffer (buffer
), stdout
);
690 * Run text through the format engine with no special processing
694 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
695 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
709 c
= fmt_findcomp("text");
711 for (i
= 0; i
< text
->size
; i
++) {
714 c
->c_text
= getcpy(text
->msgs
[i
]);
717 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
718 fputs(charstring_buffer (buffer
), stdout
);
724 * Our basic tracing support callback.
726 * Print out each instruction as it's executed, including the values of
727 * the num and str registers if they've changed.
731 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
734 struct trace_context
*ctx
= (struct trace_context
*) context
;
735 bool changed
= false;
739 if (num
!= ctx
->num
) {
740 printf("num=%d", num
);
745 if (str
!= ctx
->str
) {
749 fputs("str=", stdout
);
757 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
758 fputs("outbuf=", stdout
);
762 ctx
->outbuf
= mh_xstrdup(outbuf
);
767 fmt_dump (char *nfs
, struct format
*fmth
)
771 printf("Instruction dump of format string: \n%s\n", nfs
);
774 for (fmt
= fmth
; fmt
; ++fmt
) {
776 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
782 dumpone(struct format
*fmt
)
786 if ((i
= findlabel(fmt
)) >= 0)
790 fputs(f_typestr((int)fmt
->f_type
), stdout
);
792 switch (fmt
->f_type
) {
798 fputs(", comp ", stdout
);
799 litputs(fmt
->f_comp
->c_name
);
800 if (fmt
->f_comp
->c_type
)
801 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
802 if (fmt
->f_comp
->c_flags
)
803 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
830 fputs(", c_name ", stdout
);
831 litputs(fmt
->f_comp
->c_name
);
832 if (fmt
->f_comp
->c_type
)
833 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
834 if (fmt
->f_comp
->c_flags
)
835 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
854 fputs(", c_name ", stdout
);
855 litputs(fmt
->f_comp
->c_name
);
856 if (fmt
->f_comp
->c_type
)
857 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
858 if (fmt
->f_comp
->c_flags
)
859 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
863 printf(", width %d, fill '", fmt
->f_width
);
864 litputc(fmt
->f_fill
);
865 fputs("' name ", stdout
);
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
));
875 printf(", width %d, fill '", fmt
->f_width
);
876 litputc(fmt
->f_fill
);
882 litputs(fmt
->f_text
);
886 printf(", width %d, fill '", fmt
->f_width
);
887 litputc(fmt
->f_fill
);
889 litputs(fmt
->f_text
);
895 litputc(fmt
->f_char
);
904 fputs(" continue else goto", stdout
);
907 i
= findlabel(fmt
+ fmt
->f_skip
);
914 i
= findlabel(fmt
+ fmt
->f_skip
);
915 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
924 case FT_LV_MULTIPLY_L
:
927 printf(" value %d", fmt
->f_value
);
931 fputs(" str ", stdout
);
932 litputs(fmt
->f_text
);
936 fputs(" getenv ", stdout
);
937 litputs(fmt
->f_text
);
940 case FT_LS_DECODECOMP
:
941 fputs(", comp ", stdout
);
942 litputs(fmt
->f_comp
->c_name
);
949 printf(", width %d", fmt
->f_width
);
953 printf(", value dat[%d]", fmt
->f_value
);
960 * Iterate over all instructions and assign labels to the targets of
965 initlabels(struct format
*fmth
)
967 struct format
*fmt
, *addr
;
971 for (fmt
= fmth
; fmt
; ++fmt
) {
981 addr
= fmt
+ fmt
->f_skip
;
982 if (findlabel(addr
) < 0)
985 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
992 findlabel(struct format
*addr
)
996 for (i
= 0; i
< lused
; ++i
)
1003 assignlabel(struct format
*addr
)
1005 if (lused
>= lallocated
) {
1007 lvec
= (struct format
**)
1008 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1011 lvec
[lused
++] = addr
;
1017 static char buf
[32];
1020 case FT_COMP
: return "COMP";
1021 case FT_COMPF
: return "COMPF";
1022 case FT_LIT
: return "LIT";
1023 case FT_LITF
: return "LITF";
1024 case FT_CHAR
: return "CHAR";
1025 case FT_NUM
: return "NUM";
1026 case FT_NUMF
: return "NUMF";
1027 case FT_STR
: return "STR";
1028 case FT_STRF
: return "STRF";
1029 case FT_STRFW
: return "STRFW";
1030 case FT_STRLIT
: return "STRLIT";
1031 case FT_STRLITZ
: return "STRLITZ";
1032 case FT_PUTADDR
: return "PUTADDR";
1033 case FT_LS_COMP
: return "LS_COMP";
1034 case FT_LS_LIT
: return "LS_LIT";
1035 case FT_LS_GETENV
: return "LS_GETENV";
1036 case FT_LS_CFIND
: return "LS_CFIND";
1037 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1038 case FT_LS_DECODE
: return "LS_DECODE";
1039 case FT_LS_TRIM
: return "LS_TRIM";
1040 case FT_LV_COMP
: return "LV_COMP";
1041 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1042 case FT_LV_LIT
: return "LV_LIT";
1043 case FT_LV_DAT
: return "LV_DAT";
1044 case FT_LV_STRLEN
: return "LV_STRLEN";
1045 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1046 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1047 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1048 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1049 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1050 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1051 case FT_LS_MONTH
: return "LS_MONTH";
1052 case FT_LS_LMONTH
: return "LS_LMONTH";
1053 case FT_LS_ZONE
: return "LS_ZONE";
1054 case FT_LS_DAY
: return "LS_DAY";
1055 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1056 case FT_LS_822DATE
: return "LS_822DATE";
1057 case FT_LS_PRETTY
: return "LS_PRETTY";
1058 case FT_LS_KILO
: return "LS_KILO";
1059 case FT_LS_KIBI
: return "LS_KIBI";
1060 case FT_LV_SEC
: return "LV_SEC";
1061 case FT_LV_MIN
: return "LV_MIN";
1062 case FT_LV_HOUR
: return "LV_HOUR";
1063 case FT_LV_MDAY
: return "LV_MDAY";
1064 case FT_LV_MON
: return "LV_MON";
1065 case FT_LV_YEAR
: return "LV_YEAR";
1066 case FT_LV_YDAY
: return "LV_YDAY";
1067 case FT_LV_WDAY
: return "LV_WDAY";
1068 case FT_LV_ZONE
: return "LV_ZONE";
1069 case FT_LV_CLOCK
: return "LV_CLOCK";
1070 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1071 case FT_LV_DAYF
: return "LV_DAYF";
1072 case FT_LV_DST
: return "LV_DST";
1073 case FT_LV_ZONEF
: return "LV_ZONEF";
1074 case FT_LS_PERS
: return "LS_PERS";
1075 case FT_LS_MBOX
: return "LS_MBOX";
1076 case FT_LS_HOST
: return "LS_HOST";
1077 case FT_LS_PATH
: return "LS_PATH";
1078 case FT_LS_GNAME
: return "LS_GNAME";
1079 case FT_LS_NOTE
: return "LS_NOTE";
1080 case FT_LS_ADDR
: return "LS_ADDR";
1081 case FT_LS_822ADDR
: return "LS_822ADDR";
1082 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1083 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1084 case FT_LV_INGRPF
: return "LV_INGRPF";
1085 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1086 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1087 case FT_LOCALDATE
: return "LOCALDATE";
1088 case FT_GMTDATE
: return "GMTDATE";
1089 case FT_PARSEDATE
: return "PARSEDATE";
1090 case FT_PARSEADDR
: return "PARSEADDR";
1091 case FT_FORMATADDR
: return "FORMATADDR";
1092 case FT_CONCATADDR
: return "CONCATADDR";
1093 case FT_MYMBOX
: return "MYMBOX";
1094 case FT_GETMYMBOX
: return "GETMYMBOX";
1095 case FT_GETMYADDR
: return "GETMYADDR";
1096 case FT_SAVESTR
: return "SAVESTR";
1097 case FT_DONE
: return "DONE";
1098 case FT_PAUSE
: return "PAUSE";
1099 case FT_NOP
: return "NOP";
1100 case FT_GOTO
: return "GOTO";
1101 case FT_IF_S_NULL
: return "IF_S_NULL";
1102 case FT_IF_S
: return "IF_S";
1103 case FT_IF_V_EQ
: return "IF_V_EQ";
1104 case FT_IF_V_NE
: return "IF_V_NE";
1105 case FT_IF_V_GT
: return "IF_V_GT";
1106 case FT_IF_MATCH
: return "IF_MATCH";
1107 case FT_IF_AMATCH
: return "IF_AMATCH";
1108 case FT_S_NULL
: return "S_NULL";
1109 case FT_S_NONNULL
: return "S_NONNULL";
1110 case FT_V_EQ
: return "V_EQ";
1111 case FT_V_NE
: return "V_NE";
1112 case FT_V_GT
: return "V_GT";
1113 case FT_V_MATCH
: return "V_MATCH";
1114 case FT_V_AMATCH
: return "V_AMATCH";
1116 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1124 static char buf
[64];
1126 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1133 static char buf
[64];
1135 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1140 litputs(const char *s
)
1148 fputs("<nil>", stdout
);
1155 printf("\\x%02x", (unsigned char) c
);
1156 } else if (c
< 0x20 || c
== 0177) {
1160 } else if (c
== '\f') {
1163 } else if (c
== '\n') {
1166 } else if (c
== '\r') {
1169 } else if (c
== '\t') {
1174 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1181 * Routines/code to support the duplicate address suppression code, adapted
1185 static char *buf
; /* our current working buffer */
1186 static char *bufend
; /* end of working buffer */
1187 static char *last_dst
; /* buf ptr at end of last call */
1188 static unsigned int bufsiz
=0; /* current size of buf */
1190 #define BUFINCR 512 /* how much to expand buf when if fills */
1192 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1195 * check if there's enough room in buf for str.
1196 * add more mem if needed
1198 #define CHECKMEM(str) \
1199 if ((len = strlen (str)) >= bufend - dst) {\
1201 int n = last_dst - buf;\
1202 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1203 buf = mh_xrealloc (buf, bufsiz);\
1205 last_dst = buf + n;\
1206 bufend = buf + bufsiz;\
1211 * These are versions of similar routines from replsbr.c; the purpose is
1212 * to suppress duplicate addresses from being added to a list when building
1213 * up addresses for the %(formataddr) format function. This is used by
1214 * repl to prevent duplicate addresses from being added to the "to" line.
1215 * See replsbr.c for more information.
1217 * We can't use the functions in replsbr.c directly because they are slightly
1218 * different and depend on the rest of replsbr.c
1221 test_formataddr (char *orig
, char *str
)
1229 struct mailname
*mp
= NULL
;
1231 /* if we don't have a buffer yet, get one */
1233 buf
= mh_xmalloc (BUFINCR
);
1234 last_dst
= buf
; /* XXX */
1235 bufsiz
= BUFINCR
- 6; /* leave some slop */
1236 bufend
= buf
+ bufsiz
;
1239 * If "orig" points to our buffer we can just pick up where we
1240 * left off. Otherwise we have to copy orig into our buffer.
1244 else if (!orig
|| !*orig
) {
1248 dst
= last_dst
; /* XXX */
1253 /* concatenate all the new addresses onto 'buf' */
1254 for (isgroup
= false; (cp
= getname (str
)); ) {
1255 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1256 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1259 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1264 /* if we get here we're going to add an address */
1270 CHECKMEM (mp
->m_gname
);
1274 sp
= adrformat (mp
);
1290 * The companion to test_formataddr(); it behaves the same way, except doesn't
1291 * do duplicate address detection.
1294 test_concataddr(char *orig
, char *str
)
1299 cp
= test_formataddr(orig
, str
);
1305 insert (struct mailname
*np
)
1307 struct mailname
*mp
;
1312 if (np
->m_mbox
== NULL
)
1315 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1316 if (!strcasecmp (FENDNULL(np
->m_host
),
1317 FENDNULL(mp
->m_next
->m_host
)) &&
1318 !strcasecmp (FENDNULL(np
->m_mbox
),
1319 FENDNULL(mp
->m_next
->m_mbox
)))
1322 if (!ccme
&& ismymbox (np
))
1331 * Reset our duplicate address list
1337 struct mailname
*mp
, *mp2
;
1339 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {