Path: blob/main/sys/contrib/dev/acpica/common/getopt.c
48378 views
/******************************************************************************1*2* Module Name: getopt3*4*****************************************************************************/56/******************************************************************************7*8* 1. Copyright Notice9*10* Some or all of this work - Copyright (c) 1999 - 2025, Intel Corp.11* All rights reserved.12*13* 2. License14*15* 2.1. This is your license from Intel Corp. under its intellectual property16* rights. You may have additional license terms from the party that provided17* you this software, covering your right to use that party's intellectual18* property rights.19*20* 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a21* copy of the source code appearing in this file ("Covered Code") an22* irrevocable, perpetual, worldwide license under Intel's copyrights in the23* base code distributed originally by Intel ("Original Intel Code") to copy,24* make derivatives, distribute, use and display any portion of the Covered25* Code in any form, with the right to sublicense such rights; and26*27* 2.3. Intel grants Licensee a non-exclusive and non-transferable patent28* license (with the right to sublicense), under only those claims of Intel29* patents that are infringed by the Original Intel Code, to make, use, sell,30* offer to sell, and import the Covered Code and derivative works thereof31* solely to the minimum extent necessary to exercise the above copyright32* license, and in no event shall the patent license extend to any additions33* to or modifications of the Original Intel Code. No other license or right34* is granted directly or by implication, estoppel or otherwise;35*36* The above copyright and patent license is granted only if the following37* conditions are met:38*39* 3. Conditions40*41* 3.1. Redistribution of Source with Rights to Further Distribute Source.42* Redistribution of source code of any substantial portion of the Covered43* Code or modification with rights to further distribute source must include44* the above Copyright Notice, the above License, this list of Conditions,45* and the following Disclaimer and Export Compliance provision. In addition,46* Licensee must cause all Covered Code to which Licensee contributes to47* contain a file documenting the changes Licensee made to create that Covered48* Code and the date of any change. Licensee must include in that file the49* documentation of any changes made by any predecessor Licensee. Licensee50* must include a prominent statement that the modification is derived,51* directly or indirectly, from Original Intel Code.52*53* 3.2. Redistribution of Source with no Rights to Further Distribute Source.54* Redistribution of source code of any substantial portion of the Covered55* Code or modification without rights to further distribute source must56* include the following Disclaimer and Export Compliance provision in the57* documentation and/or other materials provided with distribution. In58* addition, Licensee may not authorize further sublicense of source of any59* portion of the Covered Code, and must include terms to the effect that the60* license from Licensee to its licensee is limited to the intellectual61* property embodied in the software Licensee provides to its licensee, and62* not to intellectual property embodied in modifications its licensee may63* make.64*65* 3.3. Redistribution of Executable. Redistribution in executable form of any66* substantial portion of the Covered Code or modification must reproduce the67* above Copyright Notice, and the following Disclaimer and Export Compliance68* provision in the documentation and/or other materials provided with the69* distribution.70*71* 3.4. Intel retains all right, title, and interest in and to the Original72* Intel Code.73*74* 3.5. Neither the name Intel nor any other trademark owned or controlled by75* Intel shall be used in advertising or otherwise to promote the sale, use or76* other dealings in products derived from or relating to the Covered Code77* without prior written authorization from Intel.78*79* 4. Disclaimer and Export Compliance80*81* 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED82* HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE83* IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,84* INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY85* UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY86* IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A87* PARTICULAR PURPOSE.88*89* 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES90* OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR91* COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,92* SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY93* CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL94* HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS95* SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY96* LIMITED REMEDY.97*98* 4.3. Licensee shall not export, either directly or indirectly, any of this99* software or system incorporating such software without first obtaining any100* required license or other approval from the U. S. Department of Commerce or101* any other agency or department of the United States Government. In the102* event Licensee exports any such software from the United States or103* re-exports any such software from a foreign destination, Licensee shall104* ensure that the distribution and export/re-export of the software is in105* compliance with all laws, regulations, orders, or other restrictions of the106* U.S. Export Administration Regulations. Licensee agrees that neither it nor107* any of its subsidiaries will export/re-export any technical data, process,108* software, or service, directly or indirectly, to any country for which the109* United States government or any agency thereof requires an export license,110* other governmental approval, or letter of assurance, without first obtaining111* such license, approval or letter.112*113*****************************************************************************114*115* Alternatively, you may choose to be licensed under the terms of the116* following license:117*118* Redistribution and use in source and binary forms, with or without119* modification, are permitted provided that the following conditions120* are met:121* 1. Redistributions of source code must retain the above copyright122* notice, this list of conditions, and the following disclaimer,123* without modification.124* 2. Redistributions in binary form must reproduce at minimum a disclaimer125* substantially similar to the "NO WARRANTY" disclaimer below126* ("Disclaimer") and any redistribution must be conditioned upon127* including a substantially similar Disclaimer requirement for further128* binary redistribution.129* 3. Neither the names of the above-listed copyright holders nor the names130* of any contributors may be used to endorse or promote products derived131* from this software without specific prior written permission.132*133* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS134* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT135* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR136* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT137* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,138* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT139* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,140* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY141* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT142* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE143* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.144*145* Alternatively, you may choose to be licensed under the terms of the146* GNU General Public License ("GPL") version 2 as published by the Free147* Software Foundation.148*149*****************************************************************************/150151/*152* ACPICA getopt() implementation153*154* Option strings:155* "f" - Option has no arguments156* "f:" - Option requires an argument157* "f+" - Option has an optional argument158* "f^" - Option has optional single-char sub-options159* "f|" - Option has required single-char sub-options160*/161162#include <contrib/dev/acpica/include/acpi.h>163#include <contrib/dev/acpica/include/accommon.h>164#include <contrib/dev/acpica/include/acapps.h>165166#define ACPI_OPTION_ERROR(msg, badchar) \167if (AcpiGbl_Opterr) {fprintf (stderr, "%s%c\n", msg, badchar);}168169170int AcpiGbl_Opterr = 1;171int AcpiGbl_Optind = 1;172int AcpiGbl_SubOptChar = 0;173char *AcpiGbl_Optarg;174175static int CurrentCharPtr = 1;176177178/*******************************************************************************179*180* FUNCTION: AcpiGetoptArgument181*182* PARAMETERS: argc, argv - from main183*184* RETURN: 0 if an argument was found, -1 otherwise. Sets AcpiGbl_Optarg185* to point to the next argument.186*187* DESCRIPTION: Get the next argument. Used to obtain arguments for the188* two-character options after the original call to AcpiGetopt.189* Note: Either the argument starts at the next character after190* the option, or it is pointed to by the next argv entry.191* (After call to AcpiGetopt, we need to backup to the previous192* argv entry).193*194******************************************************************************/195196int197AcpiGetoptArgument (198int argc,199char **argv)200{201202AcpiGbl_Optind--;203CurrentCharPtr++;204205if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')206{207AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];208}209else if (++AcpiGbl_Optind >= argc)210{211ACPI_OPTION_ERROR ("\nOption requires an argument", 0);212213CurrentCharPtr = 1;214return (-1);215}216else217{218AcpiGbl_Optarg = argv[AcpiGbl_Optind++];219}220221CurrentCharPtr = 1;222return (0);223}224225226/*******************************************************************************227*228* FUNCTION: AcpiGetopt229*230* PARAMETERS: argc, argv - from main231* opts - options info list232*233* RETURN: Option character or ACPI_OPT_END234*235* DESCRIPTION: Get the next option236*237******************************************************************************/238239int240AcpiGetopt(241int argc,242char **argv,243char *opts)244{245int CurrentChar;246char *OptsPtr;247248249if (CurrentCharPtr == 1)250{251if (AcpiGbl_Optind >= argc ||252argv[AcpiGbl_Optind][0] != '-' ||253argv[AcpiGbl_Optind][1] == '\0')254{255return (ACPI_OPT_END);256}257else if (strcmp (argv[AcpiGbl_Optind], "--") == 0)258{259AcpiGbl_Optind++;260return (ACPI_OPT_END);261}262}263264/* Get the option */265266CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr];267268/* Make sure that the option is legal */269270if (CurrentChar == ':' ||271(OptsPtr = strchr (opts, CurrentChar)) == NULL)272{273ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar);274275if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')276{277AcpiGbl_Optind++;278CurrentCharPtr = 1;279}280281return ('?');282}283284/* Option requires an argument? */285286if (*++OptsPtr == ':')287{288if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')289{290AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];291}292else if (++AcpiGbl_Optind >= argc)293{294ACPI_OPTION_ERROR (295"Option requires an argument: -", CurrentChar);296297CurrentCharPtr = 1;298return ('?');299}300else301{302AcpiGbl_Optarg = argv[AcpiGbl_Optind++];303}304305CurrentCharPtr = 1;306}307308/* Option has an optional argument? */309310else if (*OptsPtr == '+')311{312if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')313{314AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];315}316else if (++AcpiGbl_Optind >= argc)317{318AcpiGbl_Optarg = NULL;319}320else321{322AcpiGbl_Optarg = argv[AcpiGbl_Optind++];323}324325CurrentCharPtr = 1;326}327328/* Option has optional single-char arguments? */329330else if (*OptsPtr == '^')331{332if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')333{334AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];335}336else337{338AcpiGbl_Optarg = "^";339}340341AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];342AcpiGbl_Optind++;343CurrentCharPtr = 1;344}345346/* Option has a required single-char argument? */347348else if (*OptsPtr == '|')349{350if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')351{352AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];353}354else355{356ACPI_OPTION_ERROR (357"Option requires a single-character suboption: -",358CurrentChar);359360CurrentCharPtr = 1;361return ('?');362}363364AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];365AcpiGbl_Optind++;366CurrentCharPtr = 1;367}368369/* Option with no arguments */370371else372{373if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')374{375CurrentCharPtr = 1;376AcpiGbl_Optind++;377}378379AcpiGbl_Optarg = NULL;380}381382return (CurrentChar);383}384385386