2 Copyright © 1995-2011, 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>
11 #include <aros/startup.h>
14 #include <aros/debug.h>
16 #include "autoinit_intern.h"
18 int __nocommandline
__attribute__((weak
)) = 0;
20 extern char *__argstr
;
21 extern ULONG __argsize
;
29 static void process_cmdline(int *argc
, char *args
, char *argv
[]);
31 static void __initcommandline(struct ExecBase
*SysBase
)
35 if (WBenchMsg
|| __nocommandline
) {
36 __startup_entries_next();
44 /* Copy args into buffer */
45 if (!(__args
= AllocMem(__argsize
+1, MEMF_ANY
)))
54 /* Find out how many arguments we have */
55 process_cmdline(&__argmax
, __args
, NULL
);
57 if (!(__argv
= AllocMem (sizeof (char *) * (__argmax
+1), MEMF_ANY
| MEMF_CLEAR
)) )
60 D(bug("arg(%d)=\"%s\", argmax=%d\n", __argsize
, __args
, __argmax
));
63 process_cmdline(&__argc
, __args
, __argv
);
69 if (!(__argv
= AllocMem (sizeof (char *)*2, MEMF_CLEAR
| MEMF_ANY
)))
76 __argv
[0] = __get_command_name();
78 #if DEBUG /* Debug argument parsing */
80 kprintf("argc = %d\n", __argc
);
83 for (t
=0; t
<__argc
; t
++)
84 kprintf("argv[%d] = \"%s\"\n", t
, __argv
[t
]);
89 __startup_entries_next();
91 if (WBenchMsg
!= NULL
)
95 FreeMem(__argv
, sizeof (char *) * (__argmax
+1));
98 FreeMem(__args
, __argsize
+1);
101 static BOOL
is_space(char c
)
117 static BOOL
is_escape(char c
)
125 static BOOL
is_final_quote(char *ptr
)
127 if(*ptr
== '\"' && (ptr
[1] == '\0' || is_space(ptr
[1])))
133 static void process_cmdline(int *pargc
, char *args
, char *argv
[])
141 /* skip leading white spaces */
142 while(is_space(*ptr
))
151 /* quoted parameter starts here */
154 /* store pointer to the parameter */
158 /* unescape quoted parameter */
160 while(!(*ptr
== '\0' || is_final_quote(ptr
)))
190 /* don't touch anything, just skip escapes */
201 /* skip final quote */
204 /* quoted parameter ends here */
210 /* unquoted parameter starts here */
212 /* store pointer to the parameter */
216 /* no escaping, just find the end */
217 while(!(*ptr
== '\0' || is_space(*ptr
)))
220 /* stop processing if we reached the end of argument string */
224 /* unquoted parameter ends here */
229 /* store the number of arguments */
233 ADD2SET(__initcommandline
, PROGRAM_ENTRIES
, -10);