Exemple #1
0
/***************************************************************************
 * Routine to scan the CtrlStr, upto Max and count the number of parameters
 * to that point:
 * 1. Each option is counted as one parameter - boolean variable (int)
 * 2. Within an option, each %? or !? is counted once - pointer to something
 * 3. Within an option, %*? or !*? is counted twice - one for item count
 * and one for pointer to block pointers.
 * Note ALL variables are passed by address and so of fixed size (address).
 **************************************************************************/
static void
GASetParamCount(char *CtrlStr,
                int Max,
                int *ParamCount) {
    int i;

    *ParamCount = 0;
    for (i = 0; i < Max; i++)
        if (ISCTRLCHAR(CtrlStr[i])) {
            if (CtrlStr[i + 1] == '*')
                *ParamCount += 2;
            else
                (*ParamCount)++;
        }
}
Exemple #2
0
/***************************************************************************
 Routine to search for unsatisfied flags - simply scan the list for !- 
 sequence. Before this scan, this routine updates the rest of the command
 line into the last two parameters if it is requested by the CtrlStr 
 (last item in CtrlStr is NOT an option). 
 Return ARG_OK if all satisfied, CMD_ERR_AllSatis error else. 
***************************************************************************/
static int
GATestAllSatis(char *CtrlStrCopy,
               char *CtrlStr,
               int *argc,
               char ***argv,
               int *Parameters[MAX_PARAM],
               int *ParamCount) {

    int i;
    static char *LocalToken = NULL;

    /* If LocalToken is not initialized - do it now. Note that this string
     * should be writable as well so we can not assign it directly.
     */
    if (LocalToken == NULL) {
        LocalToken = (char *)malloc(3);
        strcpy(LocalToken, "-?");
    }

    /* Check if last item is an option. If not then copy rest of command
     * line into it as 1. NumOfprm, 2. pointer to block of pointers.
     */
    for (i = strlen(CtrlStr) - 1; i > 0 && !ISSPACE(CtrlStr[i]); i--) ;
    if (!ISCTRLCHAR(CtrlStr[i + 2])) {
        GASetParamCount(CtrlStr, i, ParamCount); /* Point in correct param. */
        *Parameters[(*ParamCount)++] = *argc;
        memcpy((void *)Parameters[(*ParamCount)++], (void *)argv,
                   sizeof(char *));
    }

    i = 0;
    while (++i < (int)strlen(CtrlStrCopy))
        if ((CtrlStrCopy[i] == '-') && (CtrlStrCopy[i - 1] == '!')) {
            GAErrorToken = LocalToken;
            LocalToken[1] = CtrlStrCopy[i - 2];    /* Set the correct flag. */
            return CMD_ERR_AllSatis;
        }

    return ARG_OK;
}
Exemple #3
0
/***************************************************************************
 * Routine to print correct format of command line allowed:
 **************************************************************************/
