]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
0d20e79a7204ed8d81cfb8fcc3fce04206365d1f
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/context_find.h"
10 #include "sbr/brkstring.h"
11 #include "sbr/ambigsw.h"
13 #include "sbr/print_version.h"
14 #include "sbr/print_help.h"
15 #include "sbr/seq_getnum.h"
16 #include "sbr/error.h"
17 #include "h/fmt_scan.h"
18 #include "h/fmt_compile.h"
20 #include "h/scansbr.h"
21 #include "h/addrsbr.h"
23 #include "sbr/m_maildir.h"
24 #include "sbr/terminal.h"
26 #define FMTTEST_SWITCHES \
27 X("form formatfile", 0, FORMSW) \
28 X("format string", 5, FMTSW) \
29 X("address", 0, ADDRSW) \
31 X("date", 0, DATESW) \
32 X("message", 0, MESSAGESW) \
33 X("file", 0, FILESW) \
34 X("nofile", 0, NFILESW) \
35 X("-component-name component-text", 0, OTHERSW) \
36 X("dupaddrs", 0, DUPADDRSW) \
37 X("nodupaddrs", 0, NDUPADDRSW) \
38 X("ccme", 0, CCMESW) \
39 X("noccme", 0, NCCMESW) \
40 X("outsize size-in-characters", 0, OUTSIZESW) \
41 X("width column-width", 0, WIDTHSW) \
42 X("msgnum number", 0, MSGNUMSW) \
43 X("msgcur flag", 0, MSGCURSW) \
44 X("msgsize size", 0, MSGSIZESW) \
45 X("unseen flag", 0, UNSEENSW) \
46 X("dump", 0, DUMPSW) \
47 X("nodump", 0, NDUMPSW) \
48 X("trace", 0, TRACESW) \
49 X("notrace", 0, NTRACESW) \
50 X("version", 0, VERSIONSW) \
51 X("help", 0, HELPSW) \
53 #define X(sw, minchars, id) id,
54 DEFINE_SWITCH_ENUM(FMTTEST
);
57 #define X(sw, minchars, id) { sw, minchars, id },
58 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
62 * An array containing labels used for branch instructions
65 static struct format
**lvec
= NULL
;
67 static int lallocated
= 0;
69 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
70 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
71 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
74 * Context structure used by the tracing routines
77 struct trace_context
{
86 static void fmt_dump (char *, struct format
*);
87 static void dumpone(struct format
*);
88 static void initlabels(struct format
*);
89 static int findlabel(struct format
*);
90 static void assignlabel(struct format
*);
91 static char *f_typestr(int);
92 static char *c_typestr(int);
93 static char *c_flagsstr(int);
94 static void litputs(const char *);
95 static void litputc(char);
96 static void process_addresses(struct format
*, struct msgs_array
*,
97 charstring_t
, int, int *,
98 struct fmt_callbacks
*);
99 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
100 int, int *, struct fmt_callbacks
*);
101 static void process_messages(struct format
*, struct msgs_array
*,
102 struct msgs_array
*, charstring_t
, char *, int,
103 int, int *, struct fmt_callbacks
*);
104 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
105 struct format
*, charstring_t
, int,
106 struct fmt_callbacks
*);
107 static void test_trace(void *, struct format
*, int, char *, const char *);
108 static char *test_formataddr(char *, char *);
109 static char *test_concataddr(char *, char *);
110 static int insert(struct mailname
*);
111 static void mlistfree(void);
113 static bool nodupcheck
; /* If set, no check for duplicates */
114 static bool ccme
; /* Should I cc myself? */
115 static struct mailname mq
; /* Mail addresses to check for duplicates */
116 static char *dummy
= "dummy";
119 main (int argc
, char **argv
)
121 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
122 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
126 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
130 bool dupaddrs
= true;
133 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
134 enum mode_t mode
= MESSAGE
;
136 struct fmt_callbacks cb
, *cbp
= NULL
;
138 if (nmh_init(argv
[0], true, true)) { return 1; }
140 arguments
= getarguments (invo_name
, argc
, argv
, 1);
143 while ((cp
= *argp
++)) {
146 * A -- means that we have a component name (like pick);
147 * save the component name and the next argument for the text.
151 die("missing component name after --");
152 app_msgarg(&compargs
, cp
);
153 /* Grab next argument for component text */
155 die("missing argument to %s", argp
[-2]);
156 app_msgarg(&compargs
, cp
);
159 switch (smatch (cp
, switches
)) {
161 ambigsw (cp
, switches
);
164 die("-%s unknown", cp
);
167 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
168 print_help (buf
, switches
, 1);
171 print_version(invo_name
);
174 die("internal argument error!");
178 if (!(cp
= *argp
++) || *cp
== '-')
179 die("missing argument to %s", argp
[-2]);
180 if (strcmp(cp
, "max") == 0)
181 outputsize
= INT_MAX
;
182 else if (strcmp(cp
, "width") == 0)
183 outputsize
= sc_width();
185 outputsize
= atoi(cp
);
189 if (!(form
= *argp
++) || *form
== '-')
190 die("missing argument to %s", argp
[-2]);
194 if (!(format
= *argp
++) || *format
== '-')
195 die("missing argument to %s", argp
[-2]);
208 defformat
= DEFADDRFORMAT
;
220 defformat
= DEFDATEFORMAT
;
245 if (!(cp
= *argp
++) || *cp
== '-')
246 die("missing argument to %s", argp
[-2]);
250 if (!(cp
= *argp
++) || *cp
== '-')
251 die("missing argument to %s", argp
[-2]);
255 if (!(cp
= *argp
++) || *cp
== '-')
256 die("missing argument to %s", argp
[-2]);
260 if (!(cp
= *argp
++) || *cp
== '-')
261 die("missing argument to %s", argp
[-2]);
265 if (!(cp
= *argp
++) || *cp
== '-')
266 die("missing argument to %s", argp
[-2]);
267 msgunseen
= atoi(cp
);
281 * Only interpret as a folder if we're in message mode
284 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
286 die("only one folder at a time!");
287 folder
= pluspath (cp
);
289 app_msgarg(&msgs
, cp
);
293 * Here's our weird heuristic:
295 * - We allow -dump without any other arguments.
296 * - If you've given any component arguments, we don't require any
298 * - The arguments are interpreted as folders/messages _if_ we're in
299 * message mode, otherwise pass as strings in the text component.
302 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
303 die("usage: [switches] [+folder] msgs | strings...");
307 * If you're picking "raw" as a mode, then you have to select
311 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
312 die("You must specify a format with -form or -format when "
317 * Get new format string. Must be before chdir().
319 nfs
= new_fs (form
, format
, defformat
);
320 (void) fmt_compile(nfs
, &fmt
, 1);
326 if (compargs
.size
== 0 && msgs
.size
== 0)
331 buffer
= charstring_create(BUFSIZ
);
333 if (outputsize
== 0) {
335 outputsize
= sc_width();
337 outputsize
= INT_MAX
;
343 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
347 * If we want to provide our own formataddr, concactaddr, or tracing
348 * callback, do that now. Also, prime ismymbox if we use it.
351 if (!dupaddrs
|| trace
) {
356 cb
.formataddr
= test_formataddr
;
357 cb
.concataddr
= test_concataddr
;
363 struct trace_context
*ctx
;
368 ctx
->outbuf
= mh_xstrdup("");
370 cb
.trace_func
= test_trace
;
371 cb
.trace_context
= ctx
;
375 if (mode
== MESSAGE
) {
376 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
380 for (i
= 0; i
< compargs
.size
; i
+= 2) {
381 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
383 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
387 if (mode
== ADDRESS
) {
388 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
389 } else /* Fall-through for RAW or DATE */
390 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
393 charstring_free(buffer
);
401 * Process each address with fmt_scan().
407 struct pqpair
*pq_next
;
411 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
412 charstring_t buffer
, int outwidth
, int *dat
,
413 struct fmt_callbacks
*cb
)
416 char *cp
, error
[BUFSIZ
];
418 struct pqpair
*p
, *q
;
431 for (i
= 0; i
< addrs
->size
; i
++) {
432 (q
= &pq
)->pq_next
= NULL
;
433 while ((cp
= getname(addrs
->msgs
[i
]))) {
435 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
436 p
->pq_text
= mh_xstrdup(cp
);
437 p
->pq_error
= mh_xstrdup(error
);
439 p
->pq_text
= getcpy(mp
->m_text
);
442 q
= (q
->pq_next
= p
);
445 for (p
= pq
.pq_next
; p
; p
= q
) {
446 c
= fmt_findcomp("text");
449 c
->c_text
= p
->pq_text
;
452 c
= fmt_findcomp("error");
455 c
->c_text
= p
->pq_error
;
459 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
460 fputs(charstring_buffer(buffer
), stdout
);
472 * Process messages and run them through the format engine. A lot taken
477 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
478 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
479 int outwidth
, int files
, int *dat
,
480 struct fmt_callbacks
*cb
)
482 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
483 int num_unseen_seq
= 0;
484 ivector_t seqnum
= ivector_create (0);
490 * If 'files' is set, short-circuit everything else and just process
495 for (i
= 0; i
< msgs
->size
; i
++) {
496 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
497 admonish(cp
, "unable to open file");
500 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
508 folder
= getfolder(1);
510 maildir
= m_maildir(folder
);
512 if (chdir(maildir
) < 0)
513 adios(maildir
, "unable to change directory to");
515 if (!(mp
= folder_read(folder
, 1)))
516 die("unable to read folder %s", folder
);
519 die("no messages in %s", folder
);
521 for (i
= 0; i
< msgs
->size
; i
++)
522 if (!m_convert(mp
, msgs
->msgs
[i
]))
524 seq_setprev(mp
); /* set the Previous-Sequence */
526 context_replace(pfolder
, folder
); /* update current folder */
527 seq_save(mp
); /* synchronize message sequences */
528 context_save(); /* save the context file */
531 * We want to set the unseen flag if requested, so we have to check
532 * the unseen sequence as well.
536 if ((cp
= context_find(usequence
)) && *cp
) {
540 ap
= brkstring(dp
, " ", "\n");
541 for (i
= 0; ap
&& *ap
; i
++, ap
++)
542 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
549 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
550 if (is_selected(mp
, msgnum
)) {
551 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
552 admonish(cp
, "unable to open message");
562 dat
[1] = msgnum
== mp
->curmsg
;
565 * Check to see if this is in the unseen sequence
569 for (i
= 0; i
< num_unseen_seq
; i
++) {
570 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
577 * Read in the message and process the components
580 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
585 ivector_free (seqnum
);
590 * Process a single file in message mode
594 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
595 struct format
*fmt
, charstring_t buffer
, int outwidth
,
596 struct fmt_callbacks
*cb
)
599 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
600 m_getfld_state_t gstate
;
605 * Get our size if we didn't include one
611 if (fstat(fileno(in
), &st
) < 0)
618 * Initialize everything else
629 * Read in the message and process the components
632 gstate
= m_getfld_state_init(in
);
634 bufsz
= sizeof(rbuf
);
635 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
639 i
= fmt_addcomptext(name
, rbuf
);
641 while (state
== FLDPLUS
) {
642 bufsz
= sizeof(rbuf
);
643 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
644 fmt_appendcomp(i
, name
, rbuf
);
648 while (state
== FLDPLUS
) {
649 bufsz
= sizeof(rbuf
);
650 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
655 if (fmt_findcomp("body")) {
656 if ((i
= strlen(rbuf
)) < outwidth
) {
657 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
658 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
661 fmt_addcomptext("body", rbuf
);
671 m_getfld_state_destroy(&gstate
);
674 * Do this now to override any components in the original message
677 for (i
= 0; i
< comps
->size
; i
+= 2) {
678 c
= fmt_findcomp(comps
->msgs
[i
]);
681 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
685 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
686 fputs(charstring_buffer (buffer
), stdout
);
691 * Run text through the format engine with no special processing
695 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
696 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
710 c
= fmt_findcomp("text");
712 for (i
= 0; i
< text
->size
; i
++) {
715 c
->c_text
= getcpy(text
->msgs
[i
]);
718 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
719 fputs(charstring_buffer (buffer
), stdout
);
725 * Our basic tracing support callback.
727 * Print out each instruction as it's executed, including the values of
728 * the num and str registers if they've changed.
732 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
735 struct trace_context
*ctx
= (struct trace_context
*) context
;
736 bool changed
= false;
740 if (num
!= ctx
->num
) {
741 printf("num=%d", num
);
746 if (str
!= ctx
->str
) {
750 fputs("str=", stdout
);
758 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
759 fputs("outbuf=", stdout
);
763 ctx
->outbuf
= mh_xstrdup(outbuf
);
768 fmt_dump (char *nfs
, struct format
*fmth
)
772 printf("Instruction dump of format string: \n%s\n", nfs
);
775 for (fmt
= fmth
; fmt
; ++fmt
) {
777 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
783 dumpone(struct format
*fmt
)
787 if ((i
= findlabel(fmt
)) >= 0)
791 fputs(f_typestr((int)fmt
->f_type
), stdout
);
793 switch (fmt
->f_type
) {
799 fputs(", comp ", stdout
);
800 litputs(fmt
->f_comp
->c_name
);
801 if (fmt
->f_comp
->c_type
)
802 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
803 if (fmt
->f_comp
->c_flags
)
804 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
831 fputs(", c_name ", stdout
);
832 litputs(fmt
->f_comp
->c_name
);
833 if (fmt
->f_comp
->c_type
)
834 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
835 if (fmt
->f_comp
->c_flags
)
836 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
855 fputs(", c_name ", stdout
);
856 litputs(fmt
->f_comp
->c_name
);
857 if (fmt
->f_comp
->c_type
)
858 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
859 if (fmt
->f_comp
->c_flags
)
860 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
864 printf(", width %d, fill '", fmt
->f_width
);
865 litputc(fmt
->f_fill
);
866 fputs("' name ", stdout
);
867 litputs(fmt
->f_comp
->c_name
);
868 if (fmt
->f_comp
->c_type
)
869 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
870 if (fmt
->f_comp
->c_flags
)
871 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
876 printf(", width %d, fill '", fmt
->f_width
);
877 litputc(fmt
->f_fill
);
883 litputs(fmt
->f_text
);
887 printf(", width %d, fill '", fmt
->f_width
);
888 litputc(fmt
->f_fill
);
890 litputs(fmt
->f_text
);
896 litputc(fmt
->f_char
);
905 fputs(" continue else goto", stdout
);
908 i
= findlabel(fmt
+ fmt
->f_skip
);
915 i
= findlabel(fmt
+ fmt
->f_skip
);
916 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
925 case FT_LV_MULTIPLY_L
:
928 printf(" value %d", fmt
->f_value
);
932 fputs(" str ", stdout
);
933 litputs(fmt
->f_text
);
937 fputs(" getenv ", stdout
);
938 litputs(fmt
->f_text
);
941 case FT_LS_DECODECOMP
:
942 fputs(", comp ", stdout
);
943 litputs(fmt
->f_comp
->c_name
);
950 printf(", width %d", fmt
->f_width
);
954 printf(", value dat[%d]", fmt
->f_value
);
961 * Iterate over all instructions and assign labels to the targets of
966 initlabels(struct format
*fmth
)
968 struct format
*fmt
, *addr
;
972 for (fmt
= fmth
; fmt
; ++fmt
) {
982 addr
= fmt
+ fmt
->f_skip
;
983 if (findlabel(addr
) < 0)
986 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
993 findlabel(struct format
*addr
)
997 for (i
= 0; i
< lused
; ++i
)
1004 assignlabel(struct format
*addr
)
1006 if (lused
>= lallocated
) {
1008 lvec
= (struct format
**)
1009 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1012 lvec
[lused
++] = addr
;
1018 static char buf
[32];
1021 case FT_COMP
: return "COMP";
1022 case FT_COMPF
: return "COMPF";
1023 case FT_LIT
: return "LIT";
1024 case FT_LITF
: return "LITF";
1025 case FT_CHAR
: return "CHAR";
1026 case FT_NUM
: return "NUM";
1027 case FT_NUMF
: return "NUMF";
1028 case FT_STR
: return "STR";
1029 case FT_STRF
: return "STRF";
1030 case FT_STRFW
: return "STRFW";
1031 case FT_STRLIT
: return "STRLIT";
1032 case FT_STRLITZ
: return "STRLITZ";
1033 case FT_PUTADDR
: return "PUTADDR";
1034 case FT_LS_COMP
: return "LS_COMP";
1035 case FT_LS_LIT
: return "LS_LIT";
1036 case FT_LS_GETENV
: return "LS_GETENV";
1037 case FT_LS_CFIND
: return "LS_CFIND";
1038 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1039 case FT_LS_DECODE
: return "LS_DECODE";
1040 case FT_LS_TRIM
: return "LS_TRIM";
1041 case FT_LV_COMP
: return "LV_COMP";
1042 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1043 case FT_LV_LIT
: return "LV_LIT";
1044 case FT_LV_DAT
: return "LV_DAT";
1045 case FT_LV_STRLEN
: return "LV_STRLEN";
1046 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1047 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1048 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1049 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1050 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1051 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1052 case FT_LS_MONTH
: return "LS_MONTH";
1053 case FT_LS_LMONTH
: return "LS_LMONTH";
1054 case FT_LS_ZONE
: return "LS_ZONE";
1055 case FT_LS_DAY
: return "LS_DAY";
1056 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1057 case FT_LS_822DATE
: return "LS_822DATE";
1058 case FT_LS_PRETTY
: return "LS_PRETTY";
1059 case FT_LS_KILO
: return "LS_KILO";
1060 case FT_LS_KIBI
: return "LS_KIBI";
1061 case FT_LV_SEC
: return "LV_SEC";
1062 case FT_LV_MIN
: return "LV_MIN";
1063 case FT_LV_HOUR
: return "LV_HOUR";
1064 case FT_LV_MDAY
: return "LV_MDAY";
1065 case FT_LV_MON
: return "LV_MON";
1066 case FT_LV_YEAR
: return "LV_YEAR";
1067 case FT_LV_YDAY
: return "LV_YDAY";
1068 case FT_LV_WDAY
: return "LV_WDAY";
1069 case FT_LV_ZONE
: return "LV_ZONE";
1070 case FT_LV_CLOCK
: return "LV_CLOCK";
1071 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1072 case FT_LV_DAYF
: return "LV_DAYF";
1073 case FT_LV_DST
: return "LV_DST";
1074 case FT_LV_ZONEF
: return "LV_ZONEF";
1075 case FT_LS_PERS
: return "LS_PERS";
1076 case FT_LS_MBOX
: return "LS_MBOX";
1077 case FT_LS_HOST
: return "LS_HOST";
1078 case FT_LS_PATH
: return "LS_PATH";
1079 case FT_LS_GNAME
: return "LS_GNAME";
1080 case FT_LS_NOTE
: return "LS_NOTE";
1081 case FT_LS_ADDR
: return "LS_ADDR";
1082 case FT_LS_822ADDR
: return "LS_822ADDR";
1083 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1084 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1085 case FT_LV_INGRPF
: return "LV_INGRPF";
1086 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1087 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1088 case FT_LOCALDATE
: return "LOCALDATE";
1089 case FT_GMTDATE
: return "GMTDATE";
1090 case FT_PARSEDATE
: return "PARSEDATE";
1091 case FT_PARSEADDR
: return "PARSEADDR";
1092 case FT_FORMATADDR
: return "FORMATADDR";
1093 case FT_CONCATADDR
: return "CONCATADDR";
1094 case FT_MYMBOX
: return "MYMBOX";
1095 case FT_GETMYMBOX
: return "GETMYMBOX";
1096 case FT_GETMYADDR
: return "GETMYADDR";
1097 case FT_SAVESTR
: return "SAVESTR";
1098 case FT_DONE
: return "DONE";
1099 case FT_PAUSE
: return "PAUSE";
1100 case FT_NOP
: return "NOP";
1101 case FT_GOTO
: return "GOTO";
1102 case FT_IF_S_NULL
: return "IF_S_NULL";
1103 case FT_IF_S
: return "IF_S";
1104 case FT_IF_V_EQ
: return "IF_V_EQ";
1105 case FT_IF_V_NE
: return "IF_V_NE";
1106 case FT_IF_V_GT
: return "IF_V_GT";
1107 case FT_IF_MATCH
: return "IF_MATCH";
1108 case FT_IF_AMATCH
: return "IF_AMATCH";
1109 case FT_S_NULL
: return "S_NULL";
1110 case FT_S_NONNULL
: return "S_NONNULL";
1111 case FT_V_EQ
: return "V_EQ";
1112 case FT_V_NE
: return "V_NE";
1113 case FT_V_GT
: return "V_GT";
1114 case FT_V_MATCH
: return "V_MATCH";
1115 case FT_V_AMATCH
: return "V_AMATCH";
1117 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1125 static char buf
[64];
1127 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1134 static char buf
[64];
1136 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1141 litputs(const char *s
)
1149 fputs("<nil>", stdout
);
1156 printf("\\x%02x", (unsigned char) c
);
1157 } else if (c
< 0x20 || c
== 0177) {
1161 } else if (c
== '\f') {
1164 } else if (c
== '\n') {
1167 } else if (c
== '\r') {
1170 } else if (c
== '\t') {
1175 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1182 * Routines/code to support the duplicate address suppression code, adapted
1186 static char *buf
; /* our current working buffer */
1187 static char *bufend
; /* end of working buffer */
1188 static char *last_dst
; /* buf ptr at end of last call */
1189 static unsigned int bufsiz
=0; /* current size of buf */
1191 #define BUFINCR 512 /* how much to expand buf when if fills */
1193 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1196 * check if there's enough room in buf for str.
1197 * add more mem if needed
1199 #define CHECKMEM(str) \
1200 if ((len = strlen (str)) >= bufend - dst) {\
1202 int n = last_dst - buf;\
1203 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1204 buf = mh_xrealloc (buf, bufsiz);\
1206 last_dst = buf + n;\
1207 bufend = buf + bufsiz;\
1212 * These are versions of similar routines from replsbr.c; the purpose is
1213 * to suppress duplicate addresses from being added to a list when building
1214 * up addresses for the %(formataddr) format function. This is used by
1215 * repl to prevent duplicate addresses from being added to the "to" line.
1216 * See replsbr.c for more information.
1218 * We can't use the functions in replsbr.c directly because they are slightly
1219 * different and depend on the rest of replsbr.c
1222 test_formataddr (char *orig
, char *str
)
1230 struct mailname
*mp
= NULL
;
1232 /* if we don't have a buffer yet, get one */
1234 buf
= mh_xmalloc (BUFINCR
);
1235 last_dst
= buf
; /* XXX */
1236 bufsiz
= BUFINCR
- 6; /* leave some slop */
1237 bufend
= buf
+ bufsiz
;
1240 * If "orig" points to our buffer we can just pick up where we
1241 * left off. Otherwise we have to copy orig into our buffer.
1245 else if (!orig
|| !*orig
) {
1249 dst
= last_dst
; /* XXX */
1254 /* concatenate all the new addresses onto 'buf' */
1255 for (isgroup
= false; (cp
= getname (str
)); ) {
1256 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1257 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1260 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1265 /* if we get here we're going to add an address */
1271 CHECKMEM (mp
->m_gname
);
1275 sp
= adrformat (mp
);
1291 * The companion to test_formataddr(); it behaves the same way, except doesn't
1292 * do duplicate address detection.
1295 test_concataddr(char *orig
, char *str
)
1300 cp
= test_formataddr(orig
, str
);
1306 insert (struct mailname
*np
)
1308 struct mailname
*mp
;
1313 if (np
->m_mbox
== NULL
)
1316 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1317 if (!strcasecmp (FENDNULL(np
->m_host
),
1318 FENDNULL(mp
->m_next
->m_host
)) &&
1319 !strcasecmp (FENDNULL(np
->m_mbox
),
1320 FENDNULL(mp
->m_next
->m_mbox
)))
1323 if (!ccme
&& ismymbox (np
))
1332 * Reset our duplicate address list
1338 struct mailname
*mp
, *mp2
;
1340 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {