Changeset 5810

Show
Ignore:
Timestamp:
03/13/10 19:13:36 (3 years ago)
Author:
blogic
Message:

basic commit functions. validation and datasource access is still missing

Location:
luci2/cbi2
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • luci2/cbi2/apps/language.luci

    r5710 r5810  
    22    <luci:title>Language Selection</luci:title> 
    33    Please choose your language 
    4     <luci:section src="luci"> 
     4    <luci:section src="luci" id="lang"> 
    55        <luci:option src="lang">Language</luci:option> 
    66    </luci:section> 
  • luci2/cbi2/cbi.h

    r5791 r5810  
    33 
    44#include <string.h> 
     5#define __STRICT_ANSI__ 
    56#include <json.h> 
     7#undef __STRICT_ANSI__ 
    68#include "list.h" 
    79#include "uvl.h" 
  • luci2/cbi2/json.c

    r5791 r5810  
    44#include "session.h" 
    55 
    6 static json_object* json_req_element(struct cbi_ctx *ctx, struct cbi_element *e, json_object *q) 
     6static json_object* json_req_element(struct cbi_ctx *ctx, struct cbi_element *e, json_object *q, int depth) 
    77{ 
    88    struct list_head *p; 
     
    4444            struct cbi_element *e2 = container_of(p, struct cbi_element, list); 
    4545            if((CAPS(e2) & CAP_TEMPLATE) == 0) 
    46                 json_req_element(ctx, e2, sub); 
     46                json_req_element(ctx, e2, sub, depth + 1); 
    4747        } 
    4848        if(!container) 
     
    5151    if(!container) 
    5252    { 
    53         id = cbi_prop_find(e, "id"); 
     53        if(depth == 0) 
     54            id = e->id; 
     55        if(!id) 
     56            id = cbi_prop_find(e, "id"); 
    5457        json_object_object_add(q, id, j); 
    5558    } 
     
    6568    j = json_object_new_object(); 
    6669    source_state(ctx, CBI_START); 
    67     json_req_element(ctx, e, j); 
     70    json_req_element(ctx, e, j, 0); 
    6871    source_state(ctx, CBI_STOP); 
    6972    return j; 
    7073} 
    7174 
    72 json_object* json_req_list(struct cbi_ctx *ctx, struct list_head *l) 
     75static json_object* json_req_list(struct cbi_ctx *ctx, struct list_head *l) 
    7376{ 
    7477    struct list_head *p; 
     
    8588} 
    8689 
    87 json_object* json_req_item(struct cbi_ctx *ctx, struct list_head *l, const char *node) 
     90static json_object* json_req_item(struct cbi_ctx *ctx, struct list_head *l, const char *node) 
    8891{ 
    8992    struct cbi_element *e; 
     
    9699    j = json_object_new_object(); 
    97100    source_state(ctx, CBI_START); 
    98     json_req_element(ctx, e, j); 
     101    json_req_element(ctx, e, j, 0); 
    99102    source_state(ctx, CBI_STOP); 
    100103    return j; 
    101104} 
    102105 
    103 json_object* json_req_auth(struct cbi_ctx *ctx, json_object *in) 
     106static json_object* json_req_auth(struct cbi_ctx *ctx, json_object *in) 
    104107{ 
    105108    json_object *_pass; 
     
    124127    } 
    125128    return 0; 
     129} 
     130 
     131static struct cbi_element* json_find_element(struct cbi_element *e, const char *n) 
     132{ 
     133    struct list_head *p; 
     134    list_for_each(p, &e->elements) 
     135    { 
     136        struct cbi_element *e2 = container_of(p, struct cbi_element, list); 
     137        const char *id = cbi_prop_find(e2, "id"); 
     138        if(id) 
     139            if(!strcmp(id, n)) 
     140                return e2; 
     141    } 
     142    return 0; 
     143} 
     144 
     145static json_object* json_commit_element(struct cbi_ctx *ctx, struct cbi_element *e, json_object *in) 
     146{ 
     147    json_object *out = 0, *j; 
     148    if(!in) 
     149        return 0; 
     150    json_object_object_foreach(in, key, val) 
     151    { 
     152        struct cbi_element *e2; 
     153        const char *id; 
     154        e2 = json_find_element(e, key); 
     155        if(e2) 
     156        { 
     157            json_object *elements = json_object_object_get(val, "elements"); 
     158            id = cbi_prop_find(e2, "id"); 
     159            if(!id) 
     160                continue; 
     161            j = json_object_new_object(); 
     162            json_object_object_add(j, "commit", json_object_new_string("1")); 
     163            if(j) 
     164            { 
     165                if(!out) 
     166                    out = json_object_new_object(); 
     167                json_object_object_add(out, key, j); 
     168            } 
     169            if(elements) 
     170                elements = json_commit_element(ctx, e2, elements); 
     171            if(elements) 
     172                json_object_object_add(out, "elements", elements); 
     173        } else { 
     174            if(!out) 
     175                out = json_object_new_object(); 
     176            j = json_object_new_object(); 
     177            json_object_object_add(j, "error", json_object_new_string("1")); 
     178            json_object_object_add(j, "unknown", json_object_new_string("1")); 
     179            json_object_object_add(out, key, j); 
     180        } 
     181    } 
     182    return out; 
     183} 
     184 
     185static json_object* json_commit(struct cbi_ctx *ctx, const char *node, json_object *in) 
     186{ 
     187    struct cbi_element *root = cbi_resolv(ctx, node); 
     188    if(!root) 
     189        return 0; 
     190    return json_commit_element(ctx, root, in); 
    126191} 
    127192 
     
    139204json_object* json_request(struct cbi_ctx *ctx, json_object *in, int auth) 
    140205{ 
    141     json_object *out = 0, *get, *set, *sauth, *vals, *id; 
     206    json_object *out = 0, *get, *set, *sauth, *vals, *id, *elements; 
    142207    get = json_object_object_get(in, "get"); 
    143208    set = json_object_object_get(in, "set"); 
     
    145210    vals = json_object_object_get(in, "vals"); 
    146211    sauth = json_object_object_get(in, "sauth"); 
     212    elements = json_object_object_get(in, "elements"); 
    147213    if(get) 
    148214    { 
     
    170236    } else if(set) 
    171237    { 
    172     /*  if(!json_auth(sauth) || !auth) 
    173             cbi_foo(); 
    174     */ 
     238        if(!elements) 
     239        { 
     240            out = json_object_new_object(); 
     241            json_object_object_add(out, "commit", json_object_new_string("1")); 
     242        } else { 
     243            out = json_commit(ctx, json_object_get_string(set), elements); 
     244        } 
    175245    } 
    176246    return out; 
     
    191261    answer = strdup(json_object_to_json_string(out)); 
    192262    json_object_put(out); 
    193     json_object_put(in); 
     263    if(!is_error(in)) 
     264        json_object_put(in); 
    194265//  printf("out -> %s\n", answer); 
    195266    return answer; 
  • luci2/cbi2/json.h

    r5791 r5810  
    22#define _JSON_H__ 
    33 
     4#define __STRICT_ANSI__ 
    45#include <json.h> 
     6#undef __STRICT_ANSI__ 
    57#include "cbi.h" 
    68 
  • luci2/cbi2/lucic.c

    r5788 r5810  
    1414buffer_grow(struct blob_buf *buf, int minlen) 
    1515{ 
     16    int l = buf->buflen; 
    1617    buf->buflen += ((minlen / 256) + 1) * 256; 
    1718    buf->buf = realloc(buf->buf, buf->buflen); 
     19    memset(&((char*)buf->buf)[l], 0, buf->buflen - l); 
    1820    return !!buf->buf; 
    1921} 
  • luci2/cbi2/test.sh

    r5792 r5810  
    11#!/bin/sh 
     2./luci_cli "json|{\"get\":\"path\", \"id\":\"language.luci.lang\"}" 
     3./luci_cli "json|{\"get\":\"path\", \"id\":\"language.lang.lang\"}" 
     4./luci_cli "json|{\"set\":\"language.lang.lang\"}" 
     5./luci_cli "json|{\"set\":\"language.lang.lang\", \"elements\":{\"lang\":{\"value\":\"en\",\"elements\":{\"foo\":{\"value\":\"bar\"}}}}}" 
     6exit 0 
     7 
    28./luci_cli "json|{\"get\":\"page\"}" 
    39./luci_cli "json|{\"get\":\"page\", \"id\":\"network.lan\"}"