xref: /illumos-gate/usr/src/cmd/tbl/te.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
1 /*
2  * Copyright 2005 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  /* te.c: error message control, input line count */
16 #include "t..c"
17 #include <locale.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include <string.h>
21 
22 void
23 error(char *s)
24 {
25 	(void) fprintf(stderr, gettext("\n%s: line %d: %s\n"), ifile, iline, s);
26 	(void) fprintf(stderr, gettext("tbl quits\n"));
27 	exit(1);
28 }
29 
30 char *
31 gets1(char *s, int len)
32 {
33 char *p;
34 int nbl;
35 while(len > 0)
36 	{
37 	iline++;
38 	while ((p = fgets(s,len,tabin))==0)
39 		{
40 		if (swapin()==0)
41 			return((char *)0);
42 		}
43 
44 	while (*s) s++;
45 	s--;
46 	if (*s == '\n') {
47 		*s-- = '\0';
48 	} else {
49 		if (!feof(tabin)) {
50 			if (ferror(tabin))
51 				error(strerror(errno));
52 			else
53 				error(gettext("Line too long"));
54 		}
55 	}
56 
57 	for(nbl=0; *s == '\\' && s>p; s--)
58 		nbl++;
59 	if (linstart && nbl % 2) /* fold escaped nl if in table */
60 		{
61 		s++;
62 		len -= s - p;
63 		continue;
64 		}
65 	break;
66 	}
67 
68 return(p);
69 }
70 
71 # define BACKMAX 500
72 
73 char backup[BACKMAX];
74 char *backp = backup;
75 
76 void
77 un1getc(int c)
78 {
79 if (c=='\n')
80 	iline--;
81 *backp++ = c;
82 if (backp >= backup+BACKMAX)
83 	error(gettext("too much backup"));
84 }
85 
86 int
87 get1char(void)
88 {
89 int c;
90 if (backp>backup)
91 	c = *--backp;
92 else
93 	c=getc(tabin);
94 if (c== EOF) /* EOF */
95 	{
96 	if (swapin() ==0)
97 		error(gettext("unexpected EOF"));
98 	c = getc(tabin);
99 	}
100 if (c== '\n')
101 	iline++;
102 return(c);
103 }
104