home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mntlib24 / crtinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  12.0 KB  |  449 lines

  1. /*
  2.  *
  3.  * Crtinit: C run-time initialization code.
  4.  * Written by Eric R. Smith, and placed in the public domain.
  5.  * Use at your own risk.
  6.  *
  7.  * 01/03/89 ++jrb
  8.  *    The (new) meaning of _stksize: (thanks to allan pratt for the feedback)
  9.  *
  10.  *    _stksize            meaning
  11.  *      -1L        keep all of memory (except MINFREE at top) and do
  12.  *            mallocs from own heap, with heap grown upwards towards
  13.  *            stack, and the stack growing down towards heap,
  14.  *            with a minimum slush between them so that they
  15.  *            dont meet (only checked while malloc'ing). With
  16.  *            this model, further spawning is not possible, but it is
  17.  *            well suited for programs such as gcc-cc1 etc.
  18.  *            Thanks to Piet van Oostrum & Atze Dijkstra for this idea
  19.  *
  20.  *    0L        keep minimum amount of memory. this is also the
  21.  *            case when _stksize is undefined by the user.
  22.  *    1L        keep 1/4 of memory, free 3/4 ( as in Alcyon GEMSTART)
  23.  *    2L        keep 2/4 (1/2), free rest
  24.  *    3L        keep 3/4, free 1/4
  25.  *    other        keep that many bytes
  26.  *    -other        keep |other| bytes, use the heap for mallocs
  27.  *
  28.  * 02/14/90 ++jrb (thanks edgar)
  29.  *    auto acc detect
  30.  *    undump friendly
  31.  *
  32.  * NOTE: dumping applications should use _initial_stack instead: if
  33.  *     !=0, then _stksize is initialized from _initial_stack, and
  34.  *     mallocs are always from internal heap. (TeX works much better now),
  35.  *     thanks edgar!
  36.  *
  37.  * Acc convention:
  38.  *      Preferred: user sets _stksize, then _heapbase is set to Malloc(_stksize)
  39.  *      sp to _heapbase + _stksize and all mallocs happen from heap
  40.  *
  41.  *      Old way:
  42.  *    user sets _heapbase to bottom of stack + heap area
  43.  *         sets _stksize to the size of this area
  44.  *         at startup, sp will be set to top of this area
  45.  *         (_heapbase  + _stksize ) and malloc()'s will happen from heap.
  46.  *        (note malloc() and *not* Malloc())
  47.  *
  48.  * 02/16/90 ++jrb
  49.  *  - bug fix: dont get screwed by desktop launch when fast bit is set
  50.  *             convert env string to format usable
  51.  *        (atari get your act together!!)
  52.  *
  53.  * 09-20-92 hyc
  54.  *    Support base relative addressing, for shared-text execution.
  55.  *    Also merged in some stuff from ++jrb's crt0.c.
  56.  *
  57.  * Turbo / Pure C version 21-6-92 um
  58.  *   the Turbo / Pure linker reserves stack space in the BSS and sets
  59.  *   the symbol _StkSize to its size. If _StkSize is set to 0, we use
  60.  *   _stksize etc. as usual
  61.  *
  62.  */
  63.  
  64. /* define this symbol to get ARGV argument passing that's strictly
  65.  * compatible with the Atari standard. If it's not defined, then
  66.  * the startup code won't validate the ARGV= variable by checking
  67.  * the command byte for 0x127. Note that there are still some
  68.  * applications (gulam is a notable example) that implement only
  69.  * part of the standard and don't set the command byte to 0x127.
  70.  */
  71.  
  72. #if 0
  73. #define STRICTLY_COMPATIBLE_WITH_STANDARD
  74. #endif
  75.  
  76. #include <basepage.h>
  77. #include <osbind.h>
  78. #include "lib.h"
  79.  
  80. #define isspace(c) ((c) == ' '||(c) == '\t')
  81. #define MINFREE    (8L * 1024L)        /* free at least this much mem */
  82.                     /* on top */
  83. #define MINKEEP (8L * 1024L)        /* keep at least this much mem */
  84.  
  85. BASEPAGE *_base;
  86. char **environ;
  87. static long argc;
  88. static char **argv;
  89.  
  90. /* size to be allocated for the stack */
  91. extern long _stksize;        /* picked up from user or from stksiz.c */
  92.  
  93. /* set to heap base addr when _stksize < 0 || _initial_stack || When DA
  94.  * note that we allow the user to provide a _heapbase of their own, since
  95.  * that used to be necessary for acc's; but that is no longer needed
  96.  * (or encouraged) since the acc startup code now Mallocs a heap
  97.  * automatically
  98.  */
  99. extern void *_heapbase;        /* picked up from user or from heapbase.c */
  100.  
  101. /*
  102.  * initial stack is used primarily by dumping application,
  103.  * if it is, malloc is always from heap, and _stksize is init
  104.  * from initial_stack (to preserve the value in the undumped run)
  105.  */
  106. extern long _initial_stack;    /* picked up from user or from inistack.c */
  107.  
  108. #ifdef __TURBOC__
  109. char *_StkLim;    /* for Turbo / Pure C stack checking */
  110. extern _StkSize; /* set by linker */
  111. #endif
  112.  
  113. /* default sizeof stdio buffers */
  114. size_t __DEFAULT_BUFSIZ__;    /* .comm             */
  115.  
  116. /* are we an app? */
  117. short _app;
  118.  
  119. /* are we on a split addr mem ST */
  120. short _split_mem = 0;
  121.  
  122. static long parseargs    __PROTO((BASEPAGE *));
  123. static void setup_handlers    __PROTO((void));
  124. __EXTERN void _main    __PROTO((long, char **, char **));
  125. __EXTERN void _init_signal    __PROTO((void));
  126. __EXTERN void _monstartup __PROTO((void *lowpc, void *highpc));
  127. __EXTERN void __mcleanup __PROTO((void));
  128. __EXTERN void _moncontrol __PROTO((long));
  129. __EXTERN void _setstack __PROTO((char *));
  130.  
  131. /*
  132.  * accessories start here:
  133.  */
  134.  
  135. static char    *acc_argv[] = {"", (char *) 0}; /* no name and no arguments */
  136.  
  137. void _acc_main()
  138. {
  139. #ifdef __TURBOC__
  140.     if (&_StkSize == 0)
  141.     {
  142. #endif
  143.  
  144.     if (_stksize == 0 || _stksize == -1L)
  145.         _stksize = MINKEEP;
  146.  
  147.     if (_stksize < 0)
  148.         _stksize = -_stksize;
  149.     _stksize &= 0xfffffffeL;    /* stack on word boundary */
  150.  
  151. #ifdef __TURBOC__
  152.     }
  153.     else
  154.     {
  155.         /* This compound statement is executed if the Pure linker
  156.         has reserved space for the stack in the BSS */
  157.         _stksize = (long) &_StkSize;
  158.         _heapbase = _base->p_bbase + _base->p_blen - _stksize;
  159.     }
  160.     _StkLim = (char *) _heapbase + 256;     /* for stack checking */
  161. #endif
  162.   
  163.     if (_heapbase == 0) {
  164.         _heapbase = (void *)Malloc(_stksize);
  165.     }
  166.     _setstack((char *) _heapbase + _stksize);
  167.     _app = 0;                /* this is an accessory */
  168.  
  169.     _main(1L, acc_argv, acc_argv);
  170.     /*NOTREACHED*/
  171. }
  172.  
  173. void _crtinit()
  174. {
  175.     register BASEPAGE *bp;
  176.     register long m;
  177.     register long freemem;
  178. #ifdef __GNUC__
  179.     extern void etext();    /* a "function" to fake out pc-rel addressing */
  180. #endif
  181.     _app = 1;    /* its an application */
  182.     if(!__DEFAULT_BUFSIZ__)
  183.         __DEFAULT_BUFSIZ__ = BUFSIZ;
  184.  
  185.     bp = _base;
  186.  
  187.     m = parseargs(bp);    /* m = # bytes used by environment + args */
  188.  
  189. /* make m the total number of bytes required by program sans stack/heap */
  190.     m += (bp->p_tlen + bp->p_dlen + bp->p_blen + sizeof(BASEPAGE));
  191.     m = (m + 3L) & (~3L);
  192.  
  193. #ifdef __TURBOC__
  194.     if (&_StkSize == 0)
  195.     {
  196. #endif
  197.  
  198. /* freemem the amount of free mem accounting for MINFREE at top */
  199.     if((freemem = (long)bp->p_hitpa - (long)bp - MINFREE - m) <= 0L)
  200.         goto notenough;
  201.     
  202.     if(_initial_stack)
  203.     {
  204.         /* the primary use of _initial_stack will be in dumping */
  205.         /* applications where only a heap for malloc makes sense */
  206.         _heapbase = (void *) ((long)bp + m);
  207.         _stksize = _initial_stack;
  208.     } else {
  209.         if (_stksize >= -1L)
  210.         _split_mem = 1; /* malloc from Malloc first, then from own heap */
  211.     }
  212.  
  213.     switch(_stksize)
  214.     {
  215.       case -1L:    /* keep all but MINFREE, and malloc from own heap */
  216.         _stksize = freemem;
  217.         _heapbase = (void *) ((long)bp + m);
  218.         break;
  219.         
  220.       case 0L:    /* free all but MINKEEP */
  221.         _stksize = MINKEEP;
  222.         break;
  223.  
  224.       case 1L:    /* keep 1/4, free 3/4 */
  225.         _stksize = freemem >> 2;
  226.         break;
  227.  
  228.       case 2L:    /* keep 1/2, free 1/2 */
  229.         _stksize = freemem >> 1;
  230.         break;
  231.  
  232.       case 3L:    /* keep 3/4, free 1/4 */
  233.         _stksize = freemem - (freemem >> 2); 
  234.         break;
  235.         
  236.       default:
  237.         if(_stksize < -1L) { /* keep |_stksize|, use heap for mallocs */
  238.         _stksize = -_stksize;
  239.         _heapbase = (void *)((long)bp + m);
  240.         }
  241.     }
  242.     
  243. /* make m the total number of bytes including stack */
  244.     _stksize = _stksize & (~3L);
  245.     m += _stksize;
  246.  
  247. /* make sure there's enough room for the stack */
  248.     if (((long)bp + m) > ((long)bp->p_hitpa - MINFREE))
  249.         goto notenough;
  250.  
  251. /* set up the new stack to bp + m  */
  252.  
  253. #ifdef __TURBOC__
  254.     {
  255.         char *tmp;
  256.         tmp = (char *) bp + m;
  257.         _setstack(tmp);
  258.         _StkLim = tmp - _stksize + 256; /* for stack checking */
  259.     }
  260. #else
  261.     _setstack((char *)bp + m);
  262. #endif
  263.  
  264. #ifdef __TURBOC__
  265.     } /* if (&_StkSize == 0) */
  266.     else
  267.     {
  268.         /* This compound statement is executed if the Pure linker
  269.         has reserved space for the stack in the BSS */
  270.         _stksize = (long) &_StkSize;
  271.         _stksize = _stksize & (~3L);
  272.         
  273.         {
  274.             char *tmp;
  275.             tmp = (char *) bp->p_bbase + bp->p_blen;
  276.             _setstack(tmp);
  277.             _StkLim = tmp - _stksize + 256; /* for stack checking */
  278.             _heapbase = NULL; /* no mallocs from heap */
  279.         }
  280.     }
  281. #endif /* __TURBOC__ */
  282.  
  283. /* shrink the TPA */
  284.     (void)Mshrink(bp, m);
  285.  
  286. /* establish handlers,  call the main routine */
  287.     setup_handlers();
  288.  
  289. /* start profiling, if we were linked with gcrt0.o */
  290. #ifdef __GNUC__
  291.     _monstartup((void *)bp->p_tbase, (void *)((long)etext - 1));
  292. #else
  293.     _monstartup((void *)(bp->p_tbase), 
  294.            (void *)((long)bp->p_tbase +  bp->p_tlen));
  295. #endif
  296.  
  297.     _main(argc, argv, environ);
  298.     /* not reached normally */
  299.  
  300. notenough:
  301.     Cconws("Fatal error: insufficient memory\r\n");
  302.         Pterm(-1);
  303. }
  304.  
  305.  
  306. /*
  307.  * parseargs(bp): parse the environment and arguments pointed to by the
  308.  * basepage. Return the number of bytes of environment and arguments
  309.  * that have been appended to the bss area (the environ and argv arrays
  310.  * are put here, as is a temporary buffer for the command line, if
  311.  * necessary).
  312.  *
  313.  * The MWC extended argument passing scheme is assumed.
  314.  *
  315.  */
  316.  
  317. static long parseargs(bp)
  318.     BASEPAGE *bp;
  319. {
  320.     long count = 4;        /* compensate for aligning */
  321.     long  i;
  322.     char *from, *cmdln, *to;
  323.     char **envp, **arg;
  324. /* flag to indicate desktop-style arg. passing */
  325.     long desktoparg = 0;
  326.  
  327. /* handle the environment first */
  328.  
  329.     environ = envp = (char **)(( (long)bp->p_bbase + bp->p_blen + 4) & (~3));
  330.     from = bp->p_env;
  331.     while (*from) {
  332.  
  333. /* if we find MWC arguments, tie off environment here */
  334.         if (*from == 'A' && *(from+1) == 'R' && *(from+2) == 'G' &&
  335.             *(from+3) == 'V' && *(from+4) == '=') {
  336.             *envp++ = (char *) 0; count += 4;
  337.             *from++ = 0;
  338. #ifdef STRICTLY_COMPATIBLE_WITH_STANDARD
  339.             if (bp->p_cmdlin[0] != 127)
  340.                 goto old_cmdlin;
  341. #endif
  342.             while (*from++) ; /* skip ARGV= string */
  343.             argv = arg = envp++;
  344.             *arg++ = from; count+= 4;
  345.             while (*from++) ; /* skip argv[0] */
  346.             goto do_argc;
  347.         }
  348.         *envp++ = from;
  349.         count += 4;
  350.         desktoparg = 1;
  351.         while (*from) {
  352.             if (*from == '=') {
  353.                 desktoparg = 0;
  354.             }
  355.             from++;
  356.         }
  357.         from++;        /* skip 0 */
  358.  
  359. /* the desktop (and some shells) use the environment in the wrong
  360.    way, putting in "PATH=\0C:\0" instead of "PATH=C:". so if we
  361.    find an "environment variable" without an '=' in it, we
  362.    see if the last environment variable ended with '=\0', and
  363.    if so we append this one to the last one
  364.  */
  365.         if(desktoparg && envp > &environ[1]) 
  366.         {
  367.         /* launched from desktop -- fix up env */
  368.             char *p, *q;
  369.  
  370.             q = envp[-2];    /* current one is envp[-1] */
  371.             while (*q) q++;
  372.             if (q[-1] == '=') {
  373.             p = *--envp;
  374.             while(*p)
  375.                *q++ = *p++;
  376.                 *q = '\0';
  377.            }
  378.         }
  379.     }
  380.     *envp++ = (char *)0;
  381.     count += 4;
  382.  
  383. #ifdef STRICTLY_COMPATIBLE_WITH_STANDARD
  384. old_cmdlin:
  385. #endif
  386. /* Allocate some room for the command line to be parsed */
  387.     cmdln = bp->p_cmdlin;
  388.     i = *cmdln++;
  389.     from = to = (char *) envp;
  390.     if (i > 0) {
  391.         count += (i&(~3));
  392.         envp = (char **) ( ((long) envp)  + (i&(~3)) );
  393.     }
  394.     envp += 2; count += 8;
  395.  
  396. /* Now parse the command line and put argv after the environment */
  397.  
  398.     argv = arg = envp;
  399.     *arg++ = "";        /* argv[0] not available */
  400.     count += 4;
  401.     while(i > 0 && isspace(*cmdln) )
  402.         cmdln++,--i;
  403.  
  404.     while (i > 0) {
  405.         if (isspace(*cmdln)) {
  406.             --i; cmdln++;
  407.             while (i > 0 && isspace(*cmdln))
  408.                 --i,cmdln++;
  409.             *to++ = 0;
  410.         }
  411.         else {
  412.             if ((*to++ = *cmdln++) == 0) break;
  413.             --i;
  414.         }
  415.     }
  416.     *to++ = '\0';
  417.     *to = '\0'; /* bug fix example:cmdln == '\3' 'a' ' ' 'b' '\0' */
  418.     /* the loop below expects \0\0 at end to terminate! */
  419.     /* the byte @ cmdln[i+2] != 0 when fast bit is set */
  420. do_argc:
  421.     argc = 1;        /* at this point argv[0] is done */
  422.     while (*from) {
  423.         *arg++ = from;
  424.         argc++;
  425.         count += 4;
  426.         while(*from++) ;
  427.     }
  428.     *arg++ = (char *) 0;
  429.     return count+4;
  430. }
  431.  
  432. static void setup_handlers()
  433. {
  434.     _init_signal();
  435. }
  436.  
  437. /*
  438.  * _exit: does the actual exiting. Note that _moncontrol and __mcleanup are
  439.  * dummies in crt0.s, but do something in gcrt0.s
  440.  */
  441.  
  442. void _exit(status)
  443.     int status;
  444. {
  445.     _moncontrol(0L);
  446.     __mcleanup();
  447.     Pterm(status);
  448. }
  449.