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