2 Copyright © 1995-2007, The AROS Development Team. All rights reserved.
6 #include <exec/memory.h>
7 #include <proto/exec.h>
9 #include <aros/symbolsets.h>
10 #include <aros/autoinit.h>
13 #include <aros/debug.h>
15 int __nocommandline
__attribute__((weak
)) = 0;
17 extern void *WBenchMsg
;
18 extern char *__argstr
;
19 extern ULONG __argsize
;
27 static void process_cmdline(int *argc
, char *args
, char *argv
[]);
29 int __initcommandline(void)
38 /* Copy args into buffer */
39 if (!(__args
= AllocMem(__argsize
+1, MEMF_ANY
)))
43 while ((*ptr
++ = *__argstr
++)) {}
45 /* Find out how many arguments we have */
46 process_cmdline(&__argmax
, __args
, NULL
);
48 if (!(__argv
= AllocMem (sizeof (char *) * (__argmax
+1), MEMF_ANY
| MEMF_CLEAR
)) )
51 D(bug("arg(%d)=\"%s\", argmax=%d\n", __argsize
, __args
, __argmax
));
54 process_cmdline(&__argc
, __args
, __argv
);
60 if (!(__argv
= AllocMem (sizeof (char *)*2, MEMF_CLEAR
| MEMF_ANY
)))
67 __argv
[0] = FindTask(NULL
)->tc_Node
.ln_Name
;
72 #if DEBUG /* Debug argument parsing */
74 kprintf("argc = %d\n", __argc
);
77 for (t
=0; t
<__argc
; t
++)
78 kprintf("argv[%d] = \"%s\"\n", t
, __argv
[t
]);
86 static BOOL
is_space(char c
)
102 static BOOL
is_escape(char c
)
110 static BOOL
is_final_quote(char *ptr
)
112 if(*ptr
== '\"' && (ptr
[1] == '\0' || is_space(ptr
[1])))
118 static void process_cmdline(int *pargc
, char *args
, char *argv
[])
126 /* skip leading white spaces */
127 while(is_space(*ptr
))
136 /* quoted parameter starts here */
139 /* store pointer to the parameter */
143 /* unescape quoted parameter */
145 while(!(*ptr
== '\0' || is_final_quote(ptr
)))
175 /* don't touch anything, just skip escapes */
186 /* skip final quote */
189 /* quoted parameter ends here */
195 /* unquoted parameter starts here */
197 /* store pointer to the parameter */
201 /* no escaping, just find the end */
202 while(!(*ptr
== '\0' || is_space(*ptr
)))
205 /* stop processing if we reached the end of argument string */
209 /* unquoted parameter ends here */
214 /* store the number of arguments */
218 void __exitcommandline(void)
220 if (WBenchMsg
!= NULL
)
224 FreeMem(__argv
, sizeof (char *) * (__argmax
+1));
227 FreeMem(__args
, __argsize
+1);
230 ADD2INIT(__initcommandline
, 0);
231 ADD2EXIT(__exitcommandline
, 0);