1 /****************************************************************
2 Copyright (C) 1997-1998 Lucent Technologies
3 All Rights Reserved
4 
5 Permission to use, copy, modify, and distribute this software and
6 its documentation for any purpose and without fee is hereby
7 granted, provided that the above copyright notice appear in all
8 copies and that both that the copyright notice and this
9 permission notice and warranty disclaimer appear in supporting
10 documentation, and that the name of Lucent or any of its entities
11 not be used in advertising or publicity pertaining to
12 distribution of the software without specific, written prior
13 permission.
14 
15 LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17 IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18 SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20 IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22 THIS SOFTWARE.
23 ****************************************************************/
24 
25 #ifdef KR_headers
26 #include "varargs.h"
27 #else
28 #include "stdarg.h"
29 #endif
30 
31 #include "asl.h"	/* for Long */
32 
33 #ifndef Int
34 #define Int Long
35 #endif
36 
37  static char *
38 #ifdef KR_headers
Ladvance(s,Lp)39 Ladvance(s, Lp)
40  char *s;
41  Long *Lp;
42 #else
43 Ladvance(register char *s, Long *Lp)
44 #endif
45 {
46 	register Long x;
47 	register int c;
48 
49 	int sign;
50 	c = *s++;
51 	if (c == '-') {
52 		sign = 1;
53 		c = *s++;
54 		}
55 	else
56 		sign = 0;
57 	if (c < '0' || c > '9')
58 		return 0;
59 	for(x = c - '0';; s++) {
60 		c = *s;
61 		if (c < '0' || c > '9')
62 			break;
63 		x = 10*x + c - '0';
64 		}
65 	*Lp = sign ? -x : x;
66 	return s;
67 	}
68 
69  static void
70 #ifdef KR_headers
badfmt(R,fmt0)71 badfmt(R, fmt0) EdRead *R; char *fmt0;
72 #else
73 badfmt(EdRead *R, const char *fmt0)
74 #endif
75 {
76 	badread(R);
77 	fprintf(Stderr, "bad format %s\n", fmt0);
78 	mainexit_ASL(1);
79 	}
80 
81  int
82 #ifdef KR_headers
ascanf(va_alist)83 ascanf(va_alist)
84  va_dcl
85 #else
86 ascanf(EdRead *R, const char *fmt, ...)
87 #endif
88 {
89 	Const char *fmt0;
90 	int *ip;
91 	Long L, *Lp;
92 	double x, *xp;
93 	va_list ap;
94 	char *s, *s1;
95 	int rc = 0;
96 
97 #ifdef KR_headers
98 	EdRead *R;
99 	char *fmt;
100 	va_start(ap);
101 	R = va_arg(ap, EdRead*);
102 	fmt = va_arg(ap, char*);
103 #else
104 	va_start(ap, fmt);
105 #endif
106 	s = read_line(R);
107 	if (!s)
108 		return 0;
109 	for(;;) {
110 		fmt0 = fmt;
111 		if (*fmt++ != '%')
112 			break;
113 		while(*s == ' ')
114 			s++;
115 		switch(*fmt++) {
116 			case 'd':
117 				if (!(s = Ladvance(s,&L)))
118 					return rc;
119 				ip = va_arg(ap, int*);
120 				*ip = (int)L;
121 				break;
122 			case 'l':
123 				switch(*fmt++) {
124 					case 'd':
125 						if (!(s = Ladvance(s,&L)))
126 							return rc;
127 						Lp = va_arg(ap, Long*);
128 						*Lp = (int)L;
129 						break;
130 					case 'f':
131 						x = strtod(s, &s);
132 						if (!s)
133 							return rc;
134 						xp = va_arg(ap, double*);
135 						*xp = x;
136 						break;
137 					default:
138 						badfmt(R, fmt0);
139 					}
140 				break;
141 			default:
142 				if (!(fmt = Ladvance((char *)fmt-1, &L))
143 				 || *fmt++ != 's')
144 					badfmt(R, fmt0);
145 				/* %127s */
146 				s1 = va_arg(ap, char*);
147 				while(--L > 0 && (*s1 = *s++))
148 					s1++;
149 				*s1 = 0;
150 			}
151 		rc++;
152 		while(*fmt == ' ')
153 			fmt++;
154 		}
155 	return rc;
156 	}
157 
158  int
159 #ifdef KR_headers
bscanf(va_alist)160 bscanf(va_alist)
161  va_dcl
162 #else
163 bscanf(EdRead *R, const char *fmt, ...)
164 #endif
165 {
166 	Const char *fmt0;
167 	int *ip;
168 	short *shp;
169 	Long L, *Lp;
170 	Int I;
171 	double *xp;
172 	va_list ap;
173 	char *s1;
174 	int len;
175 	FILE *fd;
176 	int rc = 0;
177 
178 #ifdef KR_headers
179 	EdRead *R;
180 	char *fmt;
181 	va_start(ap);
182 	R = va_arg(ap, EdRead*);
183 	fmt = va_arg(ap, char*);
184 #else
185 	va_start(ap, fmt);
186 #endif
187 	fd = R->nl;
188 	R->Line += R->lineinc;
189 	R->lineinc = 1;
190 	for(;;) {
191 		fmt0 = fmt;
192 		if (*fmt++ != '%')
193 			break;
194 		switch(*fmt++) {
195 			case 'd':
196 				ip = va_arg(ap, int*);
197 				if (!fread(&I, sizeof(Int), 1, fd))
198 					return rc;
199 				*ip = (int)I;
200 				if (R->iadjfcn)
201 					(*R->iadjfcn)(ip, sizeof(int));
202 				break;
203 			case 'h':
204 				if (*fmt == 'd')
205 					fmt++;
206 				shp = va_arg(ap, short*);
207 				if (!fread(shp, sizeof(short), 1, fd))
208 					return rc;
209 				if (R->iadjfcn)
210 					(*R->iadjfcn)(shp, sizeof(short));
211 				break;
212 			case 'l':
213 				switch(*fmt++) {
214 					case 'd':
215 						Lp = va_arg(ap, Long*);
216 						if (!fread(Lp, sizeof(Long),
217 								1, fd))
218 							return rc;
219 						if (R->iadjfcn)
220 							(*R->iadjfcn)(Lp,
221 								sizeof(Long));
222 
223 						break;
224 					case 'f':
225 						xp = va_arg(ap, double*);
226 						if (!fread(xp, sizeof(double), 1, fd))
227 							return rc;
228 						if (R->dadjfcn)
229 							(*R->dadjfcn)(xp,
230 								sizeof(double));
231 						break;
232 					default:
233 						badfmt(R, fmt0);
234 					}
235 				break;
236 			default:
237 				if (!(fmt = Ladvance((char *)fmt-1, &L))
238 				 || *fmt++ != 's')
239 					badfmt(R, fmt0);
240 				/* %127s */
241 				s1 = va_arg(ap, char*);
242 				if (!fread(&len, sizeof(int), 1, fd))
243 					return rc;
244 				if (R->iadjfcn)
245 					(*R->iadjfcn)(&len, sizeof(int));
246 				if (len >= L || !fread(s1, len, 1, fd))
247 					return rc;
248 				s1[len] = 0;
249 				break;
250 			}
251 		rc++;
252 		while(*fmt == ' ')
253 			fmt++;
254 		}
255 	return rc;
256 	}
257