Beispiel #1
0
static int option_begin(const char* str)
{
	if (is_short_option(str))
		return short_option_begin(str);
	else if (is_long_option(str))
		return long_option_begin(str);
	assert(0);
	return 0;
}
Beispiel #2
0
static int find_long_index(char * const *argv)
{
   for (int index = 0; argv[index]; index++)
   {
      if (is_long_option(argv[index]))
         return index;
   }

   return -1;
}
Beispiel #3
0
static int find_long_index(char * const *argv)
{
   int idx;
   for (idx = 0; argv[idx]; idx++)
   {
      if (is_long_option(argv[idx]))
         return idx;
   }

   return -1;
}
Beispiel #4
0
int getopt_long_only(int argc, char * const argv[],
	const char *optstring,
	const struct option *longopts, int *longindex)
{
	if (!argc || !argv)
		return -1;

	getopt_param param = scan_param(optstring ? optstring : "");

	if (optind >= argc) {
		if (!param.posix && !param.process_all)
			swap_values(argc, (char**)argv, optind);
		return -1;
	}

	optarg = 0;

	if (!nextchar) {// we are in begin of argument... no previous iterations on this arg
		int retval;
		if (!detect_next_option(argc, argv, param, retval))
			return retval;

		const char* str = argv[optind];
		if (is_long_option(str)) {
			int _longindex;
			if (!longindex) // if no longindex specified - create own temp...
				longindex = &_longindex;
			return get_long_option(argc, argv, param, longopts, longindex);
		}
		else if (is_short_option(str)) {
			return get_long_short_option(argc, argv, param, longopts, longindex);
		}
	}

	return get_short_option(argc, argv, param);
}
Beispiel #5
0
static bool is_option(const char* str)
{
	return (is_short_option(str) || is_long_option(str));
}
int
main(int argc, char **argv)
{
    extern char *optarg;
    extern int optind, opterr, optopt;
    int option_index;
    int err_count;
    int optc;
    int rv;

    set_eprint_fh();
    program_path = *argv;
    program_name = sname(program_path);
    option_index = 0;
    err_count = 0;
    opterr = 0;

    while (true) {
        int this_option_optind;

        if (err_count > 10) {
            eprintf("%s: Too many option errors.\n", program_name);
            break;
        }

        this_option_optind = optind ? optind : 1;
        optc = getopt_long(argc, argv, "+hVdvEHw:", long_options, &option_index);
        if (optc == -1) {
            break;
        }

        rv = 0;
        if (optc == '?' && optopt == '?') {
            optc = 'h';
        }

        switch (optc) {
        case 'V':
            show_program_version();
            exit(0);
            break;
        case 'h':
            fputs(usage_text, stdout);
            exit(0);
            break;
        case 'd':
            debug = true;
            set_debug_fh(NULL);
            break;
        case 'v':
            verbose = true;
            break;
        case 'H':
            devolve_options |= (unsigned int)OPT_SOFT_HYPHENS;
            break;
        case 'c':
            devolve_options |= (unsigned int)OPT_SHOW_COUNTS;
            break;
        case '8':
            devolve_options |= (unsigned int)OPT_SHOW_8BIT;
            break;
        case 'C':
            if (variant_strcmp(optarg, "latin-1", VARIANT_WORDS) == 0) {
                charset = CHARSET_LATIN1;
            }
            else if (variant_strcmp(optarg, "iso-8859-1", VARIANT_WORDS) == 0) {
                charset = CHARSET_LATIN1;
            }
            else if (variant_strcmp(optarg, "utf-8", VARIANT_ACRONYM) == 0) {
                charset = CHARSET_UTF8;
            }
            else {
                eprintf("Unknown character set, '%s'\n", optarg);
                ++err_count;
            }
            break;
        case 't':
            devolve_options |= (unsigned int)OPT_TRACE_CONV;
            break;
        case 'e':
            devolve_options |= (unsigned int)OPT_TRACE_ERRORS;
            break;
        case 'u':
            devolve_options |= (unsigned int)OPT_TRACE_UNTRANS;
            break;
        case '?':
            eprint(program_name);
            eprint(": ");
            if (is_long_option(argv[this_option_optind])) {
                eprintf("unknown long option, '%s'\n",
                    argv[this_option_optind]);
            }
            else {
                char chrbuf[10];
                eprintf("unknown short option, '%s'\n",
                    vischar_r(chrbuf, sizeof (chrbuf), optopt));
            }
            ++err_count;
            break;
        default:
            eprintf("%s: INTERNAL ERROR: unknown option, '%c'\n",
                program_name, optopt);
            exit(64);
            break;
        }
    }

    if (debug) {
        verbose = true;
    }
    if (verbose) {
        devolve_options |= (unsigned int)OPT_SHOW_COUNTS;
    }

    if (optind < argc) {
        filec = (size_t) (argc - optind);
        filev = argv + optind;
    }
    else {
        filec = 0;
        filev = NULL;
    }

    if (verbose) {
        fshow_str_array(errprint_fh, filec, filev);
    }

    if (verbose && optind < argc) {
        eprintf("non-option ARGV-elements:\n");
        while (optind < argc) {
            eprintf("    %s\n", argv[optind]);
            ++optind;
        }
    }

    if (err_count != 0) {
        usage();
        exit(2);
    }

    if (filec) {
        rv = filev_probe(filec, filev);
        if (rv != 0) {
            exit(rv);
        }

        rv = devolve_filev(int_to_size(filec), filev, stdout);
    }
    else {
        char *fv_stdin = { "-" };
        rv = devolve_filev(1, &fv_stdin, stdout);
    }

    if (rv != 0) {
        exit(rv);
    }

    exit(0);
}