xref: /freebsd/lib/libfigpar/string_m.c (revision c3c06f94f04847dceca2a5df80f7ef1ec00709ec)
1 /*-
2  * Copyright (c) 2001-2014 Devin Teske <dteske@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <ctype.h>
28 #include <errno.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <string.h>
32 
33 #include "string_m.h"
34 
35 /*
36  * Counts the number of occurrences of one string that appear in the source
37  * string. Return value is the total count.
38  *
39  * An example use would be if you need to know how large a block of memory
40  * needs to be for a replaceall() series.
41  */
42 unsigned int
strcount(const char * source,const char * find)43 strcount(const char *source, const char *find)
44 {
45 	const char *p = source;
46 	size_t flen;
47 	unsigned int n = 0;
48 
49 	/* Both parameters are required */
50 	if (source == NULL || find == NULL)
51 		return (0);
52 
53 	/* Cache the length of find element */
54 	flen = strlen(find);
55 	if (strlen(source) == 0 || flen == 0)
56 		return (0);
57 
58 	/* Loop until the end of the string */
59 	while (*p != '\0') {
60 		if (strncmp(p, find, flen) == 0) { /* found an instance */
61 			p += flen;
62 			n++;
63 		} else
64 			p++;
65 	}
66 
67 	return (n);
68 }
69 
70 /*
71  * Replaces all occurrences of `find' in `source' with `replace'.
72  *
73  * You should not pass a string constant as the first parameter, it needs to be
74  * a pointer to an allocated block of memory. The block of memory that source
75  * points to should be large enough to hold the result. If the length of the
76  * replacement string is greater than the length of the find string, the result
77  * will be larger than the original source string. To allocate enough space for
78  * the result, use the function strcount() declared above to determine the
79  * number of occurrences and how much larger the block size needs to be.
80  *
81  * If source is not large enough, the application will crash. The return value
82  * is the length (in bytes) of the result.
83  *
84  * When an error occurs, -1 is returned and the global variable errno is set
85  * accordingly. Returns zero on success.
86  */
87 int
replaceall(char * source,const char * find,const char * replace)88 replaceall(char *source, const char *find, const char *replace)
89 {
90 	char *p;
91 	char *t;
92 	char *temp;
93 	size_t flen;
94 	size_t rlen;
95 	size_t slen;
96 	uint32_t n = 0;
97 
98 	errno = 0; /* reset global error number */
99 
100 	/* Check that we have non-null parameters */
101 	if (source == NULL)
102 		return (0);
103 	if (find == NULL)
104 		return (strlen(source));
105 
106 	/* Cache the length of the strings */
107 	slen = strlen(source);
108 	flen = strlen(find);
109 	rlen = replace ? strlen(replace) : 0;
110 
111 	/* Cases where no replacements need to be made */
112 	if (slen == 0 || flen == 0 || slen < flen)
113 		return (slen);
114 
115 	/* If replace is longer than find, we'll need to create a temp copy */
116 	if (rlen > flen) {
117 		temp = malloc(slen + 1);
118 		if (temp == NULL) /* could not allocate memory */
119 			return (-1);
120 		memcpy(temp, source, slen + 1);
121 	} else
122 		temp = source;
123 
124 	/* Reconstruct the string with the replacements */
125 	p = source; t = temp; /* position elements */
126 
127 	while (*t != '\0') {
128 		if (strncmp(t, find, flen) == 0) {
129 			/* found an occurrence */
130 			for (n = 0; replace && replace[n]; n++)
131 				*p++ = replace[n];
132 			t += flen;
133 		} else
134 			*p++ = *t++; /* copy character and increment */
135 	}
136 
137 	/* Terminate the string */
138 	*p = '\0';
139 
140 	/* Free the temporary allocated memory */
141 	if (temp != source)
142 		free(temp);
143 
144 	/* Return the length of the completed string */
145 	return (strlen(source));
146 }
147 
148 /*
149  * Expands escape sequences in a buffer pointed to by `source'. This function
150  * steps through each character, and converts escape sequences such as "\n",
151  * "\r", "\t" and others into their respective meanings.
152  *
153  * You should not pass a string constant or literal to this function or the
154  * program will likely segmentation fault when it tries to modify the data.
155  *
156  * The string length will either shorten or stay the same depending on whether
157  * any escape sequences were converted but the amount of memory allocated does
158  * not change.
159  *
160  * Interpreted sequences are:
161  *
162  * 	\0NNN	character with octal value NNN (0 to 3 digits)
163  * 	\N	character with octal value N (0 thru 7)
164  * 	\a	alert (BEL)
165  * 	\b	backslash
166  * 	\f	form feed
167  * 	\n	new line
168  * 	\r	carriage return
169  * 	\t	horizontal tab
170  * 	\v	vertical tab
171  * 	\xNN	byte with hexadecimal value NN (1 to 2 digits)
172  *
173  * All other sequences are unescaped (ie. '\"' and '\#').
174  */
strexpand(char * source)175 void strexpand(char *source)
176 {
177 	uint8_t c;
178 	char *chr;
179 	char *pos;
180 	char d[4];
181 
182 	/* Initialize position elements */
183 	pos = chr = source;
184 
185 	/* Loop until we hit the end of the string */
186 	while (*pos != '\0') {
187 		if (*chr != '\\') {
188 			*pos = *chr; /* copy character to current offset */
189 			pos++;
190 			chr++;
191 			continue;
192 		}
193 
194 		/* Replace the backslash with the correct character */
195 		switch (*++chr) {
196 		case 'a': *pos = '\a'; break; /* bell/alert (BEL) */
197 		case 'b': *pos = '\b'; break; /* backspace */
198 		case 'f': *pos = '\f'; break; /* form feed */
199 		case 'n': *pos = '\n'; break; /* new line */
200 		case 'r': *pos = '\r'; break; /* carriage return */
201 		case 't': *pos = '\t'; break; /* horizontal tab */
202 		case 'v': *pos = '\v'; break; /* vertical tab */
203 		case 'x': /* hex value (1 to 2 digits)(\xNN) */
204 			d[2] = '\0'; /* pre-terminate the string */
205 
206 			/* verify next two characters are hex */
207 			d[0] = isxdigit(*(chr+1)) ? *++chr : '\0';
208 			if (d[0] != '\0')
209 				d[1] = isxdigit(*(chr+1)) ? *++chr : '\0';
210 
211 			/* convert the characters to decimal */
212 			c = (uint8_t)strtoul(d, 0, 16);
213 
214 			/* assign the converted value */
215 			*pos = (c != 0 || d[0] == '0') ? c : *++chr;
216 			break;
217 		case '0': /* octal value (0 to 3 digits)(\0NNN) */
218 			d[3] = '\0'; /* pre-terminate the string */
219 
220 			/* verify next three characters are octal */
221 			d[0] = (isdigit(*(chr+1)) && *(chr+1) < '8') ?
222 			    *++chr : '\0';
223 			if (d[0] != '\0')
224 				d[1] = (isdigit(*(chr+1)) && *(chr+1) < '8') ?
225 				    *++chr : '\0';
226 			if (d[1] != '\0')
227 				d[2] = (isdigit(*(chr+1)) && *(chr+1) < '8') ?
228 				    *++chr : '\0';
229 
230 			/* convert the characters to decimal */
231 			c = (uint8_t)strtoul(d, 0, 8);
232 
233 			/* assign the converted value */
234 			*pos = c;
235 			break;
236 		default: /* single octal (\0..7) or unknown sequence */
237 			if (isdigit(*chr) && *chr < '8') {
238 				d[0] = *chr;
239 				d[1] = '\0';
240 				*pos = (uint8_t)strtoul(d, 0, 8);
241 			} else
242 				*pos = *chr;
243 		}
244 
245 		/* Increment to next offset, possible next escape sequence */
246 		pos++;
247 		chr++;
248 	}
249 }
250 
251 /*
252  * Expand only the escaped newlines in a buffer pointed to by `source'. This
253  * function steps through each character, and converts the "\n" sequence into
254  * a literal newline and the "\\n" sequence into "\n".
255  *
256  * You should not pass a string constant or literal to this function or the
257  * program will likely segmentation fault when it tries to modify the data.
258  *
259  * The string length will either shorten or stay the same depending on whether
260  * any escaped newlines were converted but the amount of memory allocated does
261  * not change.
262  */
strexpandnl(char * source)263 void strexpandnl(char *source)
264 {
265 	uint8_t backslash = 0;
266 	char *cp1;
267 	char *cp2;
268 
269 	/* Replace '\n' with literal in dprompt */
270 	cp1 = cp2 = source;
271 	while (*cp2 != '\0') {
272 		*cp1 = *cp2;
273 		if (*cp2 == '\\')
274 			backslash++;
275 		else if (*cp2 != 'n')
276 			backslash = 0;
277 		else if (backslash > 0) {
278 			*(--cp1) = (backslash & 1) == 1 ? '\n' : 'n';
279 			backslash = 0;
280 		}
281 		cp1++;
282 		cp2++;
283 	}
284 	*cp1 = *cp2;
285 }
286 
287 /*
288  * Convert a string to lower case. You should not pass a string constant to
289  * this function. Only pass pointers to allocated memory with null terminated
290  * string data.
291  */
292 void
strtolower(char * source)293 strtolower(char *source)
294 {
295 	char *p = source;
296 
297 	if (source == NULL)
298 		return;
299 
300 	while (*p != '\0') {
301 		*p = tolower(*p);
302 		p++; /* would have just used `*p++' but gcc 3.x warns */
303 	}
304 }
305