summaryrefslogtreecommitdiff
path: root/unproto/tok_io.c
blob: ab1129b8f1d7c7b28406b071dc5d6e73d8cf1696 (plain)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*++
/* NAME
/*	tok_io 3
/* SUMMARY
/*	token I/O
/* PACKAGE
/*	unproto
/* SYNOPSIS
/*	#include "token.h"
/*
/*	struct token *tok_get(skip_flag)
/*	int skip_flag;
/*
/*	void tok_unget(t)
/*	struct token *t;
/*
/*	void tok_flush(t)
/*	struct token *t;
/*
/*	void tok_show(t)
/*	struct token *t;
/*
/*	void put_str(s)
/*	char *s;
/*
/*	void put_ch(c)
/*	int c;
/*
/*	void show_line_control()
/*
/*	char curr_path[];
/*	int curr_line;
/* DESCRIPTION
/*	These functions read from stdin and write to stdout. The
/*	output functions maintain some memory so that two successive
/*	words will always be separated by white space.
/*
/*	The input routines eliminate backslash-newline from the input.
/*
/*	tok_get() reads the next token from standard input. It returns
/*	a null pointer when the end of input is reached. If the skip_flag
/*	argument is nonzero, white space (except newline) will be skipped.
/*
/*	tok_unget() implements a limited amount of token push back.
/*
/*	tok_show() displays the contents of a (possibly composite) token
/*	on the standard output.
/*
/*	tok_flush() displays the contents of a (possibly composite) token
/*	on the standard output and makes it available for re-use.
/*
/*	put_str() writes a null-terminated string to standard output.
/*
/*	put_ch() writes one character to standard output.
/*
/*	show_line_control() displays the line number of the next line
/*	to be written to standard output, in a format suitable for the C
/*	compiler parser phase.
/*
/*	The curr_path[] and curr_line variables contain the input file name and 
/*	line number of the most recently read token.
/* BUGS
/*	The tokenizer is just good enough for the unproto filter.
/*	As a benefit, it is quite fast.
/* AUTHOR(S)
/*	Wietse Venema
/*	Eindhoven University of Technology
/*	Department of Mathematics and Computer Science
/*	Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
/* LAST MODIFICATION
/*	91/11/30 21:10:26
/* VERSION/RELEASE
/*	1.2
/*--*/

static char io_sccsid[] = "@(#) tok_io.c 1.2 91/11/30 21:10:26";

/* C library */

#include <stdio.h>
#include <ctype.h>

extern char *strchr();
extern char *malloc();
extern char *realloc();
extern char *strcpy();

/* Application-specific stuff */

#include "token.h"
#include "vstring.h"
#include "error.h"

/* Stuff to keep track of original source file name and position */

char    curr_path[BUFSIZ];		/* current file name */
int     curr_line = 0;			/* # of last read line */

/* Forward declarations */

static void read_quoted();
static void read_comment();

/* Buffered i/o stuff */

static struct vstring *buf = 0;		/* read-ahead buffer */
static char *bp = "";			/* buffer position */

#ifdef DEBUG
#define	INITBUF	1			/* small initial buffer size */
#else
#define	INITBUF BUFSIZ			/* reasonable initial buffer size */
#endif

#define	input()		(*bp ? *bp++ : next_line())
#define	unput(c)	(*--bp = (c))

#define	TOK_BUFSIZE	5		/* token push-back buffer size */

static struct token *tok_buf[TOK_BUFSIZE];
static int tok_bufpos = 0;

/* Type of last token sent to output, for pretty printing */

static int last_tok = 0;

/* Directives that should be ignored. */

#ifdef IGNORE_DIRECTIVES

static char *ignore_directives[] = {
    IGNORE_DIRECTIVES,
    0,
};

#endif

/* Modified string and ctype stuff. */

#define	STREQUAL(x,y)	(*(x) == *(y) && strcmp((x),(y)) == 0)

#define	ISALNUM(c)	(isalnum(c) || (c) == '_')
#define	ISALPHA(c)	(isalpha(c) || (c) == '_')
#define	ISSPACE(c)	(isspace(c) && c != '\n')
#define	ISDOT(c)	(c == '.')

