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