Fix uip/whom.c for C89 compatibility
[mmh] / uip / fmtdump.c
index 87e22bf..419d8ee 100644 (file)
-
 /*
- * fmtdump.c -- compile format file and dump out instructions
- *
- * $Id$
- */
+** fmtdump.c -- compile format file and dump out instructions
+**
+** This code is Copyright (c) 2002, by the authors of nmh.  See the
+** COPYRIGHT file in the root directory of the nmh distribution for
+** complete copyright information.
+*/
 
 #include <h/mh.h>
 #include <h/fmt_scan.h>
 #include <h/fmt_compile.h>
 #include <h/scansbr.h>
+#include <locale.h>
+#include <sysexits.h>
 
 static struct swit switches[] = {
-#define        FORMSW              0
-    { "form formatfile", 0 },
-#define        FMTSW               1
-    { "format string", 5 },
-#define VERSIONSW           2
-    { "version", 0 },
-#define        HELPSW              3
-    { "help", 4 },
-    { NULL, 0 }
+#define FORMSW  0
+       { "form formatfile", 0 },
+#define VERSIONSW  1
+       { "Version", 0 },
+#define HELPSW  2
+       { "help", 0 },
+       { NULL, 0 }
 };
 
+char *version=VERSION;
+
 /* for assignlabel */
 static struct format *lvec[128];
 static int lused = 0;
 
 /*
- * static prototypes
- */
-static void fmt_dump (struct format *);
+** static prototypes
+*/
+static void fmt_dump(struct format *);
 static void dumpone(struct format *);
 static int findlabel(struct format *);
 static void assignlabel(struct format *);
 static char *f_typestr(int);
 static char *c_typestr(int);
+static char *c_flagsstr(int);
 static void litputs(char *);
 static void litputc(char);
 
 
 int
