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