summaryrefslogtreecommitdiff
path: root/libc/stdio/scanf.c
blob: 2962125b40329baa2d369799ab14ab4edd4129f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdarg.h>

#ifdef L_scanf
#ifdef __STDC__
int scanf(const char *fmt, ...)
#else
int scanf(fmt, va_alist)
__const char *fmt;
va_dcl
#endif
{
	va_list ptr;
	int rv;

	va_start(ptr, fmt);
	rv = vfscanf(stdin, fmt, ptr);
	va_end(ptr);
	return rv;
}
#endif

#ifdef L_sscanf
#ifdef __STDC__
int sscanf(const char *sp, const char *fmt, ...)
#else
int sscanf(sp, fmt, va_alist)
__const char *sp;
__const char *fmt;
va_dcl
#endif
{
	FILE string[1] = {
		{0, (char *) (unsigned) -1, 0, 0, (char *) (unsigned) -1, -1,
		 _IOFBF | __MODE_READ}
	};

	va_list ptr;
	int rv;

	va_start(ptr, fmt);
	string->bufpos = (unsigned char *) ((void *) sp);
	rv = vfscanf(string, fmt, ptr);
	va_end(ptr);
	return rv;
}
#endif

#ifdef L_fscanf
#ifdef __STDC__
int fscanf(FILE * fp, const char *fmt, ...)
#else
int fscanf(fp, fmt, va_alist)
FILE *fp;
__const char *fmt;
va_dcl
#endif
{
	va_list ptr;
	int rv;

	va_start(ptr, fmt);
	rv = vfscanf(fp, fmt, ptr);
	va_end(ptr);
	return rv;
}
#endif

#ifdef L_vscanf
int vscanf(fmt, ap)
__const char *fmt;
va_list ap;
{
	return vfscanf(stdin, fmt, ap);
}
#endif

#ifdef L_vsscanf
int vsscanf(__const char *sp, __const char *fmt, va_list ap)
{
	FILE string[1] = {
		{0, (char *) (unsigned) -1, 0, 0, (char *) (unsigned) -1, -1,
		 _IOFBF | __MODE_READ}
	};

	string->bufpos = (unsigned char *) ((void *) sp);
	return vfscanf(string, fmt, ap);
}
#endif

#ifdef L_vfscanf

#if FLOATS
int _vfscanf_fp_ref = 1;
#else
int _vfscanf_fp_ref = 0;
#endif

/* #define	skip()	do{c=getc(fp); if (c<1) goto done;}while(isspace(c))*/

#define	skip()	while(isspace(c)) { if ((c=getc(fp))<1) goto done; }

#if FLOATS
/* fp scan actions */
#define F_NADA	0				/* just change state */
#define F_SIGN	1				/* set sign */
#define F_ESIGN	2				/* set exponent's sign */
#define F_INT	3				/* adjust integer part */
#define F_FRAC	4				/* adjust fraction part */
#define F_EXP	5				/* adjust exponent part */
#define F_QUIT	6

#define NSTATE	8
#define FS_INIT		0			/* initial state */
#define FS_SIGNED	1			/* saw sign */
#define FS_DIGS		2			/* saw digits, no . */
#define FS_DOT		3			/* saw ., no digits */
#define FS_DD		4			/* saw digits and . */
#define FS_E		5			/* saw 'e' */
#define FS_ESIGN	6			/* saw exp's sign */
#define FS_EDIGS	7			/* saw exp's digits */

#define FC_DIG		0
#define FC_DOT		1
#define FC_E		2
#define FC_SIGN		3

/* given transition,state do what action? */
int fp_do[][NSTATE] = {
	{F_INT, F_INT, F_INT,
	 F_FRAC, F_FRAC,
	 F_EXP, F_EXP, F_EXP},		/* see digit */
	{F_NADA, F_NADA, F_NADA,
	 F_QUIT, F_QUIT, F_QUIT, F_QUIT, F_QUIT},	/* see '.' */
	{F_QUIT, F_QUIT,
	 F_NADA, F_QUIT, F_NADA,
	 F_QUIT, F_QUIT, F_QUIT},	/* see e/E */
	{F_SIGN, F_QUIT, F_QUIT, F_QUIT, F_QUIT,
	 F_ESIGN, F_QUIT, F_QUIT},	/* see sign */
};

