-
-/*
- * prompter.c -- simple prompting editor front-end
+/* prompter.c -- simple prompting editor front-end
*
* 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/mh.h"
+#include "sbr/m_gmprot.h"
+#include "sbr/m_getfld.h"
+#include "sbr/getarguments.h"
+#include "sbr/smatch.h"
+#include "sbr/cpydata.h"
+#include "sbr/m_atoi.h"
+#include "sbr/context_save.h"
+#include "sbr/ambigsw.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/error.h"
#include <fcntl.h>
-#include <h/signals.h>
+#include "h/signals.h"
+#include "h/done.h"
+#include "h/utils.h"
+#include "sbr/m_mktemp.h"
#include <setjmp.h>
#include <termios.h>
#define QUOTE '\\'
-#ifndef CKILL
-# define CKILL '@'
-#endif
-
-#ifndef CERASE
-# define CERASE '#'
-#endif
-
#define PROMPTER_SWITCHES \
X("erase chr", 0, ERASESW) \
X("kill chr", 0, KILLSW) \
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]
-static int wtuser = 0;
-static int sigint = 0;
+static bool wtuser;
+static bool sigint;
static jmp_buf sigenv;
/*
* prototypes
*/
-int getln (char *, int);
+static 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;
+ bool body = true;
+ bool prepend = true;
+ bool rapid = false;
+ bool doteof = false;
+ int fdi, fdo, i, state;
char *cp, *drft = NULL, *erasep = NULL;
- char *killp = NULL, name[NAMESZ], field[BUFSIZ];
+ char *killp = NULL, name[NAMESZ], field[NMH_BUFSIZ];
char buffer[BUFSIZ];
char **arguments, **argp;
FILE *in, *out;
char *tmpfil;
- m_getfld_state_t gstate = 0;
+ m_getfld_state_t gstate;
- if (nmh_init(argv[0], 1)) { return 1; }
+ if (nmh_init(argv[0], true, false)) { return 1; }
arguments = getarguments (invo_name, argc, argv, 1);
argp = arguments;
ambigsw (cp, switches);
done (1);
case UNKWNSW:
- adios (NULL, "-%s unknown", cp);
+ die("-%s unknown", cp);
case HELPSW:
snprintf (buffer, sizeof(buffer), "%s [switches] file",
case ERASESW:
if (!(erasep = *argp++) || *erasep == '-')
- adios (NULL, "missing argument to %s", argp[-2]);
+ die("missing argument to %s", argp[-2]);
continue;
case KILLSW:
if (!(killp = *argp++) || *killp == '-')
- adios (NULL, "missing argument to %s", argp[-2]);
+ die("missing argument to %s", argp[-2]);
continue;
case PREPSW:
- prepend++;
+ prepend = true;
continue;
case NPREPSW:
- prepend = 0;
+ prepend = false;
continue;
case RAPDSW:
- rapid++;
+ rapid = true;
continue;
case NRAPDSW:
- rapid = 0;
+ rapid = false;
continue;
case BODYSW:
- body++;
+ body = true;
continue;
case NBODYSW:
- body = 0;
+ body = false;
continue;
case DOTSW:
- doteof++;
+ doteof = true;
continue;
case NDOTSW:
- doteof = 0;
+ doteof = false;
continue;
}
} else {
}
if (!drft)
- adios (NULL, "usage: %s [switches] file", invo_name);
+ die("usage: %s [switches] file", invo_name);
if ((in = fopen (drft, "r")) == NULL)
adios (drft, "unable to open");
if ((tmpfil = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) {
- adios(NULL, "unable to create temporary file in %s", get_temp_dir());
+ die("unable to create temporary file in %s", get_temp_dir());
}
/*
tcgetattr(0, &tio);
/* save original kill, erase character for later */
- save_kill = KILL;
- save_erase = ERASE;
+ save_kill = tio.c_cc[VKILL];
+ save_erase = tio.c_cc[VERASE];
/* set new kill, erase character in terminal structure */
- KILL = killp ? chrcnv (killp) : save_kill;
- ERASE = erasep ? chrcnv (erasep) : save_erase;
+ tio.c_cc[VKILL] = killp ? chrcnv (killp) : save_kill;
+ tio.c_cc[VERASE] = 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);
+ chrdsp ("erase", tio.c_cc[VERASE]);
+ chrdsp (", kill", tio.c_cc[VKILL]);
+ chrdsp (", intr", tio.c_cc[VINTR]);
putchar ('\n');
fflush (stdout);
* setup in terminal structure so we can easily
* restore it upon exit.
*/
- KILL = save_kill;
- ERASE = save_erase;
+ tio.c_cc[VKILL] = save_kill;
+ tio.c_cc[VERASE] = save_erase;
}
- sigint = 0;
+ sigint = false;
SIGNAL2 (SIGINT, intrser);
/*
* Loop through the lines of the draft skeleton.
*/
+ gstate = m_getfld_state_init(in);
for (;;) {
int fieldsz = sizeof field;
- switch (state = m_getfld (&gstate, name, field, &fieldsz, in)) {
+ switch (state = m_getfld2(&gstate, name, field, &fieldsz)) {
case FLD:
case FLDPLUS:
/*
fprintf (out, "%s:%s", name, field);
while (state == FLDPLUS) {
fieldsz = sizeof field;
- state = m_getfld (&gstate, name, field, &fieldsz, in);
- printf ("%s", field);
- fprintf (out, "%s", field);
+ state = m_getfld2(&gstate, name, field, &fieldsz);
+ fputs(field, stdout);
+ fputs(field, out);
}
} else {
/* Else, get value of header field */
do {
if (field[0] != ' ' && field[0] != '\t')
putc (' ', out);
- fprintf (out, "%s", field);
+ fputs(field, out);
} while (i == 1
&& (i = getln (field, sizeof(field))) >= 0);
if (i == -1)
break;
fprintf (out, "--------\n");
if (field[0] == 0 || !prepend)
- printf ("--------\n");
+ puts("--------");
if (field[0]) {
if (prepend && body) {
- printf ("\n--------Enter initial text\n\n");
+ puts("\n--------Enter initial text\n");
fflush (stdout);
for (;;) {
getln (buffer, sizeof(buffer));
break;
if (buffer[0] == 0)
break;
- fprintf (out, "%s", buffer);
+ fputs(buffer, out);
}
}
do {
- fprintf (out, "%s", field);
+ fputs(field, out);
if (!rapid && !sigint)
- printf ("%s", field);
+ fputs(field, stdout);
} while (state == BODY &&
(fieldsz = sizeof field,
- state = m_getfld (&gstate, name, field, &fieldsz, in)));
+ state = m_getfld2(&gstate, name, field, &fieldsz)));
if (prepend || !body)
break;
- else
- printf ("\n--------Enter additional text\n\n");
+ puts("\n--------Enter additional text\n");
}
fflush (stdout);
break;
if (field[0] == 0)
break;
- fprintf (out, "%s", field);
+ fputs(field, out);
}
break;
default:
- adios (NULL, "skeleton is poorly formatted");
+ die("skeleton is poorly formatted");
}
break;
}
m_getfld_state_destroy (&gstate);
if (body)
- printf ("--------\n");
+ puts("--------");
fflush (stdout);
fclose (in);
}
-int
+static int
getln (char *buffer, int n)
{
int c;
char *cp;
- static int quoting = 0;
+ static bool quoting;
*buffer = 0;
switch (setjmp (sigenv)) {
case OK:
- wtuser = 1;
+ wtuser = true;
break;
case DONE:
- wtuser = 0;
+ wtuser = false;
return 0;
default:
- wtuser = 0;
+ wtuser = false;
return NOTOK;
}
for (;;) {
switch (c = getchar ()) {
case EOF:
- quoting = 0;
+ quoting = false;
clearerr (stdin);
longjmp (sigenv, DONE);
case '\n':
if (quoting) {
*(cp - 1) = c;
- quoting = 0;
- wtuser = 0;
+ quoting = false;
+ wtuser = false;
return 1;
}
*cp++ = c;
*cp = 0;
- wtuser = 0;
+ wtuser = false;
return 0;
default:
if (c == QUOTE) {
- quoting = 1;
+ quoting = true;
} else {
- quoting = 0;
+ quoting = false;
}
if (cp < buffer + n)
*cp++ = c;
if (wtuser)
longjmp (sigenv, NOTOK);
- sigint++;
+ sigint = true;
}
static int
chrcnv (char *cp)
{
- return (*cp != QUOTE ? *cp : m_atoi (++cp));
+ return *cp != QUOTE ? *cp : m_atoi(++cp);
}