xref: /titanic_51/usr/src/cmd/tbl/te.c (revision 13bb89069ebe3cbce237b2708bda9946a2ff4607)
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 
22 void
23 error(char *s)
24 {
25 fprintf(stderr, gettext("\n%s: line %d: %s\n"), ifile, iline, s);
26 # ifdef unix
27 fprintf(stderr, gettext("tbl quits\n"));
28 exit(1);
29 # endif
30 # ifdef gcos
31 fprintf(stderr, "run terminated due to error condition detected by tbl preprocessor\n");
32 exit(0);
33 # endif
34 }
35 
36 char *
37 errmsg(int errnum)
38 {
39 extern int sys_nerr;
40 extern char *sys_errlist[];
41 static char errmsgbuf[18];
42 if (errnum > sys_nerr)
43 	{
44 	sprintf(errmsgbuf, "Error %d", errnum);
45 	return (errmsgbuf);
46 	}
47 else
48 	return (sys_errlist[errnum]);
49 }
50 
51 char *
52 gets1(char *s, int len)
53 {
54 char *p;
55 int nbl;
56 while(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 
89 return(p);
90 }
91 
92 # define BACKMAX 500
93 
94 char backup[BACKMAX];
95 char *backp = backup;
96 
97 void
98 un1getc(int c)
99 {
100 if (c=='\n')
101 	iline--;
102 *backp++ = c;
103 if (backp >= backup+BACKMAX)
104 	error(gettext("too much backup"));
105 }
106 
107 int
108 get1char(void)
109 {
110 int c;
111 if (backp>backup)
112 	c = *--backp;
113 else
114 	c=getc(tabin);
115 if (c== EOF) /* EOF */
116 	{
117 	if (swapin() ==0)
118 		error(gettext("unexpected EOF"));
119 	c = getc(tabin);
120 	}
121 if (c== '\n')
122 	iline++;
123 return(c);
124 }
125