]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
7d528c1f90c1d6753bdb9fc5363e938171292a71
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/folder_free.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/brkstring.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/seq_getnum.h"
19 #include "sbr/error.h"
20 #include "h/fmt_scan.h"
21 #include "h/fmt_compile.h"
23 #include "h/scansbr.h"
24 #include "h/addrsbr.h"
26 #include "sbr/m_maildir.h"
27 #include "sbr/terminal.h"
29 #define FMTTEST_SWITCHES \
30 X("form formatfile", 0, FORMSW) \
31 X("format string", 5, FMTSW) \
32 X("address", 0, ADDRSW) \
34 X("date", 0, DATESW) \
35 X("message", 0, MESSAGESW) \
36 X("file", 0, FILESW) \
37 X("nofile", 0, NFILESW) \
38 X("-component-name component-text", 0, OTHERSW) \
39 X("dupaddrs", 0, DUPADDRSW) \
40 X("nodupaddrs", 0, NDUPADDRSW) \
41 X("ccme", 0, CCMESW) \
42 X("noccme", 0, NCCMESW) \
43 X("outsize size-in-characters", 0, OUTSIZESW) \
44 X("width column-width", 0, WIDTHSW) \
45 X("msgnum number", 0, MSGNUMSW) \
46 X("msgcur flag", 0, MSGCURSW) \
47 X("msgsize size", 0, MSGSIZESW) \
48 X("unseen flag", 0, UNSEENSW) \
49 X("dump", 0, DUMPSW) \
50 X("nodump", 0, NDUMPSW) \
51 X("trace", 0, TRACESW) \
52 X("notrace", 0, NTRACESW) \
53 X("version", 0, VERSIONSW) \
54 X("help", 0, HELPSW) \
56 #define X(sw, minchars, id) id,
57 DEFINE_SWITCH_ENUM(FMTTEST
);
60 #define X(sw, minchars, id) { sw, minchars, id },
61 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
65 * An array containing labels used for branch instructions
68 static struct format
**lvec
= NULL
;
70 static int lallocated
= 0;
72 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
73 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
74 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
77 * Context structure used by the tracing routines
80 struct trace_context
{
89 static void fmt_dump (char *, struct format
*);
90 static void dumpone(struct format
*);
91 static void initlabels(struct format
*);
92 static int findlabel(struct format
*);
93 static void assignlabel(struct format
*);
94 static char *f_typestr(int);
95 static char *c_typestr(int);
96 static char *c_flagsstr(int);
97 static void litputs(const char *);
98 static void litputc(char);
99 static void process_addresses(struct format
*, struct msgs_array
*,
100 charstring_t
, int, int *,
101 struct fmt_callbacks
*);
102 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
103 int, int *, struct fmt_callbacks
*);
104 static void process_messages(struct format
*, struct msgs_array
*,
105 struct msgs_array
*, charstring_t
, char *, int,
106 int, int *, struct fmt_callbacks
*);
107 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
108 struct format
*, charstring_t
, int,
109 struct fmt_callbacks
*);
110 static void test_trace(void *, struct format
*, int, char *, const char *);
111 static char *test_formataddr(char *, char *);
112 static char *test_concataddr(char *, char *);
113 static int insert(struct mailname
*);
114 static void mlistfree(void);
116 static bool nodupcheck
; /* If set, no check for duplicates */
117 static bool ccme
; /* Should I cc myself? */
118 static struct mailname mq
; /* Mail addresses to check for duplicates */
119 static char *dummy
= "dummy";
122 main (int argc
, char **argv
)
124 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
125 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
129 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
133 bool dupaddrs
= true;
136 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
137 enum mode_t mode
= MESSAGE
;
139 struct fmt_callbacks cb
, *cbp
= NULL
;
141 if (nmh_init(argv
[0], true, true)) { return 1; }
143 arguments
= getarguments (invo_name
, argc
, argv
, 1);
146 while ((cp
= *argp
++)) {
149 * A -- means that we have a component name (like pick);
150 * save the component name and the next argument for the text.
154 die("missing component name after --");
155 app_msgarg(&compargs
, cp
);
156 /* Grab next argument for component text */
158 die("missing argument to %s", argp
[-2]);
159 app_msgarg(&compargs
, cp
);
162 switch (smatch (cp
, switches
)) {
164 ambigsw (cp
, switches
);
167 die("-%s unknown", cp
);
170 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
171 print_help (buf
, switches
, 1);
174 print_version(invo_name
);
177 die("internal argument error!");
181 if (!(cp
= *argp
++) || *cp
== '-')
182 die("missing argument to %s", argp
[-2]);
183 if (strcmp(cp
, "max") == 0)
184 outputsize
= INT_MAX
;
185 else if (strcmp(cp
, "width") == 0)
186 outputsize
= sc_width();
188 outputsize
= atoi(cp
);
192 if (!(form
= *argp
++) || *form
== '-')
193 die("missing argument to %s", argp
[-2]);
197 if (!(format
= *argp
++) || *format
== '-')
198 die("missing argument to %s", argp
[-2]);
211 defformat
= DEFADDRFORMAT
;
223 defformat
= DEFDATEFORMAT
;
248 if (!(cp
= *argp
++) || *cp
== '-')
249 die("missing argument to %s", argp
[-2]);
253 if (!(cp
= *argp
++) || *cp
== '-')
254 die("missing argument to %s", argp
[-2]);
258 if (!(cp
= *argp
++) || *cp
== '-')
259 die("missing argument to %s", argp
[-2]);
263 if (!(cp
= *argp
++) || *cp
== '-')
264 die("missing argument to %s", argp
[-2]);
268 if (!(cp
= *argp
++) || *cp
== '-')
269 die("missing argument to %s", argp
[-2]);
270 msgunseen
= atoi(cp
);
284 * Only interpret as a folder if we're in message mode
287 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
289 die("only one folder at a time!");
290 folder
= pluspath (cp
);
292 app_msgarg(&msgs
, cp
);
296 * Here's our weird heuristic:
298 * - We allow -dump without any other arguments.
299 * - If you've given any component arguments, we don't require any
301 * - The arguments are interpreted as folders/messages _if_ we're in
302 * message mode, otherwise pass as strings in the text component.
305 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
306 die("usage: [switches] [+folder] msgs | strings...");
310 * If you're picking "raw" as a mode, then you have to select
314 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
315 die("You must specify a format with -form or -format when "
320 * Get new format string. Must be before chdir().
322 nfs
= new_fs (form
, format
, defformat
);
323 (void) fmt_compile(nfs
, &fmt
, 1);
329 if (compargs
.size
== 0 && msgs
.size
== 0)
334 buffer
= charstring_create(BUFSIZ
);
336 if (outputsize
== 0) {
338 outputsize
= sc_width();
340 outputsize
= INT_MAX
;
346 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
350 * If we want to provide our own formataddr, concactaddr, or tracing
351 * callback, do that now. Also, prime ismymbox if we use it.
354 if (!dupaddrs
|| trace
) {
359 cb
.formataddr
= test_formataddr
;
360 cb
.concataddr
= test_concataddr
;
366 struct trace_context
*ctx
;
371 ctx
->outbuf
= mh_xstrdup("");
373 cb
.trace_func
= test_trace
;
374 cb
.trace_context
= ctx
;
378 if (mode
== MESSAGE
) {
379 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
383 for (i
= 0; i
< compargs
.size
; i
+= 2) {
384 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
386 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
390 if (mode
== ADDRESS
) {
391 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
392 } else /* Fall-through for RAW or DATE */
393 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
396 charstring_free(buffer
);
404 * Process each address with fmt_scan().
410 struct pqpair
*pq_next
;
414 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
415 charstring_t buffer
, int outwidth
, int *dat
,
416 struct fmt_callbacks
*cb
)
419 char *cp
, error
[BUFSIZ
];
421 struct pqpair
*p
, *q
;
434 for (i
= 0; i
< addrs
->size
; i
++) {
435 (q
= &pq
)->pq_next
= NULL
;
436 while ((cp
= getname(addrs
->msgs
[i
]))) {
438 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
439 p
->pq_text
= mh_xstrdup(cp
);
440 p
->pq_error
= mh_xstrdup(error
);
442 p
->pq_text
= getcpy(mp
->m_text
);
445 q
= (q
->pq_next
= p
);
448 for (p
= pq
.pq_next
; p
; p
= q
) {
449 c
= fmt_findcomp("text");
452 c
->c_text
= p
->pq_text
;
455 c
= fmt_findcomp("error");
458 c
->c_text
= p
->pq_error
;
462 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
463 fputs(charstring_buffer(buffer
), stdout
);
475 * Process messages and run them through the format engine. A lot taken
480 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
481 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
482 int outwidth
, int files
, int *dat
,
483 struct fmt_callbacks
*cb
)
485 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
486 int num_unseen_seq
= 0;
487 ivector_t seqnum
= ivector_create (0);
493 * If 'files' is set, short-circuit everything else and just process
498 for (i
= 0; i
< msgs
->size
; i
++) {
499 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
500 admonish(cp
, "unable to open file");
503 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
511 folder
= getfolder(1);
513 maildir
= m_maildir(folder
);
515 if (chdir(maildir
) < 0)
516 adios(maildir
, "unable to change directory to");
518 if (!(mp
= folder_read(folder
, 1)))
519 die("unable to read folder %s", folder
);
522 die("no messages in %s", folder
);
524 for (i
= 0; i
< msgs
->size
; i
++)
525 if (!m_convert(mp
, msgs
->msgs
[i
]))
527 seq_setprev(mp
); /* set the Previous-Sequence */
529 context_replace(pfolder
, folder
); /* update current folder */
530 seq_save(mp
); /* synchronize message sequences */
531 context_save(); /* save the context file */
534 * We want to set the unseen flag if requested, so we have to check
535 * the unseen sequence as well.
539 if ((cp
= context_find(usequence
)) && *cp
) {
543 ap
= brkstring(dp
, " ", "\n");
544 for (i
= 0; ap
&& *ap
; i
++, ap
++)
545 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
552 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
553 if (is_selected(mp
, msgnum
)) {
554 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
555 admonish(cp
, "unable to open message");
565 dat
[1] = msgnum
== mp
->curmsg
;
568 * Check to see if this is in the unseen sequence
572 for (i
= 0; i
< num_unseen_seq
; i
++) {
573 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
580 * Read in the message and process the components
583 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
588 ivector_free (seqnum
);
593 * Process a single file in message mode
597 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
598 struct format
*fmt
, charstring_t buffer
, int outwidth
,
599 struct fmt_callbacks
*cb
)
602 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
603 m_getfld_state_t gstate
;
608 * Get our size if we didn't include one
614 if (fstat(fileno(in
), &st
) < 0)
621 * Initialize everything else
632 * Read in the message and process the components
635 gstate
= m_getfld_state_init(in
);
637 bufsz
= sizeof(rbuf
);
638 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
642 i
= fmt_addcomptext(name
, rbuf
);
644 while (state
== FLDPLUS
) {
645 bufsz
= sizeof(rbuf
);
646 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
647 fmt_appendcomp(i
, name
, rbuf
);
651 while (state
== FLDPLUS
) {
652 bufsz
= sizeof(rbuf
);
653 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
658 if (fmt_findcomp("body")) {
659 if ((i
= strlen(rbuf
)) < outwidth
) {
660 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
661 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
664 fmt_addcomptext("body", rbuf
);
674 m_getfld_state_destroy(&gstate
);
677 * Do this now to override any components in the original message
680 for (i
= 0; i
< comps
->size
; i
+= 2) {
681 c
= fmt_findcomp(comps
->msgs
[i
]);
684 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
688 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
689 fputs(charstring_buffer (buffer
), stdout
);
694 * Run text through the format engine with no special processing
698 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
699 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
713 c
= fmt_findcomp("text");
715 for (i
= 0; i
< text
->size
; i
++) {
718 c
->c_text
= getcpy(text
->msgs
[i
]);
721 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
722 fputs(charstring_buffer (buffer
), stdout
);
728 * Our basic tracing support callback.
730 * Print out each instruction as it's executed, including the values of
731 * the num and str registers if they've changed.
735 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
738 struct trace_context
*ctx
= (struct trace_context
*) context
;
739 bool changed
= false;
743 if (num
!= ctx
->num
) {
744 printf("num=%d", num
);
749 if (str
!= ctx
->str
) {
753 fputs("str=", stdout
);
761 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
762 fputs("outbuf=", stdout
);
766 ctx
->outbuf
= mh_xstrdup(outbuf
);
771 fmt_dump (char *nfs
, struct format
*fmth
)
775 printf("Instruction dump of format string: \n%s\n", nfs
);
778 for (fmt
= fmth
; fmt
; ++fmt
) {
780 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
786 dumpone(struct format
*fmt
)
790 if ((i
= findlabel(fmt
)) >= 0)
794 fputs(f_typestr((int)fmt
->f_type
), stdout
);
796 switch (fmt
->f_type
) {
802 fputs(", comp ", stdout
);
803 litputs(fmt
->f_comp
->c_name
);
804 if (fmt
->f_comp
->c_type
)
805 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
806 if (fmt
->f_comp
->c_flags
)
807 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
834 fputs(", c_name ", stdout
);
835 litputs(fmt
->f_comp
->c_name
);
836 if (fmt
->f_comp
->c_type
)
837 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
838 if (fmt
->f_comp
->c_flags
)
839 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
858 fputs(", c_name ", stdout
);
859 litputs(fmt
->f_comp
->c_name
);
860 if (fmt
->f_comp
->c_type
)
861 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
862 if (fmt
->f_comp
->c_flags
)
863 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
867 printf(", width %d, fill '", fmt
->f_width
);
868 litputc(fmt
->f_fill
);
869 fputs("' name ", stdout
);
870 litputs(fmt
->f_comp
->c_name
);
871 if (fmt
->f_comp
->c_type
)
872 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
873 if (fmt
->f_comp
->c_flags
)
874 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
879 printf(", width %d, fill '", fmt
->f_width
);
880 litputc(fmt
->f_fill
);
886 litputs(fmt
->f_text
);
890 printf(", width %d, fill '", fmt
->f_width
);
891 litputc(fmt
->f_fill
);
893 litputs(fmt
->f_text
);
899 litputc(fmt
->f_char
);
908 fputs(" continue else goto", stdout
);
911 i
= findlabel(fmt
+ fmt
->f_skip
);
918 i
= findlabel(fmt
+ fmt
->f_skip
);
919 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
928 case FT_LV_MULTIPLY_L
:
931 printf(" value %d", fmt
->f_value
);
935 fputs(" str ", stdout
);
936 litputs(fmt
->f_text
);
940 fputs(" getenv ", stdout
);
941 litputs(fmt
->f_text
);
944 case FT_LS_DECODECOMP
:
945 fputs(", comp ", stdout
);
946 litputs(fmt
->f_comp
->c_name
);
953 printf(", width %d", fmt
->f_width
);
957 printf(", value dat[%d]", fmt
->f_value
);
964 * Iterate over all instructions and assign labels to the targets of
969 initlabels(struct format
*fmth
)
971 struct format
*fmt
, *addr
;
975 for (fmt
= fmth
; fmt
; ++fmt
) {
985 addr
= fmt
+ fmt
->f_skip
;
986 if (findlabel(addr
) < 0)
989 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
996 findlabel(struct format
*addr
)
1000 for (i
= 0; i
< lused
; ++i
)
1001 if (addr
== lvec
[i
])
1007 assignlabel(struct format
*addr
)
1009 if (lused
>= lallocated
) {
1011 lvec
= (struct format
**)
1012 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1015 lvec
[lused
++] = addr
;
1021 static char buf
[32];
1024 case FT_COMP
: return "COMP";
1025 case FT_COMPF
: return "COMPF";
1026 case FT_LIT
: return "LIT";
1027 case FT_LITF
: return "LITF";
1028 case FT_CHAR
: return "CHAR";
1029 case FT_NUM
: return "NUM";
1030 case FT_NUMF
: return "NUMF";
1031 case FT_STR
: return "STR";
1032 case FT_STRF
: return "STRF";
1033 case FT_STRFW
: return "STRFW";
1034 case FT_STRLIT
: return "STRLIT";
1035 case FT_STRLITZ
: return "STRLITZ";
1036 case FT_PUTADDR
: return "PUTADDR";
1037 case FT_LS_COMP
: return "LS_COMP";
1038 case FT_LS_LIT
: return "LS_LIT";
1039 case FT_LS_GETENV
: return "LS_GETENV";
1040 case FT_LS_CFIND
: return "LS_CFIND";
1041 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1042 case FT_LS_DECODE
: return "LS_DECODE";
1043 case FT_LS_TRIM
: return "LS_TRIM";
1044 case FT_LV_COMP
: return "LV_COMP";
1045 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1046 case FT_LV_LIT
: return "LV_LIT";
1047 case FT_LV_DAT
: return "LV_DAT";
1048 case FT_LV_STRLEN
: return "LV_STRLEN";
1049 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1050 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1051 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1052 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1053 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1054 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1055 case FT_LS_MONTH
: return "LS_MONTH";
1056 case FT_LS_LMONTH
: return "LS_LMONTH";
1057 case FT_LS_ZONE
: return "LS_ZONE";
1058 case FT_LS_DAY
: return "LS_DAY";
1059 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1060 case FT_LS_822DATE
: return "LS_822DATE";
1061 case FT_LS_PRETTY
: return "LS_PRETTY";
1062 case FT_LS_KILO
: return "LS_KILO";
1063 case FT_LS_KIBI
: return "LS_KIBI";
1064 case FT_LV_SEC
: return "LV_SEC";
1065 case FT_LV_MIN
: return "LV_MIN";
1066 case FT_LV_HOUR
: return "LV_HOUR";
1067 case FT_LV_MDAY
: return "LV_MDAY";
1068 case FT_LV_MON
: return "LV_MON";
1069 case FT_LV_YEAR
: return "LV_YEAR";
1070 case FT_LV_YDAY
: return "LV_YDAY";
1071 case FT_LV_WDAY
: return "LV_WDAY";
1072 case FT_LV_ZONE
: return "LV_ZONE";
1073 case FT_LV_CLOCK
: return "LV_CLOCK";
1074 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1075 case FT_LV_DAYF
: return "LV_DAYF";
1076 case FT_LV_DST
: return "LV_DST";
1077 case FT_LV_ZONEF
: return "LV_ZONEF";
1078 case FT_LS_PERS
: return "LS_PERS";
1079 case FT_LS_MBOX
: return "LS_MBOX";
1080 case FT_LS_HOST
: return "LS_HOST";
1081 case FT_LS_PATH
: return "LS_PATH";
1082 case FT_LS_GNAME
: return "LS_GNAME";
1083 case FT_LS_NOTE
: return "LS_NOTE";
1084 case FT_LS_ADDR
: return "LS_ADDR";
1085 case FT_LS_822ADDR
: return "LS_822ADDR";
1086 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1087 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1088 case FT_LV_INGRPF
: return "LV_INGRPF";
1089 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1090 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1091 case FT_LOCALDATE
: return "LOCALDATE";
1092 case FT_GMTDATE
: return "GMTDATE";
1093 case FT_PARSEDATE
: return "PARSEDATE";
1094 case FT_PARSEADDR
: return "PARSEADDR";
1095 case FT_FORMATADDR
: return "FORMATADDR";
1096 case FT_CONCATADDR
: return "CONCATADDR";
1097 case FT_MYMBOX
: return "MYMBOX";
1098 case FT_GETMYMBOX
: return "GETMYMBOX";
1099 case FT_GETMYADDR
: return "GETMYADDR";
1100 case FT_SAVESTR
: return "SAVESTR";
1101 case FT_DONE
: return "DONE";
1102 case FT_PAUSE
: return "PAUSE";
1103 case FT_NOP
: return "NOP";
1104 case FT_GOTO
: return "GOTO";
1105 case FT_IF_S_NULL
: return "IF_S_NULL";
1106 case FT_IF_S
: return "IF_S";
1107 case FT_IF_V_EQ
: return "IF_V_EQ";
1108 case FT_IF_V_NE
: return "IF_V_NE";
1109 case FT_IF_V_GT
: return "IF_V_GT";
1110 case FT_IF_MATCH
: return "IF_MATCH";
1111 case FT_IF_AMATCH
: return "IF_AMATCH";
1112 case FT_S_NULL
: return "S_NULL";
1113 case FT_S_NONNULL
: return "S_NONNULL";
1114 case FT_V_EQ
: return "V_EQ";
1115 case FT_V_NE
: return "V_NE";
1116 case FT_V_GT
: return "V_GT";
1117 case FT_V_MATCH
: return "V_MATCH";
1118 case FT_V_AMATCH
: return "V_AMATCH";
1120 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1128 static char buf
[64];
1130 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1137 static char buf
[64];
1139 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1144 litputs(const char *s
)
1152 fputs("<nil>", stdout
);
1159 printf("\\x%02x", (unsigned char) c
);
1160 } else if (c
< 0x20 || c
== 0177) {
1164 } else if (c
== '\f') {
1167 } else if (c
== '\n') {
1170 } else if (c
== '\r') {
1173 } else if (c
== '\t') {
1178 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1185 * Routines/code to support the duplicate address suppression code, adapted
1189 static char *buf
; /* our current working buffer */
1190 static char *bufend
; /* end of working buffer */
1191 static char *last_dst
; /* buf ptr at end of last call */
1192 static unsigned int bufsiz
=0; /* current size of buf */
1194 #define BUFINCR 512 /* how much to expand buf when if fills */
1196 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1199 * check if there's enough room in buf for str.
1200 * add more mem if needed
1202 #define CHECKMEM(str) \
1203 if ((len = strlen (str)) >= bufend - dst) {\
1205 int n = last_dst - buf;\
1206 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1207 buf = mh_xrealloc (buf, bufsiz);\
1209 last_dst = buf + n;\
1210 bufend = buf + bufsiz;\
1215 * These are versions of similar routines from replsbr.c; the purpose is
1216 * to suppress duplicate addresses from being added to a list when building
1217 * up addresses for the %(formataddr) format function. This is used by
1218 * repl to prevent duplicate addresses from being added to the "to" line.
1219 * See replsbr.c for more information.
1221 * We can't use the functions in replsbr.c directly because they are slightly
1222 * different and depend on the rest of replsbr.c
1225 test_formataddr (char *orig
, char *str
)
1233 struct mailname
*mp
= NULL
;
1235 /* if we don't have a buffer yet, get one */
1237 buf
= mh_xmalloc (BUFINCR
);
1238 last_dst
= buf
; /* XXX */
1239 bufsiz
= BUFINCR
- 6; /* leave some slop */
1240 bufend
= buf
+ bufsiz
;
1243 * If "orig" points to our buffer we can just pick up where we
1244 * left off. Otherwise we have to copy orig into our buffer.
1248 else if (!orig
|| !*orig
) {
1252 dst
= last_dst
; /* XXX */
1257 /* concatenate all the new addresses onto 'buf' */
1258 for (isgroup
= false; (cp
= getname (str
)); ) {
1259 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1260 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1263 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1268 /* if we get here we're going to add an address */
1274 CHECKMEM (mp
->m_gname
);
1278 sp
= adrformat (mp
);
1294 * The companion to test_formataddr(); it behaves the same way, except doesn't
1295 * do duplicate address detection.
1298 test_concataddr(char *orig
, char *str
)
1303 cp
= test_formataddr(orig
, str
);
1309 insert (struct mailname
*np
)
1311 struct mailname
*mp
;
1316 if (np
->m_mbox
== NULL
)
1319 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1320 if (!strcasecmp (FENDNULL(np
->m_host
),
1321 FENDNULL(mp
->m_next
->m_host
)) &&
1322 !strcasecmp (FENDNULL(np
->m_mbox
),
1323 FENDNULL(mp
->m_next
->m_mbox
)))
1326 if (!ccme
&& ismymbox (np
))
1335 * Reset our duplicate address list
1341 struct mailname
*mp
, *mp2
;
1343 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {