xref: /minix3/sys/arch/i386/stand/lib/parseutils.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: parseutils.c,v 1.7 2014/01/05 20:52:57 jakllsch Exp $	*/
258a2b000SEvgeniy Ivanov 
358a2b000SEvgeniy Ivanov /*
458a2b000SEvgeniy Ivanov  * Copyright (c) 1996, 1997
558a2b000SEvgeniy Ivanov  * 	Matthias Drochner.  All rights reserved.
658a2b000SEvgeniy Ivanov  * Copyright (c) 1996, 1997
758a2b000SEvgeniy Ivanov  * 	Perry E. Metzger.  All rights reserved.
858a2b000SEvgeniy Ivanov  * Copyright (c) 1997
958a2b000SEvgeniy Ivanov  *	Jason R. Thorpe.  All rights reserved
1058a2b000SEvgeniy Ivanov  *
1158a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
1258a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
1358a2b000SEvgeniy Ivanov  * are met:
1458a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
1558a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
1658a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
1758a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
1858a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
1958a2b000SEvgeniy Ivanov  * 3. All advertising materials mentioning features or use of this software
2058a2b000SEvgeniy Ivanov  *    must display the following acknowledgements:
2158a2b000SEvgeniy Ivanov  *	This product includes software developed for the NetBSD Project
2258a2b000SEvgeniy Ivanov  *	by Matthias Drochner.
2358a2b000SEvgeniy Ivanov  *	This product includes software developed for the NetBSD Project
2458a2b000SEvgeniy Ivanov  *	by Perry E. Metzger.
2558a2b000SEvgeniy Ivanov  * 4. The names of the authors may not be used to endorse or promote products
2658a2b000SEvgeniy Ivanov  *    derived from this software without specific prior written permission.
2758a2b000SEvgeniy Ivanov  *
2858a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2958a2b000SEvgeniy Ivanov  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
3058a2b000SEvgeniy Ivanov  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
3158a2b000SEvgeniy Ivanov  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
3258a2b000SEvgeniy Ivanov  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
3358a2b000SEvgeniy Ivanov  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3458a2b000SEvgeniy Ivanov  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3558a2b000SEvgeniy Ivanov  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3658a2b000SEvgeniy Ivanov  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3758a2b000SEvgeniy Ivanov  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3858a2b000SEvgeniy Ivanov  */
3958a2b000SEvgeniy Ivanov 
4058a2b000SEvgeniy Ivanov #include <lib/libkern/libkern.h>
4158a2b000SEvgeniy Ivanov #include <lib/libsa/stand.h>
4258a2b000SEvgeniy Ivanov #include <sys/boot_flag.h>
4358a2b000SEvgeniy Ivanov 
4458a2b000SEvgeniy Ivanov #include "libi386.h"
4558a2b000SEvgeniy Ivanov 
4658a2b000SEvgeniy Ivanov /*
4758a2b000SEvgeniy Ivanov  * chops the head from the arguments and returns the arguments if any,
4858a2b000SEvgeniy Ivanov  * or possibly an empty string.
4958a2b000SEvgeniy Ivanov  */
5058a2b000SEvgeniy Ivanov char *
gettrailer(char * arg)5158a2b000SEvgeniy Ivanov gettrailer(char *arg)
5258a2b000SEvgeniy Ivanov {
5358a2b000SEvgeniy Ivanov 	char *options;
5458a2b000SEvgeniy Ivanov 
5558a2b000SEvgeniy Ivanov 	for (options = arg; *options; options++) {
5658a2b000SEvgeniy Ivanov 		switch (*options) {
5758a2b000SEvgeniy Ivanov 		case ' ':
5858a2b000SEvgeniy Ivanov 		case '\t':
5958a2b000SEvgeniy Ivanov 			*options++ = '\0';
6058a2b000SEvgeniy Ivanov 			break;
6158a2b000SEvgeniy Ivanov 		default:
6258a2b000SEvgeniy Ivanov 			continue;
6358a2b000SEvgeniy Ivanov 		}
6458a2b000SEvgeniy Ivanov 		break;
6558a2b000SEvgeniy Ivanov 	}
6658a2b000SEvgeniy Ivanov 	if (*options == '\0')
67*0a6a1f1dSLionel Sambuc 		return options;
6858a2b000SEvgeniy Ivanov 
6958a2b000SEvgeniy Ivanov 	/* trim leading blanks/tabs */
7058a2b000SEvgeniy Ivanov 	while (*options == ' ' || *options == '\t')
7158a2b000SEvgeniy Ivanov 		options++;
7258a2b000SEvgeniy Ivanov 
7358a2b000SEvgeniy Ivanov 	return options;
7458a2b000SEvgeniy Ivanov }
7558a2b000SEvgeniy Ivanov 
7658a2b000SEvgeniy Ivanov int
parseopts(const char * opts,int * howto)7758a2b000SEvgeniy Ivanov parseopts(const char *opts, int *howto)
7858a2b000SEvgeniy Ivanov {
7958a2b000SEvgeniy Ivanov 	int r, tmpopt = 0;
8058a2b000SEvgeniy Ivanov 
8158a2b000SEvgeniy Ivanov 	opts++; 	/* skip - */
8258a2b000SEvgeniy Ivanov 	while (*opts) {
8358a2b000SEvgeniy Ivanov 		r = 0;
8458a2b000SEvgeniy Ivanov 		BOOT_FLAG(*opts, r);
8558a2b000SEvgeniy Ivanov 		if (r == 0) {
8658a2b000SEvgeniy Ivanov 			printf("-%c: unknown flag\n", *opts);
8758a2b000SEvgeniy Ivanov 			command_help(NULL);
8858a2b000SEvgeniy Ivanov 			return 0;
8958a2b000SEvgeniy Ivanov 		}
9058a2b000SEvgeniy Ivanov 		tmpopt |= r;
9158a2b000SEvgeniy Ivanov 		opts++;
9258a2b000SEvgeniy Ivanov 		if (*opts == ' ' || *opts == '\t') {
9358a2b000SEvgeniy Ivanov 			do
9458a2b000SEvgeniy Ivanov 				opts++;		/* skip whitespace */
9558a2b000SEvgeniy Ivanov 			while (*opts == ' ' || *opts == '\t');
9658a2b000SEvgeniy Ivanov 			if (*opts == '-')
9758a2b000SEvgeniy Ivanov 				opts++;		/* skip - */
9858a2b000SEvgeniy Ivanov 			else if (*opts != '\0') {
9958a2b000SEvgeniy Ivanov 				printf("invalid arguments\n");
10058a2b000SEvgeniy Ivanov 				command_help(NULL);
10158a2b000SEvgeniy Ivanov 				return 0;
10258a2b000SEvgeniy Ivanov 			}
10358a2b000SEvgeniy Ivanov 		}
10458a2b000SEvgeniy Ivanov 	}
10558a2b000SEvgeniy Ivanov 
10658a2b000SEvgeniy Ivanov 	*howto = tmpopt;
10758a2b000SEvgeniy Ivanov 	return 1;
10858a2b000SEvgeniy Ivanov }
10958a2b000SEvgeniy Ivanov 
11058a2b000SEvgeniy Ivanov int
parseboot(char * arg,char ** filename,int * howto)11158a2b000SEvgeniy Ivanov parseboot(char *arg, char **filename, int *howto)
11258a2b000SEvgeniy Ivanov {
11358a2b000SEvgeniy Ivanov 	char *opts = NULL;
11458a2b000SEvgeniy Ivanov 
11558a2b000SEvgeniy Ivanov 	*filename = 0;
11658a2b000SEvgeniy Ivanov 	*howto = 0;
11758a2b000SEvgeniy Ivanov 
11858a2b000SEvgeniy Ivanov 	/* if there were no arguments */
11958a2b000SEvgeniy Ivanov 	if (!*arg)
12058a2b000SEvgeniy Ivanov 		return 1;
12158a2b000SEvgeniy Ivanov 
12258a2b000SEvgeniy Ivanov 	/* format is... */
12358a2b000SEvgeniy Ivanov 	/* [[xxNx:]filename] [-adqsv] */
12458a2b000SEvgeniy Ivanov 
12558a2b000SEvgeniy Ivanov 	/* check for just args */
12658a2b000SEvgeniy Ivanov 	if (arg[0] == '-')
12758a2b000SEvgeniy Ivanov 		opts = arg;
12858a2b000SEvgeniy Ivanov 	else {
12958a2b000SEvgeniy Ivanov 		/* there's a file name */
13058a2b000SEvgeniy Ivanov 		*filename = arg;
13158a2b000SEvgeniy Ivanov 
13258a2b000SEvgeniy Ivanov 		opts = gettrailer(arg);
13358a2b000SEvgeniy Ivanov 		if (!*opts)
13458a2b000SEvgeniy Ivanov 			opts = NULL;
13558a2b000SEvgeniy Ivanov 		else if (*opts != '-') {
13658a2b000SEvgeniy Ivanov 			printf("invalid arguments\n");
13758a2b000SEvgeniy Ivanov 			command_help(NULL);
13858a2b000SEvgeniy Ivanov 			return 0;
13958a2b000SEvgeniy Ivanov 		}
14058a2b000SEvgeniy Ivanov 	}
14158a2b000SEvgeniy Ivanov 
14258a2b000SEvgeniy Ivanov 	/* at this point, we have dealt with filenames. */
14358a2b000SEvgeniy Ivanov 
14458a2b000SEvgeniy Ivanov 	/* now, deal with options */
14558a2b000SEvgeniy Ivanov 	if (opts) {
14658a2b000SEvgeniy Ivanov 		if (parseopts(opts, howto) == 0)
14758a2b000SEvgeniy Ivanov 			return 0;
14858a2b000SEvgeniy Ivanov 	}
14958a2b000SEvgeniy Ivanov 
15058a2b000SEvgeniy Ivanov 	return 1;
15158a2b000SEvgeniy Ivanov }
152