diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2007-07-24 18:58:49 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2007-07-24 18:58:49 +0000 |
commit | d11845a844f1ce9d7247513d1a61f9f733c3a4f4 (patch) | |
tree | 755f9ffec702c6032f849bebde01372083ec74e5 | |
parent | de559112e90706dbab29c8f62d8f75d0ccbfe834 (diff) |
simplify computations in the var module: instead of advancing a char *
and keeping track of a length, we just advance the char *, and ditch
the length. We can still get the length at the end of the top-level
functions to satisfy existing interfaces.
Much simpler code, less error-prone.
Okay millert@
-rw-r--r-- | usr.bin/make/arch.c | 2 | ||||
-rw-r--r-- | usr.bin/make/for.c | 2 | ||||
-rw-r--r-- | usr.bin/make/main.c | 2 | ||||
-rw-r--r-- | usr.bin/make/parse.c | 2 | ||||
-rw-r--r-- | usr.bin/make/suff.c | 2 | ||||
-rw-r--r-- | usr.bin/make/var.c | 105 | ||||
-rw-r--r-- | usr.bin/make/varmodifiers.c | 14 | ||||
-rw-r--r-- | usr.bin/make/varmodifiers.h | 8 |
8 files changed, 62 insertions, 75 deletions
diff --git a/usr.bin/make/arch.c b/usr.bin/make/arch.c index d78184c855d..23806f3d76f 100644 --- a/usr.bin/make/arch.c +++ b/usr.bin/make/arch.c @@ -1,5 +1,5 @@ /* $OpenPackages$ */ -/* $OpenBSD: arch.c,v 1.58 2007/07/24 18:56:15 espie Exp $ */ +/* $OpenBSD: arch.c,v 1.59 2007/07/24 18:58:48 espie Exp $ */ /* $NetBSD: arch.c,v 1.17 1996/11/06 17:58:59 christos Exp $ */ /* diff --git a/usr.bin/make/for.c b/usr.bin/make/for.c index f7106cb783c..2b1c75ea918 100644 --- a/usr.bin/make/for.c +++ b/usr.bin/make/for.c @@ -1,5 +1,5 @@ /* $OpenPackages$ */ -/* $OpenBSD: for.c,v 1.34 2007/07/24 18:56:15 espie Exp $ */ +/* $OpenBSD: for.c,v 1.35 2007/07/24 18:58:48 espie Exp $ */ /* $NetBSD: for.c,v 1.4 1996/11/06 17:59:05 christos Exp $ */ /* diff --git a/usr.bin/make/main.c b/usr.bin/make/main.c index 4144e1edb2d..5a6c14090ae 100644 --- a/usr.bin/make/main.c +++ b/usr.bin/make/main.c @@ -1,5 +1,5 @@ /* $OpenPackages$ */ -/* $OpenBSD: main.c,v 1.73 2007/07/24 18:56:15 espie Exp $ */ +/* $OpenBSD: main.c,v 1.74 2007/07/24 18:58:48 espie Exp $ */ /* $NetBSD: main.c,v 1.34 1997/03/24 20:56:36 gwr Exp $ */ /* diff --git a/usr.bin/make/parse.c b/usr.bin/make/parse.c index e61932047f2..207a401becd 100644 --- a/usr.bin/make/parse.c +++ b/usr.bin/make/parse.c @@ -1,5 +1,5 @@ /* $OpenPackages$ */ -/* $OpenBSD: parse.c,v 1.75 2007/07/24 18:56:15 espie Exp $ */ +/* $OpenBSD: parse.c,v 1.76 2007/07/24 18:58:48 espie Exp $ */ /* $NetBSD: parse.c,v 1.29 1997/03/10 21:20:04 christos Exp $ */ /* diff --git a/usr.bin/make/suff.c b/usr.bin/make/suff.c index fe420c2aff5..1fc22a8a5ad 100644 --- a/usr.bin/make/suff.c +++ b/usr.bin/make/suff.c @@ -1,5 +1,5 @@ /* $OpenPackages$ */ -/* $OpenBSD: suff.c,v 1.58 2007/07/24 18:56:15 espie Exp $ */ +/* $OpenBSD: suff.c,v 1.59 2007/07/24 18:58:48 espie Exp $ */ /* $NetBSD: suff.c,v 1.13 1996/11/06 17:59:25 christos Exp $ */ /* diff --git a/usr.bin/make/var.c b/usr.bin/make/var.c index 5d891cc8445..747eb3e534e 100644 --- a/usr.bin/make/var.c +++ b/usr.bin/make/var.c @@ -1,5 +1,5 @@ /* $OpenPackages$ */ -/* $OpenBSD: var.c,v 1.69 2007/07/24 18:56:15 espie Exp $ */ +/* $OpenBSD: var.c,v 1.70 2007/07/24 18:58:48 espie Exp $ */ /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */ /* @@ -826,40 +826,33 @@ Var_ParseSkip(const char **pstr, SymTable *ctxt) { const char *tstr; Var *v; - char paren; const char *str = *pstr; - const char *start; - size_t length; struct Name name; bool result; + bool has_modifier = false; - v = NULL; - start = str; - str++; - - if (*str != '(' && *str != '{') { - name.tofree = false; - tstr = str + 1; - length = 2; - paren = '\0'; - } else { - paren = *str; - str++; - + switch(str[1]) { + case '(': + case '{': /* Find eventual modifiers in the variable */ - tstr = VarName_Get(str, &name, ctxt, false, find_pos(paren)); + tstr = VarName_Get(str+2, &name, ctxt, false, find_pos(str[1])); VarName_Free(&name); - length = tstr - start; - if (*tstr != 0) - length++; + if (*tstr == ':') + has_modifier = true; + else if (*tstr != '\0') { + tstr++; + } + break; + default: + tstr = str + 2; + break; } - result = true; - if (*tstr == ':' && paren != '\0') - if (VarModifiers_Apply(NULL, NULL, ctxt, true, NULL, tstr, - paren, &length) == var_Error) + if (has_modifier) + if (VarModifiers_Apply(NULL, NULL, ctxt, true, NULL, &tstr, + str[1]) == var_Error) result = false; - *pstr += length; + *pstr = tstr; return result; } @@ -964,46 +957,41 @@ Var_Parse(const char *str, /* The string to parse */ size_t *lengthPtr, /* OUT: The length of the specification */ bool *freePtr) /* OUT: true if caller should free result */ { - const char *tstr; /* Pointer into str */ - Var *v; /* Variable in invocation */ - char paren; /* Parenthesis or brace or nothing */ + const char *tstr; + Var *v; struct Name name; - const char *start; - char *val; /* Variable value */ + char *val; uint32_t k; int idx; + bool has_modifier = false; *freePtr = false; - start = str++; - - val = NULL; - v = NULL; - idx = GLOBAL_INDEX; - - if (*str != '(' && *str != '{') { - name.s = str; - name.e = str+1; - name.tofree = false; - tstr = str + 1; - *lengthPtr = 2; - paren = '\0'; - } else { - paren = *str; - str++; + switch(str[1]) { + case '(': + case '{': /* Find eventual modifiers in the variable */ - tstr = VarName_Get(str, &name, ctxt, false, find_pos(paren)); - *lengthPtr = tstr - start; - if (*tstr != '\0') - (*lengthPtr)++; + tstr = VarName_Get(str+2, &name, ctxt, false, find_pos(str[1])); + if (*tstr == ':') + has_modifier = true; + else if (*tstr != '\0') + tstr++; + break; + default: + name.s = str+1; + name.e = str+2; + name.tofree = false; + tstr = str + 2; + break; } idx = classify_var(name.s, &name.e, &k); v = find_any_var(name.s, name.e, ctxt, idx, k); val = get_expanded_value(v, idx, ctxt, err, freePtr); - if (*tstr == ':' && paren != '\0') + if (has_modifier) { val = VarModifiers_Apply(val, &name, ctxt, err, freePtr, - tstr, paren, lengthPtr); + &tstr, str[1]); + } if (val == NULL) { val = err ? var_Error : varNoError; /* Dynamic source */ @@ -1011,7 +999,7 @@ Var_Parse(const char *str, /* The string to parse */ /* can't be expanded for now: copy the spec instead. */ if (ctxt == NULL) { *freePtr = true; - val = Str_dupi(start, start+ *lengthPtr); + val = Str_dupi(str, tstr); } else { /* somehow, this should have been expanded already. */ GNode *n; @@ -1036,6 +1024,7 @@ Var_Parse(const char *str, /* The string to parse */ } } VarName_Free(&name); + *lengthPtr = tstr - str; return val; } @@ -1210,12 +1199,10 @@ Var_SubstVar(Buffer buf, /* To store result */ continue; } if (*p == ':') { - size_t length; /* Length of variable name */ bool doFree; /* should val be freed ? */ char *newval; struct Name name; - length = p - str + 1; doFree = false; name.s = var; name.e = var + (p-str); @@ -1223,12 +1210,12 @@ Var_SubstVar(Buffer buf, /* To store result */ /* val won't be freed since !doFree, but * VarModifiers_Apply doesn't know that, * hence the cast. */ - newval = VarModifiers_Apply((char *)val, &name, - NULL, false, &doFree, p, paren, &length); + newval = VarModifiers_Apply((char *)val, + &name, NULL, false, &doFree, &p, paren); Buf_AddString(buf, newval); if (doFree) free(newval); - str += length; + str = p; continue; } else str = p+1; diff --git a/usr.bin/make/varmodifiers.c b/usr.bin/make/varmodifiers.c index 89804e4f430..17f23325685 100644 --- a/usr.bin/make/varmodifiers.c +++ b/usr.bin/make/varmodifiers.c @@ -1,5 +1,5 @@ /* $OpenPackages$ */ -/* $OpenBSD: varmodifiers.c,v 1.17 2007/07/24 18:56:15 espie Exp $ */ +/* $OpenBSD: varmodifiers.c,v 1.18 2007/07/24 18:58:48 espie Exp $ */ /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */ /* @@ -1372,11 +1372,12 @@ do_regex(const char *s, const struct Name *n UNUSED, void *arg) char * VarModifiers_Apply(char *str, const struct Name *name, SymTable *ctxt, - bool err, bool *freePtr, const char *start, int paren, size_t *lengthPtr) + bool err, bool *freePtr, const char **pscan, int paren) { const char *tstr; bool atstart; /* Some ODE modifiers only make sense at start */ char endc = paren == '(' ? ')' : '}'; + const char *start = *pscan; tstr = start; /* @@ -1456,13 +1457,12 @@ VarModifiers_Apply(char *str, const struct Name *name, SymTable *ctxt, if (DEBUG(VAR)) printf("Result is \"%s\"\n", str); } - if (*tstr == '\0') { + if (*tstr == '\0') Error("Unclosed variable specification"); - /* make tstr point at the last char of the variable specification */ - tstr--; - } + else + tstr++; - *lengthPtr += tstr - start; + *pscan = tstr; return str; } diff --git a/usr.bin/make/varmodifiers.h b/usr.bin/make/varmodifiers.h index 2a11b45c819..ec11eea6906 100644 --- a/usr.bin/make/varmodifiers.h +++ b/usr.bin/make/varmodifiers.h @@ -2,7 +2,7 @@ #define VARMODIFIERS_H /* $OpenPackages$ */ -/* $OpenBSD: varmodifiers.h,v 1.6 2007/07/24 18:56:15 espie Exp $ */ +/* $OpenBSD: varmodifiers.h,v 1.7 2007/07/24 18:58:48 espie Exp $ */ /* * Copyright (c) 1999 Marc Espie. @@ -38,19 +38,19 @@ extern void VarModifiers_Init(void); /* result = VarModifiers_Apply(val, name, ctxt, undef_is_bad, - * &should_free, modstart, paren, &length); + * &should_free, &modstart, paren); * Applies variable modifiers starting at modstart (including :), * using parenthesis paren, to value val. * Variables in spec are taken from context ctxt. * If undef_is_bad, error occurs if undefined variables are mentioned. - * length is filled with the total length of the modifier spec. + * modstart is advanced past the end of the spec. * name holds the name of the corresponding variable, as some ODE * modifiers need it. * * If both val and name are NULL, VarModifiers_Apply just parses the * modifiers specification, as it can't apply it to anything. */ extern char *VarModifiers_Apply(char *, const struct Name *, SymTable *, - bool, bool *, const char *, int, size_t *); + bool, bool *, const char **, int); /* Direct interface to specific modifiers used under special circumstances. */ /* tails = Var_GetTail(string); |