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),
132 "%s [switches] file",
134 print_help(buffer, switches, 1);
137 print_version(invo_name);
141 if (!(erasep = *argp++) || *erasep == '-')
142 adios(NULL, "missing argument to %s", argp[-2]);
145 if (!(killp = *argp++) || *killp == '-')
146 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);
188 adios("prompter", "unable to create temporary file");
190 strncpy(tmpfil, tfile, sizeof(tmpfil));
193 ** Are we changing the kill or erase character?
195 if (killp || erasep) {
196 #ifdef HAVE_TERMIOS_H
197 cc_t save_erase, save_kill;
199 int save_erase, save_kill;
202 /* get the current terminal attributes */
203 #ifdef HAVE_TERMIOS_H
206 # ifdef HAVE_TERMIO_H
207 ioctl(0, TCGETA, &tio);
209 ioctl(0, TIOCGETP, (char *) &tio);
210 ioctl(0, TIOCGETC, (char *) &tc);
214 /* save original kill, erase character for later */
218 /* set new kill, erase character in terminal structure */
219 KILL = killp ? chrcnv(killp) : save_kill;
220 ERASE = erasep ? chrcnv(erasep) : save_erase;
222 /* set the new terminal attributes */
223 #ifdef HAVE_TERMIOS_H
224 tcsetattr(0, TCSADRAIN, &tio);
226 # ifdef HAVE_TERMIO_H
227 ioctl(0, TCSETAW, &tio);
229 ioctl(0, TIOCSETN, (char *) &tio);
233 /* print out new kill erase characters */
234 chrdsp("erase", ERASE);
235 chrdsp(", kill", KILL);
236 chrdsp(", intr", INTR);
241 ** We set the kill and erase character back to original
242 ** setup in terminal structure so we can easily
243 ** restore it upon exit.
250 SIGNAL2(SIGINT, intrser);
253 ** Loop through the lines of the draft skeleton.
255 for (state = FLD;;) {
256 switch (state = m_getfld(state, name, field, sizeof(field),
262 ** Check if the value of field contains
263 ** anything other than space or tab.
265 for (cp = field; *cp; cp++)
266 if (*cp != ' ' && *cp != '\t')
269 /* If so, just add header line to draft */
270 if (*cp++ != '\n' || *cp != 0) {
271 printf("%s:%s", name, field);
272 fprintf(out, "%s:%s", name, field);
273 while (state == FLDPLUS) {
274 state = m_getfld(state, name, field, sizeof(field), in);
276 fprintf(out, "%s", field);
279 /* Else, get value of header field */
280 printf("%s: ", name);
282 i = getln(field, sizeof(field));
285 if (killp || erasep) {
286 #ifdef HAVE_TERMIOS_H
287 tcsetattr(0, TCSADRAIN, &tio);
290 ioctl(0, TCSETA, &tio);
292 ioctl(0, TIOCSETN, (char *) &tio);
299 if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
300 fprintf(out, "%s:", name);
302 if (field[0] != ' ' && field[0] != '\t')
304 fprintf(out, "%s", field);
305 } while (i == 1 && (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)
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);