Hiding pointer in typedefs is evil. 'Buffer *' -> 'Buffer'
[dragonfly.git] / usr.bin / make / str.c
CommitLineData
984263bc
MD
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.
1de703da
MD
37 *
38 * @(#)str.c 5.8 (Berkeley) 6/1/90
27afa3b2 39 * $FreeBSD: src/usr.bin/make/str.c,v 1.12.2.2 2004/02/23 12:10:57 ru Exp $
6a3d9147 40 * $DragonFly: src/usr.bin/make/str.c,v 1.16 2005/01/06 13:18:58 okumoto Exp $
984263bc
MD
41 */
42
9863ce62
MO
43#include <ctype.h>
44#include <stdlib.h>
45#include <string.h>
46
6a3d9147 47#include "buf.h"
9863ce62
MO
48#include "globals.h"
49#include "str.h"
50#include "util.h"
51#include "var.h"
984263bc
MD
52
53static char **argv, *buffer;
54static int argmax, curlen;
55
56/*
57 * str_init --
58 * Initialize the strings package
59 *
60 */
61void
84de9e23 62str_init(void)
984263bc
MD
63{
64 char *p1;
fbfaa208 65
9a4c88c2 66 argv = emalloc(((argmax = 50) + 1) * sizeof(char *));
984263bc
MD
67 argv[0] = Var_Value(".MAKE", VAR_GLOBAL, &p1);
68}
69
984263bc
MD
70/*-
71 * str_concat --
59e1f91f 72 * concatenate the two strings, inserting a space or slash between them.
984263bc
MD
73 *
74 * returns --
75 * the resulting string in allocated space.
76 */
77char *
2eae1d85 78str_concat(const char *s1, const char *s2, int flags)
984263bc 79{
9a309bcc
MD
80 int len1, len2;
81 char *result;
984263bc
MD
82
83 /* get the length of both strings */
84 len1 = strlen(s1);
85 len2 = strlen(s2);
86
87 /* allocate length plus separator plus EOS */
9a4c88c2 88 result = emalloc(len1 + len2 + 2);
984263bc
MD
89
90 /* copy first string into place */
91 memcpy(result, s1, len1);
92
93 /* add separator character */
94 if (flags & STR_ADDSPACE) {
95 result[len1] = ' ';
96 ++len1;
97 } else if (flags & STR_ADDSLASH) {
98 result[len1] = '/';
99 ++len1;
100 }
101
102 /* copy second string plus EOS into place */
103 memcpy(result + len1, s2, len2 + 1);
59e1f91f 104
fbfaa208 105 return (result);
984263bc
MD
106}
107
108/*-
109 * brk_string --
110 * Fracture a string into an array of words (as delineated by tabs or
111 * spaces) taking quotation marks into account. Leading tabs/spaces
112 * are ignored.
113 *
114 * returns --
115 * Pointer to the array of pointers to the words. To make life easier,
116 * the first word is always the value of the .MAKE variable.
117 */
118char **
84de9e23 119brk_string(char *str, int *store_argc, Boolean expand)
984263bc 120{
9a309bcc
MD
121 int argc, ch;
122 char inquote, *p, *start, *t;
984263bc
MD
123 int len;
124
125 /* skip leading space chars. */
126 for (; *str == ' ' || *str == '\t'; ++str)
127 continue;
128
129 /* allocate room for a copy of the string */
130 if ((len = strlen(str) + 1) > curlen) {
131 if (buffer)
132 free(buffer);
133 buffer = emalloc(curlen = len);
134 }
135
136 /*
137 * copy the string; at the same time, parse backslashes,
138 * quotes and build the argument list.
139 */
140 argc = 1;
141 inquote = '\0';
142 for (p = str, start = t = buffer;; ++p) {
143 switch(ch = *p) {
144 case '"':
145 case '\'':
146 if (inquote) {
27afa3b2 147 if (ch != inquote)
984263bc 148 break;
27afa3b2 149 inquote = '\0';
984263bc 150 /* Don't miss "" or '' */
984263bc
MD
151 if (!start)
152 start = t;
27afa3b2 153 } else
9a4c88c2 154 inquote = (char)ch;
27afa3b2
MD
155 if (expand)
156 continue;
157 break;
984263bc
MD
158 case ' ':
159 case '\t':
160 case '\n':
161 if (inquote)
162 break;
163 if (!start)
164 continue;
165 /* FALLTHROUGH */
166 case '\0':
167 /*
168 * end of a token -- make sure there's enough argv
169 * space and save off a pointer.
170 */
171 if (!start)
172 goto done;
173
174 *t++ = '\0';
175 if (argc == argmax) {
176 argmax *= 2; /* ramp up fast */
9a4c88c2 177 argv = erealloc(argv,
984263bc
MD
178 (argmax + 1) * sizeof(char *));
179 }
180 argv[argc++] = start;
9a4c88c2 181 start = NULL;
984263bc
MD
182 if (ch == '\n' || ch == '\0')
183 goto done;
184 continue;
185 case '\\':
186 if (!expand) {
187 if (!start)
188 start = t;
189 *t++ = '\\';
190 ch = *++p;
191 break;
192 }
193
194 switch (ch = *++p) {
195 case '\0':
196 case '\n':
197 /* hmmm; fix it up as best we can */
198 ch = '\\';
199 --p;
200 break;
201 case 'b':
202 ch = '\b';
203 break;
204 case 'f':
205 ch = '\f';
206 break;
207 case 'n':
208 ch = '\n';
209 break;
210 case 'r':
211 ch = '\r';
212 break;
213 case 't':
214 ch = '\t';
215 break;
26354435
MO
216 default:
217 break;
984263bc
MD
218 }
219 break;
26354435
MO
220 default:
221 break;
984263bc
MD
222 }
223 if (!start)
224 start = t;
fbfaa208 225 *t++ = (char)ch;
984263bc 226 }
9a4c88c2 227done: argv[argc] = NULL;
984263bc 228 *store_argc = argc;
fbfaa208 229 return (argv);
984263bc
MD
230}
231
984263bc
MD
232/*
233 * Str_Match --
234 *
235 * See if a particular string matches a particular pattern.
236 *
237 * Results: Non-zero is returned if string matches pattern, 0 otherwise. The
238 * matching operation permits the following special characters in the
239 * pattern: *?\[] (see the man page for details on what these mean).
240 *
241 * Side effects: None.
242 */
243int
4faecc47 244Str_Match(const char *string, const char *pattern)
984263bc
MD
245{
246 char c2;
247
248 for (;;) {
249 /*
250 * See if we're at the end of both the pattern and the
251 * string. If, we succeeded. If we're at the end of the
252 * pattern but not at the end of the string, we failed.
253 */
254 if (*pattern == 0)
fbfaa208 255 return (!*string);
984263bc 256 if (*string == 0 && *pattern != '*')
fbfaa208 257 return (0);
984263bc
MD
258 /*
259 * Check for a "*" as the next pattern character. It matches
260 * any substring. We handle this by calling ourselves
261 * recursively for each postfix of string, until either we
262 * match or we reach the end of the string.
263 */
264 if (*pattern == '*') {
265 pattern += 1;
266 if (*pattern == 0)
fbfaa208 267 return (1);
984263bc
MD
268 while (*string != 0) {
269 if (Str_Match(string, pattern))
fbfaa208 270 return (1);
984263bc
MD
271 ++string;
272 }
fbfaa208 273 return (0);
984263bc
MD
274 }
275 /*
276 * Check for a "?" as the next pattern character. It matches
277 * any single character.
278 */
279 if (*pattern == '?')
280 goto thisCharOK;
281 /*
282 * Check for a "[" as the next pattern character. It is
283 * followed by a list of characters that are acceptable, or
284 * by a range (two characters separated by "-").
285 */
286 if (*pattern == '[') {
287 ++pattern;
288 for (;;) {
289 if ((*pattern == ']') || (*pattern == 0))
fbfaa208 290 return (0);
984263bc
MD
291 if (*pattern == *string)
292 break;
293 if (pattern[1] == '-') {
294 c2 = pattern[2];
295 if (c2 == 0)
fbfaa208 296 return (0);
984263bc
MD
297 if ((*pattern <= *string) &&
298 (c2 >= *string))
299 break;
300 if ((*pattern >= *string) &&
301 (c2 <= *string))
302 break;
303 pattern += 2;
304 }
305 ++pattern;
306 }
307 while ((*pattern != ']') && (*pattern != 0))
308 ++pattern;
309 goto thisCharOK;
310 }
311 /*
312 * If the next pattern character is '/', just strip off the
313 * '/' so we do exact matching on the character that follows.
314 */
315 if (*pattern == '\\') {
316 ++pattern;
317 if (*pattern == 0)
fbfaa208 318 return (0);
984263bc
MD
319 }
320 /*
321 * There's no special character. Just make sure that the
322 * next characters of each string match.
323 */
324 if (*pattern != *string)
fbfaa208 325 return (0);
984263bc
MD
326thisCharOK: ++pattern;
327 ++string;
328 }
329}
330
331
332/*-
333 *-----------------------------------------------------------------------
334 * Str_SYSVMatch --
335 * Check word against pattern for a match (% is wild),
336 *
337 * Results:
338 * Returns the beginning position of a match or null. The number
339 * of characters matched is returned in len.
340 *
341 * Side Effects:
342 * None
343 *
344 *-----------------------------------------------------------------------
345 */
4faecc47
MD
346const char *
347Str_SYSVMatch(const char *word, const char *pattern, int *len)
984263bc 348{
4faecc47
MD
349 const char *p = pattern;
350 const char *w = word;
351 const char *m;
984263bc
MD
352
353 if (*w == '\0') {
354 /* Zero-length word cannot be matched against */
355 *len = 0;
fbfaa208 356 return (NULL);
984263bc
MD
357 }
358
359 if (*p == '\0') {
360 /* Null pattern is the whole string */
361 *len = strlen(w);
fbfaa208 362 return (w);
984263bc
MD
363 }
364
365 if ((m = strchr(p, '%')) != NULL) {
366 /* check that the prefix matches */
367 for (; p != m && *w && *w == *p; w++, p++)
368 continue;
369
370 if (p != m)
fbfaa208 371 return (NULL); /* No match */
984263bc
MD
372
373 if (*++p == '\0') {
374 /* No more pattern, return the rest of the string */
375 *len = strlen(w);
fbfaa208 376 return (w);
984263bc
MD
377 }
378 }
379
380 m = w;
381
382 /* Find a matching tail */
383 do
384 if (strcmp(p, w) == 0) {
385 *len = w - m;
fbfaa208 386 return (m);
984263bc
MD
387 }
388 while (*w++ != '\0');
389
fbfaa208 390 return (NULL);
984263bc
MD
391}
392
393
394/*-
395 *-----------------------------------------------------------------------
396 * Str_SYSVSubst --
397 * Substitute '%' on the pattern with len characters from src.
398 * If the pattern does not contain a '%' prepend len characters
399 * from src.
400 *
401 * Results:
402 * None
403 *
404 * Side Effects:
405 * Places result on buf
406 *
407 *-----------------------------------------------------------------------
408 */
409void
6a3d9147 410Str_SYSVSubst(Buffer *buf, const char *pat, const char *src, int len)
984263bc 411{
26354435 412 const char *m;
984263bc
MD
413
414 if ((m = strchr(pat, '%')) != NULL) {
415 /* Copy the prefix */
3bef815b 416 Buf_AddBytes(buf, m - pat, (const Byte *)pat);
984263bc
MD
417 /* skip the % */
418 pat = m + 1;
419 }
420
421 /* Copy the pattern */
3bef815b 422 Buf_AddBytes(buf, len, (const Byte *)src);
984263bc
MD
423
424 /* append the rest */
3bef815b 425 Buf_AddBytes(buf, strlen(pat), (const Byte *)pat);
984263bc 426}