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