/* given transition,state what is new state? */
int fp_ns[][NSTATE] = {
	{FS_DIGS, FS_DIGS, FS_DIGS,
	 FS_DD, FS_DD,
	 FS_EDIGS, FS_EDIGS, FS_EDIGS},	/* see digit */
	{FS_DOT, FS_DOT, FS_DD,
	 },							/* see '.' */
	{0, 0,
	 FS_E, 0, FS_E,
	 },							/* see e/E */
	{FS_SIGNED, 0, 0, 0, 0,
	 FS_ESIGN, 0, 0},			/* see sign */
};

/* which states are valid terminators? */
int fp_sval[NSTATE] = {
	0, 0, 1, 0, 1, 0, 0, 1
};
#endif

int vfscanf(fp, fmt, ap)
register FILE *fp;
register const char *fmt;
va_list ap;

{
	register long n;
	register int c, width, lval, cnt = 0;
	int store, neg, base, wide1, endnull, rngflag, c2;
	register unsigned char *p;
	unsigned char delim[128], digits[17], *q;

#if FLOATS
	long frac, expo;
	int eneg, fraclen, fstate, trans;
	double fx, fp_scan();
#endif

	if (!*fmt)
		return (0);

	c = getc(fp);
	while (c > 0) {
		store = 0;
		if (*fmt == '%') {
			n = 0;
			width = -1;
			wide1 = 1;
			base = 10;
			lval = (sizeof(long) == sizeof(int));

			store = 1;
			endnull = 1;
			neg = -1;

			strcpy(delim, "\011\012\013\014\015 ");
			strcpy(digits, "0123456789ABCDEF");

			if (*++fmt == '*') {
				endnull = store = 0;
				++fmt;
			}

			while (isdigit(*fmt)) {	/* width digit(s) */
				if (width == -1)
					width = 0;
				wide1 = width = (width * 10) + (*fmt - '0');
				++fmt;
			}
			--fmt;
		  fmtnxt:
			++fmt;
			switch (tolower(*fmt)) {	/* tolower() is a MACRO! */
			case '*':
				endnull = store = 0;
				goto fmtnxt;

			case 'l':			/* long data */
				lval = 1;
				goto fmtnxt;
			case 'h':			/* short data */
				lval = 0;
				goto fmtnxt;

			case 'i':			/* any-base numeric */
				base = 0;
				goto numfmt;

			case 'b':			/* unsigned binary */
				base = 2;
				goto numfmt;

			case 'o':			/* unsigned octal */
				base = 8;
				goto numfmt;

			case 'x':			/* unsigned hexadecimal */
				base = 16;
				goto numfmt;

			case 'd':			/* SIGNED decimal */
				neg = 0;
				/* FALL-THRU */

			case 'u':			/* unsigned decimal */
			  numfmt:skip();

				if (isupper(*fmt))
					lval = 1;

				if (!base) {
					base = 10;
					neg = 0;
					if (c == '%') {
						base = 2;
						goto skip1;
					} else if (c == '0') {
						c = getc(fp);
						if (c < 1)
							goto savnum;
						if ((c != 'x')
							&& (c != 'X')) {
							base = 8;
							digits[8] = '\0';
							goto zeroin;
						}
						base = 16;
						goto skip1;
					}
				}

				if ((neg == 0) && (base == 10)
					&& ((neg = (c == '-')) || (c == '+'))) {
				  skip1:
					c = getc(fp);
					if (c < 1)
						goto done;
				}

				digits[base] = '\0';
				p = ((unsigned char *)
					 strchr(digits, toupper(c)));

				if ((!c || !p) && width)
					goto done;

				while (p && width-- && c) {
					n = (n * base) + (p - digits);
					c = getc(fp);
				  zeroin:
					p = ((unsigned char *)
						 strchr(digits, toupper(c)));
				}
			  savnum:
				if (store) {
					if (neg == 1)
						n = -n;
					if (lval)
						*va_arg(ap, long *) = n;

					else
						*va_arg(ap, short *) = n;

					++cnt;
				}
				break;

#if FLOATS
			case 'e':			/* float */
			case 'f':
			case 'g':
				skip();
				fprintf(stderr, "LIBM:SCANF");

				if (isupper(*fmt))
					lval = 1;

				fstate = FS_INIT;
				neg = 0;
				eneg = 0;
				n = 0;
				frac = 0;
				expo = 0;
				fraclen = 0;

				while (c && width--) {
					if (c >= '0' && c <= '9')
						trans = FC_DIG;
					else if (c == '.')
						trans = FC_DOT;
					else if (c == '+' || c == '-')
						trans = FC_SIGN;
					else if (tolower(c) == 'e')
						trans = FC_E;
					else
						goto fdone;

					switch (fp_do[trans][fstate]) {
					case F_SIGN:
						neg = (c == '-');
						break;
					case F_ESIGN:
						eneg = (c == '-');
						break;
					case F_INT:
						n = 10 * n + (c - '0');
						break;
					case F_FRAC:
						frac = 10 * frac + (c - '0');
						fraclen++;
						break;
					case F_EXP:
						expo = 10 * expo + (c - '0');
						break;
					case F_QUIT:
						goto fdone;
					}
					fstate = fp_ns[trans][fstate];
					c = getc(fp);
				}

			  fdone:
				if (!fp_sval[fstate])
					goto done;
				if (store) {
					fx = fp_scan(neg, eneg, n, frac, expo, fraclen);
					if (lval)
						*va_arg(ap, double *) = fx;

					else
						*va_arg(ap, float *) = fx;

					++cnt;
				}
				break;
#else
			case 'e':			/* float */
			case 'f':
			case 'g':
				fprintf(stderr, "LIBC:SCANF");
				exit(-1);
#endif

			case 'c':			/* character data */
				width = wide1;
				lval = endnull = 0;
				delim[0] = '\0';
				goto strproc;

			case '[':			/* string w/ delimiter set */

				/* get delimiters */
				p = delim;

				if (*++fmt == '^') {
					fmt++;
					lval = 0;
				} else
					lval = 1;

				rngflag = 2;
				if ((*fmt == ']') || (*fmt == '-')) {
					*p++ = *fmt++;
					rngflag = 0;
				}

				while (*fmt != ']') {
					if (*fmt == '\0')
						goto done;
					switch (rngflag) {
					case 1:
						c2 = *(p - 2);
						if (c2 <= *fmt) {
							p -= 2;
							while (c2 < *fmt)
								*p++ = c2++;
							rngflag = 2;
							break;
						}
						/* fall thru intentional */

					case 0:
						rngflag = (*fmt == '-');
						break;

					case 2:
						rngflag = 0;
					}

					*p++ = *fmt++;
				}

				*p = '\0';
				goto strproc;

			case 's':			/* string data */
				lval = 0;
				skip();
			  strproc:
				/* process string */
				p = va_arg(ap, unsigned char *);

				/* if the 1st char fails, match fails */
				if (width) {
					q = ((unsigned char *)
						 strchr(delim, c));
					if ((c < 1) || lval == (q == 0)) {
						if (endnull)
							*p = '\0';
						goto done;
					}
				}

				for (;;) {		/* FOREVER */
					if (store)
						*p++ = c;
					if (((c = getc(fp)) < 1) || (--width == 0))
						break;

					q = ((unsigned char *)
						 strchr(delim, c));
					if (lval == (q == 0))
						break;
				}

				if (store) {
					if (endnull)
						*p = '\0';
					++cnt;
				}
				break;

			case '\0':			/* early EOS */
				--fmt;
				/* FALL THRU */

			default:
				goto cmatch;
			}
		} else if (isspace(*fmt)) {	/* skip whitespace */
			skip();
		} else {				/* normal match char */
		  cmatch:
			if (c != *fmt)
				break;
			c = getc(fp);
		}

		if (!*++fmt)
			break;
	}

  done:						/* end of scan */
	if ((c == EOF) && (cnt == 0))
		return (EOF);

	if (c != EOF)
		ungetc(c, fp);
	return (cnt);
}

#endif