Commit | Line | Data |
e807ee50 |
1 | /* stolen_chunk_of_toke.c - from perl 5.8.8 toke.c |
2 | * |
3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, |
4 | * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public |
7 | * License or the Artistic License, as specified in the README file. |
8 | * |
9 | */ |
10 | |
11 | /* |
12 | * "It all comes from here, the stench and the peril." --Frodo |
13 | */ |
14 | |
15 | /* |
16 | * this is all blatantly stolen. I sincerely hopes it doesn't fuck anything |
17 | * up but if it does blame me (Matt S Trout), not the poor original authors |
18 | */ |
19 | |
71511651 |
20 | /* the following #defines are stolen from assorted headers, not toke.c (mst) */ |
e807ee50 |
21 | |
78160085 |
22 | #define skipspace(a) S_skipspace(aTHX_ a) |
23 | #define incline(a) S_incline(aTHX_ a) |
24 | #define filter_gets(a,b,c) S_filter_gets(aTHX_ a,b,c) |
25 | #define scan_str(a,b,c) S_scan_str(aTHX_ a,b,c) |
26 | #define scan_word(a,b,c,d,e) S_scan_word(aTHX_ a,b,c,d,e) |
92997e69 |
27 | #define scan_ident(a,b,c,d,e) S_scan_ident(aTHX_ a,b,c,d,e) |
78160085 |
28 | |
29 | STATIC void S_incline(pTHX_ char *s); |
30 | STATIC char* S_skipspace(pTHX_ char *s); |
31 | STATIC char * S_filter_gets(pTHX_ SV *sv, PerlIO *fp, STRLEN append); |
32 | STATIC char* S_scan_str(pTHX_ char *start, int keep_quoted, int keep_delims); |
33 | STATIC char* S_scan_word(pTHX_ char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp); |
34 | |
e807ee50 |
35 | #define DPTR2FPTR(t,p) ((t)PTR2nat(p)) /* data pointer to function pointer */ |
36 | #define FPTR2DPTR(t,p) ((t)PTR2nat(p)) /* function pointer to data pointer */ |
c630715a |
37 | #define PTR2nat(p) (PTRV)(p) /* pointer to integer of PTRSIZE */ |
e807ee50 |
38 | #define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t), |
14ab3472 |
39 | |
40 | /* conditionalise these two because as of 5.9.5 we already get them from |
41 | the headers (mst) */ |
42 | #ifndef Newx |
e807ee50 |
43 | #define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) (t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))) |
14ab3472 |
44 | #endif |
45 | #ifndef SvPVX_const |
0ba8c7aa |
46 | #define SvPVX_const(sv) ((const char*) (0 + SvPVX(sv))) |
14ab3472 |
47 | #endif |
48 | |
0ba8c7aa |
49 | #define SvPV_renew(sv,n) \ |
50 | STMT_START { SvLEN_set(sv, n); \ |
51 | SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \ |
52 | (char*)saferealloc((Malloc_t)SvPVX(sv), \ |
53 | (MEM_SIZE)((n))))); \ |
54 | } STMT_END |
e807ee50 |
55 | |
d6e7537a |
56 | #define isCONTROLVAR(x) (isUPPER(x) || strchr("[\\]^_?", (x))) |
57 | |
c630715a |
58 | /* On MacOS, respect nonbreaking spaces */ |
59 | #ifdef MACOS_TRADITIONAL |
60 | #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\312'||(c)=='\t') |
61 | #else |
62 | #define SPACE_OR_TAB(c) ((c)==' '||(c)=='\t') |
63 | #endif |
e807ee50 |
64 | |
0ba8c7aa |
65 | #define CLINE (PL_copline = (CopLINE(PL_curcop) < PL_copline ? CopLINE(PL_curcop) : PL_copline)) |
66 | |
e807ee50 |
67 | #define LEX_NORMAL 10 /* normal code (ie not within "...") */ |
68 | #define LEX_INTERPNORMAL 9 /* code within a string, eg "$foo[$x+1]" */ |
69 | #define LEX_INTERPCASEMOD 8 /* expecting a \U, \Q or \E etc */ |
70 | #define LEX_INTERPPUSH 7 /* starting a new sublex parse level */ |
71 | #define LEX_INTERPSTART 6 /* expecting the start of a $var */ |
72 | |
73 | /* at end of code, eg "$x" followed by: */ |
74 | #define LEX_INTERPEND 5 /* ... eg not one of [, { or -> */ |
75 | #define LEX_INTERPENDMAYBE 4 /* ... eg one of [, { or -> */ |
76 | |
77 | #define LEX_INTERPCONCAT 3 /* expecting anything, eg at start of |
78 | string or after \E, $foo, etc */ |
79 | #define LEX_INTERPCONST 2 /* NOT USED */ |
80 | #define LEX_FORMLINE 1 /* expecting a format line */ |
81 | #define LEX_KNOWNEXT 0 /* next token known; just return it */ |
82 | |
14ab3472 |
83 | /* and these two are my own madness (mst) */ |
71511651 |
84 | |
85 | #if PERL_REVISION == 5 && PERL_VERSION == 8 && PERL_SUBVERSION >= 8 |
86 | #define PERL_5_8_8_PLUS |
87 | #endif |
88 | |
e38f5a74 |
89 | #if PERL_REVISION == 5 && PERL_VERSION > 8 |
90 | #define PERL_5_9_PLUS |
91 | #endif |
92 | |
93 | #ifdef PERL_5_9_PLUS |
14ab3472 |
94 | /* 5.9+ moves a bunch of things to a PL_parser struct so we need to |
95 | declare the backcompat macros for things to still work (mst) */ |
96 | |
97 | /* XXX temporary backwards compatibility */ |
98 | #define PL_lex_brackets (PL_parser->lex_brackets) |
99 | #define PL_lex_brackstack (PL_parser->lex_brackstack) |
100 | #define PL_lex_casemods (PL_parser->lex_casemods) |
101 | #define PL_lex_casestack (PL_parser->lex_casestack) |
102 | #define PL_lex_defer (PL_parser->lex_defer) |
103 | #define PL_lex_dojoin (PL_parser->lex_dojoin) |
104 | #define PL_lex_expect (PL_parser->lex_expect) |
105 | #define PL_lex_formbrack (PL_parser->lex_formbrack) |
106 | #define PL_lex_inpat (PL_parser->lex_inpat) |
107 | #define PL_lex_inwhat (PL_parser->lex_inwhat) |
108 | #define PL_lex_op (PL_parser->lex_op) |
109 | #define PL_lex_repl (PL_parser->lex_repl) |
110 | #define PL_lex_starts (PL_parser->lex_starts) |
111 | #define PL_lex_stuff (PL_parser->lex_stuff) |
112 | #define PL_multi_start (PL_parser->multi_start) |
113 | #define PL_multi_open (PL_parser->multi_open) |
114 | #define PL_multi_close (PL_parser->multi_close) |
115 | #define PL_pending_ident (PL_parser->pending_ident) |
116 | #define PL_preambled (PL_parser->preambled) |
117 | #define PL_sublex_info (PL_parser->sublex_info) |
118 | #define PL_linestr (PL_parser->linestr) |
119 | #define PL_sublex_info (PL_parser->sublex_info) |
120 | #define PL_linestr (PL_parser->linestr) |
121 | #define PL_expect (PL_parser->expect) |
122 | #define PL_copline (PL_parser->copline) |
123 | #define PL_bufptr (PL_parser->bufptr) |
124 | #define PL_oldbufptr (PL_parser->oldbufptr) |
125 | #define PL_oldoldbufptr (PL_parser->oldoldbufptr) |
126 | #define PL_linestart (PL_parser->linestart) |
127 | #define PL_bufend (PL_parser->bufend) |
128 | #define PL_last_uni (PL_parser->last_uni) |
129 | #define PL_last_lop (PL_parser->last_lop) |
130 | #define PL_last_lop_op (PL_parser->last_lop_op) |
131 | #define PL_lex_state (PL_parser->lex_state) |
132 | #define PL_rsfp (PL_parser->rsfp) |
133 | #define PL_rsfp_filters (PL_parser->rsfp_filters) |
134 | #define PL_in_my (PL_parser->in_my) |
135 | #define PL_in_my_stash (PL_parser->in_my_stash) |
136 | #define PL_tokenbuf (PL_parser->tokenbuf) |
137 | #define PL_multi_end (PL_parser->multi_end) |
138 | #define PL_error_count (PL_parser->error_count) |
139 | /* these three are from the non-PERL_MAD path but I don't -think- I need |
140 | the PERL_MAD stuff since my code isn't really populating things (mst) */ |
141 | # define PL_nexttoke (PL_parser->nexttoke) |
142 | # define PL_nexttype (PL_parser->nexttype) |
143 | # define PL_nextval (PL_parser->nextval) |
144 | /* end of backcompat macros form 5.9 toke.c (mst) */ |
78160085 |
145 | #endif |
14ab3472 |
146 | |
147 | /* when ccflags include -DDEBUGGING we need this for earlier 5.8 perls */ |
148 | #ifndef SvPV_nolen_const |
149 | #define SvPV_nolen_const SvPV_nolen |
150 | #endif |
151 | |
71511651 |
152 | /* and now we're back to the toke.c stuff again (mst) */ |
153 | |
e807ee50 |
154 | static const char ident_too_long[] = |
155 | "Identifier too long"; |
156 | static const char c_without_g[] = |
157 | "Use of /c modifier is meaningless without /g"; |
158 | static const char c_in_subst[] = |
159 | "Use of /c modifier is meaningless in s///"; |
160 | |
161 | #ifdef USE_UTF8_SCRIPTS |
162 | # define UTF (!IN_BYTES) |
163 | #else |
164 | # define UTF ((PL_linestr && DO_UTF8(PL_linestr)) || (PL_hints & HINT_UTF8)) |
165 | #endif |
166 | |
167 | /* Invoke the idxth filter function for the current rsfp. */ |
168 | /* maxlen 0 = read one text line */ |
169 | I32 |
170 | Perl_filter_read(pTHX_ int idx, SV *buf_sv, int maxlen) |
171 | { |
172 | filter_t funcp; |
173 | SV *datasv = NULL; |
174 | |
175 | if (!PL_rsfp_filters) |
176 | return -1; |
177 | if (idx > AvFILLp(PL_rsfp_filters)) { /* Any more filters? */ |
178 | /* Provide a default input filter to make life easy. */ |
179 | /* Note that we append to the line. This is handy. */ |
180 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
181 | "filter_read %d: from rsfp\n", idx)); |
182 | if (maxlen) { |
183 | /* Want a block */ |
184 | int len ; |
185 | const int old_len = SvCUR(buf_sv); |
186 | |
187 | /* ensure buf_sv is large enough */ |
188 | SvGROW(buf_sv, (STRLEN)(old_len + maxlen)) ; |
189 | if ((len = PerlIO_read(PL_rsfp, SvPVX(buf_sv) + old_len, maxlen)) <= 0){ |
190 | if (PerlIO_error(PL_rsfp)) |
191 | return -1; /* error */ |
192 | else |
193 | return 0 ; /* end of file */ |
194 | } |
195 | SvCUR_set(buf_sv, old_len + len) ; |
196 | } else { |
197 | /* Want a line */ |
198 | if (sv_gets(buf_sv, PL_rsfp, SvCUR(buf_sv)) == NULL) { |
199 | if (PerlIO_error(PL_rsfp)) |
200 | return -1; /* error */ |
201 | else |
202 | return 0 ; /* end of file */ |
203 | } |
204 | } |
205 | return SvCUR(buf_sv); |
206 | } |
207 | /* Skip this filter slot if filter has been deleted */ |
208 | if ( (datasv = FILTER_DATA(idx)) == &PL_sv_undef) { |
209 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
210 | "filter_read %d: skipped (filter deleted)\n", |
211 | idx)); |
212 | return FILTER_READ(idx+1, buf_sv, maxlen); /* recurse */ |
213 | } |
214 | /* Get function pointer hidden within datasv */ |
215 | funcp = DPTR2FPTR(filter_t, IoANY(datasv)); |
216 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
217 | "filter_read %d: via function %p (%s)\n", |
218 | idx, datasv, SvPV_nolen_const(datasv))); |
219 | /* Call function. The function is expected to */ |
220 | /* call "FILTER_READ(idx+1, buf_sv)" first. */ |
221 | /* Return: <0:error, =0:eof, >0:not eof */ |
222 | return (*funcp)(aTHX_ idx, buf_sv, maxlen); |
223 | } |
224 | |
225 | STATIC char * |
226 | S_filter_gets(pTHX_ register SV *sv, register PerlIO *fp, STRLEN append) |
227 | { |
228 | #ifdef PERL_CR_FILTER |
229 | if (!PL_rsfp_filters) { |
230 | filter_add(S_cr_textfilter,NULL); |
231 | } |
232 | #endif |
233 | if (PL_rsfp_filters) { |
234 | if (!append) |
235 | SvCUR_set(sv, 0); /* start with empty line */ |
236 | if (FILTER_READ(0, sv, 0) > 0) |
237 | return ( SvPVX(sv) ) ; |
238 | else |
239 | return Nullch ; |
240 | } |
241 | else |
242 | return (sv_gets(sv, fp, append)); |
243 | } |
244 | |
245 | /* |
246 | * S_skipspace |
247 | * Called to gobble the appropriate amount and type of whitespace. |
248 | * Skips comments as well. |
249 | */ |
250 | |
251 | STATIC char * |
252 | S_skipspace(pTHX_ register char *s) |
253 | { |
254 | if (PL_lex_formbrack && PL_lex_brackets <= PL_lex_formbrack) { |
255 | while (s < PL_bufend && SPACE_OR_TAB(*s)) |
256 | s++; |
257 | return s; |
258 | } |
259 | for (;;) { |
260 | STRLEN prevlen; |
261 | SSize_t oldprevlen, oldoldprevlen; |
262 | SSize_t oldloplen = 0, oldunilen = 0; |
263 | while (s < PL_bufend && isSPACE(*s)) { |
264 | if (*s++ == '\n' && PL_in_eval && !PL_rsfp) |
265 | incline(s); |
266 | } |
267 | |
268 | /* comment */ |
269 | if (s < PL_bufend && *s == '#') { |
270 | while (s < PL_bufend && *s != '\n') |
271 | s++; |
272 | if (s < PL_bufend) { |
273 | s++; |
274 | if (PL_in_eval && !PL_rsfp) { |
275 | incline(s); |
276 | continue; |
277 | } |
278 | } |
279 | } |
280 | |
281 | /* only continue to recharge the buffer if we're at the end |
282 | * of the buffer, we're not reading from a source filter, and |
283 | * we're in normal lexing mode |
284 | */ |
285 | if (s < PL_bufend || !PL_rsfp || PL_sublex_info.sub_inwhat || |
286 | PL_lex_state == LEX_FORMLINE) |
287 | return s; |
288 | |
289 | /* try to recharge the buffer */ |
290 | if ((s = filter_gets(PL_linestr, PL_rsfp, |
291 | (prevlen = SvCUR(PL_linestr)))) == Nullch) |
292 | { |
293 | /* end of file. Add on the -p or -n magic */ |
294 | if (PL_minus_p) { |
295 | sv_setpv(PL_linestr, |
296 | ";}continue{print or die qq(-p destination: $!\\n);}"); |
297 | PL_minus_n = PL_minus_p = 0; |
298 | } |
299 | else if (PL_minus_n) { |
300 | sv_setpvn(PL_linestr, ";}", 2); |
301 | PL_minus_n = 0; |
302 | } |
303 | else |
304 | sv_setpvn(PL_linestr,";", 1); |
305 | |
306 | /* reset variables for next time we lex */ |
307 | PL_oldoldbufptr = PL_oldbufptr = PL_bufptr = s = PL_linestart |
308 | = SvPVX(PL_linestr); |
309 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); |
310 | PL_last_lop = PL_last_uni = Nullch; |
311 | |
35fc8ada |
312 | /* In perl versions previous to p4-rawid: //depot/perl@32954 -P |
313 | * preprocessors were supported here. We don't support -P at all, even |
314 | * on perls that support it, and use the following chunk from blead |
315 | * perl. (rafl) |
316 | */ |
317 | |
318 | /* Close the filehandle. Could be from |
e807ee50 |
319 | * STDIN, or a regular file. If we were reading code from |
320 | * STDIN (because the commandline held no -e or filename) |
321 | * then we don't close it, we reset it so the code can |
322 | * read from STDIN too. |
323 | */ |
324 | |
35fc8ada |
325 | if ((PerlIO*)PL_rsfp == PerlIO_stdin()) |
e807ee50 |
326 | PerlIO_clearerr(PL_rsfp); |
327 | else |
328 | (void)PerlIO_close(PL_rsfp); |
329 | PL_rsfp = Nullfp; |
330 | return s; |
331 | } |
332 | |
333 | /* not at end of file, so we only read another line */ |
334 | /* make corresponding updates to old pointers, for yyerror() */ |
335 | oldprevlen = PL_oldbufptr - PL_bufend; |
336 | oldoldprevlen = PL_oldoldbufptr - PL_bufend; |
337 | if (PL_last_uni) |
338 | oldunilen = PL_last_uni - PL_bufend; |
339 | if (PL_last_lop) |
340 | oldloplen = PL_last_lop - PL_bufend; |
341 | PL_linestart = PL_bufptr = s + prevlen; |
342 | PL_bufend = s + SvCUR(PL_linestr); |
343 | s = PL_bufptr; |
344 | PL_oldbufptr = s + oldprevlen; |
345 | PL_oldoldbufptr = s + oldoldprevlen; |
346 | if (PL_last_uni) |
347 | PL_last_uni = s + oldunilen; |
348 | if (PL_last_lop) |
349 | PL_last_lop = s + oldloplen; |
350 | incline(s); |
351 | |
352 | /* debugger active and we're not compiling the debugger code, |
353 | * so store the line into the debugger's array of lines |
354 | */ |
355 | if (PERLDB_LINE && PL_curstash != PL_debstash) { |
356 | SV * const sv = NEWSV(85,0); |
357 | |
358 | sv_upgrade(sv, SVt_PVMG); |
359 | sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr); |
360 | (void)SvIOK_on(sv); |
361 | SvIV_set(sv, 0); |
362 | av_store(CopFILEAV(PL_curcop),(I32)CopLINE(PL_curcop),sv); |
363 | } |
364 | } |
365 | } |
366 | |
367 | STATIC char * |
368 | S_scan_word(pTHX_ register char *s, char *dest, STRLEN destlen, int allow_package, STRLEN *slp) |
369 | { |
370 | register char *d = dest; |
371 | register char * const e = d + destlen - 3; /* two-character token, ending NUL */ |
372 | for (;;) { |
373 | if (d >= e) |
374 | Perl_croak(aTHX_ ident_too_long); |
375 | if (isALNUM(*s)) /* UTF handled below */ |
376 | *d++ = *s++; |
377 | else if (*s == '\'' && allow_package && isIDFIRST_lazy_if(s+1,UTF)) { |
378 | *d++ = ':'; |
379 | *d++ = ':'; |
380 | s++; |
381 | } |
382 | else if (*s == ':' && s[1] == ':' && allow_package && s[2] != '$') { |
383 | *d++ = *s++; |
384 | *d++ = *s++; |
385 | } |
386 | else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) { |
387 | char *t = s + UTF8SKIP(s); |
388 | while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t)) |
389 | t += UTF8SKIP(t); |
390 | if (d + (t - s) > e) |
391 | Perl_croak(aTHX_ ident_too_long); |
392 | Copy(s, d, t - s, char); |
393 | d += t - s; |
394 | s = t; |
395 | } |
396 | else { |
397 | *d = '\0'; |
398 | *slp = d - dest; |
399 | return s; |
400 | } |
401 | } |
402 | } |
403 | |
404 | /* |
405 | * S_incline |
406 | * This subroutine has nothing to do with tilting, whether at windmills |
407 | * or pinball tables. Its name is short for "increment line". It |
408 | * increments the current line number in CopLINE(PL_curcop) and checks |
409 | * to see whether the line starts with a comment of the form |
410 | * # line 500 "foo.pm" |
411 | * If so, it sets the current line number and file to the values in the comment. |
412 | */ |
413 | |
414 | STATIC void |
78160085 |
415 | S_incline(pTHX_ char *s) |
e807ee50 |
416 | { |
417 | char *t; |
78160085 |
418 | char *n; |
419 | char *e; |
e807ee50 |
420 | char ch; |
421 | |
422 | CopLINE_inc(PL_curcop); |
423 | if (*s++ != '#') |
424 | return; |
425 | while (SPACE_OR_TAB(*s)) s++; |
426 | if (strnEQ(s, "line", 4)) |
427 | s += 4; |
428 | else |
429 | return; |
430 | if (SPACE_OR_TAB(*s)) |
431 | s++; |
432 | else |
433 | return; |
434 | while (SPACE_OR_TAB(*s)) s++; |
435 | if (!isDIGIT(*s)) |
436 | return; |
437 | n = s; |
438 | while (isDIGIT(*s)) |
439 | s++; |
440 | while (SPACE_OR_TAB(*s)) |
441 | s++; |
442 | if (*s == '"' && (t = strchr(s+1, '"'))) { |
443 | s++; |
444 | e = t + 1; |
445 | } |
446 | else { |
78160085 |
447 | for (t = s; !isSPACE(*t); t++) ; |
e807ee50 |
448 | e = t; |
449 | } |
450 | while (SPACE_OR_TAB(*e) || *e == '\r' || *e == '\f') |
451 | e++; |
452 | if (*e != '\n' && *e != '\0') |
453 | return; /* false alarm */ |
454 | |
455 | ch = *t; |
456 | *t = '\0'; |
457 | if (t - s > 0) { |
71511651 |
458 | /* this chunk was added to S_incline during 5.8.8. I don't know why but I don't |
459 | honestly care since I probably want to be bug-compatible anyway (mst) */ |
460 | |
461 | /* ... my kingdom for a perl parser in perl ... (mst) */ |
462 | |
463 | #ifdef PERL_5_8_8_PLUS |
e807ee50 |
464 | #ifndef USE_ITHREADS |
465 | const char *cf = CopFILE(PL_curcop); |
466 | if (cf && strlen(cf) > 7 && strnEQ(cf, "(eval ", 6)) { |
467 | /* must copy *{"::_<(eval N)[oldfilename:L]"} |
468 | * to *{"::_<newfilename"} */ |
469 | char smallbuf[256], smallbuf2[256]; |
470 | char *tmpbuf, *tmpbuf2; |
471 | GV **gvp, *gv2; |
472 | STRLEN tmplen = strlen(cf); |
473 | STRLEN tmplen2 = strlen(s); |
474 | if (tmplen + 3 < sizeof smallbuf) |
475 | tmpbuf = smallbuf; |
476 | else |
477 | Newx(tmpbuf, tmplen + 3, char); |
478 | if (tmplen2 + 3 < sizeof smallbuf2) |
479 | tmpbuf2 = smallbuf2; |
480 | else |
481 | Newx(tmpbuf2, tmplen2 + 3, char); |
482 | tmpbuf[0] = tmpbuf2[0] = '_'; |
483 | tmpbuf[1] = tmpbuf2[1] = '<'; |
484 | memcpy(tmpbuf + 2, cf, ++tmplen); |
485 | memcpy(tmpbuf2 + 2, s, ++tmplen2); |
486 | ++tmplen; ++tmplen2; |
487 | gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, FALSE); |
488 | if (gvp) { |
489 | gv2 = *(GV**)hv_fetch(PL_defstash, tmpbuf2, tmplen2, TRUE); |
490 | if (!isGV(gv2)) |
491 | gv_init(gv2, PL_defstash, tmpbuf2, tmplen2, FALSE); |
492 | /* adjust ${"::_<newfilename"} to store the new file name */ |
493 | GvSV(gv2) = newSVpvn(tmpbuf2 + 2, tmplen2 - 2); |
494 | GvHV(gv2) = (HV*)SvREFCNT_inc(GvHV(*gvp)); |
495 | GvAV(gv2) = (AV*)SvREFCNT_inc(GvAV(*gvp)); |
496 | } |
497 | if (tmpbuf != smallbuf) Safefree(tmpbuf); |
498 | if (tmpbuf2 != smallbuf2) Safefree(tmpbuf2); |
499 | } |
500 | #endif |
71511651 |
501 | #endif |
502 | /* second endif closes out the "are we 5.8.(8+)" conditional */ |
e807ee50 |
503 | CopFILE_free(PL_curcop); |
504 | CopFILE_set(PL_curcop, s); |
505 | } |
506 | *t = ch; |
507 | CopLINE_set(PL_curcop, atoi(n)-1); |
508 | } |
0ba8c7aa |
509 | |
510 | /* scan_str |
511 | takes: start position in buffer |
512 | keep_quoted preserve \ on the embedded delimiter(s) |
513 | keep_delims preserve the delimiters around the string |
514 | returns: position to continue reading from buffer |
515 | side-effects: multi_start, multi_close, lex_repl or lex_stuff, and |
516 | updates the read buffer. |
517 | |
518 | This subroutine pulls a string out of the input. It is called for: |
519 | q single quotes q(literal text) |
520 | ' single quotes 'literal text' |
521 | qq double quotes qq(interpolate $here please) |
522 | " double quotes "interpolate $here please" |
523 | qx backticks qx(/bin/ls -l) |
524 | ` backticks `/bin/ls -l` |
525 | qw quote words @EXPORT_OK = qw( func() $spam ) |
526 | m// regexp match m/this/ |
527 | s/// regexp substitute s/this/that/ |
528 | tr/// string transliterate tr/this/that/ |
529 | y/// string transliterate y/this/that/ |
530 | ($*@) sub prototypes sub foo ($) |
531 | (stuff) sub attr parameters sub foo : attr(stuff) |
532 | <> readline or globs <FOO>, <>, <$fh>, or <*.c> |
533 | |
534 | In most of these cases (all but <>, patterns and transliterate) |
535 | yylex() calls scan_str(). m// makes yylex() call scan_pat() which |
536 | calls scan_str(). s/// makes yylex() call scan_subst() which calls |
537 | scan_str(). tr/// and y/// make yylex() call scan_trans() which |
538 | calls scan_str(). |
539 | |
540 | It skips whitespace before the string starts, and treats the first |
541 | character as the delimiter. If the delimiter is one of ([{< then |
542 | the corresponding "close" character )]}> is used as the closing |
543 | delimiter. It allows quoting of delimiters, and if the string has |
544 | balanced delimiters ([{<>}]) it allows nesting. |
545 | |
546 | On success, the SV with the resulting string is put into lex_stuff or, |
547 | if that is already non-NULL, into lex_repl. The second case occurs only |
548 | when parsing the RHS of the special constructs s/// and tr/// (y///). |
549 | For convenience, the terminating delimiter character is stuffed into |
550 | SvIVX of the SV. |
551 | */ |
552 | |
553 | STATIC char * |
554 | S_scan_str(pTHX_ char *start, int keep_quoted, int keep_delims) |
555 | { |
556 | SV *sv; /* scalar value: string */ |
557 | char *tmps; /* temp string, used for delimiter matching */ |
558 | register char *s = start; /* current position in the buffer */ |
559 | register char term; /* terminating character */ |
560 | register char *to; /* current position in the sv's data */ |
561 | I32 brackets = 1; /* bracket nesting level */ |
562 | bool has_utf8 = FALSE; /* is there any utf8 content? */ |
563 | I32 termcode; /* terminating char. code */ |
71511651 |
564 | /* 5.8.7+ uses UTF8_MAXBYTES but also its utf8.h defs _MAXLEN to it so |
565 | I'm reasonably hopeful this won't destroy anything (mst) */ |
566 | U8 termstr[UTF8_MAXLEN]; /* terminating string */ |
0ba8c7aa |
567 | STRLEN termlen; /* length of terminating string */ |
568 | char *last = NULL; /* last position for nesting bracket */ |
569 | |
570 | /* skip space before the delimiter */ |
571 | if (isSPACE(*s)) |
572 | s = skipspace(s); |
573 | |
574 | /* mark where we are, in case we need to report errors */ |
575 | CLINE; |
576 | |
577 | /* after skipping whitespace, the next character is the terminator */ |
578 | term = *s; |
579 | if (!UTF) { |
580 | termcode = termstr[0] = term; |
581 | termlen = 1; |
582 | } |
583 | else { |
584 | termcode = utf8_to_uvchr((U8*)s, &termlen); |
585 | Copy(s, termstr, termlen, U8); |
586 | if (!UTF8_IS_INVARIANT(term)) |
587 | has_utf8 = TRUE; |
588 | } |
589 | |
590 | /* mark where we are */ |
591 | PL_multi_start = CopLINE(PL_curcop); |
592 | PL_multi_open = term; |
593 | |
594 | /* find corresponding closing delimiter */ |
595 | if (term && (tmps = strchr("([{< )]}> )]}>",term))) |
596 | termcode = termstr[0] = term = tmps[5]; |
597 | |
598 | PL_multi_close = term; |
599 | |
600 | /* create a new SV to hold the contents. 87 is leak category, I'm |
601 | assuming. 79 is the SV's initial length. What a random number. */ |
602 | sv = NEWSV(87,79); |
603 | sv_upgrade(sv, SVt_PVIV); |
604 | SvIV_set(sv, termcode); |
605 | (void)SvPOK_only(sv); /* validate pointer */ |
606 | |
607 | /* move past delimiter and try to read a complete string */ |
608 | if (keep_delims) |
609 | sv_catpvn(sv, s, termlen); |
610 | s += termlen; |
611 | for (;;) { |
612 | if (PL_encoding && !UTF) { |
613 | bool cont = TRUE; |
614 | |
615 | while (cont) { |
616 | int offset = s - SvPVX_const(PL_linestr); |
617 | const bool found = sv_cat_decode(sv, PL_encoding, PL_linestr, |
618 | &offset, (char*)termstr, termlen); |
619 | const char *ns = SvPVX_const(PL_linestr) + offset; |
620 | char *svlast = SvEND(sv) - 1; |
621 | |
622 | for (; s < ns; s++) { |
623 | if (*s == '\n' && !PL_rsfp) |
624 | CopLINE_inc(PL_curcop); |
625 | } |
626 | if (!found) |
627 | goto read_more_line; |
628 | else { |
629 | /* handle quoted delimiters */ |
630 | if (SvCUR(sv) > 1 && *(svlast-1) == '\\') { |
631 | const char *t; |
632 | for (t = svlast-2; t >= SvPVX_const(sv) && *t == '\\';) |
633 | t--; |
634 | if ((svlast-1 - t) % 2) { |
635 | if (!keep_quoted) { |
636 | *(svlast-1) = term; |
637 | *svlast = '\0'; |
638 | SvCUR_set(sv, SvCUR(sv) - 1); |
639 | } |
640 | continue; |
641 | } |
642 | } |
643 | if (PL_multi_open == PL_multi_close) { |
644 | cont = FALSE; |
645 | } |
646 | else { |
647 | const char *t; |
648 | char *w; |
649 | if (!last) |
650 | last = SvPVX(sv); |
651 | for (t = w = last; t < svlast; w++, t++) { |
652 | /* At here, all closes are "was quoted" one, |
653 | so we don't check PL_multi_close. */ |
654 | if (*t == '\\') { |
655 | if (!keep_quoted && *(t+1) == PL_multi_open) |
656 | t++; |
657 | else |
658 | *w++ = *t++; |
659 | } |
660 | else if (*t == PL_multi_open) |
661 | brackets++; |
662 | |
663 | *w = *t; |
664 | } |
665 | if (w < t) { |
666 | *w++ = term; |
667 | *w = '\0'; |
668 | SvCUR_set(sv, w - SvPVX_const(sv)); |
669 | } |
670 | last = w; |
671 | if (--brackets <= 0) |
672 | cont = FALSE; |
673 | } |
674 | } |
675 | } |
676 | if (!keep_delims) { |
677 | SvCUR_set(sv, SvCUR(sv) - 1); |
678 | *SvEND(sv) = '\0'; |
679 | } |
680 | break; |
681 | } |
682 | |
683 | /* extend sv if need be */ |
684 | SvGROW(sv, SvCUR(sv) + (PL_bufend - s) + 1); |
685 | /* set 'to' to the next character in the sv's string */ |
686 | to = SvPVX(sv)+SvCUR(sv); |
687 | |
688 | /* if open delimiter is the close delimiter read unbridle */ |
689 | if (PL_multi_open == PL_multi_close) { |
690 | for (; s < PL_bufend; s++,to++) { |
691 | /* embedded newlines increment the current line number */ |
692 | if (*s == '\n' && !PL_rsfp) |
693 | CopLINE_inc(PL_curcop); |
694 | /* handle quoted delimiters */ |
695 | if (*s == '\\' && s+1 < PL_bufend && term != '\\') { |
696 | if (!keep_quoted && s[1] == term) |
697 | s++; |
698 | /* any other quotes are simply copied straight through */ |
699 | else |
700 | *to++ = *s++; |
701 | } |
702 | /* terminate when run out of buffer (the for() condition), or |
703 | have found the terminator */ |
704 | else if (*s == term) { |
705 | if (termlen == 1) |
706 | break; |
707 | if (s+termlen <= PL_bufend && memEQ(s, (char*)termstr, termlen)) |
708 | break; |
709 | } |
710 | else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) |
711 | has_utf8 = TRUE; |
712 | *to = *s; |
713 | } |
714 | } |
715 | |
716 | /* if the terminator isn't the same as the start character (e.g., |
717 | matched brackets), we have to allow more in the quoting, and |
718 | be prepared for nested brackets. |
719 | */ |
720 | else { |
721 | /* read until we run out of string, or we find the terminator */ |
722 | for (; s < PL_bufend; s++,to++) { |
723 | /* embedded newlines increment the line count */ |
724 | if (*s == '\n' && !PL_rsfp) |
725 | CopLINE_inc(PL_curcop); |
726 | /* backslashes can escape the open or closing characters */ |
727 | if (*s == '\\' && s+1 < PL_bufend) { |
728 | if (!keep_quoted && |
729 | ((s[1] == PL_multi_open) || (s[1] == PL_multi_close))) |
730 | s++; |
731 | else |
732 | *to++ = *s++; |
733 | } |
734 | /* allow nested opens and closes */ |
735 | else if (*s == PL_multi_close && --brackets <= 0) |
736 | break; |
737 | else if (*s == PL_multi_open) |
738 | brackets++; |
739 | else if (!has_utf8 && !UTF8_IS_INVARIANT((U8)*s) && UTF) |
740 | has_utf8 = TRUE; |
741 | *to = *s; |
742 | } |
743 | } |
744 | /* terminate the copied string and update the sv's end-of-string */ |
745 | *to = '\0'; |
746 | SvCUR_set(sv, to - SvPVX_const(sv)); |
747 | |
748 | /* |
749 | * this next chunk reads more into the buffer if we're not done yet |
750 | */ |
751 | |
752 | if (s < PL_bufend) |
753 | break; /* handle case where we are done yet :-) */ |
754 | |
755 | #ifndef PERL_STRICT_CR |
756 | if (to - SvPVX_const(sv) >= 2) { |
757 | if ((to[-2] == '\r' && to[-1] == '\n') || |
758 | (to[-2] == '\n' && to[-1] == '\r')) |
759 | { |
760 | to[-2] = '\n'; |
761 | to--; |
762 | SvCUR_set(sv, to - SvPVX_const(sv)); |
763 | } |
764 | else if (to[-1] == '\r') |
765 | to[-1] = '\n'; |
766 | } |
767 | else if (to - SvPVX_const(sv) == 1 && to[-1] == '\r') |
768 | to[-1] = '\n'; |
769 | #endif |
770 | |
771 | read_more_line: |
772 | /* if we're out of file, or a read fails, bail and reset the current |
773 | line marker so we can report where the unterminated string began |
774 | */ |
775 | if (!PL_rsfp || |
776 | !(PL_oldoldbufptr = PL_oldbufptr = s = PL_linestart = filter_gets(PL_linestr, PL_rsfp, 0))) { |
777 | sv_free(sv); |
778 | CopLINE_set(PL_curcop, (line_t)PL_multi_start); |
779 | return Nullch; |
780 | } |
781 | /* we read a line, so increment our line counter */ |
782 | CopLINE_inc(PL_curcop); |
783 | |
784 | /* update debugger info */ |
785 | if (PERLDB_LINE && PL_curstash != PL_debstash) { |
786 | SV *sv = NEWSV(88,0); |
787 | |
788 | sv_upgrade(sv, SVt_PVMG); |
789 | sv_setsv(sv,PL_linestr); |
790 | (void)SvIOK_on(sv); |
791 | SvIV_set(sv, 0); |
792 | av_store(CopFILEAV(PL_curcop), (I32)CopLINE(PL_curcop), sv); |
793 | } |
794 | |
795 | /* having changed the buffer, we must update PL_bufend */ |
796 | PL_bufend = SvPVX(PL_linestr) + SvCUR(PL_linestr); |
797 | PL_last_lop = PL_last_uni = Nullch; |
798 | } |
799 | |
800 | /* at this point, we have successfully read the delimited string */ |
801 | |
802 | if (!PL_encoding || UTF) { |
803 | if (keep_delims) |
804 | sv_catpvn(sv, s, termlen); |
805 | s += termlen; |
806 | } |
807 | if (has_utf8 || PL_encoding) |
808 | SvUTF8_on(sv); |
809 | |
810 | PL_multi_end = CopLINE(PL_curcop); |
811 | |
812 | /* if we allocated too much space, give some back */ |
813 | if (SvCUR(sv) + 5 < SvLEN(sv)) { |
814 | SvLEN_set(sv, SvCUR(sv) + 1); |
71511651 |
815 | /* 5.8.8 uses SvPV_renew, no prior version actually has the damn thing (mst) */ |
816 | #ifdef PERL_5_8_8_PLUS |
0ba8c7aa |
817 | SvPV_renew(sv, SvLEN(sv)); |
71511651 |
818 | #else |
819 | Renew(SvPVX(sv), SvLEN(sv), char); |
820 | #endif |
0ba8c7aa |
821 | } |
822 | |
823 | /* decide whether this is the first or second quoted string we've read |
824 | for this op |
825 | */ |
826 | |
827 | if (PL_lex_stuff) |
828 | PL_lex_repl = sv; |
829 | else |
830 | PL_lex_stuff = sv; |
831 | return s; |
832 | } |
833 | |
834 | /* |
835 | * S_force_next |
836 | * When the lexer realizes it knows the next token (for instance, |
837 | * it is reordering tokens for the parser) then it can call S_force_next |
838 | * to know what token to return the next time the lexer is called. Caller |
839 | * will need to set PL_nextval[], and possibly PL_expect to ensure the lexer |
840 | * handles the token correctly. |
841 | */ |
842 | |
843 | STATIC void |
844 | S_force_next(pTHX_ I32 type) |
845 | { |
846 | PL_nexttype[PL_nexttoke] = type; |
847 | PL_nexttoke++; |
848 | if (PL_lex_state != LEX_KNOWNEXT) { |
849 | PL_lex_defer = PL_lex_state; |
850 | PL_lex_expect = PL_expect; |
851 | PL_lex_state = LEX_KNOWNEXT; |
852 | } |
853 | } |
92997e69 |
854 | |
855 | #define XFAKEBRACK 128 |
856 | |
857 | STATIC char * |
858 | S_scan_ident(pTHX_ register char *s, register const char *send, char *dest, STRLEN destlen, I32 ck_uni) |
859 | { |
860 | register char *d; |
861 | register char *e; |
862 | char *bracket = Nullch; |
863 | char funny = *s++; |
864 | |
865 | if (isSPACE(*s)) |
866 | s = skipspace(s); |
867 | d = dest; |
868 | e = d + destlen - 3; /* two-character token, ending NUL */ |
869 | if (isDIGIT(*s)) { |
870 | while (isDIGIT(*s)) { |
871 | if (d >= e) |
872 | Perl_croak(aTHX_ ident_too_long); |
873 | *d++ = *s++; |
874 | } |
875 | } |
876 | else { |
877 | for (;;) { |
878 | if (d >= e) |
879 | Perl_croak(aTHX_ ident_too_long); |
880 | if (isALNUM(*s)) /* UTF handled below */ |
881 | *d++ = *s++; |
882 | else if (*s == '\'' && isIDFIRST_lazy_if(s+1,UTF)) { |
883 | *d++ = ':'; |
884 | *d++ = ':'; |
885 | s++; |
886 | } |
887 | else if (*s == ':' && s[1] == ':') { |
888 | *d++ = *s++; |
889 | *d++ = *s++; |
890 | } |
891 | else if (UTF && UTF8_IS_START(*s) && isALNUM_utf8((U8*)s)) { |
892 | char *t = s + UTF8SKIP(s); |
893 | while (UTF8_IS_CONTINUED(*t) && is_utf8_mark((U8*)t)) |
894 | t += UTF8SKIP(t); |
895 | if (d + (t - s) > e) |
896 | Perl_croak(aTHX_ ident_too_long); |
897 | Copy(s, d, t - s, char); |
898 | d += t - s; |
899 | s = t; |
900 | } |
901 | else |
902 | break; |
903 | } |
904 | } |
905 | *d = '\0'; |
906 | d = dest; |
907 | if (*d) { |
908 | if (PL_lex_state != LEX_NORMAL) |
909 | PL_lex_state = LEX_INTERPENDMAYBE; |
910 | return s; |
911 | } |
912 | if (*s == '$' && s[1] && |
913 | (isALNUM_lazy_if(s+1,UTF) || s[1] == '$' || s[1] == '{' || strnEQ(s+1,"::",2)) ) |
914 | { |
915 | return s; |
916 | } |
917 | if (*s == '{') { |
918 | bracket = s; |
919 | s++; |
920 | } |
d6e7537a |
921 | /* we always call this with ck_uni == 0 (rafl) */ |
922 | /* |
92997e69 |
923 | else if (ck_uni) |
924 | check_uni(); |
d6e7537a |
925 | */ |
92997e69 |
926 | if (s < send) |
927 | *d = *s++; |
928 | d[1] = '\0'; |
929 | if (*d == '^' && *s && isCONTROLVAR(*s)) { |
930 | *d = toCTRL(*s); |
931 | s++; |
932 | } |
933 | if (bracket) { |
934 | if (isSPACE(s[-1])) { |
935 | while (s < send) { |
936 | const char ch = *s++; |
937 | if (!SPACE_OR_TAB(ch)) { |
938 | *d = ch; |
939 | break; |
940 | } |
941 | } |
942 | } |
943 | if (isIDFIRST_lazy_if(d,UTF)) { |
944 | d++; |
945 | if (UTF) { |
946 | e = s; |
947 | while ((e < send && isALNUM_lazy_if(e,UTF)) || *e == ':') { |
948 | e += UTF8SKIP(e); |
949 | while (e < send && UTF8_IS_CONTINUED(*e) && is_utf8_mark((U8*)e)) |
950 | e += UTF8SKIP(e); |
951 | } |
952 | Copy(s, d, e - s, char); |
953 | d += e - s; |
954 | s = e; |
955 | } |
956 | else { |
957 | while ((isALNUM(*s) || *s == ':') && d < e) |
958 | *d++ = *s++; |
959 | if (d >= e) |
960 | Perl_croak(aTHX_ ident_too_long); |
961 | } |
962 | *d = '\0'; |
963 | while (s < send && SPACE_OR_TAB(*s)) s++; |
964 | if ((*s == '[' || (*s == '{' && strNE(dest, "sub")))) { |
d6e7537a |
965 | /* we don't want perl to guess what is meant. the keyword |
966 | * parser decides that later. (rafl) |
967 | */ |
968 | /* |
92997e69 |
969 | if (ckWARN(WARN_AMBIGUOUS) && keyword(dest, d - dest)) { |
970 | const char *brack = *s == '[' ? "[...]" : "{...}"; |
971 | Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS), |
972 | "Ambiguous use of %c{%s%s} resolved to %c%s%s", |
973 | funny, dest, brack, funny, dest, brack); |
974 | } |
d6e7537a |
975 | */ |
92997e69 |
976 | bracket++; |
977 | PL_lex_brackstack[PL_lex_brackets++] = (char)(XOPERATOR | XFAKEBRACK); |
978 | return s; |
979 | } |
980 | } |
981 | /* Handle extended ${^Foo} variables |
982 | * 1999-02-27 mjd-perl-patch@plover.com */ |
983 | else if (!isALNUM(*d) && !isPRINT(*d) /* isCTRL(d) */ |
984 | && isALNUM(*s)) |
985 | { |
986 | d++; |
987 | while (isALNUM(*s) && d < e) { |
988 | *d++ = *s++; |
989 | } |
990 | if (d >= e) |
991 | Perl_croak(aTHX_ ident_too_long); |
992 | *d = '\0'; |
993 | } |
994 | if (*s == '}') { |
995 | s++; |
996 | if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets) { |
997 | PL_lex_state = LEX_INTERPEND; |
998 | PL_expect = XREF; |
999 | } |
1000 | if (funny == '#') |
1001 | funny = '@'; |
d6e7537a |
1002 | /* we don't want perl to guess what is meant. the keyword |
1003 | * parser decides that later. (rafl) |
1004 | */ |
1005 | /* |
92997e69 |
1006 | if (PL_lex_state == LEX_NORMAL) { |
1007 | if (ckWARN(WARN_AMBIGUOUS) && |
1008 | (keyword(dest, d - dest) || get_cv(dest, FALSE))) |
1009 | { |
1010 | Perl_warner(aTHX_ packWARN(WARN_AMBIGUOUS), |
1011 | "Ambiguous use of %c{%s} resolved to %c%s", |
1012 | funny, dest, funny, dest); |
1013 | } |
1014 | } |
d6e7537a |
1015 | */ |
92997e69 |
1016 | } |
1017 | else { |
1018 | s = bracket; /* let the parser handle it */ |
1019 | *dest = '\0'; |
1020 | } |
1021 | } |
d6e7537a |
1022 | /* don't intuit. we really just want the string. (rafl) */ |
1023 | /* |
92997e69 |
1024 | else if (PL_lex_state == LEX_INTERPNORMAL && !PL_lex_brackets && !intuit_more(s)) |
1025 | PL_lex_state = LEX_INTERPEND; |
d6e7537a |
1026 | */ |
92997e69 |
1027 | return s; |
1028 | } |