]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
1076be4841aefc0957f8da08c9d9297b07ab8ee9
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
};
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 bool outputsize_given
= false;
159 while ((cp
= *argp
++)) {
162 * A -- means that we have a component name (like pick);
163 * save the component name and the next argument for the text.
167 die("missing component name after --");
168 app_msgarg(&compargs
, cp
);
169 /* Grab next argument for component text */
171 die("missing argument to %s", argp
[-2]);
172 app_msgarg(&compargs
, cp
);
175 switch (smatch (cp
, switches
)) {
177 ambigsw (cp
, switches
);
180 die("-%s unknown", cp
);
183 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
184 print_help (buf
, switches
, 1);
187 print_version(invo_name
);
190 die("internal argument error!");
194 outputsize_given
= true;
195 if (!(cp
= *argp
++) || *cp
== '-')
196 die("missing argument to %s", argp
[-2]);
197 if (strcmp(cp
, "max") == 0)
198 outputsize
= INT_MAX
;
199 else if (strcmp(cp
, "width") == 0)
200 outputsize
= sc_width();
202 outputsize
= atoi(cp
);
206 if (!(form
= *argp
++) || *form
== '-')
207 die("missing argument to %s", argp
[-2]);
211 if (!(format
= *argp
++) || *format
== '-')
212 die("missing argument to %s", argp
[-2]);
225 defformat
= DEFADDRFORMAT
;
237 defformat
= DEFDATEFORMAT
;
262 if (!(cp
= *argp
++) || *cp
== '-')
263 die("missing argument to %s", argp
[-2]);
267 if (!(cp
= *argp
++) || *cp
== '-')
268 die("missing argument to %s", argp
[-2]);
272 if (!(cp
= *argp
++) || *cp
== '-')
273 die("missing argument to %s", argp
[-2]);
277 if (!(cp
= *argp
++) || *cp
== '-')
278 die("missing argument to %s", argp
[-2]);
282 if (!(cp
= *argp
++) || *cp
== '-')
283 die("missing argument to %s", argp
[-2]);
284 msgunseen
= atoi(cp
);
298 * Only interpret as a folder if we're in message mode
301 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
303 die("only one folder at a time!");
304 folder
= pluspath (cp
);
306 app_msgarg(&msgs
, cp
);
310 * Here's our weird heuristic:
312 * - We allow -dump without any other arguments.
313 * - If you've given any component arguments, we don't require any
315 * - The arguments are interpreted as folders/messages _if_ we're in
316 * message mode, otherwise pass as strings in the text component.
319 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
320 die("usage: [switches] [+folder] msgs | strings...");
324 * If you're picking "raw" as a mode, then you have to select
328 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
329 die("You must specify a format with -form or -format when "
334 * Get new format string. Must be before chdir().
336 nfs
= new_fs (form
, format
, defformat
);
337 (void) fmt_compile(nfs
, &fmt
, 1);
343 if (compargs
.size
== 0 && msgs
.size
== 0)
348 buffer
= charstring_create(BUFSIZ
);
350 if (!outputsize_given
) {
351 outputsize
= mode
== MESSAGE
? sc_width() : 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
);
475 charstring_clear(buffer
);
487 * Process messages and run them through the format engine. A lot taken
492 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
493 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
494 int outwidth
, int files
, int *dat
,
495 struct fmt_callbacks
*cb
)
497 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
498 int num_unseen_seq
= 0;
499 ivector_t seqnum
= ivector_create (0);
505 * If 'files' is set, short-circuit everything else and just process
510 for (i
= 0; i
< msgs
->size
; i
++) {
511 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
512 admonish(cp
, "unable to open file");
515 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
523 folder
= getfolder(1);
525 maildir
= m_maildir(folder
);
527 if (chdir(maildir
) < 0)
528 adios(maildir
, "unable to change directory to");
530 if (!(mp
= folder_read(folder
, 1)))
531 die("unable to read folder %s", folder
);
534 die("no messages in %s", folder
);
536 for (i
= 0; i
< msgs
->size
; i
++)
537 if (!m_convert(mp
, msgs
->msgs
[i
]))
539 seq_setprev(mp
); /* set the Previous-Sequence */
541 context_replace(pfolder
, folder
); /* update current folder */
542 seq_save(mp
); /* synchronize message sequences */
543 context_save(); /* save the context file */
546 * We want to set the unseen flag if requested, so we have to check
547 * the unseen sequence as well.
551 if ((cp
= context_find(usequence
)) && *cp
) {
555 ap
= brkstring(dp
, " ", "\n");
556 for (i
= 0; ap
&& *ap
; i
++, ap
++)
557 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
564 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
565 if (is_selected(mp
, msgnum
)) {
566 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
567 admonish(cp
, "unable to open message");
577 dat
[1] = msgnum
== mp
->curmsg
;
580 * Check to see if this is in the unseen sequence
584 for (i
= 0; i
< num_unseen_seq
; i
++) {
585 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
592 * Read in the message and process the components
595 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
600 ivector_free (seqnum
);
605 * Process a single file in message mode
609 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
610 struct format
*fmt
, charstring_t buffer
, int outwidth
,
611 struct fmt_callbacks
*cb
)
614 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
615 m_getfld_state_t gstate
;
620 * Get our size if we didn't include one
626 if (fstat(fileno(in
), &st
) < 0)
633 * Initialize everything else
644 * Read in the message and process the components
647 gstate
= m_getfld_state_init(in
);
649 bufsz
= sizeof(rbuf
);
650 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
654 i
= fmt_addcomptext(name
, rbuf
);
656 while (state
== FLDPLUS
) {
657 bufsz
= sizeof(rbuf
);
658 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
659 fmt_appendcomp(i
, name
, rbuf
);
663 while (state
== FLDPLUS
) {
664 bufsz
= sizeof(rbuf
);
665 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
670 if (fmt_findcomp("body")) {
671 if ((i
= strlen(rbuf
)) < outwidth
) {
672 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
673 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
676 fmt_addcomptext("body", rbuf
);
686 m_getfld_state_destroy(&gstate
);
689 * Do this now to override any components in the original message
692 for (i
= 0; i
< comps
->size
; i
+= 2) {
693 c
= fmt_findcomp(comps
->msgs
[i
]);
696 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
700 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
701 fputs(charstring_buffer (buffer
), stdout
);
702 charstring_clear(buffer
);
707 * Run text through the format engine with no special processing
711 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
712 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
726 c
= fmt_findcomp("text");
728 for (i
= 0; i
< text
->size
; i
++) {
731 c
->c_text
= getcpy(text
->msgs
[i
]);
734 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
735 fputs(charstring_buffer (buffer
), stdout
);
736 charstring_clear(buffer
);
742 * Our basic tracing support callback.
744 * Print out each instruction as it's executed, including the values of
745 * the num and str registers if they've changed.
749 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
752 struct trace_context
*ctx
= (struct trace_context
*) context
;
753 bool changed
= false;
757 if (num
!= ctx
->num
) {
758 printf("num=%d", num
);
763 if (str
!= ctx
->str
) {
767 fputs("str=", stdout
);
775 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
776 fputs("outbuf=", stdout
);
780 ctx
->outbuf
= mh_xstrdup(outbuf
);
785 fmt_dump (char *nfs
, struct format
*fmth
)
789 printf("Instruction dump of format string: \n%s\n", nfs
);
792 for (fmt
= fmth
; fmt
; ++fmt
) {
794 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
800 dumpone(struct format
*fmt
)
804 if ((i
= findlabel(fmt
)) >= 0)
808 fputs(f_typestr((int)fmt
->f_type
), stdout
);
810 switch (fmt
->f_type
) {
816 fputs(", comp ", stdout
);
817 litputs(fmt
->f_comp
->c_name
);
818 if (fmt
->f_comp
->c_type
)
819 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
820 if (fmt
->f_comp
->c_flags
)
821 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
848 fputs(", c_name ", stdout
);
849 litputs(fmt
->f_comp
->c_name
);
850 if (fmt
->f_comp
->c_type
)
851 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
852 if (fmt
->f_comp
->c_flags
)
853 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
872 fputs(", c_name ", stdout
);
873 litputs(fmt
->f_comp
->c_name
);
874 if (fmt
->f_comp
->c_type
)
875 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
876 if (fmt
->f_comp
->c_flags
)
877 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
881 printf(", width %d, fill '", fmt
->f_width
);
882 litputc(fmt
->f_fill
);
883 fputs("' name ", stdout
);
884 litputs(fmt
->f_comp
->c_name
);
885 if (fmt
->f_comp
->c_type
)
886 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
887 if (fmt
->f_comp
->c_flags
)
888 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
893 printf(", width %d, fill '", fmt
->f_width
);
894 litputc(fmt
->f_fill
);
900 litputs(fmt
->f_text
);
904 printf(", width %d, fill '", fmt
->f_width
);
905 litputc(fmt
->f_fill
);
907 litputs(fmt
->f_text
);
913 litputc(fmt
->f_char
);
922 fputs(" continue else goto", stdout
);
925 i
= findlabel(fmt
+ fmt
->f_skip
);
932 i
= findlabel(fmt
+ fmt
->f_skip
);
933 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
942 case FT_LV_MULTIPLY_L
:
945 printf(" value %d", fmt
->f_value
);
949 fputs(" str ", stdout
);
950 litputs(fmt
->f_text
);
954 fputs(" getenv ", stdout
);
955 litputs(fmt
->f_text
);
958 case FT_LS_DECODECOMP
:
959 fputs(", comp ", stdout
);
960 litputs(fmt
->f_comp
->c_name
);
967 printf(", width %d", fmt
->f_width
);
971 printf(", value dat[%d]", fmt
->f_value
);
978 * Iterate over all instructions and assign labels to the targets of
983 initlabels(struct format
*fmth
)
985 struct format
*fmt
, *addr
;
989 for (fmt
= fmth
; fmt
; ++fmt
) {
999 addr
= fmt
+ fmt
->f_skip
;
1000 if (findlabel(addr
) < 0)
1003 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
1010 findlabel(struct format
*addr
)
1014 for (i
= 0; i
< lused
; ++i
)
1015 if (addr
== lvec
[i
])
1021 assignlabel(struct format
*addr
)
1023 if (lused
>= lallocated
) {
1025 lvec
= (struct format
**)
1026 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1029 lvec
[lused
++] = addr
;
1035 static char buf
[32];
1038 case FT_COMP
: return "COMP";
1039 case FT_COMPF
: return "COMPF";
1040 case FT_LIT
: return "LIT";
1041 case FT_LITF
: return "LITF";
1042 case FT_CHAR
: return "CHAR";
1043 case FT_NUM
: return "NUM";
1044 case FT_NUMF
: return "NUMF";
1045 case FT_STR
: return "STR";
1046 case FT_STRF
: return "STRF";
1047 case FT_STRFW
: return "STRFW";
1048 case FT_STRLIT
: return "STRLIT";
1049 case FT_STRLITZ
: return "STRLITZ";
1050 case FT_PUTADDR
: return "PUTADDR";
1051 case FT_LS_COMP
: return "LS_COMP";
1052 case FT_LS_LIT
: return "LS_LIT";
1053 case FT_LS_GETENV
: return "LS_GETENV";
1054 case FT_LS_CFIND
: return "LS_CFIND";
1055 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1056 case FT_LS_DECODE
: return "LS_DECODE";
1057 case FT_LS_TRIM
: return "LS_TRIM";
1058 case FT_LV_COMP
: return "LV_COMP";
1059 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1060 case FT_LV_LIT
: return "LV_LIT";
1061 case FT_LV_DAT
: return "LV_DAT";
1062 case FT_LV_STRLEN
: return "LV_STRLEN";
1063 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1064 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1065 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1066 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1067 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1068 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1069 case FT_LS_MONTH
: return "LS_MONTH";
1070 case FT_LS_LMONTH
: return "LS_LMONTH";
1071 case FT_LS_ZONE
: return "LS_ZONE";
1072 case FT_LS_DAY
: return "LS_DAY";
1073 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1074 case FT_LS_822DATE
: return "LS_822DATE";
1075 case FT_LS_PRETTY
: return "LS_PRETTY";
1076 case FT_LS_KILO
: return "LS_KILO";
1077 case FT_LS_KIBI
: return "LS_KIBI";
1078 case FT_LV_SEC
: return "LV_SEC";
1079 case FT_LV_MIN
: return "LV_MIN";
1080 case FT_LV_HOUR
: return "LV_HOUR";
1081 case FT_LV_MDAY
: return "LV_MDAY";
1082 case FT_LV_MON
: return "LV_MON";
1083 case FT_LV_YEAR
: return "LV_YEAR";
1084 case FT_LV_YDAY
: return "LV_YDAY";
1085 case FT_LV_WDAY
: return "LV_WDAY";
1086 case FT_LV_ZONE
: return "LV_ZONE";
1087 case FT_LV_CLOCK
: return "LV_CLOCK";
1088 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1089 case FT_LV_DAYF
: return "LV_DAYF";
1090 case FT_LV_DST
: return "LV_DST";
1091 case FT_LV_ZONEF
: return "LV_ZONEF";
1092 case FT_LS_PERS
: return "LS_PERS";
1093 case FT_LS_MBOX
: return "LS_MBOX";
1094 case FT_LS_HOST
: return "LS_HOST";
1095 case FT_LS_PATH
: return "LS_PATH";
1096 case FT_LS_GNAME
: return "LS_GNAME";
1097 case FT_LS_NOTE
: return "LS_NOTE";
1098 case FT_LS_ADDR
: return "LS_ADDR";
1099 case FT_LS_822ADDR
: return "LS_822ADDR";
1100 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1101 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1102 case FT_LV_INGRPF
: return "LV_INGRPF";
1103 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1104 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1105 case FT_LOCALDATE
: return "LOCALDATE";
1106 case FT_GMTDATE
: return "GMTDATE";
1107 case FT_PARSEDATE
: return "PARSEDATE";
1108 case FT_PARSEADDR
: return "PARSEADDR";
1109 case FT_FORMATADDR
: return "FORMATADDR";
1110 case FT_CONCATADDR
: return "CONCATADDR";
1111 case FT_MYMBOX
: return "MYMBOX";
1112 case FT_GETMYMBOX
: return "GETMYMBOX";
1113 case FT_GETMYADDR
: return "GETMYADDR";
1114 case FT_SAVESTR
: return "SAVESTR";
1115 case FT_DONE
: return "DONE";
1116 case FT_PAUSE
: return "PAUSE";
1117 case FT_NOP
: return "NOP";
1118 case FT_GOTO
: return "GOTO";
1119 case FT_IF_S_NULL
: return "IF_S_NULL";
1120 case FT_IF_S
: return "IF_S";
1121 case FT_IF_V_EQ
: return "IF_V_EQ";
1122 case FT_IF_V_NE
: return "IF_V_NE";
1123 case FT_IF_V_GT
: return "IF_V_GT";
1124 case FT_IF_MATCH
: return "IF_MATCH";
1125 case FT_IF_AMATCH
: return "IF_AMATCH";
1126 case FT_S_NULL
: return "S_NULL";
1127 case FT_S_NONNULL
: return "S_NONNULL";
1128 case FT_V_EQ
: return "V_EQ";
1129 case FT_V_NE
: return "V_NE";
1130 case FT_V_GT
: return "V_GT";
1131 case FT_V_MATCH
: return "V_MATCH";
1132 case FT_V_AMATCH
: return "V_AMATCH";
1134 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1142 static char buf
[64];
1144 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1151 static char buf
[64];
1153 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1158 litputs(const char *s
)
1166 fputs("<nil>", stdout
);
1173 printf("\\x%02x", (unsigned char) c
);
1174 } else if (c
< 0x20 || c
== 0177) {
1178 } else if (c
== '\f') {
1181 } else if (c
== '\n') {
1184 } else if (c
== '\r') {
1187 } else if (c
== '\t') {
1192 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1199 * Routines/code to support the duplicate address suppression code, adapted
1203 static char *buf
; /* our current working buffer */
1204 static char *bufend
; /* end of working buffer */
1205 static char *last_dst
; /* buf ptr at end of last call */
1206 static unsigned int bufsiz
=0; /* current size of buf */
1208 #define BUFINCR 512 /* how much to expand buf when if fills */
1210 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1213 * check if there's enough room in buf for str.
1214 * add more mem if needed
1216 #define CHECKMEM(str) \
1217 if ((len = strlen (str)) >= bufend - dst) {\
1219 int n = last_dst - buf;\
1220 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1221 buf = mh_xrealloc (buf, bufsiz);\
1223 last_dst = buf + n;\
1224 bufend = buf + bufsiz;\
1229 * These are versions of similar routines from replsbr.c; the purpose is
1230 * to suppress duplicate addresses from being added to a list when building
1231 * up addresses for the %(formataddr) format function. This is used by
1232 * repl to prevent duplicate addresses from being added to the "to" line.
1233 * See replsbr.c for more information.
1235 * We can't use the functions in replsbr.c directly because they are slightly
1236 * different and depend on the rest of replsbr.c
1239 test_formataddr (char *orig
, char *str
)
1247 struct mailname
*mp
= NULL
;
1249 /* if we don't have a buffer yet, get one */
1251 buf
= mh_xmalloc (BUFINCR
);
1252 last_dst
= buf
; /* XXX */
1253 bufsiz
= BUFINCR
- 6; /* leave some slop */
1254 bufend
= buf
+ bufsiz
;
1257 * If "orig" points to our buffer we can just pick up where we
1258 * left off. Otherwise we have to copy orig into our buffer.
1262 else if (!orig
|| !*orig
) {
1266 dst
= last_dst
; /* XXX */
1271 /* concatenate all the new addresses onto 'buf' */
1272 for (isgroup
= false; (cp
= getname (str
)); ) {
1273 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1274 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1277 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1282 /* if we get here we're going to add an address */
1288 CHECKMEM (mp
->m_gname
);
1292 sp
= adrformat (mp
);
1308 * The companion to test_formataddr(); it behaves the same way, except doesn't
1309 * do duplicate address detection.
1312 test_concataddr(char *orig
, char *str
)
1317 cp
= test_formataddr(orig
, str
);
1323 insert (struct mailname
*np
)
1325 struct mailname
*mp
;
1330 if (np
->m_mbox
== NULL
)
1333 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1334 if (!strcasecmp (FENDNULL(np
->m_host
),
1335 FENDNULL(mp
->m_next
->m_host
)) &&
1336 !strcasecmp (FENDNULL(np
->m_mbox
),
1337 FENDNULL(mp
->m_next
->m_mbox
)))
1340 if (!ccme
&& ismymbox (np
))
1349 * Reset our duplicate address list
1355 struct mailname
*mp
, *mp2
;
1357 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {