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