1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-12-13 10:02:38 +00:00

Change the return value of Var_Subst to return a Buffer instead

of a char *.

Patch:		7.49

Submitted by:	Max Okumoto <okumoto@ucsd.edu>
This commit is contained in:
Hartmut Brandt 2005-02-25 13:16:56 +00:00
parent 90dc539be0
commit 179078e76d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=142457
9 changed files with 181 additions and 87 deletions

View File

@ -98,6 +98,7 @@ __FBSDID("$FreeBSD$");
#include <utime.h>
#include "arch.h"
#include "buf.h"
#include "config.h"
#include "dir.h"
#include "globals.h"
@ -188,9 +189,12 @@ Arch_ParseArchive(char **linePtr, Lst *nodeLst, GNode *ctxt)
*cp++ = '\0';
if (subLibName) {
libName = Var_Subst(NULL, libName, ctxt, TRUE);
}
Buffer *buf;
buf = Var_Subst(NULL, libName, ctxt, TRUE);
libName = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
}
for (;;) {
/*
@ -278,8 +282,7 @@ Arch_ParseArchive(char **linePtr, Lst *nodeLst, GNode *ctxt)
char *sacrifice;
char *oldMemName = memName;
size_t sz;
memName = Var_Subst(NULL, memName, ctxt, TRUE);
Buffer *buf1;
/*
* Now form an archive spec and recurse to deal with
@ -287,12 +290,16 @@ Arch_ParseArchive(char **linePtr, Lst *nodeLst, GNode *ctxt)
* The results are just placed at the end of the
* nodeLst we're returning.
*/
buf1 = Var_Subst(NULL, memName, ctxt, TRUE);
memName = Buf_GetAll(buf1, NULL);
sz = strlen(memName) + strlen(libName) + 3;
buf = sacrifice = emalloc(sz);
buf = emalloc(sz);
snprintf(buf, sz, "%s(%s)", libName, memName);
sacrifice = buf;
if (strchr(memName, '$') &&
strcmp(memName, oldMemName) == 0) {
/*
@ -305,6 +312,7 @@ Arch_ParseArchive(char **linePtr, Lst *nodeLst, GNode *ctxt)
if (gn == NULL) {
free(buf);
Buf_Destroy(buf1, FALSE);
return (FAILURE);
}
gn->type |= OP_ARCHV;
@ -316,12 +324,14 @@ Arch_ParseArchive(char **linePtr, Lst *nodeLst, GNode *ctxt)
* return FAILURE ourselves.
*/
free(buf);
Buf_Destroy(buf1, FALSE);
return (FAILURE);
}
/*
* Free buffer and continue with our work.
*/
/* Free buffer and continue with our work. */
free(buf);
Buf_Destroy(buf1, FALSE);
} else if (Dir_HasWildcards(memName)) {
Lst members = Lst_Initializer(members);
char *member;

View File

@ -64,6 +64,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
#include <unistd.h>
#include "buf.h"
#include "compat.h"
#include "config.h"
#include "dir.h"
@ -242,6 +243,7 @@ Compat_RunCommand(char *cmd, GNode *gn)
LstNode *cmdNode; /* Node where current command is located */
char **av; /* Argument vector for thing to exec */
char *cmd_save; /* saved cmd */
Buffer *buf;
/*
* Avoid clobbered variable warnings by forcing the compiler
@ -256,7 +258,10 @@ Compat_RunCommand(char *cmd, GNode *gn)
doit = FALSE;
cmdNode = Lst_Member(&gn->commands, cmd);
cmdStart = Var_Subst(NULL, cmd, gn, FALSE);
buf = Var_Subst(NULL, cmd, gn, FALSE);
cmdStart = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
/*
* brk_string will return an argv with a NULL in av[0], thus causing

View File

@ -121,6 +121,7 @@ For_Eval(char *line)
* maybe start of a for loop
*/
Buffer *buf;
Buffer *buf1;
size_t varlen;
for (ptr++; *ptr && isspace((unsigned char)*ptr); ptr++)
@ -179,7 +180,10 @@ For_Eval(char *line)
*/
Lst_Init(&forLst);
buf = Buf_Init(0);
sub = Var_Subst(NULL, ptr, VAR_CMD, FALSE);
buf1 = Var_Subst(NULL, ptr, VAR_CMD, FALSE);
sub = Buf_GetAll(buf1, NULL);
Buf_Destroy(buf1, FALSE);
for (ptr = sub; *ptr && isspace((unsigned char)*ptr); ptr++)
;
@ -268,6 +272,8 @@ For_Run(int lineno)
Buffer *buf; /* the contents of the for loop */
const char *val; /* current value of loop variable */
LstNode *ln;
Buffer *buf1;
char *str;
if (forVar == NULL || forBuf == NULL)
return;
@ -286,10 +292,12 @@ For_Run(int lineno)
Var_Set(var, val, VAR_GLOBAL);
DEBUGF(FOR, ("--- %s = %s\n", var, val));
Parse_FromString(Var_Subst(var,
(char *)Buf_GetAll(buf, NULL),
VAR_GLOBAL, FALSE), lineno);
buf1 = Var_Subst(var, (char *)Buf_GetAll(buf, NULL),
VAR_GLOBAL, FALSE);
str = Buf_GetAll(buf1, NULL);
Buf_Destroy(buf1, FALSE);
Parse_FromString(str, lineno);
Var_Delete(var, VAR_GLOBAL);
}

