]>
diplodocus.org Git - nmh/blob - uip/fmttest.c
ecbc4481b60c611f8436d0c73d80b57a15a5ec00
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>
16 #define FMTTEST_SWITCHES \
17 X("form formatfile", 0, FORMSW) \
18 X("format string", 5, FMTSW) \
19 X("dump", 0, DUMPSW) \
20 X("address", 0, ADDRSW) \
21 X("date", 0, DATESW) \
22 X("width", 0, WIDTHSW) \
23 X("bufsize size-in-bytes", 0, BUFSZSW) \
24 X("version", 0, VERSIONSW) \
25 X("-component-name component-text", 0, OTHERSW) \
26 X("help", 0, HELPSW) \
28 #define X(sw, minchars, id) id,
29 DEFINE_SWITCH_ENUM(FMTTEST
);
32 #define X(sw, minchars, id) { sw, minchars, id },
33 DEFINE_SWITCH_ARRAY(FMTTEST
, switches
);
37 * An array containing labels used for branch instructions
40 static struct format
**lvec
= NULL
;
42 static int lallocated
= 0;
47 static void fmt_dump (char *, struct format
*);
48 static void dumpone(struct format
*);
49 static int findlabel(struct format
*);
50 static void assignlabel(struct format
*);
51 static char *f_typestr(int);
52 static char *c_typestr(int);
53 static char *c_flagsstr(int);
54 static void litputs(char *);
55 static void litputc(char);
59 main (int argc
, char **argv
)
61 char *cp
, *form
= NULL
, *format
= NULL
, *folder
= NULL
;
62 char buf
[BUFSIZ
], *nfs
, **argp
, **arguments
;
63 char **compargs
= NULL
;
66 struct msgs_array msgs
= { 0, 0, NULL
};
67 int dump
= 0, compp
= 0, comppalloc
= 0, i
;
68 int width
= 0, bufsize
= 0;
72 setlocale(LC_ALL
, "");
74 invo_name
= r1bindex (argv
[0], '/');
76 /* read user profile/context */
79 arguments
= getarguments (invo_name
, argc
, argv
, 1);
82 while ((cp
= *argp
++)) {
85 * A -- means that we have a component name (like pick);
86 * save the component name and the next argument for the text.
89 /* it's -3 because of this ptr, next ptr, and trailing NULL */
90 if (compp
> comppalloc
- 3) {
92 compargs
= (char **) mh_xrealloc(compargs
, sizeof(char *) *
95 compargs
[compp
++] = --cp
;
96 /* Grab next argument for component text */
98 adios(NULL
, "missing argument to %s", argp
[-2]);
99 compargs
[compp
++] = cp
;
102 switch (smatch (cp
, switches
)) {
104 ambigsw (cp
, switches
);
107 adios (NULL
, "-%s unknown", cp
);
110 snprintf (buf
, sizeof(buf
), "%s [switches]", invo_name
);
111 print_help (buf
, switches
, 1);
114 print_version(invo_name
);
117 adios(NULL
, "internal argument error!");
121 if (!(cp
= *argp
++) || *cp
== '-')
122 adios(NULL
, "missing argument to %s", argp
[-2]);
126 if (!(cp
= *argp
++) || *cp
== '-')
127 adios(NULL
, "missing argument to %s", argp
[-2]);
132 if (!(form
= *argp
++) || *form
== '-')
133 adios (NULL
, "missing argument to %s", argp
[-2]);
137 if (!(format
= *argp
++) || *format
== '-')
138 adios (NULL
, "missing argument to %s", argp
[-2]);
148 if (*cp
== '+' || *cp
== '@') {
150 adios (NULL
, "only one folder at a time!");
152 folder
= pluspath (cp
);
154 app_msgarg(&msgs
, cp
);
158 compargs
[compp
] = NULL
;
161 * Here's our weird heuristic:
163 * - We allow -dump without any other arguments.
164 * - If you've given any component arguments, we don't require any
166 * - The arguments are interpreted as folders/messages.
169 if (!dump
&& !compargs
&& msgs
.size
== 0) {
170 adios (NULL
, "usage: [switches] [+folder] msgs...]", invo_name
);
174 * Get new format string. Must be before chdir().
176 nfs
= new_fs (form
, format
, FORMAT
);
177 (void) fmt_compile(nfs
, &fmt
, 1);
183 for (i
= 0; compargs
[i
] != NULL
; i
+= 2) {
184 cptr
= fmt_findcomp(compargs
[i
]);
186 cptr
->c_text
= getcpy(compargs
[i
+ 1]);
194 * If we don't specify a buffer size, allocate a default one.
207 fmt_dump (char *nfs
, struct format
*fmth
)
210 register struct format
*fmt
, *addr
;
212 printf("Instruction dump of format string: \n%s\n", nfs
);
215 for (fmt
= fmth
; fmt
; ++fmt
) {
225 addr
= fmt
+ fmt
->f_skip
;
226 if (findlabel(addr
) < 0)
229 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
234 for (fmt
= fmth
; fmt
; ++fmt
) {
236 if (fmt
->f_type
== FT_DONE
&& fmt
->f_value
== 0)
242 dumpone(struct format
*fmt
)
246 if ((i
= findlabel(fmt
)) >= 0)
250 fputs(f_typestr((int)fmt
->f_type
), stdout
);
252 switch (fmt
->f_type
) {
259 litputs(fmt
->f_comp
->c_name
);
260 if (fmt
->f_comp
->c_type
)
261 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
262 if (fmt
->f_comp
->c_flags
)
263 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
291 litputs(fmt
->f_comp
->c_name
);
292 if (fmt
->f_comp
->c_type
)
293 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
294 if (fmt
->f_comp
->c_flags
)
295 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
313 litputs(fmt
->f_comp
->c_name
);
314 if (fmt
->f_comp
->c_type
)
315 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
316 if (fmt
->f_comp
->c_flags
)
317 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
321 printf(", width %d, fill '", fmt
->f_width
);
322 litputc(fmt
->f_fill
);
324 litputs(fmt
->f_comp
->c_name
);
325 if (fmt
->f_comp
->c_type
)
326 printf(", c_type %s", c_typestr(fmt
->f_comp
->c_type
));
327 if (fmt
->f_comp
->c_flags
)
328 printf(", c_flags %s", c_flagsstr(fmt
->f_comp
->c_flags
));
333 printf(", width %d, fill '", fmt
->f_width
);
334 litputc(fmt
->f_fill
);
343 litputs(fmt
->f_text
);
347 printf(", width %d, fill '", fmt
->f_width
);
348 litputc(fmt
->f_fill
);
350 litputs(fmt
->f_text
);
356 litputc(fmt
->f_char
);
365 printf(" continue else goto");
367 i
= findlabel(fmt
+ fmt
->f_skip
);
374 i
= findlabel(fmt
+ fmt
->f_skip
);
375 printf(" %d continue else goto L%d", fmt
->f_value
, i
);
386 printf(" value %d", fmt
->f_value
);
391 litputs(fmt
->f_text
);
396 litputs(fmt
->f_text
);
399 case FT_LS_DECODECOMP
:
401 litputs(fmt
->f_comp
->c_name
);
408 printf(", width %d", fmt
->f_width
);
412 printf(", value dat[%d]", fmt
->f_value
);
419 findlabel(struct format
*addr
)
423 for (i
= 0; i
< lused
; ++i
)
430 assignlabel(struct format
*addr
)
432 if (lused
>= lallocated
) {
434 lvec
= (struct format
**)
435 mh_xrealloc(lvec
, sizeof(struct format
*) * lallocated
);
438 lvec
[lused
++] = addr
;
447 case FT_COMP
: return("COMP");
448 case FT_COMPF
: return("COMPF");
449 case FT_LIT
: return("LIT");
450 case FT_LITF
: return("LITF");
452 case FT_LIT_FORCE
: return("LIT_FORCE");
454 case FT_CHAR
: return("CHAR");
455 case FT_NUM
: return("NUM");
456 case FT_NUMF
: return("NUMF");
457 case FT_STR
: return("STR");
458 case FT_STRF
: return("STRF");
459 case FT_STRFW
: return("STRFW");
460 case FT_PUTADDR
: return("PUTADDR");
461 case FT_STRLIT
: return("STRLIT");
462 case FT_STRLITZ
: return("STRLITZ");
463 case FT_LS_COMP
: return("LS_COMP");
464 case FT_LS_LIT
: return("LS_LIT");
465 case FT_LS_GETENV
: return("LS_GETENV");
466 case FT_LS_DECODECOMP
: return("FT_LS_DECODECOMP");
467 case FT_LS_DECODE
: return("FT_LS_DECODE");
468 case FT_LS_TRIM
: return("LS_TRIM");
469 case FT_LV_COMP
: return("LV_COMP");
470 case FT_LV_COMPFLAG
: return("LV_COMPFLAG");
471 case FT_LV_LIT
: return("LV_LIT");
472 case FT_LV_DAT
: return("LV_DAT");
473 case FT_LV_STRLEN
: return("LV_STRLEN");
474 case FT_LV_PLUS_L
: return("LV_PLUS_L");
475 case FT_LV_MINUS_L
: return("LV_MINUS_L");
476 case FT_LV_DIVIDE_L
: return("LV_DIVIDE_L");
477 case FT_LV_MODULO_L
: return("LV_MODULO_L");
478 case FT_LV_CHAR_LEFT
: return("LV_CHAR_LEFT");
479 case FT_LS_MONTH
: return("LS_MONTH");
480 case FT_LS_LMONTH
: return("LS_LMONTH");
481 case FT_LS_ZONE
: return("LS_ZONE");
482 case FT_LS_DAY
: return("LS_DAY");
483 case FT_LS_WEEKDAY
: return("LS_WEEKDAY");
484 case FT_LS_822DATE
: return("LS_822DATE");
485 case FT_LS_PRETTY
: return("LS_PRETTY");
486 case FT_LV_SEC
: return("LV_SEC");
487 case FT_LV_MIN
: return("LV_MIN");
488 case FT_LV_HOUR
: return("LV_HOUR");
489 case FT_LV_MDAY
: return("LV_MDAY");
490 case FT_LV_MON
: return("LV_MON");
491 case FT_LV_YEAR
: return("LV_YEAR");
492 case FT_LV_YDAY
: return("LV_YDAY");
493 case FT_LV_WDAY
: return("LV_WDAY");
494 case FT_LV_ZONE
: return("LV_ZONE");
495 case FT_LV_CLOCK
: return("LV_CLOCK");
496 case FT_LV_RCLOCK
: return("LV_RCLOCK");
497 case FT_LV_DAYF
: return("LV_DAYF");
498 case FT_LV_DST
: return("LV_DST");
499 case FT_LV_ZONEF
: return("LV_ZONEF");
500 case FT_LS_ADDR
: return("LS_ADDR");
501 case FT_LS_PERS
: return("LS_PERS");
502 case FT_LS_MBOX
: return("LS_MBOX");
503 case FT_LS_HOST
: return("LS_HOST");
504 case FT_LS_PATH
: return("LS_PATH");
505 case FT_LS_GNAME
: return("LS_GNAME");
506 case FT_LS_NOTE
: return("LS_NOTE");
507 case FT_LS_822ADDR
: return("LS_822ADDR");
508 case FT_LS_FRIENDLY
: return("LS_FRIENDLY");
509 case FT_LV_HOSTTYPE
: return("LV_HOSTTYPE");
510 case FT_LV_INGRPF
: return("LV_INGRPF");
511 case FT_LV_NOHOSTF
: return("LV_NOHOSTF");
512 case FT_LOCALDATE
: return("LOCALDATE");
513 case FT_GMTDATE
: return("GMTDATE");
514 case FT_PARSEDATE
: return("PARSEDATE");
515 case FT_PARSEADDR
: return("PARSEADDR");
516 case FT_FORMATADDR
: return("FORMATADDR");
517 case FT_CONCATADDR
: return("CONCATADDR");
518 case FT_MYMBOX
: return("MYMBOX");
520 case FT_ADDTOSEQ
: return("ADDTOSEQ");
522 case FT_SAVESTR
: return("SAVESTR");
524 case FT_PAUSE
: return ("PAUSE");
526 case FT_DONE
: return("DONE");
527 case FT_NOP
: return("NOP");
528 case FT_GOTO
: return("GOTO");
529 case FT_IF_S_NULL
: return("IF_S_NULL");
530 case FT_IF_S
: return("IF_S");
531 case FT_IF_V_EQ
: return("IF_V_EQ");
532 case FT_IF_V_NE
: return("IF_V_NE");
533 case FT_IF_V_GT
: return("IF_V_GT");
534 case FT_IF_MATCH
: return("IF_MATCH");
535 case FT_IF_AMATCH
: return("IF_AMATCH");
536 case FT_S_NULL
: return("S_NULL");
537 case FT_S_NONNULL
: return("S_NONNULL");
538 case FT_V_EQ
: return("V_EQ");
539 case FT_V_NE
: return("V_NE");
540 case FT_V_GT
: return("V_GT");
541 case FT_V_MATCH
: return("V_MATCH");
542 case FT_V_AMATCH
: return("V_AMATCH");
544 printf(buf
, "/* ??? #%d */", t
);
549 #define FNORD(v, s) if (t & (v)) { \
561 if (t
& ~(CT_ADDR
|CT_DATE
))
562 printf(buf
, "0x%x ", t
);
565 FNORD(CT_ADDR
, "ADDR");
566 FNORD(CT_DATE
, "DATE");
578 if (t
& ~(CF_TRUE
|CF_PARSED
|CF_DATEFAB
|CF_TRIMMED
))
579 printf(buf
, "0x%x ", t
);
582 FNORD(CF_TRUE
, "TRUE");
583 FNORD(CF_PARSED
, "PARSED");
584 FNORD(CF_DATEFAB
, "DATEFAB");
585 FNORD(CF_TRIMMED
, "TRIMMED");
600 fputs("<nil>", stdout
);
611 if (c
< 0x20 || c
== 0177) {
615 } else if (c
== '\f') {
618 } else if (c
== '\n') {
621 } else if (c
== '\r') {
624 } else if (c
== '\t') {
629 putc(c
^ 0x40, stdout
); /* DEL to ?, others to alpha */