1321964Ssjg/* $NetBSD: var.c,v 1.215 2017/04/16 21:39:49 riastradh Exp $ */ 2236769Sobrien 3236769Sobrien/* 4236769Sobrien * Copyright (c) 1988, 1989, 1990, 1993 5236769Sobrien * The Regents of the University of California. All rights reserved. 6236769Sobrien * 7236769Sobrien * This code is derived from software contributed to Berkeley by 8236769Sobrien * Adam de Boor. 9236769Sobrien * 10236769Sobrien * Redistribution and use in source and binary forms, with or without 11236769Sobrien * modification, are permitted provided that the following conditions 12236769Sobrien * are met: 13236769Sobrien * 1. Redistributions of source code must retain the above copyright 14236769Sobrien * notice, this list of conditions and the following disclaimer. 15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 16236769Sobrien * notice, this list of conditions and the following disclaimer in the 17236769Sobrien * documentation and/or other materials provided with the distribution. 18236769Sobrien * 3. Neither the name of the University nor the names of its contributors 19236769Sobrien * may be used to endorse or promote products derived from this software 20236769Sobrien * without specific prior written permission. 21236769Sobrien * 22236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32236769Sobrien * SUCH DAMAGE. 33236769Sobrien */ 34236769Sobrien 35236769Sobrien/* 36236769Sobrien * Copyright (c) 1989 by Berkeley Softworks 37236769Sobrien * All rights reserved. 38236769Sobrien * 39236769Sobrien * This code is derived from software contributed to Berkeley by 40236769Sobrien * Adam de Boor. 41236769Sobrien * 42236769Sobrien * Redistribution and use in source and binary forms, with or without 43236769Sobrien * modification, are permitted provided that the following conditions 44236769Sobrien * are met: 45236769Sobrien * 1. Redistributions of source code must retain the above copyright 46236769Sobrien * notice, this list of conditions and the following disclaimer. 47236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 48236769Sobrien * notice, this list of conditions and the following disclaimer in the 49236769Sobrien * documentation and/or other materials provided with the distribution. 50236769Sobrien * 3. All advertising materials mentioning features or use of this software 51236769Sobrien * must display the following acknowledgement: 52236769Sobrien * This product includes software developed by the University of 53236769Sobrien * California, Berkeley and its contributors. 54236769Sobrien * 4. Neither the name of the University nor the names of its contributors 55236769Sobrien * may be used to endorse or promote products derived from this software 56236769Sobrien * without specific prior written permission. 57236769Sobrien * 58236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 59236769Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 60236769Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 61236769Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 62236769Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 63236769Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 64236769Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 65236769Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 66236769Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 67236769Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 68236769Sobrien * SUCH DAMAGE. 69236769Sobrien */ 70236769Sobrien 71236769Sobrien#ifndef MAKE_NATIVE 72321964Ssjgstatic char rcsid[] = "$NetBSD: var.c,v 1.215 2017/04/16 21:39:49 riastradh Exp $"; 73236769Sobrien#else 74236769Sobrien#include <sys/cdefs.h> 75236769Sobrien#ifndef lint 76236769Sobrien#if 0 77236769Sobrienstatic char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; 78236769Sobrien#else 79321964Ssjg__RCSID("$NetBSD: var.c,v 1.215 2017/04/16 21:39:49 riastradh Exp $"); 80236769Sobrien#endif 81236769Sobrien#endif /* not lint */ 82236769Sobrien#endif 83236769Sobrien 84236769Sobrien/*- 85236769Sobrien * var.c -- 86236769Sobrien * Variable-handling functions 87236769Sobrien * 88236769Sobrien * Interface: 89236769Sobrien * Var_Set Set the value of a variable in the given 90236769Sobrien * context. The variable is created if it doesn't 91236769Sobrien * yet exist. The value and variable name need not 92236769Sobrien * be preserved. 93236769Sobrien * 94236769Sobrien * Var_Append Append more characters to an existing variable 95236769Sobrien * in the given context. The variable needn't 96236769Sobrien * exist already -- it will be created if it doesn't. 97236769Sobrien * A space is placed between the old value and the 98236769Sobrien * new one. 99236769Sobrien * 100236769Sobrien * Var_Exists See if a variable exists. 101236769Sobrien * 102236769Sobrien * Var_Value Return the value of a variable in a context or 103236769Sobrien * NULL if the variable is undefined. 104236769Sobrien * 105236769Sobrien * Var_Subst Substitute named variable, or all variables if 106236769Sobrien * NULL in a string using 107236769Sobrien * the given context as the top-most one. If the 108236769Sobrien * third argument is non-zero, Parse_Error is 109236769Sobrien * called if any variables are undefined. 110236769Sobrien * 111236769Sobrien * Var_Parse Parse a variable expansion from a string and 112236769Sobrien * return the result and the number of characters 113236769Sobrien * consumed. 114236769Sobrien * 115236769Sobrien * Var_Delete Delete a variable in a context. 116236769Sobrien * 117236769Sobrien * Var_Init Initialize this module. 118236769Sobrien * 119236769Sobrien * Debugging: 120236769Sobrien * Var_Dump Print out all variables defined in the given 121236769Sobrien * context. 122236769Sobrien * 123236769Sobrien * XXX: There's a lot of duplication in these functions. 124236769Sobrien */ 125236769Sobrien 126236769Sobrien#include <sys/stat.h> 127236769Sobrien#ifndef NO_REGEX 128236769Sobrien#include <sys/types.h> 129236769Sobrien#include <regex.h> 130236769Sobrien#endif 131236769Sobrien#include <ctype.h> 132236769Sobrien#include <stdlib.h> 133236769Sobrien#include <limits.h> 134236769Sobrien#include <time.h> 135236769Sobrien 136236769Sobrien#include "make.h" 137236769Sobrien#include "buf.h" 138236769Sobrien#include "dir.h" 139236769Sobrien#include "job.h" 140292068Ssjg#include "metachar.h" 141236769Sobrien 142253883Ssjgextern int makelevel; 143236769Sobrien/* 144236769Sobrien * This lets us tell if we have replaced the original environ 145236769Sobrien * (which we cannot free). 146236769Sobrien */ 147236769Sobrienchar **savedEnv = NULL; 148236769Sobrien 149236769Sobrien/* 150236769Sobrien * This is a harmless return value for Var_Parse that can be used by Var_Subst 151236769Sobrien * to determine if there was an error in parsing -- easier than returning 152236769Sobrien * a flag, as things outside this module don't give a hoot. 153236769Sobrien */ 154236769Sobrienchar var_Error[] = ""; 155236769Sobrien 156236769Sobrien/* 157321964Ssjg * Similar to var_Error, but returned when the 'VARF_UNDEFERR' flag for 158321964Ssjg * Var_Parse is not set. Why not just use a constant? Well, gcc likes 159321964Ssjg * to condense identical string instances... 160236769Sobrien */ 161236769Sobrienstatic char varNoError[] = ""; 162236769Sobrien 163236769Sobrien/* 164321964Ssjg * Traditionally we consume $$ during := like any other expansion. 165321964Ssjg * Other make's do not. 166321964Ssjg * This knob allows controlling the behavior. 167321964Ssjg * FALSE for old behavior. 168321964Ssjg * TRUE for new compatible. 169321964Ssjg */ 170321964Ssjg#define SAVE_DOLLARS ".MAKE.SAVE_DOLLARS" 171321964Ssjgstatic Boolean save_dollars = FALSE; 172321964Ssjg 173321964Ssjg/* 174236769Sobrien * Internally, variables are contained in four different contexts. 175236769Sobrien * 1) the environment. They may not be changed. If an environment 176236769Sobrien * variable is appended-to, the result is placed in the global 177236769Sobrien * context. 178236769Sobrien * 2) the global context. Variables set in the Makefile are located in 179236769Sobrien * the global context. It is the penultimate context searched when 180236769Sobrien * substituting. 181236769Sobrien * 3) the command-line context. All variables set on the command line 182236769Sobrien * are placed in this context. They are UNALTERABLE once placed here. 183236769Sobrien * 4) the local context. Each target has associated with it a context 184236769Sobrien * list. On this list are located the structures describing such 185236769Sobrien * local variables as $(@) and $(*) 186236769Sobrien * The four contexts are searched in the reverse order from which they are 187236769Sobrien * listed. 188236769Sobrien */ 189255253SsjgGNode *VAR_INTERNAL; /* variables from make itself */ 190236769SobrienGNode *VAR_GLOBAL; /* variables from the makefile */ 191236769SobrienGNode *VAR_CMD; /* variables defined on the command-line */ 192236769Sobrien 193236769Sobrien#define FIND_CMD 0x1 /* look in VAR_CMD when searching */ 194236769Sobrien#define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */ 195236769Sobrien#define FIND_ENV 0x4 /* look in the environment also */ 196236769Sobrien 197236769Sobrientypedef struct Var { 198236769Sobrien char *name; /* the variable's name */ 199236769Sobrien Buffer val; /* its value */ 200236769Sobrien int flags; /* miscellaneous status flags */ 201236769Sobrien#define VAR_IN_USE 1 /* Variable's value currently being used. 202236769Sobrien * Used to avoid recursion */ 203236769Sobrien#define VAR_FROM_ENV 2 /* Variable comes from the environment */ 204236769Sobrien#define VAR_JUNK 4 /* Variable is a junk variable that 205236769Sobrien * should be destroyed when done with 206236769Sobrien * it. Used by Var_Parse for undefined, 207236769Sobrien * modified variables */ 208236769Sobrien#define VAR_KEEP 8 /* Variable is VAR_JUNK, but we found 209236769Sobrien * a use for it in some modifier and 210236769Sobrien * the value is therefore valid */ 211236769Sobrien#define VAR_EXPORTED 16 /* Variable is exported */ 212236769Sobrien#define VAR_REEXPORT 32 /* Indicate if var needs re-export. 213236769Sobrien * This would be true if it contains $'s 214236769Sobrien */ 215236769Sobrien#define VAR_FROM_CMD 64 /* Variable came from command line */ 216236769Sobrien} Var; 217236769Sobrien 218236769Sobrien/* 219236769Sobrien * Exporting vars is expensive so skip it if we can 220236769Sobrien */ 221236769Sobrien#define VAR_EXPORTED_NONE 0 222236769Sobrien#define VAR_EXPORTED_YES 1 223236769Sobrien#define VAR_EXPORTED_ALL 2 224236769Sobrienstatic int var_exportedVars = VAR_EXPORTED_NONE; 225236769Sobrien/* 226236769Sobrien * We pass this to Var_Export when doing the initial export 227236769Sobrien * or after updating an exported var. 228236769Sobrien */ 229321964Ssjg#define VAR_EXPORT_PARENT 1 230321964Ssjg/* 231321964Ssjg * We pass this to Var_Export1 to tell it to leave the value alone. 232321964Ssjg */ 233321964Ssjg#define VAR_EXPORT_LITERAL 2 234236769Sobrien 235236769Sobrien/* Var*Pattern flags */ 236236769Sobrien#define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */ 237236769Sobrien#define VAR_SUB_ONE 0x02 /* Apply substitution to one word */ 238236769Sobrien#define VAR_SUB_MATCHED 0x04 /* There was a match */ 239236769Sobrien#define VAR_MATCH_START 0x08 /* Match at start of word */ 240236769Sobrien#define VAR_MATCH_END 0x10 /* Match at end of word */ 241236769Sobrien#define VAR_NOSUBST 0x20 /* don't expand vars in VarGetPattern */ 242236769Sobrien 243236769Sobrien/* Var_Set flags */ 244236769Sobrien#define VAR_NO_EXPORT 0x01 /* do not export */ 245236769Sobrien 246236769Sobrientypedef struct { 247236769Sobrien /* 248236769Sobrien * The following fields are set by Var_Parse() when it 249236769Sobrien * encounters modifiers that need to keep state for use by 250236769Sobrien * subsequent modifiers within the same variable expansion. 251236769Sobrien */ 252236769Sobrien Byte varSpace; /* Word separator in expansions */ 253236769Sobrien Boolean oneBigWord; /* TRUE if we will treat the variable as a 254236769Sobrien * single big word, even if it contains 255236769Sobrien * embedded spaces (as opposed to the 256236769Sobrien * usual behaviour of treating it as 257236769Sobrien * several space-separated words). */ 258236769Sobrien} Var_Parse_State; 259236769Sobrien 260236769Sobrien/* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/", 261236769Sobrien * to VarSYSVMatch() for ":lhs=rhs". */ 262236769Sobrientypedef struct { 263236769Sobrien const char *lhs; /* String to match */ 264236769Sobrien int leftLen; /* Length of string */ 265236769Sobrien const char *rhs; /* Replacement string (w/ &'s removed) */ 266236769Sobrien int rightLen; /* Length of replacement */ 267236769Sobrien int flags; 268236769Sobrien} VarPattern; 269236769Sobrien 270236769Sobrien/* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */ 271236769Sobrientypedef struct { 272236769Sobrien GNode *ctxt; /* variable context */ 273236769Sobrien char *tvar; /* name of temp var */ 274236769Sobrien int tvarLen; 275236769Sobrien char *str; /* string to expand */ 276236769Sobrien int strLen; 277236769Sobrien int errnum; /* errnum for not defined */ 278236769Sobrien} VarLoop_t; 279236769Sobrien 280236769Sobrien#ifndef NO_REGEX 281236769Sobrien/* struct passed as 'void *' to VarRESubstitute() for ":C///" */ 282236769Sobrientypedef struct { 283236769Sobrien regex_t re; 284236769Sobrien int nsub; 285236769Sobrien regmatch_t *matches; 286236769Sobrien char *replace; 287236769Sobrien int flags; 288236769Sobrien} VarREPattern; 289236769Sobrien#endif 290236769Sobrien 291236769Sobrien/* struct passed to VarSelectWords() for ":[start..end]" */ 292236769Sobrientypedef struct { 293236769Sobrien int start; /* first word to select */ 294236769Sobrien int end; /* last word to select */ 295236769Sobrien} VarSelectWords_t; 296236769Sobrien 297236769Sobrienstatic Var *VarFind(const char *, GNode *, int); 298236769Sobrienstatic void VarAdd(const char *, const char *, GNode *); 299236769Sobrienstatic Boolean VarHead(GNode *, Var_Parse_State *, 300236769Sobrien char *, Boolean, Buffer *, void *); 301236769Sobrienstatic Boolean VarTail(GNode *, Var_Parse_State *, 302236769Sobrien char *, Boolean, Buffer *, void *); 303236769Sobrienstatic Boolean VarSuffix(GNode *, Var_Parse_State *, 304236769Sobrien char *, Boolean, Buffer *, void *); 305236769Sobrienstatic Boolean VarRoot(GNode *, Var_Parse_State *, 306236769Sobrien char *, Boolean, Buffer *, void *); 307236769Sobrienstatic Boolean VarMatch(GNode *, Var_Parse_State *, 308236769Sobrien char *, Boolean, Buffer *, void *); 309236769Sobrien#ifdef SYSVVARSUB 310236769Sobrienstatic Boolean VarSYSVMatch(GNode *, Var_Parse_State *, 311236769Sobrien char *, Boolean, Buffer *, void *); 312236769Sobrien#endif 313236769Sobrienstatic Boolean VarNoMatch(GNode *, Var_Parse_State *, 314236769Sobrien char *, Boolean, Buffer *, void *); 315236769Sobrien#ifndef NO_REGEX 316236769Sobrienstatic void VarREError(int, regex_t *, const char *); 317236769Sobrienstatic Boolean VarRESubstitute(GNode *, Var_Parse_State *, 318236769Sobrien char *, Boolean, Buffer *, void *); 319236769Sobrien#endif 320236769Sobrienstatic Boolean VarSubstitute(GNode *, Var_Parse_State *, 321236769Sobrien char *, Boolean, Buffer *, void *); 322236769Sobrienstatic Boolean VarLoopExpand(GNode *, Var_Parse_State *, 323236769Sobrien char *, Boolean, Buffer *, void *); 324236769Sobrienstatic char *VarGetPattern(GNode *, Var_Parse_State *, 325236769Sobrien int, const char **, int, int *, int *, 326236769Sobrien VarPattern *); 327236769Sobrienstatic char *VarQuote(char *); 328236769Sobrienstatic char *VarHash(char *); 329236769Sobrienstatic char *VarModify(GNode *, Var_Parse_State *, 330236769Sobrien const char *, 331236769Sobrien Boolean (*)(GNode *, Var_Parse_State *, char *, Boolean, Buffer *, void *), 332236769Sobrien void *); 333236769Sobrienstatic char *VarOrder(const char *, const char); 334236769Sobrienstatic char *VarUniq(const char *); 335236769Sobrienstatic int VarWordCompare(const void *, const void *); 336236769Sobrienstatic void VarPrintVar(void *); 337236769Sobrien 338236769Sobrien#define BROPEN '{' 339236769Sobrien#define BRCLOSE '}' 340236769Sobrien#define PROPEN '(' 341236769Sobrien#define PRCLOSE ')' 342236769Sobrien 343236769Sobrien/*- 344236769Sobrien *----------------------------------------------------------------------- 345236769Sobrien * VarFind -- 346236769Sobrien * Find the given variable in the given context and any other contexts 347236769Sobrien * indicated. 348236769Sobrien * 349236769Sobrien * Input: 350236769Sobrien * name name to find 351236769Sobrien * ctxt context in which to find it 352236769Sobrien * flags FIND_GLOBAL set means to look in the 353236769Sobrien * VAR_GLOBAL context as well. FIND_CMD set means 354236769Sobrien * to look in the VAR_CMD context also. FIND_ENV 355236769Sobrien * set means to look in the environment 356236769Sobrien * 357236769Sobrien * Results: 358236769Sobrien * A pointer to the structure describing the desired variable or 359236769Sobrien * NULL if the variable does not exist. 360236769Sobrien * 361236769Sobrien * Side Effects: 362236769Sobrien * None 363236769Sobrien *----------------------------------------------------------------------- 364236769Sobrien */ 365236769Sobrienstatic Var * 366236769SobrienVarFind(const char *name, GNode *ctxt, int flags) 367236769Sobrien{ 368236769Sobrien Hash_Entry *var; 369236769Sobrien Var *v; 370236769Sobrien 371236769Sobrien /* 372236769Sobrien * If the variable name begins with a '.', it could very well be one of 373236769Sobrien * the local ones. We check the name against all the local variables 374236769Sobrien * and substitute the short version in for 'name' if it matches one of 375236769Sobrien * them. 376236769Sobrien */ 377236769Sobrien if (*name == '.' && isupper((unsigned char) name[1])) 378236769Sobrien switch (name[1]) { 379236769Sobrien case 'A': 380236769Sobrien if (!strcmp(name, ".ALLSRC")) 381236769Sobrien name = ALLSRC; 382236769Sobrien if (!strcmp(name, ".ARCHIVE")) 383236769Sobrien name = ARCHIVE; 384236769Sobrien break; 385236769Sobrien case 'I': 386236769Sobrien if (!strcmp(name, ".IMPSRC")) 387236769Sobrien name = IMPSRC; 388236769Sobrien break; 389236769Sobrien case 'M': 390236769Sobrien if (!strcmp(name, ".MEMBER")) 391236769Sobrien name = MEMBER; 392236769Sobrien break; 393236769Sobrien case 'O': 394236769Sobrien if (!strcmp(name, ".OODATE")) 395236769Sobrien name = OODATE; 396236769Sobrien break; 397236769Sobrien case 'P': 398236769Sobrien if (!strcmp(name, ".PREFIX")) 399236769Sobrien name = PREFIX; 400236769Sobrien break; 401236769Sobrien case 'T': 402236769Sobrien if (!strcmp(name, ".TARGET")) 403236769Sobrien name = TARGET; 404236769Sobrien break; 405236769Sobrien } 406236769Sobrien#ifdef notyet 407236769Sobrien /* for compatibility with gmake */ 408236769Sobrien if (name[0] == '^' && name[1] == '\0') 409236769Sobrien name = ALLSRC; 410236769Sobrien#endif 411236769Sobrien 412236769Sobrien /* 413236769Sobrien * First look for the variable in the given context. If it's not there, 414236769Sobrien * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, 415236769Sobrien * depending on the FIND_* flags in 'flags' 416236769Sobrien */ 417236769Sobrien var = Hash_FindEntry(&ctxt->context, name); 418236769Sobrien 419236769Sobrien if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) { 420236769Sobrien var = Hash_FindEntry(&VAR_CMD->context, name); 421236769Sobrien } 422236769Sobrien if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) && 423236769Sobrien (ctxt != VAR_GLOBAL)) 424236769Sobrien { 425236769Sobrien var = Hash_FindEntry(&VAR_GLOBAL->context, name); 426255253Ssjg if ((var == NULL) && (ctxt != VAR_INTERNAL)) { 427255253Ssjg /* VAR_INTERNAL is subordinate to VAR_GLOBAL */ 428255253Ssjg var = Hash_FindEntry(&VAR_INTERNAL->context, name); 429255253Ssjg } 430236769Sobrien } 431236769Sobrien if ((var == NULL) && (flags & FIND_ENV)) { 432236769Sobrien char *env; 433236769Sobrien 434236769Sobrien if ((env = getenv(name)) != NULL) { 435236769Sobrien int len; 436236769Sobrien 437236769Sobrien v = bmake_malloc(sizeof(Var)); 438236769Sobrien v->name = bmake_strdup(name); 439236769Sobrien 440236769Sobrien len = strlen(env); 441236769Sobrien 442236769Sobrien Buf_Init(&v->val, len + 1); 443236769Sobrien Buf_AddBytes(&v->val, len, env); 444236769Sobrien 445236769Sobrien v->flags = VAR_FROM_ENV; 446236769Sobrien return (v); 447236769Sobrien } else if (checkEnvFirst && (flags & FIND_GLOBAL) && 448236769Sobrien (ctxt != VAR_GLOBAL)) 449236769Sobrien { 450236769Sobrien var = Hash_FindEntry(&VAR_GLOBAL->context, name); 451255253Ssjg if ((var == NULL) && (ctxt != VAR_INTERNAL)) { 452255253Ssjg var = Hash_FindEntry(&VAR_INTERNAL->context, name); 453255253Ssjg } 454236769Sobrien if (var == NULL) { 455236769Sobrien return NULL; 456236769Sobrien } else { 457236769Sobrien return ((Var *)Hash_GetValue(var)); 458236769Sobrien } 459236769Sobrien } else { 460236769Sobrien return NULL; 461236769Sobrien } 462236769Sobrien } else if (var == NULL) { 463236769Sobrien return NULL; 464236769Sobrien } else { 465236769Sobrien return ((Var *)Hash_GetValue(var)); 466236769Sobrien } 467236769Sobrien} 468236769Sobrien 469236769Sobrien/*- 470236769Sobrien *----------------------------------------------------------------------- 471236769Sobrien * VarFreeEnv -- 472236769Sobrien * If the variable is an environment variable, free it 473236769Sobrien * 474236769Sobrien * Input: 475236769Sobrien * v the variable 476236769Sobrien * destroy true if the value buffer should be destroyed. 477236769Sobrien * 478236769Sobrien * Results: 479236769Sobrien * 1 if it is an environment variable 0 ow. 480236769Sobrien * 481236769Sobrien * Side Effects: 482236769Sobrien * The variable is free'ed if it is an environent variable. 483236769Sobrien *----------------------------------------------------------------------- 484236769Sobrien */ 485236769Sobrienstatic Boolean 486236769SobrienVarFreeEnv(Var *v, Boolean destroy) 487236769Sobrien{ 488236769Sobrien if ((v->flags & VAR_FROM_ENV) == 0) 489236769Sobrien return FALSE; 490236769Sobrien free(v->name); 491236769Sobrien Buf_Destroy(&v->val, destroy); 492236769Sobrien free(v); 493236769Sobrien return TRUE; 494236769Sobrien} 495236769Sobrien 496236769Sobrien/*- 497236769Sobrien *----------------------------------------------------------------------- 498236769Sobrien * VarAdd -- 499236769Sobrien * Add a new variable of name name and value val to the given context 500236769Sobrien * 501236769Sobrien * Input: 502236769Sobrien * name name of variable to add 503236769Sobrien * val value to set it to 504236769Sobrien * ctxt context in which to set it 505236769Sobrien * 506236769Sobrien * Results: 507236769Sobrien * None 508236769Sobrien * 509236769Sobrien * Side Effects: 510236769Sobrien * The new variable is placed at the front of the given context 511236769Sobrien * The name and val arguments are duplicated so they may 512236769Sobrien * safely be freed. 513236769Sobrien *----------------------------------------------------------------------- 514236769Sobrien */ 515236769Sobrienstatic void 516236769SobrienVarAdd(const char *name, const char *val, GNode *ctxt) 517236769Sobrien{ 518236769Sobrien Var *v; 519236769Sobrien int len; 520236769Sobrien Hash_Entry *h; 521236769Sobrien 522236769Sobrien v = bmake_malloc(sizeof(Var)); 523236769Sobrien 524236769Sobrien len = val ? strlen(val) : 0; 525236769Sobrien Buf_Init(&v->val, len+1); 526236769Sobrien Buf_AddBytes(&v->val, len, val); 527236769Sobrien 528236769Sobrien v->flags = 0; 529236769Sobrien 530236769Sobrien h = Hash_CreateEntry(&ctxt->context, name, NULL); 531236769Sobrien Hash_SetValue(h, v); 532236769Sobrien v->name = h->name; 533321964Ssjg if (DEBUG(VAR) && (ctxt->flags & INTERNAL) == 0) { 534236769Sobrien fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); 535236769Sobrien } 536236769Sobrien} 537236769Sobrien 538236769Sobrien/*- 539236769Sobrien *----------------------------------------------------------------------- 540236769Sobrien * Var_Delete -- 541236769Sobrien * Remove a variable from a context. 542236769Sobrien * 543236769Sobrien * Results: 544236769Sobrien * None. 545236769Sobrien * 546236769Sobrien * Side Effects: 547236769Sobrien * The Var structure is removed and freed. 548236769Sobrien * 549236769Sobrien *----------------------------------------------------------------------- 550236769Sobrien */ 551236769Sobrienvoid 552236769SobrienVar_Delete(const char *name, GNode *ctxt) 553236769Sobrien{ 554236769Sobrien Hash_Entry *ln; 555250770Ssjg char *cp; 556250770Ssjg 557250770Ssjg if (strchr(name, '$')) { 558321964Ssjg cp = Var_Subst(NULL, name, VAR_GLOBAL, VARF_WANTRES); 559250770Ssjg } else { 560250771Ssjg cp = (char *)name; 561250770Ssjg } 562250770Ssjg ln = Hash_FindEntry(&ctxt->context, cp); 563236769Sobrien if (DEBUG(VAR)) { 564236769Sobrien fprintf(debug_file, "%s:delete %s%s\n", 565250770Ssjg ctxt->name, cp, ln ? "" : " (not found)"); 566236769Sobrien } 567250770Ssjg if (cp != name) { 568250770Ssjg free(cp); 569250770Ssjg } 570236769Sobrien if (ln != NULL) { 571236769Sobrien Var *v; 572236769Sobrien 573236769Sobrien v = (Var *)Hash_GetValue(ln); 574236769Sobrien if ((v->flags & VAR_EXPORTED)) { 575236769Sobrien unsetenv(v->name); 576236769Sobrien } 577236769Sobrien if (strcmp(MAKE_EXPORTED, v->name) == 0) { 578236769Sobrien var_exportedVars = VAR_EXPORTED_NONE; 579236769Sobrien } 580236769Sobrien if (v->name != ln->name) 581236769Sobrien free(v->name); 582236769Sobrien Hash_DeleteEntry(&ctxt->context, ln); 583236769Sobrien Buf_Destroy(&v->val, TRUE); 584236769Sobrien free(v); 585236769Sobrien } 586236769Sobrien} 587236769Sobrien 588236769Sobrien 589236769Sobrien/* 590236769Sobrien * Export a var. 591236769Sobrien * We ignore make internal variables (those which start with '.') 592236769Sobrien * Also we jump through some hoops to avoid calling setenv 593236769Sobrien * more than necessary since it can leak. 594236769Sobrien * We only manipulate flags of vars if 'parent' is set. 595236769Sobrien */ 596236769Sobrienstatic int 597321964SsjgVar_Export1(const char *name, int flags) 598236769Sobrien{ 599236769Sobrien char tmp[BUFSIZ]; 600236769Sobrien Var *v; 601236769Sobrien char *val = NULL; 602236769Sobrien int n; 603321964Ssjg int parent = (flags & VAR_EXPORT_PARENT); 604236769Sobrien 605236769Sobrien if (*name == '.') 606236769Sobrien return 0; /* skip internals */ 607236769Sobrien if (!name[1]) { 608236769Sobrien /* 609236769Sobrien * A single char. 610236769Sobrien * If it is one of the vars that should only appear in 611236769Sobrien * local context, skip it, else we can get Var_Subst 612236769Sobrien * into a loop. 613236769Sobrien */ 614236769Sobrien switch (name[0]) { 615236769Sobrien case '@': 616236769Sobrien case '%': 617236769Sobrien case '*': 618236769Sobrien case '!': 619236769Sobrien return 0; 620236769Sobrien } 621236769Sobrien } 622236769Sobrien v = VarFind(name, VAR_GLOBAL, 0); 623236769Sobrien if (v == NULL) { 624236769Sobrien return 0; 625236769Sobrien } 626236769Sobrien if (!parent && 627236769Sobrien (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) { 628236769Sobrien return 0; /* nothing to do */ 629236769Sobrien } 630236769Sobrien val = Buf_GetAll(&v->val, NULL); 631321964Ssjg if ((flags & VAR_EXPORT_LITERAL) == 0 && strchr(val, '$')) { 632236769Sobrien if (parent) { 633236769Sobrien /* 634236769Sobrien * Flag this as something we need to re-export. 635236769Sobrien * No point actually exporting it now though, 636236769Sobrien * the child can do it at the last minute. 637236769Sobrien */ 638236769Sobrien v->flags |= (VAR_EXPORTED|VAR_REEXPORT); 639236769Sobrien return 1; 640236769Sobrien } 641236769Sobrien if (v->flags & VAR_IN_USE) { 642236769Sobrien /* 643236769Sobrien * We recursed while exporting in a child. 644236769Sobrien * This isn't going to end well, just skip it. 645236769Sobrien */ 646236769Sobrien return 0; 647236769Sobrien } 648236769Sobrien n = snprintf(tmp, sizeof(tmp), "${%s}", name); 649236769Sobrien if (n < (int)sizeof(tmp)) { 650321964Ssjg val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES); 651236769Sobrien setenv(name, val, 1); 652236769Sobrien free(val); 653236769Sobrien } 654236769Sobrien } else { 655236769Sobrien if (parent) { 656236769Sobrien v->flags &= ~VAR_REEXPORT; /* once will do */ 657236769Sobrien } 658236769Sobrien if (parent || !(v->flags & VAR_EXPORTED)) { 659236769Sobrien setenv(name, val, 1); 660236769Sobrien } 661236769Sobrien } 662236769Sobrien /* 663236769Sobrien * This is so Var_Set knows to call Var_Export again... 664236769Sobrien */ 665236769Sobrien if (parent) { 666236769Sobrien v->flags |= VAR_EXPORTED; 667236769Sobrien } 668236769Sobrien return 1; 669236769Sobrien} 670236769Sobrien 671236769Sobrien/* 672236769Sobrien * This gets called from our children. 673236769Sobrien */ 674236769Sobrienvoid 675236769SobrienVar_ExportVars(void) 676236769Sobrien{ 677236769Sobrien char tmp[BUFSIZ]; 678236769Sobrien Hash_Entry *var; 679236769Sobrien Hash_Search state; 680236769Sobrien Var *v; 681236769Sobrien char *val; 682236769Sobrien int n; 683236769Sobrien 684253883Ssjg /* 685253883Ssjg * Several make's support this sort of mechanism for tracking 686253883Ssjg * recursion - but each uses a different name. 687253883Ssjg * We allow the makefiles to update MAKELEVEL and ensure 688253883Ssjg * children see a correctly incremented value. 689253883Ssjg */ 690253883Ssjg snprintf(tmp, sizeof(tmp), "%d", makelevel + 1); 691253883Ssjg setenv(MAKE_LEVEL_ENV, tmp, 1); 692253883Ssjg 693236769Sobrien if (VAR_EXPORTED_NONE == var_exportedVars) 694236769Sobrien return; 695236769Sobrien 696236769Sobrien if (VAR_EXPORTED_ALL == var_exportedVars) { 697236769Sobrien /* 698236769Sobrien * Ouch! This is crazy... 699236769Sobrien */ 700236769Sobrien for (var = Hash_EnumFirst(&VAR_GLOBAL->context, &state); 701236769Sobrien var != NULL; 702236769Sobrien var = Hash_EnumNext(&state)) { 703236769Sobrien v = (Var *)Hash_GetValue(var); 704236769Sobrien Var_Export1(v->name, 0); 705236769Sobrien } 706236769Sobrien return; 707236769Sobrien } 708236769Sobrien /* 709236769Sobrien * We have a number of exported vars, 710236769Sobrien */ 711236769Sobrien n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}"); 712236769Sobrien if (n < (int)sizeof(tmp)) { 713236769Sobrien char **av; 714236769Sobrien char *as; 715236769Sobrien int ac; 716236769Sobrien int i; 717236769Sobrien 718321964Ssjg val = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES); 719292068Ssjg if (*val) { 720292068Ssjg av = brk_string(val, &ac, FALSE, &as); 721292068Ssjg for (i = 0; i < ac; i++) { 722292068Ssjg Var_Export1(av[i], 0); 723292068Ssjg } 724292068Ssjg free(as); 725292068Ssjg free(av); 726236769Sobrien } 727236769Sobrien free(val); 728236769Sobrien } 729236769Sobrien} 730236769Sobrien 731236769Sobrien/* 732236769Sobrien * This is called when .export is seen or 733236769Sobrien * .MAKE.EXPORTED is modified. 734236769Sobrien * It is also called when any exported var is modified. 735236769Sobrien */ 736236769Sobrienvoid 737236769SobrienVar_Export(char *str, int isExport) 738236769Sobrien{ 739236769Sobrien char *name; 740236769Sobrien char *val; 741236769Sobrien char **av; 742236769Sobrien char *as; 743321964Ssjg int flags; 744236769Sobrien int ac; 745236769Sobrien int i; 746236769Sobrien 747236769Sobrien if (isExport && (!str || !str[0])) { 748236769Sobrien var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */ 749236769Sobrien return; 750236769Sobrien } 751236769Sobrien 752321964Ssjg flags = 0; 753236769Sobrien if (strncmp(str, "-env", 4) == 0) { 754236769Sobrien str += 4; 755321964Ssjg } else if (strncmp(str, "-literal", 8) == 0) { 756321964Ssjg str += 8; 757321964Ssjg flags |= VAR_EXPORT_LITERAL; 758236769Sobrien } else { 759321964Ssjg flags |= VAR_EXPORT_PARENT; 760236769Sobrien } 761321964Ssjg val = Var_Subst(NULL, str, VAR_GLOBAL, VARF_WANTRES); 762292068Ssjg if (*val) { 763292068Ssjg av = brk_string(val, &ac, FALSE, &as); 764292068Ssjg for (i = 0; i < ac; i++) { 765292068Ssjg name = av[i]; 766292068Ssjg if (!name[1]) { 767292068Ssjg /* 768292068Ssjg * A single char. 769292068Ssjg * If it is one of the vars that should only appear in 770292068Ssjg * local context, skip it, else we can get Var_Subst 771292068Ssjg * into a loop. 772292068Ssjg */ 773292068Ssjg switch (name[0]) { 774292068Ssjg case '@': 775292068Ssjg case '%': 776292068Ssjg case '*': 777292068Ssjg case '!': 778292068Ssjg continue; 779292068Ssjg } 780236769Sobrien } 781321964Ssjg if (Var_Export1(name, flags)) { 782292068Ssjg if (VAR_EXPORTED_ALL != var_exportedVars) 783292068Ssjg var_exportedVars = VAR_EXPORTED_YES; 784321964Ssjg if (isExport && (flags & VAR_EXPORT_PARENT)) { 785292068Ssjg Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL); 786292068Ssjg } 787236769Sobrien } 788236769Sobrien } 789292068Ssjg free(as); 790292068Ssjg free(av); 791236769Sobrien } 792236769Sobrien free(val); 793236769Sobrien} 794236769Sobrien 795236769Sobrien 796236769Sobrien/* 797236769Sobrien * This is called when .unexport[-env] is seen. 798236769Sobrien */ 799236769Sobrienextern char **environ; 800236769Sobrien 801236769Sobrienvoid 802236769SobrienVar_UnExport(char *str) 803236769Sobrien{ 804236769Sobrien char tmp[BUFSIZ]; 805236769Sobrien char *vlist; 806236769Sobrien char *cp; 807236769Sobrien Boolean unexport_env; 808236769Sobrien int n; 809236769Sobrien 810236769Sobrien if (!str || !str[0]) { 811236769Sobrien return; /* assert? */ 812236769Sobrien } 813236769Sobrien 814236769Sobrien vlist = NULL; 815236769Sobrien 816236769Sobrien str += 8; 817236769Sobrien unexport_env = (strncmp(str, "-env", 4) == 0); 818236769Sobrien if (unexport_env) { 819236769Sobrien char **newenv; 820236769Sobrien 821253883Ssjg cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */ 822236769Sobrien if (environ == savedEnv) { 823236769Sobrien /* we have been here before! */ 824236769Sobrien newenv = bmake_realloc(environ, 2 * sizeof(char *)); 825236769Sobrien } else { 826236769Sobrien if (savedEnv) { 827236769Sobrien free(savedEnv); 828236769Sobrien savedEnv = NULL; 829236769Sobrien } 830236769Sobrien newenv = bmake_malloc(2 * sizeof(char *)); 831236769Sobrien } 832236769Sobrien if (!newenv) 833236769Sobrien return; 834236769Sobrien /* Note: we cannot safely free() the original environ. */ 835236769Sobrien environ = savedEnv = newenv; 836236769Sobrien newenv[0] = NULL; 837236769Sobrien newenv[1] = NULL; 838253883Ssjg setenv(MAKE_LEVEL_ENV, cp, 1); 839236769Sobrien } else { 840236769Sobrien for (; *str != '\n' && isspace((unsigned char) *str); str++) 841236769Sobrien continue; 842236769Sobrien if (str[0] && str[0] != '\n') { 843236769Sobrien vlist = str; 844236769Sobrien } 845236769Sobrien } 846236769Sobrien 847236769Sobrien if (!vlist) { 848236769Sobrien /* Using .MAKE.EXPORTED */ 849236769Sobrien n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}"); 850236769Sobrien if (n < (int)sizeof(tmp)) { 851321964Ssjg vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES); 852236769Sobrien } 853236769Sobrien } 854236769Sobrien if (vlist) { 855236769Sobrien Var *v; 856236769Sobrien char **av; 857236769Sobrien char *as; 858236769Sobrien int ac; 859236769Sobrien int i; 860236769Sobrien 861236769Sobrien av = brk_string(vlist, &ac, FALSE, &as); 862236769Sobrien for (i = 0; i < ac; i++) { 863236769Sobrien v = VarFind(av[i], VAR_GLOBAL, 0); 864236769Sobrien if (!v) 865236769Sobrien continue; 866236769Sobrien if (!unexport_env && 867236769Sobrien (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) { 868236769Sobrien unsetenv(v->name); 869236769Sobrien } 870236769Sobrien v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT); 871236769Sobrien /* 872236769Sobrien * If we are unexporting a list, 873236769Sobrien * remove each one from .MAKE.EXPORTED. 874236769Sobrien * If we are removing them all, 875236769Sobrien * just delete .MAKE.EXPORTED below. 876236769Sobrien */ 877236769Sobrien if (vlist == str) { 878236769Sobrien n = snprintf(tmp, sizeof(tmp), 879236769Sobrien "${" MAKE_EXPORTED ":N%s}", v->name); 880236769Sobrien if (n < (int)sizeof(tmp)) { 881321964Ssjg cp = Var_Subst(NULL, tmp, VAR_GLOBAL, VARF_WANTRES); 882236769Sobrien Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0); 883236769Sobrien free(cp); 884236769Sobrien } 885236769Sobrien } 886236769Sobrien } 887236769Sobrien free(as); 888236769Sobrien free(av); 889236769Sobrien if (vlist != str) { 890236769Sobrien Var_Delete(MAKE_EXPORTED, VAR_GLOBAL); 891236769Sobrien free(vlist); 892236769Sobrien } 893236769Sobrien } 894236769Sobrien} 895236769Sobrien 896236769Sobrien/*- 897236769Sobrien *----------------------------------------------------------------------- 898236769Sobrien * Var_Set -- 899236769Sobrien * Set the variable name to the value val in the given context. 900236769Sobrien * 901236769Sobrien * Input: 902236769Sobrien * name name of variable to set 903236769Sobrien * val value to give to the variable 904236769Sobrien * ctxt context in which to set it 905236769Sobrien * 906236769Sobrien * Results: 907236769Sobrien * None. 908236769Sobrien * 909236769Sobrien * Side Effects: 910236769Sobrien * If the variable doesn't yet exist, a new record is created for it. 911236769Sobrien * Else the old value is freed and the new one stuck in its place 912236769Sobrien * 913236769Sobrien * Notes: 914236769Sobrien * The variable is searched for only in its context before being 915236769Sobrien * created in that context. I.e. if the context is VAR_GLOBAL, 916236769Sobrien * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only 917236769Sobrien * VAR_CMD->context is searched. This is done to avoid the literally 918236769Sobrien * thousands of unnecessary strcmp's that used to be done to 919236769Sobrien * set, say, $(@) or $(<). 920236769Sobrien * If the context is VAR_GLOBAL though, we check if the variable 921236769Sobrien * was set in VAR_CMD from the command line and skip it if so. 922236769Sobrien *----------------------------------------------------------------------- 923236769Sobrien */ 924236769Sobrienvoid 925236769SobrienVar_Set(const char *name, const char *val, GNode *ctxt, int flags) 926236769Sobrien{ 927236769Sobrien Var *v; 928236769Sobrien char *expanded_name = NULL; 929236769Sobrien 930236769Sobrien /* 931236769Sobrien * We only look for a variable in the given context since anything set 932236769Sobrien * here will override anything in a lower context, so there's not much 933236769Sobrien * point in searching them all just to save a bit of memory... 934236769Sobrien */ 935236769Sobrien if (strchr(name, '$') != NULL) { 936321964Ssjg expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES); 937236769Sobrien if (expanded_name[0] == 0) { 938236769Sobrien if (DEBUG(VAR)) { 939236769Sobrien fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) " 940236769Sobrien "name expands to empty string - ignored\n", 941236769Sobrien name, val); 942236769Sobrien } 943236769Sobrien free(expanded_name); 944236769Sobrien return; 945236769Sobrien } 946236769Sobrien name = expanded_name; 947236769Sobrien } 948236769Sobrien if (ctxt == VAR_GLOBAL) { 949236769Sobrien v = VarFind(name, VAR_CMD, 0); 950236769Sobrien if (v != NULL) { 951236769Sobrien if ((v->flags & VAR_FROM_CMD)) { 952236769Sobrien if (DEBUG(VAR)) { 953236769Sobrien fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val); 954236769Sobrien } 955236769Sobrien goto out; 956236769Sobrien } 957236769Sobrien VarFreeEnv(v, TRUE); 958236769Sobrien } 959236769Sobrien } 960236769Sobrien v = VarFind(name, ctxt, 0); 961236769Sobrien if (v == NULL) { 962253883Ssjg if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { 963253883Ssjg /* 964253883Ssjg * This var would normally prevent the same name being added 965253883Ssjg * to VAR_GLOBAL, so delete it from there if needed. 966253883Ssjg * Otherwise -V name may show the wrong value. 967253883Ssjg */ 968253883Ssjg Var_Delete(name, VAR_GLOBAL); 969253883Ssjg } 970236769Sobrien VarAdd(name, val, ctxt); 971236769Sobrien } else { 972236769Sobrien Buf_Empty(&v->val); 973236769Sobrien Buf_AddBytes(&v->val, strlen(val), val); 974236769Sobrien 975236769Sobrien if (DEBUG(VAR)) { 976236769Sobrien fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); 977236769Sobrien } 978236769Sobrien if ((v->flags & VAR_EXPORTED)) { 979236769Sobrien Var_Export1(name, VAR_EXPORT_PARENT); 980236769Sobrien } 981236769Sobrien } 982236769Sobrien /* 983236769Sobrien * Any variables given on the command line are automatically exported 984236769Sobrien * to the environment (as per POSIX standard) 985236769Sobrien */ 986236769Sobrien if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { 987236769Sobrien if (v == NULL) { 988236769Sobrien /* we just added it */ 989236769Sobrien v = VarFind(name, ctxt, 0); 990236769Sobrien } 991236769Sobrien if (v != NULL) 992236769Sobrien v->flags |= VAR_FROM_CMD; 993236769Sobrien /* 994236769Sobrien * If requested, don't export these in the environment 995236769Sobrien * individually. We still put them in MAKEOVERRIDES so 996236769Sobrien * that the command-line settings continue to override 997236769Sobrien * Makefile settings. 998236769Sobrien */ 999236769Sobrien if (varNoExportEnv != TRUE) 1000236769Sobrien setenv(name, val, 1); 1001236769Sobrien 1002236769Sobrien Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL); 1003236769Sobrien } 1004321964Ssjg if (*name == '.') { 1005321964Ssjg if (strcmp(name, SAVE_DOLLARS) == 0) 1006321964Ssjg save_dollars = s2Boolean(val, save_dollars); 1007321964Ssjg } 1008253883Ssjg 1009236769Sobrien out: 1010292068Ssjg free(expanded_name); 1011236769Sobrien if (v != NULL) 1012236769Sobrien VarFreeEnv(v, TRUE); 1013236769Sobrien} 1014236769Sobrien 1015236769Sobrien/*- 1016236769Sobrien *----------------------------------------------------------------------- 1017236769Sobrien * Var_Append -- 1018236769Sobrien * The variable of the given name has the given value appended to it in 1019236769Sobrien * the given context. 1020236769Sobrien * 1021236769Sobrien * Input: 1022236769Sobrien * name name of variable to modify 1023236769Sobrien * val String to append to it 1024236769Sobrien * ctxt Context in which this should occur 1025236769Sobrien * 1026236769Sobrien * Results: 1027236769Sobrien * None 1028236769Sobrien * 1029236769Sobrien * Side Effects: 1030236769Sobrien * If the variable doesn't exist, it is created. Else the strings 1031236769Sobrien * are concatenated (with a space in between). 1032236769Sobrien * 1033236769Sobrien * Notes: 1034236769Sobrien * Only if the variable is being sought in the global context is the 1035236769Sobrien * environment searched. 1036236769Sobrien * XXX: Knows its calling circumstances in that if called with ctxt 1037236769Sobrien * an actual target, it will only search that context since only 1038236769Sobrien * a local variable could be being appended to. This is actually 1039236769Sobrien * a big win and must be tolerated. 1040236769Sobrien *----------------------------------------------------------------------- 1041236769Sobrien */ 1042236769Sobrienvoid 1043236769SobrienVar_Append(const char *name, const char *val, GNode *ctxt) 1044236769Sobrien{ 1045236769Sobrien Var *v; 1046236769Sobrien Hash_Entry *h; 1047236769Sobrien char *expanded_name = NULL; 1048236769Sobrien 1049236769Sobrien if (strchr(name, '$') != NULL) { 1050321964Ssjg expanded_name = Var_Subst(NULL, name, ctxt, VARF_WANTRES); 1051236769Sobrien if (expanded_name[0] == 0) { 1052236769Sobrien if (DEBUG(VAR)) { 1053236769Sobrien fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) " 1054236769Sobrien "name expands to empty string - ignored\n", 1055236769Sobrien name, val); 1056236769Sobrien } 1057236769Sobrien free(expanded_name); 1058236769Sobrien return; 1059236769Sobrien } 1060236769Sobrien name = expanded_name; 1061236769Sobrien } 1062236769Sobrien 1063236769Sobrien v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0); 1064236769Sobrien 1065236769Sobrien if (v == NULL) { 1066236769Sobrien VarAdd(name, val, ctxt); 1067236769Sobrien } else { 1068236769Sobrien Buf_AddByte(&v->val, ' '); 1069236769Sobrien Buf_AddBytes(&v->val, strlen(val), val); 1070236769Sobrien 1071236769Sobrien if (DEBUG(VAR)) { 1072236769Sobrien fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, 1073236769Sobrien Buf_GetAll(&v->val, NULL)); 1074236769Sobrien } 1075236769Sobrien 1076236769Sobrien if (v->flags & VAR_FROM_ENV) { 1077236769Sobrien /* 1078236769Sobrien * If the original variable came from the environment, we 1079236769Sobrien * have to install it in the global context (we could place 1080236769Sobrien * it in the environment, but then we should provide a way to 1081236769Sobrien * export other variables...) 1082236769Sobrien */ 1083236769Sobrien v->flags &= ~VAR_FROM_ENV; 1084236769Sobrien h = Hash_CreateEntry(&ctxt->context, name, NULL); 1085236769Sobrien Hash_SetValue(h, v); 1086236769Sobrien } 1087236769Sobrien } 1088292068Ssjg free(expanded_name); 1089236769Sobrien} 1090236769Sobrien 1091236769Sobrien/*- 1092236769Sobrien *----------------------------------------------------------------------- 1093236769Sobrien * Var_Exists -- 1094236769Sobrien * See if the given variable exists. 1095236769Sobrien * 1096236769Sobrien * Input: 1097236769Sobrien * name Variable to find 1098236769Sobrien * ctxt Context in which to start search 1099236769Sobrien * 1100236769Sobrien * Results: 1101236769Sobrien * TRUE if it does, FALSE if it doesn't 1102236769Sobrien * 1103236769Sobrien * Side Effects: 1104236769Sobrien * None. 1105236769Sobrien * 1106236769Sobrien *----------------------------------------------------------------------- 1107236769Sobrien */ 1108236769SobrienBoolean 1109236769SobrienVar_Exists(const char *name, GNode *ctxt) 1110236769Sobrien{ 1111236769Sobrien Var *v; 1112236769Sobrien char *cp; 1113236769Sobrien 1114236769Sobrien if ((cp = strchr(name, '$')) != NULL) { 1115321964Ssjg cp = Var_Subst(NULL, name, ctxt, VARF_WANTRES); 1116236769Sobrien } 1117236769Sobrien v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV); 1118292068Ssjg free(cp); 1119236769Sobrien if (v == NULL) { 1120236769Sobrien return(FALSE); 1121236769Sobrien } else { 1122236769Sobrien (void)VarFreeEnv(v, TRUE); 1123236769Sobrien } 1124236769Sobrien return(TRUE); 1125236769Sobrien} 1126236769Sobrien 1127236769Sobrien/*- 1128236769Sobrien *----------------------------------------------------------------------- 1129236769Sobrien * Var_Value -- 1130236769Sobrien * Return the value of the named variable in the given context 1131236769Sobrien * 1132236769Sobrien * Input: 1133236769Sobrien * name name to find 1134236769Sobrien * ctxt context in which to search for it 1135236769Sobrien * 1136236769Sobrien * Results: 1137236769Sobrien * The value if the variable exists, NULL if it doesn't 1138236769Sobrien * 1139236769Sobrien * Side Effects: 1140236769Sobrien * None 1141236769Sobrien *----------------------------------------------------------------------- 1142236769Sobrien */ 1143236769Sobrienchar * 1144236769SobrienVar_Value(const char *name, GNode *ctxt, char **frp) 1145236769Sobrien{ 1146236769Sobrien Var *v; 1147236769Sobrien 1148236769Sobrien v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 1149236769Sobrien *frp = NULL; 1150236769Sobrien if (v != NULL) { 1151236769Sobrien char *p = (Buf_GetAll(&v->val, NULL)); 1152236769Sobrien if (VarFreeEnv(v, FALSE)) 1153236769Sobrien *frp = p; 1154236769Sobrien return p; 1155236769Sobrien } else { 1156236769Sobrien return NULL; 1157236769Sobrien } 1158236769Sobrien} 1159236769Sobrien 1160236769Sobrien/*- 1161236769Sobrien *----------------------------------------------------------------------- 1162236769Sobrien * VarHead -- 1163236769Sobrien * Remove the tail of the given word and place the result in the given 1164236769Sobrien * buffer. 1165236769Sobrien * 1166236769Sobrien * Input: 1167236769Sobrien * word Word to trim 1168236769Sobrien * addSpace True if need to add a space to the buffer 1169236769Sobrien * before sticking in the head 1170236769Sobrien * buf Buffer in which to store it 1171236769Sobrien * 1172236769Sobrien * Results: 1173236769Sobrien * TRUE if characters were added to the buffer (a space needs to be 1174236769Sobrien * added to the buffer before the next word). 1175236769Sobrien * 1176236769Sobrien * Side Effects: 1177236769Sobrien * The trimmed word is added to the buffer. 1178236769Sobrien * 1179236769Sobrien *----------------------------------------------------------------------- 1180236769Sobrien */ 1181236769Sobrienstatic Boolean 1182237578SobrienVarHead(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1183236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1184321964Ssjg void *dummy MAKE_ATTR_UNUSED) 1185236769Sobrien{ 1186236769Sobrien char *slash; 1187236769Sobrien 1188236769Sobrien slash = strrchr(word, '/'); 1189236769Sobrien if (slash != NULL) { 1190236769Sobrien if (addSpace && vpstate->varSpace) { 1191236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1192236769Sobrien } 1193236769Sobrien *slash = '\0'; 1194236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1195236769Sobrien *slash = '/'; 1196236769Sobrien return (TRUE); 1197236769Sobrien } else { 1198236769Sobrien /* 1199236769Sobrien * If no directory part, give . (q.v. the POSIX standard) 1200236769Sobrien */ 1201236769Sobrien if (addSpace && vpstate->varSpace) 1202236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1203236769Sobrien Buf_AddByte(buf, '.'); 1204236769Sobrien } 1205321964Ssjg return TRUE; 1206236769Sobrien} 1207236769Sobrien 1208236769Sobrien/*- 1209236769Sobrien *----------------------------------------------------------------------- 1210236769Sobrien * VarTail -- 1211236769Sobrien * Remove the head of the given word and place the result in the given 1212236769Sobrien * buffer. 1213236769Sobrien * 1214236769Sobrien * Input: 1215236769Sobrien * word Word to trim 1216236769Sobrien * addSpace True if need to add a space to the buffer 1217236769Sobrien * before adding the tail 1218236769Sobrien * buf Buffer in which to store it 1219236769Sobrien * 1220236769Sobrien * Results: 1221236769Sobrien * TRUE if characters were added to the buffer (a space needs to be 1222236769Sobrien * added to the buffer before the next word). 1223236769Sobrien * 1224236769Sobrien * Side Effects: 1225236769Sobrien * The trimmed word is added to the buffer. 1226236769Sobrien * 1227236769Sobrien *----------------------------------------------------------------------- 1228236769Sobrien */ 1229236769Sobrienstatic Boolean 1230237578SobrienVarTail(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1231236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1232321964Ssjg void *dummy MAKE_ATTR_UNUSED) 1233236769Sobrien{ 1234236769Sobrien char *slash; 1235236769Sobrien 1236236769Sobrien if (addSpace && vpstate->varSpace) { 1237236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1238236769Sobrien } 1239236769Sobrien 1240236769Sobrien slash = strrchr(word, '/'); 1241236769Sobrien if (slash != NULL) { 1242236769Sobrien *slash++ = '\0'; 1243236769Sobrien Buf_AddBytes(buf, strlen(slash), slash); 1244236769Sobrien slash[-1] = '/'; 1245236769Sobrien } else { 1246236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1247236769Sobrien } 1248321964Ssjg return TRUE; 1249236769Sobrien} 1250236769Sobrien 1251236769Sobrien/*- 1252236769Sobrien *----------------------------------------------------------------------- 1253236769Sobrien * VarSuffix -- 1254236769Sobrien * Place the suffix of the given word in the given buffer. 1255236769Sobrien * 1256236769Sobrien * Input: 1257236769Sobrien * word Word to trim 1258236769Sobrien * addSpace TRUE if need to add a space before placing the 1259236769Sobrien * suffix in the buffer 1260236769Sobrien * buf Buffer in which to store it 1261236769Sobrien * 1262236769Sobrien * Results: 1263236769Sobrien * TRUE if characters were added to the buffer (a space needs to be 1264236769Sobrien * added to the buffer before the next word). 1265236769Sobrien * 1266236769Sobrien * Side Effects: 1267236769Sobrien * The suffix from the word is placed in the buffer. 1268236769Sobrien * 1269236769Sobrien *----------------------------------------------------------------------- 1270236769Sobrien */ 1271236769Sobrienstatic Boolean 1272237578SobrienVarSuffix(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1273236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1274321964Ssjg void *dummy MAKE_ATTR_UNUSED) 1275236769Sobrien{ 1276236769Sobrien char *dot; 1277236769Sobrien 1278236769Sobrien dot = strrchr(word, '.'); 1279236769Sobrien if (dot != NULL) { 1280236769Sobrien if (addSpace && vpstate->varSpace) { 1281236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1282236769Sobrien } 1283236769Sobrien *dot++ = '\0'; 1284236769Sobrien Buf_AddBytes(buf, strlen(dot), dot); 1285236769Sobrien dot[-1] = '.'; 1286236769Sobrien addSpace = TRUE; 1287236769Sobrien } 1288321964Ssjg return addSpace; 1289236769Sobrien} 1290236769Sobrien 1291236769Sobrien/*- 1292236769Sobrien *----------------------------------------------------------------------- 1293236769Sobrien * VarRoot -- 1294236769Sobrien * Remove the suffix of the given word and place the result in the 1295236769Sobrien * buffer. 1296236769Sobrien * 1297236769Sobrien * Input: 1298236769Sobrien * word Word to trim 1299236769Sobrien * addSpace TRUE if need to add a space to the buffer 1300236769Sobrien * before placing the root in it 1301236769Sobrien * buf Buffer in which to store it 1302236769Sobrien * 1303236769Sobrien * Results: 1304236769Sobrien * TRUE if characters were added to the buffer (a space needs to be 1305236769Sobrien * added to the buffer before the next word). 1306236769Sobrien * 1307236769Sobrien * Side Effects: 1308236769Sobrien * The trimmed word is added to the buffer. 1309236769Sobrien * 1310236769Sobrien *----------------------------------------------------------------------- 1311236769Sobrien */ 1312236769Sobrienstatic Boolean 1313237578SobrienVarRoot(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1314236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1315321964Ssjg void *dummy MAKE_ATTR_UNUSED) 1316236769Sobrien{ 1317236769Sobrien char *dot; 1318236769Sobrien 1319236769Sobrien if (addSpace && vpstate->varSpace) { 1320236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1321236769Sobrien } 1322236769Sobrien 1323236769Sobrien dot = strrchr(word, '.'); 1324236769Sobrien if (dot != NULL) { 1325236769Sobrien *dot = '\0'; 1326236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1327236769Sobrien *dot = '.'; 1328236769Sobrien } else { 1329236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1330236769Sobrien } 1331321964Ssjg return TRUE; 1332236769Sobrien} 1333236769Sobrien 1334236769Sobrien/*- 1335236769Sobrien *----------------------------------------------------------------------- 1336236769Sobrien * VarMatch -- 1337236769Sobrien * Place the word in the buffer if it matches the given pattern. 1338236769Sobrien * Callback function for VarModify to implement the :M modifier. 1339236769Sobrien * 1340236769Sobrien * Input: 1341236769Sobrien * word Word to examine 1342236769Sobrien * addSpace TRUE if need to add a space to the buffer 1343236769Sobrien * before adding the word, if it matches 1344236769Sobrien * buf Buffer in which to store it 1345236769Sobrien * pattern Pattern the word must match 1346236769Sobrien * 1347236769Sobrien * Results: 1348236769Sobrien * TRUE if a space should be placed in the buffer before the next 1349236769Sobrien * word. 1350236769Sobrien * 1351236769Sobrien * Side Effects: 1352236769Sobrien * The word may be copied to the buffer. 1353236769Sobrien * 1354236769Sobrien *----------------------------------------------------------------------- 1355236769Sobrien */ 1356236769Sobrienstatic Boolean 1357237578SobrienVarMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1358236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1359236769Sobrien void *pattern) 1360236769Sobrien{ 1361236769Sobrien if (DEBUG(VAR)) 1362236769Sobrien fprintf(debug_file, "VarMatch [%s] [%s]\n", word, (char *)pattern); 1363236769Sobrien if (Str_Match(word, (char *)pattern)) { 1364236769Sobrien if (addSpace && vpstate->varSpace) { 1365236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1366236769Sobrien } 1367236769Sobrien addSpace = TRUE; 1368236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1369236769Sobrien } 1370236769Sobrien return(addSpace); 1371236769Sobrien} 1372236769Sobrien 1373236769Sobrien#ifdef SYSVVARSUB 1374236769Sobrien/*- 1375236769Sobrien *----------------------------------------------------------------------- 1376236769Sobrien * VarSYSVMatch -- 1377236769Sobrien * Place the word in the buffer if it matches the given pattern. 1378236769Sobrien * Callback function for VarModify to implement the System V % 1379236769Sobrien * modifiers. 1380236769Sobrien * 1381236769Sobrien * Input: 1382236769Sobrien * word Word to examine 1383236769Sobrien * addSpace TRUE if need to add a space to the buffer 1384236769Sobrien * before adding the word, if it matches 1385236769Sobrien * buf Buffer in which to store it 1386236769Sobrien * patp Pattern the word must match 1387236769Sobrien * 1388236769Sobrien * Results: 1389236769Sobrien * TRUE if a space should be placed in the buffer before the next 1390236769Sobrien * word. 1391236769Sobrien * 1392236769Sobrien * Side Effects: 1393236769Sobrien * The word may be copied to the buffer. 1394236769Sobrien * 1395236769Sobrien *----------------------------------------------------------------------- 1396236769Sobrien */ 1397236769Sobrienstatic Boolean 1398236769SobrienVarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate, 1399236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1400236769Sobrien void *patp) 1401236769Sobrien{ 1402236769Sobrien int len; 1403236769Sobrien char *ptr; 1404236769Sobrien VarPattern *pat = (VarPattern *)patp; 1405236769Sobrien char *varexp; 1406236769Sobrien 1407236769Sobrien if (addSpace && vpstate->varSpace) 1408236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1409236769Sobrien 1410236769Sobrien addSpace = TRUE; 1411236769Sobrien 1412236769Sobrien if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) { 1413321964Ssjg varexp = Var_Subst(NULL, pat->rhs, ctx, VARF_WANTRES); 1414236769Sobrien Str_SYSVSubst(buf, varexp, ptr, len); 1415236769Sobrien free(varexp); 1416236769Sobrien } else { 1417236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1418236769Sobrien } 1419236769Sobrien 1420236769Sobrien return(addSpace); 1421236769Sobrien} 1422236769Sobrien#endif 1423236769Sobrien 1424236769Sobrien 1425236769Sobrien/*- 1426236769Sobrien *----------------------------------------------------------------------- 1427236769Sobrien * VarNoMatch -- 1428236769Sobrien * Place the word in the buffer if it doesn't match the given pattern. 1429236769Sobrien * Callback function for VarModify to implement the :N modifier. 1430236769Sobrien * 1431236769Sobrien * Input: 1432236769Sobrien * word Word to examine 1433236769Sobrien * addSpace TRUE if need to add a space to the buffer 1434236769Sobrien * before adding the word, if it matches 1435236769Sobrien * buf Buffer in which to store it 1436236769Sobrien * pattern Pattern the word must match 1437236769Sobrien * 1438236769Sobrien * Results: 1439236769Sobrien * TRUE if a space should be placed in the buffer before the next 1440236769Sobrien * word. 1441236769Sobrien * 1442236769Sobrien * Side Effects: 1443236769Sobrien * The word may be copied to the buffer. 1444236769Sobrien * 1445236769Sobrien *----------------------------------------------------------------------- 1446236769Sobrien */ 1447236769Sobrienstatic Boolean 1448237578SobrienVarNoMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1449236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1450236769Sobrien void *pattern) 1451236769Sobrien{ 1452236769Sobrien if (!Str_Match(word, (char *)pattern)) { 1453236769Sobrien if (addSpace && vpstate->varSpace) { 1454236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1455236769Sobrien } 1456236769Sobrien addSpace = TRUE; 1457236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1458236769Sobrien } 1459236769Sobrien return(addSpace); 1460236769Sobrien} 1461236769Sobrien 1462236769Sobrien 1463236769Sobrien/*- 1464236769Sobrien *----------------------------------------------------------------------- 1465236769Sobrien * VarSubstitute -- 1466236769Sobrien * Perform a string-substitution on the given word, placing the 1467236769Sobrien * result in the passed buffer. 1468236769Sobrien * 1469236769Sobrien * Input: 1470236769Sobrien * word Word to modify 1471236769Sobrien * addSpace True if space should be added before 1472236769Sobrien * other characters 1473236769Sobrien * buf Buffer for result 1474236769Sobrien * patternp Pattern for substitution 1475236769Sobrien * 1476236769Sobrien * Results: 1477236769Sobrien * TRUE if a space is needed before more characters are added. 1478236769Sobrien * 1479236769Sobrien * Side Effects: 1480236769Sobrien * None. 1481236769Sobrien * 1482236769Sobrien *----------------------------------------------------------------------- 1483236769Sobrien */ 1484236769Sobrienstatic Boolean 1485237578SobrienVarSubstitute(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1486236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1487236769Sobrien void *patternp) 1488236769Sobrien{ 1489236769Sobrien int wordLen; /* Length of word */ 1490236769Sobrien char *cp; /* General pointer */ 1491236769Sobrien VarPattern *pattern = (VarPattern *)patternp; 1492236769Sobrien 1493236769Sobrien wordLen = strlen(word); 1494236769Sobrien if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) != 1495236769Sobrien (VAR_SUB_ONE|VAR_SUB_MATCHED)) { 1496236769Sobrien /* 1497236769Sobrien * Still substituting -- break it down into simple anchored cases 1498236769Sobrien * and if none of them fits, perform the general substitution case. 1499236769Sobrien */ 1500236769Sobrien if ((pattern->flags & VAR_MATCH_START) && 1501236769Sobrien (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) { 1502236769Sobrien /* 1503236769Sobrien * Anchored at start and beginning of word matches pattern 1504236769Sobrien */ 1505236769Sobrien if ((pattern->flags & VAR_MATCH_END) && 1506236769Sobrien (wordLen == pattern->leftLen)) { 1507236769Sobrien /* 1508236769Sobrien * Also anchored at end and matches to the end (word 1509236769Sobrien * is same length as pattern) add space and rhs only 1510236769Sobrien * if rhs is non-null. 1511236769Sobrien */ 1512236769Sobrien if (pattern->rightLen != 0) { 1513236769Sobrien if (addSpace && vpstate->varSpace) { 1514236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1515236769Sobrien } 1516236769Sobrien addSpace = TRUE; 1517236769Sobrien Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1518236769Sobrien } 1519236769Sobrien pattern->flags |= VAR_SUB_MATCHED; 1520236769Sobrien } else if (pattern->flags & VAR_MATCH_END) { 1521236769Sobrien /* 1522236769Sobrien * Doesn't match to end -- copy word wholesale 1523236769Sobrien */ 1524236769Sobrien goto nosub; 1525236769Sobrien } else { 1526236769Sobrien /* 1527236769Sobrien * Matches at start but need to copy in trailing characters 1528236769Sobrien */ 1529236769Sobrien if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){ 1530236769Sobrien if (addSpace && vpstate->varSpace) { 1531236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1532236769Sobrien } 1533236769Sobrien addSpace = TRUE; 1534236769Sobrien } 1535236769Sobrien Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1536236769Sobrien Buf_AddBytes(buf, wordLen - pattern->leftLen, 1537236769Sobrien (word + pattern->leftLen)); 1538236769Sobrien pattern->flags |= VAR_SUB_MATCHED; 1539236769Sobrien } 1540236769Sobrien } else if (pattern->flags & VAR_MATCH_START) { 1541236769Sobrien /* 1542236769Sobrien * Had to match at start of word and didn't -- copy whole word. 1543236769Sobrien */ 1544236769Sobrien goto nosub; 1545236769Sobrien } else if (pattern->flags & VAR_MATCH_END) { 1546236769Sobrien /* 1547236769Sobrien * Anchored at end, Find only place match could occur (leftLen 1548236769Sobrien * characters from the end of the word) and see if it does. Note 1549236769Sobrien * that because the $ will be left at the end of the lhs, we have 1550236769Sobrien * to use strncmp. 1551236769Sobrien */ 1552236769Sobrien cp = word + (wordLen - pattern->leftLen); 1553236769Sobrien if ((cp >= word) && 1554236769Sobrien (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) { 1555236769Sobrien /* 1556236769Sobrien * Match found. If we will place characters in the buffer, 1557236769Sobrien * add a space before hand as indicated by addSpace, then 1558236769Sobrien * stuff in the initial, unmatched part of the word followed 1559236769Sobrien * by the right-hand-side. 1560236769Sobrien */ 1561236769Sobrien if (((cp - word) + pattern->rightLen) != 0) { 1562236769Sobrien if (addSpace && vpstate->varSpace) { 1563236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1564236769Sobrien } 1565236769Sobrien addSpace = TRUE; 1566236769Sobrien } 1567236769Sobrien Buf_AddBytes(buf, cp - word, word); 1568236769Sobrien Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1569236769Sobrien pattern->flags |= VAR_SUB_MATCHED; 1570236769Sobrien } else { 1571236769Sobrien /* 1572236769Sobrien * Had to match at end and didn't. Copy entire word. 1573236769Sobrien */ 1574236769Sobrien goto nosub; 1575236769Sobrien } 1576236769Sobrien } else { 1577236769Sobrien /* 1578236769Sobrien * Pattern is unanchored: search for the pattern in the word using 1579236769Sobrien * String_FindSubstring, copying unmatched portions and the 1580236769Sobrien * right-hand-side for each match found, handling non-global 1581236769Sobrien * substitutions correctly, etc. When the loop is done, any 1582236769Sobrien * remaining part of the word (word and wordLen are adjusted 1583236769Sobrien * accordingly through the loop) is copied straight into the 1584236769Sobrien * buffer. 1585236769Sobrien * addSpace is set FALSE as soon as a space is added to the 1586236769Sobrien * buffer. 1587236769Sobrien */ 1588236769Sobrien Boolean done; 1589236769Sobrien int origSize; 1590236769Sobrien 1591236769Sobrien done = FALSE; 1592236769Sobrien origSize = Buf_Size(buf); 1593236769Sobrien while (!done) { 1594236769Sobrien cp = Str_FindSubstring(word, pattern->lhs); 1595236769Sobrien if (cp != NULL) { 1596236769Sobrien if (addSpace && (((cp - word) + pattern->rightLen) != 0)){ 1597236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1598236769Sobrien addSpace = FALSE; 1599236769Sobrien } 1600236769Sobrien Buf_AddBytes(buf, cp-word, word); 1601236769Sobrien Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1602236769Sobrien wordLen -= (cp - word) + pattern->leftLen; 1603236769Sobrien word = cp + pattern->leftLen; 1604236769Sobrien if (wordLen == 0) { 1605236769Sobrien done = TRUE; 1606236769Sobrien } 1607236769Sobrien if ((pattern->flags & VAR_SUB_GLOBAL) == 0) { 1608236769Sobrien done = TRUE; 1609236769Sobrien } 1610236769Sobrien pattern->flags |= VAR_SUB_MATCHED; 1611236769Sobrien } else { 1612236769Sobrien done = TRUE; 1613236769Sobrien } 1614236769Sobrien } 1615236769Sobrien if (wordLen != 0) { 1616236769Sobrien if (addSpace && vpstate->varSpace) { 1617236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1618236769Sobrien } 1619236769Sobrien Buf_AddBytes(buf, wordLen, word); 1620236769Sobrien } 1621236769Sobrien /* 1622236769Sobrien * If added characters to the buffer, need to add a space 1623236769Sobrien * before we add any more. If we didn't add any, just return 1624236769Sobrien * the previous value of addSpace. 1625236769Sobrien */ 1626236769Sobrien return ((Buf_Size(buf) != origSize) || addSpace); 1627236769Sobrien } 1628236769Sobrien return (addSpace); 1629236769Sobrien } 1630236769Sobrien nosub: 1631236769Sobrien if (addSpace && vpstate->varSpace) { 1632236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1633236769Sobrien } 1634236769Sobrien Buf_AddBytes(buf, wordLen, word); 1635236769Sobrien return(TRUE); 1636236769Sobrien} 1637236769Sobrien 1638236769Sobrien#ifndef NO_REGEX 1639236769Sobrien/*- 1640236769Sobrien *----------------------------------------------------------------------- 1641236769Sobrien * VarREError -- 1642236769Sobrien * Print the error caused by a regcomp or regexec call. 1643236769Sobrien * 1644236769Sobrien * Results: 1645236769Sobrien * None. 1646236769Sobrien * 1647236769Sobrien * Side Effects: 1648236769Sobrien * An error gets printed. 1649236769Sobrien * 1650236769Sobrien *----------------------------------------------------------------------- 1651236769Sobrien */ 1652236769Sobrienstatic void 1653321964SsjgVarREError(int reerr, regex_t *pat, const char *str) 1654236769Sobrien{ 1655236769Sobrien char *errbuf; 1656236769Sobrien int errlen; 1657236769Sobrien 1658321964Ssjg errlen = regerror(reerr, pat, 0, 0); 1659236769Sobrien errbuf = bmake_malloc(errlen); 1660321964Ssjg regerror(reerr, pat, errbuf, errlen); 1661236769Sobrien Error("%s: %s", str, errbuf); 1662236769Sobrien free(errbuf); 1663236769Sobrien} 1664236769Sobrien 1665236769Sobrien 1666236769Sobrien/*- 1667236769Sobrien *----------------------------------------------------------------------- 1668236769Sobrien * VarRESubstitute -- 1669236769Sobrien * Perform a regex substitution on the given word, placing the 1670236769Sobrien * result in the passed buffer. 1671236769Sobrien * 1672236769Sobrien * Results: 1673236769Sobrien * TRUE if a space is needed before more characters are added. 1674236769Sobrien * 1675236769Sobrien * Side Effects: 1676236769Sobrien * None. 1677236769Sobrien * 1678236769Sobrien *----------------------------------------------------------------------- 1679236769Sobrien */ 1680236769Sobrienstatic Boolean 1681237578SobrienVarRESubstitute(GNode *ctx MAKE_ATTR_UNUSED, 1682237578Sobrien Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 1683236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1684236769Sobrien void *patternp) 1685236769Sobrien{ 1686236769Sobrien VarREPattern *pat; 1687236769Sobrien int xrv; 1688236769Sobrien char *wp; 1689236769Sobrien char *rp; 1690236769Sobrien int added; 1691236769Sobrien int flags = 0; 1692236769Sobrien 1693236769Sobrien#define MAYBE_ADD_SPACE() \ 1694236769Sobrien if (addSpace && !added) \ 1695236769Sobrien Buf_AddByte(buf, ' '); \ 1696236769Sobrien added = 1 1697236769Sobrien 1698236769Sobrien added = 0; 1699236769Sobrien wp = word; 1700236769Sobrien pat = patternp; 1701236769Sobrien 1702236769Sobrien if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) == 1703236769Sobrien (VAR_SUB_ONE|VAR_SUB_MATCHED)) 1704236769Sobrien xrv = REG_NOMATCH; 1705236769Sobrien else { 1706236769Sobrien tryagain: 1707236769Sobrien xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags); 1708236769Sobrien } 1709236769Sobrien 1710236769Sobrien switch (xrv) { 1711236769Sobrien case 0: 1712236769Sobrien pat->flags |= VAR_SUB_MATCHED; 1713236769Sobrien if (pat->matches[0].rm_so > 0) { 1714236769Sobrien MAYBE_ADD_SPACE(); 1715236769Sobrien Buf_AddBytes(buf, pat->matches[0].rm_so, wp); 1716236769Sobrien } 1717236769Sobrien 1718236769Sobrien for (rp = pat->replace; *rp; rp++) { 1719236769Sobrien if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) { 1720236769Sobrien MAYBE_ADD_SPACE(); 1721236769Sobrien Buf_AddByte(buf,rp[1]); 1722236769Sobrien rp++; 1723236769Sobrien } 1724236769Sobrien else if ((*rp == '&') || 1725236769Sobrien ((*rp == '\\') && isdigit((unsigned char)rp[1]))) { 1726236769Sobrien int n; 1727236769Sobrien const char *subbuf; 1728236769Sobrien int sublen; 1729236769Sobrien char errstr[3]; 1730236769Sobrien 1731236769Sobrien if (*rp == '&') { 1732236769Sobrien n = 0; 1733236769Sobrien errstr[0] = '&'; 1734236769Sobrien errstr[1] = '\0'; 1735236769Sobrien } else { 1736236769Sobrien n = rp[1] - '0'; 1737236769Sobrien errstr[0] = '\\'; 1738236769Sobrien errstr[1] = rp[1]; 1739236769Sobrien errstr[2] = '\0'; 1740236769Sobrien rp++; 1741236769Sobrien } 1742236769Sobrien 1743236769Sobrien if (n > pat->nsub) { 1744236769Sobrien Error("No subexpression %s", &errstr[0]); 1745236769Sobrien subbuf = ""; 1746236769Sobrien sublen = 0; 1747236769Sobrien } else if ((pat->matches[n].rm_so == -1) && 1748236769Sobrien (pat->matches[n].rm_eo == -1)) { 1749236769Sobrien Error("No match for subexpression %s", &errstr[0]); 1750236769Sobrien subbuf = ""; 1751236769Sobrien sublen = 0; 1752236769Sobrien } else { 1753236769Sobrien subbuf = wp + pat->matches[n].rm_so; 1754236769Sobrien sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so; 1755236769Sobrien } 1756236769Sobrien 1757236769Sobrien if (sublen > 0) { 1758236769Sobrien MAYBE_ADD_SPACE(); 1759236769Sobrien Buf_AddBytes(buf, sublen, subbuf); 1760236769Sobrien } 1761236769Sobrien } else { 1762236769Sobrien MAYBE_ADD_SPACE(); 1763236769Sobrien Buf_AddByte(buf, *rp); 1764236769Sobrien } 1765236769Sobrien } 1766236769Sobrien wp += pat->matches[0].rm_eo; 1767236769Sobrien if (pat->flags & VAR_SUB_GLOBAL) { 1768236769Sobrien flags |= REG_NOTBOL; 1769236769Sobrien if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) { 1770236769Sobrien MAYBE_ADD_SPACE(); 1771236769Sobrien Buf_AddByte(buf, *wp); 1772236769Sobrien wp++; 1773236769Sobrien 1774236769Sobrien } 1775236769Sobrien if (*wp) 1776236769Sobrien goto tryagain; 1777236769Sobrien } 1778236769Sobrien if (*wp) { 1779236769Sobrien MAYBE_ADD_SPACE(); 1780236769Sobrien Buf_AddBytes(buf, strlen(wp), wp); 1781236769Sobrien } 1782236769Sobrien break; 1783236769Sobrien default: 1784236769Sobrien VarREError(xrv, &pat->re, "Unexpected regex error"); 1785236769Sobrien /* fall through */ 1786236769Sobrien case REG_NOMATCH: 1787236769Sobrien if (*wp) { 1788236769Sobrien MAYBE_ADD_SPACE(); 1789236769Sobrien Buf_AddBytes(buf,strlen(wp),wp); 1790236769Sobrien } 1791236769Sobrien break; 1792236769Sobrien } 1793236769Sobrien return(addSpace||added); 1794236769Sobrien} 1795236769Sobrien#endif 1796236769Sobrien 1797236769Sobrien 1798236769Sobrien 1799236769Sobrien/*- 1800236769Sobrien *----------------------------------------------------------------------- 1801236769Sobrien * VarLoopExpand -- 1802236769Sobrien * Implements the :@<temp>@<string>@ modifier of ODE make. 1803236769Sobrien * We set the temp variable named in pattern.lhs to word and expand 1804236769Sobrien * pattern.rhs storing the result in the passed buffer. 1805236769Sobrien * 1806236769Sobrien * Input: 1807236769Sobrien * word Word to modify 1808236769Sobrien * addSpace True if space should be added before 1809236769Sobrien * other characters 1810236769Sobrien * buf Buffer for result 1811236769Sobrien * pattern Datafor substitution 1812236769Sobrien * 1813236769Sobrien * Results: 1814236769Sobrien * TRUE if a space is needed before more characters are added. 1815236769Sobrien * 1816236769Sobrien * Side Effects: 1817236769Sobrien * None. 1818236769Sobrien * 1819236769Sobrien *----------------------------------------------------------------------- 1820236769Sobrien */ 1821236769Sobrienstatic Boolean 1822237578SobrienVarLoopExpand(GNode *ctx MAKE_ATTR_UNUSED, 1823237578Sobrien Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 1824236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1825236769Sobrien void *loopp) 1826236769Sobrien{ 1827236769Sobrien VarLoop_t *loop = (VarLoop_t *)loopp; 1828236769Sobrien char *s; 1829236769Sobrien int slen; 1830236769Sobrien 1831236769Sobrien if (word && *word) { 1832236769Sobrien Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT); 1833321964Ssjg s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum | VARF_WANTRES); 1834236769Sobrien if (s != NULL && *s != '\0') { 1835236769Sobrien if (addSpace && *s != '\n') 1836236769Sobrien Buf_AddByte(buf, ' '); 1837236769Sobrien Buf_AddBytes(buf, (slen = strlen(s)), s); 1838236769Sobrien addSpace = (slen > 0 && s[slen - 1] != '\n'); 1839236769Sobrien } 1840321964Ssjg free(s); 1841236769Sobrien } 1842236769Sobrien return addSpace; 1843236769Sobrien} 1844236769Sobrien 1845236769Sobrien 1846236769Sobrien/*- 1847236769Sobrien *----------------------------------------------------------------------- 1848236769Sobrien * VarSelectWords -- 1849236769Sobrien * Implements the :[start..end] modifier. 1850236769Sobrien * This is a special case of VarModify since we want to be able 1851236769Sobrien * to scan the list backwards if start > end. 1852236769Sobrien * 1853236769Sobrien * Input: 1854236769Sobrien * str String whose words should be trimmed 1855236769Sobrien * seldata words to select 1856236769Sobrien * 1857236769Sobrien * Results: 1858236769Sobrien * A string of all the words selected. 1859236769Sobrien * 1860236769Sobrien * Side Effects: 1861236769Sobrien * None. 1862236769Sobrien * 1863236769Sobrien *----------------------------------------------------------------------- 1864236769Sobrien */ 1865236769Sobrienstatic char * 1866237578SobrienVarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1867236769Sobrien const char *str, VarSelectWords_t *seldata) 1868236769Sobrien{ 1869236769Sobrien Buffer buf; /* Buffer for the new string */ 1870236769Sobrien Boolean addSpace; /* TRUE if need to add a space to the 1871236769Sobrien * buffer before adding the trimmed 1872236769Sobrien * word */ 1873236769Sobrien char **av; /* word list */ 1874236769Sobrien char *as; /* word list memory */ 1875236769Sobrien int ac, i; 1876236769Sobrien int start, end, step; 1877236769Sobrien 1878236769Sobrien Buf_Init(&buf, 0); 1879236769Sobrien addSpace = FALSE; 1880236769Sobrien 1881236769Sobrien if (vpstate->oneBigWord) { 1882236769Sobrien /* fake what brk_string() would do if there were only one word */ 1883236769Sobrien ac = 1; 1884236769Sobrien av = bmake_malloc((ac + 1) * sizeof(char *)); 1885236769Sobrien as = bmake_strdup(str); 1886236769Sobrien av[0] = as; 1887236769Sobrien av[1] = NULL; 1888236769Sobrien } else { 1889236769Sobrien av = brk_string(str, &ac, FALSE, &as); 1890236769Sobrien } 1891236769Sobrien 1892236769Sobrien /* 1893236769Sobrien * Now sanitize seldata. 1894236769Sobrien * If seldata->start or seldata->end are negative, convert them to 1895236769Sobrien * the positive equivalents (-1 gets converted to argc, -2 gets 1896236769Sobrien * converted to (argc-1), etc.). 1897236769Sobrien */ 1898236769Sobrien if (seldata->start < 0) 1899236769Sobrien seldata->start = ac + seldata->start + 1; 1900236769Sobrien if (seldata->end < 0) 1901236769Sobrien seldata->end = ac + seldata->end + 1; 1902236769Sobrien 1903236769Sobrien /* 1904236769Sobrien * We avoid scanning more of the list than we need to. 1905236769Sobrien */ 1906236769Sobrien if (seldata->start > seldata->end) { 1907236769Sobrien start = MIN(ac, seldata->start) - 1; 1908236769Sobrien end = MAX(0, seldata->end - 1); 1909236769Sobrien step = -1; 1910236769Sobrien } else { 1911236769Sobrien start = MAX(0, seldata->start - 1); 1912236769Sobrien end = MIN(ac, seldata->end); 1913236769Sobrien step = 1; 1914236769Sobrien } 1915236769Sobrien 1916236769Sobrien for (i = start; 1917236769Sobrien (step < 0 && i >= end) || (step > 0 && i < end); 1918236769Sobrien i += step) { 1919236769Sobrien if (av[i] && *av[i]) { 1920236769Sobrien if (addSpace && vpstate->varSpace) { 1921236769Sobrien Buf_AddByte(&buf, vpstate->varSpace); 1922236769Sobrien } 1923236769Sobrien Buf_AddBytes(&buf, strlen(av[i]), av[i]); 1924236769Sobrien addSpace = TRUE; 1925236769Sobrien } 1926236769Sobrien } 1927236769Sobrien 1928236769Sobrien free(as); 1929236769Sobrien free(av); 1930236769Sobrien 1931236769Sobrien return Buf_Destroy(&buf, FALSE); 1932236769Sobrien} 1933236769Sobrien 1934236769Sobrien 1935236769Sobrien/*- 1936236769Sobrien * VarRealpath -- 1937236769Sobrien * Replace each word with the result of realpath() 1938236769Sobrien * if successful. 1939236769Sobrien */ 1940236769Sobrienstatic Boolean 1941237578SobrienVarRealpath(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1942236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1943237578Sobrien void *patternp MAKE_ATTR_UNUSED) 1944236769Sobrien{ 1945236769Sobrien struct stat st; 1946236769Sobrien char rbuf[MAXPATHLEN]; 1947236769Sobrien char *rp; 1948236769Sobrien 1949236769Sobrien if (addSpace && vpstate->varSpace) { 1950236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1951236769Sobrien } 1952236769Sobrien addSpace = TRUE; 1953321964Ssjg rp = cached_realpath(word, rbuf); 1954236769Sobrien if (rp && *rp == '/' && stat(rp, &st) == 0) 1955236769Sobrien word = rp; 1956236769Sobrien 1957236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1958236769Sobrien return(addSpace); 1959236769Sobrien} 1960236769Sobrien 1961236769Sobrien/*- 1962236769Sobrien *----------------------------------------------------------------------- 1963236769Sobrien * VarModify -- 1964236769Sobrien * Modify each of the words of the passed string using the given 1965236769Sobrien * function. Used to implement all modifiers. 1966236769Sobrien * 1967236769Sobrien * Input: 1968236769Sobrien * str String whose words should be trimmed 1969236769Sobrien * modProc Function to use to modify them 1970236769Sobrien * datum Datum to pass it 1971236769Sobrien * 1972236769Sobrien * Results: 1973236769Sobrien * A string of all the words modified appropriately. 1974236769Sobrien * 1975236769Sobrien * Side Effects: 1976236769Sobrien * None. 1977236769Sobrien * 1978236769Sobrien *----------------------------------------------------------------------- 1979236769Sobrien */ 1980236769Sobrienstatic char * 1981236769SobrienVarModify(GNode *ctx, Var_Parse_State *vpstate, 1982236769Sobrien const char *str, 1983236769Sobrien Boolean (*modProc)(GNode *, Var_Parse_State *, char *, 1984236769Sobrien Boolean, Buffer *, void *), 1985236769Sobrien void *datum) 1986236769Sobrien{ 1987236769Sobrien Buffer buf; /* Buffer for the new string */ 1988236769Sobrien Boolean addSpace; /* TRUE if need to add a space to the 1989236769Sobrien * buffer before adding the trimmed 1990236769Sobrien * word */ 1991236769Sobrien char **av; /* word list */ 1992236769Sobrien char *as; /* word list memory */ 1993236769Sobrien int ac, i; 1994236769Sobrien 1995236769Sobrien Buf_Init(&buf, 0); 1996236769Sobrien addSpace = FALSE; 1997236769Sobrien 1998236769Sobrien if (vpstate->oneBigWord) { 1999236769Sobrien /* fake what brk_string() would do if there were only one word */ 2000236769Sobrien ac = 1; 2001236769Sobrien av = bmake_malloc((ac + 1) * sizeof(char *)); 2002236769Sobrien as = bmake_strdup(str); 2003236769Sobrien av[0] = as; 2004236769Sobrien av[1] = NULL; 2005236769Sobrien } else { 2006236769Sobrien av = brk_string(str, &ac, FALSE, &as); 2007236769Sobrien } 2008236769Sobrien 2009236769Sobrien for (i = 0; i < ac; i++) { 2010236769Sobrien addSpace = (*modProc)(ctx, vpstate, av[i], addSpace, &buf, datum); 2011236769Sobrien } 2012236769Sobrien 2013236769Sobrien free(as); 2014236769Sobrien free(av); 2015236769Sobrien 2016236769Sobrien return Buf_Destroy(&buf, FALSE); 2017236769Sobrien} 2018236769Sobrien 2019236769Sobrien 2020236769Sobrienstatic int 2021236769SobrienVarWordCompare(const void *a, const void *b) 2022236769Sobrien{ 2023236769Sobrien int r = strcmp(*(const char * const *)a, *(const char * const *)b); 2024236769Sobrien return r; 2025236769Sobrien} 2026236769Sobrien 2027236769Sobrien/*- 2028236769Sobrien *----------------------------------------------------------------------- 2029236769Sobrien * VarOrder -- 2030236769Sobrien * Order the words in the string. 2031236769Sobrien * 2032236769Sobrien * Input: 2033236769Sobrien * str String whose words should be sorted. 2034236769Sobrien * otype How to order: s - sort, x - random. 2035236769Sobrien * 2036236769Sobrien * Results: 2037236769Sobrien * A string containing the words ordered. 2038236769Sobrien * 2039236769Sobrien * Side Effects: 2040236769Sobrien * None. 2041236769Sobrien * 2042236769Sobrien *----------------------------------------------------------------------- 2043236769Sobrien */ 2044236769Sobrienstatic char * 2045236769SobrienVarOrder(const char *str, const char otype) 2046236769Sobrien{ 2047236769Sobrien Buffer buf; /* Buffer for the new string */ 2048236769Sobrien char **av; /* word list [first word does not count] */ 2049236769Sobrien char *as; /* word list memory */ 2050236769Sobrien int ac, i; 2051236769Sobrien 2052236769Sobrien Buf_Init(&buf, 0); 2053236769Sobrien 2054236769Sobrien av = brk_string(str, &ac, FALSE, &as); 2055236769Sobrien 2056236769Sobrien if (ac > 0) 2057236769Sobrien switch (otype) { 2058236769Sobrien case 's': /* sort alphabetically */ 2059236769Sobrien qsort(av, ac, sizeof(char *), VarWordCompare); 2060236769Sobrien break; 2061236769Sobrien case 'x': /* randomize */ 2062236769Sobrien { 2063236769Sobrien int rndidx; 2064236769Sobrien char *t; 2065236769Sobrien 2066236769Sobrien /* 2067236769Sobrien * We will use [ac..2] range for mod factors. This will produce 2068236769Sobrien * random numbers in [(ac-1)..0] interval, and minimal 2069236769Sobrien * reasonable value for mod factor is 2 (the mod 1 will produce 2070236769Sobrien * 0 with probability 1). 2071236769Sobrien */ 2072236769Sobrien for (i = ac-1; i > 0; i--) { 2073236769Sobrien rndidx = random() % (i + 1); 2074236769Sobrien if (i != rndidx) { 2075236769Sobrien t = av[i]; 2076236769Sobrien av[i] = av[rndidx]; 2077236769Sobrien av[rndidx] = t; 2078236769Sobrien } 2079236769Sobrien } 2080236769Sobrien } 2081236769Sobrien } /* end of switch */ 2082236769Sobrien 2083236769Sobrien for (i = 0; i < ac; i++) { 2084236769Sobrien Buf_AddBytes(&buf, strlen(av[i]), av[i]); 2085236769Sobrien if (i != ac - 1) 2086236769Sobrien Buf_AddByte(&buf, ' '); 2087236769Sobrien } 2088236769Sobrien 2089236769Sobrien free(as); 2090236769Sobrien free(av); 2091236769Sobrien 2092236769Sobrien return Buf_Destroy(&buf, FALSE); 2093236769Sobrien} 2094236769Sobrien 2095236769Sobrien 2096236769Sobrien/*- 2097236769Sobrien *----------------------------------------------------------------------- 2098236769Sobrien * VarUniq -- 2099236769Sobrien * Remove adjacent duplicate words. 2100236769Sobrien * 2101236769Sobrien * Input: 2102236769Sobrien * str String whose words should be sorted 2103236769Sobrien * 2104236769Sobrien * Results: 2105236769Sobrien * A string containing the resulting words. 2106236769Sobrien * 2107236769Sobrien * Side Effects: 2108236769Sobrien * None. 2109236769Sobrien * 2110236769Sobrien *----------------------------------------------------------------------- 2111236769Sobrien */ 2112236769Sobrienstatic char * 2113236769SobrienVarUniq(const char *str) 2114236769Sobrien{ 2115236769Sobrien Buffer buf; /* Buffer for new string */ 2116236769Sobrien char **av; /* List of words to affect */ 2117236769Sobrien char *as; /* Word list memory */ 2118236769Sobrien int ac, i, j; 2119236769Sobrien 2120236769Sobrien Buf_Init(&buf, 0); 2121236769Sobrien av = brk_string(str, &ac, FALSE, &as); 2122236769Sobrien 2123236769Sobrien if (ac > 1) { 2124236769Sobrien for (j = 0, i = 1; i < ac; i++) 2125236769Sobrien if (strcmp(av[i], av[j]) != 0 && (++j != i)) 2126236769Sobrien av[j] = av[i]; 2127236769Sobrien ac = j + 1; 2128236769Sobrien } 2129236769Sobrien 2130236769Sobrien for (i = 0; i < ac; i++) { 2131236769Sobrien Buf_AddBytes(&buf, strlen(av[i]), av[i]); 2132236769Sobrien if (i != ac - 1) 2133236769Sobrien Buf_AddByte(&buf, ' '); 2134236769Sobrien } 2135236769Sobrien 2136236769Sobrien free(as); 2137236769Sobrien free(av); 2138236769Sobrien 2139236769Sobrien return Buf_Destroy(&buf, FALSE); 2140236769Sobrien} 2141236769Sobrien 2142321964Ssjg/*- 2143321964Ssjg *----------------------------------------------------------------------- 2144321964Ssjg * VarRange -- 2145321964Ssjg * Return an integer sequence 2146321964Ssjg * 2147321964Ssjg * Input: 2148321964Ssjg * str String whose words provide default range 2149321964Ssjg * ac range length, if 0 use str words 2150321964Ssjg * 2151321964Ssjg * Side Effects: 2152321964Ssjg * None. 2153321964Ssjg * 2154321964Ssjg *----------------------------------------------------------------------- 2155321964Ssjg */ 2156321964Ssjgstatic char * 2157321964SsjgVarRange(const char *str, int ac) 2158321964Ssjg{ 2159321964Ssjg Buffer buf; /* Buffer for new string */ 2160321964Ssjg char tmp[32]; /* each element */ 2161321964Ssjg char **av; /* List of words to affect */ 2162321964Ssjg char *as; /* Word list memory */ 2163321964Ssjg int i, n; 2164236769Sobrien 2165321964Ssjg Buf_Init(&buf, 0); 2166321964Ssjg if (ac > 0) { 2167321964Ssjg as = NULL; 2168321964Ssjg av = NULL; 2169321964Ssjg } else { 2170321964Ssjg av = brk_string(str, &ac, FALSE, &as); 2171321964Ssjg } 2172321964Ssjg for (i = 0; i < ac; i++) { 2173321964Ssjg n = snprintf(tmp, sizeof(tmp), "%d", 1 + i); 2174321964Ssjg if (n >= (int)sizeof(tmp)) 2175321964Ssjg break; 2176321964Ssjg Buf_AddBytes(&buf, n, tmp); 2177321964Ssjg if (i != ac - 1) 2178321964Ssjg Buf_AddByte(&buf, ' '); 2179321964Ssjg } 2180321964Ssjg 2181321964Ssjg free(as); 2182321964Ssjg free(av); 2183321964Ssjg 2184321964Ssjg return Buf_Destroy(&buf, FALSE); 2185321964Ssjg} 2186321964Ssjg 2187321964Ssjg 2188236769Sobrien/*- 2189236769Sobrien *----------------------------------------------------------------------- 2190236769Sobrien * VarGetPattern -- 2191236769Sobrien * Pass through the tstr looking for 1) escaped delimiters, 2192236769Sobrien * '$'s and backslashes (place the escaped character in 2193236769Sobrien * uninterpreted) and 2) unescaped $'s that aren't before 2194236769Sobrien * the delimiter (expand the variable substitution unless flags 2195236769Sobrien * has VAR_NOSUBST set). 2196236769Sobrien * Return the expanded string or NULL if the delimiter was missing 2197236769Sobrien * If pattern is specified, handle escaped ampersands, and replace 2198236769Sobrien * unescaped ampersands with the lhs of the pattern. 2199236769Sobrien * 2200236769Sobrien * Results: 2201236769Sobrien * A string of all the words modified appropriately. 2202236769Sobrien * If length is specified, return the string length of the buffer 2203236769Sobrien * If flags is specified and the last character of the pattern is a 2204236769Sobrien * $ set the VAR_MATCH_END bit of flags. 2205236769Sobrien * 2206236769Sobrien * Side Effects: 2207236769Sobrien * None. 2208236769Sobrien *----------------------------------------------------------------------- 2209236769Sobrien */ 2210236769Sobrienstatic char * 2211237578SobrienVarGetPattern(GNode *ctxt, Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 2212321964Ssjg int flags, const char **tstr, int delim, int *vflags, 2213236769Sobrien int *length, VarPattern *pattern) 2214236769Sobrien{ 2215236769Sobrien const char *cp; 2216236769Sobrien char *rstr; 2217236769Sobrien Buffer buf; 2218236769Sobrien int junk; 2219321964Ssjg int errnum = flags & VARF_UNDEFERR; 2220236769Sobrien 2221236769Sobrien Buf_Init(&buf, 0); 2222236769Sobrien if (length == NULL) 2223236769Sobrien length = &junk; 2224236769Sobrien 2225236769Sobrien#define IS_A_MATCH(cp, delim) \ 2226236769Sobrien ((cp[0] == '\\') && ((cp[1] == delim) || \ 2227236769Sobrien (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&')))) 2228236769Sobrien 2229236769Sobrien /* 2230236769Sobrien * Skim through until the matching delimiter is found; 2231236769Sobrien * pick up variable substitutions on the way. Also allow 2232236769Sobrien * backslashes to quote the delimiter, $, and \, but don't 2233236769Sobrien * touch other backslashes. 2234236769Sobrien */ 2235236769Sobrien for (cp = *tstr; *cp && (*cp != delim); cp++) { 2236236769Sobrien if (IS_A_MATCH(cp, delim)) { 2237236769Sobrien Buf_AddByte(&buf, cp[1]); 2238236769Sobrien cp++; 2239236769Sobrien } else if (*cp == '$') { 2240236769Sobrien if (cp[1] == delim) { 2241321964Ssjg if (vflags == NULL) 2242236769Sobrien Buf_AddByte(&buf, *cp); 2243236769Sobrien else 2244236769Sobrien /* 2245236769Sobrien * Unescaped $ at end of pattern => anchor 2246236769Sobrien * pattern at end. 2247236769Sobrien */ 2248321964Ssjg *vflags |= VAR_MATCH_END; 2249236769Sobrien } else { 2250321964Ssjg if (vflags == NULL || (*vflags & VAR_NOSUBST) == 0) { 2251236769Sobrien char *cp2; 2252236769Sobrien int len; 2253236769Sobrien void *freeIt; 2254236769Sobrien 2255236769Sobrien /* 2256236769Sobrien * If unescaped dollar sign not before the 2257236769Sobrien * delimiter, assume it's a variable 2258236769Sobrien * substitution and recurse. 2259236769Sobrien */ 2260321964Ssjg cp2 = Var_Parse(cp, ctxt, errnum | VARF_WANTRES, &len, 2261321964Ssjg &freeIt); 2262236769Sobrien Buf_AddBytes(&buf, strlen(cp2), cp2); 2263292068Ssjg free(freeIt); 2264236769Sobrien cp += len - 1; 2265236769Sobrien } else { 2266236769Sobrien const char *cp2 = &cp[1]; 2267236769Sobrien 2268236769Sobrien if (*cp2 == PROPEN || *cp2 == BROPEN) { 2269236769Sobrien /* 2270236769Sobrien * Find the end of this variable reference 2271236769Sobrien * and suck it in without further ado. 2272236769Sobrien * It will be interperated later. 2273236769Sobrien */ 2274236769Sobrien int have = *cp2; 2275236769Sobrien int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE; 2276236769Sobrien int depth = 1; 2277236769Sobrien 2278236769Sobrien for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) { 2279236769Sobrien if (cp2[-1] != '\\') { 2280236769Sobrien if (*cp2 == have) 2281236769Sobrien ++depth; 2282236769Sobrien if (*cp2 == want) 2283236769Sobrien --depth; 2284236769Sobrien } 2285236769Sobrien } 2286236769Sobrien Buf_AddBytes(&buf, cp2 - cp, cp); 2287236769Sobrien cp = --cp2; 2288236769Sobrien } else 2289236769Sobrien Buf_AddByte(&buf, *cp); 2290236769Sobrien } 2291236769Sobrien } 2292236769Sobrien } 2293236769Sobrien else if (pattern && *cp == '&') 2294236769Sobrien Buf_AddBytes(&buf, pattern->leftLen, pattern->lhs); 2295236769Sobrien else 2296236769Sobrien Buf_AddByte(&buf, *cp); 2297236769Sobrien } 2298236769Sobrien 2299236769Sobrien if (*cp != delim) { 2300236769Sobrien *tstr = cp; 2301236769Sobrien *length = 0; 2302236769Sobrien return NULL; 2303236769Sobrien } 2304236769Sobrien 2305236769Sobrien *tstr = ++cp; 2306236769Sobrien *length = Buf_Size(&buf); 2307236769Sobrien rstr = Buf_Destroy(&buf, FALSE); 2308236769Sobrien if (DEBUG(VAR)) 2309236769Sobrien fprintf(debug_file, "Modifier pattern: \"%s\"\n", rstr); 2310236769Sobrien return rstr; 2311236769Sobrien} 2312236769Sobrien 2313236769Sobrien/*- 2314236769Sobrien *----------------------------------------------------------------------- 2315236769Sobrien * VarQuote -- 2316292068Ssjg * Quote shell meta-characters and space characters in the string 2317236769Sobrien * 2318236769Sobrien * Results: 2319236769Sobrien * The quoted string 2320236769Sobrien * 2321236769Sobrien * Side Effects: 2322236769Sobrien * None. 2323236769Sobrien * 2324236769Sobrien *----------------------------------------------------------------------- 2325236769Sobrien */ 2326236769Sobrienstatic char * 2327236769SobrienVarQuote(char *str) 2328236769Sobrien{ 2329236769Sobrien 2330236769Sobrien Buffer buf; 2331236769Sobrien const char *newline; 2332292068Ssjg size_t nlen; 2333236769Sobrien 2334236769Sobrien if ((newline = Shell_GetNewline()) == NULL) 2335236769Sobrien newline = "\\\n"; 2336236769Sobrien nlen = strlen(newline); 2337236769Sobrien 2338236769Sobrien Buf_Init(&buf, 0); 2339292068Ssjg 2340292068Ssjg for (; *str != '\0'; str++) { 2341292068Ssjg if (*str == '\n') { 2342236769Sobrien Buf_AddBytes(&buf, nlen, newline); 2343292068Ssjg continue; 2344292068Ssjg } 2345292068Ssjg if (isspace((unsigned char)*str) || ismeta((unsigned char)*str)) 2346236769Sobrien Buf_AddByte(&buf, '\\'); 2347292068Ssjg Buf_AddByte(&buf, *str); 2348236769Sobrien } 2349292068Ssjg 2350236769Sobrien str = Buf_Destroy(&buf, FALSE); 2351236769Sobrien if (DEBUG(VAR)) 2352236769Sobrien fprintf(debug_file, "QuoteMeta: [%s]\n", str); 2353236769Sobrien return str; 2354236769Sobrien} 2355236769Sobrien 2356236769Sobrien/*- 2357236769Sobrien *----------------------------------------------------------------------- 2358236769Sobrien * VarHash -- 2359236769Sobrien * Hash the string using the MurmurHash3 algorithm. 2360236769Sobrien * Output is computed using 32bit Little Endian arithmetic. 2361236769Sobrien * 2362236769Sobrien * Input: 2363236769Sobrien * str String to modify 2364236769Sobrien * 2365236769Sobrien * Results: 2366236769Sobrien * Hash value of str, encoded as 8 hex digits. 2367236769Sobrien * 2368236769Sobrien * Side Effects: 2369236769Sobrien * None. 2370236769Sobrien * 2371236769Sobrien *----------------------------------------------------------------------- 2372236769Sobrien */ 2373236769Sobrienstatic char * 2374236769SobrienVarHash(char *str) 2375236769Sobrien{ 2376236769Sobrien static const char hexdigits[16] = "0123456789abcdef"; 2377236769Sobrien Buffer buf; 2378236769Sobrien size_t len, len2; 2379236769Sobrien unsigned char *ustr = (unsigned char *)str; 2380292068Ssjg unsigned int h, k, c1, c2; 2381236769Sobrien 2382236769Sobrien h = 0x971e137bU; 2383236769Sobrien c1 = 0x95543787U; 2384236769Sobrien c2 = 0x2ad7eb25U; 2385236769Sobrien len2 = strlen(str); 2386236769Sobrien 2387236769Sobrien for (len = len2; len; ) { 2388236769Sobrien k = 0; 2389236769Sobrien switch (len) { 2390236769Sobrien default: 2391236769Sobrien k = (ustr[3] << 24) | (ustr[2] << 16) | (ustr[1] << 8) | ustr[0]; 2392236769Sobrien len -= 4; 2393236769Sobrien ustr += 4; 2394236769Sobrien break; 2395236769Sobrien case 3: 2396236769Sobrien k |= (ustr[2] << 16); 2397236769Sobrien case 2: 2398236769Sobrien k |= (ustr[1] << 8); 2399236769Sobrien case 1: 2400236769Sobrien k |= ustr[0]; 2401236769Sobrien len = 0; 2402236769Sobrien } 2403236769Sobrien c1 = c1 * 5 + 0x7b7d159cU; 2404236769Sobrien c2 = c2 * 5 + 0x6bce6396U; 2405236769Sobrien k *= c1; 2406236769Sobrien k = (k << 11) ^ (k >> 21); 2407236769Sobrien k *= c2; 2408236769Sobrien h = (h << 13) ^ (h >> 19); 2409236769Sobrien h = h * 5 + 0x52dce729U; 2410236769Sobrien h ^= k; 2411251422Ssjg } 2412236769Sobrien h ^= len2; 2413236769Sobrien h *= 0x85ebca6b; 2414236769Sobrien h ^= h >> 13; 2415236769Sobrien h *= 0xc2b2ae35; 2416236769Sobrien h ^= h >> 16; 2417236769Sobrien 2418236769Sobrien Buf_Init(&buf, 0); 2419236769Sobrien for (len = 0; len < 8; ++len) { 2420236769Sobrien Buf_AddByte(&buf, hexdigits[h & 15]); 2421236769Sobrien h >>= 4; 2422236769Sobrien } 2423236769Sobrien 2424236769Sobrien return Buf_Destroy(&buf, FALSE); 2425236769Sobrien} 2426236769Sobrien 2427236769Sobrienstatic char * 2428321964SsjgVarStrftime(const char *fmt, int zulu, time_t utc) 2429236769Sobrien{ 2430236769Sobrien char buf[BUFSIZ]; 2431236769Sobrien 2432321964Ssjg if (!utc) 2433321964Ssjg time(&utc); 2434236769Sobrien if (!*fmt) 2435236769Sobrien fmt = "%c"; 2436236769Sobrien strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc)); 2437236769Sobrien 2438236769Sobrien buf[sizeof(buf) - 1] = '\0'; 2439236769Sobrien return bmake_strdup(buf); 2440236769Sobrien} 2441236769Sobrien 2442236769Sobrien/* 2443236769Sobrien * Now we need to apply any modifiers the user wants applied. 2444236769Sobrien * These are: 2445236769Sobrien * :M<pattern> words which match the given <pattern>. 2446236769Sobrien * <pattern> is of the standard file 2447236769Sobrien * wildcarding form. 2448236769Sobrien * :N<pattern> words which do not match the given <pattern>. 2449236769Sobrien * :S<d><pat1><d><pat2><d>[1gW] 2450236769Sobrien * Substitute <pat2> for <pat1> in the value 2451236769Sobrien * :C<d><pat1><d><pat2><d>[1gW] 2452236769Sobrien * Substitute <pat2> for regex <pat1> in the value 2453236769Sobrien * :H Substitute the head of each word 2454236769Sobrien * :T Substitute the tail of each word 2455236769Sobrien * :E Substitute the extension (minus '.') of 2456236769Sobrien * each word 2457236769Sobrien * :R Substitute the root of each word 2458236769Sobrien * (pathname minus the suffix). 2459236769Sobrien * :O ("Order") Alphabeticaly sort words in variable. 2460236769Sobrien * :Ox ("intermiX") Randomize words in variable. 2461236769Sobrien * :u ("uniq") Remove adjacent duplicate words. 2462236769Sobrien * :tu Converts the variable contents to uppercase. 2463236769Sobrien * :tl Converts the variable contents to lowercase. 2464236769Sobrien * :ts[c] Sets varSpace - the char used to 2465236769Sobrien * separate words to 'c'. If 'c' is 2466236769Sobrien * omitted then no separation is used. 2467236769Sobrien * :tW Treat the variable contents as a single 2468236769Sobrien * word, even if it contains spaces. 2469236769Sobrien * (Mnemonic: one big 'W'ord.) 2470236769Sobrien * :tw Treat the variable contents as multiple 2471236769Sobrien * space-separated words. 2472236769Sobrien * (Mnemonic: many small 'w'ords.) 2473236769Sobrien * :[index] Select a single word from the value. 2474236769Sobrien * :[start..end] Select multiple words from the value. 2475236769Sobrien * :[*] or :[0] Select the entire value, as a single 2476236769Sobrien * word. Equivalent to :tW. 2477236769Sobrien * :[@] Select the entire value, as multiple 2478236769Sobrien * words. Undoes the effect of :[*]. 2479236769Sobrien * Equivalent to :tw. 2480236769Sobrien * :[#] Returns the number of words in the value. 2481236769Sobrien * 2482236769Sobrien * :?<true-value>:<false-value> 2483236769Sobrien * If the variable evaluates to true, return 2484236769Sobrien * true value, else return the second value. 2485236769Sobrien * :lhs=rhs Like :S, but the rhs goes to the end of 2486236769Sobrien * the invocation. 2487236769Sobrien * :sh Treat the current value as a command 2488236769Sobrien * to be run, new value is its output. 2489236769Sobrien * The following added so we can handle ODE makefiles. 2490236769Sobrien * :@<tmpvar>@<newval>@ 2491236769Sobrien * Assign a temporary local variable <tmpvar> 2492236769Sobrien * to the current value of each word in turn 2493236769Sobrien * and replace each word with the result of 2494236769Sobrien * evaluating <newval> 2495236769Sobrien * :D<newval> Use <newval> as value if variable defined 2496236769Sobrien * :U<newval> Use <newval> as value if variable undefined 2497236769Sobrien * :L Use the name of the variable as the value. 2498236769Sobrien * :P Use the path of the node that has the same 2499236769Sobrien * name as the variable as the value. This 2500236769Sobrien * basically includes an implied :L so that 2501236769Sobrien * the common method of refering to the path 2502236769Sobrien * of your dependent 'x' in a rule is to use 2503236769Sobrien * the form '${x:P}'. 2504236769Sobrien * :!<cmd>! Run cmd much the same as :sh run's the 2505236769Sobrien * current value of the variable. 2506236769Sobrien * The ::= modifiers, actually assign a value to the variable. 2507236769Sobrien * Their main purpose is in supporting modifiers of .for loop 2508236769Sobrien * iterators and other obscure uses. They always expand to 2509236769Sobrien * nothing. In a target rule that would otherwise expand to an 2510236769Sobrien * empty line they can be preceded with @: to keep make happy. 2511236769Sobrien * Eg. 2512236769Sobrien * 2513236769Sobrien * foo: .USE 2514236769Sobrien * .for i in ${.TARGET} ${.TARGET:R}.gz 2515236769Sobrien * @: ${t::=$i} 2516236769Sobrien * @echo blah ${t:T} 2517236769Sobrien * .endfor 2518236769Sobrien * 2519236769Sobrien * ::=<str> Assigns <str> as the new value of variable. 2520236769Sobrien * ::?=<str> Assigns <str> as value of variable if 2521236769Sobrien * it was not already set. 2522236769Sobrien * ::+=<str> Appends <str> to variable. 2523236769Sobrien * ::!=<cmd> Assigns output of <cmd> as the new value of 2524236769Sobrien * variable. 2525236769Sobrien */ 2526236769Sobrien 2527236769Sobrien/* we now have some modifiers with long names */ 2528236769Sobrien#define STRMOD_MATCH(s, want, n) \ 2529236769Sobrien (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':')) 2530321964Ssjg#define STRMOD_MATCHX(s, want, n) \ 2531321964Ssjg (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':' || s[n] == '=')) 2532321964Ssjg#define CHARMOD_MATCH(c) (c == endc || c == ':') 2533236769Sobrien 2534236769Sobrienstatic char * 2535236769SobrienApplyModifiers(char *nstr, const char *tstr, 2536236769Sobrien int startc, int endc, 2537321964Ssjg Var *v, GNode *ctxt, int flags, 2538236769Sobrien int *lengthPtr, void **freePtr) 2539236769Sobrien{ 2540236769Sobrien const char *start; 2541236769Sobrien const char *cp; /* Secondary pointer into str (place marker 2542236769Sobrien * for tstr) */ 2543236769Sobrien char *newStr; /* New value to return */ 2544321964Ssjg char *ep; 2545236769Sobrien char termc; /* Character which terminated scan */ 2546236769Sobrien int cnt; /* Used to count brace pairs when variable in 2547236769Sobrien * in parens or braces */ 2548236769Sobrien char delim; 2549236769Sobrien int modifier; /* that we are processing */ 2550236769Sobrien Var_Parse_State parsestate; /* Flags passed to helper functions */ 2551321964Ssjg time_t utc; /* for VarStrftime */ 2552236769Sobrien 2553236769Sobrien delim = '\0'; 2554236769Sobrien parsestate.oneBigWord = FALSE; 2555236769Sobrien parsestate.varSpace = ' '; /* word separator */ 2556236769Sobrien 2557236769Sobrien start = cp = tstr; 2558236769Sobrien 2559236769Sobrien while (*tstr && *tstr != endc) { 2560236769Sobrien 2561236769Sobrien if (*tstr == '$') { 2562236769Sobrien /* 2563236769Sobrien * We may have some complex modifiers in a variable. 2564236769Sobrien */ 2565236769Sobrien void *freeIt; 2566236769Sobrien char *rval; 2567236769Sobrien int rlen; 2568236769Sobrien int c; 2569236769Sobrien 2570321964Ssjg rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt); 2571236769Sobrien 2572236769Sobrien /* 2573236769Sobrien * If we have not parsed up to endc or ':', 2574236769Sobrien * we are not interested. 2575236769Sobrien */ 2576236769Sobrien if (rval != NULL && *rval && 2577236769Sobrien (c = tstr[rlen]) != '\0' && 2578236769Sobrien c != ':' && 2579236769Sobrien c != endc) { 2580292068Ssjg free(freeIt); 2581236769Sobrien goto apply_mods; 2582236769Sobrien } 2583236769Sobrien 2584236769Sobrien if (DEBUG(VAR)) { 2585236769Sobrien fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n", 2586236769Sobrien rval, rlen, tstr, rlen, tstr + rlen); 2587236769Sobrien } 2588236769Sobrien 2589236769Sobrien tstr += rlen; 2590236769Sobrien 2591236769Sobrien if (rval != NULL && *rval) { 2592236769Sobrien int used; 2593236769Sobrien 2594236769Sobrien nstr = ApplyModifiers(nstr, rval, 2595321964Ssjg 0, 0, v, ctxt, flags, &used, freePtr); 2596236769Sobrien if (nstr == var_Error 2597321964Ssjg || (nstr == varNoError && (flags & VARF_UNDEFERR) == 0) 2598236769Sobrien || strlen(rval) != (size_t) used) { 2599292068Ssjg free(freeIt); 2600236769Sobrien goto out; /* error already reported */ 2601236769Sobrien } 2602236769Sobrien } 2603292068Ssjg free(freeIt); 2604236769Sobrien if (*tstr == ':') 2605236769Sobrien tstr++; 2606236769Sobrien else if (!*tstr && endc) { 2607236769Sobrien Error("Unclosed variable specification after complex modifier (expecting '%c') for %s", endc, v->name); 2608236769Sobrien goto out; 2609236769Sobrien } 2610236769Sobrien continue; 2611236769Sobrien } 2612236769Sobrien apply_mods: 2613236769Sobrien if (DEBUG(VAR)) { 2614246223Ssjg fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", v->name, 2615246223Ssjg *tstr, nstr); 2616236769Sobrien } 2617236769Sobrien newStr = var_Error; 2618236769Sobrien switch ((modifier = *tstr)) { 2619236769Sobrien case ':': 2620236769Sobrien { 2621236769Sobrien if (tstr[1] == '=' || 2622236769Sobrien (tstr[2] == '=' && 2623236769Sobrien (tstr[1] == '!' || tstr[1] == '+' || tstr[1] == '?'))) { 2624236769Sobrien /* 2625236769Sobrien * "::=", "::!=", "::+=", or "::?=" 2626236769Sobrien */ 2627236769Sobrien GNode *v_ctxt; /* context where v belongs */ 2628236769Sobrien const char *emsg; 2629236769Sobrien char *sv_name; 2630236769Sobrien VarPattern pattern; 2631236769Sobrien int how; 2632321964Ssjg int vflags; 2633236769Sobrien 2634236769Sobrien if (v->name[0] == 0) 2635236769Sobrien goto bad_modifier; 2636236769Sobrien 2637236769Sobrien v_ctxt = ctxt; 2638236769Sobrien sv_name = NULL; 2639236769Sobrien ++tstr; 2640236769Sobrien if (v->flags & VAR_JUNK) { 2641236769Sobrien /* 2642236769Sobrien * We need to bmake_strdup() it incase 2643236769Sobrien * VarGetPattern() recurses. 2644236769Sobrien */ 2645236769Sobrien sv_name = v->name; 2646236769Sobrien v->name = bmake_strdup(v->name); 2647236769Sobrien } else if (ctxt != VAR_GLOBAL) { 2648236769Sobrien Var *gv = VarFind(v->name, ctxt, 0); 2649236769Sobrien if (gv == NULL) 2650236769Sobrien v_ctxt = VAR_GLOBAL; 2651236769Sobrien else 2652236769Sobrien VarFreeEnv(gv, TRUE); 2653236769Sobrien } 2654236769Sobrien 2655236769Sobrien switch ((how = *tstr)) { 2656236769Sobrien case '+': 2657236769Sobrien case '?': 2658236769Sobrien case '!': 2659236769Sobrien cp = &tstr[2]; 2660236769Sobrien break; 2661236769Sobrien default: 2662236769Sobrien cp = ++tstr; 2663236769Sobrien break; 2664236769Sobrien } 2665236769Sobrien delim = startc == PROPEN ? PRCLOSE : BRCLOSE; 2666236769Sobrien pattern.flags = 0; 2667236769Sobrien 2668321964Ssjg vflags = (flags & VARF_WANTRES) ? 0 : VAR_NOSUBST; 2669321964Ssjg pattern.rhs = VarGetPattern(ctxt, &parsestate, flags, 2670321964Ssjg &cp, delim, &vflags, 2671236769Sobrien &pattern.rightLen, 2672236769Sobrien NULL); 2673236769Sobrien if (v->flags & VAR_JUNK) { 2674236769Sobrien /* restore original name */ 2675236769Sobrien free(v->name); 2676236769Sobrien v->name = sv_name; 2677236769Sobrien } 2678236769Sobrien if (pattern.rhs == NULL) 2679236769Sobrien goto cleanup; 2680236769Sobrien 2681236769Sobrien termc = *--cp; 2682236769Sobrien delim = '\0'; 2683236769Sobrien 2684321964Ssjg if (flags & VARF_WANTRES) { 2685292068Ssjg switch (how) { 2686292068Ssjg case '+': 2687292068Ssjg Var_Append(v->name, pattern.rhs, v_ctxt); 2688292068Ssjg break; 2689292068Ssjg case '!': 2690292068Ssjg newStr = Cmd_Exec(pattern.rhs, &emsg); 2691292068Ssjg if (emsg) 2692292068Ssjg Error(emsg, nstr); 2693292068Ssjg else 2694292068Ssjg Var_Set(v->name, newStr, v_ctxt, 0); 2695236769Sobrien free(newStr); 2696236769Sobrien break; 2697292068Ssjg case '?': 2698292068Ssjg if ((v->flags & VAR_JUNK) == 0) 2699292068Ssjg break; 2700292068Ssjg /* FALLTHROUGH */ 2701292068Ssjg default: 2702292068Ssjg Var_Set(v->name, pattern.rhs, v_ctxt, 0); 2703292068Ssjg break; 2704292068Ssjg } 2705236769Sobrien } 2706236769Sobrien free(UNCONST(pattern.rhs)); 2707276305Sngie newStr = varNoError; 2708236769Sobrien break; 2709236769Sobrien } 2710236769Sobrien goto default_case; /* "::<unrecognised>" */ 2711236769Sobrien } 2712236769Sobrien case '@': 2713236769Sobrien { 2714236769Sobrien VarLoop_t loop; 2715321964Ssjg int vflags = VAR_NOSUBST; 2716236769Sobrien 2717236769Sobrien cp = ++tstr; 2718236769Sobrien delim = '@'; 2719321964Ssjg if ((loop.tvar = VarGetPattern(ctxt, &parsestate, flags, 2720236769Sobrien &cp, delim, 2721321964Ssjg &vflags, &loop.tvarLen, 2722236769Sobrien NULL)) == NULL) 2723236769Sobrien goto cleanup; 2724236769Sobrien 2725321964Ssjg if ((loop.str = VarGetPattern(ctxt, &parsestate, flags, 2726236769Sobrien &cp, delim, 2727321964Ssjg &vflags, &loop.strLen, 2728236769Sobrien NULL)) == NULL) 2729236769Sobrien goto cleanup; 2730236769Sobrien 2731236769Sobrien termc = *cp; 2732236769Sobrien delim = '\0'; 2733236769Sobrien 2734321964Ssjg loop.errnum = flags & VARF_UNDEFERR; 2735236769Sobrien loop.ctxt = ctxt; 2736236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarLoopExpand, 2737236769Sobrien &loop); 2738321964Ssjg Var_Delete(loop.tvar, ctxt); 2739236769Sobrien free(loop.tvar); 2740236769Sobrien free(loop.str); 2741236769Sobrien break; 2742236769Sobrien } 2743321964Ssjg case '_': /* remember current value */ 2744321964Ssjg cp = tstr + 1; /* make sure it is set */ 2745321964Ssjg if (STRMOD_MATCHX(tstr, "_", 1)) { 2746321964Ssjg if (tstr[1] == '=') { 2747321964Ssjg char *np; 2748321964Ssjg int n; 2749321964Ssjg 2750321964Ssjg cp++; 2751321964Ssjg n = strcspn(cp, ":)}"); 2752321964Ssjg np = bmake_strndup(cp, n+1); 2753321964Ssjg np[n] = '\0'; 2754321964Ssjg cp = tstr + 2 + n; 2755321964Ssjg Var_Set(np, nstr, ctxt, 0); 2756321964Ssjg free(np); 2757321964Ssjg } else { 2758321964Ssjg Var_Set("_", nstr, ctxt, 0); 2759321964Ssjg } 2760321964Ssjg newStr = nstr; 2761321964Ssjg termc = *cp; 2762321964Ssjg break; 2763321964Ssjg } 2764321964Ssjg goto default_case; 2765276305Sngie case 'D': 2766250164Ssjg case 'U': 2767236769Sobrien { 2768236769Sobrien Buffer buf; /* Buffer for patterns */ 2769321964Ssjg int nflags; 2770236769Sobrien 2771321964Ssjg if (flags & VARF_WANTRES) { 2772321964Ssjg int wantres; 2773292068Ssjg if (*tstr == 'U') 2774321964Ssjg wantres = ((v->flags & VAR_JUNK) != 0); 2775292068Ssjg else 2776321964Ssjg wantres = ((v->flags & VAR_JUNK) == 0); 2777321964Ssjg nflags = flags & ~VARF_WANTRES; 2778321964Ssjg if (wantres) 2779321964Ssjg nflags |= VARF_WANTRES; 2780292068Ssjg } else 2781321964Ssjg nflags = flags; 2782236769Sobrien /* 2783236769Sobrien * Pass through tstr looking for 1) escaped delimiters, 2784236769Sobrien * '$'s and backslashes (place the escaped character in 2785236769Sobrien * uninterpreted) and 2) unescaped $'s that aren't before 2786236769Sobrien * the delimiter (expand the variable substitution). 2787236769Sobrien * The result is left in the Buffer buf. 2788236769Sobrien */ 2789236769Sobrien Buf_Init(&buf, 0); 2790236769Sobrien for (cp = tstr + 1; 2791236769Sobrien *cp != endc && *cp != ':' && *cp != '\0'; 2792236769Sobrien cp++) { 2793236769Sobrien if ((*cp == '\\') && 2794236769Sobrien ((cp[1] == ':') || 2795236769Sobrien (cp[1] == '$') || 2796236769Sobrien (cp[1] == endc) || 2797236769Sobrien (cp[1] == '\\'))) 2798236769Sobrien { 2799236769Sobrien Buf_AddByte(&buf, cp[1]); 2800236769Sobrien cp++; 2801236769Sobrien } else if (*cp == '$') { 2802236769Sobrien /* 2803236769Sobrien * If unescaped dollar sign, assume it's a 2804236769Sobrien * variable substitution and recurse. 2805236769Sobrien */ 2806236769Sobrien char *cp2; 2807236769Sobrien int len; 2808236769Sobrien void *freeIt; 2809236769Sobrien 2810321964Ssjg cp2 = Var_Parse(cp, ctxt, nflags, &len, &freeIt); 2811236769Sobrien Buf_AddBytes(&buf, strlen(cp2), cp2); 2812292068Ssjg free(freeIt); 2813236769Sobrien cp += len - 1; 2814236769Sobrien } else { 2815236769Sobrien Buf_AddByte(&buf, *cp); 2816236769Sobrien } 2817236769Sobrien } 2818236769Sobrien 2819236769Sobrien termc = *cp; 2820236769Sobrien 2821236769Sobrien if ((v->flags & VAR_JUNK) != 0) 2822236769Sobrien v->flags |= VAR_KEEP; 2823321964Ssjg if (nflags & VARF_WANTRES) { 2824236769Sobrien newStr = Buf_Destroy(&buf, FALSE); 2825236769Sobrien } else { 2826236769Sobrien newStr = nstr; 2827236769Sobrien Buf_Destroy(&buf, TRUE); 2828236769Sobrien } 2829236769Sobrien break; 2830236769Sobrien } 2831236769Sobrien case 'L': 2832236769Sobrien { 2833236769Sobrien if ((v->flags & VAR_JUNK) != 0) 2834236769Sobrien v->flags |= VAR_KEEP; 2835236769Sobrien newStr = bmake_strdup(v->name); 2836236769Sobrien cp = ++tstr; 2837236769Sobrien termc = *tstr; 2838236769Sobrien break; 2839236769Sobrien } 2840236769Sobrien case 'P': 2841236769Sobrien { 2842236769Sobrien GNode *gn; 2843236769Sobrien 2844236769Sobrien if ((v->flags & VAR_JUNK) != 0) 2845236769Sobrien v->flags |= VAR_KEEP; 2846236769Sobrien gn = Targ_FindNode(v->name, TARG_NOCREATE); 2847236769Sobrien if (gn == NULL || gn->type & OP_NOPATH) { 2848236769Sobrien newStr = NULL; 2849236769Sobrien } else if (gn->path) { 2850236769Sobrien newStr = bmake_strdup(gn->path); 2851236769Sobrien } else { 2852236769Sobrien newStr = Dir_FindFile(v->name, Suff_FindPath(gn)); 2853236769Sobrien } 2854236769Sobrien if (!newStr) { 2855236769Sobrien newStr = bmake_strdup(v->name); 2856236769Sobrien } 2857236769Sobrien cp = ++tstr; 2858236769Sobrien termc = *tstr; 2859236769Sobrien break; 2860236769Sobrien } 2861236769Sobrien case '!': 2862236769Sobrien { 2863236769Sobrien const char *emsg; 2864236769Sobrien VarPattern pattern; 2865236769Sobrien pattern.flags = 0; 2866236769Sobrien 2867236769Sobrien delim = '!'; 2868292068Ssjg emsg = NULL; 2869236769Sobrien cp = ++tstr; 2870321964Ssjg if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags, 2871236769Sobrien &cp, delim, 2872236769Sobrien NULL, &pattern.rightLen, 2873236769Sobrien NULL)) == NULL) 2874236769Sobrien goto cleanup; 2875321964Ssjg if (flags & VARF_WANTRES) 2876292068Ssjg newStr = Cmd_Exec(pattern.rhs, &emsg); 2877292068Ssjg else 2878292068Ssjg newStr = varNoError; 2879236769Sobrien free(UNCONST(pattern.rhs)); 2880236769Sobrien if (emsg) 2881236769Sobrien Error(emsg, nstr); 2882236769Sobrien termc = *cp; 2883236769Sobrien delim = '\0'; 2884236769Sobrien if (v->flags & VAR_JUNK) { 2885236769Sobrien v->flags |= VAR_KEEP; 2886236769Sobrien } 2887236769Sobrien break; 2888236769Sobrien } 2889236769Sobrien case '[': 2890236769Sobrien { 2891236769Sobrien /* 2892236769Sobrien * Look for the closing ']', recursively 2893236769Sobrien * expanding any embedded variables. 2894236769Sobrien * 2895236769Sobrien * estr is a pointer to the expanded result, 2896236769Sobrien * which we must free(). 2897236769Sobrien */ 2898236769Sobrien char *estr; 2899236769Sobrien 2900236769Sobrien cp = tstr+1; /* point to char after '[' */ 2901236769Sobrien delim = ']'; /* look for closing ']' */ 2902236769Sobrien estr = VarGetPattern(ctxt, &parsestate, 2903321964Ssjg flags, &cp, delim, 2904236769Sobrien NULL, NULL, NULL); 2905236769Sobrien if (estr == NULL) 2906236769Sobrien goto cleanup; /* report missing ']' */ 2907236769Sobrien /* now cp points just after the closing ']' */ 2908236769Sobrien delim = '\0'; 2909236769Sobrien if (cp[0] != ':' && cp[0] != endc) { 2910236769Sobrien /* Found junk after ']' */ 2911236769Sobrien free(estr); 2912236769Sobrien goto bad_modifier; 2913236769Sobrien } 2914236769Sobrien if (estr[0] == '\0') { 2915236769Sobrien /* Found empty square brackets in ":[]". */ 2916236769Sobrien free(estr); 2917236769Sobrien goto bad_modifier; 2918236769Sobrien } else if (estr[0] == '#' && estr[1] == '\0') { 2919236769Sobrien /* Found ":[#]" */ 2920236769Sobrien 2921236769Sobrien /* 2922236769Sobrien * We will need enough space for the decimal 2923236769Sobrien * representation of an int. We calculate the 2924236769Sobrien * space needed for the octal representation, 2925236769Sobrien * and add enough slop to cope with a '-' sign 2926236769Sobrien * (which should never be needed) and a '\0' 2927236769Sobrien * string terminator. 2928236769Sobrien */ 2929236769Sobrien int newStrSize = 2930236769Sobrien (sizeof(int) * CHAR_BIT + 2) / 3 + 2; 2931236769Sobrien 2932236769Sobrien newStr = bmake_malloc(newStrSize); 2933236769Sobrien if (parsestate.oneBigWord) { 2934236769Sobrien strncpy(newStr, "1", newStrSize); 2935236769Sobrien } else { 2936236769Sobrien /* XXX: brk_string() is a rather expensive 2937236769Sobrien * way of counting words. */ 2938236769Sobrien char **av; 2939236769Sobrien char *as; 2940236769Sobrien int ac; 2941236769Sobrien 2942236769Sobrien av = brk_string(nstr, &ac, FALSE, &as); 2943236769Sobrien snprintf(newStr, newStrSize, "%d", ac); 2944236769Sobrien free(as); 2945236769Sobrien free(av); 2946236769Sobrien } 2947236769Sobrien termc = *cp; 2948236769Sobrien free(estr); 2949236769Sobrien break; 2950236769Sobrien } else if (estr[0] == '*' && estr[1] == '\0') { 2951236769Sobrien /* Found ":[*]" */ 2952236769Sobrien parsestate.oneBigWord = TRUE; 2953236769Sobrien newStr = nstr; 2954236769Sobrien termc = *cp; 2955236769Sobrien free(estr); 2956236769Sobrien break; 2957236769Sobrien } else if (estr[0] == '@' && estr[1] == '\0') { 2958236769Sobrien /* Found ":[@]" */ 2959236769Sobrien parsestate.oneBigWord = FALSE; 2960236769Sobrien newStr = nstr; 2961236769Sobrien termc = *cp; 2962236769Sobrien free(estr); 2963236769Sobrien break; 2964236769Sobrien } else { 2965236769Sobrien /* 2966236769Sobrien * We expect estr to contain a single 2967236769Sobrien * integer for :[N], or two integers 2968236769Sobrien * separated by ".." for :[start..end]. 2969236769Sobrien */ 2970236769Sobrien VarSelectWords_t seldata = { 0, 0 }; 2971236769Sobrien 2972236769Sobrien seldata.start = strtol(estr, &ep, 0); 2973236769Sobrien if (ep == estr) { 2974236769Sobrien /* Found junk instead of a number */ 2975236769Sobrien free(estr); 2976236769Sobrien goto bad_modifier; 2977236769Sobrien } else if (ep[0] == '\0') { 2978236769Sobrien /* Found only one integer in :[N] */ 2979236769Sobrien seldata.end = seldata.start; 2980236769Sobrien } else if (ep[0] == '.' && ep[1] == '.' && 2981236769Sobrien ep[2] != '\0') { 2982236769Sobrien /* Expecting another integer after ".." */ 2983236769Sobrien ep += 2; 2984236769Sobrien seldata.end = strtol(ep, &ep, 0); 2985236769Sobrien if (ep[0] != '\0') { 2986236769Sobrien /* Found junk after ".." */ 2987236769Sobrien free(estr); 2988236769Sobrien goto bad_modifier; 2989236769Sobrien } 2990236769Sobrien } else { 2991236769Sobrien /* Found junk instead of ".." */ 2992236769Sobrien free(estr); 2993236769Sobrien goto bad_modifier; 2994236769Sobrien } 2995236769Sobrien /* 2996236769Sobrien * Now seldata is properly filled in, 2997236769Sobrien * but we still have to check for 0 as 2998236769Sobrien * a special case. 2999236769Sobrien */ 3000236769Sobrien if (seldata.start == 0 && seldata.end == 0) { 3001236769Sobrien /* ":[0]" or perhaps ":[0..0]" */ 3002236769Sobrien parsestate.oneBigWord = TRUE; 3003236769Sobrien newStr = nstr; 3004236769Sobrien termc = *cp; 3005236769Sobrien free(estr); 3006236769Sobrien break; 3007236769Sobrien } else if (seldata.start == 0 || 3008236769Sobrien seldata.end == 0) { 3009236769Sobrien /* ":[0..N]" or ":[N..0]" */ 3010236769Sobrien free(estr); 3011236769Sobrien goto bad_modifier; 3012236769Sobrien } 3013236769Sobrien /* 3014236769Sobrien * Normal case: select the words 3015236769Sobrien * described by seldata. 3016236769Sobrien */ 3017236769Sobrien newStr = VarSelectWords(ctxt, &parsestate, 3018236769Sobrien nstr, &seldata); 3019236769Sobrien 3020236769Sobrien termc = *cp; 3021236769Sobrien free(estr); 3022236769Sobrien break; 3023236769Sobrien } 3024236769Sobrien 3025236769Sobrien } 3026236769Sobrien case 'g': 3027236769Sobrien cp = tstr + 1; /* make sure it is set */ 3028321964Ssjg if (STRMOD_MATCHX(tstr, "gmtime", 6)) { 3029321964Ssjg if (tstr[6] == '=') { 3030321964Ssjg utc = strtoul(&tstr[7], &ep, 10); 3031321964Ssjg cp = ep; 3032321964Ssjg } else { 3033321964Ssjg utc = 0; 3034321964Ssjg cp = tstr + 6; 3035321964Ssjg } 3036321964Ssjg newStr = VarStrftime(nstr, 1, utc); 3037236769Sobrien termc = *cp; 3038236769Sobrien } else { 3039236769Sobrien goto default_case; 3040236769Sobrien } 3041236769Sobrien break; 3042236769Sobrien case 'h': 3043236769Sobrien cp = tstr + 1; /* make sure it is set */ 3044236769Sobrien if (STRMOD_MATCH(tstr, "hash", 4)) { 3045236769Sobrien newStr = VarHash(nstr); 3046236769Sobrien cp = tstr + 4; 3047236769Sobrien termc = *cp; 3048236769Sobrien } else { 3049236769Sobrien goto default_case; 3050236769Sobrien } 3051236769Sobrien break; 3052236769Sobrien case 'l': 3053236769Sobrien cp = tstr + 1; /* make sure it is set */ 3054321964Ssjg if (STRMOD_MATCHX(tstr, "localtime", 9)) { 3055321964Ssjg if (tstr[9] == '=') { 3056321964Ssjg utc = strtoul(&tstr[10], &ep, 10); 3057321964Ssjg cp = ep; 3058321964Ssjg } else { 3059321964Ssjg utc = 0; 3060321964Ssjg cp = tstr + 9; 3061321964Ssjg } 3062321964Ssjg newStr = VarStrftime(nstr, 0, utc); 3063236769Sobrien termc = *cp; 3064236769Sobrien } else { 3065236769Sobrien goto default_case; 3066236769Sobrien } 3067236769Sobrien break; 3068236769Sobrien case 't': 3069236769Sobrien { 3070236769Sobrien cp = tstr + 1; /* make sure it is set */ 3071236769Sobrien if (tstr[1] != endc && tstr[1] != ':') { 3072236769Sobrien if (tstr[1] == 's') { 3073236769Sobrien /* 3074236769Sobrien * Use the char (if any) at tstr[2] 3075236769Sobrien * as the word separator. 3076236769Sobrien */ 3077236769Sobrien VarPattern pattern; 3078236769Sobrien 3079236769Sobrien if (tstr[2] != endc && 3080236769Sobrien (tstr[3] == endc || tstr[3] == ':')) { 3081236769Sobrien /* ":ts<unrecognised><endc>" or 3082236769Sobrien * ":ts<unrecognised>:" */ 3083236769Sobrien parsestate.varSpace = tstr[2]; 3084236769Sobrien cp = tstr + 3; 3085236769Sobrien } else if (tstr[2] == endc || tstr[2] == ':') { 3086236769Sobrien /* ":ts<endc>" or ":ts:" */ 3087236769Sobrien parsestate.varSpace = 0; /* no separator */ 3088236769Sobrien cp = tstr + 2; 3089236769Sobrien } else if (tstr[2] == '\\') { 3090321964Ssjg const char *xp = &tstr[3]; 3091321964Ssjg int base = 8; /* assume octal */ 3092321964Ssjg 3093236769Sobrien switch (tstr[3]) { 3094236769Sobrien case 'n': 3095236769Sobrien parsestate.varSpace = '\n'; 3096236769Sobrien cp = tstr + 4; 3097236769Sobrien break; 3098236769Sobrien case 't': 3099236769Sobrien parsestate.varSpace = '\t'; 3100236769Sobrien cp = tstr + 4; 3101236769Sobrien break; 3102321964Ssjg case 'x': 3103321964Ssjg base = 16; 3104321964Ssjg xp++; 3105321964Ssjg goto get_numeric; 3106321964Ssjg case '0': 3107321964Ssjg base = 0; 3108321964Ssjg goto get_numeric; 3109236769Sobrien default: 3110236769Sobrien if (isdigit((unsigned char)tstr[3])) { 3111236769Sobrien 3112321964Ssjg get_numeric: 3113236769Sobrien parsestate.varSpace = 3114321964Ssjg strtoul(xp, &ep, base); 3115236769Sobrien if (*ep != ':' && *ep != endc) 3116236769Sobrien goto bad_modifier; 3117236769Sobrien cp = ep; 3118236769Sobrien } else { 3119236769Sobrien /* 3120236769Sobrien * ":ts<backslash><unrecognised>". 3121236769Sobrien */ 3122236769Sobrien goto bad_modifier; 3123236769Sobrien } 3124236769Sobrien break; 3125236769Sobrien } 3126236769Sobrien } else { 3127236769Sobrien /* 3128236769Sobrien * Found ":ts<unrecognised><unrecognised>". 3129236769Sobrien */ 3130236769Sobrien goto bad_modifier; 3131236769Sobrien } 3132236769Sobrien 3133236769Sobrien termc = *cp; 3134236769Sobrien 3135236769Sobrien /* 3136236769Sobrien * We cannot be certain that VarModify 3137236769Sobrien * will be used - even if there is a 3138236769Sobrien * subsequent modifier, so do a no-op 3139236769Sobrien * VarSubstitute now to for str to be 3140236769Sobrien * re-expanded without the spaces. 3141236769Sobrien */ 3142236769Sobrien pattern.flags = VAR_SUB_ONE; 3143236769Sobrien pattern.lhs = pattern.rhs = "\032"; 3144236769Sobrien pattern.leftLen = pattern.rightLen = 1; 3145236769Sobrien 3146236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, 3147236769Sobrien VarSubstitute, 3148236769Sobrien &pattern); 3149236769Sobrien } else if (tstr[2] == endc || tstr[2] == ':') { 3150236769Sobrien /* 3151236769Sobrien * Check for two-character options: 3152236769Sobrien * ":tu", ":tl" 3153236769Sobrien */ 3154236769Sobrien if (tstr[1] == 'A') { /* absolute path */ 3155236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, 3156236769Sobrien VarRealpath, NULL); 3157236769Sobrien cp = tstr + 2; 3158236769Sobrien termc = *cp; 3159249033Ssjg } else if (tstr[1] == 'u') { 3160249033Ssjg char *dp = bmake_strdup(nstr); 3161249033Ssjg for (newStr = dp; *dp; dp++) 3162249033Ssjg *dp = toupper((unsigned char)*dp); 3163236769Sobrien cp = tstr + 2; 3164236769Sobrien termc = *cp; 3165249033Ssjg } else if (tstr[1] == 'l') { 3166249033Ssjg char *dp = bmake_strdup(nstr); 3167249033Ssjg for (newStr = dp; *dp; dp++) 3168249033Ssjg *dp = tolower((unsigned char)*dp); 3169249033Ssjg cp = tstr + 2; 3170249033Ssjg termc = *cp; 3171236769Sobrien } else if (tstr[1] == 'W' || tstr[1] == 'w') { 3172236769Sobrien parsestate.oneBigWord = (tstr[1] == 'W'); 3173236769Sobrien newStr = nstr; 3174236769Sobrien cp = tstr + 2; 3175236769Sobrien termc = *cp; 3176236769Sobrien } else { 3177236769Sobrien /* Found ":t<unrecognised>:" or 3178236769Sobrien * ":t<unrecognised><endc>". */ 3179236769Sobrien goto bad_modifier; 3180236769Sobrien } 3181236769Sobrien } else { 3182236769Sobrien /* 3183236769Sobrien * Found ":t<unrecognised><unrecognised>". 3184236769Sobrien */ 3185236769Sobrien goto bad_modifier; 3186236769Sobrien } 3187236769Sobrien } else { 3188236769Sobrien /* 3189236769Sobrien * Found ":t<endc>" or ":t:". 3190236769Sobrien */ 3191236769Sobrien goto bad_modifier; 3192236769Sobrien } 3193236769Sobrien break; 3194236769Sobrien } 3195236769Sobrien case 'N': 3196236769Sobrien case 'M': 3197236769Sobrien { 3198236769Sobrien char *pattern; 3199236769Sobrien const char *endpat; /* points just after end of pattern */ 3200236769Sobrien char *cp2; 3201236769Sobrien Boolean copy; /* pattern should be, or has been, copied */ 3202236769Sobrien Boolean needSubst; 3203236769Sobrien int nest; 3204236769Sobrien 3205236769Sobrien copy = FALSE; 3206236769Sobrien needSubst = FALSE; 3207236769Sobrien nest = 1; 3208236769Sobrien /* 3209236769Sobrien * In the loop below, ignore ':' unless we are at 3210236769Sobrien * (or back to) the original brace level. 3211236769Sobrien * XXX This will likely not work right if $() and ${} 3212236769Sobrien * are intermixed. 3213236769Sobrien */ 3214236769Sobrien for (cp = tstr + 1; 3215236769Sobrien *cp != '\0' && !(*cp == ':' && nest == 1); 3216236769Sobrien cp++) 3217236769Sobrien { 3218236769Sobrien if (*cp == '\\' && 3219236769Sobrien (cp[1] == ':' || 3220236769Sobrien cp[1] == endc || cp[1] == startc)) { 3221236769Sobrien if (!needSubst) { 3222236769Sobrien copy = TRUE; 3223236769Sobrien } 3224236769Sobrien cp++; 3225236769Sobrien continue; 3226236769Sobrien } 3227236769Sobrien if (*cp == '$') { 3228236769Sobrien needSubst = TRUE; 3229236769Sobrien } 3230236769Sobrien if (*cp == '(' || *cp == '{') 3231236769Sobrien ++nest; 3232236769Sobrien if (*cp == ')' || *cp == '}') { 3233236769Sobrien --nest; 3234236769Sobrien if (nest == 0) 3235236769Sobrien break; 3236236769Sobrien } 3237236769Sobrien } 3238236769Sobrien termc = *cp; 3239236769Sobrien endpat = cp; 3240236769Sobrien if (copy) { 3241236769Sobrien /* 3242236769Sobrien * Need to compress the \:'s out of the pattern, so 3243236769Sobrien * allocate enough room to hold the uncompressed 3244236769Sobrien * pattern (note that cp started at tstr+1, so 3245236769Sobrien * cp - tstr takes the null byte into account) and 3246236769Sobrien * compress the pattern into the space. 3247236769Sobrien */ 3248236769Sobrien pattern = bmake_malloc(cp - tstr); 3249236769Sobrien for (cp2 = pattern, cp = tstr + 1; 3250236769Sobrien cp < endpat; 3251236769Sobrien cp++, cp2++) 3252236769Sobrien { 3253236769Sobrien if ((*cp == '\\') && (cp+1 < endpat) && 3254236769Sobrien (cp[1] == ':' || cp[1] == endc)) { 3255236769Sobrien cp++; 3256236769Sobrien } 3257236769Sobrien *cp2 = *cp; 3258236769Sobrien } 3259236769Sobrien *cp2 = '\0'; 3260236769Sobrien endpat = cp2; 3261236769Sobrien } else { 3262236769Sobrien /* 3263236769Sobrien * Either Var_Subst or VarModify will need a 3264236769Sobrien * nul-terminated string soon, so construct one now. 3265236769Sobrien */ 3266236769Sobrien pattern = bmake_strndup(tstr+1, endpat - (tstr + 1)); 3267236769Sobrien } 3268236769Sobrien if (needSubst) { 3269236769Sobrien /* 3270236769Sobrien * pattern contains embedded '$', so use Var_Subst to 3271236769Sobrien * expand it. 3272236769Sobrien */ 3273236769Sobrien cp2 = pattern; 3274321964Ssjg pattern = Var_Subst(NULL, cp2, ctxt, flags | VARF_WANTRES); 3275236769Sobrien free(cp2); 3276236769Sobrien } 3277236769Sobrien if (DEBUG(VAR)) 3278246223Ssjg fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n", 3279246223Ssjg v->name, nstr, pattern); 3280236769Sobrien if (*tstr == 'M') { 3281236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarMatch, 3282236769Sobrien pattern); 3283236769Sobrien } else { 3284236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarNoMatch, 3285236769Sobrien pattern); 3286236769Sobrien } 3287236769Sobrien free(pattern); 3288236769Sobrien break; 3289236769Sobrien } 3290236769Sobrien case 'S': 3291236769Sobrien { 3292236769Sobrien VarPattern pattern; 3293236769Sobrien Var_Parse_State tmpparsestate; 3294236769Sobrien 3295236769Sobrien pattern.flags = 0; 3296236769Sobrien tmpparsestate = parsestate; 3297236769Sobrien delim = tstr[1]; 3298236769Sobrien tstr += 2; 3299236769Sobrien 3300236769Sobrien /* 3301236769Sobrien * If pattern begins with '^', it is anchored to the 3302236769Sobrien * start of the word -- skip over it and flag pattern. 3303236769Sobrien */ 3304236769Sobrien if (*tstr == '^') { 3305236769Sobrien pattern.flags |= VAR_MATCH_START; 3306236769Sobrien tstr += 1; 3307236769Sobrien } 3308236769Sobrien 3309236769Sobrien cp = tstr; 3310321964Ssjg if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, flags, 3311236769Sobrien &cp, delim, 3312236769Sobrien &pattern.flags, 3313236769Sobrien &pattern.leftLen, 3314236769Sobrien NULL)) == NULL) 3315236769Sobrien goto cleanup; 3316236769Sobrien 3317321964Ssjg if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags, 3318236769Sobrien &cp, delim, NULL, 3319236769Sobrien &pattern.rightLen, 3320236769Sobrien &pattern)) == NULL) 3321236769Sobrien goto cleanup; 3322236769Sobrien 3323236769Sobrien /* 3324236769Sobrien * Check for global substitution. If 'g' after the final 3325236769Sobrien * delimiter, substitution is global and is marked that 3326236769Sobrien * way. 3327236769Sobrien */ 3328236769Sobrien for (;; cp++) { 3329236769Sobrien switch (*cp) { 3330236769Sobrien case 'g': 3331236769Sobrien pattern.flags |= VAR_SUB_GLOBAL; 3332236769Sobrien continue; 3333236769Sobrien case '1': 3334236769Sobrien pattern.flags |= VAR_SUB_ONE; 3335236769Sobrien continue; 3336236769Sobrien case 'W': 3337236769Sobrien tmpparsestate.oneBigWord = TRUE; 3338236769Sobrien continue; 3339236769Sobrien } 3340236769Sobrien break; 3341236769Sobrien } 3342236769Sobrien 3343236769Sobrien termc = *cp; 3344236769Sobrien newStr = VarModify(ctxt, &tmpparsestate, nstr, 3345236769Sobrien VarSubstitute, 3346236769Sobrien &pattern); 3347236769Sobrien 3348236769Sobrien /* 3349236769Sobrien * Free the two strings. 3350236769Sobrien */ 3351236769Sobrien free(UNCONST(pattern.lhs)); 3352236769Sobrien free(UNCONST(pattern.rhs)); 3353236769Sobrien delim = '\0'; 3354236769Sobrien break; 3355236769Sobrien } 3356236769Sobrien case '?': 3357236769Sobrien { 3358236769Sobrien VarPattern pattern; 3359236769Sobrien Boolean value; 3360292068Ssjg int cond_rc; 3361292068Ssjg int lhs_flags, rhs_flags; 3362292068Ssjg 3363236769Sobrien /* find ':', and then substitute accordingly */ 3364321964Ssjg if (flags & VARF_WANTRES) { 3365292068Ssjg cond_rc = Cond_EvalExpression(NULL, v->name, &value, 0, FALSE); 3366292068Ssjg if (cond_rc == COND_INVALID) { 3367292068Ssjg lhs_flags = rhs_flags = VAR_NOSUBST; 3368292068Ssjg } else if (value) { 3369292068Ssjg lhs_flags = 0; 3370292068Ssjg rhs_flags = VAR_NOSUBST; 3371292068Ssjg } else { 3372292068Ssjg lhs_flags = VAR_NOSUBST; 3373292068Ssjg rhs_flags = 0; 3374292068Ssjg } 3375292068Ssjg } else { 3376292068Ssjg /* we are just consuming and discarding */ 3377292068Ssjg cond_rc = value = 0; 3378292068Ssjg lhs_flags = rhs_flags = VAR_NOSUBST; 3379292068Ssjg } 3380236769Sobrien pattern.flags = 0; 3381236769Sobrien 3382236769Sobrien cp = ++tstr; 3383236769Sobrien delim = ':'; 3384321964Ssjg if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, flags, 3385292068Ssjg &cp, delim, &lhs_flags, 3386236769Sobrien &pattern.leftLen, 3387236769Sobrien NULL)) == NULL) 3388236769Sobrien goto cleanup; 3389236769Sobrien 3390236769Sobrien /* BROPEN or PROPEN */ 3391236769Sobrien delim = endc; 3392321964Ssjg if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, flags, 3393292068Ssjg &cp, delim, &rhs_flags, 3394236769Sobrien &pattern.rightLen, 3395236769Sobrien NULL)) == NULL) 3396236769Sobrien goto cleanup; 3397236769Sobrien 3398236769Sobrien termc = *--cp; 3399236769Sobrien delim = '\0'; 3400292068Ssjg if (cond_rc == COND_INVALID) { 3401236769Sobrien Error("Bad conditional expression `%s' in %s?%s:%s", 3402236769Sobrien v->name, v->name, pattern.lhs, pattern.rhs); 3403236769Sobrien goto cleanup; 3404236769Sobrien } 3405236769Sobrien 3406236769Sobrien if (value) { 3407236769Sobrien newStr = UNCONST(pattern.lhs); 3408236769Sobrien free(UNCONST(pattern.rhs)); 3409236769Sobrien } else { 3410236769Sobrien newStr = UNCONST(pattern.rhs); 3411236769Sobrien free(UNCONST(pattern.lhs)); 3412236769Sobrien } 3413236769Sobrien if (v->flags & VAR_JUNK) { 3414236769Sobrien v->flags |= VAR_KEEP; 3415236769Sobrien } 3416236769Sobrien break; 3417236769Sobrien } 3418236769Sobrien#ifndef NO_REGEX 3419236769Sobrien case 'C': 3420236769Sobrien { 3421236769Sobrien VarREPattern pattern; 3422236769Sobrien char *re; 3423236769Sobrien int error; 3424236769Sobrien Var_Parse_State tmpparsestate; 3425236769Sobrien 3426236769Sobrien pattern.flags = 0; 3427236769Sobrien tmpparsestate = parsestate; 3428236769Sobrien delim = tstr[1]; 3429236769Sobrien tstr += 2; 3430236769Sobrien 3431236769Sobrien cp = tstr; 3432236769Sobrien 3433321964Ssjg if ((re = VarGetPattern(ctxt, &parsestate, flags, &cp, delim, 3434236769Sobrien NULL, NULL, NULL)) == NULL) 3435236769Sobrien goto cleanup; 3436236769Sobrien 3437236769Sobrien if ((pattern.replace = VarGetPattern(ctxt, &parsestate, 3438321964Ssjg flags, &cp, delim, NULL, 3439236769Sobrien NULL, NULL)) == NULL){ 3440236769Sobrien free(re); 3441236769Sobrien goto cleanup; 3442236769Sobrien } 3443236769Sobrien 3444236769Sobrien for (;; cp++) { 3445236769Sobrien switch (*cp) { 3446236769Sobrien case 'g': 3447236769Sobrien pattern.flags |= VAR_SUB_GLOBAL; 3448236769Sobrien continue; 3449236769Sobrien case '1': 3450236769Sobrien pattern.flags |= VAR_SUB_ONE; 3451236769Sobrien continue; 3452236769Sobrien case 'W': 3453236769Sobrien tmpparsestate.oneBigWord = TRUE; 3454236769Sobrien continue; 3455236769Sobrien } 3456236769Sobrien break; 3457236769Sobrien } 3458236769Sobrien 3459236769Sobrien termc = *cp; 3460236769Sobrien 3461236769Sobrien error = regcomp(&pattern.re, re, REG_EXTENDED); 3462236769Sobrien free(re); 3463236769Sobrien if (error) { 3464236769Sobrien *lengthPtr = cp - start + 1; 3465236769Sobrien VarREError(error, &pattern.re, "RE substitution error"); 3466236769Sobrien free(pattern.replace); 3467236769Sobrien goto cleanup; 3468236769Sobrien } 3469236769Sobrien 3470236769Sobrien pattern.nsub = pattern.re.re_nsub + 1; 3471236769Sobrien if (pattern.nsub < 1) 3472236769Sobrien pattern.nsub = 1; 3473236769Sobrien if (pattern.nsub > 10) 3474236769Sobrien pattern.nsub = 10; 3475236769Sobrien pattern.matches = bmake_malloc(pattern.nsub * 3476236769Sobrien sizeof(regmatch_t)); 3477236769Sobrien newStr = VarModify(ctxt, &tmpparsestate, nstr, 3478236769Sobrien VarRESubstitute, 3479236769Sobrien &pattern); 3480236769Sobrien regfree(&pattern.re); 3481236769Sobrien free(pattern.replace); 3482236769Sobrien free(pattern.matches); 3483236769Sobrien delim = '\0'; 3484236769Sobrien break; 3485236769Sobrien } 3486236769Sobrien#endif 3487236769Sobrien case 'Q': 3488236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3489236769Sobrien newStr = VarQuote(nstr); 3490236769Sobrien cp = tstr + 1; 3491236769Sobrien termc = *cp; 3492236769Sobrien break; 3493236769Sobrien } 3494236769Sobrien goto default_case; 3495236769Sobrien case 'T': 3496236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3497236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarTail, 3498236769Sobrien NULL); 3499236769Sobrien cp = tstr + 1; 3500236769Sobrien termc = *cp; 3501236769Sobrien break; 3502236769Sobrien } 3503236769Sobrien goto default_case; 3504236769Sobrien case 'H': 3505236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3506236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarHead, 3507236769Sobrien NULL); 3508236769Sobrien cp = tstr + 1; 3509236769Sobrien termc = *cp; 3510236769Sobrien break; 3511236769Sobrien } 3512236769Sobrien goto default_case; 3513236769Sobrien case 'E': 3514236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3515236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarSuffix, 3516236769Sobrien NULL); 3517236769Sobrien cp = tstr + 1; 3518236769Sobrien termc = *cp; 3519236769Sobrien break; 3520236769Sobrien } 3521236769Sobrien goto default_case; 3522236769Sobrien case 'R': 3523236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3524236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarRoot, 3525236769Sobrien NULL); 3526236769Sobrien cp = tstr + 1; 3527236769Sobrien termc = *cp; 3528236769Sobrien break; 3529236769Sobrien } 3530236769Sobrien goto default_case; 3531321964Ssjg case 'r': 3532321964Ssjg cp = tstr + 1; /* make sure it is set */ 3533321964Ssjg if (STRMOD_MATCHX(tstr, "range", 5)) { 3534321964Ssjg int n; 3535321964Ssjg 3536321964Ssjg if (tstr[5] == '=') { 3537321964Ssjg n = strtoul(&tstr[6], &ep, 10); 3538321964Ssjg cp = ep; 3539321964Ssjg } else { 3540321964Ssjg n = 0; 3541321964Ssjg cp = tstr + 5; 3542321964Ssjg } 3543321964Ssjg newStr = VarRange(nstr, n); 3544321964Ssjg termc = *cp; 3545321964Ssjg break; 3546321964Ssjg } 3547321964Ssjg goto default_case; 3548236769Sobrien case 'O': 3549236769Sobrien { 3550236769Sobrien char otype; 3551236769Sobrien 3552236769Sobrien cp = tstr + 1; /* skip to the rest in any case */ 3553236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3554236769Sobrien otype = 's'; 3555236769Sobrien termc = *cp; 3556236769Sobrien } else if ( (tstr[1] == 'x') && 3557236769Sobrien (tstr[2] == endc || tstr[2] == ':') ) { 3558236769Sobrien otype = tstr[1]; 3559236769Sobrien cp = tstr + 2; 3560236769Sobrien termc = *cp; 3561236769Sobrien } else { 3562236769Sobrien goto bad_modifier; 3563236769Sobrien } 3564236769Sobrien newStr = VarOrder(nstr, otype); 3565236769Sobrien break; 3566236769Sobrien } 3567236769Sobrien case 'u': 3568236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3569236769Sobrien newStr = VarUniq(nstr); 3570236769Sobrien cp = tstr + 1; 3571236769Sobrien termc = *cp; 3572236769Sobrien break; 3573236769Sobrien } 3574236769Sobrien goto default_case; 3575236769Sobrien#ifdef SUNSHCMD 3576236769Sobrien case 's': 3577236769Sobrien if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) { 3578236769Sobrien const char *emsg; 3579321964Ssjg if (flags & VARF_WANTRES) { 3580292068Ssjg newStr = Cmd_Exec(nstr, &emsg); 3581292068Ssjg if (emsg) 3582292068Ssjg Error(emsg, nstr); 3583292068Ssjg } else 3584292068Ssjg newStr = varNoError; 3585236769Sobrien cp = tstr + 2; 3586236769Sobrien termc = *cp; 3587236769Sobrien break; 3588236769Sobrien } 3589236769Sobrien goto default_case; 3590236769Sobrien#endif 3591236769Sobrien default: 3592236769Sobrien default_case: 3593236769Sobrien { 3594236769Sobrien#ifdef SYSVVARSUB 3595236769Sobrien /* 3596236769Sobrien * This can either be a bogus modifier or a System-V 3597236769Sobrien * substitution command. 3598236769Sobrien */ 3599236769Sobrien VarPattern pattern; 3600236769Sobrien Boolean eqFound; 3601236769Sobrien 3602236769Sobrien pattern.flags = 0; 3603236769Sobrien eqFound = FALSE; 3604236769Sobrien /* 3605236769Sobrien * First we make a pass through the string trying 3606236769Sobrien * to verify it is a SYSV-make-style translation: 3607236769Sobrien * it must be: <string1>=<string2>) 3608236769Sobrien */ 3609236769Sobrien cp = tstr; 3610236769Sobrien cnt = 1; 3611236769Sobrien while (*cp != '\0' && cnt) { 3612236769Sobrien if (*cp == '=') { 3613236769Sobrien eqFound = TRUE; 3614236769Sobrien /* continue looking for endc */ 3615236769Sobrien } 3616236769Sobrien else if (*cp == endc) 3617236769Sobrien cnt--; 3618236769Sobrien else if (*cp == startc) 3619236769Sobrien cnt++; 3620236769Sobrien if (cnt) 3621236769Sobrien cp++; 3622236769Sobrien } 3623236769Sobrien if (*cp == endc && eqFound) { 3624236769Sobrien 3625236769Sobrien /* 3626236769Sobrien * Now we break this sucker into the lhs and 3627236769Sobrien * rhs. We must null terminate them of course. 3628236769Sobrien */ 3629236769Sobrien delim='='; 3630236769Sobrien cp = tstr; 3631236769Sobrien if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, 3632321964Ssjg flags, &cp, delim, &pattern.flags, 3633236769Sobrien &pattern.leftLen, NULL)) == NULL) 3634236769Sobrien goto cleanup; 3635236769Sobrien delim = endc; 3636236769Sobrien if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, 3637321964Ssjg flags, &cp, delim, NULL, &pattern.rightLen, 3638236769Sobrien &pattern)) == NULL) 3639236769Sobrien goto cleanup; 3640236769Sobrien 3641236769Sobrien /* 3642236769Sobrien * SYSV modifications happen through the whole 3643236769Sobrien * string. Note the pattern is anchored at the end. 3644236769Sobrien */ 3645236769Sobrien termc = *--cp; 3646236769Sobrien delim = '\0'; 3647236769Sobrien if (pattern.leftLen == 0 && *nstr == '\0') { 3648236769Sobrien newStr = nstr; /* special case */ 3649236769Sobrien } else { 3650236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, 3651236769Sobrien VarSYSVMatch, 3652236769Sobrien &pattern); 3653236769Sobrien } 3654236769Sobrien free(UNCONST(pattern.lhs)); 3655236769Sobrien free(UNCONST(pattern.rhs)); 3656236769Sobrien } else 3657236769Sobrien#endif 3658236769Sobrien { 3659236769Sobrien Error("Unknown modifier '%c'", *tstr); 3660236769Sobrien for (cp = tstr+1; 3661236769Sobrien *cp != ':' && *cp != endc && *cp != '\0'; 3662236769Sobrien cp++) 3663236769Sobrien continue; 3664236769Sobrien termc = *cp; 3665236769Sobrien newStr = var_Error; 3666236769Sobrien } 3667236769Sobrien } 3668236769Sobrien } 3669236769Sobrien if (DEBUG(VAR)) { 3670246223Ssjg fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n", 3671246223Ssjg v->name, modifier, newStr); 3672236769Sobrien } 3673236769Sobrien 3674236769Sobrien if (newStr != nstr) { 3675236769Sobrien if (*freePtr) { 3676236769Sobrien free(nstr); 3677236769Sobrien *freePtr = NULL; 3678236769Sobrien } 3679236769Sobrien nstr = newStr; 3680236769Sobrien if (nstr != var_Error && nstr != varNoError) { 3681236769Sobrien *freePtr = nstr; 3682236769Sobrien } 3683236769Sobrien } 3684236769Sobrien if (termc == '\0' && endc != '\0') { 3685236769Sobrien Error("Unclosed variable specification (expecting '%c') for \"%s\" (value \"%s\") modifier %c", endc, v->name, nstr, modifier); 3686236769Sobrien } else if (termc == ':') { 3687236769Sobrien cp++; 3688236769Sobrien } 3689236769Sobrien tstr = cp; 3690236769Sobrien } 3691236769Sobrien out: 3692236769Sobrien *lengthPtr = tstr - start; 3693236769Sobrien return (nstr); 3694236769Sobrien 3695236769Sobrien bad_modifier: 3696236769Sobrien /* "{(" */ 3697236769Sobrien Error("Bad modifier `:%.*s' for %s", (int)strcspn(tstr, ":)}"), tstr, 3698236769Sobrien v->name); 3699236769Sobrien 3700236769Sobrien cleanup: 3701236769Sobrien *lengthPtr = cp - start; 3702236769Sobrien if (delim != '\0') 3703236769Sobrien Error("Unclosed substitution for %s (%c missing)", 3704236769Sobrien v->name, delim); 3705292068Ssjg free(*freePtr); 3706292068Ssjg *freePtr = NULL; 3707236769Sobrien return (var_Error); 3708236769Sobrien} 3709236769Sobrien 3710236769Sobrien/*- 3711236769Sobrien *----------------------------------------------------------------------- 3712236769Sobrien * Var_Parse -- 3713236769Sobrien * Given the start of a variable invocation, extract the variable 3714236769Sobrien * name and find its value, then modify it according to the 3715236769Sobrien * specification. 3716236769Sobrien * 3717236769Sobrien * Input: 3718236769Sobrien * str The string to parse 3719236769Sobrien * ctxt The context for the variable 3720321964Ssjg * flags VARF_UNDEFERR if undefineds are an error 3721321964Ssjg * VARF_WANTRES if we actually want the result 3722321964Ssjg * VARF_ASSIGN if we are in a := assignment 3723236769Sobrien * lengthPtr OUT: The length of the specification 3724236769Sobrien * freePtr OUT: Non-NULL if caller should free *freePtr 3725236769Sobrien * 3726236769Sobrien * Results: 3727236769Sobrien * The (possibly-modified) value of the variable or var_Error if the 3728236769Sobrien * specification is invalid. The length of the specification is 3729236769Sobrien * placed in *lengthPtr (for invalid specifications, this is just 3730236769Sobrien * 2...?). 3731236769Sobrien * If *freePtr is non-NULL then it's a pointer that the caller 3732236769Sobrien * should pass to free() to free memory used by the result. 3733236769Sobrien * 3734236769Sobrien * Side Effects: 3735236769Sobrien * None. 3736236769Sobrien * 3737236769Sobrien *----------------------------------------------------------------------- 3738236769Sobrien */ 3739236769Sobrien/* coverity[+alloc : arg-*4] */ 3740236769Sobrienchar * 3741321964SsjgVar_Parse(const char *str, GNode *ctxt, int flags, 3742321964Ssjg int *lengthPtr, void **freePtr) 3743236769Sobrien{ 3744236769Sobrien const char *tstr; /* Pointer into str */ 3745236769Sobrien Var *v; /* Variable in invocation */ 3746236769Sobrien Boolean haveModifier;/* TRUE if have modifiers for the variable */ 3747236769Sobrien char endc; /* Ending character when variable in parens 3748236769Sobrien * or braces */ 3749236769Sobrien char startc; /* Starting character when variable in parens 3750236769Sobrien * or braces */ 3751236769Sobrien int vlen; /* Length of variable name */ 3752236769Sobrien const char *start; /* Points to original start of str */ 3753236769Sobrien char *nstr; /* New string, used during expansion */ 3754236769Sobrien Boolean dynamic; /* TRUE if the variable is local and we're 3755236769Sobrien * expanding it in a non-local context. This 3756236769Sobrien * is done to support dynamic sources. The 3757236769Sobrien * result is just the invocation, unaltered */ 3758292068Ssjg const char *extramodifiers; /* extra modifiers to apply first */ 3759236769Sobrien char name[2]; 3760236769Sobrien 3761236769Sobrien *freePtr = NULL; 3762292068Ssjg extramodifiers = NULL; 3763236769Sobrien dynamic = FALSE; 3764236769Sobrien start = str; 3765236769Sobrien 3766236769Sobrien startc = str[1]; 3767236769Sobrien if (startc != PROPEN && startc != BROPEN) { 3768236769Sobrien /* 3769236769Sobrien * If it's not bounded by braces of some sort, life is much simpler. 3770236769Sobrien * We just need to check for the first character and return the 3771236769Sobrien * value if it exists. 3772236769Sobrien */ 3773236769Sobrien 3774236769Sobrien /* Error out some really stupid names */ 3775236769Sobrien if (startc == '\0' || strchr(")}:$", startc)) { 3776236769Sobrien *lengthPtr = 1; 3777236769Sobrien return var_Error; 3778236769Sobrien } 3779236769Sobrien name[0] = startc; 3780236769Sobrien name[1] = '\0'; 3781236769Sobrien 3782236769Sobrien v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 3783236769Sobrien if (v == NULL) { 3784236769Sobrien *lengthPtr = 2; 3785236769Sobrien 3786236769Sobrien if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) { 3787236769Sobrien /* 3788236769Sobrien * If substituting a local variable in a non-local context, 3789236769Sobrien * assume it's for dynamic source stuff. We have to handle 3790236769Sobrien * this specially and return the longhand for the variable 3791236769Sobrien * with the dollar sign escaped so it makes it back to the 3792236769Sobrien * caller. Only four of the local variables are treated 3793236769Sobrien * specially as they are the only four that will be set 3794236769Sobrien * when dynamic sources are expanded. 3795236769Sobrien */ 3796236769Sobrien switch (str[1]) { 3797236769Sobrien case '@': 3798236769Sobrien return UNCONST("$(.TARGET)"); 3799236769Sobrien case '%': 3800321964Ssjg return UNCONST("$(.MEMBER)"); 3801236769Sobrien case '*': 3802236769Sobrien return UNCONST("$(.PREFIX)"); 3803236769Sobrien case '!': 3804321964Ssjg return UNCONST("$(.ARCHIVE)"); 3805236769Sobrien } 3806236769Sobrien } 3807236769Sobrien /* 3808236769Sobrien * Error 3809236769Sobrien */ 3810321964Ssjg return (flags & VARF_UNDEFERR) ? var_Error : varNoError; 3811236769Sobrien } else { 3812236769Sobrien haveModifier = FALSE; 3813236769Sobrien tstr = &str[1]; 3814236769Sobrien endc = str[1]; 3815236769Sobrien } 3816236769Sobrien } else { 3817236769Sobrien Buffer buf; /* Holds the variable name */ 3818276305Sngie int depth = 1; 3819236769Sobrien 3820236769Sobrien endc = startc == PROPEN ? PRCLOSE : BRCLOSE; 3821236769Sobrien Buf_Init(&buf, 0); 3822236769Sobrien 3823236769Sobrien /* 3824236769Sobrien * Skip to the end character or a colon, whichever comes first. 3825236769Sobrien */ 3826276305Sngie for (tstr = str + 2; *tstr != '\0'; tstr++) 3827236769Sobrien { 3828236769Sobrien /* 3829276305Sngie * Track depth so we can spot parse errors. 3830276305Sngie */ 3831276305Sngie if (*tstr == startc) { 3832276305Sngie depth++; 3833276305Sngie } 3834276305Sngie if (*tstr == endc) { 3835276305Sngie if (--depth == 0) 3836276305Sngie break; 3837276305Sngie } 3838276305Sngie if (depth == 1 && *tstr == ':') { 3839276305Sngie break; 3840276305Sngie } 3841276305Sngie /* 3842236769Sobrien * A variable inside a variable, expand 3843236769Sobrien */ 3844236769Sobrien if (*tstr == '$') { 3845236769Sobrien int rlen; 3846236769Sobrien void *freeIt; 3847321964Ssjg char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt); 3848236769Sobrien if (rval != NULL) { 3849236769Sobrien Buf_AddBytes(&buf, strlen(rval), rval); 3850236769Sobrien } 3851292068Ssjg free(freeIt); 3852236769Sobrien tstr += rlen - 1; 3853236769Sobrien } 3854236769Sobrien else 3855236769Sobrien Buf_AddByte(&buf, *tstr); 3856236769Sobrien } 3857236769Sobrien if (*tstr == ':') { 3858236769Sobrien haveModifier = TRUE; 3859276305Sngie } else if (*tstr == endc) { 3860236769Sobrien haveModifier = FALSE; 3861236769Sobrien } else { 3862236769Sobrien /* 3863236769Sobrien * If we never did find the end character, return NULL 3864236769Sobrien * right now, setting the length to be the distance to 3865236769Sobrien * the end of the string, since that's what make does. 3866236769Sobrien */ 3867236769Sobrien *lengthPtr = tstr - str; 3868236769Sobrien Buf_Destroy(&buf, TRUE); 3869236769Sobrien return (var_Error); 3870236769Sobrien } 3871236769Sobrien str = Buf_GetAll(&buf, &vlen); 3872236769Sobrien 3873236769Sobrien /* 3874236769Sobrien * At this point, str points into newly allocated memory from 3875236769Sobrien * buf, containing only the name of the variable. 3876236769Sobrien * 3877236769Sobrien * start and tstr point into the const string that was pointed 3878236769Sobrien * to by the original value of the str parameter. start points 3879236769Sobrien * to the '$' at the beginning of the string, while tstr points 3880236769Sobrien * to the char just after the end of the variable name -- this 3881236769Sobrien * will be '\0', ':', PRCLOSE, or BRCLOSE. 3882236769Sobrien */ 3883236769Sobrien 3884236769Sobrien v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 3885236769Sobrien /* 3886236769Sobrien * Check also for bogus D and F forms of local variables since we're 3887236769Sobrien * in a local context and the name is the right length. 3888236769Sobrien */ 3889236769Sobrien if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) && 3890236769Sobrien (vlen == 2) && (str[1] == 'F' || str[1] == 'D') && 3891292068Ssjg strchr("@%?*!<>", str[0]) != NULL) { 3892236769Sobrien /* 3893236769Sobrien * Well, it's local -- go look for it. 3894236769Sobrien */ 3895236769Sobrien name[0] = *str; 3896236769Sobrien name[1] = '\0'; 3897236769Sobrien v = VarFind(name, ctxt, 0); 3898236769Sobrien 3899236769Sobrien if (v != NULL) { 3900236769Sobrien if (str[1] == 'D') { 3901292068Ssjg extramodifiers = "H:"; 3902236769Sobrien } 3903292068Ssjg else { /* F */ 3904292068Ssjg extramodifiers = "T:"; 3905292068Ssjg } 3906236769Sobrien } 3907236769Sobrien } 3908236769Sobrien 3909236769Sobrien if (v == NULL) { 3910236769Sobrien if (((vlen == 1) || 3911236769Sobrien (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) && 3912236769Sobrien ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) 3913236769Sobrien { 3914236769Sobrien /* 3915236769Sobrien * If substituting a local variable in a non-local context, 3916236769Sobrien * assume it's for dynamic source stuff. We have to handle 3917236769Sobrien * this specially and return the longhand for the variable 3918236769Sobrien * with the dollar sign escaped so it makes it back to the 3919236769Sobrien * caller. Only four of the local variables are treated 3920236769Sobrien * specially as they are the only four that will be set 3921236769Sobrien * when dynamic sources are expanded. 3922236769Sobrien */ 3923236769Sobrien switch (*str) { 3924236769Sobrien case '@': 3925236769Sobrien case '%': 3926236769Sobrien case '*': 3927236769Sobrien case '!': 3928236769Sobrien dynamic = TRUE; 3929236769Sobrien break; 3930236769Sobrien } 3931236769Sobrien } else if ((vlen > 2) && (*str == '.') && 3932236769Sobrien isupper((unsigned char) str[1]) && 3933236769Sobrien ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) 3934236769Sobrien { 3935236769Sobrien int len; 3936236769Sobrien 3937236769Sobrien len = vlen - 1; 3938236769Sobrien if ((strncmp(str, ".TARGET", len) == 0) || 3939236769Sobrien (strncmp(str, ".ARCHIVE", len) == 0) || 3940236769Sobrien (strncmp(str, ".PREFIX", len) == 0) || 3941236769Sobrien (strncmp(str, ".MEMBER", len) == 0)) 3942236769Sobrien { 3943236769Sobrien dynamic = TRUE; 3944236769Sobrien } 3945236769Sobrien } 3946236769Sobrien 3947236769Sobrien if (!haveModifier) { 3948236769Sobrien /* 3949236769Sobrien * No modifiers -- have specification length so we can return 3950236769Sobrien * now. 3951236769Sobrien */ 3952236769Sobrien *lengthPtr = tstr - start + 1; 3953236769Sobrien if (dynamic) { 3954236769Sobrien char *pstr = bmake_strndup(start, *lengthPtr); 3955236769Sobrien *freePtr = pstr; 3956236769Sobrien Buf_Destroy(&buf, TRUE); 3957236769Sobrien return(pstr); 3958236769Sobrien } else { 3959236769Sobrien Buf_Destroy(&buf, TRUE); 3960321964Ssjg return (flags & VARF_UNDEFERR) ? var_Error : varNoError; 3961236769Sobrien } 3962236769Sobrien } else { 3963236769Sobrien /* 3964236769Sobrien * Still need to get to the end of the variable specification, 3965236769Sobrien * so kludge up a Var structure for the modifications 3966236769Sobrien */ 3967236769Sobrien v = bmake_malloc(sizeof(Var)); 3968236769Sobrien v->name = UNCONST(str); 3969236769Sobrien Buf_Init(&v->val, 1); 3970236769Sobrien v->flags = VAR_JUNK; 3971236769Sobrien Buf_Destroy(&buf, FALSE); 3972236769Sobrien } 3973236769Sobrien } else 3974236769Sobrien Buf_Destroy(&buf, TRUE); 3975236769Sobrien } 3976236769Sobrien 3977236769Sobrien if (v->flags & VAR_IN_USE) { 3978236769Sobrien Fatal("Variable %s is recursive.", v->name); 3979236769Sobrien /*NOTREACHED*/ 3980236769Sobrien } else { 3981236769Sobrien v->flags |= VAR_IN_USE; 3982236769Sobrien } 3983236769Sobrien /* 3984236769Sobrien * Before doing any modification, we have to make sure the value 3985236769Sobrien * has been fully expanded. If it looks like recursion might be 3986236769Sobrien * necessary (there's a dollar sign somewhere in the variable's value) 3987236769Sobrien * we just call Var_Subst to do any other substitutions that are 3988236769Sobrien * necessary. Note that the value returned by Var_Subst will have 3989236769Sobrien * been dynamically-allocated, so it will need freeing when we 3990236769Sobrien * return. 3991236769Sobrien */ 3992236769Sobrien nstr = Buf_GetAll(&v->val, NULL); 3993236769Sobrien if (strchr(nstr, '$') != NULL) { 3994321964Ssjg nstr = Var_Subst(NULL, nstr, ctxt, flags); 3995236769Sobrien *freePtr = nstr; 3996236769Sobrien } 3997236769Sobrien 3998236769Sobrien v->flags &= ~VAR_IN_USE; 3999236769Sobrien 4000292068Ssjg if ((nstr != NULL) && (haveModifier || extramodifiers != NULL)) { 4001292068Ssjg void *extraFree; 4002236769Sobrien int used; 4003236769Sobrien 4004292068Ssjg extraFree = NULL; 4005292068Ssjg if (extramodifiers != NULL) { 4006292068Ssjg nstr = ApplyModifiers(nstr, extramodifiers, '(', ')', 4007321964Ssjg v, ctxt, flags, &used, &extraFree); 4008292068Ssjg } 4009292068Ssjg 4010292068Ssjg if (haveModifier) { 4011292068Ssjg /* Skip initial colon. */ 4012292068Ssjg tstr++; 4013292068Ssjg 4014292068Ssjg nstr = ApplyModifiers(nstr, tstr, startc, endc, 4015321964Ssjg v, ctxt, flags, &used, freePtr); 4016292068Ssjg tstr += used; 4017292068Ssjg free(extraFree); 4018292068Ssjg } else { 4019292068Ssjg *freePtr = extraFree; 4020292068Ssjg } 4021236769Sobrien } 4022236769Sobrien if (*tstr) { 4023236769Sobrien *lengthPtr = tstr - start + 1; 4024236769Sobrien } else { 4025236769Sobrien *lengthPtr = tstr - start; 4026236769Sobrien } 4027236769Sobrien 4028236769Sobrien if (v->flags & VAR_FROM_ENV) { 4029236769Sobrien Boolean destroy = FALSE; 4030236769Sobrien 4031236769Sobrien if (nstr != Buf_GetAll(&v->val, NULL)) { 4032236769Sobrien destroy = TRUE; 4033236769Sobrien } else { 4034236769Sobrien /* 4035236769Sobrien * Returning the value unmodified, so tell the caller to free 4036236769Sobrien * the thing. 4037236769Sobrien */ 4038236769Sobrien *freePtr = nstr; 4039236769Sobrien } 4040236769Sobrien VarFreeEnv(v, destroy); 4041236769Sobrien } else if (v->flags & VAR_JUNK) { 4042236769Sobrien /* 4043236769Sobrien * Perform any free'ing needed and set *freePtr to NULL so the caller 4044236769Sobrien * doesn't try to free a static pointer. 4045236769Sobrien * If VAR_KEEP is also set then we want to keep str as is. 4046236769Sobrien */ 4047236769Sobrien if (!(v->flags & VAR_KEEP)) { 4048236769Sobrien if (*freePtr) { 4049236769Sobrien free(nstr); 4050236769Sobrien *freePtr = NULL; 4051236769Sobrien } 4052236769Sobrien if (dynamic) { 4053236769Sobrien nstr = bmake_strndup(start, *lengthPtr); 4054236769Sobrien *freePtr = nstr; 4055236769Sobrien } else { 4056321964Ssjg nstr = (flags & VARF_UNDEFERR) ? var_Error : varNoError; 4057236769Sobrien } 4058236769Sobrien } 4059236769Sobrien if (nstr != Buf_GetAll(&v->val, NULL)) 4060236769Sobrien Buf_Destroy(&v->val, TRUE); 4061236769Sobrien free(v->name); 4062236769Sobrien free(v); 4063236769Sobrien } 4064236769Sobrien return (nstr); 4065236769Sobrien} 4066236769Sobrien 4067236769Sobrien/*- 4068236769Sobrien *----------------------------------------------------------------------- 4069236769Sobrien * Var_Subst -- 4070236769Sobrien * Substitute for all variables in the given string in the given context 4071321964Ssjg * If flags & VARF_UNDEFERR, Parse_Error will be called when an undefined 4072236769Sobrien * variable is encountered. 4073236769Sobrien * 4074236769Sobrien * Input: 4075236769Sobrien * var Named variable || NULL for all 4076236769Sobrien * str the string which to substitute 4077236769Sobrien * ctxt the context wherein to find variables 4078321964Ssjg * flags VARF_UNDEFERR if undefineds are an error 4079321964Ssjg * VARF_WANTRES if we actually want the result 4080321964Ssjg * VARF_ASSIGN if we are in a := assignment 4081236769Sobrien * 4082236769Sobrien * Results: 4083236769Sobrien * The resulting string. 4084236769Sobrien * 4085236769Sobrien * Side Effects: 4086236769Sobrien * None. The old string must be freed by the caller 4087236769Sobrien *----------------------------------------------------------------------- 4088236769Sobrien */ 4089236769Sobrienchar * 4090321964SsjgVar_Subst(const char *var, const char *str, GNode *ctxt, int flags) 4091236769Sobrien{ 4092236769Sobrien Buffer buf; /* Buffer for forming things */ 4093236769Sobrien char *val; /* Value to substitute for a variable */ 4094236769Sobrien int length; /* Length of the variable invocation */ 4095236769Sobrien Boolean trailingBslash; /* variable ends in \ */ 4096236769Sobrien void *freeIt = NULL; /* Set if it should be freed */ 4097236769Sobrien static Boolean errorReported; /* Set true if an error has already 4098236769Sobrien * been reported to prevent a plethora 4099236769Sobrien * of messages when recursing */ 4100236769Sobrien 4101236769Sobrien Buf_Init(&buf, 0); 4102236769Sobrien errorReported = FALSE; 4103236769Sobrien trailingBslash = FALSE; 4104236769Sobrien 4105236769Sobrien while (*str) { 4106236769Sobrien if (*str == '\n' && trailingBslash) 4107236769Sobrien Buf_AddByte(&buf, ' '); 4108236769Sobrien if (var == NULL && (*str == '$') && (str[1] == '$')) { 4109236769Sobrien /* 4110236769Sobrien * A dollar sign may be escaped either with another dollar sign. 4111236769Sobrien * In such a case, we skip over the escape character and store the 4112236769Sobrien * dollar sign into the buffer directly. 4113236769Sobrien */ 4114321964Ssjg if (save_dollars && (flags & VARF_ASSIGN)) 4115321964Ssjg Buf_AddByte(&buf, *str); 4116236769Sobrien str++; 4117236769Sobrien Buf_AddByte(&buf, *str); 4118236769Sobrien str++; 4119236769Sobrien } else if (*str != '$') { 4120236769Sobrien /* 4121236769Sobrien * Skip as many characters as possible -- either to the end of 4122236769Sobrien * the string or to the next dollar sign (variable invocation). 4123236769Sobrien */ 4124236769Sobrien const char *cp; 4125236769Sobrien 4126236769Sobrien for (cp = str++; *str != '$' && *str != '\0'; str++) 4127236769Sobrien continue; 4128236769Sobrien Buf_AddBytes(&buf, str - cp, cp); 4129236769Sobrien } else { 4130236769Sobrien if (var != NULL) { 4131236769Sobrien int expand; 4132236769Sobrien for (;;) { 4133236769Sobrien if (str[1] == '\0') { 4134236769Sobrien /* A trailing $ is kind of a special case */ 4135236769Sobrien Buf_AddByte(&buf, str[0]); 4136236769Sobrien str++; 4137236769Sobrien expand = FALSE; 4138236769Sobrien } else if (str[1] != PROPEN && str[1] != BROPEN) { 4139236769Sobrien if (str[1] != *var || strlen(var) > 1) { 4140236769Sobrien Buf_AddBytes(&buf, 2, str); 4141236769Sobrien str += 2; 4142236769Sobrien expand = FALSE; 4143236769Sobrien } 4144236769Sobrien else 4145236769Sobrien expand = TRUE; 4146236769Sobrien break; 4147236769Sobrien } 4148236769Sobrien else { 4149236769Sobrien const char *p; 4150236769Sobrien 4151236769Sobrien /* 4152236769Sobrien * Scan up to the end of the variable name. 4153236769Sobrien */ 4154236769Sobrien for (p = &str[2]; *p && 4155236769Sobrien *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++) 4156236769Sobrien if (*p == '$') 4157236769Sobrien break; 4158236769Sobrien /* 4159236769Sobrien * A variable inside the variable. We cannot expand 4160236769Sobrien * the external variable yet, so we try again with 4161236769Sobrien * the nested one 4162236769Sobrien */ 4163236769Sobrien if (*p == '$') { 4164236769Sobrien Buf_AddBytes(&buf, p - str, str); 4165236769Sobrien str = p; 4166236769Sobrien continue; 4167236769Sobrien } 4168236769Sobrien 4169236769Sobrien if (strncmp(var, str + 2, p - str - 2) != 0 || 4170236769Sobrien var[p - str - 2] != '\0') { 4171236769Sobrien /* 4172236769Sobrien * Not the variable we want to expand, scan 4173236769Sobrien * until the next variable 4174236769Sobrien */ 4175236769Sobrien for (;*p != '$' && *p != '\0'; p++) 4176236769Sobrien continue; 4177236769Sobrien Buf_AddBytes(&buf, p - str, str); 4178236769Sobrien str = p; 4179236769Sobrien expand = FALSE; 4180236769Sobrien } 4181236769Sobrien else 4182236769Sobrien expand = TRUE; 4183236769Sobrien break; 4184236769Sobrien } 4185236769Sobrien } 4186236769Sobrien if (!expand) 4187236769Sobrien continue; 4188236769Sobrien } 4189236769Sobrien 4190321964Ssjg val = Var_Parse(str, ctxt, flags, &length, &freeIt); 4191236769Sobrien 4192236769Sobrien /* 4193236769Sobrien * When we come down here, val should either point to the 4194236769Sobrien * value of this variable, suitably modified, or be NULL. 4195236769Sobrien * Length should be the total length of the potential 4196236769Sobrien * variable invocation (from $ to end character...) 4197236769Sobrien */ 4198236769Sobrien if (val == var_Error || val == varNoError) { 4199236769Sobrien /* 4200236769Sobrien * If performing old-time variable substitution, skip over 4201236769Sobrien * the variable and continue with the substitution. Otherwise, 4202236769Sobrien * store the dollar sign and advance str so we continue with 4203236769Sobrien * the string... 4204236769Sobrien */ 4205236769Sobrien if (oldVars) { 4206236769Sobrien str += length; 4207321964Ssjg } else if ((flags & VARF_UNDEFERR) || val == var_Error) { 4208236769Sobrien /* 4209236769Sobrien * If variable is undefined, complain and skip the 4210236769Sobrien * variable. The complaint will stop us from doing anything 4211236769Sobrien * when the file is parsed. 4212236769Sobrien */ 4213236769Sobrien if (!errorReported) { 4214236769Sobrien Parse_Error(PARSE_FATAL, 4215236769Sobrien "Undefined variable \"%.*s\"",length,str); 4216236769Sobrien } 4217236769Sobrien str += length; 4218236769Sobrien errorReported = TRUE; 4219236769Sobrien } else { 4220236769Sobrien Buf_AddByte(&buf, *str); 4221236769Sobrien str += 1; 4222236769Sobrien } 4223236769Sobrien } else { 4224236769Sobrien /* 4225236769Sobrien * We've now got a variable structure to store in. But first, 4226236769Sobrien * advance the string pointer. 4227236769Sobrien */ 4228236769Sobrien str += length; 4229236769Sobrien 4230236769Sobrien /* 4231236769Sobrien * Copy all the characters from the variable value straight 4232236769Sobrien * into the new string. 4233236769Sobrien */ 4234236769Sobrien length = strlen(val); 4235236769Sobrien Buf_AddBytes(&buf, length, val); 4236236769Sobrien trailingBslash = length > 0 && val[length - 1] == '\\'; 4237236769Sobrien } 4238292068Ssjg free(freeIt); 4239292068Ssjg freeIt = NULL; 4240236769Sobrien } 4241236769Sobrien } 4242236769Sobrien 4243236769Sobrien return Buf_DestroyCompact(&buf); 4244236769Sobrien} 4245236769Sobrien 4246236769Sobrien/*- 4247236769Sobrien *----------------------------------------------------------------------- 4248236769Sobrien * Var_GetTail -- 4249236769Sobrien * Return the tail from each of a list of words. Used to set the 4250236769Sobrien * System V local variables. 4251236769Sobrien * 4252236769Sobrien * Input: 4253236769Sobrien * file Filename to modify 4254236769Sobrien * 4255236769Sobrien * Results: 4256236769Sobrien * The resulting string. 4257236769Sobrien * 4258236769Sobrien * Side Effects: 4259236769Sobrien * None. 4260236769Sobrien * 4261236769Sobrien *----------------------------------------------------------------------- 4262236769Sobrien */ 4263236769Sobrien#if 0 4264236769Sobrienchar * 4265236769SobrienVar_GetTail(char *file) 4266236769Sobrien{ 4267236769Sobrien return(VarModify(file, VarTail, NULL)); 4268236769Sobrien} 4269236769Sobrien 4270236769Sobrien/*- 4271236769Sobrien *----------------------------------------------------------------------- 4272236769Sobrien * Var_GetHead -- 4273236769Sobrien * Find the leading components of a (list of) filename(s). 4274236769Sobrien * XXX: VarHead does not replace foo by ., as (sun) System V make 4275236769Sobrien * does. 4276236769Sobrien * 4277236769Sobrien * Input: 4278236769Sobrien * file Filename to manipulate 4279236769Sobrien * 4280236769Sobrien * Results: 4281236769Sobrien * The leading components. 4282236769Sobrien * 4283236769Sobrien * Side Effects: 4284236769Sobrien * None. 4285236769Sobrien * 4286236769Sobrien *----------------------------------------------------------------------- 4287236769Sobrien */ 4288236769Sobrienchar * 4289236769SobrienVar_GetHead(char *file) 4290236769Sobrien{ 4291236769Sobrien return(VarModify(file, VarHead, NULL)); 4292236769Sobrien} 4293236769Sobrien#endif 4294236769Sobrien 4295236769Sobrien/*- 4296236769Sobrien *----------------------------------------------------------------------- 4297236769Sobrien * Var_Init -- 4298236769Sobrien * Initialize the module 4299236769Sobrien * 4300236769Sobrien * Results: 4301236769Sobrien * None 4302236769Sobrien * 4303236769Sobrien * Side Effects: 4304236769Sobrien * The VAR_CMD and VAR_GLOBAL contexts are created 4305236769Sobrien *----------------------------------------------------------------------- 4306236769Sobrien */ 4307236769Sobrienvoid 4308236769SobrienVar_Init(void) 4309236769Sobrien{ 4310255253Ssjg VAR_INTERNAL = Targ_NewGN("Internal"); 4311236769Sobrien VAR_GLOBAL = Targ_NewGN("Global"); 4312236769Sobrien VAR_CMD = Targ_NewGN("Command"); 4313236769Sobrien 4314236769Sobrien} 4315236769Sobrien 4316236769Sobrien 4317236769Sobrienvoid 4318236769SobrienVar_End(void) 4319236769Sobrien{ 4320236769Sobrien} 4321236769Sobrien 4322236769Sobrien 4323236769Sobrien/****************** PRINT DEBUGGING INFO *****************/ 4324236769Sobrienstatic void 4325236769SobrienVarPrintVar(void *vp) 4326236769Sobrien{ 4327236769Sobrien Var *v = (Var *)vp; 4328236769Sobrien fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL)); 4329236769Sobrien} 4330236769Sobrien 4331236769Sobrien/*- 4332236769Sobrien *----------------------------------------------------------------------- 4333236769Sobrien * Var_Dump -- 4334236769Sobrien * print all variables in a context 4335236769Sobrien *----------------------------------------------------------------------- 4336236769Sobrien */ 4337236769Sobrienvoid 4338236769SobrienVar_Dump(GNode *ctxt) 4339236769Sobrien{ 4340236769Sobrien Hash_Search search; 4341236769Sobrien Hash_Entry *h; 4342236769Sobrien 4343236769Sobrien for (h = Hash_EnumFirst(&ctxt->context, &search); 4344236769Sobrien h != NULL; 4345236769Sobrien h = Hash_EnumNext(&search)) { 4346236769Sobrien VarPrintVar(Hash_GetValue(h)); 4347236769Sobrien } 4348236769Sobrien} 4349