xref: /openbsd-src/usr.bin/make/var.h (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 #ifndef VAR_H
2 #define VAR_H
3 /* $OpenBSD: var.h,v 1.14 2010/07/19 19:46:44 espie Exp $ */
4 /*
5  * Copyright (c) 2001 Marc Espie.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OPENBSD
20  * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 extern void Var_Init(void);
30 #ifdef CLEANUP
31 extern void Var_End(void);
32 #else
33 #define Var_End()
34 #endif
35 
36 extern void Var_setCheckEnvFirst(bool);
37 
38 /* Global variable handling. */
39 /* value = Var_Valuei(name, end);
40  *	Returns value of global variable name/end, or NULL if inexistent. */
41 extern char *Var_Valuei(const char *, const char *);
42 #define Var_Value(n)	Var_Valuei(n, NULL)
43 
44 /* isDefined = Var_Definedi(name, end);
45  *	Checks whether global variable name/end is defined. */
46 extern bool Var_Definedi(const char *, const char *);
47 
48 /* Var_Seti_with_ctxt(name, end, val, ctxt);
49  *	Sets value val of variable name/end.  Copies val.
50  *	ctxt can be VAR_CMD (command line) or VAR_GLOBAL (normal variable). */
51 extern void Var_Seti_with_ctxt(const char *, const char *, const char *,
52 	int);
53 #define Var_Set(n, v)	Var_Seti_with_ctxt(n, NULL, v, VAR_GLOBAL)
54 #define Var_Seti(n, e, v) Var_Seti_with_ctxt(n, e, v, VAR_GLOBAL)
55 /* Var_Appendi_with_ctxt(name, end, val, cxt);
56  *	Appends value val to variable name/end in context ctxt, defining it
57  *	if it does not already exist, and inserting one space otherwise. */
58 extern void Var_Appendi_with_ctxt(const char *, const char *,
59 	const char *, int);
60 #define Var_Append(n, v)	Var_Appendi_with_ctxt(n, NULL, v, VAR_GLOBAL)
61 #define Var_Appendi(n, e, v) 	Var_Appendi_with_ctxt(n, e, v, VAR_GLOBAL)
62 
63 /* Var_Deletei(name, end);
64  *	Deletes a global variable. */
65 extern void Var_Deletei(const char *, const char *);
66 
67 /* Dynamic variable indices */
68 #define TARGET_INDEX	0
69 #define PREFIX_INDEX	1
70 #define ARCHIVE_INDEX	2
71 #define MEMBER_INDEX	3
72 #define OODATE_INDEX	4
73 #define ALLSRC_INDEX	5
74 #define IMPSRC_INDEX	6
75 
76 #define Var(idx, gn)	((gn)->context.locals[idx])
77 
78 
79 /* SymTable_Init(t);
80  *	Inits the local symtable in a GNode. */
81 extern void SymTable_Init(SymTable *);
82 /* SymTable_destroy(t);
83  *	Destroys the local symtable in a GNode. */
84 extern void SymTable_Destroy(SymTable *);
85 
86 /* Several ways to parse a variable specification. */
87 /* value = Var_Parse(varspec, ctxt, undef_is_bad, &length, &freeit);
88  *	Parses a variable specification varspec and evaluates it in context
89  *	ctxt.  Returns the resulting value, freeit indicates whether it's
90  *	a copy that should be freed when no longer needed.  If it's not a
91  *	copy, it's only valid until the next time variables are set.
92  *	The length of the spec is returned in length, e.g., varspec begins
93  *	at the $ and ends at the closing } or ).  Returns special value
94  *	var_Error if a problem occurred. */
95 extern char *Var_Parse(const char *, SymTable *, bool, size_t *,
96 	bool *);
97 /* Note that var_Error is an instance of the empty string "", so that
98  * callers who don't care don't need to. */
99 extern char	var_Error[];
100 
101 /* ok = Var_ParseSkip(&varspec, ctxt, &ok);
102  *	Parses a variable specification and returns true if the varspec
103  *	is correct. Advances pointer past specification.  */
104 extern bool Var_ParseSkip(const char **, SymTable *);
105 
106 /* ok = Var_ParseBuffer(buf, varspec, ctxt, undef_is_bad, &length);
107  *	Similar to Var_Parse, except the value is directly appended to
108  *	buffer buf. */
109 extern bool Var_ParseBuffer(Buffer, const char *, SymTable *,
110 	bool, size_t *);
111 
112 
113 /* The substitution itself */
114 /* subst = Var_Subst(str, ctxt, undef_is_bad);
115  *	Substitutes all variable values in string str under context ctxt.
116  *	Emit a PARSE_FATAL error if undef_is_bad and an undef variable is
117  *	encountered. The result is always a copy that should be free. */
118 extern char *Var_Subst(const char *, SymTable *, bool);
119 /* subst = Var_Substi(str, estr, ctxt, undef_if_bad);
120  */
121 extern char *Var_Substi(const char *, const char *, SymTable *, bool);
122 
123 
124 /* For loop handling.
125  *	// Create handle for variable name.
126  *	handle = Var_NewLoopVar(name, end);
127  *	// set up buffer
128  *	for (...)
129  *		// Substitute val for variable in str, and accumulate in buffer
130  *		Var_SubstVar(buffer, str, handle, val);
131  *	// Free handle
132  *	Var_DeleteLoopVar(handle);
133  */
134 struct LoopVar;	/* opaque handle */
135 struct LoopVar *Var_NewLoopVar(const char *, const char *);
136 void Var_DeleteLoopVar(struct LoopVar *);
137 extern void Var_SubstVar(Buffer, const char *, struct LoopVar *, const char *);
138 char *Var_LoopVarName(struct LoopVar *);
139 
140 
141 /* Var_Dump();
142  *	Print out all global variables. */
143 extern void Var_Dump(void);
144 
145 /* Var_AddCmdline(name);
146  *	Add all variable values from VAR_CMD to variable name.
147  *	Used to propagate variable values to submakes through MAKEFLAGS.  */
148 extern void Var_AddCmdline(const char *);
149 
150 /* stuff common to var.c and varparse.c */
151 extern bool	errorIsOkay;
152 
153 #define		VAR_GLOBAL	0
154 	/* Variables defined in a global context, e.g in the Makefile itself */
155 #define		VAR_CMD		1
156 	/* Variables defined on the command line */
157 
158 #define POISON_INVALID		0
159 #define POISON_DEFINED		1
160 #define POISON_NORMAL		64
161 #define POISON_EMPTY		128
162 #define POISON_NOT_DEFINED	256
163 
164 extern void Var_MarkPoisoned(const char *, const char *, unsigned int);
165 
166 #endif
167