Changeset 5696
- Timestamp:
- 02/28/10 21:32:14 (3 years ago)
- Location:
- luci2/cbi2
- Files:
-
- 1 added
- 7 modified
Legend:
- Unmodified
- Added
- Removed
-
luci2/cbi2/cbi.c
r5695 r5696 53 53 INIT_LIST_HEAD(&ctx->globals); 54 54 INIT_LIST_HEAD(&ctx->menus); 55 INIT_LIST_HEAD(&ctx->elements); 55 56 56 57 return ctx; … … 277 278 void cbi_create_instance(struct cbi_ctx *ctx, struct cbi_element *e1, struct cbi_element *e2) 278 279 { 279 struct blob_attr *element = (struct blob_attr*)e2->blob->blob;280 int rem = e2->blob->rem;280 struct blob_attr *element; 281 int rem; 281 282 int sub_count = 0; 283 if(!e2->blob) 284 { 285 CBILOG(e2, "failed to create instance\n"); 286 return; 287 } 288 element = (struct blob_attr*)e2->blob->blob; 289 rem = e2->blob->rem; 282 290 for(; 283 291 (blob_pad_len(element) <= rem) && (blob_pad_len(element) >= sizeof(struct blob_attr)); … … 316 324 } 317 325 326 static struct cbi_element* cbi_resolv_element(struct cbi_element *e, char *ref) 327 { 328 char *tok; 329 struct cbi_element *next; 330 tok = strstr(ref, "."); 331 if(!tok) 332 { 333 const char *id = cbi_find_prop(e, "id"); 334 if(!strcmp(id, ref)) 335 return e; 336 else 337 return 0; 338 } 339 *tok = '\0'; 340 tok++; 341 next = cbi_find_element(e, tok); 342 if(next) 343 return cbi_resolv_element(next, tok); 344 return 0; 345 } 346 347 struct cbi_element* cbi_resolv(struct cbi_ctx *ctx, const char *ref) 348 { 349 struct cbi_element *ret = 0; 350 struct list_head *p; 351 list_for_each(p, &ctx->elements) 352 { 353 struct cbi_element *e2 = container_of(p, struct cbi_element, list); 354 char *buf = strdup(ref); 355 ret = cbi_resolv_element(e2, buf); 356 free(buf); 357 if(ret) 358 break; 359 } 360 return ret; 361 } 362 318 363 void cbi_index_verify(struct cbi_ctx *ctx, struct cbi_element *e) 319 364 { … … 335 380 struct list_head *p; 336 381 LOG("indexing %s\n", f->name); 337 list_for_each(p, &f->e .elements)382 list_for_each(p, &f->e->elements) 338 383 { 339 384 struct cbi_element *e = container_of(p, struct cbi_element, list); … … 362 407 struct list_head *p; 363 408 LOG("unindexing %s\n", f->name); 364 list_for_each(p, &f->e .elements)409 list_for_each(p, &f->e->elements) 365 410 { 366 411 struct cbi_element *e = container_of(p, struct cbi_element, list); … … 403 448 cbi_index_remove(file); 404 449 LOG("dropping %s\n", file->name); 405 list_for_each_safe(p, q, &file->e .elements)450 list_for_each_safe(p, q, &file->e->elements) 406 451 { 407 452 struct cbi_element *e = container_of(p, struct cbi_element, list); … … 411 456 free(file->file); 412 457 list_del(&file->list); 458 list_del(&file->e->list); 413 459 free(file); 414 460 } … … 476 522 f->name[strlen(f->name) - 5] = '\0'; 477 523 f->mtime = st.st_mtime; 478 memset(&f->e, 0, sizeof(struct cbi_element)); 479 INIT_LIST_HEAD(&f->e.elements); 480 INIT_LIST_HEAD(&f->e.properties); 481 f->e.widget = strdup("file"); 482 f->e.w = cbi_widget_find("file"); 483 cbi_add_property(&f->e, "src", f->name, 1); 524 f->e = malloc(sizeof(struct cbi_element)); 525 memset(f->e, 0, sizeof(struct cbi_element)); 526 INIT_LIST_HEAD(&f->e->elements); 527 INIT_LIST_HEAD(&f->e->properties); 528 f->e->namespace = strdup("luci"); 529 f->e->widget = strdup("file"); 530 f->e->w = cbi_widget_find("file"); 531 cbi_add_property(f->e, "src", f->name, 1); 484 532 blob_for_each_attr(pos, map, rem) 485 533 { … … 487 535 if(blob_id(pos) != CBI_ELEMENT) 488 536 continue; 489 e = cbi_parse_element(ctx, pos, &count, &f->e);537 e = cbi_parse_element(ctx, pos, &count, 0); 490 538 if(e) 491 list_add_tail(&e->list, &f->e.elements); 492 } 539 list_add_tail(&e->list, &f->e->elements); 540 } 541 list_add_tail(&f->e->list, &ctx->elements); 493 542 list_add_tail(&f->list, &ctx->files); 494 543 if(index) … … 557 606 void cbi_dump(struct cbi_ctx *ctx) 558 607 { 559 struct list_head *p, *q; 560 561 list_for_each(p, &ctx->files) 562 { 563 struct cbi_file *f = container_of(p, struct cbi_file, list); 564 list_for_each(q, &f->e.elements) 565 { 566 struct cbi_element *e = container_of(q, struct cbi_element, list); 567 cbi_dump_element(e, 0); 568 } 608 struct list_head *p; 609 610 list_for_each(p, &ctx->elements) 611 { 612 struct cbi_element *e = container_of(p, struct cbi_element, list); 613 cbi_dump_element(e, 0); 569 614 } 570 615 } -
luci2/cbi2/cbi.h
r5695 r5696 3 3 4 4 #include <string.h> 5 #include <json.h> 5 6 #include "list.h" 6 7 #include "uvl.h" … … 26 27 struct list_head pages; 27 28 struct list_head menus; 29 struct list_head elements; 28 30 struct list_head sock_handlers; 29 31 struct uvl_context *uvl; … … 58 60 uint64_t caps; 59 61 int (*init)(struct cbi_ctx *ctx, struct cbi_element *e); 62 json_object* (*json)(struct cbi_ctx *ctx, struct cbi_element *e); 60 63 }; 61 64 … … 88 91 int prop_count; 89 92 int (*init)(struct cbi_ctx *ctx, struct cbi_element *e); 93 json_object* (*json)(struct cbi_ctx *ctx, struct cbi_element *e); 90 94 }; 91 95 … … 121 125 { 122 126 struct list_head list; 123 struct cbi_element e;127 struct cbi_element *e; 124 128 125 129 char *name; … … 140 144 const char* cbi_virt_path(struct cbi_ctx *ctx, const char *path, const char *file); 141 145 void cbi_create_instance(struct cbi_ctx *ctx, struct cbi_element *e1, struct cbi_element *e2); 146 struct cbi_element* cbi_resolv(struct cbi_ctx *ctx, const char *ref); 142 147 143 148 void cbi_widget_register(char *name, struct cbi_widget *w); -
luci2/cbi2/json.c
r5684 r5696 3 3 #include "cbi.h" 4 4 #include "session.h" 5 6 static json_object* json_req_element(struct cbi_ctx *ctx, struct cbi_element *e) 7 { 8 struct list_head *p; 9 json_object *j = 0; 10 if(e->broken) 11 return 0; 12 if((CAPS(e) & CAP_DATA) && (e->source)) 13 if(e->source->json) 14 j = e->source->json(ctx, e); 15 if(!j && e->w->json) 16 j = e->w->json(ctx, e); 17 if(!j) 18 { 19 CBILOG(e, "no json handler found\n"); 20 e->broken = 1; 21 return 0; 22 } 23 if(!list_empty(&e->elements)) 24 { 25 json_object *array = json_object_new_array(); 26 list_for_each(p, &e->elements) 27 { 28 struct cbi_element *e2 = container_of(p, struct cbi_element, list); 29 json_object *j2 = json_req_element(ctx, e2); 30 if(j2) 31 json_object_array_add(array, j2); 32 } 33 json_object_object_add(j, "elements", array); 34 } 35 return j; 36 } 37 38 json_object* json_req_node(struct cbi_ctx *ctx, const char *node) 39 { 40 struct cbi_element *e = cbi_resolv(ctx, node); 41 if(!e) 42 return 0; 43 return json_req_element(ctx, e); 44 } 5 45 6 46 json_object* json_req_auth(struct cbi_ctx *ctx, json_object *in) -
luci2/cbi2/json.h
r5682 r5696 6 6 7 7 json_object* json_request(struct cbi_ctx *ctx, json_object *in); 8 json_object* json_req_node(struct cbi_ctx *ctx, const char *node); 8 9 9 10 #endif -
luci2/cbi2/luci.c
r5686 r5696 21 21 #include "lang.h" 22 22 #include "uci.h" 23 #include "json.h" 23 24 24 25 static int loop = 1; … … 170 171 u_sock_register_handler(ctx, "json", cbi_request); 171 172 } 173 { 174 json_object *j = json_req_node(ctx, "network.internet"); 175 if(j) 176 printf("res -> %s\n", json_object_to_json_string(j)); 177 else 178 printf("error creating json\n"); 179 } 172 180 while(loop) 173 181 { -
luci2/cbi2/sources/uci.c
r5694 r5696 201 201 if(p->package && p->type && p->option) 202 202 p->uvl = validate_get_option(ctx, p->package, p->type, p->option); 203 else204 printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__);205 206 203 if(!p->uvl) 207 204 CBILOG(e, "failed to load uvl mapping to %s.@%s.%s\n", p->package, p->type, p->option); 208 205 return 0; 206 } 207 208 json_object* uci_src_json(struct cbi_ctx *ctx, struct cbi_element *e) 209 { 210 json_object *j = json_object_new_object(); 211 const char *id = cbi_find_prop(e, "id"); 212 json_object_object_add(j, "id", json_object_new_string(id)); 213 return j; 209 214 } 210 215 … … 223 228 .caps = SRC_PACKAGE | SCAP_READ | SCAP_READ, 224 229 .init = uci_src_init, 230 .json = uci_src_json, 225 231 }; 226 232 -
luci2/cbi2/widget.c
r5692 r5696 55 55 } 56 56 57 static struct cbi_element* cbi_get_root(struct cbi_element *e)58 {59 if(e->parent)60 return cbi_get_root(e->parent);61 return e;62 }63 64 struct cbi_element* cbi_find_element(struct cbi_element *e, const char *id);65 static struct cbi_element* cbi_resolv_element(struct cbi_element *e, char *ref)66 {67 char *tok;68 struct cbi_element *next;69 tok = strstr(ref, ".");70 if(!tok)71 return e;72 *tok = '\0';73 tok++;74 next = cbi_find_element(e, tok);75 if(next)76 return cbi_resolv_element(next, tok);77 return 0;78 }79 80 struct cbi_file* cbi_find_file(struct cbi_ctx *ctx, const char *name);81 struct cbi_element* cbi_resolv(struct cbi_ctx *ctx, struct cbi_element *e, const char *ref)82 {83 struct cbi_element *root = 0, *ret = 0;84 char *buf = strdup(ref);85 char *t;86 if((t = strstr(buf, ":")))87 {88 struct cbi_file *f;89 *t = '\0';90 f = cbi_find_file(ctx, buf);91 if(!f)92 goto out;93 root = &f->e;94 t++;95 ret = cbi_resolv_element(root, t);96 } else if(e)97 {98 root = cbi_get_root(e);99 ret = cbi_resolv_element(root, buf);100 }101 out:102 free(buf);103 return ret;104 }105 106 57 static int widget_abstract(struct cbi_ctx *ctx, struct cbi_element *e) 107 58 { … … 110 61 if(!ref) 111 62 return 0; 112 r = cbi_resolv(ctx, e,ref);63 r = cbi_resolv(ctx, ref); 113 64 if(!r) 114 65 {
