1.\" $OpenBSD: getsubopt.3,v 1.16 2022/08/04 06:20:24 jsg Exp $ 2.\" 3.\" Copyright (c) 1990, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. Neither the name of the University nor the names of its contributors 15.\" may be used to endorse or promote products derived from this software 16.\" without specific prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28.\" SUCH DAMAGE. 29.\" 30.\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93 31.\" 32.Dd $Mdocdate: August 4 2022 $ 33.Dt GETSUBOPT 3 34.Os 35.Sh NAME 36.Nm getsubopt 37.Nd get sub options from an argument 38.Sh SYNOPSIS 39.In stdlib.h 40.Vt extern char *suboptarg; 41.Ft int 42.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep" 43.Sh DESCRIPTION 44The 45.Fn getsubopt 46function parses a string containing tokens delimited by one or more 47tab, space, or comma 48.Pq Ql \&, 49characters. 50It is intended for use in parsing groups of option arguments provided 51as part of a utility command line. 52.Pp 53The argument 54.Fa optionp 55is a pointer to a pointer to the string. 56The argument 57.Fa tokens 58is a pointer to a 59.Dv NULL Ns -terminated 60array of pointers to strings. 61.Pp 62The 63.Fn getsubopt 64function returns the zero-based offset of the pointer in the 65.Fa tokens 66array referencing a string which matches the first token 67in the string, or \-1 if the string contains no tokens or 68.Fa tokens 69does not contain a matching string. 70.Pp 71If the token is of the form 72.Ar name Ns = Ns Ar value , 73the location referenced by 74.Fa valuep 75will be set to point to the start of the 76.Dq value 77portion of the token. 78.Pp 79On return from 80.Fn getsubopt , 81.Fa optionp 82will be set to point to the start of the next token in the string, 83or the NUL at the end of the string if no more tokens are present. 84The comma, space, or tab character ending the token just parsed, 85and the equal sign separating name and value if any, are replaced 86with NUL bytes in the original 87.Pf * Fa optionp 88input string. 89The external variable 90.Fa suboptarg 91will be set to point to the start of the current token, or 92.Dv NULL 93if no tokens were present. 94The argument 95.Fa valuep 96will be set to point to the value portion of the token, or 97.Dv NULL 98if no value portion was present. 99.Sh EXAMPLES 100.Bd -literal 101char *tokens[] = { 102 #define ONE 0 103 "one", 104 #define TWO 1 105 "two", 106 NULL 107}; 108 109\&... 110 111extern char *optarg, *suboptarg; 112char *options, *value; 113 114while ((ch = getopt(argc, argv, "ab:")) != -1) { 115 switch (ch) { 116 case 'a': 117 /* process "a" option */ 118 break; 119 case 'b': 120 options = optarg; 121 while (*options) { 122 switch (getsubopt(&options, tokens, &value)) { 123 case ONE: 124 /* process "one" sub option */ 125 break; 126 case TWO: 127 /* process "two" sub option */ 128 if (!value) 129 error("no value for two"); 130 i = atoi(value); 131 break; 132 case -1: 133 if (suboptarg) 134 error("illegal sub option %s", 135 suboptarg); 136 else 137 error("missing sub option"); 138 break; 139 } 140 } 141 break; 142 } 143} 144.Ed 145.Sh SEE ALSO 146.Xr getopt 3 , 147.Xr strsep 3 148.Sh STANDARDS 149The 150.Fn getsubopt 151function conforms to 152.St -p1003.1-2008 . 153.Pp 154Allowing space and tab characters to separate tokens 155and the external variable 156.Va suboptarg 157are extensions to that standard. 158.Sh HISTORY 159The 160.Fn getsubopt 161function first appeared in 162.Bx 4.3 Net/2 . 163