prompter: Various rework.
[mmh] / uip / prompter.c
index 024f604..e1f0bff 100644 (file)
 #include <signal.h>
 #include <setjmp.h>
 
-#include <termios.h>
-
-#define QUOTE '\\'
-
-#ifndef CKILL
-# define CKILL '@'
-#endif
-
-#ifndef CERASE
-# define CERASE '#'
-#endif
-
 static struct swit switches[] = {
-#define ERASESW  0
-       { "erase chr", 0 },
-#define KILLSW  1
-       { "kill chr", 0 },
-#define PREPSW  2
+#define PREPSW  0
        { "prepend", 0 },
-#define NPREPSW  3
+#define NPREPSW  1
        { "noprepend", 2 },
-#define RAPDSW  4
+#define RAPDSW  2
        { "rapid", 0 },
-#define NRAPDSW  5
+#define NRAPDSW  3
        { "norapid", 2 },
-#define BODYSW  6
-       { "body", -4 },
-#define NBODYSW  7
-       { "nobody", -6 },
-#define DOTSW  8
-       { "doteof", 0 },
-#define NDOTSW  9
-       { "nodoteof", 2 },
-#define VERSIONSW 10
+#define BODYSW  4
+       { "body", 0 },
+#define NBODYSW  5
+       { "nobody", 2 },
+#define VERSIONSW 6
        { "Version", 0 },
-#define HELPSW  11
+#define HELPSW  7
        { "help", 0 },
        { NULL, 0 }
 };
 
 
-static struct termios tio;
-#define ERASE tio.c_cc[VERASE]
-#define KILL  tio.c_cc[VKILL]
-#define INTR  tio.c_cc[VINTR]
-
 static int wtuser = 0;
 static int sigint = 0;
 static jmp_buf sigenv;
@@ -67,18 +42,16 @@ static jmp_buf sigenv;
 ** prototypes
 */
 int getln(char *, int);
