import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / parse.c
blob2e1c2c1e04475c86a996990339b75fe05cab4dfd
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
29 #include "mt.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <strings.h>
34 char *_strpbrk_escape(char *, char *);
37 * _strtok_escape()
38 * Like strtok_r, except we don't break on a token if it is escaped
39 * with the escape character (\).
41 char *
42 _strtok_escape(char *string, char *sepset, char **lasts)
44 char *r;
46 /* first or subsequent call */
47 if (string == NULL)
48 string = *lasts;
50 if (string == 0) /* return if no tokens remaining */
51 return (NULL);
53 if (*string == '\0') /* return if no tokens remaining */
54 return (NULL);
56 /* move past token */
57 if ((r = _strpbrk_escape(string, sepset)) == NULL)
58 *lasts = 0; /* indicate this is last token */
59 else {
60 *r = '\0';
61 *lasts = r+1;
63 return (string);
67 * Return ptr to first occurrence of any non-escaped character from `brkset'
68 * in the character string `string'; NULL if none exists.
70 char *
71 _strpbrk_escape(char *string, char *brkset)
73 const char *p;
75 do {
76 for (p = brkset; *p != '\0' && *p != *string; ++p)
78 if (p == string)
79 return ((char *)string);
80 if (*p != '\0') {
81 if (*(string-1) != '\\')
82 return ((char *)string);
84 } while (*string++);
86 return (NULL);
90 char *
91 _escape(char *s, char *esc)
93 int nescs = 0; /* number of escapes to place in s */
94 int i, j;
95 int len_s;
96 char *tmp;
98 if (s == NULL || esc == NULL)
99 return (NULL);
101 len_s = strlen(s);
102 for (i = 0; i < len_s; i++)
103 if (strchr(esc, s[i]))
104 nescs++;
105 if ((tmp = malloc(nescs + len_s + 1)) == NULL)
106 return (NULL);
107 for (i = 0, j = 0; i < len_s; i++) {
108 if (strchr(esc, s[i])) {
109 tmp[j++] = '\\';
111 tmp[j++] = s[i];
113 tmp[len_s + nescs] = '\0';
114 return (tmp);
118 char *
119 _unescape(char *s, char *esc)
121 int len_s;
122 int i, j;
123 char *tmp;
125 if (s == NULL || esc == NULL)
126 return (NULL);
128 len_s = strlen(s);
129 if ((tmp = malloc(len_s + 1)) == NULL)
130 return (NULL);
131 for (i = 0, j = 0; i < len_s; i++) {
132 if (s[i] == '\\' && strchr(esc, s[i + 1]))
133 tmp[j++] = s[++i];
134 else
135 tmp[j++] = s[i];
137 tmp[j] = '\0';
138 return (tmp);
141 char *
142 _strdup_null(char *s)
144 return (strdup(s ? s : ""));
149 * read a line into buffer from a mmap'ed file.
150 * return length of line read.
153 _readbufline(char *mapbuf, /* input mmap buffer */
154 int mapsize, /* input size */
155 char *buffer, /* output storage */
156 int buflen, /* output size */
157 int *lastlen) /* input read till here last time */
159 int linelen;
161 for (;;) {
162 linelen = 0;
163 while (linelen < buflen - 1) { /* "- 1" saves room for \n\0 */
164 if (*lastlen >= mapsize) {
165 if (linelen == 0 ||
166 buffer[linelen - 1] == '\\') {
167 return (-1);
168 } else {
169 buffer[linelen] = '\n';
170 buffer[linelen + 1] = '\0';
171 return (linelen);
174 switch (mapbuf[*lastlen]) {
175 case '\n':
176 (*lastlen)++;
177 if (linelen > 0 &&
178 buffer[linelen - 1] == '\\') {
179 --linelen; /* remove the '\\' */
180 } else {
181 buffer[linelen] = '\n';
182 buffer[linelen + 1] = '\0';
183 return (linelen);
185 break;
186 default:
187 buffer[linelen] = mapbuf[*lastlen];
188 (*lastlen)++;
189 linelen++;
192 /* Buffer overflow -- eat rest of line and loop again */
193 while (mapbuf[*lastlen] != '\n') {
194 if (mapbuf[*lastlen] == EOF) {
195 return (-1);
197 (*lastlen)++;
200 /* NOTREACHED */