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;
60 setlocale(LC_ALL, "");
61 invo_name = mhbasename(argv[0]);
63 /* read user profile/context */
66 arguments = getarguments(invo_name, argc, argv, 1);
69 while ((cp = *argp++)) {
71 switch (smatch(++cp, switches)) {
73 ambigsw(cp, switches);
76 adios(NULL, "-%s unknown", cp);
79 snprintf(buffer, sizeof(buffer),
82 print_help(buffer, switches, 1);
85 print_version(invo_name);
115 adios(NULL, "usage: %s [switches] file", invo_name);
116 if ((in = fopen(drft, "r")) == NULL)
117 adios(drft, "unable to open");
119 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
121 adios("prompter", "unable to create temporary file");
123 strncpy(tmpfil, tfile, sizeof(tmpfil));
126 SIGNAL2(SIGINT, intrser);
129 ** Loop through the lines of the draft skeleton.
131 for (state = FLD;;) {
132 switch (state = m_getfld(state, name, field, sizeof(field),
138 ** Check if the value of field contains
139 ** anything other than space or tab.
141 for (cp = field; *cp; cp++)
142 if (*cp != ' ' && *cp != '\t')
145 /* If so, just add header line to draft */
146 if (*cp++ != '\n' || *cp) {
147 printf("%s:%s", name, field);
148 fprintf(out, "%s:%s", name, field);
149 while (state == FLDPLUS) {
150 state = m_getfld(state, name, field,
153 fprintf(out, "%s", field);
156 /* Else, get value of header field */
157 printf("%s: ", name);
159 i = getln(field, sizeof(field));
163 /* sysexits.h EX_DATAERR */
166 if (i || (field[0]!='\n' && field[0]!='\0')) {
167 fprintf(out, "%s:", name);
169 if (field[0] != ' ' && field[0] != '\t')
171 fprintf(out, "%s", field);
172 } while (i == 1 && (i = getln(field, sizeof(field))) >= 0);
178 if (state == FLDEOF) { /* moby hack */
179 /* draft has no body separator; only headers */
180 fprintf(out, "--------\n");
183 printf("--------\n");
191 fprintf(out, "--------\n");
194 printf("--------\n");
199 printf("--------Enter initial text\n");
202 getln(buffer, sizeof(buffer));
205 fprintf(out, "%s", buffer);
208 printf("--------\n");
213 fprintf(out, "%s", field);
214 if (!rapid && !sigint)
216 } while (state == BODY &&
217 (state = m_getfld(state, name,
218 field, sizeof(field), in)));
220 if (prepend || !qbody)
223 printf("--------Enter additional text\n");
227 getln(field, sizeof(field));
230 fprintf(out, "%s", field);
235 adios(NULL, "skeleton is poorly formatted");
241 printf("--------\n");
246 SIGNAL(SIGINT, SIG_IGN);
248 if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK)
249 adios(tmpfil, "unable to re-open");
250 if ((fdo = creat(drft, m_gmprot())) == NOTOK)
251 adios(drft, "unable to write");
252 cpydata(fdi, fdo, tmpfil, drft);
257 context_save(); /* save the context file */
263 getln(char *buffer, int n)
271 switch (setjmp(sigenv)) {
286 switch (c = getchar()) {
289 longjmp(sigenv, DONE);
292 if (cp[-1] == '\\') {
315 longjmp(sigenv, NOTOK);