Changeset 5696

Show
Ignore:
Timestamp:
02/28/10 21:32:14 (3 years ago)
Author:
blogic
Message:

make a global elements index, cleanup cbi_element->e, cleanup cbi_resolv, generate first json

Location:
luci2/cbi2
Files:
1 added
7 modified

Legend:

Unmodified
Added
Removed
  • luci2/cbi2/cbi.c

    r5695 r5696  
    5353    INIT_LIST_HEAD(&ctx->globals); 
    5454    INIT_LIST_HEAD(&ctx->menus); 
     55    INIT_LIST_HEAD(&ctx->elements); 
    5556 
    5657    return ctx; 
     
    277278void cbi_create_instance(struct cbi_ctx *ctx, struct cbi_element *e1, struct cbi_element *e2) 
    278279{ 
    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; 
    281282    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; 
    282290    for(; 
    283291        (blob_pad_len(element) <= rem) && (blob_pad_len(element) >= sizeof(struct blob_attr)); 
     
    316324} 
    317325 
     326static 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 
     347struct 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 
    318363void cbi_index_verify(struct cbi_ctx *ctx, struct cbi_element *e) 
    319364{ 
     
    335380    struct list_head *p; 
    336381    LOG("indexing %s\n", f->name); 
    337     list_for_each(p, &f->e.elements) 
     382    list_for_each(p, &f->e->elements) 
    338383    { 
    339384        struct cbi_element *e = container_of(p, struct cbi_element, list); 
     
    362407    struct list_head *p; 
    363408    LOG("unindexing %s\n", f->name); 
    364     list_for_each(p, &f->e.elements) 
     409    list_for_each(p, &f->e->elements) 
    365410    { 
    366411        struct cbi_element *e = container_of(p, struct cbi_element, list); 
     
    403448    cbi_index_remove(file); 
    404449    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) 
    406451    { 
    407452        struct cbi_element *e = container_of(p, struct cbi_element, list); 
     
    411456    free(file->file); 
    412457    list_del(&file->list); 
     458    list_del(&file->e->list); 
    413459    free(file); 
    414460} 
     
    476522    f->name[strlen(f->name) - 5] = '\0'; 
    477523    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); 
    484532    blob_for_each_attr(pos, map, rem) 
    485533    { 
     
    487535        if(blob_id(pos) != CBI_ELEMENT) 
    488536            continue; 
    489         e = cbi_parse_element(ctx, pos, &count, &f->e); 
     537        e = cbi_parse_element(ctx, pos, &count, 0); 
    490538        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); 
    493542    list_add_tail(&f->list, &ctx->files); 
    494543    if(index) 
     
    557606void cbi_dump(struct cbi_ctx *ctx) 
    558607{ 
    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); 
    569614    } 
    570615} 
  • luci2/cbi2/cbi.h

    r5695 r5696  
    33 
    44#include <string.h> 
     5#include <json.h> 
    56#include "list.h" 
    67#include "uvl.h" 
     
    2627    struct list_head pages; 
    2728    struct list_head menus; 
     29    struct list_head elements; 
    2830    struct list_head sock_handlers; 
    2931    struct uvl_context *uvl; 
     
    5860    uint64_t caps; 
    5961    int (*init)(struct cbi_ctx *ctx, struct cbi_element *e); 
     62    json_object* (*json)(struct cbi_ctx *ctx, struct cbi_element *e); 
    6063}; 
    6164 
     
    8891    int prop_count; 
    8992    int (*init)(struct cbi_ctx *ctx, struct cbi_element *e); 
     93    json_object* (*json)(struct cbi_ctx *ctx, struct cbi_element *e); 
    9094}; 
    9195 
     
    121125{ 
    122126    struct list_head list; 
    123     struct cbi_element e; 
     127    struct cbi_element *e; 
    124128 
    125129    char *name; 
     
    140144const char* cbi_virt_path(struct cbi_ctx *ctx, const char *path, const char *file); 
    141145void cbi_create_instance(struct cbi_ctx *ctx, struct cbi_element *e1, struct cbi_element *e2); 
     146struct cbi_element* cbi_resolv(struct cbi_ctx *ctx, const char *ref); 
    142147 
    143148void cbi_widget_register(char *name, struct cbi_widget *w); 
  • luci2/cbi2/json.c

    r5684 r5696  
    33#include "cbi.h" 
    44#include "session.h" 
     5 
     6static 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 
     38json_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} 
    545 
    646json_object* json_req_auth(struct cbi_ctx *ctx, json_object *in) 
  • luci2/cbi2/json.h

    r5682 r5696  
    66 
    77json_object* json_request(struct cbi_ctx *ctx, json_object *in); 
     8json_object* json_req_node(struct cbi_ctx *ctx, const char *node); 
    89 
    910#endif 
  • luci2/cbi2/luci.c

    r5686 r5696  
    2121#include "lang.h" 
    2222#include "uci.h" 
     23#include "json.h" 
    2324 
    2425static int loop = 1; 
     
    170171        u_sock_register_handler(ctx, "json", cbi_request); 
    171172    } 
     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    } 
    172180    while(loop) 
    173181    { 
  • luci2/cbi2/sources/uci.c

    r5694 r5696  
    201201    if(p->package && p->type && p->option) 
    202202        p->uvl = validate_get_option(ctx, p->package, p->type, p->option); 
    203     else 
    204         printf("%s:%s[%d]\n", __FILE__, __func__, __LINE__); 
    205  
    206203    if(!p->uvl) 
    207204        CBILOG(e, "failed to load uvl mapping to %s.@%s.%s\n", p->package, p->type, p->option); 
    208205    return 0; 
     206} 
     207 
     208json_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; 
    209214} 
    210215 
     
    223228    .caps = SRC_PACKAGE | SCAP_READ | SCAP_READ, 
    224229    .init = uci_src_init, 
     230    .json = uci_src_json, 
    225231}; 
    226232 
  • luci2/cbi2/widget.c

    r5692 r5696  
    5555} 
    5656 
    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  
    10657static int widget_abstract(struct cbi_ctx *ctx, struct cbi_element *e) 
    10758{ 
     
    11061    if(!ref) 
    11162        return 0; 
    112     r = cbi_resolv(ctx, e, ref); 
     63    r = cbi_resolv(ctx, ref); 
    11364    if(!r) 
    11465    {