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