--- var.c 2001/12/13 14:34:17 1.46
+++ var.c 2001/12/13 16:03:42 1.47
@@ -429,12 +429,18 @@
/* The recursive-descent parser for variable expressions. */
-static int variable(const char *, const char *, const var_config_t *,
- const char_class_t, var_cb_t, void *, int, tokenbuf_t *, int);
-static int command(const char *, const char *, const var_config_t *,
- const char_class_t, var_cb_t, void *, int, tokenbuf_t *, int);
+static int variable(const char *begin, const char *end,
+ const var_config_t *config, const char_class_t nameclass,
+ var_cb_t lookup, void *lookup_context,
+ int force_expand, tokenbuf_t *result, int current_index,
+ int* rel_lookup_flag);
+static int command(const char *begin, const char *end,
+ const var_config_t *config, const char_class_t nameclass,
+ var_cb_t lookup, void *lookup_context, int force_expand,
+ tokenbuf_t *data, int current_index, int* rel_lookup_flag);
static int num_exp(const char *begin, const char *end, int current_index,
- int* result, int* failed, const var_config_t *config,
+ int* result, int* failed, int* rel_lookup_flag,
+ const var_config_t *config,
const char_class_t nameclass,
var_cb_t lookup, void* lookup_context);
@@ -520,7 +526,8 @@
}
static int num_exp_read_operand(const char *begin, const char *end, int current_index,
- int* result, int* failed, const var_config_t *config,
+ int* result, int* failed, int* rel_lookup_flag,
+ const var_config_t *config,
const char_class_t nameclass,
var_cb_t lookup, void* lookup_context)
{
@@ -533,11 +540,9 @@
if (begin == end)
return VAR_ERR_INCOMPLETE_INDEX_SPEC;
- printf("Parsing for operand: '%s'\n", p);
-
if (*p == '(')
{
- rc = num_exp(++p, end, current_index, result, failed, config, nameclass, lookup, lookup_context);
+ rc = num_exp(++p, end, current_index, result, failed, rel_lookup_flag, config, nameclass, lookup, lookup_context);
if (rc < 0)
return rc;
p += rc;
@@ -549,15 +554,14 @@
}
else if (*p == config->varinit)
{
- rc = variable(p, end, config, nameclass, lookup, lookup_context, 1, &tmp, current_index);
+ rc = variable(p, end, config, nameclass, lookup, lookup_context, 1, &tmp, current_index, rel_lookup_flag);
if (rc == VAR_ERR_UNDEFINED_VARIABLE)
{
*failed = 1;
- rc = variable(p, end, config, nameclass, lookup, lookup_context, 0, &tmp, current_index);
+ rc = variable(p, end, config, nameclass, lookup, lookup_context, 0, &tmp, current_index, rel_lookup_flag);
if (rc < 0)
return rc;
p += rc;
- printf("Expanding variable() failed, eating %d characters and substituting 0.\n", rc);
*result = 0;
}
else
@@ -565,7 +569,7 @@
if (rc < 0)
return rc;
p += rc;
- rc = num_exp(tmp.begin, tmp.end, current_index, result, failed, config, nameclass, lookup, lookup_context);
+ rc = num_exp(tmp.begin, tmp.end, current_index, result, failed, rel_lookup_flag, config, nameclass, lookup, lookup_context);
tokenbuf_free(&tmp);
if (rc < 0)
return rc;
@@ -575,6 +579,7 @@
{
++p;
*result = current_index;
+ ++(*rel_lookup_flag);
}
else if (isdigit(*p))
{
@@ -608,7 +613,8 @@
}
static int num_exp(const char *begin, const char *end, int current_index,
- int* result, int* failed, const var_config_t *config,
+ int* result, int* failed, int* rel_lookup_flag,
+ const var_config_t *config,
const char_class_t nameclass,
var_cb_t lookup, void* lookup_context)
{
@@ -620,41 +626,32 @@
if (begin == end)
return VAR_ERR_INCOMPLETE_INDEX_SPEC;
- rc = num_exp_read_operand(p, end, current_index, result, failed, config, nameclass, lookup, lookup_context);
+ rc = num_exp_read_operand(p, end, current_index, result, failed, rel_lookup_flag, config, nameclass, lookup, lookup_context);
if (rc < 0)
return rc;
p += rc;
- printf("left operand is %d\n", *result);
while (p != end)
{
- printf("Parsing for operator: '%s'\n", p);
if (*p == '+' || *p == '-')
{
operator = *p++;
- printf("Operator is '%c'\n", operator);
- rc = num_exp(p, end, current_index, &right, failed, config, nameclass, lookup, lookup_context);
+ rc = num_exp(p, end, current_index, &right, failed, rel_lookup_flag, config, nameclass, lookup, lookup_context);
if (rc < 0)
return rc;
p += rc;
- printf("Calculating %d %c %d --> ", *result, operator, right);
if (operator == '+')
*result = *result + right;
else
*result = *result - right;
- printf("%d\n", *result);
}
else if (*p == '*' || *p == '/' || *p == '%')
{
operator = *p++;
- printf("Operator is '%c'\n", operator);
-
-
- rc = num_exp_read_operand(p, end, current_index, &right, failed, config, nameclass, lookup, lookup_context);
+ rc = num_exp_read_operand(p, end, current_index, &right, failed, rel_lookup_flag, config, nameclass, lookup, lookup_context);
if (rc < 0)
return rc;
p += rc;
- printf("Calculating %d %c %d --> ", *result, operator, right);
if (operator == '*')
{
*result = *result * right;
@@ -683,12 +680,9 @@
else
*result = *result % right;
}
- printf("%d\n", *result);
}
else
break;
-
- printf("Parsing for right: '%s'\n", p);
}
return p - begin;
@@ -698,7 +692,7 @@
const var_config_t *config,
const char_class_t nameclass, var_cb_t lookup,
void *lookup_context, int force_expand,
- tokenbuf_t *result, int current_index)
+ tokenbuf_t *result, int current_index, int* rel_lookup_flag)
{
const char *p = begin;
const char *data;
@@ -742,7 +736,7 @@
}
rc = variable(p, end, config, nameclass, lookup, lookup_context,
- force_expand, &tmp, current_index);
+ force_expand, &tmp, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
if (rc > 0)
@@ -770,8 +764,7 @@
if (config->startindex && *p == config->startindex)
{
- printf("Found START-INDEX: %s\n", p);
- rc = num_exp(++p, end, current_index, &index, &failed, config, nameclass, lookup, lookup_context);
+ rc = num_exp(++p, end, current_index, &index, &failed, rel_lookup_flag, config, nameclass, lookup, lookup_context);
if (rc < 0)
goto error_return;
if (rc == 0)
@@ -781,8 +774,6 @@
}
p += rc;
- printf("Expecting END-INDEX: %s\n", p);
-
if (p == end)
{
rc = VAR_ERR_INCOMPLETE_INDEX_SPEC;
@@ -794,8 +785,6 @@
goto error_return;
}
++p;
-
- printf("Found index %d.\n", index);
}
/* Now we have the name of the variable stored in "name". The next
@@ -862,10 +851,10 @@
++p;
if (!failed)
rc = command(p, end, config, nameclass, lookup,
- lookup_context, force_expand, result, current_index);
+ lookup_context, force_expand, result, current_index, rel_lookup_flag);
else
rc = command(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &tmp, current_index);
+ lookup_context, force_expand, &tmp, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
p += rc;
@@ -901,7 +890,8 @@
static int variable(const char *begin, const char *end,
const var_config_t *config, const char_class_t nameclass,
var_cb_t lookup, void *lookup_context,
- int force_expand, tokenbuf_t *result, int current_index)
+ int force_expand, tokenbuf_t *result, int current_index,
+ int* rel_lookup_flag)
{
const char *p = begin;
const char *data;
@@ -948,7 +938,7 @@
/* OK, we're dealing with a complex expression here. */
rc = expression(p, end, config, nameclass, lookup, lookup_context,
- force_expand, result, current_index);
+ force_expand, result, current_index, rel_lookup_flag);
if (rc > 0)
++rc;
return rc;
@@ -958,7 +948,8 @@
const var_config_t *config,
const char_class_t nameclass, var_cb_t lookup,
void *lookup_context, int force_expand,
- tokenbuf_t *result, int current_index)
+ tokenbuf_t *result, int current_index,
+ int* rel_lookup_flag)
{
const char *p = begin;
tokenbuf_t tmp;
@@ -983,7 +974,7 @@
}
rc = variable(p, end, config, nameclass, lookup, lookup_context,
- force_expand, &tmp, current_index);
+ force_expand, &tmp, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
if (rc > 0) {
@@ -1010,7 +1001,8 @@
const var_config_t *config,
const char_class_t nameclass, var_cb_t lookup,
void *lookup_context, int force_expand,
- tokenbuf_t *result, int current_index)
+ tokenbuf_t *result, int current_index,
+ int* rel_lookup_flag)
{
const char *p = begin;
tokenbuf_t tmp;
@@ -1035,7 +1027,7 @@
}
rc = variable(p, end, config, nameclass, lookup, lookup_context,
- force_expand, &tmp, current_index);
+ force_expand, &tmp, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
if (rc > 0) {
@@ -1481,7 +1473,7 @@
static int command(const char *begin, const char *end,
const var_config_t *config, const char_class_t nameclass,
var_cb_t lookup, void *lookup_context, int force_expand,
- tokenbuf_t *data, int current_index)
+ tokenbuf_t *data, int current_index, int* rel_lookup_flag)
{
const char *p = begin;
tokenbuf_t tmptokbuf;
@@ -1587,7 +1579,8 @@
case '-': /* Substitute parameter if data is empty. */
++p;
rc = exptext_or_variable(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &tmptokbuf, current_index);
+ lookup_context, force_expand, &tmptokbuf,
+ current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
if (rc == 0) {
@@ -1604,7 +1597,7 @@
case '*': /* Return "" if data is not empty, parameter otherwise. */
++p;
rc = exptext_or_variable(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &tmptokbuf, current_index);
+ lookup_context, force_expand, &tmptokbuf, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
if (rc == 0) {
@@ -1627,7 +1620,7 @@
case '+': /* Substitute parameter if data is not empty. */
++p;
rc = exptext_or_variable(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &tmptokbuf, current_index);
+ lookup_context, force_expand, &tmptokbuf, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
if (rc == 0) {
@@ -1649,7 +1642,7 @@
++p;
rc = substext_or_variable(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &search, current_index);
+ lookup_context, force_expand, &search, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
p += rc;
@@ -1661,7 +1654,7 @@
++p;
rc = substext_or_variable(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &replace, current_index);
+ lookup_context, force_expand, &replace, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
p += rc;
@@ -1695,7 +1688,7 @@
++p;
rc = substext_or_variable(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &search, current_index);
+ lookup_context, force_expand, &search, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
p += rc;
@@ -1707,7 +1700,7 @@
++p;
rc = substext_or_variable(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &replace, current_index);
+ lookup_context, force_expand, &replace, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
p += rc;
@@ -1750,7 +1743,7 @@
++p;
rc = substext_or_variable(p, end, config, nameclass, lookup,
- lookup_context, force_expand, &replace, current_index);
+ lookup_context, force_expand, &replace, current_index, rel_lookup_flag);
if (rc < 0)
goto error_return;
p += rc;
@@ -1803,7 +1796,7 @@
{
var_cb_t lookup;
void* context;
- int* found_variables;
+ int* rel_lookup_flag;
};
static int lookup_wrapper(void *context,
@@ -1815,58 +1808,58 @@
struct wrapper_context* wcon = context;
int rc;
- printf("Looking up '");
- fwrite(varname, name_len, 1, stdout);
- printf("[%d]' ... rc = ", index);
-
rc = (*wcon->lookup)(wcon->context, varname, name_len, index, data, data_len, buffer_size);
- printf("%d\n", rc);
if (rc == 0)
{
+ --(*wcon->rel_lookup_flag);
*data = buf;
*data_len = 0;
*buffer_size = 0;
return 1;
}
else
- {
- *(wcon->found_variables) += 1;
return rc;
- }
}
static var_rc_t input(const char *begin, const char *end,
const var_config_t *config,
const char_class_t nameclass, var_cb_t lookup,
void *lookup_context, int force_expand,
- tokenbuf_t *output, int current_index, size_t recursion_level)
+ tokenbuf_t *output, int current_index,
+ size_t recursion_level, int* rel_lookup_flag)
{
const char* p = begin;
int rc;
tokenbuf_t result;
- int found_variables;
int i;
int output_backup;
struct wrapper_context wcon;
tokenbuf_init(&result);
+ int my_rel_lookup_flag;
+ int original_rel_lookup_state;
- do {
- printf("input(): Parsing string '%s'.\n", begin);
+ if (rel_lookup_flag == NULL)
+ {
+ rel_lookup_flag = &my_rel_lookup_flag;
+ *rel_lookup_flag = 0;
+ }
+ do {
if (begin != end && config->startindex && *begin == config->startindex)
{
- printf("Found loop construct.\n");
+ original_rel_lookup_state = *rel_lookup_flag;
wcon.lookup = lookup;
wcon.context = lookup_context;
- wcon.found_variables = &found_variables;
+ wcon.rel_lookup_flag = rel_lookup_flag;
++begin;
- for (i = 0; i == 0 || found_variables; ++i)
+ for (i = 0; i == 0 || *rel_lookup_flag > original_rel_lookup_state; ++i)
{
+ *rel_lookup_flag = original_rel_lookup_state;
output_backup = output->end - output->begin;
- found_variables = 0;
- printf("Recursing with default index %d.\n", i);
- rc = input(begin, end, config, nameclass, &lookup_wrapper, &wcon, 1, output, i, recursion_level+1);
- printf("input() recursion returned %d.\n", rc);
+ printf("%d: Recursing with default index %d.\n", recursion_level, i);
+ rc = input(begin, end, config, nameclass, &lookup_wrapper, &wcon, 1, output, i, recursion_level+1, rel_lookup_flag);
+ printf("%d: input() recursion returned %d.\n", recursion_level, rc);
+ printf("%d: input(): Had %d relative lookups.\n", recursion_level, *rel_lookup_flag);
if (rc < 0)
goto error_return;
if (begin[rc] != config->endindex)
@@ -1893,7 +1886,7 @@
goto error_return;
rc = variable(begin, end, config, nameclass, lookup,
- lookup_context, force_expand, &result, current_index);
+ lookup_context, force_expand, &result, current_index, rel_lookup_flag);
if (rc > 0) {
if (!tokenbuf_append
(output, result.begin, result.end - result.begin)) {
@@ -1962,7 +1955,7 @@
/* Call the parser. */
tokenbuf_init(&output);
rc = input(input_buf, input_buf + input_len, config, nameclass,
- lookup, lookup_context, force_expand, &output, 0, 0);
+ lookup, lookup_context, force_expand, &output, 0, 0, NULL);
*result = (char *)output.begin;
*result_len = output.end - output.begin;
if (rc >= 0)
|