srt1.c revision 1.1.2.3 1 /* $NetBSD: srt1.c,v 1.1.2.3 2003/01/07 20:46:49 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2001 Ben Harris.
5 * Copyright (c) 1996
6 * Matthias Drochner. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the NetBSD Project
19 * by Matthias Drochner.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* Command-line parsing from i386 doscommain.c */
36
37 #include <lib/libsa/stand.h>
38 #include <riscoscalls.h>
39 #include <srt0.h>
40
41
42 /* limit of the memory given to us by RISC OS's WIMPslot; inited by srt0.S */
43 /* init it also to a reasonable value also so it doesnt get into the BSS ! */
44 void *HIMEM = (void *) 0x12000;
45
46
47 static int whitespace __P((char));
48
49 static int whitespace(char c) {
50 if ((c == '\0') || (c == ' ') || (c == '\t')
51 || (c == '\r') || (c == '\n'))
52 return (1);
53 return (0);
54 }
55
56
57 enum state {skipping, doing_arg, doing_long_arg};
58
59
60 /* build argv/argc, start real main() */
61 void __start(void);
62 int splitargs(char *, int, char**);
63 extern int main(int, char**);
64
65 extern char edata[], end[];
66
67
68 void
69 __start(void)
70 {
71 int argc;
72 char *ro_args, *args, **argv;
73
74 /* Clear BSS */
75 memset(edata, 0, end - edata);
76
77 /* Define heap. */
78 setheap(end, (void *)(HIMEM - 0x1000));
79
80 ro_args = os_get_env(NULL, NULL);
81
82 args = alloc(strlen(ro_args)+10); /* some spare extra */
83 strcpy(args, ro_args);
84
85 argc = splitargs(args, 0, NULL);
86 argv = alloc(argc * sizeof(*argv));
87 if (argv == NULL)
88 panic("alloc of argv failed");
89 argc = splitargs(args, 1, argv);
90
91 /* start real main() */
92 os_exit(NULL, main(argc, argv));
93 }
94
95 int
96 splitargs(char *args, int really, char **argv)
97 {
98 int argc, i;
99 enum state s;
100
101 argc = 0;
102 s = skipping;
103
104 for (i = 0; args[i]; i++){
105
106 if (whitespace(args[i])) {
107 if (s == doing_arg) {
108 /* end of argument word */
109 if (really)
110 args[i] = '\0';
111 s = skipping;
112 }
113 continue;
114 }
115
116 if (args[i] == '"') {
117 /* start or end long arg
118 * (end only if next char is whitespace)
119 * XXX but '" ' cannot be in argument
120 */
121 switch (s) {
122 case skipping:
123 /* next char begins new argument word */
124 if (really)
125 argv[argc] = &args[i + 1];
126 argc++;
127 s = doing_long_arg;
128 break;
129 case doing_long_arg:
130 if (whitespace(args[i + 1])) {
131 if (really)
132 args[i] = '\0';
133 s = skipping;
134 }
135 break;
136 case doing_arg:
137 /* ignore in the middle of arguments */
138 default:
139 break;
140 }
141 continue;
142 }
143
144 /* all other characters */
145 if (s == skipping) {
146 /* begin new argument word */
147 if (really)
148 argv[argc] = &args[i];
149 argc++;
150 s = doing_arg;
151 }
152 }
153 return argc;
154 }
155
156 void _rtt(void)
157 {
158
159 os_exit(NULL, 0);
160 }
161