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