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>
17 static struct swit switches[] = {
38 volatile sig_atomic_t wtuser = 0;
39 volatile sig_atomic_t sigint = 0;
40 static jmp_buf sigenv;
45 int getln(char *, int);
46 static void intrser(int);
50 main(int argc, char **argv)
52 int qbody = 1, prepend = 1, rapid = 0;
53 int fdi, fdo, i, state;
54 char *cp, *drft = NULL;
55 char name[NAMESZ], field[BUFSIZ];
56 char buffer[BUFSIZ], tmpfil[BUFSIZ];
57 char **arguments, **argp;
61 setlocale(LC_ALL, "");
62 invo_name = mhbasename(argv[0]);
64 /* read user profile/context */
67 arguments = getarguments(invo_name, argc, argv, 1);
70 while ((cp = *argp++)) {
72 switch (smatch(++cp, switches)) {
74 ambigsw(cp, switches);
77 adios(NULL, "-%s unknown", cp);
80 snprintf(buffer, sizeof(buffer),
83 print_help(buffer, switches, 1);
86 print_version(invo_name);
116 adios(NULL, "usage: %s [switches] file", invo_name);
117 if ((in = fopen(drft, "r")) == NULL)
118 adios(drft, "unable to open");
120 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
122 adios("prompter", "unable to create temporary file");
124 strncpy(tmpfil, tfile, sizeof(tmpfil));
127 SIGNAL2(SIGINT, intrser);
130 ** Loop through the lines of the draft skeleton.
132 for (state = FLD;;) {
133 switch (state = m_getfld(state, name, field, sizeof(field),
139 ** Check if the value of field contains
140 ** anything other than space or tab.
142 for (cp = field; *cp; cp++)
143 if (*cp != ' ' && *cp != '\t')
146 /* If so, just add header line to draft */
147 if (*cp++ != '\n' || *cp) {
148 printf("%s:%s", name, field);
149 fprintf(out, "%s:%s", name, field);
150 while (state == FLDPLUS) {
151 state = m_getfld(state, name, field,
154 fprintf(out, "%s", field);
157 /* Else, get value of header field */
158 printf("%s: ", name);
160 i = getln(field, sizeof(field));
164 /* sysexits.h EX_DATAERR */
167 if (i || (field[0]!='\n' && field[0]!='\0')) {
168 fprintf(out, "%s:", name);
170 if (field[0] != ' ' && field[0] != '\t')
172 fprintf(out, "%s", field);
173 } while (i == 1 && (i = getln(field, sizeof(field))) >= 0);
179 if (state == FLDEOF) { /* moby hack */
180 /* draft has no body separator; only headers */
181 fprintf(out, "--------\n");
184 printf("--------\n");
192 fprintf(out, "--------\n");
195 printf("--------\n");
200 printf("--------Enter initial text\n");
203 getln(buffer, sizeof(buffer));
206 fprintf(out, "%s", buffer);
209 printf("--------\n");
214 fprintf(out, "%s", field);
215 if (!rapid && !sigint)
217 } while (state == BODY &&
218 (state = m_getfld(state, name,
219 field, sizeof(field), in)));
221 if (prepend || !qbody)
224 printf("--------Enter additional text\n");
228 getln(field, sizeof(field));
231 fprintf(out, "%s", field);
236 adios(NULL, "skeleton is poorly formatted");
242 printf("--------\n");
247 SIGNAL(SIGINT, SIG_IGN);
249 if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK)
250 adios(tmpfil, "unable to re-open");
251 if ((fdo = creat(drft, m_gmprot())) == NOTOK)
252 adios(drft, "unable to write");
253 cpydata(fdi, fdo, tmpfil, drft);
258 context_save(); /* save the context file */
264 getln(char *buffer, int n)
267 sig_atomic_t psigint;
273 switch (setjmp(sigenv)) {
281 if (sigint == psigint) {
290 switch (c = getchar()) {
293 longjmp(sigenv, DONE);
296 if (cp[-1] == '\\') {