View File

@ -119,6 +119,7 @@ __FBSDID("$FreeBSD$");
#include <utime.h>
#include "arch.h"
#include "buf.h"
#include "compat.h"
#include "dir.h"
#include "globals.h"
@ -485,6 +486,7 @@ JobPrintCommand(void *cmdp, void *jobp)
LstNode *cmdNode; /* Node for replacing the command */
char *cmd = cmdp;
Job *job = jobp;
Buffer *buf;
noSpecials = (noExecute && !(job->node->type & OP_MAKE));
@ -509,7 +511,12 @@ JobPrintCommand(void *cmdp, void *jobp)
* the variables in the command.
*/
cmdNode = Lst_Member(&job->node->commands, cmd);
cmdStart = cmd = Var_Subst(NULL, cmd, job->node, FALSE);
buf = Var_Subst(NULL, cmd, job->node, FALSE);
cmd = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
cmdStart = cmd;
Lst_Replace(cmdNode, cmdStart);
cmdTemplate = "%s\n";
@ -644,9 +651,14 @@ JobPrintCommand(void *cmdp, void *jobp)
static int
JobSaveCommand(void *cmd, void *gn)
{
Buffer *buf;
char *str;
cmd = Var_Subst(NULL, cmd, gn, FALSE);
Lst_AtEnd(&postCommands->commands, cmd);
buf = Var_Subst(NULL, cmd, gn, FALSE);
str = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
Lst_AtEnd(&postCommands->commands, str);
return (0);
}

View File

@ -811,16 +811,17 @@ main(int argc, char **argv)
* in an array
*/
static char VPATH[] = "${VPATH}";
char *vpath;
char *start;
char *ptr;
char savec;
Buffer *buf;
char *vpath;
char *ptr;
char savec;
vpath = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
start = vpath;
buf = Var_Subst(NULL, VPATH, VAR_CMD, FALSE);
vpath = Buf_GetAll(buf, NULL);
do {
/* skip to end of directory */
for (ptr = start; *ptr != ':' && *ptr != '\0'; ptr++)
for (ptr = vpath; *ptr != ':' && *ptr != '\0'; ptr++)
;
/* Save terminator character so know when to stop */
@ -828,11 +829,12 @@ main(int argc, char **argv)
*ptr = '\0';
/* Add directory to search path */
Dir_AddDir(&dirSearchPath, start);
Dir_AddDir(&dirSearchPath, vpath);
start = ptr + 1;
vpath = ptr + 1;
} while (savec != '\0');
free(vpath);
Buf_Destroy(buf, TRUE);
}
/*
@ -894,25 +896,30 @@ main(int argc, char **argv)
* Print the values of any variables requested by
* the user.
*/
LstNode *ln;
const char *name;
char *v;
char *value;
LstNode *n;
const char *name;
char *v;
char *value;
Buffer *buf;
for (ln = Lst_First(&variables); ln != NULL;
ln = Lst_Succ(ln)) {
name = Lst_Datum(ln);
LST_FOREACH(n, &variables) {
name = Lst_Datum(n);
if (expandVars) {
v = emalloc(strlen(name) + 1 + 3);
sprintf(v, "${%s}", name);
value = Var_Subst(NULL, v, VAR_GLOBAL, FALSE);
buf = Var_Subst(NULL, v, VAR_GLOBAL, FALSE);
value = Buf_GetAll(buf, FALSE);
printf("%s\n", value);
Buf_Destroy(buf, TRUE);
free(v);
} else {
value = Var_Value(name, VAR_GLOBAL, &v);
printf("%s\n", value != NULL ? value : "");
if (v != NULL)
free(v);
}
printf("%s\n", value != NULL ? value : "");
if (v != NULL)
free(v);
}
}