-main (int argc, char **argv)
+main(int argc, char **argv)
 {
-    int ncomps;
-    char *cp, *form = NULL, *format = NULL;
-    char buf[BUFSIZ], *nfs, **argp, **arguments;
-    struct format *fmt;
-
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
-
-    /* read user profile/context */
-    context_read();
-
-    arguments = getarguments (invo_name, argc, argv, 1);
-    argp = arguments;
-
-    while ((cp = *argp++)) {
-       if (*cp == '-') {
-           switch (smatch (++cp, switches)) {
-               case AMBIGSW: 
-                   ambigsw (cp, switches);
-                   done (1);
-               case UNKWNSW: 
-                   adios (NULL, "-%s unknown", cp);
-
-               case HELPSW: 
-                   snprintf (buf, sizeof(buf), "%s [switches]", invo_name);
-                   print_help (buf, switches, 1);
-                   done (1);
-               case VERSIONSW:
-                   print_version(invo_name);
-                   done (1);
-
-               case FORMSW: 
-                   if (!(form = *argp++) || *form == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   format = NULL;
-                   continue;
-               case FMTSW: 
-                   if (!(format = *argp++) || *format == '-')
-                       adios (NULL, "missing argument to %s", argp[-2]);
-                   form = NULL;
-                   continue;
-
-           }
+       char *cp, *form = NULL;
+       char buf[BUFSIZ], *fmtstr, **argp, **arguments;
+       struct format *fmt;
+
+       setlocale(LC_ALL, "");
+       invo_name = mhbasename(argv[0]);
+
+       /* read user profile/context */
+       context_read();
+
+       arguments = getarguments(invo_name, argc, argv, 1);
+       argp = arguments;
+
+       while ((cp = *argp++)) {
+               if (*cp == '-') {
+                       switch (smatch(++cp, switches)) {
+                       case AMBIGSW:
+                               ambigsw(cp, switches);
+                               exit(EX_USAGE);
+                       case UNKWNSW:
+                               adios(EX_USAGE, NULL, "-%s unknown", cp);
+
+                       case HELPSW:
+                               snprintf(buf, sizeof(buf), "%s [switches]",
+                                               invo_name);
+                               print_help(buf, switches, 1);
+                               exit(argc == 2 ? EX_OK : EX_USAGE);
+                       case VERSIONSW:
+                               print_version(invo_name);
+                               exit(argc == 2 ? EX_OK : EX_USAGE);
+
+                       case FORMSW:
+                               if (!(form = *argp++) || *form == '-')
+                                       adios(EX_USAGE, NULL, "missing argument to %s",
+                                                       argp[-2]);
+                               continue;
+
+                       }
+               }
+               if (form)
+                       adios(EX_USAGE, NULL, "only one form at a time!");
+               else
+                       form = cp;
        }
-       if (form)
-           adios (NULL, "only one form at a time!");
-       else
-           form = cp;
-    }
-
-    /*
-     * Get new format string.  Must be before chdir().
-     */
-    nfs = new_fs (form, format, FORMAT);
-    ncomps = fmt_compile(nfs, &fmt);
-
-    fmt_dump(fmt);
-    return done(0);
+
+       /* Set format string.  Must be before chdir(). */
+       fmtstr = new_fs(form, scanformat);
+       fmt_compile(fmtstr, &fmt);
+
+       fmt_dump(fmt);
+       return EX_OK;
 }
 
 static void
-fmt_dump (struct format *fmth)
+fmt_dump(struct format *fmth)
 {
        int i;
-       register struct format *fmt, *addr;
+       struct format *fmt, *addr;
 
        /* Assign labels */
        for (fmt = fmth; fmt; ++fmt) {
                i = fmt->f_type;
-               if (i == FT_IF_S ||
-                   i == FT_IF_S_NULL ||
-                   i == FT_IF_V_EQ ||
-                   i == FT_IF_V_NE ||
-                   i == FT_IF_V_GT ||
-                   i == FT_IF_MATCH ||
-                   i == FT_IF_AMATCH ||
-                   i == FT_GOTO) {
+               if (i == FT_IF_S || i == FT_IF_S_NULL || i == FT_IF_V_EQ ||
+                               i == FT_IF_V_NE || i == FT_IF_V_GT ||
+                               i == FT_IF_MATCH || i == FT_IF_AMATCH ||
+                               i == FT_GOTO) {
                        addr = fmt + fmt->f_skip;
                        if (findlabel(addr) < 0)
                                assignlabel(addr);
@@ -140,7 +131,7 @@ fmt_dump (struct format *fmth)
 static void
 dumpone(struct format *fmt)
 {
-       register int i;
+       int i;
 
        if ((i = findlabel(fmt)) >= 0)
                printf("L%d:", i);
@@ -159,7 +150,8 @@ dumpone(struct format *fmt)
                if (fmt->f_comp->c_type)
                        printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
                if (fmt->f_comp->c_flags)
-                       printf(", c_flags %d", fmt->f_comp->c_flags);
+                       printf(", c_flags %s",
+                                       c_flagsstr(fmt->f_comp->c_flags));
                break;
 
        case FT_LV_SEC:
@@ -191,7 +183,8 @@ dumpone(struct format *fmt)
                if (fmt->f_comp->c_type)
                        printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
                if (fmt->f_comp->c_flags)
-                       printf(", c_flags %d", fmt->f_comp->c_flags);
+                       printf(", c_flags %s",
+                                       c_flagsstr(fmt->f_comp->c_flags));
                break;
 
        case FT_LS_ADDR:
@@ -213,7 +206,8 @@ dumpone(struct format *fmt)
                if (fmt->f_comp->c_type)
                        printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
                if (fmt->f_comp->c_flags)
-                       printf(", c_flags %d", fmt->f_comp->c_flags);
+                       printf(", c_flags %s",
+                                       c_flagsstr(fmt->f_comp->c_flags));
                break;
 
        case FT_COMPF:
@@ -224,7 +218,8 @@ dumpone(struct format *fmt)
                if (fmt->f_comp->c_type)
                        printf(", c_type %s", c_typestr(fmt->f_comp->c_type));
                if (fmt->f_comp->c_flags)
-                       printf(", c_flags %d", fmt->f_comp->c_flags);
+                       printf(", c_flags %s",
+                                       c_flagsstr(fmt->f_comp->c_flags));
                break;
 
        case FT_STRF:
@@ -310,6 +305,9 @@ dumpone(struct format *fmt)
        case FT_LV_DAT:
                printf(", value dat[%d]", fmt->f_value);
                break;
+
+       case FT_LS_UNMAILTO:
+               break;
        }
        putchar('\n');
 }
@@ -317,7 +315,7 @@ dumpone(struct format *fmt)
 static int
 findlabel(struct format *addr)
 {
-       register int i;
+       int i;
 
        for (i = 0; i < lused; ++i)
                if (addr == lvec[i])
@@ -341,7 +339,7 @@ f_typestr(int t)
        case FT_COMPF: return("COMPF");
        case FT_LIT: return("LIT");
        case FT_LITF: return("LITF");
-#ifdef FT_LIT_FORCE
+#ifdef FT_LIT_FORCE
        case FT_LIT_FORCE: return("LIT_FORCE");
 #endif
        case FT_CHAR: return("CHAR");
@@ -406,11 +404,8 @@ f_typestr(int t)
        case FT_PARSEADDR: return("PARSEADDR");
        case FT_FORMATADDR: return("FORMATADDR");
        case FT_MYMBOX: return("MYMBOX");
-#ifdef FT_ADDTOSEQ
-       case FT_ADDTOSEQ: return("ADDTOSEQ");
-#endif
        case FT_SAVESTR: return("SAVESTR");
-#ifdef FT_PAUSE
+#ifdef FT_PAUSE
        case FT_PAUSE: return ("PAUSE");
 #endif
        case FT_DONE: return("DONE");
@@ -430,6 +425,7 @@ f_typestr(int t)
        case FT_V_GT: return("V_GT");
        case FT_V_MATCH: return("V_MATCH");
        case FT_V_AMATCH: return("V_AMATCH");
+       case FT_LS_UNMAILTO: return("LS_UNMAILTO");
        default:
                printf(buf, "/* ??? #%d */", t);
                return(buf);
@@ -437,30 +433,46 @@ f_typestr(int t)
 }
 
 #define FNORD(v, s) if (t & (v)) { \
-       if (i++ > 0) \
-               strcat(buf, "|"); \
-       strcat(buf, s); }
+               if (i++ > 0) \
+                       strcat(buf, "|"); \
+               strcat(buf, s); }
 
 static char *
 c_typestr(int t)
 {
-       register int i;
+       int i;
        static char buf[64];
 
        buf[0] = '\0';
-       if (t & ~(CT_ADDR|CT_DATE|CT_MYMBOX|CT_ADDRPARSE))
+       if (t & ~(CT_ADDR|CT_DATE))
                printf(buf, "0x%x ", t);
        strcat(buf, "<");
        i = 0;
        FNORD(CT_ADDR, "ADDR");
        FNORD(CT_DATE, "DATE");
-       FNORD(CT_MYMBOX, "MYMBOX");
-       FNORD(CT_ADDRPARSE, "ADDRPARSE");
        strcat(buf, ">");
        return(buf);
-#undef FNORD
 }
 
+static char *
+c_flagsstr(int t)
+{
+       int i;
+       static char buf[64];
+
+       buf[0] = '\0';
+       if (t & ~(CF_TRUE|CF_PARSED|CF_DATEFAB))
+               printf(buf, "0x%x ", t);
+       strcat(buf, "<");
+       i = 0;
+       FNORD(CF_TRUE, "TRUE");
+       FNORD(CF_PARSED, "PARSED");
+       FNORD(CF_DATEFAB, "DATEFAB");
+       strcat(buf, ">");
+       return(buf);
+}
+#undef FNORD
+
 static void
 litputs(char *s)
 {
@@ -499,7 +511,8 @@ litputc(char c)
                        putc('t', stdout);
                } else {
                        putc('^', stdout);
-                       putc(c ^ 0x40, stdout); /* DEL to ?, others to alpha */
+                       putc(c ^ 0x40, stdout);
+                       /* DEL to ?, others to alpha */
                }
        } else
                putc(c, stdout);