"Fossies" - the Fresh Open Source Software Archive

Member "xtermcontrol-3.8/src/configuration.c" (9 Dec 2018, 8029 Bytes) of package /linux/privat/xtermcontrol-3.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "configuration.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.4_vs_3.5.

    1 /****************************************************************************
    2 ** $Id: configuration.c,v 1.17 2002/10/26 17:20:36 jet Exp $
    3 **
    4 ** Copyright (C) 2002-2013 Jess Thrysoee <jess@thrysoee.dk>
    5 **
    6 ** This program is free software; you can redistribute it and/or modify
    7 ** it under the terms of the GNU General Public License as published by
    8 ** the Free Software Foundation; either version 2 of the License, or
    9 ** (at your option) any later version.
   10 **
   11 ***************************************************************************  */
   12 
   13 #include <assert.h>
   14 #include <regex.h>
   15 #include <stdio.h>
   16 #include <stdlib.h>
   17 #include <string.h>
   18 #include <sys/types.h>
   19 
   20 #include "configuration.h"
   21 
   22 static void configuration_add(configuration *list, const char *keyword, const char *value);
   23 static void do_regerror(int errcode, const regex_t *preg);
   24 
   25 /*=****************************************************************************
   26 **
   27 ** DESCRIPTION :
   28 **
   29 ** RETURN VALUE:
   30 **                                                                           */
   31 /*=***************************************************************************/
   32 void configuration_init(configuration *list)
   33 {
   34     list->n_elements = 0;
   35     list->first = NULL;
   36 }
   37 
   38 /*=****************************************************************************
   39 **
   40 ** DESCRIPTION :
   41 **
   42 ** RETURN VALUE:
   43 **                                                                           */
   44 /*=***************************************************************************/
   45 int configuration_read(configuration *list, const char *filepath)
   46 {
   47     int errcode;
   48     char temp[BUFSIZ];
   49     size_t matchlen;
   50 
   51     FILE *stream;
   52     char *value,
   53         *p;
   54     char *keyword;
   55 
   56     regex_t preg;
   57     regmatch_t *pmatch;
   58     const char *regex;
   59 
   60     stream = fopen(filepath, "r");
   61     if (stream == NULL)
   62     {
   63         return -1;
   64     }
   65 
   66     /* 
   67     * match: 'keyword=value'
   68     * and  : 'keyword = value'
   69     * and  : '"key word" = value'
   70     * and  : 'keyword = value # comment'
   71     * and  : 'keyword = value1 value2 # comment'
   72     * and  : 'keyword = #ffffff'
   73     * not  : '#keyword = value'
   74     */
   75     regex = "^[ \t\"']*([^#]*[^ \t\"'#])[ \t\"']*=[ \t\"']*(#?[^\"'#]*)";
   76     errcode = regcomp(&preg, regex, REG_EXTENDED | REG_NEWLINE);
   77     if (errcode)
   78     {
   79         do_regerror(errcode, &preg);
   80         return -1;
   81     }
   82     pmatch = (regmatch_t *)malloc(sizeof(regmatch_t) * (preg.re_nsub + 1));
   83     if (!pmatch)
   84     {
   85         fprintf(stderr, "out of memory\n");
   86         exit(8);
   87     }
   88 
   89     while (fgets(temp, sizeof(temp), stream) != NULL)
   90     {
   91         /* fgets keeps the newline - delete it */
   92         if ((p = strchr(temp, '\n')) != NULL)
   93         {
   94             *p = '\0';
   95         }
   96         errcode = regexec(&preg, temp, (preg.re_nsub + 1), pmatch, 0);
   97         if (errcode)
   98         {
   99             /* line did not match */
  100             continue;
  101         }
  102         /* ignore pmatch[0] which is the entire match */
  103 
  104         /* keyword */
  105         if (pmatch[1].rm_so != -1)
  106         {
  107             matchlen = pmatch[1].rm_eo - pmatch[1].rm_so;
  108             keyword = (char *)malloc(matchlen + 1);
  109             if (!keyword)
  110             {
  111                 fprintf(stderr, "out of memory\n");
  112                 exit(8);
  113             }
  114             strncpy(keyword, temp + pmatch[1].rm_so, matchlen);
  115             keyword[matchlen] = '\0';
  116         }
  117         else
  118         {
  119             fprintf(stderr, "missing keyword: %s\n", temp);
  120             continue;
  121         }
  122         /* value */
  123         if (pmatch[2].rm_so != -1)
  124         {
  125             matchlen = pmatch[2].rm_eo - pmatch[2].rm_so;
  126             value = (char *)malloc(matchlen + 1);
  127             if (!value)
  128             {
  129                 fprintf(stderr, "out of memory\n");
  130                 exit(8);
  131             }
  132             strncpy(value, temp + pmatch[2].rm_so, matchlen);
  133             value[matchlen] = '\0';
  134         }
  135         else
  136         {
  137             fprintf(stderr, "missing value: %s\n", temp);
  138             free(keyword);
  139             keyword = NULL;
  140             continue;
  141         }
  142         configuration_add(list, keyword, value);
  143 
  144         free(keyword);
  145         keyword = NULL;
  146         free(value);
  147         value = NULL;
  148     }
  149 
  150     free(pmatch);
  151     regfree(&preg);
  152     fclose(stream);
  153 
  154     return 0;
  155 }
  156 
  157 /*=****************************************************************************
  158 **
  159 ** DESCRIPTION :
  160 **
  161 ** RETURN VALUE:
  162 **                                                                           */
  163 /*=***************************************************************************/
  164 static void configuration_add(configuration *list, const char *keyword, const char *value)
  165 {
  166     configuration_element *new_element;
  167 
  168     /* check if keyword already exists */
  169     new_element = (configuration_element *)configuration_find(list, keyword);
  170     if (new_element)
  171     {
  172         if (new_element->value)
  173         {
  174             free(new_element->value);
  175             new_element->value = NULL;
  176         }
  177     }
  178     else
  179     {
  180         /* allocate mem for the struct */
  181         new_element = (configuration_element *)malloc(sizeof(configuration_element));
  182         if (!new_element)
  183         {
  184             fprintf(stderr, "out of memory\n");
  185             exit(8);
  186         }
  187 
  188         /* allocate mem for the keyword */
  189         new_element->keyword = (char *)malloc(strlen(keyword) + 1);
  190         if (new_element->keyword)
  191         {
  192             strcpy(new_element->keyword, keyword);
  193         }
  194         else
  195         {
  196             fprintf(stderr, "out of memory\n");
  197             exit(8);
  198         }
  199 
  200         /* link */
  201         new_element->next = list->first;
  202         list->first = new_element;
  203         list->n_elements++;
  204     }
  205 
  206     /* allocate mem for the value */
  207     new_element->value = (char *)malloc(strlen(value) + 1);
  208     if (new_element->value)
  209     {
  210         strcpy(new_element->value, value);
  211     }
  212     else
  213     {
  214         fprintf(stderr, "out of memory\n");
  215         exit(8);
  216     }
  217 }
  218 
  219 /*=****************************************************************************
  220 **
  221 ** DESCRIPTION :
  222 **
  223 ** RETURN VALUE:
  224 **                                                                           */
  225 /*=***************************************************************************/
  226 void configuration_free(configuration *list)
  227 {
  228     configuration_element *lp,
  229         *lp_next;
  230 
  231     for (lp_next = list->first; lp_next != NULL;)
  232     {
  233         lp = lp_next;
  234         lp_next = lp_next->next;
  235         if (lp->keyword)
  236         {
  237             free(lp->keyword);
  238             lp->keyword = NULL;
  239         }
  240         if (lp->value)
  241         {
  242             free(lp->value);
  243             lp->value = NULL;
  244         }
  245         free(lp);
  246     }
  247     list->first = NULL;
  248     list->n_elements = 0;
  249 }
  250 
  251 /*=****************************************************************************
  252 **
  253 ** DESCRIPTION :
  254 **
  255 ** RETURN VALUE:
  256 **                                                                           */
  257 /*=***************************************************************************/
  258 const configuration_element *configuration_find(configuration *list, const char *keyword)
  259 {
  260     configuration_element *lp = NULL;
  261 
  262     if (!keyword)
  263     {
  264         return NULL;
  265     }
  266 
  267     for (lp = list->first; lp != NULL; lp = lp->next)
  268     {
  269         if (strcmp(lp->keyword, keyword) == 0)
  270         {
  271             break;
  272         }
  273     }
  274 
  275     return lp;
  276 }
  277 
  278 /*=****************************************************************************
  279 **
  280 ** DESCRIPTION :
  281 **
  282 ** RETURN VALUE:
  283 **                                                                           */
  284 /*=***************************************************************************/
  285 static void do_regerror(int errcode, const regex_t *preg)
  286 {
  287     char *errbuf;
  288     size_t errbuf_size;
  289 
  290     errbuf_size = regerror(errcode, preg, NULL, 0);
  291     errbuf = (char *)malloc(errbuf_size);
  292     if (!errbuf)
  293     {
  294         fprintf(stderr, "out of memory\n");
  295         exit(8);
  296     }
  297     regerror(errcode, preg, errbuf, errbuf_size);
  298     fprintf(stderr, "%s\n", errbuf);
  299 
  300     free(errbuf);
  301 }