Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/make make: extract duplicate code for detecting a mo...
details: https://anonhg.NetBSD.org/src/rev/b2a0d97799bd
branches: trunk
changeset: 981531:b2a0d97799bd
user: rillig <rillig%NetBSD.org@localhost>
date: Sun Mar 14 15:06:19 2021 +0000
description:
make: extract duplicate code for detecting a modifier's end
No functional change.
diffstat:
usr.bin/make/var.c | 86 +++++++++++++++++++++++++++--------------------------
1 files changed, 44 insertions(+), 42 deletions(-)
diffs (truncated from 321 to 300 lines):
diff -r a6c9fb398792 -r b2a0d97799bd usr.bin/make/var.c
--- a/usr.bin/make/var.c Sun Mar 14 15:04:13 2021 +0000
+++ b/usr.bin/make/var.c Sun Mar 14 15:06:19 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: var.c,v 1.858 2021/03/14 15:04:13 rillig Exp $ */
+/* $NetBSD: var.c,v 1.859 2021/03/14 15:06:19 rillig Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@@ -140,7 +140,7 @@
#include "metachar.h"
/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.858 2021/03/14 15:04:13 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.859 2021/03/14 15:06:19 rillig Exp $");
typedef enum VarFlags {
VFL_NONE = 0,
@@ -2277,22 +2277,27 @@
NULL, NULL, NULL);
}
+MAKE_INLINE Boolean
+IsDelimiter(char ch, const ApplyModifiersState *st)
+{
+ return ch == ':' || ch == st->endc;
+}
+
/* Test whether mod starts with modname, followed by a delimiter. */
MAKE_INLINE Boolean
-ModMatch(const char *mod, const char *modname, char endc)
+ModMatch(const char *mod, const char *modname, const ApplyModifiersState *st)
{
size_t n = strlen(modname);
- return strncmp(mod, modname, n) == 0 &&
- (mod[n] == endc || mod[n] == ':');
+ return strncmp(mod, modname, n) == 0 && IsDelimiter(mod[n], st);
}
/* Test whether mod starts with modname, followed by a delimiter or '='. */
MAKE_INLINE Boolean
-ModMatchEq(const char *mod, const char *modname, char endc)
+ModMatchEq(const char *mod, const char *modname, const ApplyModifiersState *st)
{
size_t n = strlen(modname);
return strncmp(mod, modname, n) == 0 &&
- (mod[n] == endc || mod[n] == ':' || mod[n] == '=');
+ (IsDelimiter(mod[n], st) || mod[n] == '=');
}
static Boolean
@@ -2452,7 +2457,7 @@
Buf_Init(&buf);
p = *pp + 1;
- while (*p != st->endc && *p != ':' && *p != '\0') {
+ while (!IsDelimiter(*p, st) && *p != '\0') {
/* XXX: This code is similar to the one in Var_Parse.
* See if the code can be merged.
@@ -2462,8 +2467,7 @@
/* See Buf_AddEscaped in for.c. */
if (*p == '\\') {
char c = p[1];
- if (c == st->endc || c == ':' || c == '$' ||
- c == '\\') {
+ if (IsDelimiter(c, st) || c == '$' || c == '\\') {
Buf_AddByte(&buf, c);
p += 2;
continue;
@@ -2534,7 +2538,7 @@
time_t utc;
const char *mod = *pp;
- if (!ModMatchEq(mod, "gmtime", st->endc))
+ if (!ModMatchEq(mod, "gmtime", st))
return AMR_UNKNOWN;
if (mod[6] == '=') {
@@ -2561,7 +2565,7 @@
time_t utc;
const char *mod = *pp;
- if (!ModMatchEq(mod, "localtime", st->endc))
+ if (!ModMatchEq(mod, "localtime", st))
return AMR_UNKNOWN;
if (mod[9] == '=') {
@@ -2585,7 +2589,7 @@
static ApplyModifierResult
ApplyModifier_Hash(const char **pp, ApplyModifiersState *st)
{
- if (!ModMatch(*pp, "hash", st->endc))
+ if (!ModMatch(*pp, "hash", st))
return AMR_UNKNOWN;
Expr_SetValueOwn(st->expr, VarHash(st->expr->value.str));
@@ -2659,7 +2663,7 @@
size_t i;
const char *mod = *pp;
- if (!ModMatchEq(mod, "range", st->endc))
+ if (!ModMatchEq(mod, "range", st))
return AMR_UNKNOWN;
if (mod[5] == '=') {
@@ -2722,7 +2726,7 @@
const char *p;
for (p = mod + 1; *p != '\0' && !(*p == ':' && nest == 0); p++) {
if (*p == '\\' &&
- (p[1] == ':' || p[1] == st->endc || p[1] == st->startc)) {
+ (IsDelimiter(p[1], st) || p[1] == st->startc)) {
if (!needSubst)
copy = TRUE;
p++;
@@ -2752,7 +2756,7 @@
for (; src < endpat; src++, dst++) {
if (src[0] == '\\' && src + 1 < endpat &&
/* XXX: st->startc is missing here; see above */
- (src[1] == ':' || src[1] == st->endc))
+ IsDelimiter(src[1], st))
src++;
*dst = *src;
}
@@ -2923,7 +2927,7 @@
static ApplyModifierResult
ApplyModifier_Quote(const char **pp, ApplyModifiersState *st)
{
- if ((*pp)[1] == st->endc || (*pp)[1] == ':') {
+ if (IsDelimiter((*pp)[1], st)) {
Expr_SetValueOwn(st->expr,
VarQuote(st->expr->value.str, **pp == 'q'));
(*pp)++;
@@ -2946,14 +2950,14 @@
const char *sep = *pp + 2;
/* ":ts<any><endc>" or ":ts<any>:" */
- if (sep[0] != st->endc && (sep[1] == st->endc || sep[1] == ':')) {
+ if (sep[0] != st->endc && IsDelimiter(sep[1], st)) {
st->sep = sep[0];
*pp = sep + 1;
goto ok;
}
/* ":ts<endc>" or ":ts:" */
- if (sep[0] == st->endc || sep[0] == ':') {
+ if (IsDelimiter(sep[0], st)) {
st->sep = '\0'; /* no separator */
*pp = sep;
goto ok;
@@ -2997,7 +3001,7 @@
"Invalid character number: %s", p);
return AMR_CLEANUP;
}
- if (*p != ':' && *p != st->endc) {
+ if (!IsDelimiter(*p, st)) {
(*pp)++; /* just for backwards compatibility */
return AMR_BAD;
}
@@ -3046,7 +3050,7 @@
const char *mod = *pp;
assert(mod[0] == 't');
- if (mod[1] == st->endc || mod[1] == ':' || mod[1] == '\0') {
+ if (IsDelimiter(mod[1], st) || mod[1] == '\0') {
*pp = mod + 1;
return AMR_BAD; /* Found ":t<endc>" or ":t:". */
}
@@ -3054,7 +3058,7 @@
if (mod[1] == 's')
return ApplyModifier_ToSep(pp, st);
- if (mod[2] != st->endc && mod[2] != ':') { /* :t<unrecognized> */
+ if (!IsDelimiter(mod[2], st)) { /* :t<unrecognized> */
*pp = mod + 1;
return AMR_BAD;
}
@@ -3103,7 +3107,7 @@
if (res != VPR_OK)
return AMR_CLEANUP;
- if (**pp != ':' && **pp != st->endc)
+ if (!IsDelimiter(**pp, st))
goto bad_modifier; /* Found junk after ']' */
if (estr[0] == '\0')
@@ -3216,13 +3220,13 @@
Words words = Str_Words(st->expr->value.str, FALSE);
- if (mod[1] == st->endc || mod[1] == ':') {
+ if (IsDelimiter(mod[1], st)) {
/* :O sorts ascending */
qsort(words.words, words.len, sizeof words.words[0],
str_cmp_asc);
} else if ((mod[1] == 'r' || mod[1] == 'x') &&
- (mod[2] == st->endc || mod[2] == ':')) {
+ IsDelimiter(mod[2], st)) {
(*pp)++;
if (mod[1] == 'r') { /* :Or sorts descending */
@@ -3400,7 +3404,7 @@
{
Expr *expr = st->expr;
const char *mod = *pp;
- if (!ModMatchEq(mod, "_", st->endc))
+ if (!ModMatchEq(mod, "_", st))
return AMR_UNKNOWN;
if (mod[1] == '=') {
@@ -3429,8 +3433,7 @@
ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st,
ModifyWordProc modifyWord)
{
- char delim = (*pp)[1];
- if (delim != st->endc && delim != ':')
+ if (!IsDelimiter((*pp)[1], st))
return AMR_UNKNOWN;
ModifyWords(st, modifyWord, NULL, st->oneBigWord);
@@ -3441,7 +3444,7 @@
static ApplyModifierResult
ApplyModifier_Unique(const char **pp, ApplyModifiersState *st)
{
- if ((*pp)[1] == st->endc || (*pp)[1] == ':') {
+ if (IsDelimiter((*pp)[1], st)) {
Expr_SetValueOwn(st->expr, VarUniq(st->expr->value.str));
(*pp)++;
return AMR_OK;
@@ -3513,7 +3516,7 @@
{
Expr *expr = st->expr;
const char *p = *pp;
- if (p[1] == 'h' && (p[2] == st->endc || p[2] == ':')) {
+ if (p[1] == 'h' && IsDelimiter(p[2], st)) {
if (expr->eflags & VARE_WANTRES) {
const char *errfmt;
char *output = Cmd_Exec(expr->value.str, &errfmt);
@@ -3538,13 +3541,12 @@
#endif
static void
-LogBeforeApply(const ApplyModifiersState *st, const char *mod, char endc)
+LogBeforeApply(const ApplyModifiersState *st, const char *mod)
{
- Expr *expr = st->expr;
+ const Expr *expr = st->expr;
char eflags_str[VarEvalFlags_ToStringSize];
char vflags_str[VarFlags_ToStringSize];
- Boolean is_single_char = mod[0] != '\0' &&
- (mod[1] == endc || mod[1] == ':');
+ Boolean is_single_char = mod[0] != '\0' && IsDelimiter(mod[1], st);
/* At this point, only the first character of the modifier can
* be used since the end of the modifier is not yet known. */
@@ -3557,9 +3559,9 @@
}
static void
-LogAfterApply(ApplyModifiersState *st, const char *p, const char *mod)
+LogAfterApply(const ApplyModifiersState *st, const char *p, const char *mod)
{
- Expr *expr = st->expr;
+ const Expr *expr = st->expr;
const char *value = expr->value.str;
char eflags_str[VarEvalFlags_ToStringSize];
char vflags_str[VarFlags_ToStringSize];
@@ -3674,7 +3676,7 @@
(void)Var_Parse(&p, expr->scope, expr->eflags, &mods);
/* TODO: handle errors */
- if (mods.str[0] != '\0' && *p != '\0' && *p != ':' && *p != st->endc) {
+ if (mods.str[0] != '\0' && *p != '\0' && !IsDelimiter(*p, st)) {
FStr_Done(&mods);
return AMIR_SYSV;
}
@@ -3708,14 +3710,14 @@
}
static ApplyModifierResult
-ApplySingleModifier(const char **pp, char endc, ApplyModifiersState *st)
+ApplySingleModifier(const char **pp, ApplyModifiersState *st)
{
ApplyModifierResult res;
const char *mod = *pp;
const char *p = *pp;
if (DEBUG(VAR))
- LogBeforeApply(st, mod, endc);
+ LogBeforeApply(st, mod);
res = ApplyModifier(&p, st);
@@ -3733,7 +3735,7 @@
* errors and leads to wrong results.
* Parsing should rather stop here.
*/
- for (p++; *p != ':' && *p != st->endc && *p != '\0'; p++)
+ for (p++; !IsDelimiter(*p, st) && *p != '\0'; p++)
Home |
Main Index |
Thread Index |
Old Index