Home | History | Annotate | Line # | Download | only in lib
srt1.c revision 1.3
      1 /*	$NetBSD: srt1.c,v 1.3 2003/09/21 14:17:13 matt 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/libkern/libkern.h>
     38 #include <lib/libsa/stand.h>
     39 #include <riscoscalls.h>
     40 #include <srt0.h>
     41 
     42 
     43 /* limit of the memory given to us by RISC OS's WIMPslot; inited by srt0.S */
     44 /* init it also to a reasonable value also so it doesnt get into the BSS ! */
     45 void *HIMEM = (void *) 0x12000;
     46 
     47 
     48 static int whitespace __P((char));
     49 
     50 static int whitespace(char c) {
     51 	if ((c == '\0') || (c == ' ') || (c == '\t')
     52 	    || (c == '\r') || (c == '\n'))
     53 		return (1);
     54 	return (0);
     55 }
     56 
     57 
     58 enum state {skipping, doing_arg, doing_long_arg};
     59 
     60 
     61 /* build argv/argc, start real main() */
     62 void __start(void);
     63 int splitargs(char *, int, char**);
     64 extern int main(int, char**);
     65 
     66 extern char edata[], end[];
     67 
     68 
     69 void
     70 __start(void)
     71 {
     72 	int argc;
     73 	char *ro_args, *args, **argv;
     74 
     75 	/* Clear BSS */
     76 	memset(edata, 0, end - edata);
     77 
     78 	/* Define heap. */
     79 	setheap(end, (void *)(HIMEM - 0x1000));
     80 
     81 	ro_args = os_get_env(NULL, NULL);
     82 
     83 	args = alloc(strlen(ro_args)+10);	/* some spare extra */
     84 	strcpy(args, ro_args);
     85 
     86 	argc = splitargs(args, 0, NULL);
     87 	argv = alloc(argc * sizeof(*argv));
     88 	if (argv == NULL)
     89 		panic("alloc of argv failed");
     90 	argc = splitargs(args, 1, argv);
     91 
     92 	/* start real main() */
     93 	os_exit(NULL, main(argc, argv));
     94 }
     95 
     96 int
     97 splitargs(char *args, int really, char **argv)
     98 {
     99 	int argc, i;
    100 	enum state s;
    101 
    102 	argc = 0;
    103 	s = skipping;
    104 
    105 	for (i = 0; args[i]; i++){
    106 
    107 		if (whitespace(args[i])) {
    108 			if (s == doing_arg) {
    109 				/* end of argument word */
    110 				if (really)
    111 					args[i] = '\0';
    112 				s = skipping;
    113 			}
    114 			continue;
    115 		}
    116 
    117 		if (args[i] == '"') {
    118 			/* start or end long arg
    119 			 * (end only if next char is whitespace)
    120 			 *  XXX but '" ' cannot be in argument
    121 			 */
    122 			switch (s) {
    123 			case skipping:
    124 				/* next char begins new argument word */
    125 				if (really)
    126 					argv[argc] = &args[i + 1];
    127 				argc++;
    128 				s = doing_long_arg;
    129 				break;
    130 			case doing_long_arg:
    131 				if (whitespace(args[i + 1])) {
    132 					if (really)
    133 						args[i] = '\0';
    134 					s = skipping;
    135 				}
    136 				break;
    137 			case doing_arg:
    138 				/* ignore in the middle of arguments */
    139 			default:
    140 				break;
    141 			}
    142 			continue;
    143 		}
    144 
    145 		/* all other characters */
    146 		if (s == skipping) {
    147 			/* begin new argument word */
    148 			if (really)
    149 				argv[argc] = &args[i];
    150 			argc++;
    151 			s = doing_arg;
    152 		}
    153 	}
    154 	return argc;
    155 }
    156 
    157 void _rtt(void)
    158 {
    159 
    160 	os_exit(NULL, 0);
    161 }
    162