aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/tbl/te.c
blob: 54df7e82e1b1da992d59ac4237273af4db49dd0c (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
/* te.c: error message control, input line count */
# include "t.h"

void
error(char *s)
{
	fprint(2, "\n%s:%d: %s\n", ifile, iline, s);
	fprint(2, "tbl quits\n");
	exits(s);
}


char	*
gets1(char *s, int size)
{
	char	*p, *ns;
	int	nbl;

	iline++;
	ns = s;
	p = Brdline(tabin, '\n');
	while (p == 0) {
		if (swapin() == 0)
			return(0);
		p = Brdline(tabin, '\n');
	}
	nbl = Blinelen(tabin)-1;
	if(nbl >= size)
		error("input buffer too small");
	p[nbl] = 0;
	strcpy(s, p);
	s += nbl;
	for (nbl = 0; *s == '\\' && s > ns; s--)
		nbl++;
	if (linstart && nbl % 2) /* fold escaped nl if in table */
		gets1(s + 1, size - (s-ns));

	return(p);
}


# define BACKMAX 500
char	backup[BACKMAX];
char	*backp = backup;

void
un1getc(int c)
{
	if (c == '\n')
		iline--;
	*backp++ = c;
	if (backp >= backup + BACKMAX)
		error("too much backup");
}


int
get1char(void)
{
	int	c;
	if (backp > backup)
		c = *--backp;
	else
		c = Bgetc(tabin);
	if (c == 0) /* EOF */ {
		if (swapin() == 0)
			error("unexpected EOF");
		c = Bgetc(tabin);
	}
	if (c == '\n')
		iline++;
	return(c);
}