View File

@ -1320,6 +1320,8 @@ Parse_DoVar(char *line, GNode *ctxt)
} type; /* Type of assignment */
char *opc; /* ptr to operator character to
* null-terminate the variable name */
Buffer *buf;
/*
* Avoid clobbered variable warnings by forcing the compiler
* to ``unregister'' variables
@ -1425,7 +1427,10 @@ Parse_DoVar(char *line, GNode *ctxt)
if (!Var_Exists(line, ctxt))
Var_Set(line, "", ctxt);
cp = Var_Subst(NULL, cp, ctxt, FALSE);
buf = Var_Subst(NULL, cp, ctxt, FALSE);
cp = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
oldVars = oldOldVars;
Var_Set(line, cp, ctxt);
@ -1442,7 +1447,9 @@ Parse_DoVar(char *line, GNode *ctxt)
* expansion on the whole thing. The resulting string will need
* freeing when we're done, so set freeCmd to TRUE.
*/
cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
buf = Var_Subst(NULL, cp, VAR_CMD, TRUE);
cp = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
freeCmd = TRUE;
}
@ -1548,6 +1555,7 @@ Parse_AddIncludeDir(char *dir)
static void
ParseDoError(char *errmsg)
{
Buffer *buf;
if (!isspace((unsigned char)*errmsg)) {
Parse_Error(PARSE_WARNING, "invalid syntax: .error%s", errmsg);
@ -1557,9 +1565,12 @@ ParseDoError(char *errmsg)
while (isspace((unsigned char)*errmsg))
errmsg++;
errmsg = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
buf = Var_Subst(NULL, errmsg, VAR_GLOBAL, FALSE);
errmsg = Buf_GetAll(buf, NULL);
Parse_Error(PARSE_FATAL, "%s", errmsg);
Buf_Destroy(buf, TRUE);
/* Terminate immediately. */
exit(1);
}
@ -1577,6 +1588,7 @@ ParseDoError(char *errmsg)
static void
ParseDoWarning(char *warnmsg)
{
Buffer *buf;
if (!isspace((unsigned char)*warnmsg)) {
Parse_Error(PARSE_WARNING, "invalid syntax: .warning%s",
@ -1587,9 +1599,11 @@ ParseDoWarning(char *warnmsg)
while (isspace((unsigned char)*warnmsg))
warnmsg++;
warnmsg = Var_Subst(NULL, warnmsg, VAR_GLOBAL, FALSE);
buf = Var_Subst(NULL, warnmsg, VAR_GLOBAL, FALSE);
warnmsg = Buf_GetAll(buf, NULL);
Parse_Error(PARSE_WARNING, "%s", warnmsg);
Buf_Destroy(buf, TRUE);
}
/*-
@ -1618,6 +1632,7 @@ ParseDoInclude(char *file)
char endc; /* the character which ends the file spec */
char *cp; /* current position in file spec */
Boolean isSystem; /* TRUE if makefile is a system makefile */
Buffer *buf;
/*
* Skip to delimiter character so we know where to look
@ -1664,7 +1679,9 @@ ParseDoInclude(char *file)
* Substitute for any variables in the file name before trying to
* find the thing.
*/
file = Var_Subst(NULL, file, VAR_CMD, FALSE);
buf = Var_Subst(NULL, file, VAR_CMD, FALSE);
file = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
/*
* Now we know the file's name and its search path, we attempt to
@ -1731,6 +1748,7 @@ ParseDoInclude(char *file)
if (fullname == NULL) {
*cp = endc;
Parse_Error(PARSE_FATAL, "Could not find %s", file);
/* XXXHB free(file) */
return;
}
@ -1825,6 +1843,7 @@ ParseTraditionalInclude(char *file)
char *fullname; /* full pathname of file */
IFile *oldFile; /* state associated with current file */
char *cp; /* current position in file spec */
Buffer *buf;
/*
* Skip over whitespace
@ -1852,7 +1871,9 @@ ParseTraditionalInclude(char *file)
* Substitute for any variables in the file name before trying to
* find the thing.
*/
file = Var_Subst(NULL, file, VAR_CMD, FALSE);
buf = Var_Subst(NULL, file, VAR_CMD, FALSE);
file = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
/*
* Now we know the file's name, we attempt to find the durn thing.
@ -1874,9 +1895,12 @@ ParseTraditionalInclude(char *file)
if (fullname == NULL) {
Parse_Error(PARSE_FATAL, "Could not find %s", file);
/* XXXHB free(file) */
return;
}
/* XXXHB free(file) */
/*
* Once we find the absolute path to the file, we get to save all the
* state from the current file before we can start reading this
@ -2373,6 +2397,7 @@ Parse_File(char *name, FILE *stream)
{
char *cp, /* pointer into the line */
*line; /* the line we're working on */
Buffer *buf;
inLine = FALSE;
curFile.fname = name;
@ -2414,7 +2439,10 @@ Parse_File(char *name, FILE *stream)
*cp2 = '\0';
cp = Var_Subst(NULL, cp, VAR_CMD, FALSE);
buf = Var_Subst(NULL, cp, VAR_CMD, FALSE);
cp = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
Var_Delete(cp, VAR_GLOBAL);
goto nextLine;
}
@ -2483,7 +2511,10 @@ Parse_File(char *name, FILE *stream)
ParseFinishLine();
cp = Var_Subst(NULL, line, VAR_CMD, TRUE);
buf = Var_Subst(NULL, line, VAR_CMD, TRUE);
cp = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
free(line);
line = cp;

