/* **************************************************************************** * * iListFix - * * NOTE * This function destroys its input string 's'. */ static int iListFix(int* iV, char* s, int* error) { char* tmP = s; char* endP = &s[strlen(s)]; int ix = 0; PA_M(("incoming list: '%s'", s)); baWsStrip(s); while ((uint64_t) tmP < (uint64_t) endP) { tmP = strchr(s, ' '); if (tmP == NULL) { tmP = strchr(s, '\t'); } if (tmP >= endP) { break; } if (tmP == NULL) /* last argument */ { /* Check 's' for valid integer - reflect in *error parameter */ iV[ix + 1] = baStoi(s); PA_M(("item %d in int-list: %d", ix + 1, iV[ix + 1])); ++ix; iV[0] = ix; break; } *tmP = 0; /* Check 's' for valid integer - reflect in *error parameter */ iV[ix + 1] = baStoi(s); s = &tmP[1]; LM_T(LmtPaIList, ("item %d in int-list: %d", ix + 1, iV[ix + 1])); LM_T(LmtPaIList, ("rest: '%s'", s)); ++ix; baWsStrip(s); } LM_T(LmtPaIList, ("got %d items in int-list", ix)); *error = PaOk; return ix; }
/* **************************************************************************** * * paGetVal - calculate the integer value of a string */ void* paGetVal(char* string, int* error) { long value; int type; char errorText[256]; errorText[0] = 0; *error = PaOk; value = baStoi(string, &type, errorText); if (errorText[0] != 0) { PA_WARNING(PasBadValue, errorText); *error = type; return NULL; } return (void*) value; }
/* **************************************************************************** * * paRcFileParse - parse startup file */ int paRcFileParse(void) { char dir[1024]; char path[1024]; char line[512]; int lineNo = 0; FILE* fP; char w[512]; LM_ENTRY(); if ((paRcFileName == NULL) || (paRcFileName[0] == 0)) { LM_EXIT(); return 0; } if (dirFind(dir, sizeof(dir)) == 0) { LM_T(LmtPaRcFile, ("RC file '%s' found in directory '%s'", paRcFileName, dir)); } else { return 0; } snprintf(path, sizeof(path), "%s/%s", dir, paRcFileName); if ((fP = fopen(path, "r")) == NULL) { LM_RE(-1, ("error opening RC file '%s': %s", path, strerror(errno))); } LM_T(LmtPaRcFile, ("parsing RC file %s", path)); while (fgets(line, sizeof(line), fP) != NULL) { char* delim; char* var; char* val; PaiArgument* aP; bool varFound; char envVarName[128]; ++lineNo; LM_T(LmtPaRcFile, ("got line %d", lineNo)); newlineStrip(line); LM_T(LmtPaRcFile, ("got line %d", lineNo)); commentStrip(line, '#'); LM_T(LmtPaRcFile, ("got line %d", lineNo)); baWsStrip(line); LM_T(LmtPaRcFile, ("got line %d", lineNo)); if (line[0] == 0) { continue; } LM_T(LmtPaRcFile, ("got line %d", lineNo)); delim = strchr(line, '='); if (delim == NULL) { char w[512]; snprintf(w, sizeof(w), "%s[%d]: no delimiter found", path, lineNo); PA_WARNING(PasParseError, w); continue; } *delim = 0; var = line; val = &delim[1]; baWsStrip(var); baWsStrip(val); if (var[0] == 0) { fclose(fP); LM_RE(-1, ("%s[%d]: no variable ...", path, lineNo)); } if (val[0] == 0) { fclose(fP); LM_RE(-1, ("%s[%d]: no value for variable %s", path, lineNo, var)); } varFound = false; paIterateInit(); while ((aP = paIterateNext(paiList)) != NULL) { paEnvName(aP, envVarName, sizeof(envVarName)); if (strcmp(var, envVarName) == 0) { aP->from = PafRcFile; LM_T(LmtPaRcFileVal, ("got value '%s' for %s", val, envVarName)); varFound = true; break; } } if (varFound == false) { char w[512]; snprintf(w, sizeof(w), "%s[%d]: variable '%s' not recognized", path, lineNo, var); PA_WARNING(PasNoSuchVariable, w); continue; } switch (aP->type) { case PaString: strcpy((char*) aP->varP, val); break; case PaBoolean: if ((strcmp(val, "TRUE") == 0) || (strcmp(val, "ON") == 0) || (strcmp(val, "yes") == 0) || (strcmp(val, "1") == 0)) *((bool*) (int64_t) aP->varP) = true; else if ((strcmp(val, "FALSE") == 0) || (strcmp(val, "OFF") == 0) || (strcmp(val, "no") == 0) || (strcmp(val, "0") == 0)) *((bool*) (int64_t) aP->varP) = false; else { snprintf(w, sizeof(w), "bad value '%s' for boolean variable %s", val, envVarName); PA_WARNING(PasNoSuchBooleanValue, w); } break; case PaSList: case PaIList: LM_TODO(("lists ...")); break; case PaInt: case PaIntU: *((int64_t*) aP->varP) = baStoi(val); break; case PaShort: case PaShortU: *((int16_t*) (int64_t) aP->varP) = baStoi(val); break; case PaFloat: *((float*) (int64_t) aP->varP) = baStof(val); break; case PaDouble: *((double*) (int64_t) aP->varP) = baStod(val); break; case PaChar: case PaCharU: *((char*) (int64_t) aP->varP) = baStoi(val); break; default: snprintf(w, sizeof(w), "bad type %d for variable %s", aP->type, envVarName); PA_WARNING(PasNoSuchType, w); } } fclose(fP); return 0; }