Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

getsubopt(3) [netbsd man page]

GETSUBOPT(3)						   BSD Library Functions Manual 					      GETSUBOPT(3)

NAME
getsubopt -- get sub options from an argument LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <unistd.h> extern char *suboptarg int getsubopt(char **optionp, char * const *tokens, char **valuep); DESCRIPTION
The getsubopt() function parses a string containing tokens delimited by one or more tab, space or comma (',') characters. It is intended for use in parsing groups of option arguments provided as part of a utility command line. The argument optionp is a pointer to a pointer to the string. The argument tokens is a pointer to a NULL-terminated array of pointers to strings. The getsubopt() function returns the zero-based offset of the pointer in the tokens array referencing a string which matches the first token in the string, or -1 if the string contains no tokens or tokens does not contain a matching string. If the token is of the form ``name=value'', the location referenced by valuep will be set to point to the start of the ``value'' portion of the token. On return from getsubopt(), optionp will be set to point to the start of the next token in the string, or the null at the end of the string if no more tokens are present. The external variable suboptarg will be set to point to the start of the current token, or NULL if no tokens were present. The argument valuep will be set to point to the ``value'' portion of the token, or NULL if no ``value'' portion was present. EXAMPLES
char *tokens[] = { #define ONE 0 "one", #define TWO 1 "two", NULL }; ... extern char *optarg, *suboptarg; char *options, *value; while ((ch = getopt(argc, argv, "ab:")) != -1) { switch(ch) { case 'a': /* process ``a'' option */ break; case 'b': options = optarg; while (*options) { switch(getsubopt(&options, tokens, &value)) { case ONE: /* process ``one'' sub option */ break; case TWO: /* process ``two'' sub option */ if (!value) error("no value for two"); i = atoi(value); break; case -1: if (suboptarg) error("unknown sub option %s", suboptarg); else error("missing sub option"); break; } break; } SEE ALSO
getopt(3), strsep(3) HISTORY
The getsubopt() function first appeared in 4.4BSD, and is included in IEEE Std 1003.1-2008 (``POSIX.1''). BSD
November 17, 2009 BSD

Check Out this Related Man Page

GETSUBOPT(3)						     Linux Programmer's Manual						      GETSUBOPT(3)

NAME
getsubopt - parse suboption arguments from a string SYNOPSIS
#define _XOPEN_SOURCE 500 #include <stdlib.h> int getsubopt(char **optionp, char * const *tokens, char **valuep); DESCRIPTION
getsubopt() parses the list of comma-separated suboptions provided in optionp. (Such a suboption list is typically produced when getopt(3) is used to parse a command line; see for example the -o option of mount(8).) Each suboption may include an associated value, which is sep- arated from the suboption name by an equal sign. The following is an example of the kind of string that might be passed in optionp: ro,name=xyz The tokens argument is a pointer to a NULL-terminated list of the tokens that getsubopt() will look for in optionp. The tokens should be distinct, null-terminated strings containing at least one character, with no embedded equal signs or commas. Each call to getsubopt() returns information about the next unprocessed suboption in optionp. The first equal sign in a suboption (if any) is interpreted as a separator between the name and the value of that suboption. The value extends to the next comma, or (for the last sub- option) to the end of the string. If the name of the suboption matches a known name from tokens, and a value string was found, getsubopt() sets *valuep to the address of that string. The first comma in optionp is overwritten with a null byte, so *valuep is precisely the "value string" for that suboption. If the suboption is recognized, but no value string was found, *valuep is set to NULL. When getsubopt() returns, optionp points to the next suboption, or to the null character at the end of the string if the last suboption was just processed. RETURN VALUE
If the first suboption in optionp is recognized, getsubopt() returns the index of the matching suboption element in tokens. Otherwise, -1 is returned and *valuep is the entire name[=value] string. Since *optionp is changed, the first suboption before the call to getsubopt() is not (necessarily) the same as the first suboption after getsubopt(). CONFORMING TO
POSIX.1-2001. NOTES
Since getsubopt() overwrites any commas it finds in the string *optionp, that string must be writable; it cannot be a string constant. EXAMPLE
The following program expects suboptions following a "-o" option. #define _XOPEN_SOURCE 500 #include <stdlib.h> #include <assert.h> #include <stdio.h> int main(int argc, char **argv) { enum { RO_OPT = 0, RW_OPT, NAME_OPT }; char *const token[] = { [RO_OPT] = "ro", [RW_OPT] = "rw", [NAME_OPT] = "name", NULL }; char *subopts; char *value; int opt; int readonly = 0; int readwrite = 0; char *name = NULL; int errfnd = 0; while ((opt = getopt(argc, argv, "o:")) != -1) { switch (opt) { case 'o': subopts = optarg; while (*subopts != '' && !errfnd) { switch (getsubopt(&subopts, token, &value)) { case RO_OPT: readonly = 1; break; case RW_OPT: readwrite = 1; break; case NAME_OPT: if (value == NULL) { fprintf(stderr, "Missing value for " "suboption '%s' ", token[NAME_OPT]); errfnd = 1; continue; } name = value; break; default: fprintf(stderr, "No match found " "for token: /%s/ ", value); errfnd = 1; break; } } if (readwrite && readonly) { fprintf(stderr, "Only one of '%s' and '%s' can be " "specified ", token[RO_OPT], token[RW_OPT]); errfnd = 1; } break; default: errfnd = 1; } } if (errfnd || argc == 1) { fprintf(stderr, " Usage: %s -o <suboptstring> ", argv[0]); fprintf(stderr, "suboptions are 'ro', 'rw', " "and 'name=<value>' "); exit(EXIT_FAILURE); } /* Remainder of program... */ exit(EXIT_SUCCESS); } SEE ALSO
getopt(3), feature_test_macros(7) COLOPHON
This page is part of release 3.25 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2008-05-29 GETSUBOPT(3)
Man Page