void
GAPrintHowTo(char *CtrlStr) {

    int i = 0, SpaceFlag;

    fprintf(stderr, "Usage: ");
    /* Print program name - first word in ctrl. str. (optional): */
    while (!(ISSPACE(CtrlStr[i])) && (!ISCTRLCHAR(CtrlStr[i + 1])))
        fprintf(stderr, "%c", CtrlStr[i++]);

    while (i < (int)strlen(CtrlStr)) {
        while ((ISSPACE(CtrlStr[i])) && (i < (int)strlen(CtrlStr)))
            i++;
        switch (CtrlStr[i + 1]) {
          case '%':
              fprintf(stderr, " [-%c", CtrlStr[i++]);
              i += 2;    /* Skip the '%-' or '!- after the char! */
              SpaceFlag = TRUE;
              while (!ISCTRLCHAR(CtrlStr[i]) && (i < (int)strlen(CtrlStr)) &&
                     (!ISSPACE(CtrlStr[i])))
                  if (SpaceFlag) {
                      if (CtrlStr[i++] == SPACE_CHAR)
                          fprintf(stderr, " ");
                      else
                          fprintf(stderr, " %c", CtrlStr[i - 1]);
                      SpaceFlag = FALSE;
                  } else if (CtrlStr[i++] == SPACE_CHAR)
                      fprintf(stderr, " ");
                  else
                      fprintf(stderr, "%c", CtrlStr[i - 1]);
              while (!ISSPACE(CtrlStr[i]) && (i < (int)strlen(CtrlStr))) {
                  if (CtrlStr[i] == '*')
                      fprintf(stderr, "...");
                  i++;    /* Skip the rest of it. */
              }
              fprintf(stderr, "]");
              break;
          case '!':
              fprintf(stderr, " -%c", CtrlStr[i++]);
              i += 2;    /* Skip the '%-' or '!- after the char! */
              SpaceFlag = TRUE;
              while (!ISCTRLCHAR(CtrlStr[i]) && (i < (int)strlen(CtrlStr)) &&
                     (!ISSPACE(CtrlStr[i])))
                  if (SpaceFlag) {
                      if (CtrlStr[i++] == SPACE_CHAR)
                          fprintf(stderr, " ");
                      else
                          fprintf(stderr, " %c", CtrlStr[i - 1]);
                      SpaceFlag = FALSE;
                  } else if (CtrlStr[i++] == SPACE_CHAR)
                      fprintf(stderr, " ");
                  else
                      fprintf(stderr, "%c", CtrlStr[i - 1]);
              while (!ISSPACE(CtrlStr[i]) && (i < (int)strlen(CtrlStr))) {
                  if (CtrlStr[i] == '*')
                      fprintf(stderr, "...");
                  i++;    /* Skip the rest of it. */
              }
              break;
          default:    /* Not checked, but must be last one! */
              fprintf(stderr, " ");
              while (!ISSPACE(CtrlStr[i]) && (i < (int)strlen(CtrlStr)) &&
                     !ISCTRLCHAR(CtrlStr[i]))
                  fprintf(stderr, "%c", CtrlStr[i++]);
              fprintf(stderr, "\n");
              return;
        }
    }
    fprintf(stderr, "\n");
}
Exemple #4
0
//#ifdef HAVE_STDARG_H
int
GAGetArgs(int argc,
        char **argv,
        char *CtrlStr, ...) {

    int i, Error = FALSE, ParamCount = 0;
    int *Parameters[MAX_PARAM];     /* Save here parameter addresses. */
    char *Option, CtrlStrCopy[CTRL_STR_MAX_LEN];
    va_list ap;

    strcpy(CtrlStrCopy, CtrlStr);
    va_start(ap, CtrlStr);
    for (i = 1; i <= MAX_PARAM; i++)
        Parameters[i - 1] = va_arg(ap, int *);
    va_end(ap);

#if 0//#elif defined(HAVE_VARARGS_H)
int GAGetArgs(va_alist)
    va_dcl
{
    va_list ap;
    int argc, i, Error = FALSE, ParamCount = 0;
    int *Parameters[MAX_PARAM];     /* Save here parameter addresses. */
    char **argv, *CtrlStr, *Option, CtrlStrCopy[CTRL_STR_MAX_LEN];

    va_start(ap);

    argc = va_arg(ap, int);
    argv = va_arg(ap, char **);
    CtrlStr = va_arg(ap, char *);

    va_end(ap);

    strcpy(CtrlStrCopy, CtrlStr);

    /* Using base address of parameters we access other parameters addr:
     * Note that me (for sure!) samples data beyond the current function
     * frame, but we accesson these set address only by demand. */
    for (i = 1; i <= MAX_PARAM; i++)
        Parameters[i - 1] = va_arg(ap, int *);
#endif /* HAVE_STDARG_H */

    --argc;
    argv++;    /* Skip the program name (first in argv/c list). */
    while (argc >= 0) {
        if (!GAOptionExists(argc, argv))
            break;    /* The loop. */
        argc--;
        Option = *argv++;
        if ((Error = GAUpdateParameters(Parameters, &ParamCount, Option,
                                        CtrlStrCopy, CtrlStr, &argc,
                                        &argv)) != FALSE)
            return Error;
    }
    /* Check for results and update trail of command line: */
    return GATestAllSatis(CtrlStrCopy, CtrlStr, &argc, &argv, Parameters,
                          &ParamCount);
}

/***************************************************************************
 * Routine to search for unsatisfied flags - simply scan the list for !- 
 * sequence. Before this scan, this routine updates the rest of the command
 * line into the last two parameters if it is requested by the CtrlStr 
 * (last item in CtrlStr is NOT an option). 
 * Return ARG_OK if all satisfied, CMD_ERR_AllSatis error else. 
 **************************************************************************/
static int
GATestAllSatis(char *CtrlStrCopy,
               char *CtrlStr,
               int *argc,
               char ***argv,
               int *Parameters[MAX_PARAM],
               int *ParamCount) {

    int i;
    static char *LocalToken = NULL;

    /* If LocalToken is not initialized - do it now. Note that this string
     * should be writable as well so we can not assign it directly.
     */
    if (LocalToken == NULL) {
        LocalToken = (char *)malloc(3);
        strcpy(LocalToken, "-?");
    }

    /* Check if last item is an option. If not then copy rest of command
     * line into it as 1. NumOfprm, 2. pointer to block of pointers.
     */
    for (i = strlen(CtrlStr) - 1; i > 0 && !ISSPACE(CtrlStr[i]); i--) ;
    if (!ISCTRLCHAR(CtrlStr[i + 2])) {
        GASetParamCount(CtrlStr, i, ParamCount); /* Point in correct param. */
        *Parameters[(*ParamCount)++] = *argc;
        GAByteCopy((char *)Parameters[(*ParamCount)++], (char *)argv,
                   sizeof(char *));
    }

    i = 0;
    while (++i < (int)strlen(CtrlStrCopy))
        if ((CtrlStrCopy[i] == '-') && (CtrlStrCopy[i - 1] == '!')) {
            GAErrorToken = LocalToken;
            LocalToken[1] = CtrlStrCopy[i - 2];    /* Set the correct flag. */
            return CMD_ERR_AllSatis;
        }

    return ARG_OK;
}