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