-static int chrcnv(char *);
-static void chrdsp(char *, char);
 static void intrser(int);
 
 
 int
 main(int argc, char **argv)
 {
-       int body = 1, prepend = 1, rapid = 0;
-       int doteof = 0, fdi, fdo, i, state;
-       char *cp, *drft = NULL, *erasep = NULL;
-       char *killp = NULL, name[NAMESZ], field[BUFSIZ];
+       int qbody = 1, prepend = 1, rapid = 0;
+       int fdi, fdo, i, state;
+       char *cp, *drft = NULL;
+       char name[NAMESZ], field[BUFSIZ];
        char buffer[BUFSIZ], tmpfil[BUFSIZ];
        char **arguments, **argp;
        FILE *in, *out;
@@ -95,7 +68,7 @@ main(int argc, char **argv)
        arguments = getarguments(invo_name, argc, argv, 1);
        argp = arguments;
 
-       while ((cp = *argp++))
+       while ((cp = *argp++)) {
                if (*cp == '-') {
                        switch (smatch(++cp, switches)) {
                        case AMBIGSW:
@@ -114,17 +87,6 @@ main(int argc, char **argv)
                                print_version(invo_name);
                                done(1);
 
-                       case ERASESW:
-                               if (!(erasep = *argp++) || *erasep == '-')
-                                       adios(NULL, "missing argument to %s",
-                                                       argp[-2]);
-                               continue;
-                       case KILLSW:
-                               if (!(killp = *argp++) || *killp == '-')
-                                       adios(NULL, "missing argument to %s",
-                                                       argp[-2]);
-                               continue;
-
                        case PREPSW:
                                prepend++;
                                continue;
@@ -140,22 +102,16 @@ main(int argc, char **argv)
                                continue;
 
                        case BODYSW:
-                               body++;
+                               qbody++;
                                continue;
                        case NBODYSW:
-                               body = 0;
-                               continue;
-
-                       case DOTSW:
-                               doteof++;
-                               continue;
-                       case NDOTSW:
-                               doteof = 0;
+                               qbody = 0;
                                continue;
                        }
                } else if (!drft) {
                        drft = cp;
                }
+       }
 
        if (!drft)
                adios(NULL, "usage: %s [switches] file", invo_name);
@@ -168,42 +124,6 @@ main(int argc, char **argv)
        chmod(tmpfil, 0600);
        strncpy(tmpfil, tfile, sizeof(tmpfil));
 
-       /*
-       ** Are we changing the kill or erase character?
-       */
-       if (killp || erasep) {
-               cc_t save_erase, save_kill;
-
-               /* get the current terminal attributes */
-               tcgetattr(0, &tio);
-
-               /* save original kill, erase character for later */
-               save_kill = KILL;
-               save_erase = ERASE;
-
-               /* set new kill, erase character in terminal structure */
-               KILL = killp ? chrcnv(killp) : save_kill;
-               ERASE = erasep ? chrcnv(erasep) : save_erase;
-
-               /* set the new terminal attributes */
-                tcsetattr(0, TCSADRAIN, &tio);
-
-               /* print out new kill erase characters */
-               chrdsp("erase", ERASE);
-               chrdsp(", kill", KILL);
-               chrdsp(", intr", INTR);
-               putchar('\n');
-               fflush(stdout);
-
-               /*
-               ** We set the kill and erase character back to original
-               ** setup in terminal structure so we can easily
-               ** restore it upon exit.
-               */
-               KILL = save_kill;
-               ERASE = save_erase;
-       }
-
        sigint = 0;
        SIGNAL2(SIGINT, intrser);
 
@@ -225,7 +145,7 @@ main(int argc, char **argv)
                                        break;
 
                        /* If so, just add header line to draft */
-                       if (*cp++ != '\n' || *cp != 0) {
+                       if (*cp++ != '\n' || *cp) {
                                printf("%s:%s", name, field);
                                fprintf(out, "%s:%s", name, field);
                                while (state == FLDPLUS) {
@@ -241,13 +161,10 @@ main(int argc, char **argv)
                                i = getln(field, sizeof(field));
                                if (i == -1) {
 abort:
-                                       if (killp || erasep) {
-                                               tcsetattr(0, TCSADRAIN, &tio);
-                                       }
                                        unlink(tmpfil);
                                        done(1);
                                }
-                               if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
+                               if (i || (field[0]!='\n' && field[0]!='\0')) {
                                        fprintf(out, "%s:", name);
                                        do {
                                                if (field[0] != ' ' && field[0] != '\t')
@@ -260,55 +177,58 @@ abort:
                        }
 
                        if (state == FLDEOF) {  /* moby hack */
+                               /* draft has no body separator; only headers */
                                fprintf(out, "--------\n");
-                               printf("--------\n");
-                               if (!body)
+                               if (!qbody)
                                        break;
-                               goto no_body;
+                               printf("--------\n");
+                               goto has_no_body;
                        }
                        continue;
 
                case BODY:
                case BODYEOF:
                case FILEEOF:
-                       if (!body)
-                               break;
                        fprintf(out, "--------\n");
-                       if (field[0] == 0 || !prepend)
-                               printf("--------\n");
-                       if (field[0]) {
-                               if (prepend && body) {
-                                       printf("\n--------Enter initial text\n\n");
+                       if (qbody) {
+                               if (!*field) {
+                                       printf("--------\n");
+                                       goto has_no_body;
+                               }
+
+                               if (prepend) {
+                                       printf("--------Enter initial text\n");
                                        fflush(stdout);
                                        for (;;) {
                                                getln(buffer, sizeof(buffer));
-                                               if (doteof && buffer[0] == '.' && buffer[1] == '\n')
-                                                       break;
-                                               if (buffer[0] == 0)
+                                               if (!*buffer)
                                                        break;
                                                fprintf(out, "%s", buffer);
                                        }
+                               } else {
+                                       printf("--------\n");
                                }
-
-                               do {
-                                       fprintf(out, "%s", field);
-                                       if (!rapid && !sigint)
-                                               printf("%s", field);
-                               } while (state == BODY && (state = m_getfld(state, name, field, sizeof(field), in)));
-                               if (prepend || !body)
-                                       break;
-                               else
-                                       printf ("\n--------Enter additional text\n\n");
                        }
-no_body:
+
+                       do {
+                               fprintf(out, "%s", field);
+                               if (!rapid && !sigint)
+                                       printf("%s", field);
+                       } while (state == BODY &&
+                                       (state = m_getfld(state, name,
+                                       field, sizeof(field), in)));
+
+                       if (prepend || !qbody)
+                               break;
+
+                       printf("--------Enter additional text\n");
+has_no_body:
                        fflush(stdout);
                        for (;;) {
                                getln(field, sizeof(field));
-                               if (doteof && field[0] == '.' && field[1] == '\n')
-                                       break;
-                               if (field[0] == 0)
+                               if (!*field)
                                        break;
-                                fprintf(out, "%s", field);
+                               fprintf(out, "%s", field);
                        }
                        break;
 
@@ -318,7 +238,7 @@ no_body:
                break;
        }
 
-       if (body)
+       if (qbody)
                printf("--------\n");
 
        fflush(stdout);
@@ -326,10 +246,6 @@ no_body:
        fclose(out);
        SIGNAL(SIGINT, SIG_IGN);
 
-       if (killp || erasep) {
-                tcsetattr(0, TCSADRAIN, &tio);
-       }
-
        if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK)
                adios(tmpfil, "unable to re-open");
        if ((fdo = creat(drft, m_gmprot())) == NOTOK)
@@ -352,7 +268,7 @@ getln(char *buffer, int n)
        char *cp;
 
        cp = buffer;
-       *cp = 0;
+       *cp = '\0';
 
        switch (setjmp(sigenv)) {
        case OK:
@@ -375,20 +291,20 @@ getln(char *buffer, int n)
                        longjmp(sigenv, DONE);
 
                case '\n':
-                       if (cp[-1] == QUOTE) {
+                       if (cp[-1] == '\\') {
                                cp[-1] = c;
                                wtuser = 0;
                                return 1;
                        }
                        *cp++ = c;
-                       *cp = 0;
+                       *cp = '\0';
                        wtuser = 0;
                        return 0;
 
                default:
                        if (cp < buffer + n)
                                *cp++ = c;
-                       *cp = 0;
+                       *cp = '\0';
                }
        }
 }
@@ -401,21 +317,3 @@ intrser(int i)
                longjmp(sigenv, NOTOK);
        sigint++;
 }
-
-
-static int
-chrcnv(char *cp)
-{
-       return (*cp != QUOTE ? *cp : m_atoi(++cp));
-}
-
-
-static void
-chrdsp(char *s, char c)
-{
-       printf("%s ", s);
-       if (c < ' ' || c == 0177)
-               printf("^%c", c ^ 0100);
-       else
-               printf("%c", c);
-}