]> diplodocus.org Git - nmh/blobdiff - uip/prompter.c
Try using -man instead of -mandoc in test-manpages on OpenBSD
[nmh] / uip / prompter.c
index 35f6860a4ac49536a06f085991d56daf24b09294..05f8348cfa91f4f2ef2aabb7ff2112c326460364 100644 (file)
@@ -2,8 +2,6 @@
 /*
  * prompter.c -- simple prompting editor front-end
  *
- * $Id$
- *
  * 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 <fcntl.h>
 #include <h/signals.h>
-#include <errno.h>
-#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 '\\'
 
 # 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 }
-};
-
-
-#ifdef HAVE_TERMIOS_H
+#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;
-# 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;
@@ -95,7 +62,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
@@ -108,6 +75,8 @@ main (int argc, char **argv)
     char buffer[BUFSIZ], tmpfil[BUFSIZ];
     char **arguments, **argp;
     FILE *in, *out;
+    char *tfile = NULL;
+    m_getfld_state_t gstate = 0;
 
 #ifdef LOCALE
     setlocale(LC_ALL, "");
@@ -133,10 +102,10 @@ main (int argc, char **argv)
                    snprintf (buffer, sizeof(buffer), "%s [switches] file",
                        invo_name);
                    print_help (buffer, switches, 1);
-                   done (1);
+                   done (0);
                case VERSIONSW:
                    print_version(invo_name);
-                   done (1);
+                   done (0);
 
                case ERASESW: 
                    if (!(erasep = *argp++) || *erasep == '-')
@@ -185,32 +154,19 @@ main (int argc, char **argv)
     if ((in = fopen (drft, "r")) == NULL)
        adios (drft, "unable to open");
 
-    strncpy (tmpfil, m_tmpfil (invo_name), sizeof(tmpfil));
-    if ((out = fopen (tmpfil, "w")) == NULL)
-       adios (tmpfil, "unable to create");
+    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));
 
     /*
      * 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;
@@ -221,15 +177,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);
@@ -253,10 +201,10 @@ main (int argc, char **argv)
     /*
      * Loop through the lines of the draft skeleton.
      */
-    for (state = FLD;;) {
-       switch (state = m_getfld (state, name, field, sizeof(field), in)) {
+    for (;;) {
+       int fieldsz = sizeof field;
+       switch (state = m_getfld (&gstate, name, field, &fieldsz, in)) {
            case FLD: 
-           case FLDEOF: 
            case FLDPLUS: 
                /*
                 * Check if the value of field contains anything
@@ -271,8 +219,8 @@ main (int argc, char **argv)
                    printf ("%s:%s", name, field);
                    fprintf (out, "%s:%s", name, field);
                    while (state == FLDPLUS) {
-                       state =
-                           m_getfld (state, name, field, sizeof(field), in);
+                       fieldsz = sizeof field;
+                       state = m_getfld (&gstate, name, field, &fieldsz, in);
                        printf ("%s", field);
                        fprintf (out, "%s", field);
                    }
@@ -284,15 +232,7 @@ main (int argc, char **argv)
                    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);
@@ -310,17 +250,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;
@@ -346,13 +278,14 @@ abort:
                        if (!rapid && !sigint)
                            printf ("%s", field);
                    } while (state == BODY &&
-                           (state = m_getfld (state, name, field, sizeof(field), in)));
+                           (fieldsz = sizeof field,
+                            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));
@@ -369,6 +302,7 @@ no_body:
        }
        break;
     }
+    m_getfld_state_destroy (&gstate);
 
     if (body)
        printf ("--------\n");
@@ -379,15 +313,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)
@@ -454,12 +380,10 @@ getln (char *buffer, int n)
 }
 
 
-static RETSIGTYPE
+static void
 intrser (int i)
 {
-#ifndef        RELIABLE_SIGNALS
-    SIGNAL (SIGINT, intrser);
-#endif
+    NMH_UNUSED (i);
 
     if (wtuser)
        longjmp (sigenv, NOTOK);