# HG changeset patch # User Mikael Berthe # Date 1121792043 -3600 # Node ID dea407d53fe671a9a8f7c0d1f4b5d5853534b441 # Parent 2362167ac0f339d0552a2128e46173b11f47209c Improve configuration file reading * Move cfg_file() to settings.c, cfg_read_file() * Optimize the function * Remove parsecfg.[ch] diff -r 2362167ac0f3 -r dea407d53fe6 mcabber/src/Makefile.am --- a/mcabber/src/Makefile.am Tue Jul 19 17:51:44 2005 +0100 +++ b/mcabber/src/Makefile.am Tue Jul 19 17:54:03 2005 +0100 @@ -2,7 +2,7 @@ mcabber_SOURCES = main.c jabglue.c jabglue.h roster.c roster.h \ commands.c commands.h compl.c compl.h \ hbuf.c hbuf.h screen.c screen.h \ - settings.c settings.h parsecfg.c parsecfg.h \ + settings.c settings.h \ hooks.c hooks.h histolog.c histolog.h \ utf8.c utf8.h utils.c utils.h list.h harddefines.h diff -r 2362167ac0f3 -r dea407d53fe6 mcabber/src/main.c --- a/mcabber/src/main.c Tue Jul 19 17:51:44 2005 +0100 +++ b/mcabber/src/main.c Tue Jul 19 17:54:03 2005 +0100 @@ -34,7 +34,6 @@ #include "jabglue.h" #include "screen.h" -#include "parsecfg.h" #include "settings.h" #include "roster.h" #include "commands.h" @@ -217,7 +216,7 @@ /* Parsing config file... */ ut_WriteLog("Parsing config file...\n"); - ret = cfg_file(configFile); + ret = cfg_read_file(configFile); if (configFile) g_free(configFile); /* Leave if there was an error in the config. file */ if (ret) diff -r 2362167ac0f3 -r dea407d53fe6 mcabber/src/parsecfg.c --- a/mcabber/src/parsecfg.c Tue Jul 19 17:51:44 2005 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "settings.h" -#include "commands.h" -#include "utils.h" -#include "screen.h" - -// cfg_file(filename) -// Read and parse config file "filename". If filename is NULL, -// try to open the configuration file at the default locations. -// -// This function comes from Cabber, and has been slightly modified. -int cfg_file(char *filename) -{ - FILE *fp; - char *buf; - char *line; - unsigned int ln = 0; - int err = 0; - - if (!filename) { - // Use default config file locations - char *home = getenv("HOME"); - if (!home) { - ut_WriteLog("Can't find home dir!\n"); - fprintf(stderr, "Can't find home dir!\n"); - return -1; - } - filename = g_new(char, strlen(home)+24); - sprintf(filename, "%s/.mcabber/mcabberrc", home); - if ((fp = fopen(filename, "r")) == NULL) { - // 2nd try... - sprintf(filename, "%s/.mcabberrc", home); - if ((fp = fopen(filename, "r")) == NULL) { - fprintf(stderr, "Cannot open config file!\n"); - return -1; - } - } - g_free(filename); - } - else if ((fp = fopen(filename, "r")) == NULL) { - perror("fopen (parsecfg.c:46)"); - return -1; - } - - // This should be fully rewritten... - buf = g_new(char, 512); - - while (fgets(buf+1, 511, fp) != NULL) { - line = buf+1; - ln++; - - while (isspace(*line)) - line++; - - while ((strlen(line) > 0) && isspace((int) line[strlen(line) - 1])) - line[strlen(line) - 1] = '\0'; - - if ((*line == '\n') || (*line == '\0') || (*line == '#')) - continue; - - if ((strchr(line, '=') != NULL)) { - while ((strlen(line) > 0) && isspace(line[strlen(line) - 1])) - line[strlen(line) - 1] = '\0'; - - if (strncmp(line, "set ", 4) && - strncmp(line, "bind ", 5) && - strncmp(line, "alias ", 6)) { - scr_LogPrint("Error in configuration file (l. %d)", ln); - err++; - continue; - } - *(--line) = '/'; - process_command(line); - } else { - scr_LogPrint("Error in configuration file (l. %d)", ln); - err++; - } - } - g_free(buf); - fclose(fp); - return err; -} diff -r 2362167ac0f3 -r dea407d53fe6 mcabber/src/parsecfg.h --- a/mcabber/src/parsecfg.h Tue Jul 19 17:51:44 2005 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,8 +0,0 @@ -#ifndef __PARSECFG_H__ -#define __PARSECFG_H__ 1 - -int cfg_file(char *filename); -char *cfg_read(char *key); -int cfg_read_int(char *key); - -#endif diff -r 2362167ac0f3 -r dea407d53fe6 mcabber/src/settings.c --- a/mcabber/src/settings.c Tue Jul 19 17:51:44 2005 +0100 +++ b/mcabber/src/settings.c Tue Jul 19 17:54:03 2005 +0100 @@ -24,6 +24,9 @@ #include #include "settings.h" +#include "commands.h" +#include "utils.h" +#include "screen.h" static GSList *option; static GSList *alias; @@ -59,6 +62,89 @@ /* -- */ +// cfg_read_file(filename) +// Read and parse config file "filename". If filename is NULL, +// try to open the configuration file at the default locations. +// +int cfg_read_file(char *filename) +{ + FILE *fp; + char *buf; + char *line, *eol; + unsigned int ln = 0; + int err = 0; + + if (!filename) { + // Use default config file locations + char *home = getenv("HOME"); + if (!home) { + ut_WriteLog("Can't find home dir!\n"); + fprintf(stderr, "Can't find home dir!\n"); + return -1; + } + filename = g_new(char, strlen(home)+24); + sprintf(filename, "%s/.mcabber/mcabberrc", home); + if ((fp = fopen(filename, "r")) == NULL) { + // 2nd try... + sprintf(filename, "%s/.mcabberrc", home); + if ((fp = fopen(filename, "r")) == NULL) { + fprintf(stderr, "Cannot open config file!\n"); + return -1; + } + } + g_free(filename); + } + else if ((fp = fopen(filename, "r")) == NULL) { + perror("fopen (cfg_file())"); + return -1; + } + + buf = g_new(char, 512); + + while (fgets(buf+1, 511, fp) != NULL) { + // The first char is reserved to add a '/', to make a command line + line = buf+1; + ln++; + + // Strip leading spaces + while (isspace(*line)) + line++; + + // Make eol point to the last char of the line + for (eol = line ; *eol ; eol++) + ; + if (eol > line) + eol--; + + // Strip trailing spaces + while (eol > line && isspace(*eol)) + *eol-- = 0; + + // Ignore empty lines and comments + if ((*line == '\n') || (*line == '\0') || (*line == '#')) + continue; + + if ((strchr(line, '=') != NULL)) { + // Only accept the set, alias and bind commands + if (strncmp(line, "set ", 4) && + strncmp(line, "bind ", 5) && + strncmp(line, "alias ", 6)) { + scr_LogPrint("Error in configuration file (l. %d): bad command", ln); + err++; + continue; + } + *(--line) = '/'; // Set the leading '/' to build a command line + process_command(line); // Process the command + } else { + scr_LogPrint("Error in configuration file (l. %d): no assignment", ln); + err++; + } + } + g_free(buf); + fclose(fp); + return err; +} + // parse_assigment(assignment, pkey, pval) // Read assignment and split it to key, value // diff -r 2362167ac0f3 -r dea407d53fe6 mcabber/src/settings.h --- a/mcabber/src/settings.h Tue Jul 19 17:51:44 2005 +0100 +++ b/mcabber/src/settings.h Tue Jul 19 17:54:03 2005 +0100 @@ -26,6 +26,7 @@ #define settings_opt_get(k) settings_get(SETTINGS_TYPE_OPTION, k) #define settings_opt_get_int(k) settings_get_int(SETTINGS_TYPE_OPTION, k) +int cfg_read_file(char *filename); guint parse_assigment(gchar *assignment, const gchar **pkey, const gchar **pval); void settings_set(guint type, const gchar *key, const gchar *value);