]>
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"
16 #define FMTTEST_SWITCHES \
17 X("form formatfile", 0, FORMSW) \
18 X("format string", 5, FMTSW) \
19 X("address", 0, ADDRSW) \
21 X("date", 0, DATESW) \
22 X("message", 0, MESSAGESW) \
23 X("file", 0, FILESW) \
24 X("nofile", 0, NFILESW) \
25 X("-component-name component-text", 0, OTHERSW) \
26 X("dupaddrs", 0, DUPADDRSW) \
27 X("nodupaddrs", 0, NDUPADDRSW) \
28 X("ccme", 0, CCMESW) \
29 X("noccme", 0, NCCMESW) \
30 X("outsize size-in-characters", 0, OUTSIZESW) \
31 X("width column-width", 0, WIDTHSW) \
32 X("msgnum number", 0, MSGNUMSW) \
33 X("msgcur flag", 0, MSGCURSW) \
34 X("msgsize size", 0, MSGSIZESW) \
35 X("unseen flag", 0, UNSEENSW) \
36 X("dump", 0, DUMPSW) \
37 X("nodump", 0, NDUMPSW) \
38 X("trace", 0, TRACESW) \
39 X("notrace", 0, NTRACESW) \
40 X("version", 0, VERSIONSW) \
41 X("help", 0, HELPSW) \
43 #define X(sw, minchars, id) id,
44 DEFINE_SWITCH_ENUM(FMTTEST
);
47 #define X(sw, minchars, id) { sw, minchars, id },
48 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
52 * An array containing labels used for branch instructions
55 static struct format
**lvec
= NULL
;
57 static int lallocated
= 0;
59 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
60 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
61 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
64 * Context structure used by the tracing routines
67 struct trace_context
{
76 static void fmt_dump (char *, struct format
*);
77 static void dumpone(struct format
*);
78 static void initlabels(struct format
*);
79 static int findlabel(struct format
*);
80 static void assignlabel(struct format
*);
81 static char *f_typestr(int);
82 static char *c_typestr(int);
83 static char *c_flagsstr(int);
84 static void litputs(const char *);
85 static void litputc(char);
86 static void process_addresses(struct format
*, struct msgs_array
*,
87 charstring_t
, int, int *,
88 struct fmt_callbacks
*);
89 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
90 int, int *, struct fmt_callbacks
*);
91 static void process_messages(struct format
*, struct msgs_array
*,
92 struct msgs_array
*, charstring_t
, char *, int,
93 int, int *, struct fmt_callbacks
*);
94 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
95 struct format
*, charstring_t
, int,
96 struct fmt_callbacks
*);
97 static void test_trace(void *, struct format
*, int, char *, const char *);
98 static char *test_formataddr(char *, char *);
99 static char *test_concataddr(char *, char *);
100 static int insert(struct mailname
*);
101 static void mlistfree(void);
103 static int nodupcheck
= 0; /* If set, no check for duplicates */
104 static int ccme
= 0; /* Should I cc myself? */
105 static struct mailname mq
; /* Mail addresses to check for duplicates */
106 static char *dummy
= "dummy";
109 main (int argc
, char **argv
)
111 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
112 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
116 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
118 int outputsize
= 0, dupaddrs
= 1, trace
= 0, files
= 0;
119 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
120 enum mode_t mode
= MESSAGE
;
122 struct fmt_callbacks cb
, *cbp
= NULL
;
124 if (nmh_init(argv
[0], 1)) { return 1; }
126 arguments
= getarguments (invo_name
, argc
, argv
, 1);
129 while ((cp
= *argp
++)) {
132 * A -- means that we have a component name (like pick);
133 * save the component name and the next argument for the text.
137 adios(NULL
, "missing component name after --");
138 app_msgarg(&compargs
, cp
);
139 /* Grab next argument for component text */
141 adios(NULL
, "missing argument to %s", argp
[-2]);
142 app_msgarg(&compargs
, cp
);
145 switch (smatch (cp
, switches
)) {
147 ambigsw (cp
, switches
);
150 adios (NULL
, "-%s unknown", cp
);
153 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
154 print_help (buf
, switches
, 1);
157 print_version(invo_name
);
160 adios(NULL
, "internal argument error!");
164 if (!(cp
= *argp
++) || *cp
== '-')
165 adios(NULL
, "missing argument to %s", argp
[-2]);
166 if (strcmp(cp
, "max") == 0)
167 outputsize
= INT_MAX
;
168 else if (strcmp(cp
, "width") == 0)
169 outputsize
= sc_width();
171 outputsize
= atoi(cp
);
175 if (!(form
= *argp
++) || *form
== '-')
176 adios (NULL
, "missing argument to %s", argp
[-2]);
180 if (!(format
= *argp
++) || *format
== '-')
181 adios (NULL
, "missing argument to %s", argp
[-2]);
194 defformat
= DEFADDRFORMAT
;
206 defformat
= DEFDATEFORMAT
;
231 if (!(cp
= *argp
++) || *cp
== '-')
232 adios(NULL
, "missing argument to %s", argp
[-2]);
236 if (!(cp
= *argp
++) || *cp
== '-')
237 adios(NULL
, "missing argument to %s", argp
[-2]);
241 if (!(cp
= *argp
++) || *cp
== '-')
242 adios(NULL
, "missing argument to %s", argp
[-2]);
246 if (!(cp
= *argp
++) || *cp
== '-')
247 adios(NULL
, "missing argument to %s", argp
[-2]);
251 if (!(cp
= *argp
++) || *cp
== '-')
252 adios(NULL
, "missing argument to %s", argp
[-2]);
253 msgunseen
= atoi(cp
);
267 * Only interpret as a folder if we're in message mode
270 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
272 adios (NULL
, "only one folder at a time!");
273 folder
= pluspath (cp
);
275 app_msgarg(&msgs
, cp
);
279 * Here's our weird heuristic:
281 * - We allow -dump without any other arguments.
282 * - If you've given any component arguments, we don't require any
284 * - The arguments are interpreted as folders/messages _if_ we're in
285 * message mode, otherwise pass as strings in the text component.
288 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
289 adios(NULL
, "usage: [switches] [+folder] msgs | strings...");
293 * If you're picking "raw" as a mode, then you have to select
297 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
298 adios (NULL
, "You must specify a format with -form or -format when "
303 * Get new format string. Must be before chdir().
305 nfs
= new_fs (form
, format
, defformat
);
306 (void) fmt_compile(nfs
, &fmt
, 1);
312 if (compargs
.size
== 0 && msgs
.size
== 0)
317 buffer
= charstring_create(BUFSIZ
);
319 if (outputsize
== 0) {
321 outputsize
= sc_width();
323 outputsize
= INT_MAX
;
329 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
333 * If we want to provide our own formataddr, concactaddr, or tracing
334 * callback, do that now. Also, prime ismymbox if we use it.
337 if (dupaddrs
== 0 || trace
) {
342 cb
.formataddr
= test_formataddr
;
343 cb
.concataddr
= test_concataddr
;
349 struct trace_context
*ctx
;
354 ctx
->outbuf
= mh_xstrdup("");
356 cb
.trace_func
= test_trace
;
357 cb
.trace_context
= ctx
;
361 if (mode
== MESSAGE
) {
362 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
366 for (i
= 0; i
< compargs
.size
; i
+= 2) {
367 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
369 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
373 if (mode
== ADDRESS
) {
374 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
375 } else /* Fall-through for RAW or DATE */
376 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
379 charstring_free(buffer
);
387 * Process each address with fmt_scan().
393 struct pqpair
*pq_next
;
397 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
398 charstring_t buffer
, int outwidth
, int *dat
,
399 struct fmt_callbacks
*cb
)
402 char *cp
, error
[BUFSIZ
];
404 struct pqpair
*p
, *q
;
417 for (i
= 0; i
< addrs
->size
; i
++) {
418 (q
= &pq
)->pq_next
= NULL
;
419 while ((cp
= getname(addrs
->msgs
[i
]))) {
421 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
422 p
->pq_text
= mh_xstrdup(cp
);
423 p
->pq_error
= mh_xstrdup(error
);
425 p
->pq_text
= getcpy(mp
->m_text
);
428 q
= (q
->pq_next
= p
);
431 for (p
= pq
.pq_next
; p
; p
= q
) {
432 c
= fmt_findcomp("text");
435 c
->c_text
= p
->pq_text
;
438 c
= fmt_findcomp("error");
441 c
->c_text
= p
->pq_error
;
445 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
446 fputs(charstring_buffer(buffer
), stdout
);
449 mh_xfree(p
->pq_text
);
450 mh_xfree(p
->pq_error
);
458 * Process messages and run them through the format engine. A lot taken
463 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
464 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
465 int outwidth
, int files
, int *dat
,
466 struct fmt_callbacks
*cb
)
468 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
469 int num_unseen_seq
= 0;
470 ivector_t seqnum
= ivector_create (0);
476 * If 'files' is set, short-circuit everything else and just process
481 for (i
= 0; i
< msgs
->size
; i
++) {
482 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
483 admonish(cp
, "unable to open file");
486 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
494 folder
= getfolder(1);
496 maildir
= m_maildir(folder
);
498 if (chdir(maildir
) < 0)
499 adios(maildir
, "unable to change directory to");
501 if (!(mp
= folder_read(folder
, 1)))
502 adios(NULL
, "unable to read folder %s", folder
);
505 adios(NULL
, "no messages in %s", folder
);
507 for (i
= 0; i
< msgs
->size
; i
++)
508 if (!m_convert(mp
, msgs
->msgs
[i
]))
510 seq_setprev(mp
); /* set the Previous-Sequence */
512 context_replace(pfolder
, folder
); /* update current folder */
513 seq_save(mp
); /* synchronize message sequences */
514 context_save(); /* save the context file */
517 * We want to set the unseen flag if requested, so we have to check
518 * the unseen sequence as well.
522 if ((cp
= context_find(usequence
)) && *cp
) {
526 ap
= brkstring(dp
, " ", "\n");
527 for (i
= 0; ap
&& *ap
; i
++, ap
++)
528 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
535 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
536 if (is_selected(mp
, msgnum
)) {
537 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
538 admonish(cp
, "unable to open message");
548 dat
[1] = msgnum
== mp
->curmsg
;
551 * Check to see if this is in the unseen sequence
555 for (i
= 0; i
< num_unseen_seq
; i
++) {
556 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
563 * Read in the message and process the components
566 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
571 ivector_free (seqnum
);
576 * Process a single file in message mode
580 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
581 struct format
*fmt
, charstring_t buffer
, int outwidth
,
582 struct fmt_callbacks
*cb
)
585 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
586 m_getfld_state_t gstate
;
591 * Get our size if we didn't include one
597 if (fstat(fileno(in
), &st
) < 0)
604 * Initialize everything else
615 * Read in the message and process the components
618 gstate
= m_getfld_state_init(in
);
620 bufsz
= sizeof(rbuf
);
621 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
625 i
= fmt_addcomptext(name
, rbuf
);
627 while (state
== FLDPLUS
) {
628 bufsz
= sizeof(rbuf
);
629 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
630 fmt_appendcomp(i
, name
, rbuf
);
634 while (state
== FLDPLUS
) {
635 bufsz
= sizeof(rbuf
);
636 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
641 if (fmt_findcomp("body")) {
642 if ((i
= strlen(rbuf
)) < outwidth
) {
643 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
644 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
647 fmt_addcomptext("body", rbuf
);
657 m_getfld_state_destroy(&gstate
);
660 * Do this now to override any components in the original message
663 for (i
= 0; i
< comps
->size
; i
+= 2) {
664 c
= fmt_findcomp(comps
->msgs
[i
]);
667 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
671 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
672 fputs(charstring_buffer (buffer
), stdout
);
677 * Run text through the format engine with no special processing
681 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
682 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
696 c
= fmt_findcomp("text");
698 for (i
= 0; i
< text
->size
; i
++) {
701 c
->c_text
= getcpy(text
->msgs
[i
]);
704 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
705 fputs(charstring_buffer (buffer
), stdout
);
711 * Our basic tracing support callback.
713 * Print out each instruction as it's executed, including the values of
714 * the num and str registers if they've changed.
718 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
721 struct trace_context
*ctx
= (struct trace_context
*) context
;
726 if (num
!= ctx
->num
) {
727 printf("num=%d", num
);
732 if (str
!= ctx
->str
) {
743 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
748 ctx
->outbuf
= mh_xstrdup(outbuf
);
753 fmt_dump (char *nfs
, struct format
*fmth
)
757 printf("Instruction dump of format string: \n%s\n", nfs
);
760 for (fmt
= fmth
; fmt
; ++fmt
) {
762 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
768 dumpone(struct format
*fmt
)
772 if ((i
= findlabel(fmt
)) >= 0)
776 fputs(f_typestr((int)fmt
->f_type
), stdout
);
778 switch (fmt
->f_type
) {
785 litputs(fmt
->f_comp
->c_name
);
786 if (fmt
->f_comp
->c_type
)
787 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
788 if (fmt
->f_comp
->c_flags
)
789 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
817 litputs(fmt
->f_comp
->c_name
);
818 if (fmt
->f_comp
->c_type
)
819 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
820 if (fmt
->f_comp
->c_flags
)
821 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
841 litputs(fmt
->f_comp
->c_name
);
842 if (fmt
->f_comp
->c_type
)
843 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
844 if (fmt
->f_comp
->c_flags
)
845 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
849 printf(", width %d, fill '", fmt
->f_width
);
850 litputc(fmt
->f_fill
);
852 litputs(fmt
->f_comp
->c_name
);
853 if (fmt
->f_comp
->c_type
)
854 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
855 if (fmt
->f_comp
->c_flags
)
856 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
861 printf(", width %d, fill '", fmt
->f_width
);
862 litputc(fmt
->f_fill
);
868 litputs(fmt
->f_text
);
872 printf(", width %d, fill '", fmt
->f_width
);
873 litputc(fmt
->f_fill
);
875 litputs(fmt
->f_text
);
881 litputc(fmt
->f_char
);
890 printf(" continue else goto");
893 i
= findlabel(fmt
+ fmt
->f_skip
);
900 i
= findlabel(fmt
+ fmt
->f_skip
);
901 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
910 case FT_LV_MULTIPLY_L
:
913 printf(" value %d", fmt
->f_value
);
918 litputs(fmt
->f_text
);
923 litputs(fmt
->f_text
);
926 case FT_LS_DECODECOMP
:
928 litputs(fmt
->f_comp
->c_name
);
935 printf(", width %d", fmt
->f_width
);
939 printf(", value dat[%d]", fmt
->f_value
);
946 * Iterate over all instructions and assign labels to the targets of
951 initlabels(struct format
*fmth
)
953 struct format
*fmt
, *addr
;
957 for (fmt
= fmth
; fmt
; ++fmt
) {
967 addr
= fmt
+ fmt
->f_skip
;
968 if (findlabel(addr
) < 0)
971 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
978 findlabel(struct format
*addr
)
982 for (i
= 0; i
< lused
; ++i
)
989 assignlabel(struct format
*addr
)
991 if (lused
>= lallocated
) {
993 lvec
= (struct format
**)
994 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
997 lvec
[lused
++] = addr
;
1003 static char buf
[32];
1006 case FT_COMP
: return "COMP";
1007 case FT_COMPF
: return "COMPF";
1008 case FT_LIT
: return "LIT";
1009 case FT_LITF
: return "LITF";
1010 case FT_CHAR
: return "CHAR";
1011 case FT_NUM
: return "NUM";
1012 case FT_NUMF
: return "NUMF";
1013 case FT_STR
: return "STR";
1014 case FT_STRF
: return "STRF";
1015 case FT_STRFW
: return "STRFW";
1016 case FT_STRLIT
: return "STRLIT";
1017 case FT_STRLITZ
: return "STRLITZ";
1018 case FT_PUTADDR
: return "PUTADDR";
1019 case FT_LS_COMP
: return "LS_COMP";
1020 case FT_LS_LIT
: return "LS_LIT";
1021 case FT_LS_GETENV
: return "LS_GETENV";
1022 case FT_LS_CFIND
: return "LS_CFIND";
1023 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1024 case FT_LS_DECODE
: return "LS_DECODE";
1025 case FT_LS_TRIM
: return "LS_TRIM";
1026 case FT_LV_COMP
: return "LV_COMP";
1027 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1028 case FT_LV_LIT
: return "LV_LIT";
1029 case FT_LV_DAT
: return "LV_DAT";
1030 case FT_LV_STRLEN
: return "LV_STRLEN";
1031 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1032 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1033 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1034 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1035 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1036 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1037 case FT_LS_MONTH
: return "LS_MONTH";
1038 case FT_LS_LMONTH
: return "LS_LMONTH";
1039 case FT_LS_ZONE
: return "LS_ZONE";
1040 case FT_LS_DAY
: return "LS_DAY";
1041 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1042 case FT_LS_822DATE
: return "LS_822DATE";
1043 case FT_LS_PRETTY
: return "LS_PRETTY";
1044 case FT_LS_KILO
: return "LS_KILO";
1045 case FT_LS_KIBI
: return "LS_KIBI";
1046 case FT_LV_SEC
: return "LV_SEC";
1047 case FT_LV_MIN
: return "LV_MIN";
1048 case FT_LV_HOUR
: return "LV_HOUR";
1049 case FT_LV_MDAY
: return "LV_MDAY";
1050 case FT_LV_MON
: return "LV_MON";
1051 case FT_LV_YEAR
: return "LV_YEAR";
1052 case FT_LV_YDAY
: return "LV_YDAY";
1053 case FT_LV_WDAY
: return "LV_WDAY";
1054 case FT_LV_ZONE
: return "LV_ZONE";
1055 case FT_LV_CLOCK
: return "LV_CLOCK";
1056 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1057 case FT_LV_DAYF
: return "LV_DAYF";
1058 case FT_LV_DST
: return "LV_DST";
1059 case FT_LV_ZONEF
: return "LV_ZONEF";
1060 case FT_LS_PERS
: return "LS_PERS";
1061 case FT_LS_MBOX
: return "LS_MBOX";
1062 case FT_LS_HOST
: return "LS_HOST";
1063 case FT_LS_PATH
: return "LS_PATH";
1064 case FT_LS_GNAME
: return "LS_GNAME";
1065 case FT_LS_NOTE
: return "LS_NOTE";
1066 case FT_LS_ADDR
: return "LS_ADDR";
1067 case FT_LS_822ADDR
: return "LS_822ADDR";
1068 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1069 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1070 case FT_LV_INGRPF
: return "LV_INGRPF";
1071 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1072 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1073 case FT_LOCALDATE
: return "LOCALDATE";
1074 case FT_GMTDATE
: return "GMTDATE";
1075 case FT_PARSEDATE
: return "PARSEDATE";
1076 case FT_PARSEADDR
: return "PARSEADDR";
1077 case FT_FORMATADDR
: return "FORMATADDR";
1078 case FT_CONCATADDR
: return "CONCATADDR";
1079 case FT_MYMBOX
: return "MYMBOX";
1080 case FT_GETMYMBOX
: return "GETMYMBOX";
1081 case FT_GETMYADDR
: return "GETMYADDR";
1082 case FT_SAVESTR
: return "SAVESTR";
1083 case FT_DONE
: return "DONE";
1084 case FT_PAUSE
: return "PAUSE";
1085 case FT_NOP
: return "NOP";
1086 case FT_GOTO
: return "GOTO";
1087 case FT_IF_S_NULL
: return "IF_S_NULL";
1088 case FT_IF_S
: return "IF_S";
1089 case FT_IF_V_EQ
: return "IF_V_EQ";
1090 case FT_IF_V_NE
: return "IF_V_NE";
1091 case FT_IF_V_GT
: return "IF_V_GT";
1092 case FT_IF_MATCH
: return "IF_MATCH";
1093 case FT_IF_AMATCH
: return "IF_AMATCH";
1094 case FT_S_NULL
: return "S_NULL";
1095 case FT_S_NONNULL
: return "S_NONNULL";
1096 case FT_V_EQ
: return "V_EQ";
1097 case FT_V_NE
: return "V_NE";
1098 case FT_V_GT
: return "V_GT";
1099 case FT_V_MATCH
: return "V_MATCH";
1100 case FT_V_AMATCH
: return "V_AMATCH";
1102 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1110 static char buf
[64];
1112 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1119 static char buf
[64];
1121 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1126 litputs(const char *s
)
1134 fputs("<nil>", stdout
);
1141 printf("\\x%02x", (unsigned char) c
);
1142 } else if (c
< 0x20 || c
== 0177) {
1146 } else if (c
== '\f') {
1149 } else if (c
== '\n') {
1152 } else if (c
== '\r') {
1155 } else if (c
== '\t') {
1160 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1167 * Routines/code to support the duplicate address suppression code, adapted
1171 static char *buf
; /* our current working buffer */
1172 static char *bufend
; /* end of working buffer */
1173 static char *last_dst
; /* buf ptr at end of last call */
1174 static unsigned int bufsiz
=0; /* current size of buf */
1176 #define BUFINCR 512 /* how much to expand buf when if fills */
1178 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1181 * check if there's enough room in buf for str.
1182 * add more mem if needed
1184 #define CHECKMEM(str) \
1185 if ((len = strlen (str)) >= bufend - dst) {\
1187 int n = last_dst - buf;\
1188 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1189 buf = mh_xrealloc (buf, bufsiz);\
1191 last_dst = buf + n;\
1192 bufend = buf + bufsiz;\
1197 * These are versions of similar routines from replsbr.c; the purpose is
1198 * to suppress duplicate addresses from being added to a list when building
1199 * up addresses for the %(formataddr) format function. This is used by
1200 * repl to prevent duplicate addresses from being added to the "to" line.
1201 * See replsbr.c for more information.
1203 * We can't use the functions in replsbr.c directly because they are slightly
1204 * different and depend on the rest of replsbr.c
1207 test_formataddr (char *orig
, char *str
)
1215 struct mailname
*mp
= NULL
;
1217 /* if we don't have a buffer yet, get one */
1219 buf
= mh_xmalloc (BUFINCR
);
1220 last_dst
= buf
; /* XXX */
1221 bufsiz
= BUFINCR
- 6; /* leave some slop */
1222 bufend
= buf
+ bufsiz
;
1225 * If "orig" points to our buffer we can just pick up where we
1226 * left off. Otherwise we have to copy orig into our buffer.
1230 else if (!orig
|| !*orig
) {
1234 dst
= last_dst
; /* XXX */
1239 /* concatenate all the new addresses onto 'buf' */
1240 for (isgroup
= 0; (cp
= getname (str
)); ) {
1241 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1242 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1245 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1250 /* if we get here we're going to add an address */
1256 CHECKMEM (mp
->m_gname
);
1260 sp
= adrformat (mp
);
1276 * The companion to test_formataddr(); it behaves the same way, except doesn't
1277 * do duplicate address detection.
1280 test_concataddr(char *orig
, char *str
)
1285 cp
= test_formataddr(orig
, str
);
1291 insert (struct mailname
*np
)
1293 struct mailname
*mp
;
1298 if (np
->m_mbox
== NULL
)
1301 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1302 if (!strcasecmp (FENDNULL(np
->m_host
),
1303 FENDNULL(mp
->m_next
->m_host
)) &&
1304 !strcasecmp (FENDNULL(np
->m_mbox
),
1305 FENDNULL(mp
->m_next
->m_mbox
)))
1308 if (!ccme
&& ismymbox (np
))
1317 * Reset our duplicate address list
1323 struct mailname
*mp
, *mp2
;
1325 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {