1 #ifndef lint
2 static char sccsid[] = "@(#)io.c 1.5 (Berkeley) 07/27/93";
3 #endif lint
4
5
6 /*
7 * error message control
8 * input
9 * line count
10 */
11
12 #include "defs.h"
13 #include "ext.h"
14
error(s)15 error(s)
16 char *s;
17 {
18 fprintf(stderr, "\n%s: line %d: %s\n", ifile, iline, s);
19 fprintf(stderr, "tbl quits\n");
20 exit(1);
21 }
22
23 /*
24 * get a line from the input
25 */
26 char *
gets1(s)27 gets1(s)
28 char *s;
29 {
30 char *p;
31 register int nbl = 0;
32
33 next:
34 iline++;
35 p = fgets(s, BUFSIZ, tabin);
36 /*
37 * Undocumented feature: tables can be arbitrarily split in
38 * various files
39 */
40 while(p == NULL){
41 if(swapin() == 0)
42 return(0);
43 p = fgets(s, BUFSIZ, tabin);
44 }
45
46 /*
47 * Clumsy support for .lf request (jna)
48 */
49 if(p[0] == '.' && p[1] == 'l' && p[2] == 'f'){
50 sscanf(p+3, "%d %s", &iline, oldname);
51 printf(".lf %d %s\n", iline, strlen(oldname) ? oldname: ifile);
52 goto next;
53 }
54
55 while(*s)
56
57 s++;
58 s--;
59
60 /*
61 * remove \n fom input
62 */
63 if(*s == '\n')
64 *s-- = 0;
65
66 /*
67 * is last character a \ ?
68 */
69 for(nbl = 0; *s == '\\' && s > p; s--)
70 nbl++;
71
72 /*
73 * Then fold escaped nl if in table
74 */
75 if(linstart && nbl % 2)
76 (void) gets1(s + 1);
77 return(p);
78 }
79
80 #define BACKMAX 2048
81 static char backup[BACKMAX];
82 static char *backp = backup;
83
un1getc(c)84 un1getc(c){
85 if(c == '\n')
86 iline--;
87 *backp++ = c;
88 if(backp >= backup + BACKMAX)
89 error("too much backup");
90 }
91
get1char()92 get1char(){
93 register int c;
94 if(backp > backup)
95 c = *--backp;
96 else
97 c = getc(tabin);
98 if(c == EOF){
99 if(swapin() == 0)
100 error("unexpected EOF");
101 c = getc(tabin);
102 }
103 if(c == '\n')
104 iline++;
105 return(c);
106 }
107
backrest(cp)108 backrest(cp)
109 char *cp;
110 {
111 register char *s;
112 for(s=cp; *s; s++)
113 ;
114 un1getc('\n');
115 while (s>cp)
116 un1getc(*--s);
117 }
118