xref: /titanic_44/usr/src/cmd/tbl/te.c (revision b55148877d473978f0b46d593fd6213fa526fcc5)
1*b5514887Smuffin /*
2*b5514887Smuffin  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
3*b5514887Smuffin  * Use is subject to license terms.
4*b5514887Smuffin  */
5*b5514887Smuffin 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate  /* te.c: error message control, input line count */
187c478bd9Sstevel@tonic-gate # include "t..c"
197c478bd9Sstevel@tonic-gate # include <locale.h>
207c478bd9Sstevel@tonic-gate # include <errno.h>
21*b5514887Smuffin 
22*b5514887Smuffin void
23*b5514887Smuffin error(char *s)
247c478bd9Sstevel@tonic-gate {
257c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("\n%s: line %d: %s\n"), ifile, iline, s);
267c478bd9Sstevel@tonic-gate # ifdef unix
277c478bd9Sstevel@tonic-gate fprintf(stderr, gettext("tbl quits\n"));
287c478bd9Sstevel@tonic-gate exit(1);
297c478bd9Sstevel@tonic-gate # endif
307c478bd9Sstevel@tonic-gate # ifdef gcos
317c478bd9Sstevel@tonic-gate fprintf(stderr, "run terminated due to error condition detected by tbl preprocessor\n");
327c478bd9Sstevel@tonic-gate exit(0);
337c478bd9Sstevel@tonic-gate # endif
347c478bd9Sstevel@tonic-gate }
35*b5514887Smuffin 
367c478bd9Sstevel@tonic-gate char *
37*b5514887Smuffin errmsg(int errnum)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate extern int sys_nerr;
407c478bd9Sstevel@tonic-gate extern char *sys_errlist[];
417c478bd9Sstevel@tonic-gate static char errmsgbuf[18];
427c478bd9Sstevel@tonic-gate if (errnum > sys_nerr)
437c478bd9Sstevel@tonic-gate 	{
447c478bd9Sstevel@tonic-gate 	sprintf(errmsgbuf, "Error %d", errnum);
457c478bd9Sstevel@tonic-gate 	return (errmsgbuf);
467c478bd9Sstevel@tonic-gate 	}
477c478bd9Sstevel@tonic-gate else
487c478bd9Sstevel@tonic-gate 	return (sys_errlist[errnum]);
497c478bd9Sstevel@tonic-gate }
50*b5514887Smuffin 
517c478bd9Sstevel@tonic-gate char *
52*b5514887Smuffin gets1(char *s, int len)
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate char *p;
557c478bd9Sstevel@tonic-gate int nbl;
567c478bd9Sstevel@tonic-gate while(len > 0)
577c478bd9Sstevel@tonic-gate 	{
587c478bd9Sstevel@tonic-gate 	iline++;
597c478bd9Sstevel@tonic-gate 	while ((p = fgets(s,len,tabin))==0)
607c478bd9Sstevel@tonic-gate 		{
617c478bd9Sstevel@tonic-gate 		if (swapin()==0)
62*b5514887Smuffin 			return((char *)0);
637c478bd9Sstevel@tonic-gate 		}
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	while (*s) s++;
667c478bd9Sstevel@tonic-gate 	s--;
677c478bd9Sstevel@tonic-gate 	if (*s == '\n') *s-- =0;
687c478bd9Sstevel@tonic-gate 	else
697c478bd9Sstevel@tonic-gate 		{
707c478bd9Sstevel@tonic-gate 		if (!feof(tabin))
717c478bd9Sstevel@tonic-gate 			{
727c478bd9Sstevel@tonic-gate 			if (ferror(tabin))
737c478bd9Sstevel@tonic-gate 				error(errmsg(errno));
747c478bd9Sstevel@tonic-gate 			else
757c478bd9Sstevel@tonic-gate 				error(gettext("Line too long"));
767c478bd9Sstevel@tonic-gate 			}
777c478bd9Sstevel@tonic-gate 		}
787c478bd9Sstevel@tonic-gate 	for(nbl=0; *s == '\\' && s>p; s--)
797c478bd9Sstevel@tonic-gate 		nbl++;
807c478bd9Sstevel@tonic-gate 	if (linstart && nbl % 2) /* fold escaped nl if in table */
817c478bd9Sstevel@tonic-gate 		{
827c478bd9Sstevel@tonic-gate 		s++;
837c478bd9Sstevel@tonic-gate 		len -= s - p;
847c478bd9Sstevel@tonic-gate 		continue;
857c478bd9Sstevel@tonic-gate 		}
867c478bd9Sstevel@tonic-gate 	break;
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate return(p);
907c478bd9Sstevel@tonic-gate }
91*b5514887Smuffin 
927c478bd9Sstevel@tonic-gate # define BACKMAX 500
93*b5514887Smuffin 
947c478bd9Sstevel@tonic-gate char backup[BACKMAX];
957c478bd9Sstevel@tonic-gate char *backp = backup;
96*b5514887Smuffin 
97*b5514887Smuffin void
98*b5514887Smuffin un1getc(int c)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate if (c=='\n')
1017c478bd9Sstevel@tonic-gate 	iline--;
1027c478bd9Sstevel@tonic-gate *backp++ = c;
1037c478bd9Sstevel@tonic-gate if (backp >= backup+BACKMAX)
1047c478bd9Sstevel@tonic-gate 	error(gettext("too much backup"));
1057c478bd9Sstevel@tonic-gate }
106*b5514887Smuffin 
107*b5514887Smuffin int
108*b5514887Smuffin get1char(void)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate int c;
1117c478bd9Sstevel@tonic-gate if (backp>backup)
1127c478bd9Sstevel@tonic-gate 	c = *--backp;
1137c478bd9Sstevel@tonic-gate else
1147c478bd9Sstevel@tonic-gate 	c=getc(tabin);
1157c478bd9Sstevel@tonic-gate if (c== EOF) /* EOF */
1167c478bd9Sstevel@tonic-gate 	{
1177c478bd9Sstevel@tonic-gate 	if (swapin() ==0)
1187c478bd9Sstevel@tonic-gate 		error(gettext("unexpected EOF"));
1197c478bd9Sstevel@tonic-gate 	c = getc(tabin);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate if (c== '\n')
1227c478bd9Sstevel@tonic-gate 	iline++;
1237c478bd9Sstevel@tonic-gate return(c);
1247c478bd9Sstevel@tonic-gate }
125