make(1): fix indentation and alignment

This commit is contained in:
rillig 2020-07-03 17:00:47 +00:00
parent ef06fc5cad
commit aa9c7243ec

View File

@ -1,4 +1,4 @@
/* $NetBSD: var.c,v 1.239 2020/07/03 15:42:43 rillig Exp $ */ /* $NetBSD: var.c,v 1.240 2020/07/03 17:00:47 rillig Exp $ */
/* /*
* Copyright (c) 1988, 1989, 1990, 1993 * Copyright (c) 1988, 1989, 1990, 1993
@ -69,14 +69,14 @@
*/ */
#ifndef MAKE_NATIVE #ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: var.c,v 1.239 2020/07/03 15:42:43 rillig Exp $"; static char rcsid[] = "$NetBSD: var.c,v 1.240 2020/07/03 17:00:47 rillig Exp $";
#else #else
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
#if 0 #if 0
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
#else #else
__RCSID("$NetBSD: var.c,v 1.239 2020/07/03 15:42:43 rillig Exp $"); __RCSID("$NetBSD: var.c,v 1.240 2020/07/03 17:00:47 rillig Exp $");
#endif #endif
#endif /* not lint */ #endif /* not lint */
#endif #endif
@ -187,29 +187,29 @@ static Boolean save_dollars = TRUE;
* The four contexts are searched in the reverse order from which they are * The four contexts are searched in the reverse order from which they are
* listed. * listed.
*/ */
GNode *VAR_INTERNAL; /* variables from make itself */ GNode *VAR_INTERNAL; /* variables from make itself */
GNode *VAR_GLOBAL; /* variables from the makefile */ GNode *VAR_GLOBAL; /* variables from the makefile */
GNode *VAR_CMD; /* variables defined on the command-line */ GNode *VAR_CMD; /* variables defined on the command-line */
#define FIND_CMD 0x1 /* look in VAR_CMD when searching */ #define FIND_CMD 0x1 /* look in VAR_CMD when searching */
#define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */ #define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */
#define FIND_ENV 0x4 /* look in the environment also */ #define FIND_ENV 0x4 /* look in the environment also */
typedef enum { typedef enum {
VAR_IN_USE = 0x01, /* Variable's value is currently being used. VAR_IN_USE = 0x01, /* Variable's value is currently being used.
* Used to avoid endless recursion */ * Used to avoid endless recursion */
VAR_FROM_ENV = 0x02, /* Variable comes from the environment */ VAR_FROM_ENV = 0x02, /* Variable comes from the environment */
VAR_JUNK = 0x04, /* Variable is a junk variable that VAR_JUNK = 0x04, /* Variable is a junk variable that
* should be destroyed when done with * should be destroyed when done with
* it. Used by Var_Parse for undefined, * it. Used by Var_Parse for undefined,
* modified variables */ * modified variables */
VAR_KEEP = 0x08, /* Variable is VAR_JUNK, but we found VAR_KEEP = 0x08, /* Variable is VAR_JUNK, but we found
* a use for it in some modifier and * a use for it in some modifier and
* the value is therefore valid */ * the value is therefore valid */
VAR_EXPORTED = 0x10, /* Variable is exported */ VAR_EXPORTED = 0x10, /* Variable is exported */
VAR_REEXPORT = 0x20, /* Indicate if var needs re-export. VAR_REEXPORT = 0x20, /* Indicate if var needs re-export.
* This would be true if it contains $'s */ * This would be true if it contains $'s */
VAR_FROM_CMD = 0x40 /* Variable came from command line */ VAR_FROM_CMD = 0x40 /* Variable came from command line */
} Var_Flags; } Var_Flags;
typedef struct Var { typedef struct Var {
@ -608,7 +608,7 @@ Var_Export1(const char *name, int flags)
int parent = (flags & VAR_EXPORT_PARENT); int parent = (flags & VAR_EXPORT_PARENT);
if (*name == '.') if (*name == '.')
return 0; /* skip internals */ return 0; /* skip internals */
if (!name[1]) { if (!name[1]) {
/* /*
* A single char. * A single char.
@ -680,8 +680,8 @@ void
Var_ExportVars(void) Var_ExportVars(void)
{ {
char tmp[BUFSIZ]; char tmp[BUFSIZ];
Hash_Entry *var; Hash_Entry *var;
Hash_Search state; Hash_Search state;
Var *v; Var *v;
char *val; char *val;
int n; int n;
@ -813,7 +813,7 @@ Var_UnExport(char *str)
int n; int n;
if (!str || !str[0]) { if (!str || !str[0]) {
return; /* assert? */ return; /* assert? */
} }
vlist = NULL; vlist = NULL;
@ -1770,12 +1770,12 @@ static char *
VarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate, VarSelectWords(GNode *ctx MAKE_ATTR_UNUSED, Var_Parse_State *vpstate,
const char *str, VarSelectWords_t *seldata) const char *str, VarSelectWords_t *seldata)
{ {
Buffer buf; /* Buffer for the new string */ Buffer buf; /* Buffer for the new string */
Boolean addSpace; /* TRUE if need to add a space to the Boolean addSpace; /* TRUE if need to add a space to the
* buffer before adding the trimmed * buffer before adding the trimmed
* word */ * word */
char **av; /* word list */ char **av; /* word list */
char *as; /* word list memory */ char *as; /* word list memory */
int ac, i; int ac, i;
int start, end, step; int start, end, step;
@ -1888,12 +1888,11 @@ VarModify(GNode *ctx, Var_Parse_State *vpstate,
Boolean, Buffer *, void *), Boolean, Buffer *, void *),
void *datum) void *datum)
{ {
Buffer buf; /* Buffer for the new string */ Buffer buf; /* Buffer for the new string */
Boolean addSpace; /* TRUE if need to add a space to the Boolean addSpace; /* TRUE if need to add a space to the
* buffer before adding the trimmed * buffer before adding the trimmed word */
* word */ char **av; /* word list */
char **av; /* word list */ char *as; /* word list memory */
char *as; /* word list memory */
int ac, i; int ac, i;
Buf_Init(&buf, 0); Buf_Init(&buf, 0);
@ -1955,44 +1954,40 @@ VarWordCompareReverse(const void *a, const void *b)
static char * static char *
VarOrder(const char *str, const char otype) VarOrder(const char *str, const char otype)
{ {
Buffer buf; /* Buffer for the new string */ Buffer buf; /* Buffer for the new string */
char **av; /* word list [first word does not count] */ char **av; /* word list [first word does not count] */
char *as; /* word list memory */ char *as; /* word list memory */
int ac, i; int ac, i;
Buf_Init(&buf, 0); Buf_Init(&buf, 0);
av = brk_string(str, &ac, FALSE, &as); av = brk_string(str, &ac, FALSE, &as);
if (ac > 0) if (ac > 0) {
switch (otype) { switch (otype) {
case 'r': /* reverse sort alphabetically */ case 'r': /* reverse sort alphabetically */
qsort(av, ac, sizeof(char *), VarWordCompareReverse); qsort(av, ac, sizeof(char *), VarWordCompareReverse);
break; break;
case 's': /* sort alphabetically */ case 's': /* sort alphabetically */
qsort(av, ac, sizeof(char *), VarWordCompare); qsort(av, ac, sizeof(char *), VarWordCompare);
break; break;
case 'x': /* randomize */ case 'x': /* randomize */
{ {
int rndidx; /*
char *t; * We will use [ac..2] range for mod factors. This will produce
* random numbers in [(ac-1)..0] interval, and minimal
/* * reasonable value for mod factor is 2 (the mod 1 will produce
* We will use [ac..2] range for mod factors. This will produce * 0 with probability 1).
* random numbers in [(ac-1)..0] interval, and minimal */
* reasonable value for mod factor is 2 (the mod 1 will produce for (i = ac - 1; i > 0; i--) {
* 0 with probability 1). int rndidx = random() % (i + 1);
*/ char *t = av[i];
for (i = ac-1; i > 0; i--) {
rndidx = random() % (i + 1);
if (i != rndidx) {
t = av[i];
av[i] = av[rndidx]; av[i] = av[rndidx];
av[rndidx] = t; av[rndidx] = t;
} }
} }
} }
} /* end of switch */ }
for (i = 0; i < ac; i++) { for (i = 0; i < ac; i++) {
Buf_AddBytes(&buf, strlen(av[i]), av[i]); Buf_AddBytes(&buf, strlen(av[i]), av[i]);
@ -2026,9 +2021,9 @@ VarOrder(const char *str, const char otype)
static char * static char *
VarUniq(const char *str) VarUniq(const char *str)
{ {
Buffer buf; /* Buffer for new string */ Buffer buf; /* Buffer for new string */
char **av; /* List of words to affect */ char **av; /* List of words to affect */
char *as; /* Word list memory */ char *as; /* Word list memory */
int ac, i, j; int ac, i, j;
Buf_Init(&buf, 0); Buf_Init(&buf, 0);
@ -2070,10 +2065,10 @@ VarUniq(const char *str)
static char * static char *
VarRange(const char *str, int ac) VarRange(const char *str, int ac)
{ {
Buffer buf; /* Buffer for new string */ Buffer buf; /* Buffer for new string */
char tmp[32]; /* each element */ char tmp[32]; /* each element */
char **av; /* List of words to affect */ char **av; /* List of words to affect */
char *as; /* Word list memory */ char *as; /* Word list memory */
int i, n; int i, n;
Buf_Init(&buf, 0); Buf_Init(&buf, 0);
@ -2383,7 +2378,6 @@ typedef struct {
/* result */ /* result */
char *newStr; /* New value to return */ char *newStr; /* New value to return */
} ApplyModifiersState; } ApplyModifiersState;
/* we now have some modifiers with long names */ /* we now have some modifiers with long names */
@ -2429,7 +2423,7 @@ ApplyModifier_At(ApplyModifiersState *st) {
static void static void
ApplyModifier_Defined(ApplyModifiersState *st) ApplyModifier_Defined(ApplyModifiersState *st)
{ {
Buffer buf; /* Buffer for patterns */ Buffer buf; /* Buffer for patterns */
int nflags; int nflags;
if (st->flags & VARF_WANTRES) { if (st->flags & VARF_WANTRES) {
@ -2629,9 +2623,9 @@ static void
ApplyModifier_Match(ApplyModifiersState *st) ApplyModifier_Match(ApplyModifiersState *st)
{ {
char *pattern; char *pattern;
const char *endpat; /* points just after end of pattern */ const char *endpat; /* points just after end of pattern */
char *cp2; char *cp2;
Boolean copy; /* pattern should be, or has been, copied */ Boolean copy; /* pattern should be, or has been, copied */
Boolean needSubst; Boolean needSubst;
int nest; int nest;
@ -2857,10 +2851,7 @@ ApplyModifier_To(ApplyModifiersState *st)
st->cp = st->tstr + 1; /* make sure it is set */ st->cp = st->tstr + 1; /* make sure it is set */
if (st->tstr[1] != st->endc && st->tstr[1] != ':') { if (st->tstr[1] != st->endc && st->tstr[1] != ':') {
if (st->tstr[1] == 's') { if (st->tstr[1] == 's') {
/* /* Use the char (if any) at st->tstr[2] as the word separator. */
* Use the char (if any) at st->tstr[2]
* as the word separator.
*/
VarPattern pattern; VarPattern pattern;
if (st->tstr[2] != st->endc && if (st->tstr[2] != st->endc &&
@ -2875,7 +2866,7 @@ ApplyModifier_To(ApplyModifiersState *st)
st->cp = st->tstr + 2; st->cp = st->tstr + 2;
} else if (st->tstr[2] == '\\') { } else if (st->tstr[2] == '\\') {
const char *xp = &st->tstr[3]; const char *xp = &st->tstr[3];
int base = 8; /* assume octal */ int base = 8; /* assume octal */
switch (st->tstr[3]) { switch (st->tstr[3]) {
case 'n': case 'n':
@ -3512,7 +3503,7 @@ ApplyModifiers(char *nstr, const char *tstr,
|| (st.nstr == varNoError && (st.flags & VARF_UNDEFERR) == 0) || (st.nstr == varNoError && (st.flags & VARF_UNDEFERR) == 0)
|| strlen(rval) != (size_t) used) { || strlen(rval) != (size_t) used) {
free(freeIt); free(freeIt);
goto out; /* error already reported */ goto out; /* error already reported */
} }
} }
free(freeIt); free(freeIt);
@ -3731,16 +3722,16 @@ ApplyModifiers(char *nstr, const char *tstr,
} }
st.tstr = st.cp; st.tstr = st.cp;
} }
out: out:
*st.lengthPtr = st.tstr - st.start; *st.lengthPtr = st.tstr - st.start;
return st.nstr; return st.nstr;
bad_modifier: bad_modifier:
/* "{(" */ /* "{(" */
Error("Bad modifier `:%.*s' for %s", (int)strcspn(st.tstr, ":)}"), st.tstr, Error("Bad modifier `:%.*s' for %s", (int)strcspn(st.tstr, ":)}"), st.tstr,
st.v->name); st.v->name);
cleanup: cleanup:
*st.lengthPtr = st.cp - st.start; *st.lengthPtr = st.cp - st.start;
if (st.delim != '\0') if (st.delim != '\0')
Error("Unclosed substitution for %s (%c missing)", Error("Unclosed substitution for %s (%c missing)",
@ -3784,22 +3775,22 @@ char *
Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags, Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
int *lengthPtr, void **freePtr) int *lengthPtr, void **freePtr)
{ {
const char *tstr; /* Pointer into str */ const char *tstr; /* Pointer into str */
Var *v; /* Variable in invocation */ Var *v; /* Variable in invocation */
Boolean haveModifier;/* TRUE if have modifiers for the variable */ Boolean haveModifier; /* TRUE if have modifiers for the variable */
char endc; /* Ending character when variable in parens char endc; /* Ending character when variable in parens
* or braces */ * or braces */
char startc; /* Starting character when variable in parens char startc; /* Starting character when variable in parens
* or braces */ * or braces */
int vlen; /* Length of variable name */ int vlen; /* Length of variable name */
const char *start; /* Points to original start of str */ const char *start; /* Points to original start of str */
char *nstr; /* New string, used during expansion */ char *nstr; /* New string, used during expansion */
Boolean dynamic; /* TRUE if the variable is local and we're Boolean dynamic; /* TRUE if the variable is local and we're
* expanding it in a non-local context. This * expanding it in a non-local context. This
* is done to support dynamic sources. The * is done to support dynamic sources. The
* result is just the invocation, unaltered */ * result is just the invocation, unaltered */
const char *extramodifiers; /* extra modifiers to apply first */ const char *extramodifiers; /* extra modifiers to apply first */
char name[2]; char name[2];
*freePtr = NULL; *freePtr = NULL;
extramodifiers = NULL; extramodifiers = NULL;
@ -3857,7 +3848,7 @@ Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
endc = str[1]; endc = str[1];
} }
} else { } else {
Buffer buf; /* Holds the variable name */ Buffer buf; /* Holds the variable name */
int depth = 1; int depth = 1;
endc = startc == PROPEN ? PRCLOSE : BRCLOSE; endc = startc == PROPEN ? PRCLOSE : BRCLOSE;
@ -3868,29 +3859,22 @@ Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
*/ */
for (tstr = str + 2; *tstr != '\0'; tstr++) for (tstr = str + 2; *tstr != '\0'; tstr++)
{ {
/* /* Track depth so we can spot parse errors. */
* Track depth so we can spot parse errors. if (*tstr == startc)
*/
if (*tstr == startc) {
depth++; depth++;
}
if (*tstr == endc) { if (*tstr == endc) {
if (--depth == 0) if (--depth == 0)
break; break;
} }
if (depth == 1 && *tstr == ':') { if (depth == 1 && *tstr == ':')
break; break;
} /* A variable inside a variable, expand. */
/*
* A variable inside a variable, expand.
*/
if (*tstr == '$') { if (*tstr == '$') {
int rlen; int rlen;
void *freeIt; void *freeIt;
char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt); char *rval = Var_Parse(tstr, ctxt, flags, &rlen, &freeIt);
if (rval != NULL) { if (rval != NULL)
Buf_AddBytes(&buf, strlen(rval), rval); Buf_AddBytes(&buf, strlen(rval), rval);
}
free(freeIt); free(freeIt);
tstr += rlen - 1; tstr += rlen - 1;
} }
@ -3941,10 +3925,10 @@ Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
if (v != NULL) { if (v != NULL) {
if (str[1] == 'D') { if (str[1] == 'D') {
extramodifiers = "H:"; extramodifiers = "H:";
} }
else { /* F */ else { /* F */
extramodifiers = "T:"; extramodifiers = "T:";
} }
} }
} }
@ -3971,13 +3955,11 @@ Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
dynamic = TRUE; dynamic = TRUE;
break; break;
} }
} else if ((vlen > 2) && (*str == '.') && } else if (vlen > 2 && *str == '.' &&
isupper((unsigned char) str[1]) && isupper((unsigned char) str[1]) &&
((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL))) (ctxt == VAR_CMD || ctxt == VAR_GLOBAL))
{ {
int len; int len = vlen - 1;
len = vlen - 1;
if ((strncmp(str, ".TARGET", len) == 0) || if ((strncmp(str, ".TARGET", len) == 0) ||
(strncmp(str, ".ARCHIVE", len) == 0) || (strncmp(str, ".ARCHIVE", len) == 0) ||
(strncmp(str, ".PREFIX", len) == 0) || (strncmp(str, ".PREFIX", len) == 0) ||
@ -4040,33 +4022,29 @@ Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
v->flags &= ~VAR_IN_USE; v->flags &= ~VAR_IN_USE;
if ((nstr != NULL) && (haveModifier || extramodifiers != NULL)) { if (nstr != NULL && (haveModifier || extramodifiers != NULL)) {
void *extraFree; void *extraFree;
int used; int used;
extraFree = NULL; extraFree = NULL;
if (extramodifiers != NULL) { if (extramodifiers != NULL) {
nstr = ApplyModifiers(nstr, extramodifiers, '(', ')', nstr = ApplyModifiers(nstr, extramodifiers, '(', ')',
v, ctxt, flags, &used, &extraFree); v, ctxt, flags, &used, &extraFree);
} }
if (haveModifier) { if (haveModifier) {
/* Skip initial colon. */ /* Skip initial colon. */
tstr++; tstr++;
nstr = ApplyModifiers(nstr, tstr, startc, endc, nstr = ApplyModifiers(nstr, tstr, startc, endc,
v, ctxt, flags, &used, freePtr); v, ctxt, flags, &used, freePtr);
tstr += used; tstr += used;
free(extraFree); free(extraFree);
} else { } else {
*freePtr = extraFree; *freePtr = extraFree;
} }
} }
if (*tstr) { *lengthPtr = tstr - start + (*tstr ? 1 : 0);
*lengthPtr = tstr - start + 1;
} else {
*lengthPtr = tstr - start;
}
if (v->flags & VAR_FROM_ENV) { if (v->flags & VAR_FROM_ENV) {
Boolean destroy = FALSE; Boolean destroy = FALSE;
@ -4132,14 +4110,14 @@ Var_Parse(const char *str, GNode *ctxt, Varf_Flags flags,
char * char *
Var_Subst(const char *var, const char *str, GNode *ctxt, Varf_Flags flags) Var_Subst(const char *var, const char *str, GNode *ctxt, Varf_Flags flags)
{ {
Buffer buf; /* Buffer for forming things */ Buffer buf; /* Buffer for forming things */
char *val; /* Value to substitute for a variable */ char *val; /* Value to substitute for a variable */
int length; /* Length of the variable invocation */ int length; /* Length of the variable invocation */
Boolean trailingBslash; /* variable ends in \ */ Boolean trailingBslash; /* variable ends in \ */
void *freeIt = NULL; /* Set if it should be freed */ void *freeIt = NULL; /* Set if it should be freed */
static Boolean errorReported; /* Set true if an error has already static Boolean errorReported; /* Set true if an error has already
* been reported to prevent a plethora * been reported to prevent a plethora
* of messages when recursing */ * of messages when recursing */
Buf_Init(&buf, 0); Buf_Init(&buf, 0);
errorReported = FALSE; errorReported = FALSE;
@ -4187,8 +4165,7 @@ Var_Subst(const char *var, const char *str, GNode *ctxt, Varf_Flags flags)
else else
expand = TRUE; expand = TRUE;
break; break;
} } else {
else {
const char *p; const char *p;
/* /*
@ -4220,8 +4197,7 @@ Var_Subst(const char *var, const char *str, GNode *ctxt, Varf_Flags flags)
Buf_AddBytes(&buf, p - str, str); Buf_AddBytes(&buf, p - str, str);
str = p; str = p;
expand = FALSE; expand = FALSE;
} } else
else
expand = TRUE; expand = TRUE;
break; break;
} }
@ -4336,6 +4312,6 @@ Var_Dump(GNode *ctxt)
for (h = Hash_EnumFirst(&ctxt->context, &search); for (h = Hash_EnumFirst(&ctxt->context, &search);
h != NULL; h != NULL;
h = Hash_EnumNext(&search)) { h = Hash_EnumNext(&search)) {
VarPrintVar(Hash_GetValue(h)); VarPrintVar(Hash_GetValue(h));
} }
} }