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