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>
16 static struct swit switches[] = {
37 static int wtuser = 0;
38 static int sigint = 0;
39 static jmp_buf sigenv;
44 int getln(char *, int);
45 static void intrser(int);
49 main(int argc, char **argv)
51 int qbody = 1, prepend = 1, rapid = 0;
52 int fdi, fdo, i, state;
53 char *cp, *drft = NULL;
54 char name[NAMESZ], field[BUFSIZ];
55 char buffer[BUFSIZ], tmpfil[BUFSIZ];
56 char **arguments, **argp;
61 setlocale(LC_ALL, "");
63 invo_name = mhbasename(argv[0]);
65 /* read user profile/context */
68 arguments = getarguments(invo_name, argc, argv, 1);
71 while ((cp = *argp++)) {
73 switch (smatch(++cp, switches)) {
75 ambigsw(cp, switches);
78 adios(NULL, "-%s unknown", cp);
81 snprintf(buffer, sizeof(buffer),
84 print_help(buffer, switches, 1);
87 print_version(invo_name);
117 adios(NULL, "usage: %s [switches] file", invo_name);
118 if ((in = fopen(drft, "r")) == NULL)
119 adios(drft, "unable to open");
121 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
123 adios("prompter", "unable to create temporary file");
125 strncpy(tmpfil, tfile, sizeof(tmpfil));
128 SIGNAL2(SIGINT, intrser);
131 ** Loop through the lines of the draft skeleton.
133 for (state = FLD;;) {
134 switch (state = m_getfld(state, name, field, sizeof(field),
140 ** Check if the value of field contains
141 ** anything other than space or tab.
143 for (cp = field; *cp; cp++)
144 if (*cp != ' ' && *cp != '\t')
147 /* If so, just add header line to draft */
148 if (*cp++ != '\n' || *cp) {
149 printf("%s:%s", name, field);
150 fprintf(out, "%s:%s", name, field);
151 while (state == FLDPLUS) {
152 state = m_getfld(state, name, field,
155 fprintf(out, "%s", field);
158 /* Else, get value of header field */
159 printf("%s: ", name);
161 i = getln(field, sizeof(field));
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 */
265 getln(char *buffer, int n)
273 switch (setjmp(sigenv)) {
288 switch (c = getchar()) {
291 longjmp(sigenv, DONE);
294 if (cp[-1] == '\\') {
317 longjmp(sigenv, NOTOK);