Пример #1
0
/* ****************************************************************************
*
* paParse - parse the argument list
*
*/
int paParse
(
  PaArgument*  paList,
  int          argC,
  char*        argV[],
  int          level,
  bool         pid,
  const char*  extra
)
{
  char*  progNameCopy;
  int    ix;
  int    s;
  FILE*  fP;

  if (extra != NULL)
  {
    paExtraLogSuffix = strdup(extra);
  }

  memset(paResultString, 0, sizeof(paResultString));

  /* **************************************************** */
  /* Creating flat command line string paCommandLine      */
  /*                                                      */
  if (argC > 1)
  {
    strncpy(paCommandLine, argV[1], sizeof(paCommandLine));
    for (ix = 2; ix < argC; ix++)
    {
      if (sizeof(paCommandLine) - strlen(paCommandLine) > 1)
      {
        strncat(paCommandLine, " ", sizeof(paCommandLine) - strlen(paCommandLine) - 1);
      }
      else
      {
        break;
      }

      if (sizeof(paCommandLine) - strlen(paCommandLine) > 1)
      {
        strncat(paCommandLine, argV[ix], sizeof(paCommandLine) - strlen(paCommandLine) - 1);
      }
      else
      {
        break;
      }
    }
  }


  /* ********************************************************* */
  /* Calling paConfig just to make sure paConfigInit is called */
  /*                                                           */
  paConfig("make sure paConfigInit is called", 0);



  /* *************************** */
  /* Setting up the program name */
  /*                             */
  if (paProgName == NULL) /* hasn't been set with paConfig */
  {
    progNameCopy = strdup(argV[0]);
    progName     = strdup(paProgNameSet(argV[0], level, pid, extra));
    paProgName   = strdup(progName);
  }
  else
  {
    progNameCopy = strdup(paProgName);
    progName     = strdup(paProgName);
  }


  /* ************************************************* */
  /* The rcFileName must not have any '/' in its name. */
  /* it should be called (by default) .<progname>rc    */
  /* So, first we must get a progName without '/' ...  */
  /*                                                   */
  if (paRcFileName == NULL)
  {
    char rName[128];

    snprintf(rName, sizeof(rName), ".%src", paProgNameSet(progNameCopy, 1, false));
    paRcFileName = strdup(rName);
  }
  free(progNameCopy);


  /* ****************************************************** */
  /* Initializing all subsystems                            */
  /*                                                        */
  if ((s = paArgInit(paList)) == -1)
  {
    RETURN_ERROR("paArgInit error");
  }

  if ((s != -2) && ((s = paConfigActions(true)) == -1))
  {
    RETURN_ERROR("paConfigActions");
  }

  if ((s != -2) && ((s = paDefaultValues(paiList)) == -1))
    RETURN_ERROR("paDefaultValues");

#if 0
  // Orion doesn't use paRcFileParse ...
  if ((s != -2) && ((s = paRcFileParse()) == -1))
    RETURN_ERROR("paRcFileParse");
#endif

  if ((s != -2) && ((s = paEnvVals(paiList)) == -1))
  {
    RETURN_ERROR("paEnvVals");
  }

  if ((s != -2) && ((s = paOptionsParse(paiList, argV, argC)) == -1))
  {
    RETURN_ERROR("paOptionsParse");
  }

  if (paLogSetup() == -1)
  {
    RETURN_ERROR("paLogSetup error");
  }

  if ((s != -2) && ((s = paLimitCheck(paiList)) == -1))
  {
    RETURN_ERROR("paLimitCheck");
  }

  if ((s != -2) && ((s = paConfigActions(false)) == -1))
  {
    RETURN_ERROR("paConfigActions");
  }


  fP = NULL;
  if (paMsgsToStdout)
  {
    fP = stdout;
  }

  if (paMsgsToStderr)
  {
    fP = stderr;
  }

  if (paResultString[0] != 0)
  {
    if (fP != NULL)
    {
      fprintf(fP, "%s\n", paResultString);
    }
  }
  else if (paWarnings > 0)
  {
    int ix;

    if (fP != NULL)
    {
      // fprintf(fP, "\nEntire command line: '%s'\n\n", paCommandLine);

      for (ix = 0; ix < paWarnings; ix++)
      {
        fprintf(fP, "%s\n\n", paWarning[ix].string);
      }

      if (paUsageOnAnyWarning)
      {
        paUsage();
      }

      if (paResultString[0] != 0)
      {
        fprintf(fP, "%s\n", paResultString);
      }

      exit(1);
    }
    else
    {
      char s[64000];

      snprintf(paResultString, sizeof(paResultString), "\nEntire command line options: '%s'\n\n", paCommandLine);

      snprintf(s, sizeof(s), "--- %s warnings ---\n", progName);
      strncat(paResultString, s, sizeof(paResultString) - 1);

      for (ix = 0; ix < paWarnings; ix++)
      {
        snprintf(s, sizeof(s), "Severity % 2d: %s\n\n", paWarning[ix].severity, paWarning[ix].string);
        strncat(paResultString, s, sizeof(paResultString) - 1);
      }

      strncat(paResultString, "\n\n", sizeof(paResultString) - 1);

      if (paUsageOnAnyWarning)
      {
        printf("paUsageOnAnyWarning == true (2)\n");
        paUsage();
      }
    }
  }

  free(paiList);
  free(paRcFileName);
  free(paUsageProgName);

  return 0;
}
Пример #2
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, &param)) != 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;
}
Пример #3
0
/* ****************************************************************************
*
* paHelp - print help text
*/
void paHelp(void)
{
    LM_ENTRY();

    paManHelp();
    exit(1);

#if 0
    pid_t  pid   = getpid();

    snprintf(paPid, sizeof(paPid), "%d", pid);

    memset(paResultString, 0, sizeof(paResultString));
    paExitOnUsage = false;
    paUsage();
    strncpy(usageString, paResultString, sizeof(usageString));
    memset(paResultString, 0, sizeof(paResultString));

    if (paHelpFile != NULL)
    {
        char  s[512];
        FILE* fP;
        char  line[1024];
        char  start[512];
        char  end[512];

        LM_T(LmtHelp, ("Got help file '%s'", paHelpFile));

        snprintf(s, sizeof(s), "----- %s Help -----\n", progName);
        strncat(paResultString, s, sizeof(paResultString) - 1);

        if ((fP = fopen(paHelpFile, "r")) == NULL)
        {
            snprintf(s, sizeof(s), "error opening help file '%s': %s", paHelpFile, strerror(errno));
            strncat(paResultString, s, sizeof(paResultString) - 1);
            return;
        }

        while (fgets(line, sizeof(line), fP) != NULL)
        {
            int ix;
            int changes;

            do
            {
                changes = 0;

                for (ix = 0; ix < (int) BA_VEC_SIZE(helpVar); ix++)
                {
                    char* tmp;

                    if ((tmp = strstr(line, helpVar[ix].varName)) != NULL)
                    {
                        if (helpVar[ix].varP == NULL)
                            continue;

                        LM_T(LmtHelp, ("found variable '%s'", helpVar[ix].varName));
                        ++changes;

                        strncpy(end, &tmp[strlen(helpVar[ix].varName)], sizeof(end));
                        *tmp = 0;
                        strncpy(start, line, sizeof(start));
                        snprintf(line, sizeof(line), "%s%s%s", start, helpVar[ix].varP, end);
                    }
                }
            } while (changes != 0);

            strncat(paResultString, line, sizeof(paResultString) - 1);
        }

        fclose(fP);
        strncat(paResultString,
                "\n---------------------------------------------\n",
                sizeof(paResultString) - 1);
    }
    else
    {
        paUsage();
    }
#endif
}