xref: /netbsd-src/sys/arch/acorn32/stand/lib/srt1.c (revision 02cdf4d2c869ecfa1ae5484cf77eefe2fbcc6d94)
1 /*	$NetBSD: srt1.c,v 1.6 2009/03/14 14:45:52 dsl 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  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* Command-line parsing from i386 doscommain.c */
30 
31 #include <lib/libkern/libkern.h>
32 #include <lib/libsa/stand.h>
33 #include <riscoscalls.h>
34 #include <srt0.h>
35 
36 
37 /* limit of the memory given to us by RISC OS's WIMPslot; inited by srt0.S */
38 /* init it also to a reasonable value also so it doesnt get into the BSS ! */
39 void *HIMEM = (void *) 0x12000;
40 
41 
42 static int whitespace(char);
43 
whitespace(char c)44 static int whitespace(char c) {
45 	if ((c == '\0') || (c == ' ') || (c == '\t')
46 	    || (c == '\r') || (c == '\n'))
47 		return (1);
48 	return (0);
49 }
50 
51 
52 enum state {skipping, doing_arg, doing_long_arg};
53 
54 
55 /* build argv/argc, start real main() */
56 void __start(void);
57 int splitargs(char *, int, char**);
58 extern int main(int, char**);
59 
60 extern char edata[], end[];
61 
62 
63 void
__start(void)64 __start(void)
65 {
66 	int argc;
67 	char *ro_args, *args, **argv;
68 
69 	/* Clear BSS */
70 	memset(edata, 0, end - edata);
71 
72 	/* Define heap. */
73 	setheap(end, (void *)(HIMEM - 0x1000));
74 
75 	ro_args = os_get_env(NULL, NULL);
76 
77 	args = alloc(strlen(ro_args)+10);	/* some spare extra */
78 	strcpy(args, ro_args);
79 
80 	argc = splitargs(args, 0, NULL);
81 	argv = alloc(argc * sizeof(*argv));
82 	if (argv == NULL)
83 		panic("alloc of argv failed");
84 	argc = splitargs(args, 1, argv);
85 
86 	/* start real main() */
87 	os_exit(NULL, main(argc, argv));
88 }
89 
90 int
splitargs(char * args,int really,char ** argv)91 splitargs(char *args, int really, char **argv)
92 {
93 	int argc, i;
94 	enum state s;
95 
96 	argc = 0;
97 	s = skipping;
98 
99 	for (i = 0; args[i]; i++){
100 
101 		if (whitespace(args[i])) {
102 			if (s == doing_arg) {
103 				/* end of argument word */
104 				if (really)
105 					args[i] = '\0';
106 				s = skipping;
107 			}
108 			continue;
109 		}
110 
111 		if (args[i] == '"') {
112 			/* start or end long arg
113 			 * (end only if next char is whitespace)
114 			 *  XXX but '" ' cannot be in argument
115 			 */
116 			switch (s) {
117 			case skipping:
118 				/* next char begins new argument word */
119 				if (really)
120 					argv[argc] = &args[i + 1];
121 				argc++;
122 				s = doing_long_arg;
123 				break;
124 			case doing_long_arg:
125 				if (whitespace(args[i + 1])) {
126 					if (really)
127 						args[i] = '\0';
128 					s = skipping;
129 				}
130 				break;
131 			case doing_arg:
132 				/* ignore in the middle of arguments */
133 			default:
134 				break;
135 			}
136 			continue;
137 		}
138 
139 		/* all other characters */
140 		if (s == skipping) {
141 			/* begin new argument word */
142 			if (really)
143 				argv[argc] = &args[i];
144 			argc++;
145 			s = doing_arg;
146 		}
147 	}
148 	return argc;
149 }
150 
_rtt(void)151 void _rtt(void)
152 {
153 
154 	os_exit(NULL, 0);
155 }
156