]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/make/str.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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  */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include "buf.h"
49 #include "globals.h"
50 #include "str.h"
51 #include "util.h"
52
53 /**
54  * Initialize the argument array object.  The array is initially
55  * eight positions, and will be expaned as neccessary.  The first
56  * position is set to NULL since everything ignores it.  We allocate
57  * (size + 1) since we need space for the terminating NULL.  The
58  * buffer is set to NULL, since no common buffer is alloated yet.
59  */
60 void
61 ArgArray_Init(ArgArray *aa)
62 {
63
64         aa->size = 8;
65         aa->argv = emalloc((aa->size + 1) * sizeof(char *));
66         aa->argc = 0;
67         aa->argv[aa->argc++] = NULL;
68         aa->len = 0;
69         aa->buffer = NULL;
70 }
71
72 /**
73  * Cleanup the memory allocated for in the argument array object. 
74  */
75 void
76 ArgArray_Done(ArgArray *aa)
77 {
78
79         if (aa->buffer == NULL) {
80                 int     i;
81                 /* args are individually allocated */
82                 for (i = 0; i < aa->argc; ++i) {
83                         if (aa->argv[i]) {
84                                 free(aa->argv[i]);
85                                 aa->argv[i] = NULL;
86                         }
87                 }
88         } else {
89                 /* args are part of a single allocation */
90                 free(aa->buffer);
91                 aa->buffer = NULL;
92         }
93         free(aa->argv);
94         aa->argv = NULL;
95         aa->argc = 0;
96         aa->size = 0;
97 }
98
99 /*-
100  * str_concat --
101  *      concatenate the two strings, inserting a space or slash between them.
102  *
103  * returns --
104  *      the resulting string in allocated space.
105  */
106 char *
107 str_concat(const char *s1, const char *s2, int flags)
108 {
109         int len1, len2;
110         char *result;
111
112         /* get the length of both strings */
113         len1 = strlen(s1);
114         len2 = strlen(s2);
115
116         /* allocate length plus separator plus EOS */
117         result = emalloc(len1 + len2 + 2);
118
119         /* copy first string into place */
120         memcpy(result, s1, len1);
121
122         /* add separator character */
123         if (flags & STR_ADDSPACE) {
124                 result[len1] = ' ';
125                 ++len1;
126         } else if (flags & STR_ADDSLASH) {
127                 result[len1] = '/';
128                 ++len1;
129         }
130
131         /* copy second string plus EOS into place */
132         memcpy(result + len1, s2, len2 + 1);
133
134         return (result);
135 }
136
137 /**
138  * Fracture a string into an array of words (as delineated by tabs or
139  * spaces) taking quotation marks into account.  Leading tabs/spaces
140  * are ignored.
141  */
142 void
143 brk_string(ArgArray *aa, const char str[], Boolean expand)
144 {
145         char    inquote;
146         char    *start;
147         char    *arg;
148
149         /* skip leading space chars. */
150         for (; *str == ' ' || *str == '\t'; ++str)
151                 continue;
152
153         ArgArray_Init(aa);
154
155         aa->buffer = estrdup(str);;
156
157         arg = aa->buffer;
158         start = arg;
159         inquote = '\0';
160
161         /*
162          * copy the string; at the same time, parse backslashes,
163          * quotes and build the argument list.
164          */
165         for (;;) {
166                 switch (str[0]) {
167                 case '"':
168                 case '\'':
169                         if (inquote == '\0') {
170                                 inquote = str[0];
171                                 if (expand)
172                                         break;
173                                 if (start == NULL)
174                                         start = arg;
175                         } else if (inquote == str[0]) {
176                                 inquote = '\0';
177                                 /* Don't miss "" or '' */
178                                 if (start == NULL)
179                                         start = arg;
180                                 if (expand)
181                                         break;
182                         } else {
183                                 /* other type of quote found */
184                                 if (start == NULL)
185                                         start = arg;
186                         }
187                         *arg++ = str[0];
188                         break;
189                 case ' ':
190                 case '\t':
191                 case '\n':
192                         if (inquote) {
193                                 if (start == NULL)
194                                         start = arg;
195                                 *arg++ = str[0];
196                                 break;
197                         }
198                         if (start == NULL)
199                                 break;
200                         /* FALLTHROUGH */
201                 case '\0':
202                         /*
203                          * end of a token -- make sure there's enough argv
204                          * space and save off a pointer.
205                          */
206                         if (aa->argc == aa->size) {
207                                 aa->size *= 2;          /* ramp up fast */
208                                 aa->argv = erealloc(aa->argv,
209                                     (aa->size + 1) * sizeof(char *));
210                         }
211
212                         *arg++ = '\0';
213                         if (start == NULL) {
214                                 aa->argv[aa->argc] = start;
215                                 return;
216                         }
217                         if (str[0] == '\n' || str[0] == '\0') {
218                                 aa->argv[aa->argc++] = start;
219                                 aa->argv[aa->argc] = NULL;
220                                 return;
221                         } else {
222                                 aa->argv[aa->argc++] = start;
223                                 start = NULL;
224                                 break;
225                         }
226                 case '\\':
227                         if (start == NULL)
228                                 start = arg;
229                         if (expand) {
230                                 switch (str[1]) {
231                                 case '\0':
232                                 case '\n':
233                                         /* hmmm; fix it up as best we can */
234                                         *arg++ = '\\';
235                                         break;
236                                 case 'b':
237                                         *arg++ = '\b';
238                                         ++str;
239                                         break;
240                                 case 'f':
241                                         *arg++ = '\f';
242                                         ++str;
243                                         break;
244                                 case 'n':
245                                         *arg++ = '\n';
246                                         ++str;
247                                         break;
248                                 case 'r':
249                                         *arg++ = '\r';
250                                         ++str;
251                                         break;
252                                 case 't':
253                                         *arg++ = '\t';
254                                         ++str;
255                                         break;
256                                 default:
257                                         *arg++ = str[1];
258                                         ++str;
259                                         break;
260                                 }
261                         } else {
262                                 *arg++ = str[0];
263                                 if (str[1] != '\0') {
264                                         ++str;
265                                         *arg++ = str[0];
266                                 }
267                         }
268                         break;
269                 default:
270                         if (start == NULL)
271                                 start = arg;
272                         *arg++ = str[0];
273                         break;
274                 }
275                 ++str;
276         }
277 }
278
279 /*
280  * Quote a string for appending it to MAKEFLAGS. According to Posix the
281  * kind of quoting here is implementation-defined. This quoting must ensure
282  * that the parsing of MAKEFLAGS's contents in a sub-shell yields the same
283  * options, option arguments and macro definitions as in the calling make.
284  * We simply quote all blanks, which according to Posix are space and tab
285  * in the POSIX locale. Don't use isblank because in that case makes with
286  * different locale settings could not communicate. We must also quote
287  * backslashes obviously.
288  */
289 char *
290 MAKEFLAGS_quote(const char *str)
291 {
292         char *ret, *q;
293         const char *p;
294
295         /* assume worst case - everything has to be quoted */
296         ret = emalloc(strlen(str) * 2 + 1);
297
298         p = str;
299         q = ret;
300         while (*p != '\0') {
301                 switch (*p) {
302
303                   case ' ':
304                   case '\t':
305                         *q++ = '\\';
306                         break;
307
308                   default:
309                         break;
310                 }
311                 *q++ = *p++;
312         }
313         *q++ = '\0';
314         return (ret);
315 }
316
317 void
318 MAKEFLAGS_break(ArgArray *aa, const char str[])
319 {
320         char    *arg;
321         char    *start;
322
323         ArgArray_Init(aa);
324
325         aa->buffer = strdup(str);
326
327         arg = aa->buffer;
328         start = NULL;
329
330         for (;;) {
331                 switch (str[0]) {
332                 case ' ':
333                 case '\t':
334                         /* word separator */
335                         if (start == NULL) {
336                                 /* not in a word */
337                                 str++;
338                                 continue;
339                         }
340                         /* FALLTHRU */
341                 case '\0':
342                         if (aa->argc == aa->size) {
343                                 aa->size *= 2;
344                                 aa->argv = erealloc(aa->argv,
345                                     (aa->size + 1) * sizeof(char *));
346                         }
347
348                         *arg++ = '\0';
349                         if (start == NULL) {
350                                 aa->argv[aa->argc] = start;
351                                 return;
352                         }
353                         if (str[0] == '\0') {
354                                 aa->argv[aa->argc++] = start;
355                                 aa->argv[aa->argc] = NULL;
356                                 return;
357                         } else {
358                                 aa->argv[aa->argc++] = start;
359                                 start = NULL;
360                                 str++;
361                                 continue;
362                         }
363
364                 case '\\':
365                         if (str[1] == ' ' || str[1] == '\t')
366                                 str++;
367                         break;
368
369                 default:
370                         break;
371                 }
372                 if (start == NULL)
373                         start = arg;
374                 *arg++ = *str++;
375         }
376 }
377
378 /*
379  * Str_Match --
380  *
381  * See if a particular string matches a particular pattern.
382  *
383  * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
384  * matching operation permits the following special characters in the
385  * pattern: *?\[] (see the man page for details on what these mean).
386  *
387  * Side effects: None.
388  */
389 int
390 Str_Match(const char *string, const char *pattern)
391 {
392         char c2;
393
394         for (;;) {
395                 /*
396                  * See if we're at the end of both the pattern and the
397                  * string. If, we succeeded.  If we're at the end of the
398                  * pattern but not at the end of the string, we failed.
399                  */
400                 if (*pattern == 0)
401                         return (!*string);
402                 if (*string == 0 && *pattern != '*')
403                         return (0);
404                 /*
405                  * Check for a "*" as the next pattern character.  It matches
406                  * any substring.  We handle this by calling ourselves
407                  * recursively for each postfix of string, until either we
408                  * match or we reach the end of the string.
409                  */
410                 if (*pattern == '*') {
411                         pattern += 1;
412                         if (*pattern == 0)
413                                 return (1);
414                         while (*string != 0) {
415                                 if (Str_Match(string, pattern))
416                                         return (1);
417                                 ++string;
418                         }
419                         return (0);
420                 }
421                 /*
422                  * Check for a "?" as the next pattern character.  It matches
423                  * any single character.
424                  */
425                 if (*pattern == '?')
426                         goto thisCharOK;
427                 /*
428                  * Check for a "[" as the next pattern character.  It is
429                  * followed by a list of characters that are acceptable, or
430                  * by a range (two characters separated by "-").
431                  */
432                 if (*pattern == '[') {
433                         ++pattern;
434                         for (;;) {
435                                 if ((*pattern == ']') || (*pattern == 0))
436                                         return (0);
437                                 if (*pattern == *string)
438                                         break;
439                                 if (pattern[1] == '-') {
440                                         c2 = pattern[2];
441                                         if (c2 == 0)
442                                                 return (0);
443                                         if ((*pattern <= *string) &&
444                                             (c2 >= *string))
445                                                 break;
446                                         if ((*pattern >= *string) &&
447                                             (c2 <= *string))
448                                                 break;
449                                         pattern += 2;
450                                 }
451                                 ++pattern;
452                         }
453                         while ((*pattern != ']') && (*pattern != 0))
454                                 ++pattern;
455                         goto thisCharOK;
456                 }
457                 /*
458                  * If the next pattern character is '/', just strip off the
459                  * '/' so we do exact matching on the character that follows.
460                  */
461                 if (*pattern == '\\') {
462                         ++pattern;
463                         if (*pattern == 0)
464                                 return (0);
465                 }
466                 /*
467                  * There's no special character.  Just make sure that the
468                  * next characters of each string match.
469                  */
470                 if (*pattern != *string)
471                         return (0);
472 thisCharOK:     ++pattern;
473                 ++string;
474         }
475 }
476
477
478 /**
479  * Str_SYSVMatch
480  *      Check word against pattern for a match (% is wild),
481  *
482  * Results:
483  *      Returns the beginning position of a match or null. The number
484  *      of characters matched is returned in len.
485  */
486 const char *
487 Str_SYSVMatch(const char *word, const char *pattern, int *len)
488 {
489         const char *m, *p, *w;
490
491         p = pattern;
492         w = word;
493
494         if (*w == '\0') {
495                 /* Zero-length word cannot be matched against */
496                 *len = 0;
497                 return (NULL);
498         }
499
500         if (*p == '\0') {
501                 /* Null pattern is the whole string */
502                 *len = strlen(w);
503                 return (w);
504         }
505
506         if ((m = strchr(p, '%')) != NULL) {
507                 /* check that the prefix matches */
508                 for (; p != m && *w && *w == *p; w++, p++)
509                         continue;
510
511                 if (p != m)
512                         return (NULL);  /* No match */
513
514                 if (*++p == '\0') {
515                         /* No more pattern, return the rest of the string */
516                         *len = strlen(w);
517                         return (w);
518                 }
519         }
520
521         m = w;
522
523         /* Find a matching tail */
524         do
525                 if (strcmp(p, w) == 0) {
526                         *len = w - m;
527                         return (m);
528                 }
529         while (*w++ != '\0');
530
531         return (NULL);
532 }
533
534
535 /**
536  * Str_SYSVSubst
537  *      Substitute '%' on the pattern with len characters from src.
538  *      If the pattern does not contain a '%' prepend len characters
539  *      from src.
540  *
541  * Side Effects:
542  *      Places result on buf
543  */
544 void
545 Str_SYSVSubst(Buffer *buf, const char *pat, const char *src, int len)
546 {
547         const char *m;
548
549         if ((m = strchr(pat, '%')) != NULL) {
550                 /* Copy the prefix */
551                 Buf_AppendRange(buf, pat, m);
552                 /* skip the % */
553                 pat = m + 1;
554         }
555
556         /* Copy the pattern */
557         Buf_AddBytes(buf, len, (const Byte *)src);
558
559         /* append the rest */
560         Buf_Append(buf, pat);
561 }