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;
58 char *cp, *drft = NULL;
60 struct field f = {{0}};
61 char buffer[BUFSIZ], tmpfil[BUFSIZ];
62 char **arguments, **argp;
66 setlocale(LC_ALL, "");
67 invo_name = mhbasename(argv[0]);
69 /* read user profile/context */
72 arguments = getarguments(invo_name, argc, argv, 1);
75 while ((cp = *argp++)) {
77 switch (smatch(++cp, switches)) {
79 ambigsw(cp, switches);
82 adios(EX_USAGE, NULL, "-%s unknown", cp);
85 snprintf(buffer, sizeof(buffer),
88 print_help(buffer, switches, 1);
89 exit(argc == 2 ? EX_OK : EX_USAGE);
92 print_version(invo_name);
93 exit(argc == 2 ? EX_OK : EX_USAGE);
122 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
124 if ((in = fopen(drft, "r")) == NULL) {
125 adios(EX_IOERR, drft, "unable to open");
128 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
130 adios(EX_CANTCREAT, "prompter", "unable to create temporary file");
133 strncpy(tmpfil, tfile, sizeof(tmpfil));
136 SIGNAL2(SIGINT, intrser);
139 ** Loop through the lines of the draft skeleton.
141 for (state = FLD2;;) {
142 switch (state = m_getfld2(state, &f, in)) {
149 ** Check if the value of field contains
150 ** anything other than space or tab.
152 for (cp = f.value; *cp; cp++) {
153 if (*cp != ' ' && *cp != '\t') {
158 /* If so, just add header line to draft */
159 if (*cp++ != '\n' || *cp) {
160 printf("%s:%s", f.name, f.value);
161 fprintf(out, "%s:%s", f.name, f.value);
163 /* Else, get value of header field */
164 printf("%s: ", f.name);
166 i = getln(buffer, sizeof(buffer));
172 if (i || (buffer[0]!='\n' && buffer[0]!='\0')) {
173 fprintf(out, "%s:", f.name);
175 if (buffer[0] != ' ' && buffer[0] != '\t') {
178 fprintf(out, "%s", buffer);
179 } while (i == 1 && (i = getln(buffer, sizeof(buffer))) >= 0);
189 fprintf(out, "--------\n");
191 if (f.value == NULL) {
192 printf("--------\n");
197 printf("--------Enter initial text\n");
200 getln(buffer, sizeof(buffer));
204 fprintf(out, "%s", buffer);
207 printf("--------\n");
211 if (state == BODY2) {
213 fprintf(out, "%s", f.value);
214 if (!rapid && !sigint) {
215 printf("%s", f.value);
217 } while ((state = m_getfld2(state, &f, in))
219 if (state != FILEEOF2) {
220 adios(EX_IOERR, "m_getfld2", "io error");
224 if (prepend || !qbody) {
228 printf("--------Enter additional text\n");
232 getln(buffer, sizeof(buffer));
236 fprintf(out, "%s", buffer);
241 advise(NULL, "skeleton is poorly formatted");
244 adios(EX_IOERR, "m_getfld2", "io error");
250 printf("--------\n");
256 SIGNAL(SIGINT, SIG_IGN);
258 if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK) {
259 adios(EX_IOERR, tmpfil, "unable to re-open");
261 if ((fdo = creat(drft, m_gmprot())) == NOTOK) {
262 adios(EX_IOERR, drft, "unable to write");
264 cpydata(fdi, fdo, tmpfil, drft);
275 getln(char *buffer, int n)
278 sig_atomic_t psigint = sigint;
284 switch (setjmp(sigenv)) {
291 if (sigint == psigint) {
300 switch (c = getchar()) {
303 longjmp(sigenv, DONE);
306 if (cp[-1] == '\\') {