]>
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.
10 #include "sbr/m_name.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/seq_setprev.h"
14 #include "sbr/seq_save.h"
15 #include "sbr/smatch.h"
16 #include "sbr/snprintb.h"
17 #include "sbr/getcpy.h"
18 #include "sbr/m_convert.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/folder_read.h"
21 #include "sbr/folder_free.h"
22 #include "sbr/context_save.h"
23 #include "sbr/context_replace.h"
24 #include "sbr/context_find.h"
25 #include "sbr/brkstring.h"
26 #include "sbr/ambigsw.h"
28 #include "sbr/print_version.h"
29 #include "sbr/print_help.h"
30 #include "sbr/seq_getnum.h"
31 #include "sbr/error.h"
32 #include "h/fmt_scan.h"
33 #include "h/fmt_compile.h"
35 #include "h/addrsbr.h"
37 #include "sbr/m_maildir.h"
38 #include "sbr/terminal.h"
40 #define FMTTEST_SWITCHES \
41 X("form formatfile", 0, FORMSW) \
42 X("format string", 5, FMTSW) \
43 X("address", 0, ADDRSW) \
45 X("date", 0, DATESW) \
46 X("message", 0, MESSAGESW) \
47 X("file", 0, FILESW) \
48 X("nofile", 0, NFILESW) \
49 X("-component-name component-text", 0, OTHERSW) \
50 X("dupaddrs", 0, DUPADDRSW) \
51 X("nodupaddrs", 0, NDUPADDRSW) \
52 X("ccme", 0, CCMESW) \
53 X("noccme", 0, NCCMESW) \
54 X("outsize size-in-characters", 0, OUTSIZESW) \
55 X("width column-width", 0, WIDTHSW) \
56 X("msgnum number", 0, MSGNUMSW) \
57 X("msgcur flag", 0, MSGCURSW) \
58 X("msgsize size", 0, MSGSIZESW) \
59 X("unseen flag", 0, UNSEENSW) \
60 X("dump", 0, DUMPSW) \
61 X("nodump", 0, NDUMPSW) \
62 X("trace", 0, TRACESW) \
63 X("notrace", 0, NTRACESW) \
64 X("version", 0, VERSIONSW) \
65 X("help", 0, HELPSW) \
67 #define X(sw, minchars, id) id,
68 DEFINE_SWITCH_ENUM(FMTTEST
);
71 #define X(sw, minchars, id) { sw, minchars, id },
72 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
76 * An array containing labels used for branch instructions
79 static struct format
**lvec
= NULL
;
81 static int lallocated
= 0;
83 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
84 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
85 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
88 * Context structure used by the tracing routines
91 struct trace_context
{
100 static void fmt_dump (char *, struct format
*);
101 static void dumpone(struct format
*);
102 static void initlabels(struct format
*);
103 static int findlabel(struct format
*);
104 static void assignlabel(struct format
*);
105 static char *f_typestr(int);
106 static char *c_typestr(int);
107 static char *c_flagsstr(int);
108 static void litputs(const char *);
109 static void litputc(char);
110 static void process_addresses(struct format
*, struct msgs_array
*,
111 charstring_t
, int, int *,
112 struct fmt_callbacks
*);
113 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
114 int, int *, struct fmt_callbacks
*);
115 static void process_messages(struct format
*, struct msgs_array
*,
116 struct msgs_array
*, charstring_t
, char *, int,
117 int, int *, struct fmt_callbacks
*);
118 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
119 struct format
*, charstring_t
, int,
120 struct fmt_callbacks
*);
121 static void test_trace(void *, struct format
*, int, char *, const char *);
122 static char *test_formataddr(char *, char *);
123 static char *test_concataddr(char *, char *);
124 static int insert(struct mailname
*);
125 static void mlistfree(void);
127 static bool nodupcheck
; /* If set, no check for duplicates */
128 static bool ccme
; /* Should I cc myself? */
129 static struct mailname mq
; /* Mail addresses to check for duplicates */
130 static char *dummy
= "dummy";
133 main (int argc
, char **argv
)
135 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
136 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
140 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
144 bool dupaddrs
= true;
147 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
148 enum mode_t mode
= MESSAGE
;
150 struct fmt_callbacks cb
, *cbp
= NULL
;
152 if (nmh_init(argv
[0], true, true)) { return 1; }
154 arguments
= getarguments (invo_name
, argc
, argv
, 1);
157 while ((cp
= *argp
++)) {
160 * A -- means that we have a component name (like pick);
161 * save the component name and the next argument for the text.
165 die("missing component name after --");
166 app_msgarg(&compargs
, cp
);
167 /* Grab next argument for component text */
169 die("missing argument to %s", argp
[-2]);
170 app_msgarg(&compargs
, cp
);
173 switch (smatch (cp
, switches
)) {
175 ambigsw (cp
, switches
);
178 die("-%s unknown", cp
);
181 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
182 print_help (buf
, switches
, 1);
185 print_version(invo_name
);
188 die("internal argument error!");
192 if (!(cp
= *argp
++) || *cp
== '-')
193 die("missing argument to %s", argp
[-2]);
194 if (strcmp(cp
, "max") == 0)
195 outputsize
= INT_MAX
;
196 else if (strcmp(cp
, "width") == 0)
197 outputsize
= sc_width();
199 outputsize
= atoi(cp
);
203 if (!(form
= *argp
++) || *form
== '-')
204 die("missing argument to %s", argp
[-2]);
208 if (!(format
= *argp
++) || *format
== '-')
209 die("missing argument to %s", argp
[-2]);
222 defformat
= DEFADDRFORMAT
;
234 defformat
= DEFDATEFORMAT
;
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]);
279 if (!(cp
= *argp
++) || *cp
== '-')
280 die("missing argument to %s", argp
[-2]);
281 msgunseen
= atoi(cp
);
295 * Only interpret as a folder if we're in message mode
298 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
300 die("only one folder at a time!");
301 folder
= pluspath (cp
);
303 app_msgarg(&msgs
, cp
);
307 * Here's our weird heuristic:
309 * - We allow -dump without any other arguments.
310 * - If you've given any component arguments, we don't require any
312 * - The arguments are interpreted as folders/messages _if_ we're in
313 * message mode, otherwise pass as strings in the text component.
316 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
317 die("usage: [switches] [+folder] msgs | strings...");
321 * If you're picking "raw" as a mode, then you have to select
325 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
326 die("You must specify a format with -form or -format when "
331 * Get new format string. Must be before chdir().
333 nfs
= new_fs (form
, format
, defformat
);
334 (void) fmt_compile(nfs
, &fmt
, 1);
340 if (compargs
.size
== 0 && msgs
.size
== 0)
345 buffer
= charstring_create(BUFSIZ
);
347 if (outputsize
== 0) {
349 outputsize
= sc_width();
351 outputsize
= INT_MAX
;
357 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
361 * If we want to provide our own formataddr, concactaddr, or tracing
362 * callback, do that now. Also, prime ismymbox if we use it.
365 if (!dupaddrs
|| trace
) {
370 cb
.formataddr
= test_formataddr
;
371 cb
.concataddr
= test_concataddr
;
377 struct trace_context
*ctx
;
382 ctx
->outbuf
= mh_xstrdup("");
384 cb
.trace_func
= test_trace
;
385 cb
.trace_context
= ctx
;
389 if (mode
== MESSAGE
) {
390 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
394 for (i
= 0; i
< compargs
.size
; i
+= 2) {
395 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
397 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
401 if (mode
== ADDRESS
) {
402 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
403 } else /* Fall-through for RAW or DATE */
404 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
407 charstring_free(buffer
);
415 * Process each address with fmt_scan().
421 struct pqpair
*pq_next
;
425 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
426 charstring_t buffer
, int outwidth
, int *dat
,
427 struct fmt_callbacks
*cb
)
430 char *cp
, error
[BUFSIZ
];
432 struct pqpair
*p
, *q
;
445 for (i
= 0; i
< addrs
->size
; i
++) {
446 (q
= &pq
)->pq_next
= NULL
;
447 while ((cp
= getname(addrs
->msgs
[i
]))) {
449 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
450 p
->pq_text
= mh_xstrdup(cp
);
451 p
->pq_error
= mh_xstrdup(error
);
453 p
->pq_text
= getcpy(mp
->m_text
);
456 q
= (q
->pq_next
= p
);
459 for (p
= pq
.pq_next
; p
; p
= q
) {
460 c
= fmt_findcomp("text");
463 c
->c_text
= p
->pq_text
;
466 c
= fmt_findcomp("error");
469 c
->c_text
= p
->pq_error
;
473 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
474 fputs(charstring_buffer(buffer
), stdout
);
486 * Process messages and run them through the format engine. A lot taken
491 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
492 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
493 int outwidth
, int files
, int *dat
,
494 struct fmt_callbacks
*cb
)
496 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
497 int num_unseen_seq
= 0;
498 ivector_t seqnum
= ivector_create (0);
504 * If 'files' is set, short-circuit everything else and just process
509 for (i
= 0; i
< msgs
->size
; i
++) {
510 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
511 admonish(cp
, "unable to open file");
514 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
522 folder
= getfolder(1);
524 maildir
= m_maildir(folder
);
526 if (chdir(maildir
) < 0)
527 adios(maildir
, "unable to change directory to");
529 if (!(mp
= folder_read(folder
, 1)))
530 die("unable to read folder %s", folder
);
533 die("no messages in %s", folder
);
535 for (i
= 0; i
< msgs
->size
; i
++)
536 if (!m_convert(mp
, msgs
->msgs
[i
]))
538 seq_setprev(mp
); /* set the Previous-Sequence */
540 context_replace(pfolder
, folder
); /* update current folder */
541 seq_save(mp
); /* synchronize message sequences */
542 context_save(); /* save the context file */
545 * We want to set the unseen flag if requested, so we have to check
546 * the unseen sequence as well.
550 if ((cp
= context_find(usequence
)) && *cp
) {
554 ap
= brkstring(dp
, " ", "\n");
555 for (i
= 0; ap
&& *ap
; i
++, ap
++)
556 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
563 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
564 if (is_selected(mp
, msgnum
)) {
565 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
566 admonish(cp
, "unable to open message");
576 dat
[1] = msgnum
== mp
->curmsg
;
579 * Check to see if this is in the unseen sequence
583 for (i
= 0; i
< num_unseen_seq
; i
++) {
584 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
591 * Read in the message and process the components
594 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
599 ivector_free (seqnum
);
604 * Process a single file in message mode
608 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
609 struct format
*fmt
, charstring_t buffer
, int outwidth
,
610 struct fmt_callbacks
*cb
)
613 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
614 m_getfld_state_t gstate
;
619 * Get our size if we didn't include one
625 if (fstat(fileno(in
), &st
) < 0)
632 * Initialize everything else
643 * Read in the message and process the components
646 gstate
= m_getfld_state_init(in
);
648 bufsz
= sizeof(rbuf
);
649 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
653 i
= fmt_addcomptext(name
, rbuf
);
655 while (state
== FLDPLUS
) {
656 bufsz
= sizeof(rbuf
);
657 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
658 fmt_appendcomp(i
, name
, rbuf
);
662 while (state
== FLDPLUS
) {
663 bufsz
= sizeof(rbuf
);
664 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
669 if (fmt_findcomp("body")) {
670 if ((i
= strlen(rbuf
)) < outwidth
) {
671 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
672 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
675 fmt_addcomptext("body", rbuf
);
685 m_getfld_state_destroy(&gstate
);
688 * Do this now to override any components in the original message
691 for (i
= 0; i
< comps
->size
; i
+= 2) {
692 c
= fmt_findcomp(comps
->msgs
[i
]);
695 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
699 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
700 fputs(charstring_buffer (buffer
), stdout
);
705 * Run text through the format engine with no special processing
709 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
710 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
724 c
= fmt_findcomp("text");
726 for (i
= 0; i
< text
->size
; i
++) {
729 c
->c_text
= getcpy(text
->msgs
[i
]);
732 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
733 fputs(charstring_buffer (buffer
), stdout
);
739 * Our basic tracing support callback.
741 * Print out each instruction as it's executed, including the values of
742 * the num and str registers if they've changed.
746 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
749 struct trace_context
*ctx
= (struct trace_context
*) context
;
750 bool changed
= false;
754 if (num
!= ctx
->num
) {
755 printf("num=%d", num
);
760 if (str
!= ctx
->str
) {
764 fputs("str=", stdout
);
772 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
773 fputs("outbuf=", stdout
);
777 ctx
->outbuf
= mh_xstrdup(outbuf
);
782 fmt_dump (char *nfs
, struct format
*fmth
)
786 printf("Instruction dump of format string: \n%s\n", nfs
);
789 for (fmt
= fmth
; fmt
; ++fmt
) {
791 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
797 dumpone(struct format
*fmt
)
801 if ((i
= findlabel(fmt
)) >= 0)
805 fputs(f_typestr((int)fmt
->f_type
), stdout
);
807 switch (fmt
->f_type
) {
813 fputs(", comp ", stdout
);
814 litputs(fmt
->f_comp
->c_name
);
815 if (fmt
->f_comp
->c_type
)
816 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
817 if (fmt
->f_comp
->c_flags
)
818 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
845 fputs(", c_name ", stdout
);
846 litputs(fmt
->f_comp
->c_name
);
847 if (fmt
->f_comp
->c_type
)
848 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
849 if (fmt
->f_comp
->c_flags
)
850 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
869 fputs(", c_name ", stdout
);
870 litputs(fmt
->f_comp
->c_name
);
871 if (fmt
->f_comp
->c_type
)
872 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
873 if (fmt
->f_comp
->c_flags
)
874 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
878 printf(", width %d, fill '", fmt
->f_width
);
879 litputc(fmt
->f_fill
);
880 fputs("' name ", stdout
);
881 litputs(fmt
->f_comp
->c_name
);
882 if (fmt
->f_comp
->c_type
)
883 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
884 if (fmt
->f_comp
->c_flags
)
885 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
890 printf(", width %d, fill '", fmt
->f_width
);
891 litputc(fmt
->f_fill
);
897 litputs(fmt
->f_text
);
901 printf(", width %d, fill '", fmt
->f_width
);
902 litputc(fmt
->f_fill
);
904 litputs(fmt
->f_text
);
910 litputc(fmt
->f_char
);
919 fputs(" continue else goto", stdout
);
922 i
= findlabel(fmt
+ fmt
->f_skip
);
929 i
= findlabel(fmt
+ fmt
->f_skip
);
930 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
939 case FT_LV_MULTIPLY_L
:
942 printf(" value %d", fmt
->f_value
);
946 fputs(" str ", stdout
);
947 litputs(fmt
->f_text
);
951 fputs(" getenv ", stdout
);
952 litputs(fmt
->f_text
);
955 case FT_LS_DECODECOMP
:
956 fputs(", comp ", stdout
);
957 litputs(fmt
->f_comp
->c_name
);
964 printf(", width %d", fmt
->f_width
);
968 printf(", value dat[%d]", fmt
->f_value
);
975 * Iterate over all instructions and assign labels to the targets of
980 initlabels(struct format
*fmth
)
982 struct format
*fmt
, *addr
;
986 for (fmt
= fmth
; fmt
; ++fmt
) {
996 addr
= fmt
+ fmt
->f_skip
;
997 if (findlabel(addr
) < 0)
1000 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
1007 findlabel(struct format
*addr
)
1011 for (i
= 0; i
< lused
; ++i
)
1012 if (addr
== lvec
[i
])
1018 assignlabel(struct format
*addr
)
1020 if (lused
>= lallocated
) {
1022 lvec
= (struct format
**)
1023 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1026 lvec
[lused
++] = addr
;
1032 static char buf
[32];
1035 case FT_COMP
: return "COMP";
1036 case FT_COMPF
: return "COMPF";
1037 case FT_LIT
: return "LIT";
1038 case FT_LITF
: return "LITF";
1039 case FT_CHAR
: return "CHAR";
1040 case FT_NUM
: return "NUM";
1041 case FT_NUMF
: return "NUMF";
1042 case FT_STR
: return "STR";
1043 case FT_STRF
: return "STRF";
1044 case FT_STRFW
: return "STRFW";
1045 case FT_STRLIT
: return "STRLIT";
1046 case FT_STRLITZ
: return "STRLITZ";
1047 case FT_PUTADDR
: return "PUTADDR";
1048 case FT_LS_COMP
: return "LS_COMP";
1049 case FT_LS_LIT
: return "LS_LIT";
1050 case FT_LS_GETENV
: return "LS_GETENV";
1051 case FT_LS_CFIND
: return "LS_CFIND";
1052 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1053 case FT_LS_DECODE
: return "LS_DECODE";
1054 case FT_LS_TRIM
: return "LS_TRIM";
1055 case FT_LV_COMP
: return "LV_COMP";
1056 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1057 case FT_LV_LIT
: return "LV_LIT";
1058 case FT_LV_DAT
: return "LV_DAT";
1059 case FT_LV_STRLEN
: return "LV_STRLEN";
1060 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1061 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1062 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1063 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1064 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1065 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1066 case FT_LS_MONTH
: return "LS_MONTH";
1067 case FT_LS_LMONTH
: return "LS_LMONTH";
1068 case FT_LS_ZONE
: return "LS_ZONE";
1069 case FT_LS_DAY
: return "LS_DAY";
1070 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1071 case FT_LS_822DATE
: return "LS_822DATE";
1072 case FT_LS_PRETTY
: return "LS_PRETTY";
1073 case FT_LS_KILO
: return "LS_KILO";
1074 case FT_LS_KIBI
: return "LS_KIBI";
1075 case FT_LV_SEC
: return "LV_SEC";
1076 case FT_LV_MIN
: return "LV_MIN";
1077 case FT_LV_HOUR
: return "LV_HOUR";
1078 case FT_LV_MDAY
: return "LV_MDAY";
1079 case FT_LV_MON
: return "LV_MON";
1080 case FT_LV_YEAR
: return "LV_YEAR";
1081 case FT_LV_YDAY
: return "LV_YDAY";
1082 case FT_LV_WDAY
: return "LV_WDAY";
1083 case FT_LV_ZONE
: return "LV_ZONE";
1084 case FT_LV_CLOCK
: return "LV_CLOCK";
1085 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1086 case FT_LV_DAYF
: return "LV_DAYF";
1087 case FT_LV_DST
: return "LV_DST";
1088 case FT_LV_ZONEF
: return "LV_ZONEF";
1089 case FT_LS_PERS
: return "LS_PERS";
1090 case FT_LS_MBOX
: return "LS_MBOX";
1091 case FT_LS_HOST
: return "LS_HOST";
1092 case FT_LS_PATH
: return "LS_PATH";
1093 case FT_LS_GNAME
: return "LS_GNAME";
1094 case FT_LS_NOTE
: return "LS_NOTE";
1095 case FT_LS_ADDR
: return "LS_ADDR";
1096 case FT_LS_822ADDR
: return "LS_822ADDR";
1097 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1098 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1099 case FT_LV_INGRPF
: return "LV_INGRPF";
1100 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1101 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1102 case FT_LOCALDATE
: return "LOCALDATE";
1103 case FT_GMTDATE
: return "GMTDATE";
1104 case FT_PARSEDATE
: return "PARSEDATE";
1105 case FT_PARSEADDR
: return "PARSEADDR";
1106 case FT_FORMATADDR
: return "FORMATADDR";
1107 case FT_CONCATADDR
: return "CONCATADDR";
1108 case FT_MYMBOX
: return "MYMBOX";
1109 case FT_GETMYMBOX
: return "GETMYMBOX";
1110 case FT_GETMYADDR
: return "GETMYADDR";
1111 case FT_SAVESTR
: return "SAVESTR";
1112 case FT_DONE
: return "DONE";
1113 case FT_PAUSE
: return "PAUSE";
1114 case FT_NOP
: return "NOP";
1115 case FT_GOTO
: return "GOTO";
1116 case FT_IF_S_NULL
: return "IF_S_NULL";
1117 case FT_IF_S
: return "IF_S";
1118 case FT_IF_V_EQ
: return "IF_V_EQ";
1119 case FT_IF_V_NE
: return "IF_V_NE";
1120 case FT_IF_V_GT
: return "IF_V_GT";
1121 case FT_IF_MATCH
: return "IF_MATCH";
1122 case FT_IF_AMATCH
: return "IF_AMATCH";
1123 case FT_S_NULL
: return "S_NULL";
1124 case FT_S_NONNULL
: return "S_NONNULL";
1125 case FT_V_EQ
: return "V_EQ";
1126 case FT_V_NE
: return "V_NE";
1127 case FT_V_GT
: return "V_GT";
1128 case FT_V_MATCH
: return "V_MATCH";
1129 case FT_V_AMATCH
: return "V_AMATCH";
1131 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1139 static char buf
[64];
1141 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1148 static char buf
[64];
1150 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1155 litputs(const char *s
)
1163 fputs("<nil>", stdout
);
1170 printf("\\x%02x", (unsigned char) c
);
1171 } else if (c
< 0x20 || c
== 0177) {
1175 } else if (c
== '\f') {
1178 } else if (c
== '\n') {
1181 } else if (c
== '\r') {
1184 } else if (c
== '\t') {
1189 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1196 * Routines/code to support the duplicate address suppression code, adapted
1200 static char *buf
; /* our current working buffer */
1201 static char *bufend
; /* end of working buffer */
1202 static char *last_dst
; /* buf ptr at end of last call */
1203 static unsigned int bufsiz
=0; /* current size of buf */
1205 #define BUFINCR 512 /* how much to expand buf when if fills */
1207 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1210 * check if there's enough room in buf for str.
1211 * add more mem if needed
1213 #define CHECKMEM(str) \
1214 if ((len = strlen (str)) >= bufend - dst) {\
1216 int n = last_dst - buf;\
1217 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1218 buf = mh_xrealloc (buf, bufsiz);\
1220 last_dst = buf + n;\
1221 bufend = buf + bufsiz;\
1226 * These are versions of similar routines from replsbr.c; the purpose is
1227 * to suppress duplicate addresses from being added to a list when building
1228 * up addresses for the %(formataddr) format function. This is used by
1229 * repl to prevent duplicate addresses from being added to the "to" line.
1230 * See replsbr.c for more information.
1232 * We can't use the functions in replsbr.c directly because they are slightly
1233 * different and depend on the rest of replsbr.c
1236 test_formataddr (char *orig
, char *str
)
1244 struct mailname
*mp
= NULL
;
1246 /* if we don't have a buffer yet, get one */
1248 buf
= mh_xmalloc (BUFINCR
);
1249 last_dst
= buf
; /* XXX */
1250 bufsiz
= BUFINCR
- 6; /* leave some slop */
1251 bufend
= buf
+ bufsiz
;
1254 * If "orig" points to our buffer we can just pick up where we
1255 * left off. Otherwise we have to copy orig into our buffer.
1259 else if (!orig
|| !*orig
) {
1263 dst
= last_dst
; /* XXX */
1268 /* concatenate all the new addresses onto 'buf' */
1269 for (isgroup
= false; (cp
= getname (str
)); ) {
1270 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1271 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1274 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1279 /* if we get here we're going to add an address */
1285 CHECKMEM (mp
->m_gname
);
1289 sp
= adrformat (mp
);
1305 * The companion to test_formataddr(); it behaves the same way, except doesn't
1306 * do duplicate address detection.
1309 test_concataddr(char *orig
, char *str
)
1314 cp
= test_formataddr(orig
, str
);
1320 insert (struct mailname
*np
)
1322 struct mailname
*mp
;
1327 if (np
->m_mbox
== NULL
)
1330 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1331 if (!strcasecmp (FENDNULL(np
->m_host
),
1332 FENDNULL(mp
->m_next
->m_host
)) &&
1333 !strcasecmp (FENDNULL(np
->m_mbox
),
1334 FENDNULL(mp
->m_next
->m_mbox
)))
1337 if (!ccme
&& ismymbox (np
))
1346 * Reset our duplicate address list
1352 struct mailname
*mp
, *mp2
;
1354 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {