xref: /netbsd-src/external/bsd/mdocml/dist/compat_getsubopt.c (revision 9ff1f2ac944731963b1d62cde605dabb5cb29a3a)
1cf816816Sjoerg #include "config.h"
2cf816816Sjoerg 
3fec65c98Schristos #if HAVE_GETSUBOPT
4c5f73b34Sjoerg 
5c5f73b34Sjoerg int dummy;
6c5f73b34Sjoerg 
7c5f73b34Sjoerg #else
8c5f73b34Sjoerg 
9*9ff1f2acSchristos /*	Id: compat_getsubopt.c,v 1.5 2014/08/17 20:53:50 schwarze Exp 	*/
10c5f73b34Sjoerg /*	$OpenBSD: getsubopt.c,v 1.4 2005/08/08 08:05:36 espie Exp $	*/
11c5f73b34Sjoerg 
12c5f73b34Sjoerg /*-
13c5f73b34Sjoerg  * Copyright (c) 1990, 1993
14c5f73b34Sjoerg  *	The Regents of the University of California.  All rights reserved.
15c5f73b34Sjoerg  *
16c5f73b34Sjoerg  * Redistribution and use in source and binary forms, with or without
17c5f73b34Sjoerg  * modification, are permitted provided that the following conditions
18c5f73b34Sjoerg  * are met:
19c5f73b34Sjoerg  * 1. Redistributions of source code must retain the above copyright
20c5f73b34Sjoerg  *    notice, this list of conditions and the following disclaimer.
21c5f73b34Sjoerg  * 2. Redistributions in binary form must reproduce the above copyright
22c5f73b34Sjoerg  *    notice, this list of conditions and the following disclaimer in the
23c5f73b34Sjoerg  *    documentation and/or other materials provided with the distribution.
24c5f73b34Sjoerg  * 3. Neither the name of the University nor the names of its contributors
25c5f73b34Sjoerg  *    may be used to endorse or promote products derived from this software
26c5f73b34Sjoerg  *    without specific prior written permission.
27c5f73b34Sjoerg  *
28c5f73b34Sjoerg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29c5f73b34Sjoerg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30c5f73b34Sjoerg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31c5f73b34Sjoerg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32c5f73b34Sjoerg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33c5f73b34Sjoerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34c5f73b34Sjoerg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35c5f73b34Sjoerg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36c5f73b34Sjoerg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37c5f73b34Sjoerg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38c5f73b34Sjoerg  * SUCH DAMAGE.
39c5f73b34Sjoerg  */
40c5f73b34Sjoerg 
41c5f73b34Sjoerg #include <unistd.h>
42c5f73b34Sjoerg #include <stdlib.h>
43c5f73b34Sjoerg #include <string.h>
44c5f73b34Sjoerg 
45c5f73b34Sjoerg int
getsubopt(char ** optionp,char * const * tokens,char ** valuep)46c5f73b34Sjoerg getsubopt(char **optionp, char * const *tokens, char **valuep)
47c5f73b34Sjoerg {
48c5f73b34Sjoerg 	int cnt;
49fec65c98Schristos 	char *suboptarg;
50c5f73b34Sjoerg 	char *p;
51c5f73b34Sjoerg 
52c5f73b34Sjoerg 	suboptarg = *valuep = NULL;
53c5f73b34Sjoerg 
54c5f73b34Sjoerg 	if (!optionp || !*optionp)
55c5f73b34Sjoerg 		return(-1);
56c5f73b34Sjoerg 
57c5f73b34Sjoerg 	/* skip leading white-space, commas */
58c5f73b34Sjoerg 	for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
59c5f73b34Sjoerg 
60c5f73b34Sjoerg 	if (!*p) {
61c5f73b34Sjoerg 		*optionp = p;
62c5f73b34Sjoerg 		return(-1);
63c5f73b34Sjoerg 	}
64c5f73b34Sjoerg 
65c5f73b34Sjoerg 	/* save the start of the token, and skip the rest of the token. */
66c5f73b34Sjoerg 	for (suboptarg = p;
67c5f73b34Sjoerg 	    *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
68c5f73b34Sjoerg 
69c5f73b34Sjoerg 	if (*p) {
70c5f73b34Sjoerg 		/*
71c5f73b34Sjoerg 		 * If there's an equals sign, set the value pointer, and
72c5f73b34Sjoerg 		 * skip over the value part of the token.  Terminate the
73c5f73b34Sjoerg 		 * token.
74c5f73b34Sjoerg 		 */
75c5f73b34Sjoerg 		if (*p == '=') {
76c5f73b34Sjoerg 			*p = '\0';
77c5f73b34Sjoerg 			for (*valuep = ++p;
78c5f73b34Sjoerg 			    *p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
79c5f73b34Sjoerg 			if (*p)
80c5f73b34Sjoerg 				*p++ = '\0';
81c5f73b34Sjoerg 		} else
82c5f73b34Sjoerg 			*p++ = '\0';
83c5f73b34Sjoerg 		/* Skip any whitespace or commas after this token. */
84c5f73b34Sjoerg 		for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
85c5f73b34Sjoerg 	}
86c5f73b34Sjoerg 
87c5f73b34Sjoerg 	/* set optionp for next round. */
88c5f73b34Sjoerg 	*optionp = p;
89c5f73b34Sjoerg 
90c5f73b34Sjoerg 	for (cnt = 0; *tokens; ++tokens, ++cnt)
91c5f73b34Sjoerg 		if (!strcmp(suboptarg, *tokens))
92c5f73b34Sjoerg 			return(cnt);
93c5f73b34Sjoerg 	return(-1);
94c5f73b34Sjoerg }
95c5f73b34Sjoerg 
96c5f73b34Sjoerg #endif
97