]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
e3308384b0db116d6aeb076cec2bcbd3c0d93059
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/fmt_new.h"
11 #include "sbr/m_name.h"
12 #include "sbr/m_getfld.h"
13 #include "sbr/getarguments.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_save.h"
16 #include "sbr/smatch.h"
17 #include "sbr/snprintb.h"
18 #include "sbr/getcpy.h"
19 #include "sbr/m_convert.h"
20 #include "sbr/getfolder.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/folder_free.h"
23 #include "sbr/context_save.h"
24 #include "sbr/context_replace.h"
25 #include "sbr/context_find.h"
26 #include "sbr/brkstring.h"
27 #include "sbr/ambigsw.h"
29 #include "sbr/print_version.h"
30 #include "sbr/print_help.h"
31 #include "sbr/seq_getnum.h"
32 #include "sbr/error.h"
33 #include "h/fmt_scan.h"
34 #include "h/fmt_compile.h"
36 #include "h/addrsbr.h"
38 #include "sbr/m_maildir.h"
39 #include "sbr/terminal.h"
41 #define FMTTEST_SWITCHES \
42 X("form formatfile", 0, FORMSW) \
43 X("format string", 5, FMTSW) \
44 X("address", 0, ADDRSW) \
46 X("date", 0, DATESW) \
47 X("message", 0, MESSAGESW) \
48 X("file", 0, FILESW) \
49 X("nofile", 0, NFILESW) \
50 X("-component-name component-text", 0, OTHERSW) \
51 X("dupaddrs", 0, DUPADDRSW) \
52 X("nodupaddrs", 0, NDUPADDRSW) \
53 X("ccme", 0, CCMESW) \
54 X("noccme", 0, NCCMESW) \
55 X("outsize size-in-characters", 0, OUTSIZESW) \
56 X("width column-width", 0, WIDTHSW) \
57 X("msgnum number", 0, MSGNUMSW) \
58 X("msgcur flag", 0, MSGCURSW) \
59 X("msgsize size", 0, MSGSIZESW) \
60 X("unseen flag", 0, UNSEENSW) \
61 X("dump", 0, DUMPSW) \
62 X("nodump", 0, NDUMPSW) \
63 X("trace", 0, TRACESW) \
64 X("notrace", 0, NTRACESW) \
65 X("version", 0, VERSIONSW) \
66 X("help", 0, HELPSW) \
68 #define X(sw, minchars, id) id,
69 DEFINE_SWITCH_ENUM(FMTTEST
);
72 #define X(sw, minchars, id) { sw, minchars, id },
73 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
77 * An array containing labels used for branch instructions
80 static struct format
**lvec
= NULL
;
82 static int lallocated
= 0;
84 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
85 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
86 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
89 * Context structure used by the tracing routines
92 struct trace_context
{
101 static void fmt_dump (char *, struct format
*);
102 static void dumpone(struct format
*);
103 static void initlabels(struct format
*);
104 static int findlabel(struct format
*);
105 static void assignlabel(struct format
*);
106 static char *f_typestr(int);
107 static char *c_typestr(int);
108 static char *c_flagsstr(int);
109 static void litputs(const char *);
110 static void litputc(char);
111 static void process_addresses(struct format
*, struct msgs_array
*,
112 charstring_t
, int, int *,
113 struct fmt_callbacks
*);
114 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
115 int, int *, struct fmt_callbacks
*);
116 static void process_messages(struct format
*, struct msgs_array
*,
117 struct msgs_array
*, charstring_t
, char *, int,
118 int, int *, struct fmt_callbacks
*);
119 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
120 struct format
*, charstring_t
, int,
121 struct fmt_callbacks
*);
122 static void test_trace(void *, struct format
*, int, char *, const char *);
123 static char *test_formataddr(char *, char *);
124 static char *test_concataddr(char *, char *);
125 static int insert(struct mailname
*);
126 static void mlistfree(void);
128 static bool nodupcheck
; /* If set, no check for duplicates */
129 static bool ccme
; /* Should I cc myself? */
130 static struct mailname mq
; /* Mail addresses to check for duplicates */
131 static char *dummy
= "dummy";
134 main (int argc
, char **argv
)
136 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
137 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
141 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
145 bool dupaddrs
= true;
148 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
149 enum mode_t mode
= MESSAGE
;
151 struct fmt_callbacks cb
, *cbp
= NULL
;
153 if (nmh_init(argv
[0], true, true)) { return 1; }
155 arguments
= getarguments (invo_name
, argc
, argv
, 1);
158 while ((cp
= *argp
++)) {
161 * A -- means that we have a component name (like pick);
162 * save the component name and the next argument for the text.
166 die("missing component name after --");
167 app_msgarg(&compargs
, cp
);
168 /* Grab next argument for component text */
170 die("missing argument to %s", argp
[-2]);
171 app_msgarg(&compargs
, cp
);
174 switch (smatch (cp
, switches
)) {
176 ambigsw (cp
, switches
);
179 die("-%s unknown", cp
);
182 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
183 print_help (buf
, switches
, 1);
186 print_version(invo_name
);
189 die("internal argument error!");
193 if (!(cp
= *argp
++) || *cp
== '-')
194 die("missing argument to %s", argp
[-2]);
195 if (strcmp(cp
, "max") == 0)
196 outputsize
= INT_MAX
;
197 else if (strcmp(cp
, "width") == 0)
198 outputsize
= sc_width();
200 outputsize
= atoi(cp
);
204 if (!(form
= *argp
++) || *form
== '-')
205 die("missing argument to %s", argp
[-2]);
209 if (!(format
= *argp
++) || *format
== '-')
210 die("missing argument to %s", argp
[-2]);
223 defformat
= DEFADDRFORMAT
;
235 defformat
= DEFDATEFORMAT
;
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]);
270 if (!(cp
= *argp
++) || *cp
== '-')
271 die("missing argument to %s", argp
[-2]);
275 if (!(cp
= *argp
++) || *cp
== '-')
276 die("missing argument to %s", argp
[-2]);
280 if (!(cp
= *argp
++) || *cp
== '-')
281 die("missing argument to %s", argp
[-2]);
282 msgunseen
= atoi(cp
);
296 * Only interpret as a folder if we're in message mode
299 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
301 die("only one folder at a time!");
302 folder
= pluspath (cp
);
304 app_msgarg(&msgs
, cp
);
308 * Here's our weird heuristic:
310 * - We allow -dump without any other arguments.
311 * - If you've given any component arguments, we don't require any
313 * - The arguments are interpreted as folders/messages _if_ we're in
314 * message mode, otherwise pass as strings in the text component.
317 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
318 die("usage: [switches] [+folder] msgs | strings...");
322 * If you're picking "raw" as a mode, then you have to select
326 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
327 die("You must specify a format with -form or -format when "
332 * Get new format string. Must be before chdir().
334 nfs
= new_fs (form
, format
, defformat
);
335 (void) fmt_compile(nfs
, &fmt
, 1);
341 if (compargs
.size
== 0 && msgs
.size
== 0)
346 buffer
= charstring_create(BUFSIZ
);
348 if (outputsize
== 0) {
350 outputsize
= sc_width();
352 outputsize
= INT_MAX
;
358 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
362 * If we want to provide our own formataddr, concactaddr, or tracing
363 * callback, do that now. Also, prime ismymbox if we use it.
366 if (!dupaddrs
|| trace
) {
371 cb
.formataddr
= test_formataddr
;
372 cb
.concataddr
= test_concataddr
;
378 struct trace_context
*ctx
;
383 ctx
->outbuf
= mh_xstrdup("");
385 cb
.trace_func
= test_trace
;
386 cb
.trace_context
= ctx
;
390 if (mode
== MESSAGE
) {
391 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
395 for (i
= 0; i
< compargs
.size
; i
+= 2) {
396 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
398 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
402 if (mode
== ADDRESS
) {
403 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
404 } else /* Fall-through for RAW or DATE */
405 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
408 charstring_free(buffer
);
416 * Process each address with fmt_scan().
422 struct pqpair
*pq_next
;
426 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
427 charstring_t buffer
, int outwidth
, int *dat
,
428 struct fmt_callbacks
*cb
)
431 char *cp
, error
[BUFSIZ
];
433 struct pqpair
*p
, *q
;
446 for (i
= 0; i
< addrs
->size
; i
++) {
447 (q
= &pq
)->pq_next
= NULL
;
448 while ((cp
= getname(addrs
->msgs
[i
]))) {
450 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
451 p
->pq_text
= mh_xstrdup(cp
);
452 p
->pq_error
= mh_xstrdup(error
);
454 p
->pq_text
= getcpy(mp
->m_text
);
457 q
= (q
->pq_next
= p
);
460 for (p
= pq
.pq_next
; p
; p
= q
) {
461 c
= fmt_findcomp("text");
464 c
->c_text
= p
->pq_text
;
467 c
= fmt_findcomp("error");
470 c
->c_text
= p
->pq_error
;
474 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
475 fputs(charstring_buffer(buffer
), stdout
);
476 charstring_clear(buffer
);
488 * Process messages and run them through the format engine. A lot taken
493 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
494 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
495 int outwidth
, int files
, int *dat
,
496 struct fmt_callbacks
*cb
)
498 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
499 int num_unseen_seq
= 0;
500 ivector_t seqnum
= ivector_create (0);
506 * If 'files' is set, short-circuit everything else and just process
511 for (i
= 0; i
< msgs
->size
; i
++) {
512 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
513 admonish(cp
, "unable to open file");
516 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
524 folder
= getfolder(1);
526 maildir
= m_maildir(folder
);
528 if (chdir(maildir
) < 0)
529 adios(maildir
, "unable to change directory to");
531 if (!(mp
= folder_read(folder
, 1)))
532 die("unable to read folder %s", folder
);
535 die("no messages in %s", folder
);
537 for (i
= 0; i
< msgs
->size
; i
++)
538 if (!m_convert(mp
, msgs
->msgs
[i
]))
540 seq_setprev(mp
); /* set the Previous-Sequence */
542 context_replace(pfolder
, folder
); /* update current folder */
543 seq_save(mp
); /* synchronize message sequences */
544 context_save(); /* save the context file */
547 * We want to set the unseen flag if requested, so we have to check
548 * the unseen sequence as well.
552 if ((cp
= context_find(usequence
)) && *cp
) {
556 ap
= brkstring(dp
, " ", "\n");
557 for (i
= 0; ap
&& *ap
; i
++, ap
++)
558 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
565 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
566 if (is_selected(mp
, msgnum
)) {
567 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
568 admonish(cp
, "unable to open message");
578 dat
[1] = msgnum
== mp
->curmsg
;
581 * Check to see if this is in the unseen sequence
585 for (i
= 0; i
< num_unseen_seq
; i
++) {
586 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
593 * Read in the message and process the components
596 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
601 ivector_free (seqnum
);
606 * Process a single file in message mode
610 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
611 struct format
*fmt
, charstring_t buffer
, int outwidth
,
612 struct fmt_callbacks
*cb
)
615 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
616 m_getfld_state_t gstate
;
621 * Get our size if we didn't include one
627 if (fstat(fileno(in
), &st
) < 0)
634 * Initialize everything else
645 * Read in the message and process the components
648 gstate
= m_getfld_state_init(in
);
650 bufsz
= sizeof(rbuf
);
651 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
655 i
= fmt_addcomptext(name
, rbuf
);
657 while (state
== FLDPLUS
) {
658 bufsz
= sizeof(rbuf
);
659 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
660 fmt_appendcomp(i
, name
, rbuf
);
664 while (state
== FLDPLUS
) {
665 bufsz
= sizeof(rbuf
);
666 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
671 if (fmt_findcomp("body")) {
672 if ((i
= strlen(rbuf
)) < outwidth
) {
673 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
674 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
677 fmt_addcomptext("body", rbuf
);
687 m_getfld_state_destroy(&gstate
);
690 * Do this now to override any components in the original message
693 for (i
= 0; i
< comps
->size
; i
+= 2) {
694 c
= fmt_findcomp(comps
->msgs
[i
]);
697 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
701 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
702 fputs(charstring_buffer (buffer
), stdout
);
703 charstring_clear(buffer
);
708 * Run text through the format engine with no special processing
712 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
713 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
727 c
= fmt_findcomp("text");
729 for (i
= 0; i
< text
->size
; i
++) {
732 c
->c_text
= getcpy(text
->msgs
[i
]);
735 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
736 fputs(charstring_buffer (buffer
), stdout
);
737 charstring_clear(buffer
);
743 * Our basic tracing support callback.
745 * Print out each instruction as it's executed, including the values of
746 * the num and str registers if they've changed.
750 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
753 struct trace_context
*ctx
= (struct trace_context
*) context
;
754 bool changed
= false;
758 if (num
!= ctx
->num
) {
759 printf("num=%d", num
);
764 if (str
!= ctx
->str
) {
768 fputs("str=", stdout
);
776 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
777 fputs("outbuf=", stdout
);
781 ctx
->outbuf
= mh_xstrdup(outbuf
);
786 fmt_dump (char *nfs
, struct format
*fmth
)
790 printf("Instruction dump of format string: \n%s\n", nfs
);
793 for (fmt
= fmth
; fmt
; ++fmt
) {
795 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
801 dumpone(struct format
*fmt
)
805 if ((i
= findlabel(fmt
)) >= 0)
809 fputs(f_typestr((int)fmt
->f_type
), stdout
);
811 switch (fmt
->f_type
) {
817 fputs(", comp ", stdout
);
818 litputs(fmt
->f_comp
->c_name
);
819 if (fmt
->f_comp
->c_type
)
820 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
821 if (fmt
->f_comp
->c_flags
)
822 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
849 fputs(", c_name ", stdout
);
850 litputs(fmt
->f_comp
->c_name
);
851 if (fmt
->f_comp
->c_type
)
852 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
853 if (fmt
->f_comp
->c_flags
)
854 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
873 fputs(", c_name ", stdout
);
874 litputs(fmt
->f_comp
->c_name
);
875 if (fmt
->f_comp
->c_type
)
876 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
877 if (fmt
->f_comp
->c_flags
)
878 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
882 printf(", width %d, fill '", fmt
->f_width
);
883 litputc(fmt
->f_fill
);
884 fputs("' name ", stdout
);
885 litputs(fmt
->f_comp
->c_name
);
886 if (fmt
->f_comp
->c_type
)
887 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
888 if (fmt
->f_comp
->c_flags
)
889 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
894 printf(", width %d, fill '", fmt
->f_width
);
895 litputc(fmt
->f_fill
);
901 litputs(fmt
->f_text
);
905 printf(", width %d, fill '", fmt
->f_width
);
906 litputc(fmt
->f_fill
);
908 litputs(fmt
->f_text
);
914 litputc(fmt
->f_char
);
923 fputs(" continue else goto", stdout
);
926 i
= findlabel(fmt
+ fmt
->f_skip
);
933 i
= findlabel(fmt
+ fmt
->f_skip
);
934 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
943 case FT_LV_MULTIPLY_L
:
946 printf(" value %d", fmt
->f_value
);
950 fputs(" str ", stdout
);
951 litputs(fmt
->f_text
);
955 fputs(" getenv ", stdout
);
956 litputs(fmt
->f_text
);
959 case FT_LS_DECODECOMP
:
960 fputs(", comp ", stdout
);
961 litputs(fmt
->f_comp
->c_name
);
968 printf(", width %d", fmt
->f_width
);
972 printf(", value dat[%d]", fmt
->f_value
);
979 * Iterate over all instructions and assign labels to the targets of
984 initlabels(struct format
*fmth
)
986 struct format
*fmt
, *addr
;
990 for (fmt
= fmth
; fmt
; ++fmt
) {
1000 addr
= fmt
+ fmt
->f_skip
;
1001 if (findlabel(addr
) < 0)
1004 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
1011 findlabel(struct format
*addr
)
1015 for (i
= 0; i
< lused
; ++i
)
1016 if (addr
== lvec
[i
])
1022 assignlabel(struct format
*addr
)
1024 if (lused
>= lallocated
) {
1026 lvec
= (struct format
**)
1027 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1030 lvec
[lused
++] = addr
;
1036 static char buf
[32];
1039 case FT_COMP
: return "COMP";
1040 case FT_COMPF
: return "COMPF";
1041 case FT_LIT
: return "LIT";
1042 case FT_LITF
: return "LITF";
1043 case FT_CHAR
: return "CHAR";
1044 case FT_NUM
: return "NUM";
1045 case FT_NUMF
: return "NUMF";
1046 case FT_STR
: return "STR";
1047 case FT_STRF
: return "STRF";
1048 case FT_STRFW
: return "STRFW";
1049 case FT_STRLIT
: return "STRLIT";
1050 case FT_STRLITZ
: return "STRLITZ";
1051 case FT_PUTADDR
: return "PUTADDR";
1052 case FT_LS_COMP
: return "LS_COMP";
1053 case FT_LS_LIT
: return "LS_LIT";
1054 case FT_LS_GETENV
: return "LS_GETENV";
1055 case FT_LS_CFIND
: return "LS_CFIND";
1056 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1057 case FT_LS_DECODE
: return "LS_DECODE";
1058 case FT_LS_TRIM
: return "LS_TRIM";
1059 case FT_LV_COMP
: return "LV_COMP";
1060 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1061 case FT_LV_LIT
: return "LV_LIT";
1062 case FT_LV_DAT
: return "LV_DAT";
1063 case FT_LV_STRLEN
: return "LV_STRLEN";
1064 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1065 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1066 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1067 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1068 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1069 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1070 case FT_LS_MONTH
: return "LS_MONTH";
1071 case FT_LS_LMONTH
: return "LS_LMONTH";
1072 case FT_LS_ZONE
: return "LS_ZONE";
1073 case FT_LS_DAY
: return "LS_DAY";
1074 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1075 case FT_LS_822DATE
: return "LS_822DATE";
1076 case FT_LS_PRETTY
: return "LS_PRETTY";
1077 case FT_LS_KILO
: return "LS_KILO";
1078 case FT_LS_KIBI
: return "LS_KIBI";
1079 case FT_LV_SEC
: return "LV_SEC";
1080 case FT_LV_MIN
: return "LV_MIN";
1081 case FT_LV_HOUR
: return "LV_HOUR";
1082 case FT_LV_MDAY
: return "LV_MDAY";
1083 case FT_LV_MON
: return "LV_MON";
1084 case FT_LV_YEAR
: return "LV_YEAR";
1085 case FT_LV_YDAY
: return "LV_YDAY";
1086 case FT_LV_WDAY
: return "LV_WDAY";
1087 case FT_LV_ZONE
: return "LV_ZONE";
1088 case FT_LV_CLOCK
: return "LV_CLOCK";
1089 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1090 case FT_LV_DAYF
: return "LV_DAYF";
1091 case FT_LV_DST
: return "LV_DST";
1092 case FT_LV_ZONEF
: return "LV_ZONEF";
1093 case FT_LS_PERS
: return "LS_PERS";
1094 case FT_LS_MBOX
: return "LS_MBOX";
1095 case FT_LS_HOST
: return "LS_HOST";
1096 case FT_LS_PATH
: return "LS_PATH";
1097 case FT_LS_GNAME
: return "LS_GNAME";
1098 case FT_LS_NOTE
: return "LS_NOTE";
1099 case FT_LS_ADDR
: return "LS_ADDR";
1100 case FT_LS_822ADDR
: return "LS_822ADDR";
1101 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1102 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1103 case FT_LV_INGRPF
: return "LV_INGRPF";
1104 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1105 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1106 case FT_LOCALDATE
: return "LOCALDATE";
1107 case FT_GMTDATE
: return "GMTDATE";
1108 case FT_PARSEDATE
: return "PARSEDATE";
1109 case FT_PARSEADDR
: return "PARSEADDR";
1110 case FT_FORMATADDR
: return "FORMATADDR";
1111 case FT_CONCATADDR
: return "CONCATADDR";
1112 case FT_MYMBOX
: return "MYMBOX";
1113 case FT_GETMYMBOX
: return "GETMYMBOX";
1114 case FT_GETMYADDR
: return "GETMYADDR";
1115 case FT_SAVESTR
: return "SAVESTR";
1116 case FT_DONE
: return "DONE";
1117 case FT_PAUSE
: return "PAUSE";
1118 case FT_NOP
: return "NOP";
1119 case FT_GOTO
: return "GOTO";
1120 case FT_IF_S_NULL
: return "IF_S_NULL";
1121 case FT_IF_S
: return "IF_S";
1122 case FT_IF_V_EQ
: return "IF_V_EQ";
1123 case FT_IF_V_NE
: return "IF_V_NE";
1124 case FT_IF_V_GT
: return "IF_V_GT";
1125 case FT_IF_MATCH
: return "IF_MATCH";
1126 case FT_IF_AMATCH
: return "IF_AMATCH";
1127 case FT_S_NULL
: return "S_NULL";
1128 case FT_S_NONNULL
: return "S_NONNULL";
1129 case FT_V_EQ
: return "V_EQ";
1130 case FT_V_NE
: return "V_NE";
1131 case FT_V_GT
: return "V_GT";
1132 case FT_V_MATCH
: return "V_MATCH";
1133 case FT_V_AMATCH
: return "V_AMATCH";
1135 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1143 static char buf
[64];
1145 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1152 static char buf
[64];
1154 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1159 litputs(const char *s
)
1167 fputs("<nil>", stdout
);
1174 printf("\\x%02x", (unsigned char) c
);
1175 } else if (c
< 0x20 || c
== 0177) {
1179 } else if (c
== '\f') {
1182 } else if (c
== '\n') {
1185 } else if (c
== '\r') {
1188 } else if (c
== '\t') {
1193 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1200 * Routines/code to support the duplicate address suppression code, adapted
1204 static char *buf
; /* our current working buffer */
1205 static char *bufend
; /* end of working buffer */
1206 static char *last_dst
; /* buf ptr at end of last call */
1207 static unsigned int bufsiz
=0; /* current size of buf */
1209 #define BUFINCR 512 /* how much to expand buf when if fills */
1211 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1214 * check if there's enough room in buf for str.
1215 * add more mem if needed
1217 #define CHECKMEM(str) \
1218 if ((len = strlen (str)) >= bufend - dst) {\
1220 int n = last_dst - buf;\
1221 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1222 buf = mh_xrealloc (buf, bufsiz);\
1224 last_dst = buf + n;\
1225 bufend = buf + bufsiz;\
1230 * These are versions of similar routines from replsbr.c; the purpose is
1231 * to suppress duplicate addresses from being added to a list when building
1232 * up addresses for the %(formataddr) format function. This is used by
1233 * repl to prevent duplicate addresses from being added to the "to" line.
1234 * See replsbr.c for more information.
1236 * We can't use the functions in replsbr.c directly because they are slightly
1237 * different and depend on the rest of replsbr.c
1240 test_formataddr (char *orig
, char *str
)
1248 struct mailname
*mp
= NULL
;
1250 /* if we don't have a buffer yet, get one */
1252 buf
= mh_xmalloc (BUFINCR
);
1253 last_dst
= buf
; /* XXX */
1254 bufsiz
= BUFINCR
- 6; /* leave some slop */
1255 bufend
= buf
+ bufsiz
;
1258 * If "orig" points to our buffer we can just pick up where we
1259 * left off. Otherwise we have to copy orig into our buffer.
1263 else if (!orig
|| !*orig
) {
1267 dst
= last_dst
; /* XXX */
1272 /* concatenate all the new addresses onto 'buf' */
1273 for (isgroup
= false; (cp
= getname (str
)); ) {
1274 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1275 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1278 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1283 /* if we get here we're going to add an address */
1289 CHECKMEM (mp
->m_gname
);
1293 sp
= adrformat (mp
);
1309 * The companion to test_formataddr(); it behaves the same way, except doesn't
1310 * do duplicate address detection.
1313 test_concataddr(char *orig
, char *str
)
1318 cp
= test_formataddr(orig
, str
);
1324 insert (struct mailname
*np
)
1326 struct mailname
*mp
;
1331 if (np
->m_mbox
== NULL
)
1334 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1335 if (!strcasecmp (FENDNULL(np
->m_host
),
1336 FENDNULL(mp
->m_next
->m_host
)) &&
1337 !strcasecmp (FENDNULL(np
->m_mbox
),
1338 FENDNULL(mp
->m_next
->m_mbox
)))
1341 if (!ccme
&& ismymbox (np
))
1350 * Reset our duplicate address list
1356 struct mailname
*mp
, *mp2
;
1358 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {