1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 1992-1996,2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate 31*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate /* 34*7c478bd9Sstevel@tonic-gate * This file contains getoptstr(), which is getopt() for strings of arguments 35*7c478bd9Sstevel@tonic-gate * (not arrays of strings). It is used by the bootloader ({*fs,inet}boot) and 36*7c478bd9Sstevel@tonic-gate * the kernel to process argument strings. 37*7c478bd9Sstevel@tonic-gate */ 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #include "getoptstr.h" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate /* 44*7c478bd9Sstevel@tonic-gate * This routine needs to be supplied by whatever uses this file. 45*7c478bd9Sstevel@tonic-gate */ 46*7c478bd9Sstevel@tonic-gate char *strchr(const char *s, int c); 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate #define NULL ((void *)0) 50*7c478bd9Sstevel@tonic-gate 51*7c478bd9Sstevel@tonic-gate #define ISNTWORDCH(c) ((c) == '\0' || ISSPACE(c)) 52*7c478bd9Sstevel@tonic-gate 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate /* 55*7c478bd9Sstevel@tonic-gate * Prepare a gos_params structure for use by getoptstr(). 56*7c478bd9Sstevel@tonic-gate */ 57*7c478bd9Sstevel@tonic-gate void 58*7c478bd9Sstevel@tonic-gate getoptstr_init(struct gos_params *params) 59*7c478bd9Sstevel@tonic-gate { 60*7c478bd9Sstevel@tonic-gate params->gos_pos = 1; 61*7c478bd9Sstevel@tonic-gate } 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate /* 64*7c478bd9Sstevel@tonic-gate * Modeled after lib/libc/port/gen/getopt.c. 65*7c478bd9Sstevel@tonic-gate * 66*7c478bd9Sstevel@tonic-gate * With params->gos_opts set to a string of options and params->gos_strp set to 67*7c478bd9Sstevel@tonic-gate * an argument string, getoptstr returns 68*7c478bd9Sstevel@tonic-gate * * the next option letter, where the options are given by the 69*7c478bd9Sstevel@tonic-gate * params->gos_opts string. If the option is followed by a ':' in gos_opts, 70*7c478bd9Sstevel@tonic-gate * then params->gos_optargp will point to the beginning of the argument in 71*7c478bd9Sstevel@tonic-gate * the string, and params->gos_optarglen will contain its length. 72*7c478bd9Sstevel@tonic-gate * * -1 if "--" or a non-option argument is encountered. In the former 73*7c478bd9Sstevel@tonic-gate * case, params->gos_strp is advanced to the next argument. 74*7c478bd9Sstevel@tonic-gate * * '?' if an illegal option is encountered or if an argument is not 75*7c478bd9Sstevel@tonic-gate * supplied for an option which requires one and ':' is not the first 76*7c478bd9Sstevel@tonic-gate * character of opts. In both cases, the option letter is available in 77*7c478bd9Sstevel@tonic-gate * params->gos_last_opt, and in the former case, params->gos_errp will 78*7c478bd9Sstevel@tonic-gate * point to the offending character. 79*7c478bd9Sstevel@tonic-gate * * ':' if an argument is not supplied for an option which requires one and 80*7c478bd9Sstevel@tonic-gate * ':' is the first character of params->gos_opts. 81*7c478bd9Sstevel@tonic-gate */ 82*7c478bd9Sstevel@tonic-gate int 83*7c478bd9Sstevel@tonic-gate getoptstr(struct gos_params *params) 84*7c478bd9Sstevel@tonic-gate { 85*7c478bd9Sstevel@tonic-gate char c; 86*7c478bd9Sstevel@tonic-gate char *cp; 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate /* 89*7c478bd9Sstevel@tonic-gate * const because we should update params. Just make sure you don't 90*7c478bd9Sstevel@tonic-gate * use this after you update params->gos_strp. 91*7c478bd9Sstevel@tonic-gate */ 92*7c478bd9Sstevel@tonic-gate const char * const strp = params->gos_strp; 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate 95*7c478bd9Sstevel@tonic-gate if (params->gos_opts == NULL || strp == NULL) 96*7c478bd9Sstevel@tonic-gate return (-1); 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate if (params->gos_pos == 1) { 99*7c478bd9Sstevel@tonic-gate /* At beginning of new word. */ 100*7c478bd9Sstevel@tonic-gate 101*7c478bd9Sstevel@tonic-gate if (strp[0] == '\0' || strp[0] != '-') 102*7c478bd9Sstevel@tonic-gate return (-1); 103*7c478bd9Sstevel@tonic-gate if (ISNTWORDCH(strp[1])) { 104*7c478bd9Sstevel@tonic-gate /* Lone dash. */ 105*7c478bd9Sstevel@tonic-gate return (-1); 106*7c478bd9Sstevel@tonic-gate } 107*7c478bd9Sstevel@tonic-gate 108*7c478bd9Sstevel@tonic-gate /* Check for "--" */ 109*7c478bd9Sstevel@tonic-gate if (strp[1] == '-' && ISNTWORDCH(strp[2])) { 110*7c478bd9Sstevel@tonic-gate params->gos_strp = &strp[2]; 111*7c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 112*7c478bd9Sstevel@tonic-gate return (-1); 113*7c478bd9Sstevel@tonic-gate } 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate params->gos_last_opt = c = strp[params->gos_pos]; 117*7c478bd9Sstevel@tonic-gate if (c == ':' || (cp = strchr(params->gos_opts, c)) == NULL) { 118*7c478bd9Sstevel@tonic-gate /* Unrecognized option error. */ 119*7c478bd9Sstevel@tonic-gate params->gos_errp = &strp[params->gos_pos]; 120*7c478bd9Sstevel@tonic-gate ++params->gos_pos; 121*7c478bd9Sstevel@tonic-gate if (ISNTWORDCH(strp[params->gos_pos])) { 122*7c478bd9Sstevel@tonic-gate params->gos_strp = &strp[params->gos_pos]; 123*7c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 124*7c478bd9Sstevel@tonic-gate params->gos_pos = 1; 125*7c478bd9Sstevel@tonic-gate } 126*7c478bd9Sstevel@tonic-gate return ('?'); 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate if (cp[1] == ':') { 130*7c478bd9Sstevel@tonic-gate /* This option expects an argument. */ 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate params->gos_strp = &strp[params->gos_pos + 1]; 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate if (ISNTWORDCH(*params->gos_strp)) { 135*7c478bd9Sstevel@tonic-gate /* The argument is in the next word. */ 136*7c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate if (*params->gos_strp == '\0') { 139*7c478bd9Sstevel@tonic-gate /* Not. Missing argument. */ 140*7c478bd9Sstevel@tonic-gate params->gos_pos = 1; 141*7c478bd9Sstevel@tonic-gate params->gos_optargp = NULL; 142*7c478bd9Sstevel@tonic-gate return (params->gos_opts[0] == ':' ? ':' : '?'); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate } 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate params->gos_optargp = params->gos_strp; 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate /* Advance to the next word. */ 149*7c478bd9Sstevel@tonic-gate SKIP_WORD(params->gos_strp); 150*7c478bd9Sstevel@tonic-gate params->gos_optarglen = params->gos_strp - params->gos_optargp; 151*7c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate params->gos_pos = 1; 154*7c478bd9Sstevel@tonic-gate } else { 155*7c478bd9Sstevel@tonic-gate ++params->gos_pos; 156*7c478bd9Sstevel@tonic-gate if (ISNTWORDCH(strp[params->gos_pos])) { 157*7c478bd9Sstevel@tonic-gate params->gos_strp = &strp[params->gos_pos]; 158*7c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 159*7c478bd9Sstevel@tonic-gate params->gos_pos = 1; 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate params->gos_optargp = NULL; 162*7c478bd9Sstevel@tonic-gate } 163*7c478bd9Sstevel@tonic-gate return (c); 164*7c478bd9Sstevel@tonic-gate } 165