1255253Ssjg/* $NetBSD: var.c,v 1.184 2013/09/04 15:38:26 sjg 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 72255253Ssjgstatic char rcsid[] = "$NetBSD: var.c,v 1.184 2013/09/04 15:38:26 sjg 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 79255253Ssjg__RCSID("$NetBSD: var.c,v 1.184 2013/09/04 15:38:26 sjg 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 <inttypes.h> 133236769Sobrien#include <stdlib.h> 134236769Sobrien#include <limits.h> 135236769Sobrien#include <time.h> 136236769Sobrien 137236769Sobrien#include "make.h" 138236769Sobrien#include "buf.h" 139236769Sobrien#include "dir.h" 140236769Sobrien#include "job.h" 141236769Sobrien 142253883Ssjgextern int makelevel; 143236769Sobrien/* 144250164Ssjg * XXX transition hack for FreeBSD ports. 145250164Ssjg * bsd.port.mk can set .MAKE.FreeBSD_UL=yes 146250164Ssjg * to cause us to treat :[LU] as aliases for :t[lu] 147250164Ssjg * To be reverted when ports converts to :t[lu] (when 8.3 is EOL) 148250164Ssjg */ 149250164Ssjg#define MAKE_FREEBSD_UL ".MAKE.FreeBSD_UL" 150250164Ssjg#ifdef MAKE_FREEBSD_UL 151250164Ssjgstatic int FreeBSD_UL = FALSE; 152250164Ssjg#endif 153250164Ssjg 154250164Ssjg/* 155236769Sobrien * This lets us tell if we have replaced the original environ 156236769Sobrien * (which we cannot free). 157236769Sobrien */ 158236769Sobrienchar **savedEnv = NULL; 159236769Sobrien 160236769Sobrien/* 161236769Sobrien * This is a harmless return value for Var_Parse that can be used by Var_Subst 162236769Sobrien * to determine if there was an error in parsing -- easier than returning 163236769Sobrien * a flag, as things outside this module don't give a hoot. 164236769Sobrien */ 165236769Sobrienchar var_Error[] = ""; 166236769Sobrien 167236769Sobrien/* 168236769Sobrien * Similar to var_Error, but returned when the 'errnum' flag for Var_Parse is 169236769Sobrien * set false. Why not just use a constant? Well, gcc likes to condense 170236769Sobrien * identical string instances... 171236769Sobrien */ 172236769Sobrienstatic char varNoError[] = ""; 173236769Sobrien 174236769Sobrien/* 175236769Sobrien * Internally, variables are contained in four different contexts. 176236769Sobrien * 1) the environment. They may not be changed. If an environment 177236769Sobrien * variable is appended-to, the result is placed in the global 178236769Sobrien * context. 179236769Sobrien * 2) the global context. Variables set in the Makefile are located in 180236769Sobrien * the global context. It is the penultimate context searched when 181236769Sobrien * substituting. 182236769Sobrien * 3) the command-line context. All variables set on the command line 183236769Sobrien * are placed in this context. They are UNALTERABLE once placed here. 184236769Sobrien * 4) the local context. Each target has associated with it a context 185236769Sobrien * list. On this list are located the structures describing such 186236769Sobrien * local variables as $(@) and $(*) 187236769Sobrien * The four contexts are searched in the reverse order from which they are 188236769Sobrien * listed. 189236769Sobrien */ 190255253SsjgGNode *VAR_INTERNAL; /* variables from make itself */ 191236769SobrienGNode *VAR_GLOBAL; /* variables from the makefile */ 192236769SobrienGNode *VAR_CMD; /* variables defined on the command-line */ 193236769Sobrien 194236769Sobrien#define FIND_CMD 0x1 /* look in VAR_CMD when searching */ 195236769Sobrien#define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */ 196236769Sobrien#define FIND_ENV 0x4 /* look in the environment also */ 197236769Sobrien 198236769Sobrientypedef struct Var { 199236769Sobrien char *name; /* the variable's name */ 200236769Sobrien Buffer val; /* its value */ 201236769Sobrien int flags; /* miscellaneous status flags */ 202236769Sobrien#define VAR_IN_USE 1 /* Variable's value currently being used. 203236769Sobrien * Used to avoid recursion */ 204236769Sobrien#define VAR_FROM_ENV 2 /* Variable comes from the environment */ 205236769Sobrien#define VAR_JUNK 4 /* Variable is a junk variable that 206236769Sobrien * should be destroyed when done with 207236769Sobrien * it. Used by Var_Parse for undefined, 208236769Sobrien * modified variables */ 209236769Sobrien#define VAR_KEEP 8 /* Variable is VAR_JUNK, but we found 210236769Sobrien * a use for it in some modifier and 211236769Sobrien * the value is therefore valid */ 212236769Sobrien#define VAR_EXPORTED 16 /* Variable is exported */ 213236769Sobrien#define VAR_REEXPORT 32 /* Indicate if var needs re-export. 214236769Sobrien * This would be true if it contains $'s 215236769Sobrien */ 216236769Sobrien#define VAR_FROM_CMD 64 /* Variable came from command line */ 217236769Sobrien} Var; 218236769Sobrien 219236769Sobrien/* 220236769Sobrien * Exporting vars is expensive so skip it if we can 221236769Sobrien */ 222236769Sobrien#define VAR_EXPORTED_NONE 0 223236769Sobrien#define VAR_EXPORTED_YES 1 224236769Sobrien#define VAR_EXPORTED_ALL 2 225236769Sobrienstatic int var_exportedVars = VAR_EXPORTED_NONE; 226236769Sobrien/* 227236769Sobrien * We pass this to Var_Export when doing the initial export 228236769Sobrien * or after updating an exported var. 229236769Sobrien */ 230236769Sobrien#define VAR_EXPORT_PARENT 1 231236769Sobrien 232236769Sobrien/* Var*Pattern flags */ 233236769Sobrien#define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */ 234236769Sobrien#define VAR_SUB_ONE 0x02 /* Apply substitution to one word */ 235236769Sobrien#define VAR_SUB_MATCHED 0x04 /* There was a match */ 236236769Sobrien#define VAR_MATCH_START 0x08 /* Match at start of word */ 237236769Sobrien#define VAR_MATCH_END 0x10 /* Match at end of word */ 238236769Sobrien#define VAR_NOSUBST 0x20 /* don't expand vars in VarGetPattern */ 239236769Sobrien 240236769Sobrien/* Var_Set flags */ 241236769Sobrien#define VAR_NO_EXPORT 0x01 /* do not export */ 242236769Sobrien 243236769Sobrientypedef struct { 244236769Sobrien /* 245236769Sobrien * The following fields are set by Var_Parse() when it 246236769Sobrien * encounters modifiers that need to keep state for use by 247236769Sobrien * subsequent modifiers within the same variable expansion. 248236769Sobrien */ 249236769Sobrien Byte varSpace; /* Word separator in expansions */ 250236769Sobrien Boolean oneBigWord; /* TRUE if we will treat the variable as a 251236769Sobrien * single big word, even if it contains 252236769Sobrien * embedded spaces (as opposed to the 253236769Sobrien * usual behaviour of treating it as 254236769Sobrien * several space-separated words). */ 255236769Sobrien} Var_Parse_State; 256236769Sobrien 257236769Sobrien/* struct passed as 'void *' to VarSubstitute() for ":S/lhs/rhs/", 258236769Sobrien * to VarSYSVMatch() for ":lhs=rhs". */ 259236769Sobrientypedef struct { 260236769Sobrien const char *lhs; /* String to match */ 261236769Sobrien int leftLen; /* Length of string */ 262236769Sobrien const char *rhs; /* Replacement string (w/ &'s removed) */ 263236769Sobrien int rightLen; /* Length of replacement */ 264236769Sobrien int flags; 265236769Sobrien} VarPattern; 266236769Sobrien 267236769Sobrien/* struct passed as 'void *' to VarLoopExpand() for ":@tvar@str@" */ 268236769Sobrientypedef struct { 269236769Sobrien GNode *ctxt; /* variable context */ 270236769Sobrien char *tvar; /* name of temp var */ 271236769Sobrien int tvarLen; 272236769Sobrien char *str; /* string to expand */ 273236769Sobrien int strLen; 274236769Sobrien int errnum; /* errnum for not defined */ 275236769Sobrien} VarLoop_t; 276236769Sobrien 277236769Sobrien#ifndef NO_REGEX 278236769Sobrien/* struct passed as 'void *' to VarRESubstitute() for ":C///" */ 279236769Sobrientypedef struct { 280236769Sobrien regex_t re; 281236769Sobrien int nsub; 282236769Sobrien regmatch_t *matches; 283236769Sobrien char *replace; 284236769Sobrien int flags; 285236769Sobrien} VarREPattern; 286236769Sobrien#endif 287236769Sobrien 288236769Sobrien/* struct passed to VarSelectWords() for ":[start..end]" */ 289236769Sobrientypedef struct { 290236769Sobrien int start; /* first word to select */ 291236769Sobrien int end; /* last word to select */ 292236769Sobrien} VarSelectWords_t; 293236769Sobrien 294236769Sobrienstatic Var *VarFind(const char *, GNode *, int); 295236769Sobrienstatic void VarAdd(const char *, const char *, GNode *); 296236769Sobrienstatic Boolean VarHead(GNode *, Var_Parse_State *, 297236769Sobrien char *, Boolean, Buffer *, void *); 298236769Sobrienstatic Boolean VarTail(GNode *, Var_Parse_State *, 299236769Sobrien char *, Boolean, Buffer *, void *); 300236769Sobrienstatic Boolean VarSuffix(GNode *, Var_Parse_State *, 301236769Sobrien char *, Boolean, Buffer *, void *); 302236769Sobrienstatic Boolean VarRoot(GNode *, Var_Parse_State *, 303236769Sobrien char *, Boolean, Buffer *, void *); 304236769Sobrienstatic Boolean VarMatch(GNode *, Var_Parse_State *, 305236769Sobrien char *, Boolean, Buffer *, void *); 306236769Sobrien#ifdef SYSVVARSUB 307236769Sobrienstatic Boolean VarSYSVMatch(GNode *, Var_Parse_State *, 308236769Sobrien char *, Boolean, Buffer *, void *); 309236769Sobrien#endif 310236769Sobrienstatic Boolean VarNoMatch(GNode *, Var_Parse_State *, 311236769Sobrien char *, Boolean, Buffer *, void *); 312236769Sobrien#ifndef NO_REGEX 313236769Sobrienstatic void VarREError(int, regex_t *, const char *); 314236769Sobrienstatic Boolean VarRESubstitute(GNode *, Var_Parse_State *, 315236769Sobrien char *, Boolean, Buffer *, void *); 316236769Sobrien#endif 317236769Sobrienstatic Boolean VarSubstitute(GNode *, Var_Parse_State *, 318236769Sobrien char *, Boolean, Buffer *, void *); 319236769Sobrienstatic Boolean VarLoopExpand(GNode *, Var_Parse_State *, 320236769Sobrien char *, Boolean, Buffer *, void *); 321236769Sobrienstatic char *VarGetPattern(GNode *, Var_Parse_State *, 322236769Sobrien int, const char **, int, int *, int *, 323236769Sobrien VarPattern *); 324236769Sobrienstatic char *VarQuote(char *); 325236769Sobrienstatic char *VarHash(char *); 326236769Sobrienstatic char *VarModify(GNode *, Var_Parse_State *, 327236769Sobrien const char *, 328236769Sobrien Boolean (*)(GNode *, Var_Parse_State *, char *, Boolean, Buffer *, void *), 329236769Sobrien void *); 330236769Sobrienstatic char *VarOrder(const char *, const char); 331236769Sobrienstatic char *VarUniq(const char *); 332236769Sobrienstatic int VarWordCompare(const void *, const void *); 333236769Sobrienstatic void VarPrintVar(void *); 334236769Sobrien 335236769Sobrien#define BROPEN '{' 336236769Sobrien#define BRCLOSE '}' 337236769Sobrien#define PROPEN '(' 338236769Sobrien#define PRCLOSE ')' 339236769Sobrien 340236769Sobrien/*- 341236769Sobrien *----------------------------------------------------------------------- 342236769Sobrien * VarFind -- 343236769Sobrien * Find the given variable in the given context and any other contexts 344236769Sobrien * indicated. 345236769Sobrien * 346236769Sobrien * Input: 347236769Sobrien * name name to find 348236769Sobrien * ctxt context in which to find it 349236769Sobrien * flags FIND_GLOBAL set means to look in the 350236769Sobrien * VAR_GLOBAL context as well. FIND_CMD set means 351236769Sobrien * to look in the VAR_CMD context also. FIND_ENV 352236769Sobrien * set means to look in the environment 353236769Sobrien * 354236769Sobrien * Results: 355236769Sobrien * A pointer to the structure describing the desired variable or 356236769Sobrien * NULL if the variable does not exist. 357236769Sobrien * 358236769Sobrien * Side Effects: 359236769Sobrien * None 360236769Sobrien *----------------------------------------------------------------------- 361236769Sobrien */ 362236769Sobrienstatic Var * 363236769SobrienVarFind(const char *name, GNode *ctxt, int flags) 364236769Sobrien{ 365236769Sobrien Hash_Entry *var; 366236769Sobrien Var *v; 367236769Sobrien 368236769Sobrien /* 369236769Sobrien * If the variable name begins with a '.', it could very well be one of 370236769Sobrien * the local ones. We check the name against all the local variables 371236769Sobrien * and substitute the short version in for 'name' if it matches one of 372236769Sobrien * them. 373236769Sobrien */ 374236769Sobrien if (*name == '.' && isupper((unsigned char) name[1])) 375236769Sobrien switch (name[1]) { 376236769Sobrien case 'A': 377236769Sobrien if (!strcmp(name, ".ALLSRC")) 378236769Sobrien name = ALLSRC; 379236769Sobrien if (!strcmp(name, ".ARCHIVE")) 380236769Sobrien name = ARCHIVE; 381236769Sobrien break; 382236769Sobrien case 'I': 383236769Sobrien if (!strcmp(name, ".IMPSRC")) 384236769Sobrien name = IMPSRC; 385236769Sobrien break; 386236769Sobrien case 'M': 387236769Sobrien if (!strcmp(name, ".MEMBER")) 388236769Sobrien name = MEMBER; 389236769Sobrien break; 390236769Sobrien case 'O': 391236769Sobrien if (!strcmp(name, ".OODATE")) 392236769Sobrien name = OODATE; 393236769Sobrien break; 394236769Sobrien case 'P': 395236769Sobrien if (!strcmp(name, ".PREFIX")) 396236769Sobrien name = PREFIX; 397236769Sobrien break; 398236769Sobrien case 'T': 399236769Sobrien if (!strcmp(name, ".TARGET")) 400236769Sobrien name = TARGET; 401236769Sobrien break; 402236769Sobrien } 403236769Sobrien#ifdef notyet 404236769Sobrien /* for compatibility with gmake */ 405236769Sobrien if (name[0] == '^' && name[1] == '\0') 406236769Sobrien name = ALLSRC; 407236769Sobrien#endif 408236769Sobrien 409236769Sobrien /* 410236769Sobrien * First look for the variable in the given context. If it's not there, 411236769Sobrien * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, 412236769Sobrien * depending on the FIND_* flags in 'flags' 413236769Sobrien */ 414236769Sobrien var = Hash_FindEntry(&ctxt->context, name); 415236769Sobrien 416236769Sobrien if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD)) { 417236769Sobrien var = Hash_FindEntry(&VAR_CMD->context, name); 418236769Sobrien } 419236769Sobrien if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) && 420236769Sobrien (ctxt != VAR_GLOBAL)) 421236769Sobrien { 422236769Sobrien var = Hash_FindEntry(&VAR_GLOBAL->context, name); 423255253Ssjg if ((var == NULL) && (ctxt != VAR_INTERNAL)) { 424255253Ssjg /* VAR_INTERNAL is subordinate to VAR_GLOBAL */ 425255253Ssjg var = Hash_FindEntry(&VAR_INTERNAL->context, name); 426255253Ssjg } 427236769Sobrien } 428236769Sobrien if ((var == NULL) && (flags & FIND_ENV)) { 429236769Sobrien char *env; 430236769Sobrien 431236769Sobrien if ((env = getenv(name)) != NULL) { 432236769Sobrien int len; 433236769Sobrien 434236769Sobrien v = bmake_malloc(sizeof(Var)); 435236769Sobrien v->name = bmake_strdup(name); 436236769Sobrien 437236769Sobrien len = strlen(env); 438236769Sobrien 439236769Sobrien Buf_Init(&v->val, len + 1); 440236769Sobrien Buf_AddBytes(&v->val, len, env); 441236769Sobrien 442236769Sobrien v->flags = VAR_FROM_ENV; 443236769Sobrien return (v); 444236769Sobrien } else if (checkEnvFirst && (flags & FIND_GLOBAL) && 445236769Sobrien (ctxt != VAR_GLOBAL)) 446236769Sobrien { 447236769Sobrien var = Hash_FindEntry(&VAR_GLOBAL->context, name); 448255253Ssjg if ((var == NULL) && (ctxt != VAR_INTERNAL)) { 449255253Ssjg var = Hash_FindEntry(&VAR_INTERNAL->context, name); 450255253Ssjg } 451236769Sobrien if (var == NULL) { 452236769Sobrien return NULL; 453236769Sobrien } else { 454236769Sobrien return ((Var *)Hash_GetValue(var)); 455236769Sobrien } 456236769Sobrien } else { 457236769Sobrien return NULL; 458236769Sobrien } 459236769Sobrien } else if (var == NULL) { 460236769Sobrien return NULL; 461236769Sobrien } else { 462236769Sobrien return ((Var *)Hash_GetValue(var)); 463236769Sobrien } 464236769Sobrien} 465236769Sobrien 466236769Sobrien/*- 467236769Sobrien *----------------------------------------------------------------------- 468236769Sobrien * VarFreeEnv -- 469236769Sobrien * If the variable is an environment variable, free it 470236769Sobrien * 471236769Sobrien * Input: 472236769Sobrien * v the variable 473236769Sobrien * destroy true if the value buffer should be destroyed. 474236769Sobrien * 475236769Sobrien * Results: 476236769Sobrien * 1 if it is an environment variable 0 ow. 477236769Sobrien * 478236769Sobrien * Side Effects: 479236769Sobrien * The variable is free'ed if it is an environent variable. 480236769Sobrien *----------------------------------------------------------------------- 481236769Sobrien */ 482236769Sobrienstatic Boolean 483236769SobrienVarFreeEnv(Var *v, Boolean destroy) 484236769Sobrien{ 485236769Sobrien if ((v->flags & VAR_FROM_ENV) == 0) 486236769Sobrien return FALSE; 487236769Sobrien free(v->name); 488236769Sobrien Buf_Destroy(&v->val, destroy); 489236769Sobrien free(v); 490236769Sobrien return TRUE; 491236769Sobrien} 492236769Sobrien 493236769Sobrien/*- 494236769Sobrien *----------------------------------------------------------------------- 495236769Sobrien * VarAdd -- 496236769Sobrien * Add a new variable of name name and value val to the given context 497236769Sobrien * 498236769Sobrien * Input: 499236769Sobrien * name name of variable to add 500236769Sobrien * val value to set it to 501236769Sobrien * ctxt context in which to set it 502236769Sobrien * 503236769Sobrien * Results: 504236769Sobrien * None 505236769Sobrien * 506236769Sobrien * Side Effects: 507236769Sobrien * The new variable is placed at the front of the given context 508236769Sobrien * The name and val arguments are duplicated so they may 509236769Sobrien * safely be freed. 510236769Sobrien *----------------------------------------------------------------------- 511236769Sobrien */ 512236769Sobrienstatic void 513236769SobrienVarAdd(const char *name, const char *val, GNode *ctxt) 514236769Sobrien{ 515236769Sobrien Var *v; 516236769Sobrien int len; 517236769Sobrien Hash_Entry *h; 518236769Sobrien 519236769Sobrien v = bmake_malloc(sizeof(Var)); 520236769Sobrien 521236769Sobrien len = val ? strlen(val) : 0; 522236769Sobrien Buf_Init(&v->val, len+1); 523236769Sobrien Buf_AddBytes(&v->val, len, val); 524236769Sobrien 525236769Sobrien v->flags = 0; 526236769Sobrien 527236769Sobrien h = Hash_CreateEntry(&ctxt->context, name, NULL); 528236769Sobrien Hash_SetValue(h, v); 529236769Sobrien v->name = h->name; 530236769Sobrien if (DEBUG(VAR)) { 531236769Sobrien fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); 532236769Sobrien } 533236769Sobrien} 534236769Sobrien 535236769Sobrien/*- 536236769Sobrien *----------------------------------------------------------------------- 537236769Sobrien * Var_Delete -- 538236769Sobrien * Remove a variable from a context. 539236769Sobrien * 540236769Sobrien * Results: 541236769Sobrien * None. 542236769Sobrien * 543236769Sobrien * Side Effects: 544236769Sobrien * The Var structure is removed and freed. 545236769Sobrien * 546236769Sobrien *----------------------------------------------------------------------- 547236769Sobrien */ 548236769Sobrienvoid 549236769SobrienVar_Delete(const char *name, GNode *ctxt) 550236769Sobrien{ 551236769Sobrien Hash_Entry *ln; 552250770Ssjg char *cp; 553250770Ssjg 554250770Ssjg if (strchr(name, '$')) { 555250770Ssjg cp = Var_Subst(NULL, name, VAR_GLOBAL, 0); 556250770Ssjg } else { 557250771Ssjg cp = (char *)name; 558250770Ssjg } 559250770Ssjg ln = Hash_FindEntry(&ctxt->context, cp); 560236769Sobrien if (DEBUG(VAR)) { 561236769Sobrien fprintf(debug_file, "%s:delete %s%s\n", 562250770Ssjg ctxt->name, cp, ln ? "" : " (not found)"); 563236769Sobrien } 564250770Ssjg if (cp != name) { 565250770Ssjg free(cp); 566250770Ssjg } 567236769Sobrien if (ln != NULL) { 568236769Sobrien Var *v; 569236769Sobrien 570236769Sobrien v = (Var *)Hash_GetValue(ln); 571236769Sobrien if ((v->flags & VAR_EXPORTED)) { 572236769Sobrien unsetenv(v->name); 573236769Sobrien } 574236769Sobrien if (strcmp(MAKE_EXPORTED, v->name) == 0) { 575236769Sobrien var_exportedVars = VAR_EXPORTED_NONE; 576236769Sobrien } 577236769Sobrien if (v->name != ln->name) 578236769Sobrien free(v->name); 579236769Sobrien Hash_DeleteEntry(&ctxt->context, ln); 580236769Sobrien Buf_Destroy(&v->val, TRUE); 581236769Sobrien free(v); 582236769Sobrien } 583236769Sobrien} 584236769Sobrien 585236769Sobrien 586236769Sobrien/* 587236769Sobrien * Export a var. 588236769Sobrien * We ignore make internal variables (those which start with '.') 589236769Sobrien * Also we jump through some hoops to avoid calling setenv 590236769Sobrien * more than necessary since it can leak. 591236769Sobrien * We only manipulate flags of vars if 'parent' is set. 592236769Sobrien */ 593236769Sobrienstatic int 594236769SobrienVar_Export1(const char *name, int parent) 595236769Sobrien{ 596236769Sobrien char tmp[BUFSIZ]; 597236769Sobrien Var *v; 598236769Sobrien char *val = NULL; 599236769Sobrien int n; 600236769Sobrien 601236769Sobrien if (*name == '.') 602236769Sobrien return 0; /* skip internals */ 603236769Sobrien if (!name[1]) { 604236769Sobrien /* 605236769Sobrien * A single char. 606236769Sobrien * If it is one of the vars that should only appear in 607236769Sobrien * local context, skip it, else we can get Var_Subst 608236769Sobrien * into a loop. 609236769Sobrien */ 610236769Sobrien switch (name[0]) { 611236769Sobrien case '@': 612236769Sobrien case '%': 613236769Sobrien case '*': 614236769Sobrien case '!': 615236769Sobrien return 0; 616236769Sobrien } 617236769Sobrien } 618236769Sobrien v = VarFind(name, VAR_GLOBAL, 0); 619236769Sobrien if (v == NULL) { 620236769Sobrien return 0; 621236769Sobrien } 622236769Sobrien if (!parent && 623236769Sobrien (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) { 624236769Sobrien return 0; /* nothing to do */ 625236769Sobrien } 626236769Sobrien val = Buf_GetAll(&v->val, NULL); 627236769Sobrien if (strchr(val, '$')) { 628236769Sobrien if (parent) { 629236769Sobrien /* 630236769Sobrien * Flag this as something we need to re-export. 631236769Sobrien * No point actually exporting it now though, 632236769Sobrien * the child can do it at the last minute. 633236769Sobrien */ 634236769Sobrien v->flags |= (VAR_EXPORTED|VAR_REEXPORT); 635236769Sobrien return 1; 636236769Sobrien } 637236769Sobrien if (v->flags & VAR_IN_USE) { 638236769Sobrien /* 639236769Sobrien * We recursed while exporting in a child. 640236769Sobrien * This isn't going to end well, just skip it. 641236769Sobrien */ 642236769Sobrien return 0; 643236769Sobrien } 644236769Sobrien n = snprintf(tmp, sizeof(tmp), "${%s}", name); 645236769Sobrien if (n < (int)sizeof(tmp)) { 646236769Sobrien val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 647236769Sobrien setenv(name, val, 1); 648236769Sobrien free(val); 649236769Sobrien } 650236769Sobrien } else { 651236769Sobrien if (parent) { 652236769Sobrien v->flags &= ~VAR_REEXPORT; /* once will do */ 653236769Sobrien } 654236769Sobrien if (parent || !(v->flags & VAR_EXPORTED)) { 655236769Sobrien setenv(name, val, 1); 656236769Sobrien } 657236769Sobrien } 658236769Sobrien /* 659236769Sobrien * This is so Var_Set knows to call Var_Export again... 660236769Sobrien */ 661236769Sobrien if (parent) { 662236769Sobrien v->flags |= VAR_EXPORTED; 663236769Sobrien } 664236769Sobrien return 1; 665236769Sobrien} 666236769Sobrien 667236769Sobrien/* 668236769Sobrien * This gets called from our children. 669236769Sobrien */ 670236769Sobrienvoid 671236769SobrienVar_ExportVars(void) 672236769Sobrien{ 673236769Sobrien char tmp[BUFSIZ]; 674236769Sobrien Hash_Entry *var; 675236769Sobrien Hash_Search state; 676236769Sobrien Var *v; 677236769Sobrien char *val; 678236769Sobrien int n; 679236769Sobrien 680253883Ssjg /* 681253883Ssjg * Several make's support this sort of mechanism for tracking 682253883Ssjg * recursion - but each uses a different name. 683253883Ssjg * We allow the makefiles to update MAKELEVEL and ensure 684253883Ssjg * children see a correctly incremented value. 685253883Ssjg */ 686253883Ssjg snprintf(tmp, sizeof(tmp), "%d", makelevel + 1); 687253883Ssjg setenv(MAKE_LEVEL_ENV, tmp, 1); 688253883Ssjg 689236769Sobrien if (VAR_EXPORTED_NONE == var_exportedVars) 690236769Sobrien return; 691236769Sobrien 692236769Sobrien if (VAR_EXPORTED_ALL == var_exportedVars) { 693236769Sobrien /* 694236769Sobrien * Ouch! This is crazy... 695236769Sobrien */ 696236769Sobrien for (var = Hash_EnumFirst(&VAR_GLOBAL->context, &state); 697236769Sobrien var != NULL; 698236769Sobrien var = Hash_EnumNext(&state)) { 699236769Sobrien v = (Var *)Hash_GetValue(var); 700236769Sobrien Var_Export1(v->name, 0); 701236769Sobrien } 702236769Sobrien return; 703236769Sobrien } 704236769Sobrien /* 705236769Sobrien * We have a number of exported vars, 706236769Sobrien */ 707236769Sobrien n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}"); 708236769Sobrien if (n < (int)sizeof(tmp)) { 709236769Sobrien char **av; 710236769Sobrien char *as; 711236769Sobrien int ac; 712236769Sobrien int i; 713236769Sobrien 714236769Sobrien val = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 715236769Sobrien av = brk_string(val, &ac, FALSE, &as); 716236769Sobrien for (i = 0; i < ac; i++) { 717236769Sobrien Var_Export1(av[i], 0); 718236769Sobrien } 719236769Sobrien free(val); 720236769Sobrien free(as); 721236769Sobrien free(av); 722236769Sobrien } 723236769Sobrien} 724236769Sobrien 725236769Sobrien/* 726236769Sobrien * This is called when .export is seen or 727236769Sobrien * .MAKE.EXPORTED is modified. 728236769Sobrien * It is also called when any exported var is modified. 729236769Sobrien */ 730236769Sobrienvoid 731236769SobrienVar_Export(char *str, int isExport) 732236769Sobrien{ 733236769Sobrien char *name; 734236769Sobrien char *val; 735236769Sobrien char **av; 736236769Sobrien char *as; 737236769Sobrien int track; 738236769Sobrien int ac; 739236769Sobrien int i; 740236769Sobrien 741236769Sobrien if (isExport && (!str || !str[0])) { 742236769Sobrien var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */ 743236769Sobrien return; 744236769Sobrien } 745236769Sobrien 746236769Sobrien if (strncmp(str, "-env", 4) == 0) { 747236769Sobrien track = 0; 748236769Sobrien str += 4; 749236769Sobrien } else { 750236769Sobrien track = VAR_EXPORT_PARENT; 751236769Sobrien } 752236769Sobrien val = Var_Subst(NULL, str, VAR_GLOBAL, 0); 753236769Sobrien av = brk_string(val, &ac, FALSE, &as); 754236769Sobrien for (i = 0; i < ac; i++) { 755236769Sobrien name = av[i]; 756236769Sobrien if (!name[1]) { 757236769Sobrien /* 758236769Sobrien * A single char. 759236769Sobrien * If it is one of the vars that should only appear in 760236769Sobrien * local context, skip it, else we can get Var_Subst 761236769Sobrien * into a loop. 762236769Sobrien */ 763236769Sobrien switch (name[0]) { 764236769Sobrien case '@': 765236769Sobrien case '%': 766236769Sobrien case '*': 767236769Sobrien case '!': 768236769Sobrien continue; 769236769Sobrien } 770236769Sobrien } 771236769Sobrien if (Var_Export1(name, track)) { 772236769Sobrien if (VAR_EXPORTED_ALL != var_exportedVars) 773236769Sobrien var_exportedVars = VAR_EXPORTED_YES; 774236769Sobrien if (isExport && track) { 775236769Sobrien Var_Append(MAKE_EXPORTED, name, VAR_GLOBAL); 776236769Sobrien } 777236769Sobrien } 778236769Sobrien } 779236769Sobrien free(val); 780236769Sobrien free(as); 781236769Sobrien free(av); 782236769Sobrien} 783236769Sobrien 784236769Sobrien 785236769Sobrien/* 786236769Sobrien * This is called when .unexport[-env] is seen. 787236769Sobrien */ 788236769Sobrienextern char **environ; 789236769Sobrien 790236769Sobrienvoid 791236769SobrienVar_UnExport(char *str) 792236769Sobrien{ 793236769Sobrien char tmp[BUFSIZ]; 794236769Sobrien char *vlist; 795236769Sobrien char *cp; 796236769Sobrien Boolean unexport_env; 797236769Sobrien int n; 798236769Sobrien 799236769Sobrien if (!str || !str[0]) { 800236769Sobrien return; /* assert? */ 801236769Sobrien } 802236769Sobrien 803236769Sobrien vlist = NULL; 804236769Sobrien 805236769Sobrien str += 8; 806236769Sobrien unexport_env = (strncmp(str, "-env", 4) == 0); 807236769Sobrien if (unexport_env) { 808236769Sobrien char **newenv; 809236769Sobrien 810253883Ssjg cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */ 811236769Sobrien if (environ == savedEnv) { 812236769Sobrien /* we have been here before! */ 813236769Sobrien newenv = bmake_realloc(environ, 2 * sizeof(char *)); 814236769Sobrien } else { 815236769Sobrien if (savedEnv) { 816236769Sobrien free(savedEnv); 817236769Sobrien savedEnv = NULL; 818236769Sobrien } 819236769Sobrien newenv = bmake_malloc(2 * sizeof(char *)); 820236769Sobrien } 821236769Sobrien if (!newenv) 822236769Sobrien return; 823236769Sobrien /* Note: we cannot safely free() the original environ. */ 824236769Sobrien environ = savedEnv = newenv; 825236769Sobrien newenv[0] = NULL; 826236769Sobrien newenv[1] = NULL; 827253883Ssjg setenv(MAKE_LEVEL_ENV, cp, 1); 828236769Sobrien } else { 829236769Sobrien for (; *str != '\n' && isspace((unsigned char) *str); str++) 830236769Sobrien continue; 831236769Sobrien if (str[0] && str[0] != '\n') { 832236769Sobrien vlist = str; 833236769Sobrien } 834236769Sobrien } 835236769Sobrien 836236769Sobrien if (!vlist) { 837236769Sobrien /* Using .MAKE.EXPORTED */ 838236769Sobrien n = snprintf(tmp, sizeof(tmp), "${" MAKE_EXPORTED ":O:u}"); 839236769Sobrien if (n < (int)sizeof(tmp)) { 840236769Sobrien vlist = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 841236769Sobrien } 842236769Sobrien } 843236769Sobrien if (vlist) { 844236769Sobrien Var *v; 845236769Sobrien char **av; 846236769Sobrien char *as; 847236769Sobrien int ac; 848236769Sobrien int i; 849236769Sobrien 850236769Sobrien av = brk_string(vlist, &ac, FALSE, &as); 851236769Sobrien for (i = 0; i < ac; i++) { 852236769Sobrien v = VarFind(av[i], VAR_GLOBAL, 0); 853236769Sobrien if (!v) 854236769Sobrien continue; 855236769Sobrien if (!unexport_env && 856236769Sobrien (v->flags & (VAR_EXPORTED|VAR_REEXPORT)) == VAR_EXPORTED) { 857236769Sobrien unsetenv(v->name); 858236769Sobrien } 859236769Sobrien v->flags &= ~(VAR_EXPORTED|VAR_REEXPORT); 860236769Sobrien /* 861236769Sobrien * If we are unexporting a list, 862236769Sobrien * remove each one from .MAKE.EXPORTED. 863236769Sobrien * If we are removing them all, 864236769Sobrien * just delete .MAKE.EXPORTED below. 865236769Sobrien */ 866236769Sobrien if (vlist == str) { 867236769Sobrien n = snprintf(tmp, sizeof(tmp), 868236769Sobrien "${" MAKE_EXPORTED ":N%s}", v->name); 869236769Sobrien if (n < (int)sizeof(tmp)) { 870236769Sobrien cp = Var_Subst(NULL, tmp, VAR_GLOBAL, 0); 871236769Sobrien Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL, 0); 872236769Sobrien free(cp); 873236769Sobrien } 874236769Sobrien } 875236769Sobrien } 876236769Sobrien free(as); 877236769Sobrien free(av); 878236769Sobrien if (vlist != str) { 879236769Sobrien Var_Delete(MAKE_EXPORTED, VAR_GLOBAL); 880236769Sobrien free(vlist); 881236769Sobrien } 882236769Sobrien } 883236769Sobrien} 884236769Sobrien 885236769Sobrien/*- 886236769Sobrien *----------------------------------------------------------------------- 887236769Sobrien * Var_Set -- 888236769Sobrien * Set the variable name to the value val in the given context. 889236769Sobrien * 890236769Sobrien * Input: 891236769Sobrien * name name of variable to set 892236769Sobrien * val value to give to the variable 893236769Sobrien * ctxt context in which to set it 894236769Sobrien * 895236769Sobrien * Results: 896236769Sobrien * None. 897236769Sobrien * 898236769Sobrien * Side Effects: 899236769Sobrien * If the variable doesn't yet exist, a new record is created for it. 900236769Sobrien * Else the old value is freed and the new one stuck in its place 901236769Sobrien * 902236769Sobrien * Notes: 903236769Sobrien * The variable is searched for only in its context before being 904236769Sobrien * created in that context. I.e. if the context is VAR_GLOBAL, 905236769Sobrien * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only 906236769Sobrien * VAR_CMD->context is searched. This is done to avoid the literally 907236769Sobrien * thousands of unnecessary strcmp's that used to be done to 908236769Sobrien * set, say, $(@) or $(<). 909236769Sobrien * If the context is VAR_GLOBAL though, we check if the variable 910236769Sobrien * was set in VAR_CMD from the command line and skip it if so. 911236769Sobrien *----------------------------------------------------------------------- 912236769Sobrien */ 913236769Sobrienvoid 914236769SobrienVar_Set(const char *name, const char *val, GNode *ctxt, int flags) 915236769Sobrien{ 916236769Sobrien Var *v; 917236769Sobrien char *expanded_name = NULL; 918236769Sobrien 919236769Sobrien /* 920236769Sobrien * We only look for a variable in the given context since anything set 921236769Sobrien * here will override anything in a lower context, so there's not much 922236769Sobrien * point in searching them all just to save a bit of memory... 923236769Sobrien */ 924236769Sobrien if (strchr(name, '$') != NULL) { 925236769Sobrien expanded_name = Var_Subst(NULL, name, ctxt, 0); 926236769Sobrien if (expanded_name[0] == 0) { 927236769Sobrien if (DEBUG(VAR)) { 928236769Sobrien fprintf(debug_file, "Var_Set(\"%s\", \"%s\", ...) " 929236769Sobrien "name expands to empty string - ignored\n", 930236769Sobrien name, val); 931236769Sobrien } 932236769Sobrien free(expanded_name); 933236769Sobrien return; 934236769Sobrien } 935236769Sobrien name = expanded_name; 936236769Sobrien } 937236769Sobrien if (ctxt == VAR_GLOBAL) { 938236769Sobrien v = VarFind(name, VAR_CMD, 0); 939236769Sobrien if (v != NULL) { 940236769Sobrien if ((v->flags & VAR_FROM_CMD)) { 941236769Sobrien if (DEBUG(VAR)) { 942236769Sobrien fprintf(debug_file, "%s:%s = %s ignored!\n", ctxt->name, name, val); 943236769Sobrien } 944236769Sobrien goto out; 945236769Sobrien } 946236769Sobrien VarFreeEnv(v, TRUE); 947236769Sobrien } 948236769Sobrien } 949236769Sobrien v = VarFind(name, ctxt, 0); 950236769Sobrien if (v == NULL) { 951253883Ssjg if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { 952253883Ssjg /* 953253883Ssjg * This var would normally prevent the same name being added 954253883Ssjg * to VAR_GLOBAL, so delete it from there if needed. 955253883Ssjg * Otherwise -V name may show the wrong value. 956253883Ssjg */ 957253883Ssjg Var_Delete(name, VAR_GLOBAL); 958253883Ssjg } 959236769Sobrien VarAdd(name, val, ctxt); 960236769Sobrien } else { 961236769Sobrien Buf_Empty(&v->val); 962236769Sobrien Buf_AddBytes(&v->val, strlen(val), val); 963236769Sobrien 964236769Sobrien if (DEBUG(VAR)) { 965236769Sobrien fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, val); 966236769Sobrien } 967236769Sobrien if ((v->flags & VAR_EXPORTED)) { 968236769Sobrien Var_Export1(name, VAR_EXPORT_PARENT); 969236769Sobrien } 970236769Sobrien } 971236769Sobrien /* 972236769Sobrien * Any variables given on the command line are automatically exported 973236769Sobrien * to the environment (as per POSIX standard) 974236769Sobrien */ 975236769Sobrien if (ctxt == VAR_CMD && (flags & VAR_NO_EXPORT) == 0) { 976236769Sobrien if (v == NULL) { 977236769Sobrien /* we just added it */ 978236769Sobrien v = VarFind(name, ctxt, 0); 979236769Sobrien } 980236769Sobrien if (v != NULL) 981236769Sobrien v->flags |= VAR_FROM_CMD; 982236769Sobrien /* 983236769Sobrien * If requested, don't export these in the environment 984236769Sobrien * individually. We still put them in MAKEOVERRIDES so 985236769Sobrien * that the command-line settings continue to override 986236769Sobrien * Makefile settings. 987236769Sobrien */ 988236769Sobrien if (varNoExportEnv != TRUE) 989236769Sobrien setenv(name, val, 1); 990236769Sobrien 991236769Sobrien Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL); 992236769Sobrien } 993236769Sobrien 994250164Ssjg#ifdef MAKE_FREEBSD_UL 995250164Ssjg if (strcmp(MAKE_FREEBSD_UL, name) == 0) { 996250164Ssjg FreeBSD_UL = getBoolean(MAKE_FREEBSD_UL, FALSE); 997250164Ssjg } 998250164Ssjg#endif 999253883Ssjg 1000253883Ssjg 1001236769Sobrien out: 1002236769Sobrien if (expanded_name != NULL) 1003236769Sobrien free(expanded_name); 1004236769Sobrien if (v != NULL) 1005236769Sobrien VarFreeEnv(v, TRUE); 1006236769Sobrien} 1007236769Sobrien 1008236769Sobrien/*- 1009236769Sobrien *----------------------------------------------------------------------- 1010236769Sobrien * Var_Append -- 1011236769Sobrien * The variable of the given name has the given value appended to it in 1012236769Sobrien * the given context. 1013236769Sobrien * 1014236769Sobrien * Input: 1015236769Sobrien * name name of variable to modify 1016236769Sobrien * val String to append to it 1017236769Sobrien * ctxt Context in which this should occur 1018236769Sobrien * 1019236769Sobrien * Results: 1020236769Sobrien * None 1021236769Sobrien * 1022236769Sobrien * Side Effects: 1023236769Sobrien * If the variable doesn't exist, it is created. Else the strings 1024236769Sobrien * are concatenated (with a space in between). 1025236769Sobrien * 1026236769Sobrien * Notes: 1027236769Sobrien * Only if the variable is being sought in the global context is the 1028236769Sobrien * environment searched. 1029236769Sobrien * XXX: Knows its calling circumstances in that if called with ctxt 1030236769Sobrien * an actual target, it will only search that context since only 1031236769Sobrien * a local variable could be being appended to. This is actually 1032236769Sobrien * a big win and must be tolerated. 1033236769Sobrien *----------------------------------------------------------------------- 1034236769Sobrien */ 1035236769Sobrienvoid 1036236769SobrienVar_Append(const char *name, const char *val, GNode *ctxt) 1037236769Sobrien{ 1038236769Sobrien Var *v; 1039236769Sobrien Hash_Entry *h; 1040236769Sobrien char *expanded_name = NULL; 1041236769Sobrien 1042236769Sobrien if (strchr(name, '$') != NULL) { 1043236769Sobrien expanded_name = Var_Subst(NULL, name, ctxt, 0); 1044236769Sobrien if (expanded_name[0] == 0) { 1045236769Sobrien if (DEBUG(VAR)) { 1046236769Sobrien fprintf(debug_file, "Var_Append(\"%s\", \"%s\", ...) " 1047236769Sobrien "name expands to empty string - ignored\n", 1048236769Sobrien name, val); 1049236769Sobrien } 1050236769Sobrien free(expanded_name); 1051236769Sobrien return; 1052236769Sobrien } 1053236769Sobrien name = expanded_name; 1054236769Sobrien } 1055236769Sobrien 1056236769Sobrien v = VarFind(name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0); 1057236769Sobrien 1058236769Sobrien if (v == NULL) { 1059236769Sobrien VarAdd(name, val, ctxt); 1060236769Sobrien } else { 1061236769Sobrien Buf_AddByte(&v->val, ' '); 1062236769Sobrien Buf_AddBytes(&v->val, strlen(val), val); 1063236769Sobrien 1064236769Sobrien if (DEBUG(VAR)) { 1065236769Sobrien fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name, 1066236769Sobrien Buf_GetAll(&v->val, NULL)); 1067236769Sobrien } 1068236769Sobrien 1069236769Sobrien if (v->flags & VAR_FROM_ENV) { 1070236769Sobrien /* 1071236769Sobrien * If the original variable came from the environment, we 1072236769Sobrien * have to install it in the global context (we could place 1073236769Sobrien * it in the environment, but then we should provide a way to 1074236769Sobrien * export other variables...) 1075236769Sobrien */ 1076236769Sobrien v->flags &= ~VAR_FROM_ENV; 1077236769Sobrien h = Hash_CreateEntry(&ctxt->context, name, NULL); 1078236769Sobrien Hash_SetValue(h, v); 1079236769Sobrien } 1080236769Sobrien } 1081236769Sobrien if (expanded_name != NULL) 1082236769Sobrien free(expanded_name); 1083236769Sobrien} 1084236769Sobrien 1085236769Sobrien/*- 1086236769Sobrien *----------------------------------------------------------------------- 1087236769Sobrien * Var_Exists -- 1088236769Sobrien * See if the given variable exists. 1089236769Sobrien * 1090236769Sobrien * Input: 1091236769Sobrien * name Variable to find 1092236769Sobrien * ctxt Context in which to start search 1093236769Sobrien * 1094236769Sobrien * Results: 1095236769Sobrien * TRUE if it does, FALSE if it doesn't 1096236769Sobrien * 1097236769Sobrien * Side Effects: 1098236769Sobrien * None. 1099236769Sobrien * 1100236769Sobrien *----------------------------------------------------------------------- 1101236769Sobrien */ 1102236769SobrienBoolean 1103236769SobrienVar_Exists(const char *name, GNode *ctxt) 1104236769Sobrien{ 1105236769Sobrien Var *v; 1106236769Sobrien char *cp; 1107236769Sobrien 1108236769Sobrien if ((cp = strchr(name, '$')) != NULL) { 1109236769Sobrien cp = Var_Subst(NULL, name, ctxt, FALSE); 1110236769Sobrien } 1111236769Sobrien v = VarFind(cp ? cp : name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV); 1112236769Sobrien if (cp != NULL) { 1113236769Sobrien free(cp); 1114236769Sobrien } 1115236769Sobrien if (v == NULL) { 1116236769Sobrien return(FALSE); 1117236769Sobrien } else { 1118236769Sobrien (void)VarFreeEnv(v, TRUE); 1119236769Sobrien } 1120236769Sobrien return(TRUE); 1121236769Sobrien} 1122236769Sobrien 1123236769Sobrien/*- 1124236769Sobrien *----------------------------------------------------------------------- 1125236769Sobrien * Var_Value -- 1126236769Sobrien * Return the value of the named variable in the given context 1127236769Sobrien * 1128236769Sobrien * Input: 1129236769Sobrien * name name to find 1130236769Sobrien * ctxt context in which to search for it 1131236769Sobrien * 1132236769Sobrien * Results: 1133236769Sobrien * The value if the variable exists, NULL if it doesn't 1134236769Sobrien * 1135236769Sobrien * Side Effects: 1136236769Sobrien * None 1137236769Sobrien *----------------------------------------------------------------------- 1138236769Sobrien */ 1139236769Sobrienchar * 1140236769SobrienVar_Value(const char *name, GNode *ctxt, char **frp) 1141236769Sobrien{ 1142236769Sobrien Var *v; 1143236769Sobrien 1144236769Sobrien v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 1145236769Sobrien *frp = NULL; 1146236769Sobrien if (v != NULL) { 1147236769Sobrien char *p = (Buf_GetAll(&v->val, NULL)); 1148236769Sobrien if (VarFreeEnv(v, FALSE)) 1149236769Sobrien *frp = p; 1150236769Sobrien return p; 1151236769Sobrien } else { 1152236769Sobrien return NULL; 1153236769Sobrien } 1154236769Sobrien} 1155236769Sobrien 1156236769Sobrien/*- 1157236769Sobrien *----------------------------------------------------------------------- 1158236769Sobrien * VarHead -- 1159236769Sobrien * Remove the tail of the given word and place the result in the given 1160236769Sobrien * buffer. 1161236769Sobrien * 1162236769Sobrien * Input: 1163236769Sobrien * word Word to trim 1164236769Sobrien * addSpace True if need to add a space to the buffer 1165236769Sobrien * before sticking in the head 1166236769Sobrien * buf Buffer in which to store it 1167236769Sobrien * 1168236769Sobrien * Results: 1169236769Sobrien * TRUE if characters were added to the buffer (a space needs to be 1170236769Sobrien * added to the buffer before the next word). 1171236769Sobrien * 1172236769Sobrien * Side Effects: 1173236769Sobrien * The trimmed word is added to the buffer. 1174236769Sobrien * 1175236769Sobrien *----------------------------------------------------------------------- 1176236769Sobrien */ 1177236769Sobrienstatic Boolean 1178237578SobrienVarHead(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1179236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1180236769Sobrien void *dummy) 1181236769Sobrien{ 1182236769Sobrien char *slash; 1183236769Sobrien 1184236769Sobrien slash = strrchr(word, '/'); 1185236769Sobrien if (slash != NULL) { 1186236769Sobrien if (addSpace && vpstate->varSpace) { 1187236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1188236769Sobrien } 1189236769Sobrien *slash = '\0'; 1190236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1191236769Sobrien *slash = '/'; 1192236769Sobrien return (TRUE); 1193236769Sobrien } else { 1194236769Sobrien /* 1195236769Sobrien * If no directory part, give . (q.v. the POSIX standard) 1196236769Sobrien */ 1197236769Sobrien if (addSpace && vpstate->varSpace) 1198236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1199236769Sobrien Buf_AddByte(buf, '.'); 1200236769Sobrien } 1201236769Sobrien return(dummy ? TRUE : TRUE); 1202236769Sobrien} 1203236769Sobrien 1204236769Sobrien/*- 1205236769Sobrien *----------------------------------------------------------------------- 1206236769Sobrien * VarTail -- 1207236769Sobrien * Remove the head of the given word and place the result in the given 1208236769Sobrien * buffer. 1209236769Sobrien * 1210236769Sobrien * Input: 1211236769Sobrien * word Word to trim 1212236769Sobrien * addSpace True if need to add a space to the buffer 1213236769Sobrien * before adding the tail 1214236769Sobrien * buf Buffer in which to store it 1215236769Sobrien * 1216236769Sobrien * Results: 1217236769Sobrien * TRUE if characters were added to the buffer (a space needs to be 1218236769Sobrien * added to the buffer before the next word). 1219236769Sobrien * 1220236769Sobrien * Side Effects: 1221236769Sobrien * The trimmed word is added to the buffer. 1222236769Sobrien * 1223236769Sobrien *----------------------------------------------------------------------- 1224236769Sobrien */ 1225236769Sobrienstatic Boolean 1226237578SobrienVarTail(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1227236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1228236769Sobrien void *dummy) 1229236769Sobrien{ 1230236769Sobrien char *slash; 1231236769Sobrien 1232236769Sobrien if (addSpace && vpstate->varSpace) { 1233236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1234236769Sobrien } 1235236769Sobrien 1236236769Sobrien slash = strrchr(word, '/'); 1237236769Sobrien if (slash != NULL) { 1238236769Sobrien *slash++ = '\0'; 1239236769Sobrien Buf_AddBytes(buf, strlen(slash), slash); 1240236769Sobrien slash[-1] = '/'; 1241236769Sobrien } else { 1242236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1243236769Sobrien } 1244236769Sobrien return (dummy ? TRUE : TRUE); 1245236769Sobrien} 1246236769Sobrien 1247236769Sobrien/*- 1248236769Sobrien *----------------------------------------------------------------------- 1249236769Sobrien * VarSuffix -- 1250236769Sobrien * Place the suffix of the given word in the given buffer. 1251236769Sobrien * 1252236769Sobrien * Input: 1253236769Sobrien * word Word to trim 1254236769Sobrien * addSpace TRUE if need to add a space before placing the 1255236769Sobrien * suffix in the buffer 1256236769Sobrien * buf Buffer in which to store it 1257236769Sobrien * 1258236769Sobrien * Results: 1259236769Sobrien * TRUE if characters were added to the buffer (a space needs to be 1260236769Sobrien * added to the buffer before the next word). 1261236769Sobrien * 1262236769Sobrien * Side Effects: 1263236769Sobrien * The suffix from the word is placed in the buffer. 1264236769Sobrien * 1265236769Sobrien *----------------------------------------------------------------------- 1266236769Sobrien */ 1267236769Sobrienstatic Boolean 1268237578SobrienVarSuffix(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1269236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1270236769Sobrien void *dummy) 1271236769Sobrien{ 1272236769Sobrien char *dot; 1273236769Sobrien 1274236769Sobrien dot = strrchr(word, '.'); 1275236769Sobrien if (dot != NULL) { 1276236769Sobrien if (addSpace && vpstate->varSpace) { 1277236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1278236769Sobrien } 1279236769Sobrien *dot++ = '\0'; 1280236769Sobrien Buf_AddBytes(buf, strlen(dot), dot); 1281236769Sobrien dot[-1] = '.'; 1282236769Sobrien addSpace = TRUE; 1283236769Sobrien } 1284236769Sobrien return (dummy ? addSpace : addSpace); 1285236769Sobrien} 1286236769Sobrien 1287236769Sobrien/*- 1288236769Sobrien *----------------------------------------------------------------------- 1289236769Sobrien * VarRoot -- 1290236769Sobrien * Remove the suffix of the given word and place the result in the 1291236769Sobrien * buffer. 1292236769Sobrien * 1293236769Sobrien * Input: 1294236769Sobrien * word Word to trim 1295236769Sobrien * addSpace TRUE if need to add a space to the buffer 1296236769Sobrien * before placing the root in it 1297236769Sobrien * buf Buffer in which to store it 1298236769Sobrien * 1299236769Sobrien * Results: 1300236769Sobrien * TRUE if characters were added to the buffer (a space needs to be 1301236769Sobrien * added to the buffer before the next word). 1302236769Sobrien * 1303236769Sobrien * Side Effects: 1304236769Sobrien * The trimmed word is added to the buffer. 1305236769Sobrien * 1306236769Sobrien *----------------------------------------------------------------------- 1307236769Sobrien */ 1308236769Sobrienstatic Boolean 1309237578SobrienVarRoot(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1310236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1311236769Sobrien void *dummy) 1312236769Sobrien{ 1313236769Sobrien char *dot; 1314236769Sobrien 1315236769Sobrien if (addSpace && vpstate->varSpace) { 1316236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1317236769Sobrien } 1318236769Sobrien 1319236769Sobrien dot = strrchr(word, '.'); 1320236769Sobrien if (dot != NULL) { 1321236769Sobrien *dot = '\0'; 1322236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1323236769Sobrien *dot = '.'; 1324236769Sobrien } else { 1325236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1326236769Sobrien } 1327236769Sobrien return (dummy ? TRUE : TRUE); 1328236769Sobrien} 1329236769Sobrien 1330236769Sobrien/*- 1331236769Sobrien *----------------------------------------------------------------------- 1332236769Sobrien * VarMatch -- 1333236769Sobrien * Place the word in the buffer if it matches the given pattern. 1334236769Sobrien * Callback function for VarModify to implement the :M modifier. 1335236769Sobrien * 1336236769Sobrien * Input: 1337236769Sobrien * word Word to examine 1338236769Sobrien * addSpace TRUE if need to add a space to the buffer 1339236769Sobrien * before adding the word, if it matches 1340236769Sobrien * buf Buffer in which to store it 1341236769Sobrien * pattern Pattern the word must match 1342236769Sobrien * 1343236769Sobrien * Results: 1344236769Sobrien * TRUE if a space should be placed in the buffer before the next 1345236769Sobrien * word. 1346236769Sobrien * 1347236769Sobrien * Side Effects: 1348236769Sobrien * The word may be copied to the buffer. 1349236769Sobrien * 1350236769Sobrien *----------------------------------------------------------------------- 1351236769Sobrien */ 1352236769Sobrienstatic Boolean 1353237578SobrienVarMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1354236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1355236769Sobrien void *pattern) 1356236769Sobrien{ 1357236769Sobrien if (DEBUG(VAR)) 1358236769Sobrien fprintf(debug_file, "VarMatch [%s] [%s]\n", word, (char *)pattern); 1359236769Sobrien if (Str_Match(word, (char *)pattern)) { 1360236769Sobrien if (addSpace && vpstate->varSpace) { 1361236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1362236769Sobrien } 1363236769Sobrien addSpace = TRUE; 1364236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1365236769Sobrien } 1366236769Sobrien return(addSpace); 1367236769Sobrien} 1368236769Sobrien 1369236769Sobrien#ifdef SYSVVARSUB 1370236769Sobrien/*- 1371236769Sobrien *----------------------------------------------------------------------- 1372236769Sobrien * VarSYSVMatch -- 1373236769Sobrien * Place the word in the buffer if it matches the given pattern. 1374236769Sobrien * Callback function for VarModify to implement the System V % 1375236769Sobrien * modifiers. 1376236769Sobrien * 1377236769Sobrien * Input: 1378236769Sobrien * word Word to examine 1379236769Sobrien * addSpace TRUE if need to add a space to the buffer 1380236769Sobrien * before adding the word, if it matches 1381236769Sobrien * buf Buffer in which to store it 1382236769Sobrien * patp Pattern the word must match 1383236769Sobrien * 1384236769Sobrien * Results: 1385236769Sobrien * TRUE if a space should be placed in the buffer before the next 1386236769Sobrien * word. 1387236769Sobrien * 1388236769Sobrien * Side Effects: 1389236769Sobrien * The word may be copied to the buffer. 1390236769Sobrien * 1391236769Sobrien *----------------------------------------------------------------------- 1392236769Sobrien */ 1393236769Sobrienstatic Boolean 1394236769SobrienVarSYSVMatch(GNode *ctx, Var_Parse_State *vpstate, 1395236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1396236769Sobrien void *patp) 1397236769Sobrien{ 1398236769Sobrien int len; 1399236769Sobrien char *ptr; 1400236769Sobrien VarPattern *pat = (VarPattern *)patp; 1401236769Sobrien char *varexp; 1402236769Sobrien 1403236769Sobrien if (addSpace && vpstate->varSpace) 1404236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1405236769Sobrien 1406236769Sobrien addSpace = TRUE; 1407236769Sobrien 1408236769Sobrien if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL) { 1409236769Sobrien varexp = Var_Subst(NULL, pat->rhs, ctx, 0); 1410236769Sobrien Str_SYSVSubst(buf, varexp, ptr, len); 1411236769Sobrien free(varexp); 1412236769Sobrien } else { 1413236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1414236769Sobrien } 1415236769Sobrien 1416236769Sobrien return(addSpace); 1417236769Sobrien} 1418236769Sobrien#endif 1419236769Sobrien 1420236769Sobrien 1421236769Sobrien/*- 1422236769Sobrien *----------------------------------------------------------------------- 1423236769Sobrien * VarNoMatch -- 1424236769Sobrien * Place the word in the buffer if it doesn't match the given pattern. 1425236769Sobrien * Callback function for VarModify to implement the :N modifier. 1426236769Sobrien * 1427236769Sobrien * Input: 1428236769Sobrien * word Word to examine 1429236769Sobrien * addSpace TRUE if need to add a space to the buffer 1430236769Sobrien * before adding the word, if it matches 1431236769Sobrien * buf Buffer in which to store it 1432236769Sobrien * pattern Pattern the word must match 1433236769Sobrien * 1434236769Sobrien * Results: 1435236769Sobrien * TRUE if a space should be placed in the buffer before the next 1436236769Sobrien * word. 1437236769Sobrien * 1438236769Sobrien * Side Effects: 1439236769Sobrien * The word may be copied to the buffer. 1440236769Sobrien * 1441236769Sobrien *----------------------------------------------------------------------- 1442236769Sobrien */ 1443236769Sobrienstatic Boolean 1444237578SobrienVarNoMatch(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1445236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1446236769Sobrien void *pattern) 1447236769Sobrien{ 1448236769Sobrien if (!Str_Match(word, (char *)pattern)) { 1449236769Sobrien if (addSpace && vpstate->varSpace) { 1450236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1451236769Sobrien } 1452236769Sobrien addSpace = TRUE; 1453236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1454236769Sobrien } 1455236769Sobrien return(addSpace); 1456236769Sobrien} 1457236769Sobrien 1458236769Sobrien 1459236769Sobrien/*- 1460236769Sobrien *----------------------------------------------------------------------- 1461236769Sobrien * VarSubstitute -- 1462236769Sobrien * Perform a string-substitution on the given word, placing the 1463236769Sobrien * result in the passed buffer. 1464236769Sobrien * 1465236769Sobrien * Input: 1466236769Sobrien * word Word to modify 1467236769Sobrien * addSpace True if space should be added before 1468236769Sobrien * other characters 1469236769Sobrien * buf Buffer for result 1470236769Sobrien * patternp Pattern for substitution 1471236769Sobrien * 1472236769Sobrien * Results: 1473236769Sobrien * TRUE if a space is needed before more characters are added. 1474236769Sobrien * 1475236769Sobrien * Side Effects: 1476236769Sobrien * None. 1477236769Sobrien * 1478236769Sobrien *----------------------------------------------------------------------- 1479236769Sobrien */ 1480236769Sobrienstatic Boolean 1481237578SobrienVarSubstitute(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1482236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1483236769Sobrien void *patternp) 1484236769Sobrien{ 1485236769Sobrien int wordLen; /* Length of word */ 1486236769Sobrien char *cp; /* General pointer */ 1487236769Sobrien VarPattern *pattern = (VarPattern *)patternp; 1488236769Sobrien 1489236769Sobrien wordLen = strlen(word); 1490236769Sobrien if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) != 1491236769Sobrien (VAR_SUB_ONE|VAR_SUB_MATCHED)) { 1492236769Sobrien /* 1493236769Sobrien * Still substituting -- break it down into simple anchored cases 1494236769Sobrien * and if none of them fits, perform the general substitution case. 1495236769Sobrien */ 1496236769Sobrien if ((pattern->flags & VAR_MATCH_START) && 1497236769Sobrien (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) { 1498236769Sobrien /* 1499236769Sobrien * Anchored at start and beginning of word matches pattern 1500236769Sobrien */ 1501236769Sobrien if ((pattern->flags & VAR_MATCH_END) && 1502236769Sobrien (wordLen == pattern->leftLen)) { 1503236769Sobrien /* 1504236769Sobrien * Also anchored at end and matches to the end (word 1505236769Sobrien * is same length as pattern) add space and rhs only 1506236769Sobrien * if rhs is non-null. 1507236769Sobrien */ 1508236769Sobrien if (pattern->rightLen != 0) { 1509236769Sobrien if (addSpace && vpstate->varSpace) { 1510236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1511236769Sobrien } 1512236769Sobrien addSpace = TRUE; 1513236769Sobrien Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1514236769Sobrien } 1515236769Sobrien pattern->flags |= VAR_SUB_MATCHED; 1516236769Sobrien } else if (pattern->flags & VAR_MATCH_END) { 1517236769Sobrien /* 1518236769Sobrien * Doesn't match to end -- copy word wholesale 1519236769Sobrien */ 1520236769Sobrien goto nosub; 1521236769Sobrien } else { 1522236769Sobrien /* 1523236769Sobrien * Matches at start but need to copy in trailing characters 1524236769Sobrien */ 1525236769Sobrien if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){ 1526236769Sobrien if (addSpace && vpstate->varSpace) { 1527236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1528236769Sobrien } 1529236769Sobrien addSpace = TRUE; 1530236769Sobrien } 1531236769Sobrien Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1532236769Sobrien Buf_AddBytes(buf, wordLen - pattern->leftLen, 1533236769Sobrien (word + pattern->leftLen)); 1534236769Sobrien pattern->flags |= VAR_SUB_MATCHED; 1535236769Sobrien } 1536236769Sobrien } else if (pattern->flags & VAR_MATCH_START) { 1537236769Sobrien /* 1538236769Sobrien * Had to match at start of word and didn't -- copy whole word. 1539236769Sobrien */ 1540236769Sobrien goto nosub; 1541236769Sobrien } else if (pattern->flags & VAR_MATCH_END) { 1542236769Sobrien /* 1543236769Sobrien * Anchored at end, Find only place match could occur (leftLen 1544236769Sobrien * characters from the end of the word) and see if it does. Note 1545236769Sobrien * that because the $ will be left at the end of the lhs, we have 1546236769Sobrien * to use strncmp. 1547236769Sobrien */ 1548236769Sobrien cp = word + (wordLen - pattern->leftLen); 1549236769Sobrien if ((cp >= word) && 1550236769Sobrien (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) { 1551236769Sobrien /* 1552236769Sobrien * Match found. If we will place characters in the buffer, 1553236769Sobrien * add a space before hand as indicated by addSpace, then 1554236769Sobrien * stuff in the initial, unmatched part of the word followed 1555236769Sobrien * by the right-hand-side. 1556236769Sobrien */ 1557236769Sobrien if (((cp - word) + pattern->rightLen) != 0) { 1558236769Sobrien if (addSpace && vpstate->varSpace) { 1559236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1560236769Sobrien } 1561236769Sobrien addSpace = TRUE; 1562236769Sobrien } 1563236769Sobrien Buf_AddBytes(buf, cp - word, word); 1564236769Sobrien Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1565236769Sobrien pattern->flags |= VAR_SUB_MATCHED; 1566236769Sobrien } else { 1567236769Sobrien /* 1568236769Sobrien * Had to match at end and didn't. Copy entire word. 1569236769Sobrien */ 1570236769Sobrien goto nosub; 1571236769Sobrien } 1572236769Sobrien } else { 1573236769Sobrien /* 1574236769Sobrien * Pattern is unanchored: search for the pattern in the word using 1575236769Sobrien * String_FindSubstring, copying unmatched portions and the 1576236769Sobrien * right-hand-side for each match found, handling non-global 1577236769Sobrien * substitutions correctly, etc. When the loop is done, any 1578236769Sobrien * remaining part of the word (word and wordLen are adjusted 1579236769Sobrien * accordingly through the loop) is copied straight into the 1580236769Sobrien * buffer. 1581236769Sobrien * addSpace is set FALSE as soon as a space is added to the 1582236769Sobrien * buffer. 1583236769Sobrien */ 1584236769Sobrien Boolean done; 1585236769Sobrien int origSize; 1586236769Sobrien 1587236769Sobrien done = FALSE; 1588236769Sobrien origSize = Buf_Size(buf); 1589236769Sobrien while (!done) { 1590236769Sobrien cp = Str_FindSubstring(word, pattern->lhs); 1591236769Sobrien if (cp != NULL) { 1592236769Sobrien if (addSpace && (((cp - word) + pattern->rightLen) != 0)){ 1593236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1594236769Sobrien addSpace = FALSE; 1595236769Sobrien } 1596236769Sobrien Buf_AddBytes(buf, cp-word, word); 1597236769Sobrien Buf_AddBytes(buf, pattern->rightLen, pattern->rhs); 1598236769Sobrien wordLen -= (cp - word) + pattern->leftLen; 1599236769Sobrien word = cp + pattern->leftLen; 1600236769Sobrien if (wordLen == 0) { 1601236769Sobrien done = TRUE; 1602236769Sobrien } 1603236769Sobrien if ((pattern->flags & VAR_SUB_GLOBAL) == 0) { 1604236769Sobrien done = TRUE; 1605236769Sobrien } 1606236769Sobrien pattern->flags |= VAR_SUB_MATCHED; 1607236769Sobrien } else { 1608236769Sobrien done = TRUE; 1609236769Sobrien } 1610236769Sobrien } 1611236769Sobrien if (wordLen != 0) { 1612236769Sobrien if (addSpace && vpstate->varSpace) { 1613236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1614236769Sobrien } 1615236769Sobrien Buf_AddBytes(buf, wordLen, word); 1616236769Sobrien } 1617236769Sobrien /* 1618236769Sobrien * If added characters to the buffer, need to add a space 1619236769Sobrien * before we add any more. If we didn't add any, just return 1620236769Sobrien * the previous value of addSpace. 1621236769Sobrien */ 1622236769Sobrien return ((Buf_Size(buf) != origSize) || addSpace); 1623236769Sobrien } 1624236769Sobrien return (addSpace); 1625236769Sobrien } 1626236769Sobrien nosub: 1627236769Sobrien if (addSpace && vpstate->varSpace) { 1628236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1629236769Sobrien } 1630236769Sobrien Buf_AddBytes(buf, wordLen, word); 1631236769Sobrien return(TRUE); 1632236769Sobrien} 1633236769Sobrien 1634236769Sobrien#ifndef NO_REGEX 1635236769Sobrien/*- 1636236769Sobrien *----------------------------------------------------------------------- 1637236769Sobrien * VarREError -- 1638236769Sobrien * Print the error caused by a regcomp or regexec call. 1639236769Sobrien * 1640236769Sobrien * Results: 1641236769Sobrien * None. 1642236769Sobrien * 1643236769Sobrien * Side Effects: 1644236769Sobrien * An error gets printed. 1645236769Sobrien * 1646236769Sobrien *----------------------------------------------------------------------- 1647236769Sobrien */ 1648236769Sobrienstatic void 1649236769SobrienVarREError(int errnum, regex_t *pat, const char *str) 1650236769Sobrien{ 1651236769Sobrien char *errbuf; 1652236769Sobrien int errlen; 1653236769Sobrien 1654236769Sobrien errlen = regerror(errnum, pat, 0, 0); 1655236769Sobrien errbuf = bmake_malloc(errlen); 1656236769Sobrien regerror(errnum, pat, errbuf, errlen); 1657236769Sobrien Error("%s: %s", str, errbuf); 1658236769Sobrien free(errbuf); 1659236769Sobrien} 1660236769Sobrien 1661236769Sobrien 1662236769Sobrien/*- 1663236769Sobrien *----------------------------------------------------------------------- 1664236769Sobrien * VarRESubstitute -- 1665236769Sobrien * Perform a regex substitution on the given word, placing the 1666236769Sobrien * result in the passed buffer. 1667236769Sobrien * 1668236769Sobrien * Results: 1669236769Sobrien * TRUE if a space is needed before more characters are added. 1670236769Sobrien * 1671236769Sobrien * Side Effects: 1672236769Sobrien * None. 1673236769Sobrien * 1674236769Sobrien *----------------------------------------------------------------------- 1675236769Sobrien */ 1676236769Sobrienstatic Boolean 1677237578SobrienVarRESubstitute(GNode *ctx MAKE_ATTR_UNUSED, 1678237578Sobrien Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 1679236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1680236769Sobrien void *patternp) 1681236769Sobrien{ 1682236769Sobrien VarREPattern *pat; 1683236769Sobrien int xrv; 1684236769Sobrien char *wp; 1685236769Sobrien char *rp; 1686236769Sobrien int added; 1687236769Sobrien int flags = 0; 1688236769Sobrien 1689236769Sobrien#define MAYBE_ADD_SPACE() \ 1690236769Sobrien if (addSpace && !added) \ 1691236769Sobrien Buf_AddByte(buf, ' '); \ 1692236769Sobrien added = 1 1693236769Sobrien 1694236769Sobrien added = 0; 1695236769Sobrien wp = word; 1696236769Sobrien pat = patternp; 1697236769Sobrien 1698236769Sobrien if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) == 1699236769Sobrien (VAR_SUB_ONE|VAR_SUB_MATCHED)) 1700236769Sobrien xrv = REG_NOMATCH; 1701236769Sobrien else { 1702236769Sobrien tryagain: 1703236769Sobrien xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, flags); 1704236769Sobrien } 1705236769Sobrien 1706236769Sobrien switch (xrv) { 1707236769Sobrien case 0: 1708236769Sobrien pat->flags |= VAR_SUB_MATCHED; 1709236769Sobrien if (pat->matches[0].rm_so > 0) { 1710236769Sobrien MAYBE_ADD_SPACE(); 1711236769Sobrien Buf_AddBytes(buf, pat->matches[0].rm_so, wp); 1712236769Sobrien } 1713236769Sobrien 1714236769Sobrien for (rp = pat->replace; *rp; rp++) { 1715236769Sobrien if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) { 1716236769Sobrien MAYBE_ADD_SPACE(); 1717236769Sobrien Buf_AddByte(buf,rp[1]); 1718236769Sobrien rp++; 1719236769Sobrien } 1720236769Sobrien else if ((*rp == '&') || 1721236769Sobrien ((*rp == '\\') && isdigit((unsigned char)rp[1]))) { 1722236769Sobrien int n; 1723236769Sobrien const char *subbuf; 1724236769Sobrien int sublen; 1725236769Sobrien char errstr[3]; 1726236769Sobrien 1727236769Sobrien if (*rp == '&') { 1728236769Sobrien n = 0; 1729236769Sobrien errstr[0] = '&'; 1730236769Sobrien errstr[1] = '\0'; 1731236769Sobrien } else { 1732236769Sobrien n = rp[1] - '0'; 1733236769Sobrien errstr[0] = '\\'; 1734236769Sobrien errstr[1] = rp[1]; 1735236769Sobrien errstr[2] = '\0'; 1736236769Sobrien rp++; 1737236769Sobrien } 1738236769Sobrien 1739236769Sobrien if (n > pat->nsub) { 1740236769Sobrien Error("No subexpression %s", &errstr[0]); 1741236769Sobrien subbuf = ""; 1742236769Sobrien sublen = 0; 1743236769Sobrien } else if ((pat->matches[n].rm_so == -1) && 1744236769Sobrien (pat->matches[n].rm_eo == -1)) { 1745236769Sobrien Error("No match for subexpression %s", &errstr[0]); 1746236769Sobrien subbuf = ""; 1747236769Sobrien sublen = 0; 1748236769Sobrien } else { 1749236769Sobrien subbuf = wp + pat->matches[n].rm_so; 1750236769Sobrien sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so; 1751236769Sobrien } 1752236769Sobrien 1753236769Sobrien if (sublen > 0) { 1754236769Sobrien MAYBE_ADD_SPACE(); 1755236769Sobrien Buf_AddBytes(buf, sublen, subbuf); 1756236769Sobrien } 1757236769Sobrien } else { 1758236769Sobrien MAYBE_ADD_SPACE(); 1759236769Sobrien Buf_AddByte(buf, *rp); 1760236769Sobrien } 1761236769Sobrien } 1762236769Sobrien wp += pat->matches[0].rm_eo; 1763236769Sobrien if (pat->flags & VAR_SUB_GLOBAL) { 1764236769Sobrien flags |= REG_NOTBOL; 1765236769Sobrien if (pat->matches[0].rm_so == 0 && pat->matches[0].rm_eo == 0) { 1766236769Sobrien MAYBE_ADD_SPACE(); 1767236769Sobrien Buf_AddByte(buf, *wp); 1768236769Sobrien wp++; 1769236769Sobrien 1770236769Sobrien } 1771236769Sobrien if (*wp) 1772236769Sobrien goto tryagain; 1773236769Sobrien } 1774236769Sobrien if (*wp) { 1775236769Sobrien MAYBE_ADD_SPACE(); 1776236769Sobrien Buf_AddBytes(buf, strlen(wp), wp); 1777236769Sobrien } 1778236769Sobrien break; 1779236769Sobrien default: 1780236769Sobrien VarREError(xrv, &pat->re, "Unexpected regex error"); 1781236769Sobrien /* fall through */ 1782236769Sobrien case REG_NOMATCH: 1783236769Sobrien if (*wp) { 1784236769Sobrien MAYBE_ADD_SPACE(); 1785236769Sobrien Buf_AddBytes(buf,strlen(wp),wp); 1786236769Sobrien } 1787236769Sobrien break; 1788236769Sobrien } 1789236769Sobrien return(addSpace||added); 1790236769Sobrien} 1791236769Sobrien#endif 1792236769Sobrien 1793236769Sobrien 1794236769Sobrien 1795236769Sobrien/*- 1796236769Sobrien *----------------------------------------------------------------------- 1797236769Sobrien * VarLoopExpand -- 1798236769Sobrien * Implements the :@<temp>@<string>@ modifier of ODE make. 1799236769Sobrien * We set the temp variable named in pattern.lhs to word and expand 1800236769Sobrien * pattern.rhs storing the result in the passed buffer. 1801236769Sobrien * 1802236769Sobrien * Input: 1803236769Sobrien * word Word to modify 1804236769Sobrien * addSpace True if space should be added before 1805236769Sobrien * other characters 1806236769Sobrien * buf Buffer for result 1807236769Sobrien * pattern Datafor substitution 1808236769Sobrien * 1809236769Sobrien * Results: 1810236769Sobrien * TRUE if a space is needed before more characters are added. 1811236769Sobrien * 1812236769Sobrien * Side Effects: 1813236769Sobrien * None. 1814236769Sobrien * 1815236769Sobrien *----------------------------------------------------------------------- 1816236769Sobrien */ 1817236769Sobrienstatic Boolean 1818237578SobrienVarLoopExpand(GNode *ctx MAKE_ATTR_UNUSED, 1819237578Sobrien Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 1820236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1821236769Sobrien void *loopp) 1822236769Sobrien{ 1823236769Sobrien VarLoop_t *loop = (VarLoop_t *)loopp; 1824236769Sobrien char *s; 1825236769Sobrien int slen; 1826236769Sobrien 1827236769Sobrien if (word && *word) { 1828236769Sobrien Var_Set(loop->tvar, word, loop->ctxt, VAR_NO_EXPORT); 1829236769Sobrien s = Var_Subst(NULL, loop->str, loop->ctxt, loop->errnum); 1830236769Sobrien if (s != NULL && *s != '\0') { 1831236769Sobrien if (addSpace && *s != '\n') 1832236769Sobrien Buf_AddByte(buf, ' '); 1833236769Sobrien Buf_AddBytes(buf, (slen = strlen(s)), s); 1834236769Sobrien addSpace = (slen > 0 && s[slen - 1] != '\n'); 1835236769Sobrien free(s); 1836236769Sobrien } 1837236769Sobrien } 1838236769Sobrien return addSpace; 1839236769Sobrien} 1840236769Sobrien 1841236769Sobrien 1842236769Sobrien/*- 1843236769Sobrien *----------------------------------------------------------------------- 1844236769Sobrien * VarSelectWords -- 1845236769Sobrien * Implements the :[start..end] modifier. 1846236769Sobrien * This is a special case of VarModify since we want to be able 1847236769Sobrien * to scan the list backwards if start > end. 1848236769Sobrien * 1849236769Sobrien * Input: 1850236769Sobrien * str String whose words should be trimmed 1851236769Sobrien * seldata words to select 1852236769Sobrien * 1853236769Sobrien * Results: 1854236769Sobrien * A string of all the words selected. 1855236769Sobrien * 1856236769Sobrien * Side Effects: 1857236769Sobrien * None. 1858236769Sobrien * 1859236769Sobrien *----------------------------------------------------------------------- 1860236769Sobrien */ 1861236769Sobrienstatic char * 1862237578SobrienVarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1863236769Sobrien const char *str, VarSelectWords_t *seldata) 1864236769Sobrien{ 1865236769Sobrien Buffer buf; /* Buffer for the new string */ 1866236769Sobrien Boolean addSpace; /* TRUE if need to add a space to the 1867236769Sobrien * buffer before adding the trimmed 1868236769Sobrien * word */ 1869236769Sobrien char **av; /* word list */ 1870236769Sobrien char *as; /* word list memory */ 1871236769Sobrien int ac, i; 1872236769Sobrien int start, end, step; 1873236769Sobrien 1874236769Sobrien Buf_Init(&buf, 0); 1875236769Sobrien addSpace = FALSE; 1876236769Sobrien 1877236769Sobrien if (vpstate->oneBigWord) { 1878236769Sobrien /* fake what brk_string() would do if there were only one word */ 1879236769Sobrien ac = 1; 1880236769Sobrien av = bmake_malloc((ac + 1) * sizeof(char *)); 1881236769Sobrien as = bmake_strdup(str); 1882236769Sobrien av[0] = as; 1883236769Sobrien av[1] = NULL; 1884236769Sobrien } else { 1885236769Sobrien av = brk_string(str, &ac, FALSE, &as); 1886236769Sobrien } 1887236769Sobrien 1888236769Sobrien /* 1889236769Sobrien * Now sanitize seldata. 1890236769Sobrien * If seldata->start or seldata->end are negative, convert them to 1891236769Sobrien * the positive equivalents (-1 gets converted to argc, -2 gets 1892236769Sobrien * converted to (argc-1), etc.). 1893236769Sobrien */ 1894236769Sobrien if (seldata->start < 0) 1895236769Sobrien seldata->start = ac + seldata->start + 1; 1896236769Sobrien if (seldata->end < 0) 1897236769Sobrien seldata->end = ac + seldata->end + 1; 1898236769Sobrien 1899236769Sobrien /* 1900236769Sobrien * We avoid scanning more of the list than we need to. 1901236769Sobrien */ 1902236769Sobrien if (seldata->start > seldata->end) { 1903236769Sobrien start = MIN(ac, seldata->start) - 1; 1904236769Sobrien end = MAX(0, seldata->end - 1); 1905236769Sobrien step = -1; 1906236769Sobrien } else { 1907236769Sobrien start = MAX(0, seldata->start - 1); 1908236769Sobrien end = MIN(ac, seldata->end); 1909236769Sobrien step = 1; 1910236769Sobrien } 1911236769Sobrien 1912236769Sobrien for (i = start; 1913236769Sobrien (step < 0 && i >= end) || (step > 0 && i < end); 1914236769Sobrien i += step) { 1915236769Sobrien if (av[i] && *av[i]) { 1916236769Sobrien if (addSpace && vpstate->varSpace) { 1917236769Sobrien Buf_AddByte(&buf, vpstate->varSpace); 1918236769Sobrien } 1919236769Sobrien Buf_AddBytes(&buf, strlen(av[i]), av[i]); 1920236769Sobrien addSpace = TRUE; 1921236769Sobrien } 1922236769Sobrien } 1923236769Sobrien 1924236769Sobrien free(as); 1925236769Sobrien free(av); 1926236769Sobrien 1927236769Sobrien return Buf_Destroy(&buf, FALSE); 1928236769Sobrien} 1929236769Sobrien 1930236769Sobrien 1931236769Sobrien/*- 1932236769Sobrien * VarRealpath -- 1933236769Sobrien * Replace each word with the result of realpath() 1934236769Sobrien * if successful. 1935236769Sobrien */ 1936236769Sobrienstatic Boolean 1937237578SobrienVarRealpath(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, 1938236769Sobrien char *word, Boolean addSpace, Buffer *buf, 1939237578Sobrien void *patternp MAKE_ATTR_UNUSED) 1940236769Sobrien{ 1941236769Sobrien struct stat st; 1942236769Sobrien char rbuf[MAXPATHLEN]; 1943236769Sobrien char *rp; 1944236769Sobrien 1945236769Sobrien if (addSpace && vpstate->varSpace) { 1946236769Sobrien Buf_AddByte(buf, vpstate->varSpace); 1947236769Sobrien } 1948236769Sobrien addSpace = TRUE; 1949236769Sobrien rp = realpath(word, rbuf); 1950236769Sobrien if (rp && *rp == '/' && stat(rp, &st) == 0) 1951236769Sobrien word = rp; 1952236769Sobrien 1953236769Sobrien Buf_AddBytes(buf, strlen(word), word); 1954236769Sobrien return(addSpace); 1955236769Sobrien} 1956236769Sobrien 1957236769Sobrien/*- 1958236769Sobrien *----------------------------------------------------------------------- 1959236769Sobrien * VarModify -- 1960236769Sobrien * Modify each of the words of the passed string using the given 1961236769Sobrien * function. Used to implement all modifiers. 1962236769Sobrien * 1963236769Sobrien * Input: 1964236769Sobrien * str String whose words should be trimmed 1965236769Sobrien * modProc Function to use to modify them 1966236769Sobrien * datum Datum to pass it 1967236769Sobrien * 1968236769Sobrien * Results: 1969236769Sobrien * A string of all the words modified appropriately. 1970236769Sobrien * 1971236769Sobrien * Side Effects: 1972236769Sobrien * None. 1973236769Sobrien * 1974236769Sobrien *----------------------------------------------------------------------- 1975236769Sobrien */ 1976236769Sobrienstatic char * 1977236769SobrienVarModify(GNode *ctx, Var_Parse_State *vpstate, 1978236769Sobrien const char *str, 1979236769Sobrien Boolean (*modProc)(GNode *, Var_Parse_State *, char *, 1980236769Sobrien Boolean, Buffer *, void *), 1981236769Sobrien void *datum) 1982236769Sobrien{ 1983236769Sobrien Buffer buf; /* Buffer for the new string */ 1984236769Sobrien Boolean addSpace; /* TRUE if need to add a space to the 1985236769Sobrien * buffer before adding the trimmed 1986236769Sobrien * word */ 1987236769Sobrien char **av; /* word list */ 1988236769Sobrien char *as; /* word list memory */ 1989236769Sobrien int ac, i; 1990236769Sobrien 1991236769Sobrien Buf_Init(&buf, 0); 1992236769Sobrien addSpace = FALSE; 1993236769Sobrien 1994236769Sobrien if (vpstate->oneBigWord) { 1995236769Sobrien /* fake what brk_string() would do if there were only one word */ 1996236769Sobrien ac = 1; 1997236769Sobrien av = bmake_malloc((ac + 1) * sizeof(char *)); 1998236769Sobrien as = bmake_strdup(str); 1999236769Sobrien av[0] = as; 2000236769Sobrien av[1] = NULL; 2001236769Sobrien } else { 2002236769Sobrien av = brk_string(str, &ac, FALSE, &as); 2003236769Sobrien } 2004236769Sobrien 2005236769Sobrien for (i = 0; i < ac; i++) { 2006236769Sobrien addSpace = (*modProc)(ctx, vpstate, av[i], addSpace, &buf, datum); 2007236769Sobrien } 2008236769Sobrien 2009236769Sobrien free(as); 2010236769Sobrien free(av); 2011236769Sobrien 2012236769Sobrien return Buf_Destroy(&buf, FALSE); 2013236769Sobrien} 2014236769Sobrien 2015236769Sobrien 2016236769Sobrienstatic int 2017236769SobrienVarWordCompare(const void *a, const void *b) 2018236769Sobrien{ 2019236769Sobrien int r = strcmp(*(const char * const *)a, *(const char * const *)b); 2020236769Sobrien return r; 2021236769Sobrien} 2022236769Sobrien 2023236769Sobrien/*- 2024236769Sobrien *----------------------------------------------------------------------- 2025236769Sobrien * VarOrder -- 2026236769Sobrien * Order the words in the string. 2027236769Sobrien * 2028236769Sobrien * Input: 2029236769Sobrien * str String whose words should be sorted. 2030236769Sobrien * otype How to order: s - sort, x - random. 2031236769Sobrien * 2032236769Sobrien * Results: 2033236769Sobrien * A string containing the words ordered. 2034236769Sobrien * 2035236769Sobrien * Side Effects: 2036236769Sobrien * None. 2037236769Sobrien * 2038236769Sobrien *----------------------------------------------------------------------- 2039236769Sobrien */ 2040236769Sobrienstatic char * 2041236769SobrienVarOrder(const char *str, const char otype) 2042236769Sobrien{ 2043236769Sobrien Buffer buf; /* Buffer for the new string */ 2044236769Sobrien char **av; /* word list [first word does not count] */ 2045236769Sobrien char *as; /* word list memory */ 2046236769Sobrien int ac, i; 2047236769Sobrien 2048236769Sobrien Buf_Init(&buf, 0); 2049236769Sobrien 2050236769Sobrien av = brk_string(str, &ac, FALSE, &as); 2051236769Sobrien 2052236769Sobrien if (ac > 0) 2053236769Sobrien switch (otype) { 2054236769Sobrien case 's': /* sort alphabetically */ 2055236769Sobrien qsort(av, ac, sizeof(char *), VarWordCompare); 2056236769Sobrien break; 2057236769Sobrien case 'x': /* randomize */ 2058236769Sobrien { 2059236769Sobrien int rndidx; 2060236769Sobrien char *t; 2061236769Sobrien 2062236769Sobrien /* 2063236769Sobrien * We will use [ac..2] range for mod factors. This will produce 2064236769Sobrien * random numbers in [(ac-1)..0] interval, and minimal 2065236769Sobrien * reasonable value for mod factor is 2 (the mod 1 will produce 2066236769Sobrien * 0 with probability 1). 2067236769Sobrien */ 2068236769Sobrien for (i = ac-1; i > 0; i--) { 2069236769Sobrien rndidx = random() % (i + 1); 2070236769Sobrien if (i != rndidx) { 2071236769Sobrien t = av[i]; 2072236769Sobrien av[i] = av[rndidx]; 2073236769Sobrien av[rndidx] = t; 2074236769Sobrien } 2075236769Sobrien } 2076236769Sobrien } 2077236769Sobrien } /* end of switch */ 2078236769Sobrien 2079236769Sobrien for (i = 0; i < ac; i++) { 2080236769Sobrien Buf_AddBytes(&buf, strlen(av[i]), av[i]); 2081236769Sobrien if (i != ac - 1) 2082236769Sobrien Buf_AddByte(&buf, ' '); 2083236769Sobrien } 2084236769Sobrien 2085236769Sobrien free(as); 2086236769Sobrien free(av); 2087236769Sobrien 2088236769Sobrien return Buf_Destroy(&buf, FALSE); 2089236769Sobrien} 2090236769Sobrien 2091236769Sobrien 2092236769Sobrien/*- 2093236769Sobrien *----------------------------------------------------------------------- 2094236769Sobrien * VarUniq -- 2095236769Sobrien * Remove adjacent duplicate words. 2096236769Sobrien * 2097236769Sobrien * Input: 2098236769Sobrien * str String whose words should be sorted 2099236769Sobrien * 2100236769Sobrien * Results: 2101236769Sobrien * A string containing the resulting words. 2102236769Sobrien * 2103236769Sobrien * Side Effects: 2104236769Sobrien * None. 2105236769Sobrien * 2106236769Sobrien *----------------------------------------------------------------------- 2107236769Sobrien */ 2108236769Sobrienstatic char * 2109236769SobrienVarUniq(const char *str) 2110236769Sobrien{ 2111236769Sobrien Buffer buf; /* Buffer for new string */ 2112236769Sobrien char **av; /* List of words to affect */ 2113236769Sobrien char *as; /* Word list memory */ 2114236769Sobrien int ac, i, j; 2115236769Sobrien 2116236769Sobrien Buf_Init(&buf, 0); 2117236769Sobrien av = brk_string(str, &ac, FALSE, &as); 2118236769Sobrien 2119236769Sobrien if (ac > 1) { 2120236769Sobrien for (j = 0, i = 1; i < ac; i++) 2121236769Sobrien if (strcmp(av[i], av[j]) != 0 && (++j != i)) 2122236769Sobrien av[j] = av[i]; 2123236769Sobrien ac = j + 1; 2124236769Sobrien } 2125236769Sobrien 2126236769Sobrien for (i = 0; i < ac; i++) { 2127236769Sobrien Buf_AddBytes(&buf, strlen(av[i]), av[i]); 2128236769Sobrien if (i != ac - 1) 2129236769Sobrien Buf_AddByte(&buf, ' '); 2130236769Sobrien } 2131236769Sobrien 2132236769Sobrien free(as); 2133236769Sobrien free(av); 2134236769Sobrien 2135236769Sobrien return Buf_Destroy(&buf, FALSE); 2136236769Sobrien} 2137236769Sobrien 2138236769Sobrien 2139236769Sobrien/*- 2140236769Sobrien *----------------------------------------------------------------------- 2141236769Sobrien * VarGetPattern -- 2142236769Sobrien * Pass through the tstr looking for 1) escaped delimiters, 2143236769Sobrien * '$'s and backslashes (place the escaped character in 2144236769Sobrien * uninterpreted) and 2) unescaped $'s that aren't before 2145236769Sobrien * the delimiter (expand the variable substitution unless flags 2146236769Sobrien * has VAR_NOSUBST set). 2147236769Sobrien * Return the expanded string or NULL if the delimiter was missing 2148236769Sobrien * If pattern is specified, handle escaped ampersands, and replace 2149236769Sobrien * unescaped ampersands with the lhs of the pattern. 2150236769Sobrien * 2151236769Sobrien * Results: 2152236769Sobrien * A string of all the words modified appropriately. 2153236769Sobrien * If length is specified, return the string length of the buffer 2154236769Sobrien * If flags is specified and the last character of the pattern is a 2155236769Sobrien * $ set the VAR_MATCH_END bit of flags. 2156236769Sobrien * 2157236769Sobrien * Side Effects: 2158236769Sobrien * None. 2159236769Sobrien *----------------------------------------------------------------------- 2160236769Sobrien */ 2161236769Sobrienstatic char * 2162237578SobrienVarGetPattern(GNode *ctxt, Var_Parse_State *vpstate MAKE_ATTR_UNUSED, 2163236769Sobrien int errnum, const char **tstr, int delim, int *flags, 2164236769Sobrien int *length, VarPattern *pattern) 2165236769Sobrien{ 2166236769Sobrien const char *cp; 2167236769Sobrien char *rstr; 2168236769Sobrien Buffer buf; 2169236769Sobrien int junk; 2170236769Sobrien 2171236769Sobrien Buf_Init(&buf, 0); 2172236769Sobrien if (length == NULL) 2173236769Sobrien length = &junk; 2174236769Sobrien 2175236769Sobrien#define IS_A_MATCH(cp, delim) \ 2176236769Sobrien ((cp[0] == '\\') && ((cp[1] == delim) || \ 2177236769Sobrien (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&')))) 2178236769Sobrien 2179236769Sobrien /* 2180236769Sobrien * Skim through until the matching delimiter is found; 2181236769Sobrien * pick up variable substitutions on the way. Also allow 2182236769Sobrien * backslashes to quote the delimiter, $, and \, but don't 2183236769Sobrien * touch other backslashes. 2184236769Sobrien */ 2185236769Sobrien for (cp = *tstr; *cp && (*cp != delim); cp++) { 2186236769Sobrien if (IS_A_MATCH(cp, delim)) { 2187236769Sobrien Buf_AddByte(&buf, cp[1]); 2188236769Sobrien cp++; 2189236769Sobrien } else if (*cp == '$') { 2190236769Sobrien if (cp[1] == delim) { 2191236769Sobrien if (flags == NULL) 2192236769Sobrien Buf_AddByte(&buf, *cp); 2193236769Sobrien else 2194236769Sobrien /* 2195236769Sobrien * Unescaped $ at end of pattern => anchor 2196236769Sobrien * pattern at end. 2197236769Sobrien */ 2198236769Sobrien *flags |= VAR_MATCH_END; 2199236769Sobrien } else { 2200236769Sobrien if (flags == NULL || (*flags & VAR_NOSUBST) == 0) { 2201236769Sobrien char *cp2; 2202236769Sobrien int len; 2203236769Sobrien void *freeIt; 2204236769Sobrien 2205236769Sobrien /* 2206236769Sobrien * If unescaped dollar sign not before the 2207236769Sobrien * delimiter, assume it's a variable 2208236769Sobrien * substitution and recurse. 2209236769Sobrien */ 2210236769Sobrien cp2 = Var_Parse(cp, ctxt, errnum, &len, &freeIt); 2211236769Sobrien Buf_AddBytes(&buf, strlen(cp2), cp2); 2212236769Sobrien if (freeIt) 2213236769Sobrien free(freeIt); 2214236769Sobrien cp += len - 1; 2215236769Sobrien } else { 2216236769Sobrien const char *cp2 = &cp[1]; 2217236769Sobrien 2218236769Sobrien if (*cp2 == PROPEN || *cp2 == BROPEN) { 2219236769Sobrien /* 2220236769Sobrien * Find the end of this variable reference 2221236769Sobrien * and suck it in without further ado. 2222236769Sobrien * It will be interperated later. 2223236769Sobrien */ 2224236769Sobrien int have = *cp2; 2225236769Sobrien int want = (*cp2 == PROPEN) ? PRCLOSE : BRCLOSE; 2226236769Sobrien int depth = 1; 2227236769Sobrien 2228236769Sobrien for (++cp2; *cp2 != '\0' && depth > 0; ++cp2) { 2229236769Sobrien if (cp2[-1] != '\\') { 2230236769Sobrien if (*cp2 == have) 2231236769Sobrien ++depth; 2232236769Sobrien if (*cp2 == want) 2233236769Sobrien --depth; 2234236769Sobrien } 2235236769Sobrien } 2236236769Sobrien Buf_AddBytes(&buf, cp2 - cp, cp); 2237236769Sobrien cp = --cp2; 2238236769Sobrien } else 2239236769Sobrien Buf_AddByte(&buf, *cp); 2240236769Sobrien } 2241236769Sobrien } 2242236769Sobrien } 2243236769Sobrien else if (pattern && *cp == '&') 2244236769Sobrien Buf_AddBytes(&buf, pattern->leftLen, pattern->lhs); 2245236769Sobrien else 2246236769Sobrien Buf_AddByte(&buf, *cp); 2247236769Sobrien } 2248236769Sobrien 2249236769Sobrien if (*cp != delim) { 2250236769Sobrien *tstr = cp; 2251236769Sobrien *length = 0; 2252236769Sobrien return NULL; 2253236769Sobrien } 2254236769Sobrien 2255236769Sobrien *tstr = ++cp; 2256236769Sobrien *length = Buf_Size(&buf); 2257236769Sobrien rstr = Buf_Destroy(&buf, FALSE); 2258236769Sobrien if (DEBUG(VAR)) 2259236769Sobrien fprintf(debug_file, "Modifier pattern: \"%s\"\n", rstr); 2260236769Sobrien return rstr; 2261236769Sobrien} 2262236769Sobrien 2263236769Sobrien/*- 2264236769Sobrien *----------------------------------------------------------------------- 2265236769Sobrien * VarQuote -- 2266236769Sobrien * Quote shell meta-characters in the string 2267236769Sobrien * 2268236769Sobrien * Results: 2269236769Sobrien * The quoted string 2270236769Sobrien * 2271236769Sobrien * Side Effects: 2272236769Sobrien * None. 2273236769Sobrien * 2274236769Sobrien *----------------------------------------------------------------------- 2275236769Sobrien */ 2276236769Sobrienstatic char * 2277236769SobrienVarQuote(char *str) 2278236769Sobrien{ 2279236769Sobrien 2280236769Sobrien Buffer buf; 2281236769Sobrien /* This should cover most shells :-( */ 2282236769Sobrien static const char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~"; 2283236769Sobrien const char *newline; 2284236769Sobrien size_t len, nlen; 2285236769Sobrien 2286236769Sobrien if ((newline = Shell_GetNewline()) == NULL) 2287236769Sobrien newline = "\\\n"; 2288236769Sobrien nlen = strlen(newline); 2289236769Sobrien 2290236769Sobrien Buf_Init(&buf, 0); 2291236769Sobrien while (*str != '\0') { 2292236769Sobrien if ((len = strcspn(str, meta)) != 0) { 2293236769Sobrien Buf_AddBytes(&buf, len, str); 2294236769Sobrien str += len; 2295236769Sobrien } else if (*str == '\n') { 2296236769Sobrien Buf_AddBytes(&buf, nlen, newline); 2297236769Sobrien ++str; 2298236769Sobrien } else { 2299236769Sobrien Buf_AddByte(&buf, '\\'); 2300236769Sobrien Buf_AddByte(&buf, *str); 2301236769Sobrien ++str; 2302236769Sobrien } 2303236769Sobrien } 2304236769Sobrien str = Buf_Destroy(&buf, FALSE); 2305236769Sobrien if (DEBUG(VAR)) 2306236769Sobrien fprintf(debug_file, "QuoteMeta: [%s]\n", str); 2307236769Sobrien return str; 2308236769Sobrien} 2309236769Sobrien 2310236769Sobrien/*- 2311236769Sobrien *----------------------------------------------------------------------- 2312236769Sobrien * VarHash -- 2313236769Sobrien * Hash the string using the MurmurHash3 algorithm. 2314236769Sobrien * Output is computed using 32bit Little Endian arithmetic. 2315236769Sobrien * 2316236769Sobrien * Input: 2317236769Sobrien * str String to modify 2318236769Sobrien * 2319236769Sobrien * Results: 2320236769Sobrien * Hash value of str, encoded as 8 hex digits. 2321236769Sobrien * 2322236769Sobrien * Side Effects: 2323236769Sobrien * None. 2324236769Sobrien * 2325236769Sobrien *----------------------------------------------------------------------- 2326236769Sobrien */ 2327236769Sobrienstatic char * 2328236769SobrienVarHash(char *str) 2329236769Sobrien{ 2330236769Sobrien static const char hexdigits[16] = "0123456789abcdef"; 2331236769Sobrien Buffer buf; 2332236769Sobrien size_t len, len2; 2333236769Sobrien unsigned char *ustr = (unsigned char *)str; 2334236769Sobrien uint32_t h, k, c1, c2; 2335236769Sobrien 2336236769Sobrien h = 0x971e137bU; 2337236769Sobrien c1 = 0x95543787U; 2338236769Sobrien c2 = 0x2ad7eb25U; 2339236769Sobrien len2 = strlen(str); 2340236769Sobrien 2341236769Sobrien for (len = len2; len; ) { 2342236769Sobrien k = 0; 2343236769Sobrien switch (len) { 2344236769Sobrien default: 2345236769Sobrien k = (ustr[3] << 24) | (ustr[2] << 16) | (ustr[1] << 8) | ustr[0]; 2346236769Sobrien len -= 4; 2347236769Sobrien ustr += 4; 2348236769Sobrien break; 2349236769Sobrien case 3: 2350236769Sobrien k |= (ustr[2] << 16); 2351236769Sobrien case 2: 2352236769Sobrien k |= (ustr[1] << 8); 2353236769Sobrien case 1: 2354236769Sobrien k |= ustr[0]; 2355236769Sobrien len = 0; 2356236769Sobrien } 2357236769Sobrien c1 = c1 * 5 + 0x7b7d159cU; 2358236769Sobrien c2 = c2 * 5 + 0x6bce6396U; 2359236769Sobrien k *= c1; 2360236769Sobrien k = (k << 11) ^ (k >> 21); 2361236769Sobrien k *= c2; 2362236769Sobrien h = (h << 13) ^ (h >> 19); 2363236769Sobrien h = h * 5 + 0x52dce729U; 2364236769Sobrien h ^= k; 2365251422Ssjg } 2366236769Sobrien h ^= len2; 2367236769Sobrien h *= 0x85ebca6b; 2368236769Sobrien h ^= h >> 13; 2369236769Sobrien h *= 0xc2b2ae35; 2370236769Sobrien h ^= h >> 16; 2371236769Sobrien 2372236769Sobrien Buf_Init(&buf, 0); 2373236769Sobrien for (len = 0; len < 8; ++len) { 2374236769Sobrien Buf_AddByte(&buf, hexdigits[h & 15]); 2375236769Sobrien h >>= 4; 2376236769Sobrien } 2377236769Sobrien 2378236769Sobrien return Buf_Destroy(&buf, FALSE); 2379236769Sobrien} 2380236769Sobrien 2381236769Sobrienstatic char * 2382236769SobrienVarStrftime(const char *fmt, int zulu) 2383236769Sobrien{ 2384236769Sobrien char buf[BUFSIZ]; 2385236769Sobrien time_t utc; 2386236769Sobrien 2387236769Sobrien time(&utc); 2388236769Sobrien if (!*fmt) 2389236769Sobrien fmt = "%c"; 2390236769Sobrien strftime(buf, sizeof(buf), fmt, zulu ? gmtime(&utc) : localtime(&utc)); 2391236769Sobrien 2392236769Sobrien buf[sizeof(buf) - 1] = '\0'; 2393236769Sobrien return bmake_strdup(buf); 2394236769Sobrien} 2395236769Sobrien 2396236769Sobrien/* 2397236769Sobrien * Now we need to apply any modifiers the user wants applied. 2398236769Sobrien * These are: 2399236769Sobrien * :M<pattern> words which match the given <pattern>. 2400236769Sobrien * <pattern> is of the standard file 2401236769Sobrien * wildcarding form. 2402236769Sobrien * :N<pattern> words which do not match the given <pattern>. 2403236769Sobrien * :S<d><pat1><d><pat2><d>[1gW] 2404236769Sobrien * Substitute <pat2> for <pat1> in the value 2405236769Sobrien * :C<d><pat1><d><pat2><d>[1gW] 2406236769Sobrien * Substitute <pat2> for regex <pat1> in the value 2407236769Sobrien * :H Substitute the head of each word 2408236769Sobrien * :T Substitute the tail of each word 2409236769Sobrien * :E Substitute the extension (minus '.') of 2410236769Sobrien * each word 2411236769Sobrien * :R Substitute the root of each word 2412236769Sobrien * (pathname minus the suffix). 2413236769Sobrien * :O ("Order") Alphabeticaly sort words in variable. 2414236769Sobrien * :Ox ("intermiX") Randomize words in variable. 2415236769Sobrien * :u ("uniq") Remove adjacent duplicate words. 2416236769Sobrien * :tu Converts the variable contents to uppercase. 2417236769Sobrien * :tl Converts the variable contents to lowercase. 2418236769Sobrien * :ts[c] Sets varSpace - the char used to 2419236769Sobrien * separate words to 'c'. If 'c' is 2420236769Sobrien * omitted then no separation is used. 2421236769Sobrien * :tW Treat the variable contents as a single 2422236769Sobrien * word, even if it contains spaces. 2423236769Sobrien * (Mnemonic: one big 'W'ord.) 2424236769Sobrien * :tw Treat the variable contents as multiple 2425236769Sobrien * space-separated words. 2426236769Sobrien * (Mnemonic: many small 'w'ords.) 2427236769Sobrien * :[index] Select a single word from the value. 2428236769Sobrien * :[start..end] Select multiple words from the value. 2429236769Sobrien * :[*] or :[0] Select the entire value, as a single 2430236769Sobrien * word. Equivalent to :tW. 2431236769Sobrien * :[@] Select the entire value, as multiple 2432236769Sobrien * words. Undoes the effect of :[*]. 2433236769Sobrien * Equivalent to :tw. 2434236769Sobrien * :[#] Returns the number of words in the value. 2435236769Sobrien * 2436236769Sobrien * :?<true-value>:<false-value> 2437236769Sobrien * If the variable evaluates to true, return 2438236769Sobrien * true value, else return the second value. 2439236769Sobrien * :lhs=rhs Like :S, but the rhs goes to the end of 2440236769Sobrien * the invocation. 2441236769Sobrien * :sh Treat the current value as a command 2442236769Sobrien * to be run, new value is its output. 2443236769Sobrien * The following added so we can handle ODE makefiles. 2444236769Sobrien * :@<tmpvar>@<newval>@ 2445236769Sobrien * Assign a temporary local variable <tmpvar> 2446236769Sobrien * to the current value of each word in turn 2447236769Sobrien * and replace each word with the result of 2448236769Sobrien * evaluating <newval> 2449236769Sobrien * :D<newval> Use <newval> as value if variable defined 2450236769Sobrien * :U<newval> Use <newval> as value if variable undefined 2451236769Sobrien * :L Use the name of the variable as the value. 2452236769Sobrien * :P Use the path of the node that has the same 2453236769Sobrien * name as the variable as the value. This 2454236769Sobrien * basically includes an implied :L so that 2455236769Sobrien * the common method of refering to the path 2456236769Sobrien * of your dependent 'x' in a rule is to use 2457236769Sobrien * the form '${x:P}'. 2458236769Sobrien * :!<cmd>! Run cmd much the same as :sh run's the 2459236769Sobrien * current value of the variable. 2460236769Sobrien * The ::= modifiers, actually assign a value to the variable. 2461236769Sobrien * Their main purpose is in supporting modifiers of .for loop 2462236769Sobrien * iterators and other obscure uses. They always expand to 2463236769Sobrien * nothing. In a target rule that would otherwise expand to an 2464236769Sobrien * empty line they can be preceded with @: to keep make happy. 2465236769Sobrien * Eg. 2466236769Sobrien * 2467236769Sobrien * foo: .USE 2468236769Sobrien * .for i in ${.TARGET} ${.TARGET:R}.gz 2469236769Sobrien * @: ${t::=$i} 2470236769Sobrien * @echo blah ${t:T} 2471236769Sobrien * .endfor 2472236769Sobrien * 2473236769Sobrien * ::=<str> Assigns <str> as the new value of variable. 2474236769Sobrien * ::?=<str> Assigns <str> as value of variable if 2475236769Sobrien * it was not already set. 2476236769Sobrien * ::+=<str> Appends <str> to variable. 2477236769Sobrien * ::!=<cmd> Assigns output of <cmd> as the new value of 2478236769Sobrien * variable. 2479236769Sobrien */ 2480236769Sobrien 2481236769Sobrien/* we now have some modifiers with long names */ 2482236769Sobrien#define STRMOD_MATCH(s, want, n) \ 2483236769Sobrien (strncmp(s, want, n) == 0 && (s[n] == endc || s[n] == ':')) 2484236769Sobrien 2485236769Sobrienstatic char * 2486236769SobrienApplyModifiers(char *nstr, const char *tstr, 2487236769Sobrien int startc, int endc, 2488236769Sobrien Var *v, GNode *ctxt, Boolean errnum, 2489236769Sobrien int *lengthPtr, void **freePtr) 2490236769Sobrien{ 2491236769Sobrien const char *start; 2492236769Sobrien const char *cp; /* Secondary pointer into str (place marker 2493236769Sobrien * for tstr) */ 2494236769Sobrien char *newStr; /* New value to return */ 2495236769Sobrien char termc; /* Character which terminated scan */ 2496236769Sobrien int cnt; /* Used to count brace pairs when variable in 2497236769Sobrien * in parens or braces */ 2498236769Sobrien char delim; 2499236769Sobrien int modifier; /* that we are processing */ 2500236769Sobrien Var_Parse_State parsestate; /* Flags passed to helper functions */ 2501236769Sobrien 2502236769Sobrien delim = '\0'; 2503236769Sobrien parsestate.oneBigWord = FALSE; 2504236769Sobrien parsestate.varSpace = ' '; /* word separator */ 2505236769Sobrien 2506236769Sobrien start = cp = tstr; 2507236769Sobrien 2508236769Sobrien while (*tstr && *tstr != endc) { 2509236769Sobrien 2510236769Sobrien if (*tstr == '$') { 2511236769Sobrien /* 2512236769Sobrien * We may have some complex modifiers in a variable. 2513236769Sobrien */ 2514236769Sobrien void *freeIt; 2515236769Sobrien char *rval; 2516236769Sobrien int rlen; 2517236769Sobrien int c; 2518236769Sobrien 2519236769Sobrien rval = Var_Parse(tstr, ctxt, errnum, &rlen, &freeIt); 2520236769Sobrien 2521236769Sobrien /* 2522236769Sobrien * If we have not parsed up to endc or ':', 2523236769Sobrien * we are not interested. 2524236769Sobrien */ 2525236769Sobrien if (rval != NULL && *rval && 2526236769Sobrien (c = tstr[rlen]) != '\0' && 2527236769Sobrien c != ':' && 2528236769Sobrien c != endc) { 2529236769Sobrien if (freeIt) 2530236769Sobrien free(freeIt); 2531236769Sobrien goto apply_mods; 2532236769Sobrien } 2533236769Sobrien 2534236769Sobrien if (DEBUG(VAR)) { 2535236769Sobrien fprintf(debug_file, "Got '%s' from '%.*s'%.*s\n", 2536236769Sobrien rval, rlen, tstr, rlen, tstr + rlen); 2537236769Sobrien } 2538236769Sobrien 2539236769Sobrien tstr += rlen; 2540236769Sobrien 2541236769Sobrien if (rval != NULL && *rval) { 2542236769Sobrien int used; 2543236769Sobrien 2544236769Sobrien nstr = ApplyModifiers(nstr, rval, 2545236769Sobrien 0, 0, 2546236769Sobrien v, ctxt, errnum, &used, freePtr); 2547236769Sobrien if (nstr == var_Error 2548236769Sobrien || (nstr == varNoError && errnum == 0) 2549236769Sobrien || strlen(rval) != (size_t) used) { 2550236769Sobrien if (freeIt) 2551236769Sobrien free(freeIt); 2552236769Sobrien goto out; /* error already reported */ 2553236769Sobrien } 2554236769Sobrien } 2555236769Sobrien if (freeIt) 2556236769Sobrien free(freeIt); 2557236769Sobrien if (*tstr == ':') 2558236769Sobrien tstr++; 2559236769Sobrien else if (!*tstr && endc) { 2560236769Sobrien Error("Unclosed variable specification after complex modifier (expecting '%c') for %s", endc, v->name); 2561236769Sobrien goto out; 2562236769Sobrien } 2563236769Sobrien continue; 2564236769Sobrien } 2565236769Sobrien apply_mods: 2566236769Sobrien if (DEBUG(VAR)) { 2567246223Ssjg fprintf(debug_file, "Applying[%s] :%c to \"%s\"\n", v->name, 2568246223Ssjg *tstr, nstr); 2569236769Sobrien } 2570236769Sobrien newStr = var_Error; 2571236769Sobrien switch ((modifier = *tstr)) { 2572236769Sobrien case ':': 2573236769Sobrien { 2574236769Sobrien if (tstr[1] == '=' || 2575236769Sobrien (tstr[2] == '=' && 2576236769Sobrien (tstr[1] == '!' || tstr[1] == '+' || tstr[1] == '?'))) { 2577236769Sobrien /* 2578236769Sobrien * "::=", "::!=", "::+=", or "::?=" 2579236769Sobrien */ 2580236769Sobrien GNode *v_ctxt; /* context where v belongs */ 2581236769Sobrien const char *emsg; 2582236769Sobrien char *sv_name; 2583236769Sobrien VarPattern pattern; 2584236769Sobrien int how; 2585236769Sobrien 2586236769Sobrien if (v->name[0] == 0) 2587236769Sobrien goto bad_modifier; 2588236769Sobrien 2589236769Sobrien v_ctxt = ctxt; 2590236769Sobrien sv_name = NULL; 2591236769Sobrien ++tstr; 2592236769Sobrien if (v->flags & VAR_JUNK) { 2593236769Sobrien /* 2594236769Sobrien * We need to bmake_strdup() it incase 2595236769Sobrien * VarGetPattern() recurses. 2596236769Sobrien */ 2597236769Sobrien sv_name = v->name; 2598236769Sobrien v->name = bmake_strdup(v->name); 2599236769Sobrien } else if (ctxt != VAR_GLOBAL) { 2600236769Sobrien Var *gv = VarFind(v->name, ctxt, 0); 2601236769Sobrien if (gv == NULL) 2602236769Sobrien v_ctxt = VAR_GLOBAL; 2603236769Sobrien else 2604236769Sobrien VarFreeEnv(gv, TRUE); 2605236769Sobrien } 2606236769Sobrien 2607236769Sobrien switch ((how = *tstr)) { 2608236769Sobrien case '+': 2609236769Sobrien case '?': 2610236769Sobrien case '!': 2611236769Sobrien cp = &tstr[2]; 2612236769Sobrien break; 2613236769Sobrien default: 2614236769Sobrien cp = ++tstr; 2615236769Sobrien break; 2616236769Sobrien } 2617236769Sobrien delim = startc == PROPEN ? PRCLOSE : BRCLOSE; 2618236769Sobrien pattern.flags = 0; 2619236769Sobrien 2620236769Sobrien pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum, 2621236769Sobrien &cp, delim, NULL, 2622236769Sobrien &pattern.rightLen, 2623236769Sobrien NULL); 2624236769Sobrien if (v->flags & VAR_JUNK) { 2625236769Sobrien /* restore original name */ 2626236769Sobrien free(v->name); 2627236769Sobrien v->name = sv_name; 2628236769Sobrien } 2629236769Sobrien if (pattern.rhs == NULL) 2630236769Sobrien goto cleanup; 2631236769Sobrien 2632236769Sobrien termc = *--cp; 2633236769Sobrien delim = '\0'; 2634236769Sobrien 2635236769Sobrien switch (how) { 2636236769Sobrien case '+': 2637236769Sobrien Var_Append(v->name, pattern.rhs, v_ctxt); 2638236769Sobrien break; 2639236769Sobrien case '!': 2640236769Sobrien newStr = Cmd_Exec(pattern.rhs, &emsg); 2641236769Sobrien if (emsg) 2642236769Sobrien Error(emsg, nstr); 2643236769Sobrien else 2644236769Sobrien Var_Set(v->name, newStr, v_ctxt, 0); 2645236769Sobrien if (newStr) 2646236769Sobrien free(newStr); 2647236769Sobrien break; 2648236769Sobrien case '?': 2649236769Sobrien if ((v->flags & VAR_JUNK) == 0) 2650236769Sobrien break; 2651236769Sobrien /* FALLTHROUGH */ 2652236769Sobrien default: 2653236769Sobrien Var_Set(v->name, pattern.rhs, v_ctxt, 0); 2654236769Sobrien break; 2655236769Sobrien } 2656236769Sobrien free(UNCONST(pattern.rhs)); 2657236769Sobrien newStr = var_Error; 2658236769Sobrien break; 2659236769Sobrien } 2660236769Sobrien goto default_case; /* "::<unrecognised>" */ 2661236769Sobrien } 2662236769Sobrien case '@': 2663236769Sobrien { 2664236769Sobrien VarLoop_t loop; 2665236769Sobrien int flags = VAR_NOSUBST; 2666236769Sobrien 2667236769Sobrien cp = ++tstr; 2668236769Sobrien delim = '@'; 2669236769Sobrien if ((loop.tvar = VarGetPattern(ctxt, &parsestate, errnum, 2670236769Sobrien &cp, delim, 2671236769Sobrien &flags, &loop.tvarLen, 2672236769Sobrien NULL)) == NULL) 2673236769Sobrien goto cleanup; 2674236769Sobrien 2675236769Sobrien if ((loop.str = VarGetPattern(ctxt, &parsestate, errnum, 2676236769Sobrien &cp, delim, 2677236769Sobrien &flags, &loop.strLen, 2678236769Sobrien NULL)) == NULL) 2679236769Sobrien goto cleanup; 2680236769Sobrien 2681236769Sobrien termc = *cp; 2682236769Sobrien delim = '\0'; 2683236769Sobrien 2684236769Sobrien loop.errnum = errnum; 2685236769Sobrien loop.ctxt = ctxt; 2686236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarLoopExpand, 2687236769Sobrien &loop); 2688236769Sobrien free(loop.tvar); 2689236769Sobrien free(loop.str); 2690236769Sobrien break; 2691236769Sobrien } 2692250164Ssjg case 'U': 2693250164Ssjg#ifdef MAKE_FREEBSD_UL 2694250164Ssjg if (FreeBSD_UL) { 2695250164Ssjg int nc = tstr[1]; 2696250164Ssjg 2697250164Ssjg /* we have to be careful, since :U is used internally */ 2698250164Ssjg if (nc == ':' || nc == endc) { 2699250164Ssjg char *dp = bmake_strdup(nstr); 2700250164Ssjg for (newStr = dp; *dp; dp++) 2701250164Ssjg *dp = toupper((unsigned char)*dp); 2702250164Ssjg cp = tstr + 1; 2703250164Ssjg termc = *cp; 2704250164Ssjg break; /* yes inside the conditional */ 2705250164Ssjg } 2706250164Ssjg /* FALLTHROUGH */ 2707250164Ssjg } 2708250164Ssjg#endif 2709236769Sobrien case 'D': 2710236769Sobrien { 2711236769Sobrien Buffer buf; /* Buffer for patterns */ 2712236769Sobrien int wantit; /* want data in buffer */ 2713236769Sobrien 2714236769Sobrien /* 2715236769Sobrien * Pass through tstr looking for 1) escaped delimiters, 2716236769Sobrien * '$'s and backslashes (place the escaped character in 2717236769Sobrien * uninterpreted) and 2) unescaped $'s that aren't before 2718236769Sobrien * the delimiter (expand the variable substitution). 2719236769Sobrien * The result is left in the Buffer buf. 2720236769Sobrien */ 2721236769Sobrien Buf_Init(&buf, 0); 2722236769Sobrien for (cp = tstr + 1; 2723236769Sobrien *cp != endc && *cp != ':' && *cp != '\0'; 2724236769Sobrien cp++) { 2725236769Sobrien if ((*cp == '\\') && 2726236769Sobrien ((cp[1] == ':') || 2727236769Sobrien (cp[1] == '$') || 2728236769Sobrien (cp[1] == endc) || 2729236769Sobrien (cp[1] == '\\'))) 2730236769Sobrien { 2731236769Sobrien Buf_AddByte(&buf, cp[1]); 2732236769Sobrien cp++; 2733236769Sobrien } else if (*cp == '$') { 2734236769Sobrien /* 2735236769Sobrien * If unescaped dollar sign, assume it's a 2736236769Sobrien * variable substitution and recurse. 2737236769Sobrien */ 2738236769Sobrien char *cp2; 2739236769Sobrien int len; 2740236769Sobrien void *freeIt; 2741236769Sobrien 2742236769Sobrien cp2 = Var_Parse(cp, ctxt, errnum, &len, &freeIt); 2743236769Sobrien Buf_AddBytes(&buf, strlen(cp2), cp2); 2744236769Sobrien if (freeIt) 2745236769Sobrien free(freeIt); 2746236769Sobrien cp += len - 1; 2747236769Sobrien } else { 2748236769Sobrien Buf_AddByte(&buf, *cp); 2749236769Sobrien } 2750236769Sobrien } 2751236769Sobrien 2752236769Sobrien termc = *cp; 2753236769Sobrien 2754236769Sobrien if (*tstr == 'U') 2755236769Sobrien wantit = ((v->flags & VAR_JUNK) != 0); 2756236769Sobrien else 2757236769Sobrien wantit = ((v->flags & VAR_JUNK) == 0); 2758236769Sobrien if ((v->flags & VAR_JUNK) != 0) 2759236769Sobrien v->flags |= VAR_KEEP; 2760236769Sobrien if (wantit) { 2761236769Sobrien newStr = Buf_Destroy(&buf, FALSE); 2762236769Sobrien } else { 2763236769Sobrien newStr = nstr; 2764236769Sobrien Buf_Destroy(&buf, TRUE); 2765236769Sobrien } 2766236769Sobrien break; 2767236769Sobrien } 2768236769Sobrien case 'L': 2769250164Ssjg#ifdef MAKE_FREEBSD_UL 2770250164Ssjg if (FreeBSD_UL) { 2771250164Ssjg char *dp = bmake_strdup(nstr); 2772250164Ssjg for (newStr = dp; *dp; dp++) 2773250164Ssjg *dp = tolower((unsigned char)*dp); 2774250164Ssjg cp = tstr + 1; 2775250164Ssjg termc = *cp; 2776250164Ssjg break; 2777250164Ssjg } 2778250164Ssjg /* FALLTHROUGH */ 2779250164Ssjg#endif 2780236769Sobrien { 2781236769Sobrien if ((v->flags & VAR_JUNK) != 0) 2782236769Sobrien v->flags |= VAR_KEEP; 2783236769Sobrien newStr = bmake_strdup(v->name); 2784236769Sobrien cp = ++tstr; 2785236769Sobrien termc = *tstr; 2786236769Sobrien break; 2787236769Sobrien } 2788236769Sobrien case 'P': 2789236769Sobrien { 2790236769Sobrien GNode *gn; 2791236769Sobrien 2792236769Sobrien if ((v->flags & VAR_JUNK) != 0) 2793236769Sobrien v->flags |= VAR_KEEP; 2794236769Sobrien gn = Targ_FindNode(v->name, TARG_NOCREATE); 2795236769Sobrien if (gn == NULL || gn->type & OP_NOPATH) { 2796236769Sobrien newStr = NULL; 2797236769Sobrien } else if (gn->path) { 2798236769Sobrien newStr = bmake_strdup(gn->path); 2799236769Sobrien } else { 2800236769Sobrien newStr = Dir_FindFile(v->name, Suff_FindPath(gn)); 2801236769Sobrien } 2802236769Sobrien if (!newStr) { 2803236769Sobrien newStr = bmake_strdup(v->name); 2804236769Sobrien } 2805236769Sobrien cp = ++tstr; 2806236769Sobrien termc = *tstr; 2807236769Sobrien break; 2808236769Sobrien } 2809236769Sobrien case '!': 2810236769Sobrien { 2811236769Sobrien const char *emsg; 2812236769Sobrien VarPattern pattern; 2813236769Sobrien pattern.flags = 0; 2814236769Sobrien 2815236769Sobrien delim = '!'; 2816236769Sobrien 2817236769Sobrien cp = ++tstr; 2818236769Sobrien if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum, 2819236769Sobrien &cp, delim, 2820236769Sobrien NULL, &pattern.rightLen, 2821236769Sobrien NULL)) == NULL) 2822236769Sobrien goto cleanup; 2823236769Sobrien newStr = Cmd_Exec(pattern.rhs, &emsg); 2824236769Sobrien free(UNCONST(pattern.rhs)); 2825236769Sobrien if (emsg) 2826236769Sobrien Error(emsg, nstr); 2827236769Sobrien termc = *cp; 2828236769Sobrien delim = '\0'; 2829236769Sobrien if (v->flags & VAR_JUNK) { 2830236769Sobrien v->flags |= VAR_KEEP; 2831236769Sobrien } 2832236769Sobrien break; 2833236769Sobrien } 2834236769Sobrien case '[': 2835236769Sobrien { 2836236769Sobrien /* 2837236769Sobrien * Look for the closing ']', recursively 2838236769Sobrien * expanding any embedded variables. 2839236769Sobrien * 2840236769Sobrien * estr is a pointer to the expanded result, 2841236769Sobrien * which we must free(). 2842236769Sobrien */ 2843236769Sobrien char *estr; 2844236769Sobrien 2845236769Sobrien cp = tstr+1; /* point to char after '[' */ 2846236769Sobrien delim = ']'; /* look for closing ']' */ 2847236769Sobrien estr = VarGetPattern(ctxt, &parsestate, 2848236769Sobrien errnum, &cp, delim, 2849236769Sobrien NULL, NULL, NULL); 2850236769Sobrien if (estr == NULL) 2851236769Sobrien goto cleanup; /* report missing ']' */ 2852236769Sobrien /* now cp points just after the closing ']' */ 2853236769Sobrien delim = '\0'; 2854236769Sobrien if (cp[0] != ':' && cp[0] != endc) { 2855236769Sobrien /* Found junk after ']' */ 2856236769Sobrien free(estr); 2857236769Sobrien goto bad_modifier; 2858236769Sobrien } 2859236769Sobrien if (estr[0] == '\0') { 2860236769Sobrien /* Found empty square brackets in ":[]". */ 2861236769Sobrien free(estr); 2862236769Sobrien goto bad_modifier; 2863236769Sobrien } else if (estr[0] == '#' && estr[1] == '\0') { 2864236769Sobrien /* Found ":[#]" */ 2865236769Sobrien 2866236769Sobrien /* 2867236769Sobrien * We will need enough space for the decimal 2868236769Sobrien * representation of an int. We calculate the 2869236769Sobrien * space needed for the octal representation, 2870236769Sobrien * and add enough slop to cope with a '-' sign 2871236769Sobrien * (which should never be needed) and a '\0' 2872236769Sobrien * string terminator. 2873236769Sobrien */ 2874236769Sobrien int newStrSize = 2875236769Sobrien (sizeof(int) * CHAR_BIT + 2) / 3 + 2; 2876236769Sobrien 2877236769Sobrien newStr = bmake_malloc(newStrSize); 2878236769Sobrien if (parsestate.oneBigWord) { 2879236769Sobrien strncpy(newStr, "1", newStrSize); 2880236769Sobrien } else { 2881236769Sobrien /* XXX: brk_string() is a rather expensive 2882236769Sobrien * way of counting words. */ 2883236769Sobrien char **av; 2884236769Sobrien char *as; 2885236769Sobrien int ac; 2886236769Sobrien 2887236769Sobrien av = brk_string(nstr, &ac, FALSE, &as); 2888236769Sobrien snprintf(newStr, newStrSize, "%d", ac); 2889236769Sobrien free(as); 2890236769Sobrien free(av); 2891236769Sobrien } 2892236769Sobrien termc = *cp; 2893236769Sobrien free(estr); 2894236769Sobrien break; 2895236769Sobrien } else if (estr[0] == '*' && estr[1] == '\0') { 2896236769Sobrien /* Found ":[*]" */ 2897236769Sobrien parsestate.oneBigWord = TRUE; 2898236769Sobrien newStr = nstr; 2899236769Sobrien termc = *cp; 2900236769Sobrien free(estr); 2901236769Sobrien break; 2902236769Sobrien } else if (estr[0] == '@' && estr[1] == '\0') { 2903236769Sobrien /* Found ":[@]" */ 2904236769Sobrien parsestate.oneBigWord = FALSE; 2905236769Sobrien newStr = nstr; 2906236769Sobrien termc = *cp; 2907236769Sobrien free(estr); 2908236769Sobrien break; 2909236769Sobrien } else { 2910236769Sobrien /* 2911236769Sobrien * We expect estr to contain a single 2912236769Sobrien * integer for :[N], or two integers 2913236769Sobrien * separated by ".." for :[start..end]. 2914236769Sobrien */ 2915236769Sobrien char *ep; 2916236769Sobrien 2917236769Sobrien VarSelectWords_t seldata = { 0, 0 }; 2918236769Sobrien 2919236769Sobrien seldata.start = strtol(estr, &ep, 0); 2920236769Sobrien if (ep == estr) { 2921236769Sobrien /* Found junk instead of a number */ 2922236769Sobrien free(estr); 2923236769Sobrien goto bad_modifier; 2924236769Sobrien } else if (ep[0] == '\0') { 2925236769Sobrien /* Found only one integer in :[N] */ 2926236769Sobrien seldata.end = seldata.start; 2927236769Sobrien } else if (ep[0] == '.' && ep[1] == '.' && 2928236769Sobrien ep[2] != '\0') { 2929236769Sobrien /* Expecting another integer after ".." */ 2930236769Sobrien ep += 2; 2931236769Sobrien seldata.end = strtol(ep, &ep, 0); 2932236769Sobrien if (ep[0] != '\0') { 2933236769Sobrien /* Found junk after ".." */ 2934236769Sobrien free(estr); 2935236769Sobrien goto bad_modifier; 2936236769Sobrien } 2937236769Sobrien } else { 2938236769Sobrien /* Found junk instead of ".." */ 2939236769Sobrien free(estr); 2940236769Sobrien goto bad_modifier; 2941236769Sobrien } 2942236769Sobrien /* 2943236769Sobrien * Now seldata is properly filled in, 2944236769Sobrien * but we still have to check for 0 as 2945236769Sobrien * a special case. 2946236769Sobrien */ 2947236769Sobrien if (seldata.start == 0 && seldata.end == 0) { 2948236769Sobrien /* ":[0]" or perhaps ":[0..0]" */ 2949236769Sobrien parsestate.oneBigWord = TRUE; 2950236769Sobrien newStr = nstr; 2951236769Sobrien termc = *cp; 2952236769Sobrien free(estr); 2953236769Sobrien break; 2954236769Sobrien } else if (seldata.start == 0 || 2955236769Sobrien seldata.end == 0) { 2956236769Sobrien /* ":[0..N]" or ":[N..0]" */ 2957236769Sobrien free(estr); 2958236769Sobrien goto bad_modifier; 2959236769Sobrien } 2960236769Sobrien /* 2961236769Sobrien * Normal case: select the words 2962236769Sobrien * described by seldata. 2963236769Sobrien */ 2964236769Sobrien newStr = VarSelectWords(ctxt, &parsestate, 2965236769Sobrien nstr, &seldata); 2966236769Sobrien 2967236769Sobrien termc = *cp; 2968236769Sobrien free(estr); 2969236769Sobrien break; 2970236769Sobrien } 2971236769Sobrien 2972236769Sobrien } 2973236769Sobrien case 'g': 2974236769Sobrien cp = tstr + 1; /* make sure it is set */ 2975236769Sobrien if (STRMOD_MATCH(tstr, "gmtime", 6)) { 2976236769Sobrien newStr = VarStrftime(nstr, 1); 2977236769Sobrien cp = tstr + 6; 2978236769Sobrien termc = *cp; 2979236769Sobrien } else { 2980236769Sobrien goto default_case; 2981236769Sobrien } 2982236769Sobrien break; 2983236769Sobrien case 'h': 2984236769Sobrien cp = tstr + 1; /* make sure it is set */ 2985236769Sobrien if (STRMOD_MATCH(tstr, "hash", 4)) { 2986236769Sobrien newStr = VarHash(nstr); 2987236769Sobrien cp = tstr + 4; 2988236769Sobrien termc = *cp; 2989236769Sobrien } else { 2990236769Sobrien goto default_case; 2991236769Sobrien } 2992236769Sobrien break; 2993236769Sobrien case 'l': 2994236769Sobrien cp = tstr + 1; /* make sure it is set */ 2995236769Sobrien if (STRMOD_MATCH(tstr, "localtime", 9)) { 2996236769Sobrien newStr = VarStrftime(nstr, 0); 2997236769Sobrien cp = tstr + 9; 2998236769Sobrien termc = *cp; 2999236769Sobrien } else { 3000236769Sobrien goto default_case; 3001236769Sobrien } 3002236769Sobrien break; 3003236769Sobrien case 't': 3004236769Sobrien { 3005236769Sobrien cp = tstr + 1; /* make sure it is set */ 3006236769Sobrien if (tstr[1] != endc && tstr[1] != ':') { 3007236769Sobrien if (tstr[1] == 's') { 3008236769Sobrien /* 3009236769Sobrien * Use the char (if any) at tstr[2] 3010236769Sobrien * as the word separator. 3011236769Sobrien */ 3012236769Sobrien VarPattern pattern; 3013236769Sobrien 3014236769Sobrien if (tstr[2] != endc && 3015236769Sobrien (tstr[3] == endc || tstr[3] == ':')) { 3016236769Sobrien /* ":ts<unrecognised><endc>" or 3017236769Sobrien * ":ts<unrecognised>:" */ 3018236769Sobrien parsestate.varSpace = tstr[2]; 3019236769Sobrien cp = tstr + 3; 3020236769Sobrien } else if (tstr[2] == endc || tstr[2] == ':') { 3021236769Sobrien /* ":ts<endc>" or ":ts:" */ 3022236769Sobrien parsestate.varSpace = 0; /* no separator */ 3023236769Sobrien cp = tstr + 2; 3024236769Sobrien } else if (tstr[2] == '\\') { 3025236769Sobrien switch (tstr[3]) { 3026236769Sobrien case 'n': 3027236769Sobrien parsestate.varSpace = '\n'; 3028236769Sobrien cp = tstr + 4; 3029236769Sobrien break; 3030236769Sobrien case 't': 3031236769Sobrien parsestate.varSpace = '\t'; 3032236769Sobrien cp = tstr + 4; 3033236769Sobrien break; 3034236769Sobrien default: 3035236769Sobrien if (isdigit((unsigned char)tstr[3])) { 3036236769Sobrien char *ep; 3037236769Sobrien 3038236769Sobrien parsestate.varSpace = 3039236769Sobrien strtoul(&tstr[3], &ep, 0); 3040236769Sobrien if (*ep != ':' && *ep != endc) 3041236769Sobrien goto bad_modifier; 3042236769Sobrien cp = ep; 3043236769Sobrien } else { 3044236769Sobrien /* 3045236769Sobrien * ":ts<backslash><unrecognised>". 3046236769Sobrien */ 3047236769Sobrien goto bad_modifier; 3048236769Sobrien } 3049236769Sobrien break; 3050236769Sobrien } 3051236769Sobrien } else { 3052236769Sobrien /* 3053236769Sobrien * Found ":ts<unrecognised><unrecognised>". 3054236769Sobrien */ 3055236769Sobrien goto bad_modifier; 3056236769Sobrien } 3057236769Sobrien 3058236769Sobrien termc = *cp; 3059236769Sobrien 3060236769Sobrien /* 3061236769Sobrien * We cannot be certain that VarModify 3062236769Sobrien * will be used - even if there is a 3063236769Sobrien * subsequent modifier, so do a no-op 3064236769Sobrien * VarSubstitute now to for str to be 3065236769Sobrien * re-expanded without the spaces. 3066236769Sobrien */ 3067236769Sobrien pattern.flags = VAR_SUB_ONE; 3068236769Sobrien pattern.lhs = pattern.rhs = "\032"; 3069236769Sobrien pattern.leftLen = pattern.rightLen = 1; 3070236769Sobrien 3071236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, 3072236769Sobrien VarSubstitute, 3073236769Sobrien &pattern); 3074236769Sobrien } else if (tstr[2] == endc || tstr[2] == ':') { 3075236769Sobrien /* 3076236769Sobrien * Check for two-character options: 3077236769Sobrien * ":tu", ":tl" 3078236769Sobrien */ 3079236769Sobrien if (tstr[1] == 'A') { /* absolute path */ 3080236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, 3081236769Sobrien VarRealpath, NULL); 3082236769Sobrien cp = tstr + 2; 3083236769Sobrien termc = *cp; 3084249033Ssjg } else if (tstr[1] == 'u') { 3085249033Ssjg char *dp = bmake_strdup(nstr); 3086249033Ssjg for (newStr = dp; *dp; dp++) 3087249033Ssjg *dp = toupper((unsigned char)*dp); 3088236769Sobrien cp = tstr + 2; 3089236769Sobrien termc = *cp; 3090249033Ssjg } else if (tstr[1] == 'l') { 3091249033Ssjg char *dp = bmake_strdup(nstr); 3092249033Ssjg for (newStr = dp; *dp; dp++) 3093249033Ssjg *dp = tolower((unsigned char)*dp); 3094249033Ssjg cp = tstr + 2; 3095249033Ssjg termc = *cp; 3096236769Sobrien } else if (tstr[1] == 'W' || tstr[1] == 'w') { 3097236769Sobrien parsestate.oneBigWord = (tstr[1] == 'W'); 3098236769Sobrien newStr = nstr; 3099236769Sobrien cp = tstr + 2; 3100236769Sobrien termc = *cp; 3101236769Sobrien } else { 3102236769Sobrien /* Found ":t<unrecognised>:" or 3103236769Sobrien * ":t<unrecognised><endc>". */ 3104236769Sobrien goto bad_modifier; 3105236769Sobrien } 3106236769Sobrien } else { 3107236769Sobrien /* 3108236769Sobrien * Found ":t<unrecognised><unrecognised>". 3109236769Sobrien */ 3110236769Sobrien goto bad_modifier; 3111236769Sobrien } 3112236769Sobrien } else { 3113236769Sobrien /* 3114236769Sobrien * Found ":t<endc>" or ":t:". 3115236769Sobrien */ 3116236769Sobrien goto bad_modifier; 3117236769Sobrien } 3118236769Sobrien break; 3119236769Sobrien } 3120236769Sobrien case 'N': 3121236769Sobrien case 'M': 3122236769Sobrien { 3123236769Sobrien char *pattern; 3124236769Sobrien const char *endpat; /* points just after end of pattern */ 3125236769Sobrien char *cp2; 3126236769Sobrien Boolean copy; /* pattern should be, or has been, copied */ 3127236769Sobrien Boolean needSubst; 3128236769Sobrien int nest; 3129236769Sobrien 3130236769Sobrien copy = FALSE; 3131236769Sobrien needSubst = FALSE; 3132236769Sobrien nest = 1; 3133236769Sobrien /* 3134236769Sobrien * In the loop below, ignore ':' unless we are at 3135236769Sobrien * (or back to) the original brace level. 3136236769Sobrien * XXX This will likely not work right if $() and ${} 3137236769Sobrien * are intermixed. 3138236769Sobrien */ 3139236769Sobrien for (cp = tstr + 1; 3140236769Sobrien *cp != '\0' && !(*cp == ':' && nest == 1); 3141236769Sobrien cp++) 3142236769Sobrien { 3143236769Sobrien if (*cp == '\\' && 3144236769Sobrien (cp[1] == ':' || 3145236769Sobrien cp[1] == endc || cp[1] == startc)) { 3146236769Sobrien if (!needSubst) { 3147236769Sobrien copy = TRUE; 3148236769Sobrien } 3149236769Sobrien cp++; 3150236769Sobrien continue; 3151236769Sobrien } 3152236769Sobrien if (*cp == '$') { 3153236769Sobrien needSubst = TRUE; 3154236769Sobrien } 3155236769Sobrien if (*cp == '(' || *cp == '{') 3156236769Sobrien ++nest; 3157236769Sobrien if (*cp == ')' || *cp == '}') { 3158236769Sobrien --nest; 3159236769Sobrien if (nest == 0) 3160236769Sobrien break; 3161236769Sobrien } 3162236769Sobrien } 3163236769Sobrien termc = *cp; 3164236769Sobrien endpat = cp; 3165236769Sobrien if (copy) { 3166236769Sobrien /* 3167236769Sobrien * Need to compress the \:'s out of the pattern, so 3168236769Sobrien * allocate enough room to hold the uncompressed 3169236769Sobrien * pattern (note that cp started at tstr+1, so 3170236769Sobrien * cp - tstr takes the null byte into account) and 3171236769Sobrien * compress the pattern into the space. 3172236769Sobrien */ 3173236769Sobrien pattern = bmake_malloc(cp - tstr); 3174236769Sobrien for (cp2 = pattern, cp = tstr + 1; 3175236769Sobrien cp < endpat; 3176236769Sobrien cp++, cp2++) 3177236769Sobrien { 3178236769Sobrien if ((*cp == '\\') && (cp+1 < endpat) && 3179236769Sobrien (cp[1] == ':' || cp[1] == endc)) { 3180236769Sobrien cp++; 3181236769Sobrien } 3182236769Sobrien *cp2 = *cp; 3183236769Sobrien } 3184236769Sobrien *cp2 = '\0'; 3185236769Sobrien endpat = cp2; 3186236769Sobrien } else { 3187236769Sobrien /* 3188236769Sobrien * Either Var_Subst or VarModify will need a 3189236769Sobrien * nul-terminated string soon, so construct one now. 3190236769Sobrien */ 3191236769Sobrien pattern = bmake_strndup(tstr+1, endpat - (tstr + 1)); 3192236769Sobrien } 3193236769Sobrien if (needSubst) { 3194236769Sobrien /* 3195236769Sobrien * pattern contains embedded '$', so use Var_Subst to 3196236769Sobrien * expand it. 3197236769Sobrien */ 3198236769Sobrien cp2 = pattern; 3199236769Sobrien pattern = Var_Subst(NULL, cp2, ctxt, errnum); 3200236769Sobrien free(cp2); 3201236769Sobrien } 3202236769Sobrien if (DEBUG(VAR)) 3203246223Ssjg fprintf(debug_file, "Pattern[%s] for [%s] is [%s]\n", 3204246223Ssjg v->name, nstr, pattern); 3205236769Sobrien if (*tstr == 'M') { 3206236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarMatch, 3207236769Sobrien pattern); 3208236769Sobrien } else { 3209236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarNoMatch, 3210236769Sobrien pattern); 3211236769Sobrien } 3212236769Sobrien free(pattern); 3213236769Sobrien break; 3214236769Sobrien } 3215236769Sobrien case 'S': 3216236769Sobrien { 3217236769Sobrien VarPattern pattern; 3218236769Sobrien Var_Parse_State tmpparsestate; 3219236769Sobrien 3220236769Sobrien pattern.flags = 0; 3221236769Sobrien tmpparsestate = parsestate; 3222236769Sobrien delim = tstr[1]; 3223236769Sobrien tstr += 2; 3224236769Sobrien 3225236769Sobrien /* 3226236769Sobrien * If pattern begins with '^', it is anchored to the 3227236769Sobrien * start of the word -- skip over it and flag pattern. 3228236769Sobrien */ 3229236769Sobrien if (*tstr == '^') { 3230236769Sobrien pattern.flags |= VAR_MATCH_START; 3231236769Sobrien tstr += 1; 3232236769Sobrien } 3233236769Sobrien 3234236769Sobrien cp = tstr; 3235236769Sobrien if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, errnum, 3236236769Sobrien &cp, delim, 3237236769Sobrien &pattern.flags, 3238236769Sobrien &pattern.leftLen, 3239236769Sobrien NULL)) == NULL) 3240236769Sobrien goto cleanup; 3241236769Sobrien 3242236769Sobrien if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum, 3243236769Sobrien &cp, delim, NULL, 3244236769Sobrien &pattern.rightLen, 3245236769Sobrien &pattern)) == NULL) 3246236769Sobrien goto cleanup; 3247236769Sobrien 3248236769Sobrien /* 3249236769Sobrien * Check for global substitution. If 'g' after the final 3250236769Sobrien * delimiter, substitution is global and is marked that 3251236769Sobrien * way. 3252236769Sobrien */ 3253236769Sobrien for (;; cp++) { 3254236769Sobrien switch (*cp) { 3255236769Sobrien case 'g': 3256236769Sobrien pattern.flags |= VAR_SUB_GLOBAL; 3257236769Sobrien continue; 3258236769Sobrien case '1': 3259236769Sobrien pattern.flags |= VAR_SUB_ONE; 3260236769Sobrien continue; 3261236769Sobrien case 'W': 3262236769Sobrien tmpparsestate.oneBigWord = TRUE; 3263236769Sobrien continue; 3264236769Sobrien } 3265236769Sobrien break; 3266236769Sobrien } 3267236769Sobrien 3268236769Sobrien termc = *cp; 3269236769Sobrien newStr = VarModify(ctxt, &tmpparsestate, nstr, 3270236769Sobrien VarSubstitute, 3271236769Sobrien &pattern); 3272236769Sobrien 3273236769Sobrien /* 3274236769Sobrien * Free the two strings. 3275236769Sobrien */ 3276236769Sobrien free(UNCONST(pattern.lhs)); 3277236769Sobrien free(UNCONST(pattern.rhs)); 3278236769Sobrien delim = '\0'; 3279236769Sobrien break; 3280236769Sobrien } 3281236769Sobrien case '?': 3282236769Sobrien { 3283236769Sobrien VarPattern pattern; 3284236769Sobrien Boolean value; 3285236769Sobrien 3286236769Sobrien /* find ':', and then substitute accordingly */ 3287236769Sobrien 3288236769Sobrien pattern.flags = 0; 3289236769Sobrien 3290236769Sobrien cp = ++tstr; 3291236769Sobrien delim = ':'; 3292236769Sobrien if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, errnum, 3293236769Sobrien &cp, delim, NULL, 3294236769Sobrien &pattern.leftLen, 3295236769Sobrien NULL)) == NULL) 3296236769Sobrien goto cleanup; 3297236769Sobrien 3298236769Sobrien /* BROPEN or PROPEN */ 3299236769Sobrien delim = endc; 3300236769Sobrien if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, errnum, 3301236769Sobrien &cp, delim, NULL, 3302236769Sobrien &pattern.rightLen, 3303236769Sobrien NULL)) == NULL) 3304236769Sobrien goto cleanup; 3305236769Sobrien 3306236769Sobrien termc = *--cp; 3307236769Sobrien delim = '\0'; 3308236769Sobrien if (Cond_EvalExpression(NULL, v->name, &value, 0) 3309236769Sobrien == COND_INVALID) { 3310236769Sobrien Error("Bad conditional expression `%s' in %s?%s:%s", 3311236769Sobrien v->name, v->name, pattern.lhs, pattern.rhs); 3312236769Sobrien goto cleanup; 3313236769Sobrien } 3314236769Sobrien 3315236769Sobrien if (value) { 3316236769Sobrien newStr = UNCONST(pattern.lhs); 3317236769Sobrien free(UNCONST(pattern.rhs)); 3318236769Sobrien } else { 3319236769Sobrien newStr = UNCONST(pattern.rhs); 3320236769Sobrien free(UNCONST(pattern.lhs)); 3321236769Sobrien } 3322236769Sobrien if (v->flags & VAR_JUNK) { 3323236769Sobrien v->flags |= VAR_KEEP; 3324236769Sobrien } 3325236769Sobrien break; 3326236769Sobrien } 3327236769Sobrien#ifndef NO_REGEX 3328236769Sobrien case 'C': 3329236769Sobrien { 3330236769Sobrien VarREPattern pattern; 3331236769Sobrien char *re; 3332236769Sobrien int error; 3333236769Sobrien Var_Parse_State tmpparsestate; 3334236769Sobrien 3335236769Sobrien pattern.flags = 0; 3336236769Sobrien tmpparsestate = parsestate; 3337236769Sobrien delim = tstr[1]; 3338236769Sobrien tstr += 2; 3339236769Sobrien 3340236769Sobrien cp = tstr; 3341236769Sobrien 3342236769Sobrien if ((re = VarGetPattern(ctxt, &parsestate, errnum, &cp, delim, 3343236769Sobrien NULL, NULL, NULL)) == NULL) 3344236769Sobrien goto cleanup; 3345236769Sobrien 3346236769Sobrien if ((pattern.replace = VarGetPattern(ctxt, &parsestate, 3347236769Sobrien errnum, &cp, delim, NULL, 3348236769Sobrien NULL, NULL)) == NULL){ 3349236769Sobrien free(re); 3350236769Sobrien goto cleanup; 3351236769Sobrien } 3352236769Sobrien 3353236769Sobrien for (;; cp++) { 3354236769Sobrien switch (*cp) { 3355236769Sobrien case 'g': 3356236769Sobrien pattern.flags |= VAR_SUB_GLOBAL; 3357236769Sobrien continue; 3358236769Sobrien case '1': 3359236769Sobrien pattern.flags |= VAR_SUB_ONE; 3360236769Sobrien continue; 3361236769Sobrien case 'W': 3362236769Sobrien tmpparsestate.oneBigWord = TRUE; 3363236769Sobrien continue; 3364236769Sobrien } 3365236769Sobrien break; 3366236769Sobrien } 3367236769Sobrien 3368236769Sobrien termc = *cp; 3369236769Sobrien 3370236769Sobrien error = regcomp(&pattern.re, re, REG_EXTENDED); 3371236769Sobrien free(re); 3372236769Sobrien if (error) { 3373236769Sobrien *lengthPtr = cp - start + 1; 3374236769Sobrien VarREError(error, &pattern.re, "RE substitution error"); 3375236769Sobrien free(pattern.replace); 3376236769Sobrien goto cleanup; 3377236769Sobrien } 3378236769Sobrien 3379236769Sobrien pattern.nsub = pattern.re.re_nsub + 1; 3380236769Sobrien if (pattern.nsub < 1) 3381236769Sobrien pattern.nsub = 1; 3382236769Sobrien if (pattern.nsub > 10) 3383236769Sobrien pattern.nsub = 10; 3384236769Sobrien pattern.matches = bmake_malloc(pattern.nsub * 3385236769Sobrien sizeof(regmatch_t)); 3386236769Sobrien newStr = VarModify(ctxt, &tmpparsestate, nstr, 3387236769Sobrien VarRESubstitute, 3388236769Sobrien &pattern); 3389236769Sobrien regfree(&pattern.re); 3390236769Sobrien free(pattern.replace); 3391236769Sobrien free(pattern.matches); 3392236769Sobrien delim = '\0'; 3393236769Sobrien break; 3394236769Sobrien } 3395236769Sobrien#endif 3396236769Sobrien case 'Q': 3397236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3398236769Sobrien newStr = VarQuote(nstr); 3399236769Sobrien cp = tstr + 1; 3400236769Sobrien termc = *cp; 3401236769Sobrien break; 3402236769Sobrien } 3403236769Sobrien goto default_case; 3404236769Sobrien case 'T': 3405236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3406236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarTail, 3407236769Sobrien NULL); 3408236769Sobrien cp = tstr + 1; 3409236769Sobrien termc = *cp; 3410236769Sobrien break; 3411236769Sobrien } 3412236769Sobrien goto default_case; 3413236769Sobrien case 'H': 3414236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3415236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarHead, 3416236769Sobrien NULL); 3417236769Sobrien cp = tstr + 1; 3418236769Sobrien termc = *cp; 3419236769Sobrien break; 3420236769Sobrien } 3421236769Sobrien goto default_case; 3422236769Sobrien case 'E': 3423236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3424236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarSuffix, 3425236769Sobrien NULL); 3426236769Sobrien cp = tstr + 1; 3427236769Sobrien termc = *cp; 3428236769Sobrien break; 3429236769Sobrien } 3430236769Sobrien goto default_case; 3431236769Sobrien case 'R': 3432236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3433236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, VarRoot, 3434236769Sobrien NULL); 3435236769Sobrien cp = tstr + 1; 3436236769Sobrien termc = *cp; 3437236769Sobrien break; 3438236769Sobrien } 3439236769Sobrien goto default_case; 3440236769Sobrien case 'O': 3441236769Sobrien { 3442236769Sobrien char otype; 3443236769Sobrien 3444236769Sobrien cp = tstr + 1; /* skip to the rest in any case */ 3445236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3446236769Sobrien otype = 's'; 3447236769Sobrien termc = *cp; 3448236769Sobrien } else if ( (tstr[1] == 'x') && 3449236769Sobrien (tstr[2] == endc || tstr[2] == ':') ) { 3450236769Sobrien otype = tstr[1]; 3451236769Sobrien cp = tstr + 2; 3452236769Sobrien termc = *cp; 3453236769Sobrien } else { 3454236769Sobrien goto bad_modifier; 3455236769Sobrien } 3456236769Sobrien newStr = VarOrder(nstr, otype); 3457236769Sobrien break; 3458236769Sobrien } 3459236769Sobrien case 'u': 3460236769Sobrien if (tstr[1] == endc || tstr[1] == ':') { 3461236769Sobrien newStr = VarUniq(nstr); 3462236769Sobrien cp = tstr + 1; 3463236769Sobrien termc = *cp; 3464236769Sobrien break; 3465236769Sobrien } 3466236769Sobrien goto default_case; 3467236769Sobrien#ifdef SUNSHCMD 3468236769Sobrien case 's': 3469236769Sobrien if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) { 3470236769Sobrien const char *emsg; 3471236769Sobrien newStr = Cmd_Exec(nstr, &emsg); 3472236769Sobrien if (emsg) 3473236769Sobrien Error(emsg, nstr); 3474236769Sobrien cp = tstr + 2; 3475236769Sobrien termc = *cp; 3476236769Sobrien break; 3477236769Sobrien } 3478236769Sobrien goto default_case; 3479236769Sobrien#endif 3480236769Sobrien default: 3481236769Sobrien default_case: 3482236769Sobrien { 3483236769Sobrien#ifdef SYSVVARSUB 3484236769Sobrien /* 3485236769Sobrien * This can either be a bogus modifier or a System-V 3486236769Sobrien * substitution command. 3487236769Sobrien */ 3488236769Sobrien VarPattern pattern; 3489236769Sobrien Boolean eqFound; 3490236769Sobrien 3491236769Sobrien pattern.flags = 0; 3492236769Sobrien eqFound = FALSE; 3493236769Sobrien /* 3494236769Sobrien * First we make a pass through the string trying 3495236769Sobrien * to verify it is a SYSV-make-style translation: 3496236769Sobrien * it must be: <string1>=<string2>) 3497236769Sobrien */ 3498236769Sobrien cp = tstr; 3499236769Sobrien cnt = 1; 3500236769Sobrien while (*cp != '\0' && cnt) { 3501236769Sobrien if (*cp == '=') { 3502236769Sobrien eqFound = TRUE; 3503236769Sobrien /* continue looking for endc */ 3504236769Sobrien } 3505236769Sobrien else if (*cp == endc) 3506236769Sobrien cnt--; 3507236769Sobrien else if (*cp == startc) 3508236769Sobrien cnt++; 3509236769Sobrien if (cnt) 3510236769Sobrien cp++; 3511236769Sobrien } 3512236769Sobrien if (*cp == endc && eqFound) { 3513236769Sobrien 3514236769Sobrien /* 3515236769Sobrien * Now we break this sucker into the lhs and 3516236769Sobrien * rhs. We must null terminate them of course. 3517236769Sobrien */ 3518236769Sobrien delim='='; 3519236769Sobrien cp = tstr; 3520236769Sobrien if ((pattern.lhs = VarGetPattern(ctxt, &parsestate, 3521236769Sobrien errnum, &cp, delim, &pattern.flags, 3522236769Sobrien &pattern.leftLen, NULL)) == NULL) 3523236769Sobrien goto cleanup; 3524236769Sobrien delim = endc; 3525236769Sobrien if ((pattern.rhs = VarGetPattern(ctxt, &parsestate, 3526236769Sobrien errnum, &cp, delim, NULL, &pattern.rightLen, 3527236769Sobrien &pattern)) == NULL) 3528236769Sobrien goto cleanup; 3529236769Sobrien 3530236769Sobrien /* 3531236769Sobrien * SYSV modifications happen through the whole 3532236769Sobrien * string. Note the pattern is anchored at the end. 3533236769Sobrien */ 3534236769Sobrien termc = *--cp; 3535236769Sobrien delim = '\0'; 3536236769Sobrien if (pattern.leftLen == 0 && *nstr == '\0') { 3537236769Sobrien newStr = nstr; /* special case */ 3538236769Sobrien } else { 3539236769Sobrien newStr = VarModify(ctxt, &parsestate, nstr, 3540236769Sobrien VarSYSVMatch, 3541236769Sobrien &pattern); 3542236769Sobrien } 3543236769Sobrien free(UNCONST(pattern.lhs)); 3544236769Sobrien free(UNCONST(pattern.rhs)); 3545236769Sobrien } else 3546236769Sobrien#endif 3547236769Sobrien { 3548236769Sobrien Error("Unknown modifier '%c'", *tstr); 3549236769Sobrien for (cp = tstr+1; 3550236769Sobrien *cp != ':' && *cp != endc && *cp != '\0'; 3551236769Sobrien cp++) 3552236769Sobrien continue; 3553236769Sobrien termc = *cp; 3554236769Sobrien newStr = var_Error; 3555236769Sobrien } 3556236769Sobrien } 3557236769Sobrien } 3558236769Sobrien if (DEBUG(VAR)) { 3559246223Ssjg fprintf(debug_file, "Result[%s] of :%c is \"%s\"\n", 3560246223Ssjg v->name, modifier, newStr); 3561236769Sobrien } 3562236769Sobrien 3563236769Sobrien if (newStr != nstr) { 3564236769Sobrien if (*freePtr) { 3565236769Sobrien free(nstr); 3566236769Sobrien *freePtr = NULL; 3567236769Sobrien } 3568236769Sobrien nstr = newStr; 3569236769Sobrien if (nstr != var_Error && nstr != varNoError) { 3570236769Sobrien *freePtr = nstr; 3571236769Sobrien } 3572236769Sobrien } 3573236769Sobrien if (termc == '\0' && endc != '\0') { 3574236769Sobrien Error("Unclosed variable specification (expecting '%c') for \"%s\" (value \"%s\") modifier %c", endc, v->name, nstr, modifier); 3575236769Sobrien } else if (termc == ':') { 3576236769Sobrien cp++; 3577236769Sobrien } 3578236769Sobrien tstr = cp; 3579236769Sobrien } 3580236769Sobrien out: 3581236769Sobrien *lengthPtr = tstr - start; 3582236769Sobrien return (nstr); 3583236769Sobrien 3584236769Sobrien bad_modifier: 3585236769Sobrien /* "{(" */ 3586236769Sobrien Error("Bad modifier `:%.*s' for %s", (int)strcspn(tstr, ":)}"), tstr, 3587236769Sobrien v->name); 3588236769Sobrien 3589236769Sobrien cleanup: 3590236769Sobrien *lengthPtr = cp - start; 3591236769Sobrien if (delim != '\0') 3592236769Sobrien Error("Unclosed substitution for %s (%c missing)", 3593236769Sobrien v->name, delim); 3594236769Sobrien if (*freePtr) { 3595236769Sobrien free(*freePtr); 3596236769Sobrien *freePtr = NULL; 3597236769Sobrien } 3598236769Sobrien return (var_Error); 3599236769Sobrien} 3600236769Sobrien 3601236769Sobrien/*- 3602236769Sobrien *----------------------------------------------------------------------- 3603236769Sobrien * Var_Parse -- 3604236769Sobrien * Given the start of a variable invocation, extract the variable 3605236769Sobrien * name and find its value, then modify it according to the 3606236769Sobrien * specification. 3607236769Sobrien * 3608236769Sobrien * Input: 3609236769Sobrien * str The string to parse 3610236769Sobrien * ctxt The context for the variable 3611236769Sobrien * errnum TRUE if undefined variables are an error 3612236769Sobrien * lengthPtr OUT: The length of the specification 3613236769Sobrien * freePtr OUT: Non-NULL if caller should free *freePtr 3614236769Sobrien * 3615236769Sobrien * Results: 3616236769Sobrien * The (possibly-modified) value of the variable or var_Error if the 3617236769Sobrien * specification is invalid. The length of the specification is 3618236769Sobrien * placed in *lengthPtr (for invalid specifications, this is just 3619236769Sobrien * 2...?). 3620236769Sobrien * If *freePtr is non-NULL then it's a pointer that the caller 3621236769Sobrien * should pass to free() to free memory used by the result. 3622236769Sobrien * 3623236769Sobrien * Side Effects: 3624236769Sobrien * None. 3625236769Sobrien * 3626236769Sobrien *----------------------------------------------------------------------- 3627236769Sobrien */ 3628236769Sobrien/* coverity[+alloc : arg-*4] */ 3629236769Sobrienchar * 3630236769SobrienVar_Parse(const char *str, GNode *ctxt, Boolean errnum, int *lengthPtr, 3631236769Sobrien void **freePtr) 3632236769Sobrien{ 3633236769Sobrien const char *tstr; /* Pointer into str */ 3634236769Sobrien Var *v; /* Variable in invocation */ 3635236769Sobrien Boolean haveModifier;/* TRUE if have modifiers for the variable */ 3636236769Sobrien char endc; /* Ending character when variable in parens 3637236769Sobrien * or braces */ 3638236769Sobrien char startc; /* Starting character when variable in parens 3639236769Sobrien * or braces */ 3640236769Sobrien int vlen; /* Length of variable name */ 3641236769Sobrien const char *start; /* Points to original start of str */ 3642236769Sobrien char *nstr; /* New string, used during expansion */ 3643236769Sobrien Boolean dynamic; /* TRUE if the variable is local and we're 3644236769Sobrien * expanding it in a non-local context. This 3645236769Sobrien * is done to support dynamic sources. The 3646236769Sobrien * result is just the invocation, unaltered */ 3647236769Sobrien Var_Parse_State parsestate; /* Flags passed to helper functions */ 3648236769Sobrien char name[2]; 3649236769Sobrien 3650236769Sobrien *freePtr = NULL; 3651236769Sobrien dynamic = FALSE; 3652236769Sobrien start = str; 3653236769Sobrien parsestate.oneBigWord = FALSE; 3654236769Sobrien parsestate.varSpace = ' '; /* word separator */ 3655236769Sobrien 3656236769Sobrien startc = str[1]; 3657236769Sobrien if (startc != PROPEN && startc != BROPEN) { 3658236769Sobrien /* 3659236769Sobrien * If it's not bounded by braces of some sort, life is much simpler. 3660236769Sobrien * We just need to check for the first character and return the 3661236769Sobrien * value if it exists. 3662236769Sobrien */ 3663236769Sobrien 3664236769Sobrien /* Error out some really stupid names */ 3665236769Sobrien if (startc == '\0' || strchr(")}:$", startc)) { 3666236769Sobrien *lengthPtr = 1; 3667236769Sobrien return var_Error; 3668236769Sobrien } 3669236769Sobrien name[0] = startc; 3670236769Sobrien name[1] = '\0'; 3671236769Sobrien 3672236769Sobrien v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 3673236769Sobrien if (v == NULL) { 3674236769Sobrien *lengthPtr = 2; 3675236769Sobrien 3676236769Sobrien if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) { 3677236769Sobrien /* 3678236769Sobrien * If substituting a local variable in a non-local context, 3679236769Sobrien * assume it's for dynamic source stuff. We have to handle 3680236769Sobrien * this specially and return the longhand for the variable 3681236769Sobrien * with the dollar sign escaped so it makes it back to the 3682236769Sobrien * caller. Only four of the local variables are treated 3683236769Sobrien * specially as they are the only four that will be set 3684236769Sobrien * when dynamic sources are expanded. 3685236769Sobrien */ 3686236769Sobrien switch (str[1]) { 3687236769Sobrien case '@': 3688236769Sobrien return UNCONST("$(.TARGET)"); 3689236769Sobrien case '%': 3690236769Sobrien return UNCONST("$(.ARCHIVE)"); 3691236769Sobrien case '*': 3692236769Sobrien return UNCONST("$(.PREFIX)"); 3693236769Sobrien case '!': 3694236769Sobrien return UNCONST("$(.MEMBER)"); 3695236769Sobrien } 3696236769Sobrien } 3697236769Sobrien /* 3698236769Sobrien * Error 3699236769Sobrien */ 3700236769Sobrien return (errnum ? var_Error : varNoError); 3701236769Sobrien } else { 3702236769Sobrien haveModifier = FALSE; 3703236769Sobrien tstr = &str[1]; 3704236769Sobrien endc = str[1]; 3705236769Sobrien } 3706236769Sobrien } else { 3707236769Sobrien Buffer buf; /* Holds the variable name */ 3708236769Sobrien 3709236769Sobrien endc = startc == PROPEN ? PRCLOSE : BRCLOSE; 3710236769Sobrien Buf_Init(&buf, 0); 3711236769Sobrien 3712236769Sobrien /* 3713236769Sobrien * Skip to the end character or a colon, whichever comes first. 3714236769Sobrien */ 3715236769Sobrien for (tstr = str + 2; 3716236769Sobrien *tstr != '\0' && *tstr != endc && *tstr != ':'; 3717236769Sobrien tstr++) 3718236769Sobrien { 3719236769Sobrien /* 3720236769Sobrien * A variable inside a variable, expand 3721236769Sobrien */ 3722236769Sobrien if (*tstr == '$') { 3723236769Sobrien int rlen; 3724236769Sobrien void *freeIt; 3725236769Sobrien char *rval = Var_Parse(tstr, ctxt, errnum, &rlen, &freeIt); 3726236769Sobrien if (rval != NULL) { 3727236769Sobrien Buf_AddBytes(&buf, strlen(rval), rval); 3728236769Sobrien } 3729236769Sobrien if (freeIt) 3730236769Sobrien free(freeIt); 3731236769Sobrien tstr += rlen - 1; 3732236769Sobrien } 3733236769Sobrien else 3734236769Sobrien Buf_AddByte(&buf, *tstr); 3735236769Sobrien } 3736236769Sobrien if (*tstr == ':') { 3737236769Sobrien haveModifier = TRUE; 3738236769Sobrien } else if (*tstr != '\0') { 3739236769Sobrien haveModifier = FALSE; 3740236769Sobrien } else { 3741236769Sobrien /* 3742236769Sobrien * If we never did find the end character, return NULL 3743236769Sobrien * right now, setting the length to be the distance to 3744236769Sobrien * the end of the string, since that's what make does. 3745236769Sobrien */ 3746236769Sobrien *lengthPtr = tstr - str; 3747236769Sobrien Buf_Destroy(&buf, TRUE); 3748236769Sobrien return (var_Error); 3749236769Sobrien } 3750236769Sobrien str = Buf_GetAll(&buf, &vlen); 3751236769Sobrien 3752236769Sobrien /* 3753236769Sobrien * At this point, str points into newly allocated memory from 3754236769Sobrien * buf, containing only the name of the variable. 3755236769Sobrien * 3756236769Sobrien * start and tstr point into the const string that was pointed 3757236769Sobrien * to by the original value of the str parameter. start points 3758236769Sobrien * to the '$' at the beginning of the string, while tstr points 3759236769Sobrien * to the char just after the end of the variable name -- this 3760236769Sobrien * will be '\0', ':', PRCLOSE, or BRCLOSE. 3761236769Sobrien */ 3762236769Sobrien 3763236769Sobrien v = VarFind(str, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); 3764236769Sobrien /* 3765236769Sobrien * Check also for bogus D and F forms of local variables since we're 3766236769Sobrien * in a local context and the name is the right length. 3767236769Sobrien */ 3768236769Sobrien if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) && 3769236769Sobrien (vlen == 2) && (str[1] == 'F' || str[1] == 'D') && 3770236769Sobrien strchr("@%*!<>", str[0]) != NULL) { 3771236769Sobrien /* 3772236769Sobrien * Well, it's local -- go look for it. 3773236769Sobrien */ 3774236769Sobrien name[0] = *str; 3775236769Sobrien name[1] = '\0'; 3776236769Sobrien v = VarFind(name, ctxt, 0); 3777236769Sobrien 3778236769Sobrien if (v != NULL) { 3779236769Sobrien /* 3780236769Sobrien * No need for nested expansion or anything, as we're 3781236769Sobrien * the only one who sets these things and we sure don't 3782236769Sobrien * but nested invocations in them... 3783236769Sobrien */ 3784236769Sobrien nstr = Buf_GetAll(&v->val, NULL); 3785236769Sobrien 3786236769Sobrien if (str[1] == 'D') { 3787236769Sobrien nstr = VarModify(ctxt, &parsestate, nstr, VarHead, 3788236769Sobrien NULL); 3789236769Sobrien } else { 3790236769Sobrien nstr = VarModify(ctxt, &parsestate, nstr, VarTail, 3791236769Sobrien NULL); 3792236769Sobrien } 3793236769Sobrien /* 3794236769Sobrien * Resulting string is dynamically allocated, so 3795236769Sobrien * tell caller to free it. 3796236769Sobrien */ 3797236769Sobrien *freePtr = nstr; 3798236769Sobrien *lengthPtr = tstr-start+1; 3799236769Sobrien Buf_Destroy(&buf, TRUE); 3800236769Sobrien VarFreeEnv(v, TRUE); 3801236769Sobrien return nstr; 3802236769Sobrien } 3803236769Sobrien } 3804236769Sobrien 3805236769Sobrien if (v == NULL) { 3806236769Sobrien if (((vlen == 1) || 3807236769Sobrien (((vlen == 2) && (str[1] == 'F' || str[1] == 'D')))) && 3808236769Sobrien ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) 3809236769Sobrien { 3810236769Sobrien /* 3811236769Sobrien * If substituting a local variable in a non-local context, 3812236769Sobrien * assume it's for dynamic source stuff. We have to handle 3813236769Sobrien * this specially and return the longhand for the variable 3814236769Sobrien * with the dollar sign escaped so it makes it back to the 3815236769Sobrien * caller. Only four of the local variables are treated 3816236769Sobrien * specially as they are the only four that will be set 3817236769Sobrien * when dynamic sources are expanded. 3818236769Sobrien */ 3819236769Sobrien switch (*str) { 3820236769Sobrien case '@': 3821236769Sobrien case '%': 3822236769Sobrien case '*': 3823236769Sobrien case '!': 3824236769Sobrien dynamic = TRUE; 3825236769Sobrien break; 3826236769Sobrien } 3827236769Sobrien } else if ((vlen > 2) && (*str == '.') && 3828236769Sobrien isupper((unsigned char) str[1]) && 3829236769Sobrien ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) 3830236769Sobrien { 3831236769Sobrien int len; 3832236769Sobrien 3833236769Sobrien len = vlen - 1; 3834236769Sobrien if ((strncmp(str, ".TARGET", len) == 0) || 3835236769Sobrien (strncmp(str, ".ARCHIVE", len) == 0) || 3836236769Sobrien (strncmp(str, ".PREFIX", len) == 0) || 3837236769Sobrien (strncmp(str, ".MEMBER", len) == 0)) 3838236769Sobrien { 3839236769Sobrien dynamic = TRUE; 3840236769Sobrien } 3841236769Sobrien } 3842236769Sobrien 3843236769Sobrien if (!haveModifier) { 3844236769Sobrien /* 3845236769Sobrien * No modifiers -- have specification length so we can return 3846236769Sobrien * now. 3847236769Sobrien */ 3848236769Sobrien *lengthPtr = tstr - start + 1; 3849236769Sobrien if (dynamic) { 3850236769Sobrien char *pstr = bmake_strndup(start, *lengthPtr); 3851236769Sobrien *freePtr = pstr; 3852236769Sobrien Buf_Destroy(&buf, TRUE); 3853236769Sobrien return(pstr); 3854236769Sobrien } else { 3855236769Sobrien Buf_Destroy(&buf, TRUE); 3856236769Sobrien return (errnum ? var_Error : varNoError); 3857236769Sobrien } 3858236769Sobrien } else { 3859236769Sobrien /* 3860236769Sobrien * Still need to get to the end of the variable specification, 3861236769Sobrien * so kludge up a Var structure for the modifications 3862236769Sobrien */ 3863236769Sobrien v = bmake_malloc(sizeof(Var)); 3864236769Sobrien v->name = UNCONST(str); 3865236769Sobrien Buf_Init(&v->val, 1); 3866236769Sobrien v->flags = VAR_JUNK; 3867236769Sobrien Buf_Destroy(&buf, FALSE); 3868236769Sobrien } 3869236769Sobrien } else 3870236769Sobrien Buf_Destroy(&buf, TRUE); 3871236769Sobrien } 3872236769Sobrien 3873236769Sobrien if (v->flags & VAR_IN_USE) { 3874236769Sobrien Fatal("Variable %s is recursive.", v->name); 3875236769Sobrien /*NOTREACHED*/ 3876236769Sobrien } else { 3877236769Sobrien v->flags |= VAR_IN_USE; 3878236769Sobrien } 3879236769Sobrien /* 3880236769Sobrien * Before doing any modification, we have to make sure the value 3881236769Sobrien * has been fully expanded. If it looks like recursion might be 3882236769Sobrien * necessary (there's a dollar sign somewhere in the variable's value) 3883236769Sobrien * we just call Var_Subst to do any other substitutions that are 3884236769Sobrien * necessary. Note that the value returned by Var_Subst will have 3885236769Sobrien * been dynamically-allocated, so it will need freeing when we 3886236769Sobrien * return. 3887236769Sobrien */ 3888236769Sobrien nstr = Buf_GetAll(&v->val, NULL); 3889236769Sobrien if (strchr(nstr, '$') != NULL) { 3890236769Sobrien nstr = Var_Subst(NULL, nstr, ctxt, errnum); 3891236769Sobrien *freePtr = nstr; 3892236769Sobrien } 3893236769Sobrien 3894236769Sobrien v->flags &= ~VAR_IN_USE; 3895236769Sobrien 3896236769Sobrien if ((nstr != NULL) && haveModifier) { 3897236769Sobrien int used; 3898236769Sobrien /* 3899236769Sobrien * Skip initial colon. 3900236769Sobrien */ 3901236769Sobrien tstr++; 3902236769Sobrien 3903236769Sobrien nstr = ApplyModifiers(nstr, tstr, startc, endc, 3904236769Sobrien v, ctxt, errnum, &used, freePtr); 3905236769Sobrien tstr += used; 3906236769Sobrien } 3907236769Sobrien if (*tstr) { 3908236769Sobrien *lengthPtr = tstr - start + 1; 3909236769Sobrien } else { 3910236769Sobrien *lengthPtr = tstr - start; 3911236769Sobrien } 3912236769Sobrien 3913236769Sobrien if (v->flags & VAR_FROM_ENV) { 3914236769Sobrien Boolean destroy = FALSE; 3915236769Sobrien 3916236769Sobrien if (nstr != Buf_GetAll(&v->val, NULL)) { 3917236769Sobrien destroy = TRUE; 3918236769Sobrien } else { 3919236769Sobrien /* 3920236769Sobrien * Returning the value unmodified, so tell the caller to free 3921236769Sobrien * the thing. 3922236769Sobrien */ 3923236769Sobrien *freePtr = nstr; 3924236769Sobrien } 3925236769Sobrien VarFreeEnv(v, destroy); 3926236769Sobrien } else if (v->flags & VAR_JUNK) { 3927236769Sobrien /* 3928236769Sobrien * Perform any free'ing needed and set *freePtr to NULL so the caller 3929236769Sobrien * doesn't try to free a static pointer. 3930236769Sobrien * If VAR_KEEP is also set then we want to keep str as is. 3931236769Sobrien */ 3932236769Sobrien if (!(v->flags & VAR_KEEP)) { 3933236769Sobrien if (*freePtr) { 3934236769Sobrien free(nstr); 3935236769Sobrien *freePtr = NULL; 3936236769Sobrien } 3937236769Sobrien if (dynamic) { 3938236769Sobrien nstr = bmake_strndup(start, *lengthPtr); 3939236769Sobrien *freePtr = nstr; 3940236769Sobrien } else { 3941236769Sobrien nstr = errnum ? var_Error : varNoError; 3942236769Sobrien } 3943236769Sobrien } 3944236769Sobrien if (nstr != Buf_GetAll(&v->val, NULL)) 3945236769Sobrien Buf_Destroy(&v->val, TRUE); 3946236769Sobrien free(v->name); 3947236769Sobrien free(v); 3948236769Sobrien } 3949236769Sobrien return (nstr); 3950236769Sobrien} 3951236769Sobrien 3952236769Sobrien/*- 3953236769Sobrien *----------------------------------------------------------------------- 3954236769Sobrien * Var_Subst -- 3955236769Sobrien * Substitute for all variables in the given string in the given context 3956236769Sobrien * If undefErr is TRUE, Parse_Error will be called when an undefined 3957236769Sobrien * variable is encountered. 3958236769Sobrien * 3959236769Sobrien * Input: 3960236769Sobrien * var Named variable || NULL for all 3961236769Sobrien * str the string which to substitute 3962236769Sobrien * ctxt the context wherein to find variables 3963236769Sobrien * undefErr TRUE if undefineds are an error 3964236769Sobrien * 3965236769Sobrien * Results: 3966236769Sobrien * The resulting string. 3967236769Sobrien * 3968236769Sobrien * Side Effects: 3969236769Sobrien * None. The old string must be freed by the caller 3970236769Sobrien *----------------------------------------------------------------------- 3971236769Sobrien */ 3972236769Sobrienchar * 3973236769SobrienVar_Subst(const char *var, const char *str, GNode *ctxt, Boolean undefErr) 3974236769Sobrien{ 3975236769Sobrien Buffer buf; /* Buffer for forming things */ 3976236769Sobrien char *val; /* Value to substitute for a variable */ 3977236769Sobrien int length; /* Length of the variable invocation */ 3978236769Sobrien Boolean trailingBslash; /* variable ends in \ */ 3979236769Sobrien void *freeIt = NULL; /* Set if it should be freed */ 3980236769Sobrien static Boolean errorReported; /* Set true if an error has already 3981236769Sobrien * been reported to prevent a plethora 3982236769Sobrien * of messages when recursing */ 3983236769Sobrien 3984236769Sobrien Buf_Init(&buf, 0); 3985236769Sobrien errorReported = FALSE; 3986236769Sobrien trailingBslash = FALSE; 3987236769Sobrien 3988236769Sobrien while (*str) { 3989236769Sobrien if (*str == '\n' && trailingBslash) 3990236769Sobrien Buf_AddByte(&buf, ' '); 3991236769Sobrien if (var == NULL && (*str == '$') && (str[1] == '$')) { 3992236769Sobrien /* 3993236769Sobrien * A dollar sign may be escaped either with another dollar sign. 3994236769Sobrien * In such a case, we skip over the escape character and store the 3995236769Sobrien * dollar sign into the buffer directly. 3996236769Sobrien */ 3997236769Sobrien str++; 3998236769Sobrien Buf_AddByte(&buf, *str); 3999236769Sobrien str++; 4000236769Sobrien } else if (*str != '$') { 4001236769Sobrien /* 4002236769Sobrien * Skip as many characters as possible -- either to the end of 4003236769Sobrien * the string or to the next dollar sign (variable invocation). 4004236769Sobrien */ 4005236769Sobrien const char *cp; 4006236769Sobrien 4007236769Sobrien for (cp = str++; *str != '$' && *str != '\0'; str++) 4008236769Sobrien continue; 4009236769Sobrien Buf_AddBytes(&buf, str - cp, cp); 4010236769Sobrien } else { 4011236769Sobrien if (var != NULL) { 4012236769Sobrien int expand; 4013236769Sobrien for (;;) { 4014236769Sobrien if (str[1] == '\0') { 4015236769Sobrien /* A trailing $ is kind of a special case */ 4016236769Sobrien Buf_AddByte(&buf, str[0]); 4017236769Sobrien str++; 4018236769Sobrien expand = FALSE; 4019236769Sobrien } else if (str[1] != PROPEN && str[1] != BROPEN) { 4020236769Sobrien if (str[1] != *var || strlen(var) > 1) { 4021236769Sobrien Buf_AddBytes(&buf, 2, str); 4022236769Sobrien str += 2; 4023236769Sobrien expand = FALSE; 4024236769Sobrien } 4025236769Sobrien else 4026236769Sobrien expand = TRUE; 4027236769Sobrien break; 4028236769Sobrien } 4029236769Sobrien else { 4030236769Sobrien const char *p; 4031236769Sobrien 4032236769Sobrien /* 4033236769Sobrien * Scan up to the end of the variable name. 4034236769Sobrien */ 4035236769Sobrien for (p = &str[2]; *p && 4036236769Sobrien *p != ':' && *p != PRCLOSE && *p != BRCLOSE; p++) 4037236769Sobrien if (*p == '$') 4038236769Sobrien break; 4039236769Sobrien /* 4040236769Sobrien * A variable inside the variable. We cannot expand 4041236769Sobrien * the external variable yet, so we try again with 4042236769Sobrien * the nested one 4043236769Sobrien */ 4044236769Sobrien if (*p == '$') { 4045236769Sobrien Buf_AddBytes(&buf, p - str, str); 4046236769Sobrien str = p; 4047236769Sobrien continue; 4048236769Sobrien } 4049236769Sobrien 4050236769Sobrien if (strncmp(var, str + 2, p - str - 2) != 0 || 4051236769Sobrien var[p - str - 2] != '\0') { 4052236769Sobrien /* 4053236769Sobrien * Not the variable we want to expand, scan 4054236769Sobrien * until the next variable 4055236769Sobrien */ 4056236769Sobrien for (;*p != '$' && *p != '\0'; p++) 4057236769Sobrien continue; 4058236769Sobrien Buf_AddBytes(&buf, p - str, str); 4059236769Sobrien str = p; 4060236769Sobrien expand = FALSE; 4061236769Sobrien } 4062236769Sobrien else 4063236769Sobrien expand = TRUE; 4064236769Sobrien break; 4065236769Sobrien } 4066236769Sobrien } 4067236769Sobrien if (!expand) 4068236769Sobrien continue; 4069236769Sobrien } 4070236769Sobrien 4071236769Sobrien val = Var_Parse(str, ctxt, undefErr, &length, &freeIt); 4072236769Sobrien 4073236769Sobrien /* 4074236769Sobrien * When we come down here, val should either point to the 4075236769Sobrien * value of this variable, suitably modified, or be NULL. 4076236769Sobrien * Length should be the total length of the potential 4077236769Sobrien * variable invocation (from $ to end character...) 4078236769Sobrien */ 4079236769Sobrien if (val == var_Error || val == varNoError) { 4080236769Sobrien /* 4081236769Sobrien * If performing old-time variable substitution, skip over 4082236769Sobrien * the variable and continue with the substitution. Otherwise, 4083236769Sobrien * store the dollar sign and advance str so we continue with 4084236769Sobrien * the string... 4085236769Sobrien */ 4086236769Sobrien if (oldVars) { 4087236769Sobrien str += length; 4088236769Sobrien } else if (undefErr) { 4089236769Sobrien /* 4090236769Sobrien * If variable is undefined, complain and skip the 4091236769Sobrien * variable. The complaint will stop us from doing anything 4092236769Sobrien * when the file is parsed. 4093236769Sobrien */ 4094236769Sobrien if (!errorReported) { 4095236769Sobrien Parse_Error(PARSE_FATAL, 4096236769Sobrien "Undefined variable \"%.*s\"",length,str); 4097236769Sobrien } 4098236769Sobrien str += length; 4099236769Sobrien errorReported = TRUE; 4100236769Sobrien } else { 4101236769Sobrien Buf_AddByte(&buf, *str); 4102236769Sobrien str += 1; 4103236769Sobrien } 4104236769Sobrien } else { 4105236769Sobrien /* 4106236769Sobrien * We've now got a variable structure to store in. But first, 4107236769Sobrien * advance the string pointer. 4108236769Sobrien */ 4109236769Sobrien str += length; 4110236769Sobrien 4111236769Sobrien /* 4112236769Sobrien * Copy all the characters from the variable value straight 4113236769Sobrien * into the new string. 4114236769Sobrien */ 4115236769Sobrien length = strlen(val); 4116236769Sobrien Buf_AddBytes(&buf, length, val); 4117236769Sobrien trailingBslash = length > 0 && val[length - 1] == '\\'; 4118236769Sobrien } 4119236769Sobrien if (freeIt) { 4120236769Sobrien free(freeIt); 4121236769Sobrien freeIt = NULL; 4122236769Sobrien } 4123236769Sobrien } 4124236769Sobrien } 4125236769Sobrien 4126236769Sobrien return Buf_DestroyCompact(&buf); 4127236769Sobrien} 4128236769Sobrien 4129236769Sobrien/*- 4130236769Sobrien *----------------------------------------------------------------------- 4131236769Sobrien * Var_GetTail -- 4132236769Sobrien * Return the tail from each of a list of words. Used to set the 4133236769Sobrien * System V local variables. 4134236769Sobrien * 4135236769Sobrien * Input: 4136236769Sobrien * file Filename to modify 4137236769Sobrien * 4138236769Sobrien * Results: 4139236769Sobrien * The resulting string. 4140236769Sobrien * 4141236769Sobrien * Side Effects: 4142236769Sobrien * None. 4143236769Sobrien * 4144236769Sobrien *----------------------------------------------------------------------- 4145236769Sobrien */ 4146236769Sobrien#if 0 4147236769Sobrienchar * 4148236769SobrienVar_GetTail(char *file) 4149236769Sobrien{ 4150236769Sobrien return(VarModify(file, VarTail, NULL)); 4151236769Sobrien} 4152236769Sobrien 4153236769Sobrien/*- 4154236769Sobrien *----------------------------------------------------------------------- 4155236769Sobrien * Var_GetHead -- 4156236769Sobrien * Find the leading components of a (list of) filename(s). 4157236769Sobrien * XXX: VarHead does not replace foo by ., as (sun) System V make 4158236769Sobrien * does. 4159236769Sobrien * 4160236769Sobrien * Input: 4161236769Sobrien * file Filename to manipulate 4162236769Sobrien * 4163236769Sobrien * Results: 4164236769Sobrien * The leading components. 4165236769Sobrien * 4166236769Sobrien * Side Effects: 4167236769Sobrien * None. 4168236769Sobrien * 4169236769Sobrien *----------------------------------------------------------------------- 4170236769Sobrien */ 4171236769Sobrienchar * 4172236769SobrienVar_GetHead(char *file) 4173236769Sobrien{ 4174236769Sobrien return(VarModify(file, VarHead, NULL)); 4175236769Sobrien} 4176236769Sobrien#endif 4177236769Sobrien 4178236769Sobrien/*- 4179236769Sobrien *----------------------------------------------------------------------- 4180236769Sobrien * Var_Init -- 4181236769Sobrien * Initialize the module 4182236769Sobrien * 4183236769Sobrien * Results: 4184236769Sobrien * None 4185236769Sobrien * 4186236769Sobrien * Side Effects: 4187236769Sobrien * The VAR_CMD and VAR_GLOBAL contexts are created 4188236769Sobrien *----------------------------------------------------------------------- 4189236769Sobrien */ 4190236769Sobrienvoid 4191236769SobrienVar_Init(void) 4192236769Sobrien{ 4193255253Ssjg VAR_INTERNAL = Targ_NewGN("Internal"); 4194236769Sobrien VAR_GLOBAL = Targ_NewGN("Global"); 4195236769Sobrien VAR_CMD = Targ_NewGN("Command"); 4196236769Sobrien 4197236769Sobrien} 4198236769Sobrien 4199236769Sobrien 4200236769Sobrienvoid 4201236769SobrienVar_End(void) 4202236769Sobrien{ 4203236769Sobrien} 4204236769Sobrien 4205236769Sobrien 4206236769Sobrien/****************** PRINT DEBUGGING INFO *****************/ 4207236769Sobrienstatic void 4208236769SobrienVarPrintVar(void *vp) 4209236769Sobrien{ 4210236769Sobrien Var *v = (Var *)vp; 4211236769Sobrien fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL)); 4212236769Sobrien} 4213236769Sobrien 4214236769Sobrien/*- 4215236769Sobrien *----------------------------------------------------------------------- 4216236769Sobrien * Var_Dump -- 4217236769Sobrien * print all variables in a context 4218236769Sobrien *----------------------------------------------------------------------- 4219236769Sobrien */ 4220236769Sobrienvoid 4221236769SobrienVar_Dump(GNode *ctxt) 4222236769Sobrien{ 4223236769Sobrien Hash_Search search; 4224236769Sobrien Hash_Entry *h; 4225236769Sobrien 4226236769Sobrien for (h = Hash_EnumFirst(&ctxt->context, &search); 4227236769Sobrien h != NULL; 4228236769Sobrien h = Hash_EnumNext(&search)) { 4229236769Sobrien VarPrintVar(Hash_GetValue(h)); 4230236769Sobrien } 4231236769Sobrien} 4232