home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / compiler / alib / startup.c < prev   
Encoding:
C/C++ Source or Header  |  1997-01-29  |  3.6 KB  |  190 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: startup.c,v 1.10 1997/01/29 19:09:47 digulla Exp $
  4.  
  5.     Desc: Common startup code
  6.     Lang: english
  7. */
  8. #include <setjmp.h>
  9. #include <dos/dos.h>
  10. #include <exec/memory.h>
  11. #include <proto/exec.h>
  12. #include <proto/dos.h>
  13. #include <aros/asmcall.h>
  14. #if 1
  15. #   include <aros/debug.h>
  16. #endif
  17.  
  18. /* Don't define symbols before the entry point. */
  19. extern struct ExecBase * SysBase;
  20. extern int main (int argc, char ** argv);
  21. extern APTR __startup_mempool; /* malloc() and free() */
  22. extern jmp_buf __startup_jmp_buf;
  23. extern LONG __startup_error;
  24.  
  25. /*
  26.     This won't work for normal AmigaOS because you can't expect SysBase to
  27.     be in A6. The correct way is to use *(struct ExecBase **)4 and because
  28.     gcc emits strings for a certain function _before_ the code the program
  29.     will crash immediately because the first element in the code won't be
  30.     valid assembler code.
  31. */
  32. AROS_UFH3(LONG, entry,
  33.     AROS_UFHA(char *,argstr,A0),
  34.     AROS_UFHA(ULONG,argsize,D0),
  35.     AROS_UFHA(struct ExecBase *,sysbase,A6)
  36. )
  37. {
  38.     char * args,
  39.     ** argv,
  40.      * ptr;
  41.     int    argc,
  42.        argmax;
  43.     __startup_error = RETURN_FAIL;
  44.  
  45.     SysBase=sysbase;
  46.  
  47.     /* Copy args into buffer */
  48.     if (!(args = AllocMem (argsize+1, MEMF_ANY)) )
  49.     {
  50.     argv = NULL;
  51.     goto error;
  52.     }
  53.  
  54.     ptr = args;
  55.  
  56.     while ((*ptr++ = *argstr++));
  57.  
  58.     /* Find out how many arguments we have */
  59.     for (argmax=1,ptr=args; *ptr; )
  60.     {
  61.     if (*ptr == ' ' || *ptr == '\t')
  62.     {
  63.         /* Skip whitespace */
  64.         while (*ptr && (*ptr == ' ' || *ptr == '\t'))
  65.         ptr ++;
  66.     }
  67.  
  68.     if (*ptr == '"')
  69.     {
  70.         /* "..." argument ? */
  71.         argmax ++;
  72.  
  73.         ptr ++;
  74.  
  75.         /* Skip until next " */
  76.         while (*ptr && *ptr != '"')
  77.         ptr ++;
  78.  
  79.         if (*ptr)
  80.         ptr ++;
  81.     }
  82.     else if (*ptr)
  83.     {
  84.         argmax ++;
  85.  
  86.         while (*ptr && *ptr != ' ' && *ptr != '\t')
  87.         ptr ++;
  88.     }
  89.     }
  90.  
  91.     if (!(argv = AllocMem (sizeof (char *) * argmax, MEMF_ANY)) )
  92.     {
  93.     goto error;
  94.     }
  95.  
  96.     /* Find out how many arguments we have */
  97.     for (argc=1,ptr=args; *ptr; )
  98.     {
  99.     if (*ptr == ' ' || *ptr == '\t')
  100.     {
  101.         /* Skip whitespace */
  102.         while (*ptr && (*ptr == ' ' || *ptr == '\t'))
  103.         ptr ++;
  104.     }
  105.  
  106.     if (*ptr == '"')
  107.     {
  108.         /* "..." argument ? */
  109.         ptr ++;
  110.         argv[argc++] = ptr;
  111.  
  112.         /* Skip until next " */
  113.         while (*ptr && *ptr != '"')
  114.         ptr ++;
  115.  
  116.         /* Terminate argument */
  117.         if (*ptr)
  118.         *ptr ++ = 0;
  119.     }
  120.     else if (*ptr)
  121.     {
  122.         argv[argc++] = ptr;
  123.  
  124.         while (*ptr && *ptr != ' ' && *ptr != '\t')
  125.         ptr ++;
  126.  
  127.         /* Not at end of string ? Terminate arg */
  128.         if (*ptr)
  129.         *ptr ++ = 0;
  130.     }
  131.     }
  132.  
  133.     DOSBase = (struct DosLibrary *)OpenLibrary (DOSNAME, 39);
  134.  
  135.     /* Get name of program * /
  136.     argv[0] = GetProgrammName (); */
  137.     argv[0] = "dummy";
  138.  
  139. #if 0
  140. kprintf ("arg(%d)=\"%s\", argmax=%d, argc=%d\n", argsize, argstr, argmax, argc);
  141. {
  142. int t;
  143. for (t=0; t<argc; t++)
  144.     kprintf ("argv[%d] = \"%s\"\n", t, argv[t]);
  145. }
  146. #endif
  147.  
  148.     if (DOSBase != NULL)
  149.     {
  150.     if (!setjmp (__startup_jmp_buf))
  151.         __startup_error = main (argc, argv);
  152.  
  153.     CloseLibrary((struct Library *)DOSBase);
  154.     }
  155.  
  156. error:
  157.     if (args)
  158.     {
  159.     if (argv)
  160.         FreeMem (argv, sizeof (char *) * argmax);
  161.  
  162.     FreeMem (args, argsize+1);
  163.     }
  164.  
  165.     if (__startup_mempool)
  166.     DeletePool (__startup_mempool);
  167.  
  168.     return __startup_error;
  169. } /* entry */
  170.  
  171. struct ExecBase *SysBase;
  172. struct DosLibrary *DOSBase;
  173.  
  174. APTR __startup_mempool = NULL;
  175. jmp_buf __startup_jmp_buf;
  176. LONG __startup_error;
  177.  
  178. /*    Stub function for GCC __main().
  179.  
  180.     The __main() function is originally used for C++ style constructors
  181.     and destructors in C. This replacement does nothing and gets rid of
  182.     linker-errors about references to __main().
  183. */
  184.  
  185. void __main(void)
  186. {
  187. /* Do nothing. */
  188. }
  189.  
  190.