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