]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
21e6de68cb51d0570ec758f689d037b4c182cb9e
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/getarguments.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_save.h"
12 #include "sbr/smatch.h"
13 #include "sbr/snprintb.h"
14 #include "sbr/getcpy.h"
15 #include "sbr/m_convert.h"
16 #include "sbr/getfolder.h"
17 #include "sbr/folder_read.h"
18 #include "sbr/folder_free.h"
19 #include "sbr/context_save.h"
20 #include "sbr/context_replace.h"
21 #include "sbr/context_find.h"
22 #include "sbr/brkstring.h"
23 #include "sbr/ambigsw.h"
25 #include "sbr/print_version.h"
26 #include "sbr/print_help.h"
27 #include "sbr/seq_getnum.h"
28 #include "sbr/error.h"
29 #include "h/fmt_scan.h"
30 #include "h/fmt_compile.h"
32 #include "h/scansbr.h"
33 #include "h/addrsbr.h"
35 #include "sbr/m_maildir.h"
36 #include "sbr/terminal.h"
38 #define FMTTEST_SWITCHES \
39 X("form formatfile", 0, FORMSW) \
40 X("format string", 5, FMTSW) \
41 X("address", 0, ADDRSW) \
43 X("date", 0, DATESW) \
44 X("message", 0, MESSAGESW) \
45 X("file", 0, FILESW) \
46 X("nofile", 0, NFILESW) \
47 X("-component-name component-text", 0, OTHERSW) \
48 X("dupaddrs", 0, DUPADDRSW) \
49 X("nodupaddrs", 0, NDUPADDRSW) \
50 X("ccme", 0, CCMESW) \
51 X("noccme", 0, NCCMESW) \
52 X("outsize size-in-characters", 0, OUTSIZESW) \
53 X("width column-width", 0, WIDTHSW) \
54 X("msgnum number", 0, MSGNUMSW) \
55 X("msgcur flag", 0, MSGCURSW) \
56 X("msgsize size", 0, MSGSIZESW) \
57 X("unseen flag", 0, UNSEENSW) \
58 X("dump", 0, DUMPSW) \
59 X("nodump", 0, NDUMPSW) \
60 X("trace", 0, TRACESW) \
61 X("notrace", 0, NTRACESW) \
62 X("version", 0, VERSIONSW) \
63 X("help", 0, HELPSW) \
65 #define X(sw, minchars, id) id,
66 DEFINE_SWITCH_ENUM(FMTTEST
);
69 #define X(sw, minchars, id) { sw, minchars, id },
70 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
74 * An array containing labels used for branch instructions
77 static struct format
**lvec
= NULL
;
79 static int lallocated
= 0;
81 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
82 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
83 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
86 * Context structure used by the tracing routines
89 struct trace_context
{
98 static void fmt_dump (char *, struct format
*);
99 static void dumpone(struct format
*);
100 static void initlabels(struct format
*);
101 static int findlabel(struct format
*);
102 static void assignlabel(struct format
*);
103 static char *f_typestr(int);
104 static char *c_typestr(int);
105 static char *c_flagsstr(int);
106 static void litputs(const char *);
107 static void litputc(char);
108 static void process_addresses(struct format
*, struct msgs_array
*,
109 charstring_t
, int, int *,
110 struct fmt_callbacks
*);
111 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
112 int, int *, struct fmt_callbacks
*);
113 static void process_messages(struct format
*, struct msgs_array
*,
114 struct msgs_array
*, charstring_t
, char *, int,
115 int, int *, struct fmt_callbacks
*);
116 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
117 struct format
*, charstring_t
, int,
118 struct fmt_callbacks
*);
119 static void test_trace(void *, struct format
*, int, char *, const char *);
120 static char *test_formataddr(char *, char *);
121 static char *test_concataddr(char *, char *);
122 static int insert(struct mailname
*);
123 static void mlistfree(void);
125 static bool nodupcheck
; /* If set, no check for duplicates */
126 static bool ccme
; /* Should I cc myself? */
127 static struct mailname mq
; /* Mail addresses to check for duplicates */
128 static char *dummy
= "dummy";
131 main (int argc
, char **argv
)
133 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
134 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
138 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
142 bool dupaddrs
= true;
145 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
146 enum mode_t mode
= MESSAGE
;
148 struct fmt_callbacks cb
, *cbp
= NULL
;
150 if (nmh_init(argv
[0], true, true)) { return 1; }
152 arguments
= getarguments (invo_name
, argc
, argv
, 1);
155 while ((cp
= *argp
++)) {
158 * A -- means that we have a component name (like pick);
159 * save the component name and the next argument for the text.
163 die("missing component name after --");
164 app_msgarg(&compargs
, cp
);
165 /* Grab next argument for component text */
167 die("missing argument to %s", argp
[-2]);
168 app_msgarg(&compargs
, cp
);
171 switch (smatch (cp
, switches
)) {
173 ambigsw (cp
, switches
);
176 die("-%s unknown", cp
);
179 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
180 print_help (buf
, switches
, 1);
183 print_version(invo_name
);
186 die("internal argument error!");
190 if (!(cp
= *argp
++) || *cp
== '-')
191 die("missing argument to %s", argp
[-2]);
192 if (strcmp(cp
, "max") == 0)
193 outputsize
= INT_MAX
;
194 else if (strcmp(cp
, "width") == 0)
195 outputsize
= sc_width();
197 outputsize
= atoi(cp
);
201 if (!(form
= *argp
++) || *form
== '-')
202 die("missing argument to %s", argp
[-2]);
206 if (!(format
= *argp
++) || *format
== '-')
207 die("missing argument to %s", argp
[-2]);
220 defformat
= DEFADDRFORMAT
;
232 defformat
= DEFDATEFORMAT
;
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]);
267 if (!(cp
= *argp
++) || *cp
== '-')
268 die("missing argument to %s", argp
[-2]);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 die("missing argument to %s", argp
[-2]);
277 if (!(cp
= *argp
++) || *cp
== '-')
278 die("missing argument to %s", argp
[-2]);
279 msgunseen
= atoi(cp
);
293 * Only interpret as a folder if we're in message mode
296 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
298 die("only one folder at a time!");
299 folder
= pluspath (cp
);
301 app_msgarg(&msgs
, cp
);
305 * Here's our weird heuristic:
307 * - We allow -dump without any other arguments.
308 * - If you've given any component arguments, we don't require any
310 * - The arguments are interpreted as folders/messages _if_ we're in
311 * message mode, otherwise pass as strings in the text component.
314 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
315 die("usage: [switches] [+folder] msgs | strings...");
319 * If you're picking "raw" as a mode, then you have to select
323 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
324 die("You must specify a format with -form or -format when "
329 * Get new format string. Must be before chdir().
331 nfs
= new_fs (form
, format
, defformat
);
332 (void) fmt_compile(nfs
, &fmt
, 1);
338 if (compargs
.size
== 0 && msgs
.size
== 0)
343 buffer
= charstring_create(BUFSIZ
);
345 if (outputsize
== 0) {
347 outputsize
= sc_width();
349 outputsize
= INT_MAX
;
355 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
359 * If we want to provide our own formataddr, concactaddr, or tracing
360 * callback, do that now. Also, prime ismymbox if we use it.
363 if (!dupaddrs
|| trace
) {
368 cb
.formataddr
= test_formataddr
;
369 cb
.concataddr
= test_concataddr
;
375 struct trace_context
*ctx
;
380 ctx
->outbuf
= mh_xstrdup("");
382 cb
.trace_func
= test_trace
;
383 cb
.trace_context
= ctx
;
387 if (mode
== MESSAGE
) {
388 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
392 for (i
= 0; i
< compargs
.size
; i
+= 2) {
393 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
395 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
399 if (mode
== ADDRESS
) {
400 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
401 } else /* Fall-through for RAW or DATE */
402 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
405 charstring_free(buffer
);
413 * Process each address with fmt_scan().
419 struct pqpair
*pq_next
;
423 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
424 charstring_t buffer
, int outwidth
, int *dat
,
425 struct fmt_callbacks
*cb
)
428 char *cp
, error
[BUFSIZ
];
430 struct pqpair
*p
, *q
;
443 for (i
= 0; i
< addrs
->size
; i
++) {
444 (q
= &pq
)->pq_next
= NULL
;
445 while ((cp
= getname(addrs
->msgs
[i
]))) {
447 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
448 p
->pq_text
= mh_xstrdup(cp
);
449 p
->pq_error
= mh_xstrdup(error
);
451 p
->pq_text
= getcpy(mp
->m_text
);
454 q
= (q
->pq_next
= p
);
457 for (p
= pq
.pq_next
; p
; p
= q
) {
458 c
= fmt_findcomp("text");
461 c
->c_text
= p
->pq_text
;
464 c
= fmt_findcomp("error");
467 c
->c_text
= p
->pq_error
;
471 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
472 fputs(charstring_buffer(buffer
), stdout
);
484 * Process messages and run them through the format engine. A lot taken
489 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
490 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
491 int outwidth
, int files
, int *dat
,
492 struct fmt_callbacks
*cb
)
494 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
495 int num_unseen_seq
= 0;
496 ivector_t seqnum
= ivector_create (0);
502 * If 'files' is set, short-circuit everything else and just process
507 for (i
= 0; i
< msgs
->size
; i
++) {
508 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
509 admonish(cp
, "unable to open file");
512 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
520 folder
= getfolder(1);
522 maildir
= m_maildir(folder
);
524 if (chdir(maildir
) < 0)
525 adios(maildir
, "unable to change directory to");
527 if (!(mp
= folder_read(folder
, 1)))
528 die("unable to read folder %s", folder
);
531 die("no messages in %s", folder
);
533 for (i
= 0; i
< msgs
->size
; i
++)
534 if (!m_convert(mp
, msgs
->msgs
[i
]))
536 seq_setprev(mp
); /* set the Previous-Sequence */
538 context_replace(pfolder
, folder
); /* update current folder */
539 seq_save(mp
); /* synchronize message sequences */
540 context_save(); /* save the context file */
543 * We want to set the unseen flag if requested, so we have to check
544 * the unseen sequence as well.
548 if ((cp
= context_find(usequence
)) && *cp
) {
552 ap
= brkstring(dp
, " ", "\n");
553 for (i
= 0; ap
&& *ap
; i
++, ap
++)
554 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
561 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
562 if (is_selected(mp
, msgnum
)) {
563 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
564 admonish(cp
, "unable to open message");
574 dat
[1] = msgnum
== mp
->curmsg
;
577 * Check to see if this is in the unseen sequence
581 for (i
= 0; i
< num_unseen_seq
; i
++) {
582 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
589 * Read in the message and process the components
592 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
597 ivector_free (seqnum
);
602 * Process a single file in message mode
606 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
607 struct format
*fmt
, charstring_t buffer
, int outwidth
,
608 struct fmt_callbacks
*cb
)
611 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
612 m_getfld_state_t gstate
;
617 * Get our size if we didn't include one
623 if (fstat(fileno(in
), &st
) < 0)
630 * Initialize everything else
641 * Read in the message and process the components
644 gstate
= m_getfld_state_init(in
);
646 bufsz
= sizeof(rbuf
);
647 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
651 i
= fmt_addcomptext(name
, rbuf
);
653 while (state
== FLDPLUS
) {
654 bufsz
= sizeof(rbuf
);
655 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
656 fmt_appendcomp(i
, name
, rbuf
);
660 while (state
== FLDPLUS
) {
661 bufsz
= sizeof(rbuf
);
662 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
667 if (fmt_findcomp("body")) {
668 if ((i
= strlen(rbuf
)) < outwidth
) {
669 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
670 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
673 fmt_addcomptext("body", rbuf
);
683 m_getfld_state_destroy(&gstate
);
686 * Do this now to override any components in the original message
689 for (i
= 0; i
< comps
->size
; i
+= 2) {
690 c
= fmt_findcomp(comps
->msgs
[i
]);
693 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
697 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
698 fputs(charstring_buffer (buffer
), stdout
);
703 * Run text through the format engine with no special processing
707 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
708 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
722 c
= fmt_findcomp("text");
724 for (i
= 0; i
< text
->size
; i
++) {
727 c
->c_text
= getcpy(text
->msgs
[i
]);
730 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
731 fputs(charstring_buffer (buffer
), stdout
);
737 * Our basic tracing support callback.
739 * Print out each instruction as it's executed, including the values of
740 * the num and str registers if they've changed.
744 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
747 struct trace_context
*ctx
= (struct trace_context
*) context
;
748 bool changed
= false;
752 if (num
!= ctx
->num
) {
753 printf("num=%d", num
);
758 if (str
!= ctx
->str
) {
762 fputs("str=", stdout
);
770 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
771 fputs("outbuf=", stdout
);
775 ctx
->outbuf
= mh_xstrdup(outbuf
);
780 fmt_dump (char *nfs
, struct format
*fmth
)
784 printf("Instruction dump of format string: \n%s\n", nfs
);
787 for (fmt
= fmth
; fmt
; ++fmt
) {
789 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
795 dumpone(struct format
*fmt
)
799 if ((i
= findlabel(fmt
)) >= 0)
803 fputs(f_typestr((int)fmt
->f_type
), stdout
);
805 switch (fmt
->f_type
) {
811 fputs(", comp ", stdout
);
812 litputs(fmt
->f_comp
->c_name
);
813 if (fmt
->f_comp
->c_type
)
814 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
815 if (fmt
->f_comp
->c_flags
)
816 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
843 fputs(", c_name ", stdout
);
844 litputs(fmt
->f_comp
->c_name
);
845 if (fmt
->f_comp
->c_type
)
846 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
847 if (fmt
->f_comp
->c_flags
)
848 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
867 fputs(", c_name ", stdout
);
868 litputs(fmt
->f_comp
->c_name
);
869 if (fmt
->f_comp
->c_type
)
870 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
871 if (fmt
->f_comp
->c_flags
)
872 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
876 printf(", width %d, fill '", fmt
->f_width
);
877 litputc(fmt
->f_fill
);
878 fputs("' name ", stdout
);
879 litputs(fmt
->f_comp
->c_name
);
880 if (fmt
->f_comp
->c_type
)
881 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
882 if (fmt
->f_comp
->c_flags
)
883 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
888 printf(", width %d, fill '", fmt
->f_width
);
889 litputc(fmt
->f_fill
);
895 litputs(fmt
->f_text
);
899 printf(", width %d, fill '", fmt
->f_width
);
900 litputc(fmt
->f_fill
);
902 litputs(fmt
->f_text
);
908 litputc(fmt
->f_char
);
917 fputs(" continue else goto", stdout
);
920 i
= findlabel(fmt
+ fmt
->f_skip
);
927 i
= findlabel(fmt
+ fmt
->f_skip
);
928 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
937 case FT_LV_MULTIPLY_L
:
940 printf(" value %d", fmt
->f_value
);
944 fputs(" str ", stdout
);
945 litputs(fmt
->f_text
);
949 fputs(" getenv ", stdout
);
950 litputs(fmt
->f_text
);
953 case FT_LS_DECODECOMP
:
954 fputs(", comp ", stdout
);
955 litputs(fmt
->f_comp
->c_name
);
962 printf(", width %d", fmt
->f_width
);
966 printf(", value dat[%d]", fmt
->f_value
);
973 * Iterate over all instructions and assign labels to the targets of
978 initlabels(struct format
*fmth
)
980 struct format
*fmt
, *addr
;
984 for (fmt
= fmth
; fmt
; ++fmt
) {
994 addr
= fmt
+ fmt
->f_skip
;
995 if (findlabel(addr
) < 0)
998 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
1005 findlabel(struct format
*addr
)
1009 for (i
= 0; i
< lused
; ++i
)
1010 if (addr
== lvec
[i
])
1016 assignlabel(struct format
*addr
)
1018 if (lused
>= lallocated
) {
1020 lvec
= (struct format
**)
1021 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1024 lvec
[lused
++] = addr
;
1030 static char buf
[32];
1033 case FT_COMP
: return "COMP";
1034 case FT_COMPF
: return "COMPF";
1035 case FT_LIT
: return "LIT";
1036 case FT_LITF
: return "LITF";
1037 case FT_CHAR
: return "CHAR";
1038 case FT_NUM
: return "NUM";
1039 case FT_NUMF
: return "NUMF";
1040 case FT_STR
: return "STR";
1041 case FT_STRF
: return "STRF";
1042 case FT_STRFW
: return "STRFW";
1043 case FT_STRLIT
: return "STRLIT";
1044 case FT_STRLITZ
: return "STRLITZ";
1045 case FT_PUTADDR
: return "PUTADDR";
1046 case FT_LS_COMP
: return "LS_COMP";
1047 case FT_LS_LIT
: return "LS_LIT";
1048 case FT_LS_GETENV
: return "LS_GETENV";
1049 case FT_LS_CFIND
: return "LS_CFIND";
1050 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1051 case FT_LS_DECODE
: return "LS_DECODE";
1052 case FT_LS_TRIM
: return "LS_TRIM";
1053 case FT_LV_COMP
: return "LV_COMP";
1054 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1055 case FT_LV_LIT
: return "LV_LIT";
1056 case FT_LV_DAT
: return "LV_DAT";
1057 case FT_LV_STRLEN
: return "LV_STRLEN";
1058 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1059 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1060 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1061 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1062 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1063 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1064 case FT_LS_MONTH
: return "LS_MONTH";
1065 case FT_LS_LMONTH
: return "LS_LMONTH";
1066 case FT_LS_ZONE
: return "LS_ZONE";
1067 case FT_LS_DAY
: return "LS_DAY";
1068 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1069 case FT_LS_822DATE
: return "LS_822DATE";
1070 case FT_LS_PRETTY
: return "LS_PRETTY";
1071 case FT_LS_KILO
: return "LS_KILO";
1072 case FT_LS_KIBI
: return "LS_KIBI";
1073 case FT_LV_SEC
: return "LV_SEC";
1074 case FT_LV_MIN
: return "LV_MIN";
1075 case FT_LV_HOUR
: return "LV_HOUR";
1076 case FT_LV_MDAY
: return "LV_MDAY";
1077 case FT_LV_MON
: return "LV_MON";
1078 case FT_LV_YEAR
: return "LV_YEAR";
1079 case FT_LV_YDAY
: return "LV_YDAY";
1080 case FT_LV_WDAY
: return "LV_WDAY";
1081 case FT_LV_ZONE
: return "LV_ZONE";
1082 case FT_LV_CLOCK
: return "LV_CLOCK";
1083 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1084 case FT_LV_DAYF
: return "LV_DAYF";
1085 case FT_LV_DST
: return "LV_DST";
1086 case FT_LV_ZONEF
: return "LV_ZONEF";
1087 case FT_LS_PERS
: return "LS_PERS";
1088 case FT_LS_MBOX
: return "LS_MBOX";
1089 case FT_LS_HOST
: return "LS_HOST";
1090 case FT_LS_PATH
: return "LS_PATH";
1091 case FT_LS_GNAME
: return "LS_GNAME";
1092 case FT_LS_NOTE
: return "LS_NOTE";
1093 case FT_LS_ADDR
: return "LS_ADDR";
1094 case FT_LS_822ADDR
: return "LS_822ADDR";
1095 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1096 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1097 case FT_LV_INGRPF
: return "LV_INGRPF";
1098 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1099 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1100 case FT_LOCALDATE
: return "LOCALDATE";
1101 case FT_GMTDATE
: return "GMTDATE";
1102 case FT_PARSEDATE
: return "PARSEDATE";
1103 case FT_PARSEADDR
: return "PARSEADDR";
1104 case FT_FORMATADDR
: return "FORMATADDR";
1105 case FT_CONCATADDR
: return "CONCATADDR";
1106 case FT_MYMBOX
: return "MYMBOX";
1107 case FT_GETMYMBOX
: return "GETMYMBOX";
1108 case FT_GETMYADDR
: return "GETMYADDR";
1109 case FT_SAVESTR
: return "SAVESTR";
1110 case FT_DONE
: return "DONE";
1111 case FT_PAUSE
: return "PAUSE";
1112 case FT_NOP
: return "NOP";
1113 case FT_GOTO
: return "GOTO";
1114 case FT_IF_S_NULL
: return "IF_S_NULL";
1115 case FT_IF_S
: return "IF_S";
1116 case FT_IF_V_EQ
: return "IF_V_EQ";
1117 case FT_IF_V_NE
: return "IF_V_NE";
1118 case FT_IF_V_GT
: return "IF_V_GT";
1119 case FT_IF_MATCH
: return "IF_MATCH";
1120 case FT_IF_AMATCH
: return "IF_AMATCH";
1121 case FT_S_NULL
: return "S_NULL";
1122 case FT_S_NONNULL
: return "S_NONNULL";
1123 case FT_V_EQ
: return "V_EQ";
1124 case FT_V_NE
: return "V_NE";
1125 case FT_V_GT
: return "V_GT";
1126 case FT_V_MATCH
: return "V_MATCH";
1127 case FT_V_AMATCH
: return "V_AMATCH";
1129 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1137 static char buf
[64];
1139 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1146 static char buf
[64];
1148 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1153 litputs(const char *s
)
1161 fputs("<nil>", stdout
);
1168 printf("\\x%02x", (unsigned char) c
);
1169 } else if (c
< 0x20 || c
== 0177) {
1173 } else if (c
== '\f') {
1176 } else if (c
== '\n') {
1179 } else if (c
== '\r') {
1182 } else if (c
== '\t') {
1187 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1194 * Routines/code to support the duplicate address suppression code, adapted
1198 static char *buf
; /* our current working buffer */
1199 static char *bufend
; /* end of working buffer */
1200 static char *last_dst
; /* buf ptr at end of last call */
1201 static unsigned int bufsiz
=0; /* current size of buf */
1203 #define BUFINCR 512 /* how much to expand buf when if fills */
1205 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1208 * check if there's enough room in buf for str.
1209 * add more mem if needed
1211 #define CHECKMEM(str) \
1212 if ((len = strlen (str)) >= bufend - dst) {\
1214 int n = last_dst - buf;\
1215 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1216 buf = mh_xrealloc (buf, bufsiz);\
1218 last_dst = buf + n;\
1219 bufend = buf + bufsiz;\
1224 * These are versions of similar routines from replsbr.c; the purpose is
1225 * to suppress duplicate addresses from being added to a list when building
1226 * up addresses for the %(formataddr) format function. This is used by
1227 * repl to prevent duplicate addresses from being added to the "to" line.
1228 * See replsbr.c for more information.
1230 * We can't use the functions in replsbr.c directly because they are slightly
1231 * different and depend on the rest of replsbr.c
1234 test_formataddr (char *orig
, char *str
)
1242 struct mailname
*mp
= NULL
;
1244 /* if we don't have a buffer yet, get one */
1246 buf
= mh_xmalloc (BUFINCR
);
1247 last_dst
= buf
; /* XXX */
1248 bufsiz
= BUFINCR
- 6; /* leave some slop */
1249 bufend
= buf
+ bufsiz
;
1252 * If "orig" points to our buffer we can just pick up where we
1253 * left off. Otherwise we have to copy orig into our buffer.
1257 else if (!orig
|| !*orig
) {
1261 dst
= last_dst
; /* XXX */
1266 /* concatenate all the new addresses onto 'buf' */
1267 for (isgroup
= false; (cp
= getname (str
)); ) {
1268 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1269 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1272 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1277 /* if we get here we're going to add an address */
1283 CHECKMEM (mp
->m_gname
);
1287 sp
= adrformat (mp
);
1303 * The companion to test_formataddr(); it behaves the same way, except doesn't
1304 * do duplicate address detection.
1307 test_concataddr(char *orig
, char *str
)
1312 cp
= test_formataddr(orig
, str
);
1318 insert (struct mailname
*np
)
1320 struct mailname
*mp
;
1325 if (np
->m_mbox
== NULL
)
1328 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1329 if (!strcasecmp (FENDNULL(np
->m_host
),
1330 FENDNULL(mp
->m_next
->m_host
)) &&
1331 !strcasecmp (FENDNULL(np
->m_mbox
),
1332 FENDNULL(mp
->m_next
->m_mbox
)))
1335 if (!ccme
&& ismymbox (np
))
1344 * Reset our duplicate address list
1350 struct mailname
*mp
, *mp2
;
1352 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {