Remove cleanup functions. There is no reason to free memory
[dragonfly.git] / usr.bin / make / str.c
1 /*-
2  * Copyright (c) 1988, 1989, 1990, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1989 by Berkeley Softworks
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  * @(#)str.c    5.8 (Berkeley) 6/1/90
39  * $FreeBSD: src/usr.bin/make/str.c,v 1.12.2.2 2004/02/23 12:10:57 ru Exp $
40  * $DragonFly: src/usr.bin/make/str.c,v 1.14 2005/01/05 23:28:20 okumoto Exp $
41  */
42
43 #include "make.h"
44
45 static char **argv, *buffer;
46 static int argmax, curlen;
47
48 /*
49  * str_init --
50  *      Initialize the strings package
51  *
52  */
53 void
54 str_init(void)
55 {
56     char *p1;
57
58     argv = emalloc(((argmax = 50) + 1) * sizeof(char *));
59     argv[0] = Var_Value(".MAKE", VAR_GLOBAL, &p1);
60 }
61
62 /*-
63  * str_concat --
64  *      concatenate the two strings, inserting a space or slash between them.
65  *
66  * returns --
67  *      the resulting string in allocated space.
68  */
69 char *
70 str_concat(const char *s1, const char *s2, int flags)
71 {
72         int len1, len2;
73         char *result;
74
75         /* get the length of both strings */
76         len1 = strlen(s1);
77         len2 = strlen(s2);
78
79         /* allocate length plus separator plus EOS */
80         result = emalloc(len1 + len2 + 2);
81
82         /* copy first string into place */
83         memcpy(result, s1, len1);
84
85         /* add separator character */
86         if (flags & STR_ADDSPACE) {
87                 result[len1] = ' ';
88                 ++len1;
89         } else if (flags & STR_ADDSLASH) {
90                 result[len1] = '/';
91                 ++len1;
92         }
93
94         /* copy second string plus EOS into place */
95         memcpy(result + len1, s2, len2 + 1);
96
97         return (result);
98 }
99
100 /*-
101  * brk_string --
102  *      Fracture a string into an array of words (as delineated by tabs or
103  *      spaces) taking quotation marks into account.  Leading tabs/spaces
104  *      are ignored.
105  *
106  * returns --
107  *      Pointer to the array of pointers to the words.  To make life easier,
108  *      the first word is always the value of the .MAKE variable.
109  */
110 char **
111 brk_string(char *str, int *store_argc, Boolean expand)
112 {
113         int argc, ch;
114         char inquote, *p, *start, *t;
115         int len;
116
117         /* skip leading space chars. */
118         for (; *str == ' ' || *str == '\t'; ++str)
119                 continue;
120
121         /* allocate room for a copy of the string */
122         if ((len = strlen(str) + 1) > curlen) {
123                 if (buffer)
124                     free(buffer);
125                 buffer = emalloc(curlen = len);
126         }
127
128         /*
129          * copy the string; at the same time, parse backslashes,
130          * quotes and build the argument list.
131          */
132         argc = 1;
133         inquote = '\0';
134         for (p = str, start = t = buffer;; ++p) {
135                 switch(ch = *p) {
136                 case '"':
137                 case '\'':
138                         if (inquote) {
139                                 if (ch != inquote)
140                                         break;
141                                 inquote = '\0';
142                                 /* Don't miss "" or '' */
143                                 if (!start)
144                                         start = t;
145                         } else
146                                 inquote = (char)ch;
147                         if (expand)
148                                 continue;
149                         break;
150                 case ' ':
151                 case '\t':
152                 case '\n':
153                         if (inquote)
154                                 break;
155                         if (!start)
156                                 continue;
157                         /* FALLTHROUGH */
158                 case '\0':
159                         /*
160                          * end of a token -- make sure there's enough argv
161                          * space and save off a pointer.
162                          */
163                         if (!start)
164                             goto done;
165
166                         *t++ = '\0';
167                         if (argc == argmax) {
168                                 argmax *= 2;            /* ramp up fast */
169                                 argv = erealloc(argv,
170                                     (argmax + 1) * sizeof(char *));
171                         }
172                         argv[argc++] = start;
173                         start = NULL;
174                         if (ch == '\n' || ch == '\0')
175                                 goto done;
176                         continue;
177                 case '\\':
178                         if (!expand) {
179                                 if (!start)
180                                         start = t;
181                                 *t++ = '\\';
182                                 ch = *++p;
183                                 break;
184                         }
185
186                         switch (ch = *++p) {
187                         case '\0':
188                         case '\n':
189                                 /* hmmm; fix it up as best we can */
190                                 ch = '\\';
191                                 --p;
192                                 break;
193                         case 'b':
194                                 ch = '\b';
195                                 break;
196                         case 'f':
197                                 ch = '\f';
198                                 break;
199                         case 'n':
200                                 ch = '\n';
201                                 break;
202                         case 'r':
203                                 ch = '\r';
204                                 break;
205                         case 't':
206                                 ch = '\t';
207                                 break;
208                         default:
209                                 break;
210                         }
211                         break;
212                 default:
213                         break;
214                 }
215                 if (!start)
216                         start = t;
217                 *t++ = (char)ch;
218         }
219 done:   argv[argc] = NULL;
220         *store_argc = argc;
221         return (argv);
222 }
223
224 /*
225  * Str_Match --
226  *
227  * See if a particular string matches a particular pattern.
228  *
229  * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
230  * matching operation permits the following special characters in the
231  * pattern: *?\[] (see the man page for details on what these mean).
232  *
233  * Side effects: None.
234  */
235 int
236 Str_Match(const char *string, const char *pattern)
237 {
238         char c2;
239
240         for (;;) {
241                 /*
242                  * See if we're at the end of both the pattern and the
243                  * string. If, we succeeded.  If we're at the end of the
244                  * pattern but not at the end of the string, we failed.
245                  */
246                 if (*pattern == 0)
247                         return (!*string);
248                 if (*string == 0 && *pattern != '*')
249                         return (0);
250                 /*
251                  * Check for a "*" as the next pattern character.  It matches
252                  * any substring.  We handle this by calling ourselves
253                  * recursively for each postfix of string, until either we
254                  * match or we reach the end of the string.
255                  */
256                 if (*pattern == '*') {
257                         pattern += 1;
258                         if (*pattern == 0)
259                                 return (1);
260                         while (*string != 0) {
261                                 if (Str_Match(string, pattern))
262                                         return (1);
263                                 ++string;
264                         }
265                         return (0);
266                 }
267                 /*
268                  * Check for a "?" as the next pattern character.  It matches
269                  * any single character.
270                  */
271                 if (*pattern == '?')
272                         goto thisCharOK;
273                 /*
274                  * Check for a "[" as the next pattern character.  It is
275                  * followed by a list of characters that are acceptable, or
276                  * by a range (two characters separated by "-").
277                  */
278                 if (*pattern == '[') {
279                         ++pattern;
280                         for (;;) {
281                                 if ((*pattern == ']') || (*pattern == 0))
282                                         return (0);
283                                 if (*pattern == *string)
284                                         break;
285                                 if (pattern[1] == '-') {
286                                         c2 = pattern[2];
287                                         if (c2 == 0)
288                                                 return (0);
289                                         if ((*pattern <= *string) &&
290                                             (c2 >= *string))
291                                                 break;
292                                         if ((*pattern >= *string) &&
293                                             (c2 <= *string))
294                                                 break;
295                                         pattern += 2;
296                                 }
297                                 ++pattern;
298                         }
299                         while ((*pattern != ']') && (*pattern != 0))
300                                 ++pattern;
301                         goto thisCharOK;
302                 }
303                 /*
304                  * If the next pattern character is '/', just strip off the
305                  * '/' so we do exact matching on the character that follows.
306                  */
307                 if (*pattern == '\\') {
308                         ++pattern;
309                         if (*pattern == 0)
310                                 return (0);
311                 }
312                 /*
313                  * There's no special character.  Just make sure that the
314                  * next characters of each string match.
315                  */
316                 if (*pattern != *string)
317                         return (0);
318 thisCharOK:     ++pattern;
319                 ++string;
320         }
321 }
322
323
324 /*-
325  *-----------------------------------------------------------------------
326  * Str_SYSVMatch --
327  *      Check word against pattern for a match (% is wild),
328  *
329  * Results:
330  *      Returns the beginning position of a match or null. The number
331  *      of characters matched is returned in len.
332  *
333  * Side Effects:
334  *      None
335  *
336  *-----------------------------------------------------------------------
337  */
338 const char *
339 Str_SYSVMatch(const char *word, const char *pattern, int *len)
340 {
341     const char *p = pattern;
342     const char *w = word;
343     const char *m;
344
345     if (*w == '\0') {
346         /* Zero-length word cannot be matched against */
347         *len = 0;
348         return (NULL);
349     }
350
351     if (*p == '\0') {
352         /* Null pattern is the whole string */
353         *len = strlen(w);
354         return (w);
355     }
356
357     if ((m = strchr(p, '%')) != NULL) {
358         /* check that the prefix matches */
359         for (; p != m && *w && *w == *p; w++, p++)
360              continue;
361
362         if (p != m)
363             return (NULL);      /* No match */
364
365         if (*++p == '\0') {
366             /* No more pattern, return the rest of the string */
367             *len = strlen(w);
368             return (w);
369         }
370     }
371
372     m = w;
373
374     /* Find a matching tail */
375     do
376         if (strcmp(p, w) == 0) {
377             *len = w - m;
378             return (m);
379         }
380     while (*w++ != '\0');
381
382     return (NULL);
383 }
384
385
386 /*-
387  *-----------------------------------------------------------------------
388  * Str_SYSVSubst --
389  *      Substitute '%' on the pattern with len characters from src.
390  *      If the pattern does not contain a '%' prepend len characters
391  *      from src.
392  *
393  * Results:
394  *      None
395  *
396  * Side Effects:
397  *      Places result on buf
398  *
399  *-----------------------------------------------------------------------
400  */
401 void
402 Str_SYSVSubst(Buffer buf, const char *pat, const char *src, int len)
403 {
404     const char *m;
405
406     if ((m = strchr(pat, '%')) != NULL) {
407         /* Copy the prefix */
408         Buf_AddBytes(buf, m - pat, (const Byte *)pat);
409         /* skip the % */
410         pat = m + 1;
411     }
412
413     /* Copy the pattern */
414     Buf_AddBytes(buf, len, (const Byte *)src);
415
416     /* append the rest */
417     Buf_AddBytes(buf, strlen(pat), (const Byte *)pat);
418 }