/* Collect all characters that satisfy one condition */

#define	COLLECT(v,c,cond) { \
				register struct vstring *vs = v; \
				register char *cp = vs->str; \
				*cp++ = c; \
				for (;;) { \
				    if ((c = input()) == 0) { \
					break; \
				    } else if (cond) { \
					if (VS_ADDCH(vs, cp, c) == 0) \
					    error(1, "out of memory"); \
				    } else { \
					unput(c); \
					break; \
				    } \
				} \
				*cp = 0; \
			    }

/* do_control - parse control line, uses tok_get() */

static int do_control()
{
    struct token *t1;
    struct token *t2;
    int     pass_thru = 1;		/* 0 = ignore, 1 = output */

    (void) input();				/* skip the hash */

    if (t1 = tok_get(NO_WSPACE)) {
	switch (t1->tokno) {

	    /*
	     * In case of line number control, the remainder of the line has
	     * the format: linenumber "pathname".
	     */
	case TOK_NUMBER:
	    if (t2 = tok_get(NO_WSPACE)) {
		if (t2->tokno == '"') {
		    curr_line = atoi(t1->vstr->str) - 1;
		    strcpy(curr_path, t2->vstr->str);
		}
		tok_free(t2);
	    }
	    break;

#ifdef IGNORE_DIRECTIVES
	case TOK_WORD:
	    /* Optionally ignore other #directives, such as #pragma. */
	    {
		char  **cpp;
		char   *cp = t1->vstr->str;

		for (cpp = ignore_directives; *cpp; cpp++) {
		    if (STREQUAL(cp, *cpp)) {
			pass_thru = 0;
			break;
		    }
		}
	    }
	    break;
#endif
	}
	tok_free(t1);
    }
    return (pass_thru);
}

/* next_line - read one logical line, handle #control */

static int next_line()
{
    register int c;
    register char *cp;

    /* Allocate buffer upon first entry */

    if (buf == 0)
	buf = vs_alloc(INITBUF);

    for (;;) {
	cp = buf->str;

	/* Account for EOF and line continuations */

	while ((c = getchar()) != EOF) {
	    if (VS_ADDCH(buf, cp, c) == 0)	/* store character */
		error(1, "out of memory");
	    if (c == '\n') {			/* real end of line */
		curr_line++;
		break;
	    } else if (c == '\\') {
		if ((c = getchar()) == EOF) {	/* XXX strip backslash-EOF */
		    break;
		} else if (c == '\n') {		/* strip backslash-newline */
		    curr_line++;
		    put_ch('\n');		/* preserve line count */
		    cp--;			/* un-store backslash */
		} else {
		    ungetc(c, stdin);		/* keep backslash-other */
		}
	    }
	}
	*cp = 0;
	bp = buf->str;

	/* Account for EOF and #control */

	switch (bp[0]) {
	case 0:					/* EOF */
	    return (0);
	case '#':				/* control */
	    if (do_control())
		fputs(buf->str, stdout);	/* pass through */
	    else
		putchar('\n');			/* filter out */
	    break;
	default:				/* non-control */
	    return (input());
	}
    }
}

/* tok_unget - push back one token */

void    tok_unget(t)
register struct token *t;
{
    if (tok_bufpos >= TOK_BUFSIZE)
	error(1, "too much pushback");
    tok_buf[tok_bufpos++] = t;
}

/* tok_get - get next token */

