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