1f7923656Sespie #ifndef VAR_H 2f7923656Sespie #define VAR_H 3*ce7279d8Sjsg /* $OpenBSD: var.h,v 1.21 2024/05/21 05:00:48 jsg Exp $ */ 4f7923656Sespie /* 5f7923656Sespie * Copyright (c) 2001 Marc Espie. 6f7923656Sespie * 7f7923656Sespie * Redistribution and use in source and binary forms, with or without 8f7923656Sespie * modification, are permitted provided that the following conditions 9f7923656Sespie * are met: 10f7923656Sespie * 1. Redistributions of source code must retain the above copyright 11f7923656Sespie * notice, this list of conditions and the following disclaimer. 12f7923656Sespie * 2. Redistributions in binary form must reproduce the above copyright 13f7923656Sespie * notice, this list of conditions and the following disclaimer in the 14f7923656Sespie * documentation and/or other materials provided with the distribution. 15f7923656Sespie * 16f7923656Sespie * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS 17f7923656Sespie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18f7923656Sespie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19f7923656Sespie * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD 20f7923656Sespie * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21f7923656Sespie * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22f7923656Sespie * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23f7923656Sespie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24f7923656Sespie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25f7923656Sespie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26f7923656Sespie * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27f7923656Sespie */ 28f7923656Sespie 290adf6829Sespie extern GNode *current_node; 30f7923656Sespie extern void Var_Init(void); 31d88ab755Sespie extern void Var_setCheckEnvFirst(bool); 32f7923656Sespie 3368636f61Sespie /* Global variable handling. */ 34f7923656Sespie /* value = Var_Valuei(name, end); 35f7923656Sespie * Returns value of global variable name/end, or NULL if inexistent. */ 36f7923656Sespie extern char *Var_Valuei(const char *, const char *); 37f7923656Sespie #define Var_Value(n) Var_Valuei(n, NULL) 3868636f61Sespie 3968636f61Sespie /* isDefined = Var_Definedi(name, end); 4068636f61Sespie * Checks whether global variable name/end is defined. */ 41d88ab755Sespie extern bool Var_Definedi(const char *, const char *); 42f7923656Sespie 435a5ca93eSespie /* Var_Seti_with_ctxt(name, end, val, ctxt); 4468636f61Sespie * Sets value val of variable name/end. Copies val. 4568636f61Sespie * ctxt can be VAR_CMD (command line) or VAR_GLOBAL (normal variable). */ 465a5ca93eSespie extern void Var_Seti_with_ctxt(const char *, const char *, const char *, 47d88ab755Sespie int); 485a5ca93eSespie #define Var_Set(n, v) Var_Seti_with_ctxt(n, NULL, v, VAR_GLOBAL) 495a5ca93eSespie #define Var_Seti(n, e, v) Var_Seti_with_ctxt(n, e, v, VAR_GLOBAL) 505a5ca93eSespie /* Var_Appendi_with_ctxt(name, end, val, cxt); 5191797de1Sespie * Appends value val to variable name/end in global context ctxt, 5291797de1Sespie * defining it if it does not already exist, and inserting one 5391797de1Sespie * space otherwise. */ 545a5ca93eSespie extern void Var_Appendi_with_ctxt(const char *, const char *, 55d88ab755Sespie const char *, int); 565a5ca93eSespie #define Var_Append(n, v) Var_Appendi_with_ctxt(n, NULL, v, VAR_GLOBAL) 575a5ca93eSespie #define Var_Appendi(n, e, v) Var_Appendi_with_ctxt(n, e, v, VAR_GLOBAL) 58f7923656Sespie 5968636f61Sespie /* Var_Deletei(name, end); 6068636f61Sespie * Deletes a global variable. */ 6168636f61Sespie extern void Var_Deletei(const char *, const char *); 62f7923656Sespie 6368636f61Sespie /* Dynamic variable indices */ 64f7923656Sespie #define TARGET_INDEX 0 65f7923656Sespie #define PREFIX_INDEX 1 66f7923656Sespie #define ARCHIVE_INDEX 2 67f7923656Sespie #define MEMBER_INDEX 3 68f6402ccaSnatano #define IMPSRC_INDEX 4 69f6402ccaSnatano #define OODATE_INDEX 5 70f6402ccaSnatano #define ALLSRC_INDEX 6 713d97cd1aSespie 7291797de1Sespie #define Var(idx, gn) ((gn)->localvars.locals[idx]) 733d97cd1aSespie 74f7923656Sespie 75f7923656Sespie /* SymTable_Init(t); 76f7923656Sespie * Inits the local symtable in a GNode. */ 77f7923656Sespie extern void SymTable_Init(SymTable *); 78f7923656Sespie 79f7923656Sespie /* Several ways to parse a variable specification. */ 80f7923656Sespie /* value = Var_Parse(varspec, ctxt, undef_is_bad, &length, &freeit); 81f7923656Sespie * Parses a variable specification varspec and evaluates it in context 82f7923656Sespie * ctxt. Returns the resulting value, freeit indicates whether it's 83f7923656Sespie * a copy that should be freed when no longer needed. If it's not a 84f7923656Sespie * copy, it's only valid until the next time variables are set. 85f7923656Sespie * The length of the spec is returned in length, e.g., varspec begins 86f7923656Sespie * at the $ and ends at the closing } or ). Returns special value 87f7923656Sespie * var_Error if a problem occurred. */ 88f7923656Sespie extern char *Var_Parse(const char *, SymTable *, bool, size_t *, 89f7923656Sespie bool *); 90f7923656Sespie /* Note that var_Error is an instance of the empty string "", so that 91f7923656Sespie * callers who don't care don't need to. */ 92f7923656Sespie extern char var_Error[]; 93f7923656Sespie 9434b15a7fSespie /* ok = Var_ParseSkip(&varspec, ctxt, &ok); 9534b15a7fSespie * Parses a variable specification and returns true if the varspec 9634b15a7fSespie * is correct. Advances pointer past specification. */ 9734b15a7fSespie extern bool Var_ParseSkip(const char **, SymTable *); 98f7923656Sespie 99f7923656Sespie /* ok = Var_ParseBuffer(buf, varspec, ctxt, undef_is_bad, &length); 100f7923656Sespie * Similar to Var_Parse, except the value is directly appended to 101f7923656Sespie * buffer buf. */ 102f7923656Sespie extern bool Var_ParseBuffer(Buffer, const char *, SymTable *, 103f7923656Sespie bool, size_t *); 104f7923656Sespie 105f7923656Sespie 106f7923656Sespie /* The substitution itself */ 107f7923656Sespie /* subst = Var_Subst(str, ctxt, undef_is_bad); 108f7923656Sespie * Substitutes all variable values in string str under context ctxt. 109f7923656Sespie * Emit a PARSE_FATAL error if undef_is_bad and an undef variable is 110f7923656Sespie * encountered. The result is always a copy that should be free. */ 111f7923656Sespie extern char *Var_Subst(const char *, SymTable *, bool); 112c51bd17eSespie /* subst = Var_Substi(str, estr, ctxt, undef_if_bad); 113c51bd17eSespie */ 114c51bd17eSespie extern char *Var_Substi(const char *, const char *, SymTable *, bool); 115f7923656Sespie 11676c67f06Sespie /* has_target = Var_Check_for_target(s): 11776c67f06Sespie * specialized tweak on Var_Subst that reads through a command line 11876c67f06Sespie * and looks for stuff like $@. 11976c67f06Sespie * Use to desambiguate a list of targets into a target group. 12076c67f06Sespie */ 12176c67f06Sespie extern bool Var_Check_for_target(const char *); 122f7923656Sespie 12368636f61Sespie /* For loop handling. 12468636f61Sespie * // Create handle for variable name. 12568636f61Sespie * handle = Var_NewLoopVar(name, end); 12668636f61Sespie * // set up buffer 12768636f61Sespie * for (...) 12868636f61Sespie * // Substitute val for variable in str, and accumulate in buffer 12968636f61Sespie * Var_SubstVar(buffer, str, handle, val); 13068636f61Sespie * // Free handle 13168636f61Sespie * Var_DeleteLoopVar(handle); 13268636f61Sespie */ 13368636f61Sespie struct LoopVar; /* opaque handle */ 13468636f61Sespie struct LoopVar *Var_NewLoopVar(const char *, const char *); 13568636f61Sespie void Var_DeleteLoopVar(struct LoopVar *); 13668636f61Sespie extern void Var_SubstVar(Buffer, const char *, struct LoopVar *, const char *); 13780629fe7Sespie char *Var_LoopVarName(struct LoopVar *); 13868636f61Sespie 139f7923656Sespie 140f7923656Sespie /* Var_Dump(); 141f7923656Sespie * Print out all global variables. */ 142f7923656Sespie extern void Var_Dump(void); 143f7923656Sespie 144f7923656Sespie /* Var_AddCmdline(name); 145f7923656Sespie * Add all variable values from VAR_CMD to variable name. 146f7923656Sespie * Used to propagate variable values to submakes through MAKEFLAGS. */ 147f7923656Sespie extern void Var_AddCmdline(const char *); 148f7923656Sespie 149d88ab755Sespie /* stuff common to var.c and varparse.c */ 15093a10c24Sespie extern bool errorIsOkay; 151f7923656Sespie 152d88ab755Sespie #define VAR_GLOBAL 0 153d88ab755Sespie /* Variables defined in a global context, e.g in the Makefile itself */ 154d88ab755Sespie #define VAR_CMD 1 155d88ab755Sespie /* Variables defined on the command line */ 156d88ab755Sespie 157d88ab755Sespie #define POISON_INVALID 0 158d88ab755Sespie #define POISON_DEFINED 1 159d88ab755Sespie #define POISON_NORMAL 64 160d88ab755Sespie #define POISON_EMPTY 128 161d88ab755Sespie #define POISON_NOT_DEFINED 256 16283f3589bSespie #define VAR_EXEC_LATER 512 163d88ab755Sespie 16483f3589bSespie extern void Var_Mark(const char *, const char *, unsigned int); 165f7923656Sespie #endif 166