xref: /onnv-gate/usr/src/lib/libnsl/nss/parse.c (revision 132)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*132Srobinson 
230Sstevel@tonic-gate /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*132Srobinson  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <strings.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate char *_strpbrk_escape(char *, char *);
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * _strtok_escape()
380Sstevel@tonic-gate  *   Like strtok_r, except we don't break on a token if it is escaped
390Sstevel@tonic-gate  *   with the escape character (\).
400Sstevel@tonic-gate  */
410Sstevel@tonic-gate char *
420Sstevel@tonic-gate _strtok_escape(char *string, char *sepset, char **lasts)
430Sstevel@tonic-gate {
44*132Srobinson 	char	*r;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	/* first or subsequent call */
470Sstevel@tonic-gate 	if (string == NULL)
480Sstevel@tonic-gate 		string = *lasts;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	if (string == 0)		/* return if no tokens remaining */
510Sstevel@tonic-gate 		return (NULL);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if (*string == '\0')		/* return if no tokens remaining */
540Sstevel@tonic-gate 		return (NULL);
550Sstevel@tonic-gate 
56*132Srobinson 	/* move past token */
57*132Srobinson 	if ((r = _strpbrk_escape(string, sepset)) == NULL)
580Sstevel@tonic-gate 		*lasts = 0;	/* indicate this is last token */
590Sstevel@tonic-gate 	else {
600Sstevel@tonic-gate 		*r = '\0';
610Sstevel@tonic-gate 		*lasts = r+1;
620Sstevel@tonic-gate 	}
630Sstevel@tonic-gate 	return (string);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Return ptr to first occurrence of any non-escaped character from `brkset'
680Sstevel@tonic-gate  * in the character string `string'; NULL if none exists.
690Sstevel@tonic-gate  */
700Sstevel@tonic-gate char *
710Sstevel@tonic-gate _strpbrk_escape(char *string, char *brkset)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	const char *p;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	do {
760Sstevel@tonic-gate 		for (p = brkset; *p != '\0' && *p != *string; ++p)
770Sstevel@tonic-gate 			;
780Sstevel@tonic-gate 		if (p == string)
790Sstevel@tonic-gate 			return ((char *)string);
800Sstevel@tonic-gate 		if (*p != '\0') {
810Sstevel@tonic-gate 			if (*(string-1) != '\\')
820Sstevel@tonic-gate 				return ((char *)string);
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 	} while (*string++);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	return (NULL);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
900Sstevel@tonic-gate char   *
910Sstevel@tonic-gate _escape(char *s, char *esc)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	int	nescs = 0;	/* number of escapes to place in s */
940Sstevel@tonic-gate 	int	i, j;
950Sstevel@tonic-gate 	int	len_s;
960Sstevel@tonic-gate 	char	*tmp;
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (s == NULL || esc == NULL)
990Sstevel@tonic-gate 		return (NULL);
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	len_s = strlen(s);
1020Sstevel@tonic-gate 	for (i = 0; i < len_s; i++)
1030Sstevel@tonic-gate 		if (strchr(esc, s[i]))
1040Sstevel@tonic-gate 			nescs++;
105*132Srobinson 	if ((tmp = malloc(nescs + len_s + 1)) == NULL)
1060Sstevel@tonic-gate 		return (NULL);
1070Sstevel@tonic-gate 	for (i = 0, j = 0; i < len_s; i++) {
1080Sstevel@tonic-gate 		if (strchr(esc, s[i])) {
1090Sstevel@tonic-gate 			tmp[j++] = '\\';
1100Sstevel@tonic-gate 		}
1110Sstevel@tonic-gate 		tmp[j++] = s[i];
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	tmp[len_s + nescs] = '\0';
1140Sstevel@tonic-gate 	return (tmp);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate char *
1190Sstevel@tonic-gate _unescape(char *s, char *esc)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	int	len_s;
1220Sstevel@tonic-gate 	int	i, j;
1230Sstevel@tonic-gate 	char	*tmp;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (s == NULL || esc == NULL)
126*132Srobinson 		return (NULL);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	len_s = strlen(s);
129*132Srobinson 	if ((tmp = malloc(len_s + 1)) == NULL)
130*132Srobinson 		return (NULL);
1310Sstevel@tonic-gate 	for (i = 0, j = 0; i < len_s; i++) {
1320Sstevel@tonic-gate 		if (s[i] == '\\' && strchr(esc, s[i + 1]))
1330Sstevel@tonic-gate 			tmp[j++] = s[++i];
1340Sstevel@tonic-gate 		else
1350Sstevel@tonic-gate 			tmp[j++] = s[i];
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	tmp[j] = NULL;
1380Sstevel@tonic-gate 	return (tmp);
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate char *
1420Sstevel@tonic-gate _strdup_null(char *s)
1430Sstevel@tonic-gate {
1440Sstevel@tonic-gate 	return (strdup(s ? s : ""));
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate  * read a line into buffer from a mmap'ed file.
1500Sstevel@tonic-gate  * return length of line read.
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate int
1530Sstevel@tonic-gate _readbufline(char *mapbuf,	/* input mmap buffer */
1540Sstevel@tonic-gate     int mapsize,		/* input size */
1550Sstevel@tonic-gate     char *buffer,		/* output storage */
1560Sstevel@tonic-gate     int buflen,			/* output size */
1570Sstevel@tonic-gate     int *lastlen)		/* input read till here last time */
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	int	linelen;
1600Sstevel@tonic-gate 
161*132Srobinson 	for (;;) {
1620Sstevel@tonic-gate 		linelen = 0;
1630Sstevel@tonic-gate 		while (linelen < buflen - 1) {	/* "- 1" saves room for \n\0 */
1640Sstevel@tonic-gate 			if (*lastlen >= mapsize) {
1650Sstevel@tonic-gate 				if (linelen == 0 ||
1660Sstevel@tonic-gate 					buffer[linelen - 1] == '\\') {
1670Sstevel@tonic-gate 						return (-1);
1680Sstevel@tonic-gate 					} else {
1690Sstevel@tonic-gate 						buffer[linelen] = '\n';
1700Sstevel@tonic-gate 						buffer[linelen + 1] = '\0';
1710Sstevel@tonic-gate 						return (linelen);
1720Sstevel@tonic-gate 					}
1730Sstevel@tonic-gate 			}
1740Sstevel@tonic-gate 			switch (mapbuf[*lastlen]) {
1750Sstevel@tonic-gate 			case '\n':
1760Sstevel@tonic-gate 				(*lastlen)++;
1770Sstevel@tonic-gate 				if (linelen > 0 &&
1780Sstevel@tonic-gate 				    buffer[linelen - 1] == '\\') {
1790Sstevel@tonic-gate 					--linelen;	/* remove the '\\' */
1800Sstevel@tonic-gate 				} else {
1810Sstevel@tonic-gate 					buffer[linelen] = '\n';
1820Sstevel@tonic-gate 					buffer[linelen + 1] = '\0';
1830Sstevel@tonic-gate 					return (linelen);
1840Sstevel@tonic-gate 				}
1850Sstevel@tonic-gate 				break;
1860Sstevel@tonic-gate 			default:
1870Sstevel@tonic-gate 				buffer[linelen] = mapbuf[*lastlen];
1880Sstevel@tonic-gate 				(*lastlen)++;
1890Sstevel@tonic-gate 				linelen++;
1900Sstevel@tonic-gate 			}
1910Sstevel@tonic-gate 		}
1920Sstevel@tonic-gate 		/* Buffer overflow -- eat rest of line and loop again */
1930Sstevel@tonic-gate 		while (mapbuf[*lastlen] != '\n') {
1940Sstevel@tonic-gate 			if (mapbuf[*lastlen] == EOF) {
1950Sstevel@tonic-gate 				return (-1);
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 			(*lastlen)++;
1980Sstevel@tonic-gate 		};
1990Sstevel@tonic-gate 	}
200*132Srobinson 	/* NOTREACHED */
2010Sstevel@tonic-gate }
202