OSDN Git Service

Updated to tcl 8.4.1
[pf3gnuchains/pf3gnuchains3x.git] / tcl / doc / scan.n
1 '\"
2 '\" Copyright (c) 1993 The Regents of the University of California.
3 '\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
4 '\" Copyright (c) 2000 Scriptics Corporation.
5 '\"
6 '\" See the file "license.terms" for information on usage and redistribution
7 '\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
8 '\" 
9 '\" RCS: @(#) $Id$
10 '\" 
11 .so man.macros
12 .TH scan n 8.4 Tcl "Tcl Built-In Commands"
13 .BS
14 '\" Note:  do not modify the .SH NAME line immediately below!
15 .SH NAME
16 scan \- Parse string using conversion specifiers in the style of sscanf
17 .SH SYNOPSIS
18 \fBscan \fIstring format \fR?\fIvarName varName ...\fR?
19 .BE
20
21 .SH INTRODUCTION
22 .PP
23 This command parses fields from an input string in the same fashion as the
24 ANSI C \fBsscanf\fR procedure and returns a count of the number of
25 conversions performed, or -1 if the end of the input string is reached
26 before any conversions have been performed.  \fIString\fR gives the input
27 to be parsed and \fIformat\fR indicates how to parse it, using \fB%\fR
28 conversion specifiers as in \fBsscanf\fR.  Each \fIvarName\fR gives the
29 name of a variable; when a field is scanned from \fIstring\fR the result is
30 converted back into a string and assigned to the corresponding variable.
31 If no \fIvarName\fR variables are specified, then \fBscan\fR works in an
32 inline manner, returning the data that would otherwise be stored in the
33 variables as a list.  In the inline case, an empty string is returned when
34 the end of the input string is reached before any conversions have been
35 performed.
36
37 .SH "DETAILS ON SCANNING"
38 .PP
39 \fBScan\fR operates by scanning \fIstring\fR and \fIformat\fR together.
40 If the next character in \fIformat\fR is a blank or tab then it
41 matches any number of white space characters in \fIstring\fR (including
42 zero).
43 Otherwise, if it isn't a \fB%\fR character then it 
44 must match the next character of \fIstring\fR.
45 When a \fB%\fR is encountered in \fIformat\fR, it indicates
46 the start of a conversion specifier.
47 .VS 8.4
48 A conversion specifier contains up to four fields after the \fB%\fR:
49 a \fB*\fR, which indicates that the converted value is to be discarded 
50 instead of assigned to a variable; a XPG3 position specifier; a number
51 indicating a maximum field width; a field size modifier; and a
52 conversion character.
53 .VE 8.4
54 All of these fields are optional except for the conversion character.
55 The fields that are present must appear in the order given above.
56 .PP
57 When \fBscan\fR finds a conversion specifier in \fIformat\fR, it
58 first skips any white-space characters in \fIstring\fR (unless the
59 specifier is \fB[\fR or \fBc\fR).
60 Then it converts the next input characters according to the 
61 conversion specifier and stores the result in the variable given
62 by the next argument to \fBscan\fR.
63 .PP
64 If the \fB%\fR is followed by a decimal number and a \fB$\fR, as in
65 ``\fB%2$d\fR'', then the variable to use is not taken from the next
66 sequential argument.  Instead, it is taken from the argument indicated
67 by the number, where 1 corresponds to the first \fIvarName\fR.  If
68 there are any positional specifiers in \fIformat\fR then all of the
69 specifiers must be positional.  Every \fIvarName\fR on the argument
70 list must correspond to exactly one conversion specifier or an error
71 is generated, or in the inline case, any position can be specified
72 at most once and the empty positions will be filled in with empty strings.
73 .PP
74 The following conversion characters are supported:
75 .TP 10
76 \fBd\fR
77 The input field must be a decimal integer.
78 It is read in and the value is stored in the variable as a decimal string.
79 .VS 8.4
80 If the \fBl\fR or \fBL\fR field size modifier is given, the scanned
81 value will have an internal representation that is at least 64-bits in
82 size.
83 .VE 8.4
84 .TP 10
85 \fBo\fR
86 The input field must be an octal integer. It is read in and the 
87 value is stored in the variable as a decimal string.
88 .VS 8.4
89 If the \fBl\fR or \fBL\fR field size modifier is given, the scanned
90 value will have an internal representation that is at least 64-bits in
91 size.
92 If the value exceeds MAX_INT (017777777777 on platforms using 32-bit
93 integers when the \fBl\fR and \fBL\fR modifiers are not given), it
94 will be truncated to a signed integer.  Hence, 037777777777 will
95 appear as -1 on a 32-bit machine by default.
96 .VE 8.4
97 .TP 10
98 \fBx\fR
99 The input field must be a hexadecimal integer. It is read in 
100 and the value is stored in the variable as a decimal string.
101 .VS 8.4
102 If the \fBl\fR or \fBL\fR field size modifier is given, the scanned
103 value will have an internal representation that is at least 64-bits in
104 size.
105 If the value exceeds MAX_INT (0x7FFFFFFF on platforms using 32-bit
106 integers when the \fBl\fR and \fBL\fR modifiers are not given), it
107 will be truncated to a signed integer.  Hence, 0xFFFFFFFF will appear
108 as -1 on a 32-bit machine.
109 .VE 8.4
110 .TP 10
111 \fBu\fR
112 The input field must be a decimal integer.  The value is stored in the
113 variable as an unsigned decimal integer string.
114 .VS 8.4
115 If the \fBl\fR or \fBL\fR field size modifier is given, the scanned
116 value will have an internal representation that is at least 64-bits in
117 size.
118 .VE 8.4
119 .TP 10
120 \fBi\fR 
121 The input field must be an integer.  The base (i.e. decimal, octal, or
122 hexadecimal) is determined in the same fashion as described in
123 \fBexpr\fR.  The value is stored in the variable as a decimal string.
124 .VS 8.4
125 If the \fBl\fR or \fBL\fR field size modifier is given, the scanned
126 value will have an internal representation that is at least 64-bits in
127 size.
128 .VE 8.4
129 .TP 10
130 \fBc\fR
131 A single character is read in and its binary value is stored in 
132 the variable as a decimal string.
133 Initial white space is not skipped in this case, so the input
134 field may be a white-space character.
135 This conversion is different from the ANSI standard in that the
136 input field always consists of a single character and no field
137 width may be specified.
138 .TP 10
139 \fBs\fR
140 The input field consists of all the characters up to the next 
141 white-space character; the characters are copied to the variable.
142 .TP 10
143 \fBe\fR or \fBf\fR or \fBg\fR
144 The input field must be a floating-point number consisting 
145 of an optional sign, a string of decimal digits possibly
146 containing a decimal point, and an optional exponent consisting 
147 of an \fBe\fR or \fBE\fR followed by an optional sign and a string of 
148 decimal digits.
149 It is read in and stored in the variable as a floating-point string.
150 .TP 10
151 \fB[\fIchars\fB]\fR
152 The input field consists of any number of characters in 
153 \fIchars\fR.
154 The matching string is stored in the variable.
155 If the first character between the brackets is a \fB]\fR then
156 it is treated as part of \fIchars\fR rather than the closing
157 bracket for the set.
158 If \fIchars\fR
159 contains a sequence of the form \fIa\fB\-\fIb\fR then any
160 character between \fIa\fR and \fIb\fR (inclusive) will match.
161 If the first or last character between the brackets is a \fB\-\fR, then
162 it is treated as part of \fIchars\fR rather than indicating a range.
163 .TP 10
164 \fB[^\fIchars\fB]\fR
165 The input field consists of any number of characters not in 
166 \fIchars\fR.
167 The matching string is stored in the variable.
168 If the character immediately following the \fB^\fR is a \fB]\fR then it is 
169 treated as part of the set rather than the closing bracket for 
170 the set.
171 If \fIchars\fR
172 contains a sequence of the form \fIa\fB\-\fIb\fR then any
173 character between \fIa\fR and \fIb\fR (inclusive) will be excluded
174 from the set.
175 If the first or last character between the brackets is a \fB\-\fR, then
176 it is treated as part of \fIchars\fR rather than indicating a range.
177 .TP 10
178 \fBn\fR
179 No input is consumed from the input string.  Instead, the total number
180 of characters scanned from the input string so far is stored in the variable.
181 .LP
182 The number of characters read from the input for a conversion is the
183 largest number that makes sense for that particular conversion (e.g.
184 as many decimal digits as possible for \fB%d\fR, as 
185 many octal digits as possible for \fB%o\fR, and so on).
186 The input field for a given conversion terminates either when a
187 white-space character is encountered or when the maximum field 
188 width has been reached, whichever comes first.
189 If a \fB*\fR is present in the conversion specifier 
190 then no variable is assigned and the next scan argument is not consumed.
191
192 .SH "DIFFERENCES FROM ANSI SSCANF"
193 .PP
194 The behavior of the \fBscan\fR command is the same as the behavior of
195 the ANSI C \fBsscanf\fR procedure except for the following differences:
196 .IP [1]
197 \fB%p\fR conversion specifier is not currently supported.
198 .IP [2]
199 For \fB%c\fR conversions a single character value is
200 converted to a decimal string, which is then assigned to the
201 corresponding \fIvarName\fR;
202 no field width may be specified for this conversion.
203 .IP [3]
204 .VS 8.4
205 The \fBh\fR modifier is always ignored and the \fBl\fR and \fBL\fR
206 modifiers are ignored when converting real values (i.e. type
207 \fBdouble\fR is used for the internal representation).
208 .VE 8.4
209 .IP [4]
210 If the end of the input string is reached before any conversions have been
211 performed and no variables are given, an empty string is returned.
212
213 .SH "SEE ALSO"
214 format(n), sscanf(3)
215
216 .SH KEYWORDS
217 conversion specifier, parse, scan