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