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>
20 static struct swit switches[] = {
41 volatile sig_atomic_t wtuser = 0;
42 volatile sig_atomic_t sigint = 0;
43 static jmp_buf sigenv;
48 int getln(char *, int);
49 static void intrser(int);
53 main(int argc, char **argv)
55 int qbody = 1, prepend = 1, rapid = 0;
56 int fdi, fdo, i, state;
57 char *cp, *drft = NULL;
58 char name[NAMESZ], field[BUFSIZ];
59 char buffer[BUFSIZ], tmpfil[BUFSIZ];
60 char **arguments, **argp;
64 setlocale(LC_ALL, "");
65 invo_name = mhbasename(argv[0]);
67 /* read user profile/context */
70 arguments = getarguments(invo_name, argc, argv, 1);
73 while ((cp = *argp++)) {
75 switch (smatch(++cp, switches)) {
77 ambigsw(cp, switches);
80 adios(NULL, "-%s unknown", cp);
83 snprintf(buffer, sizeof(buffer),
86 print_help(buffer, switches, 1);
89 print_version(invo_name);
119 adios(NULL, "usage: %s [switches] file", invo_name);
120 if ((in = fopen(drft, "r")) == NULL)
121 adios(drft, "unable to open");
123 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
125 adios("prompter", "unable to create temporary file");
127 strncpy(tmpfil, tfile, sizeof(tmpfil));
130 SIGNAL2(SIGINT, intrser);
133 ** Loop through the lines of the draft skeleton.
135 for (state = FLD;;) {
136 switch (state = m_getfld(state, name, field, sizeof(field),
142 ** Check if the value of field contains
143 ** anything other than space or tab.
145 for (cp = field; *cp; cp++)
146 if (*cp != ' ' && *cp != '\t')
149 /* If so, just add header line to draft */
150 if (*cp++ != '\n' || *cp) {
151 printf("%s:%s", name, field);
152 fprintf(out, "%s:%s", name, field);
153 while (state == FLDPLUS) {
154 state = m_getfld(state, name, field,
157 fprintf(out, "%s", field);
160 /* Else, get value of header field */
161 printf("%s: ", name);
163 i = getln(field, sizeof(field));
167 /* sysexits.h EX_DATAERR */
170 if (i || (field[0]!='\n' && field[0]!='\0')) {
171 fprintf(out, "%s:", name);
173 if (field[0] != ' ' && field[0] != '\t')
175 fprintf(out, "%s", field);
176 } while (i == 1 && (i = getln(field, sizeof(field))) >= 0);
182 if (state == FLDEOF) { /* moby hack */
183 /* draft has no body separator; only headers */
184 fprintf(out, "--------\n");
187 printf("--------\n");
195 fprintf(out, "--------\n");
198 printf("--------\n");
203 printf("--------Enter initial text\n");
206 getln(buffer, sizeof(buffer));
209 fprintf(out, "%s", buffer);
212 printf("--------\n");
217 fprintf(out, "%s", field);
218 if (!rapid && !sigint)
220 } while (state == BODY &&
221 (state = m_getfld(state, name,
222 field, sizeof(field), in)));
224 if (prepend || !qbody)
227 printf("--------Enter additional text\n");
231 getln(field, sizeof(field));
234 fprintf(out, "%s", field);
239 adios(NULL, "skeleton is poorly formatted");
245 printf("--------\n");
250 SIGNAL(SIGINT, SIG_IGN);
252 if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK)
253 adios(tmpfil, "unable to re-open");
254 if ((fdo = creat(drft, m_gmprot())) == NOTOK)
255 adios(drft, "unable to write");
256 cpydata(fdi, fdo, tmpfil, drft);
261 context_save(); /* save the context file */
267 getln(char *buffer, int n)
270 sig_atomic_t psigint;
276 switch (setjmp(sigenv)) {
284 if (sigint == psigint) {
293 switch (c = getchar()) {
296 longjmp(sigenv, DONE);
299 if (cp[-1] == '\\') {