]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
1 /* fmttest.c -- A program to help test and debug format instructions
3 * This code is Copyright (c) 2012, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/seq_setprev.h"
10 #include "sbr/seq_save.h"
11 #include "sbr/smatch.h"
12 #include "sbr/snprintb.h"
13 #include "sbr/getcpy.h"
14 #include "sbr/m_convert.h"
15 #include "sbr/getfolder.h"
16 #include "sbr/folder_read.h"
17 #include "sbr/folder_free.h"
18 #include "sbr/context_save.h"
19 #include "sbr/context_replace.h"
20 #include "sbr/context_find.h"
21 #include "sbr/brkstring.h"
22 #include "sbr/ambigsw.h"
24 #include "sbr/print_version.h"
25 #include "sbr/print_help.h"
26 #include "sbr/seq_getnum.h"
27 #include "sbr/error.h"
28 #include "h/fmt_scan.h"
29 #include "h/fmt_compile.h"
31 #include "h/scansbr.h"
32 #include "h/addrsbr.h"
34 #include "sbr/m_maildir.h"
35 #include "sbr/terminal.h"
37 #define FMTTEST_SWITCHES \
38 X("form formatfile", 0, FORMSW) \
39 X("format string", 5, FMTSW) \
40 X("address", 0, ADDRSW) \
42 X("date", 0, DATESW) \
43 X("message", 0, MESSAGESW) \
44 X("file", 0, FILESW) \
45 X("nofile", 0, NFILESW) \
46 X("-component-name component-text", 0, OTHERSW) \
47 X("dupaddrs", 0, DUPADDRSW) \
48 X("nodupaddrs", 0, NDUPADDRSW) \
49 X("ccme", 0, CCMESW) \
50 X("noccme", 0, NCCMESW) \
51 X("outsize size-in-characters", 0, OUTSIZESW) \
52 X("width column-width", 0, WIDTHSW) \
53 X("msgnum number", 0, MSGNUMSW) \
54 X("msgcur flag", 0, MSGCURSW) \
55 X("msgsize size", 0, MSGSIZESW) \
56 X("unseen flag", 0, UNSEENSW) \
57 X("dump", 0, DUMPSW) \
58 X("nodump", 0, NDUMPSW) \
59 X("trace", 0, TRACESW) \
60 X("notrace", 0, NTRACESW) \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
64 #define X(sw, minchars, id) id,
65 DEFINE_SWITCH_ENUM(FMTTEST
);
68 #define X(sw, minchars, id) { sw, minchars, id },
69 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
73 * An array containing labels used for branch instructions
76 static struct format
**lvec
= NULL
;
78 static int lallocated
= 0;
80 enum mode_t
{ MESSAGE
, ADDRESS
, DATE
, RAW
};
81 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
82 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
85 * Context structure used by the tracing routines
88 struct trace_context
{
97 static void fmt_dump (char *, struct format
*);
98 static void dumpone(struct format
*);
99 static void initlabels(struct format
*);
100 static int findlabel(struct format
*);
101 static void assignlabel(struct format
*);
102 static char *f_typestr(int);
103 static char *c_typestr(int);
104 static char *c_flagsstr(int);
105 static void litputs(const char *);
106 static void litputc(char);
107 static void process_addresses(struct format
*, struct msgs_array
*,
108 charstring_t
, int, int *,
109 struct fmt_callbacks
*);
110 static void process_raw(struct format
*, struct msgs_array
*, charstring_t
,
111 int, int *, struct fmt_callbacks
*);
112 static void process_messages(struct format
*, struct msgs_array
*,
113 struct msgs_array
*, charstring_t
, char *, int,
114 int, int *, struct fmt_callbacks
*);
115 static void process_single_file(FILE *, struct msgs_array
*, int *, int,
116 struct format
*, charstring_t
, int,
117 struct fmt_callbacks
*);
118 static void test_trace(void *, struct format
*, int, char *, const char *);
119 static char *test_formataddr(char *, char *);
120 static char *test_concataddr(char *, char *);
121 static int insert(struct mailname
*);
122 static void mlistfree(void);
124 static bool nodupcheck
; /* If set, no check for duplicates */
125 static bool ccme
; /* Should I cc myself? */
126 static struct mailname mq
; /* Mail addresses to check for duplicates */
127 static char *dummy
= "dummy";
130 main (int argc
, char **argv
)
132 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
133 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
137 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
141 bool dupaddrs
= true;
144 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
145 enum mode_t mode
= MESSAGE
;
147 struct fmt_callbacks cb
, *cbp
= NULL
;
149 if (nmh_init(argv
[0], true, true)) { return 1; }
151 arguments
= getarguments (invo_name
, argc
, argv
, 1);
154 while ((cp
= *argp
++)) {
157 * A -- means that we have a component name (like pick);
158 * save the component name and the next argument for the text.
162 die("missing component name after --");
163 app_msgarg(&compargs
, cp
);
164 /* Grab next argument for component text */
166 die("missing argument to %s", argp
[-2]);
167 app_msgarg(&compargs
, cp
);
170 switch (smatch (cp
, switches
)) {
172 ambigsw (cp
, switches
);
175 die("-%s unknown", cp
);
178 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
179 print_help (buf
, switches
, 1);
182 print_version(invo_name
);
185 die("internal argument error!");
189 if (!(cp
= *argp
++) || *cp
== '-')
190 die("missing argument to %s", argp
[-2]);
191 if (strcmp(cp
, "max") == 0)
192 outputsize
= INT_MAX
;
193 else if (strcmp(cp
, "width") == 0)
194 outputsize
= sc_width();
196 outputsize
= atoi(cp
);
200 if (!(form
= *argp
++) || *form
== '-')
201 die("missing argument to %s", argp
[-2]);
205 if (!(format
= *argp
++) || *format
== '-')
206 die("missing argument to %s", argp
[-2]);
219 defformat
= DEFADDRFORMAT
;
231 defformat
= DEFDATEFORMAT
;
256 if (!(cp
= *argp
++) || *cp
== '-')
257 die("missing argument to %s", argp
[-2]);
261 if (!(cp
= *argp
++) || *cp
== '-')
262 die("missing argument to %s", argp
[-2]);
266 if (!(cp
= *argp
++) || *cp
== '-')
267 die("missing argument to %s", argp
[-2]);
271 if (!(cp
= *argp
++) || *cp
== '-')
272 die("missing argument to %s", argp
[-2]);
276 if (!(cp
= *argp
++) || *cp
== '-')
277 die("missing argument to %s", argp
[-2]);
278 msgunseen
= atoi(cp
);
292 * Only interpret as a folder if we're in message mode
295 if (mode
== MESSAGE
&& !files
&& (*cp
== '+' || *cp
== '@')) {
297 die("only one folder at a time!");
298 folder
= pluspath (cp
);
300 app_msgarg(&msgs
, cp
);
304 * Here's our weird heuristic:
306 * - We allow -dump without any other arguments.
307 * - If you've given any component arguments, we don't require any
309 * - The arguments are interpreted as folders/messages _if_ we're in
310 * message mode, otherwise pass as strings in the text component.
313 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
314 die("usage: [switches] [+folder] msgs | strings...");
318 * If you're picking "raw" as a mode, then you have to select
322 if (mode
== RAW
&& form
== NULL
&& format
== NULL
) {
323 die("You must specify a format with -form or -format when "
328 * Get new format string. Must be before chdir().
330 nfs
= new_fs (form
, format
, defformat
);
331 (void) fmt_compile(nfs
, &fmt
, 1);
337 if (compargs
.size
== 0 && msgs
.size
== 0)
342 buffer
= charstring_create(BUFSIZ
);
344 if (outputsize
== 0) {
346 outputsize
= sc_width();
348 outputsize
= INT_MAX
;
354 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
358 * If we want to provide our own formataddr, concactaddr, or tracing
359 * callback, do that now. Also, prime ismymbox if we use it.
362 if (!dupaddrs
|| trace
) {
367 cb
.formataddr
= test_formataddr
;
368 cb
.concataddr
= test_concataddr
;
374 struct trace_context
*ctx
;
379 ctx
->outbuf
= mh_xstrdup("");
381 cb
.trace_func
= test_trace
;
382 cb
.trace_context
= ctx
;
386 if (mode
== MESSAGE
) {
387 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, outputsize
,
391 for (i
= 0; i
< compargs
.size
; i
+= 2) {
392 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
394 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
398 if (mode
== ADDRESS
) {
399 process_addresses(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
400 } else /* Fall-through for RAW or DATE */
401 process_raw(fmt
, &msgs
, buffer
, outputsize
, dat
, cbp
);
404 charstring_free(buffer
);
412 * Process each address with fmt_scan().
418 struct pqpair
*pq_next
;
422 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
,
423 charstring_t buffer
, int outwidth
, int *dat
,
424 struct fmt_callbacks
*cb
)
427 char *cp
, error
[BUFSIZ
];
429 struct pqpair
*p
, *q
;
442 for (i
= 0; i
< addrs
->size
; i
++) {
443 (q
= &pq
)->pq_next
= NULL
;
444 while ((cp
= getname(addrs
->msgs
[i
]))) {
446 if ((mp
= getm(cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
447 p
->pq_text
= mh_xstrdup(cp
);
448 p
->pq_error
= mh_xstrdup(error
);
450 p
->pq_text
= getcpy(mp
->m_text
);
453 q
= (q
->pq_next
= p
);
456 for (p
= pq
.pq_next
; p
; p
= q
) {
457 c
= fmt_findcomp("text");
460 c
->c_text
= p
->pq_text
;
463 c
= fmt_findcomp("error");
466 c
->c_text
= p
->pq_error
;
470 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
471 fputs(charstring_buffer(buffer
), stdout
);
483 * Process messages and run them through the format engine. A lot taken
488 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
489 struct msgs_array
*msgs
, charstring_t buffer
, char *folder
,
490 int outwidth
, int files
, int *dat
,
491 struct fmt_callbacks
*cb
)
493 int i
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
494 int num_unseen_seq
= 0;
495 ivector_t seqnum
= ivector_create (0);
501 * If 'files' is set, short-circuit everything else and just process
506 for (i
= 0; i
< msgs
->size
; i
++) {
507 if ((in
= fopen(cp
= msgs
->msgs
[i
], "r")) == NULL
) {
508 admonish(cp
, "unable to open file");
511 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
519 folder
= getfolder(1);
521 maildir
= m_maildir(folder
);
523 if (chdir(maildir
) < 0)
524 adios(maildir
, "unable to change directory to");
526 if (!(mp
= folder_read(folder
, 1)))
527 die("unable to read folder %s", folder
);
530 die("no messages in %s", folder
);
532 for (i
= 0; i
< msgs
->size
; i
++)
533 if (!m_convert(mp
, msgs
->msgs
[i
]))
535 seq_setprev(mp
); /* set the Previous-Sequence */
537 context_replace(pfolder
, folder
); /* update current folder */
538 seq_save(mp
); /* synchronize message sequences */
539 context_save(); /* save the context file */
542 * We want to set the unseen flag if requested, so we have to check
543 * the unseen sequence as well.
547 if ((cp
= context_find(usequence
)) && *cp
) {
551 ap
= brkstring(dp
, " ", "\n");
552 for (i
= 0; ap
&& *ap
; i
++, ap
++)
553 ivector_push_back (seqnum
, seq_getnum(mp
, *ap
));
560 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
561 if (is_selected(mp
, msgnum
)) {
562 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
563 admonish(cp
, "unable to open message");
573 dat
[1] = msgnum
== mp
->curmsg
;
576 * Check to see if this is in the unseen sequence
580 for (i
= 0; i
< num_unseen_seq
; i
++) {
581 if (in_sequence(mp
, ivector_at (seqnum
, i
), msgnum
)) {
588 * Read in the message and process the components
591 process_single_file(in
, comps
, dat
, msgsize
, fmt
, buffer
,
596 ivector_free (seqnum
);
601 * Process a single file in message mode
605 process_single_file(FILE *in
, struct msgs_array
*comps
, int *dat
, int msgsize
,
606 struct format
*fmt
, charstring_t buffer
, int outwidth
,
607 struct fmt_callbacks
*cb
)
610 char name
[NAMESZ
], rbuf
[NMH_BUFSIZ
];
611 m_getfld_state_t gstate
;
616 * Get our size if we didn't include one
622 if (fstat(fileno(in
), &st
) < 0)
629 * Initialize everything else
640 * Read in the message and process the components
643 gstate
= m_getfld_state_init(in
);
645 bufsz
= sizeof(rbuf
);
646 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
650 i
= fmt_addcomptext(name
, rbuf
);
652 while (state
== FLDPLUS
) {
653 bufsz
= sizeof(rbuf
);
654 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
655 fmt_appendcomp(i
, name
, rbuf
);
659 while (state
== FLDPLUS
) {
660 bufsz
= sizeof(rbuf
);
661 state
= m_getfld2(&gstate
, name
, rbuf
, &bufsz
);
666 if (fmt_findcomp("body")) {
667 if ((i
= strlen(rbuf
)) < outwidth
) {
668 bufsz
= min (outwidth
, (int) sizeof rbuf
- i
);
669 m_getfld2(&gstate
, name
, rbuf
+ i
, &bufsz
);
672 fmt_addcomptext("body", rbuf
);
682 m_getfld_state_destroy(&gstate
);
685 * Do this now to override any components in the original message
688 for (i
= 0; i
< comps
->size
; i
+= 2) {
689 c
= fmt_findcomp(comps
->msgs
[i
]);
692 c
->c_text
= getcpy(comps
->msgs
[i
+ 1]);
696 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
697 fputs(charstring_buffer (buffer
), stdout
);
702 * Run text through the format engine with no special processing
706 process_raw(struct format
*fmt
, struct msgs_array
*text
, charstring_t buffer
,
707 int outwidth
, int *dat
, struct fmt_callbacks
*cb
)
721 c
= fmt_findcomp("text");
723 for (i
= 0; i
< text
->size
; i
++) {
726 c
->c_text
= getcpy(text
->msgs
[i
]);
729 fmt_scan(fmt
, buffer
, outwidth
, dat
, cb
);
730 fputs(charstring_buffer (buffer
), stdout
);
736 * Our basic tracing support callback.
738 * Print out each instruction as it's executed, including the values of
739 * the num and str registers if they've changed.
743 test_trace(void *context
, struct format
*fmt
, int num
, char *str
,
746 struct trace_context
*ctx
= (struct trace_context
*) context
;
747 bool changed
= false;
751 if (num
!= ctx
->num
) {
752 printf("num=%d", num
);
757 if (str
!= ctx
->str
) {
761 fputs("str=", stdout
);
769 if (strcmp(outbuf
, ctx
->outbuf
) != 0) {
770 fputs("outbuf=", stdout
);
774 ctx
->outbuf
= mh_xstrdup(outbuf
);
779 fmt_dump (char *nfs
, struct format
*fmth
)
783 printf("Instruction dump of format string: \n%s\n", nfs
);
786 for (fmt
= fmth
; fmt
; ++fmt
) {
788 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
794 dumpone(struct format
*fmt
)
798 if ((i
= findlabel(fmt
)) >= 0)
802 fputs(f_typestr((int)fmt
->f_type
), stdout
);
804 switch (fmt
->f_type
) {
810 fputs(", comp ", stdout
);
811 litputs(fmt
->f_comp
->c_name
);
812 if (fmt
->f_comp
->c_type
)
813 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
814 if (fmt
->f_comp
->c_flags
)
815 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
842 fputs(", c_name ", stdout
);
843 litputs(fmt
->f_comp
->c_name
);
844 if (fmt
->f_comp
->c_type
)
845 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
846 if (fmt
->f_comp
->c_flags
)
847 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
866 fputs(", c_name ", stdout
);
867 litputs(fmt
->f_comp
->c_name
);
868 if (fmt
->f_comp
->c_type
)
869 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
870 if (fmt
->f_comp
->c_flags
)
871 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
875 printf(", width %d, fill '", fmt
->f_width
);
876 litputc(fmt
->f_fill
);
877 fputs("' name ", stdout
);
878 litputs(fmt
->f_comp
->c_name
);
879 if (fmt
->f_comp
->c_type
)
880 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
881 if (fmt
->f_comp
->c_flags
)
882 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
887 printf(", width %d, fill '", fmt
->f_width
);
888 litputc(fmt
->f_fill
);
894 litputs(fmt
->f_text
);
898 printf(", width %d, fill '", fmt
->f_width
);
899 litputc(fmt
->f_fill
);
901 litputs(fmt
->f_text
);
907 litputc(fmt
->f_char
);
916 fputs(" continue else goto", stdout
);
919 i
= findlabel(fmt
+ fmt
->f_skip
);
926 i
= findlabel(fmt
+ fmt
->f_skip
);
927 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
936 case FT_LV_MULTIPLY_L
:
939 printf(" value %d", fmt
->f_value
);
943 fputs(" str ", stdout
);
944 litputs(fmt
->f_text
);
948 fputs(" getenv ", stdout
);
949 litputs(fmt
->f_text
);
952 case FT_LS_DECODECOMP
:
953 fputs(", comp ", stdout
);
954 litputs(fmt
->f_comp
->c_name
);
961 printf(", width %d", fmt
->f_width
);
965 printf(", value dat[%d]", fmt
->f_value
);
972 * Iterate over all instructions and assign labels to the targets of
977 initlabels(struct format
*fmth
)
979 struct format
*fmt
, *addr
;
983 for (fmt
= fmth
; fmt
; ++fmt
) {
993 addr
= fmt
+ fmt
->f_skip
;
994 if (findlabel(addr
) < 0)
997 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
1004 findlabel(struct format
*addr
)
1008 for (i
= 0; i
< lused
; ++i
)
1009 if (addr
== lvec
[i
])
1015 assignlabel(struct format
*addr
)
1017 if (lused
>= lallocated
) {
1019 lvec
= (struct format
**)
1020 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
1023 lvec
[lused
++] = addr
;
1029 static char buf
[32];
1032 case FT_COMP
: return "COMP";
1033 case FT_COMPF
: return "COMPF";
1034 case FT_LIT
: return "LIT";
1035 case FT_LITF
: return "LITF";
1036 case FT_CHAR
: return "CHAR";
1037 case FT_NUM
: return "NUM";
1038 case FT_NUMF
: return "NUMF";
1039 case FT_STR
: return "STR";
1040 case FT_STRF
: return "STRF";
1041 case FT_STRFW
: return "STRFW";
1042 case FT_STRLIT
: return "STRLIT";
1043 case FT_STRLITZ
: return "STRLITZ";
1044 case FT_PUTADDR
: return "PUTADDR";
1045 case FT_LS_COMP
: return "LS_COMP";
1046 case FT_LS_LIT
: return "LS_LIT";
1047 case FT_LS_GETENV
: return "LS_GETENV";
1048 case FT_LS_CFIND
: return "LS_CFIND";
1049 case FT_LS_DECODECOMP
: return "LS_DECODECOMP";
1050 case FT_LS_DECODE
: return "LS_DECODE";
1051 case FT_LS_TRIM
: return "LS_TRIM";
1052 case FT_LV_COMP
: return "LV_COMP";
1053 case FT_LV_COMPFLAG
: return "LV_COMPFLAG";
1054 case FT_LV_LIT
: return "LV_LIT";
1055 case FT_LV_DAT
: return "LV_DAT";
1056 case FT_LV_STRLEN
: return "LV_STRLEN";
1057 case FT_LV_PLUS_L
: return "LV_PLUS_L";
1058 case FT_LV_MINUS_L
: return "LV_MINUS_L";
1059 case FT_LV_MULTIPLY_L
: return "LV_MULTIPLY_L";
1060 case FT_LV_DIVIDE_L
: return "LV_DIVIDE_L";
1061 case FT_LV_MODULO_L
: return "LV_MODULO_L";
1062 case FT_LV_CHAR_LEFT
: return "LV_CHAR_LEFT";
1063 case FT_LS_MONTH
: return "LS_MONTH";
1064 case FT_LS_LMONTH
: return "LS_LMONTH";
1065 case FT_LS_ZONE
: return "LS_ZONE";
1066 case FT_LS_DAY
: return "LS_DAY";
1067 case FT_LS_WEEKDAY
: return "LS_WEEKDAY";
1068 case FT_LS_822DATE
: return "LS_822DATE";
1069 case FT_LS_PRETTY
: return "LS_PRETTY";
1070 case FT_LS_KILO
: return "LS_KILO";
1071 case FT_LS_KIBI
: return "LS_KIBI";
1072 case FT_LV_SEC
: return "LV_SEC";
1073 case FT_LV_MIN
: return "LV_MIN";
1074 case FT_LV_HOUR
: return "LV_HOUR";
1075 case FT_LV_MDAY
: return "LV_MDAY";
1076 case FT_LV_MON
: return "LV_MON";
1077 case FT_LV_YEAR
: return "LV_YEAR";
1078 case FT_LV_YDAY
: return "LV_YDAY";
1079 case FT_LV_WDAY
: return "LV_WDAY";
1080 case FT_LV_ZONE
: return "LV_ZONE";
1081 case FT_LV_CLOCK
: return "LV_CLOCK";
1082 case FT_LV_RCLOCK
: return "LV_RCLOCK";
1083 case FT_LV_DAYF
: return "LV_DAYF";
1084 case FT_LV_DST
: return "LV_DST";
1085 case FT_LV_ZONEF
: return "LV_ZONEF";
1086 case FT_LS_PERS
: return "LS_PERS";
1087 case FT_LS_MBOX
: return "LS_MBOX";
1088 case FT_LS_HOST
: return "LS_HOST";
1089 case FT_LS_PATH
: return "LS_PATH";
1090 case FT_LS_GNAME
: return "LS_GNAME";
1091 case FT_LS_NOTE
: return "LS_NOTE";
1092 case FT_LS_ADDR
: return "LS_ADDR";
1093 case FT_LS_822ADDR
: return "LS_822ADDR";
1094 case FT_LS_FRIENDLY
: return "LS_FRIENDLY";
1095 case FT_LV_HOSTTYPE
: return "LV_HOSTTYPE";
1096 case FT_LV_INGRPF
: return "LV_INGRPF";
1097 case FT_LS_UNQUOTE
: return "LS_UNQUOTE";
1098 case FT_LV_NOHOSTF
: return "LV_NOHOSTF";
1099 case FT_LOCALDATE
: return "LOCALDATE";
1100 case FT_GMTDATE
: return "GMTDATE";
1101 case FT_PARSEDATE
: return "PARSEDATE";
1102 case FT_PARSEADDR
: return "PARSEADDR";
1103 case FT_FORMATADDR
: return "FORMATADDR";
1104 case FT_CONCATADDR
: return "CONCATADDR";
1105 case FT_MYMBOX
: return "MYMBOX";
1106 case FT_GETMYMBOX
: return "GETMYMBOX";
1107 case FT_GETMYADDR
: return "GETMYADDR";
1108 case FT_SAVESTR
: return "SAVESTR";
1109 case FT_DONE
: return "DONE";
1110 case FT_PAUSE
: return "PAUSE";
1111 case FT_NOP
: return "NOP";
1112 case FT_GOTO
: return "GOTO";
1113 case FT_IF_S_NULL
: return "IF_S_NULL";
1114 case FT_IF_S
: return "IF_S";
1115 case FT_IF_V_EQ
: return "IF_V_EQ";
1116 case FT_IF_V_NE
: return "IF_V_NE";
1117 case FT_IF_V_GT
: return "IF_V_GT";
1118 case FT_IF_MATCH
: return "IF_MATCH";
1119 case FT_IF_AMATCH
: return "IF_AMATCH";
1120 case FT_S_NULL
: return "S_NULL";
1121 case FT_S_NONNULL
: return "S_NONNULL";
1122 case FT_V_EQ
: return "V_EQ";
1123 case FT_V_NE
: return "V_NE";
1124 case FT_V_GT
: return "V_GT";
1125 case FT_V_MATCH
: return "V_MATCH";
1126 case FT_V_AMATCH
: return "V_AMATCH";
1128 snprintf(buf
, sizeof(buf
), "/* ??? #%d */", t
);
1136 static char buf
[64];
1138 snprintb(buf
, sizeof(buf
), t
, CT_BITS
);
1145 static char buf
[64];
1147 snprintb(buf
, sizeof(buf
), t
, CF_BITS
);
1152 litputs(const char *s
)
1160 fputs("<nil>", stdout
);
1167 printf("\\x%02x", (unsigned char) c
);
1168 } else if (c
< 0x20 || c
== 0177) {
1172 } else if (c
== '\f') {
1175 } else if (c
== '\n') {
1178 } else if (c
== '\r') {
1181 } else if (c
== '\t') {
1186 putchar(c
^ 0x40); /* DEL to ?, others to alpha */
1193 * Routines/code to support the duplicate address suppression code, adapted
1197 static char *buf
; /* our current working buffer */
1198 static char *bufend
; /* end of working buffer */
1199 static char *last_dst
; /* buf ptr at end of last call */
1200 static unsigned int bufsiz
=0; /* current size of buf */
1202 #define BUFINCR 512 /* how much to expand buf when if fills */
1204 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
1207 * check if there's enough room in buf for str.
1208 * add more mem if needed
1210 #define CHECKMEM(str) \
1211 if ((len = strlen (str)) >= bufend - dst) {\
1213 int n = last_dst - buf;\
1214 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
1215 buf = mh_xrealloc (buf, bufsiz);\
1217 last_dst = buf + n;\
1218 bufend = buf + bufsiz;\
1223 * These are versions of similar routines from replsbr.c; the purpose is
1224 * to suppress duplicate addresses from being added to a list when building
1225 * up addresses for the %(formataddr) format function. This is used by
1226 * repl to prevent duplicate addresses from being added to the "to" line.
1227 * See replsbr.c for more information.
1229 * We can't use the functions in replsbr.c directly because they are slightly
1230 * different and depend on the rest of replsbr.c
1233 test_formataddr (char *orig
, char *str
)
1241 struct mailname
*mp
= NULL
;
1243 /* if we don't have a buffer yet, get one */
1245 buf
= mh_xmalloc (BUFINCR
);
1246 last_dst
= buf
; /* XXX */
1247 bufsiz
= BUFINCR
- 6; /* leave some slop */
1248 bufend
= buf
+ bufsiz
;
1251 * If "orig" points to our buffer we can just pick up where we
1252 * left off. Otherwise we have to copy orig into our buffer.
1256 else if (!orig
|| !*orig
) {
1260 dst
= last_dst
; /* XXX */
1265 /* concatenate all the new addresses onto 'buf' */
1266 for (isgroup
= false; (cp
= getname (str
)); ) {
1267 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1268 fprintf(stderr
, "bad address \"%s\" -- %s\n", cp
, error
);
1271 if (isgroup
&& (mp
->m_gname
|| !mp
->m_ingrp
)) {
1276 /* if we get here we're going to add an address */
1282 CHECKMEM (mp
->m_gname
);
1286 sp
= adrformat (mp
);
1302 * The companion to test_formataddr(); it behaves the same way, except doesn't
1303 * do duplicate address detection.
1306 test_concataddr(char *orig
, char *str
)
1311 cp
= test_formataddr(orig
, str
);
1317 insert (struct mailname
*np
)
1319 struct mailname
*mp
;
1324 if (np
->m_mbox
== NULL
)
1327 for (mp
= &mq
; mp
->m_next
; mp
= mp
->m_next
) {
1328 if (!strcasecmp (FENDNULL(np
->m_host
),
1329 FENDNULL(mp
->m_next
->m_host
)) &&
1330 !strcasecmp (FENDNULL(np
->m_mbox
),
1331 FENDNULL(mp
->m_next
->m_mbox
)))
1334 if (!ccme
&& ismymbox (np
))
1343 * Reset our duplicate address list
1349 struct mailname
*mp
, *mp2
;
1351 for (mp
= mq
.m_next
; mp
; mp
= mp2
) {