struct token *tok_get(skip_flag)
int     skip_flag;
{
    register struct token *t;
    register int c;
    int     d;

    /* Use push-back token, if any. */

    if (tok_bufpos) {
	t = tok_buf[--tok_bufpos];
	return (t);
    }

    /*
     * Get one from the pool and fill it in. The loop is here in case we
     * should skip white-space tokens, which happens in a minority of all
     * cases.
     */

    t = tok_alloc();

    for (;;) {
	if ((c = input()) == 0) {
	    tok_free(t);
	    return (0);
	} else if (!isascii(c)) {
	    t->vstr->str[0] = c;
	    t->vstr->str[1] = 0;
	    t->tokno = TOK_OTHER;
	    return (t);
	} else if (c == '"' || c == '\'') {
	    read_quoted(t, c);
	    t->tokno = c;
	    return (t);
	} else if (ISALPHA(c)) {
	    COLLECT(t->vstr, c, ISALNUM(c));
	    t->tokno = TOK_WORD;
	    return (t);
	} else if (isdigit(c)) {
	    COLLECT(t->vstr, c, isdigit(c));
	    t->tokno = TOK_NUMBER;
	    return (t);
	} else if (ISSPACE(c)) {
	    COLLECT(t->vstr, c, ISSPACE(c));
	    if (skip_flag)
		continue;
	    t->tokno = TOK_WSPACE;
	    return (t);
	} else if (ISDOT(c)) {
	    COLLECT(t->vstr, c, ISDOT(c));
	    t->tokno = TOK_OTHER;
	    return (t);
	} else {
	    t->vstr->str[0] = c;
	    if (c == '/') {
		if ((d = input()) == '*') {
		    t->vstr->str[1] = d;	/* comment */
		    read_comment(t->vstr);
		    if (skip_flag)
			continue;
		    t->tokno = TOK_WSPACE;
		    return (t);
		} else {
		    unput(d);
		}
	    }
	    t->vstr->str[1] = 0;
	    t->tokno = c;
	    return (t);
	}
    }
}

/* read_qouted - read string or character literal */

static void read_quoted(t, ch)
register struct token *t;
int     ch;
{
    register char *cp = t->vstr->str;
    register int c;

    *cp++ = ch;

    while (c = input()) {
	if (c == '\n') {			/* newline in string */
	    unput(c);
	    break;
	}
	if (VS_ADDCH(t->vstr, cp, c) == 0)	/* store character */
	    error(1, "out of memory");
	if (c == ch)				/* end of string */
	    break;
	if (c == '\\')				/* eat next character */
	    if ((c = input()) != 0 && VS_ADDCH(t->vstr, cp, c) == 0)
		error(1, "out of memory");
    }
    *cp = 0;
    return;
}

/* read_comment - stuff a whole comment into one huge token */

static void read_comment(vs)
register struct vstring *vs;
{
    register char *cp = vs->str + 2;	/* skip slash star */
    register int c;
    register int d;

    while (c = input()) {
	if (VS_ADDCH(vs, cp, c) == 0)
	    error(1, "out of memory");
	if (c == '*') {
	    if ((d = input()) == '/') {
		if (VS_ADDCH(vs, cp, d) == 0)
		    error(1, "out of memory");
		break;
	    } else {
		unput(d);
	    }
	}
    }
    *cp = 0;
}

/* put_str - output a string */

void    put_str(s)
char   *s;
{
    fputs(s, stdout);
    last_tok = s[0];				/* XXX */
#ifdef DEBUG
    fflush(stdout);
#endif
}

/* put_ch - put character */

void    put_ch(c)
int     c;
{
    last_tok = putchar(c);
#ifdef DEBUG
    fflush(stdout);
#endif
}

/* tok_show - output (possibly composite) token */

void    tok_show(t)
struct token *t;
{
    register struct token *p;
    register struct token *s;

    switch (t->tokno) {
    case TOK_LIST:
	for (s = t->head; s; s = s->next) {
	    put_ch(s->tokno);			/* opening paren or ',' */
	    for (p = s->head; p; p = p->next)
		tok_show(p);
	}
	put_ch(')');				/* closing paren */
	break;
    case TOK_WORD:
	if (ISALPHA(last_tok))
	    putchar(' ');
	/* FALLTRHOUGH */
    default:
	fputs(t->vstr->str, stdout);		/* token contents */
	last_tok = t->vstr->str[0];
#ifdef DEBUG
	fflush(stdout);
#endif
	if (t->head)				/* trailing blanks */
            for (p = t->head; p; p = p->next)
                tok_show(p);
    }
}