xref: /openbsd/gnu/usr.bin/perl/miniperlmain.c (revision e0680481)
1*e0680481Safresh1 /* -*- mode: C; buffer-read-only: t -*-
2e5157e49Safresh1    !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
3e5157e49Safresh1    This file is built by regen/miniperlmain.pl and ExtUtils::Miniperl.
4e5157e49Safresh1    Any changes made here will be lost!
5e5157e49Safresh1  */
6e5157e49Safresh1 
79f11ffb7Safresh1 /*    miniperlmain.c or perlmain.c - a generated file
879cd0b9aSmillert  *
9ad15181aSmillert  *    Copyright (C) 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
109f11ffb7Safresh1  *    2004, 2005, 2006, 2007, 2016 by Larry Wall and others
1179cd0b9aSmillert  *
1279cd0b9aSmillert  *    You may distribute under the terms of either the GNU General Public
1379cd0b9aSmillert  *    License or the Artistic License, as specified in the README file.
1479cd0b9aSmillert  *
1579cd0b9aSmillert  */
1679cd0b9aSmillert 
174a4f25f9Sdownsj /*
18df042708Smillert  *      The Road goes ever on and on
19df042708Smillert  *          Down from the door where it began.
20df042708Smillert  *
21df042708Smillert  *     [Bilbo on p.35 of _The Lord of the Rings_, I/i: "A Long-Expected Party"]
22df042708Smillert  *     [Frodo on p.73 of _The Lord of the Rings_, I/iii: "Three Is Company"]
234a4f25f9Sdownsj  */
244a4f25f9Sdownsj 
25f3142520Smillert /* This file contains the main() function for the perl interpreter.
26f3142520Smillert  * Note that miniperlmain.c contains main() for the 'miniperl' binary,
279f11ffb7Safresh1  * while perlmain.c contains main() for the 'perl' binary. The typical
289f11ffb7Safresh1  * difference being that the latter includes Dynaloader.
29f3142520Smillert  *
30f3142520Smillert  * Miniperl is like perl except that it does not support dynamic loading,
31f3142520Smillert  * and in fact is used to build the dynamic modules needed for the 'real'
32f3142520Smillert  * perl executable.
339f11ffb7Safresh1  *
349f11ffb7Safresh1  * The content of the body of this generated file is mostly contained
359f11ffb7Safresh1  * in Miniperl.pm - edit that file if you want to change anything.
36b46d8ef2Safresh1  * miniperlmain.c is generated by running regen/miniperlmain.pl, while
379f11ffb7Safresh1  * perlmain.c is built automatically by Makefile (so the former is
389f11ffb7Safresh1  * included in the tarball while the latter isn't).
39f3142520Smillert  */
40f3142520Smillert 
41ba47ec9dSmillert #ifdef OEMVS
4279cd0b9aSmillert #ifdef MYMALLOC
43f3142520Smillert /* sbrk is limited to first heap segment so make it big */
4479cd0b9aSmillert #pragma runopts(HEAP(8M,500K,ANYWHERE,KEEP,8K,4K) STACK(,,ANY,) ALL31(ON))
4579cd0b9aSmillert #else
4679cd0b9aSmillert #pragma runopts(HEAP(2M,500K,ANYWHERE,KEEP,8K,4K) STACK(,,ANY,) ALL31(ON))
4779cd0b9aSmillert #endif
48ba47ec9dSmillert #endif
49ba47ec9dSmillert 
50e2e5c5d3Smillert #define PERL_IN_MINIPERLMAIN_C
51b46d8ef2Safresh1 
52b46d8ef2Safresh1 /* work round bug in MakeMaker which doesn't currently (2019) supply this
53b46d8ef2Safresh1  * flag when making a statically linked perl */
54b46d8ef2Safresh1 #define PERL_CORE 1
55b46d8ef2Safresh1 
56e5157e49Safresh1 #include "EXTERN.h"
574a4f25f9Sdownsj #include "perl.h"
58e5157e49Safresh1 #include "XSUB.h"
594a4f25f9Sdownsj 
60e2e5c5d3Smillert static void xs_init (pTHX);
614a4f25f9Sdownsj static PerlInterpreter *my_perl;
624a4f25f9Sdownsj 
637bfa9f44Smillert #ifdef NO_ENV_ARRAY_IN_MAIN
647bfa9f44Smillert extern char **environ;
657bfa9f44Smillert int
main(int argc,char ** argv)667bfa9f44Smillert main(int argc, char **argv)
677bfa9f44Smillert #else
686345ca90Smillert int
696345ca90Smillert main(int argc, char **argv, char **env)
707bfa9f44Smillert #endif
714a4f25f9Sdownsj {
720dc2eaceSmillert     int exitstatus, i;
7348950c12Ssthen #ifndef NO_ENV_ARRAY_IN_MAIN
7448950c12Ssthen     PERL_UNUSED_ARG(env);
7548950c12Ssthen #endif
764a4f25f9Sdownsj 
7779cd0b9aSmillert     /* if user wants control of gprof profiling off by default */
7879cd0b9aSmillert     /* noop unless Configure is given -Accflags=-DPERL_GPROF_CONTROL */
7979cd0b9aSmillert     PERL_GPROF_MONCONTROL(0);
8079cd0b9aSmillert 
817bfa9f44Smillert #ifdef NO_ENV_ARRAY_IN_MAIN
827bfa9f44Smillert     PERL_SYS_INIT3(&argc,&argv,&environ);
837bfa9f44Smillert #else
84e2e5c5d3Smillert     PERL_SYS_INIT3(&argc,&argv,&env);
857bfa9f44Smillert #endif
864a4f25f9Sdownsj 
877bfa9f44Smillert #if defined(USE_ITHREADS)
8879cd0b9aSmillert     /* XXX Ideally, this should really be happening in perl_alloc() or
8979cd0b9aSmillert      * perl_construct() to keep libperl.a transparently fork()-safe.
9079cd0b9aSmillert      * It is currently done here only because Apache/mod_perl have
9179cd0b9aSmillert      * problems due to lack of a call to cancel pthread_atfork()
9279cd0b9aSmillert      * handlers when shared objects that contain the handlers may
9379cd0b9aSmillert      * be dlclose()d.  This forces applications that embed perl to
9479cd0b9aSmillert      * call PTHREAD_ATFORK() explicitly, but if and only if it hasn't
9579cd0b9aSmillert      * been called at least once before in the current process.
9679cd0b9aSmillert      * --GSAR 2001-07-20 */
9779cd0b9aSmillert     PTHREAD_ATFORK(Perl_atfork_lock,
9879cd0b9aSmillert                    Perl_atfork_unlock,
9979cd0b9aSmillert                    Perl_atfork_unlock);
10079cd0b9aSmillert #endif
10179cd0b9aSmillert 
102b8851fccSafresh1     PERL_SYS_FPU_INIT;
103b8851fccSafresh1 
1046345ca90Smillert     if (!PL_do_undump) {
1054a4f25f9Sdownsj 	my_perl = perl_alloc();
1064a4f25f9Sdownsj 	if (!my_perl)
1074a4f25f9Sdownsj 	    exit(1);
1084a4f25f9Sdownsj 	perl_construct(my_perl);
1096345ca90Smillert 	PL_perl_destruct_level = 0;
1104a4f25f9Sdownsj     }
11179cd0b9aSmillert     PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
112*e0680481Safresh1     if (!perl_parse(my_perl, xs_init, argc, argv, (char **)NULL)) {
113*e0680481Safresh1 
114*e0680481Safresh1         /* perl_parse() may end up starting its own run loops, which
115*e0680481Safresh1          * might end up "leaking" PL_restartop from the parse phase into
116*e0680481Safresh1          * the run phase which then ends up confusing run_body(). This
117*e0680481Safresh1          * leakage shouldn't happen and if it does its a bug.
118*e0680481Safresh1          *
119*e0680481Safresh1          * Note we do not do this assert in perl_run() or perl_parse()
120*e0680481Safresh1          * as there are modules out there which explicitly set
121*e0680481Safresh1          * PL_restartop before calling perl_run() directly from XS code
122*e0680481Safresh1          * (Coro), and it is conceivable PL_restartop could be set prior
123*e0680481Safresh1          * to calling perl_parse() by XS code as well.
124*e0680481Safresh1          *
125*e0680481Safresh1          * What we want to check is that the top level perl_parse(),
126*e0680481Safresh1          * perl_run() pairing does not allow a leaking PL_restartop, as
127*e0680481Safresh1          * that indicates a bug in perl. By putting the assert here we
128*e0680481Safresh1          * can validate that Perl itself is operating correctly without
129*e0680481Safresh1          * risking breakage to XS code under DEBUGGING. - Yves
130*e0680481Safresh1          */
131*e0680481Safresh1         assert(!PL_restartop);
132*e0680481Safresh1 
13379cd0b9aSmillert         perl_run(my_perl);
134*e0680481Safresh1     }
1354a4f25f9Sdownsj 
13648950c12Ssthen #ifndef PERL_MICRO
1370dc2eaceSmillert     /* Unregister our signal handler before destroying my_perl */
138e9ce3842Safresh1     for (i = 1; PL_sig_name[i]; i++) {
1390dc2eaceSmillert 	if (rsignal_state(PL_sig_num[i]) == (Sighandler_t) PL_csighandlerp) {
1400dc2eaceSmillert 	    rsignal(PL_sig_num[i], (Sighandler_t) SIG_DFL);
1410dc2eaceSmillert 	}
1420dc2eaceSmillert     }
14348950c12Ssthen #endif
1440dc2eaceSmillert 
14579cd0b9aSmillert     exitstatus = perl_destruct(my_perl);
14679cd0b9aSmillert 
1474a4f25f9Sdownsj     perl_free(my_perl);
1484a4f25f9Sdownsj 
149e9ce3842Safresh1     PERL_SYS_TERM();
150e9ce3842Safresh1 
1514a4f25f9Sdownsj     exit(exitstatus);
1524a4f25f9Sdownsj }
1534a4f25f9Sdownsj 
1544a4f25f9Sdownsj /* Register any extra external extensions */
1554a4f25f9Sdownsj 
1564a4f25f9Sdownsj 
1574a4f25f9Sdownsj static void
xs_init(pTHX)158e2e5c5d3Smillert xs_init(pTHX)
1594a4f25f9Sdownsj {
1604a4f25f9Sdownsj     dXSUB_SYS;
161e5157e49Safresh1     PERL_UNUSED_CONTEXT;
1624a4f25f9Sdownsj }
163ad15181aSmillert 
164*e0680481Safresh1 /* ex: set ro ft=c: */
165