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)) {
145 ** Check if the value of field contains
146 ** anything other than space or tab.
148 for (cp = f.value; *cp; cp++) {
149 if (*cp != ' ' && *cp != '\t') {
154 /* If so, just add header line to draft */
155 if (*cp++ != '\n' || *cp) {
156 printf("%s:%s", f.name, f.value);
157 fprintf(out, "%s:%s", f.name, f.value);
159 /* Else, get value of header field */
160 printf("%s: ", f.name);
162 i = getln(buffer, sizeof(buffer));
168 if (i || (buffer[0]!='\n' && buffer[0]!='\0')) {
169 fprintf(out, "%s:", f.name);
171 if (buffer[0] != ' ' && buffer[0] != '\t') {
174 fprintf(out, "%s", buffer);
175 } while (i == 1 && (i = getln(buffer, sizeof(buffer))) >= 0);
185 fprintf(out, "--------\n");
188 printf("--------\n");
193 printf("--------Enter initial text\n");
196 getln(buffer, sizeof(buffer));
200 fprintf(out, "%s", buffer);
203 printf("--------\n");
207 if (state == BODY2) {
209 fprintf(out, "%s", f.value);
210 if (!rapid && !sigint) {
211 printf("%s", f.value);
213 } while ((state = m_getfld2(state, &f, in))
217 if (prepend || !qbody) {
221 printf("--------Enter additional text\n");
225 getln(buffer, sizeof(buffer));
229 fprintf(out, "%s", buffer);
234 adios(EX_DATAERR, NULL, "skeleton is poorly formatted");
240 printf("--------\n");
246 SIGNAL(SIGINT, SIG_IGN);
248 if ((fdi = open(tmpfil, O_RDONLY)) == NOTOK) {
249 adios(EX_IOERR, tmpfil, "unable to re-open");
251 if ((fdo = creat(drft, m_gmprot())) == NOTOK) {
252 adios(EX_IOERR, drft, "unable to write");
254 cpydata(fdi, fdo, tmpfil, drft);
265 getln(char *buffer, int n)
268 sig_atomic_t psigint = sigint;
274 switch (setjmp(sigenv)) {
281 if (sigint == psigint) {
290 switch (c = getchar()) {
293 longjmp(sigenv, DONE);
296 if (cp[-1] == '\\') {