]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
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.
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/seq_getnum.h"
13 #include "sbr/error.h"
14 #include "h/fmt_scan.h"
15 #include "h/fmt_compile.h"
17 #include "h/scansbr.h"
18 #include "h/addrsbr.h"
20 #include "sbr/m_maildir.h"
21 #include "sbr/terminal.h"
23 #define FMTTEST_SWITCHES \
24 X("form formatfile", 0, FORMSW) \
25 X("format string", 5, FMTSW) \
26 X("address", 0, ADDRSW) \
28 X("date", 0, DATESW) \
29 X("message", 0, MESSAGESW) \
30 X("file", 0, FILESW) \
31 X("nofile", 0, NFILESW) \
32 X("-component-name component-text", 0, OTHERSW) \
33 X("dupaddrs", 0, DUPADDRSW) \
34 X("nodupaddrs", 0, NDUPADDRSW) \
35 X("ccme", 0, CCMESW) \
36 X("noccme", 0, NCCMESW) \
37 X("outsize size-in-characters", 0, OUTSIZESW) \
38 X("width column-width", 0, WIDTHSW) \
39 X("msgnum number", 0, MSGNUMSW) \
40 X("msgcur flag", 0, MSGCURSW) \
41 X("msgsize size", 0, MSGSIZESW) \
42 X("unseen flag", 0, UNSEENSW) \
43 X("dump", 0, DUMPSW) \
44 X("nodump", 0, NDUMPSW) \
45 X("trace", 0, TRACESW) \
46 X("notrace", 0, NTRACESW) \
47 X("version", 0, VERSIONSW) \
48 X("help", 0, HELPSW) \
50 #define X(sw, minchars, id) id,
51 DEFINE_SWITCH_ENUM(FMTTEST
);
54 #define X(sw, minchars, id) { sw, minchars, id },
55 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
59 * An array containing labels used for branch instructions
62 static struct format
**lvec
= NULL
;
64 static int lallocated
= 0;
66 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
67 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
68 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
71 * Context structure used by the tracing routines
74 struct trace_context
{
83 static void fmt_dump (char *, struct format
*);
84 static void dumpone(struct format
*);
85 static void initlabels(struct format
*);
86 static int findlabel(struct format
*);
87 static void assignlabel(struct format
*);
88 static char *f_typestr(int);
89 static char *c_typestr(int);
90 static char *c_flagsstr(int);
91 static void litputs(const char *);
92 static void litputc(char);
93 static void process_addresses(struct format
*, struct msgs_array
*,
94 charstring_t
, int, int *,
95 struct fmt_callbacks
*);
96 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
97 int, int *, struct fmt_callbacks
*);
98 static void process_messages(struct format
*, struct msgs_array
*,
99 struct msgs_array
*, charstring_t
, char *, int,
100 int, int *, struct fmt_callbacks
*);
101 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
102 struct format
*, charstring_t
, int,
103 struct fmt_callbacks
*);
104 static void test_trace(void *, struct format
*, int, char *, const char *);
105 static char *test_formataddr(char *, char *);
106 static char *test_concataddr(char *, char *);
107 static int insert(struct mailname
*);
108 static void mlistfree(void);
110 static bool nodupcheck
; /* If set, no check for duplicates */
111 static bool ccme
; /* Should I cc myself? */
112 static struct mailname mq
; /* Mail addresses to check for duplicates */
113 static char *dummy
= "dummy";
116 main (int argc
, char **argv
)
118 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
119 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
123 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
127 bool dupaddrs
= true;
130 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
131 enum mode_t mode
= MESSAGE
;
133 struct fmt_callbacks cb
, *cbp
= NULL
;
135 if (nmh_init(argv
[0], true, true)) { return 1; }
137 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 while ((cp
= *argp
++)) {
143 * A -- means that we have a component name (like pick);
144 * save the component name and the next argument for the text.
148 die("missing component name after --");
149 app_msgarg(&compargs
, cp
);
150 /* Grab next argument for component text */
152 die("missing argument to %s", argp
[-2]);
153 app_msgarg(&compargs
, cp
);
156 switch (smatch (cp
, switches
)) {
158 ambigsw (cp
, switches
);
161 die("-%s unknown", cp
);
164 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
165 print_help (buf
, switches
, 1);
168 print_version(invo_name
);
171 die("internal argument error!");
175 if (!(cp
= *argp
++) || *cp
== '-')
176 die("missing argument to %s", argp
[-2]);
177 if (strcmp(cp
, "max") == 0)
178 outputsize
= INT_MAX
;
179 else if (strcmp(cp
, "width") == 0)
180 outputsize
= sc_width();
182 outputsize
= atoi(cp
);
186 if (!(form
= *argp
++) || *form
== '-')
187 die("missing argument to %s", argp
[-2]);
191 if (!(format
= *argp
++) || *format
== '-')
192 die("missing argument to %s", argp
[-2]);
205 defformat
= DEFADDRFORMAT
;
217 defformat
= DEFDATEFORMAT
;
242 if (!(cp
= *argp
++) || *cp
== '-')
243 die("missing argument to %s", argp
[-2]);
247 if (!(cp
= *argp
++) || *cp
== '-')
248 die("missing argument to %s", argp
[-2]);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 die("missing argument to %s", argp
[-2]);
257 if (!(cp
= *argp
++) || *cp
== '-')
258 die("missing argument to %s", argp
[-2]);
262 if (!(cp
= *argp
++) || *cp
== '-')
263 die("missing argument to %s", argp
[-2]);
264 msgunseen
= atoi(cp
);
278 * Only interpret as a folder if we're in message mode
281 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
283 die("only one folder at a time!");
284 folder
= pluspath (cp
);
286 app_msgarg(&msgs
, cp
);
290 * Here's our weird heuristic:
292 * - We allow -dump without any other arguments.
293 * - If you've given any component arguments, we don't require any
295 * - The arguments are interpreted as folders/messages _if_ we're in
296 * message mode, otherwise pass as strings in the text component.
299 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
300 die("usage: [switches] [+folder] msgs | strings...");
304 * If you're picking "raw" as a mode, then you have to select
308 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
309 die("You must specify a format with -form or -format when "
314 * Get new format string. Must be before chdir().
316 nfs
= new_fs (form
, format
, defformat
);
317 (void) fmt_compile(nfs
, &fmt
, 1);
323 if (compargs
.size
== 0 && msgs
.size
== 0)
328 buffer
= charstring_create(BUFSIZ
);
330 if (outputsize
== 0) {
332 outputsize
= sc_width();
334 outputsize
= INT_MAX
;
340 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
344 * If we want to provide our own formataddr, concactaddr, or tracing
345 * callback, do that now. Also, prime ismymbox if we use it.
348 if (!dupaddrs
|| trace
) {
353 cb
.formataddr
= test_formataddr
;
354 cb
.concataddr
= test_concataddr
;
360 struct trace_context
*ctx
;
365 ctx
->outbuf
= mh_xstrdup("");
367 cb
.trace_func
= test_trace
;
368 cb
.trace_context
= ctx
;
372 if (mode
== MESSAGE
) {
373 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
377 for (i
= 0; i
< compargs
.size
; i
+= 2) {
378 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
380 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
384 if (mode
== ADDRESS
) {
385 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
386 } else /* Fall-through for RAW or DATE */
387 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
390 charstring_free(buffer
);
398 * Process each address with fmt_scan().
404 struct pqpair
*pq_next
;
408 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
409 charstring_t buffer
, int outwidth
, int *dat
,
410 struct fmt_callbacks
*cb
)
413 char *cp
, error
[BUFSIZ
];
415 struct pqpair
*p
, *q
;
428 for (i
= 0; i
< addrs
->size
; i
++) {
429 (q
= &pq
)->pq_next
= NULL
;
430 while ((cp
= getname(addrs
->msgs
[i
]))) {
432 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
433 p
->pq_text
= mh_xstrdup(cp
);
434 p
->pq_error
= mh_xstrdup(error
);
436 p
->pq_text
= getcpy(mp
->m_text
);
439 q
= (q
->pq_next
= p
);
442 for (p
= pq
.pq_next
; p
; p
= q
) {
443 c
= fmt_findcomp("text");
446 c
->c_text
= p
->pq_text
;
449 c
= fmt_findcomp("error");
452 c
->c_text
= p
->pq_error
;
456 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
457 fputs(charstring_buffer(buffer
), stdout
);
469 * Process messages and run them through the format engine. A lot taken
474 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
475 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
476 int outwidth
, int files
, int *dat
,
477 struct fmt_callbacks
*cb
)
479 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
480 int num_unseen_seq
= 0;
481 ivector_t seqnum
= ivector_create (0);
487 * If 'files' is set, short-circuit everything else and just process
492 for (i
= 0; i
< msgs
->size
; i
++) {
493 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
494 admonish(cp
, "unable to open file");
497 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
505 folder
= getfolder(1);
507 maildir
= m_maildir(folder
);
509 if (chdir(maildir
) < 0)
510 adios(maildir
, "unable to change directory to");
512 if (!(mp
= folder_read(folder
, 1)))
513 die("unable to read folder %s", folder
);
516 die("no messages in %s", folder
);
518 for (i
= 0; i
< msgs
->size
; i
++)
519 if (!m_convert(mp
, msgs
->msgs
[i
]))
521 seq_setprev(mp
); /* set the Previous-Sequence */
523 context_replace(pfolder
, folder
); /* update current folder */
524 seq_save(mp
); /* synchronize message sequences */
525 context_save(); /* save the context file */
528 * We want to set the unseen flag if requested, so we have to check
529 * the unseen sequence as well.
533 if ((cp
= context_find(usequence
)) && *cp
) {
537 ap
= brkstring(dp
, " ", "\n");
538 for (i
= 0; ap
&& *ap
; i
++, ap
++)
539 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
546 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
547 if (is_selected(mp
, msgnum
)) {
548 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
549 admonish(cp
, "unable to open message");
559 dat
[1] = msgnum
== mp
->curmsg
;
562 * Check to see if this is in the unseen sequence
566 for (i
= 0; i
< num_unseen_seq
; i
++) {
567 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
574 * Read in the message and process the components
577 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
582 ivector_free (seqnum
);
587 * Process a single file in message mode
591 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
592 struct format
*fmt
, charstring_t buffer
, int outwidth
,
593 struct fmt_callbacks
*cb
)
596 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
597 m_getfld_state_t gstate
;
602 * Get our size if we didn't include one
608 if (fstat(fileno(in
), &st
) < 0)
615 * Initialize everything else
626 * Read in the message and process the components
629 gstate
= m_getfld_state_init(in
);
631 bufsz
= sizeof(rbuf
);
632 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
636 i
= fmt_addcomptext(name
, rbuf
);
638 while (state
== FLDPLUS
) {
639 bufsz
= sizeof(rbuf
);
640 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
641 fmt_appendcomp(i
, name
, rbuf
);
645 while (state
== FLDPLUS
) {
646 bufsz
= sizeof(rbuf
);
647 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
652 if (fmt_findcomp("body")) {
653 if ((i
= strlen(rbuf
)) < outwidth
) {
654 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
655 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
658 fmt_addcomptext("body", rbuf
);
668 m_getfld_state_destroy(&gstate
);
671 * Do this now to override any components in the original message
674 for (i
= 0; i
< comps
->size
; i
+= 2) {
675 c
= fmt_findcomp(comps
->msgs
[i
]);
678 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
682 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
683 fputs(charstring_buffer (buffer
), stdout
);
688 * Run text through the format engine with no special processing
692 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
693 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
707 c
= fmt_findcomp("text");
709 for (i
= 0; i
< text
->size
; i
++) {
712 c
->c_text
= getcpy(text
->msgs
[i
]);
715 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
716 fputs(charstring_buffer (buffer
), stdout
);
722 * Our basic tracing support callback.
724 * Print out each instruction as it's executed, including the values of
725 * the num and str registers if they've changed.
729 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
732 struct trace_context
*ctx
= (struct trace_context
*) context
;
733 bool changed
= false;
737 if (num
!= ctx
->num
) {
738 printf("num=%d", num
);
743 if (str
!= ctx
->str
) {
747 fputs("str=", stdout
);
755 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
756 fputs("outbuf=", stdout
);
760 ctx
->outbuf
= mh_xstrdup(outbuf
);
765 fmt_dump (char *nfs
, struct format
*fmth
)
769 printf("Instruction dump of format string: \n%s\n", nfs
);
772 for (fmt
= fmth
; fmt
; ++fmt
) {
774 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
780 dumpone(struct format
*fmt
)
784 if ((i
= findlabel(fmt
)) >= 0)
788 fputs(f_typestr((int)fmt
->f_type
), stdout
);
790 switch (fmt
->f_type
) {
796 fputs(", comp ", stdout
);
797 litputs(fmt
->f_comp
->c_name
);
798 if (fmt
->f_comp
->c_type
)
799 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
800 if (fmt
->f_comp
->c_flags
)
801 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
828 fputs(", c_name ", stdout
);
829 litputs(fmt
->f_comp
->c_name
);
830 if (fmt
->f_comp
->c_type
)
831 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
832 if (fmt
->f_comp
->c_flags
)
833 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
852 fputs(", c_name ", stdout
);
853 litputs(fmt
->f_comp
->c_name
);
854 if (fmt
->f_comp
->c_type
)
855 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
856 if (fmt
->f_comp
->c_flags
)
857 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
861 printf(", width %d, fill '", fmt
->f_width
);
862 litputc(fmt
->f_fill
);
863 fputs("' name ", stdout
);
864 litputs(fmt
->f_comp
->c_name
);
865 if (fmt
->f_comp
->c_type
)
866 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
867 if (fmt
->f_comp
->c_flags
)
868 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
873 printf(", width %d, fill '", fmt
->f_width
);
874 litputc(fmt
->f_fill
);
880 litputs(fmt
->f_text
);
884 printf(", width %d, fill '", fmt
->f_width
);
885 litputc(fmt
->f_fill
);
887 litputs(fmt
->f_text
);
893 litputc(fmt
->f_char
);
902 fputs(" continue else goto", stdout
);
905 i
= findlabel(fmt
+ fmt
->f_skip
);
912 i
= findlabel(fmt
+ fmt
->f_skip
);
913 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
922 case FT_LV_MULTIPLY_L
:
925 printf(" value %d", fmt
->f_value
);
929 fputs(" str ", stdout
);
930 litputs(fmt
->f_text
);
934 fputs(" getenv ", stdout
);
935 litputs(fmt
->f_text
);
938 case FT_LS_DECODECOMP
:
939 fputs(", comp ", stdout
);
940 litputs(fmt
->f_comp
->c_name
);
947 printf(", width %d", fmt
->f_width
);
951 printf(", value dat[%d]", fmt
->f_value
);
958 * Iterate over all instructions and assign labels to the targets of
963 initlabels(struct format
*fmth
)
965 struct format
*fmt
, *addr
;
969 for (fmt
= fmth
; fmt
; ++fmt
) {
979 addr
= fmt
+ fmt
->f_skip
;
980 if (findlabel(addr
) < 0)
983 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
990 findlabel(struct format
*addr
)
994 for (i
= 0; i
< lused
; ++i
)
1001 assignlabel(struct format
*addr
)
1003 if (lused
>= lallocated
) {
1005 lvec
= (struct format
**)
1006 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1009 lvec
[lused
++] = addr
;
1015 static char buf
[32];
1018 case FT_COMP
: return "COMP";
1019 case FT_COMPF
: return "COMPF";
1020 case FT_LIT
: return "LIT";
1021 case FT_LITF
: return "LITF";
1022 case FT_CHAR
: return "CHAR";
1023 case FT_NUM
: return "NUM";
1024 case FT_NUMF
: return "NUMF";
1025 case FT_STR
: return "STR";
1026 case FT_STRF
: return "STRF";
1027 case FT_STRFW
: return "STRFW";
1028 case FT_STRLIT
: return "STRLIT";
1029 case FT_STRLITZ
: return "STRLITZ";
1030 case FT_PUTADDR
: return "PUTADDR";
1031 case FT_LS_COMP
: return "LS_COMP";
1032 case FT_LS_LIT
: return "LS_LIT";
1033 case FT_LS_GETENV
: return "LS_GETENV";
1034 case FT_LS_CFIND
: return "LS_CFIND";
1035 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1036 case FT_LS_DECODE
: return "LS_DECODE";
1037 case FT_LS_TRIM
: return "LS_TRIM";
1038 case FT_LV_COMP
: return "LV_COMP";
1039 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1040 case FT_LV_LIT
: return "LV_LIT";
1041 case FT_LV_DAT
: return "LV_DAT";
1042 case FT_LV_STRLEN
: return "LV_STRLEN";
1043 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1044 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1045 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1046 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1047 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1048 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1049 case FT_LS_MONTH
: return "LS_MONTH";
1050 case FT_LS_LMONTH
: return "LS_LMONTH";
1051 case FT_LS_ZONE
: return "LS_ZONE";
1052 case FT_LS_DAY
: return "LS_DAY";
1053 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1054 case FT_LS_822DATE
: return "LS_822DATE";
1055 case FT_LS_PRETTY
: return "LS_PRETTY";
1056 case FT_LS_KILO
: return "LS_KILO";
1057 case FT_LS_KIBI
: return "LS_KIBI";
1058 case FT_LV_SEC
: return "LV_SEC";
1059 case FT_LV_MIN
: return "LV_MIN";
1060 case FT_LV_HOUR
: return "LV_HOUR";
1061 case FT_LV_MDAY
: return "LV_MDAY";
1062 case FT_LV_MON
: return "LV_MON";
1063 case FT_LV_YEAR
: return "LV_YEAR";
1064 case FT_LV_YDAY
: return "LV_YDAY";
1065 case FT_LV_WDAY
: return "LV_WDAY";
1066 case FT_LV_ZONE
: return "LV_ZONE";
1067 case FT_LV_CLOCK
: return "LV_CLOCK";
1068 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1069 case FT_LV_DAYF
: return "LV_DAYF";
1070 case FT_LV_DST
: return "LV_DST";
1071 case FT_LV_ZONEF
: return "LV_ZONEF";
1072 case FT_LS_PERS
: return "LS_PERS";
1073 case FT_LS_MBOX
: return "LS_MBOX";
1074 case FT_LS_HOST
: return "LS_HOST";
1075 case FT_LS_PATH
: return "LS_PATH";
1076 case FT_LS_GNAME
: return "LS_GNAME";
1077 case FT_LS_NOTE
: return "LS_NOTE";
1078 case FT_LS_ADDR
: return "LS_ADDR";
1079 case FT_LS_822ADDR
: return "LS_822ADDR";
1080 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1081 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1082 case FT_LV_INGRPF
: return "LV_INGRPF";
1083 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1084 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1085 case FT_LOCALDATE
: return "LOCALDATE";
1086 case FT_GMTDATE
: return "GMTDATE";
1087 case FT_PARSEDATE
: return "PARSEDATE";
1088 case FT_PARSEADDR
: return "PARSEADDR";
1089 case FT_FORMATADDR
: return "FORMATADDR";
1090 case FT_CONCATADDR
: return "CONCATADDR";
1091 case FT_MYMBOX
: return "MYMBOX";
1092 case FT_GETMYMBOX
: return "GETMYMBOX";
1093 case FT_GETMYADDR
: return "GETMYADDR";
1094 case FT_SAVESTR
: return "SAVESTR";
1095 case FT_DONE
: return "DONE";
1096 case FT_PAUSE
: return "PAUSE";
1097 case FT_NOP
: return "NOP";
1098 case FT_GOTO
: return "GOTO";
1099 case FT_IF_S_NULL
: return "IF_S_NULL";
1100 case FT_IF_S
: return "IF_S";
1101 case FT_IF_V_EQ
: return "IF_V_EQ";
1102 case FT_IF_V_NE
: return "IF_V_NE";
1103 case FT_IF_V_GT
: return "IF_V_GT";
1104 case FT_IF_MATCH
: return "IF_MATCH";
1105 case FT_IF_AMATCH
: return "IF_AMATCH";
1106 case FT_S_NULL
: return "S_NULL";
1107 case FT_S_NONNULL
: return "S_NONNULL";
1108 case FT_V_EQ
: return "V_EQ";
1109 case FT_V_NE
: return "V_NE";
1110 case FT_V_GT
: return "V_GT";
1111 case FT_V_MATCH
: return "V_MATCH";
1112 case FT_V_AMATCH
: return "V_AMATCH";
1114 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1122 static char buf
[64];
1124 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1131 static char buf
[64];
1133 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1138 litputs(const char *s
)
1146 fputs("<nil>", stdout
);
1153 printf("\\x%02x", (unsigned char) c
);
1154 } else if (c
< 0x20 || c
== 0177) {
1158 } else if (c
== '\f') {
1161 } else if (c
== '\n') {
1164 } else if (c
== '\r') {
1167 } else if (c
== '\t') {
1172 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1179 * Routines/code to support the duplicate address suppression code, adapted
1183 static char *buf
; /* our current working buffer */
1184 static char *bufend
; /* end of working buffer */
1185 static char *last_dst
; /* buf ptr at end of last call */
1186 static unsigned int bufsiz
=0; /* current size of buf */
1188 #define BUFINCR 512 /* how much to expand buf when if fills */
1190 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1193 * check if there's enough room in buf for str.
1194 * add more mem if needed
1196 #define CHECKMEM(str) \
1197 if ((len = strlen (str)) >= bufend - dst) {\
1199 int n = last_dst - buf;\
1200 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1201 buf = mh_xrealloc (buf, bufsiz);\
1203 last_dst = buf + n;\
1204 bufend = buf + bufsiz;\
1209 * These are versions of similar routines from replsbr.c; the purpose is
1210 * to suppress duplicate addresses from being added to a list when building
1211 * up addresses for the %(formataddr) format function. This is used by
1212 * repl to prevent duplicate addresses from being added to the "to" line.
1213 * See replsbr.c for more information.
1215 * We can't use the functions in replsbr.c directly because they are slightly
1216 * different and depend on the rest of replsbr.c
1219 test_formataddr (char *orig
, char *str
)
1227 struct mailname
*mp
= NULL
;
1229 /* if we don't have a buffer yet, get one */
1231 buf
= mh_xmalloc (BUFINCR
);
1232 last_dst
= buf
; /* XXX */
1233 bufsiz
= BUFINCR
- 6; /* leave some slop */
1234 bufend
= buf
+ bufsiz
;
1237 * If "orig" points to our buffer we can just pick up where we
1238 * left off. Otherwise we have to copy orig into our buffer.
1242 else if (!orig
|| !*orig
) {
1246 dst
= last_dst
; /* XXX */
1251 /* concatenate all the new addresses onto 'buf' */
1252 for (isgroup
= false; (cp
= getname (str
)); ) {
1253 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1254 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1257 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1262 /* if we get here we're going to add an address */
1268 CHECKMEM (mp
->m_gname
);
1272 sp
= adrformat (mp
);
1288 * The companion to test_formataddr(); it behaves the same way, except doesn't
1289 * do duplicate address detection.
1292 test_concataddr(char *orig
, char *str
)
1297 cp
= test_formataddr(orig
, str
);
1303 insert (struct mailname
*np
)
1305 struct mailname
*mp
;
1310 if (np
->m_mbox
== NULL
)
1313 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1314 if (!strcasecmp (FENDNULL(np
->m_host
),
1315 FENDNULL(mp
->m_next
->m_host
)) &&
1316 !strcasecmp (FENDNULL(np
->m_mbox
),
1317 FENDNULL(mp
->m_next
->m_mbox
)))
1320 if (!ccme
&& ismymbox (np
))
1329 * Reset our duplicate address list
1335 struct mailname
*mp
, *mp2
;
1337 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {