Renamed -version switch to -Version to remove the conflict with -verbose.
[mmh] / uip / prompter.c
index 5b2131c..6db0455 100644 (file)
 #include <signal.h>
 #include <setjmp.h>
 
-#ifdef HAVE_TERMIOS_H
-# include <termios.h>
-#else
-# ifdef HAVE_TERMIO_H
-#  include <termio.h>
-# else
-#  include <sgtty.h>
-# endif
-#endif
+#include <termios.h>
 
 #define QUOTE '\\'
 
@@ -55,32 +47,17 @@ static struct swit switches[] = {
 #define NDOTSW  9
        { "nodoteof", 0 },
 #define VERSIONSW 10
-       { "version", 0 },
+       { "Version", 0 },
 #define HELPSW  11
        { "help", 0 },
        { NULL, 0 }
 };
 
 
-#ifdef HAVE_TERMIOS_H
 static struct termios tio;
-# define ERASE tio.c_cc[VERASE]
-# define KILL  tio.c_cc[VKILL]
-# define INTR  tio.c_cc[VINTR]
-#else
-# ifdef HAVE_TERMIO_H
-static struct termio tio;
-#  define ERASE tio.c_cc[VERASE]
-#  define KILL  tio.c_cc[VKILL]
-#  define INTR  tio.c_cc[VINTR]
-# else
-static struct sgttyb tio;
-static struct tchars tc;
-#  define ERASE tio.sg_erase
-#  define KILL  tio.sg_kill
-#  define INTR  tc.t_intrc
-# endif
-#endif
+#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;
@@ -92,7 +69,7 @@ static jmp_buf sigenv;
 int getln(char *, int);
 static int chrcnv(char *);
 static void chrdsp(char *, char);
-static RETSIGTYPE intrser(int);
+static void intrser(int);
 
 
 int
@@ -121,58 +98,60 @@ main(int argc, char **argv)
        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(buffer, sizeof(buffer),
-                                                       "%s [switches] file",
-                                                       invo_name);
-                                       print_help(buffer, switches, 1);
-                                       done(1);
-                               case VERSIONSW:
-                                       print_version(invo_name);
-                                       done(1);
+                       case AMBIGSW:
+                               ambigsw(cp, switches);
+                               done(1);
+                       case UNKWNSW:
+                               adios(NULL, "-%s unknown", cp);
+
+                       case HELPSW:
+                               snprintf(buffer, sizeof(buffer),
+                                               "%s [switches] file",
+                                               invo_name);
+                               print_help(buffer, switches, 1);
+                               done(1);
+                       case VERSIONSW:
+                               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;
+                       case NPREPSW:
+                               prepend = 0;
+                               continue;
 
-                               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;
-                               case NPREPSW:
-                                       prepend = 0;
-                                       continue;
-
-                               case RAPDSW:
-                                       rapid++;
-                                       continue;
-                               case NRAPDSW:
-                                       rapid = 0;
-                                       continue;
-
-                               case BODYSW:
-                                       body++;
-                                       continue;
-                               case NBODYSW:
-                                       body = 0;
-                                       continue;
-
-                               case DOTSW:
-                                       doteof++;
-                                       continue;
-                               case NDOTSW:
-                                       doteof = 0;
-                                       continue;
+                       case RAPDSW:
+                               rapid++;
+                               continue;
+                       case NRAPDSW:
+                               rapid = 0;
+                               continue;
+
+                       case BODYSW:
+                               body++;
+                               continue;
+                       case NBODYSW:
+                               body = 0;
+                               continue;
+
+                       case DOTSW:
+                               doteof++;
+                               continue;
+                       case NDOTSW:
+                               doteof = 0;
+                               continue;
                        }
                } else if (!drft) {
                        drft = cp;
@@ -193,23 +172,10 @@ main(int argc, char **argv)
        ** Are we changing the kill or erase character?
        */
        if (killp || erasep) {
-#ifdef HAVE_TERMIOS_H
                cc_t save_erase, save_kill;
-#else
-               int save_erase, save_kill;
-#endif
 
                /* get the current terminal attributes */
-#ifdef HAVE_TERMIOS_H
                tcgetattr(0, &tio);
-#else
-# ifdef HAVE_TERMIO_H
-               ioctl(0, TCGETA, &tio);
-# else
-               ioctl(0, TIOCGETP, (char *) &tio);
-               ioctl(0, TIOCGETC, (char *) &tc);
-# endif
-#endif
 
                /* save original kill, erase character for later */
                save_kill = KILL;
@@ -220,15 +186,7 @@ main(int argc, char **argv)
                ERASE = erasep ? chrcnv(erasep) : save_erase;
 
                /* set the new terminal attributes */
-#ifdef HAVE_TERMIOS_H
                 tcsetattr(0, TCSADRAIN, &tio);
-#else
-# ifdef HAVE_TERMIO_H
-               ioctl(0, TCSETAW, &tio);
-# else
-               ioctl(0, TIOCSETN, (char *) &tio);
-# endif
-#endif
 
                /* print out new kill erase characters */
                chrdsp("erase", ERASE);
@@ -255,115 +213,107 @@ main(int argc, char **argv)
        for (state = FLD;;) {
                switch (state = m_getfld(state, name, field, sizeof(field),
                                in)) {
-                       case FLD:
-                       case FLDEOF:
-                       case FLDPLUS:
-                               /*
-                               ** Check if the value of field contains
-                               ** anything other than space or tab.
-                               */
-                               for (cp = field; *cp; cp++)
-                                       if (*cp != ' ' && *cp != '\t')
-                                               break;
-
-                               /* If so, just add header line to draft */
-                               if (*cp++ != '\n' || *cp != 0) {
-                                       printf("%s:%s", name, field);
-                                       fprintf(out, "%s:%s", name, field);
-                                       while (state == FLDPLUS) {
-                                               state = m_getfld(state, name, field, sizeof(field), in);
-                                               printf("%s", field);
-                                               fprintf(out, "%s", field);
-                                       }
-                               } else {
-                                       /* Else, get value of header field */
-                                       printf("%s: ", name);
-                                       fflush(stdout);
-                                       i = getln(field, sizeof(field));
-                                       if (i == -1) {
+               case FLD:
+               case FLDEOF:
+               case FLDPLUS:
+                       /*
+                       ** Check if the value of field contains
+                       ** anything other than space or tab.
+                       */
+                       for (cp = field; *cp; cp++)
+                               if (*cp != ' ' && *cp != '\t')
+                                       break;
+
+                       /* If so, just add header line to draft */
+                       if (*cp++ != '\n' || *cp != 0) {
+                               printf("%s:%s", name, field);
+                               fprintf(out, "%s:%s", name, field);
+                               while (state == FLDPLUS) {
+                                       state = m_getfld(state, name, field,
+                                                       sizeof(field), in);
+                                       printf("%s", field);
+                                       fprintf(out, "%s", field);
+                               }
+                       } else {
+                               /* Else, get value of header field */
+                               printf("%s: ", name);
+                               fflush(stdout);
+                               i = getln(field, sizeof(field));
+                               if (i == -1) {
 abort:
-                                               if (killp || erasep) {
-#ifdef HAVE_TERMIOS_H
-                                                       tcsetattr(0, TCSADRAIN, &tio);
-#else
-# ifdef HAVE_TERMIO
-                                                       ioctl(0, TCSETA, &tio);
-# else
-                                                       ioctl(0, TIOCSETN, (char *) &tio);
-# endif
-#endif
-                                               }
-                                               unlink(tmpfil);
-                                               done(1);
-                                       }
-                                       if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
-                                               fprintf(out, "%s:", name);
-                                               do {
-                                                       if (field[0] != ' ' && field[0] != '\t')
-                                                               putc(' ', out);
-                                                       fprintf(out, "%s", field);
-                                               } while (i == 1 && (i = getln(field, sizeof(field))) >= 0);
-                                               if (i == -1)
-                                                       goto abort;
+                                       if (killp || erasep) {
+                                               tcsetattr(0, TCSADRAIN, &tio);
                                        }
+                                       unlink(tmpfil);
+                                       done(1);
                                }
-
-                               if (state == FLDEOF) {  /* moby hack */
-                                       fprintf(out, "--------\n");
-                                       printf("--------\n");
-                                       if (!body)
-                                               break;
-                                       goto no_body;
+                               if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
+                                       fprintf(out, "%s:", name);
+                                       do {
+                                               if (field[0] != ' ' && field[0] != '\t')
+                                                       putc(' ', out);
+                                               fprintf(out, "%s", field);
+                                       } while (i == 1 && (i = getln(field, sizeof(field))) >= 0);
+                                       if (i == -1)
+                                               goto abort;
                                }
-                               continue;
+                       }
 
-                       case BODY:
-                       case BODYEOF:
-                       case FILEEOF:
+                       if (state == FLDEOF) {  /* moby hack */
+                               fprintf(out, "--------\n");
+                               printf("--------\n");
                                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");
-                                               fflush(stdout);
-                                               for (;;) {
-                                                       getln(buffer, sizeof(buffer));
-                                                       if (doteof && buffer[0] == '.' && buffer[1] == '\n')
-                                                               break;
-                                                       if (buffer[0] == 0)
-                                                               break;
-                                                       fprintf(out, "%s", buffer);
-                                               }
-                                       }
+                               goto no_body;
+                       }
+                       continue;
 
-                                       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");
+               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");
+                                       fflush(stdout);
+                                       for (;;) {
+                                               getln(buffer, sizeof(buffer));
+                                               if (doteof && buffer[0] == '.' && buffer[1] == '\n')
+                                                       break;
+                                               if (buffer[0] == 0)
+                                                       break;
+                                               fprintf(out, "%s", buffer);
+                                       }
                                }
+
+                               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:
-                               fflush(stdout);
-                               for (;;) {
-                                       getln(field, sizeof(field));
-                                       if (doteof && field[0] == '.' && field[1] == '\n')
-                                               break;
-                                       if (field[0] == 0)
-                                               break;
-                                        fprintf(out, "%s", field);
-                               }
-                               break;
+                       fflush(stdout);
+                       for (;;) {
+                               getln(field, sizeof(field));
+                               if (doteof && field[0] == '.' && field[1] == '\n')
+                                       break;
+                               if (field[0] == 0)
+                                       break;
+                                fprintf(out, "%s", field);
+                       }
+                       break;
 
-                       default:
-                               adios(NULL, "skeleton is poorly formatted");
+               default:
+                       adios(NULL, "skeleton is poorly formatted");
                }
                break;
        }
@@ -377,15 +327,7 @@ no_body:
        SIGNAL(SIGINT, SIG_IGN);
 
        if (killp || erasep) {
-#ifdef HAVE_TERMIOS_H
                 tcsetattr(0, TCSADRAIN, &tio);
-#else
-# ifdef HAVE_TERMIO_H
-               ioctl(0, TCSETAW, &tio);
-# else
-               ioctl(0, TIOCSETN, (char *) &tio);
-# endif
-#endif
        }
 
        if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK)
