xref: /illumos-gate/usr/src/common/util/sscanf.c (revision ae115bc7)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1990, 1993
24  *	The Regents of the University of California.  All rights reserved.
25  *
26  * This code is derived from software contributed to Berkeley by
27  * Chris Torek.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  * 1. Redistributions of source code must retain the above copyright
33  *    notice, this list of conditions and the following disclaimer.
34  * 2. Redistributions in binary form must reproduce the above copyright
35  *    notice, this list of conditions and the following disclaimer in the
36  *    documentation and/or other materials provided with the distribution.
37  * 4. Neither the name of the University nor the names of its contributors
38  *    may be used to endorse or promote products derived from this software
39  *    without specific prior written permission.
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * From: Id: vfscanf.c,v 1.13 1998/09/25 12:20:27 obrien Exp
54  * From: static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
55  * From: static char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
56  */
57 
58 
59 /*
60  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
61  * Use is subject to license terms.
62  */
63 
64 #pragma ident	"%Z%%M%	%I%	%E% SMI"
65 
66 #include <sys/types.h>
67 #include <sys/systm.h>
68 #include <sys/ctype.h>
69 #include <sys/sunddi.h>
70 #include <util/sscanf.h>
71 
72 #define	BUF		32 	/* Maximum length of numeric string. */
73 
74 /*
75  * Flags used during conversion.
76  */
77 #define	LONG		0x01	/* l: long or double */
78 #define	SHORT		0x04	/* h: short */
79 #define	SUPPRESS	0x08	/* suppress assignment */
80 #define	POINTER		0x10	/* weird %p pointer (`fake hex') */
81 #define	NOSKIP		0x20	/* do not skip blanks */
82 
83 /*
84  * The following are used in numeric conversions only:
85  * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
86  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
87  */
88 #define	SIGNOK		0x40	/* +/- is (still) legal */
89 #define	NDIGITS		0x80	/* no digits detected */
90 
91 #define	DPTOK		0x100	/* (float) decimal point is still legal */
92 #define	EXPOK		0x200	/* (float) exponent (e+3, etc) still legal */
93 
94 #define	PFXOK		0x100	/* 0x prefix is (still) legal */
95 #define	NZDIGITS	0x200	/* no zero digits detected */
96 
97 /*
98  * Conversion types.
99  */
100 #define	CT_CHAR		0	/* %c conversion */
101 #define	CT_CCL		1	/* %[...] conversion */
102 #define	CT_STRING	2	/* %s conversion */
103 #define	CT_INT		3	/* integer, i.e., strtoq or strtouq */
104 
105 static const uchar_t *set_ccl(char *, const uchar_t *);
106 
107 #define	isspace(ch)	(((ch) == ' ') || ((ch) == '\r') || ((ch) == '\n') || \
108 			((ch) == '\t') || ((ch) == '\f'))
109 
110 int
111 vsscanf(const char *inp, char const *fmt0, va_list ap)
112 {
113 	int inr;
114 	const uchar_t *fmt = (const uchar_t *)fmt0;
115 	int c;			/* character from format, or conversion */
116 	size_t width;		/* field width, or 0 */
117 	char *p;		/* points into all kinds of strings */
118 	int n;			/* handy integer */
119 	int flags;		/* flags as defined above */
120 	char *p0;		/* saves original value of p when necessary */
121 	int nassigned;		/* number of fields assigned */
122 	int nconversions;	/* number of conversions */
123 	int nread;		/* number of characters consumed from fp */
124 	int base;		/* base argument to strtoq/strtouq */
125 	int sconv;		/* do signed conversion */
126 	char ccltab[256];	/* character class table for %[...] */
127 	char buf[BUF];		/* buffer for numeric conversions */
128 
129 	/* `basefix' is used to avoid `if' tests in the integer scanner */
130 	static short basefix[17] =
131 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
132 
133 	inr = strlen(inp);
134 
135 	sconv = 0;
136 	nassigned = 0;
137 	nconversions = 0;
138 	nread = 0;
139 	base = 0;
140 	for (;;) {
141 		c = *fmt++;
142 		if (c == 0)
143 			return (nassigned);
144 		if (isspace(c)) {
145 			while (inr > 0 && isspace(*inp))
146 				nread++, inr--, inp++;
147 			continue;
148 		}
149 		if (c != '%')
150 			goto literal;
151 		width = 0;
152 		flags = 0;
153 		/*
154 		 * switch on the format.  continue if done;
155 		 * break once format type is derived.
156 		 */
157 again:		c = *fmt++;
158 		switch (c) {
159 		case '%':
160 literal:
161 			if (inr <= 0)
162 				goto input_failure;
163 			if (*inp != c)
164 				goto match_failure;
165 			inr--, inp++;
166 			nread++;
167 			continue;
168 
169 		case '*':
170 			flags |= SUPPRESS;
171 			goto again;
172 		case 'l':
173 			flags |= LONG;
174 			goto again;
175 		case 'h':
176 			flags |= SHORT;
177 			goto again;
178 
179 		case '0': case '1': case '2': case '3': case '4':
180 		case '5': case '6': case '7': case '8': case '9':
181 			width = width * 10 + c - '0';
182 			goto again;
183 
184 		/*
185 		 * Conversions.
186 		 *
187 		 */
188 		case 'd':
189 			c = CT_INT;
190 			sconv = 1;
191 			base = 10;
192 			break;
193 
194 		case 'i':
195 			c = CT_INT;
196 			sconv = 1;
197 			base = 0;
198 			break;
199 
200 		case 'o':
201 			c = CT_INT;
202 			base = 8;
203 			break;
204 
205 		case 'u':
206 			c = CT_INT;
207 			base = 10;
208 			break;
209 
210 		case 'x':
211 			flags |= PFXOK;	/* enable 0x prefixing */
212 			c = CT_INT;
213 			base = 16;
214 			break;
215 
216 		case 's':
217 			c = CT_STRING;
218 			break;
219 
220 		case '[':
221 			fmt = set_ccl(ccltab, fmt);
222 			flags |= NOSKIP;
223 			c = CT_CCL;
224 			break;
225 
226 		case 'c':
227 			flags |= NOSKIP;
228 			c = CT_CHAR;
229 			break;
230 
231 		case 'p':	/* pointer format is like hex */
232 			flags |= POINTER | PFXOK;
233 			c = CT_INT;
234 			base = 16;
235 			break;
236 
237 		case 'n':
238 			nconversions++;
239 			if (flags & SUPPRESS)	/* ??? */
240 				continue;
241 			if (flags & SHORT)
242 				*va_arg(ap, short *) = (short)nread;
243 			else if (flags & LONG)
244 				*va_arg(ap, long *) = (long)nread;
245 			else
246 				*va_arg(ap, int *) = nread;
247 			continue;
248 		}
249 
250 		/*
251 		 * We have a conversion that requires input.
252 		 */
253 		if (inr <= 0)
254 			goto input_failure;
255 
256 		/*
257 		 * Consume leading white space, except for formats
258 		 * that suppress this.
259 		 */
260 		if ((flags & NOSKIP) == 0) {
261 			while (isspace(*inp)) {
262 				nread++;
263 				if (--inr > 0)
264 					inp++;
265 				else
266 					goto input_failure;
267 			}
268 			/*
269 			 * Note that there is at least one character in
270 			 * the buffer, so conversions that do not set NOSKIP
271 			 * can no longer result in an input failure.
272 			 */
273 		}
274 
275 		/*
276 		 * Do the conversion.
277 		 */
278 		switch (c) {
279 
280 		case CT_CHAR:
281 			/* scan arbitrary characters (sets NOSKIP) */
282 			if (width == 0)
283 				width = 1;
284 			if (flags & SUPPRESS) {
285 				size_t sum = 0;
286 
287 				if ((n = inr) < width) {
288 					sum += n;
289 					width -= n;
290 					inp += n;
291 					if (sum == 0)
292 						goto input_failure;
293 				} else {
294 					sum += width;
295 					inr -= width;
296 					inp += width;
297 				}
298 				nread += sum;
299 			} else {
300 				bcopy(inp, va_arg(ap, char *), width);
301 				inr -= width;
302 				inp += width;
303 				nread += width;
304 				nassigned++;
305 			}
306 			nconversions++;
307 			break;
308 
309 		case CT_CCL:
310 			/* scan a (nonempty) character class (sets NOSKIP) */
311 			if (width == 0)
312 				width = (size_t)~0;	/* `infinity' */
313 			/* take only those things in the class */
314 			if (flags & SUPPRESS) {
315 				n = 0;
316 				while (ccltab[(unsigned char)*inp]) {
317 					n++, inr--, inp++;
318 					if (--width == 0)
319 						break;
320 					if (inr <= 0) {
321 						if (n == 0)
322 							goto input_failure;
323 						break;
324 					}
325 				}
326 				if (n == 0)
327 					goto match_failure;
328 			} else {
329 				p0 = p = va_arg(ap, char *);
330 				while (ccltab[(unsigned char)*inp]) {
331 					inr--;
332 					*p++ = *inp++;
333 					if (--width == 0)
334 						break;
335 					if (inr <= 0) {
336 						if (p == p0)
337 							goto input_failure;
338 						break;
339 					}
340 				}
341 				n = p - p0;
342 				if (n == 0)
343 					goto match_failure;
344 				*p = 0;
345 				nassigned++;
346 			}
347 			nread += n;
348 			nconversions++;
349 			break;
350 
351 		case CT_STRING:
352 			/* like CCL, but zero-length string OK, & no NOSKIP */
353 			if (width == 0)
354 				width = (size_t)~0;
355 			if (flags & SUPPRESS) {
356 				n = 0;
357 				while (!isspace(*inp)) {
358 					n++, inr--, inp++;
359 					if (--width == 0)
360 						break;
361 					if (inr <= 0)
362 						break;
363 				}
364 				nread += n;
365 			} else {
366 				p0 = p = va_arg(ap, char *);
367 				while (!isspace(*inp)) {
368 					inr--;
369 					*p++ = *inp++;
370 					if (--width == 0)
371 						break;
372 					if (inr <= 0)
373 						break;
374 				}
375 				*p = 0;
376 				nread += p - p0;
377 				nassigned++;
378 			}
379 			nconversions++;
380 			continue;
381 
382 		case CT_INT:
383 			/* scan an integer as if by strtoq/strtouq */
384 			/* size_t is unsigned, hence this optimisation */
385 			if (--width > sizeof (buf) - 2)
386 				width = sizeof (buf) - 2;
387 			width++;
388 			flags |= SIGNOK | NDIGITS | NZDIGITS;
389 			for (p = buf; width; width--) {
390 				c = *inp;
391 				/*
392 				 * Switch on the character; `goto ok'
393 				 * if we accept it as a part of number.
394 				 */
395 				switch (c) {
396 
397 				/*
398 				 * The digit 0 is always legal, but is
399 				 * special.  For %i conversions, if no
400 				 * digits (zero or nonzero) have been
401 				 * scanned (only signs), we will have
402 				 * base==0.  In that case, we should set
403 				 * it to 8 and enable 0x prefixing.
404 				 * Also, if we have not scanned zero digits
405 				 * before this, do not turn off prefixing
406 				 * (someone else will turn it off if we
407 				 * have scanned any nonzero digits).
408 				 */
409 				case '0':
410 					if (base == 0) {
411 						base = 8;
412 						flags |= PFXOK;
413 					}
414 					if (flags & NZDIGITS)
415 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
416 					else
417 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
418 					goto ok;
419 
420 				/* 1 through 7 always legal */
421 				case '1': case '2': case '3':
422 				case '4': case '5': case '6': case '7':
423 					base = basefix[base];
424 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
425 					goto ok;
426 
427 				/* digits 8 and 9 ok iff decimal or hex */
428 				case '8': case '9':
429 					base = basefix[base];
430 					if (base <= 8)
431 						break;	/* not legal here */
432 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
433 					goto ok;
434 
435 				/* letters ok iff hex */
436 				case 'A': case 'B': case 'C':
437 				case 'D': case 'E': case 'F':
438 				case 'a': case 'b': case 'c':
439 				case 'd': case 'e': case 'f':
440 					/* no need to fix base here */
441 					if (base <= 10)
442 						break;	/* not legal here */
443 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
444 					goto ok;
445 
446 				/* sign ok only as first character */
447 				case '+': case '-':
448 					if (flags & SIGNOK) {
449 						flags &= ~SIGNOK;
450 						goto ok;
451 					}
452 					break;
453 
454 				/* x ok iff flag still set & 2nd char */
455 				case 'x': case 'X':
456 					if (flags & PFXOK && p == buf + 1) {
457 						base = 16;	/* if %i */
458 						flags &= ~PFXOK;
459 						goto ok;
460 					}
461 					break;
462 				}
463 
464 				/*
465 				 * If we got here, c is not a legal character
466 				 * for a number.  Stop accumulating digits.
467 				 */
468 				break;
469 		ok:
470 				/*
471 				 * c is legal: store it and look at the next.
472 				 */
473 				*p++ = c;
474 				if (--inr > 0)
475 					inp++;
476 				else
477 					break;		/* end of input */
478 			}
479 			/*
480 			 * If we had only a sign, it is no good; push
481 			 * back the sign.  If the number ends in `x',
482 			 * it was [sign] '0' 'x', so push back the x
483 			 * and treat it as [sign] '0'.
484 			 */
485 			if (flags & NDIGITS) {
486 				if (p > buf) {
487 					inp--;
488 					inr++;
489 				}
490 				goto match_failure;
491 			}
492 			c = ((uchar_t *)p)[-1];
493 			if (c == 'x' || c == 'X') {
494 				--p;
495 				inp--;
496 				inr++;
497 			}
498 			if ((flags & SUPPRESS) == 0) {
499 				ulong_t res;
500 
501 				*p = 0;
502 				if (sconv)
503 					(void) ddi_strtol(buf, (char **)NULL,
504 					    base, (long *)(&res));
505 				else
506 					(void) ddi_strtoul(buf, (char **)NULL,
507 					    base, &res);
508 				if (flags & POINTER)
509 					*va_arg(ap, void **) =
510 						(void *)(uintptr_t)res;
511 				else if (flags & SHORT)
512 					*va_arg(ap, short *) = (short)res;
513 				else if (flags & LONG)
514 					*va_arg(ap, long *) = (long)res;
515 				else
516 					*va_arg(ap, int *) = (int)res;
517 				nassigned++;
518 			}
519 			nread += p - buf;
520 			nconversions++;
521 			break;
522 
523 		}
524 	}
525 input_failure:
526 	return (nconversions != 0 ? nassigned : -1);
527 match_failure:
528 	return (nassigned);
529 }
530 
531 /*
532  * Fill in the given table from the scanset at the given format
533  * (just after `[').  Return a pointer to the character past the
534  * closing `]'.  The table has a 1 wherever characters should be
535  * considered part of the scanset.
536  */
537 static const uchar_t *
538 set_ccl(char *tab, const uchar_t *fmt)
539 {
540 	int c, n, v;
541 
542 	/* first `clear' the whole table */
543 	c = *fmt++;		/* first char hat => negated scanset */
544 	if (c == '^') {
545 		v = 1;		/* default => accept */
546 		c = *fmt++;	/* get new first char */
547 	} else
548 		v = 0;		/* default => reject */
549 
550 	/* XXX: Will not work if sizeof(tab*) > sizeof(char) */
551 	for (n = 0; n < 256; n++)
552 		tab[n] = v;	/* memset(tab, v, 256) */
553 
554 	if (c == 0)
555 		return (fmt - 1); /* format ended before closing ] */
556 
557 	/*
558 	 * Now set the entries corresponding to the actual scanset
559 	 * to the opposite of the above.
560 	 *
561 	 * The first character may be ']' (or '-') without being special;
562 	 * the last character may be '-'.
563 	 */
564 	v = 1 - v;
565 	for (;;) {
566 		tab[c] = v;		/* take character c */
567 doswitch:
568 		n = *fmt++;		/* and examine the next */
569 		switch (n) {
570 
571 		case 0:			/* format ended too soon */
572 			return (fmt - 1);
573 
574 		case '-':
575 			/*
576 			 * A scanset of the form
577 			 *	[01+-]
578 			 * is defined as `the digit 0, the digit 1,
579 			 * the character +, the character -', but
580 			 * the effect of a scanset such as
581 			 *	[a-zA-Z0-9]
582 			 * is implementation defined.  The V7 Unix
583 			 * scanf treats `a-z' as `the letters a through
584 			 * z', but treats `a-a' as `the letter a, the
585 			 * character -, and the letter a'.
586 			 *
587 			 * For compatibility, the `-' is not considerd
588 			 * to define a range if the character following
589 			 * it is either a close bracket (required by ANSI)
590 			 * or is not numerically greater than the character
591 			 * we just stored in the table (c).
592 			 */
593 			n = *fmt;
594 			if (n == ']' || n < c) {
595 				c = '-';
596 				break;	/* resume the for(;;) */
597 			}
598 			fmt++;
599 			/* fill in the range */
600 			do {
601 			    tab[++c] = v;
602 			} while (c < n);
603 			c = n;
604 			/*
605 			 * Alas, the V7 Unix scanf also treats formats
606 			 * such as [a-c-e] as `the letters a through e'.
607 			 * This too is permitted by the standard....
608 			 */
609 			goto doswitch;
610 			/* NOTREACHED */
611 
612 		case ']':		/* end of scanset */
613 			return (fmt);
614 
615 		default:		/* just another character */
616 			c = n;
617 			break;
618 		}
619 	}
620 	/* NOTREACHED */
621 }
622 
623 int
624 sscanf(const char *ibuf, const char *fmt, ...)
625 {
626 	va_list ap;
627 	int ret;
628 
629 	va_start(ap, fmt);
630 	ret = vsscanf(ibuf, fmt, ap);
631 	va_end(ap);
632 	return (ret);
633 }
634