OSSP CVS Repository

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

Check-in Number: 1410
Date: 2001-Dec-08 17:17:56 (local)
2001-Dec-08 16:17:56 (UTC)
User:simons
Branch:
Comment: Implemented force_expand mode in num_exp().
Tickets:
Inspections:
Files:
ossp-pkg/var/var.c      1.40 -> 1.41     59 inserted, 41 deleted
ossp-pkg/var/var_test.c      1.19 -> 1.20     1 inserted, 1 deleted

ossp-pkg/var/var.c 1.40 -> 1.41

--- var.c        2001/12/08 16:02:15     1.40
+++ var.c        2001/12/08 16:17:56     1.41
@@ -432,7 +432,7 @@
 static int command(const char *, const char *, const var_config_t *,
                    const char_class_t, var_cb_t, void *, int, tokenbuf_t *);
 static int num_exp(const char *begin, const char *end, int current_index,
-                   int* result, const var_config_t *config,
+                   int* result, int* failed, const var_config_t *config,
                    const char_class_t nameclass,
                    var_cb_t lookup, void* lookup_context);
 
@@ -510,7 +510,7 @@
     }
 
 static int num_exp_read_operand(const char *begin, const char *end, int current_index,
-                                int* result, const var_config_t *config,
+                                int* result, int* failed, const var_config_t *config,
                                 const char_class_t nameclass,
                                 var_cb_t lookup, void* lookup_context)
     {
@@ -527,7 +527,7 @@
 
     if (*p == '(')
         {
-        rc = num_exp(++p, end, current_index, result, config, nameclass, lookup, lookup_context);
+        rc = num_exp(++p, end, current_index, result, failed, config, nameclass, lookup, lookup_context);
         if (rc < 0)
             return rc;
         p += rc;
@@ -540,14 +540,26 @@
     else if (*p == config->varinit)
         {
         rc = variable(p, end, config, nameclass, lookup, lookup_context, 1, &tmp);
-        if (rc < 0)
-            return rc;
-        p += rc;
-
-        rc = num_exp(tmp.begin, tmp.end, current_index, result, config, nameclass, lookup, lookup_context);
-        tokenbuf_free(&tmp);
-        if (rc < 0)
-            return rc;
+        if (rc == VAR_ERR_UNDEFINED_VARIABLE)
+            {
+            *failed = 1;
+            rc = variable(p, end, config, nameclass, lookup, lookup_context, 0, &tmp);
+            if (rc < 0)
+                return rc;
+            p += rc;
+            printf("Expanding variable() failed, eating %d characters and substituting 1.\n", rc);
+            *result = 1;
+            }
+        else
+            {
+            if (rc < 0)
+                return rc;
+            p += rc;
+            rc = num_exp(tmp.begin, tmp.end, current_index, result, failed, config, nameclass, lookup, lookup_context);
+            tokenbuf_free(&tmp);
+            if (rc < 0)
+                return rc;
+            }
         }
     else if (config->current_index && *p == config->current_index)
         {
@@ -586,7 +598,7 @@
     }
 
 static int num_exp(const char *begin, const char *end, int current_index,
-                   int* result, const var_config_t *config,
+                   int* result, int* failed, const var_config_t *config,
                    const char_class_t nameclass,
                    var_cb_t lookup, void* lookup_context)
     {
@@ -598,7 +610,7 @@
     if (begin == end)
         return VAR_ERR_INCOMPLETE_INDEX_SPEC;
 
-    rc = num_exp_read_operand(p, end, current_index, result, config, nameclass, lookup, lookup_context);
+    rc = num_exp_read_operand(p, end, current_index, result, failed, config, nameclass, lookup, lookup_context);
     if (rc < 0)
         return rc;
     p += rc;
@@ -611,7 +623,7 @@
             {
             operator = *p++;
             printf("Operator is '%c'\n", operator);
-            rc = num_exp(p, end, current_index, &right, config, nameclass, lookup, lookup_context);
+            rc = num_exp(p, end, current_index, &right, failed, config, nameclass, lookup, lookup_context);
             if (rc < 0)
                 return rc;
             p += rc;
@@ -628,7 +640,7 @@
             printf("Operator is '%c'\n", operator);
 
 
-            rc = num_exp_read_operand(p, end, current_index, &right, config, nameclass, lookup, lookup_context);
+            rc = num_exp_read_operand(p, end, current_index, &right, failed, config, nameclass, lookup, lookup_context);
             if (rc < 0)
                 return rc;
             p += rc;
@@ -727,9 +739,7 @@
     if (config->startindex && *p == config->startindex)
         {
         printf("Found START-INDEX: %s\n", p);
-        ++p;
-
-        rc = num_exp(p, end, 0, &index, config, nameclass, lookup, lookup_context);
+        rc = num_exp(++p, end, 0, &index, &failed, config, nameclass, lookup, lookup_context);
         if (rc < 0)
             goto error_return;
         if (rc == 0)
@@ -754,7 +764,6 @@
         ++p;
 
         printf("Found index %d.\n", index);
-
         }
 
     /* Now we have the name of the variable stored in "name". The next
@@ -769,36 +778,45 @@
 
     /* Use the lookup callback to get the variable's contents. */
 
-    rc = (*lookup) (lookup_context, name.begin, name.end - name.begin, index,
-                    &data, &len, &buffer_size);
-    if (rc < 0)
-        goto error_return;
-    if (rc == 0)
+    if (failed)
         {
-        /* The variable is undefined. What we'll do now depends on the
-           force_expand flag. */
-
-        if (force_expand)
-            {
-            rc = VAR_ERR_UNDEFINED_VARIABLE;
-            goto error_return;
-            }
-        /* Initialize result to point back to the original text in
-           the buffer. */
-
         result->begin = begin - 1;
         result->end = p;
         result->buffer_size = 0;
-        failed = 1;
         }
     else
         {
-        /* The preliminary result is the contents of the variable.
-           This may be modified by the commands that may follow. */
+        rc = (*lookup) (lookup_context, name.begin, name.end - name.begin, index,
+                        &data, &len, &buffer_size);
+        if (rc < 0)
+            goto error_return;
+        if (rc == 0)
+            {
+            /* The variable is undefined. What we'll do now depends on the
+               force_expand flag. */
+
+            if (force_expand)
+                {
+                rc = VAR_ERR_UNDEFINED_VARIABLE;
+                goto error_return;
+                }
+            /* Initialize result to point back to the original text in
+               the buffer. */
 
-        result->begin = data;
-        result->end = data + len;
-        result->buffer_size = buffer_size;
+            result->begin = begin - 1;
+            result->end = p;
+            result->buffer_size = 0;
+            failed = 1;
+            }
+        else
+            {
+            /* The preliminary result is the contents of the variable.
+               This may be modified by the commands that may follow. */
+
+            result->begin = data;
+            result->end = data + len;
+            result->buffer_size = buffer_size;
+            }
         }
 
     if (p[-1] == ':')


ossp-pkg/var/var_test.c 1.19 -> 1.20

--- var_test.c   2001/12/08 16:02:15     1.19
+++ var_test.c   2001/12/08 16:17:56     1.20
@@ -141,7 +141,7 @@
         { "${ARRAY[$NUMBER]}",            "entry2"                                         },
         { "${ARRAY[$NUMEXP]}",            "entry1"                                         },
         { "${ARRAY[$NUMEXP-1]}",          "entry0"                                         },
-        { "${ARRAY[${ARRAY[-1]}-1]}",     "entry3"                                         }
+        { "${ARRAY[${UNDEFINED}-1]}",     "${ARRAY[${UNDEFINED}-1]}"                       }
     };
     char *tmp;
     size_t tmp_len;

CVSTrac 2.0.1