]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
35bab7521f3d749963786a0e64e529a81378f482
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/smatch.h"
10 #include "sbr/snprintb.h"
11 #include "sbr/getcpy.h"
12 #include "sbr/m_convert.h"
13 #include "sbr/getfolder.h"
14 #include "sbr/folder_read.h"
15 #include "sbr/folder_free.h"
16 #include "sbr/context_save.h"
17 #include "sbr/context_replace.h"
18 #include "sbr/context_find.h"
19 #include "sbr/brkstring.h"
20 #include "sbr/ambigsw.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/seq_getnum.h"
25 #include "sbr/error.h"
26 #include "h/fmt_scan.h"
27 #include "h/fmt_compile.h"
29 #include "h/scansbr.h"
30 #include "h/addrsbr.h"
32 #include "sbr/m_maildir.h"
33 #include "sbr/terminal.h"
35 #define FMTTEST_SWITCHES \
36 X("form formatfile", 0, FORMSW) \
37 X("format string", 5, FMTSW) \
38 X("address", 0, ADDRSW) \
40 X("date", 0, DATESW) \
41 X("message", 0, MESSAGESW) \
42 X("file", 0, FILESW) \
43 X("nofile", 0, NFILESW) \
44 X("-component-name component-text", 0, OTHERSW) \
45 X("dupaddrs", 0, DUPADDRSW) \
46 X("nodupaddrs", 0, NDUPADDRSW) \
47 X("ccme", 0, CCMESW) \
48 X("noccme", 0, NCCMESW) \
49 X("outsize size-in-characters", 0, OUTSIZESW) \
50 X("width column-width", 0, WIDTHSW) \
51 X("msgnum number", 0, MSGNUMSW) \
52 X("msgcur flag", 0, MSGCURSW) \
53 X("msgsize size", 0, MSGSIZESW) \
54 X("unseen flag", 0, UNSEENSW) \
55 X("dump", 0, DUMPSW) \
56 X("nodump", 0, NDUMPSW) \
57 X("trace", 0, TRACESW) \
58 X("notrace", 0, NTRACESW) \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(FMTTEST
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
71 * An array containing labels used for branch instructions
74 static struct format
**lvec
= NULL
;
76 static int lallocated
= 0;
78 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
79 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
80 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
83 * Context structure used by the tracing routines
86 struct trace_context
{
95 static void fmt_dump (char *, struct format
*);
96 static void dumpone(struct format
*);
97 static void initlabels(struct format
*);
98 static int findlabel(struct format
*);
99 static void assignlabel(struct format
*);
100 static char *f_typestr(int);
101 static char *c_typestr(int);
102 static char *c_flagsstr(int);
103 static void litputs(const char *);
104 static void litputc(char);
105 static void process_addresses(struct format
*, struct msgs_array
*,
106 charstring_t
, int, int *,
107 struct fmt_callbacks
*);
108 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
109 int, int *, struct fmt_callbacks
*);
110 static void process_messages(struct format
*, struct msgs_array
*,
111 struct msgs_array
*, charstring_t
, char *, int,
112 int, int *, struct fmt_callbacks
*);
113 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
114 struct format
*, charstring_t
, int,
115 struct fmt_callbacks
*);
116 static void test_trace(void *, struct format
*, int, char *, const char *);
117 static char *test_formataddr(char *, char *);
118 static char *test_concataddr(char *, char *);
119 static int insert(struct mailname
*);
120 static void mlistfree(void);
122 static bool nodupcheck
; /* If set, no check for duplicates */
123 static bool ccme
; /* Should I cc myself? */
124 static struct mailname mq
; /* Mail addresses to check for duplicates */
125 static char *dummy
= "dummy";
128 main (int argc
, char **argv
)
130 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
131 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
135 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
139 bool dupaddrs
= true;
142 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
143 enum mode_t mode
= MESSAGE
;
145 struct fmt_callbacks cb
, *cbp
= NULL
;
147 if (nmh_init(argv
[0], true, true)) { return 1; }
149 arguments
= getarguments (invo_name
, argc
, argv
, 1);
152 while ((cp
= *argp
++)) {
155 * A -- means that we have a component name (like pick);
156 * save the component name and the next argument for the text.
160 die("missing component name after --");
161 app_msgarg(&compargs
, cp
);
162 /* Grab next argument for component text */
164 die("missing argument to %s", argp
[-2]);
165 app_msgarg(&compargs
, cp
);
168 switch (smatch (cp
, switches
)) {
170 ambigsw (cp
, switches
);
173 die("-%s unknown", cp
);
176 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
177 print_help (buf
, switches
, 1);
180 print_version(invo_name
);
183 die("internal argument error!");
187 if (!(cp
= *argp
++) || *cp
== '-')
188 die("missing argument to %s", argp
[-2]);
189 if (strcmp(cp
, "max") == 0)
190 outputsize
= INT_MAX
;
191 else if (strcmp(cp
, "width") == 0)
192 outputsize
= sc_width();
194 outputsize
= atoi(cp
);
198 if (!(form
= *argp
++) || *form
== '-')
199 die("missing argument to %s", argp
[-2]);
203 if (!(format
= *argp
++) || *format
== '-')
204 die("missing argument to %s", argp
[-2]);
217 defformat
= DEFADDRFORMAT
;
229 defformat
= DEFDATEFORMAT
;
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]);
274 if (!(cp
= *argp
++) || *cp
== '-')
275 die("missing argument to %s", argp
[-2]);
276 msgunseen
= atoi(cp
);
290 * Only interpret as a folder if we're in message mode
293 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
295 die("only one folder at a time!");
296 folder
= pluspath (cp
);
298 app_msgarg(&msgs
, cp
);
302 * Here's our weird heuristic:
304 * - We allow -dump without any other arguments.
305 * - If you've given any component arguments, we don't require any
307 * - The arguments are interpreted as folders/messages _if_ we're in
308 * message mode, otherwise pass as strings in the text component.
311 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
312 die("usage: [switches] [+folder] msgs | strings...");
316 * If you're picking "raw" as a mode, then you have to select
320 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
321 die("You must specify a format with -form or -format when "
326 * Get new format string. Must be before chdir().
328 nfs
= new_fs (form
, format
, defformat
);
329 (void) fmt_compile(nfs
, &fmt
, 1);
335 if (compargs
.size
== 0 && msgs
.size
== 0)
340 buffer
= charstring_create(BUFSIZ
);
342 if (outputsize
== 0) {
344 outputsize
= sc_width();
346 outputsize
= INT_MAX
;
352 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
356 * If we want to provide our own formataddr, concactaddr, or tracing
357 * callback, do that now. Also, prime ismymbox if we use it.
360 if (!dupaddrs
|| trace
) {
365 cb
.formataddr
= test_formataddr
;
366 cb
.concataddr
= test_concataddr
;
372 struct trace_context
*ctx
;
377 ctx
->outbuf
= mh_xstrdup("");
379 cb
.trace_func
= test_trace
;
380 cb
.trace_context
= ctx
;
384 if (mode
== MESSAGE
) {
385 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
389 for (i
= 0; i
< compargs
.size
; i
+= 2) {
390 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
392 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
396 if (mode
== ADDRESS
) {
397 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
398 } else /* Fall-through for RAW or DATE */
399 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
402 charstring_free(buffer
);
410 * Process each address with fmt_scan().
416 struct pqpair
*pq_next
;
420 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
421 charstring_t buffer
, int outwidth
, int *dat
,
422 struct fmt_callbacks
*cb
)
425 char *cp
, error
[BUFSIZ
];
427 struct pqpair
*p
, *q
;
440 for (i
= 0; i
< addrs
->size
; i
++) {
441 (q
= &pq
)->pq_next
= NULL
;
442 while ((cp
= getname(addrs
->msgs
[i
]))) {
444 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
445 p
->pq_text
= mh_xstrdup(cp
);
446 p
->pq_error
= mh_xstrdup(error
);
448 p
->pq_text
= getcpy(mp
->m_text
);
451 q
= (q
->pq_next
= p
);
454 for (p
= pq
.pq_next
; p
; p
= q
) {
455 c
= fmt_findcomp("text");
458 c
->c_text
= p
->pq_text
;
461 c
= fmt_findcomp("error");
464 c
->c_text
= p
->pq_error
;
468 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
469 fputs(charstring_buffer(buffer
), stdout
);
481 * Process messages and run them through the format engine. A lot taken
486 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
487 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
488 int outwidth
, int files
, int *dat
,
489 struct fmt_callbacks
*cb
)
491 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
492 int num_unseen_seq
= 0;
493 ivector_t seqnum
= ivector_create (0);
499 * If 'files' is set, short-circuit everything else and just process
504 for (i
= 0; i
< msgs
->size
; i
++) {
505 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
506 admonish(cp
, "unable to open file");
509 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
517 folder
= getfolder(1);
519 maildir
= m_maildir(folder
);
521 if (chdir(maildir
) < 0)
522 adios(maildir
, "unable to change directory to");
524 if (!(mp
= folder_read(folder
, 1)))
525 die("unable to read folder %s", folder
);
528 die("no messages in %s", folder
);
530 for (i
= 0; i
< msgs
->size
; i
++)
531 if (!m_convert(mp
, msgs
->msgs
[i
]))
533 seq_setprev(mp
); /* set the Previous-Sequence */
535 context_replace(pfolder
, folder
); /* update current folder */
536 seq_save(mp
); /* synchronize message sequences */
537 context_save(); /* save the context file */
540 * We want to set the unseen flag if requested, so we have to check
541 * the unseen sequence as well.
545 if ((cp
= context_find(usequence
)) && *cp
) {
549 ap
= brkstring(dp
, " ", "\n");
550 for (i
= 0; ap
&& *ap
; i
++, ap
++)
551 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
558 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
559 if (is_selected(mp
, msgnum
)) {
560 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
561 admonish(cp
, "unable to open message");
571 dat
[1] = msgnum
== mp
->curmsg
;
574 * Check to see if this is in the unseen sequence
578 for (i
= 0; i
< num_unseen_seq
; i
++) {
579 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
586 * Read in the message and process the components
589 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
594 ivector_free (seqnum
);
599 * Process a single file in message mode
603 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
604 struct format
*fmt
, charstring_t buffer
, int outwidth
,
605 struct fmt_callbacks
*cb
)
608 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
609 m_getfld_state_t gstate
;
614 * Get our size if we didn't include one
620 if (fstat(fileno(in
), &st
) < 0)
627 * Initialize everything else
638 * Read in the message and process the components
641 gstate
= m_getfld_state_init(in
);
643 bufsz
= sizeof(rbuf
);
644 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
648 i
= fmt_addcomptext(name
, rbuf
);
650 while (state
== FLDPLUS
) {
651 bufsz
= sizeof(rbuf
);
652 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
653 fmt_appendcomp(i
, name
, rbuf
);
657 while (state
== FLDPLUS
) {
658 bufsz
= sizeof(rbuf
);
659 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
664 if (fmt_findcomp("body")) {
665 if ((i
= strlen(rbuf
)) < outwidth
) {
666 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
667 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
670 fmt_addcomptext("body", rbuf
);
680 m_getfld_state_destroy(&gstate
);
683 * Do this now to override any components in the original message
686 for (i
= 0; i
< comps
->size
; i
+= 2) {
687 c
= fmt_findcomp(comps
->msgs
[i
]);
690 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
694 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
695 fputs(charstring_buffer (buffer
), stdout
);
700 * Run text through the format engine with no special processing
704 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
705 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
719 c
= fmt_findcomp("text");
721 for (i
= 0; i
< text
->size
; i
++) {
724 c
->c_text
= getcpy(text
->msgs
[i
]);
727 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
728 fputs(charstring_buffer (buffer
), stdout
);
734 * Our basic tracing support callback.
736 * Print out each instruction as it's executed, including the values of
737 * the num and str registers if they've changed.
741 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
744 struct trace_context
*ctx
= (struct trace_context
*) context
;
745 bool changed
= false;
749 if (num
!= ctx
->num
) {
750 printf("num=%d", num
);
755 if (str
!= ctx
->str
) {
759 fputs("str=", stdout
);
767 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
768 fputs("outbuf=", stdout
);
772 ctx
->outbuf
= mh_xstrdup(outbuf
);
777 fmt_dump (char *nfs
, struct format
*fmth
)
781 printf("Instruction dump of format string: \n%s\n", nfs
);
784 for (fmt
= fmth
; fmt
; ++fmt
) {
786 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
792 dumpone(struct format
*fmt
)
796 if ((i
= findlabel(fmt
)) >= 0)
800 fputs(f_typestr((int)fmt
->f_type
), stdout
);
802 switch (fmt
->f_type
) {
808 fputs(", comp ", stdout
);
809 litputs(fmt
->f_comp
->c_name
);
810 if (fmt
->f_comp
->c_type
)
811 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
812 if (fmt
->f_comp
->c_flags
)
813 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
840 fputs(", c_name ", stdout
);
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
));
864 fputs(", c_name ", stdout
);
865 litputs(fmt
->f_comp
->c_name
);
866 if (fmt
->f_comp
->c_type
)
867 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
868 if (fmt
->f_comp
->c_flags
)
869 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
873 printf(", width %d, fill '", fmt
->f_width
);
874 litputc(fmt
->f_fill
);
875 fputs("' name ", stdout
);
876 litputs(fmt
->f_comp
->c_name
);
877 if (fmt
->f_comp
->c_type
)
878 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
879 if (fmt
->f_comp
->c_flags
)
880 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
885 printf(", width %d, fill '", fmt
->f_width
);
886 litputc(fmt
->f_fill
);
892 litputs(fmt
->f_text
);
896 printf(", width %d, fill '", fmt
->f_width
);
897 litputc(fmt
->f_fill
);
899 litputs(fmt
->f_text
);
905 litputc(fmt
->f_char
);
914 fputs(" continue else goto", stdout
);
917 i
= findlabel(fmt
+ fmt
->f_skip
);
924 i
= findlabel(fmt
+ fmt
->f_skip
);
925 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
934 case FT_LV_MULTIPLY_L
:
937 printf(" value %d", fmt
->f_value
);
941 fputs(" str ", stdout
);
942 litputs(fmt
->f_text
);
946 fputs(" getenv ", stdout
);
947 litputs(fmt
->f_text
);
950 case FT_LS_DECODECOMP
:
951 fputs(", comp ", stdout
);
952 litputs(fmt
->f_comp
->c_name
);
959 printf(", width %d", fmt
->f_width
);
963 printf(", value dat[%d]", fmt
->f_value
);
970 * Iterate over all instructions and assign labels to the targets of
975 initlabels(struct format
*fmth
)
977 struct format
*fmt
, *addr
;
981 for (fmt
= fmth
; fmt
; ++fmt
) {
991 addr
= fmt
+ fmt
->f_skip
;
992 if (findlabel(addr
) < 0)
995 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
1002 findlabel(struct format
*addr
)
1006 for (i
= 0; i
< lused
; ++i
)
1007 if (addr
== lvec
[i
])
1013 assignlabel(struct format
*addr
)
1015 if (lused
>= lallocated
) {
1017 lvec
= (struct format
**)
1018 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1021 lvec
[lused
++] = addr
;
1027 static char buf
[32];
1030 case FT_COMP
: return "COMP";
1031 case FT_COMPF
: return "COMPF";
1032 case FT_LIT
: return "LIT";
1033 case FT_LITF
: return "LITF";
1034 case FT_CHAR
: return "CHAR";
1035 case FT_NUM
: return "NUM";
1036 case FT_NUMF
: return "NUMF";
1037 case FT_STR
: return "STR";
1038 case FT_STRF
: return "STRF";
1039 case FT_STRFW
: return "STRFW";
1040 case FT_STRLIT
: return "STRLIT";
1041 case FT_STRLITZ
: return "STRLITZ";
1042 case FT_PUTADDR
: return "PUTADDR";
1043 case FT_LS_COMP
: return "LS_COMP";
1044 case FT_LS_LIT
: return "LS_LIT";
1045 case FT_LS_GETENV
: return "LS_GETENV";
1046 case FT_LS_CFIND
: return "LS_CFIND";
1047 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1048 case FT_LS_DECODE
: return "LS_DECODE";
1049 case FT_LS_TRIM
: return "LS_TRIM";
1050 case FT_LV_COMP
: return "LV_COMP";
1051 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1052 case FT_LV_LIT
: return "LV_LIT";
1053 case FT_LV_DAT
: return "LV_DAT";
1054 case FT_LV_STRLEN
: return "LV_STRLEN";
1055 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1056 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1057 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1058 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1059 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1060 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1061 case FT_LS_MONTH
: return "LS_MONTH";
1062 case FT_LS_LMONTH
: return "LS_LMONTH";
1063 case FT_LS_ZONE
: return "LS_ZONE";
1064 case FT_LS_DAY
: return "LS_DAY";
1065 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1066 case FT_LS_822DATE
: return "LS_822DATE";
1067 case FT_LS_PRETTY
: return "LS_PRETTY";
1068 case FT_LS_KILO
: return "LS_KILO";
1069 case FT_LS_KIBI
: return "LS_KIBI";
1070 case FT_LV_SEC
: return "LV_SEC";
1071 case FT_LV_MIN
: return "LV_MIN";
1072 case FT_LV_HOUR
: return "LV_HOUR";
1073 case FT_LV_MDAY
: return "LV_MDAY";
1074 case FT_LV_MON
: return "LV_MON";
1075 case FT_LV_YEAR
: return "LV_YEAR";
1076 case FT_LV_YDAY
: return "LV_YDAY";
1077 case FT_LV_WDAY
: return "LV_WDAY";
1078 case FT_LV_ZONE
: return "LV_ZONE";
1079 case FT_LV_CLOCK
: return "LV_CLOCK";
1080 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1081 case FT_LV_DAYF
: return "LV_DAYF";
1082 case FT_LV_DST
: return "LV_DST";
1083 case FT_LV_ZONEF
: return "LV_ZONEF";
1084 case FT_LS_PERS
: return "LS_PERS";
1085 case FT_LS_MBOX
: return "LS_MBOX";
1086 case FT_LS_HOST
: return "LS_HOST";
1087 case FT_LS_PATH
: return "LS_PATH";
1088 case FT_LS_GNAME
: return "LS_GNAME";
1089 case FT_LS_NOTE
: return "LS_NOTE";
1090 case FT_LS_ADDR
: return "LS_ADDR";
1091 case FT_LS_822ADDR
: return "LS_822ADDR";
1092 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1093 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1094 case FT_LV_INGRPF
: return "LV_INGRPF";
1095 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1096 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1097 case FT_LOCALDATE
: return "LOCALDATE";
1098 case FT_GMTDATE
: return "GMTDATE";
1099 case FT_PARSEDATE
: return "PARSEDATE";
1100 case FT_PARSEADDR
: return "PARSEADDR";
1101 case FT_FORMATADDR
: return "FORMATADDR";
1102 case FT_CONCATADDR
: return "CONCATADDR";
1103 case FT_MYMBOX
: return "MYMBOX";
1104 case FT_GETMYMBOX
: return "GETMYMBOX";
1105 case FT_GETMYADDR
: return "GETMYADDR";
1106 case FT_SAVESTR
: return "SAVESTR";
1107 case FT_DONE
: return "DONE";
1108 case FT_PAUSE
: return "PAUSE";
1109 case FT_NOP
: return "NOP";
1110 case FT_GOTO
: return "GOTO";
1111 case FT_IF_S_NULL
: return "IF_S_NULL";
1112 case FT_IF_S
: return "IF_S";
1113 case FT_IF_V_EQ
: return "IF_V_EQ";
1114 case FT_IF_V_NE
: return "IF_V_NE";
1115 case FT_IF_V_GT
: return "IF_V_GT";
1116 case FT_IF_MATCH
: return "IF_MATCH";
1117 case FT_IF_AMATCH
: return "IF_AMATCH";
1118 case FT_S_NULL
: return "S_NULL";
1119 case FT_S_NONNULL
: return "S_NONNULL";
1120 case FT_V_EQ
: return "V_EQ";
1121 case FT_V_NE
: return "V_NE";
1122 case FT_V_GT
: return "V_GT";
1123 case FT_V_MATCH
: return "V_MATCH";
1124 case FT_V_AMATCH
: return "V_AMATCH";
1126 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1134 static char buf
[64];
1136 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1143 static char buf
[64];
1145 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1150 litputs(const char *s
)
1158 fputs("<nil>", stdout
);
1165 printf("\\x%02x", (unsigned char) c
);
1166 } else if (c
< 0x20 || c
== 0177) {
1170 } else if (c
== '\f') {
1173 } else if (c
== '\n') {
1176 } else if (c
== '\r') {
1179 } else if (c
== '\t') {
1184 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1191 * Routines/code to support the duplicate address suppression code, adapted
1195 static char *buf
; /* our current working buffer */
1196 static char *bufend
; /* end of working buffer */
1197 static char *last_dst
; /* buf ptr at end of last call */
1198 static unsigned int bufsiz
=0; /* current size of buf */
1200 #define BUFINCR 512 /* how much to expand buf when if fills */
1202 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1205 * check if there's enough room in buf for str.
1206 * add more mem if needed
1208 #define CHECKMEM(str) \
1209 if ((len = strlen (str)) >= bufend - dst) {\
1211 int n = last_dst - buf;\
1212 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1213 buf = mh_xrealloc (buf, bufsiz);\
1215 last_dst = buf + n;\
1216 bufend = buf + bufsiz;\
1221 * These are versions of similar routines from replsbr.c; the purpose is
1222 * to suppress duplicate addresses from being added to a list when building
1223 * up addresses for the %(formataddr) format function. This is used by
1224 * repl to prevent duplicate addresses from being added to the "to" line.
1225 * See replsbr.c for more information.
1227 * We can't use the functions in replsbr.c directly because they are slightly
1228 * different and depend on the rest of replsbr.c
1231 test_formataddr (char *orig
, char *str
)
1239 struct mailname
*mp
= NULL
;
1241 /* if we don't have a buffer yet, get one */
1243 buf
= mh_xmalloc (BUFINCR
);
1244 last_dst
= buf
; /* XXX */
1245 bufsiz
= BUFINCR
- 6; /* leave some slop */
1246 bufend
= buf
+ bufsiz
;
1249 * If "orig" points to our buffer we can just pick up where we
1250 * left off. Otherwise we have to copy orig into our buffer.
1254 else if (!orig
|| !*orig
) {
1258 dst
= last_dst
; /* XXX */
1263 /* concatenate all the new addresses onto 'buf' */
1264 for (isgroup
= false; (cp
= getname (str
)); ) {
1265 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1266 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1269 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1274 /* if we get here we're going to add an address */
1280 CHECKMEM (mp
->m_gname
);
1284 sp
= adrformat (mp
);
1300 * The companion to test_formataddr(); it behaves the same way, except doesn't
1301 * do duplicate address detection.
1304 test_concataddr(char *orig
, char *str
)
1309 cp
= test_formataddr(orig
, str
);
1315 insert (struct mailname
*np
)
1317 struct mailname
*mp
;
1322 if (np
->m_mbox
== NULL
)
1325 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1326 if (!strcasecmp (FENDNULL(np
->m_host
),
1327 FENDNULL(mp
->m_next
->m_host
)) &&
1328 !strcasecmp (FENDNULL(np
->m_mbox
),
1329 FENDNULL(mp
->m_next
->m_mbox
)))
1332 if (!ccme
&& ismymbox (np
))
1341 * Reset our duplicate address list
1347 struct mailname
*mp
, *mp2
;
1349 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {