1*62931Sbostic.\" Copyright (c) 1990, 1991, 1993 2*62931Sbostic.\" The Regents of the University of California. All rights reserved. 345446Sbostic.\" 445446Sbostic.\" %sccs.include.redist.man% 545446Sbostic.\" 6*62931Sbostic.\" @(#)getsubopt.3 8.1 (Berkeley) 06/09/93 745446Sbostic.\" 848352Scael.Dd 948352Scael.Dt GETSUBOPT 3 1048352Scael.Os 1148352Scael.Sh NAME 1248352Scael.Nm getsubopt 1348352Scael.Nd get sub options from an argument 1448352Scael.Sh SYNOPSIS 1558390Sbostic.Fd #include <stdlib.h> 1651707Sbostic.Vt extern char *suboptarg 1748352Scael.Ft int 1848352Scael.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep" 1948352Scael.Sh DESCRIPTION 2048352ScaelThe 2148352Scael.Fn getsubopt 2258390Sbosticfunction 2345446Sbosticparses a string containing tokens delimited by one or more tab, space or 2448352Scaelcomma 2548352Scael.Pq Ql \&, 2648352Scaelcharacters. 2745446SbosticIt is intended for use in parsing groups of option arguments provided 2845446Sbosticas part of a utility command line. 2948352Scael.Pp 3048352ScaelThe argument 3148352Scael.Fa optionp 3245446Sbosticis a pointer to a pointer to the string. 3348352ScaelThe argument 3448352Scael.Fa tokens 3548352Scaelis a pointer to a 3648352Scael.Dv NULL Ns -terminated 3748352Scaelarray of pointers to strings. 3848352Scael.Pp 3948352ScaelThe 4048352Scael.Fn getsubopt 4148352Scaelfunction 4245446Sbosticreturns the zero-based offset of the pointer in the 4348352Scael.Fa tokens 4445446Sbosticarray referencing a string which matches the first token 4548352Scaelin the string, or, \-1 if the string contains no tokens or 4648352Scael.Fa tokens 4745446Sbosticdoes not contain a matching string. 4848352Scael.Pp 4945446SbosticIf the token is of the form ``name=value'', the location referenced by 5048352Scael.Fa valuep 5145446Sbosticwill be set to point to the start of the ``value'' portion of the token. 5248352Scael.Pp 5345446SbosticOn return from 5448352Scael.Fn getsubopt , 5548352Scael.Fa optionp 5645446Sbosticwill be set to point to the start of the next token in the string, 5745446Sbosticor the null at the end of the string if no more tokens are present. 5845446SbosticThe external variable 5948352Scael.Fa suboptarg 6048352Scaelwill be set to point to the start of the current token, or 6148352Scael.Dv NULL 6248352Scaelif no 6345446Sbostictokens were present. 6448352ScaelThe argument 6548352Scael.Fa valuep 6648352Scaelwill be set to point to the ``value'' portion of the token, or 6748352Scael.Dv NULL 6845446Sbosticif no ``value'' portion was present. 6948352Scael.Sh EXAMPLE 7050725Scael.Bd -literal -compact 7145446Sbosticchar *tokens[] = { 7245446Sbostic #define ONE 0 7345446Sbostic "one", 7445446Sbostic #define TWO 1 7545446Sbostic "two", 7645446Sbostic NULL 7745446Sbostic}; 7845446Sbostic 7945446Sbostic\&... 8045446Sbostic 8145446Sbosticextern char *optarg, *suboptarg; 8245446Sbosticchar *options, *value; 8345446Sbostic 8448352Scaelwhile ((ch = getopt(argc, argv, "ab:")) != \-1) { 8545446Sbostic switch(ch) { 8645446Sbostic case 'a': 8745446Sbostic /* process ``a'' option */ 8845446Sbostic break; 8945446Sbostic case 'b': 9045446Sbostic options = optarg; 9145446Sbostic while (*options) { 9245446Sbostic switch(getsubopt(&options, tokens, &value)) { 9345446Sbostic case ONE: 9445446Sbostic /* process ``one'' sub option */ 9545446Sbostic break; 9645446Sbostic case TWO: 9745446Sbostic /* process ``two'' sub option */ 9845446Sbostic if (!value) 9945446Sbostic error("no value for two"); 10045446Sbostic i = atoi(value); 10145446Sbostic break; 10248352Scael case \-1: 10345446Sbostic if (suboptarg) 10445446Sbostic error("illegal sub option %s", 10548352Scael suboptarg); 10645446Sbostic else 10745446Sbostic error("missing sub option"); 10845446Sbostic break; 10945446Sbostic } 11045446Sbostic break; 11145446Sbostic } 11248352Scael.Ed 11348352Scael.Sh SEE ALSO 11448352Scael.Xr getopt 3 , 11548352Scael.Xr strsep 3 11648352Scael.Sh HISTORY 11748352ScaelThe 11848352Scael.Fn getsubopt 11962930Sbosticfunction first appeared in 4.4BSD. 120