xref: /netbsd-src/sys/arch/i386/stand/lib/parseutils.c (revision 7cf250db21df30fd43f24234125bf62413089754)
1*7cf250dbSjakllsch /*	$NetBSD: parseutils.c,v 1.7 2014/01/05 20:52:57 jakllsch Exp $	*/
227d7edd9Sdrochner 
327d7edd9Sdrochner /*
427d7edd9Sdrochner  * Copyright (c) 1996, 1997
527d7edd9Sdrochner  * 	Matthias Drochner.  All rights reserved.
627d7edd9Sdrochner  * Copyright (c) 1996, 1997
727d7edd9Sdrochner  * 	Perry E. Metzger.  All rights reserved.
827d7edd9Sdrochner  * Copyright (c) 1997
927d7edd9Sdrochner  *	Jason R. Thorpe.  All rights reserved
1027d7edd9Sdrochner  *
1127d7edd9Sdrochner  * Redistribution and use in source and binary forms, with or without
1227d7edd9Sdrochner  * modification, are permitted provided that the following conditions
1327d7edd9Sdrochner  * are met:
1427d7edd9Sdrochner  * 1. Redistributions of source code must retain the above copyright
1527d7edd9Sdrochner  *    notice, this list of conditions and the following disclaimer.
1627d7edd9Sdrochner  * 2. Redistributions in binary form must reproduce the above copyright
1727d7edd9Sdrochner  *    notice, this list of conditions and the following disclaimer in the
1827d7edd9Sdrochner  *    documentation and/or other materials provided with the distribution.
1927d7edd9Sdrochner  * 3. All advertising materials mentioning features or use of this software
2027d7edd9Sdrochner  *    must display the following acknowledgements:
2127d7edd9Sdrochner  *	This product includes software developed for the NetBSD Project
2227d7edd9Sdrochner  *	by Matthias Drochner.
2327d7edd9Sdrochner  *	This product includes software developed for the NetBSD Project
2427d7edd9Sdrochner  *	by Perry E. Metzger.
2527d7edd9Sdrochner  * 4. The names of the authors may not be used to endorse or promote products
2627d7edd9Sdrochner  *    derived from this software without specific prior written permission.
2727d7edd9Sdrochner  *
2827d7edd9Sdrochner  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2927d7edd9Sdrochner  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3027d7edd9Sdrochner  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
3127d7edd9Sdrochner  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3227d7edd9Sdrochner  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3327d7edd9Sdrochner  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3427d7edd9Sdrochner  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3527d7edd9Sdrochner  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3627d7edd9Sdrochner  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3727d7edd9Sdrochner  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3827d7edd9Sdrochner  */
3927d7edd9Sdrochner 
4027d7edd9Sdrochner #include <lib/libkern/libkern.h>
4127d7edd9Sdrochner #include <lib/libsa/stand.h>
4249c105ffSjdolecek #include <sys/boot_flag.h>
4327d7edd9Sdrochner 
4427d7edd9Sdrochner #include "libi386.h"
4527d7edd9Sdrochner 
4627d7edd9Sdrochner /*
4727d7edd9Sdrochner  * chops the head from the arguments and returns the arguments if any,
4827d7edd9Sdrochner  * or possibly an empty string.
4927d7edd9Sdrochner  */
5027d7edd9Sdrochner char *
gettrailer(char * arg)51334f5e8fSchristos gettrailer(char *arg)
5227d7edd9Sdrochner {
5327d7edd9Sdrochner 	char *options;
5427d7edd9Sdrochner 
559bc1ed71Schristos 	for (options = arg; *options; options++) {
569bc1ed71Schristos 		switch (*options) {
579bc1ed71Schristos 		case ' ':
589bc1ed71Schristos 		case '\t':
5927d7edd9Sdrochner 			*options++ = '\0';
609bc1ed71Schristos 			break;
619bc1ed71Schristos 		default:
629bc1ed71Schristos 			continue;
639bc1ed71Schristos 		}
649bc1ed71Schristos 		break;
659bc1ed71Schristos 	}
669bc1ed71Schristos 	if (*options == '\0')
67*7cf250dbSjakllsch 		return options;
6827d7edd9Sdrochner 
699bc1ed71Schristos 	/* trim leading blanks/tabs */
709bc1ed71Schristos 	while (*options == ' ' || *options == '\t')
7127d7edd9Sdrochner 		options++;
7227d7edd9Sdrochner 
73334f5e8fSchristos 	return options;
7427d7edd9Sdrochner }
7527d7edd9Sdrochner 
7627d7edd9Sdrochner int
parseopts(const char * opts,int * howto)77334f5e8fSchristos parseopts(const char *opts, int *howto)
7827d7edd9Sdrochner {
7927d7edd9Sdrochner 	int r, tmpopt = 0;
8027d7edd9Sdrochner 
8127d7edd9Sdrochner 	opts++; 	/* skip - */
826d2637b2Schristos 	while (*opts) {
8349c105ffSjdolecek 		r = 0;
8449c105ffSjdolecek 		BOOT_FLAG(*opts, r);
8549c105ffSjdolecek 		if (r == 0) {
8627d7edd9Sdrochner 			printf("-%c: unknown flag\n", *opts);
8727d7edd9Sdrochner 			command_help(NULL);
88334f5e8fSchristos 			return 0;
8927d7edd9Sdrochner 		}
9027d7edd9Sdrochner 		tmpopt |= r;
9127d7edd9Sdrochner 		opts++;
926d2637b2Schristos 		if (*opts == ' ' || *opts == '\t') {
936d2637b2Schristos 			do
946d2637b2Schristos 				opts++;		/* skip whitespace */
956d2637b2Schristos 			while (*opts == ' ' || *opts == '\t');
966d2637b2Schristos 			if (*opts == '-')
976d2637b2Schristos 				opts++;		/* skip - */
986d2637b2Schristos 			else if (*opts != '\0') {
996d2637b2Schristos 				printf("invalid arguments\n");
1006d2637b2Schristos 				command_help(NULL);
101334f5e8fSchristos 				return 0;
1026d2637b2Schristos 			}
1036d2637b2Schristos 		}
10427d7edd9Sdrochner 	}
10527d7edd9Sdrochner 
10627d7edd9Sdrochner 	*howto = tmpopt;
107334f5e8fSchristos 	return 1;
10827d7edd9Sdrochner }
10927d7edd9Sdrochner 
11027d7edd9Sdrochner int
parseboot(char * arg,char ** filename,int * howto)111334f5e8fSchristos parseboot(char *arg, char **filename, int *howto)
11227d7edd9Sdrochner {
11327d7edd9Sdrochner 	char *opts = NULL;
11427d7edd9Sdrochner 
11527d7edd9Sdrochner 	*filename = 0;
11627d7edd9Sdrochner 	*howto = 0;
11727d7edd9Sdrochner 
11827d7edd9Sdrochner 	/* if there were no arguments */
11927d7edd9Sdrochner 	if (!*arg)
120334f5e8fSchristos 		return 1;
12127d7edd9Sdrochner 
12227d7edd9Sdrochner 	/* format is... */
12349c105ffSjdolecek 	/* [[xxNx:]filename] [-adqsv] */
12427d7edd9Sdrochner 
12527d7edd9Sdrochner 	/* check for just args */
12627d7edd9Sdrochner 	if (arg[0] == '-')
12727d7edd9Sdrochner 		opts = arg;
12827d7edd9Sdrochner 	else {
12927d7edd9Sdrochner 		/* there's a file name */
13027d7edd9Sdrochner 		*filename = arg;
13127d7edd9Sdrochner 
13227d7edd9Sdrochner 		opts = gettrailer(arg);
13327d7edd9Sdrochner 		if (!*opts)
13427d7edd9Sdrochner 			opts = NULL;
13527d7edd9Sdrochner 		else if (*opts != '-') {
13627d7edd9Sdrochner 			printf("invalid arguments\n");
13727d7edd9Sdrochner 			command_help(NULL);
138334f5e8fSchristos 			return 0;
13927d7edd9Sdrochner 		}
14027d7edd9Sdrochner 	}
14127d7edd9Sdrochner 
14227d7edd9Sdrochner 	/* at this point, we have dealt with filenames. */
14327d7edd9Sdrochner 
14427d7edd9Sdrochner 	/* now, deal with options */
14527d7edd9Sdrochner 	if (opts) {
14627d7edd9Sdrochner 		if (parseopts(opts, howto) == 0)
147334f5e8fSchristos 			return 0;
14827d7edd9Sdrochner 	}
14927d7edd9Sdrochner 
150334f5e8fSchristos 	return 1;
15127d7edd9Sdrochner }
152