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