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>
21 static struct swit switches[] = {
42 volatile sig_atomic_t wtuser = 0;
43 volatile sig_atomic_t sigint = 0;
44 static jmp_buf sigenv;
49 int getln(char *, int);
50 static void intrser(int);
54 main(int argc, char **argv)
56 int qbody = 1, prepend = 1, rapid = 0;
57 int fdi, fdo, i, state;
58 char *cp, *drft = NULL;
59 char name[NAMESZ], field[BUFSIZ];
60 char buffer[BUFSIZ], tmpfil[BUFSIZ];
61 char **arguments, **argp;
65 setlocale(LC_ALL, "");
66 invo_name = mhbasename(argv[0]);
68 /* read user profile/context */
71 arguments = getarguments(invo_name, argc, argv, 1);
74 while ((cp = *argp++)) {
76 switch (smatch(++cp, switches)) {
78 ambigsw(cp, switches);
81 adios(EX_USAGE, NULL, "-%s unknown", cp);
84 snprintf(buffer, sizeof(buffer),
87 print_help(buffer, switches, 1);
88 exit(argc == 2 ? EX_OK : EX_USAGE);
91 print_version(invo_name);
92 exit(argc == 2 ? EX_OK : EX_USAGE);
121 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
123 if ((in = fopen(drft, "r")) == NULL) {
124 adios(EX_IOERR, drft, "unable to open");
127 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
129 adios(EX_CANTCREAT, "prompter", "unable to create temporary file");
132 strncpy(tmpfil, tfile, sizeof(tmpfil));
135 SIGNAL2(SIGINT, intrser);
138 ** Loop through the lines of the draft skeleton.
140 for (state = FLD;;) {
141 switch (state = m_getfld(state, name, field, sizeof(field),
146 ** Check if the value of field contains
147 ** anything other than space or tab.
149 for (cp = field; *cp; cp++)
150 if (*cp != ' ' && *cp != '\t')
153 /* If so, just add header line to draft */
154 if (*cp++ != '\n' || *cp) {
155 printf("%s:%s", name, field);
156 fprintf(out, "%s:%s", name, field);
157 while (state == FLDPLUS) {
158 state = m_getfld(state, name, field,
161 fprintf(out, "%s", field);
164 /* Else, get value of header field */
165 printf("%s: ", name);
167 i = getln(field, sizeof(field));
173 if (i || (field[0]!='\n' && field[0]!='\0')) {
174 fprintf(out, "%s:", name);
176 if (field[0] != ' ' && field[0] != '\t')
178 fprintf(out, "%s", field);
179 } while (i == 1 && (i = getln(field, sizeof(field))) >= 0);
189 fprintf(out, "--------\n");
192 printf("--------\n");
197 printf("--------Enter initial text\n");
200 getln(buffer, sizeof(buffer));
203 fprintf(out, "%s", buffer);
206 printf("--------\n");
211 fprintf(out, "%s", field);
212 if (!rapid && !sigint)
214 } while (state == BODY &&
215 (state = m_getfld(state, name,
216 field, sizeof(field), in)));
218 if (prepend || !qbody)
221 printf("--------Enter additional text\n");
225 getln(field, sizeof(field));
228 fprintf(out, "%s", field);
233 adios(EX_DATAERR, NULL, "skeleton is poorly formatted");
239 printf("--------\n");
244 SIGNAL(SIGINT, SIG_IGN);
246 if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK) {
247 adios(EX_IOERR, tmpfil, "unable to re-open");
249 if ((fdo = creat(drft, m_gmprot())) == NOTOK) {
250 adios(EX_IOERR, drft, "unable to write");
252 cpydata(fdi, fdo, tmpfil, drft);
257 context_save(); /* save the context file */
263 getln(char *buffer, int n)
266 sig_atomic_t psigint = sigint;
272 switch (setjmp(sigenv)) {
279 if (sigint == psigint) {
288 switch (c = getchar()) {
291 longjmp(sigenv, DONE);
294 if (cp[-1] == '\\') {