home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / CodeWarrior Lite / Metrowerks C⁄C++ Lite / Libraries / Runtime / Runtime PPC / Sources / Startup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-16  |  2.4 KB  |  95 lines  |  [TEXT/MPCC]

  1.  
  2. /*
  3.  *    Startup.c        -    Default init/startup/termination routines for Metrowerks C++ (PowerPC)
  4.  *
  5.  *    Copyright © 1993 metrowerks inc. All Rights Reserved.
  6.  *
  7.  */
  8.  
  9. #include <Quickdraw.h>
  10. #include <SegLoad.h>
  11. #include <setjmp.h>
  12. #include <MWException.h>
  13.  
  14.     /*    public data        */
  15.  
  16. QDGlobals qd;                        /*    define the Quickdraw globals here!    */
  17. jmp_buf __program_exit;                /*    exit() does a longjmp() to here        */
  18. void (*__atexit_hook)(void);        /*    atexit()  sets this up if it is ever called    */
  19. void (*___atexit_hook)(void);        /*    _atexit() sets this up if it is ever called    */
  20. int __aborting;                        /*    abort() sets this and longjmps to __program_exit    */
  21.  
  22.  
  23.      /*    external references    */
  24.  
  25. extern int main(int argc, char **argv);
  26.  
  27.  
  28.     /*    prototypes    */
  29.  
  30. void __sinit(void);
  31. void __start(void);
  32.  
  33.  
  34. /*
  35.  *    __sinit        -    stub for static initialization of C++ objects
  36.  *
  37.  *    The linker replaces the contents of this module with something of the form:
  38.  *
  39.  *            mflr    r0
  40.  *            stw        r0,8(sp)
  41.  *            stwu    sp,-64(sp)
  42.  *            bl        __sinit_<file1>
  43.  *            bl        __sinit_<file2>
  44.  *            ...
  45.  *            bl        __sinit_<fileN>
  46.  *            addi    sp,sp,64
  47.  *            lwz        r0,8(sp)
  48.  *            mtlr    r0
  49.  *            blr
  50.  */
  51.  
  52. void __sinit(void)
  53. {
  54. }
  55.  
  56.  
  57. /*
  58.  *    __start    -    Default startup routine for Metrowerks C++ (PowerPC)
  59.  *
  60.  *    This routine should be specified as the PEF main routine in the container
  61.  *    for any application. The program startup/termination sequence is:
  62.  *
  63.  *    1.    Set up a jmp_buf for exit() to jump to
  64.  *    2.    Call all static initializers--these might call exit()
  65.  *    3.    Set up default values for 'argc' and 'argv' and call main()
  66.  *    4.    Execute any atexit() routines
  67.  *    5.    Destroy all static objects
  68.  *    6.    Execute any cleanup code, e.g. close open files, console window, etc.
  69.  *    7.    Terminate the Macintosh program
  70.  *
  71.  *    We postulate that there are 2 kinds of atexit() routines: those which must
  72.  *    execute before any  objects are destroyed, and those which must execute after.
  73.  *    We assume there is a non-standard _atexit() routine which can be used to install
  74.  *    the post-destruction exit handlers.
  75.  *
  76.  */
  77.  
  78. void __start(void)
  79. {
  80.     char *argv = 0;
  81.     
  82.     if (setjmp(__program_exit) == 0) {    //    set up jmp_buf for exit()
  83.         __sinit();                        //    call all static initializers
  84.         main(0, &argv);                    //    call main(argc, argv)
  85.         if (__atexit_hook)
  86.             __atexit_hook();            //    call atexit() procs
  87.     }
  88.     if (!__aborting) {
  89.         __destroy_global_chain();        //    destroy static objects
  90.         if (___atexit_hook)
  91.             ___atexit_hook();            //    call _atexit() procs
  92.     }
  93. //    ExitToShell();
  94. }
  95.