File: prompter.c

package info (click to toggle)
nmh 1.8-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,860 kB
  • sloc: ansic: 50,445; sh: 22,697; makefile: 1,138; lex: 740; perl: 509; yacc: 265
file content (417 lines) | stat: -rw-r--r-- 8,657 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* prompter.c -- simple prompting editor front-end
 *
 * This code is Copyright (c) 2002, by the authors of nmh.  See the
 * COPYRIGHT file in the root directory of the nmh distribution for
 * complete copyright information.
 */

#include "h/mh.h"
#include "sbr/m_gmprot.h"
#include "sbr/m_getfld.h"
#include "sbr/getarguments.h"
#include "sbr/smatch.h"
#include "sbr/cpydata.h"
#include "sbr/m_atoi.h"
#include "sbr/context_save.h"
#include "sbr/ambigsw.h"
#include "sbr/print_version.h"
#include "sbr/print_help.h"
#include "sbr/error.h"
#include <fcntl.h>
#include <signal.h>
#include "sbr/signals.h"
#include "sbr/done.h"
#include "sbr/utils.h"
#include "sbr/m_mktemp.h"
#include <setjmp.h>

#include <termios.h>
#include "sbr/globals.h"

#define	QUOTE '\\'

#define PROMPTER_SWITCHES \
    X("erase chr", 0, ERASESW) \
    X("kill chr", 0, KILLSW) \
    X("prepend", 0, PREPSW) \
    X("noprepend", 0, NPREPSW) \
    X("rapid", 0, RAPDSW) \
    X("norapid", 0, NRAPDSW) \
    X("body", -4, BODYSW) \
    X("nobody", -6, NBODYSW) \
    X("doteof", 0, DOTSW) \
    X("nodoteof", 0, NDOTSW) \
    X("version", 0, VERSIONSW) \
    X("help", 0, HELPSW) \

#define X(sw, minchars, id) id,
DEFINE_SWITCH_ENUM(PROMPTER);
#undef X

#define X(sw, minchars, id) { sw, minchars, id },
DEFINE_SWITCH_ARRAY(PROMPTER, switches);
#undef X

static struct termios tio;

static bool wtuser;
static bool sigint;
static jmp_buf sigenv;

/*
 * prototypes
 */
static int getln (char *, int);
static int chrcnv (char *);
static void chrdsp (char *, char);
static void intrser (int);


