home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / amiga / libnix-0.7-src.lha / libnix-0.7 / sources / nixmain / __nocommandline.c next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  3.0 KB  |  140 lines

  1. #include <stdlib.h>
  2. #include <exec/memory.h>
  3. #include <workbench/startup.h>
  4. #ifdef __GNUC__
  5. #include <inline/exec.h>
  6. #include <dos/dos.h>
  7. #include <inline/dos.h>
  8. #else
  9. #include <clib/exec_protos.h>
  10. #include <clib/dos_protos.h>
  11. #endif
  12. #include <stabs.h>
  13.  
  14. extern int    __argc; /* Defined in startup */
  15. extern char **__argv;
  16. extern char  *__commandline;
  17. extern struct WBStartup *_WBenchMsg;
  18.  
  19. extern char __stdiowin[];
  20.  
  21. static char *cline=NULL; /* Copy of commandline */
  22. static BPTR cd=0l;       /* Lock for Current Directory */
  23. static BPTR window=0l;   /* CLI-window for start from workbench */
  24.  
  25. /* This guarantees that this module gets linked in.
  26.    If you replace this by an own reference called
  27.    __nocommandline you get no commandline arguments */
  28. ALIAS(__nocommandline,__initcommandline);
  29.  
  30. void __initcommandline(void)
  31. {
  32.   struct WBStartup **wbs;
  33.   char ***av;
  34.  
  35.   if(*(wbs=&_WBenchMsg)!=NULL)
  36.   { if(__stdiowin[0])
  37.     { BPTR *win;
  38.  
  39.       if((*(win=&window)=Open(__stdiowin,MODE_OLDFILE))==0l)
  40.         exit(RETURN_FAIL);
  41.       SelectInput(*win);
  42.       SelectOutput(*win);
  43.     }
  44.     if((*wbs)->sm_ArgList!=NULL) /* cd to icon */
  45.       cd=CurrentDir(DupLock((*wbs)->sm_ArgList->wa_Lock));
  46.  
  47.     __argc=0;
  48.     __argv=(char **)*wbs;
  49.   }else
  50.   { 
  51.     size_t i;
  52.     char *a,*cl=__commandline;
  53.  
  54.     for(i=0;cl[i]!='\n';i++) /* calculate size of commandline */
  55.       ;
  56.     if(!(cline=(char *)AllocVec(i+1,MEMF_ANY)))
  57.       exit(RETURN_FAIL);
  58.   
  59.     a=cline; /* and parse it */
  60.     __argc=1;
  61.     while(*cl!='\n')
  62.     {
  63.       if(*cl!='\"')
  64.         while(*cl!='\n'&&*cl!=' '&&*cl!='\t')
  65.           *a++=*cl++;
  66.       else
  67.       { cl++;
  68.         while(*cl!='\n'&&*cl!='\"')
  69.         {
  70.           if(*cl=='*')
  71.             if(*++cl=='\n')
  72.               break;
  73.           *a++=*cl++;
  74.         }
  75.       }
  76.       if(*cl!='\n')
  77.       {
  78.         cl++;
  79.         while(*cl==' '||*cl=='\t')
  80.           cl++;
  81.       }
  82.       *a++='\0';
  83.       __argc++;
  84.     }
  85.       /* NULL Terminated */
  86.     if(!(*(av=&__argv)=(char **)AllocVec((__argc+1)*sizeof(char *),MEMF_ANY|MEMF_CLEAR)))
  87.       exit(RETURN_FAIL);
  88.     
  89.     a=cline;
  90.     for(i=1;i<__argc;i++)
  91.     { 
  92.       (*av)[i]=a;
  93.       while(*a++)
  94.         ; 
  95.     }
  96.   
  97.     for(i=256;;i+=256) /* try in steps of 256 bytes */
  98.     { if(!(**av=(char *)AllocVec(i,MEMF_ANY)))
  99.         break;
  100.       GetProgramName(**av,i); /* Who am I ? */
  101.       if(IoErr()!=ERROR_LINE_TOO_LONG)
  102.         break;
  103.       FreeVec(**av);
  104.     }
  105.   
  106.     if(**av==NULL)
  107.       exit(RETURN_FAIL);
  108.   }
  109. }
  110.   
  111. void __exitcommandline(void)
  112. { struct WBStartup **wbs;
  113.  
  114.   if(*(wbs=&_WBenchMsg)!=NULL)
  115.   { BPTR file;
  116.     if((file=window)!=0l)
  117.       Close(file);
  118.     if((*wbs)->sm_ArgList!=NULL) /* set original lock */
  119.       UnLock(CurrentDir(cd));
  120.   }else
  121.   { char **cl;
  122.  
  123.     if(*(cl=&cline)!=NULL)
  124.     { char ***av;
  125.  
  126.       if(*(av=&__argv)!=NULL)
  127.       { 
  128.         if(**av!=NULL)
  129.           FreeVec(**av);
  130.         FreeVec(*av);
  131.       }
  132.       FreeVec(*cl);
  133.     }
  134.   }
  135. }
  136.   
  137. /* Add these two functions to the lists */
  138. ADD2INIT(__initcommandline,-40);
  139. ADD2EXIT(__exitcommandline,-40);
  140.