home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / GCC / GERLIB_DEV08B.LHA / gerlib / amiga / normal / build_argc_argv.c next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  6.4 KB  |  346 lines

  1. #include <exec/types.h>
  2. #include <exec/ports.h>
  3. #include <exec/memory.h>
  4. #include <exec/execbase.h>
  5. #include <dos/dos.h>
  6. #include <dos/dosextens.h>
  7. #include <dos/exall.h>
  8. #include <dos/rdargs.h>
  9. #include <clib/alib_protos.h>
  10. #include <clib/alib_stdio_protos.h>
  11. #include <intuition/intuitionbase.h>
  12. #include <graphics/gfxbase.h>
  13. #include <inline/stubs.h>
  14. #ifdef __OPTIMIZE__
  15. #include <inline/exec.h>
  16. #include <inline/dos.h>
  17. #include <inline/intuition.h>
  18. #else
  19. #include <clib/exec_protos.h>
  20. #include <clib/dos_protos.h>
  21. #include <clib/intuition_protos.h>
  22. #endif
  23.  
  24. int     atexit(void (*)(void));
  25.  
  26. /* build_argc_argv will build the argv array pointer and store
  27.    the number of arguements in argc. The length of the provided
  28.    commandline or the number of arguements should not be limited too much.
  29.    I think 100 (MAX_TEMPLATE_ITEMS) arguements should be enough
  30.  
  31.     We also have to parse workbench startups !
  32. */
  33.  
  34. /* things for commandline-parsing */
  35.  
  36. static struct RDArgs    *rda = NULL;    /* for CLI-startup */
  37.  
  38. static LONG **arguement_array=NULL;
  39.  
  40. static char *arguement_string=NULL;
  41.  
  42. static UBYTE    **ToolTypes=NULL;     /* for WB-startup */
  43.  
  44.  
  45. static char *MyGetProgramName(void);
  46.  
  47. UBYTE *__default_wb_window=NULL;
  48.  
  49. void destroy_argc_argv(void)
  50. {
  51.     /* Free all the space that was occupied by build_argc_argv */
  52.  
  53.     if(arguement_string)
  54.         FreeVec(arguement_string);
  55.  
  56.     if(arguement_array)
  57.     {
  58.             /* Free Programm-name */
  59.  
  60.         if(arguement_array[0])
  61.             FreeVec(arguement_array[0]);
  62.  
  63.         FreeVec(arguement_array);
  64.     }
  65.  
  66.         /* free WB-arguments */
  67.  
  68.     if( ToolTypes )
  69.     {
  70.         ArgArrayDone();
  71.     }
  72.  
  73.  
  74.         /* Free arguments passed by commandline allocated by ReadArgs() */
  75.  
  76.     if( rda )
  77.     {
  78.         FreeArgs( rda );
  79.         FreeDosObject( DOS_RDARGS, rda );
  80.     }
  81.  
  82. }
  83.  
  84. BOOL build_argc_argv(int orig_d0, char *orig_a0, int *argc, char **argv[])
  85. {
  86.     /* We will store the name of our program in argv[0] */
  87.  
  88.     *argc=0;
  89.     *argv=NULL;
  90.  
  91.     if(orig_d0)
  92.     {
  93.         /* start from CLI */
  94.         /* parse arguements */
  95.  
  96.             /* copy the arguement string for security reasons */
  97.             /* first: get len, arguement string can be terminated by zero or return (0x0a) */
  98.  
  99.         char *s=orig_a0;
  100.         int arguement_len=0;
  101.  
  102.         if(orig_a0)
  103.         {
  104.             while( (*s) && (*s !='\n'))
  105.             {
  106.                 s++;
  107.                 arguement_len++;
  108.             }
  109.  
  110.             if(arguement_len)
  111.             {
  112.                     /* we need a zero-terminated buffer */
  113.  
  114.                 if( (arguement_string=AllocVec(arguement_len+2,MEMF_CLEAR)) )
  115.                 {
  116.                     int x;
  117.  
  118.                         /* copy commandline */
  119.  
  120.                     for(x=0;x<arguement_len;x++)
  121.                         arguement_string[x]=orig_a0[x];
  122.  
  123.  
  124.                         /* terminate buffer with return for ReadArgs */
  125.  
  126.                     arguement_string[x] = '\n';
  127.                     arguement_string[x+1] = 0;
  128.  
  129.  
  130.                 }
  131.                 else
  132.                 {
  133.                     /* out of memory error */
  134.                     return FALSE;
  135.                 }
  136.             }
  137.             else
  138.             {
  139.                 /* the string is empty */
  140.             }
  141.         }
  142.         else
  143.         {
  144.             /* we have a NULL-pointer, we can do nothing */
  145.         }
  146.  
  147.  
  148.  
  149.         /* here we have a return-terminated string in arguement_string,
  150.            or nothing */
  151.  
  152.         if(arguement_string)
  153.         {
  154.             /* Get memory for the arguement array */
  155.  
  156.             if( (arguement_array = AllocVec(sizeof(long)*110+100, MEMF_CLEAR)))
  157.             {
  158.                     /* We have the memory */
  159.  
  160.                     /* Set up arguement template */
  161.  
  162.                 int x;
  163.                 UBYTE *template=(UBYTE *)&arguement_array[110];
  164.                 UBYTE *s=template;
  165.  
  166.                 for(x=0;x<99;x++) *s++=',';
  167.  
  168.                     /* Set the program name */
  169.  
  170.                 arguement_array[0] = (LONG *)MyGetProgramName();
  171.  
  172.                     /* Do ReadArgs */
  173.  
  174.                 if( (rda = (struct RDArgs *)AllocDosObject( DOS_RDARGS, NULL )))
  175.                 {
  176.                         /* Setup CSource */
  177.  
  178.                     rda->RDA_Source.CS_Buffer = arguement_string;
  179.                     rda->RDA_Source.CS_Length = strlen(arguement_string);
  180.  
  181.                     if(ReadArgs( template, (LONG *) &arguement_array[1], rda ) )
  182.                     {
  183.                         /* Args erfolgreich gelesen */
  184.  
  185.                         /* Count args to store them in argc */
  186.  
  187.                         int x=1;
  188.  
  189.                         while( arguement_array[x] )
  190.                             x++;
  191.  
  192.                         *argc=x;
  193.                         *argv=(char **)arguement_array;
  194.  
  195.                     }
  196.                     else
  197.                     {
  198.                         /* ReadArgs error: give it to stdout */
  199.  
  200.                         PrintFault(IoErr(), "");
  201.                         return FALSE;
  202.                     }
  203.                 }
  204.                 else
  205.                 {
  206.                     /* panic, we don't have the memory for RDArgs */
  207.                     return FALSE;
  208.                 }
  209.             }
  210.             else
  211.             {
  212.                 /* panic, we don't have the memory for an arguement array */
  213.                 return FALSE;
  214.             }
  215.         }
  216.         else
  217.         {
  218.             /* we have only to initialize argv[0]/[1] */
  219.  
  220.             if( (arguement_array = (LONG **)AllocVec(sizeof(long)*2, MEMF_CLEAR)))
  221.             {
  222.                 arguement_array[0] = (LONG *)MyGetProgramName();
  223.                 *argc    = 1;
  224.                 *argv    = (char **)arguement_array;
  225.             }
  226.             else
  227.             {
  228.                 /* panic, we don't have enough memory ! */
  229.                 return FALSE;
  230.             }
  231.  
  232.         }
  233.     }
  234.     else
  235.     {
  236.         /* Workbench-Start, needs icon.library to be open !! */
  237.  
  238.         ToolTypes = (UBYTE **)ArgArrayInit((LONG)orig_d0,(UBYTE **)orig_a0);
  239.  
  240.         if(ToolTypes)
  241.         {
  242.             /* As we want to have our program name in argv[0], we have
  243.                 to copy the array
  244.  
  245.                 first get the number of arguements : */
  246.  
  247.             int x=0;
  248.             while(ToolTypes[x])
  249.             {
  250.                 x++;
  251.             }
  252.  
  253.             x++;    /* Add one entry for our argv[0] */
  254.             x++;    /* Add one entry for argv[x]=NULL */
  255.  
  256.             if( (arguement_array = AllocVec(sizeof(long)*x, MEMF_CLEAR)))
  257.             {
  258.                     /* We have the memory */
  259.  
  260.                     /* Set the program name */
  261.  
  262.                 arguement_array[0] = (LONG *)MyGetProgramName();
  263.                              *argc = x-1;
  264.  
  265.  
  266.                     /* copy arguements */
  267.  
  268.                 x=0;
  269.                 while(ToolTypes[x])
  270.                 {
  271.                     arguement_array[x+1]=(LONG *)ToolTypes[x];
  272.                     x++;
  273.                 }
  274.  
  275.                     /* now look at an entry that starts with "WINDOW=",
  276.                         we will cancel that entry */
  277.  
  278.                 {
  279.                     int cancel_entry=0;
  280.                     x=1;
  281.  
  282.                     while(arguement_array[x])
  283.                     {
  284.                         if(strncmp(arguement_array[x],"WINDOW=",7)==0)
  285.                         {
  286.                             cancel_entry=x;
  287.                             break;
  288.                         }
  289.                         x++;
  290.                     }
  291.  
  292.                         /* remove the WINDOW-Entry */
  293.  
  294.                     if(cancel_entry)
  295.                     {
  296.                         __default_wb_window = (UBYTE *)(arguement_array[cancel_entry]) + 7;
  297.                         *argc-=1;
  298.                         x=cancel_entry;
  299.                         while(arguement_array[x])
  300.                         {
  301.                             arguement_array[x]=arguement_array[x+1];
  302.                             x++;
  303.                         }
  304.                     }
  305.                 }
  306.  
  307.                 *argv= (char **)arguement_array;
  308.             }
  309.             else
  310.                 return FALSE;
  311.         }
  312.     }
  313.  
  314.  
  315.     return TRUE;
  316. }
  317.  
  318.  
  319.  
  320.     /*
  321.      * MyGetProgramName()
  322.      *
  323.      * Returns a pointer to the name of the program. The User is responsible
  324.      * to free the pointer with FreeVec()!
  325.      *
  326.      * a maximum of 100 characters (incl. zero) are returned
  327.      *
  328.      * from WB there is no name, so you get a ZERO pointer!
  329.      */
  330.  
  331. static char *MyGetProgramName(void)
  332. {
  333.     UBYTE Buffer[100];
  334.  
  335.     if(GetProgramName(Buffer, 100))
  336.     {
  337.         char *mem=(char *)AllocVec(100, 0);
  338.         if(mem)
  339.             strcpy(mem, Buffer);
  340.  
  341.         return mem;
  342.     }
  343.     else
  344.         return NULL;
  345. }
  346.