/* **************************************************************************** * * main - */ int main(int argC, char* argV[]) { char* fName = "main"; paConfig("trace levels", "0-255"); paConfig("help file", "/users/kzangeli/systems/parseArgs/parseTest3.help"); paConfig("log to file", (void*) TRUE); paParse(NULL, argC, argV, 1, FALSE); lmVerbose = true; LM_V(("line with newlines ...\n 1\n2\n3\n4\n")); return 0; }
int main(int argC, char* argV[]) { char* fName = "main"; paConfig("prefix", "P2_"); paConfig("log to file", "/tmp/"); paConfig("msgs to stdout", (void*) FALSE); paParse(NULL, argC, argV, 1, FALSE); LM_V(("verbose message")); if (paResultString[0] != 0) printf("Got a paResultString:\n%s", paResultString); else if (paWarnings != 0) { int ix; printf("Got warnings:\n"); for (ix = 0; ix < paWarnings; ix++) printf("Severity %02d: %s\n", paWarning[ix].severity, paWarning[ix].string); printf("\n"); } return 0; }
/* **************************************************************************** * * paOptionsParse - */ int paOptionsParse(PaiArgument* paList, char* argV[], int argC) { PaiArgument* aP; char* valueP; int argNo = 0; int param = 1; bool extendedUsage = false; char w[512]; LM_ENTRY(); w[0] = 0; PA_M(("incoming arg list of %d args", argC)); while (++argNo < argC) { char e[80]; char o[80]; int eee; int* eP; eP = &eee; *eP = PaNotOk; PA_M(("Looking up '%s'", argV[argNo])); if ((aP = argFind(paList, argV[argNo], STRICT, NULL)) != NULL) { valueP = argV[++argNo]; if (aP->type == PaBoolean) --argNo; } else if ((aP = argFind(paList, argV[argNo], UNSTRICT, NULL)) != NULL) valueP = &argV[argNo][strlen(aP->option)]; else if ((aP = argFind(paList, (char*) "", UNSTRICT, ¶m)) != NULL) valueP = argV[argNo]; else { snprintf(w, sizeof(w), "%s '%s' not recognized", optOrPar(argV[argNo]), argV[argNo]); PA_W(("Warning: '%s'", w)); PA_WARNING(PasNoSuchOption, w); continue; } LM_T(LmtPaApVals, ("found option '%s'", aP->name)); if (aP->varP == (void*) &paUsageVar) { memset(paResultString, 0, sizeof(paResultString)); if (extendedUsage == false) { paUsage(); return -2; } } else if (aP->varP == (void*) &paVersion) { paVersionPrint(); exit(0); } else if (aP->varP == (void*) &paLogDir) { if (valueP != NULL) { snprintf(paLogDir, sizeof(paLogDir), "%s", (char*) valueP); printf("log directory: '%s'\n", paLogDir); } } else if (aP->varP == (void*) &paEUsageVar) extendedUsage = true; else if (aP->varP == (void*) &paHelpVar) { memset(paResultString, 0, sizeof(paResultString)); paHelp(); return -2; } if (aP->used > 0) { if ((aP->type != PaSList) && (aP->type != PaIList) && (aP->what != PawParameter)) { snprintf(w, sizeof(w), "multiple use of %s", aP->name); PA_WARNING(PasMultipleOptionUse, w); continue; } } aP->from = PafArgument; if (aP->type == PaBoolean) { if (strlen(argV[argNo]) != strlen(aP->option)) { char tmp[128]; snprintf(w, sizeof(w), "boolean option '%s' doesn't take parameters", paFullName(aP, e, sizeof(e))); snprintf(tmp, sizeof(tmp), "%c%s", argV[argNo][0], &argV[argNo][2]); LM_W(("Changing arg %d from '%s' to '%s'", argNo, argV[argNo], tmp)); snprintf(argV[argNo], strlen(argV[argNo]), "%s", tmp); --argNo; } *((bool*) aP->varP) = (((bool) ((int) aP->def)) == true)? false : true; aP->used++; continue; } if ((argNo >= argC) || (paIsOption(paList, valueP) == true)) { REQUIRE(aP); break; } paFullName(aP, o, sizeof(o)); switch (aP->type) { case PaInt: case PaIntU: *((int*) aP->varP) = (int) (int64_t) paGetVal(valueP, eP); if (*eP != PaOk) { return -1; } LM_T(LmtPaApVals, ("got value %d for %s", *((int*) aP->varP), aP->name)); break; case PaInt64: case PaIntU64: *((int64_t*) aP->varP) = (int64_t) paGetVal(valueP, eP); if (*eP != PaOk) { return -1; } LM_T(LmtPaApVals, ("got value %ul for %s", *((int64_t*) aP->varP), aP->name)); break; case PaChar: case PaCharU: *((char*) aP->varP) = (char) (int64_t) paGetVal(valueP, eP); if (*eP != PaOk) { return -1; } LM_T(LmtPaApVals, ("got value %d for %s", *((char*) aP->varP), aP->name)); break; case PaShort: case PaShortU: *((int16_t*) aP->varP) = (int16_t) (int64_t) paGetVal(valueP, eP); if (*eP != PaOk) { return -1; } LM_T(LmtPaApVals, ("got value %d for %s", *((int16_t*) aP->varP), aP->name)); break; case PaFloat: *((float*) aP->varP) = baStof(valueP); *eP = PaOk; LM_T(LmtPaApVals, ("got value %f for %s", *((float*) aP->varP), aP->name)); break; case PaDouble: *((double*) aP->varP) = baStof(valueP); *eP = PaOk; LM_T(LmtPaApVals, ("got value %f for %s", *((double*) aP->varP), aP->name)); break; case PaString: strcpy((char*) aP->varP, valueP); LM_T(LmtPaApVals, ("got value '%s' for %s", (char*) aP->varP, aP->name)); *eP = PaOk; break; case PaIList: LM_T(LmtPaIList, ("setting list '%s' to var %s", valueP, aP->name)); iListFix((int*) aP->varP, valueP, eP); break; case PaSList: LM_T(LmtPaSList, ("setting list '%s' to var %s", valueP, aP->name)); sListFix((char**) aP->varP, valueP, eP); break; case PaBoolean: case PaLastArg: LM_T(LmtPaApVals, ("PaList, PaBoolean, PaLastArg ...")); *eP = PaOk; break; default: LM_W(("bad type for option '%s'", aP->name)); } if (*eP != PaOk) { REQUIRE(aP); } aP->used++; LM_V(("%s used %d times", aP->name, aP->used)); } /* checking that all required arguments are set */ paIterateInit(); while ((aP = paIterateNext(paList)) != NULL) { if ((aP->sort == PaReq) && (aP->used == 0)) { snprintf(w, sizeof(w), "%s required", aP->name); PA_WARNING(PasRequiredOption, w); } } if (extendedUsage == true) { paExtendedUsage(); return -2; } LM_EXIT(); return 0; }
/* **************************************************************************** * * restStart - */ static int restStart(IpVersion ipVersion, const char* httpsKey = NULL, const char* httpsCertificate = NULL) { if (port == 0) LM_RE(1, ("Please call restInit before starting the REST service")); if ((ipVersion == IPV4) || (ipVersion == IPDUAL)) { memset(&sad, 0, sizeof(sad)); if (inet_pton(AF_INET, bindIp, &(sad.sin_addr.s_addr)) != 1) LM_RE(2, ("V4 inet_pton fail for %s", bindIp)); sad.sin_family = AF_INET; sad.sin_port = htons(port); if ((httpsKey != NULL) && (httpsCertificate != NULL)) { mhdDaemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_SSL, // MHD_USE_SELECT_INTERNALLY htons(port), NULL, NULL, connectionTreat, NULL, MHD_OPTION_HTTPS_MEM_KEY, httpsKey, MHD_OPTION_HTTPS_MEM_CERT, httpsCertificate, MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL, MHD_OPTION_CONNECTION_MEMORY_LIMIT, 2 * PAYLOAD_SIZE, MHD_OPTION_SOCK_ADDR, (struct sockaddr*) &sad, MHD_OPTION_END); } else { LM_V(("Starting HTTP daemon on IPv4 %s port %d", bindIp, port)); mhdDaemon = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION, // MHD_USE_SELECT_INTERNALLY htons(port), NULL, NULL, connectionTreat, NULL, MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL, MHD_OPTION_CONNECTION_MEMORY_LIMIT, 2 * PAYLOAD_SIZE, MHD_OPTION_SOCK_ADDR, (struct sockaddr*) &sad, MHD_OPTION_END); } if (mhdDaemon == NULL) LM_RE(3, ("MHD_start_daemon failed")); } if ((ipVersion == IPV6) || (ipVersion == IPDUAL)) { memset(&sad_v6, 0, sizeof(sad_v6)); if (inet_pton(AF_INET6, bindIPv6, &(sad_v6.sin6_addr.s6_addr)) != 1) LM_RE(1, ("V6 inet_pton fail for %s", bindIPv6)); sad_v6.sin6_family = AF_INET6; sad_v6.sin6_port = htons(port); if ((httpsKey != NULL) && (httpsCertificate != NULL)) { LM_V(("Starting HTTPS daemon on IPv6 %s port %d", bindIPv6, port)); mhdDaemon_v6 = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_IPv6 | MHD_USE_SSL, htons(port), NULL, NULL, connectionTreat, NULL, MHD_OPTION_HTTPS_MEM_KEY, httpsKey, MHD_OPTION_HTTPS_MEM_CERT, httpsCertificate, MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL, MHD_OPTION_CONNECTION_MEMORY_LIMIT, 2 * PAYLOAD_SIZE, MHD_OPTION_SOCK_ADDR, (struct sockaddr*) &sad_v6, MHD_OPTION_END); } else { LM_V(("Starting HTTP daemon on IPv6 %s port %d", bindIPv6, port)); mhdDaemon_v6 = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION | MHD_USE_IPv6, htons(port), NULL, NULL, connectionTreat, NULL, MHD_OPTION_NOTIFY_COMPLETED, requestCompleted, NULL, MHD_OPTION_CONNECTION_MEMORY_LIMIT, 2 * PAYLOAD_SIZE, MHD_OPTION_SOCK_ADDR, (struct sockaddr*) &sad_v6, MHD_OPTION_END); } if (mhdDaemon_v6 == NULL) LM_RE(1, ("MHD_start_daemon_v6 failed")); } return 0; }