17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 237c478bd9Sstevel@tonic-gate * Copyright 1992-1996,2003 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate /* 327c478bd9Sstevel@tonic-gate * This file contains getoptstr(), which is getopt() for strings of arguments 337c478bd9Sstevel@tonic-gate * (not arrays of strings). It is used by the bootloader ({*fs,inet}boot) and 347c478bd9Sstevel@tonic-gate * the kernel to process argument strings. 357c478bd9Sstevel@tonic-gate */ 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include "getoptstr.h" 39*4870e0a7SRichard PALO #include <sys/null.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * This routine needs to be supplied by whatever uses this file. 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate char *strchr(const char *s, int c); 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #define ISNTWORDCH(c) ((c) == '\0' || ISSPACE(c)) 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate /* 527c478bd9Sstevel@tonic-gate * Prepare a gos_params structure for use by getoptstr(). 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate void 557c478bd9Sstevel@tonic-gate getoptstr_init(struct gos_params *params) 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate params->gos_pos = 1; 587c478bd9Sstevel@tonic-gate } 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate /* 617c478bd9Sstevel@tonic-gate * Modeled after lib/libc/port/gen/getopt.c. 627c478bd9Sstevel@tonic-gate * 637c478bd9Sstevel@tonic-gate * With params->gos_opts set to a string of options and params->gos_strp set to 647c478bd9Sstevel@tonic-gate * an argument string, getoptstr returns 657c478bd9Sstevel@tonic-gate * * the next option letter, where the options are given by the 667c478bd9Sstevel@tonic-gate * params->gos_opts string. If the option is followed by a ':' in gos_opts, 677c478bd9Sstevel@tonic-gate * then params->gos_optargp will point to the beginning of the argument in 687c478bd9Sstevel@tonic-gate * the string, and params->gos_optarglen will contain its length. 697c478bd9Sstevel@tonic-gate * * -1 if "--" or a non-option argument is encountered. In the former 707c478bd9Sstevel@tonic-gate * case, params->gos_strp is advanced to the next argument. 717c478bd9Sstevel@tonic-gate * * '?' if an illegal option is encountered or if an argument is not 727c478bd9Sstevel@tonic-gate * supplied for an option which requires one and ':' is not the first 737c478bd9Sstevel@tonic-gate * character of opts. In both cases, the option letter is available in 747c478bd9Sstevel@tonic-gate * params->gos_last_opt, and in the former case, params->gos_errp will 757c478bd9Sstevel@tonic-gate * point to the offending character. 767c478bd9Sstevel@tonic-gate * * ':' if an argument is not supplied for an option which requires one and 777c478bd9Sstevel@tonic-gate * ':' is the first character of params->gos_opts. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate int 807c478bd9Sstevel@tonic-gate getoptstr(struct gos_params *params) 817c478bd9Sstevel@tonic-gate { 827c478bd9Sstevel@tonic-gate char c; 837c478bd9Sstevel@tonic-gate char *cp; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * const because we should update params. Just make sure you don't 877c478bd9Sstevel@tonic-gate * use this after you update params->gos_strp. 887c478bd9Sstevel@tonic-gate */ 897c478bd9Sstevel@tonic-gate const char * const strp = params->gos_strp; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate if (params->gos_opts == NULL || strp == NULL) 937c478bd9Sstevel@tonic-gate return (-1); 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (params->gos_pos == 1) { 967c478bd9Sstevel@tonic-gate /* At beginning of new word. */ 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate if (strp[0] == '\0' || strp[0] != '-') 997c478bd9Sstevel@tonic-gate return (-1); 1007c478bd9Sstevel@tonic-gate if (ISNTWORDCH(strp[1])) { 1017c478bd9Sstevel@tonic-gate /* Lone dash. */ 1027c478bd9Sstevel@tonic-gate return (-1); 1037c478bd9Sstevel@tonic-gate } 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate /* Check for "--" */ 1067c478bd9Sstevel@tonic-gate if (strp[1] == '-' && ISNTWORDCH(strp[2])) { 1077c478bd9Sstevel@tonic-gate params->gos_strp = &strp[2]; 1087c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 1097c478bd9Sstevel@tonic-gate return (-1); 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate params->gos_last_opt = c = strp[params->gos_pos]; 1147c478bd9Sstevel@tonic-gate if (c == ':' || (cp = strchr(params->gos_opts, c)) == NULL) { 1157c478bd9Sstevel@tonic-gate /* Unrecognized option error. */ 1167c478bd9Sstevel@tonic-gate params->gos_errp = &strp[params->gos_pos]; 1177c478bd9Sstevel@tonic-gate ++params->gos_pos; 1187c478bd9Sstevel@tonic-gate if (ISNTWORDCH(strp[params->gos_pos])) { 1197c478bd9Sstevel@tonic-gate params->gos_strp = &strp[params->gos_pos]; 1207c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 1217c478bd9Sstevel@tonic-gate params->gos_pos = 1; 1227c478bd9Sstevel@tonic-gate } 1237c478bd9Sstevel@tonic-gate return ('?'); 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate if (cp[1] == ':') { 1277c478bd9Sstevel@tonic-gate /* This option expects an argument. */ 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate params->gos_strp = &strp[params->gos_pos + 1]; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate if (ISNTWORDCH(*params->gos_strp)) { 1327c478bd9Sstevel@tonic-gate /* The argument is in the next word. */ 1337c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate if (*params->gos_strp == '\0') { 1367c478bd9Sstevel@tonic-gate /* Not. Missing argument. */ 1377c478bd9Sstevel@tonic-gate params->gos_pos = 1; 1387c478bd9Sstevel@tonic-gate params->gos_optargp = NULL; 1397c478bd9Sstevel@tonic-gate return (params->gos_opts[0] == ':' ? ':' : '?'); 1407c478bd9Sstevel@tonic-gate } 1417c478bd9Sstevel@tonic-gate } 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate params->gos_optargp = params->gos_strp; 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate /* Advance to the next word. */ 1467c478bd9Sstevel@tonic-gate SKIP_WORD(params->gos_strp); 1477c478bd9Sstevel@tonic-gate params->gos_optarglen = params->gos_strp - params->gos_optargp; 1487c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate params->gos_pos = 1; 1517c478bd9Sstevel@tonic-gate } else { 1527c478bd9Sstevel@tonic-gate ++params->gos_pos; 1537c478bd9Sstevel@tonic-gate if (ISNTWORDCH(strp[params->gos_pos])) { 1547c478bd9Sstevel@tonic-gate params->gos_strp = &strp[params->gos_pos]; 1557c478bd9Sstevel@tonic-gate SKIP_SPC(params->gos_strp); 1567c478bd9Sstevel@tonic-gate params->gos_pos = 1; 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate params->gos_optargp = NULL; 1597c478bd9Sstevel@tonic-gate } 1607c478bd9Sstevel@tonic-gate return (c); 1617c478bd9Sstevel@tonic-gate } 162