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