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