| | 309 | } |
|---|
| | 310 | |
|---|
| | 311 | static int |
|---|
| | 312 | noit_console_config_section(noit_console_closure_t ncct, |
|---|
| | 313 | int argc, char **argv, |
|---|
| | 314 | noit_console_state_t *state, void *closure) { |
|---|
| | 315 | const char *err = "internal error"; |
|---|
| | 316 | char *path, xpath[1024]; |
|---|
| | 317 | noit_conf_t_userdata_t *info; |
|---|
| | 318 | xmlXPathObjectPtr pobj = NULL; |
|---|
| | 319 | xmlNodePtr node = NULL, newnode; |
|---|
| | 320 | |
|---|
| | 321 | if(argc != 1) { |
|---|
| | 322 | nc_printf(ncct, "requires one argument\n"); |
|---|
| | 323 | return -1; |
|---|
| | 324 | } |
|---|
| | 325 | if(strchr(argv[0], '/')) { |
|---|
| | 326 | nc_printf(ncct, "invalid section name\n"); |
|---|
| | 327 | return -1; |
|---|
| | 328 | } |
|---|
| | 329 | if(!strcmp(argv[0], "check")) { |
|---|
| | 330 | nc_printf(ncct, "use 'check' to create checks\n"); |
|---|
| | 331 | return -1; |
|---|
| | 332 | } |
|---|
| | 333 | info = noit_console_userdata_get(ncct, NOIT_CONF_T_USERDATA); |
|---|
| | 334 | if(!strcmp(info->path, "/")) { |
|---|
| | 335 | nc_printf(ncct, "creation of new toplevel section disallowed\n"); |
|---|
| | 336 | return -1; |
|---|
| | 337 | } |
|---|
| | 338 | |
|---|
| | 339 | snprintf(xpath, sizeof(xpath), "/noit%s/%s", info->path, argv[0]); |
|---|
| | 340 | pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); |
|---|
| | 341 | if(!pobj || pobj->type != XPATH_NODESET || |
|---|
| | 342 | !xmlXPathNodeSetIsEmpty(pobj->nodesetval)) { |
|---|
| | 343 | err = "cannot create section"; |
|---|
| | 344 | goto bad; |
|---|
| | 345 | } |
|---|
| | 346 | if(pobj) xmlXPathFreeObject(pobj); |
|---|
| | 347 | |
|---|
| | 348 | path = strcmp(path, "/") ? info->path : ""; |
|---|
| | 349 | snprintf(xpath, sizeof(xpath), "/noit%s", path); |
|---|
| | 350 | pobj = xmlXPathEval((xmlChar *)xpath, xpath_ctxt); |
|---|
| | 351 | if(!pobj || pobj->type != XPATH_NODESET || |
|---|
| | 352 | xmlXPathNodeSetGetLength(pobj->nodesetval) != 1) { |
|---|
| | 353 | err = "path invalid?"; |
|---|
| | 354 | goto bad; |
|---|
| | 355 | } |
|---|
| | 356 | node = (noit_conf_section_t)xmlXPathNodeSetItem(pobj->nodesetval, 0); |
|---|
| | 357 | if((newnode = xmlNewChild(node, NULL, (xmlChar *)argv[0], NULL)) != NULL) |
|---|
| | 358 | info->path = strdup((char *)xmlGetNodePath(newnode) + strlen("/noit")); |
|---|
| | 359 | else { |
|---|
| | 360 | err = "failed to create section"; |
|---|
| | 361 | goto bad; |
|---|
| | 362 | } |
|---|
| | 363 | if(pobj) xmlXPathFreeObject(pobj); |
|---|
| | 364 | return 0; |
|---|
| | 365 | bad: |
|---|
| | 366 | if(pobj) xmlXPathFreeObject(pobj); |
|---|
| | 367 | nc_printf(ncct, "%s\n", err); |
|---|
| | 368 | return -1; |
|---|