OSSP CVS Repository

ossp - Check-in [1949]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 1949
Date: 2002-Mar-06 11:18:19 (local)
2002-Mar-06 10:18:19 (UTC)
User:rse
Branch:
Comment: Change semantics of ${name:-word} from "If $name is not empty string, then $name, else word" to: "If $name is not empty string and not undefined, then $name, else word". This provides a more intuitive usage of ${name:-word} because it allows the user to turn an undefined variable (as returned with VAR_ERR_UNDEFINED_VARIABLE by the callback) into a defined one. This is important because changing the callback to return undefined variables as empty variables is incorrect because it usually breaks the array loop construct.
Tickets:
Inspections:
Files:
ossp-pkg/var/var.c      1.80 -> 1.81     34 inserted, 7 deleted
ossp-pkg/var/var.pod      1.26 -> 1.27     2 inserted, 2 deleted

ossp-pkg/var/var.c 1.80 -> 1.81

--- var.c        2002/03/06 10:05:10     1.80
+++ var.c        2002/03/06 10:18:19     1.81
@@ -152,6 +152,24 @@
     return;
 }
 
+static int 
+tokenbuf_isundef(
+    tokenbuf_t *buf)
+{
+    if (buf->begin == NULL && buf->end == NULL)
+        return 1;
+    return 0;
+}
+
+static int 
+tokenbuf_isempty(
+    tokenbuf_t *buf)
+{
+    if (buf->begin == buf->end)
+        return 1;
+    return 0;
+}
+
 static void 
 tokenbuf_set(
     tokenbuf_t *buf, const char *begin, const char *end, size_t buffer_size)
@@ -1252,7 +1270,9 @@
                 goto error_return;
             }
             p += rc;
-            if (data->begin != NULL && data->begin == data->end) {
+            if (tokenbuf_isundef(data))
+                tokenbuf_move(&tmptokbuf, data);
+            else if (tokenbuf_isempty(data)) {
                 tokenbuf_free(data);
                 tokenbuf_move(&tmptokbuf, data);
             }
@@ -1823,12 +1843,8 @@
         rc = lookup_value(var, ctx,
                           name.begin, name.end-name.begin, idx,
                           &data, &len, &buffer_size);
-        if (rc == VAR_ERR_UNDEFINED_VARIABLE) {
-            if (ctx->force_expand)
-                goto error_return;
-            tokenbuf_set(result, begin - 1, p, 0);
-            failed = 1;
-        }
+        if (rc == VAR_ERR_UNDEFINED_VARIABLE)
+            tokenbuf_init(result); /* delayed handling of undefined variable */
         else if (rc < 0)
             goto error_return;
         else
@@ -1841,6 +1857,7 @@
     goahead:
     if (p[-1] == ':') {
         tokenbuf_free(&tmp);
+        tokenbuf_init(&tmp);
         p--;
         while (p != end && *p == ':') {
             p++;
@@ -1863,6 +1880,16 @@
             result->end++;
     }
 
+    /* lazy handling of undefined variable */
+    if (!failed && tokenbuf_isundef(result)) {
+        if (ctx->force_expand) {
+            rc = VAR_ERR_UNDEFINED_VARIABLE;
+            goto error_return;
+        }
+        else
+            tokenbuf_set(result, begin - 1, p, 0);
+    }
+
     /* return successfully */
     tokenbuf_free(&name);
     tokenbuf_free(&tmp);


ossp-pkg/var/var.pod 1.26 -> 1.27

--- var.pod      2002/03/04 12:01:53     1.26
+++ var.pod      2002/03/06 10:18:19     1.27
@@ -129,8 +129,8 @@
 
 =item C<${>I<name>C<:->I<word>C<}>
 
-If C<$>I<name> is not empty string, then C<$>I<name>, else I<word>
-(default value).
+If C<$>I<name> is not empty string and not undefined, then C<$>I<name>,
+else I<word> (default value).
 
 =item C<${>I<name>C<:+>I<word>C<}>
 

CVSTrac 2.0.1