diff --git a/plugins/ini.c b/plugins/ini.c index c55d897..288079b 100644 --- a/plugins/ini.c +++ b/plugins/ini.c @@ -143,9 +143,11 @@ static IniFileData * iniGetFileDataFromFilename (CompDisplay *d, const char *filename) { - int len, i; - int pluginSep = 0, screenSep = 0; - char *pluginStr, *screenStr; + int fnLen, length; + char *pluginSep; + char *screenSep; + char *pluginStr; + char *screenStr; IniFileData *fd; INI_DISPLAY (d); @@ -153,35 +155,27 @@ iniGetFileDataFromFilename (CompDisplay *d, if (!filename) return NULL; - len = strlen (filename); + fnLen = strlen(filename); - if (len < (strlen(FILE_SUFFIX) + 2)) + if (fnLen < (strlen(FILE_SUFFIX) + 2)) return NULL; - if ((filename[0]=='.') || (filename[len-1]=='~')) + if ((filename[0] == '.') || (filename[fnLen-1] == '~')) return NULL; for (fd = id->fileData; fd; fd = fd->next) if (strcmp (fd->filename, filename) == 0) return fd; - for (i=0; ifilename = strdup (filename); - pluginStr = malloc (sizeof (char) * pluginSep + 1); - screenStr = malloc (sizeof (char) * (screenSep - pluginSep) + 1); - - if (!pluginStr || !screenStr) - return NULL; - - strncpy (pluginStr, filename, pluginSep + 1); - pluginStr[pluginSep + 1] = '\0'; - - strncpy (screenStr, &filename[pluginSep+2], (screenSep - pluginSep) + 1); - screenStr[(screenSep - pluginSep) -1] = '\0'; + /* get plugin and screen name */ + length = strlen(filename) - strlen(pluginSep); + pluginStr = strndup(filename, length); + pluginSep++; /* skip the '-' */ + length = strlen(pluginSep) - strlen(screenSep); + screenStr = strndup(pluginSep, length); if (strcmp (pluginStr, CORE_NAME) == 0) newFd->plugin = NULL; @@ -330,7 +319,7 @@ iniGetFilename (CompDisplay *d, if (fn) { sprintf (fn, "%s-%s%s", - plugin?plugin:CORE_NAME, screenStr, FILE_SUFFIX); + plugin ? plugin : CORE_NAME, screenStr, FILE_SUFFIX); *filename = strdup (fn); @@ -348,48 +337,20 @@ iniGetFilename (CompDisplay *d, static Bool iniParseLine (char *line, char **optionName, char **optionValue) { - int pos = 0; - int splitPos = 0; - int endPos = 0; - char tmpName[MAX_OPTION_LENGTH]; - char tmpValue[MAX_OPTION_LENGTH]; + char *splitPos; + int length; if (line[0] == '\0' || line[0] == '\n') return FALSE; - while (pos < strlen(line)) - { - if (!splitPos && line[pos] == '=') - splitPos = pos; - if (line[pos] == '\n') - { - endPos = pos; - break; - } - pos++; - } - - if (splitPos && endPos) - { - tmpName[0] = '\0'; - tmpValue[0] = '\0'; - - int i; - for (i=0; i < splitPos; i++) - tmpName[i] = line[i]; - tmpName[splitPos] = '\0'; - - for (i=splitPos+1; inValue = 0; return FALSE; } - - csvtmp = strdup (csv); - csvtmp = strchr (csv, ','); - - while (csvtmp) - { - csvtmp++; /* avoid the comma */ - count++; - csvtmp = strchr (csvtmp, ','); - } - + + int length = strlen(csv); + count = 1; + for (i = 0; i < length; i++) + if (csv[i] == ',' && i != length-1) + count++; + + splitStart = csv; list->value = malloc (sizeof (CompOptionValue) * count); if (list->value) { - for (i=0; i 1 value */ - itemLength = strlen(csv) - strlen(split); - item = realloc (item, sizeof (char) * (itemLength+1)); - strncpy (item, csv, itemLength); - item[itemLength] = '\0'; - csv += itemLength + 1; + itemLength = strlen(splitStart) - strlen(splitEnd); + item = strndup(splitStart, itemLength); } - else + else // last value { - /* 1 value only */ - itemLength = strlen(csv); - item = realloc (item, sizeof (char) * (itemLength+1)); - strncpy (item, csv, itemLength); - item[itemLength] = '\0'; + item = strdup(splitStart); } switch (type) { case CompOptionTypeString: - list->value[i].s = strdup (item); + if (item[0] != '\0') + list->value[i].s = strdup (item); break; case CompOptionTypeBool: if (item[0] != '\0') @@ -464,13 +420,14 @@ csvToList (char *csv, CompListValue *list, CompOptionType type) default: break; } + + splitStart = ++splitEnd; + if (item) + free(item); } list->nValue = count; } - if (item) - free (item); - return TRUE; } @@ -543,6 +500,7 @@ parseAction(CompDisplay *d, char *optionName, char *optionValue, IniAction *acti action->a.bell = FALSE; action->a.edgeMask = 0; action->a.edgeButton = 0; + action->valueMasks = 0; } /* detect a new option (might happen when the options are incomplete) */ else if (action->valueMasks != ACTION_VALUES_ALL) @@ -639,7 +597,7 @@ iniLoadOptionsFromFile (CompDisplay *d, CompScreen *s = NULL; CompPlugin *p = NULL; Bool status = FALSE; - Bool hv = FALSE; + Bool hasValue = FALSE; CompOptionValue value; if (plugin) @@ -688,12 +646,13 @@ iniLoadOptionsFromFile (CompDisplay *d, IniAction action; action.realOptionName = NULL; - Bool continueReading = FALSE; - while (fgets (&tmp[0], MAX_OPTION_LENGTH, optionFile) != NULL) + Bool continueReading; + while (fgets (tmp, MAX_OPTION_LENGTH, optionFile) != NULL) { status = FALSE; + continueReading = FALSE; - if (!iniParseLine (&tmp[0], &optionName, &optionValue)) + if (!iniParseLine (tmp, &optionName, &optionValue)) { fprintf(stderr, "Ignoring line '%s' in %s %i\n", tmp, plugin, screen); @@ -710,29 +669,29 @@ iniLoadOptionsFromFile (CompDisplay *d, switch (o->type) { case CompOptionTypeBool: - hv = TRUE; + hasValue = TRUE; value.b = (Bool) atoi (optionValue); break; case CompOptionTypeInt: - hv = TRUE; + hasValue = TRUE; value.i = atoi (optionValue); break; case CompOptionTypeFloat: - hv = TRUE; + hasValue = TRUE; value.f = atof (optionValue); break; case CompOptionTypeString: - hv = TRUE; + hasValue = TRUE; value.s = strdup (optionValue); break; case CompOptionTypeColor: - hv = stringToColor (optionValue, value.c); + hasValue = stringToColor (optionValue, value.c); break; case CompOptionTypeList: - hv = csvToList (optionValue, &value.list, value.list.type); + hasValue = csvToList (optionValue, &value.list, value.list.type); break; case CompOptionTypeMatch: - hv = TRUE; + hasValue = TRUE; matchInit (&value.match); matchAddFromString (&value.match, optionValue); break; @@ -740,7 +699,7 @@ iniLoadOptionsFromFile (CompDisplay *d, break; } - if (hv) + if (hasValue) { if (plugin && p) { @@ -780,10 +739,10 @@ iniLoadOptionsFromFile (CompDisplay *d, if (action.realOptionName && !continueReading) { - o = compFindOption (option, nOption, action.realOptionName, 0); - if (o) + CompOption *realOption = compFindOption (option, nOption, action.realOptionName, 0); + if (realOption) { - value = o->value; + value = realOption->value; value.action.type = action.a.type; value.action.key = action.a.key; @@ -811,16 +770,12 @@ iniLoadOptionsFromFile (CompDisplay *d, free(action.realOptionName); action.realOptionName = NULL; - /* we missed the current line because we exited it in the first call */ - if (!o && action.valueMasks == ACTION_VALUES_ALL) - { - action.valueMasks = 0; + /* we missed the current line because we exited it in the first call. + we also need to check wether we have a incomplete options here, + because other while parsing the last line again, would caus real + trouble. ;-) */ + if (!o && action.valueMasks != ACTION_VALUES_ALL) parseAction(d, optionName, optionValue, &action); - } - else - { - action.valueMasks = 0; - } } } } @@ -830,7 +785,6 @@ iniLoadOptionsFromFile (CompDisplay *d, free (optionName); if (optionValue) free (optionValue); - continueReading = FALSE; } return TRUE;