xref: /netbsd-src/sys/arch/i386/stand/lib/parseutils.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: parseutils.c,v 1.1 1997/09/17 17:13:03 drochner 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 
43 #include "libi386.h"
44 
45 /*
46  * chops the head from the arguments and returns the arguments if any,
47  * or possibly an empty string.
48  */
49 char *
50 gettrailer(arg)
51 	char *arg;
52 {
53 	char *options;
54 
55 	if ((options = strchr(arg, ' ')) == NULL)
56 		return ("");
57 	else
58 		*options++ = '\0';
59 
60 	/* trim leading blanks */
61 	while (*options && *options == ' ')
62 		options++;
63 
64 	return (options);
65 }
66 
67 int
68 parseopts(opts, howto)
69 	char *opts;
70 	int *howto;
71 {
72 	int r, tmpopt = 0;
73 
74 	opts++; 	/* skip - */
75 	while (*opts && *opts != ' ') {
76 		r = netbsd_opt(*opts);
77 		if (r == -1) {
78 			printf("-%c: unknown flag\n", *opts);
79 			command_help(NULL);
80 			return(0);
81 		}
82 		tmpopt |= r;
83 		opts++;
84 	}
85 
86 	*howto = tmpopt;
87 	return(1);
88 }
89 
90 int
91 parseboot(arg, filename, howto)
92 	char *arg;
93 	char **filename;
94 	int *howto;
95 {
96 	char *opts = NULL;
97 
98 	*filename = 0;
99 	*howto = 0;
100 
101 	/* if there were no arguments */
102 	if (!*arg)
103 		return(1);
104 
105 	/* format is... */
106 	/* [[xxNx:]filename] [-adrs] */
107 
108 	/* check for just args */
109 	if (arg[0] == '-')
110 		opts = arg;
111 	else {
112 		/* there's a file name */
113 		*filename = arg;
114 
115 		opts = gettrailer(arg);
116 		if (!*opts)
117 			opts = NULL;
118 		else if (*opts != '-') {
119 			printf("invalid arguments\n");
120 			command_help(NULL);
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