1 /*	$NetBSD: nonints.h,v 1.213 2021/04/11 13:35:56 rillig Exp $	*/
2 
3 /*
4  * Copyright (c) 1988, 1989, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Adam de Boor.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)nonints.h	8.3 (Berkeley) 3/19/94
35  */
36 
37 /*
38  * Copyright (c) 1989 by Berkeley Softworks
39  * All rights reserved.
40  *
41  * This code is derived from software contributed to Berkeley by
42  * Adam de Boor.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *	This product includes software developed by the University of
55  *	California, Berkeley and its contributors.
56  * 4. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *	from: @(#)nonints.h	8.3 (Berkeley) 3/19/94
73  */
74 
75 /* arch.c */
76 void Arch_Init(void);
77 void Arch_End(void);
78 
79 bool Arch_ParseArchive(char **, GNodeList *, GNode *);
80 void Arch_Touch(GNode *);
81 void Arch_TouchLib(GNode *);
82 void Arch_UpdateMTime(GNode *gn);
83 void Arch_UpdateMemberMTime(GNode *gn);
84 void Arch_FindLib(GNode *, SearchPath *);
85 bool Arch_LibOODate(GNode *);
86 bool Arch_IsLib(GNode *);
87 
88 /* compat.c */
89 int Compat_RunCommand(const char *, GNode *, StringListNode *);
90 void Compat_Run(GNodeList *);
91 void Compat_Make(GNode *, GNode *);
92 
93 /* cond.c */
94 CondEvalResult Cond_EvalCondition(const char *, bool *);
95 CondEvalResult Cond_EvalLine(const char *);
96 void Cond_restore_depth(unsigned int);
97 unsigned int Cond_save_depth(void);
98 
99 /* dir.c; see also dir.h */
100 
101 MAKE_INLINE const char *
str_basename(const char * pathname)102 str_basename(const char *pathname)
103 {
104 	const char *lastSlash = strrchr(pathname, '/');
105 	return lastSlash != NULL ? lastSlash + 1 : pathname;
106 }
107 
108 MAKE_INLINE SearchPath *
SearchPath_New(void)109 SearchPath_New(void)
110 {
111 	SearchPath *path = bmake_malloc(sizeof *path);
112 	Lst_Init(&path->dirs);
113 	return path;
114 }
115 
116 void SearchPath_Free(SearchPath *);
117 
118 /* for.c */
119 int For_Eval(const char *);
120 bool For_Accum(const char *);
121 void For_Run(int);
122 
123 /* job.c */
124 #ifdef WAIT_T
125 void JobReapChild(pid_t, WAIT_T, bool);
126 #endif
127 
128 /* main.c */
129 bool GetBooleanExpr(const char *, bool);
130 void Main_ParseArgLine(const char *);
131 char *Cmd_Exec(const char *, const char **);
132 void Error(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2);
133 void Fatal(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2) MAKE_ATTR_DEAD;
134 void Punt(const char *, ...) MAKE_ATTR_PRINTFLIKE(1, 2) MAKE_ATTR_DEAD;
135 void DieHorribly(void) MAKE_ATTR_DEAD;
136 void Finish(int) MAKE_ATTR_DEAD;
137 int eunlink(const char *);
138 void execDie(const char *, const char *);
139 char *getTmpdir(void);
140 bool ParseBoolean(const char *, bool);
141 char *cached_realpath(const char *, char *);
142 
143 /* parse.c */
144 void Parse_Init(void);
145 void Parse_End(void);
146 
147 typedef enum VarAssignOp {
148 	VAR_NORMAL,		/* = */
149 	VAR_SUBST,		/* := */
150 	VAR_SHELL,		/* != or :sh= */
151 	VAR_APPEND,		/* += */
152 	VAR_DEFAULT		/* ?= */
153 } VarAssignOp;
154 
155 typedef struct VarAssign {
156 	char *varname;		/* unexpanded */
157 	VarAssignOp op;
158 	const char *value;	/* unexpanded */
159 } VarAssign;
160 
161 typedef char *(*ReadMoreProc)(void *, size_t *);
162 
163 void Parse_Error(ParseErrorLevel, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3);
164 bool Parse_IsVar(const char *, VarAssign *out_var);
165 void Parse_Var(VarAssign *, GNode *);
166 void Parse_AddIncludeDir(const char *);
167 void Parse_File(const char *, int);
168 void Parse_SetInput(const char *, int, int, ReadMoreProc, void *);
169 void Parse_MainName(GNodeList *);
170 int Parse_GetFatals(void);
171 
172 
173 #ifndef HAVE_STRLCPY
174 /* strlcpy.c */
175 size_t strlcpy(char *, const char *, size_t);
176 #endif
177 
178 /* suff.c */
179 void Suff_Init(void);
180 void Suff_End(void);
181 
182 void Suff_ClearSuffixes(void);
183 bool Suff_IsTransform(const char *);
184 GNode *Suff_AddTransform(const char *);
185 void Suff_EndTransform(GNode *);
186 void Suff_AddSuffix(const char *, GNode **);
187 SearchPath *Suff_GetPath(const char *);
188 void Suff_ExtendPaths(void);
189 void Suff_AddInclude(const char *);
190 void Suff_AddLib(const char *);
191 void Suff_FindDeps(GNode *);
192 SearchPath *Suff_FindPath(GNode *);
193 void Suff_SetNull(const char *);
194 void Suff_PrintAll(void);
195 
196 /* targ.c */
197 void Targ_Init(void);
198 void Targ_End(void);
199 
200 void Targ_Stats(void);
201 GNodeList *Targ_List(void);
202 GNode *GNode_New(const char *);
203 GNode *Targ_FindNode(const char *);
204 GNode *Targ_GetNode(const char *);
205 GNode *Targ_NewInternalNode(const char *);
206 GNode *Targ_GetEndNode(void);
207 void Targ_FindList(GNodeList *, StringList *);
208 bool Targ_Precious(const GNode *);
209 void Targ_SetMain(GNode *);
210 void Targ_PrintCmds(GNode *);
211 void Targ_PrintNode(GNode *, int);
212 void Targ_PrintNodes(GNodeList *, int);
213 const char *Targ_FmtTime(time_t);
214 void Targ_PrintType(int);
215 void Targ_PrintGraph(int);
216 void Targ_Propagate(void);
217 const char *GNodeMade_Name(GNodeMade);
218 
219 /* var.c */
220 void Var_Init(void);
221 void Var_End(void);
222 
223 typedef enum VarEvalMode {
224 
225 	/*
226 	 * Only parse the expression but don't evaluate any part of it.
227 	 *
228 	 * TODO: Document what Var_Parse and Var_Subst return in this mode.
229 	 *  As of 2021-03-15, they return unspecified, inconsistent results.
230 	 */
231 	VARE_PARSE_ONLY,
232 
233 	/* Parse and evaluate the expression. */
234 	VARE_WANTRES,
235 
236 	/*
237 	 * Parse and evaluate the expression.  It is an error if a
238 	 * subexpression evaluates to undefined.
239 	 */
240 	VARE_UNDEFERR,
241 
242 	/*
243 	 * Parse and evaluate the expression.  Keep '$$' as '$$' instead of
244 	 * reducing it to a single '$'.  Subexpressions that evaluate to
245 	 * undefined expand to an empty string.
246 	 *
247 	 * Used in variable assignments using the ':=' operator.  It allows
248 	 * multiple such assignments to be chained without accidentally
249 	 * expanding '$$file' to '$file' in the first assignment and
250 	 * interpreting it as '${f}' followed by 'ile' in the next assignment.
251 	 */
252 	VARE_EVAL_KEEP_DOLLAR,
253 
254 	/*
255 	 * Parse and evaluate the expression.  Keep undefined variables as-is
256 	 * instead of expanding them to an empty string.
257 	 *
258 	 * Example for a ':=' assignment:
259 	 *	CFLAGS = $(.INCLUDES)
260 	 *	CFLAGS := -I.. $(CFLAGS)
261 	 *	# If .INCLUDES (an undocumented special variable, by the
262 	 *	# way) is still undefined, the updated CFLAGS becomes
263 	 *	# "-I.. $(.INCLUDES)".
264 	 */
265 	VARE_EVAL_KEEP_UNDEF,
266 
267 	/*
268 	 * Parse and evaluate the expression.  Keep '$$' as '$$' and preserve
269 	 * undefined subexpressions.
270 	 */
271 	VARE_KEEP_DOLLAR_UNDEF
272 } VarEvalMode;
273 
274 typedef enum VarSetFlags {
275 	VAR_SET_NONE		= 0,
276 
277 	/* do not export */
278 	VAR_SET_NO_EXPORT	= 1 << 0,
279 
280 	/* Make the variable read-only. No further modification is possible,
281 	 * except for another call to Var_Set with the same flag. */
282 	VAR_SET_READONLY	= 1 << 1
283 } VarSetFlags;
284 
285 /* The state of error handling returned by Var_Parse. */
286 typedef enum VarParseResult {
287 
288 	/* Both parsing and evaluation succeeded. */
289 	VPR_OK,
290 
291 	/* Parsing or evaluating failed, with an error message. */
292 	VPR_ERR,
293 
294 	/*
295 	 * Parsing succeeded, undefined expressions are allowed and the
296 	 * expression was still undefined after applying all modifiers.
297 	 * No error message is printed in this case.
298 	 *
299 	 * Some callers handle this case differently, so return this
300 	 * information to them, for now.
301 	 *
302 	 * TODO: Instead of having this special return value, rather ensure
303 	 *  that VARE_EVAL_KEEP_UNDEF is processed properly.
304 	 */
305 	VPR_UNDEF
306 
307 } VarParseResult;
308 
309 typedef enum VarExportMode {
310 	/* .export-env */
311 	VEM_ENV,
312 	/* .export: Initial export or update an already exported variable. */
313 	VEM_PLAIN,
314 	/* .export-literal: Do not expand the variable value. */
315 	VEM_LITERAL
316 } VarExportMode;
317 
318 void Var_Delete(GNode *, const char *);
319 void Var_DeleteExpand(GNode *, const char *);
320 void Var_Undef(const char *);
321 void Var_Set(GNode *, const char *, const char *);
322 void Var_SetExpand(GNode *, const char *, const char *);
323 void Var_SetWithFlags(GNode *, const char *, const char *, VarSetFlags);
324 void Var_SetExpandWithFlags(GNode *, const char *, const char *, VarSetFlags);
325 void Var_Append(GNode *, const char *, const char *);
326 void Var_AppendExpand(GNode *, const char *, const char *);
327 bool Var_Exists(GNode *, const char *);
328 bool Var_ExistsExpand(GNode *, const char *);
329 FStr Var_Value(GNode *, const char *);
330 const char *GNode_ValueDirect(GNode *, const char *);
331 VarParseResult Var_Parse(const char **, GNode *, VarEvalMode, FStr *);
332 VarParseResult Var_Subst(const char *, GNode *, VarEvalMode, char **);
333 void Var_Stats(void);
334 void Var_Dump(GNode *);
335 void Var_ReexportVars(void);
336 void Var_Export(VarExportMode, const char *);
337 void Var_ExportVars(const char *);
338 void Var_UnExport(bool, const char *);
339 
340 void Global_Set(const char *, const char *);
341 void Global_SetExpand(const char *, const char *);
342 void Global_Append(const char *, const char *);
343 void Global_Delete(const char *);
344 
345 /* util.c */
346 typedef void (*SignalProc)(int);
347 SignalProc bmake_signal(int, SignalProc);
348