]> ToastFreeware Gitweb - gregoa/bti.git/blobdiff - config.c
get_string*: fix a memory leak
[gregoa/bti.git] / config.c
index 63b10c2037083d6009e8843d8ffdda3e5aa35e3d..e06f480bc87078a5585e373a035634042332ee9a 100644 (file)
--- a/config.c
+++ b/config.c
@@ -140,7 +140,7 @@ static int get_key(struct session *session, char *line, char **key, char **value
                 * the value.
                 */
        }
                 * the value.
                 */
        }
-       printf("%s = %s\n", *key, *value);
+       /* printf("%s = %s\n", *key, *value); */
        return 0;
 }
 
        return 0;
 }
 
@@ -295,7 +295,7 @@ static struct config_function config_table[] = {
        { NULL, NULL }
 };
 
        { NULL, NULL }
 };
 
-void process_line(struct session *session, char *key, char *value)
+static void process_line(struct session *session, char *key, char *value)
 {
        struct config_function *item;
        int result;
 {
        struct config_function *item;
        int result;
@@ -309,7 +309,10 @@ void process_line(struct session *session, char *key, char *value)
                        break;
 
                if (strncasecmp(item->key, key, strlen(item->key)) == 0) {
                        break;
 
                if (strncasecmp(item->key, key, strlen(item->key)) == 0) {
-                       printf("calling %p, for key = '%s' and value = '%s'\n", item->callback, key, value);
+                       /*
+                        * printf("calling %p, for key = '%s' and value = * '%s'\n",
+                        *        item->callback, key, value);
+                        */
                        result = item->callback(session, value);
                        if (!result)
                                return;
                        result = item->callback(session, value);
                        if (!result)
                                return;
@@ -324,8 +327,9 @@ void bti_parse_configfile(struct session *session)
        char *line = NULL;
        char *key = NULL;
        char *value = NULL;
        char *line = NULL;
        char *key = NULL;
        char *value = NULL;
+       char *hashmarker;
        size_t len = 0;
        size_t len = 0;
-       size_t n;
+       ssize_t n;
        char *c;
 
        config_file = fopen(session->configfile, "r");
        char *c;
 
        config_file = fopen(session->configfile, "r");
@@ -341,8 +345,29 @@ void bti_parse_configfile(struct session *session)
                if (line[n - 1] == '\n')
                        line[n - 1] = '\0';
 
                if (line[n - 1] == '\n')
                        line[n - 1] = '\0';
 
-               /* '#' is comment markers, like bash style */
-               *strchrnul(line, '#') = '\0';
+               /*
+                * '#' is comment markers, like bash style but it is a valid
+                * character in some fields, so only treat it as a comment
+                * marker if it occurs at the beginning of the line, or after
+                * whitespace
+                */
+               hashmarker = strchrnul(line, '#');
+               if (line == hashmarker)
+                       line[0] = '\0';
+               else {
+                       while (hashmarker[0] != '\0') {
+                               --hashmarker;
+                               if (isblank(hashmarker[0]))
+                                       hashmarker[0] = '\0';
+                               else {
+                                       /*
+                                        * false positive; '#' occured
+                                        * within a string
+                                        */
+                                       hashmarker = strchrnul(hashmarker+2, '#');
+                               }
+                       }
+               }
                c = line;
                while (isspace(*c))
                        c++;
                c = line;
                while (isspace(*c))
                        c++;