@@ -413,52 +355,48 @@ getln(char *buffer, int n)
        *cp = 0;
 
        switch (setjmp(sigenv)) {
-               case OK:
-                       wtuser = 1;
-                       break;
+       case OK:
+               wtuser = 1;
+               break;
 
-               case DONE:
-                       wtuser = 0;
-                       return 0;
+       case DONE:
+               wtuser = 0;
+               return 0;
 
-               default:
-                       wtuser = 0;
-                       return NOTOK;
+       default:
+               wtuser = 0;
+               return NOTOK;
        }
 
        for (;;) {
                switch (c = getchar()) {
-                       case EOF:
-                               clearerr(stdin);
-                               longjmp(sigenv, DONE);
-
-                       case '\n':
-                               if (cp[-1] == QUOTE) {
-                                       cp[-1] = c;
-                                       wtuser = 0;
-                                       return 1;
-                               }
-                               *cp++ = c;
-                               *cp = 0;
+               case EOF:
+                       clearerr(stdin);
+                       longjmp(sigenv, DONE);
+
+               case '\n':
+                       if (cp[-1] == QUOTE) {
+                               cp[-1] = c;
                                wtuser = 0;
-                               return 0;
+                               return 1;
+                       }
+                       *cp++ = c;
+                       *cp = 0;
+                       wtuser = 0;
+                       return 0;
 
-                       default:
-                               if (cp < buffer + n)
-                                       *cp++ = c;
-                               *cp = 0;
+               default:
+                       if (cp < buffer + n)
+                               *cp++ = c;
+                       *cp = 0;
                }
        }
 }
 
 
-static RETSIGTYPE
+static void
 intrser(int i)
 {
-#ifndef RELIABLE_SIGNALS
-       SIGNAL(SIGINT, intrser);
-#endif
-
        if (wtuser)
                longjmp(sigenv, NOTOK);
        sigint++;