]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
bec1b45b3f494bd5541cd0037d202674bdd8c318
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/getfolder.h"
10 #include "sbr/folder_read.h"
11 #include "sbr/folder_free.h"
12 #include "sbr/context_save.h"
13 #include "sbr/context_replace.h"
14 #include "sbr/context_find.h"
15 #include "sbr/brkstring.h"
16 #include "sbr/ambigsw.h"
18 #include "sbr/print_version.h"
19 #include "sbr/print_help.h"
20 #include "sbr/seq_getnum.h"
21 #include "sbr/error.h"
22 #include "h/fmt_scan.h"
23 #include "h/fmt_compile.h"
25 #include "h/scansbr.h"
26 #include "h/addrsbr.h"
28 #include "sbr/m_maildir.h"
29 #include "sbr/terminal.h"
31 #define FMTTEST_SWITCHES \
32 X("form formatfile", 0, FORMSW) \
33 X("format string", 5, FMTSW) \
34 X("address", 0, ADDRSW) \
36 X("date", 0, DATESW) \
37 X("message", 0, MESSAGESW) \
38 X("file", 0, FILESW) \
39 X("nofile", 0, NFILESW) \
40 X("-component-name component-text", 0, OTHERSW) \
41 X("dupaddrs", 0, DUPADDRSW) \
42 X("nodupaddrs", 0, NDUPADDRSW) \
43 X("ccme", 0, CCMESW) \
44 X("noccme", 0, NCCMESW) \
45 X("outsize size-in-characters", 0, OUTSIZESW) \
46 X("width column-width", 0, WIDTHSW) \
47 X("msgnum number", 0, MSGNUMSW) \
48 X("msgcur flag", 0, MSGCURSW) \
49 X("msgsize size", 0, MSGSIZESW) \
50 X("unseen flag", 0, UNSEENSW) \
51 X("dump", 0, DUMPSW) \
52 X("nodump", 0, NDUMPSW) \
53 X("trace", 0, TRACESW) \
54 X("notrace", 0, NTRACESW) \
55 X("version", 0, VERSIONSW) \
56 X("help", 0, HELPSW) \
58 #define X(sw, minchars, id) id,
59 DEFINE_SWITCH_ENUM(FMTTEST
);
62 #define X(sw, minchars, id) { sw, minchars, id },
63 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
67 * An array containing labels used for branch instructions
70 static struct format
**lvec
= NULL
;
72 static int lallocated
= 0;
74 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
75 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
76 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
79 * Context structure used by the tracing routines
82 struct trace_context
{
91 static void fmt_dump (char *, struct format
*);
92 static void dumpone(struct format
*);
93 static void initlabels(struct format
*);
94 static int findlabel(struct format
*);
95 static void assignlabel(struct format
*);
96 static char *f_typestr(int);
97 static char *c_typestr(int);
98 static char *c_flagsstr(int);
99 static void litputs(const char *);
100 static void litputc(char);
101 static void process_addresses(struct format
*, struct msgs_array
*,
102 charstring_t
, int, int *,
103 struct fmt_callbacks
*);
104 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
105 int, int *, struct fmt_callbacks
*);
106 static void process_messages(struct format
*, struct msgs_array
*,
107 struct msgs_array
*, charstring_t
, char *, int,
108 int, int *, struct fmt_callbacks
*);
109 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
110 struct format
*, charstring_t
, int,
111 struct fmt_callbacks
*);
112 static void test_trace(void *, struct format
*, int, char *, const char *);
113 static char *test_formataddr(char *, char *);
114 static char *test_concataddr(char *, char *);
115 static int insert(struct mailname
*);
116 static void mlistfree(void);
118 static bool nodupcheck
; /* If set, no check for duplicates */
119 static bool ccme
; /* Should I cc myself? */
120 static struct mailname mq
; /* Mail addresses to check for duplicates */
121 static char *dummy
= "dummy";
124 main (int argc
, char **argv
)
126 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
127 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
131 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
135 bool dupaddrs
= true;
138 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
139 enum mode_t mode
= MESSAGE
;
141 struct fmt_callbacks cb
, *cbp
= NULL
;
143 if (nmh_init(argv
[0], true, true)) { return 1; }
145 arguments
= getarguments (invo_name
, argc
, argv
, 1);
148 while ((cp
= *argp
++)) {
151 * A -- means that we have a component name (like pick);
152 * save the component name and the next argument for the text.
156 die("missing component name after --");
157 app_msgarg(&compargs
, cp
);
158 /* Grab next argument for component text */
160 die("missing argument to %s", argp
[-2]);
161 app_msgarg(&compargs
, cp
);
164 switch (smatch (cp
, switches
)) {
166 ambigsw (cp
, switches
);
169 die("-%s unknown", cp
);
172 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
173 print_help (buf
, switches
, 1);
176 print_version(invo_name
);
179 die("internal argument error!");
183 if (!(cp
= *argp
++) || *cp
== '-')
184 die("missing argument to %s", argp
[-2]);
185 if (strcmp(cp
, "max") == 0)
186 outputsize
= INT_MAX
;
187 else if (strcmp(cp
, "width") == 0)
188 outputsize
= sc_width();
190 outputsize
= atoi(cp
);
194 if (!(form
= *argp
++) || *form
== '-')
195 die("missing argument to %s", argp
[-2]);
199 if (!(format
= *argp
++) || *format
== '-')
200 die("missing argument to %s", argp
[-2]);
213 defformat
= DEFADDRFORMAT
;
225 defformat
= DEFDATEFORMAT
;
250 if (!(cp
= *argp
++) || *cp
== '-')
251 die("missing argument to %s", argp
[-2]);
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
260 if (!(cp
= *argp
++) || *cp
== '-')
261 die("missing argument to %s", argp
[-2]);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 die("missing argument to %s", argp
[-2]);
270 if (!(cp
= *argp
++) || *cp
== '-')
271 die("missing argument to %s", argp
[-2]);
272 msgunseen
= atoi(cp
);
286 * Only interpret as a folder if we're in message mode
289 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
291 die("only one folder at a time!");
292 folder
= pluspath (cp
);
294 app_msgarg(&msgs
, cp
);
298 * Here's our weird heuristic:
300 * - We allow -dump without any other arguments.
301 * - If you've given any component arguments, we don't require any
303 * - The arguments are interpreted as folders/messages _if_ we're in
304 * message mode, otherwise pass as strings in the text component.
307 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
308 die("usage: [switches] [+folder] msgs | strings...");
312 * If you're picking "raw" as a mode, then you have to select
316 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
317 die("You must specify a format with -form or -format when "
322 * Get new format string. Must be before chdir().
324 nfs
= new_fs (form
, format
, defformat
);
325 (void) fmt_compile(nfs
, &fmt
, 1);
331 if (compargs
.size
== 0 && msgs
.size
== 0)
336 buffer
= charstring_create(BUFSIZ
);
338 if (outputsize
== 0) {
340 outputsize
= sc_width();
342 outputsize
= INT_MAX
;
348 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
352 * If we want to provide our own formataddr, concactaddr, or tracing
353 * callback, do that now. Also, prime ismymbox if we use it.
356 if (!dupaddrs
|| trace
) {
361 cb
.formataddr
= test_formataddr
;
362 cb
.concataddr
= test_concataddr
;
368 struct trace_context
*ctx
;
373 ctx
->outbuf
= mh_xstrdup("");
375 cb
.trace_func
= test_trace
;
376 cb
.trace_context
= ctx
;
380 if (mode
== MESSAGE
) {
381 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
385 for (i
= 0; i
< compargs
.size
; i
+= 2) {
386 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
388 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
392 if (mode
== ADDRESS
) {
393 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
394 } else /* Fall-through for RAW or DATE */
395 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
398 charstring_free(buffer
);
406 * Process each address with fmt_scan().
412 struct pqpair
*pq_next
;
416 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
417 charstring_t buffer
, int outwidth
, int *dat
,
418 struct fmt_callbacks
*cb
)
421 char *cp
, error
[BUFSIZ
];
423 struct pqpair
*p
, *q
;
436 for (i
= 0; i
< addrs
->size
; i
++) {
437 (q
= &pq
)->pq_next
= NULL
;
438 while ((cp
= getname(addrs
->msgs
[i
]))) {
440 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
441 p
->pq_text
= mh_xstrdup(cp
);
442 p
->pq_error
= mh_xstrdup(error
);
444 p
->pq_text
= getcpy(mp
->m_text
);
447 q
= (q
->pq_next
= p
);
450 for (p
= pq
.pq_next
; p
; p
= q
) {
451 c
= fmt_findcomp("text");
454 c
->c_text
= p
->pq_text
;
457 c
= fmt_findcomp("error");
460 c
->c_text
= p
->pq_error
;
464 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
465 fputs(charstring_buffer(buffer
), stdout
);
477 * Process messages and run them through the format engine. A lot taken
482 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
483 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
484 int outwidth
, int files
, int *dat
,
485 struct fmt_callbacks
*cb
)
487 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
488 int num_unseen_seq
= 0;
489 ivector_t seqnum
= ivector_create (0);
495 * If 'files' is set, short-circuit everything else and just process
500 for (i
= 0; i
< msgs
->size
; i
++) {
501 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
502 admonish(cp
, "unable to open file");
505 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
513 folder
= getfolder(1);
515 maildir
= m_maildir(folder
);
517 if (chdir(maildir
) < 0)
518 adios(maildir
, "unable to change directory to");
520 if (!(mp
= folder_read(folder
, 1)))
521 die("unable to read folder %s", folder
);
524 die("no messages in %s", folder
);
526 for (i
= 0; i
< msgs
->size
; i
++)
527 if (!m_convert(mp
, msgs
->msgs
[i
]))
529 seq_setprev(mp
); /* set the Previous-Sequence */
531 context_replace(pfolder
, folder
); /* update current folder */
532 seq_save(mp
); /* synchronize message sequences */
533 context_save(); /* save the context file */
536 * We want to set the unseen flag if requested, so we have to check
537 * the unseen sequence as well.
541 if ((cp
= context_find(usequence
)) && *cp
) {
545 ap
= brkstring(dp
, " ", "\n");
546 for (i
= 0; ap
&& *ap
; i
++, ap
++)
547 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
554 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
555 if (is_selected(mp
, msgnum
)) {
556 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
557 admonish(cp
, "unable to open message");
567 dat
[1] = msgnum
== mp
->curmsg
;
570 * Check to see if this is in the unseen sequence
574 for (i
= 0; i
< num_unseen_seq
; i
++) {
575 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
582 * Read in the message and process the components
585 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
590 ivector_free (seqnum
);
595 * Process a single file in message mode
599 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
600 struct format
*fmt
, charstring_t buffer
, int outwidth
,
601 struct fmt_callbacks
*cb
)
604 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
605 m_getfld_state_t gstate
;
610 * Get our size if we didn't include one
616 if (fstat(fileno(in
), &st
) < 0)
623 * Initialize everything else
634 * Read in the message and process the components
637 gstate
= m_getfld_state_init(in
);
639 bufsz
= sizeof(rbuf
);
640 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
644 i
= fmt_addcomptext(name
, rbuf
);
646 while (state
== FLDPLUS
) {
647 bufsz
= sizeof(rbuf
);
648 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
649 fmt_appendcomp(i
, name
, rbuf
);
653 while (state
== FLDPLUS
) {
654 bufsz
= sizeof(rbuf
);
655 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
660 if (fmt_findcomp("body")) {
661 if ((i
= strlen(rbuf
)) < outwidth
) {
662 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
663 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
666 fmt_addcomptext("body", rbuf
);
676 m_getfld_state_destroy(&gstate
);
679 * Do this now to override any components in the original message
682 for (i
= 0; i
< comps
->size
; i
+= 2) {
683 c
= fmt_findcomp(comps
->msgs
[i
]);
686 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
690 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
691 fputs(charstring_buffer (buffer
), stdout
);
696 * Run text through the format engine with no special processing
700 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
701 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
715 c
= fmt_findcomp("text");
717 for (i
= 0; i
< text
->size
; i
++) {
720 c
->c_text
= getcpy(text
->msgs
[i
]);
723 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
724 fputs(charstring_buffer (buffer
), stdout
);
730 * Our basic tracing support callback.
732 * Print out each instruction as it's executed, including the values of
733 * the num and str registers if they've changed.
737 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
740 struct trace_context
*ctx
= (struct trace_context
*) context
;
741 bool changed
= false;
745 if (num
!= ctx
->num
) {
746 printf("num=%d", num
);
751 if (str
!= ctx
->str
) {
755 fputs("str=", stdout
);
763 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
764 fputs("outbuf=", stdout
);
768 ctx
->outbuf
= mh_xstrdup(outbuf
);
773 fmt_dump (char *nfs
, struct format
*fmth
)
777 printf("Instruction dump of format string: \n%s\n", nfs
);
780 for (fmt
= fmth
; fmt
; ++fmt
) {
782 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
788 dumpone(struct format
*fmt
)
792 if ((i
= findlabel(fmt
)) >= 0)
796 fputs(f_typestr((int)fmt
->f_type
), stdout
);
798 switch (fmt
->f_type
) {
804 fputs(", comp ", stdout
);
805 litputs(fmt
->f_comp
->c_name
);
806 if (fmt
->f_comp
->c_type
)
807 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
808 if (fmt
->f_comp
->c_flags
)
809 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
836 fputs(", c_name ", stdout
);
837 litputs(fmt
->f_comp
->c_name
);
838 if (fmt
->f_comp
->c_type
)
839 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
840 if (fmt
->f_comp
->c_flags
)
841 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
860 fputs(", c_name ", stdout
);
861 litputs(fmt
->f_comp
->c_name
);
862 if (fmt
->f_comp
->c_type
)
863 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
864 if (fmt
->f_comp
->c_flags
)
865 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
869 printf(", width %d, fill '", fmt
->f_width
);
870 litputc(fmt
->f_fill
);
871 fputs("' name ", stdout
);
872 litputs(fmt
->f_comp
->c_name
);
873 if (fmt
->f_comp
->c_type
)
874 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
875 if (fmt
->f_comp
->c_flags
)
876 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
881 printf(", width %d, fill '", fmt
->f_width
);
882 litputc(fmt
->f_fill
);
888 litputs(fmt
->f_text
);
892 printf(", width %d, fill '", fmt
->f_width
);
893 litputc(fmt
->f_fill
);
895 litputs(fmt
->f_text
);
901 litputc(fmt
->f_char
);
910 fputs(" continue else goto", stdout
);
913 i
= findlabel(fmt
+ fmt
->f_skip
);
920 i
= findlabel(fmt
+ fmt
->f_skip
);
921 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
930 case FT_LV_MULTIPLY_L
:
933 printf(" value %d", fmt
->f_value
);
937 fputs(" str ", stdout
);
938 litputs(fmt
->f_text
);
942 fputs(" getenv ", stdout
);
943 litputs(fmt
->f_text
);
946 case FT_LS_DECODECOMP
:
947 fputs(", comp ", stdout
);
948 litputs(fmt
->f_comp
->c_name
);
955 printf(", width %d", fmt
->f_width
);
959 printf(", value dat[%d]", fmt
->f_value
);
966 * Iterate over all instructions and assign labels to the targets of
971 initlabels(struct format
*fmth
)
973 struct format
*fmt
, *addr
;
977 for (fmt
= fmth
; fmt
; ++fmt
) {
987 addr
= fmt
+ fmt
->f_skip
;
988 if (findlabel(addr
) < 0)
991 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
998 findlabel(struct format
*addr
)
1002 for (i
= 0; i
< lused
; ++i
)
1003 if (addr
== lvec
[i
])
1009 assignlabel(struct format
*addr
)
1011 if (lused
>= lallocated
) {
1013 lvec
= (struct format
**)
1014 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1017 lvec
[lused
++] = addr
;
1023 static char buf
[32];
1026 case FT_COMP
: return "COMP";
1027 case FT_COMPF
: return "COMPF";
1028 case FT_LIT
: return "LIT";
1029 case FT_LITF
: return "LITF";
1030 case FT_CHAR
: return "CHAR";
1031 case FT_NUM
: return "NUM";
1032 case FT_NUMF
: return "NUMF";
1033 case FT_STR
: return "STR";
1034 case FT_STRF
: return "STRF";
1035 case FT_STRFW
: return "STRFW";
1036 case FT_STRLIT
: return "STRLIT";
1037 case FT_STRLITZ
: return "STRLITZ";
1038 case FT_PUTADDR
: return "PUTADDR";
1039 case FT_LS_COMP
: return "LS_COMP";
1040 case FT_LS_LIT
: return "LS_LIT";
1041 case FT_LS_GETENV
: return "LS_GETENV";
1042 case FT_LS_CFIND
: return "LS_CFIND";
1043 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1044 case FT_LS_DECODE
: return "LS_DECODE";
1045 case FT_LS_TRIM
: return "LS_TRIM";
1046 case FT_LV_COMP
: return "LV_COMP";
1047 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1048 case FT_LV_LIT
: return "LV_LIT";
1049 case FT_LV_DAT
: return "LV_DAT";
1050 case FT_LV_STRLEN
: return "LV_STRLEN";
1051 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1052 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1053 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1054 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1055 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1056 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1057 case FT_LS_MONTH
: return "LS_MONTH";
1058 case FT_LS_LMONTH
: return "LS_LMONTH";
1059 case FT_LS_ZONE
: return "LS_ZONE";
1060 case FT_LS_DAY
: return "LS_DAY";
1061 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1062 case FT_LS_822DATE
: return "LS_822DATE";
1063 case FT_LS_PRETTY
: return "LS_PRETTY";
1064 case FT_LS_KILO
: return "LS_KILO";
1065 case FT_LS_KIBI
: return "LS_KIBI";
1066 case FT_LV_SEC
: return "LV_SEC";
1067 case FT_LV_MIN
: return "LV_MIN";
1068 case FT_LV_HOUR
: return "LV_HOUR";
1069 case FT_LV_MDAY
: return "LV_MDAY";
1070 case FT_LV_MON
: return "LV_MON";
1071 case FT_LV_YEAR
: return "LV_YEAR";
1072 case FT_LV_YDAY
: return "LV_YDAY";
1073 case FT_LV_WDAY
: return "LV_WDAY";
1074 case FT_LV_ZONE
: return "LV_ZONE";
1075 case FT_LV_CLOCK
: return "LV_CLOCK";
1076 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1077 case FT_LV_DAYF
: return "LV_DAYF";
1078 case FT_LV_DST
: return "LV_DST";
1079 case FT_LV_ZONEF
: return "LV_ZONEF";
1080 case FT_LS_PERS
: return "LS_PERS";
1081 case FT_LS_MBOX
: return "LS_MBOX";
1082 case FT_LS_HOST
: return "LS_HOST";
1083 case FT_LS_PATH
: return "LS_PATH";
1084 case FT_LS_GNAME
: return "LS_GNAME";
1085 case FT_LS_NOTE
: return "LS_NOTE";
1086 case FT_LS_ADDR
: return "LS_ADDR";
1087 case FT_LS_822ADDR
: return "LS_822ADDR";
1088 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1089 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1090 case FT_LV_INGRPF
: return "LV_INGRPF";
1091 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1092 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1093 case FT_LOCALDATE
: return "LOCALDATE";
1094 case FT_GMTDATE
: return "GMTDATE";
1095 case FT_PARSEDATE
: return "PARSEDATE";
1096 case FT_PARSEADDR
: return "PARSEADDR";
1097 case FT_FORMATADDR
: return "FORMATADDR";
1098 case FT_CONCATADDR
: return "CONCATADDR";
1099 case FT_MYMBOX
: return "MYMBOX";
1100 case FT_GETMYMBOX
: return "GETMYMBOX";
1101 case FT_GETMYADDR
: return "GETMYADDR";
1102 case FT_SAVESTR
: return "SAVESTR";
1103 case FT_DONE
: return "DONE";
1104 case FT_PAUSE
: return "PAUSE";
1105 case FT_NOP
: return "NOP";
1106 case FT_GOTO
: return "GOTO";
1107 case FT_IF_S_NULL
: return "IF_S_NULL";
1108 case FT_IF_S
: return "IF_S";
1109 case FT_IF_V_EQ
: return "IF_V_EQ";
1110 case FT_IF_V_NE
: return "IF_V_NE";
1111 case FT_IF_V_GT
: return "IF_V_GT";
1112 case FT_IF_MATCH
: return "IF_MATCH";
1113 case FT_IF_AMATCH
: return "IF_AMATCH";
1114 case FT_S_NULL
: return "S_NULL";
1115 case FT_S_NONNULL
: return "S_NONNULL";
1116 case FT_V_EQ
: return "V_EQ";
1117 case FT_V_NE
: return "V_NE";
1118 case FT_V_GT
: return "V_GT";
1119 case FT_V_MATCH
: return "V_MATCH";
1120 case FT_V_AMATCH
: return "V_AMATCH";
1122 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1130 static char buf
[64];
1132 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1139 static char buf
[64];
1141 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1146 litputs(const char *s
)
1154 fputs("<nil>", stdout
);
1161 printf("\\x%02x", (unsigned char) c
);
1162 } else if (c
< 0x20 || c
== 0177) {
1166 } else if (c
== '\f') {
1169 } else if (c
== '\n') {
1172 } else if (c
== '\r') {
1175 } else if (c
== '\t') {
1180 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1187 * Routines/code to support the duplicate address suppression code, adapted
1191 static char *buf
; /* our current working buffer */
1192 static char *bufend
; /* end of working buffer */
1193 static char *last_dst
; /* buf ptr at end of last call */
1194 static unsigned int bufsiz
=0; /* current size of buf */
1196 #define BUFINCR 512 /* how much to expand buf when if fills */
1198 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1201 * check if there's enough room in buf for str.
1202 * add more mem if needed
1204 #define CHECKMEM(str) \
1205 if ((len = strlen (str)) >= bufend - dst) {\
1207 int n = last_dst - buf;\
1208 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1209 buf = mh_xrealloc (buf, bufsiz);\
1211 last_dst = buf + n;\
1212 bufend = buf + bufsiz;\
1217 * These are versions of similar routines from replsbr.c; the purpose is
1218 * to suppress duplicate addresses from being added to a list when building
1219 * up addresses for the %(formataddr) format function. This is used by
1220 * repl to prevent duplicate addresses from being added to the "to" line.
1221 * See replsbr.c for more information.
1223 * We can't use the functions in replsbr.c directly because they are slightly
1224 * different and depend on the rest of replsbr.c
1227 test_formataddr (char *orig
, char *str
)
1235 struct mailname
*mp
= NULL
;
1237 /* if we don't have a buffer yet, get one */
1239 buf
= mh_xmalloc (BUFINCR
);
1240 last_dst
= buf
; /* XXX */
1241 bufsiz
= BUFINCR
- 6; /* leave some slop */
1242 bufend
= buf
+ bufsiz
;
1245 * If "orig" points to our buffer we can just pick up where we
1246 * left off. Otherwise we have to copy orig into our buffer.
1250 else if (!orig
|| !*orig
) {
1254 dst
= last_dst
; /* XXX */
1259 /* concatenate all the new addresses onto 'buf' */
1260 for (isgroup
= false; (cp
= getname (str
)); ) {
1261 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1262 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1265 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1270 /* if we get here we're going to add an address */
1276 CHECKMEM (mp
->m_gname
);
1280 sp
= adrformat (mp
);
1296 * The companion to test_formataddr(); it behaves the same way, except doesn't
1297 * do duplicate address detection.
1300 test_concataddr(char *orig
, char *str
)
1305 cp
= test_formataddr(orig
, str
);
1311 insert (struct mailname
*np
)
1313 struct mailname
*mp
;
1318 if (np
->m_mbox
== NULL
)
1321 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1322 if (!strcasecmp (FENDNULL(np
->m_host
),
1323 FENDNULL(mp
->m_next
->m_host
)) &&
1324 !strcasecmp (FENDNULL(np
->m_mbox
),
1325 FENDNULL(mp
->m_next
->m_mbox
)))
1328 if (!ccme
&& ismymbox (np
))
1337 * Reset our duplicate address list
1343 struct mailname
*mp
, *mp2
;
1345 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {