Changeset 5810
- Timestamp:
- 03/13/10 19:13:36 (3 years ago)
- Location:
- luci2/cbi2
- Files:
-
- 6 modified
Legend:
- Unmodified
- Added
- Removed
-
luci2/cbi2/apps/language.luci
r5710 r5810 2 2 <luci:title>Language Selection</luci:title> 3 3 Please choose your language 4 <luci:section src="luci" >4 <luci:section src="luci" id="lang"> 5 5 <luci:option src="lang">Language</luci:option> 6 6 </luci:section> -
luci2/cbi2/cbi.h
r5791 r5810 3 3 4 4 #include <string.h> 5 #define __STRICT_ANSI__ 5 6 #include <json.h> 7 #undef __STRICT_ANSI__ 6 8 #include "list.h" 7 9 #include "uvl.h" -
luci2/cbi2/json.c
r5791 r5810 4 4 #include "session.h" 5 5 6 static json_object* json_req_element(struct cbi_ctx *ctx, struct cbi_element *e, json_object *q )6 static json_object* json_req_element(struct cbi_ctx *ctx, struct cbi_element *e, json_object *q, int depth) 7 7 { 8 8 struct list_head *p; … … 44 44 struct cbi_element *e2 = container_of(p, struct cbi_element, list); 45 45 if((CAPS(e2) & CAP_TEMPLATE) == 0) 46 json_req_element(ctx, e2, sub );46 json_req_element(ctx, e2, sub, depth + 1); 47 47 } 48 48 if(!container) … … 51 51 if(!container) 52 52 { 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"); 54 57 json_object_object_add(q, id, j); 55 58 } … … 65 68 j = json_object_new_object(); 66 69 source_state(ctx, CBI_START); 67 json_req_element(ctx, e, j );70 json_req_element(ctx, e, j, 0); 68 71 source_state(ctx, CBI_STOP); 69 72 return j; 70 73 } 71 74 72 json_object* json_req_list(struct cbi_ctx *ctx, struct list_head *l)75 static json_object* json_req_list(struct cbi_ctx *ctx, struct list_head *l) 73 76 { 74 77 struct list_head *p; … … 85 88 } 86 89 87 json_object* json_req_item(struct cbi_ctx *ctx, struct list_head *l, const char *node)90 static json_object* json_req_item(struct cbi_ctx *ctx, struct list_head *l, const char *node) 88 91 { 89 92 struct cbi_element *e; … … 96 99 j = json_object_new_object(); 97 100 source_state(ctx, CBI_START); 98 json_req_element(ctx, e, j );101 json_req_element(ctx, e, j, 0); 99 102 source_state(ctx, CBI_STOP); 100 103 return j; 101 104 } 102 105 103 json_object* json_req_auth(struct cbi_ctx *ctx, json_object *in)106 static json_object* json_req_auth(struct cbi_ctx *ctx, json_object *in) 104 107 { 105 108 json_object *_pass; … … 124 127 } 125 128 return 0; 129 } 130 131 static 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 145 static 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 185 static 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); 126 191 } 127 192 … … 139 204 json_object* json_request(struct cbi_ctx *ctx, json_object *in, int auth) 140 205 { 141 json_object *out = 0, *get, *set, *sauth, *vals, *id ;206 json_object *out = 0, *get, *set, *sauth, *vals, *id, *elements; 142 207 get = json_object_object_get(in, "get"); 143 208 set = json_object_object_get(in, "set"); … … 145 210 vals = json_object_object_get(in, "vals"); 146 211 sauth = json_object_object_get(in, "sauth"); 212 elements = json_object_object_get(in, "elements"); 147 213 if(get) 148 214 { … … 170 236 } else if(set) 171 237 { 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 } 175 245 } 176 246 return out; … … 191 261 answer = strdup(json_object_to_json_string(out)); 192 262 json_object_put(out); 193 json_object_put(in); 263 if(!is_error(in)) 264 json_object_put(in); 194 265 // printf("out -> %s\n", answer); 195 266 return answer; -
luci2/cbi2/json.h
r5791 r5810 2 2 #define _JSON_H__ 3 3 4 #define __STRICT_ANSI__ 4 5 #include <json.h> 6 #undef __STRICT_ANSI__ 5 7 #include "cbi.h" 6 8 -
luci2/cbi2/lucic.c
r5788 r5810 14 14 buffer_grow(struct blob_buf *buf, int minlen) 15 15 { 16 int l = buf->buflen; 16 17 buf->buflen += ((minlen / 256) + 1) * 256; 17 18 buf->buf = realloc(buf->buf, buf->buflen); 19 memset(&((char*)buf->buf)[l], 0, buf->buflen - l); 18 20 return !!buf->buf; 19 21 } -
luci2/cbi2/test.sh
r5792 r5810 1 1 #!/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\"}}}}}" 6 exit 0 7 2 8 ./luci_cli "json|{\"get\":\"page\"}" 3 9 ./luci_cli "json|{\"get\":\"page\", \"id\":\"network.lan\"}"
