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