Debian Codegraph @

pTk/mTk/tclGeneric/tclScan.c

raw symbols SCIP
1 /*
2 * tclScan.c --
3 *
4 * This file contains the implementation of the "scan" command.
5 *
6 * Copyright (c) 1998 by Scriptics Corporation.
7 *
8 * See the file "license.terms" for information on usage and redistribution
9 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id: tclScan.c,v 1.12 2002/02/25 15:23:02 dkf Exp $
12 */
13
14 #include "tclInt.h"
15 /*
16 * For strtoll() and strtoull() declarations on some platforms...
17 */
18 #include "tclPort.h"
19
20 /*
21 * Flag values used by Tcl_ScanObjCmd.
22 */
23
24 #define SCAN_NOSKIP 0x1 /* Don't skip blanks. */
25 #define SCAN_SUPPRESS 0x2 /* Suppress assignment. */
26 #define SCAN_UNSIGNED 0x4 /* Read an unsigned value. */
27 #define SCAN_WIDTH 0x8 /* A width value was supplied. */
28
29 #define SCAN_SIGNOK 0x10 /* A +/- character is allowed. */
30 #define SCAN_NODIGITS 0x20 /* No digits have been scanned. */
31 #define SCAN_NOZERO 0x40 /* No zero digits have been scanned. */
32 #define SCAN_XOK 0x80 /* An 'x' is allowed. */
33 #define SCAN_PTOK 0x100 /* Decimal point is allowed. */
34 #define SCAN_EXPOK 0x200 /* An exponent is allowed. */
35
36 #define SCAN_LONGER 0x400 /* Asked for a wide value. */
37
38 /*
39 * The following structure contains the information associated with
40 * a character set.
41 */
42
43 typedef struct CharSet {
44 int exclude; /* 1 if this is an exclusion set. */
45 int nchars;
46 Tcl_UniChar *chars;
47 int nranges;
48 struct Range {
49 Tcl_UniChar start;
50 Tcl_UniChar end;
51 } *ranges;
52 } CharSet;
53
54 /*
55 * Declarations for functions used only in this file.
56 */
57
58 static char * BuildCharSet _ANSI_ARGS_((CharSet *cset, char *format));
59 static int CharInSet _ANSI_ARGS_((CharSet *cset, int ch));
60 static void ReleaseCharSet _ANSI_ARGS_((CharSet *cset));
61 static int ValidateFormat _ANSI_ARGS_((Tcl_Interp *interp, char *format,
62 int numVars, int *totalVars));
63
64 /*
65 *----------------------------------------------------------------------
66 *
67 * BuildCharSet --
68 *
69 * This function examines a character set format specification
70 * and builds a CharSet containing the individual characters and
71 * character ranges specified.
72 *
73 * Results:
74 * Returns the next format position.
75 *
76 * Side effects:
77 * Initializes the charset.
78 *
79 *----------------------------------------------------------------------
80 */
81
82 static char *
83 BuildCharSet(cset, format)
84 CharSet *cset;
85 char *format; /* Points to first char of set. */
86 {
87 Tcl_UniChar ch, start;
88 int offset, nranges;
89 char *end;
90
91 memset(cset, 0, sizeof(CharSet));
92
93 offset = Tcl_UtfToUniChar(format, &ch);
94 if (ch == '^') {
95 cset->exclude = 1;
96 format += offset;
97 offset = Tcl_UtfToUniChar(format, &ch);
98 }
99 end = format + offset;
100
101 /*
102 * Find the close bracket so we can overallocate the set.
103 */
104
105 if (ch == ']') {
106 end += Tcl_UtfToUniChar(end, &ch);
107 }
108 nranges = 0;
109 while (ch != ']') {
110 if (ch == '-') {
111 nranges++;
112 }
113 end += Tcl_UtfToUniChar(end, &ch);
114 }
115
116 cset->chars = (Tcl_UniChar *) ckalloc(sizeof(Tcl_UniChar)
117 * (end - format - 1));
118 if (nranges > 0) {
119 cset->ranges = (struct Range *) ckalloc(sizeof(struct Range)*nranges);
120 } else {
121 cset->ranges = NULL;
122 }
123
124 /*
125 * Now build the character set.
126 */
127
128 cset->nchars = cset->nranges = 0;
129 format += Tcl_UtfToUniChar(format, &ch);
130 start = ch;
131 if (ch == ']' || ch == '-') {
132 cset->chars[cset->nchars++] = ch;
133 format += Tcl_UtfToUniChar(format, &ch);
134 }
135 while (ch != ']') {
136 if (*format == '-') {
137 /*
138 * This may be the first character of a range, so don't add
139 * it yet.
140 */
141
142 start = ch;
143 } else if (ch == '-') {
144 /*
145 * Check to see if this is the last character in the set, in which
146 * case it is not a range and we should add the previous character
147 * as well as the dash.
148 */
149
150 if (*format == ']') {
151 cset->chars[cset->nchars++] = start;
152 cset->chars[cset->nchars++] = ch;
153 } else {
154 format += Tcl_UtfToUniChar(format, &ch);
155
156 /*
157 * Check to see if the range is in reverse order.
158 */
159
160 if (start < ch) {
161 cset->ranges[cset->nranges].start = start;
162 cset->ranges[cset->nranges].end = ch;
163 } else {
164 cset->ranges[cset->nranges].start = ch;
165 cset->ranges[cset->nranges].end = start;
166 }
167 cset->nranges++;
168 }
169 } else {
170 cset->chars[cset->nchars++] = ch;
171 }
172 format += Tcl_UtfToUniChar(format, &ch);
173 }
174 return format;
175 }
176
177 /*
178 *----------------------------------------------------------------------
179 *
180 * CharInSet --
181 *
182 * Check to see if a character matches the given set.
183 *
184 * Results:
185 * Returns non-zero if the character matches the given set.
186 *
187 * Side effects:
188 * None.
189 *
190 *----------------------------------------------------------------------
191 */
192
193 static int
194 CharInSet(cset, c)
195 CharSet *cset;
196 int c; /* Character to test, passed as int because
197 * of non-ANSI prototypes. */
198 {
199 Tcl_UniChar ch = (Tcl_UniChar) c;
200 int i, match = 0;
201 for (i = 0; i < cset->nchars; i++) {
202 if (cset->chars[i] == ch) {
203 match = 1;
204 break;
205 }
206 }
207 if (!match) {
208 for (i = 0; i < cset->nranges; i++) {
209 if ((cset->ranges[i].start <= ch)
210 && (ch <= cset->ranges[i].end)) {
211 match = 1;
212 break;
213 }
214 }
215 }
216 return (cset->exclude ? !match : match);
217 }
218
219 /*
220 *----------------------------------------------------------------------
221 *
222 * ReleaseCharSet --
223 *
224 * Free the storage associated with a character set.
225 *
226 * Results:
227 * None.
228 *
229 * Side effects:
230 * None.
231 *
232 *----------------------------------------------------------------------
233 */
234
235 static void
236 ReleaseCharSet(cset)
237 CharSet *cset;
238 {
239 ckfree((char *)cset->chars);
240 if (cset->ranges) {
241 ckfree((char *)cset->ranges);
242 }
243 }
244
245 /*
246 *----------------------------------------------------------------------
247 *
248 * ValidateFormat --
249 *
250 * Parse the format string and verify that it is properly formed
251 * and that there are exactly enough variables on the command line.
252 *
253 * Results:
254 * A standard Tcl result.
255 *
256 * Side effects:
257 * May place an error in the interpreter result.
258 *
259 *----------------------------------------------------------------------
260 */
261
262 static int
263 ValidateFormat(interp, format, numVars, totalSubs)
264 Tcl_Interp *interp; /* Current interpreter. */
265 char *format; /* The format string. */
266 int numVars; /* The number of variables passed to the
267 * scan command. */
268 int *totalSubs; /* The number of variables that will be
269 * required. */
270 {
271 #define STATIC_LIST_SIZE 16
272 int gotXpg, gotSequential, value, i, flags;
273 char *end;
274 Tcl_UniChar ch;
275 int staticAssign[STATIC_LIST_SIZE];
276 int *nassign = staticAssign;
277 int objIndex, xpgSize, nspace = STATIC_LIST_SIZE;
278 char buf[TCL_UTF_MAX+1];
279
280 /*
281 * Initialize an array that records the number of times a variable
282 * is assigned to by the format string. We use this to detect if
283 * a variable is multiply assigned or left unassigned.
284 */
285
286 if (numVars > nspace) {
287 nassign = (int*)ckalloc(sizeof(int) * numVars);
288 nspace = numVars;
289 }
290 for (i = 0; i < nspace; i++) {
291 nassign[i] = 0;
292 }
293
294 xpgSize = objIndex = gotXpg = gotSequential = 0;
295
296 while (*format != '\0') {
297 format += Tcl_UtfToUniChar(format, &ch);
298
299 flags = 0;
300
301 if (ch != '%') {
302 continue;
303 }
304 format += Tcl_UtfToUniChar(format, &ch);
305 if (ch == '%') {
306 continue;
307 }
308 if (ch == '*') {
309 flags |= SCAN_SUPPRESS;
310 format += Tcl_UtfToUniChar(format, &ch);
311 goto xpgCheckDone;
312 }
313
314 if ((ch < 0x80) && isdigit(UCHAR(ch))) { /* INTL: "C" locale. */
315 /*
316 * Check for an XPG3-style %n$ specification. Note: there
317 * must not be a mixture of XPG3 specs and non-XPG3 specs
318 * in the same format string.
319 */
320
321 value = strtoul(format-1, &end, 10); /* INTL: "C" locale. */
322 if (*end != '$') {
323 goto notXpg;
324 }
325 format = end+1;
326 format += Tcl_UtfToUniChar(format, &ch);
327 gotXpg = 1;
328 if (gotSequential) {
329 goto mixedXPG;
330 }
331 objIndex = value - 1;
332 if ((objIndex < 0) || (numVars && (objIndex >= numVars))) {
333 goto badIndex;
334 } else if (numVars == 0) {
335 /*
336 * In the case where no vars are specified, the user can
337 * specify %9999$ legally, so we have to consider special
338 * rules for growing the assign array. 'value' is
339 * guaranteed to be > 0.
340 */
341 xpgSize = (xpgSize > value) ? xpgSize : value;
342 }
343 goto xpgCheckDone;
344 }
345
346 notXpg:
347 gotSequential = 1;
348 if (gotXpg) {
349 mixedXPG:
350 Tcl_SetResult(interp,
351 "cannot mix \"%\" and \"%n$\" conversion specifiers",
352 TCL_STATIC);
353 goto error;
354 }
355
356 xpgCheckDone:
357 /*
358 * Parse any width specifier.
359 */
360
361 if ((ch < 0x80) && isdigit(UCHAR(ch))) { /* INTL: "C" locale. */
362 value = strtoul(format-1, &format, 10); /* INTL: "C" locale. */
363 flags |= SCAN_WIDTH;
364 format += Tcl_UtfToUniChar(format, &ch);
365 }
366
367 /*
368 * Handle any size specifier.
369 */
370
371 switch (ch) {
372 case 'l':
373 case 'L':
374 #ifndef TCL_WIDE_INT_IS_LONG
375 flags |= SCAN_LONGER;
376 #endif
377 case 'h':
378 format += Tcl_UtfToUniChar(format, &ch);
379 }
380
381 if (!(flags & SCAN_SUPPRESS) && numVars && (objIndex >= numVars)) {
382 goto badIndex;
383 }
384
385 /*
386 * Handle the various field types.
387 */
388
389 switch (ch) {
390 case 'c':
391 if (flags & SCAN_WIDTH) {
392 Tcl_SetResult(interp,
393 "field width may not be specified in %c conversion",
394 TCL_STATIC);
395 goto error;
396 }
397 /*
398 * Fall through!
399 */
400 case 'n':
401 case 's':
402 if (flags & SCAN_LONGER) {
403 invalidLonger:
404 buf[Tcl_UniCharToUtf(ch, buf)] = '\0';
405 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
406 "'l' modifier may not be specified in %", buf,
407 " conversion", NULL);
408 goto error;
409 }
410 /*
411 * Fall through!
412 */
413 case 'd':
414 case 'e':
415 case 'f':
416 case 'g':
417 case 'i':
418 case 'o':
419 case 'u':
420 case 'x':
421 break;
422 /*
423 * Bracket terms need special checking
424 */
425 case '[':
426 if (flags & SCAN_LONGER) {
427 goto invalidLonger;
428 }
429 if (*format == '\0') {
430 goto badSet;
431 }
432 format += Tcl_UtfToUniChar(format, &ch);
433 if (ch == '^') {
434 if (*format == '\0') {
435 goto badSet;
436 }
437 format += Tcl_UtfToUniChar(format, &ch);
438 }
439 if (ch == ']') {
440 if (*format == '\0') {
441 goto badSet;
442 }
443 format += Tcl_UtfToUniChar(format, &ch);
444 }
445 while (ch != ']') {
446 if (*format == '\0') {
447 goto badSet;
448 }
449 format += Tcl_UtfToUniChar(format, &ch);
450 }
451 break;
452 badSet:
453 Tcl_SetResult(interp, "unmatched [ in format string",
454 TCL_STATIC);
455 goto error;
456 default:
457 {
458 char buf[TCL_UTF_MAX+1];
459
460 buf[Tcl_UniCharToUtf(ch, buf)] = '\0';
461 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
462 "bad scan conversion character \"", buf, "\"", NULL);
463 goto error;
464 }
465 }
466 if (!(flags & SCAN_SUPPRESS)) {
467 if (objIndex >= nspace) {
468 /*
469 * Expand the nassign buffer. If we are using XPG specifiers,
470 * make sure that we grow to a large enough size. xpgSize is
471 * guaranteed to be at least one larger than objIndex.
472 */
473 value = nspace;
474 if (xpgSize) {
475 nspace = xpgSize;
476 } else {
477 nspace += STATIC_LIST_SIZE;
478 }
479 if (nassign == staticAssign) {
480 nassign = (void *)ckalloc(nspace * sizeof(int));
481 for (i = 0; i < STATIC_LIST_SIZE; ++i) {
482 nassign[i] = staticAssign[i];
483 }
484 } else {
485 nassign = (void *)ckrealloc((void *)nassign,
486 nspace * sizeof(int));
487 }
488 for (i = value; i < nspace; i++) {
489 nassign[i] = 0;
490 }
491 }
492 nassign[objIndex]++;
493 objIndex++;
494 }
495 }
496
497 /*
498 * Verify that all of the variable were assigned exactly once.
499 */
500
501 if (numVars == 0) {
502 if (xpgSize) {
503 numVars = xpgSize;
504 } else {
505 numVars = objIndex;
506 }
507 }
508 if (totalSubs) {
509 *totalSubs = numVars;
510 }
511 for (i = 0; i < numVars; i++) {
512 if (nassign[i] > 1) {
513 Tcl_SetResult(interp, "variable is assigned by multiple \"%n$\" conversion specifiers", TCL_STATIC);
514 goto error;
515 } else if (!xpgSize && (nassign[i] == 0)) {
516 /*
517 * If the space is empty, and xpgSize is 0 (means XPG wasn't
518 * used, and/or numVars != 0), then too many vars were given
519 */
520 Tcl_SetResult(interp, "variable is not assigned by any conversion specifiers", TCL_STATIC);
521 goto error;
522 }
523 }
524
525 if (nassign != staticAssign) {
526 ckfree((char *)nassign);
527 }
528 return TCL_OK;
529
530 badIndex:
531 if (gotXpg) {
532 Tcl_SetResult(interp, "\"%n$\" argument index out of range",
533 TCL_STATIC);
534 } else {
535 Tcl_SetResult(interp,
536 "different numbers of variable names and field specifiers",
537 TCL_STATIC);
538 }
539
540 error:
541 if (nassign != staticAssign) {
542 ckfree((char *)nassign);
543 }
544 return TCL_ERROR;
545 #undef STATIC_LIST_SIZE
546 }
547
548 /*
549 *----------------------------------------------------------------------
550 *
551 * Tcl_ScanObjCmd --
552 *
553 * This procedure is invoked to process the "scan" Tcl command.
554 * See the user documentation for details on what it does.
555 *
556 * Results:
557 * A standard Tcl result.
558 *
559 * Side effects:
560 * See the user documentation.
561 *
562 *----------------------------------------------------------------------
563 */
564
565 /* ARGSUSED */
566 int
567 Tcl_ScanObjCmd(dummy, interp, objc, objv)
568 ClientData dummy; /* Not used. */
569 Tcl_Interp *interp; /* Current interpreter. */
570 int objc; /* Number of arguments. */
571 Tcl_Obj *CONST objv[]; /* Argument objects. */
572 {
573 char *format;
574 int numVars, nconversions, totalVars = -1;
575 int objIndex, offset, i, result, code;
576 long value;
577 char *string, *end, *baseString;
578 char op = 0;
579 int base = 0;
580 int underflow = 0;
581 size_t width;
582 long (*fn)() = NULL;
583 #ifndef TCL_WIDE_INT_IS_LONG
584 Tcl_WideInt (*lfn)() = NULL;
585 Tcl_WideInt wideValue;
586 #endif
587 Tcl_UniChar ch, sch;
588 Tcl_Obj **objs = NULL, *objPtr = NULL;
589 int flags;
590 char buf[513]; /* Temporary buffer to hold scanned
591 * number strings before they are
592 * passed to strtoul. */
593
594 if (objc < 3) {
595 Tcl_WrongNumArgs(interp, 1, objv,
596 "string format ?varName varName ...?");
597 return TCL_ERROR;
598 }
599
600 format = Tcl_GetStringFromObj(objv[2], NULL);
601 numVars = objc-3;
602
603 /*
604 * Check for errors in the format string.
605 */
606
607 if (ValidateFormat(interp, format, numVars, &totalVars) == TCL_ERROR) {
608 return TCL_ERROR;
609 }
610
611 /*
612 * Allocate space for the result objects.
613 */
614
615 if (totalVars > 0) {
616 objs = (Tcl_Obj **) ckalloc(sizeof(Tcl_Obj*) * totalVars);
617 for (i = 0; i < totalVars; i++) {
618 objs[i] = NULL;
619 }
620 }
621
622 string = Tcl_GetStringFromObj(objv[1], NULL);
623 baseString = string;
624
625 /*
626 * Iterate over the format string filling in the result objects until
627 * we reach the end of input, the end of the format string, or there
628 * is a mismatch.
629 */
630
631 objIndex = 0;
632 nconversions = 0;
633 while (*format != '\0') {
634 format += Tcl_UtfToUniChar(format, &ch);
635
636 flags = 0;
637
638 /*
639 * If we see whitespace in the format, skip whitespace in the string.
640 */
641
642 if (Tcl_UniCharIsSpace(ch)) {
643 offset = Tcl_UtfToUniChar(string, &sch);
644 while (Tcl_UniCharIsSpace(sch)) {
645 if (*string == '\0') {
646 goto done;
647 }
648 string += offset;
649 offset = Tcl_UtfToUniChar(string, &sch);
650 }
651 continue;
652 }
653
654 if (ch != '%') {
655 literal:
656 if (*string == '\0') {
657 underflow = 1;
658 goto done;
659 }
660 string += Tcl_UtfToUniChar(string, &sch);
661 if (ch != sch) {
662 goto done;
663 }
664 continue;
665 }
666
667 format += Tcl_UtfToUniChar(format, &ch);
668 if (ch == '%') {
669 goto literal;
670 }
671
672 /*
673 * Check for assignment suppression ('*') or an XPG3-style
674 * assignment ('%n$').
675 */
676
677 if (ch == '*') {
678 flags |= SCAN_SUPPRESS;
679 format += Tcl_UtfToUniChar(format, &ch);
680 } else if ((ch < 0x80) && isdigit(UCHAR(ch))) { /* INTL: "C" locale. */
681 value = strtoul(format-1, &end, 10); /* INTL: "C" locale. */
682 if (*end == '$') {
683 format = end+1;
684 format += Tcl_UtfToUniChar(format, &ch);
685 objIndex = (int) value - 1;
686 }
687 }
688
689 /*
690 * Parse any width specifier.
691 */
692
693 if ((ch < 0x80) && isdigit(UCHAR(ch))) { /* INTL: "C" locale. */
694 width = strtoul(format-1, &format, 10); /* INTL: "C" locale. */
695 format += Tcl_UtfToUniChar(format, &ch);
696 } else {
697 width = 0;
698 }
699
700 /*
701 * Handle any size specifier.
702 */
703
704 switch (ch) {
705 case 'l':
706 case 'L':
707 #ifndef TCL_WIDE_INT_IS_LONG
708 flags |= SCAN_LONGER;
709 #endif
710 /*
711 * Fall through so we skip to the next character.
712 */
713 case 'h':
714 format += Tcl_UtfToUniChar(format, &ch);
715 }
716
717 /*
718 * Handle the various field types.
719 */
720
721 switch (ch) {
722 case 'n':
723 if (!(flags & SCAN_SUPPRESS)) {
724 objPtr = Tcl_NewIntObj(string - baseString);
725 Tcl_IncrRefCount(objPtr);
726 objs[objIndex++] = objPtr;
727 }
728 nconversions++;
729 continue;
730
731 case 'd':
732 op = 'i';
733 base = 10;
734 fn = (long (*)())strtol;
735 #ifndef TCL_WIDE_INT_IS_LONG
736 lfn = (Tcl_WideInt (*)())strtoll;
737 #endif
738 break;
739 case 'i':
740 op = 'i';
741 base = 0;
742 fn = (long (*)())strtol;
743 #ifndef TCL_WIDE_INT_IS_LONG
744 lfn = (Tcl_WideInt (*)())strtoll;
745 #endif
746 break;
747 case 'o':
748 op = 'i';
749 base = 8;
750 fn = (long (*)())strtoul;
751 #ifndef TCL_WIDE_INT_IS_LONG
752 lfn = (Tcl_WideInt (*)())strtoull;
753 #endif
754 break;
755 case 'x':
756 op = 'i';
757 base = 16;
758 fn = (long (*)())strtoul;
759 #ifndef TCL_WIDE_INT_IS_LONG
760 lfn = (Tcl_WideInt (*)())strtoull;
761 #endif
762 break;
763 case 'u':
764 op = 'i';
765 base = 10;
766 flags |= SCAN_UNSIGNED;
767 fn = (long (*)())strtoul;
768 #ifndef TCL_WIDE_INT_IS_LONG
769 lfn = (Tcl_WideInt (*)())strtoull;
770 #endif
771 break;
772
773 case 'f':
774 case 'e':
775 case 'g':
776 op = 'f';
777 break;
778
779 case 's':
780 op = 's';
781 break;
782
783 case 'c':
784 op = 'c';
785 flags |= SCAN_NOSKIP;
786 break;
787 case '[':
788 op = '[';
789 flags |= SCAN_NOSKIP;
790 break;
791 }
792
793 /*
794 * At this point, we will need additional characters from the
795 * string to proceed.
796 */
797
798 if (*string == '\0') {
799 underflow = 1;
800 goto done;
801 }
802
803 /*
804 * Skip any leading whitespace at the beginning of a field unless
805 * the format suppresses this behavior.
806 */
807
808 if (!(flags & SCAN_NOSKIP)) {
809 while (*string != '\0') {
810 offset = Tcl_UtfToUniChar(string, &sch);
811 if (!Tcl_UniCharIsSpace(sch)) {
812 break;
813 }
814 string += offset;
815 }
816 if (*string == '\0') {
817 underflow = 1;
818 goto done;
819 }
820 }
821
822 /*
823 * Perform the requested scanning operation.
824 */
825
826 switch (op) {
827 case 's':
828 /*
829 * Scan a string up to width characters or whitespace.
830 */
831
832 if (width == 0) {
833 width = (size_t) ~0;
834 }
835 end = string;
836 while (*end != '\0') {
837 offset = Tcl_UtfToUniChar(end, &sch);
838 if (Tcl_UniCharIsSpace(sch)) {
839 break;
840 }
841 end += offset;
842 if (--width == 0) {
843 break;
844 }
845 }
846 if (!(flags & SCAN_SUPPRESS)) {
847 objPtr = Tcl_NewStringObj(string, end-string);
848 Tcl_IncrRefCount(objPtr);
849 objs[objIndex++] = objPtr;
850 }
851 string = end;
852 break;
853
854 case '[': {
855 CharSet cset;
856
857 if (width == 0) {
858 width = (size_t) ~0;
859 }
860 end = string;
861
862 format = BuildCharSet(&cset, format);
863 while (*end != '\0') {
864 offset = Tcl_UtfToUniChar(end, &sch);
865 if (!CharInSet(&cset, (int)sch)) {
866 break;
867 }
868 end += offset;
869 if (--width == 0) {
870 break;
871 }
872 }
873 ReleaseCharSet(&cset);
874
875 if (string == end) {
876 /*
877 * Nothing matched the range, stop processing
878 */
879 goto done;
880 }
881 if (!(flags & SCAN_SUPPRESS)) {
882 objPtr = Tcl_NewStringObj(string, end-string);
883 Tcl_IncrRefCount(objPtr);
884 objs[objIndex++] = objPtr;
885 }
886 string = end;
887
888 break;
889 }
890 case 'c':
891 /*
892 * Scan a single Unicode character.
893 */
894
895 string += Tcl_UtfToUniChar(string, &sch);
896 if (!(flags & SCAN_SUPPRESS)) {
897 objPtr = Tcl_NewIntObj((int)sch);
898 Tcl_IncrRefCount(objPtr);
899 objs[objIndex++] = objPtr;
900 }
901 break;
902
903 case 'i':
904 /*
905 * Scan an unsigned or signed integer.
906 */
907
908 if ((width == 0) || (width > sizeof(buf) - 1)) {
909 width = sizeof(buf) - 1;
910 }
911 flags |= SCAN_SIGNOK | SCAN_NODIGITS | SCAN_NOZERO;
912 for (end = buf; width > 0; width--) {
913 switch (*string) {
914 /*
915 * The 0 digit has special meaning at the beginning of
916 * a number. If we are unsure of the base, it
917 * indicates that we are in base 8 or base 16 (if it is
918 * followed by an 'x').
919 *
920 * 8.1 - 8.3.4 incorrectly handled 0x... base-16
921 * cases for %x by not reading the 0x as the
922 * auto-prelude for base-16. [Bug #495213]
923 */
924 case '0':
925 if (base == 0) {
926 base = 8;
927 flags |= SCAN_XOK;
928 }
929 if (base == 16) {
930 flags |= SCAN_XOK;
931 }
932 if (flags & SCAN_NOZERO) {
933 flags &= ~(SCAN_SIGNOK | SCAN_NODIGITS
934 | SCAN_NOZERO);
935 } else {
936 flags &= ~(SCAN_SIGNOK | SCAN_XOK
937 | SCAN_NODIGITS);
938 }
939 goto addToInt;
940
941 case '1': case '2': case '3': case '4':
942 case '5': case '6': case '7':
943 if (base == 0) {
944 base = 10;
945 }
946 flags &= ~(SCAN_SIGNOK | SCAN_XOK | SCAN_NODIGITS);
947 goto addToInt;
948
949 case '8': case '9':
950 if (base == 0) {
951 base = 10;
952 }
953 if (base <= 8) {
954 break;
955 }
956 flags &= ~(SCAN_SIGNOK | SCAN_XOK | SCAN_NODIGITS);
957 goto addToInt;
958
959 case 'A': case 'B': case 'C':
960 case 'D': case 'E': case 'F':
961 case 'a': case 'b': case 'c':
962 case 'd': case 'e': case 'f':
963 if (base <= 10) {
964 break;
965 }
966 flags &= ~(SCAN_SIGNOK | SCAN_XOK | SCAN_NODIGITS);
967 goto addToInt;
968
969 case '+': case '-':
970 if (flags & SCAN_SIGNOK) {
971 flags &= ~SCAN_SIGNOK;
972 goto addToInt;
973 }
974 break;
975
976 case 'x': case 'X':
977 if ((flags & SCAN_XOK) && (end == buf+1)) {
978 base = 16;
979 flags &= ~SCAN_XOK;
980 goto addToInt;
981 }
982 break;
983 }
984
985 /*
986 * We got an illegal character so we are done accumulating.
987 */
988
989 break;
990
991 addToInt:
992 /*
993 * Add the character to the temporary buffer.
994 */
995
996 *end++ = *string++;
997 if (*string == '\0') {
998 break;
999 }
1000 }
1001
1002 /*
1003 * Check to see if we need to back up because we only got a
1004 * sign or a trailing x after a 0.
1005 */
1006
1007 if (flags & SCAN_NODIGITS) {
1008 if (*string == '\0') {
1009 underflow = 1;
1010 }
1011 goto done;
1012 } else if (end[-1] == 'x' || end[-1] == 'X') {
1013 end--;
1014 string--;
1015 }
1016
1017
1018 /*
1019 * Scan the value from the temporary buffer. If we are
1020 * returning a large unsigned value, we have to convert it back
1021 * to a string since Tcl only supports signed values.
1022 */
1023
1024 if (!(flags & SCAN_SUPPRESS)) {
1025 *end = '\0';
1026 #ifndef TCL_WIDE_INT_IS_LONG
1027 if (flags & SCAN_LONGER) {
1028 wideValue = (Tcl_WideInt) (*lfn)(buf, NULL, base);
1029 if ((flags & SCAN_UNSIGNED) && (wideValue < 0)) {
1030 /* INTL: ISO digit */
1031 sprintf(buf, "%" TCL_LL_MODIFIER "u",
1032 (Tcl_WideUInt)wideValue);
1033 objPtr = Tcl_NewStringObj(buf, -1);
1034 } else {
1035 objPtr = Tcl_NewWideIntObj(wideValue);
1036 }
1037 } else {
1038 #endif /* !TCL_WIDE_INT_IS_LONG */
1039 value = (long) (*fn)(buf, NULL, base);
1040 if ((flags & SCAN_UNSIGNED) && (value < 0)) {
1041 sprintf(buf, "%lu", value); /* INTL: ISO digit */
1042 objPtr = Tcl_NewStringObj(buf, -1);
1043 } else {
1044 if ((unsigned long) value > UINT_MAX) {
1045 objPtr = Tcl_NewLongObj(value);
1046 } else {
1047 objPtr = Tcl_NewIntObj(value);
1048 }
1049 }
1050 #ifndef TCL_WIDE_INT_IS_LONG
1051 }
1052 #endif
1053 Tcl_IncrRefCount(objPtr);
1054 objs[objIndex++] = objPtr;
1055 }
1056
1057 break;
1058
1059 case 'f':
1060 /*
1061 * Scan a floating point number
1062 */
1063
1064 if ((width == 0) || (width > sizeof(buf) - 1)) {
1065 width = sizeof(buf) - 1;
1066 }
1067 flags &= ~SCAN_LONGER;
1068 flags |= SCAN_SIGNOK | SCAN_NODIGITS | SCAN_PTOK | SCAN_EXPOK;
1069 for (end = buf; width > 0; width--) {
1070 switch (*string) {
1071 case '0': case '1': case '2': case '3':
1072 case '4': case '5': case '6': case '7':
1073 case '8': case '9':
1074 flags &= ~(SCAN_SIGNOK | SCAN_NODIGITS);
1075 goto addToFloat;
1076 case '+': case '-':
1077 if (flags & SCAN_SIGNOK) {
1078 flags &= ~SCAN_SIGNOK;
1079 goto addToFloat;
1080 }
1081 break;
1082 case '.':
1083 if (flags & SCAN_PTOK) {
1084 flags &= ~(SCAN_SIGNOK | SCAN_PTOK);
1085 goto addToFloat;
1086 }
1087 break;
1088 case 'e': case 'E':
1089 /*
1090 * An exponent is not allowed until there has
1091 * been at least one digit.
1092 */
1093
1094 if ((flags & (SCAN_NODIGITS | SCAN_EXPOK))
1095 == SCAN_EXPOK) {
1096 flags = (flags & ~(SCAN_EXPOK|SCAN_PTOK))
1097 | SCAN_SIGNOK | SCAN_NODIGITS;
1098 goto addToFloat;
1099 }
1100 break;
1101 }
1102
1103 /*
1104 * We got an illegal character so we are done accumulating.
1105 */
1106
1107 break;
1108
1109 addToFloat:
1110 /*
1111 * Add the character to the temporary buffer.
1112 */
1113
1114 *end++ = *string++;
1115 if (*string == '\0') {
1116 break;
1117 }
1118 }
1119
1120 /*
1121 * Check to see if we need to back up because we saw a
1122 * trailing 'e' or sign.
1123 */
1124
1125 if (flags & SCAN_NODIGITS) {
1126 if (flags & SCAN_EXPOK) {
1127 /*
1128 * There were no digits at all so scanning has
1129 * failed and we are done.
1130 */
1131 if (*string == '\0') {
1132 underflow = 1;
1133 }
1134 goto done;
1135 }
1136
1137 /*
1138 * We got a bad exponent ('e' and maybe a sign).
1139 */
1140
1141 end--;
1142 string--;
1143 if (*end != 'e' && *end != 'E') {
1144 end--;
1145 string--;
1146 }
1147 }
1148
1149 /*
1150 * Scan the value from the temporary buffer.
1151 */
1152
1153 if (!(flags & SCAN_SUPPRESS)) {
1154 double dvalue;
1155 *end = '\0';
1156 dvalue = strtod(buf, NULL);
1157 objPtr = Tcl_NewDoubleObj(dvalue);
1158 Tcl_IncrRefCount(objPtr);
1159 objs[objIndex++] = objPtr;
1160 }
1161 break;
1162 }
1163 nconversions++;
1164 }
1165
1166 done:
1167 result = 0;
1168 code = TCL_OK;
1169
1170 if (numVars) {
1171 /*
1172 * In this case, variables were specified (classic scan)
1173 */
1174 for (i = 0; i < totalVars; i++) {
1175 if (objs[i] != NULL) {
1176 result++;
1177 if (Tcl_ObjSetVar2(interp, objv[i+3], NULL,
1178 objs[i], 0) == NULL) {
1179 Tcl_AppendStringsToObj(Tcl_GetObjResult(interp),
1180 "couldn't set variable \"",
1181 Tcl_GetString(objv[i+3]), "\"", (char *) NULL);
1182 code = TCL_ERROR;
1183 }
1184 Tcl_DecrRefCount(objs[i]);
1185 }
1186 }
1187 } else {
1188 /*
1189 * Here no vars were specified, we want a list returned (inline scan)
1190 */
1191 objPtr = Tcl_NewObj();
1192 for (i = 0; i < totalVars; i++) {
1193 if (objs[i] != NULL) {
1194 Tcl_ListObjAppendElement(NULL, objPtr, objs[i]);
1195 Tcl_DecrRefCount(objs[i]);
1196 } else {
1197 /*
1198 * More %-specifiers than matching chars, so we
1199 * just spit out empty strings for these
1200 */
1201 Tcl_ListObjAppendElement(NULL, objPtr, Tcl_NewObj());
1202 }
1203 }
1204 }
1205 if (objs != NULL) {
1206 ckfree((char*) objs);
1207 }
1208 if (code == TCL_OK) {
1209 if (underflow && (nconversions == 0)) {
1210 if (numVars) {
1211 objPtr = Tcl_NewIntObj(-1);
1212 } else {
1213 if (objPtr) {
1214 Tcl_SetListObj(objPtr, 0, NULL);
1215 } else {
1216 objPtr = Tcl_NewObj();
1217 }
1218 }
1219 } else if (numVars) {
1220 objPtr = Tcl_NewIntObj(result);
1221 }
1222 Tcl_SetObjResult(interp, objPtr);
1223 }
1224 return code;
1225 }