1*6b664a71Sisaki /* $NetBSD: parseutils.c,v 1.4 2024/01/07 07:58:34 isaki Exp $ */
27e950cc6Sminoura
37e950cc6Sminoura /*
47e950cc6Sminoura * from /sys/arch/i386/lib/parseutils.c
57e950cc6Sminoura * NetBSD: parseutils.c,v 1.3 2000/09/24 12:32:35 jdolecek Exp
67e950cc6Sminoura */
77e950cc6Sminoura
87e950cc6Sminoura /*
97e950cc6Sminoura * Copyright (c) 1996, 1997
107e950cc6Sminoura * Matthias Drochner. All rights reserved.
117e950cc6Sminoura * Copyright (c) 1996, 1997
127e950cc6Sminoura * Perry E. Metzger. All rights reserved.
137e950cc6Sminoura * Copyright (c) 1997
147e950cc6Sminoura * Jason R. Thorpe. All rights reserved
157e950cc6Sminoura *
167e950cc6Sminoura * Redistribution and use in source and binary forms, with or without
177e950cc6Sminoura * modification, are permitted provided that the following conditions
187e950cc6Sminoura * are met:
197e950cc6Sminoura * 1. Redistributions of source code must retain the above copyright
207e950cc6Sminoura * notice, this list of conditions and the following disclaimer.
217e950cc6Sminoura * 2. Redistributions in binary form must reproduce the above copyright
227e950cc6Sminoura * notice, this list of conditions and the following disclaimer in the
237e950cc6Sminoura * documentation and/or other materials provided with the distribution.
247e950cc6Sminoura * 3. All advertising materials mentioning features or use of this software
257e950cc6Sminoura * must display the following acknowledgements:
267e950cc6Sminoura * This product includes software developed for the NetBSD Project
277e950cc6Sminoura * by Matthias Drochner.
287e950cc6Sminoura * This product includes software developed for the NetBSD Project
297e950cc6Sminoura * by Perry E. Metzger.
307e950cc6Sminoura * 4. The names of the authors may not be used to endorse or promote products
317e950cc6Sminoura * derived from this software without specific prior written permission.
327e950cc6Sminoura *
337e950cc6Sminoura * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
347e950cc6Sminoura * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
357e950cc6Sminoura * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
367e950cc6Sminoura * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
377e950cc6Sminoura * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
387e950cc6Sminoura * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
397e950cc6Sminoura * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
407e950cc6Sminoura * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
417e950cc6Sminoura * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
427e950cc6Sminoura * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
437e950cc6Sminoura */
447e950cc6Sminoura
457e950cc6Sminoura #include <lib/libkern/libkern.h>
467e950cc6Sminoura #include <lib/libsa/stand.h>
477e950cc6Sminoura #include <sys/boot_flag.h>
487e950cc6Sminoura
497e950cc6Sminoura #include "libx68k.h"
507e950cc6Sminoura
517e950cc6Sminoura
527e950cc6Sminoura /*
537e950cc6Sminoura * chops the head from the arguments and returns the arguments if any,
547e950cc6Sminoura * or possibly an empty string.
557e950cc6Sminoura */
567e950cc6Sminoura char *
gettrailer(char * arg)57eaf7a401Sisaki gettrailer(char *arg)
587e950cc6Sminoura {
597e950cc6Sminoura char *options;
607e950cc6Sminoura
617e950cc6Sminoura if ((options = strchr(arg, ' ')) == NULL)
62eaf7a401Sisaki return "";
637e950cc6Sminoura else
647e950cc6Sminoura *options++ = '\0';
657e950cc6Sminoura
667e950cc6Sminoura /* trim leading blanks */
67f56f9deaSdholland while (*options == ' ')
687e950cc6Sminoura options++;
697e950cc6Sminoura
70eaf7a401Sisaki return options;
717e950cc6Sminoura }
727e950cc6Sminoura
737e950cc6Sminoura int
parseopts(const char * opts,int * howto)74eaf7a401Sisaki parseopts(const char *opts, int *howto)
757e950cc6Sminoura {
767e950cc6Sminoura int r, tmpopt = 0;
777e950cc6Sminoura
787e950cc6Sminoura opts++; /* skip - */
797e950cc6Sminoura while (*opts && *opts != ' ') {
807e950cc6Sminoura r = 0;
817e950cc6Sminoura BOOT_FLAG(*opts, r);
827e950cc6Sminoura if (r == 0) {
837e950cc6Sminoura printf("-%c: unknown flag\n", *opts);
84eaf7a401Sisaki return 0;
857e950cc6Sminoura }
867e950cc6Sminoura tmpopt |= r;
877e950cc6Sminoura opts++;
887e950cc6Sminoura }
897e950cc6Sminoura
907e950cc6Sminoura *howto = tmpopt;
91eaf7a401Sisaki return 1;
927e950cc6Sminoura }
937e950cc6Sminoura
947e950cc6Sminoura int
parseboot(char * arg,char ** filename,int * howto)95eaf7a401Sisaki parseboot(char *arg, char **filename, int *howto)
967e950cc6Sminoura {
977e950cc6Sminoura char *opts = NULL;
987e950cc6Sminoura
997e950cc6Sminoura *filename = 0;
1007e950cc6Sminoura *howto = 0;
1017e950cc6Sminoura
1027e950cc6Sminoura /* if there were no arguments */
1037e950cc6Sminoura if (!*arg)
104eaf7a401Sisaki return 1;
1057e950cc6Sminoura
1067e950cc6Sminoura /* format is... */
1077e950cc6Sminoura /* [[xxNx:]filename] [-adqsv] */
1087e950cc6Sminoura
1097e950cc6Sminoura /* check for just args */
110eaf7a401Sisaki if (arg[0] == '-') {
1117e950cc6Sminoura opts = arg;
112eaf7a401Sisaki } else {
1137e950cc6Sminoura /* there's a file name */
1147e950cc6Sminoura *filename = arg;
1157e950cc6Sminoura
1167e950cc6Sminoura opts = gettrailer(arg);
117eaf7a401Sisaki if (!*opts) {
1187e950cc6Sminoura opts = NULL;
119eaf7a401Sisaki } else if (*opts != '-') {
1207e950cc6Sminoura printf("invalid arguments\n");
121eaf7a401Sisaki return 0;
1227e950cc6Sminoura }
1237e950cc6Sminoura }
1247e950cc6Sminoura
1257e950cc6Sminoura /* at this point, we have dealt with filenames. */
1267e950cc6Sminoura
1277e950cc6Sminoura /* now, deal with options */
1287e950cc6Sminoura if (opts) {
1297e950cc6Sminoura if (parseopts(opts, howto) == 0)
130eaf7a401Sisaki return 0;
1317e950cc6Sminoura }
1327e950cc6Sminoura
133eaf7a401Sisaki return 1;
1347e950cc6Sminoura }
135