示例#1
0
int _config_del(config_t *cfg, const char *section, const char *entry)
{
        int start;
        char *tmp, *value;
        unsigned int line = 0, end;

        if ( ! entry ) {
                start = search_section(cfg, section, 0);
                if ( start < 0 )
                        return start;

                for ( end = (unsigned int) start + 1; end < cfg->elements && ! is_section(cfg->content[end]); end++ );

                while ( start >= 2 && ! *cfg->content[start - 1] && ! *cfg->content[start - 2] )
                        start--;

        } else {
                start = search_entry(cfg, section, entry, &line, &tmp, &value);
                if ( start < 0 )
                        return start;

                free_val(&tmp);
                free_val(&value);

                end = (unsigned int) start + 1;
        }

        cfg->need_sync = TRUE;
        return op_delete_line(cfg, start, end);
}
示例#2
0
/**
 * _config_get_section:
 * @cfg: Configuration file identifier.
 * @section: Section we are searching.
 * @line: Pointer to a line number we should start the search at.
 *
 * If @section is found, @line is updated to reflect
 * the line where the section was found.
 *
 * Returns: 0 if the section was found, -1 otherwise.
 */
int _config_get_section(config_t *cfg, const char *section, unsigned int *line)
{
        int ret;

        if ( ! cfg->content )
                return -1;

        ret = search_section(cfg, section, *line);
        if ( ret < 0 )
                return -1;

        *line = ret;

        return 0;
}
示例#3
0
static int new_section_line(config_t *cfg, const char *section,
                            const char *entry, const char *val, unsigned int *index)
{
        int ret;
        char *eout, *vout;
        unsigned int eindex;

        ret = search_section(cfg, section, *index);
        if ( ret < 0 ) {
                char buf[1024];

                snprintf(buf, sizeof(buf), "[%s]", section);

                if ( *index )
                        *index = adjust_insertion_point(cfg, *index);
                else
                        *index = cfg->elements;

                ret = op_insert_line(cfg, strdup(buf), *index);
                if ( ret < 0 )
                        return ret;

                return (! entry) ? 0 : op_insert_line(cfg, create_new_line(entry, val), *index + 1);
        }

        *index = ret;
        if ( ! entry )
                return 0;

        eindex = *index + 1;

        ret = search_entry(cfg, section, entry, &eindex, &eout, &vout);
        if ( ret < 0 )
                return op_insert_line(cfg, create_new_line(entry, val), eindex);

        free_val(&eout);
        free_val(&vout);

        op_modify_line(&cfg->content[eindex], create_new_line(entry, val));

        return 0;
}
示例#4
0
/*
 * Search an entry (delimited by '=' character) in content.
 * returns the line number matching 'entry' or -1.
 */
static int search_entry(config_t *cfg, const char *section,
                        const char *entry, unsigned int *index, char **eout, char **vout)
{
        int ret;
        unsigned int i = *index;

        if ( ! cfg->content || i >= cfg->elements )
                return -1;

        if ( section && ! index ) {

                ret = search_section(cfg, section, 0);
                if ( ret < 0 )
                        return ret;

                i = (unsigned int) ret + 1;
        }

        for ( ; i < cfg->elements; i++ ) {

                if ( section && is_section(cfg->content[i]) )
                        return -1;

                ret = parse_buffer(cfg->content[i], eout, vout);
                if ( ret < 0 || ! *eout )
                        continue;

                ret = strcmp(entry, *eout);
                if ( ret == 0 ) {
                        *index = i;
                        return 0;
                }

                free_val(eout);
                free_val(vout);
        }

        return -1;
}
示例#5
0
void load_conf(void)
{
   FILE *fc;
   char line[128];
   char *p, *q, **tmp;
   int lineno = 0;
   size_t tmplen;
   struct conf_entry *curr_section = NULL;
   void *value = NULL;

   /* initialize the structures */
   init_structures();
   
   DEBUG_MSG("load_conf");
  
   /* the user has specified an alternative config file */
   if (GBL_CONF->file) {
      DEBUG_MSG("load_conf: alternative config: %s", GBL_CONF->file);
      fc = fopen(GBL_CONF->file, FOPEN_READ_TEXT);
      ON_ERROR(fc, NULL, "Cannot open %s", GBL_CONF->file);
   } else {
      /* errors are handled by the function */
      fc = open_data("etc", ETTER_CONF, FOPEN_READ_TEXT);
      ON_ERROR(fc, NULL, "Cannot open %s", ETTER_CONF);
   }
  
   /* read the file */
   while (fgets(line, 128, fc) != 0) {
      
      /* update the line count */
      lineno++;
      
      /* trim out the comments */
      if ((p = strchr(line, '#')))
         *p = '\0';
      
      /* trim out the new line */
      if ((p = strchr(line, '\n')))
         *p = '\0';

      q = line;
      
      /* trim the initial spaces */
      while (q < line + sizeof(line) && *q == ' ')
         q++;
      
      /* skip empty lines */
      if (line[0] == '\0' || *q == '\0')
         continue;
      
      /* here starts a new section [...] */
      if (*q == '[') {
         
         /* remove the square brackets */
         if ((p = strchr(line, ']')))
            *p = '\0';
         else
            FATAL_ERROR("Missing ] in %s line %d", ETTER_CONF, lineno);
         
         p = q + 1;
         
         DEBUG_MSG("load_conf: SECTION: %s", p);

         /* get the pointer to the right structure */
         if ( (curr_section = search_section(p)) == NULL)
            FATAL_ERROR("Invalid section in %s line %d", ETTER_CONF, lineno);
         
         /* read the next line */
         continue;
      }
   
      /* variable outside a section */
      if (curr_section == NULL)
         FATAL_ERROR("Entry outside a section in %s line %d", ETTER_CONF, lineno);
      
      /* sanity check */
      if (!strchr(q, '='))
         FATAL_ERROR("Parse error %s line %d", ETTER_CONF, lineno);
      
      p = q;

      /* split the entry name from the value */
      do {
         if (*p == ' ' || *p == '='){
            *p = '\0';
            break;
         }
      } while (p++ < line + sizeof(line) );
      
      /* move p to the value */
      p++;
      do {
         if (*p != ' ' && *p != '=')
            break;
      } while (p++ < line + sizeof(line) );
      
      /* 
       * if it is the "dissector" section,
       * do it in a different way
       */
      if (curr_section == (struct conf_entry *)&dissectors) {
         set_dissector(q, p, lineno);
         continue;
      }
     
      /* search the entry name */
      if ( (value = search_entry(curr_section, q)) == NULL)
         FATAL_ERROR("Invalid entry in %s line %d", ETTER_CONF, lineno);
   
      /* strings must be handled in a different way */
      if (curr_section == (struct conf_entry *)&strings) {
         /* trim the quotes */
         if (*p == '"')
            p++;
         
         /* set the string value */ 
         tmp = (char **)value;
         *tmp = strdup(p);
         
         /* trim the ending quotes */
         p = *tmp;
         tmplen = strlen(*tmp);
         do {
            if (*p == '"') {
               *p = 0;
               break;
            }
         } while (p++ < *tmp + tmplen );
         
         DEBUG_MSG("load_conf: \tENTRY: %s  [%s]", q, *tmp);
      } else {
         /* set the integer value */ 
         *(int *)value = strtol(p, (char **)NULL, 10);
         DEBUG_MSG("load_conf: \tENTRY: %s  %d", q, *(int *)value);
      }
   }

   sanity_checks();
   fclose(fc);
}