]>
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 "sbr/folder_read.h"
10 #include "sbr/folder_free.h"
11 #include "sbr/context_save.h"
12 #include "sbr/context_replace.h"
13 #include "sbr/context_find.h"
14 #include "sbr/brkstring.h"
15 #include "sbr/ambigsw.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/seq_getnum.h"
20 #include "sbr/error.h"
21 #include "h/fmt_scan.h"
22 #include "h/fmt_compile.h"
24 #include "h/scansbr.h"
25 #include "h/addrsbr.h"
27 #include "sbr/m_maildir.h"
28 #include "sbr/terminal.h"
30 #define FMTTEST_SWITCHES \
31 X("form formatfile", 0, FORMSW) \
32 X("format string", 5, FMTSW) \
33 X("address", 0, ADDRSW) \
35 X("date", 0, DATESW) \
36 X("message", 0, MESSAGESW) \
37 X("file", 0, FILESW) \
38 X("nofile", 0, NFILESW) \
39 X("-component-name component-text", 0, OTHERSW) \
40 X("dupaddrs", 0, DUPADDRSW) \
41 X("nodupaddrs", 0, NDUPADDRSW) \
42 X("ccme", 0, CCMESW) \
43 X("noccme", 0, NCCMESW) \
44 X("outsize size-in-characters", 0, OUTSIZESW) \
45 X("width column-width", 0, WIDTHSW) \
46 X("msgnum number", 0, MSGNUMSW) \
47 X("msgcur flag", 0, MSGCURSW) \
48 X("msgsize size", 0, MSGSIZESW) \
49 X("unseen flag", 0, UNSEENSW) \
50 X("dump", 0, DUMPSW) \
51 X("nodump", 0, NDUMPSW) \
52 X("trace", 0, TRACESW) \
53 X("notrace", 0, NTRACESW) \
54 X("version", 0, VERSIONSW) \
55 X("help", 0, HELPSW) \
57 #define X(sw, minchars, id) id,
58 DEFINE_SWITCH_ENUM(FMTTEST
);
61 #define X(sw, minchars, id) { sw, minchars, id },
62 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
66 * An array containing labels used for branch instructions
69 static struct format
**lvec
= NULL
;
71 static int lallocated
= 0;
73 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
74 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
75 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
78 * Context structure used by the tracing routines
81 struct trace_context
{
90 static void fmt_dump (char *, struct format
*);
91 static void dumpone(struct format
*);
92 static void initlabels(struct format
*);
93 static int findlabel(struct format
*);
94 static void assignlabel(struct format
*);
95 static char *f_typestr(int);
96 static char *c_typestr(int);
97 static char *c_flagsstr(int);
98 static void litputs(const char *);
99 static void litputc(char);
100 static void process_addresses(struct format
*, struct msgs_array
*,
101 charstring_t
, int, int *,
102 struct fmt_callbacks
*);
103 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
104 int, int *, struct fmt_callbacks
*);
105 static void process_messages(struct format
*, struct msgs_array
*,
106 struct msgs_array
*, charstring_t
, char *, int,
107 int, int *, struct fmt_callbacks
*);
108 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
109 struct format
*, charstring_t
, int,
110 struct fmt_callbacks
*);
111 static void test_trace(void *, struct format
*, int, char *, const char *);
112 static char *test_formataddr(char *, char *);
113 static char *test_concataddr(char *, char *);
114 static int insert(struct mailname
*);
115 static void mlistfree(void);
117 static bool nodupcheck
; /* If set, no check for duplicates */
118 static bool ccme
; /* Should I cc myself? */
119 static struct mailname mq
; /* Mail addresses to check for duplicates */
120 static char *dummy
= "dummy";
123 main (int argc
, char **argv
)
125 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
126 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
130 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
134 bool dupaddrs
= true;
137 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
138 enum mode_t mode
= MESSAGE
;
140 struct fmt_callbacks cb
, *cbp
= NULL
;
142 if (nmh_init(argv
[0], true, true)) { return 1; }
144 arguments
= getarguments (invo_name
, argc
, argv
, 1);
147 while ((cp
= *argp
++)) {
150 * A -- means that we have a component name (like pick);
151 * save the component name and the next argument for the text.
155 die("missing component name after --");
156 app_msgarg(&compargs
, cp
);
157 /* Grab next argument for component text */
159 die("missing argument to %s", argp
[-2]);
160 app_msgarg(&compargs
, cp
);
163 switch (smatch (cp
, switches
)) {
165 ambigsw (cp
, switches
);
168 die("-%s unknown", cp
);
171 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
172 print_help (buf
, switches
, 1);
175 print_version(invo_name
);
178 die("internal argument error!");
182 if (!(cp
= *argp
++) || *cp
== '-')
183 die("missing argument to %s", argp
[-2]);
184 if (strcmp(cp
, "max") == 0)
185 outputsize
= INT_MAX
;
186 else if (strcmp(cp
, "width") == 0)
187 outputsize
= sc_width();
189 outputsize
= atoi(cp
);
193 if (!(form
= *argp
++) || *form
== '-')
194 die("missing argument to %s", argp
[-2]);
198 if (!(format
= *argp
++) || *format
== '-')
199 die("missing argument to %s", argp
[-2]);
212 defformat
= DEFADDRFORMAT
;
224 defformat
= DEFDATEFORMAT
;
249 if (!(cp
= *argp
++) || *cp
== '-')
250 die("missing argument to %s", argp
[-2]);
254 if (!(cp
= *argp
++) || *cp
== '-')
255 die("missing argument to %s", argp
[-2]);
259 if (!(cp
= *argp
++) || *cp
== '-')
260 die("missing argument to %s", argp
[-2]);
264 if (!(cp
= *argp
++) || *cp
== '-')
265 die("missing argument to %s", argp
[-2]);
269 if (!(cp
= *argp
++) || *cp
== '-')
270 die("missing argument to %s", argp
[-2]);
271 msgunseen
= atoi(cp
);
285 * Only interpret as a folder if we're in message mode
288 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
290 die("only one folder at a time!");
291 folder
= pluspath (cp
);
293 app_msgarg(&msgs
, cp
);
297 * Here's our weird heuristic:
299 * - We allow -dump without any other arguments.
300 * - If you've given any component arguments, we don't require any
302 * - The arguments are interpreted as folders/messages _if_ we're in
303 * message mode, otherwise pass as strings in the text component.
306 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
307 die("usage: [switches] [+folder] msgs | strings...");
311 * If you're picking "raw" as a mode, then you have to select
315 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
316 die("You must specify a format with -form or -format when "
321 * Get new format string. Must be before chdir().
323 nfs
= new_fs (form
, format
, defformat
);
324 (void) fmt_compile(nfs
, &fmt
, 1);
330 if (compargs
.size
== 0 && msgs
.size
== 0)
335 buffer
= charstring_create(BUFSIZ
);
337 if (outputsize
== 0) {
339 outputsize
= sc_width();
341 outputsize
= INT_MAX
;
347 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
351 * If we want to provide our own formataddr, concactaddr, or tracing
352 * callback, do that now. Also, prime ismymbox if we use it.
355 if (!dupaddrs
|| trace
) {
360 cb
.formataddr
= test_formataddr
;
361 cb
.concataddr
= test_concataddr
;
367 struct trace_context
*ctx
;
372 ctx
->outbuf
= mh_xstrdup("");
374 cb
.trace_func
= test_trace
;
375 cb
.trace_context
= ctx
;
379 if (mode
== MESSAGE
) {
380 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
384 for (i
= 0; i
< compargs
.size
; i
+= 2) {
385 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
387 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
391 if (mode
== ADDRESS
) {
392 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
393 } else /* Fall-through for RAW or DATE */
394 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
397 charstring_free(buffer
);
405 * Process each address with fmt_scan().
411 struct pqpair
*pq_next
;
415 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
416 charstring_t buffer
, int outwidth
, int *dat
,
417 struct fmt_callbacks
*cb
)
420 char *cp
, error
[BUFSIZ
];
422 struct pqpair
*p
, *q
;
435 for (i
= 0; i
< addrs
->size
; i
++) {
436 (q
= &pq
)->pq_next
= NULL
;
437 while ((cp
= getname(addrs
->msgs
[i
]))) {
439 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
440 p
->pq_text
= mh_xstrdup(cp
);
441 p
->pq_error
= mh_xstrdup(error
);
443 p
->pq_text
= getcpy(mp
->m_text
);
446 q
= (q
->pq_next
= p
);
449 for (p
= pq
.pq_next
; p
; p
= q
) {
450 c
= fmt_findcomp("text");
453 c
->c_text
= p
->pq_text
;
456 c
= fmt_findcomp("error");
459 c
->c_text
= p
->pq_error
;
463 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
464 fputs(charstring_buffer(buffer
), stdout
);
476 * Process messages and run them through the format engine. A lot taken
481 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
482 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
483 int outwidth
, int files
, int *dat
,
484 struct fmt_callbacks
*cb
)
486 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
487 int num_unseen_seq
= 0;
488 ivector_t seqnum
= ivector_create (0);
494 * If 'files' is set, short-circuit everything else and just process
499 for (i
= 0; i
< msgs
->size
; i
++) {
500 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
501 admonish(cp
, "unable to open file");
504 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
512 folder
= getfolder(1);
514 maildir
= m_maildir(folder
);
516 if (chdir(maildir
) < 0)
517 adios(maildir
, "unable to change directory to");
519 if (!(mp
= folder_read(folder
, 1)))
520 die("unable to read folder %s", folder
);
523 die("no messages in %s", folder
);
525 for (i
= 0; i
< msgs
->size
; i
++)
526 if (!m_convert(mp
, msgs
->msgs
[i
]))
528 seq_setprev(mp
); /* set the Previous-Sequence */
530 context_replace(pfolder
, folder
); /* update current folder */
531 seq_save(mp
); /* synchronize message sequences */
532 context_save(); /* save the context file */
535 * We want to set the unseen flag if requested, so we have to check
536 * the unseen sequence as well.
540 if ((cp
= context_find(usequence
)) && *cp
) {
544 ap
= brkstring(dp
, " ", "\n");
545 for (i
= 0; ap
&& *ap
; i
++, ap
++)
546 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
553 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
554 if (is_selected(mp
, msgnum
)) {
555 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
556 admonish(cp
, "unable to open message");
566 dat
[1] = msgnum
== mp
->curmsg
;
569 * Check to see if this is in the unseen sequence
573 for (i
= 0; i
< num_unseen_seq
; i
++) {
574 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
581 * Read in the message and process the components
584 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
589 ivector_free (seqnum
);
594 * Process a single file in message mode
598 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
599 struct format
*fmt
, charstring_t buffer
, int outwidth
,
600 struct fmt_callbacks
*cb
)
603 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
604 m_getfld_state_t gstate
;
609 * Get our size if we didn't include one
615 if (fstat(fileno(in
), &st
) < 0)
622 * Initialize everything else
633 * Read in the message and process the components
636 gstate
= m_getfld_state_init(in
);
638 bufsz
= sizeof(rbuf
);
639 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
643 i
= fmt_addcomptext(name
, rbuf
);
645 while (state
== FLDPLUS
) {
646 bufsz
= sizeof(rbuf
);
647 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
648 fmt_appendcomp(i
, name
, rbuf
);
652 while (state
== FLDPLUS
) {
653 bufsz
= sizeof(rbuf
);
654 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
659 if (fmt_findcomp("body")) {
660 if ((i
= strlen(rbuf
)) < outwidth
) {
661 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
662 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
665 fmt_addcomptext("body", rbuf
);
675 m_getfld_state_destroy(&gstate
);
678 * Do this now to override any components in the original message
681 for (i
= 0; i
< comps
->size
; i
+= 2) {
682 c
= fmt_findcomp(comps
->msgs
[i
]);
685 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
689 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
690 fputs(charstring_buffer (buffer
), stdout
);
695 * Run text through the format engine with no special processing
699 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
700 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
714 c
= fmt_findcomp("text");
716 for (i
= 0; i
< text
->size
; i
++) {
719 c
->c_text
= getcpy(text
->msgs
[i
]);
722 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
723 fputs(charstring_buffer (buffer
), stdout
);
729 * Our basic tracing support callback.
731 * Print out each instruction as it's executed, including the values of
732 * the num and str registers if they've changed.
736 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
739 struct trace_context
*ctx
= (struct trace_context
*) context
;
740 bool changed
= false;
744 if (num
!= ctx
->num
) {
745 printf("num=%d", num
);
750 if (str
!= ctx
->str
) {
754 fputs("str=", stdout
);
762 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
763 fputs("outbuf=", stdout
);
767 ctx
->outbuf
= mh_xstrdup(outbuf
);
772 fmt_dump (char *nfs
, struct format
*fmth
)
776 printf("Instruction dump of format string: \n%s\n", nfs
);
779 for (fmt
= fmth
; fmt
; ++fmt
) {
781 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
787 dumpone(struct format
*fmt
)
791 if ((i
= findlabel(fmt
)) >= 0)
795 fputs(f_typestr((int)fmt
->f_type
), stdout
);
797 switch (fmt
->f_type
) {
803 fputs(", comp ", stdout
);
804 litputs(fmt
->f_comp
->c_name
);
805 if (fmt
->f_comp
->c_type
)
806 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
807 if (fmt
->f_comp
->c_flags
)
808 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
835 fputs(", c_name ", stdout
);
836 litputs(fmt
->f_comp
->c_name
);
837 if (fmt
->f_comp
->c_type
)
838 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
839 if (fmt
->f_comp
->c_flags
)
840 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
859 fputs(", c_name ", stdout
);
860 litputs(fmt
->f_comp
->c_name
);
861 if (fmt
->f_comp
->c_type
)
862 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
863 if (fmt
->f_comp
->c_flags
)
864 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
868 printf(", width %d, fill '", fmt
->f_width
);
869 litputc(fmt
->f_fill
);
870 fputs("' name ", stdout
);
871 litputs(fmt
->f_comp
->c_name
);
872 if (fmt
->f_comp
->c_type
)
873 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
874 if (fmt
->f_comp
->c_flags
)
875 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
880 printf(", width %d, fill '", fmt
->f_width
);
881 litputc(fmt
->f_fill
);
887 litputs(fmt
->f_text
);
891 printf(", width %d, fill '", fmt
->f_width
);
892 litputc(fmt
->f_fill
);
894 litputs(fmt
->f_text
);
900 litputc(fmt
->f_char
);
909 fputs(" continue else goto", stdout
);
912 i
= findlabel(fmt
+ fmt
->f_skip
);
919 i
= findlabel(fmt
+ fmt
->f_skip
);
920 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
929 case FT_LV_MULTIPLY_L
:
932 printf(" value %d", fmt
->f_value
);
936 fputs(" str ", stdout
);
937 litputs(fmt
->f_text
);
941 fputs(" getenv ", stdout
);
942 litputs(fmt
->f_text
);
945 case FT_LS_DECODECOMP
:
946 fputs(", comp ", stdout
);
947 litputs(fmt
->f_comp
->c_name
);
954 printf(", width %d", fmt
->f_width
);
958 printf(", value dat[%d]", fmt
->f_value
);
965 * Iterate over all instructions and assign labels to the targets of
970 initlabels(struct format
*fmth
)
972 struct format
*fmt
, *addr
;
976 for (fmt
= fmth
; fmt
; ++fmt
) {
986 addr
= fmt
+ fmt
->f_skip
;
987 if (findlabel(addr
) < 0)
990 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
997 findlabel(struct format
*addr
)
1001 for (i
= 0; i
< lused
; ++i
)
1002 if (addr
== lvec
[i
])
1008 assignlabel(struct format
*addr
)
1010 if (lused
>= lallocated
) {
1012 lvec
= (struct format
**)
1013 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1016 lvec
[lused
++] = addr
;
1022 static char buf
[32];
1025 case FT_COMP
: return "COMP";
1026 case FT_COMPF
: return "COMPF";
1027 case FT_LIT
: return "LIT";
1028 case FT_LITF
: return "LITF";
1029 case FT_CHAR
: return "CHAR";
1030 case FT_NUM
: return "NUM";
1031 case FT_NUMF
: return "NUMF";
1032 case FT_STR
: return "STR";
1033 case FT_STRF
: return "STRF";
1034 case FT_STRFW
: return "STRFW";
1035 case FT_STRLIT
: return "STRLIT";
1036 case FT_STRLITZ
: return "STRLITZ";
1037 case FT_PUTADDR
: return "PUTADDR";
1038 case FT_LS_COMP
: return "LS_COMP";
1039 case FT_LS_LIT
: return "LS_LIT";
1040 case FT_LS_GETENV
: return "LS_GETENV";
1041 case FT_LS_CFIND
: return "LS_CFIND";
1042 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1043 case FT_LS_DECODE
: return "LS_DECODE";
1044 case FT_LS_TRIM
: return "LS_TRIM";
1045 case FT_LV_COMP
: return "LV_COMP";
1046 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1047 case FT_LV_LIT
: return "LV_LIT";
1048 case FT_LV_DAT
: return "LV_DAT";
1049 case FT_LV_STRLEN
: return "LV_STRLEN";
1050 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1051 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1052 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1053 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1054 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1055 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1056 case FT_LS_MONTH
: return "LS_MONTH";
1057 case FT_LS_LMONTH
: return "LS_LMONTH";
1058 case FT_LS_ZONE
: return "LS_ZONE";
1059 case FT_LS_DAY
: return "LS_DAY";
1060 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1061 case FT_LS_822DATE
: return "LS_822DATE";
1062 case FT_LS_PRETTY
: return "LS_PRETTY";
1063 case FT_LS_KILO
: return "LS_KILO";
1064 case FT_LS_KIBI
: return "LS_KIBI";
1065 case FT_LV_SEC
: return "LV_SEC";
1066 case FT_LV_MIN
: return "LV_MIN";
1067 case FT_LV_HOUR
: return "LV_HOUR";
1068 case FT_LV_MDAY
: return "LV_MDAY";
1069 case FT_LV_MON
: return "LV_MON";
1070 case FT_LV_YEAR
: return "LV_YEAR";
1071 case FT_LV_YDAY
: return "LV_YDAY";
1072 case FT_LV_WDAY
: return "LV_WDAY";
1073 case FT_LV_ZONE
: return "LV_ZONE";
1074 case FT_LV_CLOCK
: return "LV_CLOCK";
1075 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1076 case FT_LV_DAYF
: return "LV_DAYF";
1077 case FT_LV_DST
: return "LV_DST";
1078 case FT_LV_ZONEF
: return "LV_ZONEF";
1079 case FT_LS_PERS
: return "LS_PERS";
1080 case FT_LS_MBOX
: return "LS_MBOX";
1081 case FT_LS_HOST
: return "LS_HOST";
1082 case FT_LS_PATH
: return "LS_PATH";
1083 case FT_LS_GNAME
: return "LS_GNAME";
1084 case FT_LS_NOTE
: return "LS_NOTE";
1085 case FT_LS_ADDR
: return "LS_ADDR";
1086 case FT_LS_822ADDR
: return "LS_822ADDR";
1087 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1088 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1089 case FT_LV_INGRPF
: return "LV_INGRPF";
1090 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1091 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1092 case FT_LOCALDATE
: return "LOCALDATE";
1093 case FT_GMTDATE
: return "GMTDATE";
1094 case FT_PARSEDATE
: return "PARSEDATE";
1095 case FT_PARSEADDR
: return "PARSEADDR";
1096 case FT_FORMATADDR
: return "FORMATADDR";
1097 case FT_CONCATADDR
: return "CONCATADDR";
1098 case FT_MYMBOX
: return "MYMBOX";
1099 case FT_GETMYMBOX
: return "GETMYMBOX";
1100 case FT_GETMYADDR
: return "GETMYADDR";
1101 case FT_SAVESTR
: return "SAVESTR";
1102 case FT_DONE
: return "DONE";
1103 case FT_PAUSE
: return "PAUSE";
1104 case FT_NOP
: return "NOP";
1105 case FT_GOTO
: return "GOTO";
1106 case FT_IF_S_NULL
: return "IF_S_NULL";
1107 case FT_IF_S
: return "IF_S";
1108 case FT_IF_V_EQ
: return "IF_V_EQ";
1109 case FT_IF_V_NE
: return "IF_V_NE";
1110 case FT_IF_V_GT
: return "IF_V_GT";
1111 case FT_IF_MATCH
: return "IF_MATCH";
1112 case FT_IF_AMATCH
: return "IF_AMATCH";
1113 case FT_S_NULL
: return "S_NULL";
1114 case FT_S_NONNULL
: return "S_NONNULL";
1115 case FT_V_EQ
: return "V_EQ";
1116 case FT_V_NE
: return "V_NE";
1117 case FT_V_GT
: return "V_GT";
1118 case FT_V_MATCH
: return "V_MATCH";
1119 case FT_V_AMATCH
: return "V_AMATCH";
1121 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1129 static char buf
[64];
1131 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1138 static char buf
[64];
1140 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1145 litputs(const char *s
)
1153 fputs("<nil>", stdout
);
1160 printf("\\x%02x", (unsigned char) c
);
1161 } else if (c
< 0x20 || c
== 0177) {
1165 } else if (c
== '\f') {
1168 } else if (c
== '\n') {
1171 } else if (c
== '\r') {
1174 } else if (c
== '\t') {
1179 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1186 * Routines/code to support the duplicate address suppression code, adapted
1190 static char *buf
; /* our current working buffer */
1191 static char *bufend
; /* end of working buffer */
1192 static char *last_dst
; /* buf ptr at end of last call */
1193 static unsigned int bufsiz
=0; /* current size of buf */
1195 #define BUFINCR 512 /* how much to expand buf when if fills */
1197 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1200 * check if there's enough room in buf for str.
1201 * add more mem if needed
1203 #define CHECKMEM(str) \
1204 if ((len = strlen (str)) >= bufend - dst) {\
1206 int n = last_dst - buf;\
1207 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1208 buf = mh_xrealloc (buf, bufsiz);\
1210 last_dst = buf + n;\
1211 bufend = buf + bufsiz;\
1216 * These are versions of similar routines from replsbr.c; the purpose is
1217 * to suppress duplicate addresses from being added to a list when building
1218 * up addresses for the %(formataddr) format function. This is used by
1219 * repl to prevent duplicate addresses from being added to the "to" line.
1220 * See replsbr.c for more information.
1222 * We can't use the functions in replsbr.c directly because they are slightly
1223 * different and depend on the rest of replsbr.c
1226 test_formataddr (char *orig
, char *str
)
1234 struct mailname
*mp
= NULL
;
1236 /* if we don't have a buffer yet, get one */
1238 buf
= mh_xmalloc (BUFINCR
);
1239 last_dst
= buf
; /* XXX */
1240 bufsiz
= BUFINCR
- 6; /* leave some slop */
1241 bufend
= buf
+ bufsiz
;
1244 * If "orig" points to our buffer we can just pick up where we
1245 * left off. Otherwise we have to copy orig into our buffer.
1249 else if (!orig
|| !*orig
) {
1253 dst
= last_dst
; /* XXX */
1258 /* concatenate all the new addresses onto 'buf' */
1259 for (isgroup
= false; (cp
= getname (str
)); ) {
1260 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1261 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1264 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1269 /* if we get here we're going to add an address */
1275 CHECKMEM (mp
->m_gname
);
1279 sp
= adrformat (mp
);
1295 * The companion to test_formataddr(); it behaves the same way, except doesn't
1296 * do duplicate address detection.
1299 test_concataddr(char *orig
, char *str
)
1304 cp
= test_formataddr(orig
, str
);
1310 insert (struct mailname
*np
)
1312 struct mailname
*mp
;
1317 if (np
->m_mbox
== NULL
)
1320 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1321 if (!strcasecmp (FENDNULL(np
->m_host
),
1322 FENDNULL(mp
->m_next
->m_host
)) &&
1323 !strcasecmp (FENDNULL(np
->m_mbox
),
1324 FENDNULL(mp
->m_next
->m_mbox
)))
1327 if (!ccme
&& ismymbox (np
))
1336 * Reset our duplicate address list
1342 struct mailname
*mp
, *mp2
;
1344 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {