Beispiel #1
0
int parse_opts(int argc, char *argv[])
{
    int ch;

    bool fourandsix = false;
    bool delayed_flood = false;

    Flag_AI_Family = AF_UNSPEC;
    Flag_Count = DEFAULT_STATS_INTERVAL;
    Flag_Delay = DEFAULT_DELAY;
    Flag_Padding = sizeof(uint32_t);

    while ((ch = getopt(argc, argv, "46C:c:d:flNP:p:")) != -1) {
        switch (ch) {

        case '4':
            if (fourandsix) {
                warnx("need just one of -4 or -6");
                emit_usage();
            }
            Flag_AI_Family = AF_INET;
            fourandsix = true;
            break;
        case '6':
            if (fourandsix) {
                warnx("need just one of -4 or -6");
                emit_usage();
            }
            Flag_AI_Family = AF_INET6;
            fourandsix = true;
            break;

        case 'C':
            Flag_Max_Send = flagtoul(ch, optarg, 0UL, (unsigned long) INT_MAX);
            break;

        case 'c':
            Flag_Count = flagtoul(ch, optarg, 0UL, (unsigned long) INT_MAX);
            break;

        case 'd':
            if (delayed_flood) {
                warnx("cannot both delay and flood packets");
                emit_usage();
            }
            Flag_Delay =
                (unsigned int) flagtoul(ch, optarg, 0UL,
                                        (unsigned long) INT_MAX);
            Flag_Flood = 0;
            delayed_flood = true;
            break;

        case 'f':
            if (delayed_flood) {
                warnx("cannot both delay and flood packets");
                emit_usage();
            }
            Flag_Flood = true;
            delayed_flood = true;
            break;

        case 'l':
            Flag_Line_Buf = true;
            break;

        case 'N':
            Flag_Nanoseconds = true;
            break;

        case 'P':
            // NOTE greatly restrict max size of packet by default
            Flag_Padding = (size_t) flagtoul(ch, optarg, 0UL, 8192UL);
            // ... but do need a minimum size for the counter in the packet
            if (Flag_Padding < sizeof(uint32_t))
                Flag_Padding = sizeof(uint32_t);
            break;

        case 'p':
            Flag_Port = optarg;
            break;

        case 'h':
        default:
            emit_usage();
            /* NOTREACHED */
        }
    }

    if (!Flag_Port) {
        warnx("-p port option is mandatory");
        emit_usage();
    }

    return optind;
}
Beispiel #2
0
/*=export_func  optionParseShell
 * private:
 *
 * what:  Decipher a boolean value
 * arg:   + tOptions * + pOpts    + program options descriptor +
 *
 * doc:
 *  Emit a shell script that will parse the command line options.
=*/
void
optionParseShell(tOptions * opts)
{
    /*
     *  Check for our SHELL option now.
     *  IF the output file contains the "#!" magic marker,
     *  it will override anything we do here.
     */
    if (HAVE_GENSHELL_OPT(SHELL))
        shell_prog = GENSHELL_OPT_ARG(SHELL);

    else if (! ENABLED_GENSHELL_OPT(SHELL))
        shell_prog = NULL;

    else if ((shell_prog = getenv("SHELL")),
             shell_prog == NULL)

        shell_prog = POSIX_SHELL;

    /*
     *  Check for a specified output file
     */
    if (HAVE_GENSHELL_OPT(SCRIPT))
        open_out(GENSHELL_OPT_ARG(SCRIPT), opts->pzProgName);
    
    emit_usage(opts);
    emit_setup(opts);

    /*
     *  There are four modes of option processing.
     */
    switch (opts->fOptSet & (OPTPROC_LONGOPT|OPTPROC_SHORTOPT)) {
    case OPTPROC_LONGOPT:
        fputs(LOOP_STR,         stdout);

        fputs(LONG_OPT_MARK,    stdout);
        fputs(INIT_LOPT_STR,    stdout);
        emit_long(opts);
        printf(LOPT_ARG_FMT,    opts->pzPROGNAME);
        fputs(END_OPT_SEL_STR,  stdout);

        fputs(NOT_FOUND_STR,    stdout);
        break;

    case 0:
        fputs(ONLY_OPTS_LOOP,   stdout);
        fputs(INIT_LOPT_STR,    stdout);
        emit_long(opts);
        printf(LOPT_ARG_FMT,    opts->pzPROGNAME);
        break;

    case OPTPROC_SHORTOPT:
        fputs(LOOP_STR,         stdout);

        fputs(FLAG_OPT_MARK,    stdout);
        fputs(INIT_OPT_STR,     stdout);
        emit_flag(opts);
        printf(OPT_ARG_FMT,     opts->pzPROGNAME);
        fputs(END_OPT_SEL_STR,  stdout);

        fputs(NOT_FOUND_STR,    stdout);
        break;

    case OPTPROC_LONGOPT|OPTPROC_SHORTOPT:
        fputs(LOOP_STR,         stdout);

        fputs(LONG_OPT_MARK,    stdout);
        fputs(INIT_LOPT_STR,    stdout);
        emit_long(opts);
        printf(LOPT_ARG_FMT,    opts->pzPROGNAME);
        fputs(END_OPT_SEL_STR,  stdout);

        fputs(FLAG_OPT_MARK,    stdout);
        fputs(INIT_OPT_STR,     stdout);
        emit_flag(opts);
        printf(OPT_ARG_FMT,     opts->pzPROGNAME);
        fputs(END_OPT_SEL_STR,  stdout);

        fputs(NOT_FOUND_STR,    stdout);
        break;
    }

    emit_wrapup(opts);
    if ((script_trailer != NULL) && (*script_trailer != NUL))
        fputs(script_trailer, stdout);
    else if (ENABLED_GENSHELL_OPT(SHELL))
        printf(SHOW_PROG_ENV, opts->pzPROGNAME);

#ifdef HAVE_FCHMOD
    fchmod(STDOUT_FILENO, 0755);
#endif
    fclose(stdout);

    if (ferror(stdout))
        fserr_exit(opts->pzProgName, zwriting, zstdout_name);

    AGFREE(script_text);
    script_leader    = NULL;
    script_trailer   = NULL;
    script_text      = NULL;
}
Beispiel #3
0
/*=export_func  optionParseShell
 * private:
 *
 * what:  Decipher a boolean value
 * arg:   + tOptions* + pOpts    + program options descriptor +
 *
 * doc:
 *  Emit a shell script that will parse the command line options.
=*/
void
optionParseShell(tOptions * pOpts)
{
    /*
     *  Check for our SHELL option now.
     *  IF the output file contains the "#!" magic marker,
     *  it will override anything we do here.
     */
    if (HAVE_GENSHELL_OPT(SHELL))
        shell_prog = GENSHELL_OPT_ARG(SHELL);

    else if (! ENABLED_GENSHELL_OPT(SHELL))
        shell_prog = NULL;

    else if ((shell_prog = getenv("SHELL")),
             shell_prog == NULL)

        shell_prog = POSIX_SHELL;

    /*
     *  Check for a specified output file
     */
    if (HAVE_GENSHELL_OPT(SCRIPT))
        open_out(GENSHELL_OPT_ARG(SCRIPT));

    emit_usage(pOpts);
    emit_setup(pOpts);

    /*
     *  There are four modes of option processing.
     */
    switch (pOpts->fOptSet & (OPTPROC_LONGOPT|OPTPROC_SHORTOPT)) {
    case OPTPROC_LONGOPT:
        fputs(LOOP_STR,         stdout);

        fputs(LONG_OPT_MARK,    stdout);
        fputs(INIT_LOPT_STR,    stdout);
        emitLong(pOpts);
        printf(LOPT_ARG_FMT,    pOpts->pzPROGNAME);
        fputs(END_OPT_SEL_STR,  stdout);

        fputs(NOT_FOUND_STR,    stdout);
        break;

    case 0:
        fputs(ONLY_OPTS_LOOP,   stdout);
        fputs(INIT_LOPT_STR,    stdout);
        emitLong(pOpts);
        printf(LOPT_ARG_FMT,    pOpts->pzPROGNAME);
        break;

    case OPTPROC_SHORTOPT:
        fputs(LOOP_STR,         stdout);

        fputs(FLAG_OPT_MARK,    stdout);
        fputs(INIT_OPT_STR,     stdout);
        emit_flag(pOpts);
        printf(OPT_ARG_FMT,     pOpts->pzPROGNAME);
        fputs(END_OPT_SEL_STR,  stdout);

        fputs(NOT_FOUND_STR,    stdout);
        break;

    case OPTPROC_LONGOPT|OPTPROC_SHORTOPT:
        fputs(LOOP_STR,         stdout);

        fputs(LONG_OPT_MARK,    stdout);
        fputs(INIT_LOPT_STR,    stdout);
        emitLong(pOpts);
        printf(LOPT_ARG_FMT,    pOpts->pzPROGNAME);
        fputs(END_OPT_SEL_STR,  stdout);

        fputs(FLAG_OPT_MARK,    stdout);
        fputs(INIT_OPT_STR,     stdout);
        emit_flag(pOpts);
        printf(OPT_ARG_FMT,     pOpts->pzPROGNAME);
        fputs(END_OPT_SEL_STR,  stdout);

        fputs(NOT_FOUND_STR,    stdout);
        break;
    }

    printf(zLoopEnd, pOpts->pzPROGNAME, END_MARK);
    if ((script_trailer != NULL) && (*script_trailer != NUL))
        fputs(script_trailer, stdout);
    else if (ENABLED_GENSHELL_OPT(SHELL))
        printf(SHOW_PROG_ENV, pOpts->pzPROGNAME);

    fflush(stdout);
    fchmod(STDOUT_FILENO, 0755);
    fclose(stdout);
    if (ferror(stdout)) {
        fputs(zOutputFail, stderr);
        exit(EXIT_FAILURE);
    }
}