te.c revision b55148877d473978f0b46d593fd6213fa526fcc5
1/*
2 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3 * Use is subject to license terms.
4 */
5
6/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7/*	  All Rights Reserved  	*/
8
9/*
10 * Copyright (c) 1980 Regents of the University of California.
11 * All rights reserved. The Berkeley software License Agreement
12 * specifies the terms and conditions for redistribution.
13 */
14
15#pragma ident	"%Z%%M%	%I%	%E% SMI"
16
17 /* te.c: error message control, input line count */
18# include "t..c"
19# include <locale.h>
20# include <errno.h>
21
22void
23error(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}
35
36char *
37errmsg(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}
50
51char *
52gets1(char *s, int len)
53{
54char *p;
55int nbl;
56while(len > 0)
57	{
58	iline++;
59	while ((p = fgets(s,len,tabin))==0)
60		{
61		if (swapin()==0)
62			return((char *)0);
63		}
64
65	while (*s) s++;
66	s--;
67	if (*s == '\n') *s-- =0;
68	else
69		{
70		if (!feof(tabin))
71			{
72			if (ferror(tabin))
73				error(errmsg(errno));
74			else
75				error(gettext("Line too long"));
76			}
77		}
78	for(nbl=0; *s == '\\' && s>p; s--)
79		nbl++;
80	if (linstart && nbl % 2) /* fold escaped nl if in table */
81		{
82		s++;
83		len -= s - p;
84		continue;
85		}
86	break;
87	}
88
89return(p);
90}
91
92# define BACKMAX 500
93
94char backup[BACKMAX];
95char *backp = backup;
96
97void
98un1getc(int c)
99{
100if (c=='\n')
101	iline--;
102*backp++ = c;
103if (backp >= backup+BACKMAX)
104	error(gettext("too much backup"));
105}
106
107int
108get1char(void)
109{
110int c;
111if (backp>backup)
112	c = *--backp;
113else
114	c=getc(tabin);
115if (c== EOF) /* EOF */
116	{
117	if (swapin() ==0)
118		error(gettext("unexpected EOF"));
119	c = getc(tabin);
120	}
121if (c== '\n')
122	iline++;
123return(c);
124}
125