xref: /illumos-gate/usr/src/cmd/tbl/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>
22 error(s)
23 	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 char *
36 errmsg(errnum)
37 	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 char *
51 gets1(s, len)
52 	char *s;
53 	int len;
54 {
55 char *p;
56 int nbl;
57 while(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 
90 return(p);
91 }
92 # define BACKMAX 500
93 char backup[BACKMAX];
94 char *backp = backup;
95 un1getc(c)
96 {
97 if (c=='\n')
98 	iline--;
99 *backp++ = c;
100 if (backp >= backup+BACKMAX)
101 	error(gettext("too much backup"));
102 }
103 get1char()
104 {
105 int c;
106 if (backp>backup)
107 	c = *--backp;
108 else
109 	c=getc(tabin);
110 if (c== EOF) /* EOF */
111 	{
112 	if (swapin() ==0)
113 		error(gettext("unexpected EOF"));
114 	c = getc(tabin);
115 	}
116 if (c== '\n')
117 	iline++;
118 return(c);
119 }
120