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