xref: /original-bsd/usr.bin/struct/struct/1.form.c (revision c3e32dec)
1 /*-
2  * %sccs.include.proprietary.c%
3  */
4 
5 #ifndef lint
6 static char sccsid[] = "@(#)1.form.c	8.1 (Berkeley) 06/06/93";
7 #endif /* not lint */
8 
9 #include <stdio.h>
10 #include "1.defs.h"
11 #include "def.h"
12 
13 /*
14  * The following are used in <stdio.h> but are defines as constants
15  * in 1.defs.h -- since their values are never used here we simply
16  * discard them.			XXX
17  */
18 #undef _r
19 #undef _p
20 
21 extern int linechar, errflag, debug;
22 extern int (*input)(), (*unput)();
23 
24 
25 
26 uptolow(c)		/*translates upper to lower case */
27 int c;
28 	{
29 	if ('A' <= c && c <= 'Z')
30 		return(c+'a'-'A');
31 	else
32 		return(c);
33 	}
34 
35 rdfree(func)
36 int (*func)();
37 	{
38 	int c;
39 	while ( (c = (*input)()) != '\n')
40 		{
41 		(*func)(c);
42 		}
43 	}
44 
45 rdstand(func)
46 int (*func)();
47 	{
48 	int c;
49 	while ( (c=(*input)()) != '\n')
50 		{
51 		(*func)(c);
52 		}
53 	}
54 
55 labfree(func)			/* labels in freeform input */
56 int (*func)();
57 	{
58 	int c;
59 	int temp[6];
60 	int j;
61 	for (j = 0; j < 5; ++j)
62 		{
63 		while ( (c = (*input)()) == ' ' || c == '\t' );
64 		if (c == '\n')
65 			{
66 			if (j != 0)
67 				{
68 				temp[j] = '\0';
69 				error("label without code - ignored:","","");
70 				}
71 			}
72 		if (c < '0' || c > '9')
73 			{
74 			(*unput)(c);
75 			break;
76 			}
77 		else
78 			{
79 			temp[j] = c;
80 			(*func)(c);
81 			}
82 		}
83 	for ( ; j < 5; ++j)
84 		(*func)(' ');
85 	}
86 
87 labstand(func)			/* labels in standard form input */
88 int (*func)();
89 	{
90 	int c;
91 	int j;
92 
93 	for (j = 0; j < 5; ++j)
94 		{
95 		c = (*input)();
96 		if (c == '\n')
97 			{
98 			error("line shorter than 5 characters","","");
99 			errflag = 1;
100 			(*unput)('\n');
101 			}
102 		if (c == '\t' || c == '\n')
103 			{
104 			for ( ;j<5; ++j)
105 				(*func)(' ');
106 			return;
107 			}
108 		(*func)(c);
109 		}
110 	(*input)();			/* throw away continuation char */
111 	}
112 
113 
114 
115 contfree()			/* identify continuation lines in free-form input */
116 	{
117 	return(nonblchar(_diglet,0));	/* any non-alpha non-digit */
118 	}
119 
120 
121 nonblchar(class,yesno)
122 int class,yesno;
123 	{
124 #define CARDSIZE	121
125 	int temp[CARDSIZE];
126 	int j;
127 	for (j=0; (temp[j]=(*input)()) == ' ' || temp[j] == '\t'; ++j)
128 		if (j>=CARDSIZE-1)
129 			{
130 			temp[CARDSIZE-1] = '\0';
131 			 error ("line unexpectedly long","","");
132 			break;
133 			}
134 	if (temp[j]!=EOF && classmatch(temp[j],class)==yesno)
135 		return(1);
136 	else
137 		{
138 		for ( ; j >= 0; --j)
139 			(*unput)(temp[j]);
140 		return(0);
141 		}
142 	}
143 
144 
145 contstand()			/* continuation lines in standard form input */
146 	{
147 	int temp[6];
148 	int i;
149 
150 	for (i = 0; i < 6; ++i)
151 		{
152 		temp[i] = (*input)();
153 		if (temp[i] == '\t' || temp[i] == '\n' || temp[i] == '\0' || temp[i] == EOF)
154 			{
155 			for ( ;i >= 0; --i)
156 				(*unput)(temp[i]);
157 			return(0);
158 			}
159 		}
160 	if (temp[5] != '0' && temp[5] != ' ')
161 		return(1);
162 	else
163 		{
164 		for ( i = 5 ; i >= 0; --i)
165 			(*unput)(temp[i]);
166 		return(0);
167 		}
168 	}
169 
170 
171 
172 comstand(posafter)			/* standard form comments */
173 int posafter;
174 	{
175 	int c;
176 	c = (*input)();
177 	if (!posafter)
178 		(*unput)(c);
179 	if (c == 'c' || c == '*' || c== '#')
180 		return(1);
181 	else
182 		return(0);
183 	}
184 
185 
186 comfree(posafter)
187 int posafter;
188 	{
189 	return(comstand(posafter));
190 	}
191 int (*rline[])()		= {rdfree,rdstand};
192 int (*comment[])()		= {comfree,comstand};
193 int (*getlabel[])()		= {labfree, labstand};
194 int (*chkcont[])()		= {contfree,contstand};
195 
196 blankline()
197 	{
198 	if ( nonblchar(_nl,1) )		/* first non-blank is nl */
199 		{
200 		(*unput) ('\n');
201 		return(1);
202 		}
203 	else return(0);
204 	}
205 
206 #define maxunbp	80
207 char unbuf[maxunbp+1];
208 int unbp;
209 
210 empseek(linebeg)
211 int linebeg;
212 	{
213 	unbp = 0;
214 	if (fseek(infd,(long)(linebeg+rtnbeg),0) == -1)
215 		faterr("in disk seek","","");
216 	}
217 
218 inchar()
219 	{
220 	if (unbp > 0)
221 		return( unbuf[--unbp] );
222 	else
223 		{
224 		return( uptolow(getc(infd)) );
225 		}
226 	}
227 
228 
229 unchar(c)
230 int c;
231 	{
232 	if (unbp >= maxunbp)
233 		faterr("dec.rat: unbuf size exceeded","","");
234 	if(c!=EOF)unbuf[unbp++] = c;
235 	}
236