Index: ossp-pkg/var/var.c RCS File: /v/ossp/cvs/ossp-pkg/var/var.c,v rcsdiff -q -kk '-r1.48' '-r1.49' -u '/v/ossp/cvs/ossp-pkg/var/var.c,v' 2>/dev/null --- var.c 2001/12/13 16:25:58 1.48 +++ var.c 2001/12/14 13:42:56 1.49 @@ -446,39 +446,38 @@ static int text(const char *begin, const char *end, char varinit, char startindex, char endindex, char escape) - { +{ const char *p; - for (p = begin; p != end; ++p) - { - if (*p == escape) - { + + for (p = begin; p != end; ++p) { + if (*p == escape) { if (++p == end) return VAR_ERR_INCOMPLETE_QUOTED_PAIR; - } + } else if (*p == varinit) - { break; - } else if (startindex && (*p == startindex || *p == endindex)) - { break; - } - } - return p - begin; } + return p - begin; +} static int varname(const char *begin, const char *end, const char_class_t nameclass) { const char *p; - for (p = begin; p != end && nameclass[(int) *p]; ++p); + + for (p = begin; p != end && nameclass[(int) *p]; p++) + ; return p - begin; } static int number(const char *begin, const char *end) { const char *p; - for (p = begin; p != end && isdigit((int)*p); ++p); + + for (p = begin; p != end && isdigit((int)*p); p++) + ; return p - begin; } @@ -486,11 +485,12 @@ const var_config_t *config) { const char *p; - for (p = begin; p != end && *p != config->varinit && *p != '/'; ++p) { + + for (p = begin; p != end && *p != config->varinit && *p != '/'; p++) { if (*p == config->escape) { if (p + 1 == end) return VAR_ERR_INCOMPLETE_QUOTED_PAIR; - ++p; + p++; } } return p - begin; @@ -500,37 +500,39 @@ const var_config_t *config) { const char *p; + for (p = begin; - p != end && *p != config->varinit && *p != config->enddelim - && *p != ':'; ++p) { + p != end + && *p != config->varinit + && *p != config->enddelim + && *p != ':'; p++) { if (*p == config->escape) { if (p + 1 == end) return VAR_ERR_INCOMPLETE_QUOTED_PAIR; - ++p; + p++; } } return p - begin; } -static int num_exp_read_int(const char** begin, const char* end) - { +static int num_exp_read_int(const char **begin, const char *end) +{ int num = 0; - do - { + + do { num *= 10; num += **begin - '0'; ++(*begin); - } - while (isdigit(**begin) && *begin != end); + } while (isdigit(**begin) && *begin != end); return num; - } +} static int num_exp_read_operand(const char *begin, const char *end, int current_index, - int* result, int* failed, int* rel_lookup_flag, + 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) - { + var_cb_t lookup, void *lookup_context) +{ const char* p = begin; tokenbuf_t tmp; int rc; @@ -540,9 +542,9 @@ if (begin == end) return VAR_ERR_INCOMPLETE_INDEX_SPEC; - if (*p == '(') - { - rc = num_exp(++p, end, current_index, result, failed, rel_lookup_flag, config, nameclass, lookup, lookup_context); + if (*p == '(') { + rc = num_exp(++p, end, current_index, result, failed, + rel_lookup_flag, config, nameclass, lookup, lookup_context); if (rc < 0) return rc; p += rc; @@ -551,74 +553,68 @@ if (*p != ')') return VAR_ERR_UNCLOSED_BRACKET_IN_INDEX; ++p; - } - else if (*p == config->varinit) - { - rc = variable(p, end, config, nameclass, lookup, lookup_context, 1, &tmp, current_index, rel_lookup_flag); - if (rc == VAR_ERR_UNDEFINED_VARIABLE) - { + } + else if (*p == config->varinit) { + 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, rel_lookup_flag); + rc = variable(p, end, config, nameclass, lookup, + lookup_context, 0, &tmp, current_index, rel_lookup_flag); if (rc < 0) return rc; p += rc; *result = 0; - } - else - { + } + else { if (rc < 0) return rc; p += rc; - rc = num_exp(tmp.begin, tmp.end, current_index, result, failed, rel_lookup_flag, 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; - } } - else if (config->current_index && *p == config->current_index) - { - ++p; + } + else if (config->current_index && *p == config->current_index) { + p++; *result = current_index; - ++(*rel_lookup_flag); - } - else if (isdigit(*p)) - { + (*rel_lookup_flag)++; + } + else if (isdigit(*p)) { *result = num_exp_read_int(&p, end); - } - else if (*p == '+') - { - if (end - p > 1 && isdigit(p[1])) - { - ++p; + } + else if (*p == '+') { + if (end - p > 1 && isdigit(p[1])) { + p++; *result = num_exp_read_int(&p, end); - } + } else return VAR_ERR_INVALID_CHAR_IN_INDEX_SPEC; - } - else if (*p == '-') - { - if (end - p > 1 && isdigit(p[1])) - { - ++p; + } + else if (*p == '-') { + if (end - p > 1 && isdigit(p[1])) { + p++; *result = num_exp_read_int(&p, end); *result = 0 - *result; - } + } else return VAR_ERR_INVALID_CHAR_IN_INDEX_SPEC; - } + } else return VAR_ERR_INVALID_CHAR_IN_INDEX_SPEC; return p - begin; - } +} static int num_exp(const char *begin, const char *end, int current_index, - int* result, int* failed, int* rel_lookup_flag, + 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) - { - const char* p = begin; + var_cb_t lookup, void *lookup_context) +{ + const char *p = begin; char operator; int right; int rc; @@ -626,17 +622,18 @@ if (begin == end) return VAR_ERR_INCOMPLETE_INDEX_SPEC; - rc = num_exp_read_operand(p, end, current_index, result, failed, rel_lookup_flag, 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; - while (p != end) - { - if (*p == '+' || *p == '-') - { + while (p != end) { + if (*p == '+' || *p == '-') { operator = *p++; - rc = num_exp(p, end, current_index, &right, failed, rel_lookup_flag, 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; @@ -644,55 +641,49 @@ *result = *result + right; else *result = *result - right; - } - else if (*p == '*' || *p == '/' || *p == '%') - { + } + else if (*p == '*' || *p == '/' || *p == '%') { operator = *p++; - rc = num_exp_read_operand(p, end, current_index, &right, failed, rel_lookup_flag, 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; - if (operator == '*') - { + if (operator == '*') { *result = *result * right; - } - else if (operator == '/') - { - if (right == 0) - { + } + else if (operator == '/') { + if (right == 0) { if (*failed) *result = 0; else return VAR_ERR_DIVISION_BY_ZERO_IN_INDEX; - } + } else *result = *result / right; - } - else if (operator == '%') - { - if (right == 0) - { + } + else if (operator == '%') { + if (right == 0) { if (*failed) *result = 0; else return VAR_ERR_DIVISION_BY_ZERO_IN_INDEX; - } + } else *result = *result % right; - } } + } else break; - } - - return p - begin; } + return p - begin; +} static int expression(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) + tokenbuf_t *result, int current_index, int *rel_lookup_flag) { const char *p = begin; const char *data; @@ -720,157 +711,138 @@ /* Get the name of the variable to expand. The name may consist of an arbitrary number of VARNAMEs and VARIABLEs. */ - do - { + do { rc = varname(p, end, nameclass); if (rc < 0) goto error_return; - if (rc > 0) - { - if (!tokenbuf_append(&name, p, rc)) - { + if (rc > 0) { + if (!tokenbuf_append(&name, p, rc)) { rc = VAR_ERR_OUT_OF_MEMORY; goto error_return; - } - p += rc; } + p += rc; + } rc = variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &tmp, current_index, rel_lookup_flag); if (rc < 0) goto error_return; - if (rc > 0) - { - if (!tokenbuf_append(&name, tmp.begin, tmp.end - tmp.begin)) - { + if (rc > 0) { + if (!tokenbuf_append(&name, tmp.begin, tmp.end - tmp.begin)) { rc = VAR_ERR_OUT_OF_MEMORY; goto error_return; - } - p += rc; } + p += rc; } - while (rc > 0); + } while (rc > 0); /* We must have the complete variable name now, so make sure we do. */ - if (name.begin == name.end) - { + if (name.begin == name.end) { rc = VAR_ERR_INCOMPLETE_VARIABLE_SPEC; goto error_return; - } + } /* If the next token is START-INDEX, read the index specification. */ - if (config->startindex && *p == config->startindex) - { - rc = num_exp(++p, end, current_index, &index, &failed, rel_lookup_flag, config, nameclass, lookup, lookup_context); + if (config->startindex && *p == config->startindex) { + 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) - { + if (rc == 0) { rc = VAR_ERR_INCOMPLETE_INDEX_SPEC; goto error_return; - } + } p += rc; - if (p == end) - { + if (p == end) { rc = VAR_ERR_INCOMPLETE_INDEX_SPEC; goto error_return; - } - if (*p != config->endindex) - { + } + if (*p != config->endindex) { rc = VAR_ERR_INVALID_CHAR_IN_INDEX_SPEC; goto error_return; - } - ++p; } + p++; + } /* Now we have the name of the variable stored in "name". The next token here must either be an END-DELIM or a ':'. */ - if (p == end || (*p != config->enddelim && *p != ':')) - { + if (p == end || (*p != config->enddelim && *p != ':')) { rc = VAR_ERR_INCOMPLETE_VARIABLE_SPEC; goto error_return; - } - ++p; + } + p++; /* Use the lookup callback to get the variable's contents. */ - if (failed) - { + if (failed) { result->begin = begin - 1; result->end = p; result->buffer_size = 0; - } - else - { + } + else { 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 (rc == 0) { /* The variable is undefined. What we'll do now depends on the force_expand flag. */ - - if (force_expand) - { + 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 - { + } + 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] == ':') - { + if (p[-1] == ':') { /* Parse and execute commands. */ tokenbuf_free(&tmp); - --p; - while (p != end && *p == ':') - { - ++p; + p--; + while (p != end && *p == ':') { + p++; if (!failed) rc = command(p, end, config, nameclass, lookup, - lookup_context, force_expand, result, current_index, rel_lookup_flag); + 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, rel_lookup_flag); + lookup_context, force_expand, &tmp, + current_index, rel_lookup_flag); if (rc < 0) goto error_return; p += rc; if (failed) result->end += rc; - } + } - if (p == end || *p != config->enddelim) - { + if (p == end || *p != config->enddelim) { rc = VAR_ERR_INCOMPLETE_VARIABLE_SPEC; goto error_return; - } - ++p; - if (failed) - ++result->end; } + p++; + if (failed) + result->end++; + } /* Exit gracefully. */ @@ -885,13 +857,13 @@ tokenbuf_free(&tmp); tokenbuf_free(result); return rc; - } +} 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) + int *rel_lookup_flag) { const char *p = begin; const char *data; @@ -918,7 +890,7 @@ if (rc < 0) return rc; if (rc > 0) { - rc2 = (*lookup) (lookup_context, p, rc, 0, &data, &len, &buffer_size); + rc2 = (*lookup)(lookup_context, p, rc, 0, &data, &len, &buffer_size); if (rc2 < 0) return rc2; if (rc2 == 0) { @@ -940,7 +912,7 @@ rc = expression(p, end, config, nameclass, lookup, lookup_context, force_expand, result, current_index, rel_lookup_flag); if (rc > 0) - ++rc; + rc++; return rc; } @@ -949,7 +921,7 @@ 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) + int *rel_lookup_flag) { const char *p = begin; tokenbuf_t tmp; @@ -985,8 +957,7 @@ goto error_return; } } - } - while (rc > 0); + } while (rc > 0); tokenbuf_free(&tmp); return p - begin; @@ -1002,7 +973,7 @@ 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) + int *rel_lookup_flag) { const char *p = begin; tokenbuf_t tmp; @@ -1038,8 +1009,7 @@ goto error_return; } } - } - while (rc > 0); + } while (rc > 0); tokenbuf_free(&tmp); return p - begin; @@ -1050,24 +1020,24 @@ return rc; } - static int expand_class_description(tokenbuf_t *src, tokenbuf_t *dst) { unsigned char c, d; const char *p = src->begin; + while (p != src->end) { if ((src->end - p) >= 3 && p[1] == '-') { if (*p > p[2]) return VAR_ERR_INCORRECT_TRANSPOSE_CLASS_SPEC; for (c = *p, d = p[2]; c <= d; ++c) { - if (!tokenbuf_append(dst, (char *) &c, 1)) + if (!tokenbuf_append(dst, (char *)&c, 1)) return VAR_ERR_OUT_OF_MEMORY; } p += 3; } else { if (!tokenbuf_append(dst, p, 1)) return VAR_ERR_OUT_OF_MEMORY; - ++p; + p++; } } return VAR_OK; @@ -1100,8 +1070,7 @@ if (data->buffer_size == 0) { tokenbuf_t tmp; - if (!tokenbuf_assign - (&tmp, data->begin, data->end - data->begin)) { + if (!tokenbuf_assign(&tmp, data->begin, data->end - data->begin)) { rc = VAR_ERR_OUT_OF_MEMORY; goto error_return; } @@ -1134,8 +1103,11 @@ { tokenbuf_t res; const char *p; - size_t num1 = tokenbuf_toint(number1); - size_t num2 = tokenbuf_toint(number2); + size_t num1; + size_t num2; + + num1 = tokenbuf_toint(number1); + num2 = tokenbuf_toint(number2); /* Determine begin of result string. */ @@ -1149,7 +1121,8 @@ if (num2 == 0) { if (!tokenbuf_assign(&res, p, data->end - p)) return VAR_ERR_OUT_OF_MEMORY; - } else { /* OK, then use num2. */ + } else { + /* OK, then use num2. */ if (isrange) { if ((p + num2) > data->end) return VAR_ERR_RANGE_OUT_OF_BOUNDS; @@ -1183,13 +1156,13 @@ tokenbuf_free(expanded); return VAR_ERR_INCOMPLETE_QUOTED_PAIR; } - ++p; + p++; if (*p == '\\') { if (!tokenbuf_append(expanded, p, 1)) { tokenbuf_free(expanded); return VAR_ERR_OUT_OF_MEMORY; } - ++p; + p++; continue; } if (!isdigit((int)*p)) { @@ -1197,7 +1170,7 @@ return VAR_ERR_UNKNOWN_QUOTED_PAIR_IN_REPLACE; } i = *p - '0'; - ++p; + p++; if (pmatch[i].rm_so == -1) { tokenbuf_free(expanded); return VAR_ERR_SUBMATCH_OUT_OF_RANGE; @@ -1212,7 +1185,7 @@ tokenbuf_free(expanded); return VAR_ERR_OUT_OF_MEMORY; } - ++p; + p++; } } @@ -1258,7 +1231,8 @@ else rc = strncmp(p, search->begin, search->end - search->begin); - if (rc != 0) { /* no match, copy character */ + if (rc != 0) { + /* no match, copy character */ if (!tokenbuf_append(&tmp, p, 1)) { tokenbuf_free(&tmp); return VAR_ERR_OUT_OF_MEMORY; @@ -1266,7 +1240,7 @@ ++p; } else { tokenbuf_append(&tmp, replace->begin, - replace->end - replace->begin); + replace->end - replace->begin); p += search->end - search->begin; if (!global) { if (!tokenbuf_append(&tmp, p, data->end - p)) { @@ -1291,19 +1265,16 @@ /* Copy the pattern and the data to our own buffer to make sure they're terminated with a null byte. */ - if (!tokenbuf_assign - (&tmp, search->begin, search->end - search->begin)) + if (!tokenbuf_assign(&tmp, search->begin, search->end - search->begin)) return VAR_ERR_OUT_OF_MEMORY; - if (!tokenbuf_assign - (&mydata, data->begin, data->end - data->begin)) { + if (!tokenbuf_assign(&mydata, data->begin, data->end - data->begin)) { tokenbuf_free(&tmp); return VAR_ERR_OUT_OF_MEMORY; } /* Compile the pattern. */ - rc = regcomp(&preg, tmp.begin, - REG_EXTENDED | ((case_insensitive) ? REG_ICASE : 0)); + rc = regcomp(&preg, tmp.begin, REG_EXTENDED|((case_insensitive)?REG_ICASE:0)); tokenbuf_free(&tmp); if (rc != 0) { tokenbuf_free(&mydata); @@ -1313,7 +1284,7 @@ /* Match the pattern and create the result string in the tmp buffer. */ - for (p = mydata.begin; p != mydata.end;) { + for (p = mydata.begin; p != mydata.end; ) { if (p == mydata.begin || p[-1] == '\n') regexec_flag = 0; else @@ -1379,10 +1350,10 @@ if (!tokenbuf_append (data, fill->begin, fill->end - fill->begin)) return VAR_ERR_OUT_OF_MEMORY; - --i; + i--; } - i = (width - (data->end - data->begin)) % (fill->end - - fill->begin); + i = (width - (data->end - data->begin)) + % (fill->end - fill->begin); if (!tokenbuf_append(data, fill->begin, i)) return VAR_ERR_OUT_OF_MEMORY; } @@ -1396,16 +1367,15 @@ tokenbuf_free(&result); return VAR_ERR_OUT_OF_MEMORY; } - --i; + i--; } - i = (width - (data->end - data->begin)) % (fill->end - - fill->begin); + i = (width - (data->end - data->begin)) + % (fill->end - fill->begin); if (!tokenbuf_append(&result, fill->begin, i)) { tokenbuf_free(&result); return VAR_ERR_OUT_OF_MEMORY; } - if (!tokenbuf_append - (&result, data->begin, data->end - data->begin)) { + if (!tokenbuf_append(&result, data->begin, data->end - data->begin)) { tokenbuf_free(&result); return VAR_ERR_OUT_OF_MEMORY; } @@ -1420,15 +1390,14 @@ i = i / (fill->end - fill->begin); while (i > 0) { - if (!tokenbuf_append - (&result, fill->begin, fill->end - fill->begin)) { + if (!tokenbuf_append(&result, fill->begin, fill->end - fill->begin)) { tokenbuf_free(&result); return VAR_ERR_OUT_OF_MEMORY; } - --i; + i--; } - i = ((width - (data->end - data->begin)) / 2) % (fill->end - - fill->begin); + i = ((width - (data->end - data->begin)) / 2) + % (fill->end - fill->begin); if (!tokenbuf_append(&result, fill->begin, i)) { tokenbuf_free(&result); return VAR_ERR_OUT_OF_MEMORY; @@ -1436,8 +1405,7 @@ /* Append the actual data string. */ - if (!tokenbuf_append - (&result, data->begin, data->end - data->begin)) { + if (!tokenbuf_append(&result, data->begin, data->end - data->begin)) { tokenbuf_free(&result); return VAR_ERR_OUT_OF_MEMORY; } @@ -1452,7 +1420,7 @@ tokenbuf_free(&result); return VAR_ERR_OUT_OF_MEMORY; } - --i; + i--; } i = width - (result.end - result.begin); if (!tokenbuf_append(&result, fill->begin, i)) { @@ -1473,7 +1441,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, int* rel_lookup_flag) + tokenbuf_t *data, int current_index, int *rel_lookup_flag) { const char *p = begin; tokenbuf_t tmptokbuf; @@ -1500,16 +1468,15 @@ we have to copy it before modifying the contents. */ if (data->buffer_size == 0) { - if (!tokenbuf_assign - (data, data->begin, data->end - data->begin)) { + if (!tokenbuf_assign(data, data->begin, data->end - data->begin)) { rc = VAR_ERR_OUT_OF_MEMORY; goto error_return; } } - for (ptr = (char *) data->begin; ptr != data->end; ++ptr) + for (ptr = (char *)data->begin; ptr != data->end; ++ptr) *ptr = tolower(*ptr); } - ++p; + p++; break; case 'u': /* Turn data to uppercase. */ @@ -1577,7 +1544,7 @@ break; case '-': /* Substitute parameter if data is empty. */ - ++p; + p++; rc = exptext_or_variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &tmptokbuf, current_index, rel_lookup_flag); @@ -1595,7 +1562,7 @@ break; case '*': /* Return "" if data is not empty, parameter otherwise. */ - ++p; + p++; rc = exptext_or_variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &tmptokbuf, current_index, rel_lookup_flag); if (rc < 0) @@ -1618,7 +1585,7 @@ break; case '+': /* Substitute parameter if data is not empty. */ - ++p; + p++; rc = exptext_or_variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &tmptokbuf, current_index, rel_lookup_flag); if (rc < 0) @@ -1635,11 +1602,11 @@ break; case 's': /* Search and replace. */ - ++p; + p++; if (*p != '/') return VAR_ERR_MALFORMATTED_REPLACE; - ++p; + p++; rc = substext_or_variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &search, current_index, rel_lookup_flag); @@ -1651,7 +1618,7 @@ rc = VAR_ERR_MALFORMATTED_REPLACE; goto error_return; } - ++p; + p++; rc = substext_or_variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &replace, current_index, rel_lookup_flag); @@ -1663,7 +1630,7 @@ rc = VAR_ERR_MALFORMATTED_REPLACE; goto error_return; } - ++p; + p++; rc = exptext(p, end, config); if (rc < 0) @@ -1681,11 +1648,11 @@ break; case 'y': /* Transpose characters from class A to class B. */ - ++p; + p++; if (*p != '/') return VAR_ERR_MALFORMATTED_TRANSPOSE; - ++p; + p++; rc = substext_or_variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &search, current_index, rel_lookup_flag); @@ -1697,7 +1664,7 @@ rc = VAR_ERR_MALFORMATTED_TRANSPOSE; goto error_return; } - ++p; + p++; rc = substext_or_variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &replace, current_index, rel_lookup_flag); @@ -1720,11 +1687,11 @@ case 'p': /* Padding. */ - ++p; + p++; if (*p != '/') return VAR_ERR_MALFORMATTED_PADDING; - ++p; + p++; rc = number(p, end); if (rc == 0) { @@ -1740,7 +1707,7 @@ rc = VAR_ERR_MALFORMATTED_PADDING; goto error_return; } - ++p; + p++; rc = substext_or_variable(p, end, config, nameclass, lookup, lookup_context, force_expand, &replace, current_index, rel_lookup_flag); @@ -1752,13 +1719,13 @@ rc = VAR_ERR_MALFORMATTED_PADDING; goto error_return; } - ++p; + p++; if (*p != 'l' && *p != 'c' && *p != 'r') { rc = VAR_ERR_MALFORMATTED_PADDING; goto error_return; } - ++p; + p++; if (data->begin) { rc = padding(data, &number1, &replace, p[-1]); @@ -1792,43 +1759,42 @@ return rc; } -struct wrapper_context - { +struct wrapper_context { var_cb_t lookup; - void* context; - int* rel_lookup_flag; - }; + void *context; + int *rel_lookup_flag; +}; static int lookup_wrapper(void *context, const char *varname, size_t name_len, int index, const char **data, size_t *data_len, size_t *buffer_size) - { +{ static char buf[1]; - struct wrapper_context* wcon = context; + struct wrapper_context *wcon = context; int rc; - rc = (*wcon->lookup)(wcon->context, varname, name_len, index, data, data_len, buffer_size); - if (rc == 0) - { - --(*wcon->rel_lookup_flag); + rc = (*wcon->lookup)(wcon->context, varname, name_len, + index, data, data_len, buffer_size); + if (rc == 0) { + (*wcon->rel_lookup_flag)--; *data = buf; *data_len = 0; *buffer_size = 0; return 1; - } + } else 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, int* rel_lookup_flag) + size_t recursion_level, int *rel_lookup_flag) { - const char* p = begin; + const char *p = begin; int rc; tokenbuf_t result; int i; @@ -1836,45 +1802,44 @@ struct wrapper_context wcon; int my_rel_lookup_flag; int original_rel_lookup_state; + tokenbuf_init(&result); - if (rel_lookup_flag == NULL) - { + 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) - { + if (begin != end && config->startindex && *begin == config->startindex) { original_rel_lookup_state = *rel_lookup_flag; wcon.lookup = lookup; wcon.context = lookup_context; wcon.rel_lookup_flag = rel_lookup_flag; - ++begin; - for (i = 0; i == 0 || *rel_lookup_flag > original_rel_lookup_state; ++i) - { + begin++; + 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; 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); + 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) - { + if (begin[rc] != config->endindex) { rc = VAR_ERR_UNTERMINATED_LOOP_CONSTRUCT; goto error_return; - } } + } output->end = output->begin + output_backup; begin += rc; - ++begin; + begin++; continue; - } + } - rc = text(begin, end, config->varinit, config->startindex, config->endindex, config->escape); + rc = text(begin, end, config->varinit, config->startindex, + config->endindex, config->escape); if (rc > 0) { if (!tokenbuf_append(output, begin, rc)) { rc = VAR_ERR_OUT_OF_MEMORY; @@ -1886,10 +1851,10 @@ goto error_return; rc = variable(begin, end, config, nameclass, lookup, - lookup_context, force_expand, &result, current_index, rel_lookup_flag); + lookup_context, force_expand, &result, + current_index, rel_lookup_flag); if (rc > 0) { - if (!tokenbuf_append - (output, result.begin, result.end - result.begin)) { + if (!tokenbuf_append(output, result.begin, result.end - result.begin)) { rc = VAR_ERR_OUT_OF_MEMORY; goto error_return; } @@ -1898,8 +1863,7 @@ } if (rc < 0) goto error_return; - } - while (begin != end && rc > 0); + } while (begin != end && rc > 0); if (recursion_level == 0 && begin != end) { rc = VAR_ERR_INPUT_ISNT_TEXT_NOR_VARIABLE; @@ -1958,18 +1922,16 @@ lookup, lookup_context, force_expand, &output, 0, 0, NULL); *result = (char *)output.begin; *result_len = output.end - output.begin; - if (rc >= 0) - { - if (!tokenbuf_append(&output, "\0", 1)) - { + if (rc >= 0) { + if (!tokenbuf_append(&output, "\0", 1)) { tokenbuf_free(&output); return VAR_ERR_OUT_OF_MEMORY; - } - --output.end; - rc = VAR_OK; } - + output.end--; + rc = VAR_OK; + } *result = (char *)output.begin; *result_len = output.end - output.begin; return rc; } +