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