xref: /freebsd-src/contrib/mandoc/compat_getsubopt.c (revision 6d38604fc532a3fc060788e3ce40464b46047eaf)
1*6d38604fSBaptiste Daroussin /*	$Id: compat_getsubopt.c,v 1.6 2020/06/15 01:37:15 schwarze Exp $ */
261d06d6bSBaptiste Daroussin /*	$OpenBSD: getsubopt.c,v 1.4 2005/08/08 08:05:36 espie Exp $	*/
361d06d6bSBaptiste Daroussin 
461d06d6bSBaptiste Daroussin /*-
561d06d6bSBaptiste Daroussin  * Copyright (c) 1990, 1993
661d06d6bSBaptiste Daroussin  *	The Regents of the University of California.  All rights reserved.
761d06d6bSBaptiste Daroussin  *
861d06d6bSBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
961d06d6bSBaptiste Daroussin  * modification, are permitted provided that the following conditions
1061d06d6bSBaptiste Daroussin  * are met:
1161d06d6bSBaptiste Daroussin  * 1. Redistributions of source code must retain the above copyright
1261d06d6bSBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer.
1361d06d6bSBaptiste Daroussin  * 2. Redistributions in binary form must reproduce the above copyright
1461d06d6bSBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer in the
1561d06d6bSBaptiste Daroussin  *    documentation and/or other materials provided with the distribution.
1661d06d6bSBaptiste Daroussin  * 3. Neither the name of the University nor the names of its contributors
1761d06d6bSBaptiste Daroussin  *    may be used to endorse or promote products derived from this software
1861d06d6bSBaptiste Daroussin  *    without specific prior written permission.
1961d06d6bSBaptiste Daroussin  *
2061d06d6bSBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2161d06d6bSBaptiste Daroussin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2261d06d6bSBaptiste Daroussin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2361d06d6bSBaptiste Daroussin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2461d06d6bSBaptiste Daroussin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2561d06d6bSBaptiste Daroussin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2661d06d6bSBaptiste Daroussin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2761d06d6bSBaptiste Daroussin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2861d06d6bSBaptiste Daroussin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2961d06d6bSBaptiste Daroussin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3061d06d6bSBaptiste Daroussin  * SUCH DAMAGE.
3161d06d6bSBaptiste Daroussin  */
32*6d38604fSBaptiste Daroussin #include "config.h"
3361d06d6bSBaptiste Daroussin 
3461d06d6bSBaptiste Daroussin #include <unistd.h>
3561d06d6bSBaptiste Daroussin #include <stdlib.h>
3661d06d6bSBaptiste Daroussin #include <string.h>
3761d06d6bSBaptiste Daroussin 
3861d06d6bSBaptiste Daroussin int
getsubopt(char ** optionp,char * const * tokens,char ** valuep)3961d06d6bSBaptiste Daroussin getsubopt(char **optionp, char * const *tokens, char **valuep)
4061d06d6bSBaptiste Daroussin {
4161d06d6bSBaptiste Daroussin 	int cnt;
4261d06d6bSBaptiste Daroussin 	char *suboptarg;
4361d06d6bSBaptiste Daroussin 	char *p;
4461d06d6bSBaptiste Daroussin 
4561d06d6bSBaptiste Daroussin 	suboptarg = *valuep = NULL;
4661d06d6bSBaptiste Daroussin 
4761d06d6bSBaptiste Daroussin 	if (!optionp || !*optionp)
4861d06d6bSBaptiste Daroussin 		return(-1);
4961d06d6bSBaptiste Daroussin 
5061d06d6bSBaptiste Daroussin 	/* skip leading white-space, commas */
5161d06d6bSBaptiste Daroussin 	for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
5261d06d6bSBaptiste Daroussin 
5361d06d6bSBaptiste Daroussin 	if (!*p) {
5461d06d6bSBaptiste Daroussin 		*optionp = p;
5561d06d6bSBaptiste Daroussin 		return(-1);
5661d06d6bSBaptiste Daroussin 	}
5761d06d6bSBaptiste Daroussin 
5861d06d6bSBaptiste Daroussin 	/* save the start of the token, and skip the rest of the token. */
5961d06d6bSBaptiste Daroussin 	for (suboptarg = p;
6061d06d6bSBaptiste Daroussin 	    *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';);
6161d06d6bSBaptiste Daroussin 
6261d06d6bSBaptiste Daroussin 	if (*p) {
6361d06d6bSBaptiste Daroussin 		/*
6461d06d6bSBaptiste Daroussin 		 * If there's an equals sign, set the value pointer, and
6561d06d6bSBaptiste Daroussin 		 * skip over the value part of the token.  Terminate the
6661d06d6bSBaptiste Daroussin 		 * token.
6761d06d6bSBaptiste Daroussin 		 */
6861d06d6bSBaptiste Daroussin 		if (*p == '=') {
6961d06d6bSBaptiste Daroussin 			*p = '\0';
7061d06d6bSBaptiste Daroussin 			for (*valuep = ++p;
7161d06d6bSBaptiste Daroussin 			    *p && *p != ',' && *p != ' ' && *p != '\t'; ++p);
7261d06d6bSBaptiste Daroussin 			if (*p)
7361d06d6bSBaptiste Daroussin 				*p++ = '\0';
7461d06d6bSBaptiste Daroussin 		} else
7561d06d6bSBaptiste Daroussin 			*p++ = '\0';
7661d06d6bSBaptiste Daroussin 		/* Skip any whitespace or commas after this token. */
7761d06d6bSBaptiste Daroussin 		for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p);
7861d06d6bSBaptiste Daroussin 	}
7961d06d6bSBaptiste Daroussin 
8061d06d6bSBaptiste Daroussin 	/* set optionp for next round. */
8161d06d6bSBaptiste Daroussin 	*optionp = p;
8261d06d6bSBaptiste Daroussin 
8361d06d6bSBaptiste Daroussin 	for (cnt = 0; *tokens; ++tokens, ++cnt)
8461d06d6bSBaptiste Daroussin 		if (!strcmp(suboptarg, *tokens))
8561d06d6bSBaptiste Daroussin 			return(cnt);
8661d06d6bSBaptiste Daroussin 	return(-1);
8761d06d6bSBaptiste Daroussin }
88