Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* perl.c |
2 | * | |
bc89e66f | 3 | * Copyright (c) 1987-2001 Larry Wall |
a687059c | 4 | * |
352d5a3a LW |
5 | * You may distribute under the terms of either the GNU General Public |
6 | * License or the Artistic License, as specified in the README file. | |
a687059c | 7 | * |
8d063cd8 LW |
8 | */ |
9 | ||
a0d0e21e LW |
10 | /* |
11 | * "A ship then new they built for him/of mithril and of elven glass" --Bilbo | |
12 | */ | |
45d8adaa | 13 | |
378cc40b | 14 | #include "EXTERN.h" |
864dbfa3 | 15 | #define PERL_IN_PERL_C |
378cc40b | 16 | #include "perl.h" |
e3321bb0 | 17 | #include "patchlevel.h" /* for local_patches */ |
378cc40b | 18 | |
df5cef82 | 19 | /* XXX If this causes problems, set i_unistd=undef in the hint file. */ |
a0d0e21e LW |
20 | #ifdef I_UNISTD |
21 | #include <unistd.h> | |
22 | #endif | |
a0d0e21e | 23 | |
54310121 | 24 | #if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE) |
20ce7b12 | 25 | char *getenv (char *); /* Usually in <stdlib.h> */ |
54310121 | 26 | #endif |
27 | ||
0cb96387 GS |
28 | static I32 read_e_script(pTHXo_ int idx, SV *buf_sv, int maxlen); |
29 | ||
a687059c LW |
30 | #ifdef IAMSUID |
31 | #ifndef DOSUID | |
32 | #define DOSUID | |
33 | #endif | |
34 | #endif | |
378cc40b | 35 | |
a687059c LW |
36 | #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW |
37 | #ifdef DOSUID | |
38 | #undef DOSUID | |
39 | #endif | |
40 | #endif | |
8d063cd8 | 41 | |
873ef191 | 42 | #ifdef PERL_OBJECT |
7766f137 GS |
43 | #define perl_construct Perl_construct |
44 | #define perl_parse Perl_parse | |
45 | #define perl_run Perl_run | |
46 | #define perl_destruct Perl_destruct | |
47 | #define perl_free Perl_free | |
48 | #endif | |
32e30700 | 49 | |
06d86050 GS |
50 | #if defined(USE_THREADS) |
51 | # define INIT_TLS_AND_INTERP \ | |
52 | STMT_START { \ | |
53 | if (!PL_curinterp) { \ | |
54 | PERL_SET_INTERP(my_perl); \ | |
55 | INIT_THREADS; \ | |
56 | ALLOC_THREAD_KEY; \ | |
57 | } \ | |
58 | } STMT_END | |
59 | #else | |
60 | # if defined(USE_ITHREADS) | |
61 | # define INIT_TLS_AND_INTERP \ | |
62 | STMT_START { \ | |
63 | if (!PL_curinterp) { \ | |
64 | PERL_SET_INTERP(my_perl); \ | |
65 | INIT_THREADS; \ | |
66 | ALLOC_THREAD_KEY; \ | |
534825c4 GS |
67 | PERL_SET_THX(my_perl); \ |
68 | OP_REFCNT_INIT; \ | |
69 | } \ | |
70 | else { \ | |
71 | PERL_SET_THX(my_perl); \ | |
06d86050 | 72 | } \ |
06d86050 GS |
73 | } STMT_END |
74 | # else | |
75 | # define INIT_TLS_AND_INTERP \ | |
76 | STMT_START { \ | |
77 | if (!PL_curinterp) { \ | |
78 | PERL_SET_INTERP(my_perl); \ | |
79 | } \ | |
80 | PERL_SET_THX(my_perl); \ | |
81 | } STMT_END | |
82 | # endif | |
83 | #endif | |
84 | ||
32e30700 GS |
85 | #ifdef PERL_IMPLICIT_SYS |
86 | PerlInterpreter * | |
7766f137 GS |
87 | perl_alloc_using(struct IPerlMem* ipM, struct IPerlMem* ipMS, |
88 | struct IPerlMem* ipMP, struct IPerlEnv* ipE, | |
32e30700 GS |
89 | struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO, |
90 | struct IPerlDir* ipD, struct IPerlSock* ipS, | |
91 | struct IPerlProc* ipP) | |
92 | { | |
93 | PerlInterpreter *my_perl; | |
7766f137 GS |
94 | #ifdef PERL_OBJECT |
95 | my_perl = (PerlInterpreter*)new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd, | |
96 | ipLIO, ipD, ipS, ipP); | |
06d86050 | 97 | INIT_TLS_AND_INTERP; |
7766f137 | 98 | #else |
32e30700 GS |
99 | /* New() needs interpreter, so call malloc() instead */ |
100 | my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter)); | |
06d86050 | 101 | INIT_TLS_AND_INTERP; |
32e30700 GS |
102 | Zero(my_perl, 1, PerlInterpreter); |
103 | PL_Mem = ipM; | |
7766f137 GS |
104 | PL_MemShared = ipMS; |
105 | PL_MemParse = ipMP; | |
32e30700 GS |
106 | PL_Env = ipE; |
107 | PL_StdIO = ipStd; | |
108 | PL_LIO = ipLIO; | |
109 | PL_Dir = ipD; | |
110 | PL_Sock = ipS; | |
111 | PL_Proc = ipP; | |
7766f137 GS |
112 | #endif |
113 | ||
32e30700 GS |
114 | return my_perl; |
115 | } | |
116 | #else | |
954c1994 GS |
117 | |
118 | /* | |
119 | =for apidoc perl_alloc | |
120 | ||
121 | Allocates a new Perl interpreter. See L<perlembed>. | |
122 | ||
123 | =cut | |
124 | */ | |
125 | ||
93a17b20 | 126 | PerlInterpreter * |
cea2e8a9 | 127 | perl_alloc(void) |
79072805 | 128 | { |
cea2e8a9 | 129 | PerlInterpreter *my_perl; |
79072805 | 130 | |
54aff467 | 131 | /* New() needs interpreter, so call malloc() instead */ |
e8ee3774 | 132 | my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter)); |
ba869deb | 133 | |
06d86050 | 134 | INIT_TLS_AND_INTERP; |
dedcbb81 | 135 | Zero(my_perl, 1, PerlInterpreter); |
cea2e8a9 | 136 | return my_perl; |
79072805 | 137 | } |
32e30700 | 138 | #endif /* PERL_IMPLICIT_SYS */ |
79072805 | 139 | |
954c1994 GS |
140 | /* |
141 | =for apidoc perl_construct | |
142 | ||
143 | Initializes a new Perl interpreter. See L<perlembed>. | |
144 | ||
145 | =cut | |
146 | */ | |
147 | ||
79072805 | 148 | void |
0cb96387 | 149 | perl_construct(pTHXx) |
79072805 | 150 | { |
a863c7d1 | 151 | #ifdef USE_THREADS |
a863c7d1 | 152 | #ifndef FAKE_THREADS |
e1f15930 | 153 | struct perl_thread *thr = NULL; |
a863c7d1 MB |
154 | #endif /* FAKE_THREADS */ |
155 | #endif /* USE_THREADS */ | |
ba869deb | 156 | |
8990e307 | 157 | #ifdef MULTIPLICITY |
54aff467 | 158 | init_interp(); |
ac27b0f5 | 159 | PL_perl_destruct_level = 1; |
54aff467 GS |
160 | #else |
161 | if (PL_perl_destruct_level > 0) | |
162 | init_interp(); | |
163 | #endif | |
164 | ||
33f46ff6 | 165 | /* Init the real globals (and main thread)? */ |
3280af22 | 166 | if (!PL_linestr) { |
1feb2720 | 167 | #ifdef USE_THREADS |
533c011a | 168 | MUTEX_INIT(&PL_sv_mutex); |
a863c7d1 MB |
169 | /* |
170 | * Safe to use basic SV functions from now on (though | |
171 | * not things like mortals or tainting yet). | |
172 | */ | |
533c011a NIS |
173 | MUTEX_INIT(&PL_eval_mutex); |
174 | COND_INIT(&PL_eval_cond); | |
175 | MUTEX_INIT(&PL_threads_mutex); | |
176 | COND_INIT(&PL_nthreads_cond); | |
ba869deb | 177 | # ifdef EMULATE_ATOMIC_REFCOUNTS |
533c011a | 178 | MUTEX_INIT(&PL_svref_mutex); |
ba869deb | 179 | # endif /* EMULATE_ATOMIC_REFCOUNTS */ |
a863c7d1 | 180 | |
5ff3f7a4 | 181 | MUTEX_INIT(&PL_cred_mutex); |
3d35f11b GS |
182 | MUTEX_INIT(&PL_sv_lock_mutex); |
183 | MUTEX_INIT(&PL_fdpid_mutex); | |
5ff3f7a4 | 184 | |
199100c8 | 185 | thr = init_main_thread(); |
11343788 MB |
186 | #endif /* USE_THREADS */ |
187 | ||
14dd3ad8 | 188 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
0b94c7bb | 189 | PL_protect = MEMBER_TO_FPTR(Perl_default_protect); /* for exceptions */ |
14dd3ad8 | 190 | #endif |
312caa8e | 191 | |
2aea9f8a GS |
192 | PL_curcop = &PL_compiling; /* needed by ckWARN, right away */ |
193 | ||
3280af22 NIS |
194 | PL_linestr = NEWSV(65,79); |
195 | sv_upgrade(PL_linestr,SVt_PVIV); | |
79072805 | 196 | |
3280af22 | 197 | if (!SvREADONLY(&PL_sv_undef)) { |
d689ffdd JP |
198 | /* set read-only and try to insure than we wont see REFCNT==0 |
199 | very often */ | |
200 | ||
3280af22 NIS |
201 | SvREADONLY_on(&PL_sv_undef); |
202 | SvREFCNT(&PL_sv_undef) = (~(U32)0)/2; | |
79072805 | 203 | |
3280af22 NIS |
204 | sv_setpv(&PL_sv_no,PL_No); |
205 | SvNV(&PL_sv_no); | |
206 | SvREADONLY_on(&PL_sv_no); | |
207 | SvREFCNT(&PL_sv_no) = (~(U32)0)/2; | |
79072805 | 208 | |
3280af22 NIS |
209 | sv_setpv(&PL_sv_yes,PL_Yes); |
210 | SvNV(&PL_sv_yes); | |
211 | SvREADONLY_on(&PL_sv_yes); | |
212 | SvREFCNT(&PL_sv_yes) = (~(U32)0)/2; | |
6e72f9df | 213 | } |
79072805 | 214 | |
76e3520e GS |
215 | #ifdef PERL_OBJECT |
216 | /* TODO: */ | |
6b88bc9c | 217 | /* PL_sighandlerp = sighandler; */ |
76e3520e | 218 | #else |
cea2e8a9 | 219 | PL_sighandlerp = Perl_sighandler; |
76e3520e | 220 | #endif |
3280af22 | 221 | PL_pidstatus = newHV(); |
44a8e56a | 222 | |
79072805 LW |
223 | #ifdef MSDOS |
224 | /* | |
225 | * There is no way we can refer to them from Perl so close them to save | |
226 | * space. The other alternative would be to provide STDAUX and STDPRN | |
227 | * filehandles. | |
228 | */ | |
2b96b0a5 JH |
229 | (void)PerlIO_close(PerlIO_importFILE(stdaux, 0)); |
230 | (void)PerlIO_close(PerlIO_importFILE(stdprn, 0)); | |
79072805 LW |
231 | #endif |
232 | } | |
233 | ||
79cb57f6 | 234 | PL_nrs = newSVpvn("\n", 1); |
3280af22 | 235 | PL_rs = SvREFCNT_inc(PL_nrs); |
dc92893f | 236 | |
cea2e8a9 | 237 | init_stacks(); |
79072805 | 238 | |
748a9306 | 239 | init_ids(); |
3280af22 | 240 | PL_lex_state = LEX_NOTPARSING; |
a5f75d66 | 241 | |
312caa8e | 242 | JMPENV_BOOTSTRAP; |
f86702cc | 243 | STATUS_ALL_SUCCESS; |
244 | ||
0672f40e | 245 | init_i18nl10n(1); |
36477c24 | 246 | SET_NUMERIC_STANDARD(); |
0b5b802d | 247 | |
a7cb1f99 GS |
248 | { |
249 | U8 *s; | |
250 | PL_patchlevel = NEWSV(0,4); | |
155aba94 | 251 | (void)SvUPGRADE(PL_patchlevel, SVt_PVNV); |
a7cb1f99 | 252 | if (PERL_REVISION > 127 || PERL_VERSION > 127 || PERL_SUBVERSION > 127) |
806e7201 | 253 | SvGROW(PL_patchlevel, UTF8_MAXLEN*3+1); |
a7cb1f99 | 254 | s = (U8*)SvPVX(PL_patchlevel); |
9041c2e3 NIS |
255 | /* Build version strings using "native" characters */ |
256 | s = uvchr_to_utf8(s, (UV)PERL_REVISION); | |
257 | s = uvchr_to_utf8(s, (UV)PERL_VERSION); | |
258 | s = uvchr_to_utf8(s, (UV)PERL_SUBVERSION); | |
a7cb1f99 GS |
259 | *s = '\0'; |
260 | SvCUR_set(PL_patchlevel, s - (U8*)SvPVX(PL_patchlevel)); | |
261 | SvPOK_on(PL_patchlevel); | |
262 | SvNVX(PL_patchlevel) = (NV)PERL_REVISION | |
263 | + ((NV)PERL_VERSION / (NV)1000) | |
cceca5ed | 264 | #if defined(PERL_SUBVERSION) && PERL_SUBVERSION > 0 |
a7cb1f99 | 265 | + ((NV)PERL_SUBVERSION / (NV)1000000) |
a5f75d66 | 266 | #endif |
a7cb1f99 GS |
267 | ; |
268 | SvNOK_on(PL_patchlevel); /* dual valued */ | |
269 | SvUTF8_on(PL_patchlevel); | |
270 | SvREADONLY_on(PL_patchlevel); | |
271 | } | |
79072805 | 272 | |
ab821d7f | 273 | #if defined(LOCAL_PATCH_COUNT) |
3280af22 | 274 | PL_localpatches = local_patches; /* For possible -v */ |
ab821d7f | 275 | #endif |
276 | ||
52853b95 GS |
277 | #ifdef HAVE_INTERP_INTERN |
278 | sys_intern_init(); | |
279 | #endif | |
280 | ||
4b556e6c | 281 | PerlIO_init(); /* Hook to IO system */ |
760ac839 | 282 | |
3280af22 NIS |
283 | PL_fdpid = newAV(); /* for remembering popen pids by fd */ |
284 | PL_modglobal = newHV(); /* pointers to per-interpreter module globals */ | |
24944567 | 285 | PL_errors = newSVpvn("",0); |
1fcf4c12 AB |
286 | #ifdef USE_ITHREADS |
287 | PL_regex_padav = newAV(); | |
288 | #endif | |
e5dd39fc AB |
289 | #ifdef USE_REENTRANT_API |
290 | New(31337, PL_reentrant_buffer,1, REBUF); | |
291 | New(31337, PL_reentrant_buffer->tmbuff,1, struct tm); | |
292 | #endif | |
8990e307 | 293 | ENTER; |
79072805 LW |
294 | } |
295 | ||
954c1994 GS |
296 | /* |
297 | =for apidoc perl_destruct | |
298 | ||
299 | Shuts down a Perl interpreter. See L<perlembed>. | |
300 | ||
301 | =cut | |
302 | */ | |
303 | ||
79072805 | 304 | void |
0cb96387 | 305 | perl_destruct(pTHXx) |
79072805 | 306 | { |
748a9306 | 307 | int destruct_level; /* 0=none, 1=full, 2=full with checks */ |
a0d0e21e | 308 | HV *hv; |
1f2bfc8a | 309 | #ifdef USE_THREADS |
33f46ff6 | 310 | Thread t; |
cea2e8a9 | 311 | dTHX; |
1f2bfc8a | 312 | #endif /* USE_THREADS */ |
8990e307 | 313 | |
7766f137 GS |
314 | /* wait for all pseudo-forked children to finish */ |
315 | PERL_WAIT_FOR_CHILDREN; | |
316 | ||
11343788 | 317 | #ifdef USE_THREADS |
0f15f207 | 318 | #ifndef FAKE_THREADS |
8023c3ce MB |
319 | /* Pass 1 on any remaining threads: detach joinables, join zombies */ |
320 | retry_cleanup: | |
533c011a | 321 | MUTEX_LOCK(&PL_threads_mutex); |
bf49b057 | 322 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
c7848ba1 | 323 | "perl_destruct: waiting for %d threads...\n", |
533c011a | 324 | PL_nthreads - 1)); |
33f46ff6 | 325 | for (t = thr->next; t != thr; t = t->next) { |
605e5515 MB |
326 | MUTEX_LOCK(&t->mutex); |
327 | switch (ThrSTATE(t)) { | |
328 | AV *av; | |
c7848ba1 | 329 | case THRf_ZOMBIE: |
bf49b057 | 330 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
c7848ba1 | 331 | "perl_destruct: joining zombie %p\n", t)); |
605e5515 MB |
332 | ThrSETSTATE(t, THRf_DEAD); |
333 | MUTEX_UNLOCK(&t->mutex); | |
533c011a | 334 | PL_nthreads--; |
8023c3ce MB |
335 | /* |
336 | * The SvREFCNT_dec below may take a long time (e.g. av | |
337 | * may contain an object scalar whose destructor gets | |
338 | * called) so we have to unlock threads_mutex and start | |
339 | * all over again. | |
340 | */ | |
533c011a | 341 | MUTEX_UNLOCK(&PL_threads_mutex); |
ea0efc06 | 342 | JOIN(t, &av); |
605e5515 | 343 | SvREFCNT_dec((SV*)av); |
bf49b057 | 344 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
c7848ba1 | 345 | "perl_destruct: joined zombie %p OK\n", t)); |
8023c3ce | 346 | goto retry_cleanup; |
c7848ba1 | 347 | case THRf_R_JOINABLE: |
bf49b057 | 348 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
c7848ba1 MB |
349 | "perl_destruct: detaching thread %p\n", t)); |
350 | ThrSETSTATE(t, THRf_R_DETACHED); | |
ac27b0f5 | 351 | /* |
c7848ba1 MB |
352 | * We unlock threads_mutex and t->mutex in the opposite order |
353 | * from which we locked them just so that DETACH won't | |
354 | * deadlock if it panics. It's only a breach of good style | |
355 | * not a bug since they are unlocks not locks. | |
356 | */ | |
533c011a | 357 | MUTEX_UNLOCK(&PL_threads_mutex); |
c7848ba1 MB |
358 | DETACH(t); |
359 | MUTEX_UNLOCK(&t->mutex); | |
8023c3ce | 360 | goto retry_cleanup; |
c7848ba1 | 361 | default: |
bf49b057 | 362 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
c7848ba1 MB |
363 | "perl_destruct: ignoring %p (state %u)\n", |
364 | t, ThrSTATE(t))); | |
365 | MUTEX_UNLOCK(&t->mutex); | |
c7848ba1 | 366 | /* fall through and out */ |
33f46ff6 MB |
367 | } |
368 | } | |
8023c3ce MB |
369 | /* We leave the above "Pass 1" loop with threads_mutex still locked */ |
370 | ||
371 | /* Pass 2 on remaining threads: wait for the thread count to drop to one */ | |
533c011a | 372 | while (PL_nthreads > 1) |
11343788 | 373 | { |
bf49b057 | 374 | DEBUG_S(PerlIO_printf(Perl_debug_log, |
c7848ba1 | 375 | "perl_destruct: final wait for %d threads\n", |
533c011a NIS |
376 | PL_nthreads - 1)); |
377 | COND_WAIT(&PL_nthreads_cond, &PL_threads_mutex); | |
11343788 MB |
378 | } |
379 | /* At this point, we're the last thread */ | |
533c011a | 380 | MUTEX_UNLOCK(&PL_threads_mutex); |
bf49b057 | 381 | DEBUG_S(PerlIO_printf(Perl_debug_log, "perl_destruct: armageddon has arrived\n")); |
533c011a NIS |
382 | MUTEX_DESTROY(&PL_threads_mutex); |
383 | COND_DESTROY(&PL_nthreads_cond); | |
b57a092c | 384 | PL_nthreads--; |
0f15f207 | 385 | #endif /* !defined(FAKE_THREADS) */ |
11343788 MB |
386 | #endif /* USE_THREADS */ |
387 | ||
3280af22 | 388 | destruct_level = PL_perl_destruct_level; |
4633a7c4 LW |
389 | #ifdef DEBUGGING |
390 | { | |
391 | char *s; | |
155aba94 | 392 | if ((s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL"))) { |
5f05dabc | 393 | int i = atoi(s); |
394 | if (destruct_level < i) | |
395 | destruct_level = i; | |
396 | } | |
4633a7c4 LW |
397 | } |
398 | #endif | |
399 | ||
26f423df | 400 | { |
f3faeb53 AB |
401 | dJMPENV; |
402 | int x = 0; | |
403 | ||
404 | JMPENV_PUSH(x); | |
405 | if (PL_endav && !PL_minus_c) | |
406 | call_list(PL_scopestack_ix, PL_endav); | |
407 | JMPENV_POP; | |
26f423df | 408 | } |
f3faeb53 | 409 | LEAVE; |
a0d0e21e LW |
410 | FREETMPS; |
411 | ||
ff0cee69 | 412 | /* We must account for everything. */ |
413 | ||
414 | /* Destroy the main CV and syntax tree */ | |
3280af22 NIS |
415 | if (PL_main_root) { |
416 | PL_curpad = AvARRAY(PL_comppad); | |
417 | op_free(PL_main_root); | |
418 | PL_main_root = Nullop; | |
a0d0e21e | 419 | } |
3280af22 NIS |
420 | PL_curcop = &PL_compiling; |
421 | PL_main_start = Nullop; | |
422 | SvREFCNT_dec(PL_main_cv); | |
423 | PL_main_cv = Nullcv; | |
24d3c518 | 424 | PL_dirty = TRUE; |
ff0cee69 | 425 | |
13621cfb NIS |
426 | /* Tell PerlIO we are about to tear things apart in case |
427 | we have layers which are using resources that should | |
428 | be cleaned up now. | |
429 | */ | |
430 | ||
431 | PerlIO_destruct(aTHX); | |
432 | ||
3280af22 | 433 | if (PL_sv_objcount) { |
a0d0e21e LW |
434 | /* |
435 | * Try to destruct global references. We do this first so that the | |
436 | * destructors and destructees still exist. Some sv's might remain. | |
437 | * Non-referenced objects are on their own. | |
438 | */ | |
a0d0e21e | 439 | sv_clean_objs(); |
8990e307 LW |
440 | } |
441 | ||
5cd24f17 | 442 | /* unhook hooks which will soon be, or use, destroyed data */ |
3280af22 NIS |
443 | SvREFCNT_dec(PL_warnhook); |
444 | PL_warnhook = Nullsv; | |
445 | SvREFCNT_dec(PL_diehook); | |
446 | PL_diehook = Nullsv; | |
5cd24f17 | 447 | |
4b556e6c | 448 | /* call exit list functions */ |
3280af22 | 449 | while (PL_exitlistlen-- > 0) |
0cb96387 | 450 | PL_exitlist[PL_exitlistlen].fn(aTHXo_ PL_exitlist[PL_exitlistlen].ptr); |
4b556e6c | 451 | |
3280af22 | 452 | Safefree(PL_exitlist); |
4b556e6c | 453 | |
a0d0e21e | 454 | if (destruct_level == 0){ |
8990e307 | 455 | |
a0d0e21e | 456 | DEBUG_P(debprofdump()); |
ac27b0f5 | 457 | |
a0d0e21e LW |
458 | /* The exit() function will do everything that needs doing. */ |
459 | return; | |
460 | } | |
5dd60ef7 | 461 | |
551a8b83 JH |
462 | /* jettison our possibly duplicated environment */ |
463 | ||
464 | #ifdef USE_ENVIRON_ARRAY | |
465 | if (environ != PL_origenviron) { | |
466 | I32 i; | |
467 | ||
468 | for (i = 0; environ[i]; i++) | |
4b420006 JH |
469 | safesysfree(environ[i]); |
470 | /* Must use safesysfree() when working with environ. */ | |
471 | safesysfree(environ); | |
551a8b83 JH |
472 | |
473 | environ = PL_origenviron; | |
474 | } | |
475 | #endif | |
476 | ||
5f8cb046 DM |
477 | #ifdef USE_ITHREADS |
478 | /* the syntax tree is shared between clones | |
479 | * so op_free(PL_main_root) only ReREFCNT_dec's | |
480 | * REGEXPs in the parent interpreter | |
481 | * we need to manually ReREFCNT_dec for the clones | |
482 | */ | |
483 | { | |
484 | I32 i = AvFILLp(PL_regex_padav) + 1; | |
485 | SV **ary = AvARRAY(PL_regex_padav); | |
486 | ||
487 | while (i) { | |
35061a7e DM |
488 | SV *resv = ary[--i]; |
489 | REGEXP *re = (REGEXP *)SvIVX(resv); | |
490 | ||
491 | if (SvFLAGS(resv) & SVf_BREAK) { | |
577e12cc | 492 | /* this is PL_reg_curpm, already freed |
35061a7e DM |
493 | * flag is set in regexec.c:S_regtry |
494 | */ | |
495 | SvFLAGS(resv) &= ~SVf_BREAK; | |
496 | } | |
497 | else { | |
5f8cb046 DM |
498 | ReREFCNT_dec(re); |
499 | } | |
500 | } | |
501 | } | |
502 | SvREFCNT_dec(PL_regex_padav); | |
503 | PL_regex_padav = Nullav; | |
504 | PL_regex_pad = NULL; | |
505 | #endif | |
506 | ||
5f05dabc | 507 | /* loosen bonds of global variables */ |
508 | ||
3280af22 NIS |
509 | if(PL_rsfp) { |
510 | (void)PerlIO_close(PL_rsfp); | |
511 | PL_rsfp = Nullfp; | |
8ebc5c01 | 512 | } |
513 | ||
514 | /* Filters for program text */ | |
3280af22 NIS |
515 | SvREFCNT_dec(PL_rsfp_filters); |
516 | PL_rsfp_filters = Nullav; | |
8ebc5c01 | 517 | |
518 | /* switches */ | |
3280af22 NIS |
519 | PL_preprocess = FALSE; |
520 | PL_minus_n = FALSE; | |
521 | PL_minus_p = FALSE; | |
522 | PL_minus_l = FALSE; | |
523 | PL_minus_a = FALSE; | |
524 | PL_minus_F = FALSE; | |
525 | PL_doswitches = FALSE; | |
599cee73 | 526 | PL_dowarn = G_WARN_OFF; |
3280af22 NIS |
527 | PL_doextract = FALSE; |
528 | PL_sawampersand = FALSE; /* must save all match strings */ | |
3280af22 NIS |
529 | PL_unsafe = FALSE; |
530 | ||
531 | Safefree(PL_inplace); | |
532 | PL_inplace = Nullch; | |
a7cb1f99 | 533 | SvREFCNT_dec(PL_patchlevel); |
3280af22 NIS |
534 | |
535 | if (PL_e_script) { | |
536 | SvREFCNT_dec(PL_e_script); | |
537 | PL_e_script = Nullsv; | |
8ebc5c01 | 538 | } |
539 | ||
bc0a45ed DM |
540 | while (--PL_origargc >= 0) { |
541 | Safefree(PL_origargv[PL_origargc]); | |
542 | } | |
543 | Safefree(PL_origargv); | |
544 | ||
8ebc5c01 | 545 | /* magical thingies */ |
546 | ||
7889fe52 NIS |
547 | SvREFCNT_dec(PL_ofs_sv); /* $, */ |
548 | PL_ofs_sv = Nullsv; | |
5f05dabc | 549 | |
7889fe52 NIS |
550 | SvREFCNT_dec(PL_ors_sv); /* $\ */ |
551 | PL_ors_sv = Nullsv; | |
8ebc5c01 | 552 | |
3280af22 NIS |
553 | SvREFCNT_dec(PL_rs); /* $/ */ |
554 | PL_rs = Nullsv; | |
dc92893f | 555 | |
3280af22 NIS |
556 | SvREFCNT_dec(PL_nrs); /* $/ helper */ |
557 | PL_nrs = Nullsv; | |
5f05dabc | 558 | |
d33b2eba GS |
559 | PL_multiline = 0; /* $* */ |
560 | Safefree(PL_osname); /* $^O */ | |
561 | PL_osname = Nullch; | |
5f05dabc | 562 | |
3280af22 NIS |
563 | SvREFCNT_dec(PL_statname); |
564 | PL_statname = Nullsv; | |
565 | PL_statgv = Nullgv; | |
5f05dabc | 566 | |
8ebc5c01 | 567 | /* defgv, aka *_ should be taken care of elsewhere */ |
568 | ||
8ebc5c01 | 569 | /* clean up after study() */ |
3280af22 NIS |
570 | SvREFCNT_dec(PL_lastscream); |
571 | PL_lastscream = Nullsv; | |
572 | Safefree(PL_screamfirst); | |
573 | PL_screamfirst = 0; | |
574 | Safefree(PL_screamnext); | |
575 | PL_screamnext = 0; | |
8ebc5c01 | 576 | |
7d5ea4e7 GS |
577 | /* float buffer */ |
578 | Safefree(PL_efloatbuf); | |
579 | PL_efloatbuf = Nullch; | |
580 | PL_efloatsize = 0; | |
581 | ||
8ebc5c01 | 582 | /* startup and shutdown function lists */ |
3280af22 NIS |
583 | SvREFCNT_dec(PL_beginav); |
584 | SvREFCNT_dec(PL_endav); | |
7d30b5c4 | 585 | SvREFCNT_dec(PL_checkav); |
3280af22 NIS |
586 | SvREFCNT_dec(PL_initav); |
587 | PL_beginav = Nullav; | |
588 | PL_endav = Nullav; | |
7d30b5c4 | 589 | PL_checkav = Nullav; |
3280af22 | 590 | PL_initav = Nullav; |
5618dfe8 | 591 | |
8ebc5c01 | 592 | /* shortcuts just get cleared */ |
3280af22 | 593 | PL_envgv = Nullgv; |
3280af22 NIS |
594 | PL_incgv = Nullgv; |
595 | PL_hintgv = Nullgv; | |
596 | PL_errgv = Nullgv; | |
597 | PL_argvgv = Nullgv; | |
598 | PL_argvoutgv = Nullgv; | |
599 | PL_stdingv = Nullgv; | |
bf49b057 | 600 | PL_stderrgv = Nullgv; |
3280af22 NIS |
601 | PL_last_in_gv = Nullgv; |
602 | PL_replgv = Nullgv; | |
5c831c24 | 603 | PL_debstash = Nullhv; |
8ebc5c01 | 604 | |
605 | /* reset so print() ends up where we expect */ | |
606 | setdefout(Nullgv); | |
5c831c24 | 607 | |
7a1c5554 GS |
608 | SvREFCNT_dec(PL_argvout_stack); |
609 | PL_argvout_stack = Nullav; | |
8ebc5c01 | 610 | |
5c831c24 GS |
611 | SvREFCNT_dec(PL_modglobal); |
612 | PL_modglobal = Nullhv; | |
613 | SvREFCNT_dec(PL_preambleav); | |
614 | PL_preambleav = Nullav; | |
615 | SvREFCNT_dec(PL_subname); | |
616 | PL_subname = Nullsv; | |
617 | SvREFCNT_dec(PL_linestr); | |
618 | PL_linestr = Nullsv; | |
619 | SvREFCNT_dec(PL_pidstatus); | |
620 | PL_pidstatus = Nullhv; | |
621 | SvREFCNT_dec(PL_toptarget); | |
622 | PL_toptarget = Nullsv; | |
623 | SvREFCNT_dec(PL_bodytarget); | |
624 | PL_bodytarget = Nullsv; | |
625 | PL_formtarget = Nullsv; | |
626 | ||
d33b2eba | 627 | /* free locale stuff */ |
b9582b6a | 628 | #ifdef USE_LOCALE_COLLATE |
d33b2eba GS |
629 | Safefree(PL_collation_name); |
630 | PL_collation_name = Nullch; | |
b9582b6a | 631 | #endif |
d33b2eba | 632 | |
b9582b6a | 633 | #ifdef USE_LOCALE_NUMERIC |
d33b2eba GS |
634 | Safefree(PL_numeric_name); |
635 | PL_numeric_name = Nullch; | |
a453c169 | 636 | SvREFCNT_dec(PL_numeric_radix_sv); |
b9582b6a | 637 | #endif |
d33b2eba | 638 | |
5c831c24 GS |
639 | /* clear utf8 character classes */ |
640 | SvREFCNT_dec(PL_utf8_alnum); | |
641 | SvREFCNT_dec(PL_utf8_alnumc); | |
642 | SvREFCNT_dec(PL_utf8_ascii); | |
643 | SvREFCNT_dec(PL_utf8_alpha); | |
644 | SvREFCNT_dec(PL_utf8_space); | |
645 | SvREFCNT_dec(PL_utf8_cntrl); | |
646 | SvREFCNT_dec(PL_utf8_graph); | |
647 | SvREFCNT_dec(PL_utf8_digit); | |
648 | SvREFCNT_dec(PL_utf8_upper); | |
649 | SvREFCNT_dec(PL_utf8_lower); | |
650 | SvREFCNT_dec(PL_utf8_print); | |
651 | SvREFCNT_dec(PL_utf8_punct); | |
652 | SvREFCNT_dec(PL_utf8_xdigit); | |
653 | SvREFCNT_dec(PL_utf8_mark); | |
654 | SvREFCNT_dec(PL_utf8_toupper); | |
655 | SvREFCNT_dec(PL_utf8_tolower); | |
656 | PL_utf8_alnum = Nullsv; | |
657 | PL_utf8_alnumc = Nullsv; | |
658 | PL_utf8_ascii = Nullsv; | |
659 | PL_utf8_alpha = Nullsv; | |
660 | PL_utf8_space = Nullsv; | |
661 | PL_utf8_cntrl = Nullsv; | |
662 | PL_utf8_graph = Nullsv; | |
663 | PL_utf8_digit = Nullsv; | |
664 | PL_utf8_upper = Nullsv; | |
665 | PL_utf8_lower = Nullsv; | |
666 | PL_utf8_print = Nullsv; | |
667 | PL_utf8_punct = Nullsv; | |
668 | PL_utf8_xdigit = Nullsv; | |
669 | PL_utf8_mark = Nullsv; | |
670 | PL_utf8_toupper = Nullsv; | |
671 | PL_utf8_totitle = Nullsv; | |
672 | PL_utf8_tolower = Nullsv; | |
673 | ||
971a9dd3 GS |
674 | if (!specialWARN(PL_compiling.cop_warnings)) |
675 | SvREFCNT_dec(PL_compiling.cop_warnings); | |
5c831c24 | 676 | PL_compiling.cop_warnings = Nullsv; |
ac27b0f5 NIS |
677 | if (!specialCopIO(PL_compiling.cop_io)) |
678 | SvREFCNT_dec(PL_compiling.cop_io); | |
679 | PL_compiling.cop_io = Nullsv; | |
f4dd75d9 GS |
680 | #ifdef USE_ITHREADS |
681 | Safefree(CopFILE(&PL_compiling)); | |
682 | CopFILE(&PL_compiling) = Nullch; | |
683 | Safefree(CopSTASHPV(&PL_compiling)); | |
684 | #else | |
01724ea0 | 685 | SvREFCNT_dec(CopFILEGV(&PL_compiling)); |
f4dd75d9 GS |
686 | CopFILEGV(&PL_compiling) = Nullgv; |
687 | /* cop_stash is not refcounted */ | |
01724ea0 | 688 | #endif |
5c831c24 | 689 | |
a0d0e21e | 690 | /* Prepare to destruct main symbol table. */ |
5f05dabc | 691 | |
3280af22 NIS |
692 | hv = PL_defstash; |
693 | PL_defstash = 0; | |
a0d0e21e | 694 | SvREFCNT_dec(hv); |
5c831c24 GS |
695 | SvREFCNT_dec(PL_curstname); |
696 | PL_curstname = Nullsv; | |
a0d0e21e | 697 | |
5a844595 GS |
698 | /* clear queued errors */ |
699 | SvREFCNT_dec(PL_errors); | |
700 | PL_errors = Nullsv; | |
701 | ||
a0d0e21e | 702 | FREETMPS; |
0453d815 | 703 | if (destruct_level >= 2 && ckWARN_d(WARN_INTERNAL)) { |
3280af22 | 704 | if (PL_scopestack_ix != 0) |
0453d815 PM |
705 | Perl_warner(aTHX_ WARN_INTERNAL, |
706 | "Unbalanced scopes: %ld more ENTERs than LEAVEs\n", | |
3280af22 NIS |
707 | (long)PL_scopestack_ix); |
708 | if (PL_savestack_ix != 0) | |
0453d815 PM |
709 | Perl_warner(aTHX_ WARN_INTERNAL, |
710 | "Unbalanced saves: %ld more saves than restores\n", | |
3280af22 NIS |
711 | (long)PL_savestack_ix); |
712 | if (PL_tmps_floor != -1) | |
0453d815 | 713 | Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced tmps: %ld more allocs than frees\n", |
3280af22 | 714 | (long)PL_tmps_floor + 1); |
a0d0e21e | 715 | if (cxstack_ix != -1) |
0453d815 | 716 | Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced context: %ld more PUSHes than POPs\n", |
ff0cee69 | 717 | (long)cxstack_ix + 1); |
a0d0e21e | 718 | } |
8990e307 LW |
719 | |
720 | /* Now absolutely destruct everything, somehow or other, loops or no. */ | |
d33b2eba | 721 | SvFLAGS(PL_fdpid) |= SVTYPEMASK; /* don't clean out pid table now */ |
3280af22 | 722 | SvFLAGS(PL_strtab) |= SVTYPEMASK; /* don't clean out strtab now */ |
5226ed68 JH |
723 | |
724 | /* the 2 is for PL_fdpid and PL_strtab */ | |
725 | while (PL_sv_count > 2 && sv_clean_all()) | |
726 | ; | |
727 | ||
d33b2eba GS |
728 | SvFLAGS(PL_fdpid) &= ~SVTYPEMASK; |
729 | SvFLAGS(PL_fdpid) |= SVt_PVAV; | |
3280af22 NIS |
730 | SvFLAGS(PL_strtab) &= ~SVTYPEMASK; |
731 | SvFLAGS(PL_strtab) |= SVt_PVHV; | |
d33b2eba | 732 | |
d4777f27 GS |
733 | AvREAL_off(PL_fdpid); /* no surviving entries */ |
734 | SvREFCNT_dec(PL_fdpid); /* needed in io_close() */ | |
d33b2eba GS |
735 | PL_fdpid = Nullav; |
736 | ||
6c644e78 GS |
737 | #ifdef HAVE_INTERP_INTERN |
738 | sys_intern_clear(); | |
739 | #endif | |
740 | ||
6e72f9df | 741 | /* Destruct the global string table. */ |
742 | { | |
743 | /* Yell and reset the HeVAL() slots that are still holding refcounts, | |
744 | * so that sv_free() won't fail on them. | |
745 | */ | |
746 | I32 riter; | |
747 | I32 max; | |
748 | HE *hent; | |
749 | HE **array; | |
750 | ||
751 | riter = 0; | |
3280af22 NIS |
752 | max = HvMAX(PL_strtab); |
753 | array = HvARRAY(PL_strtab); | |
6e72f9df | 754 | hent = array[0]; |
755 | for (;;) { | |
0453d815 PM |
756 | if (hent && ckWARN_d(WARN_INTERNAL)) { |
757 | Perl_warner(aTHX_ WARN_INTERNAL, | |
758 | "Unbalanced string table refcount: (%d) for \"%s\"", | |
6e72f9df | 759 | HeVAL(hent) - Nullsv, HeKEY(hent)); |
760 | HeVAL(hent) = Nullsv; | |
761 | hent = HeNEXT(hent); | |
762 | } | |
763 | if (!hent) { | |
764 | if (++riter > max) | |
765 | break; | |
766 | hent = array[riter]; | |
767 | } | |
768 | } | |
769 | } | |
3280af22 | 770 | SvREFCNT_dec(PL_strtab); |
6e72f9df | 771 | |
e652bb2f | 772 | #ifdef USE_ITHREADS |
a0739874 DM |
773 | /* free the pointer table used for cloning */ |
774 | ptr_table_free(PL_ptr_table); | |
53186e96 | 775 | #endif |
a0739874 | 776 | |
d33b2eba GS |
777 | /* free special SVs */ |
778 | ||
779 | SvREFCNT(&PL_sv_yes) = 0; | |
780 | sv_clear(&PL_sv_yes); | |
781 | SvANY(&PL_sv_yes) = NULL; | |
4c5e2b0d | 782 | SvFLAGS(&PL_sv_yes) = 0; |
d33b2eba GS |
783 | |
784 | SvREFCNT(&PL_sv_no) = 0; | |
785 | sv_clear(&PL_sv_no); | |
786 | SvANY(&PL_sv_no) = NULL; | |
4c5e2b0d | 787 | SvFLAGS(&PL_sv_no) = 0; |
01724ea0 GS |
788 | |
789 | SvREFCNT(&PL_sv_undef) = 0; | |
790 | SvREADONLY_off(&PL_sv_undef); | |
d33b2eba | 791 | |
0453d815 PM |
792 | if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL)) |
793 | Perl_warner(aTHX_ WARN_INTERNAL,"Scalars leaked: %ld\n", (long)PL_sv_count); | |
6e72f9df | 794 | |
3280af22 | 795 | Safefree(PL_origfilename); |
3280af22 | 796 | Safefree(PL_reg_start_tmp); |
5c5e4c24 IZ |
797 | if (PL_reg_curpm) |
798 | Safefree(PL_reg_curpm); | |
82ba1be6 | 799 | Safefree(PL_reg_poscache); |
3280af22 NIS |
800 | Safefree(HeKEY_hek(&PL_hv_fetch_ent_mh)); |
801 | Safefree(PL_op_mask); | |
cf36064f GS |
802 | Safefree(PL_psig_ptr); |
803 | Safefree(PL_psig_name); | |
2c2666fc | 804 | Safefree(PL_bitcount); |
ce08f86c | 805 | Safefree(PL_psig_pend); |
6e72f9df | 806 | nuke_stacks(); |
3280af22 | 807 | PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */ |
ac27b0f5 | 808 | |
a0d0e21e | 809 | DEBUG_P(debprofdump()); |
11343788 | 810 | #ifdef USE_THREADS |
5f08fbcd | 811 | MUTEX_DESTROY(&PL_strtab_mutex); |
533c011a NIS |
812 | MUTEX_DESTROY(&PL_sv_mutex); |
813 | MUTEX_DESTROY(&PL_eval_mutex); | |
5ff3f7a4 | 814 | MUTEX_DESTROY(&PL_cred_mutex); |
3d35f11b | 815 | MUTEX_DESTROY(&PL_fdpid_mutex); |
533c011a | 816 | COND_DESTROY(&PL_eval_cond); |
11d617a5 GS |
817 | #ifdef EMULATE_ATOMIC_REFCOUNTS |
818 | MUTEX_DESTROY(&PL_svref_mutex); | |
819 | #endif /* EMULATE_ATOMIC_REFCOUNTS */ | |
fc36a67e | 820 | |
8023c3ce | 821 | /* As the penultimate thing, free the non-arena SV for thrsv */ |
533c011a NIS |
822 | Safefree(SvPVX(PL_thrsv)); |
823 | Safefree(SvANY(PL_thrsv)); | |
824 | Safefree(PL_thrsv); | |
825 | PL_thrsv = Nullsv; | |
8023c3ce | 826 | #endif /* USE_THREADS */ |
d33b2eba | 827 | |
e5dd39fc AB |
828 | #ifdef USE_REENTRANT_API |
829 | Safefree(PL_reentrant_buffer->tmbuff); | |
830 | Safefree(PL_reentrant_buffer); | |
831 | #endif | |
832 | ||
612f20c3 GS |
833 | sv_free_arenas(); |
834 | ||
fc36a67e | 835 | /* As the absolutely last thing, free the non-arena SV for mess() */ |
836 | ||
3280af22 | 837 | if (PL_mess_sv) { |
9c63abab GS |
838 | /* it could have accumulated taint magic */ |
839 | if (SvTYPE(PL_mess_sv) >= SVt_PVMG) { | |
840 | MAGIC* mg; | |
841 | MAGIC* moremagic; | |
842 | for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) { | |
843 | moremagic = mg->mg_moremagic; | |
14befaf4 DM |
844 | if (mg->mg_ptr && mg->mg_type != PERL_MAGIC_regex_global |
845 | && mg->mg_len >= 0) | |
9c63abab GS |
846 | Safefree(mg->mg_ptr); |
847 | Safefree(mg); | |
848 | } | |
849 | } | |
fc36a67e | 850 | /* we know that type >= SVt_PV */ |
155aba94 | 851 | (void)SvOOK_off(PL_mess_sv); |
3280af22 NIS |
852 | Safefree(SvPVX(PL_mess_sv)); |
853 | Safefree(SvANY(PL_mess_sv)); | |
854 | Safefree(PL_mess_sv); | |
855 | PL_mess_sv = Nullsv; | |
fc36a67e | 856 | } |
79072805 LW |
857 | } |
858 | ||
954c1994 GS |
859 | /* |
860 | =for apidoc perl_free | |
861 | ||
862 | Releases a Perl interpreter. See L<perlembed>. | |
863 | ||
864 | =cut | |
865 | */ | |
866 | ||
79072805 | 867 | void |
0cb96387 | 868 | perl_free(pTHXx) |
79072805 | 869 | { |
c5be433b | 870 | #if defined(PERL_OBJECT) |
7f0d82f7 | 871 | PerlMem_free(this); |
76e3520e | 872 | #else |
2986a63f | 873 | # if defined(WIN32) || defined(NETWARE) |
ce3e5b80 | 874 | # if defined(PERL_IMPLICIT_SYS) |
2986a63f JH |
875 | #ifdef NETWARE |
876 | void *host = nw_internal_host; | |
877 | #else | |
878 | void *host = w32_internal_host; | |
879 | #endif | |
880 | #ifndef NETWARE | |
881 | if (PerlProc_lasthost()) { | |
ce08f86c | 882 | PerlIO_cleanup(); |
2986a63f JH |
883 | } |
884 | #endif | |
7f0d82f7 | 885 | PerlMem_free(aTHXx); |
2986a63f JH |
886 | #ifdef NETWARE |
887 | nw5_delete_internal_host(host); | |
888 | #else | |
889 | win32_delete_internal_host(host); | |
890 | #endif | |
ce3e5b80 | 891 | #else |
ce08f86c | 892 | PerlIO_cleanup(); |
ce3e5b80 NIS |
893 | PerlMem_free(aTHXx); |
894 | #endif | |
1c0ca838 GS |
895 | # else |
896 | PerlMem_free(aTHXx); | |
897 | # endif | |
76e3520e | 898 | #endif |
79072805 LW |
899 | } |
900 | ||
4b556e6c | 901 | void |
864dbfa3 | 902 | Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr) |
4b556e6c | 903 | { |
3280af22 NIS |
904 | Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry); |
905 | PL_exitlist[PL_exitlistlen].fn = fn; | |
906 | PL_exitlist[PL_exitlistlen].ptr = ptr; | |
907 | ++PL_exitlistlen; | |
4b556e6c JD |
908 | } |
909 | ||
954c1994 GS |
910 | /* |
911 | =for apidoc perl_parse | |
912 | ||
913 | Tells a Perl interpreter to parse a Perl script. See L<perlembed>. | |
914 | ||
915 | =cut | |
916 | */ | |
917 | ||
79072805 | 918 | int |
0cb96387 | 919 | perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env) |
8d063cd8 | 920 | { |
6224f72b | 921 | I32 oldscope; |
6224f72b | 922 | int ret; |
db36c5a1 | 923 | dJMPENV; |
cea2e8a9 GS |
924 | #ifdef USE_THREADS |
925 | dTHX; | |
926 | #endif | |
8d063cd8 | 927 | |
a687059c LW |
928 | #ifdef SETUID_SCRIPTS_ARE_SECURE_NOW |
929 | #ifdef IAMSUID | |
930 | #undef IAMSUID | |
cea2e8a9 | 931 | Perl_croak(aTHX_ "suidperl is no longer needed since the kernel can now execute\n\ |
a687059c LW |
932 | setuid perl scripts securely.\n"); |
933 | #endif | |
934 | #endif | |
935 | ||
8f1f23e8 | 936 | #if defined(__DYNAMIC__) && (defined(NeXT) || defined(__NeXT__)) |
6e72f9df | 937 | _dyld_lookup_and_bind |
938 | ("__environ", (unsigned long *) &environ_pointer, NULL); | |
939 | #endif /* environ */ | |
940 | ||
3280af22 | 941 | PL_origargc = argc; |
bc0a45ed DM |
942 | { |
943 | /* we copy rather than point to argv | |
944 | * since perl_clone will copy and perl_destruct | |
945 | * has no way of knowing if we've made a copy or | |
946 | * just point to argv | |
947 | */ | |
948 | int i = PL_origargc; | |
949 | New(0, PL_origargv, i+1, char*); | |
950 | PL_origargv[i] = '\0'; | |
951 | while (i-- > 0) { | |
952 | PL_origargv[i] = savepv(argv[i]); | |
953 | } | |
954 | } | |
955 | ||
fa6a1c44 | 956 | #ifdef USE_ENVIRON_ARRAY |
3280af22 | 957 | PL_origenviron = environ; |
a0d0e21e LW |
958 | #endif |
959 | ||
3280af22 | 960 | if (PL_do_undump) { |
a0d0e21e LW |
961 | |
962 | /* Come here if running an undumped a.out. */ | |
963 | ||
3280af22 NIS |
964 | PL_origfilename = savepv(argv[0]); |
965 | PL_do_undump = FALSE; | |
a0d0e21e | 966 | cxstack_ix = -1; /* start label stack again */ |
748a9306 | 967 | init_ids(); |
a0d0e21e LW |
968 | init_postdump_symbols(argc,argv,env); |
969 | return 0; | |
970 | } | |
971 | ||
3280af22 NIS |
972 | if (PL_main_root) { |
973 | PL_curpad = AvARRAY(PL_comppad); | |
974 | op_free(PL_main_root); | |
975 | PL_main_root = Nullop; | |
ff0cee69 | 976 | } |
3280af22 NIS |
977 | PL_main_start = Nullop; |
978 | SvREFCNT_dec(PL_main_cv); | |
979 | PL_main_cv = Nullcv; | |
79072805 | 980 | |
3280af22 NIS |
981 | time(&PL_basetime); |
982 | oldscope = PL_scopestack_ix; | |
599cee73 | 983 | PL_dowarn = G_WARN_OFF; |
f86702cc | 984 | |
14dd3ad8 GS |
985 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
986 | CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vparse_body), env, xsinit); | |
987 | #else | |
988 | JMPENV_PUSH(ret); | |
989 | #endif | |
6224f72b | 990 | switch (ret) { |
312caa8e | 991 | case 0: |
14dd3ad8 GS |
992 | #ifndef PERL_FLEXIBLE_EXCEPTIONS |
993 | parse_body(env,xsinit); | |
994 | #endif | |
7d30b5c4 GS |
995 | if (PL_checkav) |
996 | call_list(oldscope, PL_checkav); | |
14dd3ad8 GS |
997 | ret = 0; |
998 | break; | |
6224f72b GS |
999 | case 1: |
1000 | STATUS_ALL_FAILURE; | |
1001 | /* FALL THROUGH */ | |
1002 | case 2: | |
1003 | /* my_exit() was called */ | |
3280af22 | 1004 | while (PL_scopestack_ix > oldscope) |
6224f72b GS |
1005 | LEAVE; |
1006 | FREETMPS; | |
3280af22 | 1007 | PL_curstash = PL_defstash; |
7d30b5c4 GS |
1008 | if (PL_checkav) |
1009 | call_list(oldscope, PL_checkav); | |
14dd3ad8 GS |
1010 | ret = STATUS_NATIVE_EXPORT; |
1011 | break; | |
6224f72b | 1012 | case 3: |
bf49b057 | 1013 | PerlIO_printf(Perl_error_log, "panic: top_env\n"); |
14dd3ad8 GS |
1014 | ret = 1; |
1015 | break; | |
6224f72b | 1016 | } |
14dd3ad8 GS |
1017 | JMPENV_POP; |
1018 | return ret; | |
1019 | } | |
1020 | ||
1021 | #ifdef PERL_FLEXIBLE_EXCEPTIONS | |
1022 | STATIC void * | |
1023 | S_vparse_body(pTHX_ va_list args) | |
1024 | { | |
1025 | char **env = va_arg(args, char**); | |
1026 | XSINIT_t xsinit = va_arg(args, XSINIT_t); | |
1027 | ||
1028 | return parse_body(env, xsinit); | |
312caa8e | 1029 | } |
14dd3ad8 | 1030 | #endif |
312caa8e CS |
1031 | |
1032 | STATIC void * | |
14dd3ad8 | 1033 | S_parse_body(pTHX_ char **env, XSINIT_t xsinit) |
312caa8e | 1034 | { |
312caa8e CS |
1035 | int argc = PL_origargc; |
1036 | char **argv = PL_origargv; | |
312caa8e CS |
1037 | char *scriptname = NULL; |
1038 | int fdscript = -1; | |
1039 | VOL bool dosearch = FALSE; | |
1040 | char *validarg = ""; | |
1041 | AV* comppadlist; | |
1042 | register SV *sv; | |
1043 | register char *s; | |
659ca9ea | 1044 | char *popts, *cddir = Nullch; |
312caa8e | 1045 | |
3280af22 | 1046 | sv_setpvn(PL_linestr,"",0); |
79cb57f6 | 1047 | sv = newSVpvn("",0); /* first used for -I flags */ |
6224f72b GS |
1048 | SAVEFREESV(sv); |
1049 | init_main_stash(); | |
54310121 | 1050 | |
6224f72b GS |
1051 | for (argc--,argv++; argc > 0; argc--,argv++) { |
1052 | if (argv[0][0] != '-' || !argv[0][1]) | |
1053 | break; | |
1054 | #ifdef DOSUID | |
1055 | if (*validarg) | |
1056 | validarg = " PHOOEY "; | |
1057 | else | |
1058 | validarg = argv[0]; | |
13281fa4 | 1059 | #endif |
6224f72b GS |
1060 | s = argv[0]+1; |
1061 | reswitch: | |
1062 | switch (*s) { | |
729a02f2 GS |
1063 | case 'C': |
1064 | #ifdef WIN32 | |
c0932edc | 1065 | win32_argv2utf8(argc-1, argv+1); |
729a02f2 GS |
1066 | /* FALL THROUGH */ |
1067 | #endif | |
1d5472a9 GS |
1068 | #ifndef PERL_STRICT_CR |
1069 | case '\r': | |
1070 | #endif | |
6224f72b GS |
1071 | case ' ': |
1072 | case '0': | |
1073 | case 'F': | |
1074 | case 'a': | |
1075 | case 'c': | |
1076 | case 'd': | |
1077 | case 'D': | |
1078 | case 'h': | |
1079 | case 'i': | |
1080 | case 'l': | |
1081 | case 'M': | |
1082 | case 'm': | |
1083 | case 'n': | |
1084 | case 'p': | |
1085 | case 's': | |
1086 | case 'u': | |
1087 | case 'U': | |
1088 | case 'v': | |
599cee73 PM |
1089 | case 'W': |
1090 | case 'X': | |
6224f72b | 1091 | case 'w': |
155aba94 | 1092 | if ((s = moreswitches(s))) |
6224f72b GS |
1093 | goto reswitch; |
1094 | break; | |
33b78306 | 1095 | |
6224f72b | 1096 | case 'T': |
3280af22 | 1097 | PL_tainting = TRUE; |
6224f72b GS |
1098 | s++; |
1099 | goto reswitch; | |
f86702cc | 1100 | |
6224f72b | 1101 | case 'e': |
bf4acbe4 GS |
1102 | #ifdef MACOS_TRADITIONAL |
1103 | /* ignore -e for Dev:Pseudo argument */ | |
1104 | if (argv[1] && !strcmp(argv[1], "Dev:Pseudo")) | |
ac27b0f5 | 1105 | break; |
bf4acbe4 | 1106 | #endif |
3280af22 | 1107 | if (PL_euid != PL_uid || PL_egid != PL_gid) |
cea2e8a9 | 1108 | Perl_croak(aTHX_ "No -e allowed in setuid scripts"); |
3280af22 | 1109 | if (!PL_e_script) { |
79cb57f6 | 1110 | PL_e_script = newSVpvn("",0); |
0cb96387 | 1111 | filter_add(read_e_script, NULL); |
6224f72b GS |
1112 | } |
1113 | if (*++s) | |
3280af22 | 1114 | sv_catpv(PL_e_script, s); |
6224f72b | 1115 | else if (argv[1]) { |
3280af22 | 1116 | sv_catpv(PL_e_script, argv[1]); |
6224f72b GS |
1117 | argc--,argv++; |
1118 | } | |
1119 | else | |
cea2e8a9 | 1120 | Perl_croak(aTHX_ "No code specified for -e"); |
3280af22 | 1121 | sv_catpv(PL_e_script, "\n"); |
6224f72b | 1122 | break; |
afe37c7d | 1123 | |
6224f72b GS |
1124 | case 'I': /* -I handled both here and in moreswitches() */ |
1125 | forbid_setid("-I"); | |
1126 | if (!*++s && (s=argv[1]) != Nullch) { | |
1127 | argc--,argv++; | |
1128 | } | |
6224f72b | 1129 | if (s && *s) { |
0df16ed7 GS |
1130 | char *p; |
1131 | STRLEN len = strlen(s); | |
1132 | p = savepvn(s, len); | |
9c8a64f0 | 1133 | incpush(p, TRUE, TRUE); |
0df16ed7 GS |
1134 | sv_catpvn(sv, "-I", 2); |
1135 | sv_catpvn(sv, p, len); | |
1136 | sv_catpvn(sv, " ", 1); | |
6224f72b | 1137 | Safefree(p); |
0df16ed7 GS |
1138 | } |
1139 | else | |
a67e862a | 1140 | Perl_croak(aTHX_ "No directory specified for -I"); |
6224f72b GS |
1141 | break; |
1142 | case 'P': | |
1143 | forbid_setid("-P"); | |
3280af22 | 1144 | PL_preprocess = TRUE; |
6224f72b GS |
1145 | s++; |
1146 | goto reswitch; | |
1147 | case 'S': | |
1148 | forbid_setid("-S"); | |
1149 | dosearch = TRUE; | |
1150 | s++; | |
1151 | goto reswitch; | |
1152 | case 'V': | |
3280af22 NIS |
1153 | if (!PL_preambleav) |
1154 | PL_preambleav = newAV(); | |
1155 | av_push(PL_preambleav, newSVpv("use Config qw(myconfig config_vars)",0)); | |
6224f72b | 1156 | if (*++s != ':') { |
3280af22 | 1157 | PL_Sv = newSVpv("print myconfig();",0); |
6224f72b | 1158 | #ifdef VMS |
6b88bc9c | 1159 | sv_catpv(PL_Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\","); |
6224f72b | 1160 | #else |
3280af22 | 1161 | sv_catpv(PL_Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\","); |
6224f72b | 1162 | #endif |
3280af22 | 1163 | sv_catpv(PL_Sv,"\" Compile-time options:"); |
6224f72b | 1164 | # ifdef DEBUGGING |
3280af22 | 1165 | sv_catpv(PL_Sv," DEBUGGING"); |
6224f72b | 1166 | # endif |
6224f72b | 1167 | # ifdef MULTIPLICITY |
8f872242 | 1168 | sv_catpv(PL_Sv," MULTIPLICITY"); |
6224f72b | 1169 | # endif |
b363f7ed GS |
1170 | # ifdef USE_THREADS |
1171 | sv_catpv(PL_Sv," USE_THREADS"); | |
1172 | # endif | |
ac5e8965 JH |
1173 | # ifdef USE_ITHREADS |
1174 | sv_catpv(PL_Sv," USE_ITHREADS"); | |
1175 | # endif | |
10cc9d2a JH |
1176 | # ifdef USE_64_BIT_INT |
1177 | sv_catpv(PL_Sv," USE_64_BIT_INT"); | |
1178 | # endif | |
1179 | # ifdef USE_64_BIT_ALL | |
1180 | sv_catpv(PL_Sv," USE_64_BIT_ALL"); | |
ac5e8965 JH |
1181 | # endif |
1182 | # ifdef USE_LONG_DOUBLE | |
1183 | sv_catpv(PL_Sv," USE_LONG_DOUBLE"); | |
1184 | # endif | |
53430762 JH |
1185 | # ifdef USE_LARGE_FILES |
1186 | sv_catpv(PL_Sv," USE_LARGE_FILES"); | |
1187 | # endif | |
ac5e8965 JH |
1188 | # ifdef USE_SOCKS |
1189 | sv_catpv(PL_Sv," USE_SOCKS"); | |
1190 | # endif | |
b363f7ed GS |
1191 | # ifdef PERL_OBJECT |
1192 | sv_catpv(PL_Sv," PERL_OBJECT"); | |
1193 | # endif | |
1194 | # ifdef PERL_IMPLICIT_CONTEXT | |
1195 | sv_catpv(PL_Sv," PERL_IMPLICIT_CONTEXT"); | |
1196 | # endif | |
1197 | # ifdef PERL_IMPLICIT_SYS | |
1198 | sv_catpv(PL_Sv," PERL_IMPLICIT_SYS"); | |
1199 | # endif | |
3280af22 | 1200 | sv_catpv(PL_Sv,"\\n\","); |
b363f7ed | 1201 | |
6224f72b GS |
1202 | #if defined(LOCAL_PATCH_COUNT) |
1203 | if (LOCAL_PATCH_COUNT > 0) { | |
1204 | int i; | |
3280af22 | 1205 | sv_catpv(PL_Sv,"\" Locally applied patches:\\n\","); |
6224f72b | 1206 | for (i = 1; i <= LOCAL_PATCH_COUNT; i++) { |
3280af22 | 1207 | if (PL_localpatches[i]) |
cea2e8a9 | 1208 | Perl_sv_catpvf(aTHX_ PL_Sv,"q\" \t%s\n\",",PL_localpatches[i]); |
6224f72b GS |
1209 | } |
1210 | } | |
1211 | #endif | |
cea2e8a9 | 1212 | Perl_sv_catpvf(aTHX_ PL_Sv,"\" Built under %s\\n\"",OSNAME); |
6224f72b GS |
1213 | #ifdef __DATE__ |
1214 | # ifdef __TIME__ | |
cea2e8a9 | 1215 | Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__); |
6224f72b | 1216 | # else |
cea2e8a9 | 1217 | Perl_sv_catpvf(aTHX_ PL_Sv,",\" Compiled on %s\\n\"",__DATE__); |
6224f72b GS |
1218 | # endif |
1219 | #endif | |
3280af22 | 1220 | sv_catpv(PL_Sv, "; \ |
6224f72b | 1221 | $\"=\"\\n \"; \ |
69fcd688 JH |
1222 | @env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; "); |
1223 | #ifdef __CYGWIN__ | |
1224 | sv_catpv(PL_Sv,"\ | |
1225 | push @env, \"CYGWIN=\\\"$ENV{CYGWIN}\\\"\";"); | |
1226 | #endif | |
1227 | sv_catpv(PL_Sv, "\ | |
6224f72b GS |
1228 | print \" \\%ENV:\\n @env\\n\" if @env; \ |
1229 | print \" \\@INC:\\n @INC\\n\";"); | |
1230 | } | |
1231 | else { | |
3280af22 NIS |
1232 | PL_Sv = newSVpv("config_vars(qw(",0); |
1233 | sv_catpv(PL_Sv, ++s); | |
1234 | sv_catpv(PL_Sv, "))"); | |
6224f72b GS |
1235 | s += strlen(s); |
1236 | } | |
3280af22 | 1237 | av_push(PL_preambleav, PL_Sv); |
6224f72b GS |
1238 | scriptname = BIT_BUCKET; /* don't look for script or read stdin */ |
1239 | goto reswitch; | |
1240 | case 'x': | |
3280af22 | 1241 | PL_doextract = TRUE; |
6224f72b GS |
1242 | s++; |
1243 | if (*s) | |
f4c556ac | 1244 | cddir = s; |
6224f72b GS |
1245 | break; |
1246 | case 0: | |
1247 | break; | |
1248 | case '-': | |
1249 | if (!*++s || isSPACE(*s)) { | |
1250 | argc--,argv++; | |
1251 | goto switch_end; | |
1252 | } | |
1253 | /* catch use of gnu style long options */ | |
1254 | if (strEQ(s, "version")) { | |
1255 | s = "v"; | |
1256 | goto reswitch; | |
1257 | } | |
1258 | if (strEQ(s, "help")) { | |
1259 | s = "h"; | |
1260 | goto reswitch; | |
1261 | } | |
1262 | s--; | |
1263 | /* FALL THROUGH */ | |
1264 | default: | |
cea2e8a9 | 1265 | Perl_croak(aTHX_ "Unrecognized switch: -%s (-h will show valid options)",s); |
8d063cd8 LW |
1266 | } |
1267 | } | |
6224f72b | 1268 | switch_end: |
54310121 | 1269 | |
f675dbe5 CB |
1270 | if ( |
1271 | #ifndef SECURE_INTERNAL_GETENV | |
1272 | !PL_tainting && | |
1273 | #endif | |
659ca9ea | 1274 | (popts = PerlEnv_getenv("PERL5OPT"))) |
0df16ed7 | 1275 | { |
659ca9ea | 1276 | s = savepv(popts); |
74288ac8 GS |
1277 | while (isSPACE(*s)) |
1278 | s++; | |
1279 | if (*s == '-' && *(s+1) == 'T') | |
1280 | PL_tainting = TRUE; | |
1281 | else { | |
1282 | while (s && *s) { | |
4ea8f8fb | 1283 | char *d; |
74288ac8 GS |
1284 | while (isSPACE(*s)) |
1285 | s++; | |
1286 | if (*s == '-') { | |
1287 | s++; | |
1288 | if (isSPACE(*s)) | |
1289 | continue; | |
1290 | } | |
4ea8f8fb | 1291 | d = s; |
74288ac8 GS |
1292 | if (!*s) |
1293 | break; | |
1294 | if (!strchr("DIMUdmw", *s)) | |
cea2e8a9 | 1295 | Perl_croak(aTHX_ "Illegal switch in PERL5OPT: -%c", *s); |
4ea8f8fb MS |
1296 | while (++s && *s) { |
1297 | if (isSPACE(*s)) { | |
1298 | *s++ = '\0'; | |
1299 | break; | |
1300 | } | |
1301 | } | |
1302 | moreswitches(d); | |
6224f72b | 1303 | } |
6224f72b GS |
1304 | } |
1305 | } | |
a0d0e21e | 1306 | |
6224f72b GS |
1307 | if (!scriptname) |
1308 | scriptname = argv[0]; | |
3280af22 | 1309 | if (PL_e_script) { |
6224f72b GS |
1310 | argc++,argv--; |
1311 | scriptname = BIT_BUCKET; /* don't look for script or read stdin */ | |
1312 | } | |
1313 | else if (scriptname == Nullch) { | |
1314 | #ifdef MSDOS | |
1315 | if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) ) | |
1316 | moreswitches("h"); | |
1317 | #endif | |
1318 | scriptname = "-"; | |
1319 | } | |
1320 | ||
1321 | init_perllib(); | |
1322 | ||
1323 | open_script(scriptname,dosearch,sv,&fdscript); | |
1324 | ||
1325 | validate_suid(validarg, scriptname,fdscript); | |
1326 | ||
64ca3a65 | 1327 | #ifndef PERL_MICRO |
0b5b802d GS |
1328 | #if defined(SIGCHLD) || defined(SIGCLD) |
1329 | { | |
1330 | #ifndef SIGCHLD | |
1331 | # define SIGCHLD SIGCLD | |
1332 | #endif | |
1333 | Sighandler_t sigstate = rsignal_state(SIGCHLD); | |
1334 | if (sigstate == SIG_IGN) { | |
1335 | if (ckWARN(WARN_SIGNAL)) | |
1336 | Perl_warner(aTHX_ WARN_SIGNAL, | |
1337 | "Can't ignore signal CHLD, forcing to default"); | |
1338 | (void)rsignal(SIGCHLD, (Sighandler_t)SIG_DFL); | |
1339 | } | |
1340 | } | |
1341 | #endif | |
64ca3a65 | 1342 | #endif |
0b5b802d | 1343 | |
bf4acbe4 GS |
1344 | #ifdef MACOS_TRADITIONAL |
1345 | if (PL_doextract || gMacPerl_AlwaysExtract) { | |
1346 | #else | |
f4c556ac | 1347 | if (PL_doextract) { |
bf4acbe4 | 1348 | #endif |
6224f72b | 1349 | find_beginning(); |
f4c556ac GS |
1350 | if (cddir && PerlDir_chdir(cddir) < 0) |
1351 | Perl_croak(aTHX_ "Can't chdir to %s",cddir); | |
1352 | ||
1353 | } | |
6224f72b | 1354 | |
3280af22 NIS |
1355 | PL_main_cv = PL_compcv = (CV*)NEWSV(1104,0); |
1356 | sv_upgrade((SV *)PL_compcv, SVt_PVCV); | |
1357 | CvUNIQUE_on(PL_compcv); | |
1358 | ||
1359 | PL_comppad = newAV(); | |
1360 | av_push(PL_comppad, Nullsv); | |
1361 | PL_curpad = AvARRAY(PL_comppad); | |
1362 | PL_comppad_name = newAV(); | |
1363 | PL_comppad_name_fill = 0; | |
1364 | PL_min_intro_pending = 0; | |
1365 | PL_padix = 0; | |
6224f72b | 1366 | #ifdef USE_THREADS |
79cb57f6 | 1367 | av_store(PL_comppad_name, 0, newSVpvn("@_", 2)); |
533c011a NIS |
1368 | PL_curpad[0] = (SV*)newAV(); |
1369 | SvPADMY_on(PL_curpad[0]); /* XXX Needed? */ | |
1370 | CvOWNER(PL_compcv) = 0; | |
1371 | New(666, CvMUTEXP(PL_compcv), 1, perl_mutex); | |
1372 | MUTEX_INIT(CvMUTEXP(PL_compcv)); | |
6224f72b GS |
1373 | #endif /* USE_THREADS */ |
1374 | ||
1375 | comppadlist = newAV(); | |
1376 | AvREAL_off(comppadlist); | |
3280af22 NIS |
1377 | av_store(comppadlist, 0, (SV*)PL_comppad_name); |
1378 | av_store(comppadlist, 1, (SV*)PL_comppad); | |
1379 | CvPADLIST(PL_compcv) = comppadlist; | |
6224f72b | 1380 | |
0c4f7ff0 | 1381 | boot_core_PerlIO(); |
6224f72b | 1382 | boot_core_UNIVERSAL(); |
9a34ef1d | 1383 | #ifndef PERL_MICRO |
09bef843 | 1384 | boot_core_xsutils(); |
9a34ef1d | 1385 | #endif |
6224f72b GS |
1386 | |
1387 | if (xsinit) | |
0cb96387 | 1388 | (*xsinit)(aTHXo); /* in case linked C routines want magical variables */ |
64ca3a65 | 1389 | #ifndef PERL_MICRO |
ed79a026 | 1390 | #if defined(VMS) || defined(WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(EPOC) |
c5be433b | 1391 | init_os_extras(); |
6224f72b | 1392 | #endif |
64ca3a65 | 1393 | #endif |
6224f72b | 1394 | |
29209bc5 | 1395 | #ifdef USE_SOCKS |
1b9c9cf5 DH |
1396 | # ifdef HAS_SOCKS5_INIT |
1397 | socks5_init(argv[0]); | |
1398 | # else | |
29209bc5 | 1399 | SOCKSinit(argv[0]); |
1b9c9cf5 | 1400 | # endif |
ac27b0f5 | 1401 | #endif |
29209bc5 | 1402 | |
6224f72b GS |
1403 | init_predump_symbols(); |
1404 | /* init_postdump_symbols not currently designed to be called */ | |
1405 | /* more than once (ENV isn't cleared first, for example) */ | |
1406 | /* But running with -u leaves %ENV & @ARGV undefined! XXX */ | |
3280af22 | 1407 | if (!PL_do_undump) |
6224f72b GS |
1408 | init_postdump_symbols(argc,argv,env); |
1409 | ||
1410 | init_lexer(); | |
1411 | ||
1412 | /* now parse the script */ | |
1413 | ||
1414 | SETERRNO(0,SS$_NORMAL); | |
3280af22 | 1415 | PL_error_count = 0; |
bf4acbe4 GS |
1416 | #ifdef MACOS_TRADITIONAL |
1417 | if (gMacPerl_SyntaxError = (yyparse() || PL_error_count)) { | |
1418 | if (PL_minus_c) | |
1419 | Perl_croak(aTHX_ "%s had compilation errors.\n", MacPerl_MPWFileName(PL_origfilename)); | |
1420 | else { | |
1421 | Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n", | |
1422 | MacPerl_MPWFileName(PL_origfilename)); | |
1423 | } | |
1424 | } | |
1425 | #else | |
3280af22 NIS |
1426 | if (yyparse() || PL_error_count) { |
1427 | if (PL_minus_c) | |
cea2e8a9 | 1428 | Perl_croak(aTHX_ "%s had compilation errors.\n", PL_origfilename); |
6224f72b | 1429 | else { |
cea2e8a9 | 1430 | Perl_croak(aTHX_ "Execution of %s aborted due to compilation errors.\n", |
097ee67d | 1431 | PL_origfilename); |
6224f72b GS |
1432 | } |
1433 | } | |
bf4acbe4 | 1434 | #endif |
57843af0 | 1435 | CopLINE_set(PL_curcop, 0); |
3280af22 NIS |
1436 | PL_curstash = PL_defstash; |
1437 | PL_preprocess = FALSE; | |
1438 | if (PL_e_script) { | |
1439 | SvREFCNT_dec(PL_e_script); | |
1440 | PL_e_script = Nullsv; | |
6224f72b GS |
1441 | } |
1442 | ||
1443 | /* now that script is parsed, we can modify record separator */ | |
3280af22 NIS |
1444 | SvREFCNT_dec(PL_rs); |
1445 | PL_rs = SvREFCNT_inc(PL_nrs); | |
864dbfa3 | 1446 | sv_setsv(get_sv("/", TRUE), PL_rs); |
3280af22 | 1447 | if (PL_do_undump) |
6224f72b GS |
1448 | my_unexec(); |
1449 | ||
57843af0 GS |
1450 | if (isWARN_ONCE) { |
1451 | SAVECOPFILE(PL_curcop); | |
1452 | SAVECOPLINE(PL_curcop); | |
3280af22 | 1453 | gv_check(PL_defstash); |
57843af0 | 1454 | } |
6224f72b GS |
1455 | |
1456 | LEAVE; | |
1457 | FREETMPS; | |
1458 | ||
1459 | #ifdef MYMALLOC | |
1460 | if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2) | |
1461 | dump_mstats("after compilation:"); | |
1462 | #endif | |
1463 | ||
1464 | ENTER; | |
3280af22 | 1465 | PL_restartop = 0; |
312caa8e | 1466 | return NULL; |
6224f72b GS |
1467 | } |
1468 | ||
954c1994 GS |
1469 | /* |
1470 | =for apidoc perl_run | |
1471 | ||
1472 | Tells a Perl interpreter to run. See L<perlembed>. | |
1473 | ||
1474 | =cut | |
1475 | */ | |
1476 | ||
6224f72b | 1477 | int |
0cb96387 | 1478 | perl_run(pTHXx) |
6224f72b | 1479 | { |
6224f72b | 1480 | I32 oldscope; |
14dd3ad8 | 1481 | int ret = 0; |
db36c5a1 | 1482 | dJMPENV; |
cea2e8a9 GS |
1483 | #ifdef USE_THREADS |
1484 | dTHX; | |
1485 | #endif | |
6224f72b | 1486 | |
3280af22 | 1487 | oldscope = PL_scopestack_ix; |
6224f72b | 1488 | |
14dd3ad8 | 1489 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
312caa8e | 1490 | redo_body: |
14dd3ad8 GS |
1491 | CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vrun_body), oldscope); |
1492 | #else | |
1493 | JMPENV_PUSH(ret); | |
1494 | #endif | |
6224f72b GS |
1495 | switch (ret) { |
1496 | case 1: | |
1497 | cxstack_ix = -1; /* start context stack again */ | |
312caa8e | 1498 | goto redo_body; |
14dd3ad8 GS |
1499 | case 0: /* normal completion */ |
1500 | #ifndef PERL_FLEXIBLE_EXCEPTIONS | |
1501 | redo_body: | |
1502 | run_body(oldscope); | |
1503 | #endif | |
1504 | /* FALL THROUGH */ | |
1505 | case 2: /* my_exit() */ | |
3280af22 | 1506 | while (PL_scopestack_ix > oldscope) |
6224f72b GS |
1507 | LEAVE; |
1508 | FREETMPS; | |
3280af22 | 1509 | PL_curstash = PL_defstash; |
6224f72b GS |
1510 | #ifdef MYMALLOC |
1511 | if (PerlEnv_getenv("PERL_DEBUG_MSTATS")) | |
1512 | dump_mstats("after execution: "); | |
1513 | #endif | |
14dd3ad8 GS |
1514 | ret = STATUS_NATIVE_EXPORT; |
1515 | break; | |
6224f72b | 1516 | case 3: |
312caa8e CS |
1517 | if (PL_restartop) { |
1518 | POPSTACK_TO(PL_mainstack); | |
1519 | goto redo_body; | |
6224f72b | 1520 | } |
bf49b057 | 1521 | PerlIO_printf(Perl_error_log, "panic: restartop\n"); |
312caa8e | 1522 | FREETMPS; |
14dd3ad8 GS |
1523 | ret = 1; |
1524 | break; | |
6224f72b GS |
1525 | } |
1526 | ||
14dd3ad8 GS |
1527 | JMPENV_POP; |
1528 | return ret; | |
312caa8e CS |
1529 | } |
1530 | ||
14dd3ad8 | 1531 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
312caa8e | 1532 | STATIC void * |
14dd3ad8 | 1533 | S_vrun_body(pTHX_ va_list args) |
312caa8e | 1534 | { |
312caa8e CS |
1535 | I32 oldscope = va_arg(args, I32); |
1536 | ||
14dd3ad8 GS |
1537 | return run_body(oldscope); |
1538 | } | |
1539 | #endif | |
1540 | ||
1541 | ||
1542 | STATIC void * | |
1543 | S_run_body(pTHX_ I32 oldscope) | |
1544 | { | |
6224f72b | 1545 | DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n", |
3280af22 | 1546 | PL_sawampersand ? "Enabling" : "Omitting")); |
6224f72b | 1547 | |
3280af22 | 1548 | if (!PL_restartop) { |
6224f72b GS |
1549 | DEBUG_x(dump_all()); |
1550 | DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n")); | |
b900a521 JH |
1551 | DEBUG_S(PerlIO_printf(Perl_debug_log, "main thread is 0x%"UVxf"\n", |
1552 | PTR2UV(thr))); | |
6224f72b | 1553 | |
3280af22 | 1554 | if (PL_minus_c) { |
bf4acbe4 GS |
1555 | #ifdef MACOS_TRADITIONAL |
1556 | PerlIO_printf(Perl_error_log, "%s syntax OK\n", MacPerl_MPWFileName(PL_origfilename)); | |
1557 | #else | |
bf49b057 | 1558 | PerlIO_printf(Perl_error_log, "%s syntax OK\n", PL_origfilename); |
bf4acbe4 | 1559 | #endif |
6224f72b GS |
1560 | my_exit(0); |
1561 | } | |
3280af22 | 1562 | if (PERLDB_SINGLE && PL_DBsingle) |
ac27b0f5 | 1563 | sv_setiv(PL_DBsingle, 1); |
3280af22 NIS |
1564 | if (PL_initav) |
1565 | call_list(oldscope, PL_initav); | |
6224f72b GS |
1566 | } |
1567 | ||
1568 | /* do it */ | |
1569 | ||
3280af22 | 1570 | if (PL_restartop) { |
533c011a | 1571 | PL_op = PL_restartop; |
3280af22 | 1572 | PL_restartop = 0; |
cea2e8a9 | 1573 | CALLRUNOPS(aTHX); |
6224f72b | 1574 | } |
3280af22 NIS |
1575 | else if (PL_main_start) { |
1576 | CvDEPTH(PL_main_cv) = 1; | |
533c011a | 1577 | PL_op = PL_main_start; |
cea2e8a9 | 1578 | CALLRUNOPS(aTHX); |
6224f72b GS |
1579 | } |
1580 | ||
f6b3007c JH |
1581 | my_exit(0); |
1582 | /* NOTREACHED */ | |
312caa8e | 1583 | return NULL; |
6224f72b GS |
1584 | } |
1585 | ||
954c1994 GS |
1586 | /* |
1587 | =for apidoc p||get_sv | |
1588 | ||
1589 | Returns the SV of the specified Perl scalar. If C<create> is set and the | |
1590 | Perl variable does not exist then it will be created. If C<create> is not | |
1591 | set and the variable does not exist then NULL is returned. | |
1592 | ||
1593 | =cut | |
1594 | */ | |
1595 | ||
6224f72b | 1596 | SV* |
864dbfa3 | 1597 | Perl_get_sv(pTHX_ const char *name, I32 create) |
6224f72b GS |
1598 | { |
1599 | GV *gv; | |
1600 | #ifdef USE_THREADS | |
1601 | if (name[1] == '\0' && !isALPHA(name[0])) { | |
1602 | PADOFFSET tmp = find_threadsv(name); | |
411caa50 | 1603 | if (tmp != NOT_IN_PAD) |
6224f72b | 1604 | return THREADSV(tmp); |
6224f72b GS |
1605 | } |
1606 | #endif /* USE_THREADS */ | |
1607 | gv = gv_fetchpv(name, create, SVt_PV); | |
1608 | if (gv) | |
1609 | return GvSV(gv); | |
1610 | return Nullsv; | |
1611 | } | |
1612 | ||
954c1994 GS |
1613 | /* |
1614 | =for apidoc p||get_av | |
1615 | ||
1616 | Returns the AV of the specified Perl array. If C<create> is set and the | |
1617 | Perl variable does not exist then it will be created. If C<create> is not | |
1618 | set and the variable does not exist then NULL is returned. | |
1619 | ||
1620 | =cut | |
1621 | */ | |
1622 | ||
6224f72b | 1623 | AV* |
864dbfa3 | 1624 | Perl_get_av(pTHX_ const char *name, I32 create) |
6224f72b GS |
1625 | { |
1626 | GV* gv = gv_fetchpv(name, create, SVt_PVAV); | |
1627 | if (create) | |
1628 | return GvAVn(gv); | |
1629 | if (gv) | |
1630 | return GvAV(gv); | |
1631 | return Nullav; | |
1632 | } | |
1633 | ||
954c1994 GS |
1634 | /* |
1635 | =for apidoc p||get_hv | |
1636 | ||
1637 | Returns the HV of the specified Perl hash. If C<create> is set and the | |
1638 | Perl variable does not exist then it will be created. If C<create> is not | |
1639 | set and the variable does not exist then NULL is returned. | |
1640 | ||
1641 | =cut | |
1642 | */ | |
1643 | ||
6224f72b | 1644 | HV* |
864dbfa3 | 1645 | Perl_get_hv(pTHX_ const char *name, I32 create) |
6224f72b | 1646 | { |
a0d0e21e LW |
1647 | GV* gv = gv_fetchpv(name, create, SVt_PVHV); |
1648 | if (create) | |
1649 | return GvHVn(gv); | |
1650 | if (gv) | |
1651 | return GvHV(gv); | |
1652 | return Nullhv; | |
1653 | } | |
1654 | ||
954c1994 GS |
1655 | /* |
1656 | =for apidoc p||get_cv | |
1657 | ||
1658 | Returns the CV of the specified Perl subroutine. If C<create> is set and | |
1659 | the Perl subroutine does not exist then it will be declared (which has the | |
1660 | same effect as saying C<sub name;>). If C<create> is not set and the | |
1661 | subroutine does not exist then NULL is returned. | |
1662 | ||
1663 | =cut | |
1664 | */ | |
1665 | ||
a0d0e21e | 1666 | CV* |
864dbfa3 | 1667 | Perl_get_cv(pTHX_ const char *name, I32 create) |
a0d0e21e LW |
1668 | { |
1669 | GV* gv = gv_fetchpv(name, create, SVt_PVCV); | |
b099ddc0 | 1670 | /* XXX unsafe for threads if eval_owner isn't held */ |
f6ec51f7 GS |
1671 | /* XXX this is probably not what they think they're getting. |
1672 | * It has the same effect as "sub name;", i.e. just a forward | |
1673 | * declaration! */ | |
8ebc5c01 | 1674 | if (create && !GvCVu(gv)) |
774d564b | 1675 | return newSUB(start_subparse(FALSE, 0), |
a0d0e21e | 1676 | newSVOP(OP_CONST, 0, newSVpv(name,0)), |
4633a7c4 | 1677 | Nullop, |
a0d0e21e LW |
1678 | Nullop); |
1679 | if (gv) | |
8ebc5c01 | 1680 | return GvCVu(gv); |
a0d0e21e LW |
1681 | return Nullcv; |
1682 | } | |
1683 | ||
79072805 LW |
1684 | /* Be sure to refetch the stack pointer after calling these routines. */ |
1685 | ||
954c1994 GS |
1686 | /* |
1687 | =for apidoc p||call_argv | |
1688 | ||
1689 | Performs a callback to the specified Perl sub. See L<perlcall>. | |
1690 | ||
1691 | =cut | |
1692 | */ | |
1693 | ||
a0d0e21e | 1694 | I32 |
864dbfa3 | 1695 | Perl_call_argv(pTHX_ const char *sub_name, I32 flags, register char **argv) |
ac27b0f5 | 1696 | |
8ac85365 NIS |
1697 | /* See G_* flags in cop.h */ |
1698 | /* null terminated arg list */ | |
8990e307 | 1699 | { |
a0d0e21e | 1700 | dSP; |
8990e307 | 1701 | |
924508f0 | 1702 | PUSHMARK(SP); |
a0d0e21e | 1703 | if (argv) { |
8990e307 | 1704 | while (*argv) { |
a0d0e21e | 1705 | XPUSHs(sv_2mortal(newSVpv(*argv,0))); |
8990e307 LW |
1706 | argv++; |
1707 | } | |
a0d0e21e | 1708 | PUTBACK; |
8990e307 | 1709 | } |
864dbfa3 | 1710 | return call_pv(sub_name, flags); |
8990e307 LW |
1711 | } |
1712 | ||
954c1994 GS |
1713 | /* |
1714 | =for apidoc p||call_pv | |
1715 | ||
1716 | Performs a callback to the specified Perl sub. See L<perlcall>. | |
1717 | ||
1718 | =cut | |
1719 | */ | |
1720 | ||
a0d0e21e | 1721 | I32 |
864dbfa3 | 1722 | Perl_call_pv(pTHX_ const char *sub_name, I32 flags) |
8ac85365 NIS |
1723 | /* name of the subroutine */ |
1724 | /* See G_* flags in cop.h */ | |
a0d0e21e | 1725 | { |
864dbfa3 | 1726 | return call_sv((SV*)get_cv(sub_name, TRUE), flags); |
a0d0e21e LW |
1727 | } |
1728 | ||
954c1994 GS |
1729 | /* |
1730 | =for apidoc p||call_method | |
1731 | ||
1732 | Performs a callback to the specified Perl method. The blessed object must | |
1733 | be on the stack. See L<perlcall>. | |
1734 | ||
1735 | =cut | |
1736 | */ | |
1737 | ||
a0d0e21e | 1738 | I32 |
864dbfa3 | 1739 | Perl_call_method(pTHX_ const char *methname, I32 flags) |
8ac85365 NIS |
1740 | /* name of the subroutine */ |
1741 | /* See G_* flags in cop.h */ | |
a0d0e21e | 1742 | { |
968b3946 | 1743 | return call_sv(sv_2mortal(newSVpv(methname,0)), flags | G_METHOD); |
a0d0e21e LW |
1744 | } |
1745 | ||
1746 | /* May be called with any of a CV, a GV, or an SV containing the name. */ | |
954c1994 GS |
1747 | /* |
1748 | =for apidoc p||call_sv | |
1749 | ||
1750 | Performs a callback to the Perl sub whose name is in the SV. See | |
1751 | L<perlcall>. | |
1752 | ||
1753 | =cut | |
1754 | */ | |
1755 | ||
a0d0e21e | 1756 | I32 |
864dbfa3 | 1757 | Perl_call_sv(pTHX_ SV *sv, I32 flags) |
8ac85365 | 1758 | /* See G_* flags in cop.h */ |
a0d0e21e | 1759 | { |
924508f0 | 1760 | dSP; |
a0d0e21e | 1761 | LOGOP myop; /* fake syntax tree node */ |
968b3946 | 1762 | UNOP method_op; |
aa689395 | 1763 | I32 oldmark; |
13689cfe | 1764 | volatile I32 retval = 0; |
a0d0e21e | 1765 | I32 oldscope; |
54310121 | 1766 | bool oldcatch = CATCH_GET; |
6224f72b | 1767 | int ret; |
533c011a | 1768 | OP* oldop = PL_op; |
db36c5a1 | 1769 | dJMPENV; |
1e422769 | 1770 | |
a0d0e21e LW |
1771 | if (flags & G_DISCARD) { |
1772 | ENTER; | |
1773 | SAVETMPS; | |
1774 | } | |
1775 | ||
aa689395 | 1776 | Zero(&myop, 1, LOGOP); |
54310121 | 1777 | myop.op_next = Nullop; |
f51d4af5 | 1778 | if (!(flags & G_NOARGS)) |
aa689395 | 1779 | myop.op_flags |= OPf_STACKED; |
54310121 | 1780 | myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID : |
1781 | (flags & G_ARRAY) ? OPf_WANT_LIST : | |
1782 | OPf_WANT_SCALAR); | |
462e5cf6 | 1783 | SAVEOP(); |
533c011a | 1784 | PL_op = (OP*)&myop; |
aa689395 | 1785 | |
3280af22 NIS |
1786 | EXTEND(PL_stack_sp, 1); |
1787 | *++PL_stack_sp = sv; | |
aa689395 | 1788 | oldmark = TOPMARK; |
3280af22 | 1789 | oldscope = PL_scopestack_ix; |
a0d0e21e | 1790 | |
3280af22 | 1791 | if (PERLDB_SUB && PL_curstash != PL_debstash |
36477c24 | 1792 | /* Handle first BEGIN of -d. */ |
3280af22 | 1793 | && (PL_DBcv || (PL_DBcv = GvCV(PL_DBsub))) |
36477c24 | 1794 | /* Try harder, since this may have been a sighandler, thus |
1795 | * curstash may be meaningless. */ | |
3280af22 | 1796 | && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != PL_debstash) |
491527d0 | 1797 | && !(flags & G_NODEBUG)) |
533c011a | 1798 | PL_op->op_private |= OPpENTERSUB_DB; |
a0d0e21e | 1799 | |
968b3946 GS |
1800 | if (flags & G_METHOD) { |
1801 | Zero(&method_op, 1, UNOP); | |
1802 | method_op.op_next = PL_op; | |
1803 | method_op.op_ppaddr = PL_ppaddr[OP_METHOD]; | |
1804 | myop.op_ppaddr = PL_ppaddr[OP_ENTERSUB]; | |
f39d0b86 | 1805 | PL_op = (OP*)&method_op; |
968b3946 GS |
1806 | } |
1807 | ||
312caa8e | 1808 | if (!(flags & G_EVAL)) { |
0cdb2077 | 1809 | CATCH_SET(TRUE); |
14dd3ad8 | 1810 | call_body((OP*)&myop, FALSE); |
312caa8e | 1811 | retval = PL_stack_sp - (PL_stack_base + oldmark); |
0253cb41 | 1812 | CATCH_SET(oldcatch); |
312caa8e CS |
1813 | } |
1814 | else { | |
d78bda3d | 1815 | myop.op_other = (OP*)&myop; |
3280af22 | 1816 | PL_markstack_ptr--; |
4633a7c4 LW |
1817 | /* we're trying to emulate pp_entertry() here */ |
1818 | { | |
c09156bb | 1819 | register PERL_CONTEXT *cx; |
54310121 | 1820 | I32 gimme = GIMME_V; |
ac27b0f5 | 1821 | |
4633a7c4 LW |
1822 | ENTER; |
1823 | SAVETMPS; | |
ac27b0f5 | 1824 | |
968b3946 | 1825 | push_return(Nullop); |
1d76a5c3 | 1826 | PUSHBLOCK(cx, (CXt_EVAL|CXp_TRYBLOCK), PL_stack_sp); |
4633a7c4 | 1827 | PUSHEVAL(cx, 0, 0); |
533c011a | 1828 | PL_eval_root = PL_op; /* Only needed so that goto works right. */ |
ac27b0f5 | 1829 | |
faef0170 | 1830 | PL_in_eval = EVAL_INEVAL; |
4633a7c4 | 1831 | if (flags & G_KEEPERR) |
faef0170 | 1832 | PL_in_eval |= EVAL_KEEPERR; |
4633a7c4 | 1833 | else |
38a03e6e | 1834 | sv_setpv(ERRSV,""); |
4633a7c4 | 1835 | } |
3280af22 | 1836 | PL_markstack_ptr++; |
a0d0e21e | 1837 | |
14dd3ad8 GS |
1838 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
1839 | redo_body: | |
1840 | CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body), | |
db36c5a1 | 1841 | (OP*)&myop, FALSE); |
14dd3ad8 GS |
1842 | #else |
1843 | JMPENV_PUSH(ret); | |
1844 | #endif | |
6224f72b GS |
1845 | switch (ret) { |
1846 | case 0: | |
14dd3ad8 GS |
1847 | #ifndef PERL_FLEXIBLE_EXCEPTIONS |
1848 | redo_body: | |
1849 | call_body((OP*)&myop, FALSE); | |
1850 | #endif | |
312caa8e CS |
1851 | retval = PL_stack_sp - (PL_stack_base + oldmark); |
1852 | if (!(flags & G_KEEPERR)) | |
1853 | sv_setpv(ERRSV,""); | |
a0d0e21e | 1854 | break; |
6224f72b | 1855 | case 1: |
f86702cc | 1856 | STATUS_ALL_FAILURE; |
a0d0e21e | 1857 | /* FALL THROUGH */ |
6224f72b | 1858 | case 2: |
a0d0e21e | 1859 | /* my_exit() was called */ |
3280af22 | 1860 | PL_curstash = PL_defstash; |
a0d0e21e | 1861 | FREETMPS; |
14dd3ad8 | 1862 | JMPENV_POP; |
cc3604b1 | 1863 | if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED)) |
cea2e8a9 | 1864 | Perl_croak(aTHX_ "Callback called exit"); |
f86702cc | 1865 | my_exit_jump(); |
a0d0e21e | 1866 | /* NOTREACHED */ |
6224f72b | 1867 | case 3: |
3280af22 | 1868 | if (PL_restartop) { |
533c011a | 1869 | PL_op = PL_restartop; |
3280af22 | 1870 | PL_restartop = 0; |
312caa8e | 1871 | goto redo_body; |
a0d0e21e | 1872 | } |
3280af22 | 1873 | PL_stack_sp = PL_stack_base + oldmark; |
a0d0e21e LW |
1874 | if (flags & G_ARRAY) |
1875 | retval = 0; | |
1876 | else { | |
1877 | retval = 1; | |
3280af22 | 1878 | *++PL_stack_sp = &PL_sv_undef; |
a0d0e21e | 1879 | } |
312caa8e | 1880 | break; |
a0d0e21e | 1881 | } |
a0d0e21e | 1882 | |
3280af22 | 1883 | if (PL_scopestack_ix > oldscope) { |
a0a2876f LW |
1884 | SV **newsp; |
1885 | PMOP *newpm; | |
1886 | I32 gimme; | |
c09156bb | 1887 | register PERL_CONTEXT *cx; |
a0a2876f LW |
1888 | I32 optype; |
1889 | ||
1890 | POPBLOCK(cx,newpm); | |
1891 | POPEVAL(cx); | |
1892 | pop_return(); | |
3280af22 | 1893 | PL_curpm = newpm; |
a0a2876f | 1894 | LEAVE; |
a0d0e21e | 1895 | } |
14dd3ad8 | 1896 | JMPENV_POP; |
a0d0e21e | 1897 | } |
1e422769 | 1898 | |
a0d0e21e | 1899 | if (flags & G_DISCARD) { |
3280af22 | 1900 | PL_stack_sp = PL_stack_base + oldmark; |
a0d0e21e LW |
1901 | retval = 0; |
1902 | FREETMPS; | |
1903 | LEAVE; | |
1904 | } | |
533c011a | 1905 | PL_op = oldop; |
a0d0e21e LW |
1906 | return retval; |
1907 | } | |
1908 | ||
14dd3ad8 | 1909 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
312caa8e | 1910 | STATIC void * |
14dd3ad8 | 1911 | S_vcall_body(pTHX_ va_list args) |
312caa8e CS |
1912 | { |
1913 | OP *myop = va_arg(args, OP*); | |
1914 | int is_eval = va_arg(args, int); | |
1915 | ||
14dd3ad8 | 1916 | call_body(myop, is_eval); |
312caa8e CS |
1917 | return NULL; |
1918 | } | |
14dd3ad8 | 1919 | #endif |
312caa8e CS |
1920 | |
1921 | STATIC void | |
14dd3ad8 | 1922 | S_call_body(pTHX_ OP *myop, int is_eval) |
312caa8e | 1923 | { |
312caa8e CS |
1924 | if (PL_op == myop) { |
1925 | if (is_eval) | |
f807eda9 | 1926 | PL_op = Perl_pp_entereval(aTHX); /* this doesn't do a POPMARK */ |
312caa8e | 1927 | else |
f807eda9 | 1928 | PL_op = Perl_pp_entersub(aTHX); /* this does */ |
312caa8e CS |
1929 | } |
1930 | if (PL_op) | |
cea2e8a9 | 1931 | CALLRUNOPS(aTHX); |
312caa8e CS |
1932 | } |
1933 | ||
6e72f9df | 1934 | /* Eval a string. The G_EVAL flag is always assumed. */ |
8990e307 | 1935 | |
954c1994 GS |
1936 | /* |
1937 | =for apidoc p||eval_sv | |
1938 | ||
1939 | Tells Perl to C<eval> the string in the SV. | |
1940 | ||
1941 | =cut | |
1942 | */ | |
1943 | ||
a0d0e21e | 1944 | I32 |
864dbfa3 | 1945 | Perl_eval_sv(pTHX_ SV *sv, I32 flags) |
ac27b0f5 | 1946 | |
8ac85365 | 1947 | /* See G_* flags in cop.h */ |
a0d0e21e | 1948 | { |
924508f0 | 1949 | dSP; |
a0d0e21e | 1950 | UNOP myop; /* fake syntax tree node */ |
8fa7f367 | 1951 | volatile I32 oldmark = SP - PL_stack_base; |
13689cfe | 1952 | volatile I32 retval = 0; |
4633a7c4 | 1953 | I32 oldscope; |
6224f72b | 1954 | int ret; |
533c011a | 1955 | OP* oldop = PL_op; |
db36c5a1 | 1956 | dJMPENV; |
84902520 | 1957 | |
4633a7c4 LW |
1958 | if (flags & G_DISCARD) { |
1959 | ENTER; | |
1960 | SAVETMPS; | |
1961 | } | |
1962 | ||
462e5cf6 | 1963 | SAVEOP(); |
533c011a NIS |
1964 | PL_op = (OP*)&myop; |
1965 | Zero(PL_op, 1, UNOP); | |
3280af22 NIS |
1966 | EXTEND(PL_stack_sp, 1); |
1967 | *++PL_stack_sp = sv; | |
1968 | oldscope = PL_scopestack_ix; | |
79072805 | 1969 | |
4633a7c4 LW |
1970 | if (!(flags & G_NOARGS)) |
1971 | myop.op_flags = OPf_STACKED; | |
79072805 | 1972 | myop.op_next = Nullop; |
6e72f9df | 1973 | myop.op_type = OP_ENTEREVAL; |
54310121 | 1974 | myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID : |
1975 | (flags & G_ARRAY) ? OPf_WANT_LIST : | |
1976 | OPf_WANT_SCALAR); | |
6e72f9df | 1977 | if (flags & G_KEEPERR) |
1978 | myop.op_flags |= OPf_SPECIAL; | |
4633a7c4 | 1979 | |
14dd3ad8 | 1980 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
312caa8e | 1981 | redo_body: |
14dd3ad8 | 1982 | CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_body), |
db36c5a1 | 1983 | (OP*)&myop, TRUE); |
14dd3ad8 GS |
1984 | #else |
1985 | JMPENV_PUSH(ret); | |
1986 | #endif | |
6224f72b GS |
1987 | switch (ret) { |
1988 | case 0: | |
14dd3ad8 GS |
1989 | #ifndef PERL_FLEXIBLE_EXCEPTIONS |
1990 | redo_body: | |
1991 | call_body((OP*)&myop,TRUE); | |
1992 | #endif | |
312caa8e CS |
1993 | retval = PL_stack_sp - (PL_stack_base + oldmark); |
1994 | if (!(flags & G_KEEPERR)) | |
1995 | sv_setpv(ERRSV,""); | |
4633a7c4 | 1996 | break; |
6224f72b | 1997 | case 1: |
f86702cc | 1998 | STATUS_ALL_FAILURE; |
4633a7c4 | 1999 | /* FALL THROUGH */ |
6224f72b | 2000 | case 2: |
4633a7c4 | 2001 | /* my_exit() was called */ |
3280af22 | 2002 | PL_curstash = PL_defstash; |
4633a7c4 | 2003 | FREETMPS; |
14dd3ad8 | 2004 | JMPENV_POP; |
cc3604b1 | 2005 | if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED)) |
cea2e8a9 | 2006 | Perl_croak(aTHX_ "Callback called exit"); |
f86702cc | 2007 | my_exit_jump(); |
4633a7c4 | 2008 | /* NOTREACHED */ |
6224f72b | 2009 | case 3: |
3280af22 | 2010 | if (PL_restartop) { |
533c011a | 2011 | PL_op = PL_restartop; |
3280af22 | 2012 | PL_restartop = 0; |
312caa8e | 2013 | goto redo_body; |
4633a7c4 | 2014 | } |
3280af22 | 2015 | PL_stack_sp = PL_stack_base + oldmark; |
4633a7c4 LW |
2016 | if (flags & G_ARRAY) |
2017 | retval = 0; | |
2018 | else { | |
2019 | retval = 1; | |
3280af22 | 2020 | *++PL_stack_sp = &PL_sv_undef; |
4633a7c4 | 2021 | } |
312caa8e | 2022 | break; |
4633a7c4 LW |
2023 | } |
2024 | ||
14dd3ad8 | 2025 | JMPENV_POP; |
4633a7c4 | 2026 | if (flags & G_DISCARD) { |
3280af22 | 2027 | PL_stack_sp = PL_stack_base + oldmark; |
4633a7c4 LW |
2028 | retval = 0; |
2029 | FREETMPS; | |
2030 | LEAVE; | |
2031 | } | |
533c011a | 2032 | PL_op = oldop; |
4633a7c4 LW |
2033 | return retval; |
2034 | } | |
2035 | ||
954c1994 GS |
2036 | /* |
2037 | =for apidoc p||eval_pv | |
2038 | ||
2039 | Tells Perl to C<eval> the given string and return an SV* result. | |
2040 | ||
2041 | =cut | |
2042 | */ | |
2043 | ||
137443ea | 2044 | SV* |
864dbfa3 | 2045 | Perl_eval_pv(pTHX_ const char *p, I32 croak_on_error) |
137443ea | 2046 | { |
2047 | dSP; | |
2048 | SV* sv = newSVpv(p, 0); | |
2049 | ||
864dbfa3 | 2050 | eval_sv(sv, G_SCALAR); |
137443ea | 2051 | SvREFCNT_dec(sv); |
2052 | ||
2053 | SPAGAIN; | |
2054 | sv = POPs; | |
2055 | PUTBACK; | |
2056 | ||
2d8e6c8d GS |
2057 | if (croak_on_error && SvTRUE(ERRSV)) { |
2058 | STRLEN n_a; | |
cea2e8a9 | 2059 | Perl_croak(aTHX_ SvPVx(ERRSV, n_a)); |
2d8e6c8d | 2060 | } |
137443ea | 2061 | |
2062 | return sv; | |
2063 | } | |
2064 | ||
4633a7c4 LW |
2065 | /* Require a module. */ |
2066 | ||
954c1994 GS |
2067 | /* |
2068 | =for apidoc p||require_pv | |
2069 | ||
7d3fb230 BS |
2070 | Tells Perl to C<require> the file named by the string argument. It is |
2071 | analogous to the Perl code C<eval "require '$file'">. It's even | |
2072 | implemented that way; consider using Perl_load_module instead. | |
954c1994 | 2073 | |
7d3fb230 | 2074 | =cut */ |
954c1994 | 2075 | |
4633a7c4 | 2076 | void |
864dbfa3 | 2077 | Perl_require_pv(pTHX_ const char *pv) |
4633a7c4 | 2078 | { |
d3acc0f7 JP |
2079 | SV* sv; |
2080 | dSP; | |
e788e7d3 | 2081 | PUSHSTACKi(PERLSI_REQUIRE); |
d3acc0f7 JP |
2082 | PUTBACK; |
2083 | sv = sv_newmortal(); | |
4633a7c4 LW |
2084 | sv_setpv(sv, "require '"); |
2085 | sv_catpv(sv, pv); | |
2086 | sv_catpv(sv, "'"); | |
864dbfa3 | 2087 | eval_sv(sv, G_DISCARD); |
d3acc0f7 JP |
2088 | SPAGAIN; |
2089 | POPSTACK; | |
79072805 LW |
2090 | } |
2091 | ||
79072805 | 2092 | void |
864dbfa3 | 2093 | Perl_magicname(pTHX_ char *sym, char *name, I32 namlen) |
79072805 LW |
2094 | { |
2095 | register GV *gv; | |
2096 | ||
155aba94 | 2097 | if ((gv = gv_fetchpv(sym,TRUE, SVt_PV))) |
14befaf4 | 2098 | sv_magic(GvSV(gv), (SV*)gv, PERL_MAGIC_sv, name, namlen); |
79072805 LW |
2099 | } |
2100 | ||
76e3520e | 2101 | STATIC void |
cea2e8a9 | 2102 | S_usage(pTHX_ char *name) /* XXX move this out into a module ? */ |
4633a7c4 | 2103 | { |
ab821d7f | 2104 | /* This message really ought to be max 23 lines. |
75c72d73 | 2105 | * Removed -h because the user already knows that option. Others? */ |
fb73857a | 2106 | |
76e3520e | 2107 | static char *usage_msg[] = { |
fb73857a | 2108 | "-0[octal] specify record separator (\\0, if no argument)", |
2109 | "-a autosplit mode with -n or -p (splits $_ into @F)", | |
46487f74 | 2110 | "-C enable native wide character system interfaces", |
1950ee41 | 2111 | "-c check syntax only (runs BEGIN and CHECK blocks)", |
aac3bd0d GS |
2112 | "-d[:debugger] run program under debugger", |
2113 | "-D[number/list] set debugging flags (argument is a bit mask or alphabets)", | |
2114 | "-e 'command' one line of program (several -e's allowed, omit programfile)", | |
2115 | "-F/pattern/ split() pattern for -a switch (//'s are optional)", | |
2116 | "-i[extension] edit <> files in place (makes backup if extension supplied)", | |
2117 | "-Idirectory specify @INC/#include directory (several -I's allowed)", | |
fb73857a | 2118 | "-l[octal] enable line ending processing, specifies line terminator", |
aac3bd0d GS |
2119 | "-[mM][-]module execute `use/no module...' before executing program", |
2120 | "-n assume 'while (<>) { ... }' loop around program", | |
2121 | "-p assume loop like -n but print line also, like sed", | |
2122 | "-P run program through C preprocessor before compilation", | |
2123 | "-s enable rudimentary parsing for switches after programfile", | |
2124 | "-S look for programfile using PATH environment variable", | |
2125 | "-T enable tainting checks", | |
2126 | "-u dump core after parsing program", | |
fb73857a | 2127 | "-U allow unsafe operations", |
aac3bd0d GS |
2128 | "-v print version, subversion (includes VERY IMPORTANT perl info)", |
2129 | "-V[:variable] print configuration summary (or a single Config.pm variable)", | |
2130 | "-w enable many useful warnings (RECOMMENDED)", | |
3c0facb2 GS |
2131 | "-W enable all warnings", |
2132 | "-X disable all warnings", | |
fb73857a | 2133 | "-x[directory] strip off text before #!perl line and perhaps cd to directory", |
2134 | "\n", | |
2135 | NULL | |
2136 | }; | |
76e3520e | 2137 | char **p = usage_msg; |
fb73857a | 2138 | |
b0e47665 GS |
2139 | PerlIO_printf(PerlIO_stdout(), |
2140 | "\nUsage: %s [switches] [--] [programfile] [arguments]", | |
2141 | name); | |
fb73857a | 2142 | while (*p) |
b0e47665 | 2143 | PerlIO_printf(PerlIO_stdout(), "\n %s", *p++); |
4633a7c4 LW |
2144 | } |
2145 | ||
79072805 LW |
2146 | /* This routine handles any switches that can be given during run */ |
2147 | ||
2148 | char * | |
864dbfa3 | 2149 | Perl_moreswitches(pTHX_ char *s) |
79072805 | 2150 | { |
ba210ebe | 2151 | STRLEN numlen; |
c07a80fd | 2152 | U32 rschar; |
79072805 LW |
2153 | |
2154 | switch (*s) { | |
2155 | case '0': | |
a863c7d1 | 2156 | { |
b21ed0a9 | 2157 | numlen = 0; /* disallow underscores */ |
dff6d3cd | 2158 | rschar = (U32)scan_oct(s, 4, &numlen); |
3280af22 | 2159 | SvREFCNT_dec(PL_nrs); |
c07a80fd | 2160 | if (rschar & ~((U8)~0)) |
3280af22 | 2161 | PL_nrs = &PL_sv_undef; |
c07a80fd | 2162 | else if (!rschar && numlen >= 2) |
79cb57f6 | 2163 | PL_nrs = newSVpvn("", 0); |
c07a80fd | 2164 | else { |
2165 | char ch = rschar; | |
79cb57f6 | 2166 | PL_nrs = newSVpvn(&ch, 1); |
79072805 LW |
2167 | } |
2168 | return s + numlen; | |
a863c7d1 | 2169 | } |
46487f74 GS |
2170 | case 'C': |
2171 | PL_widesyscalls = TRUE; | |
2172 | s++; | |
2173 | return s; | |
2304df62 | 2174 | case 'F': |
3280af22 NIS |
2175 | PL_minus_F = TRUE; |
2176 | PL_splitstr = savepv(s + 1); | |
2304df62 AD |
2177 | s += strlen(s); |
2178 | return s; | |
79072805 | 2179 | case 'a': |
3280af22 | 2180 | PL_minus_a = TRUE; |
79072805 LW |
2181 | s++; |
2182 | return s; | |
2183 | case 'c': | |
3280af22 | 2184 | PL_minus_c = TRUE; |
79072805 LW |
2185 | s++; |
2186 | return s; | |
2187 | case 'd': | |
bbce6d69 | 2188 | forbid_setid("-d"); |
4633a7c4 | 2189 | s++; |
70c94a19 RR |
2190 | /* The following permits -d:Mod to accepts arguments following an = |
2191 | in the fashion that -MSome::Mod does. */ | |
2192 | if (*s == ':' || *s == '=') { | |
2193 | char *start; | |
2194 | SV *sv; | |
2195 | sv = newSVpv("use Devel::", 0); | |
2196 | start = ++s; | |
2197 | /* We now allow -d:Module=Foo,Bar */ | |
2198 | while(isALNUM(*s) || *s==':') ++s; | |
2199 | if (*s != '=') | |
2200 | sv_catpv(sv, start); | |
2201 | else { | |
2202 | sv_catpvn(sv, start, s-start); | |
2203 | sv_catpv(sv, " split(/,/,q{"); | |
2204 | sv_catpv(sv, ++s); | |
2205 | sv_catpv(sv, "})"); | |
2206 | } | |
4633a7c4 | 2207 | s += strlen(s); |
70c94a19 | 2208 | my_setenv("PERL5DB", SvPV(sv, PL_na)); |
4633a7c4 | 2209 | } |
ed094faf | 2210 | if (!PL_perldb) { |
3280af22 | 2211 | PL_perldb = PERLDB_ALL; |
a0d0e21e | 2212 | init_debugger(); |
ed094faf | 2213 | } |
79072805 LW |
2214 | return s; |
2215 | case 'D': | |
0453d815 | 2216 | { |
79072805 | 2217 | #ifdef DEBUGGING |
bbce6d69 | 2218 | forbid_setid("-D"); |
79072805 | 2219 | if (isALPHA(s[1])) { |
04932ac8 DM |
2220 | /* if adding extra options, remember to update DEBUG_MASK */ |
2221 | static char debopts[] = "psltocPmfrxuLHXDSTR"; | |
79072805 LW |
2222 | char *d; |
2223 | ||
93a17b20 | 2224 | for (s++; *s && (d = strchr(debopts,*s)); s++) |
3280af22 | 2225 | PL_debug |= 1 << (d - debopts); |
79072805 LW |
2226 | } |
2227 | else { | |
3280af22 | 2228 | PL_debug = atoi(s+1); |
79072805 LW |
2229 | for (s++; isDIGIT(*s); s++) ; |
2230 | } | |
aea4f609 | 2231 | PL_debug |= DEBUG_TOP_FLAG; |
79072805 | 2232 | #else |
0453d815 PM |
2233 | if (ckWARN_d(WARN_DEBUGGING)) |
2234 | Perl_warner(aTHX_ WARN_DEBUGGING, | |
2235 | "Recompile perl with -DDEBUGGING to use -D switch\n"); | |
a0d0e21e | 2236 | for (s++; isALNUM(*s); s++) ; |
79072805 LW |
2237 | #endif |
2238 | /*SUPPRESS 530*/ | |
2239 | return s; | |
0453d815 | 2240 | } |
4633a7c4 | 2241 | case 'h': |
ac27b0f5 | 2242 | usage(PL_origargv[0]); |
6ad3d225 | 2243 | PerlProc_exit(0); |
79072805 | 2244 | case 'i': |
3280af22 NIS |
2245 | if (PL_inplace) |
2246 | Safefree(PL_inplace); | |
2247 | PL_inplace = savepv(s+1); | |
79072805 | 2248 | /*SUPPRESS 530*/ |
3280af22 | 2249 | for (s = PL_inplace; *s && !isSPACE(*s); s++) ; |
7b8d334a | 2250 | if (*s) { |
fb73857a | 2251 | *s++ = '\0'; |
7b8d334a GS |
2252 | if (*s == '-') /* Additional switches on #! line. */ |
2253 | s++; | |
2254 | } | |
fb73857a | 2255 | return s; |
2256 | case 'I': /* -I handled both here and in parse_perl() */ | |
bbce6d69 | 2257 | forbid_setid("-I"); |
fb73857a | 2258 | ++s; |
2259 | while (*s && isSPACE(*s)) | |
2260 | ++s; | |
2261 | if (*s) { | |
774d564b | 2262 | char *e, *p; |
0df16ed7 GS |
2263 | p = s; |
2264 | /* ignore trailing spaces (possibly followed by other switches) */ | |
2265 | do { | |
2266 | for (e = p; *e && !isSPACE(*e); e++) ; | |
2267 | p = e; | |
2268 | while (isSPACE(*p)) | |
2269 | p++; | |
2270 | } while (*p && *p != '-'); | |
2271 | e = savepvn(s, e-s); | |
9c8a64f0 | 2272 | incpush(e, TRUE, TRUE); |
0df16ed7 GS |
2273 | Safefree(e); |
2274 | s = p; | |
2275 | if (*s == '-') | |
2276 | s++; | |
79072805 LW |
2277 | } |
2278 | else | |
a67e862a | 2279 | Perl_croak(aTHX_ "No directory specified for -I"); |
fb73857a | 2280 | return s; |
79072805 | 2281 | case 'l': |
3280af22 | 2282 | PL_minus_l = TRUE; |
79072805 | 2283 | s++; |
7889fe52 NIS |
2284 | if (PL_ors_sv) { |
2285 | SvREFCNT_dec(PL_ors_sv); | |
2286 | PL_ors_sv = Nullsv; | |
2287 | } | |
79072805 | 2288 | if (isDIGIT(*s)) { |
7889fe52 | 2289 | PL_ors_sv = newSVpvn("\n",1); |
b21ed0a9 | 2290 | numlen = 0; /* disallow underscores */ |
7889fe52 | 2291 | *SvPVX(PL_ors_sv) = (char)scan_oct(s, 3 + (*s == '0'), &numlen); |
79072805 LW |
2292 | s += numlen; |
2293 | } | |
2294 | else { | |
3280af22 | 2295 | if (RsPARA(PL_nrs)) { |
7889fe52 NIS |
2296 | PL_ors_sv = newSVpvn("\n\n",2); |
2297 | } | |
2298 | else { | |
2299 | PL_ors_sv = newSVsv(PL_nrs); | |
c07a80fd | 2300 | } |
79072805 LW |
2301 | } |
2302 | return s; | |
1a30305b | 2303 | case 'M': |
bbce6d69 | 2304 | forbid_setid("-M"); /* XXX ? */ |
1a30305b | 2305 | /* FALL THROUGH */ |
2306 | case 'm': | |
bbce6d69 | 2307 | forbid_setid("-m"); /* XXX ? */ |
1a30305b | 2308 | if (*++s) { |
a5f75d66 | 2309 | char *start; |
11343788 | 2310 | SV *sv; |
a5f75d66 AD |
2311 | char *use = "use "; |
2312 | /* -M-foo == 'no foo' */ | |
2313 | if (*s == '-') { use = "no "; ++s; } | |
11343788 | 2314 | sv = newSVpv(use,0); |
a5f75d66 | 2315 | start = s; |
1a30305b | 2316 | /* We allow -M'Module qw(Foo Bar)' */ |
c07a80fd | 2317 | while(isALNUM(*s) || *s==':') ++s; |
2318 | if (*s != '=') { | |
11343788 | 2319 | sv_catpv(sv, start); |
c07a80fd | 2320 | if (*(start-1) == 'm') { |
2321 | if (*s != '\0') | |
cea2e8a9 | 2322 | Perl_croak(aTHX_ "Can't use '%c' after -mname", *s); |
11343788 | 2323 | sv_catpv( sv, " ()"); |
c07a80fd | 2324 | } |
2325 | } else { | |
6df41af2 | 2326 | if (s == start) |
be98fb35 GS |
2327 | Perl_croak(aTHX_ "Module name required with -%c option", |
2328 | s[-1]); | |
11343788 MB |
2329 | sv_catpvn(sv, start, s-start); |
2330 | sv_catpv(sv, " split(/,/,q{"); | |
2331 | sv_catpv(sv, ++s); | |
2332 | sv_catpv(sv, "})"); | |
c07a80fd | 2333 | } |
1a30305b | 2334 | s += strlen(s); |
5c831c24 | 2335 | if (!PL_preambleav) |
3280af22 NIS |
2336 | PL_preambleav = newAV(); |
2337 | av_push(PL_preambleav, sv); | |
1a30305b | 2338 | } |
2339 | else | |
cea2e8a9 | 2340 | Perl_croak(aTHX_ "No space allowed after -%c", *(s-1)); |
1a30305b | 2341 | return s; |
79072805 | 2342 | case 'n': |
3280af22 | 2343 | PL_minus_n = TRUE; |
79072805 LW |
2344 | s++; |
2345 | return s; | |
2346 | case 'p': | |
3280af22 | 2347 | PL_minus_p = TRUE; |
79072805 LW |
2348 | s++; |
2349 | return s; | |
2350 | case 's': | |
bbce6d69 | 2351 | forbid_setid("-s"); |
3280af22 | 2352 | PL_doswitches = TRUE; |
79072805 LW |
2353 | s++; |
2354 | return s; | |
463ee0b2 | 2355 | case 'T': |
3280af22 | 2356 | if (!PL_tainting) |
cea2e8a9 | 2357 | Perl_croak(aTHX_ "Too late for \"-T\" option"); |
463ee0b2 LW |
2358 | s++; |
2359 | return s; | |
79072805 | 2360 | case 'u': |
bf4acbe4 GS |
2361 | #ifdef MACOS_TRADITIONAL |
2362 | Perl_croak(aTHX_ "Believe me, you don't want to use \"-u\" on a Macintosh"); | |
2363 | #endif | |
3280af22 | 2364 | PL_do_undump = TRUE; |
79072805 LW |
2365 | s++; |
2366 | return s; | |
2367 | case 'U': | |
3280af22 | 2368 | PL_unsafe = TRUE; |
79072805 LW |
2369 | s++; |
2370 | return s; | |
2371 | case 'v': | |
8e9464f1 | 2372 | #if !defined(DGUX) |
b0e47665 | 2373 | PerlIO_printf(PerlIO_stdout(), |
d2560b70 | 2374 | Perl_form(aTHX_ "\nThis is perl, v%"VDf" built for %s", |
b0e47665 | 2375 | PL_patchlevel, ARCHNAME)); |
8e9464f1 JH |
2376 | #else /* DGUX */ |
2377 | /* Adjust verbose output as in the perl that ships with the DG/UX OS from EMC */ | |
2378 | PerlIO_printf(PerlIO_stdout(), | |
2379 | Perl_form(aTHX_ "\nThis is perl, version %vd\n", PL_patchlevel)); | |
2380 | PerlIO_printf(PerlIO_stdout(), | |
2381 | Perl_form(aTHX_ " built under %s at %s %s\n", | |
2382 | OSNAME, __DATE__, __TIME__)); | |
2383 | PerlIO_printf(PerlIO_stdout(), | |
2384 | Perl_form(aTHX_ " OS Specific Release: %s\n", | |
40a39f85 | 2385 | OSVERS)); |
8e9464f1 JH |
2386 | #endif /* !DGUX */ |
2387 | ||
fb73857a | 2388 | #if defined(LOCAL_PATCH_COUNT) |
2389 | if (LOCAL_PATCH_COUNT > 0) | |
b0e47665 GS |
2390 | PerlIO_printf(PerlIO_stdout(), |
2391 | "\n(with %d registered patch%s, " | |
2392 | "see perl -V for more detail)", | |
2393 | (int)LOCAL_PATCH_COUNT, | |
2394 | (LOCAL_PATCH_COUNT!=1) ? "es" : ""); | |
a5f75d66 | 2395 | #endif |
1a30305b | 2396 | |
b0e47665 | 2397 | PerlIO_printf(PerlIO_stdout(), |
bc89e66f | 2398 | "\n\nCopyright 1987-2001, Larry Wall\n"); |
eae9c151 JH |
2399 | #ifdef MACOS_TRADITIONAL |
2400 | PerlIO_printf(PerlIO_stdout(), | |
084592ab | 2401 | "\nMac OS port Copyright (c) 1991-2001, Matthias Neeracher\n"); |
eae9c151 | 2402 | #endif |
79072805 | 2403 | #ifdef MSDOS |
b0e47665 GS |
2404 | PerlIO_printf(PerlIO_stdout(), |
2405 | "\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n"); | |
55497cff | 2406 | #endif |
2407 | #ifdef DJGPP | |
b0e47665 GS |
2408 | PerlIO_printf(PerlIO_stdout(), |
2409 | "djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n" | |
2410 | "djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1999\n"); | |
4633a7c4 | 2411 | #endif |
79072805 | 2412 | #ifdef OS2 |
b0e47665 GS |
2413 | PerlIO_printf(PerlIO_stdout(), |
2414 | "\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n" | |
2415 | "Version 5 port Copyright (c) 1994-1999, Andreas Kaiser, Ilya Zakharevich\n"); | |
79072805 | 2416 | #endif |
79072805 | 2417 | #ifdef atarist |
b0e47665 GS |
2418 | PerlIO_printf(PerlIO_stdout(), |
2419 | "atariST series port, ++jrb bammi@cadence.com\n"); | |
79072805 | 2420 | #endif |
a3f9223b | 2421 | #ifdef __BEOS__ |
b0e47665 GS |
2422 | PerlIO_printf(PerlIO_stdout(), |
2423 | "BeOS port Copyright Tom Spindler, 1997-1999\n"); | |
a3f9223b | 2424 | #endif |
1d84e8df | 2425 | #ifdef MPE |
b0e47665 | 2426 | PerlIO_printf(PerlIO_stdout(), |
dc22e1c4 | 2427 | "MPE/iX port Copyright by Mark Klein and Mark Bixby, 1996-2001\n"); |
1d84e8df | 2428 | #endif |
9d116dd7 | 2429 | #ifdef OEMVS |
b0e47665 GS |
2430 | PerlIO_printf(PerlIO_stdout(), |
2431 | "MVS (OS390) port by Mortice Kern Systems, 1997-1999\n"); | |
9d116dd7 | 2432 | #endif |
495c5fdc | 2433 | #ifdef __VOS__ |
b0e47665 GS |
2434 | PerlIO_printf(PerlIO_stdout(), |
2435 | "Stratus VOS port by Paul_Green@stratus.com, 1997-1999\n"); | |
495c5fdc | 2436 | #endif |
092bebab | 2437 | #ifdef __OPEN_VM |
b0e47665 GS |
2438 | PerlIO_printf(PerlIO_stdout(), |
2439 | "VM/ESA port by Neale Ferguson, 1998-1999\n"); | |
092bebab | 2440 | #endif |
a1a0e61e | 2441 | #ifdef POSIX_BC |
b0e47665 GS |
2442 | PerlIO_printf(PerlIO_stdout(), |
2443 | "BS2000 (POSIX) port by Start Amadeus GmbH, 1998-1999\n"); | |
a1a0e61e | 2444 | #endif |
61ae2fbf | 2445 | #ifdef __MINT__ |
b0e47665 GS |
2446 | PerlIO_printf(PerlIO_stdout(), |
2447 | "MiNT port by Guido Flohr, 1997-1999\n"); | |
61ae2fbf | 2448 | #endif |
f83d2536 | 2449 | #ifdef EPOC |
b0e47665 GS |
2450 | PerlIO_printf(PerlIO_stdout(), |
2451 | "EPOC port by Olaf Flebbe, 1999-2000\n"); | |
f83d2536 | 2452 | #endif |
e1caacb4 JH |
2453 | #ifdef UNDER_CE |
2454 | printf("WINCE port by Rainer Keuchel, 2001\n"); | |
2455 | printf("Built on " __DATE__ " " __TIME__ "\n\n"); | |
2456 | wce_hitreturn(); | |
2457 | #endif | |
baed7233 DL |
2458 | #ifdef BINARY_BUILD_NOTICE |
2459 | BINARY_BUILD_NOTICE; | |
2460 | #endif | |
b0e47665 GS |
2461 | PerlIO_printf(PerlIO_stdout(), |
2462 | "\n\ | |
79072805 | 2463 | Perl may be copied only under the terms of either the Artistic License or the\n\ |
3d6f292d | 2464 | GNU General Public License, which may be found in the Perl 5 source kit.\n\n\ |
95103687 GS |
2465 | Complete documentation for Perl, including FAQ lists, should be found on\n\ |
2466 | this system using `man perl' or `perldoc perl'. If you have access to the\n\ | |
2467 | Internet, point your browser at http://www.perl.com/, the Perl Home Page.\n\n"); | |
6ad3d225 | 2468 | PerlProc_exit(0); |
79072805 | 2469 | case 'w': |
599cee73 | 2470 | if (! (PL_dowarn & G_WARN_ALL_MASK)) |
ac27b0f5 | 2471 | PL_dowarn |= G_WARN_ON; |
599cee73 PM |
2472 | s++; |
2473 | return s; | |
2474 | case 'W': | |
ac27b0f5 | 2475 | PL_dowarn = G_WARN_ALL_ON|G_WARN_ON; |
d3a7d8c7 | 2476 | PL_compiling.cop_warnings = pWARN_ALL ; |
599cee73 PM |
2477 | s++; |
2478 | return s; | |
2479 | case 'X': | |
ac27b0f5 | 2480 | PL_dowarn = G_WARN_ALL_OFF; |
d3a7d8c7 | 2481 | PL_compiling.cop_warnings = pWARN_NONE ; |
79072805 LW |
2482 | s++; |
2483 | return s; | |
a0d0e21e | 2484 | case '*': |
79072805 LW |
2485 | case ' ': |
2486 | if (s[1] == '-') /* Additional switches on #! line. */ | |
2487 | return s+2; | |
2488 | break; | |
a0d0e21e | 2489 | case '-': |
79072805 | 2490 | case 0: |
51882d45 | 2491 | #if defined(WIN32) || !defined(PERL_STRICT_CR) |
a868473f NIS |
2492 | case '\r': |
2493 | #endif | |
79072805 LW |
2494 | case '\n': |
2495 | case '\t': | |
2496 | break; | |
aa689395 | 2497 | #ifdef ALTERNATE_SHEBANG |
2498 | case 'S': /* OS/2 needs -S on "extproc" line. */ | |
2499 | break; | |
2500 | #endif | |
a0d0e21e | 2501 | case 'P': |
3280af22 | 2502 | if (PL_preprocess) |
a0d0e21e LW |
2503 | return s+1; |
2504 | /* FALL THROUGH */ | |
79072805 | 2505 | default: |
cea2e8a9 | 2506 | Perl_croak(aTHX_ "Can't emulate -%.1s on #! line",s); |
79072805 LW |
2507 | } |
2508 | return Nullch; | |
2509 | } | |
2510 | ||
2511 | /* compliments of Tom Christiansen */ | |
2512 | ||
2513 | /* unexec() can be found in the Gnu emacs distribution */ | |
ee580363 | 2514 | /* Known to work with -DUNEXEC and using unexelf.c from GNU emacs-20.2 */ |
79072805 LW |
2515 | |
2516 | void | |
864dbfa3 | 2517 | Perl_my_unexec(pTHX) |
79072805 LW |
2518 | { |
2519 | #ifdef UNEXEC | |
46fc3d4c | 2520 | SV* prog; |
2521 | SV* file; | |
ee580363 | 2522 | int status = 1; |
79072805 LW |
2523 | extern int etext; |
2524 | ||
ee580363 | 2525 | prog = newSVpv(BIN_EXP, 0); |
46fc3d4c | 2526 | sv_catpv(prog, "/perl"); |
6b88bc9c | 2527 | file = newSVpv(PL_origfilename, 0); |
46fc3d4c | 2528 | sv_catpv(file, ".perldump"); |
79072805 | 2529 | |
ee580363 GS |
2530 | unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0); |
2531 | /* unexec prints msg to stderr in case of failure */ | |
6ad3d225 | 2532 | PerlProc_exit(status); |
79072805 | 2533 | #else |
a5f75d66 AD |
2534 | # ifdef VMS |
2535 | # include <lib$routines.h> | |
2536 | lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */ | |
aa689395 | 2537 | # else |
79072805 | 2538 | ABORT(); /* for use with undump */ |
aa689395 | 2539 | # endif |
a5f75d66 | 2540 | #endif |
79072805 LW |
2541 | } |
2542 | ||
cb68f92d GS |
2543 | /* initialize curinterp */ |
2544 | STATIC void | |
cea2e8a9 | 2545 | S_init_interp(pTHX) |
cb68f92d GS |
2546 | { |
2547 | ||
066ef5b5 | 2548 | #ifdef PERL_OBJECT /* XXX kludge */ |
cb68f92d | 2549 | #define I_REINIT \ |
6b88bc9c GS |
2550 | STMT_START { \ |
2551 | PL_chopset = " \n-"; \ | |
2552 | PL_copline = NOLINE; \ | |
2553 | PL_curcop = &PL_compiling;\ | |
2554 | PL_curcopdb = NULL; \ | |
2555 | PL_dbargs = 0; \ | |
3967c732 | 2556 | PL_dumpindent = 4; \ |
6b88bc9c GS |
2557 | PL_laststatval = -1; \ |
2558 | PL_laststype = OP_STAT; \ | |
2559 | PL_maxscream = -1; \ | |
2560 | PL_maxsysfd = MAXSYSFD; \ | |
2561 | PL_statname = Nullsv; \ | |
2562 | PL_tmps_floor = -1; \ | |
2563 | PL_tmps_ix = -1; \ | |
2564 | PL_op_mask = NULL; \ | |
6b88bc9c GS |
2565 | PL_laststatval = -1; \ |
2566 | PL_laststype = OP_STAT; \ | |
2567 | PL_mess_sv = Nullsv; \ | |
2568 | PL_splitstr = " "; \ | |
2569 | PL_generation = 100; \ | |
2570 | PL_exitlist = NULL; \ | |
2571 | PL_exitlistlen = 0; \ | |
2572 | PL_regindent = 0; \ | |
2573 | PL_in_clean_objs = FALSE; \ | |
2574 | PL_in_clean_all = FALSE; \ | |
2575 | PL_profiledata = NULL; \ | |
2576 | PL_rsfp = Nullfp; \ | |
2577 | PL_rsfp_filters = Nullav; \ | |
24d3c518 | 2578 | PL_dirty = FALSE; \ |
cb68f92d | 2579 | } STMT_END |
9666903d | 2580 | I_REINIT; |
066ef5b5 GS |
2581 | #else |
2582 | # ifdef MULTIPLICITY | |
2583 | # define PERLVAR(var,type) | |
51371543 | 2584 | # define PERLVARA(var,n,type) |
cea2e8a9 | 2585 | # if defined(PERL_IMPLICIT_CONTEXT) |
54aff467 GS |
2586 | # if defined(USE_THREADS) |
2587 | # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init; | |
2588 | # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init; | |
2589 | # else /* !USE_THREADS */ | |
2590 | # define PERLVARI(var,type,init) aTHX->var = init; | |
2591 | # define PERLVARIC(var,type,init) aTHX->var = init; | |
2592 | # endif /* USE_THREADS */ | |
cea2e8a9 | 2593 | # else |
c5be433b GS |
2594 | # define PERLVARI(var,type,init) PERL_GET_INTERP->var = init; |
2595 | # define PERLVARIC(var,type,init) PERL_GET_INTERP->var = init; | |
cea2e8a9 | 2596 | # endif |
066ef5b5 GS |
2597 | # include "intrpvar.h" |
2598 | # ifndef USE_THREADS | |
2599 | # include "thrdvar.h" | |
2600 | # endif | |
2601 | # undef PERLVAR | |
51371543 | 2602 | # undef PERLVARA |
066ef5b5 GS |
2603 | # undef PERLVARI |
2604 | # undef PERLVARIC | |
3967c732 | 2605 | # else |
066ef5b5 | 2606 | # define PERLVAR(var,type) |
51371543 | 2607 | # define PERLVARA(var,n,type) |
533c011a NIS |
2608 | # define PERLVARI(var,type,init) PL_##var = init; |
2609 | # define PERLVARIC(var,type,init) PL_##var = init; | |
066ef5b5 GS |
2610 | # include "intrpvar.h" |
2611 | # ifndef USE_THREADS | |
2612 | # include "thrdvar.h" | |
2613 | # endif | |
2614 | # undef PERLVAR | |
51371543 | 2615 | # undef PERLVARA |
066ef5b5 GS |
2616 | # undef PERLVARI |
2617 | # undef PERLVARIC | |
2618 | # endif | |
cb68f92d GS |
2619 | #endif |
2620 | ||
cb68f92d GS |
2621 | } |
2622 | ||
76e3520e | 2623 | STATIC void |
cea2e8a9 | 2624 | S_init_main_stash(pTHX) |
79072805 | 2625 | { |
463ee0b2 | 2626 | GV *gv; |
6e72f9df | 2627 | |
2628 | /* Note that strtab is a rather special HV. Assumptions are made | |
2629 | about not iterating on it, and not adding tie magic to it. | |
2630 | It is properly deallocated in perl_destruct() */ | |
3280af22 | 2631 | PL_strtab = newHV(); |
5f08fbcd GS |
2632 | #ifdef USE_THREADS |
2633 | MUTEX_INIT(&PL_strtab_mutex); | |
2634 | #endif | |
3280af22 NIS |
2635 | HvSHAREKEYS_off(PL_strtab); /* mandatory */ |
2636 | hv_ksplit(PL_strtab, 512); | |
ac27b0f5 | 2637 | |
3280af22 | 2638 | PL_curstash = PL_defstash = newHV(); |
79cb57f6 | 2639 | PL_curstname = newSVpvn("main",4); |
adbc6bb1 LW |
2640 | gv = gv_fetchpv("main::",TRUE, SVt_PVHV); |
2641 | SvREFCNT_dec(GvHV(gv)); | |
3280af22 | 2642 | GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash); |
463ee0b2 | 2643 | SvREADONLY_on(gv); |
3280af22 NIS |
2644 | HvNAME(PL_defstash) = savepv("main"); |
2645 | PL_incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV))); | |
2646 | GvMULTI_on(PL_incgv); | |
2647 | PL_hintgv = gv_fetchpv("\010",TRUE, SVt_PV); /* ^H */ | |
2648 | GvMULTI_on(PL_hintgv); | |
2649 | PL_defgv = gv_fetchpv("_",TRUE, SVt_PVAV); | |
2650 | PL_errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV)); | |
2651 | GvMULTI_on(PL_errgv); | |
2652 | PL_replgv = gv_fetchpv("\022", TRUE, SVt_PV); /* ^R */ | |
2653 | GvMULTI_on(PL_replgv); | |
cea2e8a9 | 2654 | (void)Perl_form(aTHX_ "%240s",""); /* Preallocate temp - for immediate signals. */ |
38a03e6e MB |
2655 | sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */ |
2656 | sv_setpvn(ERRSV, "", 0); | |
3280af22 | 2657 | PL_curstash = PL_defstash; |
11faa288 | 2658 | CopSTASH_set(&PL_compiling, PL_defstash); |
ed094faf | 2659 | PL_debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV)); |
3280af22 | 2660 | PL_globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDMULTI, SVt_PVHV)); |
92d29cee | 2661 | PL_nullstash = GvHV(gv_fetchpv("<none>::", GV_ADDMULTI, SVt_PVHV)); |
4633a7c4 | 2662 | /* We must init $/ before switches are processed. */ |
864dbfa3 | 2663 | sv_setpvn(get_sv("/", TRUE), "\n", 1); |
79072805 LW |
2664 | } |
2665 | ||
76e3520e | 2666 | STATIC void |
cea2e8a9 | 2667 | S_open_script(pTHX_ char *scriptname, bool dosearch, SV *sv, int *fdscript) |
79072805 | 2668 | { |
6c4ab083 | 2669 | *fdscript = -1; |
79072805 | 2670 | |
3280af22 NIS |
2671 | if (PL_e_script) { |
2672 | PL_origfilename = savepv("-e"); | |
96436eeb | 2673 | } |
6c4ab083 GS |
2674 | else { |
2675 | /* if find_script() returns, it returns a malloc()-ed value */ | |
3280af22 | 2676 | PL_origfilename = scriptname = find_script(scriptname, dosearch, NULL, 1); |
6c4ab083 GS |
2677 | |
2678 | if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) { | |
2679 | char *s = scriptname + 8; | |
2680 | *fdscript = atoi(s); | |
2681 | while (isDIGIT(*s)) | |
2682 | s++; | |
2683 | if (*s) { | |
2684 | scriptname = savepv(s + 1); | |
3280af22 NIS |
2685 | Safefree(PL_origfilename); |
2686 | PL_origfilename = scriptname; | |
6c4ab083 GS |
2687 | } |
2688 | } | |
2689 | } | |
2690 | ||
f4dd75d9 GS |
2691 | #ifdef USE_ITHREADS |
2692 | Safefree(CopFILE(PL_curcop)); | |
2693 | #else | |
2694 | SvREFCNT_dec(CopFILEGV(PL_curcop)); | |
2695 | #endif | |
57843af0 | 2696 | CopFILE_set(PL_curcop, PL_origfilename); |
3280af22 | 2697 | if (strEQ(PL_origfilename,"-")) |
79072805 | 2698 | scriptname = ""; |
01f988be | 2699 | if (*fdscript >= 0) { |
3280af22 | 2700 | PL_rsfp = PerlIO_fdopen(*fdscript,PERL_SCRIPT_MODE); |
96436eeb | 2701 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
3280af22 NIS |
2702 | if (PL_rsfp) |
2703 | fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1); /* ensure close-on-exec */ | |
96436eeb | 2704 | #endif |
2705 | } | |
3280af22 | 2706 | else if (PL_preprocess) { |
46fc3d4c | 2707 | char *cpp_cfg = CPPSTDIN; |
79cb57f6 | 2708 | SV *cpp = newSVpvn("",0); |
46fc3d4c | 2709 | SV *cmd = NEWSV(0,0); |
2710 | ||
2711 | if (strEQ(cpp_cfg, "cppstdin")) | |
cea2e8a9 | 2712 | Perl_sv_catpvf(aTHX_ cpp, "%s/", BIN_EXP); |
46fc3d4c | 2713 | sv_catpv(cpp, cpp_cfg); |
79072805 | 2714 | |
0df16ed7 | 2715 | sv_catpvn(sv, "-I", 2); |
fed7345c | 2716 | sv_catpv(sv,PRIVLIB_EXP); |
46fc3d4c | 2717 | |
14953ddc MB |
2718 | DEBUG_P(PerlIO_printf(Perl_debug_log, |
2719 | "PL_preprocess: scriptname=\"%s\", cpp=\"%s\", sv=\"%s\", CPPMINUS=\"%s\"\n", | |
2720 | scriptname, SvPVX (cpp), SvPVX (sv), CPPMINUS)); | |
52853b95 | 2721 | #if defined(MSDOS) || defined(WIN32) |
cea2e8a9 | 2722 | Perl_sv_setpvf(aTHX_ cmd, "\ |
79072805 LW |
2723 | sed %s -e \"/^[^#]/b\" \ |
2724 | -e \"/^#[ ]*include[ ]/b\" \ | |
2725 | -e \"/^#[ ]*define[ ]/b\" \ | |
2726 | -e \"/^#[ ]*if[ ]/b\" \ | |
2727 | -e \"/^#[ ]*ifdef[ ]/b\" \ | |
2728 | -e \"/^#[ ]*ifndef[ ]/b\" \ | |
2729 | -e \"/^#[ ]*else/b\" \ | |
2730 | -e \"/^#[ ]*elif[ ]/b\" \ | |
2731 | -e \"/^#[ ]*undef[ ]/b\" \ | |
2732 | -e \"/^#[ ]*endif/b\" \ | |
2733 | -e \"s/^#.*//\" \ | |
894356b3 | 2734 | %s | %"SVf" -C %"SVf" %s", |
6b88bc9c | 2735 | (PL_doextract ? "-e \"1,/^#/d\n\"" : ""), |
79072805 | 2736 | #else |
092bebab | 2737 | # ifdef __OPEN_VM |
cea2e8a9 | 2738 | Perl_sv_setpvf(aTHX_ cmd, "\ |
092bebab JH |
2739 | %s %s -e '/^[^#]/b' \ |
2740 | -e '/^#[ ]*include[ ]/b' \ | |
2741 | -e '/^#[ ]*define[ ]/b' \ | |
2742 | -e '/^#[ ]*if[ ]/b' \ | |
2743 | -e '/^#[ ]*ifdef[ ]/b' \ | |
2744 | -e '/^#[ ]*ifndef[ ]/b' \ | |
2745 | -e '/^#[ ]*else/b' \ | |
2746 | -e '/^#[ ]*elif[ ]/b' \ | |
2747 | -e '/^#[ ]*undef[ ]/b' \ | |
2748 | -e '/^#[ ]*endif/b' \ | |
2749 | -e 's/^[ ]*#.*//' \ | |
894356b3 | 2750 | %s | %"SVf" %"SVf" %s", |
092bebab | 2751 | # else |
cea2e8a9 | 2752 | Perl_sv_setpvf(aTHX_ cmd, "\ |
79072805 LW |
2753 | %s %s -e '/^[^#]/b' \ |
2754 | -e '/^#[ ]*include[ ]/b' \ | |
2755 | -e '/^#[ ]*define[ ]/b' \ | |
2756 | -e '/^#[ ]*if[ ]/b' \ | |
2757 | -e '/^#[ ]*ifdef[ ]/b' \ | |
2758 | -e '/^#[ ]*ifndef[ ]/b' \ | |
2759 | -e '/^#[ ]*else/b' \ | |
2760 | -e '/^#[ ]*elif[ ]/b' \ | |
2761 | -e '/^#[ ]*undef[ ]/b' \ | |
2762 | -e '/^#[ ]*endif/b' \ | |
2763 | -e 's/^[ ]*#.*//' \ | |
894356b3 | 2764 | %s | %"SVf" -C %"SVf" %s", |
092bebab | 2765 | # endif |
79072805 LW |
2766 | #ifdef LOC_SED |
2767 | LOC_SED, | |
2768 | #else | |
2769 | "sed", | |
2770 | #endif | |
3280af22 | 2771 | (PL_doextract ? "-e '1,/^#/d\n'" : ""), |
79072805 | 2772 | #endif |
46fc3d4c | 2773 | scriptname, cpp, sv, CPPMINUS); |
3280af22 | 2774 | PL_doextract = FALSE; |
79072805 | 2775 | #ifdef IAMSUID /* actually, this is caught earlier */ |
b28d0864 | 2776 | if (PL_euid != PL_uid && !PL_euid) { /* if running suidperl */ |
79072805 | 2777 | #ifdef HAS_SETEUID |
b28d0864 | 2778 | (void)seteuid(PL_uid); /* musn't stay setuid root */ |
79072805 LW |
2779 | #else |
2780 | #ifdef HAS_SETREUID | |
b28d0864 | 2781 | (void)setreuid((Uid_t)-1, PL_uid); |
85e6fe83 LW |
2782 | #else |
2783 | #ifdef HAS_SETRESUID | |
b28d0864 | 2784 | (void)setresuid((Uid_t)-1, PL_uid, (Uid_t)-1); |
79072805 | 2785 | #else |
b28d0864 | 2786 | PerlProc_setuid(PL_uid); |
79072805 LW |
2787 | #endif |
2788 | #endif | |
85e6fe83 | 2789 | #endif |
b28d0864 | 2790 | if (PerlProc_geteuid() != PL_uid) |
cea2e8a9 | 2791 | Perl_croak(aTHX_ "Can't do seteuid!\n"); |
79072805 LW |
2792 | } |
2793 | #endif /* IAMSUID */ | |
3280af22 | 2794 | PL_rsfp = PerlProc_popen(SvPVX(cmd), "r"); |
46fc3d4c | 2795 | SvREFCNT_dec(cmd); |
2796 | SvREFCNT_dec(cpp); | |
79072805 LW |
2797 | } |
2798 | else if (!*scriptname) { | |
bbce6d69 | 2799 | forbid_setid("program input from stdin"); |
3280af22 | 2800 | PL_rsfp = PerlIO_stdin(); |
79072805 | 2801 | } |
96436eeb | 2802 | else { |
3280af22 | 2803 | PL_rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE); |
96436eeb | 2804 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
3280af22 NIS |
2805 | if (PL_rsfp) |
2806 | fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,1); /* ensure close-on-exec */ | |
96436eeb | 2807 | #endif |
2808 | } | |
3280af22 | 2809 | if (!PL_rsfp) { |
13281fa4 | 2810 | #ifdef DOSUID |
a687059c | 2811 | #ifndef IAMSUID /* in case script is not readable before setuid */ |
6b88bc9c | 2812 | if (PL_euid && |
cc49e20b | 2813 | PerlLIO_stat(CopFILE(PL_curcop),&PL_statbuf) >= 0 && |
6b88bc9c GS |
2814 | PL_statbuf.st_mode & (S_ISUID|S_ISGID)) |
2815 | { | |
46fc3d4c | 2816 | /* try again */ |
a7cb1f99 | 2817 | PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP, |
273cf8d1 GS |
2818 | (int)PERL_REVISION, (int)PERL_VERSION, |
2819 | (int)PERL_SUBVERSION), PL_origargv); | |
cea2e8a9 | 2820 | Perl_croak(aTHX_ "Can't do setuid\n"); |
13281fa4 LW |
2821 | } |
2822 | #endif | |
2823 | #endif | |
584d69ec JH |
2824 | #ifdef IAMSUID |
2825 | errno = EPERM; | |
2826 | Perl_croak(aTHX_ "Can't open perl script: %s\n", | |
2827 | Strerror(errno)); | |
2828 | #else | |
cea2e8a9 | 2829 | Perl_croak(aTHX_ "Can't open perl script \"%s\": %s\n", |
cc49e20b | 2830 | CopFILE(PL_curcop), Strerror(errno)); |
584d69ec | 2831 | #endif |
13281fa4 | 2832 | } |
79072805 | 2833 | } |
8d063cd8 | 2834 | |
7b89560d JH |
2835 | /* Mention |
2836 | * I_SYSSTATVFS HAS_FSTATVFS | |
2837 | * I_SYSMOUNT | |
c890dc6c | 2838 | * I_STATFS HAS_FSTATFS HAS_GETFSSTAT |
7b89560d JH |
2839 | * I_MNTENT HAS_GETMNTENT HAS_HASMNTOPT |
2840 | * here so that metaconfig picks them up. */ | |
2841 | ||
104d25b7 | 2842 | #ifdef IAMSUID |
864dbfa3 | 2843 | STATIC int |
e688b231 | 2844 | S_fd_on_nosuid_fs(pTHX_ int fd) |
104d25b7 | 2845 | { |
0545a864 JH |
2846 | int check_okay = 0; /* able to do all the required sys/libcalls */ |
2847 | int on_nosuid = 0; /* the fd is on a nosuid fs */ | |
104d25b7 | 2848 | /* |
ad27e871 | 2849 | * Preferred order: fstatvfs(), fstatfs(), ustat()+getmnt(), getmntent(). |
e688b231 | 2850 | * fstatvfs() is UNIX98. |
0545a864 | 2851 | * fstatfs() is 4.3 BSD. |
ad27e871 | 2852 | * ustat()+getmnt() is pre-4.3 BSD. |
0545a864 JH |
2853 | * getmntent() is O(number-of-mounted-filesystems) and can hang on |
2854 | * an irrelevant filesystem while trying to reach the right one. | |
104d25b7 JH |
2855 | */ |
2856 | ||
6439433f JH |
2857 | #undef FD_ON_NOSUID_CHECK_OKAY /* found the syscalls to do the check? */ |
2858 | ||
2859 | # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \ | |
2860 | defined(HAS_FSTATVFS) | |
2861 | # define FD_ON_NOSUID_CHECK_OKAY | |
104d25b7 | 2862 | struct statvfs stfs; |
6439433f | 2863 | |
104d25b7 JH |
2864 | check_okay = fstatvfs(fd, &stfs) == 0; |
2865 | on_nosuid = check_okay && (stfs.f_flag & ST_NOSUID); | |
6439433f | 2866 | # endif /* fstatvfs */ |
ac27b0f5 | 2867 | |
6439433f JH |
2868 | # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \ |
2869 | defined(PERL_MOUNT_NOSUID) && \ | |
2870 | defined(HAS_FSTATFS) && \ | |
2871 | defined(HAS_STRUCT_STATFS) && \ | |
2872 | defined(HAS_STRUCT_STATFS_F_FLAGS) | |
2873 | # define FD_ON_NOSUID_CHECK_OKAY | |
e688b231 | 2874 | struct statfs stfs; |
6439433f | 2875 | |
104d25b7 | 2876 | check_okay = fstatfs(fd, &stfs) == 0; |
104d25b7 | 2877 | on_nosuid = check_okay && (stfs.f_flags & PERL_MOUNT_NOSUID); |
6439433f JH |
2878 | # endif /* fstatfs */ |
2879 | ||
2880 | # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \ | |
2881 | defined(PERL_MOUNT_NOSUID) && \ | |
2882 | defined(HAS_FSTAT) && \ | |
2883 | defined(HAS_USTAT) && \ | |
2884 | defined(HAS_GETMNT) && \ | |
2885 | defined(HAS_STRUCT_FS_DATA) && \ | |
2886 | defined(NOSTAT_ONE) | |
2887 | # define FD_ON_NOSUID_CHECK_OKAY | |
0545a864 | 2888 | struct stat fdst; |
6439433f | 2889 | |
0545a864 | 2890 | if (fstat(fd, &fdst) == 0) { |
6439433f JH |
2891 | struct ustat us; |
2892 | if (ustat(fdst.st_dev, &us) == 0) { | |
2893 | struct fs_data fsd; | |
2894 | /* NOSTAT_ONE here because we're not examining fields which | |
2895 | * vary between that case and STAT_ONE. */ | |
ad27e871 | 2896 | if (getmnt((int*)0, &fsd, (int)0, NOSTAT_ONE, us.f_fname) == 0) { |
6439433f JH |
2897 | size_t cmplen = sizeof(us.f_fname); |
2898 | if (sizeof(fsd.fd_req.path) < cmplen) | |
2899 | cmplen = sizeof(fsd.fd_req.path); | |
2900 | if (strnEQ(fsd.fd_req.path, us.f_fname, cmplen) && | |
2901 | fdst.st_dev == fsd.fd_req.dev) { | |
2902 | check_okay = 1; | |
2903 | on_nosuid = fsd.fd_req.flags & PERL_MOUNT_NOSUID; | |
2904 | } | |
2905 | } | |
2906 | } | |
2907 | } | |
0545a864 | 2908 | } |
6439433f JH |
2909 | # endif /* fstat+ustat+getmnt */ |
2910 | ||
2911 | # if !defined(FD_ON_NOSUID_CHECK_OKAY) && \ | |
2912 | defined(HAS_GETMNTENT) && \ | |
2913 | defined(HAS_HASMNTOPT) && \ | |
2914 | defined(MNTOPT_NOSUID) | |
2915 | # define FD_ON_NOSUID_CHECK_OKAY | |
2916 | FILE *mtab = fopen("/etc/mtab", "r"); | |
2917 | struct mntent *entry; | |
2918 | struct stat stb, fsb; | |
104d25b7 JH |
2919 | |
2920 | if (mtab && (fstat(fd, &stb) == 0)) { | |
6439433f JH |
2921 | while (entry = getmntent(mtab)) { |
2922 | if (stat(entry->mnt_dir, &fsb) == 0 | |
2923 | && fsb.st_dev == stb.st_dev) | |
2924 | { | |
2925 | /* found the filesystem */ | |
2926 | check_okay = 1; | |
2927 | if (hasmntopt(entry, MNTOPT_NOSUID)) | |
2928 | on_nosuid = 1; | |
2929 | break; | |
2930 | } /* A single fs may well fail its stat(). */ | |
2931 | } | |
104d25b7 JH |
2932 | } |
2933 | if (mtab) | |
6439433f JH |
2934 | fclose(mtab); |
2935 | # endif /* getmntent+hasmntopt */ | |
0545a864 | 2936 | |
ac27b0f5 | 2937 | if (!check_okay) |
0545a864 | 2938 | Perl_croak(aTHX_ "Can't check filesystem of script \"%s\" for nosuid", PL_origfilename); |
104d25b7 JH |
2939 | return on_nosuid; |
2940 | } | |
2941 | #endif /* IAMSUID */ | |
2942 | ||
76e3520e | 2943 | STATIC void |
cea2e8a9 | 2944 | S_validate_suid(pTHX_ char *validarg, char *scriptname, int fdscript) |
79072805 | 2945 | { |
155aba94 | 2946 | #ifdef IAMSUID |
96436eeb | 2947 | int which; |
155aba94 | 2948 | #endif |
96436eeb | 2949 | |
13281fa4 LW |
2950 | /* do we need to emulate setuid on scripts? */ |
2951 | ||
2952 | /* This code is for those BSD systems that have setuid #! scripts disabled | |
2953 | * in the kernel because of a security problem. Merely defining DOSUID | |
2954 | * in perl will not fix that problem, but if you have disabled setuid | |
2955 | * scripts in the kernel, this will attempt to emulate setuid and setgid | |
2956 | * on scripts that have those now-otherwise-useless bits set. The setuid | |
27e2fb84 LW |
2957 | * root version must be called suidperl or sperlN.NNN. If regular perl |
2958 | * discovers that it has opened a setuid script, it calls suidperl with | |
2959 | * the same argv that it had. If suidperl finds that the script it has | |
2960 | * just opened is NOT setuid root, it sets the effective uid back to the | |
2961 | * uid. We don't just make perl setuid root because that loses the | |
2962 | * effective uid we had before invoking perl, if it was different from the | |
2963 | * uid. | |
13281fa4 LW |
2964 | * |
2965 | * DOSUID must be defined in both perl and suidperl, and IAMSUID must | |
2966 | * be defined in suidperl only. suidperl must be setuid root. The | |
2967 | * Configure script will set this up for you if you want it. | |
2968 | */ | |
a687059c | 2969 | |
13281fa4 | 2970 | #ifdef DOSUID |
6e72f9df | 2971 | char *s, *s2; |
a0d0e21e | 2972 | |
b28d0864 | 2973 | if (PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf) < 0) /* normal stat is insecure */ |
cea2e8a9 | 2974 | Perl_croak(aTHX_ "Can't stat script \"%s\"",PL_origfilename); |
b28d0864 | 2975 | if (fdscript < 0 && PL_statbuf.st_mode & (S_ISUID|S_ISGID)) { |
79072805 | 2976 | I32 len; |
2d8e6c8d | 2977 | STRLEN n_a; |
13281fa4 | 2978 | |
a687059c | 2979 | #ifdef IAMSUID |
fe14fcc3 | 2980 | #ifndef HAS_SETREUID |
a687059c LW |
2981 | /* On this access check to make sure the directories are readable, |
2982 | * there is actually a small window that the user could use to make | |
2983 | * filename point to an accessible directory. So there is a faint | |
2984 | * chance that someone could execute a setuid script down in a | |
2985 | * non-accessible directory. I don't know what to do about that. | |
2986 | * But I don't think it's too important. The manual lies when | |
2987 | * it says access() is useful in setuid programs. | |
2988 | */ | |
cc49e20b | 2989 | if (PerlLIO_access(CopFILE(PL_curcop),1)) /*double check*/ |
cea2e8a9 | 2990 | Perl_croak(aTHX_ "Permission denied"); |
a687059c LW |
2991 | #else |
2992 | /* If we can swap euid and uid, then we can determine access rights | |
2993 | * with a simple stat of the file, and then compare device and | |
2994 | * inode to make sure we did stat() on the same file we opened. | |
2995 | * Then we just have to make sure he or she can execute it. | |
2996 | */ | |
2997 | { | |
2998 | struct stat tmpstatbuf; | |
2999 | ||
85e6fe83 LW |
3000 | if ( |
3001 | #ifdef HAS_SETREUID | |
b28d0864 | 3002 | setreuid(PL_euid,PL_uid) < 0 |
a0d0e21e LW |
3003 | #else |
3004 | # if HAS_SETRESUID | |
b28d0864 | 3005 | setresuid(PL_euid,PL_uid,(Uid_t)-1) < 0 |
a0d0e21e | 3006 | # endif |
85e6fe83 | 3007 | #endif |
b28d0864 | 3008 | || PerlProc_getuid() != PL_euid || PerlProc_geteuid() != PL_uid) |
cea2e8a9 | 3009 | Perl_croak(aTHX_ "Can't swap uid and euid"); /* really paranoid */ |
cc49e20b | 3010 | if (PerlLIO_stat(CopFILE(PL_curcop),&tmpstatbuf) < 0) |
cea2e8a9 | 3011 | Perl_croak(aTHX_ "Permission denied"); /* testing full pathname here */ |
2bb3463c | 3012 | #if defined(IAMSUID) && !defined(NO_NOSUID_CHECK) |
e688b231 | 3013 | if (fd_on_nosuid_fs(PerlIO_fileno(PL_rsfp))) |
cea2e8a9 | 3014 | Perl_croak(aTHX_ "Permission denied"); |
104d25b7 | 3015 | #endif |
b28d0864 NIS |
3016 | if (tmpstatbuf.st_dev != PL_statbuf.st_dev || |
3017 | tmpstatbuf.st_ino != PL_statbuf.st_ino) { | |
3018 | (void)PerlIO_close(PL_rsfp); | |
cea2e8a9 | 3019 | Perl_croak(aTHX_ "Permission denied\n"); |
a687059c | 3020 | } |
85e6fe83 LW |
3021 | if ( |
3022 | #ifdef HAS_SETREUID | |
b28d0864 | 3023 | setreuid(PL_uid,PL_euid) < 0 |
a0d0e21e LW |
3024 | #else |
3025 | # if defined(HAS_SETRESUID) | |
b28d0864 | 3026 | setresuid(PL_uid,PL_euid,(Uid_t)-1) < 0 |
a0d0e21e | 3027 | # endif |
85e6fe83 | 3028 | #endif |
b28d0864 | 3029 | || PerlProc_getuid() != PL_uid || PerlProc_geteuid() != PL_euid) |
cea2e8a9 | 3030 | Perl_croak(aTHX_ "Can't reswap uid and euid"); |
b28d0864 | 3031 | if (!cando(S_IXUSR,FALSE,&PL_statbuf)) /* can real uid exec? */ |
cea2e8a9 | 3032 | Perl_croak(aTHX_ "Permission denied\n"); |
a687059c | 3033 | } |
fe14fcc3 | 3034 | #endif /* HAS_SETREUID */ |
a687059c LW |
3035 | #endif /* IAMSUID */ |
3036 | ||
b28d0864 | 3037 | if (!S_ISREG(PL_statbuf.st_mode)) |
cea2e8a9 | 3038 | Perl_croak(aTHX_ "Permission denied"); |
b28d0864 | 3039 | if (PL_statbuf.st_mode & S_IWOTH) |
cea2e8a9 | 3040 | Perl_croak(aTHX_ "Setuid/gid script is writable by world"); |
6b88bc9c | 3041 | PL_doswitches = FALSE; /* -s is insecure in suid */ |
57843af0 | 3042 | CopLINE_inc(PL_curcop); |
6b88bc9c | 3043 | if (sv_gets(PL_linestr, PL_rsfp, 0) == Nullch || |
2d8e6c8d | 3044 | strnNE(SvPV(PL_linestr,n_a),"#!",2) ) /* required even on Sys V */ |
cea2e8a9 | 3045 | Perl_croak(aTHX_ "No #! line"); |
2d8e6c8d | 3046 | s = SvPV(PL_linestr,n_a)+2; |
663a0e37 | 3047 | if (*s == ' ') s++; |
45d8adaa | 3048 | while (!isSPACE(*s)) s++; |
2d8e6c8d | 3049 | for (s2 = s; (s2 > SvPV(PL_linestr,n_a)+2 && |
6e72f9df | 3050 | (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ; |
3051 | if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */ | |
cea2e8a9 | 3052 | Perl_croak(aTHX_ "Not a perl script"); |
a687059c | 3053 | while (*s == ' ' || *s == '\t') s++; |
13281fa4 LW |
3054 | /* |
3055 | * #! arg must be what we saw above. They can invoke it by | |
3056 | * mentioning suidperl explicitly, but they may not add any strange | |
3057 | * arguments beyond what #! says if they do invoke suidperl that way. | |
3058 | */ | |
3059 | len = strlen(validarg); | |
3060 | if (strEQ(validarg," PHOOEY ") || | |
45d8adaa | 3061 | strnNE(s,validarg,len) || !isSPACE(s[len])) |
cea2e8a9 | 3062 | Perl_croak(aTHX_ "Args must match #! line"); |
a687059c LW |
3063 | |
3064 | #ifndef IAMSUID | |
b28d0864 NIS |
3065 | if (PL_euid != PL_uid && (PL_statbuf.st_mode & S_ISUID) && |
3066 | PL_euid == PL_statbuf.st_uid) | |
3067 | if (!PL_do_undump) | |
cea2e8a9 | 3068 | Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\ |
a687059c LW |
3069 | FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n"); |
3070 | #endif /* IAMSUID */ | |
13281fa4 | 3071 | |
b28d0864 NIS |
3072 | if (PL_euid) { /* oops, we're not the setuid root perl */ |
3073 | (void)PerlIO_close(PL_rsfp); | |
13281fa4 | 3074 | #ifndef IAMSUID |
46fc3d4c | 3075 | /* try again */ |
a7cb1f99 | 3076 | PerlProc_execv(Perl_form(aTHX_ "%s/sperl"PERL_FS_VER_FMT, BIN_EXP, |
273cf8d1 GS |
3077 | (int)PERL_REVISION, (int)PERL_VERSION, |
3078 | (int)PERL_SUBVERSION), PL_origargv); | |
13281fa4 | 3079 | #endif |
cea2e8a9 | 3080 | Perl_croak(aTHX_ "Can't do setuid\n"); |
13281fa4 LW |
3081 | } |
3082 | ||
b28d0864 | 3083 | if (PL_statbuf.st_mode & S_ISGID && PL_statbuf.st_gid != PL_egid) { |
fe14fcc3 | 3084 | #ifdef HAS_SETEGID |
b28d0864 | 3085 | (void)setegid(PL_statbuf.st_gid); |
a687059c | 3086 | #else |
fe14fcc3 | 3087 | #ifdef HAS_SETREGID |
b28d0864 | 3088 | (void)setregid((Gid_t)-1,PL_statbuf.st_gid); |
85e6fe83 LW |
3089 | #else |
3090 | #ifdef HAS_SETRESGID | |
b28d0864 | 3091 | (void)setresgid((Gid_t)-1,PL_statbuf.st_gid,(Gid_t)-1); |
a687059c | 3092 | #else |
b28d0864 | 3093 | PerlProc_setgid(PL_statbuf.st_gid); |
a687059c LW |
3094 | #endif |
3095 | #endif | |
85e6fe83 | 3096 | #endif |
b28d0864 | 3097 | if (PerlProc_getegid() != PL_statbuf.st_gid) |
cea2e8a9 | 3098 | Perl_croak(aTHX_ "Can't do setegid!\n"); |
83025b21 | 3099 | } |
b28d0864 NIS |
3100 | if (PL_statbuf.st_mode & S_ISUID) { |
3101 | if (PL_statbuf.st_uid != PL_euid) | |
fe14fcc3 | 3102 | #ifdef HAS_SETEUID |
b28d0864 | 3103 | (void)seteuid(PL_statbuf.st_uid); /* all that for this */ |
a687059c | 3104 | #else |
fe14fcc3 | 3105 | #ifdef HAS_SETREUID |
b28d0864 | 3106 | (void)setreuid((Uid_t)-1,PL_statbuf.st_uid); |
85e6fe83 LW |
3107 | #else |
3108 | #ifdef HAS_SETRESUID | |
b28d0864 | 3109 | (void)setresuid((Uid_t)-1,PL_statbuf.st_uid,(Uid_t)-1); |
a687059c | 3110 | #else |
b28d0864 | 3111 | PerlProc_setuid(PL_statbuf.st_uid); |
a687059c LW |
3112 | #endif |
3113 | #endif | |
85e6fe83 | 3114 | #endif |
b28d0864 | 3115 | if (PerlProc_geteuid() != PL_statbuf.st_uid) |
cea2e8a9 | 3116 | Perl_croak(aTHX_ "Can't do seteuid!\n"); |
a687059c | 3117 | } |
b28d0864 | 3118 | else if (PL_uid) { /* oops, mustn't run as root */ |
fe14fcc3 | 3119 | #ifdef HAS_SETEUID |
b28d0864 | 3120 | (void)seteuid((Uid_t)PL_uid); |
a687059c | 3121 | #else |
fe14fcc3 | 3122 | #ifdef HAS_SETREUID |
b28d0864 | 3123 | (void)setreuid((Uid_t)-1,(Uid_t)PL_uid); |
a687059c | 3124 | #else |
85e6fe83 | 3125 | #ifdef HAS_SETRESUID |
b28d0864 | 3126 | (void)setresuid((Uid_t)-1,(Uid_t)PL_uid,(Uid_t)-1); |
85e6fe83 | 3127 | #else |
b28d0864 | 3128 | PerlProc_setuid((Uid_t)PL_uid); |
85e6fe83 | 3129 | #endif |
a687059c LW |
3130 | #endif |
3131 | #endif | |
b28d0864 | 3132 | if (PerlProc_geteuid() != PL_uid) |
cea2e8a9 | 3133 | Perl_croak(aTHX_ "Can't do seteuid!\n"); |
83025b21 | 3134 | } |
748a9306 | 3135 | init_ids(); |
b28d0864 | 3136 | if (!cando(S_IXUSR,TRUE,&PL_statbuf)) |
cea2e8a9 | 3137 | Perl_croak(aTHX_ "Permission denied\n"); /* they can't do this */ |
13281fa4 LW |
3138 | } |
3139 | #ifdef IAMSUID | |
6b88bc9c | 3140 | else if (PL_preprocess) |
cea2e8a9 | 3141 | Perl_croak(aTHX_ "-P not allowed for setuid/setgid script\n"); |
96436eeb | 3142 | else if (fdscript >= 0) |
cea2e8a9 | 3143 | Perl_croak(aTHX_ "fd script not allowed in suidperl\n"); |
13281fa4 | 3144 | else |
cea2e8a9 | 3145 | Perl_croak(aTHX_ "Script is not setuid/setgid in suidperl\n"); |
96436eeb | 3146 | |
3147 | /* We absolutely must clear out any saved ids here, so we */ | |
3148 | /* exec the real perl, substituting fd script for scriptname. */ | |
3149 | /* (We pass script name as "subdir" of fd, which perl will grok.) */ | |
b28d0864 NIS |
3150 | PerlIO_rewind(PL_rsfp); |
3151 | PerlLIO_lseek(PerlIO_fileno(PL_rsfp),(Off_t)0,0); /* just in case rewind didn't */ | |
6b88bc9c GS |
3152 | for (which = 1; PL_origargv[which] && PL_origargv[which] != scriptname; which++) ; |
3153 | if (!PL_origargv[which]) | |
cea2e8a9 GS |
3154 | Perl_croak(aTHX_ "Permission denied"); |
3155 | PL_origargv[which] = savepv(Perl_form(aTHX_ "/dev/fd/%d/%s", | |
6b88bc9c | 3156 | PerlIO_fileno(PL_rsfp), PL_origargv[which])); |
96436eeb | 3157 | #if defined(HAS_FCNTL) && defined(F_SETFD) |
b28d0864 | 3158 | fcntl(PerlIO_fileno(PL_rsfp),F_SETFD,0); /* ensure no close-on-exec */ |
96436eeb | 3159 | #endif |
a7cb1f99 | 3160 | PerlProc_execv(Perl_form(aTHX_ "%s/perl"PERL_FS_VER_FMT, BIN_EXP, |
273cf8d1 GS |
3161 | (int)PERL_REVISION, (int)PERL_VERSION, |
3162 | (int)PERL_SUBVERSION), PL_origargv);/* try again */ | |
cea2e8a9 | 3163 | Perl_croak(aTHX_ "Can't do setuid\n"); |
13281fa4 | 3164 | #endif /* IAMSUID */ |
a687059c | 3165 | #else /* !DOSUID */ |
3280af22 | 3166 | if (PL_euid != PL_uid || PL_egid != PL_gid) { /* (suidperl doesn't exist, in fact) */ |
a687059c | 3167 | #ifndef SETUID_SCRIPTS_ARE_SECURE_NOW |
b28d0864 NIS |
3168 | PerlLIO_fstat(PerlIO_fileno(PL_rsfp),&PL_statbuf); /* may be either wrapped or real suid */ |
3169 | if ((PL_euid != PL_uid && PL_euid == PL_statbuf.st_uid && PL_statbuf.st_mode & S_ISUID) | |
a687059c | 3170 | || |
b28d0864 | 3171 | (PL_egid != PL_gid && PL_egid == PL_statbuf.st_gid && PL_statbuf.st_mode & S_ISGID) |
a687059c | 3172 | ) |
b28d0864 | 3173 | if (!PL_do_undump) |
cea2e8a9 | 3174 | Perl_croak(aTHX_ "YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\ |
a687059c LW |
3175 | FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n"); |
3176 | #endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */ | |
3177 | /* not set-id, must be wrapped */ | |
a687059c | 3178 | } |
13281fa4 | 3179 | #endif /* DOSUID */ |
79072805 | 3180 | } |
13281fa4 | 3181 | |
76e3520e | 3182 | STATIC void |
cea2e8a9 | 3183 | S_find_beginning(pTHX) |
79072805 | 3184 | { |
6e72f9df | 3185 | register char *s, *s2; |
33b78306 LW |
3186 | |
3187 | /* skip forward in input to the real script? */ | |
3188 | ||
bbce6d69 | 3189 | forbid_setid("-x"); |
bf4acbe4 | 3190 | #ifdef MACOS_TRADITIONAL |
084592ab | 3191 | /* Since the Mac OS does not honor #! arguments for us, we do it ourselves */ |
ac27b0f5 | 3192 | |
bf4acbe4 GS |
3193 | while (PL_doextract || gMacPerl_AlwaysExtract) { |
3194 | if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch) { | |
3195 | if (!gMacPerl_AlwaysExtract) | |
3196 | Perl_croak(aTHX_ "No Perl script found in input\n"); | |
3197 | ||
3198 | if (PL_doextract) /* require explicit override ? */ | |
3199 | if (!OverrideExtract(PL_origfilename)) | |
3200 | Perl_croak(aTHX_ "User aborted script\n"); | |
3201 | else | |
3202 | PL_doextract = FALSE; | |
3203 | ||
3204 | /* Pater peccavi, file does not have #! */ | |
3205 | PerlIO_rewind(PL_rsfp); | |
ac27b0f5 | 3206 | |
bf4acbe4 GS |
3207 | break; |
3208 | } | |
3209 | #else | |
3280af22 NIS |
3210 | while (PL_doextract) { |
3211 | if ((s = sv_gets(PL_linestr, PL_rsfp, 0)) == Nullch) | |
cea2e8a9 | 3212 | Perl_croak(aTHX_ "No Perl script found in input\n"); |
bf4acbe4 | 3213 | #endif |
4f0c37ba IZ |
3214 | s2 = s; |
3215 | if (*s == '#' && s[1] == '!' && ((s = instr(s,"perl")) || (s = instr(s2,"PERL")))) { | |
3280af22 NIS |
3216 | PerlIO_ungetc(PL_rsfp, '\n'); /* to keep line count right */ |
3217 | PL_doextract = FALSE; | |
6e72f9df | 3218 | while (*s && !(isSPACE (*s) || *s == '#')) s++; |
3219 | s2 = s; | |
3220 | while (*s == ' ' || *s == '\t') s++; | |
3221 | if (*s++ == '-') { | |
3222 | while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--; | |
3223 | if (strnEQ(s2-4,"perl",4)) | |
3224 | /*SUPPRESS 530*/ | |
155aba94 GS |
3225 | while ((s = moreswitches(s))) |
3226 | ; | |
33b78306 | 3227 | } |
95e8664e CN |
3228 | #ifdef MACOS_TRADITIONAL |
3229 | break; | |
3230 | #endif | |
83025b21 LW |
3231 | } |
3232 | } | |
3233 | } | |
3234 | ||
afe37c7d | 3235 | |
76e3520e | 3236 | STATIC void |
cea2e8a9 | 3237 | S_init_ids(pTHX) |
352d5a3a | 3238 | { |
d8eceb89 JH |
3239 | PL_uid = PerlProc_getuid(); |
3240 | PL_euid = PerlProc_geteuid(); | |
3241 | PL_gid = PerlProc_getgid(); | |
3242 | PL_egid = PerlProc_getegid(); | |
748a9306 | 3243 | #ifdef VMS |
b28d0864 NIS |
3244 | PL_uid |= PL_gid << 16; |
3245 | PL_euid |= PL_egid << 16; | |
748a9306 | 3246 | #endif |
3280af22 | 3247 | PL_tainting |= (PL_uid && (PL_euid != PL_uid || PL_egid != PL_gid)); |
748a9306 | 3248 | } |
79072805 | 3249 | |
76e3520e | 3250 | STATIC void |
cea2e8a9 | 3251 | S_forbid_setid(pTHX_ char *s) |
bbce6d69 | 3252 | { |
3280af22 | 3253 | if (PL_euid != PL_uid) |
cea2e8a9 | 3254 | Perl_croak(aTHX_ "No %s allowed while running setuid", s); |
3280af22 | 3255 | if (PL_egid != PL_gid) |
cea2e8a9 | 3256 | Perl_croak(aTHX_ "No %s allowed while running setgid", s); |
bbce6d69 | 3257 | } |
3258 | ||
1ee4443e IZ |
3259 | void |
3260 | Perl_init_debugger(pTHX) | |
748a9306 | 3261 | { |
1ee4443e IZ |
3262 | HV *ostash = PL_curstash; |
3263 | ||
3280af22 NIS |
3264 | PL_curstash = PL_debstash; |
3265 | PL_dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV)))); | |
3266 | AvREAL_off(PL_dbargs); | |
3267 | PL_DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV); | |
3268 | PL_DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV); | |
3269 | PL_DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV)); | |
1ee4443e | 3270 | sv_upgrade(GvSV(PL_DBsub), SVt_IV); /* IVX accessed if PERLDB_SUB_NN */ |
3280af22 | 3271 | PL_DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV))); |
ac27b0f5 | 3272 | sv_setiv(PL_DBsingle, 0); |
3280af22 | 3273 | PL_DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV))); |
ac27b0f5 | 3274 | sv_setiv(PL_DBtrace, 0); |
3280af22 | 3275 | PL_DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV))); |
ac27b0f5 | 3276 | sv_setiv(PL_DBsignal, 0); |
1ee4443e | 3277 | PL_curstash = ostash; |
352d5a3a LW |
3278 | } |
3279 | ||
2ce36478 SM |
3280 | #ifndef STRESS_REALLOC |
3281 | #define REASONABLE(size) (size) | |
3282 | #else | |
3283 | #define REASONABLE(size) (1) /* unreasonable */ | |
3284 | #endif | |
3285 | ||
11343788 | 3286 | void |
cea2e8a9 | 3287 | Perl_init_stacks(pTHX) |
79072805 | 3288 | { |
e336de0d | 3289 | /* start with 128-item stack and 8K cxstack */ |
3280af22 | 3290 | PL_curstackinfo = new_stackinfo(REASONABLE(128), |
e336de0d | 3291 | REASONABLE(8192/sizeof(PERL_CONTEXT) - 1)); |
3280af22 NIS |
3292 | PL_curstackinfo->si_type = PERLSI_MAIN; |
3293 | PL_curstack = PL_curstackinfo->si_stack; | |
3294 | PL_mainstack = PL_curstack; /* remember in case we switch stacks */ | |
79072805 | 3295 | |
3280af22 NIS |
3296 | PL_stack_base = AvARRAY(PL_curstack); |
3297 | PL_stack_sp = PL_stack_base; | |
3298 | PL_stack_max = PL_stack_base + AvMAX(PL_curstack); | |
8990e307 | 3299 | |
3280af22 NIS |
3300 | New(50,PL_tmps_stack,REASONABLE(128),SV*); |
3301 | PL_tmps_floor = -1; | |
3302 | PL_tmps_ix = -1; | |
3303 | PL_tmps_max = REASONABLE(128); | |
8990e307 | 3304 | |
3280af22 NIS |
3305 | New(54,PL_markstack,REASONABLE(32),I32); |
3306 | PL_markstack_ptr = PL_markstack; | |
3307 | PL_markstack_max = PL_markstack + REASONABLE(32); | |
79072805 | 3308 | |
ce2f7c3b | 3309 | SET_MARK_OFFSET; |
e336de0d | 3310 | |
3280af22 NIS |
3311 | New(54,PL_scopestack,REASONABLE(32),I32); |
3312 | PL_scopestack_ix = 0; | |
3313 | PL_scopestack_max = REASONABLE(32); | |
79072805 | 3314 | |
3280af22 NIS |
3315 | New(54,PL_savestack,REASONABLE(128),ANY); |
3316 | PL_savestack_ix = 0; | |
3317 | PL_savestack_max = REASONABLE(128); | |
79072805 | 3318 | |
3280af22 NIS |
3319 | New(54,PL_retstack,REASONABLE(16),OP*); |
3320 | PL_retstack_ix = 0; | |
3321 | PL_retstack_max = REASONABLE(16); | |
378cc40b | 3322 | } |
33b78306 | 3323 | |
2ce36478 SM |
3324 | #undef REASONABLE |
3325 | ||
76e3520e | 3326 | STATIC void |
cea2e8a9 | 3327 | S_nuke_stacks(pTHX) |
6e72f9df | 3328 | { |
3280af22 NIS |
3329 | while (PL_curstackinfo->si_next) |
3330 | PL_curstackinfo = PL_curstackinfo->si_next; | |
3331 | while (PL_curstackinfo) { | |
3332 | PERL_SI *p = PL_curstackinfo->si_prev; | |
bac4b2ad | 3333 | /* curstackinfo->si_stack got nuked by sv_free_arenas() */ |
3280af22 NIS |
3334 | Safefree(PL_curstackinfo->si_cxstack); |
3335 | Safefree(PL_curstackinfo); | |
3336 | PL_curstackinfo = p; | |
e336de0d | 3337 | } |
3280af22 NIS |
3338 | Safefree(PL_tmps_stack); |
3339 | Safefree(PL_markstack); | |
3340 | Safefree(PL_scopestack); | |
3341 | Safefree(PL_savestack); | |
3342 | Safefree(PL_retstack); | |
378cc40b | 3343 | } |
33b78306 | 3344 | |
76e3520e | 3345 | #ifndef PERL_OBJECT |
760ac839 | 3346 | static PerlIO *tmpfp; /* moved outside init_lexer() because of UNICOS bug */ |
76e3520e | 3347 | #endif |
7aa04957 | 3348 | |
76e3520e | 3349 | STATIC void |
cea2e8a9 | 3350 | S_init_lexer(pTHX) |
8990e307 | 3351 | { |
76e3520e GS |
3352 | #ifdef PERL_OBJECT |
3353 | PerlIO *tmpfp; | |
3354 | #endif | |
3280af22 NIS |
3355 | tmpfp = PL_rsfp; |
3356 | PL_rsfp = Nullfp; | |
3357 | lex_start(PL_linestr); | |
3358 | PL_rsfp = tmpfp; | |
79cb57f6 | 3359 | PL_subname = newSVpvn("main",4); |
8990e307 LW |
3360 | } |
3361 | ||
76e3520e | 3362 | STATIC void |
cea2e8a9 | 3363 | S_init_predump_symbols(pTHX) |
45d8adaa | 3364 | { |
93a17b20 | 3365 | GV *tmpgv; |
af8c498a | 3366 | IO *io; |
79072805 | 3367 | |
864dbfa3 | 3368 | sv_setpvn(get_sv("\"", TRUE), " ", 1); |
3280af22 NIS |
3369 | PL_stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO); |
3370 | GvMULTI_on(PL_stdingv); | |
af8c498a | 3371 | io = GvIOp(PL_stdingv); |
a04651f4 | 3372 | IoTYPE(io) = IoTYPE_RDONLY; |
af8c498a | 3373 | IoIFP(io) = PerlIO_stdin(); |
adbc6bb1 | 3374 | tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV); |
a5f75d66 | 3375 | GvMULTI_on(tmpgv); |
af8c498a | 3376 | GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io); |
79072805 | 3377 | |
85e6fe83 | 3378 | tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO); |
a5f75d66 | 3379 | GvMULTI_on(tmpgv); |
af8c498a | 3380 | io = GvIOp(tmpgv); |
a04651f4 | 3381 | IoTYPE(io) = IoTYPE_WRONLY; |
af8c498a | 3382 | IoOFP(io) = IoIFP(io) = PerlIO_stdout(); |
4633a7c4 | 3383 | setdefout(tmpgv); |
adbc6bb1 | 3384 | tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV); |
a5f75d66 | 3385 | GvMULTI_on(tmpgv); |
af8c498a | 3386 | GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io); |
79072805 | 3387 | |
bf49b057 GS |
3388 | PL_stderrgv = gv_fetchpv("STDERR",TRUE, SVt_PVIO); |
3389 | GvMULTI_on(PL_stderrgv); | |
3390 | io = GvIOp(PL_stderrgv); | |
a04651f4 | 3391 | IoTYPE(io) = IoTYPE_WRONLY; |
af8c498a | 3392 | IoOFP(io) = IoIFP(io) = PerlIO_stderr(); |
adbc6bb1 | 3393 | tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV); |
a5f75d66 | 3394 | GvMULTI_on(tmpgv); |
af8c498a | 3395 | GvIOp(tmpgv) = (IO*)SvREFCNT_inc(io); |
79072805 | 3396 | |
3280af22 | 3397 | PL_statname = NEWSV(66,0); /* last filename we did stat on */ |
ab821d7f | 3398 | |
bf4acbe4 GS |
3399 | if (PL_osname) |
3400 | Safefree(PL_osname); | |
3401 | PL_osname = savepv(OSNAME); | |
79072805 | 3402 | } |
33b78306 | 3403 | |
76e3520e | 3404 | STATIC void |
cea2e8a9 | 3405 | S_init_postdump_symbols(pTHX_ register int argc, register char **argv, register char **env) |
33b78306 | 3406 | { |
79072805 LW |
3407 | char *s; |
3408 | SV *sv; | |
3409 | GV* tmpgv; | |
9cbac4c7 | 3410 | #ifdef NEED_ENVIRON_DUP_FOR_MODIFY |
516a5887 | 3411 | char **dup_env_base = 0; |
103a7189 | 3412 | int dup_env_count = 0; |
9cbac4c7 | 3413 | #endif |
fe14fcc3 | 3414 | |
79072805 | 3415 | argc--,argv++; /* skip name of script */ |
3280af22 | 3416 | if (PL_doswitches) { |
79072805 LW |
3417 | for (; argc > 0 && **argv == '-'; argc--,argv++) { |
3418 | if (!argv[0][1]) | |
3419 | break; | |
379d538a | 3420 | if (argv[0][1] == '-' && !argv[0][2]) { |
79072805 LW |
3421 | argc--,argv++; |
3422 | break; | |
3423 | } | |
155aba94 | 3424 | if ((s = strchr(argv[0], '='))) { |
79072805 | 3425 | *s++ = '\0'; |
85e6fe83 | 3426 | sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s); |
79072805 LW |
3427 | } |
3428 | else | |
85e6fe83 | 3429 | sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1); |
fe14fcc3 | 3430 | } |
79072805 | 3431 | } |
3280af22 NIS |
3432 | PL_toptarget = NEWSV(0,0); |
3433 | sv_upgrade(PL_toptarget, SVt_PVFM); | |
3434 | sv_setpvn(PL_toptarget, "", 0); | |
3435 | PL_bodytarget = NEWSV(0,0); | |
3436 | sv_upgrade(PL_bodytarget, SVt_PVFM); | |
3437 | sv_setpvn(PL_bodytarget, "", 0); | |
3438 | PL_formtarget = PL_bodytarget; | |
79072805 | 3439 | |
bbce6d69 | 3440 | TAINT; |
155aba94 | 3441 | if ((tmpgv = gv_fetchpv("0",TRUE, SVt_PV))) { |
bf4acbe4 GS |
3442 | #ifdef MACOS_TRADITIONAL |
3443 | /* $0 is not majick on a Mac */ | |
3444 | sv_setpv(GvSV(tmpgv),MacPerl_MPWFileName(PL_origfilename)); | |
3445 | #else | |
3280af22 | 3446 | sv_setpv(GvSV(tmpgv),PL_origfilename); |
79072805 | 3447 | magicname("0", "0", 1); |
bf4acbe4 | 3448 | #endif |
79072805 | 3449 | } |
155aba94 | 3450 | if ((tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))) |
ed344e4f | 3451 | #ifdef OS2 |
23da6c43 | 3452 | sv_setpv(GvSV(tmpgv), os2_execname(aTHX)); |
ed344e4f | 3453 | #else |
3280af22 | 3454 | sv_setpv(GvSV(tmpgv),PL_origargv[0]); |
ed344e4f | 3455 | #endif |
155aba94 | 3456 | if ((PL_argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV))) { |
3280af22 NIS |
3457 | GvMULTI_on(PL_argvgv); |
3458 | (void)gv_AVadd(PL_argvgv); | |
3459 | av_clear(GvAVn(PL_argvgv)); | |
79072805 | 3460 | for (; argc > 0; argc--,argv++) { |
729a02f2 GS |
3461 | SV *sv = newSVpv(argv[0],0); |
3462 | av_push(GvAVn(PL_argvgv),sv); | |
3463 | if (PL_widesyscalls) | |
e84ff256 | 3464 | (void)sv_utf8_decode(sv); |
79072805 LW |
3465 | } |
3466 | } | |
155aba94 | 3467 | if ((PL_envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV))) { |
79072805 | 3468 | HV *hv; |
3280af22 NIS |
3469 | GvMULTI_on(PL_envgv); |
3470 | hv = GvHVn(PL_envgv); | |
14befaf4 | 3471 | hv_magic(hv, Nullgv, PERL_MAGIC_env); |
fa6a1c44 | 3472 | #ifdef USE_ENVIRON_ARRAY |
4633a7c4 LW |
3473 | /* Note that if the supplied env parameter is actually a copy |
3474 | of the global environ then it may now point to free'd memory | |
3475 | if the environment has been modified since. To avoid this | |
3476 | problem we treat env==NULL as meaning 'use the default' | |
3477 | */ | |
3478 | if (!env) | |
3479 | env = environ; | |
5aabfad6 | 3480 | if (env != environ) |
79072805 | 3481 | environ[0] = Nullch; |
103a7189 JH |
3482 | #ifdef NEED_ENVIRON_DUP_FOR_MODIFY |
3483 | { | |
3484 | char **env_base; | |
9041c2e3 | 3485 | for (env_base = env; *env; env++) |
103a7189 JH |
3486 | dup_env_count++; |
3487 | if ((dup_env_base = (char **) | |
480bfd1e | 3488 | safesysmalloc( sizeof(char *) * (dup_env_count+1) ))) { |
103a7189 JH |
3489 | char **dup_env; |
3490 | for (env = env_base, dup_env = dup_env_base; | |
3491 | *env; | |
480bfd1e JH |
3492 | env++, dup_env++) { |
3493 | /* With environ one needs to use safesysmalloc(). */ | |
3494 | *dup_env = safesysmalloc(strlen(*env) + 1); | |
3495 | (void)strcpy(*dup_env, *env); | |
3496 | } | |
103a7189 JH |
3497 | *dup_env = Nullch; |
3498 | env = dup_env_base; | |
3499 | } /* else what? */ | |
3500 | } | |
3501 | #endif /* NEED_ENVIRON_DUP_FOR_MODIFY */ | |
764df951 IZ |
3502 | if (env) |
3503 | for (; *env; env++) { | |
93a17b20 | 3504 | if (!(s = strchr(*env,'='))) |
79072805 LW |
3505 | continue; |
3506 | *s++ = '\0'; | |
60ce6247 | 3507 | #if defined(MSDOS) |
137443ea | 3508 | (void)strupr(*env); |
3509 | #endif | |
79072805 LW |
3510 | sv = newSVpv(s--,0); |
3511 | (void)hv_store(hv, *env, s - *env, sv, 0); | |
3512 | *s = '='; | |
764df951 | 3513 | } |
480bfd1e | 3514 | #ifdef NEED_ENVIRON_DUP_FOR_MODIFY |
103a7189 JH |
3515 | if (dup_env_base) { |
3516 | char **dup_env; | |
3517 | for (dup_env = dup_env_base; *dup_env; dup_env++) | |
480bfd1e JH |
3518 | safesysfree(*dup_env); |
3519 | safesysfree(dup_env_base); | |
103a7189 | 3520 | } |
7e0f6a1f | 3521 | #endif /* NEED_ENVIRON_DUP_FOR_MODIFY */ |
103a7189 | 3522 | #endif /* USE_ENVIRON_ARRAY */ |
79072805 | 3523 | } |
bbce6d69 | 3524 | TAINT_NOT; |
155aba94 | 3525 | if ((tmpgv = gv_fetchpv("$",TRUE, SVt_PV))) |
7766f137 | 3526 | sv_setiv(GvSV(tmpgv), (IV)PerlProc_getpid()); |
33b78306 | 3527 | } |
34de22dd | 3528 | |
76e3520e | 3529 | STATIC void |
cea2e8a9 | 3530 | S_init_perllib(pTHX) |
34de22dd | 3531 | { |
85e6fe83 | 3532 | char *s; |
3280af22 | 3533 | if (!PL_tainting) { |
552a7a9b | 3534 | #ifndef VMS |
76e3520e | 3535 | s = PerlEnv_getenv("PERL5LIB"); |
85e6fe83 | 3536 | if (s) |
9c8a64f0 | 3537 | incpush(s, TRUE, TRUE); |
85e6fe83 | 3538 | else |
9c8a64f0 | 3539 | incpush(PerlEnv_getenv("PERLLIB"), FALSE, FALSE); |
552a7a9b | 3540 | #else /* VMS */ |
3541 | /* Treat PERL5?LIB as a possible search list logical name -- the | |
3542 | * "natural" VMS idiom for a Unix path string. We allow each | |
3543 | * element to be a set of |-separated directories for compatibility. | |
3544 | */ | |
3545 | char buf[256]; | |
3546 | int idx = 0; | |
3547 | if (my_trnlnm("PERL5LIB",buf,0)) | |
9c8a64f0 | 3548 | do { incpush(buf,TRUE,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx)); |
552a7a9b | 3549 | else |
9c8a64f0 | 3550 | while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE,FALSE); |
552a7a9b | 3551 | #endif /* VMS */ |
85e6fe83 | 3552 | } |
34de22dd | 3553 | |
c90c0ff4 | 3554 | /* Use the ~-expanded versions of APPLLIB (undocumented), |
65f19062 | 3555 | ARCHLIB PRIVLIB SITEARCH SITELIB VENDORARCH and VENDORLIB |
df5cef82 | 3556 | */ |
4633a7c4 | 3557 | #ifdef APPLLIB_EXP |
9c8a64f0 | 3558 | incpush(APPLLIB_EXP, TRUE, TRUE); |
16d20bd9 | 3559 | #endif |
4633a7c4 | 3560 | |
fed7345c | 3561 | #ifdef ARCHLIB_EXP |
9c8a64f0 | 3562 | incpush(ARCHLIB_EXP, FALSE, FALSE); |
a0d0e21e | 3563 | #endif |
bf4acbe4 GS |
3564 | #ifdef MACOS_TRADITIONAL |
3565 | { | |
3566 | struct stat tmpstatbuf; | |
3567 | SV * privdir = NEWSV(55, 0); | |
3568 | char * macperl = PerlEnv_getenv("MACPERL"); | |
3569 | ||
3570 | if (!macperl) | |
3571 | macperl = ""; | |
3572 | ||
3573 | Perl_sv_setpvf(aTHX_ privdir, "%slib:", macperl); | |
3574 | if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode)) | |
3575 | incpush(SvPVX(privdir), TRUE, FALSE); | |
3576 | Perl_sv_setpvf(aTHX_ privdir, "%ssite_perl:", macperl); | |
3577 | if (PerlLIO_stat(SvPVX(privdir), &tmpstatbuf) >= 0 && S_ISDIR(tmpstatbuf.st_mode)) | |
3578 | incpush(SvPVX(privdir), TRUE, FALSE); | |
ac27b0f5 | 3579 | |
bf4acbe4 GS |
3580 | SvREFCNT_dec(privdir); |
3581 | } | |
3582 | if (!PL_tainting) | |
3583 | incpush(":", FALSE, FALSE); | |
3584 | #else | |
fed7345c | 3585 | #ifndef PRIVLIB_EXP |
65f19062 | 3586 | # define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl" |
34de22dd | 3587 | #endif |
ac27b0f5 | 3588 | #if defined(WIN32) |
9c8a64f0 | 3589 | incpush(PRIVLIB_EXP, TRUE, FALSE); |
00dc2f4f | 3590 | #else |
9c8a64f0 | 3591 | incpush(PRIVLIB_EXP, FALSE, FALSE); |
00dc2f4f | 3592 | #endif |
4633a7c4 | 3593 | |
65f19062 | 3594 | #ifdef SITEARCH_EXP |
3b290362 GS |
3595 | /* sitearch is always relative to sitelib on Windows for |
3596 | * DLL-based path intuition to work correctly */ | |
3597 | # if !defined(WIN32) | |
9c8a64f0 | 3598 | incpush(SITEARCH_EXP, FALSE, FALSE); |
65f19062 GS |
3599 | # endif |
3600 | #endif | |
3601 | ||
4633a7c4 | 3602 | #ifdef SITELIB_EXP |
65f19062 | 3603 | # if defined(WIN32) |
9c8a64f0 | 3604 | incpush(SITELIB_EXP, TRUE, FALSE); /* this picks up sitearch as well */ |
65f19062 | 3605 | # else |
9c8a64f0 | 3606 | incpush(SITELIB_EXP, FALSE, FALSE); |
65f19062 GS |
3607 | # endif |
3608 | #endif | |
189d1e8d | 3609 | |
65f19062 | 3610 | #ifdef SITELIB_STEM /* Search for version-specific dirs below here */ |
9c8a64f0 | 3611 | incpush(SITELIB_STEM, FALSE, TRUE); |
81c6dfba | 3612 | #endif |
65f19062 GS |
3613 | |
3614 | #ifdef PERL_VENDORARCH_EXP | |
4ea817c6 | 3615 | /* vendorarch is always relative to vendorlib on Windows for |
3b290362 GS |
3616 | * DLL-based path intuition to work correctly */ |
3617 | # if !defined(WIN32) | |
9c8a64f0 | 3618 | incpush(PERL_VENDORARCH_EXP, FALSE, FALSE); |
65f19062 | 3619 | # endif |
4b03c463 | 3620 | #endif |
65f19062 GS |
3621 | |
3622 | #ifdef PERL_VENDORLIB_EXP | |
3623 | # if defined(WIN32) | |
9c8a64f0 | 3624 | incpush(PERL_VENDORLIB_EXP, TRUE, FALSE); /* this picks up vendorarch as well */ |
65f19062 | 3625 | # else |
9c8a64f0 | 3626 | incpush(PERL_VENDORLIB_EXP, FALSE, FALSE); |
65f19062 | 3627 | # endif |
a3635516 | 3628 | #endif |
65f19062 GS |
3629 | |
3630 | #ifdef PERL_VENDORLIB_STEM /* Search for version-specific dirs below here */ | |
9c8a64f0 | 3631 | incpush(PERL_VENDORLIB_STEM, FALSE, TRUE); |
00dc2f4f | 3632 | #endif |
65f19062 | 3633 | |
3b777bb4 GS |
3634 | #ifdef PERL_OTHERLIBDIRS |
3635 | incpush(PERL_OTHERLIBDIRS, TRUE, TRUE); | |
3636 | #endif | |
3637 | ||
3280af22 | 3638 | if (!PL_tainting) |
9c8a64f0 | 3639 | incpush(".", FALSE, FALSE); |
bf4acbe4 | 3640 | #endif /* MACOS_TRADITIONAL */ |
774d564b | 3641 | } |
3642 | ||
ed79a026 | 3643 | #if defined(DOSISH) || defined(EPOC) |
774d564b | 3644 | # define PERLLIB_SEP ';' |
3645 | #else | |
3646 | # if defined(VMS) | |
3647 | # define PERLLIB_SEP '|' | |
3648 | # else | |
bf4acbe4 GS |
3649 | # if defined(MACOS_TRADITIONAL) |
3650 | # define PERLLIB_SEP ',' | |
3651 | # else | |
3652 | # define PERLLIB_SEP ':' | |
3653 | # endif | |
774d564b | 3654 | # endif |
3655 | #endif | |
3656 | #ifndef PERLLIB_MANGLE | |
3657 | # define PERLLIB_MANGLE(s,n) (s) | |
ac27b0f5 | 3658 | #endif |
774d564b | 3659 | |
76e3520e | 3660 | STATIC void |
9c8a64f0 | 3661 | S_incpush(pTHX_ char *p, int addsubdirs, int addoldvers) |
774d564b | 3662 | { |
3663 | SV *subdir = Nullsv; | |
774d564b | 3664 | |
3b290362 | 3665 | if (!p || !*p) |
774d564b | 3666 | return; |
3667 | ||
9c8a64f0 | 3668 | if (addsubdirs || addoldvers) { |
00db4c45 | 3669 | subdir = sv_newmortal(); |
774d564b | 3670 | } |
3671 | ||
3672 | /* Break at all separators */ | |
3673 | while (p && *p) { | |
8c52afec | 3674 | SV *libdir = NEWSV(55,0); |
774d564b | 3675 | char *s; |
3676 | ||
3677 | /* skip any consecutive separators */ | |
3678 | while ( *p == PERLLIB_SEP ) { | |
3679 | /* Uncomment the next line for PATH semantics */ | |
79cb57f6 | 3680 | /* av_push(GvAVn(PL_incgv), newSVpvn(".", 1)); */ |
774d564b | 3681 | p++; |
3682 | } | |
3683 | ||
3684 | if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) { | |
3685 | sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)), | |
3686 | (STRLEN)(s - p)); | |
3687 | p = s + 1; | |
3688 | } | |
3689 | else { | |
3690 | sv_setpv(libdir, PERLLIB_MANGLE(p, 0)); | |
3691 | p = Nullch; /* break out */ | |
3692 | } | |
bf4acbe4 GS |
3693 | #ifdef MACOS_TRADITIONAL |
3694 | if (!strchr(SvPVX(libdir), ':')) | |
3695 | sv_insert(libdir, 0, 0, ":", 1); | |
3696 | if (SvPVX(libdir)[SvCUR(libdir)-1] != ':') | |
3697 | sv_catpv(libdir, ":"); | |
3698 | #endif | |
774d564b | 3699 | |
3700 | /* | |
3701 | * BEFORE pushing libdir onto @INC we may first push version- and | |
3702 | * archname-specific sub-directories. | |
3703 | */ | |
9c8a64f0 | 3704 | if (addsubdirs || addoldvers) { |
29d82f8d | 3705 | #ifdef PERL_INC_VERSION_LIST |
8353b874 GS |
3706 | /* Configure terminates PERL_INC_VERSION_LIST with a NULL */ |
3707 | const char *incverlist[] = { PERL_INC_VERSION_LIST }; | |
29d82f8d GS |
3708 | const char **incver; |
3709 | #endif | |
774d564b | 3710 | struct stat tmpstatbuf; |
aa689395 | 3711 | #ifdef VMS |
3712 | char *unix; | |
3713 | STRLEN len; | |
774d564b | 3714 | |
2d8e6c8d | 3715 | if ((unix = tounixspec_ts(SvPV(libdir,len),Nullch)) != Nullch) { |
aa689395 | 3716 | len = strlen(unix); |
3717 | while (unix[len-1] == '/') len--; /* Cosmetic */ | |
3718 | sv_usepvn(libdir,unix,len); | |
3719 | } | |
3720 | else | |
bf49b057 | 3721 | PerlIO_printf(Perl_error_log, |
aa689395 | 3722 | "Failed to unixify @INC element \"%s\"\n", |
2d8e6c8d | 3723 | SvPV(libdir,len)); |
aa689395 | 3724 | #endif |
9c8a64f0 | 3725 | if (addsubdirs) { |
bf4acbe4 GS |
3726 | #ifdef MACOS_TRADITIONAL |
3727 | #define PERL_AV_SUFFIX_FMT "" | |
084592ab CN |
3728 | #define PERL_ARCH_FMT "%s:" |
3729 | #define PERL_ARCH_FMT_PATH PERL_FS_VER_FMT PERL_AV_SUFFIX_FMT | |
bf4acbe4 GS |
3730 | #else |
3731 | #define PERL_AV_SUFFIX_FMT "/" | |
3732 | #define PERL_ARCH_FMT "/%s" | |
084592ab | 3733 | #define PERL_ARCH_FMT_PATH PERL_AV_SUFFIX_FMT PERL_FS_VER_FMT |
bf4acbe4 | 3734 | #endif |
9c8a64f0 | 3735 | /* .../version/archname if -d .../version/archname */ |
084592ab | 3736 | Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH PERL_ARCH_FMT, |
9c8a64f0 GS |
3737 | libdir, |
3738 | (int)PERL_REVISION, (int)PERL_VERSION, | |
3739 | (int)PERL_SUBVERSION, ARCHNAME); | |
3740 | if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 && | |
3741 | S_ISDIR(tmpstatbuf.st_mode)) | |
3742 | av_push(GvAVn(PL_incgv), newSVsv(subdir)); | |
4b03c463 | 3743 | |
9c8a64f0 | 3744 | /* .../version if -d .../version */ |
084592ab | 3745 | Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT_PATH, libdir, |
9c8a64f0 GS |
3746 | (int)PERL_REVISION, (int)PERL_VERSION, |
3747 | (int)PERL_SUBVERSION); | |
3748 | if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 && | |
3749 | S_ISDIR(tmpstatbuf.st_mode)) | |
3750 | av_push(GvAVn(PL_incgv), newSVsv(subdir)); | |
3751 | ||
3752 | /* .../archname if -d .../archname */ | |
bf4acbe4 | 3753 | Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, ARCHNAME); |
29d82f8d GS |
3754 | if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 && |
3755 | S_ISDIR(tmpstatbuf.st_mode)) | |
3756 | av_push(GvAVn(PL_incgv), newSVsv(subdir)); | |
29d82f8d | 3757 | } |
9c8a64f0 | 3758 | |
9c8a64f0 | 3759 | #ifdef PERL_INC_VERSION_LIST |
ccc2aad8 | 3760 | if (addoldvers) { |
9c8a64f0 GS |
3761 | for (incver = incverlist; *incver; incver++) { |
3762 | /* .../xxx if -d .../xxx */ | |
bf4acbe4 | 3763 | Perl_sv_setpvf(aTHX_ subdir, "%"SVf PERL_ARCH_FMT, libdir, *incver); |
9c8a64f0 GS |
3764 | if (PerlLIO_stat(SvPVX(subdir), &tmpstatbuf) >= 0 && |
3765 | S_ISDIR(tmpstatbuf.st_mode)) | |
3766 | av_push(GvAVn(PL_incgv), newSVsv(subdir)); | |
3767 | } | |
3768 | } | |
29d82f8d | 3769 | #endif |
774d564b | 3770 | } |
3771 | ||
3772 | /* finally push this lib directory on the end of @INC */ | |
3280af22 | 3773 | av_push(GvAVn(PL_incgv), libdir); |
774d564b | 3774 | } |
34de22dd | 3775 | } |
93a17b20 | 3776 | |
199100c8 | 3777 | #ifdef USE_THREADS |
76e3520e | 3778 | STATIC struct perl_thread * |
cea2e8a9 | 3779 | S_init_main_thread(pTHX) |
199100c8 | 3780 | { |
c5be433b | 3781 | #if !defined(PERL_IMPLICIT_CONTEXT) |
52e1cb5e | 3782 | struct perl_thread *thr; |
cea2e8a9 | 3783 | #endif |
199100c8 MB |
3784 | XPV *xpv; |
3785 | ||
52e1cb5e | 3786 | Newz(53, thr, 1, struct perl_thread); |
533c011a | 3787 | PL_curcop = &PL_compiling; |
c5be433b | 3788 | thr->interp = PERL_GET_INTERP; |
199100c8 | 3789 | thr->cvcache = newHV(); |
54b9620d | 3790 | thr->threadsv = newAV(); |
940cb80d | 3791 | /* thr->threadsvp is set when find_threadsv is called */ |
199100c8 MB |
3792 | thr->specific = newAV(); |
3793 | thr->flags = THRf_R_JOINABLE; | |
3794 | MUTEX_INIT(&thr->mutex); | |
3795 | /* Handcraft thrsv similarly to mess_sv */ | |
533c011a | 3796 | New(53, PL_thrsv, 1, SV); |
199100c8 | 3797 | Newz(53, xpv, 1, XPV); |
533c011a NIS |
3798 | SvFLAGS(PL_thrsv) = SVt_PV; |
3799 | SvANY(PL_thrsv) = (void*)xpv; | |
3800 | SvREFCNT(PL_thrsv) = 1 << 30; /* practically infinite */ | |
3801 | SvPVX(PL_thrsv) = (char*)thr; | |
3802 | SvCUR_set(PL_thrsv, sizeof(thr)); | |
3803 | SvLEN_set(PL_thrsv, sizeof(thr)); | |
3804 | *SvEND(PL_thrsv) = '\0'; /* in the trailing_nul field */ | |
3805 | thr->oursv = PL_thrsv; | |
3806 | PL_chopset = " \n-"; | |
3967c732 | 3807 | PL_dumpindent = 4; |
533c011a NIS |
3808 | |
3809 | MUTEX_LOCK(&PL_threads_mutex); | |
3810 | PL_nthreads++; | |
199100c8 MB |
3811 | thr->tid = 0; |
3812 | thr->next = thr; | |
3813 | thr->prev = thr; | |
8dcd6f7b | 3814 | thr->thr_done = 0; |
533c011a | 3815 | MUTEX_UNLOCK(&PL_threads_mutex); |
199100c8 | 3816 | |
4b026b9e | 3817 | #ifdef HAVE_THREAD_INTERN |
4f63d024 | 3818 | Perl_init_thread_intern(thr); |
235db74f GS |
3819 | #endif |
3820 | ||
3821 | #ifdef SET_THREAD_SELF | |
3822 | SET_THREAD_SELF(thr); | |
199100c8 MB |
3823 | #else |
3824 | thr->self = pthread_self(); | |
235db74f | 3825 | #endif /* SET_THREAD_SELF */ |
06d86050 | 3826 | PERL_SET_THX(thr); |
199100c8 MB |
3827 | |
3828 | /* | |
411caa50 JH |
3829 | * These must come after the thread self setting |
3830 | * because sv_setpvn does SvTAINT and the taint | |
3831 | * fields thread selfness being set. | |
199100c8 | 3832 | */ |
533c011a NIS |
3833 | PL_toptarget = NEWSV(0,0); |
3834 | sv_upgrade(PL_toptarget, SVt_PVFM); | |
3835 | sv_setpvn(PL_toptarget, "", 0); | |
3836 | PL_bodytarget = NEWSV(0,0); | |
3837 | sv_upgrade(PL_bodytarget, SVt_PVFM); | |
3838 | sv_setpvn(PL_bodytarget, "", 0); | |
3839 | PL_formtarget = PL_bodytarget; | |
79cb57f6 | 3840 | thr->errsv = newSVpvn("", 0); |
78857c3c | 3841 | (void) find_threadsv("@"); /* Ensure $@ is initialised early */ |
5c0ca799 | 3842 | |
533c011a | 3843 | PL_maxscream = -1; |
a2efc822 | 3844 | PL_peepp = MEMBER_TO_FPTR(Perl_peep); |
0b94c7bb GS |
3845 | PL_regcompp = MEMBER_TO_FPTR(Perl_pregcomp); |
3846 | PL_regexecp = MEMBER_TO_FPTR(Perl_regexec_flags); | |
3847 | PL_regint_start = MEMBER_TO_FPTR(Perl_re_intuit_start); | |
3848 | PL_regint_string = MEMBER_TO_FPTR(Perl_re_intuit_string); | |
3849 | PL_regfree = MEMBER_TO_FPTR(Perl_pregfree); | |
533c011a NIS |
3850 | PL_regindent = 0; |
3851 | PL_reginterp_cnt = 0; | |
5c0ca799 | 3852 | |
199100c8 MB |
3853 | return thr; |
3854 | } | |
3855 | #endif /* USE_THREADS */ | |
3856 | ||
93a17b20 | 3857 | void |
864dbfa3 | 3858 | Perl_call_list(pTHX_ I32 oldscope, AV *paramList) |
93a17b20 | 3859 | { |
971a9dd3 | 3860 | SV *atsv; |
57843af0 | 3861 | line_t oldline = CopLINE(PL_curcop); |
312caa8e | 3862 | CV *cv; |
22921e25 | 3863 | STRLEN len; |
6224f72b | 3864 | int ret; |
db36c5a1 | 3865 | dJMPENV; |
93a17b20 | 3866 | |
76e3520e | 3867 | while (AvFILL(paramList) >= 0) { |
312caa8e | 3868 | cv = (CV*)av_shift(paramList); |
aefff11f | 3869 | if (PL_savebegin && (paramList == PL_beginav)) { |
059a8bb7 JH |
3870 | /* save PL_beginav for compiler */ |
3871 | if (! PL_beginav_save) | |
3872 | PL_beginav_save = newAV(); | |
3873 | av_push(PL_beginav_save, (SV*)cv); | |
3874 | } else { | |
3875 | SAVEFREESV(cv); | |
3876 | } | |
14dd3ad8 GS |
3877 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
3878 | CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vcall_list_body), cv); | |
3879 | #else | |
3880 | JMPENV_PUSH(ret); | |
3881 | #endif | |
6224f72b | 3882 | switch (ret) { |
312caa8e | 3883 | case 0: |
14dd3ad8 GS |
3884 | #ifndef PERL_FLEXIBLE_EXCEPTIONS |
3885 | call_list_body(cv); | |
3886 | #endif | |
971a9dd3 | 3887 | atsv = ERRSV; |
312caa8e CS |
3888 | (void)SvPV(atsv, len); |
3889 | if (len) { | |
971a9dd3 | 3890 | STRLEN n_a; |
312caa8e | 3891 | PL_curcop = &PL_compiling; |
57843af0 | 3892 | CopLINE_set(PL_curcop, oldline); |
312caa8e CS |
3893 | if (paramList == PL_beginav) |
3894 | sv_catpv(atsv, "BEGIN failed--compilation aborted"); | |
3895 | else | |
4f25aa18 GS |
3896 | Perl_sv_catpvf(aTHX_ atsv, |
3897 | "%s failed--call queue aborted", | |
7d30b5c4 | 3898 | paramList == PL_checkav ? "CHECK" |
4f25aa18 GS |
3899 | : paramList == PL_initav ? "INIT" |
3900 | : "END"); | |
312caa8e CS |
3901 | while (PL_scopestack_ix > oldscope) |
3902 | LEAVE; | |
14dd3ad8 | 3903 | JMPENV_POP; |
971a9dd3 | 3904 | Perl_croak(aTHX_ "%s", SvPVx(atsv, n_a)); |
a0d0e21e | 3905 | } |
85e6fe83 | 3906 | break; |
6224f72b | 3907 | case 1: |
f86702cc | 3908 | STATUS_ALL_FAILURE; |
85e6fe83 | 3909 | /* FALL THROUGH */ |
6224f72b | 3910 | case 2: |
85e6fe83 | 3911 | /* my_exit() was called */ |
3280af22 | 3912 | while (PL_scopestack_ix > oldscope) |
2ae324a7 | 3913 | LEAVE; |
84902520 | 3914 | FREETMPS; |
3280af22 | 3915 | PL_curstash = PL_defstash; |
3280af22 | 3916 | PL_curcop = &PL_compiling; |
57843af0 | 3917 | CopLINE_set(PL_curcop, oldline); |
14dd3ad8 | 3918 | JMPENV_POP; |
cc3604b1 | 3919 | if (PL_statusvalue && !(PL_exit_flags & PERL_EXIT_EXPECTED)) { |
3280af22 | 3920 | if (paramList == PL_beginav) |
cea2e8a9 | 3921 | Perl_croak(aTHX_ "BEGIN failed--compilation aborted"); |
85e6fe83 | 3922 | else |
4f25aa18 | 3923 | Perl_croak(aTHX_ "%s failed--call queue aborted", |
7d30b5c4 | 3924 | paramList == PL_checkav ? "CHECK" |
4f25aa18 GS |
3925 | : paramList == PL_initav ? "INIT" |
3926 | : "END"); | |
85e6fe83 | 3927 | } |
f86702cc | 3928 | my_exit_jump(); |
85e6fe83 | 3929 | /* NOTREACHED */ |
6224f72b | 3930 | case 3: |
312caa8e CS |
3931 | if (PL_restartop) { |
3932 | PL_curcop = &PL_compiling; | |
57843af0 | 3933 | CopLINE_set(PL_curcop, oldline); |
312caa8e | 3934 | JMPENV_JUMP(3); |
85e6fe83 | 3935 | } |
bf49b057 | 3936 | PerlIO_printf(Perl_error_log, "panic: restartop\n"); |
312caa8e CS |
3937 | FREETMPS; |
3938 | break; | |
8990e307 | 3939 | } |
14dd3ad8 | 3940 | JMPENV_POP; |
93a17b20 | 3941 | } |
93a17b20 | 3942 | } |
93a17b20 | 3943 | |
14dd3ad8 | 3944 | #ifdef PERL_FLEXIBLE_EXCEPTIONS |
312caa8e | 3945 | STATIC void * |
14dd3ad8 | 3946 | S_vcall_list_body(pTHX_ va_list args) |
312caa8e | 3947 | { |
312caa8e | 3948 | CV *cv = va_arg(args, CV*); |
14dd3ad8 GS |
3949 | return call_list_body(cv); |
3950 | } | |
3951 | #endif | |
312caa8e | 3952 | |
14dd3ad8 GS |
3953 | STATIC void * |
3954 | S_call_list_body(pTHX_ CV *cv) | |
3955 | { | |
312caa8e | 3956 | PUSHMARK(PL_stack_sp); |
864dbfa3 | 3957 | call_sv((SV*)cv, G_EVAL|G_DISCARD); |
312caa8e CS |
3958 | return NULL; |
3959 | } | |
3960 | ||
f86702cc | 3961 | void |
864dbfa3 | 3962 | Perl_my_exit(pTHX_ U32 status) |
f86702cc | 3963 | { |
8b73bbec | 3964 | DEBUG_S(PerlIO_printf(Perl_debug_log, "my_exit: thread %p, status %lu\n", |
a863c7d1 | 3965 | thr, (unsigned long) status)); |
f86702cc | 3966 | switch (status) { |
3967 | case 0: | |
3968 | STATUS_ALL_SUCCESS; | |
3969 | break; | |
3970 | case 1: | |
3971 | STATUS_ALL_FAILURE; | |
3972 | break; | |
3973 | default: | |
3974 | STATUS_NATIVE_SET(status); | |
3975 | break; | |
3976 | } | |
3977 | my_exit_jump(); | |
3978 | } | |
3979 | ||
3980 | void | |
864dbfa3 | 3981 | Perl_my_failure_exit(pTHX) |
f86702cc | 3982 | { |
3983 | #ifdef VMS | |
3984 | if (vaxc$errno & 1) { | |
4fdae800 | 3985 | if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */ |
3986 | STATUS_NATIVE_SET(44); | |
f86702cc | 3987 | } |
3988 | else { | |
ff0cee69 | 3989 | if (!vaxc$errno && errno) /* unlikely */ |
4fdae800 | 3990 | STATUS_NATIVE_SET(44); |
f86702cc | 3991 | else |
4fdae800 | 3992 | STATUS_NATIVE_SET(vaxc$errno); |
f86702cc | 3993 | } |
3994 | #else | |
9b599b2a | 3995 | int exitstatus; |
f86702cc | 3996 | if (errno & 255) |
3997 | STATUS_POSIX_SET(errno); | |
9b599b2a | 3998 | else { |
ac27b0f5 | 3999 | exitstatus = STATUS_POSIX >> 8; |
9b599b2a GS |
4000 | if (exitstatus & 255) |
4001 | STATUS_POSIX_SET(exitstatus); | |
4002 | else | |
4003 | STATUS_POSIX_SET(255); | |
4004 | } | |
f86702cc | 4005 | #endif |
4006 | my_exit_jump(); | |
93a17b20 LW |
4007 | } |
4008 | ||
76e3520e | 4009 | STATIC void |
cea2e8a9 | 4010 | S_my_exit_jump(pTHX) |
f86702cc | 4011 | { |
c09156bb | 4012 | register PERL_CONTEXT *cx; |
f86702cc | 4013 | I32 gimme; |
4014 | SV **newsp; | |
4015 | ||
3280af22 NIS |
4016 | if (PL_e_script) { |
4017 | SvREFCNT_dec(PL_e_script); | |
4018 | PL_e_script = Nullsv; | |
f86702cc | 4019 | } |
4020 | ||
3280af22 | 4021 | POPSTACK_TO(PL_mainstack); |
f86702cc | 4022 | if (cxstack_ix >= 0) { |
4023 | if (cxstack_ix > 0) | |
4024 | dounwind(0); | |
3280af22 | 4025 | POPBLOCK(cx,PL_curpm); |
f86702cc | 4026 | LEAVE; |
4027 | } | |
ff0cee69 | 4028 | |
6224f72b | 4029 | JMPENV_JUMP(2); |
f86702cc | 4030 | } |
873ef191 | 4031 | |
7a5f8e82 | 4032 | #ifdef PERL_OBJECT |
873ef191 | 4033 | #include "XSUB.h" |
51371543 | 4034 | #endif |
873ef191 | 4035 | |
0cb96387 GS |
4036 | static I32 |
4037 | read_e_script(pTHXo_ int idx, SV *buf_sv, int maxlen) | |
873ef191 GS |
4038 | { |
4039 | char *p, *nl; | |
3280af22 | 4040 | p = SvPVX(PL_e_script); |
873ef191 | 4041 | nl = strchr(p, '\n'); |
3280af22 | 4042 | nl = (nl) ? nl+1 : SvEND(PL_e_script); |
7dfe3f66 | 4043 | if (nl-p == 0) { |
0cb96387 | 4044 | filter_del(read_e_script); |
873ef191 | 4045 | return 0; |
7dfe3f66 | 4046 | } |
873ef191 | 4047 | sv_catpvn(buf_sv, p, nl-p); |
3280af22 | 4048 | sv_chop(PL_e_script, nl); |
873ef191 GS |
4049 | return 1; |
4050 | } |