1b5514887Smuffin /*
2*ace1a5f1Sdp * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3b5514887Smuffin * Use is subject to license terms.
4b5514887Smuffin */
5b5514887Smuffin
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*ace1a5f1Sdp #include <unistd.h>
22*ace1a5f1Sdp #include <string.h>
23b5514887Smuffin
24b5514887Smuffin void
error(char * s)25b5514887Smuffin error(char *s)
267c478bd9Sstevel@tonic-gate {
27*ace1a5f1Sdp (void) fprintf(stderr, gettext("\n%s: line %d: %s\n"), ifile, iline, s);
28*ace1a5f1Sdp (void) fprintf(stderr, gettext("tbl quits\n"));
297c478bd9Sstevel@tonic-gate exit(1);
307c478bd9Sstevel@tonic-gate }
31b5514887Smuffin
327c478bd9Sstevel@tonic-gate char *
gets1(char * s,int len)33b5514887Smuffin gets1(char *s, int len)
347c478bd9Sstevel@tonic-gate {
357c478bd9Sstevel@tonic-gate char *p;
367c478bd9Sstevel@tonic-gate int nbl;
377c478bd9Sstevel@tonic-gate while(len > 0)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate iline++;
407c478bd9Sstevel@tonic-gate while ((p = fgets(s,len,tabin))==0)
417c478bd9Sstevel@tonic-gate {
427c478bd9Sstevel@tonic-gate if (swapin()==0)
43b5514887Smuffin return((char *)0);
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate while (*s) s++;
477c478bd9Sstevel@tonic-gate s--;
48*ace1a5f1Sdp if (*s == '\n') {
49*ace1a5f1Sdp *s-- = '\0';
50*ace1a5f1Sdp } else {
51*ace1a5f1Sdp if (!feof(tabin)) {
527c478bd9Sstevel@tonic-gate if (ferror(tabin))
53*ace1a5f1Sdp error(strerror(errno));
547c478bd9Sstevel@tonic-gate else
557c478bd9Sstevel@tonic-gate error(gettext("Line too long"));
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate }
58*ace1a5f1Sdp
597c478bd9Sstevel@tonic-gate for(nbl=0; *s == '\\' && s>p; s--)
607c478bd9Sstevel@tonic-gate nbl++;
617c478bd9Sstevel@tonic-gate if (linstart && nbl % 2) /* fold escaped nl if in table */
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate s++;
647c478bd9Sstevel@tonic-gate len -= s - p;
657c478bd9Sstevel@tonic-gate continue;
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate break;
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate return(p);
717c478bd9Sstevel@tonic-gate }
72b5514887Smuffin
737c478bd9Sstevel@tonic-gate # define BACKMAX 500
74b5514887Smuffin
757c478bd9Sstevel@tonic-gate char backup[BACKMAX];
767c478bd9Sstevel@tonic-gate char *backp = backup;
77b5514887Smuffin
78b5514887Smuffin void
un1getc(int c)79b5514887Smuffin un1getc(int c)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate if (c=='\n')
827c478bd9Sstevel@tonic-gate iline--;
837c478bd9Sstevel@tonic-gate *backp++ = c;
847c478bd9Sstevel@tonic-gate if (backp >= backup+BACKMAX)
857c478bd9Sstevel@tonic-gate error(gettext("too much backup"));
867c478bd9Sstevel@tonic-gate }
87b5514887Smuffin
88b5514887Smuffin int
get1char(void)89b5514887Smuffin get1char(void)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate int c;
927c478bd9Sstevel@tonic-gate if (backp>backup)
937c478bd9Sstevel@tonic-gate c = *--backp;
947c478bd9Sstevel@tonic-gate else
957c478bd9Sstevel@tonic-gate c=getc(tabin);
967c478bd9Sstevel@tonic-gate if (c== EOF) /* EOF */
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate if (swapin() ==0)
997c478bd9Sstevel@tonic-gate error(gettext("unexpected EOF"));
1007c478bd9Sstevel@tonic-gate c = getc(tabin);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate if (c== '\n')
1037c478bd9Sstevel@tonic-gate iline++;
1047c478bd9Sstevel@tonic-gate return(c);
1057c478bd9Sstevel@tonic-gate }
106