xref: /openbsd-src/usr.bin/make/varname.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenPackages$ */
2 /*	$OpenBSD: varname.c,v 1.3 2007/07/09 12:29:45 espie Exp $	*/
3 /*
4  * Copyright (c) 2001 Marc Espie.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
19  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <stdlib.h>
29 #include "config.h"
30 #include "defines.h"
31 #include "var.h"
32 #include "buf.h"
33 #include "varname.h"
34 
35 const char *
36 VarName_Get(const char *start, struct Name *name, SymTable *ctxt, bool err, const char *(*cont)(const char *))
37 {
38 	const char *p;
39 	size_t len;
40 
41 	p = cont(start);
42 	/* If we don't want recursive variables, we skip over '$' */
43 	if (!FEATURES(FEATURE_RECVARS)) {
44 		while (*p == '$')
45 			p = cont(p);
46 	}
47 	if (*p != '$') {
48 		name->s = start;
49 		name->e = p;
50 		name->tofree = false;
51 		return p;
52 	} else {
53 		BUFFER buf;
54 		Buf_Init(&buf, MAKE_BSIZE);
55 		for (;;) {
56 			Buf_Addi(&buf, start, p);
57 			if (*p != '$') {
58 				name->s = (const char *)Buf_Retrieve(&buf);
59 				name->e = name->s + Buf_Size(&buf);
60 				name->tofree = true;
61 				return p;
62 			}
63 			start = p;
64 			Var_ParseBuffer(&buf, start, ctxt, err, &len);
65 			start += len;
66 			p = cont(start);
67 		}
68 	}
69 }
70 
71 void
72 VarName_Free(struct Name *name)
73 {
74 	if (name->tofree)
75 		free((char *)name->s);
76 }
77 
78