int
main (int argc, char **argv)
{
    bool body = true;
    bool prepend = true;
    bool rapid = false;
    bool doteof = false;
    int fdi, fdo, i, state;
    char *cp, *drft = NULL, *erasep = NULL;
    char *killp = NULL, name[NAMESZ], field[NMH_BUFSIZ];
    char buffer[BUFSIZ];
    char **arguments, **argp;
    FILE *in, *out;
    char *tmpfil;
    m_getfld_state_t gstate;

    if (nmh_init(argv[0], true, false)) { return 1; }

    arguments = getarguments (invo_name, argc, argv, 1);
    argp = arguments;

    while ((cp = *argp++))
	if (*cp == '-') {
	    switch (smatch (++cp, switches)) {
		case AMBIGSW: 
		    ambigsw (cp, switches);
		    done (1);
		case UNKWNSW: 
		    die("-%s unknown", cp);

		case HELPSW: 
		    snprintf (buffer, sizeof(buffer), "%s [switches] file",
			invo_name);
		    print_help (buffer, switches, 1);
		    done (0);
		case VERSIONSW:
		    print_version(invo_name);
		    done (0);

		case ERASESW: 
		    if (!(erasep = *argp++) || *erasep == '-')
			die("missing argument to %s", argp[-2]);
		    continue;
		case KILLSW: 
		    if (!(killp = *argp++) || *killp == '-')
			die("missing argument to %s", argp[-2]);
		    continue;

		case PREPSW: 
		    prepend = true;
		    continue;
		case NPREPSW: 
		    prepend = false;
		    continue;

		case RAPDSW: 
		    rapid = true;
		    continue;
		case NRAPDSW: 
		    rapid = false;
		    continue;

		case BODYSW: 
		    body = true;
		    continue;
		case NBODYSW: 
		    body = false;
		    continue;

		case DOTSW: 
		    doteof = true;
		    continue;
		case NDOTSW: 
		    doteof = false;
		    continue;
	    }
	} else {
	    if (!drft)
		drft = cp;
	}

    if (!drft)
	die("usage: %s [switches] file", invo_name);
    if ((in = fopen (drft, "r")) == NULL)
	adios (drft, "unable to open");

    if ((tmpfil = m_mktemp2(NULL, invo_name, NULL, &out)) == NULL) {
	die("unable to create temporary file in %s", get_temp_dir());
    }

    /*
     * Are we changing the kill or erase character?
     */
    if (killp || erasep) {
	cc_t save_erase, save_kill;

	/* get the current terminal attributes */
	tcgetattr(0, &tio);

	/* save original kill, erase character for later */
	save_kill = tio.c_cc[VKILL];
	save_erase = tio.c_cc[VERASE];

	/* set new kill, erase character in terminal structure */
	tio.c_cc[VKILL] = killp ? chrcnv (killp) : save_kill;
	tio.c_cc[VERASE] = erasep ? chrcnv (erasep) : save_erase;

	/* set the new terminal attributes */
	 tcsetattr(0, TCSADRAIN, &tio);

	/* print out new kill erase characters */
	chrdsp ("erase", tio.c_cc[VERASE]);
	chrdsp (", kill", tio.c_cc[VKILL]);
	chrdsp (", intr", tio.c_cc[VINTR]);
	putchar ('\n');
	fflush (stdout);

	/*
	 * We set the kill and erase character back to original
	 * setup in terminal structure so we can easily
	 * restore it upon exit.
	 */
	tio.c_cc[VKILL] = save_kill;
	tio.c_cc[VERASE] = save_erase;
    }

    sigint = false;
    SIGNAL2 (SIGINT, intrser);

    /*
     * Loop through the lines of the draft skeleton.
     */
    gstate = m_getfld_state_init(in);
    for (;;) {
	int fieldsz = sizeof field;
	switch (state = m_getfld2(&gstate, name, field, &fieldsz)) {
	    case FLD: 
	    case FLDPLUS: 
		/*
		 * Check if the value of field contains anything
		 * other than space or tab.
		 */
		for (cp = field; *cp; cp++)
		    if (*cp != ' ' && *cp != '\t')
			break;

		/* If so, just add header line to draft */
		if (*cp++ != '\n' || *cp != 0) {
		    printf ("%s:%s", name, field);
		    fprintf (out, "%s:%s", name, field);
		    while (state == FLDPLUS) {
			fieldsz = sizeof field;
			state = m_getfld2(&gstate, name, field, &fieldsz);
			fputs(field, stdout);
			fputs(field, out);
		    }
		} else {
		    /* Else, get value of header field */
		    printf ("%s: ", name);
		    fflush (stdout);
		    i = getln (field, sizeof(field));
		    if (i == -1) {
abort:
			if (killp || erasep) {
			    tcsetattr(0, TCSADRAIN, &tio);
			}
			(void) m_unlink (tmpfil);
			done (1);
		    }
		    if (i != 0 || (field[0] != '\n' && field[0] != 0)) {
			fprintf (out, "%s:", name);
			do {
			    if (field[0] != ' ' && field[0] != '\t')
				putc (' ', out);
			    fputs(field, out);
			} while (i == 1
				    && (i = getln (field, sizeof(field))) >= 0);
			if (i == -1)
			    goto abort;
		    }
		}

		continue;

	    case BODY: 
	    case FILEEOF: 
	        if (!body)
	            break;
		fprintf (out, "--------\n");
		if (field[0] == 0 || !prepend)
		    puts("--------");
		if (field[0]) {
		    if (prepend && body) {
			puts("\n--------Enter initial text\n");
			fflush (stdout);
			for (;;) {
			    getln (buffer, sizeof(buffer));
			    if (doteof && buffer[0] == '.' && buffer[1] == '\n')
				break;
			    if (buffer[0] == 0)
				break;
			    fputs(buffer, out);
			}
		    }

		    do {
			fputs(field, out);
			if (!rapid && !sigint)
			    fputs(field, stdout);
		    } while (state == BODY &&
			    (fieldsz = sizeof field,
			     state = m_getfld2(&gstate, name, field, &fieldsz)));
		    if (prepend || !body)
			break;
                    puts("\n--------Enter additional text\n");
		}

		fflush (stdout);
		for (;;) {
		    getln (field, sizeof(field));
		    if (doteof && field[0] == '.' && field[1] == '\n')
			break;
		    if (field[0] == 0)
			break;
		    fputs(field, out);
		}
		break;

	    default: 
		die("skeleton is poorly formatted");
	}
	break;
    }
    m_getfld_state_destroy (&gstate);

    if (body)
	puts("--------");

    fflush (stdout);
    fclose (in);
    fclose (out);
    SIGNAL (SIGINT, SIG_IGN);

    if (killp || erasep) {
	 tcsetattr(0, TCSADRAIN, &tio);
    }

    if ((fdi = open (tmpfil, O_RDONLY)) == -1)
	adios (tmpfil, "unable to re-open");
    if ((fdo = creat (drft, m_gmprot ())) == -1)
	adios (drft, "unable to write");
    cpydata (fdi, fdo, tmpfil, drft);
    close (fdi);
    close (fdo);
    (void) m_unlink (tmpfil);

    context_save ();	/* save the context file */
    done (0);
    return 1;
}


static int
getln (char *buffer, int n)
{
    int c;
    char *cp;
    static bool quoting;

    *buffer = 0;

    switch (setjmp (sigenv)) {
	case OK: 
	    wtuser = true;
	    break;

	case DONE: 
	    wtuser = false;
	    return 0;

	default: 
	    wtuser = false;
	    return NOTOK;
    }

    cp = buffer;
    *cp = 0;

    for (;;) {
	switch (c = getchar ()) {
	    case EOF: 
		quoting = false;
		clearerr (stdin);
		longjmp (sigenv, DONE);

	    case '\n': 
		if (quoting) {
		    *(cp - 1) = c;
		    quoting = false;
		    wtuser = false;
		    return 1;
		}
		*cp++ = c;
		*cp = 0;
		wtuser = false;
		return 0;

	    default: 
		if (c == QUOTE) {
		    quoting = true;
		} else {
		    quoting = false;
		}
		if (cp < buffer + n)
		    *cp++ = c;
		*cp = 0;
	}
    }
}


static void
intrser (int i)
{
    NMH_UNUSED (i);

    if (wtuser)
	longjmp (sigenv, NOTOK);
    sigint = true;
}


static int
chrcnv (char *cp)
{
    return *cp != QUOTE ? *cp : m_atoi(++cp);
}


static void
chrdsp (char *s, char c)
{
    printf ("%s ", s);
    if (c < ' ' || c == 0177)
	printf ("^%c", c ^ 0100);
    else
	printf ("%c", c);
}