This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tiehandle stuff in change 595 didn't add to MANIFEST
[perl5.git] / perl.c
CommitLineData
a0d0e21e
LW
1/* perl.c
2 *
a411490c 3 * Copyright (c) 1987-1998 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
LW
14#include "EXTERN.h"
15#include "perl.h"
a687059c 16#include "patchlevel.h"
378cc40b 17
df5cef82 18/* XXX If this causes problems, set i_unistd=undef in the hint file. */
a0d0e21e
LW
19#ifdef I_UNISTD
20#include <unistd.h>
21#endif
a0d0e21e 22
54310121 23#if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE)
24char *getenv _((char *)); /* Usually in <stdlib.h> */
25#endif
26
71be2cbc 27dEXTCONST char rcsid[] = "perl.c\nPatch level: ###\n";
463ee0b2 28
a687059c
LW
29#ifdef IAMSUID
30#ifndef DOSUID
31#define DOSUID
32#endif
33#endif
378cc40b 34
a687059c
LW
35#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
36#ifdef DOSUID
37#undef DOSUID
38#endif
39#endif
8d063cd8 40
8ebc5c01 41#define I_REINIT \
42 STMT_START { \
43 chopset = " \n-"; \
44 copline = NOLINE; \
45 curcop = &compiling; \
46 curcopdb = NULL; \
47 cxstack_ix = -1; \
48 cxstack_max = 128; \
49 dbargs = 0; \
50 dlmax = 128; \
51 laststatval = -1; \
52 laststype = OP_STAT; \
53 maxscream = -1; \
54 maxsysfd = MAXSYSFD; \
55 statname = Nullsv; \
56 tmps_floor = -1; \
57 tmps_ix = -1; \
58 op_mask = NULL; \
59 dlmax = 128; \
60 laststatval = -1; \
61 laststype = OP_STAT; \
46fc3d4c 62 mess_sv = Nullsv; \
8ebc5c01 63 } STMT_END
64
a0d0e21e 65static void find_beginning _((void));
bbce6d69 66static void forbid_setid _((char *));
774d564b 67static void incpush _((char *, int));
748a9306 68static void init_ids _((void));
a0d0e21e
LW
69static void init_debugger _((void));
70static void init_lexer _((void));
71static void init_main_stash _((void));
199100c8 72#ifdef USE_THREADS
52e1cb5e 73static struct perl_thread * init_main_thread _((void));
199100c8 74#endif /* USE_THREADS */
a0d0e21e
LW
75static void init_perllib _((void));
76static void init_postdump_symbols _((int, char **, char **));
77static void init_predump_symbols _((void));
f86702cc 78static void my_exit_jump _((void)) __attribute__((noreturn));
6e72f9df 79static void nuke_stacks _((void));
a0d0e21e 80static void open_script _((char *, bool, SV *));
ab821d7f 81static void usage _((char *));
96436eeb 82static void validate_suid _((char *, char*));
83
84static int fdscript = -1;
79072805 85
93a17b20 86PerlInterpreter *
8ac85365 87perl_alloc(void)
79072805 88{
93a17b20 89 PerlInterpreter *sv_interp;
79072805 90
8990e307 91 curinterp = 0;
93a17b20 92 New(53, sv_interp, 1, PerlInterpreter);
79072805
LW
93 return sv_interp;
94}
95
96void
8ac85365 97perl_construct(register PerlInterpreter *sv_interp)
79072805 98{
a863c7d1
MB
99#ifdef USE_THREADS
100 int i;
101#ifndef FAKE_THREADS
52e1cb5e 102 struct perl_thread *thr;
a863c7d1
MB
103#endif /* FAKE_THREADS */
104#endif /* USE_THREADS */
11343788 105
79072805
LW
106 if (!(curinterp = sv_interp))
107 return;
108
8990e307 109#ifdef MULTIPLICITY
93a17b20 110 Zero(sv_interp, 1, PerlInterpreter);
8990e307 111#endif
79072805 112
33f46ff6 113 /* Init the real globals (and main thread)? */
c116a00c 114 if (!linestr) {
11343788 115#ifdef USE_THREADS
8023c3ce 116
33f46ff6 117 INIT_THREADS;
d55594ae
GS
118#ifdef ALLOC_THREAD_KEY
119 ALLOC_THREAD_KEY;
120#else
a863c7d1
MB
121 if (pthread_key_create(&thr_key, 0))
122 croak("panic: pthread_key_create");
d55594ae 123#endif
c116a00c 124 MUTEX_INIT(&sv_mutex);
a863c7d1
MB
125 /*
126 * Safe to use basic SV functions from now on (though
127 * not things like mortals or tainting yet).
128 */
c116a00c
MB
129 MUTEX_INIT(&eval_mutex);
130 COND_INIT(&eval_cond);
33f46ff6 131 MUTEX_INIT(&threads_mutex);
c116a00c 132 COND_INIT(&nthreads_cond);
a863c7d1 133
199100c8 134 thr = init_main_thread();
11343788
MB
135#endif /* USE_THREADS */
136
79072805 137 linestr = NEWSV(65,80);
ed6116ce 138 sv_upgrade(linestr,SVt_PVIV);
79072805 139
6e72f9df 140 if (!SvREADONLY(&sv_undef)) {
141 SvREADONLY_on(&sv_undef);
79072805 142
6e72f9df 143 sv_setpv(&sv_no,No);
144 SvNV(&sv_no);
145 SvREADONLY_on(&sv_no);
79072805 146
6e72f9df 147 sv_setpv(&sv_yes,Yes);
148 SvNV(&sv_yes);
149 SvREADONLY_on(&sv_yes);
150 }
79072805 151
c07a80fd 152 nrs = newSVpv("\n", 1);
153 rs = SvREFCNT_inc(nrs);
154
c23142e2 155 sighandlerp = sighandler;
44a8e56a 156 pidstatus = newHV();
157
79072805
LW
158#ifdef MSDOS
159 /*
160 * There is no way we can refer to them from Perl so close them to save
161 * space. The other alternative would be to provide STDAUX and STDPRN
162 * filehandles.
163 */
164 (void)fclose(stdaux);
165 (void)fclose(stdprn);
166#endif
167 }
168
8990e307 169#ifdef MULTIPLICITY
8ebc5c01 170 I_REINIT;
171 perl_destruct_level = 1;
172#else
173 if(perl_destruct_level > 0)
174 I_REINIT;
79072805
LW
175#endif
176
748a9306 177 init_ids();
fb73857a 178 lex_state = LEX_NOTPARSING;
a5f75d66 179
54310121 180 start_env.je_prev = NULL;
181 start_env.je_ret = -1;
182 start_env.je_mustcatch = TRUE;
183 top_env = &start_env;
f86702cc 184 STATUS_ALL_SUCCESS;
185
36477c24 186 SET_NUMERIC_STANDARD();
a5f75d66 187#if defined(SUBVERSION) && SUBVERSION > 0
e2666263 188 sprintf(patchlevel, "%7.5f", (double) 5
189 + ((double) PATCHLEVEL / (double) 1000)
190 + ((double) SUBVERSION / (double) 100000));
a5f75d66 191#else
e2666263 192 sprintf(patchlevel, "%5.3f", (double) 5 +
193 ((double) PATCHLEVEL / (double) 1000));
a5f75d66 194#endif
79072805 195
ab821d7f 196#if defined(LOCAL_PATCH_COUNT)
6e72f9df 197 localpatches = local_patches; /* For possible -v */
ab821d7f 198#endif
199
760ac839
LW
200 PerlIO_init(); /* Hook to IO system */
201
79072805 202 fdpid = newAV(); /* for remembering popen pids by fd */
8990e307 203
11343788
MB
204 init_stacks(ARGS);
205 DEBUG( {
206 New(51,debname,128,char);
207 New(52,debdelim,128,char);
208 } )
209
8990e307 210 ENTER;
79072805
LW
211}
212
213void
8ac85365 214perl_destruct(register PerlInterpreter *sv_interp)
79072805 215{
11343788 216 dTHR;
748a9306 217 int destruct_level; /* 0=none, 1=full, 2=full with checks */
8990e307 218 I32 last_sv_count;
a0d0e21e 219 HV *hv;
1f2bfc8a 220#ifdef USE_THREADS
33f46ff6 221 Thread t;
1f2bfc8a 222#endif /* USE_THREADS */
8990e307 223
79072805
LW
224 if (!(curinterp = sv_interp))
225 return;
748a9306 226
11343788 227#ifdef USE_THREADS
0f15f207 228#ifndef FAKE_THREADS
8023c3ce
MB
229 /* Pass 1 on any remaining threads: detach joinables, join zombies */
230 retry_cleanup:
33f46ff6
MB
231 MUTEX_LOCK(&threads_mutex);
232 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
c7848ba1 233 "perl_destruct: waiting for %d threads...\n",
33f46ff6
MB
234 nthreads - 1));
235 for (t = thr->next; t != thr; t = t->next) {
605e5515
MB
236 MUTEX_LOCK(&t->mutex);
237 switch (ThrSTATE(t)) {
238 AV *av;
c7848ba1
MB
239 case THRf_ZOMBIE:
240 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
241 "perl_destruct: joining zombie %p\n", t));
605e5515
MB
242 ThrSETSTATE(t, THRf_DEAD);
243 MUTEX_UNLOCK(&t->mutex);
244 nthreads--;
8023c3ce
MB
245 /*
246 * The SvREFCNT_dec below may take a long time (e.g. av
247 * may contain an object scalar whose destructor gets
248 * called) so we have to unlock threads_mutex and start
249 * all over again.
250 */
605e5515 251 MUTEX_UNLOCK(&threads_mutex);
ea0efc06 252 JOIN(t, &av);
605e5515 253 SvREFCNT_dec((SV*)av);
c7848ba1
MB
254 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
255 "perl_destruct: joined zombie %p OK\n", t));
8023c3ce 256 goto retry_cleanup;
c7848ba1
MB
257 case THRf_R_JOINABLE:
258 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
259 "perl_destruct: detaching thread %p\n", t));
260 ThrSETSTATE(t, THRf_R_DETACHED);
261 /*
262 * We unlock threads_mutex and t->mutex in the opposite order
263 * from which we locked them just so that DETACH won't
264 * deadlock if it panics. It's only a breach of good style
265 * not a bug since they are unlocks not locks.
266 */
267 MUTEX_UNLOCK(&threads_mutex);
268 DETACH(t);
269 MUTEX_UNLOCK(&t->mutex);
8023c3ce 270 goto retry_cleanup;
c7848ba1
MB
271 default:
272 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
273 "perl_destruct: ignoring %p (state %u)\n",
274 t, ThrSTATE(t)));
275 MUTEX_UNLOCK(&t->mutex);
c7848ba1 276 /* fall through and out */
33f46ff6
MB
277 }
278 }
8023c3ce
MB
279 /* We leave the above "Pass 1" loop with threads_mutex still locked */
280
281 /* Pass 2 on remaining threads: wait for the thread count to drop to one */
11343788
MB
282 while (nthreads > 1)
283 {
33f46ff6 284 DEBUG_L(PerlIO_printf(PerlIO_stderr(),
c7848ba1 285 "perl_destruct: final wait for %d threads\n",
33f46ff6
MB
286 nthreads - 1));
287 COND_WAIT(&nthreads_cond, &threads_mutex);
11343788
MB
288 }
289 /* At this point, we're the last thread */
33f46ff6 290 MUTEX_UNLOCK(&threads_mutex);
d9f997d7 291 DEBUG_L(PerlIO_printf(PerlIO_stderr(), "perl_destruct: armageddon has arrived\n"));
33f46ff6 292 MUTEX_DESTROY(&threads_mutex);
11343788 293 COND_DESTROY(&nthreads_cond);
0f15f207 294#endif /* !defined(FAKE_THREADS) */
11343788
MB
295#endif /* USE_THREADS */
296
748a9306 297 destruct_level = perl_destruct_level;
4633a7c4
LW
298#ifdef DEBUGGING
299 {
300 char *s;
5fd9e9a4 301 if (s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL")) {
5f05dabc 302 int i = atoi(s);
303 if (destruct_level < i)
304 destruct_level = i;
305 }
4633a7c4
LW
306 }
307#endif
308
8990e307 309 LEAVE;
a0d0e21e
LW
310 FREETMPS;
311
ff0cee69 312 /* We must account for everything. */
313
314 /* Destroy the main CV and syntax tree */
6e72f9df 315 if (main_root) {
316 curpad = AvARRAY(comppad);
317 op_free(main_root);
ff0cee69 318 main_root = Nullop;
a0d0e21e 319 }
ff0cee69 320 main_start = Nullop;
321 SvREFCNT_dec(main_cv);
322 main_cv = Nullcv;
323
a0d0e21e
LW
324 if (sv_objcount) {
325 /*
326 * Try to destruct global references. We do this first so that the
327 * destructors and destructees still exist. Some sv's might remain.
328 * Non-referenced objects are on their own.
329 */
330
331 dirty = TRUE;
332 sv_clean_objs();
8990e307
LW
333 }
334
5cd24f17 335 /* unhook hooks which will soon be, or use, destroyed data */
336 SvREFCNT_dec(warnhook);
337 warnhook = Nullsv;
338 SvREFCNT_dec(diehook);
339 diehook = Nullsv;
340 SvREFCNT_dec(parsehook);
341 parsehook = Nullsv;
342
a0d0e21e 343 if (destruct_level == 0){
8990e307 344
a0d0e21e
LW
345 DEBUG_P(debprofdump());
346
347 /* The exit() function will do everything that needs doing. */
348 return;
349 }
5dd60ef7 350
5f05dabc 351 /* loosen bonds of global variables */
352
8ebc5c01 353 if(rsfp) {
354 (void)PerlIO_close(rsfp);
355 rsfp = Nullfp;
356 }
357
358 /* Filters for program text */
359 SvREFCNT_dec(rsfp_filters);
360 rsfp_filters = Nullav;
361
362 /* switches */
363 preprocess = FALSE;
364 minus_n = FALSE;
365 minus_p = FALSE;
366 minus_l = FALSE;
367 minus_a = FALSE;
368 minus_F = FALSE;
369 doswitches = FALSE;
370 dowarn = FALSE;
371 doextract = FALSE;
372 sawampersand = FALSE; /* must save all match strings */
373 sawstudy = FALSE; /* do fbm_instr on all strings */
374 sawvec = FALSE;
375 unsafe = FALSE;
376
377 Safefree(inplace);
378 inplace = Nullch;
379
380 Safefree(e_tmpname);
381 e_tmpname = Nullch;
382
383 if (e_fp) {
384 PerlIO_close(e_fp);
385 e_fp = Nullfp;
386 }
387
388 /* magical thingies */
389
390 Safefree(ofs); /* $, */
391 ofs = Nullch;
5f05dabc 392
8ebc5c01 393 Safefree(ors); /* $\ */
394 ors = Nullch;
395
396 SvREFCNT_dec(nrs); /* $\ helper */
5f05dabc 397 nrs = Nullsv;
398
8ebc5c01 399 multiline = 0; /* $* */
5f05dabc 400
8ebc5c01 401 SvREFCNT_dec(statname);
5f05dabc 402 statname = Nullsv;
403 statgv = Nullgv;
5f05dabc 404
8ebc5c01 405 /* defgv, aka *_ should be taken care of elsewhere */
406
8ebc5c01 407 /* clean up after study() */
408 SvREFCNT_dec(lastscream);
409 lastscream = Nullsv;
410 Safefree(screamfirst);
411 screamfirst = 0;
412 Safefree(screamnext);
413 screamnext = 0;
414
415 /* startup and shutdown function lists */
416 SvREFCNT_dec(beginav);
417 SvREFCNT_dec(endav);
77a005ab 418 SvREFCNT_dec(initav);
5618dfe8 419 beginav = Nullav;
5618dfe8 420 endav = Nullav;
77a005ab 421 initav = Nullav;
5618dfe8 422
8ebc5c01 423 /* temp stack during pp_sort() */
424 SvREFCNT_dec(sortstack);
425 sortstack = Nullav;
426
427 /* shortcuts just get cleared */
428 envgv = Nullgv;
429 siggv = Nullgv;
430 incgv = Nullgv;
12f917ad 431 errgv = Nullgv;
8ebc5c01 432 argvgv = Nullgv;
433 argvoutgv = Nullgv;
434 stdingv = Nullgv;
435 last_in_gv = Nullgv;
436
437 /* reset so print() ends up where we expect */
438 setdefout(Nullgv);
439
a0d0e21e 440 /* Prepare to destruct main symbol table. */
5f05dabc 441
a0d0e21e 442 hv = defstash;
85e6fe83 443 defstash = 0;
a0d0e21e
LW
444 SvREFCNT_dec(hv);
445
446 FREETMPS;
447 if (destruct_level >= 2) {
448 if (scopestack_ix != 0)
ff0cee69 449 warn("Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
450 (long)scopestack_ix);
a0d0e21e 451 if (savestack_ix != 0)
ff0cee69 452 warn("Unbalanced saves: %ld more saves than restores\n",
453 (long)savestack_ix);
a0d0e21e 454 if (tmps_floor != -1)
ff0cee69 455 warn("Unbalanced tmps: %ld more allocs than frees\n",
456 (long)tmps_floor + 1);
a0d0e21e 457 if (cxstack_ix != -1)
ff0cee69 458 warn("Unbalanced context: %ld more PUSHes than POPs\n",
459 (long)cxstack_ix + 1);
a0d0e21e 460 }
8990e307
LW
461
462 /* Now absolutely destruct everything, somehow or other, loops or no. */
8990e307 463 last_sv_count = 0;
6e72f9df 464 SvFLAGS(strtab) |= SVTYPEMASK; /* don't clean out strtab now */
8990e307
LW
465 while (sv_count != 0 && sv_count != last_sv_count) {
466 last_sv_count = sv_count;
467 sv_clean_all();
468 }
6e72f9df 469 SvFLAGS(strtab) &= ~SVTYPEMASK;
470 SvFLAGS(strtab) |= SVt_PVHV;
471
472 /* Destruct the global string table. */
473 {
474 /* Yell and reset the HeVAL() slots that are still holding refcounts,
475 * so that sv_free() won't fail on them.
476 */
477 I32 riter;
478 I32 max;
479 HE *hent;
480 HE **array;
481
482 riter = 0;
483 max = HvMAX(strtab);
484 array = HvARRAY(strtab);
485 hent = array[0];
486 for (;;) {
487 if (hent) {
488 warn("Unbalanced string table refcount: (%d) for \"%s\"",
489 HeVAL(hent) - Nullsv, HeKEY(hent));
490 HeVAL(hent) = Nullsv;
491 hent = HeNEXT(hent);
492 }
493 if (!hent) {
494 if (++riter > max)
495 break;
496 hent = array[riter];
497 }
498 }
499 }
500 SvREFCNT_dec(strtab);
501
8990e307 502 if (sv_count != 0)
ff0cee69 503 warn("Scalars leaked: %ld\n", (long)sv_count);
6e72f9df 504
4633a7c4 505 sv_free_arenas();
44a8e56a 506
507 /* No SVs have survived, need to clean out */
508 linestr = NULL;
509 pidstatus = Nullhv;
6e72f9df 510 if (origfilename)
511 Safefree(origfilename);
512 nuke_stacks();
fc36a67e 513 hints = 0; /* Reset hints. Should hints be per-interpreter ? */
a0d0e21e
LW
514
515 DEBUG_P(debprofdump());
11343788
MB
516#ifdef USE_THREADS
517 MUTEX_DESTROY(&sv_mutex);
11343788 518 MUTEX_DESTROY(&eval_mutex);
c116a00c 519 COND_DESTROY(&eval_cond);
fc36a67e 520
8023c3ce
MB
521 /* As the penultimate thing, free the non-arena SV for thrsv */
522 Safefree(SvPVX(thrsv));
523 Safefree(SvANY(thrsv));
524 Safefree(thrsv);
525 thrsv = Nullsv;
526#endif /* USE_THREADS */
527
fc36a67e 528 /* As the absolutely last thing, free the non-arena SV for mess() */
529
530 if (mess_sv) {
531 /* we know that type >= SVt_PV */
532 SvOOK_off(mess_sv);
533 Safefree(SvPVX(mess_sv));
534 Safefree(SvANY(mess_sv));
535 Safefree(mess_sv);
536 mess_sv = Nullsv;
537 }
79072805
LW
538}
539
540void
8ac85365 541perl_free(PerlInterpreter *sv_interp)
79072805
LW
542{
543 if (!(curinterp = sv_interp))
544 return;
545 Safefree(sv_interp);
546}
547
548int
8ac85365 549perl_parse(PerlInterpreter *sv_interp, void (*xsinit) (void), int argc, char **argv, char **env)
8d063cd8 550{
11343788 551 dTHR;
79072805 552 register SV *sv;
8d063cd8 553 register char *s;
1a30305b 554 char *scriptname = NULL;
a0d0e21e 555 VOL bool dosearch = FALSE;
13281fa4 556 char *validarg = "";
2ae324a7 557 I32 oldscope;
748a9306 558 AV* comppadlist;
54310121 559 dJMPENV;
22921e25 560 int ret;
8d063cd8 561
a687059c
LW
562#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
563#ifdef IAMSUID
564#undef IAMSUID
463ee0b2 565 croak("suidperl is no longer needed since the kernel can now execute\n\
a687059c
LW
566setuid perl scripts securely.\n");
567#endif
568#endif
569
79072805
LW
570 if (!(curinterp = sv_interp))
571 return 255;
572
6e72f9df 573#if defined(NeXT) && defined(__DYNAMIC__)
574 _dyld_lookup_and_bind
575 ("__environ", (unsigned long *) &environ_pointer, NULL);
576#endif /* environ */
577
ac58e20f
LW
578 origargv = argv;
579 origargc = argc;
a0d0e21e 580#ifndef VMS /* VMS doesn't have environ array */
fe14fcc3 581 origenviron = environ;
a0d0e21e 582#endif
ab821d7f 583 e_tmpname = Nullch;
a0d0e21e
LW
584
585 if (do_undump) {
586
587 /* Come here if running an undumped a.out. */
588
589 origfilename = savepv(argv[0]);
590 do_undump = FALSE;
591 cxstack_ix = -1; /* start label stack again */
748a9306 592 init_ids();
a0d0e21e
LW
593 init_postdump_symbols(argc,argv,env);
594 return 0;
595 }
596
ff0cee69 597 if (main_root) {
598 curpad = AvARRAY(comppad);
a0d0e21e 599 op_free(main_root);
ff0cee69 600 main_root = Nullop;
601 }
602 main_start = Nullop;
603 SvREFCNT_dec(main_cv);
604 main_cv = Nullcv;
79072805 605
f86702cc 606 time(&basetime);
2ae324a7 607 oldscope = scopestack_ix;
f86702cc 608
22921e25
CS
609 JMPENV_PUSH(ret);
610 switch (ret) {
79072805 611 case 1:
f86702cc 612 STATUS_ALL_FAILURE;
613 /* FALL THROUGH */
79072805 614 case 2:
f86702cc 615 /* my_exit() was called */
2ae324a7 616 while (scopestack_ix > oldscope)
617 LEAVE;
84902520 618 FREETMPS;
8990e307
LW
619 curstash = defstash;
620 if (endav)
68dc0745 621 call_list(oldscope, endav);
54310121 622 JMPENV_POP;
f86702cc 623 return STATUS_NATIVE_EXPORT;
79072805 624 case 3:
54310121 625 JMPENV_POP;
760ac839 626 PerlIO_printf(PerlIO_stderr(), "panic: top_env\n");
8990e307 627 return 1;
79072805
LW
628 }
629
79072805
LW
630 sv_setpvn(linestr,"",0);
631 sv = newSVpv("",0); /* first used for -I flags */
8990e307 632 SAVEFREESV(sv);
79072805 633 init_main_stash();
54310121 634
33b78306 635 for (argc--,argv++; argc > 0; argc--,argv++) {
8d063cd8
LW
636 if (argv[0][0] != '-' || !argv[0][1])
637 break;
13281fa4
LW
638#ifdef DOSUID
639 if (*validarg)
640 validarg = " PHOOEY ";
641 else
642 validarg = argv[0];
643#endif
644 s = argv[0]+1;
8d063cd8 645 reswitch:
13281fa4 646 switch (*s) {
27e2fb84 647 case '0':
2304df62 648 case 'F':
378cc40b 649 case 'a':
33b78306 650 case 'c':
a687059c 651 case 'd':
8d063cd8 652 case 'D':
4633a7c4 653 case 'h':
33b78306 654 case 'i':
fe14fcc3 655 case 'l':
1a30305b 656 case 'M':
657 case 'm':
33b78306
LW
658 case 'n':
659 case 'p':
79072805 660 case 's':
33b78306
LW
661 case 'u':
662 case 'U':
663 case 'v':
664 case 'w':
665 if (s = moreswitches(s))
666 goto reswitch;
8d063cd8 667 break;
33b78306 668
f86702cc 669 case 'T':
670 tainting = TRUE;
671 s++;
672 goto reswitch;
673
8d063cd8 674 case 'e':
a687059c 675 if (euid != uid || egid != gid)
463ee0b2 676 croak("No -e allowed in setuid scripts");
8d063cd8 677 if (!e_fp) {
a0d0e21e 678 e_tmpname = savepv(TMPPATH);
3028581b 679 (void)PerlLIO_mktemp(e_tmpname);
83025b21 680 if (!*e_tmpname)
463ee0b2 681 croak("Can't mktemp()");
760ac839 682 e_fp = PerlIO_open(e_tmpname,"w");
33b78306 683 if (!e_fp)
463ee0b2 684 croak("Cannot open temporary file");
8d063cd8 685 }
552a7a9b 686 if (*++s)
687 PerlIO_puts(e_fp,s);
688 else if (argv[1]) {
760ac839 689 PerlIO_puts(e_fp,argv[1]);
33b78306
LW
690 argc--,argv++;
691 }
552a7a9b 692 else
693 croak("No code specified for -e");
760ac839 694 (void)PerlIO_putc(e_fp,'\n');
8d063cd8 695 break;
fb73857a 696 case 'I': /* -I handled both here and in moreswitches() */
bbce6d69 697 forbid_setid("-I");
fb73857a 698 if (!*++s && (s=argv[1]) != Nullch) {
8d063cd8 699 argc--,argv++;
8d063cd8 700 }
fb73857a 701 while (s && isSPACE(*s))
702 ++s;
703 if (s && *s) {
704 char *e, *p;
705 for (e = s; *e && !isSPACE(*e); e++) ;
706 p = savepvn(s, e-s);
707 incpush(p, TRUE);
708 sv_catpv(sv,"-I");
709 sv_catpv(sv,p);
710 sv_catpv(sv," ");
711 Safefree(p);
712 } /* XXX else croak? */
8d063cd8 713 break;
8d063cd8 714 case 'P':
bbce6d69 715 forbid_setid("-P");
8d063cd8 716 preprocess = TRUE;
13281fa4 717 s++;
8d063cd8 718 goto reswitch;
378cc40b 719 case 'S':
bbce6d69 720 forbid_setid("-S");
378cc40b 721 dosearch = TRUE;
13281fa4 722 s++;
378cc40b 723 goto reswitch;
1a30305b 724 case 'V':
725 if (!preambleav)
726 preambleav = newAV();
727 av_push(preambleav, newSVpv("use Config qw(myconfig config_vars)",0));
728 if (*++s != ':') {
6e72f9df 729 Sv = newSVpv("print myconfig();",0);
730#ifdef VMS
731 sv_catpv(Sv,"print \"\\nCharacteristics of this PERLSHR image: \\n\",");
732#else
733 sv_catpv(Sv,"print \"\\nCharacteristics of this binary (from libperl): \\n\",");
734#endif
54310121 735#if defined(DEBUGGING) || defined(NO_EMBED) || defined(MULTIPLICITY)
46fc3d4c 736 sv_catpv(Sv,"\" Compile-time options:");
6e72f9df 737# ifdef DEBUGGING
46fc3d4c 738 sv_catpv(Sv," DEBUGGING");
6e72f9df 739# endif
54310121 740# ifdef NO_EMBED
46fc3d4c 741 sv_catpv(Sv," NO_EMBED");
6e72f9df 742# endif
743# ifdef MULTIPLICITY
46fc3d4c 744 sv_catpv(Sv," MULTIPLICITY");
6e72f9df 745# endif
46fc3d4c 746 sv_catpv(Sv,"\\n\",");
6e72f9df 747#endif
748#if defined(LOCAL_PATCH_COUNT)
54310121 749 if (LOCAL_PATCH_COUNT > 0) {
750 int i;
5cd24f17 751 sv_catpv(Sv,"\" Locally applied patches:\\n\",");
6e72f9df 752 for (i = 1; i <= LOCAL_PATCH_COUNT; i++) {
46fc3d4c 753 if (localpatches[i])
754 sv_catpvf(Sv,"\" \\t%s\\n\",",localpatches[i]);
6e72f9df 755 }
756 }
757#endif
46fc3d4c 758 sv_catpvf(Sv,"\" Built under %s\\n\"",OSNAME);
6e72f9df 759#ifdef __DATE__
760# ifdef __TIME__
46fc3d4c 761 sv_catpvf(Sv,",\" Compiled at %s %s\\n\"",__DATE__,__TIME__);
6e72f9df 762# else
46fc3d4c 763 sv_catpvf(Sv,",\" Compiled on %s\\n\"",__DATE__);
6e72f9df 764# endif
6e72f9df 765#endif
54310121 766 sv_catpv(Sv, "; \
767$\"=\"\\n \"; \
768@env = map { \"$_=\\\"$ENV{$_}\\\"\" } sort grep {/^PERL/} keys %ENV; \
769print \" \\%ENV:\\n @env\\n\" if @env; \
770print \" \\@INC:\\n @INC\\n\";");
1a30305b 771 }
772 else {
773 Sv = newSVpv("config_vars(qw(",0);
774 sv_catpv(Sv, ++s);
775 sv_catpv(Sv, "))");
776 s += strlen(s);
777 }
778 av_push(preambleav, Sv);
c07a80fd 779 scriptname = BIT_BUCKET; /* don't look for script or read stdin */
1a30305b 780 goto reswitch;
33b78306
LW
781 case 'x':
782 doextract = TRUE;
13281fa4 783 s++;
33b78306 784 if (*s)
a0d0e21e 785 cddir = savepv(s);
33b78306 786 break;
8d063cd8
LW
787 case 0:
788 break;
fb73857a 789 case '-':
790 if (!*++s || isSPACE(*s)) {
791 argc--,argv++;
792 goto switch_end;
793 }
794 /* catch use of gnu style long options */
795 if (strEQ(s, "version")) {
796 s = "v";
797 goto reswitch;
798 }
799 if (strEQ(s, "help")) {
800 s = "h";
801 goto reswitch;
802 }
803 s--;
804 /* FALL THROUGH */
8d063cd8 805 default:
90248788 806 croak("Unrecognized switch: -%s (-h will show valid options)",s);
8d063cd8
LW
807 }
808 }
809 switch_end:
54310121 810
5fd9e9a4 811 if (!tainting && (s = PerlEnv_getenv("PERL5OPT"))) {
fb73857a 812 while (s && *s) {
54310121 813 while (isSPACE(*s))
814 s++;
815 if (*s == '-') {
816 s++;
817 if (isSPACE(*s))
818 continue;
819 }
820 if (!*s)
821 break;
822 if (!strchr("DIMUdmw", *s))
823 croak("Illegal switch in PERL5OPT: -%c", *s);
824 s = moreswitches(s);
825 }
826 }
827
1a30305b 828 if (!scriptname)
829 scriptname = argv[0];
8d063cd8 830 if (e_fp) {
68dc0745 831 if (PerlIO_flush(e_fp) || PerlIO_error(e_fp) || PerlIO_close(e_fp)) {
832#ifndef MULTIPLICITY
833 warn("Did you forget to compile with -DMULTIPLICITY?");
834#endif
2304df62 835 croak("Can't write to temp file for -e: %s", Strerror(errno));
68dc0745 836 }
ab821d7f 837 e_fp = Nullfp;
8d063cd8 838 argc++,argv--;
45d8adaa 839 scriptname = e_tmpname;
8d063cd8 840 }
79072805
LW
841 else if (scriptname == Nullch) {
842#ifdef MSDOS
3028581b 843 if ( PerlLIO_isatty(PerlIO_fileno(PerlIO_stdin())) )
55497cff 844 moreswitches("h");
fe14fcc3 845#endif
79072805
LW
846 scriptname = "-";
847 }
fe14fcc3 848
79072805 849 init_perllib();
8d063cd8 850
79072805 851 open_script(scriptname,dosearch,sv);
8d063cd8 852
96436eeb 853 validate_suid(validarg, scriptname);
378cc40b 854
79072805
LW
855 if (doextract)
856 find_beginning();
857
4fdae800 858 main_cv = compcv = (CV*)NEWSV(1104,0);
748a9306 859 sv_upgrade((SV *)compcv, SVt_PVCV);
07055b4c 860 CvUNIQUE_on(compcv);
748a9306 861
6e72f9df 862 comppad = newAV();
79072805
LW
863 av_push(comppad, Nullsv);
864 curpad = AvARRAY(comppad);
6e72f9df 865 comppad_name = newAV();
8990e307 866 comppad_name_fill = 0;
e858de61
MB
867 min_intro_pending = 0;
868 padix = 0;
11343788
MB
869#ifdef USE_THREADS
870 av_store(comppad_name, 0, newSVpv("@_", 2));
e858de61 871 curpad[0] = (SV*)newAV();
6d4ff0d2 872 SvPADMY_on(curpad[0]); /* XXX Needed? */
e858de61 873 CvOWNER(compcv) = 0;
12ca11f6 874 New(666, CvMUTEXP(compcv), 1, perl_mutex);
e858de61 875 MUTEX_INIT(CvMUTEXP(compcv));
11343788 876#endif /* USE_THREADS */
79072805 877
748a9306
LW
878 comppadlist = newAV();
879 AvREAL_off(comppadlist);
8e07c86e
AD
880 av_store(comppadlist, 0, (SV*)comppad_name);
881 av_store(comppadlist, 1, (SV*)comppad);
748a9306
LW
882 CvPADLIST(compcv) = comppadlist;
883
6e72f9df 884 boot_core_UNIVERSAL();
a0d0e21e
LW
885 if (xsinit)
886 (*xsinit)(); /* in case linked C routines want magical variables */
39e571d4 887#if defined(VMS) || defined(WIN32) || defined(DJGPP)
748a9306
LW
888 init_os_extras();
889#endif
93a17b20 890
93a17b20 891 init_predump_symbols();
8990e307
LW
892 if (!do_undump)
893 init_postdump_symbols(argc,argv,env);
93a17b20 894
79072805
LW
895 init_lexer();
896
897 /* now parse the script */
898
61bb5906 899 SETERRNO(0,SS$_NORMAL);
79072805
LW
900 error_count = 0;
901 if (yyparse() || error_count) {
902 if (minus_c)
463ee0b2 903 croak("%s had compilation errors.\n", origfilename);
79072805 904 else {
463ee0b2 905 croak("Execution of %s aborted due to compilation errors.\n",
79072805 906 origfilename);
378cc40b 907 }
79072805
LW
908 }
909 curcop->cop_line = 0;
910 curstash = defstash;
911 preprocess = FALSE;
ab821d7f 912 if (e_tmpname) {
79072805 913 (void)UNLINK(e_tmpname);
ab821d7f 914 Safefree(e_tmpname);
915 e_tmpname = Nullch;
378cc40b 916 }
a687059c 917
93a17b20 918 /* now that script is parsed, we can modify record separator */
c07a80fd 919 SvREFCNT_dec(rs);
920 rs = SvREFCNT_inc(nrs);
e1c148c2 921 sv_setsv(perl_get_sv("/", TRUE), rs);
79072805
LW
922 if (do_undump)
923 my_unexec();
924
8990e307
LW
925 if (dowarn)
926 gv_check(defstash);
927
a0d0e21e
LW
928 LEAVE;
929 FREETMPS;
c07a80fd 930
3562ef9b 931#ifdef MYMALLOC
5fd9e9a4 932 if ((s=PerlEnv_getenv("PERL_DEBUG_MSTATS")) && atoi(s) >= 2)
c07a80fd 933 dump_mstats("after compilation:");
934#endif
935
a0d0e21e
LW
936 ENTER;
937 restartop = 0;
54310121 938 JMPENV_POP;
79072805
LW
939 return 0;
940}
941
942int
8ac85365 943perl_run(PerlInterpreter *sv_interp)
79072805 944{
11343788 945 dTHR;
2ae324a7 946 I32 oldscope;
22921e25
CS
947 dJMPENV;
948 int ret;
2ae324a7 949
79072805
LW
950 if (!(curinterp = sv_interp))
951 return 255;
2ae324a7 952
953 oldscope = scopestack_ix;
954
22921e25
CS
955 JMPENV_PUSH(ret);
956 switch (ret) {
79072805
LW
957 case 1:
958 cxstack_ix = -1; /* start context stack again */
959 break;
960 case 2:
f86702cc 961 /* my_exit() was called */
2ae324a7 962 while (scopestack_ix > oldscope)
963 LEAVE;
84902520 964 FREETMPS;
79072805 965 curstash = defstash;
93a17b20 966 if (endav)
68dc0745 967 call_list(oldscope, endav);
3562ef9b 968#ifdef MYMALLOC
5fd9e9a4 969 if (PerlEnv_getenv("PERL_DEBUG_MSTATS"))
c07a80fd 970 dump_mstats("after execution: ");
971#endif
54310121 972 JMPENV_POP;
f86702cc 973 return STATUS_NATIVE_EXPORT;
79072805
LW
974 case 3:
975 if (!restartop) {
760ac839 976 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
a0d0e21e 977 FREETMPS;
54310121 978 JMPENV_POP;
8990e307 979 return 1;
83025b21 980 }
6e72f9df 981 if (curstack != mainstack) {
79072805 982 dSP;
6e72f9df 983 SWITCHSTACK(curstack, mainstack);
79072805
LW
984 }
985 break;
8d063cd8 986 }
79072805 987
fb73857a 988 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s $` $& $' support.\n",
6e72f9df 989 sawampersand ? "Enabling" : "Omitting"));
990
79072805
LW
991 if (!restartop) {
992 DEBUG_x(dump_all());
760ac839 993 DEBUG(PerlIO_printf(Perl_debug_log, "\nEXECUTING...\n\n"));
11343788 994#ifdef USE_THREADS
5dc0d613
MB
995 DEBUG_L(PerlIO_printf(Perl_debug_log, "main thread is 0x%lx\n",
996 (unsigned long) thr));
11343788 997#endif /* USE_THREADS */
79072805
LW
998
999 if (minus_c) {
760ac839 1000 PerlIO_printf(PerlIO_stderr(), "%s syntax OK\n", origfilename);
79072805
LW
1001 my_exit(0);
1002 }
84902520 1003 if (PERLDB_SINGLE && DBsingle)
a0d0e21e 1004 sv_setiv(DBsingle, 1);
7d07dbc2
MB
1005 if (initav)
1006 call_list(oldscope, initav);
45d8adaa 1007 }
79072805
LW
1008
1009 /* do it */
1010
1011 if (restartop) {
1012 op = restartop;
1013 restartop = 0;
ab821d7f 1014 runops();
79072805
LW
1015 }
1016 else if (main_start) {
4fdae800 1017 CvDEPTH(main_cv) = 1;
79072805 1018 op = main_start;
ab821d7f 1019 runops();
79072805 1020 }
79072805
LW
1021
1022 my_exit(0);
54310121 1023 /* NOTREACHED */
a0d0e21e 1024 return 0;
79072805
LW
1025}
1026
a0d0e21e 1027SV*
8ac85365 1028perl_get_sv(char *name, I32 create)
a0d0e21e 1029{
2faa37cc 1030 GV *gv;
38a03e6e 1031#ifdef USE_THREADS
2faa37cc 1032 if (name[1] == '\0' && !isALPHA(name[0])) {
54b9620d 1033 PADOFFSET tmp = find_threadsv(name);
2faa37cc
MB
1034 if (tmp != NOT_IN_PAD) {
1035 dTHR;
940cb80d 1036 return THREADSV(tmp);
2faa37cc 1037 }
38a03e6e
MB
1038 }
1039#endif /* USE_THREADS */
2faa37cc 1040 gv = gv_fetchpv(name, create, SVt_PV);
a0d0e21e
LW
1041 if (gv)
1042 return GvSV(gv);
1043 return Nullsv;
1044}
1045
1046AV*
8ac85365 1047perl_get_av(char *name, I32 create)
a0d0e21e
LW
1048{
1049 GV* gv = gv_fetchpv(name, create, SVt_PVAV);
1050 if (create)
1051 return GvAVn(gv);
1052 if (gv)
1053 return GvAV(gv);
1054 return Nullav;
1055}
1056
1057HV*
8ac85365 1058perl_get_hv(char *name, I32 create)
a0d0e21e
LW
1059{
1060 GV* gv = gv_fetchpv(name, create, SVt_PVHV);
1061 if (create)
1062 return GvHVn(gv);
1063 if (gv)
1064 return GvHV(gv);
1065 return Nullhv;
1066}
1067
1068CV*
8ac85365 1069perl_get_cv(char *name, I32 create)
a0d0e21e
LW
1070{
1071 GV* gv = gv_fetchpv(name, create, SVt_PVCV);
8ebc5c01 1072 if (create && !GvCVu(gv))
774d564b 1073 return newSUB(start_subparse(FALSE, 0),
a0d0e21e 1074 newSVOP(OP_CONST, 0, newSVpv(name,0)),
4633a7c4 1075 Nullop,
a0d0e21e
LW
1076 Nullop);
1077 if (gv)
8ebc5c01 1078 return GvCVu(gv);
a0d0e21e
LW
1079 return Nullcv;
1080}
1081
79072805
LW
1082/* Be sure to refetch the stack pointer after calling these routines. */
1083
a0d0e21e 1084I32
22239a37 1085perl_call_argv(char *sub_name, I32 flags, register char **argv)
8ac85365
NIS
1086
1087 /* See G_* flags in cop.h */
1088 /* null terminated arg list */
8990e307 1089{
a0d0e21e 1090 dSP;
8990e307 1091
a0d0e21e
LW
1092 PUSHMARK(sp);
1093 if (argv) {
8990e307 1094 while (*argv) {
a0d0e21e 1095 XPUSHs(sv_2mortal(newSVpv(*argv,0)));
8990e307
LW
1096 argv++;
1097 }
a0d0e21e 1098 PUTBACK;
8990e307 1099 }
22239a37 1100 return perl_call_pv(sub_name, flags);
8990e307
LW
1101}
1102
a0d0e21e 1103I32
22239a37 1104perl_call_pv(char *sub_name, I32 flags)
8ac85365
NIS
1105 /* name of the subroutine */
1106 /* See G_* flags in cop.h */
a0d0e21e 1107{
22239a37 1108 return perl_call_sv((SV*)perl_get_cv(sub_name, TRUE), flags);
a0d0e21e
LW
1109}
1110
1111I32
8ac85365
NIS
1112perl_call_method(char *methname, I32 flags)
1113 /* name of the subroutine */
1114 /* See G_* flags in cop.h */
a0d0e21e
LW
1115{
1116 dSP;
1117 OP myop;
1118 if (!op)
1119 op = &myop;
1120 XPUSHs(sv_2mortal(newSVpv(methname,0)));
1121 PUTBACK;
11343788 1122 pp_method(ARGS);
a0d0e21e
LW
1123 return perl_call_sv(*stack_sp--, flags);
1124}
1125
1126/* May be called with any of a CV, a GV, or an SV containing the name. */
1127I32
8ac85365
NIS
1128perl_call_sv(SV *sv, I32 flags)
1129
1130 /* See G_* flags in cop.h */
a0d0e21e 1131{
11343788 1132 dTHR;
a0d0e21e
LW
1133 LOGOP myop; /* fake syntax tree node */
1134 SV** sp = stack_sp;
aa689395 1135 I32 oldmark;
a0d0e21e 1136 I32 retval;
a0d0e21e 1137 I32 oldscope;
6e72f9df 1138 static CV *DBcv;
54310121 1139 bool oldcatch = CATCH_GET;
1140 dJMPENV;
22921e25 1141 int ret;
d6602a8c 1142 OP* oldop = op;
1e422769 1143
a0d0e21e
LW
1144 if (flags & G_DISCARD) {
1145 ENTER;
1146 SAVETMPS;
1147 }
1148
aa689395 1149 Zero(&myop, 1, LOGOP);
54310121 1150 myop.op_next = Nullop;
f51d4af5 1151 if (!(flags & G_NOARGS))
aa689395 1152 myop.op_flags |= OPf_STACKED;
54310121 1153 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1154 (flags & G_ARRAY) ? OPf_WANT_LIST :
1155 OPf_WANT_SCALAR);
462e5cf6 1156 SAVEOP();
a0d0e21e 1157 op = (OP*)&myop;
aa689395 1158
a0d0e21e
LW
1159 EXTEND(stack_sp, 1);
1160 *++stack_sp = sv;
aa689395 1161 oldmark = TOPMARK;
a0d0e21e
LW
1162 oldscope = scopestack_ix;
1163
84902520 1164 if (PERLDB_SUB && curstash != debstash
36477c24 1165 /* Handle first BEGIN of -d. */
1166 && (DBcv || (DBcv = GvCV(DBsub)))
1167 /* Try harder, since this may have been a sighandler, thus
1168 * curstash may be meaningless. */
1169 && (SvTYPE(sv) != SVt_PVCV || CvSTASH((CV*)sv) != debstash))
6e72f9df 1170 op->op_private |= OPpENTERSUB_DB;
a0d0e21e
LW
1171
1172 if (flags & G_EVAL) {
a0d0e21e
LW
1173 cLOGOP->op_other = op;
1174 markstack_ptr--;
4633a7c4
LW
1175 /* we're trying to emulate pp_entertry() here */
1176 {
c09156bb 1177 register PERL_CONTEXT *cx;
54310121 1178 I32 gimme = GIMME_V;
4633a7c4
LW
1179
1180 ENTER;
1181 SAVETMPS;
1182
1183 push_return(op->op_next);
1184 PUSHBLOCK(cx, CXt_EVAL, stack_sp);
1185 PUSHEVAL(cx, 0, 0);
1186 eval_root = op; /* Only needed so that goto works right. */
1187
1188 in_eval = 1;
1189 if (flags & G_KEEPERR)
1190 in_eval |= 4;
1191 else
38a03e6e 1192 sv_setpv(ERRSV,"");
4633a7c4 1193 }
a0d0e21e
LW
1194 markstack_ptr++;
1195
22921e25
CS
1196 JMPENV_PUSH(ret);
1197 switch (ret) {
a0d0e21e
LW
1198 case 0:
1199 break;
1200 case 1:
f86702cc 1201 STATUS_ALL_FAILURE;
a0d0e21e
LW
1202 /* FALL THROUGH */
1203 case 2:
1204 /* my_exit() was called */
1205 curstash = defstash;
1206 FREETMPS;
54310121 1207 JMPENV_POP;
a0d0e21e
LW
1208 if (statusvalue)
1209 croak("Callback called exit");
f86702cc 1210 my_exit_jump();
a0d0e21e
LW
1211 /* NOTREACHED */
1212 case 3:
1213 if (restartop) {
1214 op = restartop;
1215 restartop = 0;
54310121 1216 break;
a0d0e21e
LW
1217 }
1218 stack_sp = stack_base + oldmark;
1219 if (flags & G_ARRAY)
1220 retval = 0;
1221 else {
1222 retval = 1;
1223 *++stack_sp = &sv_undef;
1224 }
1225 goto cleanup;
1226 }
1227 }
1e422769 1228 else
54310121 1229 CATCH_SET(TRUE);
a0d0e21e
LW
1230
1231 if (op == (OP*)&myop)
11343788 1232 op = pp_entersub(ARGS);
a0d0e21e 1233 if (op)
ab821d7f 1234 runops();
a0d0e21e 1235 retval = stack_sp - (stack_base + oldmark);
4633a7c4 1236 if ((flags & G_EVAL) && !(flags & G_KEEPERR))
38a03e6e 1237 sv_setpv(ERRSV,"");
a0d0e21e
LW
1238
1239 cleanup:
1240 if (flags & G_EVAL) {
1241 if (scopestack_ix > oldscope) {
a0a2876f
LW
1242 SV **newsp;
1243 PMOP *newpm;
1244 I32 gimme;
c09156bb 1245 register PERL_CONTEXT *cx;
a0a2876f
LW
1246 I32 optype;
1247
1248 POPBLOCK(cx,newpm);
1249 POPEVAL(cx);
1250 pop_return();
1251 curpm = newpm;
1252 LEAVE;
a0d0e21e 1253 }
54310121 1254 JMPENV_POP;
a0d0e21e 1255 }
1e422769 1256 else
54310121 1257 CATCH_SET(oldcatch);
1e422769 1258
a0d0e21e
LW
1259 if (flags & G_DISCARD) {
1260 stack_sp = stack_base + oldmark;
1261 retval = 0;
1262 FREETMPS;
1263 LEAVE;
1264 }
d6602a8c 1265 op = oldop;
a0d0e21e
LW
1266 return retval;
1267}
1268
6e72f9df 1269/* Eval a string. The G_EVAL flag is always assumed. */
8990e307 1270
a0d0e21e 1271I32
8ac85365
NIS
1272perl_eval_sv(SV *sv, I32 flags)
1273
1274 /* See G_* flags in cop.h */
a0d0e21e 1275{
11343788 1276 dTHR;
a0d0e21e 1277 UNOP myop; /* fake syntax tree node */
4633a7c4
LW
1278 SV** sp = stack_sp;
1279 I32 oldmark = sp - stack_base;
1280 I32 retval;
4633a7c4 1281 I32 oldscope;
54310121 1282 dJMPENV;
22921e25 1283 int ret;
84902520
TB
1284 OP* oldop = op;
1285
4633a7c4
LW
1286 if (flags & G_DISCARD) {
1287 ENTER;
1288 SAVETMPS;
1289 }
1290
462e5cf6 1291 SAVEOP();
79072805 1292 op = (OP*)&myop;
a0d0e21e 1293 Zero(op, 1, UNOP);
4633a7c4
LW
1294 EXTEND(stack_sp, 1);
1295 *++stack_sp = sv;
1296 oldscope = scopestack_ix;
79072805 1297
4633a7c4
LW
1298 if (!(flags & G_NOARGS))
1299 myop.op_flags = OPf_STACKED;
79072805 1300 myop.op_next = Nullop;
6e72f9df 1301 myop.op_type = OP_ENTEREVAL;
54310121 1302 myop.op_flags |= ((flags & G_VOID) ? OPf_WANT_VOID :
1303 (flags & G_ARRAY) ? OPf_WANT_LIST :
1304 OPf_WANT_SCALAR);
6e72f9df 1305 if (flags & G_KEEPERR)
1306 myop.op_flags |= OPf_SPECIAL;
4633a7c4 1307
22921e25
CS
1308 JMPENV_PUSH(ret);
1309 switch (ret) {
4633a7c4
LW
1310 case 0:
1311 break;
1312 case 1:
f86702cc 1313 STATUS_ALL_FAILURE;
4633a7c4
LW
1314 /* FALL THROUGH */
1315 case 2:
1316 /* my_exit() was called */
1317 curstash = defstash;
1318 FREETMPS;
54310121 1319 JMPENV_POP;
4633a7c4
LW
1320 if (statusvalue)
1321 croak("Callback called exit");
f86702cc 1322 my_exit_jump();
4633a7c4
LW
1323 /* NOTREACHED */
1324 case 3:
1325 if (restartop) {
1326 op = restartop;
1327 restartop = 0;
54310121 1328 break;
4633a7c4
LW
1329 }
1330 stack_sp = stack_base + oldmark;
1331 if (flags & G_ARRAY)
1332 retval = 0;
1333 else {
1334 retval = 1;
1335 *++stack_sp = &sv_undef;
1336 }
1337 goto cleanup;
1338 }
1339
1340 if (op == (OP*)&myop)
11343788 1341 op = pp_entereval(ARGS);
4633a7c4 1342 if (op)
ab821d7f 1343 runops();
4633a7c4 1344 retval = stack_sp - (stack_base + oldmark);
6e72f9df 1345 if (!(flags & G_KEEPERR))
38a03e6e 1346 sv_setpv(ERRSV,"");
4633a7c4
LW
1347
1348 cleanup:
54310121 1349 JMPENV_POP;
4633a7c4
LW
1350 if (flags & G_DISCARD) {
1351 stack_sp = stack_base + oldmark;
1352 retval = 0;
1353 FREETMPS;
1354 LEAVE;
1355 }
84902520 1356 op = oldop;
4633a7c4
LW
1357 return retval;
1358}
1359
137443ea 1360SV*
8ac85365 1361perl_eval_pv(char *p, I32 croak_on_error)
137443ea 1362{
1363 dSP;
1364 SV* sv = newSVpv(p, 0);
1365
1366 PUSHMARK(sp);
1367 perl_eval_sv(sv, G_SCALAR);
1368 SvREFCNT_dec(sv);
1369
1370 SPAGAIN;
1371 sv = POPs;
1372 PUTBACK;
1373
38a03e6e
MB
1374 if (croak_on_error && SvTRUE(ERRSV))
1375 croak(SvPVx(ERRSV, na));
137443ea 1376
1377 return sv;
1378}
1379
4633a7c4
LW
1380/* Require a module. */
1381
1382void
8ac85365 1383perl_require_pv(char *pv)
4633a7c4
LW
1384{
1385 SV* sv = sv_newmortal();
1386 sv_setpv(sv, "require '");
1387 sv_catpv(sv, pv);
1388 sv_catpv(sv, "'");
1389 perl_eval_sv(sv, G_DISCARD);
79072805
LW
1390}
1391
79072805 1392void
8ac85365 1393magicname(char *sym, char *name, I32 namlen)
79072805
LW
1394{
1395 register GV *gv;
1396
85e6fe83 1397 if (gv = gv_fetchpv(sym,TRUE, SVt_PV))
79072805
LW
1398 sv_magic(GvSV(gv), (SV*)gv, 0, name, namlen);
1399}
1400
ab821d7f 1401static void
8ac85365
NIS
1402usage(char *name) /* XXX move this out into a module ? */
1403
4633a7c4 1404{
ab821d7f 1405 /* This message really ought to be max 23 lines.
1406 * Removed -h because the user already knows that opton. Others? */
fb73857a 1407
1408 static char *usage[] = {
1409"-0[octal] specify record separator (\\0, if no argument)",
1410"-a autosplit mode with -n or -p (splits $_ into @F)",
1411"-c check syntax only (runs BEGIN and END blocks)",
1412"-d[:debugger] run scripts under debugger",
1413"-D[number/list] set debugging flags (argument is a bit mask or flags)",
1414"-e 'command' one line of script. Several -e's allowed. Omit [programfile].",
1415"-F/pattern/ split() pattern for autosplit (-a). The //'s are optional.",
1416"-i[extension] edit <> files in place (make backup if extension supplied)",
1417"-Idirectory specify @INC/#include directory (may be used more than once)",
1418"-l[octal] enable line ending processing, specifies line terminator",
1419"-[mM][-]module.. executes `use/no module...' before executing your script.",
1420"-n assume 'while (<>) { ... }' loop around your script",
1421"-p assume loop like -n but print line also like sed",
1422"-P run script through C preprocessor before compilation",
1423"-s enable some switch parsing for switches after script name",
1424"-S look for the script using PATH environment variable",
1425"-T turn on tainting checks",
1426"-u dump core after parsing script",
1427"-U allow unsafe operations",
1428"-v print version number and patchlevel of perl",
1429"-V[:variable] print perl configuration information",
1430"-w TURN WARNINGS ON FOR COMPILATION OF YOUR SCRIPT. Recommended.",
1431"-x[directory] strip off text before #!perl line and perhaps cd to directory",
1432"\n",
1433NULL
1434};
1435 char **p = usage;
1436
ab821d7f 1437 printf("\nUsage: %s [switches] [--] [programfile] [arguments]", name);
fb73857a 1438 while (*p)
1439 printf("\n %s", *p++);
4633a7c4
LW
1440}
1441
79072805
LW
1442/* This routine handles any switches that can be given during run */
1443
1444char *
8ac85365 1445moreswitches(char *s)
79072805
LW
1446{
1447 I32 numlen;
c07a80fd 1448 U32 rschar;
79072805
LW
1449
1450 switch (*s) {
1451 case '0':
a863c7d1
MB
1452 {
1453 dTHR;
c07a80fd 1454 rschar = scan_oct(s, 4, &numlen);
1455 SvREFCNT_dec(nrs);
1456 if (rschar & ~((U8)~0))
1457 nrs = &sv_undef;
1458 else if (!rschar && numlen >= 2)
1459 nrs = newSVpv("", 0);
1460 else {
1461 char ch = rschar;
1462 nrs = newSVpv(&ch, 1);
79072805
LW
1463 }
1464 return s + numlen;
a863c7d1 1465 }
2304df62
AD
1466 case 'F':
1467 minus_F = TRUE;
a0d0e21e 1468 splitstr = savepv(s + 1);
2304df62
AD
1469 s += strlen(s);
1470 return s;
79072805
LW
1471 case 'a':
1472 minus_a = TRUE;
1473 s++;
1474 return s;
1475 case 'c':
1476 minus_c = TRUE;
1477 s++;
1478 return s;
1479 case 'd':
bbce6d69 1480 forbid_setid("-d");
4633a7c4 1481 s++;
c07a80fd 1482 if (*s == ':' || *s == '=') {
46fc3d4c 1483 my_setenv("PERL5DB", form("use Devel::%s;", ++s));
4633a7c4 1484 s += strlen(s);
4633a7c4 1485 }
a0d0e21e 1486 if (!perldb) {
84902520 1487 perldb = PERLDB_ALL;
a0d0e21e
LW
1488 init_debugger();
1489 }
79072805
LW
1490 return s;
1491 case 'D':
1492#ifdef DEBUGGING
bbce6d69 1493 forbid_setid("-D");
79072805 1494 if (isALPHA(s[1])) {
8990e307 1495 static char debopts[] = "psltocPmfrxuLHXD";
79072805
LW
1496 char *d;
1497
93a17b20 1498 for (s++; *s && (d = strchr(debopts,*s)); s++)
79072805
LW
1499 debug |= 1 << (d - debopts);
1500 }
1501 else {
1502 debug = atoi(s+1);
1503 for (s++; isDIGIT(*s); s++) ;
1504 }
8990e307 1505 debug |= 0x80000000;
79072805
LW
1506#else
1507 warn("Recompile perl with -DDEBUGGING to use -D switch\n");
a0d0e21e 1508 for (s++; isALNUM(*s); s++) ;
79072805
LW
1509#endif
1510 /*SUPPRESS 530*/
1511 return s;
4633a7c4
LW
1512 case 'h':
1513 usage(origargv[0]);
3028581b 1514 PerlProc_exit(0);
79072805
LW
1515 case 'i':
1516 if (inplace)
1517 Safefree(inplace);
a0d0e21e 1518 inplace = savepv(s+1);
79072805
LW
1519 /*SUPPRESS 530*/
1520 for (s = inplace; *s && !isSPACE(*s); s++) ;
fb73857a 1521 if (*s)
1522 *s++ = '\0';
1523 return s;
1524 case 'I': /* -I handled both here and in parse_perl() */
bbce6d69 1525 forbid_setid("-I");
fb73857a 1526 ++s;
1527 while (*s && isSPACE(*s))
1528 ++s;
1529 if (*s) {
774d564b 1530 char *e, *p;
748a9306 1531 for (e = s; *e && !isSPACE(*e); e++) ;
774d564b 1532 p = savepvn(s, e-s);
1533 incpush(p, TRUE);
1534 Safefree(p);
fb73857a 1535 s = e;
79072805
LW
1536 }
1537 else
463ee0b2 1538 croak("No space allowed after -I");
fb73857a 1539 return s;
79072805
LW
1540 case 'l':
1541 minus_l = TRUE;
1542 s++;
a0d0e21e
LW
1543 if (ors)
1544 Safefree(ors);
79072805 1545 if (isDIGIT(*s)) {
a0d0e21e 1546 ors = savepv("\n");
79072805
LW
1547 orslen = 1;
1548 *ors = scan_oct(s, 3 + (*s == '0'), &numlen);
1549 s += numlen;
1550 }
1551 else {
a863c7d1 1552 dTHR;
c07a80fd 1553 if (RsPARA(nrs)) {
6e72f9df 1554 ors = "\n\n";
c07a80fd 1555 orslen = 2;
1556 }
1557 else
1558 ors = SvPV(nrs, orslen);
6e72f9df 1559 ors = savepvn(ors, orslen);
79072805
LW
1560 }
1561 return s;
1a30305b 1562 case 'M':
bbce6d69 1563 forbid_setid("-M"); /* XXX ? */
1a30305b 1564 /* FALL THROUGH */
1565 case 'm':
bbce6d69 1566 forbid_setid("-m"); /* XXX ? */
1a30305b 1567 if (*++s) {
a5f75d66 1568 char *start;
11343788 1569 SV *sv;
a5f75d66
AD
1570 char *use = "use ";
1571 /* -M-foo == 'no foo' */
1572 if (*s == '-') { use = "no "; ++s; }
11343788 1573 sv = newSVpv(use,0);
a5f75d66 1574 start = s;
1a30305b 1575 /* We allow -M'Module qw(Foo Bar)' */
c07a80fd 1576 while(isALNUM(*s) || *s==':') ++s;
1577 if (*s != '=') {
11343788 1578 sv_catpv(sv, start);
c07a80fd 1579 if (*(start-1) == 'm') {
1580 if (*s != '\0')
1581 croak("Can't use '%c' after -mname", *s);
11343788 1582 sv_catpv( sv, " ()");
c07a80fd 1583 }
1584 } else {
11343788
MB
1585 sv_catpvn(sv, start, s-start);
1586 sv_catpv(sv, " split(/,/,q{");
1587 sv_catpv(sv, ++s);
1588 sv_catpv(sv, "})");
c07a80fd 1589 }
1a30305b 1590 s += strlen(s);
c07a80fd 1591 if (preambleav == NULL)
1592 preambleav = newAV();
11343788 1593 av_push(preambleav, sv);
1a30305b 1594 }
1595 else
1596 croak("No space allowed after -%c", *(s-1));
1597 return s;
79072805
LW
1598 case 'n':
1599 minus_n = TRUE;
1600 s++;
1601 return s;
1602 case 'p':
1603 minus_p = TRUE;
1604 s++;
1605 return s;
1606 case 's':
bbce6d69 1607 forbid_setid("-s");
79072805
LW
1608 doswitches = TRUE;
1609 s++;
1610 return s;
463ee0b2 1611 case 'T':
f86702cc 1612 if (!tainting)
9607fc9c 1613 croak("Too late for \"-T\" option");
463ee0b2
LW
1614 s++;
1615 return s;
79072805
LW
1616 case 'u':
1617 do_undump = TRUE;
1618 s++;
1619 return s;
1620 case 'U':
1621 unsafe = TRUE;
1622 s++;
1623 return s;
1624 case 'v':
a5f75d66 1625#if defined(SUBVERSION) && SUBVERSION > 0
fb73857a 1626 printf("\nThis is perl, version 5.%03d_%02d built for %s",
1627 PATCHLEVEL, SUBVERSION, ARCHNAME);
a5f75d66 1628#else
fb73857a 1629 printf("\nThis is perl, version %s built for %s",
1630 patchlevel, ARCHNAME);
1631#endif
1632#if defined(LOCAL_PATCH_COUNT)
1633 if (LOCAL_PATCH_COUNT > 0)
1634 printf("\n(with %d registered patch%s, see perl -V for more detail)",
1635 LOCAL_PATCH_COUNT, (LOCAL_PATCH_COUNT!=1) ? "es" : "");
a5f75d66 1636#endif
1a30305b 1637
a411490c 1638 printf("\n\nCopyright 1987-1998, Larry Wall\n");
79072805 1639#ifdef MSDOS
fb73857a 1640 printf("\nMS-DOS port Copyright (c) 1989, 1990, Diomidis Spinellis\n");
55497cff 1641#endif
1642#ifdef DJGPP
1643 printf("djgpp v2 port (jpl5003c) by Hirofumi Watanabe, 1996\n");
9731c6ca 1644 printf("djgpp v2 port (perl5004+) by Laszlo Molnar, 1997-1998\n");
4633a7c4 1645#endif
79072805 1646#ifdef OS2
5dd60ef7 1647 printf("\n\nOS/2 port Copyright (c) 1990, 1991, Raymond Chen, Kai Uwe Rommel\n"
a411490c 1648 "Version 5 port Copyright (c) 1994-1998, Andreas Kaiser, Ilya Zakharevich\n");
79072805 1649#endif
79072805 1650#ifdef atarist
760ac839 1651 printf("atariST series port, ++jrb bammi@cadence.com\n");
79072805 1652#endif
760ac839 1653 printf("\n\
79072805 1654Perl may be copied only under the terms of either the Artistic License or the\n\
760ac839 1655GNU General Public License, which may be found in the Perl 5.0 source kit.\n\n");
3028581b 1656 PerlProc_exit(0);
79072805
LW
1657 case 'w':
1658 dowarn = TRUE;
1659 s++;
1660 return s;
a0d0e21e 1661 case '*':
79072805
LW
1662 case ' ':
1663 if (s[1] == '-') /* Additional switches on #! line. */
1664 return s+2;
1665 break;
a0d0e21e 1666 case '-':
79072805 1667 case 0:
a868473f
NIS
1668#ifdef WIN32
1669 case '\r':
1670#endif
79072805
LW
1671 case '\n':
1672 case '\t':
1673 break;
aa689395 1674#ifdef ALTERNATE_SHEBANG
1675 case 'S': /* OS/2 needs -S on "extproc" line. */
1676 break;
1677#endif
a0d0e21e
LW
1678 case 'P':
1679 if (preprocess)
1680 return s+1;
1681 /* FALL THROUGH */
79072805 1682 default:
a0d0e21e 1683 croak("Can't emulate -%.1s on #! line",s);
79072805
LW
1684 }
1685 return Nullch;
1686}
1687
1688/* compliments of Tom Christiansen */
1689
1690/* unexec() can be found in the Gnu emacs distribution */
1691
1692void
8ac85365 1693my_unexec(void)
79072805
LW
1694{
1695#ifdef UNEXEC
46fc3d4c 1696 SV* prog;
1697 SV* file;
79072805
LW
1698 int status;
1699 extern int etext;
1700
46fc3d4c 1701 prog = newSVpv(BIN_EXP);
1702 sv_catpv(prog, "/perl");
1703 file = newSVpv(origfilename);
1704 sv_catpv(file, ".perldump");
79072805 1705
46fc3d4c 1706 status = unexec(SvPVX(file), SvPVX(prog), &etext, sbrk(0), 0);
79072805 1707 if (status)
46fc3d4c 1708 PerlIO_printf(PerlIO_stderr(), "unexec of %s into %s failed!\n",
1709 SvPVX(prog), SvPVX(file));
3028581b 1710 PerlProc_exit(status);
79072805 1711#else
a5f75d66
AD
1712# ifdef VMS
1713# include <lib$routines.h>
1714 lib$signal(SS$_DEBUG); /* ssdef.h #included from vmsish.h */
aa689395 1715# else
79072805 1716 ABORT(); /* for use with undump */
aa689395 1717# endif
a5f75d66 1718#endif
79072805
LW
1719}
1720
1721static void
8ac85365 1722init_main_stash(void)
79072805 1723{
11343788 1724 dTHR;
463ee0b2 1725 GV *gv;
6e72f9df 1726
1727 /* Note that strtab is a rather special HV. Assumptions are made
1728 about not iterating on it, and not adding tie magic to it.
1729 It is properly deallocated in perl_destruct() */
1730 strtab = newHV();
1731 HvSHAREKEYS_off(strtab); /* mandatory */
1732 Newz(506,((XPVHV*)SvANY(strtab))->xhv_array,
1733 sizeof(HE*) * (((XPVHV*)SvANY(strtab))->xhv_max + 1), char);
1734
463ee0b2 1735 curstash = defstash = newHV();
79072805 1736 curstname = newSVpv("main",4);
adbc6bb1
LW
1737 gv = gv_fetchpv("main::",TRUE, SVt_PVHV);
1738 SvREFCNT_dec(GvHV(gv));
1739 GvHV(gv) = (HV*)SvREFCNT_inc(defstash);
463ee0b2 1740 SvREADONLY_on(gv);
a0d0e21e 1741 HvNAME(defstash) = savepv("main");
85e6fe83 1742 incgv = gv_HVadd(gv_AVadd(gv_fetchpv("INC",TRUE, SVt_PVAV)));
a5f75d66 1743 GvMULTI_on(incgv);
a0d0e21e 1744 defgv = gv_fetchpv("_",TRUE, SVt_PVAV);
12f917ad
MB
1745 errgv = gv_HVadd(gv_fetchpv("@", TRUE, SVt_PV));
1746 GvMULTI_on(errgv);
84902520 1747 (void)form("%240s",""); /* Preallocate temp - for immediate signals. */
38a03e6e
MB
1748 sv_grow(ERRSV, 240); /* Preallocate - for immediate signals. */
1749 sv_setpvn(ERRSV, "", 0);
8990e307
LW
1750 curstash = defstash;
1751 compiling.cop_stash = defstash;
adbc6bb1 1752 debstash = GvHV(gv_fetchpv("DB::", GV_ADDMULTI, SVt_PVHV));
49dc05e3 1753 globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDMULTI, SVt_PVHV));
4633a7c4
LW
1754 /* We must init $/ before switches are processed. */
1755 sv_setpvn(GvSV(gv_fetchpv("/", TRUE, SVt_PV)), "\n", 1);
79072805
LW
1756}
1757
a0d0e21e
LW
1758static void
1759open_script(char *scriptname, bool dosearch, SV *sv)
79072805 1760{
0f15f207 1761 dTHR;
79072805
LW
1762 char *xfound = Nullch;
1763 char *xfailed = Nullch;
1764 register char *s;
1765 I32 len;
a38d6535
LW
1766 int retval;
1767#if defined(DOSISH) && !defined(OS2) && !defined(atarist)
fc36a67e 1768# define SEARCH_EXTS ".bat", ".cmd", NULL
1769# define MAX_EXT_LEN 4
a38d6535 1770#endif
d8c2d278
IZ
1771#ifdef OS2
1772# define SEARCH_EXTS ".cmd", ".btm", ".bat", ".pl", NULL
1773# define MAX_EXT_LEN 4
1774#endif
ab821d7f 1775#ifdef VMS
1776# define SEARCH_EXTS ".pl", ".com", NULL
fc36a67e 1777# define MAX_EXT_LEN 4
ab821d7f 1778#endif
a38d6535
LW
1779 /* additional extensions to try in each dir if scriptname not found */
1780#ifdef SEARCH_EXTS
1781 char *ext[] = { SEARCH_EXTS };
2a92aaa0
GS
1782 int extidx = 0, i = 0;
1783 char *curext = Nullch;
fc36a67e 1784#else
1785# define MAX_EXT_LEN 0
a38d6535 1786#endif
79072805 1787
2a92aaa0
GS
1788 /*
1789 * If dosearch is true and if scriptname does not contain path
1790 * delimiters, search the PATH for scriptname.
1791 *
1792 * If SEARCH_EXTS is also defined, will look for each
1793 * scriptname{SEARCH_EXTS} whenever scriptname is not found
1794 * while searching the PATH.
1795 *
1796 * Assuming SEARCH_EXTS is C<".foo",".bar",NULL>, PATH search
1797 * proceeds as follows:
61bb5906 1798 * If DOSISH or VMSISH:
2a92aaa0
GS
1799 * + look for ./scriptname{,.foo,.bar}
1800 * + search the PATH for scriptname{,.foo,.bar}
1801 *
1802 * If !DOSISH:
1803 * + look *only* in the PATH for scriptname{,.foo,.bar} (note
1804 * this will not look in '.' if it's not in the PATH)
1805 */
1806
c07a80fd 1807#ifdef VMS
61bb5906
CB
1808# ifdef ALWAYS_DEFTYPES
1809 len = strlen(scriptname);
1810 if (!(len == 1 && *scriptname == '-') && scriptname[len-1] != ':') {
1811 int hasdir, idx = 0, deftypes = 1;
1812 bool seen_dot = 1;
1813
1814 hasdir = !dosearch || (strpbrk(scriptname,":[</") != Nullch) ;
1815# else
6e72f9df 1816 if (dosearch) {
1817 int hasdir, idx = 0, deftypes = 1;
1a2dec3c 1818 bool seen_dot = 1;
6e72f9df 1819
1820 hasdir = (strpbrk(scriptname,":[</") != Nullch) ;
61bb5906 1821# endif
6e72f9df 1822 /* The first time through, just add SEARCH_EXTS to whatever we
1823 * already have, so we can check for default file types. */
fc36a67e 1824 while (deftypes ||
1825 (!hasdir && my_trnlnm("DCL$PATH",tokenbuf,idx++)) )
1826 {
1827 if (deftypes) {
1828 deftypes = 0;
1829 *tokenbuf = '\0';
1830 }
1831 if ((strlen(tokenbuf) + strlen(scriptname)
1832 + MAX_EXT_LEN) >= sizeof tokenbuf)
1833 continue; /* don't search dir with too-long name */
1834 strcat(tokenbuf, scriptname);
c07a80fd 1835#else /* !VMS */
2a92aaa0 1836
fc36a67e 1837#ifdef DOSISH
2a92aaa0 1838 if (strEQ(scriptname, "-"))
84902520 1839 dosearch = 0;
2a92aaa0
GS
1840 if (dosearch) { /* Look in '.' first. */
1841 char *cur = scriptname;
1842#ifdef SEARCH_EXTS
1843 if ((curext = strrchr(scriptname,'.'))) /* possible current ext */
1844 while (ext[i])
1845 if (strEQ(ext[i++],curext)) {
1846 extidx = -1; /* already has an ext */
1847 break;
1848 }
1849 do {
79072805 1850#endif
2a92aaa0
GS
1851 DEBUG_p(PerlIO_printf(Perl_debug_log,
1852 "Looking for %s\n",cur));
1853 if (Stat(cur,&statbuf) >= 0) {
1854 dosearch = 0;
1855 scriptname = cur;
84902520 1856#ifdef SEARCH_EXTS
2a92aaa0 1857 break;
84902520 1858#endif
2a92aaa0
GS
1859 }
1860#ifdef SEARCH_EXTS
1861 if (cur == scriptname) {
1862 len = strlen(scriptname);
1863 if (len+MAX_EXT_LEN+1 >= sizeof(tokenbuf))
1864 break;
1865 cur = strcpy(tokenbuf, scriptname);
1866 }
1867 } while (extidx >= 0 && ext[extidx] /* try an extension? */
1868 && strcpy(tokenbuf+len, ext[extidx++]));
1869#endif
1870 }
1871#endif
84902520 1872
e92c4225
WJ
1873 if (dosearch && !strchr(scriptname, '/')
1874#ifdef DOSISH
1875 && !strchr(scriptname, '\\')
1876#endif
5fd9e9a4 1877 && (s = PerlEnv_getenv("PATH"))) {
2a92aaa0 1878 bool seen_dot = 0;
84902520 1879
79072805 1880 bufend = s + strlen(s);
fc36a67e 1881 while (s < bufend) {
2a92aaa0
GS
1882#if defined(atarist) || defined(DOSISH)
1883 for (len = 0; *s
1884# ifdef atarist
1885 && *s != ','
1886# endif
1887 && *s != ';'; len++, s++) {
fc36a67e 1888 if (len < sizeof tokenbuf)
1889 tokenbuf[len] = *s;
1890 }
1891 if (len < sizeof tokenbuf)
1892 tokenbuf[len] = '\0';
84902520
TB
1893#else /* ! (atarist || DOSISH) */
1894 s = delimcpy(tokenbuf, tokenbuf + sizeof tokenbuf, s, bufend,
1895 ':',
1896 &len);
1897#endif /* ! (atarist || DOSISH) */
fc36a67e 1898 if (s < bufend)
79072805 1899 s++;
fc36a67e 1900 if (len + 1 + strlen(scriptname) + MAX_EXT_LEN >= sizeof tokenbuf)
1901 continue; /* don't search dir with too-long name */
1902 if (len
fc36a67e 1903#if defined(atarist) || defined(DOSISH)
2a92aaa0 1904 && tokenbuf[len - 1] != '/'
fc36a67e 1905 && tokenbuf[len - 1] != '\\'
79072805 1906#endif
fc36a67e 1907 )
1908 tokenbuf[len++] = '/';
84902520 1909 if (len == 2 && tokenbuf[0] == '.')
2a92aaa0 1910 seen_dot = 1;
fc36a67e 1911 (void)strcpy(tokenbuf + len, scriptname);
c07a80fd 1912#endif /* !VMS */
a38d6535
LW
1913
1914#ifdef SEARCH_EXTS
1915 len = strlen(tokenbuf);
1916 if (extidx > 0) /* reset after previous loop */
1917 extidx = 0;
1918 do {
1919#endif
760ac839 1920 DEBUG_p(PerlIO_printf(Perl_debug_log, "Looking for %s\n",tokenbuf));
a38d6535
LW
1921 retval = Stat(tokenbuf,&statbuf);
1922#ifdef SEARCH_EXTS
1923 } while ( retval < 0 /* not there */
1924 && extidx>=0 && ext[extidx] /* try an extension? */
1925 && strcpy(tokenbuf+len, ext[extidx++])
1926 );
1927#endif
1928 if (retval < 0)
79072805
LW
1929 continue;
1930 if (S_ISREG(statbuf.st_mode)
c90c0ff4 1931 && cando(S_IRUSR,TRUE,&statbuf)
1932#ifndef DOSISH
1933 && cando(S_IXUSR,TRUE,&statbuf)
1934#endif
1935 )
1936 {
79072805
LW
1937 xfound = tokenbuf; /* bingo! */
1938 break;
1939 }
1940 if (!xfailed)
a0d0e21e 1941 xfailed = savepv(tokenbuf);
79072805 1942 }
2a92aaa0
GS
1943#ifndef DOSISH
1944 if (!xfound && !seen_dot && !xfailed && (Stat(scriptname,&statbuf) < 0))
84902520
TB
1945#endif
1946 seen_dot = 1; /* Disable message. */
79072805 1947 if (!xfound)
84902520 1948 croak("Can't %s %s%s%s",
2a92aaa0
GS
1949 (xfailed ? "execute" : "find"),
1950 (xfailed ? xfailed : scriptname),
1951 (xfailed ? "" : " on PATH"),
1952 (xfailed || seen_dot) ? "" : ", '.' not in PATH");
79072805
LW
1953 if (xfailed)
1954 Safefree(xfailed);
1955 scriptname = xfound;
1956 }
1957
96436eeb 1958 if (strnEQ(scriptname, "/dev/fd/", 8) && isDIGIT(scriptname[8]) ) {
1959 char *s = scriptname + 8;
1960 fdscript = atoi(s);
1961 while (isDIGIT(*s))
1962 s++;
1963 if (*s)
1964 scriptname = s + 1;
1965 }
1966 else
1967 fdscript = -1;
ab821d7f 1968 origfilename = savepv(e_tmpname ? "-e" : scriptname);
79072805
LW
1969 curcop->cop_filegv = gv_fetchfile(origfilename);
1970 if (strEQ(origfilename,"-"))
1971 scriptname = "";
96436eeb 1972 if (fdscript >= 0) {
a868473f 1973 rsfp = PerlIO_fdopen(fdscript,PERL_SCRIPT_MODE);
96436eeb 1974#if defined(HAS_FCNTL) && defined(F_SETFD)
7aa04957
CS
1975 if (rsfp)
1976 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
96436eeb 1977#endif
1978 }
1979 else if (preprocess) {
46fc3d4c 1980 char *cpp_cfg = CPPSTDIN;
1981 SV *cpp = NEWSV(0,0);
1982 SV *cmd = NEWSV(0,0);
1983
1984 if (strEQ(cpp_cfg, "cppstdin"))
1985 sv_catpvf(cpp, "%s/", BIN_EXP);
1986 sv_catpv(cpp, cpp_cfg);
79072805 1987
79072805 1988 sv_catpv(sv,"-I");
fed7345c 1989 sv_catpv(sv,PRIVLIB_EXP);
46fc3d4c 1990
79072805 1991#ifdef MSDOS
46fc3d4c 1992 sv_setpvf(cmd, "\
79072805
LW
1993sed %s -e \"/^[^#]/b\" \
1994 -e \"/^#[ ]*include[ ]/b\" \
1995 -e \"/^#[ ]*define[ ]/b\" \
1996 -e \"/^#[ ]*if[ ]/b\" \
1997 -e \"/^#[ ]*ifdef[ ]/b\" \
1998 -e \"/^#[ ]*ifndef[ ]/b\" \
1999 -e \"/^#[ ]*else/b\" \
2000 -e \"/^#[ ]*elif[ ]/b\" \
2001 -e \"/^#[ ]*undef[ ]/b\" \
2002 -e \"/^#[ ]*endif/b\" \
2003 -e \"s/^#.*//\" \
fc36a67e 2004 %s | %_ -C %_ %s",
79072805
LW
2005 (doextract ? "-e \"1,/^#/d\n\"" : ""),
2006#else
46fc3d4c 2007 sv_setpvf(cmd, "\
79072805
LW
2008%s %s -e '/^[^#]/b' \
2009 -e '/^#[ ]*include[ ]/b' \
2010 -e '/^#[ ]*define[ ]/b' \
2011 -e '/^#[ ]*if[ ]/b' \
2012 -e '/^#[ ]*ifdef[ ]/b' \
2013 -e '/^#[ ]*ifndef[ ]/b' \
2014 -e '/^#[ ]*else/b' \
2015 -e '/^#[ ]*elif[ ]/b' \
2016 -e '/^#[ ]*undef[ ]/b' \
2017 -e '/^#[ ]*endif/b' \
2018 -e 's/^[ ]*#.*//' \
fc36a67e 2019 %s | %_ -C %_ %s",
79072805
LW
2020#ifdef LOC_SED
2021 LOC_SED,
2022#else
2023 "sed",
2024#endif
2025 (doextract ? "-e '1,/^#/d\n'" : ""),
2026#endif
46fc3d4c 2027 scriptname, cpp, sv, CPPMINUS);
79072805
LW
2028 doextract = FALSE;
2029#ifdef IAMSUID /* actually, this is caught earlier */
2030 if (euid != uid && !euid) { /* if running suidperl */
2031#ifdef HAS_SETEUID
2032 (void)seteuid(uid); /* musn't stay setuid root */
2033#else
2034#ifdef HAS_SETREUID
85e6fe83
LW
2035 (void)setreuid((Uid_t)-1, uid);
2036#else
2037#ifdef HAS_SETRESUID
2038 (void)setresuid((Uid_t)-1, uid, (Uid_t)-1);
79072805
LW
2039#else
2040 setuid(uid);
2041#endif
2042#endif
85e6fe83 2043#endif
79072805 2044 if (geteuid() != uid)
463ee0b2 2045 croak("Can't do seteuid!\n");
79072805
LW
2046 }
2047#endif /* IAMSUID */
3028581b 2048 rsfp = PerlProc_popen(SvPVX(cmd), "r");
46fc3d4c 2049 SvREFCNT_dec(cmd);
2050 SvREFCNT_dec(cpp);
79072805
LW
2051 }
2052 else if (!*scriptname) {
bbce6d69 2053 forbid_setid("program input from stdin");
760ac839 2054 rsfp = PerlIO_stdin();
79072805 2055 }
96436eeb 2056 else {
a868473f 2057 rsfp = PerlIO_open(scriptname,PERL_SCRIPT_MODE);
96436eeb 2058#if defined(HAS_FCNTL) && defined(F_SETFD)
7aa04957
CS
2059 if (rsfp)
2060 fcntl(PerlIO_fileno(rsfp),F_SETFD,1); /* ensure close-on-exec */
96436eeb 2061#endif
2062 }
5dd60ef7 2063 if (e_tmpname) {
2064 e_fp = rsfp;
2065 }
7aa04957 2066 if (!rsfp) {
13281fa4 2067#ifdef DOSUID
a687059c 2068#ifndef IAMSUID /* in case script is not readable before setuid */
a0d0e21e 2069 if (euid && Stat(SvPVX(GvSV(curcop->cop_filegv)),&statbuf) >= 0 &&
13281fa4 2070 statbuf.st_mode & (S_ISUID|S_ISGID)) {
46fc3d4c 2071 /* try again */
3028581b 2072 PerlProc_execv(form("%s/sperl%s", BIN_EXP, patchlevel), origargv);
463ee0b2 2073 croak("Can't do setuid\n");
13281fa4
LW
2074 }
2075#endif
2076#endif
463ee0b2 2077 croak("Can't open perl script \"%s\": %s\n",
2304df62 2078 SvPVX(GvSV(curcop->cop_filegv)), Strerror(errno));
13281fa4 2079 }
79072805 2080}
8d063cd8 2081
79072805 2082static void
8ac85365 2083validate_suid(char *validarg, char *scriptname)
79072805 2084{
96436eeb 2085 int which;
2086
13281fa4
LW
2087 /* do we need to emulate setuid on scripts? */
2088
2089 /* This code is for those BSD systems that have setuid #! scripts disabled
2090 * in the kernel because of a security problem. Merely defining DOSUID
2091 * in perl will not fix that problem, but if you have disabled setuid
2092 * scripts in the kernel, this will attempt to emulate setuid and setgid
2093 * on scripts that have those now-otherwise-useless bits set. The setuid
27e2fb84
LW
2094 * root version must be called suidperl or sperlN.NNN. If regular perl
2095 * discovers that it has opened a setuid script, it calls suidperl with
2096 * the same argv that it had. If suidperl finds that the script it has
2097 * just opened is NOT setuid root, it sets the effective uid back to the
2098 * uid. We don't just make perl setuid root because that loses the
2099 * effective uid we had before invoking perl, if it was different from the
2100 * uid.
13281fa4
LW
2101 *
2102 * DOSUID must be defined in both perl and suidperl, and IAMSUID must
2103 * be defined in suidperl only. suidperl must be setuid root. The
2104 * Configure script will set this up for you if you want it.
2105 */
a687059c 2106
13281fa4 2107#ifdef DOSUID
ea0efc06 2108 dTHR;
6e72f9df 2109 char *s, *s2;
a0d0e21e 2110
3028581b 2111 if (PerlLIO_fstat(PerlIO_fileno(rsfp),&statbuf) < 0) /* normal stat is insecure */
463ee0b2 2112 croak("Can't stat script \"%s\"",origfilename);
96436eeb 2113 if (fdscript < 0 && statbuf.st_mode & (S_ISUID|S_ISGID)) {
79072805 2114 I32 len;
13281fa4 2115
a687059c 2116#ifdef IAMSUID
fe14fcc3 2117#ifndef HAS_SETREUID
a687059c
LW
2118 /* On this access check to make sure the directories are readable,
2119 * there is actually a small window that the user could use to make
2120 * filename point to an accessible directory. So there is a faint
2121 * chance that someone could execute a setuid script down in a
2122 * non-accessible directory. I don't know what to do about that.
2123 * But I don't think it's too important. The manual lies when
2124 * it says access() is useful in setuid programs.
2125 */
3028581b 2126 if (PerlLIO_access(SvPVX(GvSV(curcop->cop_filegv)),1)) /*double check*/
463ee0b2 2127 croak("Permission denied");
a687059c
LW
2128#else
2129 /* If we can swap euid and uid, then we can determine access rights
2130 * with a simple stat of the file, and then compare device and
2131 * inode to make sure we did stat() on the same file we opened.
2132 * Then we just have to make sure he or she can execute it.
2133 */
2134 {
2135 struct stat tmpstatbuf;
2136
85e6fe83
LW
2137 if (
2138#ifdef HAS_SETREUID
2139 setreuid(euid,uid) < 0
a0d0e21e
LW
2140#else
2141# if HAS_SETRESUID
85e6fe83 2142 setresuid(euid,uid,(Uid_t)-1) < 0
a0d0e21e 2143# endif
85e6fe83
LW
2144#endif
2145 || getuid() != euid || geteuid() != uid)
463ee0b2 2146 croak("Can't swap uid and euid"); /* really paranoid */
a0d0e21e 2147 if (Stat(SvPVX(GvSV(curcop->cop_filegv)),&tmpstatbuf) < 0)
463ee0b2 2148 croak("Permission denied"); /* testing full pathname here */
a687059c
LW
2149 if (tmpstatbuf.st_dev != statbuf.st_dev ||
2150 tmpstatbuf.st_ino != statbuf.st_ino) {
760ac839 2151 (void)PerlIO_close(rsfp);
3028581b 2152 if (rsfp = PerlProc_popen("/bin/mail root","w")) { /* heh, heh */
760ac839 2153 PerlIO_printf(rsfp,
ff0cee69 2154"User %ld tried to run dev %ld ino %ld in place of dev %ld ino %ld!\n\
2155(Filename of set-id script was %s, uid %ld gid %ld.)\n\nSincerely,\nperl\n",
2156 (long)uid,(long)tmpstatbuf.st_dev, (long)tmpstatbuf.st_ino,
2157 (long)statbuf.st_dev, (long)statbuf.st_ino,
463ee0b2 2158 SvPVX(GvSV(curcop->cop_filegv)),
ff0cee69 2159 (long)statbuf.st_uid, (long)statbuf.st_gid);
3028581b 2160 (void)PerlProc_pclose(rsfp);
a687059c 2161 }
463ee0b2 2162 croak("Permission denied\n");
a687059c 2163 }
85e6fe83
LW
2164 if (
2165#ifdef HAS_SETREUID
2166 setreuid(uid,euid) < 0
a0d0e21e
LW
2167#else
2168# if defined(HAS_SETRESUID)
85e6fe83 2169 setresuid(uid,euid,(Uid_t)-1) < 0
a0d0e21e 2170# endif
85e6fe83
LW
2171#endif
2172 || getuid() != uid || geteuid() != euid)
463ee0b2 2173 croak("Can't reswap uid and euid");
27e2fb84 2174 if (!cando(S_IXUSR,FALSE,&statbuf)) /* can real uid exec? */
463ee0b2 2175 croak("Permission denied\n");
a687059c 2176 }
fe14fcc3 2177#endif /* HAS_SETREUID */
a687059c
LW
2178#endif /* IAMSUID */
2179
27e2fb84 2180 if (!S_ISREG(statbuf.st_mode))
463ee0b2 2181 croak("Permission denied");
27e2fb84 2182 if (statbuf.st_mode & S_IWOTH)
463ee0b2 2183 croak("Setuid/gid script is writable by world");
13281fa4 2184 doswitches = FALSE; /* -s is insecure in suid */
79072805 2185 curcop->cop_line++;
760ac839
LW
2186 if (sv_gets(linestr, rsfp, 0) == Nullch ||
2187 strnNE(SvPV(linestr,na),"#!",2) ) /* required even on Sys V */
463ee0b2 2188 croak("No #! line");
760ac839 2189 s = SvPV(linestr,na)+2;
663a0e37 2190 if (*s == ' ') s++;
45d8adaa 2191 while (!isSPACE(*s)) s++;
760ac839 2192 for (s2 = s; (s2 > SvPV(linestr,na)+2 &&
6e72f9df 2193 (isDIGIT(s2[-1]) || strchr("._-", s2[-1]))); s2--) ;
2194 if (strnNE(s2-4,"perl",4) && strnNE(s-9,"perl",4)) /* sanity check */
463ee0b2 2195 croak("Not a perl script");
a687059c 2196 while (*s == ' ' || *s == '\t') s++;
13281fa4
LW
2197 /*
2198 * #! arg must be what we saw above. They can invoke it by
2199 * mentioning suidperl explicitly, but they may not add any strange
2200 * arguments beyond what #! says if they do invoke suidperl that way.
2201 */
2202 len = strlen(validarg);
2203 if (strEQ(validarg," PHOOEY ") ||
45d8adaa 2204 strnNE(s,validarg,len) || !isSPACE(s[len]))
463ee0b2 2205 croak("Args must match #! line");
a687059c
LW
2206
2207#ifndef IAMSUID
2208 if (euid != uid && (statbuf.st_mode & S_ISUID) &&
2209 euid == statbuf.st_uid)
2210 if (!do_undump)
463ee0b2 2211 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
a687059c
LW
2212FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
2213#endif /* IAMSUID */
13281fa4
LW
2214
2215 if (euid) { /* oops, we're not the setuid root perl */
760ac839 2216 (void)PerlIO_close(rsfp);
13281fa4 2217#ifndef IAMSUID
46fc3d4c 2218 /* try again */
3028581b 2219 PerlProc_execv(form("%s/sperl%s", BIN_EXP, patchlevel), origargv);
13281fa4 2220#endif
463ee0b2 2221 croak("Can't do setuid\n");
13281fa4
LW
2222 }
2223
83025b21 2224 if (statbuf.st_mode & S_ISGID && statbuf.st_gid != egid) {
fe14fcc3 2225#ifdef HAS_SETEGID
a687059c
LW
2226 (void)setegid(statbuf.st_gid);
2227#else
fe14fcc3 2228#ifdef HAS_SETREGID
85e6fe83
LW
2229 (void)setregid((Gid_t)-1,statbuf.st_gid);
2230#else
2231#ifdef HAS_SETRESGID
2232 (void)setresgid((Gid_t)-1,statbuf.st_gid,(Gid_t)-1);
a687059c
LW
2233#else
2234 setgid(statbuf.st_gid);
2235#endif
2236#endif
85e6fe83 2237#endif
83025b21 2238 if (getegid() != statbuf.st_gid)
463ee0b2 2239 croak("Can't do setegid!\n");
83025b21 2240 }
a687059c
LW
2241 if (statbuf.st_mode & S_ISUID) {
2242 if (statbuf.st_uid != euid)
fe14fcc3 2243#ifdef HAS_SETEUID
a687059c
LW
2244 (void)seteuid(statbuf.st_uid); /* all that for this */
2245#else
fe14fcc3 2246#ifdef HAS_SETREUID
85e6fe83
LW
2247 (void)setreuid((Uid_t)-1,statbuf.st_uid);
2248#else
2249#ifdef HAS_SETRESUID
2250 (void)setresuid((Uid_t)-1,statbuf.st_uid,(Uid_t)-1);
a687059c
LW
2251#else
2252 setuid(statbuf.st_uid);
2253#endif
2254#endif
85e6fe83 2255#endif
83025b21 2256 if (geteuid() != statbuf.st_uid)
463ee0b2 2257 croak("Can't do seteuid!\n");
a687059c 2258 }
83025b21 2259 else if (uid) { /* oops, mustn't run as root */
fe14fcc3 2260#ifdef HAS_SETEUID
85e6fe83 2261 (void)seteuid((Uid_t)uid);
a687059c 2262#else
fe14fcc3 2263#ifdef HAS_SETREUID
85e6fe83 2264 (void)setreuid((Uid_t)-1,(Uid_t)uid);
a687059c 2265#else
85e6fe83
LW
2266#ifdef HAS_SETRESUID
2267 (void)setresuid((Uid_t)-1,(Uid_t)uid,(Uid_t)-1);
2268#else
2269 setuid((Uid_t)uid);
2270#endif
a687059c
LW
2271#endif
2272#endif
83025b21 2273 if (geteuid() != uid)
463ee0b2 2274 croak("Can't do seteuid!\n");
83025b21 2275 }
748a9306 2276 init_ids();
27e2fb84 2277 if (!cando(S_IXUSR,TRUE,&statbuf))
463ee0b2 2278 croak("Permission denied\n"); /* they can't do this */
13281fa4
LW
2279 }
2280#ifdef IAMSUID
2281 else if (preprocess)
463ee0b2 2282 croak("-P not allowed for setuid/setgid script\n");
96436eeb 2283 else if (fdscript >= 0)
2284 croak("fd script not allowed in suidperl\n");
13281fa4 2285 else
463ee0b2 2286 croak("Script is not setuid/setgid in suidperl\n");
96436eeb 2287
2288 /* We absolutely must clear out any saved ids here, so we */
2289 /* exec the real perl, substituting fd script for scriptname. */
2290 /* (We pass script name as "subdir" of fd, which perl will grok.) */
760ac839 2291 PerlIO_rewind(rsfp);
3028581b 2292 PerlLIO_lseek(PerlIO_fileno(rsfp),(Off_t)0,0); /* just in case rewind didn't */
96436eeb 2293 for (which = 1; origargv[which] && origargv[which] != scriptname; which++) ;
2294 if (!origargv[which])
2295 croak("Permission denied");
46fc3d4c 2296 origargv[which] = savepv(form("/dev/fd/%d/%s",
2297 PerlIO_fileno(rsfp), origargv[which]));
96436eeb 2298#if defined(HAS_FCNTL) && defined(F_SETFD)
760ac839 2299 fcntl(PerlIO_fileno(rsfp),F_SETFD,0); /* ensure no close-on-exec */
96436eeb 2300#endif
3028581b 2301 PerlProc_execv(form("%s/perl%s", BIN_EXP, patchlevel), origargv); /* try again */
96436eeb 2302 croak("Can't do setuid\n");
13281fa4 2303#endif /* IAMSUID */
a687059c 2304#else /* !DOSUID */
a687059c
LW
2305 if (euid != uid || egid != gid) { /* (suidperl doesn't exist, in fact) */
2306#ifndef SETUID_SCRIPTS_ARE_SECURE_NOW
96827780 2307 dTHR;
3028581b 2308 PerlLIO_fstat(PerlIO_fileno(rsfp),&statbuf); /* may be either wrapped or real suid */
a687059c
LW
2309 if ((euid != uid && euid == statbuf.st_uid && statbuf.st_mode & S_ISUID)
2310 ||
2311 (egid != gid && egid == statbuf.st_gid && statbuf.st_mode & S_ISGID)
2312 )
2313 if (!do_undump)
463ee0b2 2314 croak("YOU HAVEN'T DISABLED SET-ID SCRIPTS IN THE KERNEL YET!\n\
a687059c
LW
2315FIX YOUR KERNEL, PUT A C WRAPPER AROUND THIS SCRIPT, OR USE -u AND UNDUMP!\n");
2316#endif /* SETUID_SCRIPTS_ARE_SECURE_NOW */
2317 /* not set-id, must be wrapped */
a687059c 2318 }
13281fa4 2319#endif /* DOSUID */
79072805 2320}
13281fa4 2321
79072805 2322static void
8ac85365 2323find_beginning(void)
79072805 2324{
6e72f9df 2325 register char *s, *s2;
33b78306
LW
2326
2327 /* skip forward in input to the real script? */
2328
bbce6d69 2329 forbid_setid("-x");
33b78306 2330 while (doextract) {
79072805 2331 if ((s = sv_gets(linestr, rsfp, 0)) == Nullch)
463ee0b2 2332 croak("No Perl script found in input\n");
6e72f9df 2333 if (*s == '#' && s[1] == '!' && (s = instr(s,"perl"))) {
760ac839 2334 PerlIO_ungetc(rsfp, '\n'); /* to keep line count right */
33b78306 2335 doextract = FALSE;
6e72f9df 2336 while (*s && !(isSPACE (*s) || *s == '#')) s++;
2337 s2 = s;
2338 while (*s == ' ' || *s == '\t') s++;
2339 if (*s++ == '-') {
2340 while (isDIGIT(s2[-1]) || strchr("-._", s2[-1])) s2--;
2341 if (strnEQ(s2-4,"perl",4))
2342 /*SUPPRESS 530*/
2343 while (s = moreswitches(s)) ;
33b78306 2344 }
3028581b 2345 if (cddir && PerlDir_chdir(cddir) < 0)
463ee0b2 2346 croak("Can't chdir to %s",cddir);
83025b21
LW
2347 }
2348 }
2349}
2350
79072805 2351static void
8ac85365 2352init_ids(void)
352d5a3a 2353{
748a9306
LW
2354 uid = (int)getuid();
2355 euid = (int)geteuid();
2356 gid = (int)getgid();
2357 egid = (int)getegid();
2358#ifdef VMS
2359 uid |= gid << 16;
2360 euid |= egid << 16;
2361#endif
4633a7c4 2362 tainting |= (uid && (euid != uid || egid != gid));
748a9306 2363}
79072805 2364
748a9306 2365static void
8ac85365 2366forbid_setid(char *s)
bbce6d69 2367{
2368 if (euid != uid)
2369 croak("No %s allowed while running setuid", s);
2370 if (egid != gid)
2371 croak("No %s allowed while running setgid", s);
2372}
2373
2374static void
8ac85365 2375init_debugger(void)
748a9306 2376{
11343788 2377 dTHR;
79072805 2378 curstash = debstash;
748a9306 2379 dbargs = GvAV(gv_AVadd((gv_fetchpv("args", GV_ADDMULTI, SVt_PVAV))));
79072805 2380 AvREAL_off(dbargs);
a0d0e21e
LW
2381 DBgv = gv_fetchpv("DB", GV_ADDMULTI, SVt_PVGV);
2382 DBline = gv_fetchpv("dbline", GV_ADDMULTI, SVt_PVAV);
748a9306
LW
2383 DBsub = gv_HVadd(gv_fetchpv("sub", GV_ADDMULTI, SVt_PVHV));
2384 DBsingle = GvSV((gv_fetchpv("single", GV_ADDMULTI, SVt_PV)));
c07a80fd 2385 sv_setiv(DBsingle, 0);
748a9306 2386 DBtrace = GvSV((gv_fetchpv("trace", GV_ADDMULTI, SVt_PV)));
c07a80fd 2387 sv_setiv(DBtrace, 0);
748a9306 2388 DBsignal = GvSV((gv_fetchpv("signal", GV_ADDMULTI, SVt_PV)));
c07a80fd 2389 sv_setiv(DBsignal, 0);
79072805 2390 curstash = defstash;
352d5a3a
LW
2391}
2392
11343788 2393void
8ac85365 2394init_stacks(ARGSproto)
79072805 2395{
6e72f9df 2396 curstack = newAV();
5f05dabc 2397 mainstack = curstack; /* remember in case we switch stacks */
2398 AvREAL_off(curstack); /* not a real array */
6e72f9df 2399 av_extend(curstack,127);
79072805 2400
6e72f9df 2401 stack_base = AvARRAY(curstack);
79072805 2402 stack_sp = stack_base;
8990e307 2403 stack_max = stack_base + 127;
79072805 2404
c09156bb
MB
2405 cxstack_max = 8192 / sizeof(PERL_CONTEXT) - 2; /* Use most of 8K. */
2406 New(50,cxstack,cxstack_max + 1,PERL_CONTEXT);
8990e307 2407 cxstack_ix = -1;
8990e307
LW
2408
2409 New(50,tmps_stack,128,SV*);
6d4ff0d2 2410 tmps_floor = -1;
8990e307
LW
2411 tmps_ix = -1;
2412 tmps_max = 128;
2413
5f05dabc 2414 /*
2415 * The following stacks almost certainly should be per-interpreter,
2416 * but for now they're not. XXX
2417 */
2418
6e72f9df 2419 if (markstack) {
2420 markstack_ptr = markstack;
2421 } else {
2422 New(54,markstack,64,I32);
2423 markstack_ptr = markstack;
2424 markstack_max = markstack + 64;
2425 }
79072805 2426
6e72f9df 2427 if (scopestack) {
2428 scopestack_ix = 0;
2429 } else {
2430 New(54,scopestack,32,I32);
2431 scopestack_ix = 0;
2432 scopestack_max = 32;
2433 }
79072805 2434
6e72f9df 2435 if (savestack) {
2436 savestack_ix = 0;
2437 } else {
2438 New(54,savestack,128,ANY);
2439 savestack_ix = 0;
2440 savestack_max = 128;
2441 }
79072805 2442
6e72f9df 2443 if (retstack) {
2444 retstack_ix = 0;
2445 } else {
2446 New(54,retstack,16,OP*);
2447 retstack_ix = 0;
2448 retstack_max = 16;
5f05dabc 2449 }
378cc40b 2450}
33b78306 2451
6e72f9df 2452static void
8ac85365 2453nuke_stacks(void)
6e72f9df 2454{
e858de61 2455 dTHR;
6e72f9df 2456 Safefree(cxstack);
2457 Safefree(tmps_stack);
5f05dabc 2458 DEBUG( {
2459 Safefree(debname);
2460 Safefree(debdelim);
2461 } )
378cc40b 2462}
33b78306 2463
760ac839 2464static PerlIO *tmpfp; /* moved outside init_lexer() because of UNICOS bug */
7aa04957 2465
79072805 2466static void
8ac85365 2467init_lexer(void)
8990e307 2468{
a0d0e21e 2469 tmpfp = rsfp;
90248788 2470 rsfp = Nullfp;
8990e307
LW
2471 lex_start(linestr);
2472 rsfp = tmpfp;
2473 subname = newSVpv("main",4);
2474}
2475
2476static void
8ac85365 2477init_predump_symbols(void)
45d8adaa 2478{
11343788 2479 dTHR;
93a17b20 2480 GV *tmpgv;
a0d0e21e 2481 GV *othergv;
79072805 2482
e1c148c2 2483 sv_setpvn(perl_get_sv("\"", TRUE), " ", 1);
85e6fe83 2484 stdingv = gv_fetchpv("STDIN",TRUE, SVt_PVIO);
a5f75d66 2485 GvMULTI_on(stdingv);
760ac839 2486 IoIFP(GvIOp(stdingv)) = PerlIO_stdin();
adbc6bb1 2487 tmpgv = gv_fetchpv("stdin",TRUE, SVt_PV);
a5f75d66 2488 GvMULTI_on(tmpgv);
a0d0e21e 2489 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(stdingv));
79072805 2490
85e6fe83 2491 tmpgv = gv_fetchpv("STDOUT",TRUE, SVt_PVIO);
a5f75d66 2492 GvMULTI_on(tmpgv);
760ac839 2493 IoOFP(GvIOp(tmpgv)) = IoIFP(GvIOp(tmpgv)) = PerlIO_stdout();
4633a7c4 2494 setdefout(tmpgv);
adbc6bb1 2495 tmpgv = gv_fetchpv("stdout",TRUE, SVt_PV);
a5f75d66 2496 GvMULTI_on(tmpgv);
a0d0e21e 2497 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(defoutgv));
79072805 2498
a0d0e21e 2499 othergv = gv_fetchpv("STDERR",TRUE, SVt_PVIO);
a5f75d66 2500 GvMULTI_on(othergv);
760ac839 2501 IoOFP(GvIOp(othergv)) = IoIFP(GvIOp(othergv)) = PerlIO_stderr();
adbc6bb1 2502 tmpgv = gv_fetchpv("stderr",TRUE, SVt_PV);
a5f75d66 2503 GvMULTI_on(tmpgv);
a0d0e21e 2504 GvIOp(tmpgv) = (IO*)SvREFCNT_inc(GvIOp(othergv));
79072805
LW
2505
2506 statname = NEWSV(66,0); /* last filename we did stat on */
ab821d7f 2507
6e72f9df 2508 if (!osname)
2509 osname = savepv(OSNAME);
79072805 2510}
33b78306 2511
79072805 2512static void
8ac85365 2513init_postdump_symbols(register int argc, register char **argv, register char **env)
33b78306 2514{
a863c7d1 2515 dTHR;
79072805
LW
2516 char *s;
2517 SV *sv;
2518 GV* tmpgv;
fe14fcc3 2519
79072805
LW
2520 argc--,argv++; /* skip name of script */
2521 if (doswitches) {
2522 for (; argc > 0 && **argv == '-'; argc--,argv++) {
2523 if (!argv[0][1])
2524 break;
2525 if (argv[0][1] == '-') {
2526 argc--,argv++;
2527 break;
2528 }
93a17b20 2529 if (s = strchr(argv[0], '=')) {
79072805 2530 *s++ = '\0';
85e6fe83 2531 sv_setpv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),s);
79072805
LW
2532 }
2533 else
85e6fe83 2534 sv_setiv(GvSV(gv_fetchpv(argv[0]+1,TRUE, SVt_PV)),1);
fe14fcc3 2535 }
79072805
LW
2536 }
2537 toptarget = NEWSV(0,0);
2538 sv_upgrade(toptarget, SVt_PVFM);
2539 sv_setpvn(toptarget, "", 0);
748a9306 2540 bodytarget = NEWSV(0,0);
79072805
LW
2541 sv_upgrade(bodytarget, SVt_PVFM);
2542 sv_setpvn(bodytarget, "", 0);
2543 formtarget = bodytarget;
2544
bbce6d69 2545 TAINT;
85e6fe83 2546 if (tmpgv = gv_fetchpv("0",TRUE, SVt_PV)) {
79072805
LW
2547 sv_setpv(GvSV(tmpgv),origfilename);
2548 magicname("0", "0", 1);
2549 }
85e6fe83 2550 if (tmpgv = gv_fetchpv("\030",TRUE, SVt_PV))
79072805 2551 sv_setpv(GvSV(tmpgv),origargv[0]);
85e6fe83 2552 if (argvgv = gv_fetchpv("ARGV",TRUE, SVt_PVAV)) {
a5f75d66 2553 GvMULTI_on(argvgv);
79072805
LW
2554 (void)gv_AVadd(argvgv);
2555 av_clear(GvAVn(argvgv));
2556 for (; argc > 0; argc--,argv++) {
a0d0e21e 2557 av_push(GvAVn(argvgv),newSVpv(argv[0],0));
79072805
LW
2558 }
2559 }
85e6fe83 2560 if (envgv = gv_fetchpv("ENV",TRUE, SVt_PVHV)) {
79072805 2561 HV *hv;
a5f75d66 2562 GvMULTI_on(envgv);
79072805 2563 hv = GvHVn(envgv);
5aabfad6 2564 hv_magic(hv, envgv, 'E');
a0d0e21e 2565#ifndef VMS /* VMS doesn't have environ array */
4633a7c4
LW
2566 /* Note that if the supplied env parameter is actually a copy
2567 of the global environ then it may now point to free'd memory
2568 if the environment has been modified since. To avoid this
2569 problem we treat env==NULL as meaning 'use the default'
2570 */
2571 if (!env)
2572 env = environ;
5aabfad6 2573 if (env != environ)
79072805
LW
2574 environ[0] = Nullch;
2575 for (; *env; env++) {
93a17b20 2576 if (!(s = strchr(*env,'=')))
79072805
LW
2577 continue;
2578 *s++ = '\0';
39e571d4 2579#if defined(WIN32) || defined(MSDOS)
137443ea 2580 (void)strupr(*env);
2581#endif
79072805
LW
2582 sv = newSVpv(s--,0);
2583 (void)hv_store(hv, *env, s - *env, sv, 0);
2584 *s = '=';
3e3baf6d
TB
2585#if defined(__BORLANDC__) && defined(USE_WIN32_RTL_ENV)
2586 /* Sins of the RTL. See note in my_setenv(). */
5fd9e9a4 2587 (void)PerlEnv_putenv(savepv(*env));
3e3baf6d 2588#endif
fe14fcc3 2589 }
4550b24a 2590#endif
2591#ifdef DYNAMIC_ENV_FETCH
2592 HvNAME(hv) = savepv(ENV_HV_NAME);
2593#endif
79072805 2594 }
bbce6d69 2595 TAINT_NOT;
85e6fe83 2596 if (tmpgv = gv_fetchpv("$",TRUE, SVt_PV))
1e422769 2597 sv_setiv(GvSV(tmpgv), (IV)getpid());
33b78306 2598}
34de22dd 2599
79072805 2600static void
8ac85365 2601init_perllib(void)
34de22dd 2602{
85e6fe83
LW
2603 char *s;
2604 if (!tainting) {
552a7a9b 2605#ifndef VMS
5fd9e9a4 2606 s = PerlEnv_getenv("PERL5LIB");
85e6fe83 2607 if (s)
774d564b 2608 incpush(s, TRUE);
85e6fe83 2609 else
5fd9e9a4 2610 incpush(PerlEnv_getenv("PERLLIB"), FALSE);
552a7a9b 2611#else /* VMS */
2612 /* Treat PERL5?LIB as a possible search list logical name -- the
2613 * "natural" VMS idiom for a Unix path string. We allow each
2614 * element to be a set of |-separated directories for compatibility.
2615 */
2616 char buf[256];
2617 int idx = 0;
2618 if (my_trnlnm("PERL5LIB",buf,0))
774d564b 2619 do { incpush(buf,TRUE); } while (my_trnlnm("PERL5LIB",buf,++idx));
552a7a9b 2620 else
774d564b 2621 while (my_trnlnm("PERLLIB",buf,idx++)) incpush(buf,FALSE);
552a7a9b 2622#endif /* VMS */
85e6fe83 2623 }
34de22dd 2624
c90c0ff4 2625/* Use the ~-expanded versions of APPLLIB (undocumented),
dfe9444c 2626 ARCHLIB PRIVLIB SITEARCH and SITELIB
df5cef82 2627*/
4633a7c4 2628#ifdef APPLLIB_EXP
774d564b 2629 incpush(APPLLIB_EXP, FALSE);
16d20bd9 2630#endif
4633a7c4 2631
fed7345c 2632#ifdef ARCHLIB_EXP
774d564b 2633 incpush(ARCHLIB_EXP, FALSE);
a0d0e21e 2634#endif
fed7345c
AD
2635#ifndef PRIVLIB_EXP
2636#define PRIVLIB_EXP "/usr/local/lib/perl5:/usr/local/lib/perl"
34de22dd 2637#endif
774d564b 2638 incpush(PRIVLIB_EXP, FALSE);
4633a7c4
LW
2639
2640#ifdef SITEARCH_EXP
774d564b 2641 incpush(SITEARCH_EXP, FALSE);
4633a7c4
LW
2642#endif
2643#ifdef SITELIB_EXP
774d564b 2644 incpush(SITELIB_EXP, FALSE);
4633a7c4 2645#endif
4633a7c4 2646 if (!tainting)
774d564b 2647 incpush(".", FALSE);
2648}
2649
2650#if defined(DOSISH)
2651# define PERLLIB_SEP ';'
2652#else
2653# if defined(VMS)
2654# define PERLLIB_SEP '|'
2655# else
2656# define PERLLIB_SEP ':'
2657# endif
2658#endif
2659#ifndef PERLLIB_MANGLE
2660# define PERLLIB_MANGLE(s,n) (s)
2661#endif
2662
2663static void
8ac85365 2664incpush(char *p, int addsubdirs)
774d564b 2665{
2666 SV *subdir = Nullsv;
2667 static char *archpat_auto;
2668
2669 if (!p)
2670 return;
2671
2672 if (addsubdirs) {
8c52afec 2673 subdir = NEWSV(55,0);
774d564b 2674 if (!archpat_auto) {
2675 STRLEN len = (sizeof(ARCHNAME) + strlen(patchlevel)
2676 + sizeof("//auto"));
2677 New(55, archpat_auto, len, char);
2678 sprintf(archpat_auto, "/%s/%s/auto", ARCHNAME, patchlevel);
aa689395 2679#ifdef VMS
2680 for (len = sizeof(ARCHNAME) + 2;
2681 archpat_auto[len] != '\0' && archpat_auto[len] != '/'; len++)
2682 if (archpat_auto[len] == '.') archpat_auto[len] = '_';
2683#endif
774d564b 2684 }
2685 }
2686
2687 /* Break at all separators */
2688 while (p && *p) {
8c52afec 2689 SV *libdir = NEWSV(55,0);
774d564b 2690 char *s;
2691
2692 /* skip any consecutive separators */
2693 while ( *p == PERLLIB_SEP ) {
2694 /* Uncomment the next line for PATH semantics */
2695 /* av_push(GvAVn(incgv), newSVpv(".", 1)); */
2696 p++;
2697 }
2698
2699 if ( (s = strchr(p, PERLLIB_SEP)) != Nullch ) {
2700 sv_setpvn(libdir, PERLLIB_MANGLE(p, (STRLEN)(s - p)),
2701 (STRLEN)(s - p));
2702 p = s + 1;
2703 }
2704 else {
2705 sv_setpv(libdir, PERLLIB_MANGLE(p, 0));
2706 p = Nullch; /* break out */
2707 }
2708
2709 /*
2710 * BEFORE pushing libdir onto @INC we may first push version- and
2711 * archname-specific sub-directories.
2712 */
2713 if (addsubdirs) {
2714 struct stat tmpstatbuf;
aa689395 2715#ifdef VMS
2716 char *unix;
2717 STRLEN len;
774d564b 2718
aa689395 2719 if ((unix = tounixspec_ts(SvPV(libdir,na),Nullch)) != Nullch) {
2720 len = strlen(unix);
2721 while (unix[len-1] == '/') len--; /* Cosmetic */
2722 sv_usepvn(libdir,unix,len);
2723 }
2724 else
2725 PerlIO_printf(PerlIO_stderr(),
2726 "Failed to unixify @INC element \"%s\"\n",
2727 SvPV(libdir,na));
2728#endif
4fdae800 2729 /* .../archname/version if -d .../archname/version/auto */
774d564b 2730 sv_setsv(subdir, libdir);
2731 sv_catpv(subdir, archpat_auto);
2732 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2733 S_ISDIR(tmpstatbuf.st_mode))
2734 av_push(GvAVn(incgv),
2735 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2736
4fdae800 2737 /* .../archname if -d .../archname/auto */
774d564b 2738 sv_insert(subdir, SvCUR(libdir) + sizeof(ARCHNAME),
2739 strlen(patchlevel) + 1, "", 0);
2740 if (Stat(SvPVX(subdir), &tmpstatbuf) >= 0 &&
2741 S_ISDIR(tmpstatbuf.st_mode))
2742 av_push(GvAVn(incgv),
2743 newSVpv(SvPVX(subdir), SvCUR(subdir) - sizeof "auto"));
2744 }
2745
2746 /* finally push this lib directory on the end of @INC */
2747 av_push(GvAVn(incgv), libdir);
2748 }
2749
2750 SvREFCNT_dec(subdir);
34de22dd 2751}
93a17b20 2752
199100c8 2753#ifdef USE_THREADS
52e1cb5e 2754static struct perl_thread *
199100c8
MB
2755init_main_thread()
2756{
52e1cb5e 2757 struct perl_thread *thr;
199100c8
MB
2758 XPV *xpv;
2759
52e1cb5e 2760 Newz(53, thr, 1, struct perl_thread);
199100c8
MB
2761 curcop = &compiling;
2762 thr->cvcache = newHV();
54b9620d 2763 thr->threadsv = newAV();
940cb80d 2764 /* thr->threadsvp is set when find_threadsv is called */
199100c8 2765 thr->specific = newAV();
38a03e6e 2766 thr->errhv = newHV();
199100c8
MB
2767 thr->flags = THRf_R_JOINABLE;
2768 MUTEX_INIT(&thr->mutex);
2769 /* Handcraft thrsv similarly to mess_sv */
2770 New(53, thrsv, 1, SV);
2771 Newz(53, xpv, 1, XPV);
2772 SvFLAGS(thrsv) = SVt_PV;
2773 SvANY(thrsv) = (void*)xpv;
2774 SvREFCNT(thrsv) = 1 << 30; /* practically infinite */
2775 SvPVX(thrsv) = (char*)thr;
2776 SvCUR_set(thrsv, sizeof(thr));
2777 SvLEN_set(thrsv, sizeof(thr));
2778 *SvEND(thrsv) = '\0'; /* in the trailing_nul field */
2779 thr->oursv = thrsv;
2780 curcop = &compiling;
2781 chopset = " \n-";
2782
2783 MUTEX_LOCK(&threads_mutex);
2784 nthreads++;
2785 thr->tid = 0;
2786 thr->next = thr;
2787 thr->prev = thr;
2788 MUTEX_UNLOCK(&threads_mutex);
2789
4b026b9e
GS
2790#ifdef HAVE_THREAD_INTERN
2791 init_thread_intern(thr);
235db74f
GS
2792#endif
2793
2794#ifdef SET_THREAD_SELF
2795 SET_THREAD_SELF(thr);
199100c8
MB
2796#else
2797 thr->self = pthread_self();
235db74f 2798#endif /* SET_THREAD_SELF */
199100c8
MB
2799 SET_THR(thr);
2800
2801 /*
2802 * These must come after the SET_THR because sv_setpvn does
2803 * SvTAINT and the taint fields require dTHR.
2804 */
2805 toptarget = NEWSV(0,0);
2806 sv_upgrade(toptarget, SVt_PVFM);
2807 sv_setpvn(toptarget, "", 0);
2808 bodytarget = NEWSV(0,0);
2809 sv_upgrade(bodytarget, SVt_PVFM);
2810 sv_setpvn(bodytarget, "", 0);
2811 formtarget = bodytarget;
2faa37cc 2812 thr->errsv = newSVpv("", 0);
78857c3c 2813 (void) find_threadsv("@"); /* Ensure $@ is initialised early */
199100c8
MB
2814 return thr;
2815}
2816#endif /* USE_THREADS */
2817
93a17b20 2818void
8ac85365 2819call_list(I32 oldscope, AV *list)
93a17b20 2820{
11343788 2821 dTHR;
a0d0e21e 2822 line_t oldline = curcop->cop_line;
22921e25
CS
2823 STRLEN len;
2824 dJMPENV;
2825 int ret;
93a17b20 2826
93965878 2827 while (AvFILL(list) >= 0) {
8990e307 2828 CV *cv = (CV*)av_shift(list);
93a17b20 2829
8990e307 2830 SAVEFREESV(cv);
a0d0e21e 2831
22921e25
CS
2832 JMPENV_PUSH(ret);
2833 switch (ret) {
748a9306 2834 case 0: {
38a03e6e 2835 SV* atsv = ERRSV;
748a9306
LW
2836 PUSHMARK(stack_sp);
2837 perl_call_sv((SV*)cv, G_EVAL|G_DISCARD);
12f917ad 2838 (void)SvPV(atsv, len);
748a9306 2839 if (len) {
54310121 2840 JMPENV_POP;
748a9306
LW
2841 curcop = &compiling;
2842 curcop->cop_line = oldline;
2843 if (list == beginav)
12f917ad 2844 sv_catpv(atsv, "BEGIN failed--compilation aborted");
748a9306 2845 else
12f917ad 2846 sv_catpv(atsv, "END failed--cleanup aborted");
2ae324a7 2847 while (scopestack_ix > oldscope)
2848 LEAVE;
12f917ad 2849 croak("%s", SvPVX(atsv));
748a9306 2850 }
a0d0e21e 2851 }
85e6fe83
LW
2852 break;
2853 case 1:
f86702cc 2854 STATUS_ALL_FAILURE;
85e6fe83
LW
2855 /* FALL THROUGH */
2856 case 2:
2857 /* my_exit() was called */
2ae324a7 2858 while (scopestack_ix > oldscope)
2859 LEAVE;
84902520 2860 FREETMPS;
85e6fe83
LW
2861 curstash = defstash;
2862 if (endav)
68dc0745 2863 call_list(oldscope, endav);
54310121 2864 JMPENV_POP;
a0d0e21e
LW
2865 curcop = &compiling;
2866 curcop->cop_line = oldline;
85e6fe83
LW
2867 if (statusvalue) {
2868 if (list == beginav)
a0d0e21e 2869 croak("BEGIN failed--compilation aborted");
85e6fe83 2870 else
a0d0e21e 2871 croak("END failed--cleanup aborted");
85e6fe83 2872 }
f86702cc 2873 my_exit_jump();
85e6fe83 2874 /* NOTREACHED */
85e6fe83
LW
2875 case 3:
2876 if (!restartop) {
760ac839 2877 PerlIO_printf(PerlIO_stderr(), "panic: restartop\n");
a0d0e21e 2878 FREETMPS;
85e6fe83
LW
2879 break;
2880 }
54310121 2881 JMPENV_POP;
a0d0e21e
LW
2882 curcop = &compiling;
2883 curcop->cop_line = oldline;
54310121 2884 JMPENV_JUMP(3);
8990e307 2885 }
54310121 2886 JMPENV_POP;
93a17b20 2887 }
93a17b20 2888}
93a17b20 2889
f86702cc 2890void
8ac85365 2891my_exit(U32 status)
f86702cc 2892{
5dc0d613
MB
2893 dTHR;
2894
2895#ifdef USE_THREADS
a863c7d1
MB
2896 DEBUG_L(PerlIO_printf(Perl_debug_log, "my_exit: thread %p, status %lu\n",
2897 thr, (unsigned long) status));
5dc0d613 2898#endif /* USE_THREADS */
f86702cc 2899 switch (status) {
2900 case 0:
2901 STATUS_ALL_SUCCESS;
2902 break;
2903 case 1:
2904 STATUS_ALL_FAILURE;
2905 break;
2906 default:
2907 STATUS_NATIVE_SET(status);
2908 break;
2909 }
2910 my_exit_jump();
2911}
2912
2913void
8ac85365 2914my_failure_exit(void)
f86702cc 2915{
2916#ifdef VMS
2917 if (vaxc$errno & 1) {
4fdae800 2918 if (STATUS_NATIVE & 1) /* fortuitiously includes "-1" */
2919 STATUS_NATIVE_SET(44);
f86702cc 2920 }
2921 else {
ff0cee69 2922 if (!vaxc$errno && errno) /* unlikely */
4fdae800 2923 STATUS_NATIVE_SET(44);
f86702cc 2924 else
4fdae800 2925 STATUS_NATIVE_SET(vaxc$errno);
f86702cc 2926 }
2927#else
2928 if (errno & 255)
2929 STATUS_POSIX_SET(errno);
2930 else if (STATUS_POSIX == 0)
2931 STATUS_POSIX_SET(255);
2932#endif
2933 my_exit_jump();
93a17b20
LW
2934}
2935
f86702cc 2936static void
8ac85365 2937my_exit_jump(void)
f86702cc 2938{
e858de61 2939 dTHR;
c09156bb 2940 register PERL_CONTEXT *cx;
f86702cc 2941 I32 gimme;
2942 SV **newsp;
2943
2944 if (e_tmpname) {
2945 if (e_fp) {
2946 PerlIO_close(e_fp);
2947 e_fp = Nullfp;
2948 }
2949 (void)UNLINK(e_tmpname);
2950 Safefree(e_tmpname);
2951 e_tmpname = Nullch;
2952 }
2953
2954 if (cxstack_ix >= 0) {
2955 if (cxstack_ix > 0)
2956 dounwind(0);
2957 POPBLOCK(cx,curpm);
2958 LEAVE;
2959 }
ff0cee69 2960
54310121 2961 JMPENV_JUMP(2);
f86702cc 2962}
4e35701f 2963
aeea060c 2964
22239a37 2965