xref: /illumos-gate/usr/src/lib/libnsl/nss/parse.c (revision 344db6f4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
2261961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*
24e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2561961e0fSrobinson  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28e8031f0aSraf #include "mt.h"
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <strings.h>
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate char *_strpbrk_escape(char *, char *);
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate  * _strtok_escape()
377c478bd9Sstevel@tonic-gate  *   Like strtok_r, except we don't break on a token if it is escaped
387c478bd9Sstevel@tonic-gate  *   with the escape character (\).
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate char *
_strtok_escape(char * string,char * sepset,char ** lasts)417c478bd9Sstevel@tonic-gate _strtok_escape(char *string, char *sepset, char **lasts)
427c478bd9Sstevel@tonic-gate {
4361961e0fSrobinson 	char	*r;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	/* first or subsequent call */
467c478bd9Sstevel@tonic-gate 	if (string == NULL)
477c478bd9Sstevel@tonic-gate 		string = *lasts;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	if (string == 0)		/* return if no tokens remaining */
507c478bd9Sstevel@tonic-gate 		return (NULL);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	if (*string == '\0')		/* return if no tokens remaining */
537c478bd9Sstevel@tonic-gate 		return (NULL);
547c478bd9Sstevel@tonic-gate 
5561961e0fSrobinson 	/* move past token */
5661961e0fSrobinson 	if ((r = _strpbrk_escape(string, sepset)) == NULL)
577c478bd9Sstevel@tonic-gate 		*lasts = 0;	/* indicate this is last token */
587c478bd9Sstevel@tonic-gate 	else {
597c478bd9Sstevel@tonic-gate 		*r = '\0';
607c478bd9Sstevel@tonic-gate 		*lasts = r+1;
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 	return (string);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * Return ptr to first occurrence of any non-escaped character from `brkset'
677c478bd9Sstevel@tonic-gate  * in the character string `string'; NULL if none exists.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate char *
_strpbrk_escape(char * string,char * brkset)707c478bd9Sstevel@tonic-gate _strpbrk_escape(char *string, char *brkset)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	const char *p;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	do {
757c478bd9Sstevel@tonic-gate 		for (p = brkset; *p != '\0' && *p != *string; ++p)
767c478bd9Sstevel@tonic-gate 			;
777c478bd9Sstevel@tonic-gate 		if (p == string)
787c478bd9Sstevel@tonic-gate 			return ((char *)string);
797c478bd9Sstevel@tonic-gate 		if (*p != '\0') {
807c478bd9Sstevel@tonic-gate 			if (*(string-1) != '\\')
817c478bd9Sstevel@tonic-gate 				return ((char *)string);
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 	} while (*string++);
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	return (NULL);
867c478bd9Sstevel@tonic-gate }
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate char   *
_escape(char * s,char * esc)907c478bd9Sstevel@tonic-gate _escape(char *s, char *esc)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	int	nescs = 0;	/* number of escapes to place in s */
937c478bd9Sstevel@tonic-gate 	int	i, j;
947c478bd9Sstevel@tonic-gate 	int	len_s;
957c478bd9Sstevel@tonic-gate 	char	*tmp;
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (s == NULL || esc == NULL)
987c478bd9Sstevel@tonic-gate 		return (NULL);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	len_s = strlen(s);
1017c478bd9Sstevel@tonic-gate 	for (i = 0; i < len_s; i++)
1027c478bd9Sstevel@tonic-gate 		if (strchr(esc, s[i]))
1037c478bd9Sstevel@tonic-gate 			nescs++;
10461961e0fSrobinson 	if ((tmp = malloc(nescs + len_s + 1)) == NULL)
1057c478bd9Sstevel@tonic-gate 		return (NULL);
1067c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < len_s; i++) {
1077c478bd9Sstevel@tonic-gate 		if (strchr(esc, s[i])) {
1087c478bd9Sstevel@tonic-gate 			tmp[j++] = '\\';
1097c478bd9Sstevel@tonic-gate 		}
1107c478bd9Sstevel@tonic-gate 		tmp[j++] = s[i];
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	tmp[len_s + nescs] = '\0';
1137c478bd9Sstevel@tonic-gate 	return (tmp);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate char *
_unescape(char * s,char * esc)1187c478bd9Sstevel@tonic-gate _unescape(char *s, char *esc)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	int	len_s;
1217c478bd9Sstevel@tonic-gate 	int	i, j;
1227c478bd9Sstevel@tonic-gate 	char	*tmp;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (s == NULL || esc == NULL)
12561961e0fSrobinson 		return (NULL);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	len_s = strlen(s);
12861961e0fSrobinson 	if ((tmp = malloc(len_s + 1)) == NULL)
12961961e0fSrobinson 		return (NULL);
1307c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < len_s; i++) {
1317c478bd9Sstevel@tonic-gate 		if (s[i] == '\\' && strchr(esc, s[i + 1]))
1327c478bd9Sstevel@tonic-gate 			tmp[j++] = s[++i];
1337c478bd9Sstevel@tonic-gate 		else
1347c478bd9Sstevel@tonic-gate 			tmp[j++] = s[i];
1357c478bd9Sstevel@tonic-gate 	}
136*344db6f4SToomas Soome 	tmp[j] = '\0';
1377c478bd9Sstevel@tonic-gate 	return (tmp);
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate char *
_strdup_null(char * s)1417c478bd9Sstevel@tonic-gate _strdup_null(char *s)
1427c478bd9Sstevel@tonic-gate {
1437c478bd9Sstevel@tonic-gate 	return (strdup(s ? s : ""));
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * read a line into buffer from a mmap'ed file.
1497c478bd9Sstevel@tonic-gate  * return length of line read.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate int
_readbufline(char * mapbuf,int mapsize,char * buffer,int buflen,int * lastlen)1527c478bd9Sstevel@tonic-gate _readbufline(char *mapbuf,	/* input mmap buffer */
1537c478bd9Sstevel@tonic-gate     int mapsize,		/* input size */
1547c478bd9Sstevel@tonic-gate     char *buffer,		/* output storage */
1557c478bd9Sstevel@tonic-gate     int buflen,			/* output size */
1567c478bd9Sstevel@tonic-gate     int *lastlen)		/* input read till here last time */
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	int	linelen;
1597c478bd9Sstevel@tonic-gate 
16061961e0fSrobinson 	for (;;) {
1617c478bd9Sstevel@tonic-gate 		linelen = 0;
1627c478bd9Sstevel@tonic-gate 		while (linelen < buflen - 1) {	/* "- 1" saves room for \n\0 */
1637c478bd9Sstevel@tonic-gate 			if (*lastlen >= mapsize) {
1647c478bd9Sstevel@tonic-gate 				if (linelen == 0 ||
1657c478bd9Sstevel@tonic-gate 					buffer[linelen - 1] == '\\') {
1667c478bd9Sstevel@tonic-gate 						return (-1);
1677c478bd9Sstevel@tonic-gate 					} else {
1687c478bd9Sstevel@tonic-gate 						buffer[linelen] = '\n';
1697c478bd9Sstevel@tonic-gate 						buffer[linelen + 1] = '\0';
1707c478bd9Sstevel@tonic-gate 						return (linelen);
1717c478bd9Sstevel@tonic-gate 					}
1727c478bd9Sstevel@tonic-gate 			}
1737c478bd9Sstevel@tonic-gate 			switch (mapbuf[*lastlen]) {
1747c478bd9Sstevel@tonic-gate 			case '\n':
1757c478bd9Sstevel@tonic-gate 				(*lastlen)++;
1767c478bd9Sstevel@tonic-gate 				if (linelen > 0 &&
1777c478bd9Sstevel@tonic-gate 				    buffer[linelen - 1] == '\\') {
1787c478bd9Sstevel@tonic-gate 					--linelen;	/* remove the '\\' */
1797c478bd9Sstevel@tonic-gate 				} else {
1807c478bd9Sstevel@tonic-gate 					buffer[linelen] = '\n';
1817c478bd9Sstevel@tonic-gate 					buffer[linelen + 1] = '\0';
1827c478bd9Sstevel@tonic-gate 					return (linelen);
1837c478bd9Sstevel@tonic-gate 				}
1847c478bd9Sstevel@tonic-gate 				break;
1857c478bd9Sstevel@tonic-gate 			default:
1867c478bd9Sstevel@tonic-gate 				buffer[linelen] = mapbuf[*lastlen];
1877c478bd9Sstevel@tonic-gate 				(*lastlen)++;
1887c478bd9Sstevel@tonic-gate 				linelen++;
1897c478bd9Sstevel@tonic-gate 			}
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 		/* Buffer overflow -- eat rest of line and loop again */
1927c478bd9Sstevel@tonic-gate 		while (mapbuf[*lastlen] != '\n') {
1937c478bd9Sstevel@tonic-gate 			if (mapbuf[*lastlen] == EOF) {
1947c478bd9Sstevel@tonic-gate 				return (-1);
1957c478bd9Sstevel@tonic-gate 			}
1967c478bd9Sstevel@tonic-gate 			(*lastlen)++;
1977c478bd9Sstevel@tonic-gate 		};
1987c478bd9Sstevel@tonic-gate 	}
19961961e0fSrobinson 	/* NOTREACHED */
2007c478bd9Sstevel@tonic-gate }
201