Debian Codegraph @

generic/tclScan.c

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