home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / libnix-0.8-src.lha / libnix-0.8 / sources / nixmain / __nocommandline.c next >
Encoding:
C/C++ Source or Header  |  1995-03-05  |  3.2 KB  |  153 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 unsigned long __commandlen;
  18. extern struct WBStartup *_WBenchMsg;
  19.  
  20. extern char __stdiowin[];
  21.  
  22. static char *cline=NULL; /* Copy of commandline */
  23. static BPTR cd=0l;       /* Lock for Current Directory */
  24. static BPTR window=0l;   /* CLI-window for start from workbench */
  25.  
  26. /* This guarantees that this module gets linked in.
  27.    If you replace this by an own reference called
  28.    __nocommandline you get no commandline arguments */
  29. ALIAS(__nocommandline,__initcommandline);
  30.  
  31. void __initcommandline(void)
  32. {
  33.   struct WBStartup **wbs;
  34.   char ***av;
  35.  
  36.   if(*(wbs=&_WBenchMsg)!=NULL)
  37.   { if(__stdiowin[0])
  38.     { BPTR *win;
  39.  
  40.       if((*(win=&window)=Open(__stdiowin,MODE_OLDFILE))==0l)
  41.         exit(RETURN_FAIL);
  42.       SelectInput(*win);
  43.       SelectOutput(*win);
  44.     }
  45.     if((*wbs)->sm_ArgList!=NULL) /* cd to icon */
  46.       cd=CurrentDir(DupLock((*wbs)->sm_ArgList->wa_Lock));
  47.  
  48.     __argc=0;
  49.     __argv=(char **)*wbs;
  50.   }else
  51.   { 
  52.     size_t i;
  53.     char *a,*cl=__commandline;
  54.  
  55.     if(!(cline=(char *)AllocVec(__commandlen+1,MEMF_ANY))) /* get buffer */
  56.       exit(RETURN_FAIL);
  57.   
  58.     a=cline; /* and parse commandline */
  59.     i=__commandlen;
  60.     __argc=1;
  61.     for(;;)
  62.     {
  63.       while(i&&(*cl==' '||*cl=='\t'||*cl=='\n'))
  64.       { cl++;
  65.         i--; }
  66.       if(!i)
  67.         break;
  68.       if(*cl=='\"')
  69.       {
  70.         cl++;
  71.         i--;
  72.         while(i)
  73.         {
  74.           if(*cl=='\"')
  75.           {
  76.             cl++;
  77.             i--;
  78.             break;
  79.           }
  80.           if(*cl=='*')
  81.           {
  82.             cl++;
  83.             i--;
  84.             if(!i)
  85.               break;
  86.       }
  87.           *a++=*cl++;
  88.           i--;
  89.         }
  90.       }
  91.       else
  92.         while(i&&(*cl!=' '&&*cl!='\t'&&*cl!='\n'))
  93.         { *a++=*cl++;
  94.           i--; }
  95.       *a++='\0';
  96.       __argc++;
  97.     }
  98.       /* NULL Terminated */
  99.     if(!(*(av=&__argv)=(char **)AllocVec((__argc+1)*sizeof(char *),MEMF_ANY|MEMF_CLEAR)))
  100.       exit(RETURN_FAIL);
  101.     
  102.     a=cline;
  103.     for(i=1;i<__argc;i++)
  104.     { 
  105.       (*av)[i]=a;
  106.       while(*a++)
  107.         ; 
  108.     }
  109.   
  110.     for(i=256;;i+=256) /* try in steps of 256 bytes */
  111.     { if(!(**av=(char *)AllocVec(i,MEMF_ANY)))
  112.         break;
  113.       GetProgramName(**av,i); /* Who am I ? */
  114.       if(IoErr()!=ERROR_LINE_TOO_LONG)
  115.         break;
  116.       FreeVec(**av);
  117.     }
  118.   
  119.     if(**av==NULL)
  120.       exit(RETURN_FAIL);
  121.   }
  122. }
  123.   
  124. void __exitcommandline(void)
  125. { struct WBStartup **wbs;
  126.  
  127.   if(*(wbs=&_WBenchMsg)!=NULL)
  128.   { BPTR file;
  129.     if((file=window)!=0l)
  130.       Close(file);
  131.     if((*wbs)->sm_ArgList!=NULL) /* set original lock */
  132.       UnLock(CurrentDir(cd));
  133.   }else
  134.   { char **cl;
  135.  
  136.     if(*(cl=&cline)!=NULL)
  137.     { char ***av;
  138.  
  139.       if(*(av=&__argv)!=NULL)
  140.       { 
  141.         if(**av!=NULL)
  142.           FreeVec(**av);
  143.         FreeVec(*av);
  144.       }
  145.       FreeVec(*cl);
  146.     }
  147.   }
  148. }
  149.   
  150. /* Add these two functions to the lists */
  151. ADD2INIT(__initcommandline,-40);
  152. ADD2EXIT(__exitcommandline,-40);
  153.