* Lookup a variable by name, creating it if need be. * Caller is expected to assign a value to the variable. * Returns NULL on failure (bad name). */
| 1602 | * Returns NULL on failure (bad name). |
| 1603 | */ |
| 1604 | static Variable * |
| 1605 | lookupCreateVariable(CState *st, const char *context, char *name) |
| 1606 | { |
| 1607 | Variable *var; |
| 1608 | |
| 1609 | var = lookupVariable(st, name); |
| 1610 | if (var == NULL) |
| 1611 | { |
| 1612 | Variable *newvars; |
| 1613 | |
| 1614 | /* |
| 1615 | * Check for the name only when declaring a new variable to avoid |
| 1616 | * overhead. |
| 1617 | */ |
| 1618 | if (!valid_variable_name(name)) |
| 1619 | { |
| 1620 | pg_log_error("%s: invalid variable name: \"%s\"", context, name); |
| 1621 | return NULL; |
| 1622 | } |
| 1623 | |
| 1624 | /* Create variable at the end of the array */ |
| 1625 | if (st->variables) |
| 1626 | newvars = (Variable *) pg_realloc(st->variables, |
| 1627 | (st->nvariables + 1) * sizeof(Variable)); |
| 1628 | else |
| 1629 | newvars = (Variable *) pg_malloc(sizeof(Variable)); |
| 1630 | |
| 1631 | st->variables = newvars; |
| 1632 | |
| 1633 | var = &newvars[st->nvariables]; |
| 1634 | |
| 1635 | var->name = pg_strdup(name); |
| 1636 | var->svalue = NULL; |
| 1637 | /* caller is expected to initialize remaining fields */ |
| 1638 | |
| 1639 | st->nvariables++; |
| 1640 | /* we don't re-sort the array till we have to */ |
| 1641 | st->vars_sorted = false; |
| 1642 | } |
| 1643 | |
| 1644 | return var; |
| 1645 | } |
| 1646 | |
| 1647 | /* Assign a string value to a variable, creating it if need be */ |
| 1648 | /* Returns false on failure (bad name) */ |
no test coverage detected