Avoid dangling buffer. Eliminate some redundant code.

SVN revision: 24583
This commit is contained in:
Kim Woelders 2006-08-12 10:33:47 +00:00
parent 11d01dbd4d
commit be11563150
3 changed files with 88 additions and 146 deletions

View File

@ -52,20 +52,6 @@ SkipTillEnd(FILE * ConfigFile)
}
}
static int
IsWhitespace(const char *s)
{
int i = 0;
while (s[i])
{
if ((s[i] != ' ') && (s[i] != '\n') && (s[i] != '\t'))
return 0;
i++;
}
return 1;
}
#define LINE_BUFFER_SIZE 1024
/*
* This function will get a single line from the file
@ -75,9 +61,19 @@ IsWhitespace(const char *s)
char *
GetLine(char *s, int size, FILE * f)
{
char *si, *so, ch, quote, escape;
static char *buffer = NULL;
static char *bufptr = NULL;
static const char *bufptr = NULL;
char *so, ch, quote, escape;
const char *si;
size_t nr;
if (buffer == NULL)
{
buffer = Emalloc(LINE_BUFFER_SIZE);
if (buffer == NULL)
return NULL;
buffer[LINE_BUFFER_SIZE - 1] = '\0';
}
si = bufptr;
so = s;
@ -85,83 +81,74 @@ GetLine(char *s, int size, FILE * f)
escape = '\0';
for (;;)
{
if (buffer == NULL)
{
buffer = Emalloc(LINE_BUFFER_SIZE);
if (buffer == NULL)
return NULL;
}
/* Get a line from the input file */
if (si == NULL)
{
si = fgets(buffer, LINE_BUFFER_SIZE - 1, f);
buffer[LINE_BUFFER_SIZE - 1] = '\0';
if (si == NULL)
{
/* EOF */
Efree(buffer);
buffer = bufptr = NULL;
return NULL;
}
nr = fread(buffer, 1, LINE_BUFFER_SIZE - 1, f);
if (nr == 0)
break;
buffer[nr] = '\0';
si = buffer;
}
/* Split on ';' or '\n', handle quoting */
for (; si;)
ch = *si++;
switch (ch)
{
ch = *si++;
switch (ch)
{
case '\0':
si = NULL;
if (so == s) /* Skip empty lines */
break;
goto case_eol;
case ';': /* Line separator */
if (escape || quote)
goto case_char;
case '\n':
if (so == s) /* Skip empty lines */
break;
case_eol:
*so = '\0'; /* Terminate and return */
goto done;
case '\r': /* Ignore */
break;
case '\\': /* Escape */
if (escape)
goto case_char;
escape = ch;
break;
case '"': /* Quoting */
case '\0':
si = NULL;
break;
case ';': /* Line separator */
if (escape || quote)
goto case_char;
case '\n':
if (so == s) /* Skip empty lines */
break;
*so = '\0'; /* Terminate and return */
goto done;
case '\r': /* Ignore */
break;
case '\\': /* Escape */
if (escape)
goto case_char;
escape = ch;
break;
case '"': /* Quoting */
/* case '\'': */
if (escape)
goto case_char;
if (quote == '\0')
quote = ch;
else if (quote == ch)
quote = '\0';
else
goto case_char;
break;
case ' ': /* Whitespace */
case '\t':
if (so == s) /* Skip leading whitespace */
break;
case_char: /* Normal character */
default:
*so++ = ch;
escape = '\0';
if (--size > 1)
break;
*so = '\0';
goto done;
}
if (escape)
goto case_char;
if (quote == '\0')
quote = ch;
else if (quote == ch)
quote = '\0';
else
goto case_char;
break;
case ' ': /* Whitespace */
case '\t':
if (so == s) /* Skip leading whitespace */
break;
case_char: /* Normal character */
default:
*so++ = ch;
escape = '\0';
if (--size > 1)
break;
*so = '\0';
goto done;
}
}
done:
bufptr = si;
if (si == NULL)
{
/* EOF */
Efree(buffer);
buffer = NULL;
if (so == s)
return NULL;
}
/* Strip trailing whitespace */
si = so;
@ -174,7 +161,7 @@ GetLine(char *s, int size, FILE * f)
if (so != si)
*so = '\0';
if (EventDebug(EDBUG_TYPE_CONFIG))
if (EventDebug(EDBUG_TYPE_CONFIG) > 1)
Eprintf("GetLine %s\n", s);
return s;
@ -196,7 +183,7 @@ ConfigFilePreparse(const char *path, const char *dest)
char *def_home, *def_user, *def_shell;
if (EventDebug(EDBUG_TYPE_CONFIG))
Eprintf("ConfigFilePreparse %s->%s\n", path, dest);
Eprintf("ConfigFilePreparse %s -> %s\n", path, dest);
def_home = homedir(getuid());
def_user = username(getuid());
@ -241,9 +228,6 @@ ConfigFileRead(FILE * fs)
while (GetLine(s, sizeof(s), fs))
{
if (IsWhitespace(s))
continue;
i1 = i2 = CONFIG_INVALID;
fields = sscanf(s, "%i %i", &i1, &i2);
@ -270,6 +254,7 @@ ConfigFileRead(FILE * fs)
Alert(_("CONFIG: missing required data in \"%s\"\n"), s);
}
}
if (i2 == CONFIG_OPEN)
{
if (e_cfg_ver > max_e_cfg_ver)

View File

@ -334,59 +334,34 @@ SoundExit(void)
#include "conf.h"
static int
SoundConfigLoad(void)
SoundConfigLoad(FILE * fs)
{
int err = 0;
SoundClass *sc;
char s[FILEPATH_LEN_MAX];
char s1[FILEPATH_LEN_MAX];
char s2[FILEPATH_LEN_MAX];
int i1, ret;
FILE *fs;
char *file;
file = ConfigFileFind("sound.cfg", Mode.theme.path, 1);
if (!file)
return 0;
fs = fopen(file, "r");
Efree(file);
if (!fs)
goto done;
int i1, fields;
while (GetLine(s, sizeof(s), fs))
{
i1 = -1;
ret = sscanf(s, "%d", &i1);
if (ret == 1)
fields = sscanf(s, "%d", &i1);
if (fields == 1) /* Just skip the numeric config stuff */
continue;
s1[0] = s2[0] = '\0';
fields = sscanf(s, "%4000s %4000s", s1, s2);
if (fields != 2)
{
switch (i1)
{
case CONFIG_VERSION:
case CONFIG_OPEN:
break;
case CONFIG_CLOSE:
goto done;
}
}
else
{
s1[0] = s2[0] = '\0';
ret = sscanf(s, "%4000s %4000s", s1, s2);
if (ret != 2)
{
Eprintf("Ignoring line: %s\n", s);
break;
}
sc = SclassCreate(s1, s2);
Eprintf("Ignoring line: %s\n", s);
continue;
}
sc = SclassCreate(s1, s2);
}
if (err)
ConfigAlertLoad(_("Sound"));
done:
if (fs)
fclose(fs);
return err;
}
@ -403,7 +378,7 @@ SoundSighan(int sig, void *prm __UNUSED__)
SoundInit();
break;
case ESIGNAL_CONFIGURE:
SoundConfigLoad();
ConfigFileLoad("sound.cfg", Mode.theme.path, SoundConfigLoad, 1);
break;
case ESIGNAL_START:
if (!Conf_sound.enable)

View File

@ -494,7 +494,7 @@ WindowMatchEncode(WindowMatch * wm, char *buf, int len)
return buf;
}
static void
static int
WindowMatchConfigLoad2(FILE * fs)
{
char s[FILEPATH_LEN_MAX], *ss;
@ -513,26 +513,8 @@ WindowMatchConfigLoad2(FILE * fs)
WindowMatchDecode(s);
}
}
static void
WindowMatchConfigLoadConfig(void)
{
char *file;
FILE *fs;
file = ConfigFileFind("matches.cfg", NULL, 0);
if (!file)
return;
fs = fopen(file, "r");
Efree(file);
if (!fs)
return;
WindowMatchConfigLoad2(fs);
fclose(fs);
return 0;
}
static int
@ -865,11 +847,11 @@ WindowMatchSighan(int sig, void *prm __UNUSED__)
switch (sig)
{
case ESIGNAL_CONFIGURE:
#if 0
#if 0 /* Done as part of theme loading */
ConfigFileLoad("windowmatches.cfg", Mode.theme.path,
WindowMatchConfigLoad);
WindowMatchConfigLoad, 1);
#endif
WindowMatchConfigLoadConfig();
ConfigFileLoad("matches.cfg", NULL, WindowMatchConfigLoad2, 0);
#if 0
WindowMatchConfigLoadUser();
#endif