-
Notifications
You must be signed in to change notification settings - Fork 0
/
perl.c
3876 lines (3490 loc) · 95.6 KB
/
perl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* perl.c
*
* Copyright (c) 1987-2000 Larry Wall
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* "A ship then new they built for him/of mithril and of elven glass" --Bilbo
*/
#include "EXTERN.h"
#define PERL_IN_PERL_C
#include "perl.h"
#include "patchlevel.h" /* for local_patches */
/* XXX If this causes problems, set i_unistd=undef in the hint file. */
#ifdef I_UNISTD
#include <unistd.h>
#endif
#if !defined(STANDARD_C) && !defined(HAS_GETENV_PROTOTYPE)
char *getenv (char *); /* Usually in <stdlib.h> */
#endif
static I32 read_e_script(pTHXo_ int idx, SV *buf_sv, int maxlen);
#ifdef IAMSUID
#ifndef DOSUID
#define DOSUID
#endif
#endif
#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
#ifdef DOSUID
#undef DOSUID
#endif
#endif
#ifdef PERL_OBJECT
#define perl_construct Perl_construct
#define perl_parse Perl_parse
#define perl_run Perl_run
#define perl_destruct Perl_destruct
#define perl_free Perl_free
#endif
#if defined(USE_THREADS)
# define INIT_TLS_AND_INTERP \
STMT_START { \
if (!PL_curinterp) { \
PERL_SET_INTERP(my_perl); \
INIT_THREADS; \
ALLOC_THREAD_KEY; \
} \
} STMT_END
#else
# if defined(USE_ITHREADS)
# define INIT_TLS_AND_INTERP \
STMT_START { \
if (!PL_curinterp) { \
PERL_SET_INTERP(my_perl); \
INIT_THREADS; \
ALLOC_THREAD_KEY; \
PERL_SET_THX(my_perl); \
OP_REFCNT_INIT; \
} \
else { \
PERL_SET_THX(my_perl); \
} \
} STMT_END
# else
# define INIT_TLS_AND_INTERP \
STMT_START { \
if (!PL_curinterp) { \
PERL_SET_INTERP(my_perl); \
} \
PERL_SET_THX(my_perl); \
} STMT_END
# endif
#endif
#ifdef PERL_IMPLICIT_SYS
PerlInterpreter *
perl_alloc_using(struct IPerlMem* ipM, struct IPerlMem* ipMS,
struct IPerlMem* ipMP, struct IPerlEnv* ipE,
struct IPerlStdIO* ipStd, struct IPerlLIO* ipLIO,
struct IPerlDir* ipD, struct IPerlSock* ipS,
struct IPerlProc* ipP)
{
PerlInterpreter *my_perl;
#ifdef PERL_OBJECT
my_perl = (PerlInterpreter*)new(ipM) CPerlObj(ipM, ipMS, ipMP, ipE, ipStd,
ipLIO, ipD, ipS, ipP);
INIT_TLS_AND_INTERP;
#else
/* New() needs interpreter, so call malloc() instead */
my_perl = (PerlInterpreter*)(*ipM->pMalloc)(ipM, sizeof(PerlInterpreter));
INIT_TLS_AND_INTERP;
Zero(my_perl, 1, PerlInterpreter);
PL_Mem = ipM;
PL_MemShared = ipMS;
PL_MemParse = ipMP;
PL_Env = ipE;
PL_StdIO = ipStd;
PL_LIO = ipLIO;
PL_Dir = ipD;
PL_Sock = ipS;
PL_Proc = ipP;
#endif
return my_perl;
}
#else
/*
=for apidoc perl_alloc
Allocates a new Perl interpreter. See L<perlembed>.
=cut
*/
PerlInterpreter *
perl_alloc(void)
{
PerlInterpreter *my_perl;
/* New() needs interpreter, so call malloc() instead */
my_perl = (PerlInterpreter*)PerlMem_malloc(sizeof(PerlInterpreter));
INIT_TLS_AND_INTERP;
Zero(my_perl, 1, PerlInterpreter);
return my_perl;
}
#endif /* PERL_IMPLICIT_SYS */
/*
=for apidoc perl_construct
Initializes a new Perl interpreter. See L<perlembed>.
=cut
*/
void
perl_construct(pTHXx)
{
#ifdef USE_THREADS
int i;
#ifndef FAKE_THREADS
struct perl_thread *thr = NULL;
#endif /* FAKE_THREADS */
#endif /* USE_THREADS */
#ifdef MULTIPLICITY
init_interp();
PL_perl_destruct_level = 1;
#else
if (PL_perl_destruct_level > 0)
init_interp();
#endif
/* Init the real globals (and main thread)? */
if (!PL_linestr) {
#ifdef USE_THREADS
MUTEX_INIT(&PL_sv_mutex);
/*
* Safe to use basic SV functions from now on (though
* not things like mortals or tainting yet).
*/
MUTEX_INIT(&PL_eval_mutex);
COND_INIT(&PL_eval_cond);
MUTEX_INIT(&PL_threads_mutex);
COND_INIT(&PL_nthreads_cond);
# ifdef EMULATE_ATOMIC_REFCOUNTS
MUTEX_INIT(&PL_svref_mutex);
# endif /* EMULATE_ATOMIC_REFCOUNTS */
MUTEX_INIT(&PL_cred_mutex);
MUTEX_INIT(&PL_sv_lock_mutex);
MUTEX_INIT(&PL_fdpid_mutex);
thr = init_main_thread();
#endif /* USE_THREADS */
#ifdef PERL_FLEXIBLE_EXCEPTIONS
PL_protect = MEMBER_TO_FPTR(Perl_default_protect); /* for exceptions */
#endif
PL_curcop = &PL_compiling; /* needed by ckWARN, right away */
PL_linestr = NEWSV(65,79);
sv_upgrade(PL_linestr,SVt_PVIV);
if (!SvREADONLY(&PL_sv_undef)) {
/* set read-only and try to insure than we wont see REFCNT==0
very often */
SvREADONLY_on(&PL_sv_undef);
SvREFCNT(&PL_sv_undef) = (~(U32)0)/2;
sv_setpv(&PL_sv_no,PL_No);
SvNV(&PL_sv_no);
SvREADONLY_on(&PL_sv_no);
SvREFCNT(&PL_sv_no) = (~(U32)0)/2;
sv_setpv(&PL_sv_yes,PL_Yes);
SvNV(&PL_sv_yes);
SvREADONLY_on(&PL_sv_yes);
SvREFCNT(&PL_sv_yes) = (~(U32)0)/2;
}
#ifdef PERL_OBJECT
/* TODO: */
/* PL_sighandlerp = sighandler; */
#else
PL_sighandlerp = Perl_sighandler;
#endif
PL_pidstatus = newHV();
#ifdef MSDOS
/*
* There is no way we can refer to them from Perl so close them to save
* space. The other alternative would be to provide STDAUX and STDPRN
* filehandles.
*/
(void)fclose(stdaux);
(void)fclose(stdprn);
#endif
}
PL_nrs = newSVpvn("\n", 1);
PL_rs = SvREFCNT_inc(PL_nrs);
init_stacks();
init_ids();
PL_lex_state = LEX_NOTPARSING;
JMPENV_BOOTSTRAP;
STATUS_ALL_SUCCESS;
init_i18nl10n(1);
SET_NUMERIC_STANDARD();
{
U8 *s;
PL_patchlevel = NEWSV(0,4);
(void)SvUPGRADE(PL_patchlevel, SVt_PVNV);
if (PERL_REVISION > 127 || PERL_VERSION > 127 || PERL_SUBVERSION > 127)
SvGROW(PL_patchlevel, UTF8_MAXLEN*3+1);
s = (U8*)SvPVX(PL_patchlevel);
s = uv_to_utf8(s, (UV)PERL_REVISION);
s = uv_to_utf8(s, (UV)PERL_VERSION);
s = uv_to_utf8(s, (UV)PERL_SUBVERSION);
*s = '\0';
SvCUR_set(PL_patchlevel, s - (U8*)SvPVX(PL_patchlevel));
SvPOK_on(PL_patchlevel);
SvNVX(PL_patchlevel) = (NV)PERL_REVISION
+ ((NV)PERL_VERSION / (NV)1000)
#if defined(PERL_SUBVERSION) && PERL_SUBVERSION > 0
+ ((NV)PERL_SUBVERSION / (NV)1000000)
#endif
;
SvNOK_on(PL_patchlevel); /* dual valued */
SvUTF8_on(PL_patchlevel);
SvREADONLY_on(PL_patchlevel);
}
#if defined(LOCAL_PATCH_COUNT)
PL_localpatches = local_patches; /* For possible -v */
#endif
#ifdef HAVE_INTERP_INTERN
sys_intern_init();
#endif
PerlIO_init(); /* Hook to IO system */
PL_fdpid = newAV(); /* for remembering popen pids by fd */
PL_modglobal = newHV(); /* pointers to per-interpreter module globals */
PL_errors = newSVpvn("",0);
ENTER;
}
/*
=for apidoc perl_destruct
Shuts down a Perl interpreter. See L<perlembed>.
=cut
*/
void
perl_destruct(pTHXx)
{
dTHR;
int destruct_level; /* 0=none, 1=full, 2=full with checks */
I32 last_sv_count;
HV *hv;
#ifdef USE_THREADS
Thread t;
dTHX;
#endif /* USE_THREADS */
/* wait for all pseudo-forked children to finish */
PERL_WAIT_FOR_CHILDREN;
#ifdef USE_THREADS
#ifndef FAKE_THREADS
/* Pass 1 on any remaining threads: detach joinables, join zombies */
retry_cleanup:
MUTEX_LOCK(&PL_threads_mutex);
DEBUG_S(PerlIO_printf(Perl_debug_log,
"perl_destruct: waiting for %d threads...\n",
PL_nthreads - 1));
for (t = thr->next; t != thr; t = t->next) {
MUTEX_LOCK(&t->mutex);
switch (ThrSTATE(t)) {
AV *av;
case THRf_ZOMBIE:
DEBUG_S(PerlIO_printf(Perl_debug_log,
"perl_destruct: joining zombie %p\n", t));
ThrSETSTATE(t, THRf_DEAD);
MUTEX_UNLOCK(&t->mutex);
PL_nthreads--;
/*
* The SvREFCNT_dec below may take a long time (e.g. av
* may contain an object scalar whose destructor gets
* called) so we have to unlock threads_mutex and start
* all over again.
*/
MUTEX_UNLOCK(&PL_threads_mutex);
JOIN(t, &av);
SvREFCNT_dec((SV*)av);
DEBUG_S(PerlIO_printf(Perl_debug_log,
"perl_destruct: joined zombie %p OK\n", t));
goto retry_cleanup;
case THRf_R_JOINABLE:
DEBUG_S(PerlIO_printf(Perl_debug_log,
"perl_destruct: detaching thread %p\n", t));
ThrSETSTATE(t, THRf_R_DETACHED);
/*
* We unlock threads_mutex and t->mutex in the opposite order
* from which we locked them just so that DETACH won't
* deadlock if it panics. It's only a breach of good style
* not a bug since they are unlocks not locks.
*/
MUTEX_UNLOCK(&PL_threads_mutex);
DETACH(t);
MUTEX_UNLOCK(&t->mutex);
goto retry_cleanup;
default:
DEBUG_S(PerlIO_printf(Perl_debug_log,
"perl_destruct: ignoring %p (state %u)\n",
t, ThrSTATE(t)));
MUTEX_UNLOCK(&t->mutex);
/* fall through and out */
}
}
/* We leave the above "Pass 1" loop with threads_mutex still locked */
/* Pass 2 on remaining threads: wait for the thread count to drop to one */
while (PL_nthreads > 1)
{
DEBUG_S(PerlIO_printf(Perl_debug_log,
"perl_destruct: final wait for %d threads\n",
PL_nthreads - 1));
COND_WAIT(&PL_nthreads_cond, &PL_threads_mutex);
}
/* At this point, we're the last thread */
MUTEX_UNLOCK(&PL_threads_mutex);
DEBUG_S(PerlIO_printf(Perl_debug_log, "perl_destruct: armageddon has arrived\n"));
MUTEX_DESTROY(&PL_threads_mutex);
COND_DESTROY(&PL_nthreads_cond);
#endif /* !defined(FAKE_THREADS) */
#endif /* USE_THREADS */
destruct_level = PL_perl_destruct_level;
#ifdef DEBUGGING
{
char *s;
if ((s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL"))) {
int i = atoi(s);
if (destruct_level < i)
destruct_level = i;
}
}
#endif
LEAVE;
FREETMPS;
/* We must account for everything. */
/* Destroy the main CV and syntax tree */
if (PL_main_root) {
PL_curpad = AvARRAY(PL_comppad);
op_free(PL_main_root);
PL_main_root = Nullop;
}
PL_curcop = &PL_compiling;
PL_main_start = Nullop;
SvREFCNT_dec(PL_main_cv);
PL_main_cv = Nullcv;
PL_dirty = TRUE;
if (PL_sv_objcount) {
/*
* Try to destruct global references. We do this first so that the
* destructors and destructees still exist. Some sv's might remain.
* Non-referenced objects are on their own.
*/
sv_clean_objs();
}
/* unhook hooks which will soon be, or use, destroyed data */
SvREFCNT_dec(PL_warnhook);
PL_warnhook = Nullsv;
SvREFCNT_dec(PL_diehook);
PL_diehook = Nullsv;
/* call exit list functions */
while (PL_exitlistlen-- > 0)
PL_exitlist[PL_exitlistlen].fn(aTHXo_ PL_exitlist[PL_exitlistlen].ptr);
Safefree(PL_exitlist);
if (destruct_level == 0){
DEBUG_P(debprofdump());
/* The exit() function will do everything that needs doing. */
return;
}
/* loosen bonds of global variables */
if(PL_rsfp) {
(void)PerlIO_close(PL_rsfp);
PL_rsfp = Nullfp;
}
/* Filters for program text */
SvREFCNT_dec(PL_rsfp_filters);
PL_rsfp_filters = Nullav;
/* switches */
PL_preprocess = FALSE;
PL_minus_n = FALSE;
PL_minus_p = FALSE;
PL_minus_l = FALSE;
PL_minus_a = FALSE;
PL_minus_F = FALSE;
PL_doswitches = FALSE;
PL_dowarn = G_WARN_OFF;
PL_doextract = FALSE;
PL_sawampersand = FALSE; /* must save all match strings */
PL_unsafe = FALSE;
Safefree(PL_inplace);
PL_inplace = Nullch;
SvREFCNT_dec(PL_patchlevel);
if (PL_e_script) {
SvREFCNT_dec(PL_e_script);
PL_e_script = Nullsv;
}
/* magical thingies */
Safefree(PL_ofs); /* $, */
PL_ofs = Nullch;
Safefree(PL_ors); /* $\ */
PL_ors = Nullch;
SvREFCNT_dec(PL_rs); /* $/ */
PL_rs = Nullsv;
SvREFCNT_dec(PL_nrs); /* $/ helper */
PL_nrs = Nullsv;
PL_multiline = 0; /* $* */
Safefree(PL_osname); /* $^O */
PL_osname = Nullch;
SvREFCNT_dec(PL_statname);
PL_statname = Nullsv;
PL_statgv = Nullgv;
/* defgv, aka *_ should be taken care of elsewhere */
/* clean up after study() */
SvREFCNT_dec(PL_lastscream);
PL_lastscream = Nullsv;
Safefree(PL_screamfirst);
PL_screamfirst = 0;
Safefree(PL_screamnext);
PL_screamnext = 0;
/* float buffer */
Safefree(PL_efloatbuf);
PL_efloatbuf = Nullch;
PL_efloatsize = 0;
/* startup and shutdown function lists */
SvREFCNT_dec(PL_beginav);
SvREFCNT_dec(PL_endav);
SvREFCNT_dec(PL_checkav);
SvREFCNT_dec(PL_initav);
PL_beginav = Nullav;
PL_endav = Nullav;
PL_checkav = Nullav;
PL_initav = Nullav;
/* shortcuts just get cleared */
PL_envgv = Nullgv;
PL_incgv = Nullgv;
PL_hintgv = Nullgv;
PL_errgv = Nullgv;
PL_argvgv = Nullgv;
PL_argvoutgv = Nullgv;
PL_stdingv = Nullgv;
PL_stderrgv = Nullgv;
PL_last_in_gv = Nullgv;
PL_replgv = Nullgv;
PL_debstash = Nullhv;
/* reset so print() ends up where we expect */
setdefout(Nullgv);
SvREFCNT_dec(PL_argvout_stack);
PL_argvout_stack = Nullav;
SvREFCNT_dec(PL_modglobal);
PL_modglobal = Nullhv;
SvREFCNT_dec(PL_preambleav);
PL_preambleav = Nullav;
SvREFCNT_dec(PL_subname);
PL_subname = Nullsv;
SvREFCNT_dec(PL_linestr);
PL_linestr = Nullsv;
SvREFCNT_dec(PL_pidstatus);
PL_pidstatus = Nullhv;
SvREFCNT_dec(PL_toptarget);
PL_toptarget = Nullsv;
SvREFCNT_dec(PL_bodytarget);
PL_bodytarget = Nullsv;
PL_formtarget = Nullsv;
/* free locale stuff */
#ifdef USE_LOCALE_COLLATE
Safefree(PL_collation_name);
PL_collation_name = Nullch;
#endif
#ifdef USE_LOCALE_NUMERIC
Safefree(PL_numeric_name);
PL_numeric_name = Nullch;
#endif
/* clear utf8 character classes */
SvREFCNT_dec(PL_utf8_alnum);
SvREFCNT_dec(PL_utf8_alnumc);
SvREFCNT_dec(PL_utf8_ascii);
SvREFCNT_dec(PL_utf8_alpha);
SvREFCNT_dec(PL_utf8_space);
SvREFCNT_dec(PL_utf8_cntrl);
SvREFCNT_dec(PL_utf8_graph);
SvREFCNT_dec(PL_utf8_digit);
SvREFCNT_dec(PL_utf8_upper);
SvREFCNT_dec(PL_utf8_lower);
SvREFCNT_dec(PL_utf8_print);
SvREFCNT_dec(PL_utf8_punct);
SvREFCNT_dec(PL_utf8_xdigit);
SvREFCNT_dec(PL_utf8_mark);
SvREFCNT_dec(PL_utf8_toupper);
SvREFCNT_dec(PL_utf8_tolower);
PL_utf8_alnum = Nullsv;
PL_utf8_alnumc = Nullsv;
PL_utf8_ascii = Nullsv;
PL_utf8_alpha = Nullsv;
PL_utf8_space = Nullsv;
PL_utf8_cntrl = Nullsv;
PL_utf8_graph = Nullsv;
PL_utf8_digit = Nullsv;
PL_utf8_upper = Nullsv;
PL_utf8_lower = Nullsv;
PL_utf8_print = Nullsv;
PL_utf8_punct = Nullsv;
PL_utf8_xdigit = Nullsv;
PL_utf8_mark = Nullsv;
PL_utf8_toupper = Nullsv;
PL_utf8_totitle = Nullsv;
PL_utf8_tolower = Nullsv;
if (!specialWARN(PL_compiling.cop_warnings))
SvREFCNT_dec(PL_compiling.cop_warnings);
PL_compiling.cop_warnings = Nullsv;
#ifdef USE_ITHREADS
Safefree(CopFILE(&PL_compiling));
CopFILE(&PL_compiling) = Nullch;
Safefree(CopSTASHPV(&PL_compiling));
#else
SvREFCNT_dec(CopFILEGV(&PL_compiling));
CopFILEGV(&PL_compiling) = Nullgv;
/* cop_stash is not refcounted */
#endif
/* Prepare to destruct main symbol table. */
hv = PL_defstash;
PL_defstash = 0;
SvREFCNT_dec(hv);
SvREFCNT_dec(PL_curstname);
PL_curstname = Nullsv;
/* clear queued errors */
SvREFCNT_dec(PL_errors);
PL_errors = Nullsv;
FREETMPS;
if (destruct_level >= 2 && ckWARN_d(WARN_INTERNAL)) {
if (PL_scopestack_ix != 0)
Perl_warner(aTHX_ WARN_INTERNAL,
"Unbalanced scopes: %ld more ENTERs than LEAVEs\n",
(long)PL_scopestack_ix);
if (PL_savestack_ix != 0)
Perl_warner(aTHX_ WARN_INTERNAL,
"Unbalanced saves: %ld more saves than restores\n",
(long)PL_savestack_ix);
if (PL_tmps_floor != -1)
Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced tmps: %ld more allocs than frees\n",
(long)PL_tmps_floor + 1);
if (cxstack_ix != -1)
Perl_warner(aTHX_ WARN_INTERNAL,"Unbalanced context: %ld more PUSHes than POPs\n",
(long)cxstack_ix + 1);
}
/* Now absolutely destruct everything, somehow or other, loops or no. */
last_sv_count = 0;
SvFLAGS(PL_fdpid) |= SVTYPEMASK; /* don't clean out pid table now */
SvFLAGS(PL_strtab) |= SVTYPEMASK; /* don't clean out strtab now */
while (PL_sv_count != 0 && PL_sv_count != last_sv_count) {
last_sv_count = PL_sv_count;
sv_clean_all();
}
SvFLAGS(PL_fdpid) &= ~SVTYPEMASK;
SvFLAGS(PL_fdpid) |= SVt_PVAV;
SvFLAGS(PL_strtab) &= ~SVTYPEMASK;
SvFLAGS(PL_strtab) |= SVt_PVHV;
AvREAL_off(PL_fdpid); /* no surviving entries */
SvREFCNT_dec(PL_fdpid); /* needed in io_close() */
PL_fdpid = Nullav;
#ifdef HAVE_INTERP_INTERN
sys_intern_clear();
#endif
/* Destruct the global string table. */
{
/* Yell and reset the HeVAL() slots that are still holding refcounts,
* so that sv_free() won't fail on them.
*/
I32 riter;
I32 max;
HE *hent;
HE **array;
riter = 0;
max = HvMAX(PL_strtab);
array = HvARRAY(PL_strtab);
hent = array[0];
for (;;) {
if (hent && ckWARN_d(WARN_INTERNAL)) {
Perl_warner(aTHX_ WARN_INTERNAL,
"Unbalanced string table refcount: (%d) for \"%s\"",
HeVAL(hent) - Nullsv, HeKEY(hent));
HeVAL(hent) = Nullsv;
hent = HeNEXT(hent);
}
if (!hent) {
if (++riter > max)
break;
hent = array[riter];
}
}
}
SvREFCNT_dec(PL_strtab);
/* free special SVs */
SvREFCNT(&PL_sv_yes) = 0;
sv_clear(&PL_sv_yes);
SvANY(&PL_sv_yes) = NULL;
SvFLAGS(&PL_sv_yes) = 0;
SvREFCNT(&PL_sv_no) = 0;
sv_clear(&PL_sv_no);
SvANY(&PL_sv_no) = NULL;
SvFLAGS(&PL_sv_no) = 0;
SvREFCNT(&PL_sv_undef) = 0;
SvREADONLY_off(&PL_sv_undef);
if (PL_sv_count != 0 && ckWARN_d(WARN_INTERNAL))
Perl_warner(aTHX_ WARN_INTERNAL,"Scalars leaked: %ld\n", (long)PL_sv_count);
Safefree(PL_origfilename);
Safefree(PL_reg_start_tmp);
if (PL_reg_curpm)
Safefree(PL_reg_curpm);
Safefree(PL_reg_poscache);
Safefree(HeKEY_hek(&PL_hv_fetch_ent_mh));
Safefree(PL_op_mask);
Safefree(PL_psig_ptr);
Safefree(PL_psig_name);
nuke_stacks();
PL_hints = 0; /* Reset hints. Should hints be per-interpreter ? */
DEBUG_P(debprofdump());
#ifdef USE_THREADS
MUTEX_DESTROY(&PL_strtab_mutex);
MUTEX_DESTROY(&PL_sv_mutex);
MUTEX_DESTROY(&PL_eval_mutex);
MUTEX_DESTROY(&PL_cred_mutex);
MUTEX_DESTROY(&PL_fdpid_mutex);
COND_DESTROY(&PL_eval_cond);
#ifdef EMULATE_ATOMIC_REFCOUNTS
MUTEX_DESTROY(&PL_svref_mutex);
#endif /* EMULATE_ATOMIC_REFCOUNTS */
/* As the penultimate thing, free the non-arena SV for thrsv */
Safefree(SvPVX(PL_thrsv));
Safefree(SvANY(PL_thrsv));
Safefree(PL_thrsv);
PL_thrsv = Nullsv;
#endif /* USE_THREADS */
sv_free_arenas();
/* As the absolutely last thing, free the non-arena SV for mess() */
if (PL_mess_sv) {
/* it could have accumulated taint magic */
if (SvTYPE(PL_mess_sv) >= SVt_PVMG) {
MAGIC* mg;
MAGIC* moremagic;
for (mg = SvMAGIC(PL_mess_sv); mg; mg = moremagic) {
moremagic = mg->mg_moremagic;
if (mg->mg_ptr && mg->mg_type != 'g' && mg->mg_len >= 0)
Safefree(mg->mg_ptr);
Safefree(mg);
}
}
/* we know that type >= SVt_PV */
(void)SvOOK_off(PL_mess_sv);
Safefree(SvPVX(PL_mess_sv));
Safefree(SvANY(PL_mess_sv));
Safefree(PL_mess_sv);
PL_mess_sv = Nullsv;
}
}
/*
=for apidoc perl_free
Releases a Perl interpreter. See L<perlembed>.
=cut
*/
void
perl_free(pTHXx)
{
#if defined(PERL_OBJECT)
PerlMem_free(this);
#else
# if defined(PERL_IMPLICIT_SYS) && defined(WIN32)
void *host = w32_internal_host;
PerlMem_free(aTHXx);
win32_delete_internal_host(host);
# else
PerlMem_free(aTHXx);
# endif
#endif
}
void
Perl_call_atexit(pTHX_ ATEXIT_t fn, void *ptr)
{
Renew(PL_exitlist, PL_exitlistlen+1, PerlExitListEntry);
PL_exitlist[PL_exitlistlen].fn = fn;
PL_exitlist[PL_exitlistlen].ptr = ptr;
++PL_exitlistlen;
}
/*
=for apidoc perl_parse
Tells a Perl interpreter to parse a Perl script. See L<perlembed>.
=cut
*/
int
perl_parse(pTHXx_ XSINIT_t xsinit, int argc, char **argv, char **env)
{
dTHR;
I32 oldscope;
int ret;
dJMPENV;
#ifdef USE_THREADS
dTHX;
#endif
#ifdef SETUID_SCRIPTS_ARE_SECURE_NOW
#ifdef IAMSUID
#undef IAMSUID
Perl_croak(aTHX_ "suidperl is no longer needed since the kernel can now execute\n\
setuid perl scripts securely.\n");
#endif
#endif
#if defined(__DYNAMIC__) && (defined(NeXT) || defined(__NeXT__))
_dyld_lookup_and_bind
("__environ", (unsigned long *) &environ_pointer, NULL);
#endif /* environ */
PL_origargv = argv;
PL_origargc = argc;
#ifndef VMS /* VMS doesn't have environ array */
PL_origenviron = environ;
#endif
if (PL_do_undump) {
/* Come here if running an undumped a.out. */
PL_origfilename = savepv(argv[0]);
PL_do_undump = FALSE;
cxstack_ix = -1; /* start label stack again */
init_ids();
init_postdump_symbols(argc,argv,env);
return 0;
}
if (PL_main_root) {
PL_curpad = AvARRAY(PL_comppad);
op_free(PL_main_root);
PL_main_root = Nullop;
}
PL_main_start = Nullop;
SvREFCNT_dec(PL_main_cv);
PL_main_cv = Nullcv;
time(&PL_basetime);
oldscope = PL_scopestack_ix;
PL_dowarn = G_WARN_OFF;
#ifdef PERL_FLEXIBLE_EXCEPTIONS
CALLPROTECT(aTHX_ pcur_env, &ret, MEMBER_TO_FPTR(S_vparse_body), env, xsinit);
#else
JMPENV_PUSH(ret);
#endif
switch (ret) {
case 0:
#ifndef PERL_FLEXIBLE_EXCEPTIONS
parse_body(env,xsinit);
#endif
if (PL_checkav)
call_list(oldscope, PL_checkav);
ret = 0;
break;
case 1:
STATUS_ALL_FAILURE;
/* FALL THROUGH */
case 2:
/* my_exit() was called */
while (PL_scopestack_ix > oldscope)
LEAVE;
FREETMPS;
PL_curstash = PL_defstash;
if (PL_checkav)
call_list(oldscope, PL_checkav);
ret = STATUS_NATIVE_EXPORT;
break;
case 3:
PerlIO_printf(Perl_error_log, "panic: top_env\n");
ret = 1;
break;
}
JMPENV_POP;
return ret;
}
#ifdef PERL_FLEXIBLE_EXCEPTIONS
STATIC void *
S_vparse_body(pTHX_ va_list args)
{
char **env = va_arg(args, char**);
XSINIT_t xsinit = va_arg(args, XSINIT_t);
return parse_body(env, xsinit);
}
#endif
STATIC void *
S_parse_body(pTHX_ char **env, XSINIT_t xsinit)
{
dTHR;
int argc = PL_origargc;
char **argv = PL_origargv;
char *scriptname = NULL;
int fdscript = -1;
VOL bool dosearch = FALSE;
char *validarg = "";
AV* comppadlist;
register SV *sv;
register char *s;
char *cddir = Nullch;
sv_setpvn(PL_linestr,"",0);
sv = newSVpvn("",0); /* first used for -I flags */
SAVEFREESV(sv);
init_main_stash();
for (argc--,argv++; argc > 0; argc--,argv++) {
if (argv[0][0] != '-' || !argv[0][1])
break;
#ifdef DOSUID
if (*validarg)
validarg = " PHOOEY ";
else
validarg = argv[0];
#endif
s = argv[0]+1;
reswitch:
switch (*s) {
case 'C':
#ifdef WIN32
win32_argv2utf8(argc-1, argv+1);
/* FALL THROUGH */
#endif
#ifndef PERL_STRICT_CR
case '\r':
#endif
case ' ':
case '0':
case 'F':
case 'a':
case 'c':
case 'd':
case 'D':
case 'h':
case 'i':
case 'l':
case 'M':
case 'm':
case 'n':
case 'p':
case 's':
case 'u':
case 'U':
case 'v':
case 'W':
case 'X':
case 'w':
if ((s = moreswitches(s)))
goto reswitch;
break;
case 'T':
PL_tainting = TRUE;
s++;
goto reswitch;
case 'e':
#ifdef MACOS_TRADITIONAL
/* ignore -e for Dev:Pseudo argument */
if (argv[1] && !strcmp(argv[1], "Dev:Pseudo"))
break;
#endif
if (PL_euid != PL_uid || PL_egid != PL_gid)
Perl_croak(aTHX_ "No -e allowed in setuid scripts");
if (!PL_e_script) {
PL_e_script = newSVpvn("",0);
filter_add(read_e_script, NULL);
}
if (*++s)
sv_catpv(PL_e_script, s);
else if (argv[1]) {
sv_catpv(PL_e_script, argv[1]);
argc--,argv++;