]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
782ce5166522b2099bb43f663e2320c407d372de
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/m_getfld.h"
10 #include "sbr/getarguments.h"
11 #include "sbr/seq_setprev.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/smatch.h"
14 #include "sbr/snprintb.h"
15 #include "sbr/getcpy.h"
16 #include "sbr/m_convert.h"
17 #include "sbr/getfolder.h"
18 #include "sbr/folder_read.h"
19 #include "sbr/folder_free.h"
20 #include "sbr/context_save.h"
21 #include "sbr/context_replace.h"
22 #include "sbr/context_find.h"
23 #include "sbr/brkstring.h"
24 #include "sbr/ambigsw.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/seq_getnum.h"
29 #include "sbr/error.h"
30 #include "h/fmt_scan.h"
31 #include "h/fmt_compile.h"
33 #include "h/scansbr.h"
34 #include "h/addrsbr.h"
36 #include "sbr/m_maildir.h"
37 #include "sbr/terminal.h"
39 #define FMTTEST_SWITCHES \
40 X("form formatfile", 0, FORMSW) \
41 X("format string", 5, FMTSW) \
42 X("address", 0, ADDRSW) \
44 X("date", 0, DATESW) \
45 X("message", 0, MESSAGESW) \
46 X("file", 0, FILESW) \
47 X("nofile", 0, NFILESW) \
48 X("-component-name component-text", 0, OTHERSW) \
49 X("dupaddrs", 0, DUPADDRSW) \
50 X("nodupaddrs", 0, NDUPADDRSW) \
51 X("ccme", 0, CCMESW) \
52 X("noccme", 0, NCCMESW) \
53 X("outsize size-in-characters", 0, OUTSIZESW) \
54 X("width column-width", 0, WIDTHSW) \
55 X("msgnum number", 0, MSGNUMSW) \
56 X("msgcur flag", 0, MSGCURSW) \
57 X("msgsize size", 0, MSGSIZESW) \
58 X("unseen flag", 0, UNSEENSW) \
59 X("dump", 0, DUMPSW) \
60 X("nodump", 0, NDUMPSW) \
61 X("trace", 0, TRACESW) \
62 X("notrace", 0, NTRACESW) \
63 X("version", 0, VERSIONSW) \
64 X("help", 0, HELPSW) \
66 #define X(sw, minchars, id) id,
67 DEFINE_SWITCH_ENUM(FMTTEST
);
70 #define X(sw, minchars, id) { sw, minchars, id },
71 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
75 * An array containing labels used for branch instructions
78 static struct format
**lvec
= NULL
;
80 static int lallocated
= 0;
82 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
83 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
84 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
87 * Context structure used by the tracing routines
90 struct trace_context
{
99 static void fmt_dump (char *, struct format
*);
100 static void dumpone(struct format
*);
101 static void initlabels(struct format
*);
102 static int findlabel(struct format
*);
103 static void assignlabel(struct format
*);
104 static char *f_typestr(int);
105 static char *c_typestr(int);
106 static char *c_flagsstr(int);
107 static void litputs(const char *);
108 static void litputc(char);
109 static void process_addresses(struct format
*, struct msgs_array
*,
110 charstring_t
, int, int *,
111 struct fmt_callbacks
*);
112 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
113 int, int *, struct fmt_callbacks
*);
114 static void process_messages(struct format
*, struct msgs_array
*,
115 struct msgs_array
*, charstring_t
, char *, int,
116 int, int *, struct fmt_callbacks
*);
117 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
118 struct format
*, charstring_t
, int,
119 struct fmt_callbacks
*);
120 static void test_trace(void *, struct format
*, int, char *, const char *);
121 static char *test_formataddr(char *, char *);
122 static char *test_concataddr(char *, char *);
123 static int insert(struct mailname
*);
124 static void mlistfree(void);
126 static bool nodupcheck
; /* If set, no check for duplicates */
127 static bool ccme
; /* Should I cc myself? */
128 static struct mailname mq
; /* Mail addresses to check for duplicates */
129 static char *dummy
= "dummy";
132 main (int argc
, char **argv
)
134 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
135 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
139 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
143 bool dupaddrs
= true;
146 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
147 enum mode_t mode
= MESSAGE
;
149 struct fmt_callbacks cb
, *cbp
= NULL
;
151 if (nmh_init(argv
[0], true, true)) { return 1; }
153 arguments
= getarguments (invo_name
, argc
, argv
, 1);
156 while ((cp
= *argp
++)) {
159 * A -- means that we have a component name (like pick);
160 * save the component name and the next argument for the text.
164 die("missing component name after --");
165 app_msgarg(&compargs
, cp
);
166 /* Grab next argument for component text */
168 die("missing argument to %s", argp
[-2]);
169 app_msgarg(&compargs
, cp
);
172 switch (smatch (cp
, switches
)) {
174 ambigsw (cp
, switches
);
177 die("-%s unknown", cp
);
180 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
181 print_help (buf
, switches
, 1);
184 print_version(invo_name
);
187 die("internal argument error!");
191 if (!(cp
= *argp
++) || *cp
== '-')
192 die("missing argument to %s", argp
[-2]);
193 if (strcmp(cp
, "max") == 0)
194 outputsize
= INT_MAX
;
195 else if (strcmp(cp
, "width") == 0)
196 outputsize
= sc_width();
198 outputsize
= atoi(cp
);
202 if (!(form
= *argp
++) || *form
== '-')
203 die("missing argument to %s", argp
[-2]);
207 if (!(format
= *argp
++) || *format
== '-')
208 die("missing argument to %s", argp
[-2]);
221 defformat
= DEFADDRFORMAT
;
233 defformat
= DEFDATEFORMAT
;
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]);
273 if (!(cp
= *argp
++) || *cp
== '-')
274 die("missing argument to %s", argp
[-2]);
278 if (!(cp
= *argp
++) || *cp
== '-')
279 die("missing argument to %s", argp
[-2]);
280 msgunseen
= atoi(cp
);
294 * Only interpret as a folder if we're in message mode
297 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
299 die("only one folder at a time!");
300 folder
= pluspath (cp
);
302 app_msgarg(&msgs
, cp
);
306 * Here's our weird heuristic:
308 * - We allow -dump without any other arguments.
309 * - If you've given any component arguments, we don't require any
311 * - The arguments are interpreted as folders/messages _if_ we're in
312 * message mode, otherwise pass as strings in the text component.
315 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
316 die("usage: [switches] [+folder] msgs | strings...");
320 * If you're picking "raw" as a mode, then you have to select
324 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
325 die("You must specify a format with -form or -format when "
330 * Get new format string. Must be before chdir().
332 nfs
= new_fs (form
, format
, defformat
);
333 (void) fmt_compile(nfs
, &fmt
, 1);
339 if (compargs
.size
== 0 && msgs
.size
== 0)
344 buffer
= charstring_create(BUFSIZ
);
346 if (outputsize
== 0) {
348 outputsize
= sc_width();
350 outputsize
= INT_MAX
;
356 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
360 * If we want to provide our own formataddr, concactaddr, or tracing
361 * callback, do that now. Also, prime ismymbox if we use it.
364 if (!dupaddrs
|| trace
) {
369 cb
.formataddr
= test_formataddr
;
370 cb
.concataddr
= test_concataddr
;
376 struct trace_context
*ctx
;
381 ctx
->outbuf
= mh_xstrdup("");
383 cb
.trace_func
= test_trace
;
384 cb
.trace_context
= ctx
;
388 if (mode
== MESSAGE
) {
389 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
393 for (i
= 0; i
< compargs
.size
; i
+= 2) {
394 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
396 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
400 if (mode
== ADDRESS
) {
401 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
402 } else /* Fall-through for RAW or DATE */
403 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
406 charstring_free(buffer
);
414 * Process each address with fmt_scan().
420 struct pqpair
*pq_next
;
424 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
425 charstring_t buffer
, int outwidth
, int *dat
,
426 struct fmt_callbacks
*cb
)
429 char *cp
, error
[BUFSIZ
];
431 struct pqpair
*p
, *q
;
444 for (i
= 0; i
< addrs
->size
; i
++) {
445 (q
= &pq
)->pq_next
= NULL
;
446 while ((cp
= getname(addrs
->msgs
[i
]))) {
448 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
449 p
->pq_text
= mh_xstrdup(cp
);
450 p
->pq_error
= mh_xstrdup(error
);
452 p
->pq_text
= getcpy(mp
->m_text
);
455 q
= (q
->pq_next
= p
);
458 for (p
= pq
.pq_next
; p
; p
= q
) {
459 c
= fmt_findcomp("text");
462 c
->c_text
= p
->pq_text
;
465 c
= fmt_findcomp("error");
468 c
->c_text
= p
->pq_error
;
472 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
473 fputs(charstring_buffer(buffer
), stdout
);
485 * Process messages and run them through the format engine. A lot taken
490 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
491 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
492 int outwidth
, int files
, int *dat
,
493 struct fmt_callbacks
*cb
)
495 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
496 int num_unseen_seq
= 0;
497 ivector_t seqnum
= ivector_create (0);
503 * If 'files' is set, short-circuit everything else and just process
508 for (i
= 0; i
< msgs
->size
; i
++) {
509 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
510 admonish(cp
, "unable to open file");
513 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
521 folder
= getfolder(1);
523 maildir
= m_maildir(folder
);
525 if (chdir(maildir
) < 0)
526 adios(maildir
, "unable to change directory to");
528 if (!(mp
= folder_read(folder
, 1)))
529 die("unable to read folder %s", folder
);
532 die("no messages in %s", folder
);
534 for (i
= 0; i
< msgs
->size
; i
++)
535 if (!m_convert(mp
, msgs
->msgs
[i
]))
537 seq_setprev(mp
); /* set the Previous-Sequence */
539 context_replace(pfolder
, folder
); /* update current folder */
540 seq_save(mp
); /* synchronize message sequences */
541 context_save(); /* save the context file */
544 * We want to set the unseen flag if requested, so we have to check
545 * the unseen sequence as well.
549 if ((cp
= context_find(usequence
)) && *cp
) {
553 ap
= brkstring(dp
, " ", "\n");
554 for (i
= 0; ap
&& *ap
; i
++, ap
++)
555 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
562 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
563 if (is_selected(mp
, msgnum
)) {
564 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
565 admonish(cp
, "unable to open message");
575 dat
[1] = msgnum
== mp
->curmsg
;
578 * Check to see if this is in the unseen sequence
582 for (i
= 0; i
< num_unseen_seq
; i
++) {
583 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
590 * Read in the message and process the components
593 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
598 ivector_free (seqnum
);
603 * Process a single file in message mode
607 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
608 struct format
*fmt
, charstring_t buffer
, int outwidth
,
609 struct fmt_callbacks
*cb
)
612 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
613 m_getfld_state_t gstate
;
618 * Get our size if we didn't include one
624 if (fstat(fileno(in
), &st
) < 0)
631 * Initialize everything else
642 * Read in the message and process the components
645 gstate
= m_getfld_state_init(in
);
647 bufsz
= sizeof(rbuf
);
648 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
652 i
= fmt_addcomptext(name
, rbuf
);
654 while (state
== FLDPLUS
) {
655 bufsz
= sizeof(rbuf
);
656 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
657 fmt_appendcomp(i
, name
, rbuf
);
661 while (state
== FLDPLUS
) {
662 bufsz
= sizeof(rbuf
);
663 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
668 if (fmt_findcomp("body")) {
669 if ((i
= strlen(rbuf
)) < outwidth
) {
670 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
671 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
674 fmt_addcomptext("body", rbuf
);
684 m_getfld_state_destroy(&gstate
);
687 * Do this now to override any components in the original message
690 for (i
= 0; i
< comps
->size
; i
+= 2) {
691 c
= fmt_findcomp(comps
->msgs
[i
]);
694 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
698 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
699 fputs(charstring_buffer (buffer
), stdout
);
704 * Run text through the format engine with no special processing
708 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
709 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
723 c
= fmt_findcomp("text");
725 for (i
= 0; i
< text
->size
; i
++) {
728 c
->c_text
= getcpy(text
->msgs
[i
]);
731 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
732 fputs(charstring_buffer (buffer
), stdout
);
738 * Our basic tracing support callback.
740 * Print out each instruction as it's executed, including the values of
741 * the num and str registers if they've changed.
745 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
748 struct trace_context
*ctx
= (struct trace_context
*) context
;
749 bool changed
= false;
753 if (num
!= ctx
->num
) {
754 printf("num=%d", num
);
759 if (str
!= ctx
->str
) {
763 fputs("str=", stdout
);
771 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
772 fputs("outbuf=", stdout
);
776 ctx
->outbuf
= mh_xstrdup(outbuf
);
781 fmt_dump (char *nfs
, struct format
*fmth
)
785 printf("Instruction dump of format string: \n%s\n", nfs
);
788 for (fmt
= fmth
; fmt
; ++fmt
) {
790 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
796 dumpone(struct format
*fmt
)
800 if ((i
= findlabel(fmt
)) >= 0)
804 fputs(f_typestr((int)fmt
->f_type
), stdout
);
806 switch (fmt
->f_type
) {
812 fputs(", comp ", stdout
);
813 litputs(fmt
->f_comp
->c_name
);
814 if (fmt
->f_comp
->c_type
)
815 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
816 if (fmt
->f_comp
->c_flags
)
817 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
844 fputs(", c_name ", stdout
);
845 litputs(fmt
->f_comp
->c_name
);
846 if (fmt
->f_comp
->c_type
)
847 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
848 if (fmt
->f_comp
->c_flags
)
849 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
868 fputs(", c_name ", stdout
);
869 litputs(fmt
->f_comp
->c_name
);
870 if (fmt
->f_comp
->c_type
)
871 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
872 if (fmt
->f_comp
->c_flags
)
873 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
877 printf(", width %d, fill '", fmt
->f_width
);
878 litputc(fmt
->f_fill
);
879 fputs("' name ", stdout
);
880 litputs(fmt
->f_comp
->c_name
);
881 if (fmt
->f_comp
->c_type
)
882 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
883 if (fmt
->f_comp
->c_flags
)
884 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
889 printf(", width %d, fill '", fmt
->f_width
);
890 litputc(fmt
->f_fill
);
896 litputs(fmt
->f_text
);
900 printf(", width %d, fill '", fmt
->f_width
);
901 litputc(fmt
->f_fill
);
903 litputs(fmt
->f_text
);
909 litputc(fmt
->f_char
);
918 fputs(" continue else goto", stdout
);
921 i
= findlabel(fmt
+ fmt
->f_skip
);
928 i
= findlabel(fmt
+ fmt
->f_skip
);
929 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
938 case FT_LV_MULTIPLY_L
:
941 printf(" value %d", fmt
->f_value
);
945 fputs(" str ", stdout
);
946 litputs(fmt
->f_text
);
950 fputs(" getenv ", stdout
);
951 litputs(fmt
->f_text
);
954 case FT_LS_DECODECOMP
:
955 fputs(", comp ", stdout
);
956 litputs(fmt
->f_comp
->c_name
);
963 printf(", width %d", fmt
->f_width
);
967 printf(", value dat[%d]", fmt
->f_value
);
974 * Iterate over all instructions and assign labels to the targets of
979 initlabels(struct format
*fmth
)
981 struct format
*fmt
, *addr
;
985 for (fmt
= fmth
; fmt
; ++fmt
) {
995 addr
= fmt
+ fmt
->f_skip
;
996 if (findlabel(addr
) < 0)
999 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
1006 findlabel(struct format
*addr
)
1010 for (i
= 0; i
< lused
; ++i
)
1011 if (addr
== lvec
[i
])
1017 assignlabel(struct format
*addr
)
1019 if (lused
>= lallocated
) {
1021 lvec
= (struct format
**)
1022 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1025 lvec
[lused
++] = addr
;
1031 static char buf
[32];
1034 case FT_COMP
: return "COMP";
1035 case FT_COMPF
: return "COMPF";
1036 case FT_LIT
: return "LIT";
1037 case FT_LITF
: return "LITF";
1038 case FT_CHAR
: return "CHAR";
1039 case FT_NUM
: return "NUM";
1040 case FT_NUMF
: return "NUMF";
1041 case FT_STR
: return "STR";
1042 case FT_STRF
: return "STRF";
1043 case FT_STRFW
: return "STRFW";
1044 case FT_STRLIT
: return "STRLIT";
1045 case FT_STRLITZ
: return "STRLITZ";
1046 case FT_PUTADDR
: return "PUTADDR";
1047 case FT_LS_COMP
: return "LS_COMP";
1048 case FT_LS_LIT
: return "LS_LIT";
1049 case FT_LS_GETENV
: return "LS_GETENV";
1050 case FT_LS_CFIND
: return "LS_CFIND";
1051 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1052 case FT_LS_DECODE
: return "LS_DECODE";
1053 case FT_LS_TRIM
: return "LS_TRIM";
1054 case FT_LV_COMP
: return "LV_COMP";
1055 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1056 case FT_LV_LIT
: return "LV_LIT";
1057 case FT_LV_DAT
: return "LV_DAT";
1058 case FT_LV_STRLEN
: return "LV_STRLEN";
1059 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1060 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1061 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1062 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1063 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1064 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1065 case FT_LS_MONTH
: return "LS_MONTH";
1066 case FT_LS_LMONTH
: return "LS_LMONTH";
1067 case FT_LS_ZONE
: return "LS_ZONE";
1068 case FT_LS_DAY
: return "LS_DAY";
1069 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1070 case FT_LS_822DATE
: return "LS_822DATE";
1071 case FT_LS_PRETTY
: return "LS_PRETTY";
1072 case FT_LS_KILO
: return "LS_KILO";
1073 case FT_LS_KIBI
: return "LS_KIBI";
1074 case FT_LV_SEC
: return "LV_SEC";
1075 case FT_LV_MIN
: return "LV_MIN";
1076 case FT_LV_HOUR
: return "LV_HOUR";
1077 case FT_LV_MDAY
: return "LV_MDAY";
1078 case FT_LV_MON
: return "LV_MON";
1079 case FT_LV_YEAR
: return "LV_YEAR";
1080 case FT_LV_YDAY
: return "LV_YDAY";
1081 case FT_LV_WDAY
: return "LV_WDAY";
1082 case FT_LV_ZONE
: return "LV_ZONE";
1083 case FT_LV_CLOCK
: return "LV_CLOCK";
1084 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1085 case FT_LV_DAYF
: return "LV_DAYF";
1086 case FT_LV_DST
: return "LV_DST";
1087 case FT_LV_ZONEF
: return "LV_ZONEF";
1088 case FT_LS_PERS
: return "LS_PERS";
1089 case FT_LS_MBOX
: return "LS_MBOX";
1090 case FT_LS_HOST
: return "LS_HOST";
1091 case FT_LS_PATH
: return "LS_PATH";
1092 case FT_LS_GNAME
: return "LS_GNAME";
1093 case FT_LS_NOTE
: return "LS_NOTE";
1094 case FT_LS_ADDR
: return "LS_ADDR";
1095 case FT_LS_822ADDR
: return "LS_822ADDR";
1096 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1097 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1098 case FT_LV_INGRPF
: return "LV_INGRPF";
1099 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1100 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1101 case FT_LOCALDATE
: return "LOCALDATE";
1102 case FT_GMTDATE
: return "GMTDATE";
1103 case FT_PARSEDATE
: return "PARSEDATE";
1104 case FT_PARSEADDR
: return "PARSEADDR";
1105 case FT_FORMATADDR
: return "FORMATADDR";
1106 case FT_CONCATADDR
: return "CONCATADDR";
1107 case FT_MYMBOX
: return "MYMBOX";
1108 case FT_GETMYMBOX
: return "GETMYMBOX";
1109 case FT_GETMYADDR
: return "GETMYADDR";
1110 case FT_SAVESTR
: return "SAVESTR";
1111 case FT_DONE
: return "DONE";
1112 case FT_PAUSE
: return "PAUSE";
1113 case FT_NOP
: return "NOP";
1114 case FT_GOTO
: return "GOTO";
1115 case FT_IF_S_NULL
: return "IF_S_NULL";
1116 case FT_IF_S
: return "IF_S";
1117 case FT_IF_V_EQ
: return "IF_V_EQ";
1118 case FT_IF_V_NE
: return "IF_V_NE";
1119 case FT_IF_V_GT
: return "IF_V_GT";
1120 case FT_IF_MATCH
: return "IF_MATCH";
1121 case FT_IF_AMATCH
: return "IF_AMATCH";
1122 case FT_S_NULL
: return "S_NULL";
1123 case FT_S_NONNULL
: return "S_NONNULL";
1124 case FT_V_EQ
: return "V_EQ";
1125 case FT_V_NE
: return "V_NE";
1126 case FT_V_GT
: return "V_GT";
1127 case FT_V_MATCH
: return "V_MATCH";
1128 case FT_V_AMATCH
: return "V_AMATCH";
1130 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1138 static char buf
[64];
1140 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1147 static char buf
[64];
1149 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1154 litputs(const char *s
)
1162 fputs("<nil>", stdout
);
1169 printf("\\x%02x", (unsigned char) c
);
1170 } else if (c
< 0x20 || c
== 0177) {
1174 } else if (c
== '\f') {
1177 } else if (c
== '\n') {
1180 } else if (c
== '\r') {
1183 } else if (c
== '\t') {
1188 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1195 * Routines/code to support the duplicate address suppression code, adapted
1199 static char *buf
; /* our current working buffer */
1200 static char *bufend
; /* end of working buffer */
1201 static char *last_dst
; /* buf ptr at end of last call */
1202 static unsigned int bufsiz
=0; /* current size of buf */
1204 #define BUFINCR 512 /* how much to expand buf when if fills */
1206 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1209 * check if there's enough room in buf for str.
1210 * add more mem if needed
1212 #define CHECKMEM(str) \
1213 if ((len = strlen (str)) >= bufend - dst) {\
1215 int n = last_dst - buf;\
1216 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1217 buf = mh_xrealloc (buf, bufsiz);\
1219 last_dst = buf + n;\
1220 bufend = buf + bufsiz;\
1225 * These are versions of similar routines from replsbr.c; the purpose is
1226 * to suppress duplicate addresses from being added to a list when building
1227 * up addresses for the %(formataddr) format function. This is used by
1228 * repl to prevent duplicate addresses from being added to the "to" line.
1229 * See replsbr.c for more information.
1231 * We can't use the functions in replsbr.c directly because they are slightly
1232 * different and depend on the rest of replsbr.c
1235 test_formataddr (char *orig
, char *str
)
1243 struct mailname
*mp
= NULL
;
1245 /* if we don't have a buffer yet, get one */
1247 buf
= mh_xmalloc (BUFINCR
);
1248 last_dst
= buf
; /* XXX */
1249 bufsiz
= BUFINCR
- 6; /* leave some slop */
1250 bufend
= buf
+ bufsiz
;
1253 * If "orig" points to our buffer we can just pick up where we
1254 * left off. Otherwise we have to copy orig into our buffer.
1258 else if (!orig
|| !*orig
) {
1262 dst
= last_dst
; /* XXX */
1267 /* concatenate all the new addresses onto 'buf' */
1268 for (isgroup
= false; (cp
= getname (str
)); ) {
1269 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1270 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1273 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1278 /* if we get here we're going to add an address */
1284 CHECKMEM (mp
->m_gname
);
1288 sp
= adrformat (mp
);
1304 * The companion to test_formataddr(); it behaves the same way, except doesn't
1305 * do duplicate address detection.
1308 test_concataddr(char *orig
, char *str
)
1313 cp
= test_formataddr(orig
, str
);
1319 insert (struct mailname
*np
)
1321 struct mailname
*mp
;
1326 if (np
->m_mbox
== NULL
)
1329 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1330 if (!strcasecmp (FENDNULL(np
->m_host
),
1331 FENDNULL(mp
->m_next
->m_host
)) &&
1332 !strcasecmp (FENDNULL(np
->m_mbox
),
1333 FENDNULL(mp
->m_next
->m_mbox
)))
1336 if (!ccme
&& ismymbox (np
))
1345 * Reset our duplicate address list
1351 struct mailname
*mp
, *mp2
;
1353 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {