te.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
1/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2/*	  All Rights Reserved  	*/
3
4
5/*
6 * Copyright (c) 1980 Regents of the University of California.
7 * All rights reserved. The Berkeley software License Agreement
8 * specifies the terms and conditions for redistribution.
9 */
10
11/*
12 * Copyright 1983-1988,2003 Sun Microsystems, Inc.  All rights reserved.
13 * Use is subject to license terms.
14 */
15
16#pragma ident	"%Z%%M%	%I%	%E% SMI"
17
18 /* te.c: error message control, input line count */
19# include "t..c"
20# include <locale.h>
21# include <errno.h>
22error(s)
23	char *s;
24{
25fprintf(stderr, gettext("\n%s: line %d: %s\n"), ifile, iline, s);
26# ifdef unix
27fprintf(stderr, gettext("tbl quits\n"));
28exit(1);
29# endif
30# ifdef gcos
31fprintf(stderr, "run terminated due to error condition detected by tbl preprocessor\n");
32exit(0);
33# endif
34}
35char *
36errmsg(errnum)
37	int errnum;
38{
39extern int sys_nerr;
40extern char *sys_errlist[];
41static char errmsgbuf[18];
42if (errnum > sys_nerr)
43	{
44	sprintf(errmsgbuf, "Error %d", errnum);
45	return (errmsgbuf);
46	}
47else
48	return (sys_errlist[errnum]);
49}
50char *
51gets1(s, len)
52	char *s;
53	int len;
54{
55char *p;
56int nbl;
57while(len > 0)
58	{
59	iline++;
60	while ((p = fgets(s,len,tabin))==0)
61		{
62		if (swapin()==0)
63			return(0);
64		}
65
66	while (*s) s++;
67	s--;
68	if (*s == '\n') *s-- =0;
69	else
70		{
71		if (!feof(tabin))
72			{
73			if (ferror(tabin))
74				error(errmsg(errno));
75			else
76				error(gettext("Line too long"));
77			}
78		}
79	for(nbl=0; *s == '\\' && s>p; s--)
80		nbl++;
81	if (linstart && nbl % 2) /* fold escaped nl if in table */
82		{
83		s++;
84		len -= s - p;
85		continue;
86		}
87	break;
88	}
89
90return(p);
91}
92# define BACKMAX 500
93char backup[BACKMAX];
94char *backp = backup;
95un1getc(c)
96{
97if (c=='\n')
98	iline--;
99*backp++ = c;
100if (backp >= backup+BACKMAX)
101	error(gettext("too much backup"));
102}
103get1char()
104{
105int c;
106if (backp>backup)
107	c = *--backp;
108else
109	c=getc(tabin);
110if (c== EOF) /* EOF */
111	{
112	if (swapin() ==0)
113		error(gettext("unexpected EOF"));
114	c = getc(tabin);
115	}
116if (c== '\n')
117	iline++;
118return(c);
119}
120