Commit | Line | Data |
---|---|---|
04e98a4d AD |
1 | /* dquote_static.c |
2 | * | |
3efe3cb8 | 3 | * This file contains static functions that are related to |
04e98a4d AD |
4 | * parsing double-quotish expressions, but are used in more than |
5 | * one file. | |
6 | * | |
7 | * It is currently #included by regcomp.c and toke.c. | |
8 | */ | |
9 | ||
881ffab6 | 10 | #define PERL_IN_DQUOTE_STATIC_C |
881ffab6 KW |
11 | #include "embed.h" |
12 | ||
04e98a4d AD |
13 | /* |
14 | - regcurly - a little FSA that accepts {\d+,?\d*} | |
15 | Pulled from regcomp.c. | |
16 | */ | |
04e98a4d | 17 | PERL_STATIC_INLINE I32 |
ddeaf645 | 18 | S_regcurly(const char *s) |
04e98a4d | 19 | { |
881ffab6 | 20 | PERL_ARGS_ASSERT_REGCURLY; |
04e98a4d AD |
21 | |
22 | if (*s++ != '{') | |
23 | return FALSE; | |
24 | if (!isDIGIT(*s)) | |
25 | return FALSE; | |
26 | while (isDIGIT(*s)) | |
27 | s++; | |
2ec31dd9 | 28 | if (*s == ',') { |
04e98a4d | 29 | s++; |
2ec31dd9 KW |
30 | while (isDIGIT(*s)) |
31 | s++; | |
32 | } | |
4d68ffa0 | 33 | |
412f55bb | 34 | return *s == '}'; |
04e98a4d | 35 | } |
db30362b | 36 | |
68b355dd KW |
37 | /* XXX Add documentation after final interface and behavior is decided */ |
38 | /* May want to show context for error, so would pass Perl_bslash_c(pTHX_ const char* current, const char* start, const bool output_warning) | |
39 | U8 source = *current; | |
68b355dd KW |
40 | */ |
41 | ||
42 | STATIC char | |
421e43ba | 43 | S_grok_bslash_c(pTHX_ const char source, const bool output_warning) |
68b355dd KW |
44 | { |
45 | ||
46 | U8 result; | |
47 | ||
421e43ba | 48 | if (! isPRINT_A(source)) { |
7357bd17 KW |
49 | Perl_croak(aTHX_ "%s", |
50 | "Character following \"\\c\" must be printable ASCII"); | |
68b355dd | 51 | } |
421e43ba | 52 | else if (source == '{') { |
a27ed980 KW |
53 | const char control = toCTRL('{'); |
54 | if (isPRINT_A(control)) { | |
55 | /* diag_listed_as: Use "%s" instead of "%s" */ | |
56 | Perl_croak(aTHX_ "Use \"%c\" instead of \"\\c{\"", control); | |
57 | } | |
58 | else { | |
59 | Perl_croak(aTHX_ "Sequence \"\\c{\" invalid"); | |
60 | } | |
421e43ba | 61 | } |
68b355dd KW |
62 | |
63 | result = toCTRL(source); | |
5e784d58 | 64 | if (output_warning && isPRINT_A(result)) { |
4d8be631 KW |
65 | U8 clearer[3]; |
66 | U8 i = 0; | |
67 | if (! isWORDCHAR(result)) { | |
68 | clearer[i++] = '\\'; | |
69 | } | |
70 | clearer[i++] = result; | |
71 | clearer[i++] = '\0'; | |
68b355dd | 72 | |
4d8be631 KW |
73 | Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX), |
74 | "\"\\c%c\" is more clearly written simply as \"%s\"", | |
75 | source, | |
76 | clearer); | |
68b355dd KW |
77 | } |
78 | ||
79 | return result; | |
80 | } | |
81 | ||
db30362b | 82 | STATIC bool |
00ce5563 | 83 | S_grok_bslash_o(pTHX_ char **s, UV *uv, const char** error_msg, |
80f4111b | 84 | const bool output_warning, const bool strict, |
17896a85 | 85 | const bool silence_non_portable, |
80f4111b | 86 | const bool UTF) |
db30362b KW |
87 | { |
88 | ||
89 | /* Documentation to be supplied when interface nailed down finally | |
90 | * This returns FALSE if there is an error which the caller need not recover | |
91 | * from; , otherwise TRUE. In either case the caller should look at *len | |
92 | * On input: | |
00ce5563 KW |
93 | * s is the address of a pointer to a NULL terminated string that begins |
94 | * with 'o', and the previous character was a backslash. At exit, *s | |
95 | * will be advanced to the byte just after those absorbed by this | |
96 | * function. Hence the caller can continue parsing from there. In | |
97 | * the case of an error, this routine has generally positioned *s to | |
98 | * point just to the right of the first bad spot, so that a message | |
99 | * that has a "<--" to mark the spot will be correctly positioned. | |
db30362b KW |
100 | * uv points to a UV that will hold the output value, valid only if the |
101 | * return from the function is TRUE | |
db30362b KW |
102 | * error_msg is a pointer that will be set to an internal buffer giving an |
103 | * error message upon failure (the return is FALSE). Untouched if | |
104 | * function succeeds | |
105 | * output_warning says whether to output any warning messages, or suppress | |
106 | * them | |
80f4111b KW |
107 | * strict is true if this should fail instead of warn if there are |
108 | * non-octal digits within the braces | |
17896a85 KW |
109 | * silence_non_portable is true if to suppress warnings about the code |
110 | * point returned being too large to fit on all platforms. | |
80f4111b | 111 | * UTF is true iff the string *s is encoded in UTF-8. |
db30362b | 112 | */ |
00ce5563 | 113 | char* e; |
db30362b KW |
114 | STRLEN numbers_len; |
115 | I32 flags = PERL_SCAN_ALLOW_UNDERSCORES | |
116 | | PERL_SCAN_DISALLOW_PREFIX | |
117 | /* XXX Until the message is improved in grok_oct, handle errors | |
118 | * ourselves */ | |
119 | | PERL_SCAN_SILENT_ILLDIGIT; | |
120 | ||
121 | PERL_ARGS_ASSERT_GROK_BSLASH_O; | |
122 | ||
123 | ||
00ce5563 KW |
124 | assert(**s == 'o'); |
125 | (*s)++; | |
db30362b | 126 | |
00ce5563 | 127 | if (**s != '{') { |
db30362b KW |
128 | *error_msg = "Missing braces on \\o{}"; |
129 | return FALSE; | |
130 | } | |
131 | ||
00ce5563 | 132 | e = strchr(*s, '}'); |
db30362b | 133 | if (!e) { |
00ce5563 | 134 | (*s)++; /* Move past the '{' */ |
b8de99ca KW |
135 | while (isOCTAL(**s)) { /* Position beyond the legal digits */ |
136 | (*s)++; | |
137 | } | |
00ce5563 | 138 | *error_msg = "Missing right brace on \\o{"; |
db30362b KW |
139 | return FALSE; |
140 | } | |
141 | ||
00ce5563 KW |
142 | (*s)++; /* Point to expected first digit (could be first byte of utf8 |
143 | sequence if not a digit) */ | |
144 | numbers_len = e - *s; | |
db30362b | 145 | if (numbers_len == 0) { |
00ce5563 | 146 | (*s)++; /* Move past the } */ |
db30362b KW |
147 | *error_msg = "Number with no digits"; |
148 | return FALSE; | |
149 | } | |
150 | ||
17896a85 KW |
151 | if (silence_non_portable) { |
152 | flags |= PERL_SCAN_SILENT_NON_PORTABLE; | |
153 | } | |
154 | ||
00ce5563 | 155 | *uv = grok_oct(*s, &numbers_len, &flags, NULL); |
db30362b KW |
156 | /* Note that if has non-octal, will ignore everything starting with that up |
157 | * to the '}' */ | |
158 | ||
80f4111b KW |
159 | if (numbers_len != (STRLEN) (e - *s)) { |
160 | if (strict) { | |
161 | *s += numbers_len; | |
162 | *s += (UTF) ? UTF8SKIP(*s) : (STRLEN) 1; | |
163 | *error_msg = "Non-octal character"; | |
164 | return FALSE; | |
165 | } | |
166 | else if (output_warning) { | |
b67d718a KW |
167 | Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT), |
168 | /* diag_listed_as: Non-octal character '%c'. Resolved as "%s" */ | |
169 | "Non-octal character '%c'. Resolved as \"\\o{%.*s}\"", | |
170 | *(*s + numbers_len), | |
171 | (int) numbers_len, | |
172 | *s); | |
80f4111b | 173 | } |
db30362b KW |
174 | } |
175 | ||
00ce5563 KW |
176 | /* Return past the '}' */ |
177 | *s = e + 1; | |
178 | ||
db30362b KW |
179 | return TRUE; |
180 | } | |
181 | ||
a0481293 | 182 | PERL_STATIC_INLINE bool |
00ce5563 | 183 | S_grok_bslash_x(pTHX_ char **s, UV *uv, const char** error_msg, |
80f4111b | 184 | const bool output_warning, const bool strict, |
17896a85 | 185 | const bool silence_non_portable, |
80f4111b | 186 | const bool UTF) |
a0481293 KW |
187 | { |
188 | ||
189 | /* Documentation to be supplied when interface nailed down finally | |
190 | * This returns FALSE if there is an error which the caller need not recover | |
f3c7620a | 191 | * from; , otherwise TRUE. |
a0481293 | 192 | * On input: |
00ce5563 KW |
193 | * s is the address of a pointer to a NULL terminated string that begins |
194 | * with 'x', and the previous character was a backslash. At exit, *s | |
195 | * will be advanced to the byte just after those absorbed by this | |
196 | * function. Hence the caller can continue parsing from there. In | |
197 | * the case of an error, this routine has generally positioned *s to | |
198 | * point just to the right of the first bad spot, so that a message | |
199 | * that has a "<--" to mark the spot will be correctly positioned. | |
a0481293 KW |
200 | * uv points to a UV that will hold the output value, valid only if the |
201 | * return from the function is TRUE | |
a0481293 KW |
202 | * error_msg is a pointer that will be set to an internal buffer giving an |
203 | * error message upon failure (the return is FALSE). Untouched if | |
204 | * function succeeds | |
205 | * output_warning says whether to output any warning messages, or suppress | |
206 | * them | |
80f4111b KW |
207 | * strict is true if anything out of the ordinary should cause this to |
208 | * fail instead of warn or be silent. For example, it requires | |
209 | * exactly 2 digits following the \x (when there are no braces). | |
210 | * 3 digits could be a mistake, so is forbidden in this mode. | |
344451ec KW |
211 | * silence_non_portable is true if to suppress warnings about the code |
212 | * point returned being too large to fit on all platforms. | |
80f4111b | 213 | * UTF is true iff the string *s is encoded in UTF-8. |
a0481293 | 214 | */ |
00ce5563 | 215 | char* e; |
a0481293 | 216 | STRLEN numbers_len; |
344451ec | 217 | I32 flags = PERL_SCAN_DISALLOW_PREFIX; |
a0481293 KW |
218 | |
219 | PERL_ARGS_ASSERT_GROK_BSLASH_X; | |
220 | ||
4f8dbb2d | 221 | PERL_UNUSED_ARG(output_warning); |
a0481293 | 222 | |
00ce5563 KW |
223 | assert(**s == 'x'); |
224 | (*s)++; | |
a0481293 | 225 | |
879eb604 | 226 | if (strict || ! output_warning) { |
80f4111b KW |
227 | flags |= PERL_SCAN_SILENT_ILLDIGIT; |
228 | } | |
229 | ||
00ce5563 | 230 | if (**s != '{') { |
80f4111b KW |
231 | STRLEN len = (strict) ? 3 : 2; |
232 | ||
00ce5563 KW |
233 | *uv = grok_hex(*s, &len, &flags, NULL); |
234 | *s += len; | |
80f4111b KW |
235 | if (strict && len != 2) { |
236 | if (len < 2) { | |
237 | *s += (UTF) ? UTF8SKIP(*s) : 1; | |
238 | *error_msg = "Non-hex character"; | |
239 | } | |
240 | else { | |
241 | *error_msg = "Use \\x{...} for more than two hex characters"; | |
242 | } | |
243 | return FALSE; | |
244 | } | |
a0481293 KW |
245 | return TRUE; |
246 | } | |
247 | ||
00ce5563 | 248 | e = strchr(*s, '}'); |
a0481293 | 249 | if (!e) { |
00ce5563 | 250 | (*s)++; /* Move past the '{' */ |
b8de99ca KW |
251 | while (isXDIGIT(**s)) { /* Position beyond the legal digits */ |
252 | (*s)++; | |
253 | } | |
a0481293 KW |
254 | /* XXX The corresponding message above for \o is just '\\o{'; other |
255 | * messages for other constructs include the '}', so are inconsistent. | |
256 | */ | |
257 | *error_msg = "Missing right brace on \\x{}"; | |
258 | return FALSE; | |
259 | } | |
260 | ||
00ce5563 KW |
261 | (*s)++; /* Point to expected first digit (could be first byte of utf8 |
262 | sequence if not a digit) */ | |
263 | numbers_len = e - *s; | |
80f4111b KW |
264 | if (numbers_len == 0) { |
265 | if (strict) { | |
266 | (*s)++; /* Move past the } */ | |
267 | *error_msg = "Number with no digits"; | |
268 | return FALSE; | |
269 | } | |
270 | return TRUE; | |
271 | } | |
272 | ||
344451ec | 273 | flags |= PERL_SCAN_ALLOW_UNDERSCORES; |
17896a85 KW |
274 | if (silence_non_portable) { |
275 | flags |= PERL_SCAN_SILENT_NON_PORTABLE; | |
276 | } | |
80f4111b | 277 | |
00ce5563 | 278 | *uv = grok_hex(*s, &numbers_len, &flags, NULL); |
a0481293 KW |
279 | /* Note that if has non-hex, will ignore everything starting with that up |
280 | * to the '}' */ | |
281 | ||
80f4111b KW |
282 | if (strict && numbers_len != (STRLEN) (e - *s)) { |
283 | *s += numbers_len; | |
284 | *s += (UTF) ? UTF8SKIP(*s) : 1; | |
285 | *error_msg = "Non-hex character"; | |
286 | return FALSE; | |
287 | } | |
288 | ||
00ce5563 KW |
289 | /* Return past the '}' */ |
290 | *s = e + 1; | |
291 | ||
a0481293 KW |
292 | return TRUE; |
293 | } | |
294 | ||
5e0a247b KW |
295 | STATIC char* |
296 | S_form_short_octal_warning(pTHX_ | |
297 | const char * const s, /* Points to first non-octal */ | |
298 | const STRLEN len /* Length of octals string, so | |
299 | (s-len) points to first | |
300 | octal */ | |
301 | ) { | |
302 | /* Return a character string consisting of a warning message for when a | |
303 | * string constant in octal is weird, like "\078". */ | |
304 | ||
305 | const char * sans_leading_zeros = s - len; | |
306 | ||
307 | PERL_ARGS_ASSERT_FORM_SHORT_OCTAL_WARNING; | |
308 | ||
309 | assert(*s == '8' || *s == '9'); | |
310 | ||
311 | /* Remove the leading zeros, retaining one zero so won't be zero length */ | |
312 | while (*sans_leading_zeros == '0') sans_leading_zeros++; | |
313 | if (sans_leading_zeros == s) { | |
314 | sans_leading_zeros--; | |
315 | } | |
316 | ||
317 | return Perl_form(aTHX_ | |
318 | "'%.*s' resolved to '\\o{%.*s}%c'", | |
319 | (int) (len + 2), s - len - 1, | |
320 | (int) (s - sans_leading_zeros), sans_leading_zeros, | |
321 | *s); | |
322 | } | |
323 | ||
04e98a4d AD |
324 | /* |
325 | * Local variables: | |
326 | * c-indentation-style: bsd | |
327 | * c-basic-offset: 4 | |
14d04a33 | 328 | * indent-tabs-mode: nil |
04e98a4d AD |
329 | * End: |
330 | * | |
14d04a33 | 331 | * ex: set ts=8 sts=4 sw=4 et: |
04e98a4d | 332 | */ |