]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
7e71572196178fc2d448e5c3b4825c80c19cdd67
3 * fmttest.c -- A program to help test and debug format instructions
5 * This code is Copyright (c) 2012, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/fmt_scan.h>
12 #include <h/fmt_compile.h>
14 #include <h/scansbr.h>
15 #include <h/addrsbr.h>
17 #define FMTTEST_SWITCHES \
18 X("form formatfile", 0, FORMSW) \
19 X("format string", 5, FMTSW) \
20 X("dump", 0, DUMPSW) \
21 X("address", 0, ADDRSW) \
23 X("date", 0, DATESW) \
24 X("message", 0, MESSAGESW) \
25 X("normalize", 0, NORMSW) \
26 X("nonormalize", 0, NNORMSW) \
27 X("outsize size-in-characters", 0, OUTSIZESW) \
28 X("bufsize size-in-bytes", 0, BUFSZSW) \
29 X("width column-width", 0, WIDTHSW) \
30 X("msgnum number", 0, MSGNUMSW) \
31 X("msgcur flag", 0, MSGCURSW) \
32 X("msgsize size", 0, MSGSIZESW) \
33 X("unseen flag", 0, UNSEENSW) \
34 X("version", 0, VERSIONSW) \
35 X("-component-name component-text", 0, OTHERSW) \
36 X("help", 0, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(FMTTEST
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
47 * An array containing labels used for branch instructions
50 static struct format
**lvec
= NULL
;
52 static int lallocated
= 0;
54 enum mode_t
{ MESSAGE
, ADDRESS
, RAW
};
55 #define DEFADDRFORMAT "%<{error}%{error}: %{text}%|%(putstr(proper{text}))%>"
56 #define DEFDATEFORMAT "%<(nodate{text})error: %{text}%|%(putstr(pretty{text}))%>"
61 static void fmt_dump (char *, struct format
*);
62 static void dumpone(struct format
*);
63 static void initlabels(struct format
*);
64 static int findlabel(struct format
*);
65 static void assignlabel(struct format
*);
66 static char *f_typestr(int);
67 static char *c_typestr(int);
68 static char *c_flagsstr(int);
69 static void litputs(char *);
70 static void litputc(char);
71 static void process_addresses(struct format
*, struct msgs_array
*, char *,
72 int, int, int *, int);
73 static void process_raw(struct format
*, struct msgs_array
*, char *,
75 static void process_messages(struct format
*, struct msgs_array
*,
76 struct msgs_array
*, char *, char *, int,
81 main (int argc
, char **argv
)
83 char *cp
, *form
= NULL
, *format
= NULL
, *defformat
= FORMAT
, *folder
= NULL
;
84 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
, *buffer
;
87 struct msgs_array msgs
= { 0, 0, NULL
}, compargs
= { 0, 0, NULL
};
89 int outputsize
= 0, bufsize
= 0;
90 int colwidth
= -1, msgnum
= -1, msgcur
= -1, msgsize
= -1, msgunseen
= -1;
91 int normalize
= AD_HOST
;
92 enum mode_t mode
= MESSAGE
;
96 setlocale(LC_ALL
, "");
98 invo_name
= r1bindex (argv
[0], '/');
100 /* read user profile/context */
103 arguments
= getarguments (invo_name
, argc
, argv
, 1);
106 while ((cp
= *argp
++)) {
109 * A -- means that we have a component name (like pick);
110 * save the component name and the next argument for the text.
113 app_msgarg(&compargs
, --cp
);
114 /* Grab next argument for component text */
116 adios(NULL
, "missing argument to %s", argp
[-2]);
117 app_msgarg(&compargs
, cp
);
120 switch (smatch (cp
, switches
)) {
122 ambigsw (cp
, switches
);
125 adios (NULL
, "-%s unknown", cp
);
128 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
129 print_help (buf
, switches
, 1);
132 print_version(invo_name
);
135 adios(NULL
, "internal argument error!");
139 if (!(cp
= *argp
++) || *cp
== '-')
140 adios(NULL
, "missing argument to %s", argp
[-2]);
141 if (strcmp(cp
, "max") == 0)
143 else if (strcmp(cp
, "width") == 0)
144 outputsize
= sc_width();
146 outputsize
= atoi(cp
);
149 if (!(cp
= *argp
++) || *cp
== '-')
150 adios(NULL
, "missing argument to %s", argp
[-2]);
155 if (!(form
= *argp
++) || *form
== '-')
156 adios (NULL
, "missing argument to %s", argp
[-2]);
160 if (!(format
= *argp
++) || *format
== '-')
161 adios (NULL
, "missing argument to %s", argp
[-2]);
174 defformat
= DEFADDRFORMAT
;
185 defformat
= DEFDATEFORMAT
;
189 if (!(cp
= *argp
++) || *cp
== '-')
190 adios(NULL
, "missing argument to %s", argp
[-2]);
194 if (!(cp
= *argp
++) || *cp
== '-')
195 adios(NULL
, "missing argument to %s", argp
[-2]);
199 if (!(cp
= *argp
++) || *cp
== '-')
200 adios(NULL
, "missing argument to %s", argp
[-2]);
204 if (!(cp
= *argp
++) || *cp
== '-')
205 adios(NULL
, "missing argument to %s", argp
[-2]);
209 if (!(cp
= *argp
++) || *cp
== '-')
210 adios(NULL
, "missing argument to %s", argp
[-2]);
211 msgunseen
= atoi(cp
);
222 * Only interpret as a folder if we're in message mode
225 if (mode
== MESSAGE
&& (*cp
== '+' || *cp
== '@')) {
227 adios (NULL
, "only one folder at a time!");
229 folder
= pluspath (cp
);
231 app_msgarg(&msgs
, cp
);
235 * Here's our weird heuristic:
237 * - We allow -dump without any other arguments.
238 * - If you've given any component arguments, we don't require any
240 * - The arguments are interpreted as folders/messages _if_ we're in
241 * message mode, otherwise pass as strings in the text component.
244 if (!dump
&& compargs
.size
== 0 && msgs
.size
== 0) {
245 adios (NULL
, "usage: [switches] [+folder] msgs | strings...",
250 * Get new format string. Must be before chdir().
252 nfs
= new_fs (form
, format
, defformat
);
253 (void) fmt_compile(nfs
, &fmt
, 1);
257 if (compargs
.size
== 0 && msgs
.size
== 0)
262 * If we don't specify a buffer size, allocate a default one.
268 buffer
= mh_xmalloc(bufsize
);
271 outputsize
= bufsize
- 1; /* For the trailing NUL */
272 else if (outputsize
== 0) {
274 outputsize
= sc_width();
276 outputsize
= bufsize
- 1;
282 dat
[3] = colwidth
== -1 ? outputsize
: colwidth
;
285 if (mode
== MESSAGE
) {
286 process_messages(fmt
, &compargs
, &msgs
, buffer
, folder
, bufsize
,
290 for (i
= 0; i
< compargs
.size
; i
+= 2) {
291 cptr
= fmt_findcomp(compargs
.msgs
[i
]);
293 cptr
->c_text
= getcpy(compargs
.msgs
[i
+ 1]);
297 if (mode
== ADDRESS
) {
298 fmt_norm
= normalize
;
299 process_addresses(fmt
, &msgs
, buffer
, bufsize
, outputsize
,
302 process_raw(fmt
, &msgs
, buffer
, bufsize
, outputsize
, dat
);
312 * Process each address with fmt_scan().
318 struct pqpair
*pq_next
;
322 process_addresses(struct format
*fmt
, struct msgs_array
*addrs
, char *buffer
,
323 int bufsize
, int outwidth
, int *dat
, int norm
)
326 char *cp
, error
[BUFSIZ
];
328 struct pqpair
*p
, *q
;
341 for (i
= 0; i
< addrs
->size
; i
++) {
342 (q
= &pq
)->pq_next
= NULL
;
343 while ((cp
= getname(addrs
->msgs
[i
]))) {
344 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
345 adios (NULL
, "unable to allocate pqpair memory");
346 if ((mp
= getm(cp
, NULL
, 0, norm
, error
)) == NULL
) {
347 p
->pq_text
= getcpy(cp
);
348 p
->pq_error
= getcpy(error
);
350 p
->pq_text
= getcpy(mp
->m_text
);
353 q
= (q
->pq_next
= p
);
356 for (p
= pq
.pq_next
; p
; p
= q
) {
357 c
= fmt_findcomp("text");
361 c
->c_text
= p
->pq_text
;
364 c
= fmt_findcomp("error");
368 c
->c_text
= p
->pq_error
;
372 fmt_scan(fmt
, buffer
, bufsize
, outwidth
, dat
);
373 fputs(buffer
, stdout
);
386 * Process messages and run them through the format engine. A lot taken
391 process_messages(struct format
*fmt
, struct msgs_array
*comps
,
392 struct msgs_array
*msgs
, char *buffer
, char *folder
,
393 int bufsize
, int outwidth
, int *dat
)
395 int i
, state
, msgnum
, msgsize
= dat
[2], num
= dat
[0], cur
= dat
[1];
396 int num_unseen_seq
= 0, seqnum
[NUMATTRS
];
397 char *maildir
, *cp
, name
[NAMESZ
], rbuf
[BUFSIZ
];
402 folder
= getfolder(1);
404 maildir
= m_maildir(folder
);
406 if (chdir(maildir
) < 0)
407 adios(maildir
, "unable to change directory to");
409 if (!(mp
= folder_read(folder
)))
410 adios(NULL
, "unable to read folder %s", folder
);
413 adios(NULL
, "no messages in %s", folder
);
415 for (i
= 0; i
< msgs
->size
; i
++)
416 if (!m_convert(mp
, msgs
->msgs
[i
]))
418 seq_setprev(mp
); /* set the Previous-Sequence */
420 context_replace(pfolder
, folder
); /* update curren folder */
421 seq_save(mp
); /* synchronize message sequences */
422 context_save(); /* save the context file */
425 * We want to set the unseen flag if requested, so we have to check
426 * the unseen sequence as well.
430 if ((cp
= context_find(usequence
)) && *cp
) {
434 ap
= brkstring(dp
, " ", "\n");
435 for (i
= 0; ap
&& *ap
; i
++, ap
++)
436 seqnum
[i
] = seq_getnum(mp
, *ap
);
444 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
445 if (is_selected(mp
, msgnum
)) {
446 if ((in
= fopen(cp
= m_name(msgnum
), "r")) == NULL
) {
447 admonish(cp
, "unable to open message");
457 dat
[1] = msgnum
== mp
->curmsg
;
460 * Get our size if we didn't include one
466 if (fstat(fileno(in
), &st
) < 0)
473 * Check to see if this is in the unseen sequence
477 for (i
= 0; i
< num_unseen_seq
; i
++) {
478 if (in_sequence(mp
, seqnum
[i
], msgnum
)) {
485 * Read in the message and process the components
488 for (state
= FLD
;;) {
489 state
= m_getfld(state
, name
, rbuf
, sizeof(rbuf
), in
);
493 i
= fmt_addcomptext(name
, rbuf
);
495 while (state
== FLDPLUS
) {
496 state
= m_getfld(state
, name
, rbuf
,
498 fmt_appendcomp(i
, name
, rbuf
);
502 while (state
== FLDPLUS
)
503 state
= m_getfld(state
, name
, rbuf
,
508 if (fmt_findcomp("body")) {
509 if ((i
= strlen(rbuf
)) < outwidth
)
510 state
= m_getfld(state
, name
, rbuf
+ i
,
513 fmt_addcomptext("body", rbuf
);
523 fmt_scan(fmt
, buffer
, bufsize
, outwidth
, dat
);
524 fputs(buffer
, stdout
);
533 * Run text through the format engine with no special processing
537 process_raw(struct format
*fmt
, struct msgs_array
*text
, char *buffer
,
538 int bufsize
, int outwidth
, int *dat
)
552 c
= fmt_findcomp("text");
554 for (i
= 0; i
< text
->size
; i
++) {
556 if (c
->c_text
!= NULL
)
558 c
->c_text
= getcpy(text
->msgs
[i
]);
561 fmt_scan(fmt
, buffer
, bufsize
, outwidth
, dat
);
562 fputs(buffer
, stdout
);
567 fmt_dump (char *nfs
, struct format
*fmth
)
571 printf("Instruction dump of format string: \n%s\n", nfs
);
576 for (fmt
= fmth
; fmt
; ++fmt
) {
578 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
584 dumpone(struct format
*fmt
)
588 if ((i
= findlabel(fmt
)) >= 0)
592 fputs(f_typestr((int)fmt
->f_type
), stdout
);
594 switch (fmt
->f_type
) {
601 litputs(fmt
->f_comp
->c_name
);
602 if (fmt
->f_comp
->c_type
)
603 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
604 if (fmt
->f_comp
->c_flags
)
605 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
633 litputs(fmt
->f_comp
->c_name
);
634 if (fmt
->f_comp
->c_type
)
635 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
636 if (fmt
->f_comp
->c_flags
)
637 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
655 litputs(fmt
->f_comp
->c_name
);
656 if (fmt
->f_comp
->c_type
)
657 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
658 if (fmt
->f_comp
->c_flags
)
659 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
663 printf(", width %d, fill '", fmt
->f_width
);
664 litputc(fmt
->f_fill
);
666 litputs(fmt
->f_comp
->c_name
);
667 if (fmt
->f_comp
->c_type
)
668 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
669 if (fmt
->f_comp
->c_flags
)
670 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
675 printf(", width %d, fill '", fmt
->f_width
);
676 litputc(fmt
->f_fill
);
685 litputs(fmt
->f_text
);
689 printf(", width %d, fill '", fmt
->f_width
);
690 litputc(fmt
->f_fill
);
692 litputs(fmt
->f_text
);
698 litputc(fmt
->f_char
);
707 printf(" continue else goto");
709 i
= findlabel(fmt
+ fmt
->f_skip
);
716 i
= findlabel(fmt
+ fmt
->f_skip
);
717 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
728 printf(" value %d", fmt
->f_value
);
733 litputs(fmt
->f_text
);
738 litputs(fmt
->f_text
);
741 case FT_LS_DECODECOMP
:
743 litputs(fmt
->f_comp
->c_name
);
750 printf(", width %d", fmt
->f_width
);
754 printf(", value dat[%d]", fmt
->f_value
);
761 * Iterate over all instructions and assign labels to the targets of
766 initlabels(struct format
*fmth
)
768 struct format
*fmt
, *addr
;
772 for (fmt
= fmth
; fmt
; ++fmt
) {
782 addr
= fmt
+ fmt
->f_skip
;
783 if (findlabel(addr
) < 0)
786 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
793 findlabel(struct format
*addr
)
797 for (i
= 0; i
< lused
; ++i
)
804 assignlabel(struct format
*addr
)
806 if (lused
>= lallocated
) {
808 lvec
= (struct format
**)
809 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
812 lvec
[lused
++] = addr
;
821 case FT_COMP
: return("COMP");
822 case FT_COMPF
: return("COMPF");
823 case FT_LIT
: return("LIT");
824 case FT_LITF
: return("LITF");
826 case FT_LIT_FORCE
: return("LIT_FORCE");
828 case FT_CHAR
: return("CHAR");
829 case FT_NUM
: return("NUM");
830 case FT_NUMF
: return("NUMF");
831 case FT_STR
: return("STR");
832 case FT_STRF
: return("STRF");
833 case FT_STRFW
: return("STRFW");
834 case FT_PUTADDR
: return("PUTADDR");
835 case FT_STRLIT
: return("STRLIT");
836 case FT_STRLITZ
: return("STRLITZ");
837 case FT_LS_COMP
: return("LS_COMP");
838 case FT_LS_LIT
: return("LS_LIT");
839 case FT_LS_GETENV
: return("LS_GETENV");
840 case FT_LS_DECODECOMP
: return("FT_LS_DECODECOMP");
841 case FT_LS_DECODE
: return("FT_LS_DECODE");
842 case FT_LS_TRIM
: return("LS_TRIM");
843 case FT_LV_COMP
: return("LV_COMP");
844 case FT_LV_COMPFLAG
: return("LV_COMPFLAG");
845 case FT_LV_LIT
: return("LV_LIT");
846 case FT_LV_DAT
: return("LV_DAT");
847 case FT_LV_STRLEN
: return("LV_STRLEN");
848 case FT_LV_PLUS_L
: return("LV_PLUS_L");
849 case FT_LV_MINUS_L
: return("LV_MINUS_L");
850 case FT_LV_DIVIDE_L
: return("LV_DIVIDE_L");
851 case FT_LV_MODULO_L
: return("LV_MODULO_L");
852 case FT_LV_CHAR_LEFT
: return("LV_CHAR_LEFT");
853 case FT_LS_MONTH
: return("LS_MONTH");
854 case FT_LS_LMONTH
: return("LS_LMONTH");
855 case FT_LS_ZONE
: return("LS_ZONE");
856 case FT_LS_DAY
: return("LS_DAY");
857 case FT_LS_WEEKDAY
: return("LS_WEEKDAY");
858 case FT_LS_822DATE
: return("LS_822DATE");
859 case FT_LS_PRETTY
: return("LS_PRETTY");
860 case FT_LV_SEC
: return("LV_SEC");
861 case FT_LV_MIN
: return("LV_MIN");
862 case FT_LV_HOUR
: return("LV_HOUR");
863 case FT_LV_MDAY
: return("LV_MDAY");
864 case FT_LV_MON
: return("LV_MON");
865 case FT_LV_YEAR
: return("LV_YEAR");
866 case FT_LV_YDAY
: return("LV_YDAY");
867 case FT_LV_WDAY
: return("LV_WDAY");
868 case FT_LV_ZONE
: return("LV_ZONE");
869 case FT_LV_CLOCK
: return("LV_CLOCK");
870 case FT_LV_RCLOCK
: return("LV_RCLOCK");
871 case FT_LV_DAYF
: return("LV_DAYF");
872 case FT_LV_DST
: return("LV_DST");
873 case FT_LV_ZONEF
: return("LV_ZONEF");
874 case FT_LS_ADDR
: return("LS_ADDR");
875 case FT_LS_PERS
: return("LS_PERS");
876 case FT_LS_MBOX
: return("LS_MBOX");
877 case FT_LS_HOST
: return("LS_HOST");
878 case FT_LS_PATH
: return("LS_PATH");
879 case FT_LS_GNAME
: return("LS_GNAME");
880 case FT_LS_NOTE
: return("LS_NOTE");
881 case FT_LS_822ADDR
: return("LS_822ADDR");
882 case FT_LS_FRIENDLY
: return("LS_FRIENDLY");
883 case FT_LV_HOSTTYPE
: return("LV_HOSTTYPE");
884 case FT_LV_INGRPF
: return("LV_INGRPF");
885 case FT_LV_NOHOSTF
: return("LV_NOHOSTF");
886 case FT_LOCALDATE
: return("LOCALDATE");
887 case FT_GMTDATE
: return("GMTDATE");
888 case FT_PARSEDATE
: return("PARSEDATE");
889 case FT_PARSEADDR
: return("PARSEADDR");
890 case FT_FORMATADDR
: return("FORMATADDR");
891 case FT_CONCATADDR
: return("CONCATADDR");
892 case FT_MYMBOX
: return("MYMBOX");
894 case FT_ADDTOSEQ
: return("ADDTOSEQ");
896 case FT_SAVESTR
: return("SAVESTR");
898 case FT_PAUSE
: return ("PAUSE");
900 case FT_DONE
: return("DONE");
901 case FT_NOP
: return("NOP");
902 case FT_GOTO
: return("GOTO");
903 case FT_IF_S_NULL
: return("IF_S_NULL");
904 case FT_IF_S
: return("IF_S");
905 case FT_IF_V_EQ
: return("IF_V_EQ");
906 case FT_IF_V_NE
: return("IF_V_NE");
907 case FT_IF_V_GT
: return("IF_V_GT");
908 case FT_IF_MATCH
: return("IF_MATCH");
909 case FT_IF_AMATCH
: return("IF_AMATCH");
910 case FT_S_NULL
: return("S_NULL");
911 case FT_S_NONNULL
: return("S_NONNULL");
912 case FT_V_EQ
: return("V_EQ");
913 case FT_V_NE
: return("V_NE");
914 case FT_V_GT
: return("V_GT");
915 case FT_V_MATCH
: return("V_MATCH");
916 case FT_V_AMATCH
: return("V_AMATCH");
918 printf(buf
, "/* ??? #%d */", t
);
923 #define FNORD(v, s) if (t & (v)) { \
935 if (t
& ~(CT_ADDR
|CT_DATE
))
936 printf(buf
, "0x%x ", t
);
939 FNORD(CT_ADDR
, "ADDR");
940 FNORD(CT_DATE
, "DATE");
952 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
953 printf(buf
, "0x%x ", t
);
956 FNORD(CF_TRUE
, "TRUE");
957 FNORD(CF_PARSED
, "PARSED");
958 FNORD(CF_DATEFAB
, "DATEFAB");
959 FNORD(CF_TRIMMED
, "TRIMMED");
974 fputs("<nil>", stdout
);
985 if (c
< 0x20 || c
== 0177) {
989 } else if (c
== '\f') {
992 } else if (c
== '\n') {
995 } else if (c
== '\r') {
998 } else if (c
== '\t') {
1003 putc(c
^ 0x40, stdout
); /* DEL to ?, others to alpha */