home *** CD-ROM | disk | FTP | other *** search
- #include <stdlib.h>
- #include <exec/memory.h>
- #include <workbench/startup.h>
- #ifdef __GNUC__
- #include <inline/exec.h>
- #include <dos/dos.h>
- #include <inline/dos.h>
- #else
- #include <clib/exec_protos.h>
- #include <clib/dos_protos.h>
- #endif
- #include <stabs.h>
-
- extern int __argc; /* Defined in startup */
- extern char **__argv;
- extern char *__commandline;
- extern unsigned long __commandlen;
- extern struct WBStartup *_WBenchMsg;
-
- extern char __stdiowin[];
-
- static char *cline=NULL; /* Copy of commandline */
- static BPTR cd=0l; /* Lock for Current Directory */
- static BPTR window=0l; /* CLI-window for start from workbench */
-
- /* This guarantees that this module gets linked in.
- If you replace this by an own reference called
- __nocommandline you get no commandline arguments */
- ALIAS(__nocommandline,__initcommandline);
-
- void __initcommandline(void)
- {
- struct WBStartup **wbs;
- char ***av;
-
- if(*(wbs=&_WBenchMsg)!=NULL)
- { if(__stdiowin[0])
- { BPTR *win;
-
- if((*(win=&window)=Open(__stdiowin,MODE_OLDFILE))==0l)
- exit(RETURN_FAIL);
- SelectInput(*win);
- SelectOutput(*win);
- }
- if((*wbs)->sm_ArgList!=NULL) /* cd to icon */
- cd=CurrentDir(DupLock((*wbs)->sm_ArgList->wa_Lock));
-
- __argc=0;
- __argv=(char **)*wbs;
- }else
- {
- size_t i;
- char *a,*cl=__commandline;
-
- if(!(cline=(char *)AllocVec(__commandlen+1,MEMF_ANY))) /* get buffer */
- exit(RETURN_FAIL);
-
- a=cline; /* and parse commandline */
- i=__commandlen;
- __argc=1;
- for(;;)
- {
- while(i&&(*cl==' '||*cl=='\t'||*cl=='\n'))
- { cl++;
- i--; }
- if(!i)
- break;
- if(*cl=='\"')
- {
- cl++;
- i--;
- while(i)
- {
- if(*cl=='\"')
- {
- cl++;
- i--;
- break;
- }
- if(*cl=='*')
- {
- cl++;
- i--;
- if(!i)
- break;
- }
- *a++=*cl++;
- i--;
- }
- }
- else
- while(i&&(*cl!=' '&&*cl!='\t'&&*cl!='\n'))
- { *a++=*cl++;
- i--; }
- *a++='\0';
- __argc++;
- }
- /* NULL Terminated */
- if(!(*(av=&__argv)=(char **)AllocVec((__argc+1)*sizeof(char *),MEMF_ANY|MEMF_CLEAR)))
- exit(RETURN_FAIL);
-
- a=cline;
- for(i=1;i<__argc;i++)
- {
- (*av)[i]=a;
- while(*a++)
- ;
- }
-
- for(i=256;;i+=256) /* try in steps of 256 bytes */
- { if(!(**av=(char *)AllocVec(i,MEMF_ANY)))
- break;
- GetProgramName(**av,i); /* Who am I ? */
- if(IoErr()!=ERROR_LINE_TOO_LONG)
- break;
- FreeVec(**av);
- }
-
- if(**av==NULL)
- exit(RETURN_FAIL);
- }
- }
-
- void __exitcommandline(void)
- { struct WBStartup **wbs;
-
- if(*(wbs=&_WBenchMsg)!=NULL)
- { BPTR file;
- if((file=window)!=0l)
- Close(file);
- if((*wbs)->sm_ArgList!=NULL) /* set original lock */
- UnLock(CurrentDir(cd));
- }else
- { char **cl;
-
- if(*(cl=&cline)!=NULL)
- { char ***av;
-
- if(*(av=&__argv)!=NULL)
- {
- if(**av!=NULL)
- FreeVec(**av);
- FreeVec(*av);
- }
- FreeVec(*cl);
- }
- }
- }
-
- /* Add these two functions to the lists */
- ADD2INIT(__initcommandline,-40);
- ADD2EXIT(__exitcommandline,-40);
-