2 * prompter.c -- simple prompting editor front-end
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
36 static struct swit switches[] = {
66 static struct termios tio;
67 # define ERASE tio.c_cc[VERASE]
68 # define KILL tio.c_cc[VKILL]
69 # define INTR tio.c_cc[VINTR]
72 static struct termio tio;
73 # define ERASE tio.c_cc[VERASE]
74 # define KILL tio.c_cc[VKILL]
75 # define INTR tio.c_cc[VINTR]
77 static struct sgttyb tio;
78 static struct tchars tc;
79 # define ERASE tio.sg_erase
80 # define KILL tio.sg_kill
81 # define INTR tc.t_intrc
85 static int wtuser = 0;
86 static int sigint = 0;
87 static jmp_buf sigenv;
92 int getln (char *, int);
93 static int chrcnv (char *);
94 static void chrdsp (char *, char);
95 static RETSIGTYPE intrser (int);
99 main (int argc, char **argv)
101 int body = 1, prepend = 1, rapid = 0;
102 int doteof = 0, fdi, fdo, i, state;
103 char *cp, *drft = NULL, *erasep = NULL;
104 char *killp = NULL, name[NAMESZ], field[BUFSIZ];
105 char buffer[BUFSIZ], tmpfil[BUFSIZ];
106 char **arguments, **argp;
111 setlocale(LC_ALL, "");
113 invo_name = r1bindex (argv[0], '/');
115 /* read user profile/context */
118 arguments = getarguments (invo_name, argc, argv, 1);
121 while ((cp = *argp++))
123 switch (smatch (++cp, switches)) {
125 ambigsw (cp, switches);
128 adios (NULL, "-%s unknown", cp);
131 snprintf (buffer, sizeof(buffer), "%s [switches] file",
133 print_help (buffer, switches, 1);
136 print_version(invo_name);
140 if (!(erasep = *argp++) || *erasep == '-')
141 adios (NULL, "missing argument to %s", argp[-2]);
144 if (!(killp = *argp++) || *killp == '-')
145 adios (NULL, "missing argument to %s", argp[-2]);
182 adios (NULL, "usage: %s [switches] file", invo_name);
183 if ((in = fopen (drft, "r")) == NULL)
184 adios (drft, "unable to open");
186 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
187 if (tfile == NULL) adios("prompter", "unable to create temporary file");
188 chmod (tmpfil, 0600);
189 strncpy (tmpfil, tfile, sizeof(tmpfil));
192 * Are we changing the kill or erase character?
194 if (killp || erasep) {
195 #ifdef HAVE_TERMIOS_H
196 cc_t save_erase, save_kill;
198 int save_erase, save_kill;
201 /* get the current terminal attributes */
202 #ifdef HAVE_TERMIOS_H
205 # ifdef HAVE_TERMIO_H
206 ioctl(0, TCGETA, &tio);
208 ioctl (0, TIOCGETP, (char *) &tio);
209 ioctl (0, TIOCGETC, (char *) &tc);
213 /* save original kill, erase character for later */
217 /* set new kill, erase character in terminal structure */
218 KILL = killp ? chrcnv (killp) : save_kill;
219 ERASE = erasep ? chrcnv (erasep) : save_erase;
221 /* set the new terminal attributes */
222 #ifdef HAVE_TERMIOS_H
223 tcsetattr(0, TCSADRAIN, &tio);
225 # ifdef HAVE_TERMIO_H
226 ioctl(0, TCSETAW, &tio);
228 ioctl (0, TIOCSETN, (char *) &tio);
232 /* print out new kill erase characters */
233 chrdsp ("erase", ERASE);
234 chrdsp (", kill", KILL);
235 chrdsp (", intr", INTR);
240 * We set the kill and erase character back to original
241 * setup in terminal structure so we can easily
242 * restore it upon exit.
249 SIGNAL2 (SIGINT, intrser);
252 * Loop through the lines of the draft skeleton.
254 for (state = FLD;;) {
255 switch (state = m_getfld (state, name, field, sizeof(field), in)) {
260 * Check if the value of field contains anything
261 * other than space or tab.
263 for (cp = field; *cp; cp++)
264 if (*cp != ' ' && *cp != '\t')
267 /* If so, just add header line to draft */
268 if (*cp++ != '\n' || *cp != 0) {
269 printf ("%s:%s", name, field);
270 fprintf (out, "%s:%s", name, field);
271 while (state == FLDPLUS) {
273 m_getfld (state, name, field, sizeof(field), in);
274 printf ("%s", field);
275 fprintf (out, "%s", field);
278 /* Else, get value of header field */
279 printf ("%s: ", name);
281 i = getln (field, sizeof(field));
284 if (killp || erasep) {
285 #ifdef HAVE_TERMIOS_H
286 tcsetattr(0, TCSADRAIN, &tio);
289 ioctl (0, TCSETA, &tio);
291 ioctl (0, TIOCSETN, (char *) &tio);
298 if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
299 fprintf (out, "%s:", name);
301 if (field[0] != ' ' && field[0] != '\t')
303 fprintf (out, "%s", field);
305 && (i = getln (field, sizeof(field))) >= 0);
311 if (state == FLDEOF) { /* moby hack */
312 fprintf (out, "--------\n");
313 printf ("--------\n");
325 fprintf (out, "--------\n");
326 if (field[0] == 0 || !prepend)
327 printf ("--------\n");
329 if (prepend && body) {
330 printf ("\n--------Enter initial text\n\n");
333 getln (buffer, sizeof(buffer));
334 if (doteof && buffer[0] == '.' && buffer[1] == '\n')
338 fprintf (out, "%s", buffer);
343 fprintf (out, "%s", field);
344 if (!rapid && !sigint)
345 printf ("%s", field);
346 } while (state == BODY &&
347 (state = m_getfld (state, name, field, sizeof(field), in)));
348 if (prepend || !body)
351 printf ("\n--------Enter additional text\n\n");
356 getln (field, sizeof(field));
357 if (doteof && field[0] == '.' && field[1] == '\n')
361 fprintf (out, "%s", field);
366 adios (NULL, "skeleton is poorly formatted");
372 printf ("--------\n");
377 SIGNAL (SIGINT, SIG_IGN);
379 if (killp || erasep) {
380 #ifdef HAVE_TERMIOS_H
381 tcsetattr(0, TCSADRAIN, &tio);
383 # ifdef HAVE_TERMIO_H
384 ioctl (0, TCSETAW, &tio);
386 ioctl (0, TIOCSETN, (char *) &tio);
391 if ((fdi = open (tmpfil, O_RDONLY)) == NOTOK)
392 adios (tmpfil, "unable to re-open");
393 if ((fdo = creat (drft, m_gmprot ())) == NOTOK)
394 adios (drft, "unable to write");
395 cpydata (fdi, fdo, tmpfil, drft);
400 context_save (); /* save the context file */
407 getln (char *buffer, int n)
415 switch (setjmp (sigenv)) {
430 switch (c = getchar ()) {
433 longjmp (sigenv, DONE);
436 if (cp[-1] == QUOTE) {
458 #ifndef RELIABLE_SIGNALS
459 SIGNAL (SIGINT, intrser);
463 longjmp (sigenv, NOTOK);
471 return (*cp != QUOTE ? *cp : m_atoi (++cp));
476 chrdsp (char *s, char c)
479 if (c < ' ' || c == 0177)
480 printf ("^%c", c ^ 0100);