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[] = {
41 char *version=VERSION;
43 volatile sig_atomic_t wtuser = 0;
44 volatile sig_atomic_t sigint = 0;
45 static jmp_buf sigenv;
50 int getln(char *, int);
51 static void intrser(int);
55 main(int argc, char **argv)
57 int qbody = 1, prepend = 1, rapid = 0;
59 char *cp, *drft = NULL;
61 struct field f = {{0}};
62 char buffer[BUFSIZ], tmpfil[BUFSIZ];
63 char **arguments, **argp;
67 setlocale(LC_ALL, "");
68 invo_name = mhbasename(argv[0]);
70 /* read user profile/context */
73 arguments = getarguments(invo_name, argc, argv, 1);
76 while ((cp = *argp++)) {
78 switch (smatch(++cp, switches)) {
80 ambigsw(cp, switches);
83 adios(EX_USAGE, NULL, "-%s unknown", cp);
86 snprintf(buffer, sizeof(buffer),
89 print_help(buffer, switches, 1);
90 exit(argc == 2 ? EX_OK : EX_USAGE);
93 print_version(invo_name);
94 exit(argc == 2 ? EX_OK : EX_USAGE);
123 adios(EX_USAGE, NULL, "usage: %s [switches] file", invo_name);
125 if ((in = fopen(drft, "r")) == NULL) {
126 adios(EX_IOERR, drft, "unable to open");
129 tfile = m_mktemp2(NULL, invo_name, NULL, &out);
131 adios(EX_CANTCREAT, "prompter", "unable to create temporary file");
134 strncpy(tmpfil, tfile, sizeof(tmpfil));
137 SIGNAL2(SIGINT, intrser);
140 ** Loop through the lines of the draft skeleton.
142 for (state = FLD2;;) {
143 switch (state = m_getfld2(state, &f, in)) {
150 ** Check if the value of field contains
151 ** anything other than space or tab.
153 for (cp = f.value; *cp; cp++) {
154 if (*cp != ' ' && *cp != '\t') {
159 /* If so, just add header line to draft */
160 if (*cp++ != '\n' || *cp) {
161 printf("%s:%s", f.name, f.value);
162 fprintf(out, "%s:%s", f.name, f.value);
164 /* Else, get value of header field */
165 printf("%s: ", f.name);
167 i = getln(buffer, sizeof(buffer));
173 if (i || (buffer[0]!='\n' && buffer[0]!='\0')) {
174 fprintf(out, "%s:", f.name);
176 if (buffer[0] != ' ' && buffer[0] != '\t') {
179 fprintf(out, "%s", buffer);
180 } while (i == 1 && (i = getln(buffer, sizeof(buffer))) >= 0);
190 fprintf(out, "--------\n");
192 if (f.value == NULL) {
193 printf("--------\n");
198 printf("--------Enter initial text\n");
201 getln(buffer, sizeof(buffer));
205 fprintf(out, "%s", buffer);
208 printf("--------\n");
212 if (state == BODY2) {
214 fprintf(out, "%s", f.value);
215 if (!rapid && !sigint) {
216 printf("%s", f.value);
218 } while ((state = m_getfld2(state, &f, in))
220 if (state != FILEEOF2) {
221 adios(EX_IOERR, "m_getfld2", "io error");
225 if (prepend || !qbody) {
229 printf("--------Enter additional text\n");
233 getln(buffer, sizeof(buffer));
237 fprintf(out, "%s", buffer);
242 advise(NULL, "skeleton is poorly formatted");
245 adios(EX_IOERR, "m_getfld2", "io error");
251 printf("--------\n");
257 SIGNAL(SIGINT, SIG_IGN);
259 if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK) {
260 adios(EX_IOERR, tmpfil, "unable to re-open");
262 if ((fdo = creat(drft, m_gmprot())) == NOTOK) {
263 adios(EX_IOERR, drft, "unable to write");
265 cpydata(fdi, fdo, tmpfil, drft);
276 getln(char *buffer, int n)
279 sig_atomic_t psigint = sigint;
285 switch (setjmp(sigenv)) {
292 if (sigint == psigint) {
301 switch (c = getchar()) {
304 longjmp(sigenv, DONE);
307 if (cp[-1] == '\\') {