Home | History | Annotate | Line # | Download | only in libsa
parseutils.c revision 1.1
      1  1.1  minoura /*	$NetBSD: parseutils.c,v 1.1 2001/09/27 10:03:28 minoura Exp $	*/
      2  1.1  minoura 
      3  1.1  minoura /*
      4  1.1  minoura  *	from /sys/arch/i386/lib/parseutils.c
      5  1.1  minoura  *	NetBSD: parseutils.c,v 1.3 2000/09/24 12:32:35 jdolecek Exp
      6  1.1  minoura  */
      7  1.1  minoura 
      8  1.1  minoura /*
      9  1.1  minoura  * Copyright (c) 1996, 1997
     10  1.1  minoura  * 	Matthias Drochner.  All rights reserved.
     11  1.1  minoura  * Copyright (c) 1996, 1997
     12  1.1  minoura  * 	Perry E. Metzger.  All rights reserved.
     13  1.1  minoura  * Copyright (c) 1997
     14  1.1  minoura  *	Jason R. Thorpe.  All rights reserved
     15  1.1  minoura  *
     16  1.1  minoura  * Redistribution and use in source and binary forms, with or without
     17  1.1  minoura  * modification, are permitted provided that the following conditions
     18  1.1  minoura  * are met:
     19  1.1  minoura  * 1. Redistributions of source code must retain the above copyright
     20  1.1  minoura  *    notice, this list of conditions and the following disclaimer.
     21  1.1  minoura  * 2. Redistributions in binary form must reproduce the above copyright
     22  1.1  minoura  *    notice, this list of conditions and the following disclaimer in the
     23  1.1  minoura  *    documentation and/or other materials provided with the distribution.
     24  1.1  minoura  * 3. All advertising materials mentioning features or use of this software
     25  1.1  minoura  *    must display the following acknowledgements:
     26  1.1  minoura  *	This product includes software developed for the NetBSD Project
     27  1.1  minoura  *	by Matthias Drochner.
     28  1.1  minoura  *	This product includes software developed for the NetBSD Project
     29  1.1  minoura  *	by Perry E. Metzger.
     30  1.1  minoura  * 4. The names of the authors may not be used to endorse or promote products
     31  1.1  minoura  *    derived from this software without specific prior written permission.
     32  1.1  minoura  *
     33  1.1  minoura  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     34  1.1  minoura  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     35  1.1  minoura  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     36  1.1  minoura  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     37  1.1  minoura  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38  1.1  minoura  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     39  1.1  minoura  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     40  1.1  minoura  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     41  1.1  minoura  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     42  1.1  minoura  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     43  1.1  minoura  */
     44  1.1  minoura 
     45  1.1  minoura #include <lib/libkern/libkern.h>
     46  1.1  minoura #include <lib/libsa/stand.h>
     47  1.1  minoura #include <sys/boot_flag.h>
     48  1.1  minoura 
     49  1.1  minoura #include "libx68k.h"
     50  1.1  minoura 
     51  1.1  minoura 
     52  1.1  minoura /*
     53  1.1  minoura  * chops the head from the arguments and returns the arguments if any,
     54  1.1  minoura  * or possibly an empty string.
     55  1.1  minoura  */
     56  1.1  minoura char *
     57  1.1  minoura gettrailer(arg)
     58  1.1  minoura 	char *arg;
     59  1.1  minoura {
     60  1.1  minoura 	char *options;
     61  1.1  minoura 
     62  1.1  minoura 	if ((options = strchr(arg, ' ')) == NULL)
     63  1.1  minoura 		return ("");
     64  1.1  minoura 	else
     65  1.1  minoura 		*options++ = '\0';
     66  1.1  minoura 
     67  1.1  minoura 	/* trim leading blanks */
     68  1.1  minoura 	while (*options && *options == ' ')
     69  1.1  minoura 		options++;
     70  1.1  minoura 
     71  1.1  minoura 	return (options);
     72  1.1  minoura }
     73  1.1  minoura 
     74  1.1  minoura int
     75  1.1  minoura parseopts(opts, howto)
     76  1.1  minoura 	const char *opts;
     77  1.1  minoura 	int *howto;
     78  1.1  minoura {
     79  1.1  minoura 	int r, tmpopt = 0;
     80  1.1  minoura 
     81  1.1  minoura 	opts++; 	/* skip - */
     82  1.1  minoura 	while (*opts && *opts != ' ') {
     83  1.1  minoura 		r = 0;
     84  1.1  minoura 		BOOT_FLAG(*opts, r);
     85  1.1  minoura 		if (r == 0) {
     86  1.1  minoura 			printf("-%c: unknown flag\n", *opts);
     87  1.1  minoura 			return(0);
     88  1.1  minoura 		}
     89  1.1  minoura 		tmpopt |= r;
     90  1.1  minoura 		opts++;
     91  1.1  minoura 	}
     92  1.1  minoura 
     93  1.1  minoura 	*howto = tmpopt;
     94  1.1  minoura 	return(1);
     95  1.1  minoura }
     96  1.1  minoura 
     97  1.1  minoura int
     98  1.1  minoura parseboot(arg, filename, howto)
     99  1.1  minoura 	char *arg;
    100  1.1  minoura 	char **filename;
    101  1.1  minoura 	int *howto;
    102  1.1  minoura {
    103  1.1  minoura 	char *opts = NULL;
    104  1.1  minoura 
    105  1.1  minoura 	*filename = 0;
    106  1.1  minoura 	*howto = 0;
    107  1.1  minoura 
    108  1.1  minoura 	/* if there were no arguments */
    109  1.1  minoura 	if (!*arg)
    110  1.1  minoura 		return(1);
    111  1.1  minoura 
    112  1.1  minoura 	/* format is... */
    113  1.1  minoura 	/* [[xxNx:]filename] [-adqsv] */
    114  1.1  minoura 
    115  1.1  minoura 	/* check for just args */
    116  1.1  minoura 	if (arg[0] == '-')
    117  1.1  minoura 		opts = arg;
    118  1.1  minoura 	else {
    119  1.1  minoura 		/* there's a file name */
    120  1.1  minoura 		*filename = arg;
    121  1.1  minoura 
    122  1.1  minoura 		opts = gettrailer(arg);
    123  1.1  minoura 		if (!*opts)
    124  1.1  minoura 			opts = NULL;
    125  1.1  minoura 		else if (*opts != '-') {
    126  1.1  minoura 			printf("invalid arguments\n");
    127  1.1  minoura 			return(0);
    128  1.1  minoura 		}
    129  1.1  minoura 	}
    130  1.1  minoura 
    131  1.1  minoura 	/* at this point, we have dealt with filenames. */
    132  1.1  minoura 
    133  1.1  minoura 	/* now, deal with options */
    134  1.1  minoura 	if (opts) {
    135  1.1  minoura 		if (parseopts(opts, howto) == 0)
    136  1.1  minoura 			return(0);
    137  1.1  minoura 	}
    138  1.1  minoura 
    139  1.1  minoura 	return(1);
    140  1.1  minoura }
    141