xref: /onnv-gate/usr/src/cmd/tbl/te.c (revision 634:263e46dc023d)
1381Smuffin /*
2*634Sdp  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3381Smuffin  * Use is subject to license terms.
4381Smuffin  */
5381Smuffin 
60Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
70Sstevel@tonic-gate /*	  All Rights Reserved  	*/
80Sstevel@tonic-gate 
90Sstevel@tonic-gate /*
100Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
110Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
120Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
130Sstevel@tonic-gate  */
140Sstevel@tonic-gate 
150Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
160Sstevel@tonic-gate 
170Sstevel@tonic-gate  /* te.c: error message control, input line count */
18*634Sdp #include "t..c"
19*634Sdp #include <locale.h>
20*634Sdp #include <errno.h>
21*634Sdp #include <unistd.h>
22*634Sdp #include <string.h>
23381Smuffin 
24381Smuffin void
error(char * s)25381Smuffin error(char *s)
260Sstevel@tonic-gate {
27*634Sdp 	(void) fprintf(stderr, gettext("\n%s: line %d: %s\n"), ifile, iline, s);
28*634Sdp 	(void) fprintf(stderr, gettext("tbl quits\n"));
29*634Sdp 	exit(1);
300Sstevel@tonic-gate }
31381Smuffin 
320Sstevel@tonic-gate char *
gets1(char * s,int len)33381Smuffin gets1(char *s, int len)
340Sstevel@tonic-gate {
350Sstevel@tonic-gate char *p;
360Sstevel@tonic-gate int nbl;
370Sstevel@tonic-gate while(len > 0)
380Sstevel@tonic-gate 	{
390Sstevel@tonic-gate 	iline++;
400Sstevel@tonic-gate 	while ((p = fgets(s,len,tabin))==0)
410Sstevel@tonic-gate 		{
420Sstevel@tonic-gate 		if (swapin()==0)
43381Smuffin 			return((char *)0);
440Sstevel@tonic-gate 		}
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	while (*s) s++;
470Sstevel@tonic-gate 	s--;
48*634Sdp 	if (*s == '\n') {
49*634Sdp 		*s-- = '\0';
50*634Sdp 	} else {
51*634Sdp 		if (!feof(tabin)) {
520Sstevel@tonic-gate 			if (ferror(tabin))
53*634Sdp 				error(strerror(errno));
540Sstevel@tonic-gate 			else
550Sstevel@tonic-gate 				error(gettext("Line too long"));
560Sstevel@tonic-gate 		}
57*634Sdp 	}
58*634Sdp 
590Sstevel@tonic-gate 	for(nbl=0; *s == '\\' && s>p; s--)
600Sstevel@tonic-gate 		nbl++;
610Sstevel@tonic-gate 	if (linstart && nbl % 2) /* fold escaped nl if in table */
620Sstevel@tonic-gate 		{
630Sstevel@tonic-gate 		s++;
640Sstevel@tonic-gate 		len -= s - p;
650Sstevel@tonic-gate 		continue;
660Sstevel@tonic-gate 		}
670Sstevel@tonic-gate 	break;
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 
700Sstevel@tonic-gate return(p);
710Sstevel@tonic-gate }
72381Smuffin 
730Sstevel@tonic-gate # define BACKMAX 500
74381Smuffin 
750Sstevel@tonic-gate char backup[BACKMAX];
760Sstevel@tonic-gate char *backp = backup;
77381Smuffin 
78381Smuffin void
un1getc(int c)79381Smuffin un1getc(int c)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate if (c=='\n')
820Sstevel@tonic-gate 	iline--;
830Sstevel@tonic-gate *backp++ = c;
840Sstevel@tonic-gate if (backp >= backup+BACKMAX)
850Sstevel@tonic-gate 	error(gettext("too much backup"));
860Sstevel@tonic-gate }
87381Smuffin 
88381Smuffin int
get1char(void)89381Smuffin get1char(void)
900Sstevel@tonic-gate {
910Sstevel@tonic-gate int c;
920Sstevel@tonic-gate if (backp>backup)
930Sstevel@tonic-gate 	c = *--backp;
940Sstevel@tonic-gate else
950Sstevel@tonic-gate 	c=getc(tabin);
960Sstevel@tonic-gate if (c== EOF) /* EOF */
970Sstevel@tonic-gate 	{
980Sstevel@tonic-gate 	if (swapin() ==0)
990Sstevel@tonic-gate 		error(gettext("unexpected EOF"));
1000Sstevel@tonic-gate 	c = getc(tabin);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate if (c== '\n')
1030Sstevel@tonic-gate 	iline++;
1040Sstevel@tonic-gate return(c);
1050Sstevel@tonic-gate }
106