]> diplodocus.org Git - nmh/blobdiff - uip/prompter.c
Alter HasSuffixC()'s char * to be const.
[nmh] / uip / prompter.c
index 6b540dbca49ee5e12a336d949b4e3bd5464cddcb..f5332e46d37e8249a5c2041a8c1634922f38b06d 100644 (file)
@@ -10,8 +10,6 @@
 #include <h/mh.h>
 #include <fcntl.h>
 #include <h/signals.h>
-#include <errno.h>
-#include <signal.h>
 #include <setjmp.h>
 
 #include <termios.h>
 # define CERASE '#'
 #endif
 
-static struct swit switches[] = {
-#define        ERASESW 0
-    { "erase chr", 0 },
-#define        KILLSW  1
-    { "kill chr", 0 },
-#define        PREPSW  2
-    { "prepend", 0 },
-#define        NPREPSW 3
-    { "noprepend", 0 },        
-#define        RAPDSW  4
-    { "rapid", 0 },
-#define        NRAPDSW 5
-    { "norapid", 0 },
-#define        BODYSW  6
-    { "body", -4 },
-#define        NBODYSW 7
-    { "nobody", -6 },
-#define        DOTSW   8
-    { "doteof", 0 },
-#define        NDOTSW  9
-    { "nodoteof", 0 },
-#define VERSIONSW 10
-    { "version", 0 },
-#define        HELPSW  11
-    { "help", 0 },
-    { NULL, 0 }
-};
+#define PROMPTER_SWITCHES \
+    X("erase chr", 0, ERASESW) \
+    X("kill chr", 0, KILLSW) \
+    X("prepend", 0, PREPSW) \
+    X("noprepend", 0, NPREPSW) \
+    X("rapid", 0, RAPDSW) \
+    X("norapid", 0, NRAPDSW) \
+    X("body", -4, BODYSW) \
+    X("nobody", -6, NBODYSW) \
+    X("doteof", 0, DOTSW) \
+    X("nodoteof", 0, NDOTSW) \
+    X("version", 0, VERSIONSW) \
+    X("help", 0, HELPSW) \
+
+#define X(sw, minchars, id) id,
+DEFINE_SWITCH_ENUM(PROMPTER);
+#undef X
+
+#define X(sw, minchars, id) { sw, minchars, id },
+DEFINE_SWITCH_ARRAY(PROMPTER, switches);
+#undef X
 
 
 static struct termios tio;
@@ -80,18 +72,13 @@ main (int argc, char **argv)
     int doteof = 0, fdi, fdo, i, state;
     char *cp, *drft = NULL, *erasep = NULL;
     char *killp = NULL, name[NAMESZ], field[BUFSIZ];
-    char buffer[BUFSIZ], tmpfil[BUFSIZ];
+    char buffer[BUFSIZ];
     char **arguments, **argp;
     FILE *in, *out;
-    char *tfile = NULL;
+    char *tmpfil;
+    m_getfld_state_t gstate = 0;
 
-#ifdef LOCALE
-    setlocale(LC_ALL, "");
-#endif
-    invo_name = r1bindex (argv[0], '/');
-
-    /* read user profile/context */
-    context_read();
+    if (nmh_init(argv[0], 2)) { return 1; }
 
     arguments = getarguments (invo_name, argc, argv, 1);
     argp = arguments;
@@ -161,10 +148,9 @@ main (int argc, char **argv)
     if ((in = fopen (drft, "r")) == NULL)
        adios (drft, "unable to open");
 
-    tfile = m_mktemp2(NULL, invo_name, NULL, &out);
-    if (tfile == NULL) adios("prompter", "unable to create temporary file");
-    chmod (tmpfil, 0600);
-    strncpy (tmpfil, tfile, sizeof(tmpfil));
+    if ((tmpfil = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) {
+       adios(NULL, "unable to create temporary file in %s", get_temp_dir());
+    }
 
     /*
      * Are we changing the kill or erase character?
@@ -208,11 +194,10 @@ main (int argc, char **argv)
     /*
      * Loop through the lines of the draft skeleton.
      */
-    for (state = FLD;;) {
+    for (;;) {
        int fieldsz = sizeof field;
-       switch (state = m_getfld (state, name, field, &fieldsz, in)) {
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, in)) {
            case FLD: 
-           case FLDEOF: 
            case FLDPLUS: 
                /*
                 * Check if the value of field contains anything
@@ -228,7 +213,7 @@ main (int argc, char **argv)
                    fprintf (out, "%s:%s", name, field);
                    while (state == FLDPLUS) {
                        fieldsz = sizeof field;
-                       state = m_getfld (state, name, field, &fieldsz, in);
+                       state = m_getfld (&gstate, name, field, &fieldsz, in);
                        printf ("%s", field);
                        fprintf (out, "%s", field);
                    }
@@ -242,7 +227,7 @@ abort:
                        if (killp || erasep) {
                            tcsetattr(0, TCSADRAIN, &tio);
                        }
-                       unlink (tmpfil);
+                       (void) m_unlink (tmpfil);
                        done (1);
                    }
                    if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
@@ -258,17 +243,9 @@ abort:
                    }
                }
 
-               if (state == FLDEOF) {  /* moby hack */
-                   fprintf (out, "--------\n");
-                   printf ("--------\n");
-                   if (!body)
-                       break;
-                   goto no_body;
-               }
                continue;
 
            case BODY: 
-           case BODYEOF:
            case FILEEOF: 
                if (!body)
                    break;
@@ -295,13 +272,13 @@ abort:
                            printf ("%s", field);
                    } while (state == BODY &&
                            (fieldsz = sizeof field,
-                            state = m_getfld (state, name, field, &fieldsz, in)));
+                            state = m_getfld (&gstate, name, field, &fieldsz, in)));
                    if (prepend || !body)
                        break;
                    else
                        printf ("\n--------Enter additional text\n\n");
                }
-no_body:
+
                fflush (stdout);
                for (;;) {
                    getln (field, sizeof(field));
@@ -318,6 +295,7 @@ no_body:
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     if (body)
        printf ("--------\n");
@@ -338,7 +316,7 @@ no_body:
     cpydata (fdi, fdo, tmpfil, drft);
     close (fdi);
     close (fdo);
-    unlink (tmpfil);
+    (void) m_unlink (tmpfil);
 
     context_save ();   /* save the context file */
     done (0);
@@ -351,9 +329,9 @@ getln (char *buffer, int n)
 {
     int c;
     char *cp;
+    static int quoting = 0;
 
-    cp = buffer;
-    *cp = 0;
+    *buffer = 0;
 
     switch (setjmp (sigenv)) {
        case OK: 
@@ -369,15 +347,20 @@ getln (char *buffer, int n)
            return NOTOK;
     }
 
+    cp = buffer;
+    *cp = 0;
+
     for (;;) {
        switch (c = getchar ()) {
            case EOF: 
+               quoting = 0;
                clearerr (stdin);
                longjmp (sigenv, DONE);
 
            case '\n': 
-               if (cp[-1] == QUOTE) {
-                   cp[-1] = c;
+               if (quoting) {
+                   *(cp - 1) = c;
+                   quoting = 0;
                    wtuser = 0;
                    return 1;
                }
@@ -387,6 +370,11 @@ getln (char *buffer, int n)
                return 0;
 
            default: 
+               if (c == QUOTE) {
+                   quoting = 1;
+               } else {
+                   quoting = 0;
+               }
                if (cp < buffer + n)
                    *cp++ = c;
                *cp = 0;