unstack - fix ipcvecs
[minix.git] / sys / arch / i386 / stand / lib / parseutils.c
blob37099d18763973550f5839448b5ee4753a1e40f4
1 /* $NetBSD: parseutils.c,v 1.6 2011/08/18 13:20:04 christos Exp $ */
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
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.
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.
40 #include <lib/libkern/libkern.h>
41 #include <lib/libsa/stand.h>
42 #include <sys/boot_flag.h>
44 #include "libi386.h"
47 * chops the head from the arguments and returns the arguments if any,
48 * or possibly an empty string.
50 char *
51 gettrailer(char *arg)
53 char *options;
55 for (options = arg; *options; options++) {
56 switch (*options) {
57 case ' ':
58 case '\t':
59 *options++ = '\0';
60 break;
61 default:
62 continue;
64 break;
66 if (*options == '\0')
67 return "";
69 /* trim leading blanks/tabs */
70 while (*options == ' ' || *options == '\t')
71 options++;
73 return options;
76 int
77 parseopts(const char *opts, int *howto)
79 int r, tmpopt = 0;
81 opts++; /* skip - */
82 while (*opts) {
83 r = 0;
84 BOOT_FLAG(*opts, r);
85 if (r == 0) {
86 printf("-%c: unknown flag\n", *opts);
87 command_help(NULL);
88 return 0;
90 tmpopt |= r;
91 opts++;
92 if (*opts == ' ' || *opts == '\t') {
94 opts++; /* skip whitespace */
95 while (*opts == ' ' || *opts == '\t');
96 if (*opts == '-')
97 opts++; /* skip - */
98 else if (*opts != '\0') {
99 printf("invalid arguments\n");
100 command_help(NULL);
101 return 0;
106 *howto = tmpopt;
107 return 1;
111 parseboot(char *arg, char **filename, int *howto)
113 char *opts = NULL;
115 *filename = 0;
116 *howto = 0;
118 /* if there were no arguments */
119 if (!*arg)
120 return 1;
122 /* format is... */
123 /* [[xxNx:]filename] [-adqsv] */
125 /* check for just args */
126 if (arg[0] == '-')
127 opts = arg;
128 else {
129 /* there's a file name */
130 *filename = arg;
132 opts = gettrailer(arg);
133 if (!*opts)
134 opts = NULL;
135 else if (*opts != '-') {
136 printf("invalid arguments\n");
137 command_help(NULL);
138 return 0;
142 /* at this point, we have dealt with filenames. */
144 /* now, deal with options */
145 if (opts) {
146 if (parseopts(opts, howto) == 0)
147 return 0;
150 return 1;