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),
147 ** Check if the value of field contains
148 ** anything other than space or tab.
150 for (cp = field; *cp; cp++)
151 if (*cp != ' ' && *cp != '\t')
154 /* If so, just add header line to draft */
155 if (*cp++ != '\n' || *cp) {
156 printf("%s:%s", name, field);
157 fprintf(out, "%s:%s", name, field);
158 while (state == FLDPLUS) {
159 state = m_getfld(state, name, field,
162 fprintf(out, "%s", field);
165 /* Else, get value of header field */
166 printf("%s: ", name);
168 i = getln(field, sizeof(field));
174 if (i || (field[0]!='\n' && field[0]!='\0')) {
175 fprintf(out, "%s:", name);
177 if (field[0] != ' ' && field[0] != '\t')
179 fprintf(out, "%s", field);
180 } while (i == 1 && (i = getln(field, sizeof(field))) >= 0);
186 if (state == FLDEOF) { /* moby hack */
187 /* draft has no body separator; only headers */
188 fprintf(out, "--------\n");
191 printf("--------\n");
199 fprintf(out, "--------\n");
202 printf("--------\n");
207 printf("--------Enter initial text\n");
210 getln(buffer, sizeof(buffer));
213 fprintf(out, "%s", buffer);
216 printf("--------\n");
221 fprintf(out, "%s", field);
222 if (!rapid && !sigint)
224 } while (state == BODY &&
225 (state = m_getfld(state, name,
226 field, sizeof(field), in)));
228 if (prepend || !qbody)
231 printf("--------Enter additional text\n");
235 getln(field, sizeof(field));
238 fprintf(out, "%s", field);
243 adios(EX_DATAERR, NULL, "skeleton is poorly formatted");
249 printf("--------\n");
254 SIGNAL(SIGINT, SIG_IGN);
256 if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK) {
257 adios(EX_IOERR, tmpfil, "unable to re-open");
259 if ((fdo = creat(drft, m_gmprot())) == NOTOK) {
260 adios(EX_IOERR, drft, "unable to write");
262 cpydata(fdi, fdo, tmpfil, drft);
267 context_save(); /* save the context file */
273 getln(char *buffer, int n)
276 sig_atomic_t psigint = sigint;
282 switch (setjmp(sigenv)) {
289 if (sigint == psigint) {
298 switch (c = getchar()) {
301 longjmp(sigenv, DONE);
304 if (cp[-1] == '\\') {