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