move the defines to the resource header and include that where necessary.
[AROS.git] / compiler / autoinit / commandline.c
blob8f707afb5390400a6dedeff4c91c4f6d2535029e
1 /*
2 Copyright © 1995-2011, The AROS Development Team. All rights reserved.
3 $Id$
4 */
5 #include <dos/dos.h>
6 #include <exec/memory.h>
7 #include <proto/exec.h>
8 #include <proto/dos.h>
9 #include <aros/symbolsets.h>
10 #include <aros/autoinit.h>
11 #include <aros/startup.h>
13 #define DEBUG 0
14 #include <aros/debug.h>
16 #include "autoinit_intern.h"
18 int __nocommandline __attribute__((weak)) = 0;
20 extern char *__argstr;
21 extern ULONG __argsize;
23 extern char **__argv;
24 extern int __argc;
26 static char *__args;
27 static int __argmax;
29 static void process_cmdline(int *argc, char *args, char *argv[]);
31 static void __initcommandline(struct ExecBase *SysBase)
33 char *ptr = NULL;
35 if (WBenchMsg || __nocommandline) {
36 __startup_entries_next();
37 return;
40 if (__argsize)
42 ULONG size;
44 /* Copy args into buffer */
45 if (!(__args = AllocMem(__argsize+1, MEMF_ANY)))
46 return;
48 ptr = __args;
49 size= __argsize;
50 while (size--)
51 *ptr++ = *__argstr++;
52 *ptr = 0;
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)) )
58 return;
60 D(bug("arg(%d)=\"%s\", argmax=%d\n", __argsize, __args, __argmax));
62 /* create argv */
63 process_cmdline(&__argc, __args, __argv);
65 else
67 __argmax = 1;
68 __argc = 1;
69 if (!(__argv = AllocMem (sizeof (char *)*2, MEMF_CLEAR | MEMF_ANY)))
70 return;
74 * get program name
76 __argv[0] = __get_command_name();
78 #if DEBUG /* Debug argument parsing */
80 kprintf("argc = %d\n", __argc);
82 int t;
83 for (t=0; t<__argc; t++)
84 kprintf("argv[%d] = \"%s\"\n", t, __argv[t]);
87 #endif
89 __startup_entries_next();
91 if (WBenchMsg != NULL)
92 return;
94 if (__argv)
95 FreeMem(__argv, sizeof (char *) * (__argmax+1));
97 if (__args)
98 FreeMem(__args, __argsize+1);
101 static BOOL is_space(char c)
103 switch(c)
105 case ' ':
106 case '\n':
107 case '\t':
108 case '\v':
109 case '\f':
110 case '\r':
111 return TRUE;
112 default:
113 return FALSE;
117 static BOOL is_escape(char c)
119 if(c == '*')
120 return TRUE;
121 else
122 return FALSE;
125 static BOOL is_final_quote(char *ptr)
127 if(*ptr == '\"' && (ptr[1] == '\0' || is_space(ptr[1])))
128 return TRUE;
129 else
130 return FALSE;
133 static void process_cmdline(int *pargc, char *args, char *argv[])
135 char *ptr = args;
136 char *arg;
137 int argc = 1;
139 while(TRUE)
141 /* skip leading white spaces */
142 while(is_space(*ptr))
143 ptr++;
145 if(*ptr == '\0')
146 break;
148 argc++;
149 if(*ptr == '\"')
151 /* quoted parameter starts here */
152 ptr++;
154 /* store pointer to the parameter */
155 if(argv)
156 argv[argc-1] = ptr;
158 /* unescape quoted parameter */
159 arg = ptr;
160 while(!(*ptr == '\0' || is_final_quote(ptr)))
162 if(argv)
164 /* unescaping */
165 if(is_escape(*ptr))
167 ptr++;
168 switch(*ptr)
170 case 'e':
171 case 'E':
172 *arg++ = '\033';
173 break;
174 case 'N':
175 case 'n':
176 *arg++ = '\n';
177 break;
178 case '\0':
179 break;
180 default:
181 *arg++ = *ptr;
183 ptr++;
185 else
186 *arg++ = *ptr++;
188 else
190 /* don't touch anything, just skip escapes */
191 if(is_escape(*ptr))
193 ptr++;
194 if(*ptr != '\0')
195 ptr++;
197 else
198 ptr++;
201 /* skip final quote */
202 if(*ptr != '\0')
203 ptr++;
204 /* quoted parameter ends here */
205 if(argv)
206 *arg = '\0';
208 else
210 /* unquoted parameter starts here */
212 /* store pointer to the parameter */
213 if(argv)
214 argv[argc-1] = ptr;
216 /* no escaping, just find the end */
217 while(!(*ptr == '\0' || is_space(*ptr)))
218 ptr++;
220 /* stop processing if we reached the end of argument string */
221 if(*ptr == '\0')
222 break;
224 /* unquoted parameter ends here */
225 if(argv)
226 *ptr++ = '\0';
229 /* store the number of arguments */
230 *pargc = argc;
233 ADD2SET(__initcommandline, PROGRAM_ENTRIES, -10);