]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
1 /* fmttest.c -- A program to help test and debug format instructions
3 * This code is Copyright (c) 2012, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include <h/fmt_scan.h>
10 #include <h/fmt_compile.h>
12 #include <h/scansbr.h>
13 #include <h/addrsbr.h>
14 #include "sbr/m_maildir.h"
15 #include "sbr/terminal.h"
17 #define FMTTEST_SWITCHES \
18 X("form formatfile", 0, FORMSW) \
19 X("format string", 5, FMTSW) \
20 X("address", 0, ADDRSW) \
22 X("date", 0, DATESW) \
23 X("message", 0, MESSAGESW) \
24 X("file", 0, FILESW) \
25 X("nofile", 0, NFILESW) \
26 X("-component-name component-text", 0, OTHERSW) \
27 X("dupaddrs", 0, DUPADDRSW) \
28 X("nodupaddrs", 0, NDUPADDRSW) \
29 X("ccme", 0, CCMESW) \
30 X("noccme", 0, NCCMESW) \
31 X("outsize size-in-characters", 0, OUTSIZESW) \
32 X("width column-width", 0, WIDTHSW) \
33 X("msgnum number", 0, MSGNUMSW) \
34 X("msgcur flag", 0, MSGCURSW) \
35 X("msgsize size", 0, MSGSIZESW) \
36 X("unseen flag", 0, UNSEENSW) \
37 X("dump", 0, DUMPSW) \
38 X("nodump", 0, NDUMPSW) \
39 X("trace", 0, TRACESW) \
40 X("notrace", 0, NTRACESW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(FMTTEST
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
53 * An array containing labels used for branch instructions
56 static struct format
**lvec
= NULL
;
58 static int lallocated
= 0;
60 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
61 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
62 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
65 * Context structure used by the tracing routines
68 struct trace_context
{
77 static void fmt_dump (char *, struct format
*);
78 static void dumpone(struct format
*);
79 static void initlabels(struct format
*);
80 static int findlabel(struct format
*);
81 static void assignlabel(struct format
*);
82 static char *f_typestr(int);
83 static char *c_typestr(int);
84 static char *c_flagsstr(int);
85 static void litputs(const char *);
86 static void litputc(char);
87 static void process_addresses(struct format
*, struct msgs_array
*,
88 charstring_t
, int, int *,
89 struct fmt_callbacks
*);
90 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
91 int, int *, struct fmt_callbacks
*);
92 static void process_messages(struct format
*, struct msgs_array
*,
93 struct msgs_array
*, charstring_t
, char *, int,
94 int, int *, struct fmt_callbacks
*);
95 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
96 struct format
*, charstring_t
, int,
97 struct fmt_callbacks
*);
98 static void test_trace(void *, struct format
*, int, char *, const char *);
99 static char *test_formataddr(char *, char *);
100 static char *test_concataddr(char *, char *);
101 static int insert(struct mailname
*);
102 static void mlistfree(void);
104 static int nodupcheck
= 0; /* If set, no check for duplicates */
105 static int ccme
= 0; /* Should I cc myself? */
106 static struct mailname mq
; /* Mail addresses to check for duplicates */
107 static char *dummy
= "dummy";
110 main (int argc
, char **argv
)
112 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
113 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
117 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
119 int outputsize
= 0, dupaddrs
= 1, trace
= 0, files
= 0;
120 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
121 enum mode_t mode
= MESSAGE
;
123 struct fmt_callbacks cb
, *cbp
= NULL
;
125 if (nmh_init(argv
[0], 1)) { return 1; }
127 arguments
= getarguments (invo_name
, argc
, argv
, 1);
130 while ((cp
= *argp
++)) {
133 * A -- means that we have a component name (like pick);
134 * save the component name and the next argument for the text.
138 adios(NULL
, "missing component name after --");
139 app_msgarg(&compargs
, cp
);
140 /* Grab next argument for component text */
142 adios(NULL
, "missing argument to %s", argp
[-2]);
143 app_msgarg(&compargs
, cp
);
146 switch (smatch (cp
, switches
)) {
148 ambigsw (cp
, switches
);
151 adios (NULL
, "-%s unknown", cp
);
154 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
155 print_help (buf
, switches
, 1);
158 print_version(invo_name
);
161 adios(NULL
, "internal argument error!");
165 if (!(cp
= *argp
++) || *cp
== '-')
166 adios(NULL
, "missing argument to %s", argp
[-2]);
167 if (strcmp(cp
, "max") == 0)
168 outputsize
= INT_MAX
;
169 else if (strcmp(cp
, "width") == 0)
170 outputsize
= sc_width();
172 outputsize
= atoi(cp
);
176 if (!(form
= *argp
++) || *form
== '-')
177 adios (NULL
, "missing argument to %s", argp
[-2]);
181 if (!(format
= *argp
++) || *format
== '-')
182 adios (NULL
, "missing argument to %s", argp
[-2]);
195 defformat
= DEFADDRFORMAT
;
207 defformat
= DEFDATEFORMAT
;
232 if (!(cp
= *argp
++) || *cp
== '-')
233 adios(NULL
, "missing argument to %s", argp
[-2]);
237 if (!(cp
= *argp
++) || *cp
== '-')
238 adios(NULL
, "missing argument to %s", argp
[-2]);
242 if (!(cp
= *argp
++) || *cp
== '-')
243 adios(NULL
, "missing argument to %s", argp
[-2]);
247 if (!(cp
= *argp
++) || *cp
== '-')
248 adios(NULL
, "missing argument to %s", argp
[-2]);
252 if (!(cp
= *argp
++) || *cp
== '-')
253 adios(NULL
, "missing argument to %s", argp
[-2]);
254 msgunseen
= atoi(cp
);
268 * Only interpret as a folder if we're in message mode
271 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
273 adios (NULL
, "only one folder at a time!");
274 folder
= pluspath (cp
);
276 app_msgarg(&msgs
, cp
);
280 * Here's our weird heuristic:
282 * - We allow -dump without any other arguments.
283 * - If you've given any component arguments, we don't require any
285 * - The arguments are interpreted as folders/messages _if_ we're in
286 * message mode, otherwise pass as strings in the text component.
289 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
290 adios(NULL
, "usage: [switches] [+folder] msgs | strings...");
294 * If you're picking "raw" as a mode, then you have to select
298 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
299 adios (NULL
, "You must specify a format with -form or -format when "
304 * Get new format string. Must be before chdir().
306 nfs
= new_fs (form
, format
, defformat
);
307 (void) fmt_compile(nfs
, &fmt
, 1);
313 if (compargs
.size
== 0 && msgs
.size
== 0)
318 buffer
= charstring_create(BUFSIZ
);
320 if (outputsize
== 0) {
322 outputsize
= sc_width();
324 outputsize
= INT_MAX
;
330 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
334 * If we want to provide our own formataddr, concactaddr, or tracing
335 * callback, do that now. Also, prime ismymbox if we use it.
338 if (dupaddrs
== 0 || trace
) {
343 cb
.formataddr
= test_formataddr
;
344 cb
.concataddr
= test_concataddr
;
350 struct trace_context
*ctx
;
355 ctx
->outbuf
= mh_xstrdup("");
357 cb
.trace_func
= test_trace
;
358 cb
.trace_context
= ctx
;
362 if (mode
== MESSAGE
) {
363 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
367 for (i
= 0; i
< compargs
.size
; i
+= 2) {
368 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
370 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
374 if (mode
== ADDRESS
) {
375 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
376 } else /* Fall-through for RAW or DATE */
377 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
380 charstring_free(buffer
);
388 * Process each address with fmt_scan().
394 struct pqpair
*pq_next
;
398 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
399 charstring_t buffer
, int outwidth
, int *dat
,
400 struct fmt_callbacks
*cb
)
403 char *cp
, error
[BUFSIZ
];
405 struct pqpair
*p
, *q
;
418 for (i
= 0; i
< addrs
->size
; i
++) {
419 (q
= &pq
)->pq_next
= NULL
;
420 while ((cp
= getname(addrs
->msgs
[i
]))) {
422 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
423 p
->pq_text
= mh_xstrdup(cp
);
424 p
->pq_error
= mh_xstrdup(error
);
426 p
->pq_text
= getcpy(mp
->m_text
);
429 q
= (q
->pq_next
= p
);
432 for (p
= pq
.pq_next
; p
; p
= q
) {
433 c
= fmt_findcomp("text");
436 c
->c_text
= p
->pq_text
;
439 c
= fmt_findcomp("error");
442 c
->c_text
= p
->pq_error
;
446 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
447 fputs(charstring_buffer(buffer
), stdout
);
459 * Process messages and run them through the format engine. A lot taken
464 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
465 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
466 int outwidth
, int files
, int *dat
,
467 struct fmt_callbacks
*cb
)
469 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
470 int num_unseen_seq
= 0;
471 ivector_t seqnum
= ivector_create (0);
477 * If 'files' is set, short-circuit everything else and just process
482 for (i
= 0; i
< msgs
->size
; i
++) {
483 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
484 admonish(cp
, "unable to open file");
487 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
495 folder
= getfolder(1);
497 maildir
= m_maildir(folder
);
499 if (chdir(maildir
) < 0)
500 adios(maildir
, "unable to change directory to");
502 if (!(mp
= folder_read(folder
, 1)))
503 adios(NULL
, "unable to read folder %s", folder
);
506 adios(NULL
, "no messages in %s", folder
);
508 for (i
= 0; i
< msgs
->size
; i
++)
509 if (!m_convert(mp
, msgs
->msgs
[i
]))
511 seq_setprev(mp
); /* set the Previous-Sequence */
513 context_replace(pfolder
, folder
); /* update current folder */
514 seq_save(mp
); /* synchronize message sequences */
515 context_save(); /* save the context file */
518 * We want to set the unseen flag if requested, so we have to check
519 * the unseen sequence as well.
523 if ((cp
= context_find(usequence
)) && *cp
) {
527 ap
= brkstring(dp
, " ", "\n");
528 for (i
= 0; ap
&& *ap
; i
++, ap
++)
529 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
536 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
537 if (is_selected(mp
, msgnum
)) {
538 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
539 admonish(cp
, "unable to open message");
549 dat
[1] = msgnum
== mp
->curmsg
;
552 * Check to see if this is in the unseen sequence
556 for (i
= 0; i
< num_unseen_seq
; i
++) {
557 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
564 * Read in the message and process the components
567 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
572 ivector_free (seqnum
);
577 * Process a single file in message mode
581 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
582 struct format
*fmt
, charstring_t buffer
, int outwidth
,
583 struct fmt_callbacks
*cb
)
586 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
587 m_getfld_state_t gstate
;
592 * Get our size if we didn't include one
598 if (fstat(fileno(in
), &st
) < 0)
605 * Initialize everything else
616 * Read in the message and process the components
619 gstate
= m_getfld_state_init(in
);
621 bufsz
= sizeof(rbuf
);
622 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
626 i
= fmt_addcomptext(name
, rbuf
);
628 while (state
== FLDPLUS
) {
629 bufsz
= sizeof(rbuf
);
630 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
631 fmt_appendcomp(i
, name
, rbuf
);
635 while (state
== FLDPLUS
) {
636 bufsz
= sizeof(rbuf
);
637 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
642 if (fmt_findcomp("body")) {
643 if ((i
= strlen(rbuf
)) < outwidth
) {
644 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
645 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
648 fmt_addcomptext("body", rbuf
);
658 m_getfld_state_destroy(&gstate
);
661 * Do this now to override any components in the original message
664 for (i
= 0; i
< comps
->size
; i
+= 2) {
665 c
= fmt_findcomp(comps
->msgs
[i
]);
668 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
672 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
673 fputs(charstring_buffer (buffer
), stdout
);
678 * Run text through the format engine with no special processing
682 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
683 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
697 c
= fmt_findcomp("text");
699 for (i
= 0; i
< text
->size
; i
++) {
702 c
->c_text
= getcpy(text
->msgs
[i
]);
705 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
706 fputs(charstring_buffer (buffer
), stdout
);
712 * Our basic tracing support callback.
714 * Print out each instruction as it's executed, including the values of
715 * the num and str registers if they've changed.
719 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
722 struct trace_context
*ctx
= (struct trace_context
*) context
;
727 if (num
!= ctx
->num
) {
728 printf("num=%d", num
);
733 if (str
!= ctx
->str
) {
744 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
749 ctx
->outbuf
= mh_xstrdup(outbuf
);
754 fmt_dump (char *nfs
, struct format
*fmth
)
758 printf("Instruction dump of format string: \n%s\n", nfs
);
761 for (fmt
= fmth
; fmt
; ++fmt
) {
763 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
769 dumpone(struct format
*fmt
)
773 if ((i
= findlabel(fmt
)) >= 0)
777 fputs(f_typestr((int)fmt
->f_type
), stdout
);
779 switch (fmt
->f_type
) {
786 litputs(fmt
->f_comp
->c_name
);
787 if (fmt
->f_comp
->c_type
)
788 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
789 if (fmt
->f_comp
->c_flags
)
790 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
818 litputs(fmt
->f_comp
->c_name
);
819 if (fmt
->f_comp
->c_type
)
820 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
821 if (fmt
->f_comp
->c_flags
)
822 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
842 litputs(fmt
->f_comp
->c_name
);
843 if (fmt
->f_comp
->c_type
)
844 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
845 if (fmt
->f_comp
->c_flags
)
846 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
850 printf(", width %d, fill '", fmt
->f_width
);
851 litputc(fmt
->f_fill
);
853 litputs(fmt
->f_comp
->c_name
);
854 if (fmt
->f_comp
->c_type
)
855 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
856 if (fmt
->f_comp
->c_flags
)
857 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
862 printf(", width %d, fill '", fmt
->f_width
);
863 litputc(fmt
->f_fill
);
869 litputs(fmt
->f_text
);
873 printf(", width %d, fill '", fmt
->f_width
);
874 litputc(fmt
->f_fill
);
876 litputs(fmt
->f_text
);
882 litputc(fmt
->f_char
);
891 printf(" continue else goto");
894 i
= findlabel(fmt
+ fmt
->f_skip
);
901 i
= findlabel(fmt
+ fmt
->f_skip
);
902 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
911 case FT_LV_MULTIPLY_L
:
914 printf(" value %d", fmt
->f_value
);
919 litputs(fmt
->f_text
);
924 litputs(fmt
->f_text
);
927 case FT_LS_DECODECOMP
:
929 litputs(fmt
->f_comp
->c_name
);
936 printf(", width %d", fmt
->f_width
);
940 printf(", value dat[%d]", fmt
->f_value
);
947 * Iterate over all instructions and assign labels to the targets of
952 initlabels(struct format
*fmth
)
954 struct format
*fmt
, *addr
;
958 for (fmt
= fmth
; fmt
; ++fmt
) {
968 addr
= fmt
+ fmt
->f_skip
;
969 if (findlabel(addr
) < 0)
972 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
979 findlabel(struct format
*addr
)
983 for (i
= 0; i
< lused
; ++i
)
990 assignlabel(struct format
*addr
)
992 if (lused
>= lallocated
) {
994 lvec
= (struct format
**)
995 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
998 lvec
[lused
++] = addr
;
1004 static char buf
[32];
1007 case FT_COMP
: return "COMP";
1008 case FT_COMPF
: return "COMPF";
1009 case FT_LIT
: return "LIT";
1010 case FT_LITF
: return "LITF";
1011 case FT_CHAR
: return "CHAR";
1012 case FT_NUM
: return "NUM";
1013 case FT_NUMF
: return "NUMF";
1014 case FT_STR
: return "STR";
1015 case FT_STRF
: return "STRF";
1016 case FT_STRFW
: return "STRFW";
1017 case FT_STRLIT
: return "STRLIT";
1018 case FT_STRLITZ
: return "STRLITZ";
1019 case FT_PUTADDR
: return "PUTADDR";
1020 case FT_LS_COMP
: return "LS_COMP";
1021 case FT_LS_LIT
: return "LS_LIT";
1022 case FT_LS_GETENV
: return "LS_GETENV";
1023 case FT_LS_CFIND
: return "LS_CFIND";
1024 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1025 case FT_LS_DECODE
: return "LS_DECODE";
1026 case FT_LS_TRIM
: return "LS_TRIM";
1027 case FT_LV_COMP
: return "LV_COMP";
1028 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1029 case FT_LV_LIT
: return "LV_LIT";
1030 case FT_LV_DAT
: return "LV_DAT";
1031 case FT_LV_STRLEN
: return "LV_STRLEN";
1032 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1033 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1034 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1035 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1036 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1037 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1038 case FT_LS_MONTH
: return "LS_MONTH";
1039 case FT_LS_LMONTH
: return "LS_LMONTH";
1040 case FT_LS_ZONE
: return "LS_ZONE";
1041 case FT_LS_DAY
: return "LS_DAY";
1042 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1043 case FT_LS_822DATE
: return "LS_822DATE";
1044 case FT_LS_PRETTY
: return "LS_PRETTY";
1045 case FT_LS_KILO
: return "LS_KILO";
1046 case FT_LS_KIBI
: return "LS_KIBI";
1047 case FT_LV_SEC
: return "LV_SEC";
1048 case FT_LV_MIN
: return "LV_MIN";
1049 case FT_LV_HOUR
: return "LV_HOUR";
1050 case FT_LV_MDAY
: return "LV_MDAY";
1051 case FT_LV_MON
: return "LV_MON";
1052 case FT_LV_YEAR
: return "LV_YEAR";
1053 case FT_LV_YDAY
: return "LV_YDAY";
1054 case FT_LV_WDAY
: return "LV_WDAY";
1055 case FT_LV_ZONE
: return "LV_ZONE";
1056 case FT_LV_CLOCK
: return "LV_CLOCK";
1057 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1058 case FT_LV_DAYF
: return "LV_DAYF";
1059 case FT_LV_DST
: return "LV_DST";
1060 case FT_LV_ZONEF
: return "LV_ZONEF";
1061 case FT_LS_PERS
: return "LS_PERS";
1062 case FT_LS_MBOX
: return "LS_MBOX";
1063 case FT_LS_HOST
: return "LS_HOST";
1064 case FT_LS_PATH
: return "LS_PATH";
1065 case FT_LS_GNAME
: return "LS_GNAME";
1066 case FT_LS_NOTE
: return "LS_NOTE";
1067 case FT_LS_ADDR
: return "LS_ADDR";
1068 case FT_LS_822ADDR
: return "LS_822ADDR";
1069 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1070 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1071 case FT_LV_INGRPF
: return "LV_INGRPF";
1072 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1073 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1074 case FT_LOCALDATE
: return "LOCALDATE";
1075 case FT_GMTDATE
: return "GMTDATE";
1076 case FT_PARSEDATE
: return "PARSEDATE";
1077 case FT_PARSEADDR
: return "PARSEADDR";
1078 case FT_FORMATADDR
: return "FORMATADDR";
1079 case FT_CONCATADDR
: return "CONCATADDR";
1080 case FT_MYMBOX
: return "MYMBOX";
1081 case FT_GETMYMBOX
: return "GETMYMBOX";
1082 case FT_GETMYADDR
: return "GETMYADDR";
1083 case FT_SAVESTR
: return "SAVESTR";
1084 case FT_DONE
: return "DONE";
1085 case FT_PAUSE
: return "PAUSE";
1086 case FT_NOP
: return "NOP";
1087 case FT_GOTO
: return "GOTO";
1088 case FT_IF_S_NULL
: return "IF_S_NULL";
1089 case FT_IF_S
: return "IF_S";
1090 case FT_IF_V_EQ
: return "IF_V_EQ";
1091 case FT_IF_V_NE
: return "IF_V_NE";
1092 case FT_IF_V_GT
: return "IF_V_GT";
1093 case FT_IF_MATCH
: return "IF_MATCH";
1094 case FT_IF_AMATCH
: return "IF_AMATCH";
1095 case FT_S_NULL
: return "S_NULL";
1096 case FT_S_NONNULL
: return "S_NONNULL";
1097 case FT_V_EQ
: return "V_EQ";
1098 case FT_V_NE
: return "V_NE";
1099 case FT_V_GT
: return "V_GT";
1100 case FT_V_MATCH
: return "V_MATCH";
1101 case FT_V_AMATCH
: return "V_AMATCH";
1103 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1111 static char buf
[64];
1113 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1120 static char buf
[64];
1122 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1127 litputs(const char *s
)
1135 fputs("<nil>", stdout
);
1142 printf("\\x%02x", (unsigned char) c
);
1143 } else if (c
< 0x20 || c
== 0177) {
1147 } else if (c
== '\f') {
1150 } else if (c
== '\n') {
1153 } else if (c
== '\r') {
1156 } else if (c
== '\t') {
1161 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1168 * Routines/code to support the duplicate address suppression code, adapted
1172 static char *buf
; /* our current working buffer */
1173 static char *bufend
; /* end of working buffer */
1174 static char *last_dst
; /* buf ptr at end of last call */
1175 static unsigned int bufsiz
=0; /* current size of buf */
1177 #define BUFINCR 512 /* how much to expand buf when if fills */
1179 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1182 * check if there's enough room in buf for str.
1183 * add more mem if needed
1185 #define CHECKMEM(str) \
1186 if ((len = strlen (str)) >= bufend - dst) {\
1188 int n = last_dst - buf;\
1189 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1190 buf = mh_xrealloc (buf, bufsiz);\
1192 last_dst = buf + n;\
1193 bufend = buf + bufsiz;\
1198 * These are versions of similar routines from replsbr.c; the purpose is
1199 * to suppress duplicate addresses from being added to a list when building
1200 * up addresses for the %(formataddr) format function. This is used by
1201 * repl to prevent duplicate addresses from being added to the "to" line.
1202 * See replsbr.c for more information.
1204 * We can't use the functions in replsbr.c directly because they are slightly
1205 * different and depend on the rest of replsbr.c
1208 test_formataddr (char *orig
, char *str
)
1216 struct mailname
*mp
= NULL
;
1218 /* if we don't have a buffer yet, get one */
1220 buf
= mh_xmalloc (BUFINCR
);
1221 last_dst
= buf
; /* XXX */
1222 bufsiz
= BUFINCR
- 6; /* leave some slop */
1223 bufend
= buf
+ bufsiz
;
1226 * If "orig" points to our buffer we can just pick up where we
1227 * left off. Otherwise we have to copy orig into our buffer.
1231 else if (!orig
|| !*orig
) {
1235 dst
= last_dst
; /* XXX */
1240 /* concatenate all the new addresses onto 'buf' */
1241 for (isgroup
= 0; (cp
= getname (str
)); ) {
1242 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1243 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1246 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1251 /* if we get here we're going to add an address */
1257 CHECKMEM (mp
->m_gname
);
1261 sp
= adrformat (mp
);
1277 * The companion to test_formataddr(); it behaves the same way, except doesn't
1278 * do duplicate address detection.
1281 test_concataddr(char *orig
, char *str
)
1286 cp
= test_formataddr(orig
, str
);
1292 insert (struct mailname
*np
)
1294 struct mailname
*mp
;
1299 if (np
->m_mbox
== NULL
)
1302 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1303 if (!strcasecmp (FENDNULL(np
->m_host
),
1304 FENDNULL(mp
->m_next
->m_host
)) &&
1305 !strcasecmp (FENDNULL(np
->m_mbox
),
1306 FENDNULL(mp
->m_next
->m_mbox
)))
1309 if (!ccme
&& ismymbox (np
))
1318 * Reset our duplicate address list
1324 struct mailname
*mp
, *mp2
;
1326 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {