xref: /openbsd-src/usr.bin/make/var.h (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 #ifndef VAR_H
2 #define VAR_H
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 extern void Var_Init(void);
29 #ifdef CLEANUP
30 extern void Var_End(void);
31 #else
32 #define Var_End()
33 #endif
34 
35 extern void Var_setCheckEnvFirst(bool);
36 
37 /* Global variable handling. */
38 /* value = Var_Valuei(name, end);
39  *	Returns value of global variable name/end, or NULL if inexistent. */
40 extern char *Var_Valuei(const char *, const char *);
41 #define Var_Value(n)	Var_Valuei(n, NULL)
42 
43 /* isDefined = Var_Definedi(name, end);
44  *	Checks whether global variable name/end is defined. */
45 extern bool Var_Definedi(const char *, const char *);
46 
47 /* Var_Seti_with_ctxt(name, end, val, ctxt);
48  *	Sets value val of variable name/end.  Copies val.
49  *	ctxt can be VAR_CMD (command line) or VAR_GLOBAL (normal variable). */
50 extern void Var_Seti_with_ctxt(const char *, const char *, const char *,
51 	int);
52 #define Var_Set(n, v)	Var_Seti_with_ctxt(n, NULL, v, VAR_GLOBAL)
53 #define Var_Seti(n, e, v) Var_Seti_with_ctxt(n, e, v, VAR_GLOBAL)
54 /* Var_Appendi_with_ctxt(name, end, val, cxt);
55  *	Appends value val to variable name/end in context ctxt, defining it
56  *	if it does not already exist, and inserting one space otherwise. */
57 extern void Var_Appendi_with_ctxt(const char *, const char *,
58 	const char *, int);
59 #define Var_Append(n, v)	Var_Appendi_with_ctxt(n, NULL, v, VAR_GLOBAL)
60 #define Var_Appendi(n, e, v) 	Var_Appendi_with_ctxt(n, e, v, VAR_GLOBAL)
61 
62 /* Var_Deletei(name, end);
63  *	Deletes a global variable. */
64 extern void Var_Deletei(const char *, const char *);
65 
66 /* Dynamic variable indices */
67 #define TARGET_INDEX	0
68 #define PREFIX_INDEX	1
69 #define ARCHIVE_INDEX	2
70 #define MEMBER_INDEX	3
71 #define OODATE_INDEX	4
72 #define ALLSRC_INDEX	5
73 #define IMPSRC_INDEX	6
74 
75 #define Var(idx, gn)	((gn)->context.locals[idx])
76 
77 
78 /* SymTable_Init(t);
79  *	Inits the local symtable in a GNode. */
80 extern void SymTable_Init(SymTable *);
81 /* SymTable_destroy(t);
82  *	Destroys the local symtable in a GNode. */
83 extern void SymTable_Destroy(SymTable *);
84 
85 /* Several ways to parse a variable specification. */
86 /* value = Var_Parse(varspec, ctxt, undef_is_bad, &length, &freeit);
87  *	Parses a variable specification varspec and evaluates it in context
88  *	ctxt.  Returns the resulting value, freeit indicates whether it's
89  *	a copy that should be freed when no longer needed.  If it's not a
90  *	copy, it's only valid until the next time variables are set.
91  *	The length of the spec is returned in length, e.g., varspec begins
92  *	at the $ and ends at the closing } or ).  Returns special value
93  *	var_Error if a problem occurred. */
94 extern char *Var_Parse(const char *, SymTable *, bool, size_t *,
95 	bool *);
96 /* Note that var_Error is an instance of the empty string "", so that
97  * callers who don't care don't need to. */
98 extern char	var_Error[];
99 
100 /* ok = Var_ParseSkip(&varspec, ctxt, &ok);
101  *	Parses a variable specification and returns true if the varspec
102  *	is correct. Advances pointer past specification.  */
103 extern bool Var_ParseSkip(const char **, SymTable *);
104 
105 /* ok = Var_ParseBuffer(buf, varspec, ctxt, undef_is_bad, &length);
106  *	Similar to Var_Parse, except the value is directly appended to
107  *	buffer buf. */
108 extern bool Var_ParseBuffer(Buffer, const char *, SymTable *,
109 	bool, size_t *);
110 
111 
112 /* The substitution itself */
113 /* subst = Var_Subst(str, ctxt, undef_is_bad);
114  *	Substitutes all variable values in string str under context ctxt.
115  *	Emit a PARSE_FATAL error if undef_is_bad and an undef variable is
116  *	encountered. The result is always a copy that should be free. */
117 extern char *Var_Subst(const char *, SymTable *, bool);
118 /* subst = Var_Substi(str, estr, ctxt, undef_if_bad);
119  */
120 extern char *Var_Substi(const char *, const char *, SymTable *, bool);
121 
122 
123 /* For loop handling.
124  *	// Create handle for variable name.
125  *	handle = Var_NewLoopVar(name, end);
126  *	// set up buffer
127  *	for (...)
128  *		// Substitute val for variable in str, and accumulate in buffer
129  *		Var_SubstVar(buffer, str, handle, val);
130  *	// Free handle
131  *	Var_DeleteLoopVar(handle);
132  */
133 struct LoopVar;	/* opaque handle */
134 struct LoopVar *Var_NewLoopVar(const char *, const char *);
135 void Var_DeleteLoopVar(struct LoopVar *);
136 extern void Var_SubstVar(Buffer, const char *, struct LoopVar *, const char *);
137 
138 
139 /* Var_Dump();
140  *	Print out all global variables. */
141 extern void Var_Dump(void);
142 
143 /* Var_AddCmdline(name);
144  *	Add all variable values from VAR_CMD to variable name.
145  *	Used to propagate variable values to submakes through MAKEFLAGS.  */
146 extern void Var_AddCmdline(const char *);
147 
148 /* stuff common to var.c and varparse.c */
149 extern bool	errorIsOkay;
150 
151 #define		VAR_GLOBAL	0
152 	/* Variables defined in a global context, e.g in the Makefile itself */
153 #define		VAR_CMD		1
154 	/* Variables defined on the command line */
155 
156 #define POISON_INVALID		0
157 #define POISON_DEFINED		1
158 #define POISON_NORMAL		64
159 #define POISON_EMPTY		128
160 #define POISON_NOT_DEFINED	256
161 
162 extern void Var_MarkPoisoned(const char *, const char *, unsigned int);
163 
164 #endif
165