static CondResult parseCondition (Key * key, const char * condition, const Key * suffixList, KeySet * ks, Key * parentKey) { CondResult result = FALSE; const char * regexString = "((\\(([^\\(\\)]*)\\)))"; regex_t regex; if ((regcomp (®ex, regexString, REG_EXTENDED | REG_NEWLINE))) { ELEKTRA_SET_ERROR (87, parentKey, "Couldn't compile regex: most likely out of memory"); // the regex compiles so the only // possible error would be out of // memory ksDel (ks); return ERROR; } char * localCondition = elektraStrDup (condition); int subMatches = 4; regmatch_t m[subMatches]; char * ptr = localCondition; while (1) { int nomatch = regexec (®ex, ptr, subMatches, m, 0); if (nomatch) { break; } if (m[3].rm_so == -1) { result = -1; break; } int startPos; int endPos; startPos = m[3].rm_so + (ptr - localCondition); endPos = m[3].rm_eo + (ptr - localCondition); char * singleCondition = elektraMalloc (endPos - startPos + 1); strncpy (singleCondition, localCondition + startPos, endPos - startPos); singleCondition[endPos - startPos] = '\0'; result = parseSingleCondition (key, singleCondition, suffixList, ks, parentKey); for (int i = startPos - 1; i < endPos + 1; ++i) localCondition[i] = ' '; localCondition[startPos - 1] = '\''; localCondition[startPos] = (result == TRUE) ? '1' : '0'; localCondition[startPos + 1] = '\''; elektraFree (singleCondition); } elektraFree (localCondition); regfree (®ex); return result; }
static int parseConditionString(const Key *meta, Key *parentKey, KeySet *ks) { const char *conditionString = keyString(meta); const char *regexString = "(\\(([^\\)]*)\\))\\s*(\\?)\\s*(\\(([^\\)]*)\\))\\s*(:\\s*(\\(([^\\)]*)\\))){0,1}"; regex_t regex; int ret; if((ret = regcomp(®ex, regexString, REG_EXTENDED|REG_NEWLINE))) { ELEKTRA_SET_ERROR(87, parentKey, "Couldn't compile regex: most likely out of memory"); //the regex compilers so the only possible error would be out of memory ksDel(ks); return -1; } int subMatches = 9; regmatch_t m[subMatches]; char *ptr = (char *)conditionString; int nomatch = regexec(®ex, ptr, subMatches, m, 0); if(nomatch) { ELEKTRA_SET_ERRORF(134, parentKey, "Invalid syntax: \"%s\". See README\n", conditionString); regfree(®ex); ksDel(ks); return -1; } if(m[2].rm_so == -1 || m[5].rm_so == -1) { ELEKTRA_SET_ERRORF(134, parentKey, "Invalid syntax: \"%s\". See README\n", conditionString); regfree(®ex); ksDel(ks); return -1; } char *condition = NULL; char *thenexpr = NULL; char *elseexpr = NULL; int startPos; int endPos; startPos = m[2].rm_so + (ptr - conditionString); endPos = m[2].rm_eo + (ptr - conditionString); condition = elektraMalloc(endPos - startPos +1); strncpy(condition, conditionString+startPos, endPos - startPos); condition[endPos-startPos] = '\0'; startPos = m[5].rm_so + (ptr - conditionString); endPos = m[5].rm_eo + (ptr - conditionString); thenexpr = elektraMalloc(endPos - startPos +1); strncpy(thenexpr, conditionString+startPos, endPos - startPos); thenexpr[endPos-startPos] = '\0'; if(m[8].rm_so != -1) { startPos = m[8].rm_so + (ptr - conditionString); endPos = m[8].rm_eo + (ptr - conditionString); elseexpr = elektraMalloc(endPos - startPos +1); strncpy(elseexpr, conditionString+startPos, endPos - startPos); elseexpr[endPos-startPos] = '\0'; } ret = parseSingleCondition(condition, ks, parentKey); if(ret == 1) ret = parseSingleCondition(thenexpr, ks, parentKey); else if(ret == 0) { if(elseexpr) ret = parseSingleCondition(elseexpr, ks, parentKey); else ret = 1; } elektraFree(condition); elektraFree(thenexpr); if(elseexpr) elektraFree(elseexpr); regfree(®ex); ksDel(ks); return ret; }