View File

@ -94,6 +94,7 @@ __FBSDID("$FreeBSD$");
#include <stdlib.h>
#include "arch.h"
#include "buf.h"
#include "config.h"
#include "dir.h"
#include "globals.h"
@ -1296,6 +1297,7 @@ SuffExpandChildren(void *cgnp, void *pgnp)
LstNode *prevLN; /* Node after which new source should be put */
LstNode *ln; /* List element for old source */
char *cp; /* Expanded value */
Buffer *buf;
/*
* New nodes effectively take the place of the child, so place them
@ -1311,7 +1313,9 @@ SuffExpandChildren(void *cgnp, void *pgnp)
*/
if (strchr(cgn->name, '$') != NULL) {
DEBUGF(SUFF, ("Expanding \"%s\"...", cgn->name));
cp = Var_Subst(NULL, cgn->name, pgn, TRUE);
buf = Var_Subst(NULL, cgn->name, pgn, TRUE);
cp = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
if (cp != NULL) {
Lst members = Lst_Initializer(members);

View File

@ -220,8 +220,9 @@ VarCmp(const void *v, const void *name)
static char *
VarPossiblyExpand(const char *name, GNode *ctxt)
{
char *tmp;
char *result;
Buffer *buf;
char *str;
char *tmp;
/*
* XXX make a temporary copy of the name because Var_Subst insists
@ -229,11 +230,15 @@ VarPossiblyExpand(const char *name, GNode *ctxt)
*/
tmp = estrdup(name);
if (strchr(name, '$') != NULL) {
result = Var_Subst(NULL, tmp, ctxt, 0);
buf = Var_Subst(NULL, tmp, ctxt, 0);
str = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
free(tmp);
return (result);
} else
return (str);
} else {
return (tmp);
}
}
/*-
@ -1129,29 +1134,40 @@ Var_Parse(char *str, GNode *ctxt, Boolean err, size_t *lengthPtr,
}
}
if (v->flags & VAR_IN_USE) {
Fatal("Variable %s is recursive.", v->name);
/*NOTREACHED*/
} else {
v->flags |= VAR_IN_USE;
}
/*
* Make sure this variable is fully expanded.
* XXX This section really should be its own function.
*/
{
if (v->flags & VAR_IN_USE) {
Fatal("Variable %s is recursive.", v->name);
/* NOTREACHED */
} else {
v->flags |= VAR_IN_USE;
}
/*
* Before doing any modification, we have to make sure the value
* has been fully expanded. If it looks like recursion might be
* necessary (there's a dollar sign somewhere in the variable's value)
* we just call Var_Subst to do any other substitutions that are
* necessary. Note that the value returned by Var_Subst will have
* been dynamically-allocated, so it will need freeing when we
* return.
*/
str = (char *)Buf_GetAll(v->val, (size_t *)NULL);
if (strchr(str, '$') != (char *)NULL) {
str = Var_Subst(NULL, str, ctxt, err);
*freePtr = TRUE;
}
/*
* Before doing any modification, we have to make sure the
* value has been fully expanded. If it looks like recursion
* might be necessary (there's a dollar sign somewhere in the
* variable's value) we just call Var_Subst to do any other
* substitutions that are necessary. Note that the value
* returned by Var_Subst will have been
* dynamically-allocated, so it will need freeing when we
* return.
*/
str = (char *)Buf_GetAll(v->val, (size_t *)NULL);
if (strchr(str, '$') != NULL) {
Buffer *buf;
v->flags &= ~VAR_IN_USE;
buf = Var_Subst(NULL, str, ctxt, err);
str = Buf_GetAll(buf, NULL);
Buf_Destroy(buf, FALSE);
*freePtr = TRUE;
}
v->flags &= ~VAR_IN_USE;
}
/*
* Now we need to apply any modifiers the user wants applied.
@ -1753,22 +1769,20 @@ Var_Parse(char *str, GNode *ctxt, Boolean err, size_t *lengthPtr,
* None. The old string must be freed by the caller
*-----------------------------------------------------------------------
*/
char *
Buffer *
Var_Subst(const char *var, char *str, GNode *ctxt, Boolean undefErr)
{
Buffer *buf; /* Buffer for forming things */
char *val; /* Value to substitute for a variable */
size_t length; /* Length of the variable invocation */
Boolean doFree; /* Set true if val should be freed */
char *result;
Boolean errorReported;
Buffer *buf; /* Buffer for forming things */
static Boolean errorReported; /* Set true if an error has already
* been reported to prevent a plethora
* of messages when recursing */
buf = Buf_Init(0);
/*
* Set TRUE if an error has already been reported to prevent a
* plethora of messages when recursing.
* XXXHB this comment sounds wrong.
*/
errorReported = FALSE;
buf = Buf_Init(0);
while (*str) {
if (var == NULL && (str[0] == '$') && (str[1] == '$')) {
/*
@ -1780,6 +1794,10 @@ Var_Subst(const char *var, char *str, GNode *ctxt, Boolean undefErr)
str += 2;
} else if (str[0] == '$') {
char *val; /* Value to substitute for a variable */
size_t length; /* Length of the variable invocation */
Boolean doFree; /* Set true if val should be freed */
/*
* Variable invocation.
*/
@ -1904,13 +1922,12 @@ Var_Subst(const char *var, char *str, GNode *ctxt, Boolean undefErr)
do {
str++;
} while (str[0] != '$' && str[0] != '\0');
Buf_AppendRange(buf, cp, str);
}
}
result = (char *)Buf_GetAll(buf, (size_t *)NULL);
Buf_Destroy(buf, FALSE);
return (result);
return (buf);
}
/*-

View File

@ -118,7 +118,7 @@ Boolean Var_Exists(const char *, struct GNode *);
char *Var_Value(const char *, struct GNode *, char **);
char *Var_Quote(const char *);
char *Var_Parse(char *, struct GNode *, Boolean, size_t *, Boolean *);
char *Var_Subst(const char *, char *, struct GNode *, Boolean);
struct Buffer *Var_Subst(const char *, char *, struct GNode *, Boolean);
char *Var_GetTail(char *);
char *Var_GetHead(char *);
void Var_Init(void);