www.pudn.com > Linuxserial.zip > args.c


/*
 * File         : args.c
 * Date         : 2002-04-20
 * Author       : yfy001
 * Description  : process basic arguments of main function
 * Copyright (C) 2001, 2002  yfy001
 */
#include               // printf
#include              // CHAR_MAX
#include              // getopt_long
#include              // atoi
#include              // strrchr
#include               // toupper

#include "types.h"
#include "args.h"

static void args_usage(INT8 status, const char *program_name, INT8 eargc);
static void args_version(FILE * stream, const p_pkginfo_t p_pkginfo);

/*
 * Arguments    : int argc, char **argv, INT8 eargc
 * Return value : none
 * Description  :
 * 		
 */
void args_parse(int argc, char **argv, INT8 eargc)
{
    enum LONG_OPTIONS {
        ARG_HELP = CHAR_MAX + 1,
        ARG_VERSION
    };

    int c;
    int option_index = 0;
    static struct option long_options[] = {
        {"baudrate", required_argument, 0, 'b'},
        {"databit", required_argument, 0, 'd'},
        {"debug", no_argument, 0, 'D'},
        {"echo", no_argument, 0, 'E'},
        {"flowcontrol", required_argument, 0, 'f'},
        {"parity", required_argument, 0, 'p'},
        {"stopbit", required_argument, 0, 's'},
        {"tty", required_argument, 0, 't'},
        {"prompt", no_argument, 0, 'r'},
        {"help", no_argument, 0, ARG_HELP},
        {"version", no_argument, 0, ARG_VERSION},
        {0, 0, 0, 0}
    };

    static const char *short_options = "b:d:DEf:p:r:s:t:";
    while (-1 != (c = getopt_long(argc, argv, short_options, long_options,
                                  &option_index))) {
        switch (c) {
        case 0:
            break;
        case 'b':
            p_args->p_portinfo->baudrate = atoi(optarg);
            break;
        case 'd':              // databit = 5, 6, 7, 8
            p_args->p_portinfo->databit = (INT8) atoi(optarg) % 9 % 5 + 5;
            break;
        case 'D':
            p_args->p_portinfo->debug = 1;
            break;
        case 'E':
            p_args->p_portinfo->echo = 1;
            break;
        case 'f':              // flowcontrol = 0, 1, 2
            p_args->p_portinfo->fctrl = (INT8) atoi(optarg) % 3;
            break;
        case 'p':              // parity = 0, 1, 2
            p_args->p_portinfo->parity = (INT8) atoi(optarg) % 3;
            break;
        case 'r':              // print prompt after receiving data
            p_args->p_portinfo->prompt = (INT8) atoi(optarg) % 2;
            break;
        case 's':
            p_args->p_portinfo->stopbit = (INT8) atoi(optarg) % 3;
            break;
        case 't':              // tty = 0, 1, 2, 3, 4, 5, 6, 7
            p_args->p_portinfo->tty = (INT8) atoi(optarg) % 8;
            break;
        case ARG_HELP:
            args_usage(0, p_args->p_pkginfo->program_name, eargc);
            break;
        case ARG_VERSION:
            args_version(stdout, p_args->p_pkginfo);
            break;
        default:
            break;
        }
    }
    if (argc < optind + eargc) {
        args_usage(1, p_args->p_pkginfo->program_name, eargc);
    }
}

/* 
 * Arguments    : INT8 status, const char *program_name, INT8 eargc
 * Return value : none
 * Description  : The name this program was run with.
 */
static void args_usage(INT8 status, const char *program_name, INT8 eargc)
{
    if (status) {
        fprintf(stderr, "Try `%s --help' for more information.\n",
                program_name);
        exit(status);
    }
    printf("Usage: %s [OPTION] %s\n", program_name, eargc ? "SOURCE" : "");
    printf("\n\
  -b, --baudrate=BAUDRATE   set baudrate to BAUDRATE, default: 115200\n\
  -d, --databit=DATABIT     set databit, 5, 6, 7, 8; default: 8\n\
  -D, --debug               set debug mode\n\
  -E, --echo                set echo mode\n\
  -f, --flowcontrol         set flow control, 0:none, 1:hardware, 2:software\n\
                            default: 1 (hardware)\n\
  -p, --parity=PARITY       set parity to PARITY, 0:none, 1:odd, 2:even\n\
                            default: 0 (none)\n\
  -r, --prompt=PROMPT       whether to print prompt after receiving data\n\
                            0: no prompt, 1: prompt; default: 0\n\
  -s, --stopbit=STOPBIT     set stopbit, 1, 2; default: 1\n\
  -t, --tty=TTY             set tty to TTY, default: 0 [/dev/ttyS0: COM1]\n\
      --help                display this help and exit\n\
      --version             output version information and exit\n\n\
Examples:\n  %s --baudrate 115200 --tty=1 --debug %s \n", program_name, eargc ? "*.c" : "");
    puts("\nReport bugs to .");
    exit(status);
}

/*
 * Arguments    : FILE *stream, const p_pkginfo_t p_pkginfo 
 * Return value : none
 * Description  : show version of the caller
 */
static void args_version(FILE * stream, const p_pkginfo_t p_pkginfo)
{
    char *version_etc_copyright = _("Copyright (C) 2002 Paul Studio.");
    if (p_pkginfo->program_name) {
        fprintf(stream, "%s (%s) %s\n", p_pkginfo->program_name,
                p_pkginfo->package, p_pkginfo->version);
    } else {
        fprintf(stream, "%s %s\n", p_pkginfo->package, p_pkginfo->version);
    }
    fprintf(stream, _("Written by %s.\n"), p_pkginfo->authors);
    putc('\n', stream);
    fputs(version_etc_copyright, stream);
    putc('\n', stream);
    fputs(_("\
This is free software; see the source for copying conditions.  There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"), stream);
    exit(0);
}