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 */ 22*61961e0fSrobinson 237c478bd9Sstevel@tonic-gate /* 24*61961e0fSrobinson * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 25*61961e0fSrobinson * Use is subject to license terms. 267c478bd9Sstevel@tonic-gate */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <stdio.h> 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <strings.h> 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate char *_strpbrk_escape(char *, char *); 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* 377c478bd9Sstevel@tonic-gate * _strtok_escape() 387c478bd9Sstevel@tonic-gate * Like strtok_r, except we don't break on a token if it is escaped 397c478bd9Sstevel@tonic-gate * with the escape character (\). 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate char * 427c478bd9Sstevel@tonic-gate _strtok_escape(char *string, char *sepset, char **lasts) 437c478bd9Sstevel@tonic-gate { 44*61961e0fSrobinson char *r; 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* first or subsequent call */ 477c478bd9Sstevel@tonic-gate if (string == NULL) 487c478bd9Sstevel@tonic-gate string = *lasts; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate if (string == 0) /* return if no tokens remaining */ 517c478bd9Sstevel@tonic-gate return (NULL); 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate if (*string == '\0') /* return if no tokens remaining */ 547c478bd9Sstevel@tonic-gate return (NULL); 557c478bd9Sstevel@tonic-gate 56*61961e0fSrobinson /* move past token */ 57*61961e0fSrobinson if ((r = _strpbrk_escape(string, sepset)) == NULL) 587c478bd9Sstevel@tonic-gate *lasts = 0; /* indicate this is last token */ 597c478bd9Sstevel@tonic-gate else { 607c478bd9Sstevel@tonic-gate *r = '\0'; 617c478bd9Sstevel@tonic-gate *lasts = r+1; 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate return (string); 647c478bd9Sstevel@tonic-gate } 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate /* 677c478bd9Sstevel@tonic-gate * Return ptr to first occurrence of any non-escaped character from `brkset' 687c478bd9Sstevel@tonic-gate * in the character string `string'; NULL if none exists. 697c478bd9Sstevel@tonic-gate */ 707c478bd9Sstevel@tonic-gate char * 717c478bd9Sstevel@tonic-gate _strpbrk_escape(char *string, char *brkset) 727c478bd9Sstevel@tonic-gate { 737c478bd9Sstevel@tonic-gate const char *p; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate do { 767c478bd9Sstevel@tonic-gate for (p = brkset; *p != '\0' && *p != *string; ++p) 777c478bd9Sstevel@tonic-gate ; 787c478bd9Sstevel@tonic-gate if (p == string) 797c478bd9Sstevel@tonic-gate return ((char *)string); 807c478bd9Sstevel@tonic-gate if (*p != '\0') { 817c478bd9Sstevel@tonic-gate if (*(string-1) != '\\') 827c478bd9Sstevel@tonic-gate return ((char *)string); 837c478bd9Sstevel@tonic-gate } 847c478bd9Sstevel@tonic-gate } while (*string++); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate return (NULL); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate char * 917c478bd9Sstevel@tonic-gate _escape(char *s, char *esc) 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate int nescs = 0; /* number of escapes to place in s */ 947c478bd9Sstevel@tonic-gate int i, j; 957c478bd9Sstevel@tonic-gate int len_s; 967c478bd9Sstevel@tonic-gate char *tmp; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate if (s == NULL || esc == NULL) 997c478bd9Sstevel@tonic-gate return (NULL); 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate len_s = strlen(s); 1027c478bd9Sstevel@tonic-gate for (i = 0; i < len_s; i++) 1037c478bd9Sstevel@tonic-gate if (strchr(esc, s[i])) 1047c478bd9Sstevel@tonic-gate nescs++; 105*61961e0fSrobinson if ((tmp = malloc(nescs + len_s + 1)) == NULL) 1067c478bd9Sstevel@tonic-gate return (NULL); 1077c478bd9Sstevel@tonic-gate for (i = 0, j = 0; i < len_s; i++) { 1087c478bd9Sstevel@tonic-gate if (strchr(esc, s[i])) { 1097c478bd9Sstevel@tonic-gate tmp[j++] = '\\'; 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate tmp[j++] = s[i]; 1127c478bd9Sstevel@tonic-gate } 1137c478bd9Sstevel@tonic-gate tmp[len_s + nescs] = '\0'; 1147c478bd9Sstevel@tonic-gate return (tmp); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate char * 1197c478bd9Sstevel@tonic-gate _unescape(char *s, char *esc) 1207c478bd9Sstevel@tonic-gate { 1217c478bd9Sstevel@tonic-gate int len_s; 1227c478bd9Sstevel@tonic-gate int i, j; 1237c478bd9Sstevel@tonic-gate char *tmp; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate if (s == NULL || esc == NULL) 126*61961e0fSrobinson return (NULL); 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate len_s = strlen(s); 129*61961e0fSrobinson if ((tmp = malloc(len_s + 1)) == NULL) 130*61961e0fSrobinson return (NULL); 1317c478bd9Sstevel@tonic-gate for (i = 0, j = 0; i < len_s; i++) { 1327c478bd9Sstevel@tonic-gate if (s[i] == '\\' && strchr(esc, s[i + 1])) 1337c478bd9Sstevel@tonic-gate tmp[j++] = s[++i]; 1347c478bd9Sstevel@tonic-gate else 1357c478bd9Sstevel@tonic-gate tmp[j++] = s[i]; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate tmp[j] = NULL; 1387c478bd9Sstevel@tonic-gate return (tmp); 1397c478bd9Sstevel@tonic-gate } 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate char * 1427c478bd9Sstevel@tonic-gate _strdup_null(char *s) 1437c478bd9Sstevel@tonic-gate { 1447c478bd9Sstevel@tonic-gate return (strdup(s ? s : "")); 1457c478bd9Sstevel@tonic-gate } 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* 1497c478bd9Sstevel@tonic-gate * read a line into buffer from a mmap'ed file. 1507c478bd9Sstevel@tonic-gate * return length of line read. 1517c478bd9Sstevel@tonic-gate */ 1527c478bd9Sstevel@tonic-gate int 1537c478bd9Sstevel@tonic-gate _readbufline(char *mapbuf, /* input mmap buffer */ 1547c478bd9Sstevel@tonic-gate int mapsize, /* input size */ 1557c478bd9Sstevel@tonic-gate char *buffer, /* output storage */ 1567c478bd9Sstevel@tonic-gate int buflen, /* output size */ 1577c478bd9Sstevel@tonic-gate int *lastlen) /* input read till here last time */ 1587c478bd9Sstevel@tonic-gate { 1597c478bd9Sstevel@tonic-gate int linelen; 1607c478bd9Sstevel@tonic-gate 161*61961e0fSrobinson for (;;) { 1627c478bd9Sstevel@tonic-gate linelen = 0; 1637c478bd9Sstevel@tonic-gate while (linelen < buflen - 1) { /* "- 1" saves room for \n\0 */ 1647c478bd9Sstevel@tonic-gate if (*lastlen >= mapsize) { 1657c478bd9Sstevel@tonic-gate if (linelen == 0 || 1667c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 1677c478bd9Sstevel@tonic-gate return (-1); 1687c478bd9Sstevel@tonic-gate } else { 1697c478bd9Sstevel@tonic-gate buffer[linelen] = '\n'; 1707c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 1717c478bd9Sstevel@tonic-gate return (linelen); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate switch (mapbuf[*lastlen]) { 1757c478bd9Sstevel@tonic-gate case '\n': 1767c478bd9Sstevel@tonic-gate (*lastlen)++; 1777c478bd9Sstevel@tonic-gate if (linelen > 0 && 1787c478bd9Sstevel@tonic-gate buffer[linelen - 1] == '\\') { 1797c478bd9Sstevel@tonic-gate --linelen; /* remove the '\\' */ 1807c478bd9Sstevel@tonic-gate } else { 1817c478bd9Sstevel@tonic-gate buffer[linelen] = '\n'; 1827c478bd9Sstevel@tonic-gate buffer[linelen + 1] = '\0'; 1837c478bd9Sstevel@tonic-gate return (linelen); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate break; 1867c478bd9Sstevel@tonic-gate default: 1877c478bd9Sstevel@tonic-gate buffer[linelen] = mapbuf[*lastlen]; 1887c478bd9Sstevel@tonic-gate (*lastlen)++; 1897c478bd9Sstevel@tonic-gate linelen++; 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate /* Buffer overflow -- eat rest of line and loop again */ 1937c478bd9Sstevel@tonic-gate while (mapbuf[*lastlen] != '\n') { 1947c478bd9Sstevel@tonic-gate if (mapbuf[*lastlen] == EOF) { 1957c478bd9Sstevel@tonic-gate return (-1); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate (*lastlen)++; 1987c478bd9Sstevel@tonic-gate }; 1997c478bd9Sstevel@tonic-gate } 200*61961e0fSrobinson /* NOTREACHED */ 2017c478bd9Sstevel@tonic-gate } 202