Changeset ab3215d849aeac6f677f2f106be98aad4dc8bd50

Show
Ignore:
Timestamp:
02/13/08 03:27:22 (6 years ago)
Author:
Theo Schlossnagle <jesus@omniti.com>
git-committer:
Theo Schlossnagle <jesus@omniti.com> 1202873242 +0000
git-parent:

[666f3b3ec6db879445b66943258a56a195e0af41]

git-author:
Theo Schlossnagle <jesus@omniti.com> 1202873242 +0000
Message:

support creating new sections

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/noit_conf.c

    r666f3b3 rab3215d  
    307307  noit_console_state_init(ncct); 
    308308  return 0; 
     309} 
     310 
     311static int 
     312noit_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; 
    309369} 
    310370 
     
    789849  ADD_CMD(_conf_t_state, "ls", noit_console_config_show, NULL, NULL); 
    790850  ADD_CMD(_conf_t_state, "cd", noit_console_config_cd, NULL, NULL); 
     851  ADD_CMD(_conf_t_state, "section", noit_console_config_section, NULL, NULL); 
    791852  DELEGATE_CMD(_conf_t_state, "write", _write_state); 
    792853  DELEGATE_CMD(_conf_t_state, "attribute", _attr_state);