In uci_lookup_ptr and uci_set the pointer uci_ptr ptr.last is set to
the element corresponding to the first of: ptr.o, ptr.s, ptr.p.
Thus, ptr.last is redundant and in case of uci_set is (and was) not
always consistently set.
In order to simplify the code this commit removes internal usage
of ptr.last, and remove setting it from uci_set (and from uci_add_list
that was never used anyway).
As it is part of the public C api ptr.last cannot be completely
removed though. A search on lxr.openwrt.org shows that it is used as
the output of uci_lookup_ptr in several packages.
So we leave setting ptr.last in uci_lookup_ptr intact.
Signed-off-by: Jan Venekamp <jan@venekamp.net>
static int package_cmd(int cmd, char *tuple)
{
static int package_cmd(int cmd, char *tuple)
{
- struct uci_element *e = NULL;
struct uci_ptr ptr;
int ret = 1;
struct uci_ptr ptr;
int ret = 1;
switch(cmd) {
case CMD_CHANGES:
uci_show_changes(ptr.p);
switch(cmd) {
case CMD_CHANGES:
uci_show_changes(ptr.p);
cli_perror();
goto out;
}
cli_perror();
goto out;
}
- switch(e->type) {
- case UCI_TYPE_PACKAGE:
- uci_show_package(ptr.p);
- break;
- case UCI_TYPE_SECTION:
- uci_show_section(ptr.s);
- break;
- case UCI_TYPE_OPTION:
- uci_show_option(ptr.o, true);
- break;
- default:
- /* should not happen */
- goto out;
- }
+ if (ptr.o)
+ uci_show_option(ptr.o, true);
+ else if (ptr.s)
+ uci_show_section(ptr.s);
+ else if (ptr.p)
+ uci_show_package(ptr.p);
+ else
+ goto out; /* should not happen */
static int uci_do_section_cmd(int cmd, int argc, char **argv)
{
static int uci_do_section_cmd(int cmd, int argc, char **argv)
{
struct uci_ptr ptr;
int ret = UCI_OK;
int dummy;
struct uci_ptr ptr;
int ret = UCI_OK;
int dummy;
(cmd != CMD_RENAME) && (cmd != CMD_REORDER))
return 1;
(cmd != CMD_RENAME) && (cmd != CMD_REORDER))
return 1;
switch(cmd) {
case CMD_GET:
if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
switch(cmd) {
case CMD_GET:
if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) {
cli_perror();
return 1;
}
cli_perror();
return 1;
}
- switch(e->type) {
- case UCI_TYPE_SECTION:
- printf("%s\n", ptr.s->type);
- break;
- case UCI_TYPE_OPTION:
uci_show_value(ptr.o, false);
uci_show_value(ptr.o, false);
- break;
- default:
- break;
- }
- /* throw the value to stdout */
+ else if (ptr.s)
+ printf("%s\n", ptr.s->type);
break;
case CMD_RENAME:
ret = uci_rename(ctx, &ptr);
break;
case CMD_RENAME:
ret = uci_rename(ctx, &ptr);
static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p)
{
static void uci_parse_delta_line(struct uci_context *ctx, struct uci_package *p)
{
- struct uci_element *e = NULL;
struct uci_ptr ptr;
int cmd;
struct uci_ptr ptr;
int cmd;
UCI_INTERNAL(uci_del_list, ctx, &ptr);
break;
case UCI_CMD_ADD:
UCI_INTERNAL(uci_del_list, ctx, &ptr);
break;
case UCI_CMD_ADD:
+ UCI_INTERNAL(uci_set, ctx, &ptr);
+ if (!ptr.option && ptr.s)
+ ptr.s->anonymous = true;
+ break;
case UCI_CMD_CHANGE:
UCI_INTERNAL(uci_set, ctx, &ptr);
case UCI_CMD_CHANGE:
UCI_INTERNAL(uci_set, ctx, &ptr);
- e = ptr.last;
- if (!ptr.option && e && (cmd == UCI_CMD_ADD))
- uci_to_section(e)->anonymous = true;
UCI_TRAP_SAVE(ctx, error);
ptr->o = uci_alloc_list(ptr->s, ptr->option, NULL);
UCI_TRAP_RESTORE(ctx);
UCI_TRAP_SAVE(ctx, error);
ptr->o = uci_alloc_list(ptr->s, ptr->option, NULL);
UCI_TRAP_RESTORE(ctx);
- ptr->last = &ptr->o->e;
} else if (ptr->o->type == UCI_TYPE_STRING) {
/* create new list and add old string value as item to list */
struct uci_option *old = ptr->o;
} else if (ptr->o->type == UCI_TYPE_STRING) {
/* create new list and add old string value as item to list */
struct uci_option *old = ptr->o;
if (ptr->option == old->e.name)
ptr->option = ptr->o->e.name;
uci_free_option(old);
if (ptr->option == old->e.name)
ptr->option = ptr->o->e.name;
uci_free_option(old);
- ptr->last = &ptr->o->e;
}
/* add new item to list */
}
/* add new item to list */
return uci_delete(ctx, ptr);
} else if (!ptr->o && ptr->option) { /* new option */
ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value, NULL);
return uci_delete(ctx, ptr);
} else if (!ptr->o && ptr->option) { /* new option */
ptr->o = uci_alloc_option(ptr->s, ptr->option, ptr->value, NULL);
- ptr->last = &ptr->o->e;
} else if (!ptr->s && ptr->section) { /* new section */
ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section, NULL);
} else if (!ptr->s && ptr->section) { /* new section */
ptr->s = uci_alloc_section(ptr->p, ptr->value, ptr->section, NULL);
- ptr->last = &ptr->s->e;
} else if (ptr->o && ptr->option) { /* update option */
if (ptr->o->type == UCI_TYPE_STRING && !strcmp(ptr->o->v.string, ptr->value))
return 0;
} else if (ptr->o && ptr->option) { /* update option */
if (ptr->o->type == UCI_TYPE_STRING && !strcmp(ptr->o->v.string, ptr->value))
return 0;
if (ptr->option == old->e.name)
ptr->option = ptr->o->e.name;
uci_free_option(old);
if (ptr->option == old->e.name)
ptr->option = ptr->o->e.name;
uci_free_option(old);
- ptr->last = &ptr->o->e;
}
} else if (ptr->s && ptr->section) { /* update section */
if (!strcmp(ptr->s->type, ptr->value))
}
} else if (ptr->s && ptr->section) { /* update section */
if (!strcmp(ptr->s->type, ptr->value))
ptr->section = ptr->s->e.name;
uci_free_section(old);
ptr->s->package->n_section--;
ptr->section = ptr->s->e.name;
uci_free_section(old);
ptr->s->package->n_section--;
- ptr->last = &ptr->s->e;
}
} else {
UCI_THROW(ctx, UCI_ERR_INVAL);
}
} else {
UCI_THROW(ctx, UCI_ERR_INVAL);
uci_lua_get_any(lua_State *L, bool all)
{
struct uci_context *ctx;
uci_lua_get_any(lua_State *L, bool all)
{
struct uci_context *ctx;
- struct uci_element *e = NULL;
struct uci_ptr ptr;
int offset = 0;
int nret = 1;
char *s = NULL;
struct uci_ptr ptr;
int offset = 0;
int nret = 1;
char *s = NULL;
- int err = UCI_ERR_NOTFOUND;
ctx = find_context(L, &offset);
if (lookup_args(L, ctx, offset, &ptr, &s))
goto error;
ctx = find_context(L, &offset);
if (lookup_args(L, ctx, offset, &ptr, &s))
goto error;
- lookup_ptr(ctx, &ptr, NULL, true);
if (!all && !ptr.s) {
ctx->err = UCI_ERR_INVAL;
goto error;
if (!all && !ptr.s) {
ctx->err = UCI_ERR_INVAL;
goto error;
- err = UCI_OK;
- e = ptr.last;
- switch(e->type) {
- case UCI_TYPE_PACKAGE:
- uci_push_package(L, ptr.p);
- break;
- case UCI_TYPE_SECTION:
- if (all) {
- uci_push_section(L, ptr.s, -1);
- }
- else {
- lua_pushstring(L, ptr.s->type);
- lua_pushstring(L, ptr.s->e.name);
- nret++;
- }
- break;
- case UCI_TYPE_OPTION:
- uci_push_option(L, ptr.o);
- break;
- default:
- ctx->err = UCI_ERR_INVAL;
- goto error;
+ if (ptr.o) {
+ uci_push_option(L, ptr.o);
+ } else if (ptr.s) {
+ if (all) {
+ uci_push_section(L, ptr.s, -1);
+ }
+ else {
+ lua_pushstring(L, ptr.s->type);
+ lua_pushstring(L, ptr.s->e.name);
+ nret++;
+ }
+ } else {
+ uci_push_package(L, ptr.p);
- if (!err)
- return nret;