Home | History | Annotate | Line # | Download | only in lib
srt1.c revision 1.1.2.2
      1 /*	$NetBSD: srt1.c,v 1.1.2.2 2002/12/29 19:15:20 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 *args, **argv;
     73 
     74 	/* Clear BSS */
     75 	memset(edata, 0, end - edata);
     76 
     77 	/* Define heap. */
     78 	setheap(end, (void *)(HIMEM - 0x1000));
     79 
     80 	args = os_get_env(NULL, NULL);
     81 
     82 	argc = splitargs(args, 0, NULL);
     83 	argv = alloc(argc * sizeof(*argv));
     84 	if (argv == NULL)
     85 		panic("alloc of argv failed");
     86 	argc = splitargs(args, 1, argv);
     87 
     88 	/* start real main() */
     89 	os_exit(NULL, main(argc, argv));
     90 }
     91 
     92 int
     93 splitargs(char *args, int really, char **argv)
     94 {
     95 	int argc, i;
     96 	enum state s;
     97 
     98 	argc = 0;
     99 	s = skipping;
    100 
    101 	for (i = 0; args[i]; i++){
    102 
    103 		if (whitespace(args[i])) {
    104 			if (s == doing_arg) {
    105 				/* end of argument word */
    106 				if (really)
    107 					args[i] = '\0';
    108 				s = skipping;
    109 			}
    110 			continue;
    111 		}
    112 
    113 		if (args[i] == '"') {
    114 			/* start or end long arg
    115 			 * (end only if next char is whitespace)
    116 			 *  XXX but '" ' cannot be in argument
    117 			 */
    118 			switch (s) {
    119 			case skipping:
    120 				/* next char begins new argument word */
    121 				if (really)
    122 					argv[argc] = &args[i + 1];
    123 				argc++;
    124 				s = doing_long_arg;
    125 				break;
    126 			case doing_long_arg:
    127 				if (whitespace(args[i + 1])) {
    128 					if (really)
    129 						args[i] = '\0';
    130 					s = skipping;
    131 				}
    132 				break;
    133 			case doing_arg:
    134 				/* ignore in the middle of arguments */
    135 			default:
    136 				break;
    137 			}
    138 			continue;
    139 		}
    140 
    141 		/* all other characters */
    142 		if (s == skipping) {
    143 			/* begin new argument word */
    144 			if (really)
    145 				argv[argc] = &args[i];
    146 			argc++;
    147 			s = doing_arg;
    148 		}
    149 	}
    150 	return argc;
    151 }
    152 
    153 void _rtt(void)
    154 {
    155 
    156 	os_exit(NULL, 0);
    157 }
    158