diff options
| author | Tyge Løvset <[email protected]> | 2020-03-30 21:05:02 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-03-30 21:05:02 +0200 |
| commit | 17c850ae9704f4d64817ec2f749aa38e5ecfab10 (patch) | |
| tree | 949458d17358d3494af83c1dc5dc8f8e27e93532 | |
| parent | 373136c058b02f3cd1e545e64ea8a2891a6b43a6 (diff) | |
| download | STC-modified-17c850ae9704f4d64817ec2f749aa38e5ecfab10.tar.gz STC-modified-17c850ae9704f4d64817ec2f749aa38e5ecfab10.zip | |
Add files via upload
| -rw-r--r-- | c_lib/cgetopt.h | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/c_lib/cgetopt.h b/c_lib/cgetopt.h index ba2d64ff..ae154603 100644 --- a/c_lib/cgetopt.h +++ b/c_lib/cgetopt.h @@ -32,27 +32,27 @@ enum { copt_optional_argument = 2
};
typedef struct {
- int ind; /* equivalent to optind */
- int opt; /* equivalent to optopt */
- char *arg; /* equivalent to optarg */
- int longidx; /* index of a long option; or -1 if short */
- int _i, _pos, _nargs;
+ int ind; /* equivalent to optind */
+ int opt; /* equivalent to optopt */
+ char *arg; /* equivalent to optarg */
+ int longidx; /* index of a long option; or -1 if short */
+ int _i, _pos, _nargs;
} copt_t;
struct copt_option {
- char *name;
- int has_arg;
- int val;
+ char *name;
+ int has_arg;
+ int val;
};
static const copt_t copt_init = {1, 0, 0, -1, 1, 0, 0};
static void _copt_permute(char *argv[], int j, int n) { /* move argv[j] over n elements to the left */
- int k;
- char *p = argv[j];
- for (k = 0; k < n; ++k)
- argv[j - k] = argv[j - k - 1];
- argv[j - k] = p;
+ int k;
+ char *p = argv[j];
+ for (k = 0; k < n; ++k)
+ argv[j - k] = argv[j - k - 1];
+ argv[j - k] = p;
}
@@ -78,71 +78,71 @@ static void _copt_permute(char *argv[], int j, int n) { /* move argv[j] over n e static int copt_getopt(copt_t *opt, int argc, char *argv[],
const char *shortopts, const struct copt_option *longopts,
bool permute) {
- int optc = -1, i0, j;
- if (permute) {
- while (opt->_i < argc && (argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0'))
- ++opt->_i, ++opt->_nargs;
- }
- opt->arg = 0, opt->longidx = -1, i0 = opt->_i;
- if (opt->_i >= argc || argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0') {
- opt->ind = opt->_i - opt->_nargs;
- return -1;
- }
- if (argv[opt->_i][0] == '-' && argv[opt->_i][1] == '-') { /* "--" or a long option */
- if (argv[opt->_i][2] == '\0') { /* a bare "--" */
- _copt_permute(argv, opt->_i, opt->_nargs);
- ++opt->_i, opt->ind = opt->_i - opt->_nargs;
- return -1;
- }
- opt->opt = 0, optc = '?', opt->_pos = -1;
- if (longopts) { /* parse long options */
- int k, n_exact = 0, n_partial = 0;
- const struct copt_option *o = 0, *o_exact = 0, *o_partial = 0;
- for (j = 2; argv[opt->_i][j] != '\0' && argv[opt->_i][j] != '='; ++j) {} /* find the end of the option name */
- for (k = 0; longopts[k].name != 0; ++k)
- if (strncmp(&argv[opt->_i][2], longopts[k].name, j - 2) == 0) {
- if (longopts[k].name[j - 2] == 0) ++n_exact, o_exact = &longopts[k];
- else ++n_partial, o_partial = &longopts[k];
- }
- if (n_exact > 1 || (n_exact == 0 && n_partial > 1)) return '?';
- o = n_exact == 1? o_exact : n_partial == 1? o_partial : 0;
- if (o) {
- opt->opt = optc = o->val, opt->longidx = o - longopts;
+ int optc = -1, i0, j;
+ if (permute) {
+ while (opt->_i < argc && (argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0'))
+ ++opt->_i, ++opt->_nargs;
+ }
+ opt->arg = 0, opt->longidx = -1, i0 = opt->_i;
+ if (opt->_i >= argc || argv[opt->_i][0] != '-' || argv[opt->_i][1] == '\0') {
+ opt->ind = opt->_i - opt->_nargs;
+ return -1;
+ }
+ if (argv[opt->_i][0] == '-' && argv[opt->_i][1] == '-') { /* "--" or a long option */
+ if (argv[opt->_i][2] == '\0') { /* a bare "--" */
+ _copt_permute(argv, opt->_i, opt->_nargs);
+ ++opt->_i, opt->ind = opt->_i - opt->_nargs;
+ return -1;
+ }
+ opt->opt = 0, optc = '?', opt->_pos = -1;
+ if (longopts) { /* parse long options */
+ int k, n_exact = 0, n_partial = 0;
+ const struct copt_option *o = 0, *o_exact = 0, *o_partial = 0;
+ for (j = 2; argv[opt->_i][j] != '\0' && argv[opt->_i][j] != '='; ++j) {} /* find the end of the option name */
+ for (k = 0; longopts[k].name != 0; ++k)
+ if (strncmp(&argv[opt->_i][2], longopts[k].name, j - 2) == 0) {
+ if (longopts[k].name[j - 2] == 0) ++n_exact, o_exact = &longopts[k];
+ else ++n_partial, o_partial = &longopts[k];
+ }
+ if (n_exact > 1 || (n_exact == 0 && n_partial > 1)) return '?';
+ o = n_exact == 1? o_exact : n_partial == 1? o_partial : 0;
+ if (o) {
+ opt->opt = optc = o->val, opt->longidx = o - longopts;
if (o->has_arg != copt_no_argument) {
if (argv[opt->_i][j] == '=')
opt->arg = &argv[opt->_i][j + 1];
else if (argv[opt->_i][j] == '\0' && opt->_i < argc - 1 && argv[opt->_i + 1][0] != '-')
opt->arg = argv[++opt->_i];
- else if (o->has_arg == copt_required_argument)
+ else if (o->has_arg == copt_required_argument)
optc = ':'; /* missing option argument */
- }
- }
- }
- } else { /* a short option */
- const char *p;
- if (opt->_pos == 0) opt->_pos = 1;
- optc = opt->opt = argv[opt->_i][opt->_pos++];
- p = strchr((char *) shortopts, optc);
- if (p == 0) {
- optc = '?'; /* unknown option */
- } else if (p[1] == ':') {
- if (argv[opt->_i][opt->_pos] != '\0')
+ }
+ }
+ }
+ } else { /* a short option */
+ const char *p;
+ if (opt->_pos == 0) opt->_pos = 1;
+ optc = opt->opt = argv[opt->_i][opt->_pos++];
+ p = strchr((char *) shortopts, optc);
+ if (p == 0) {
+ optc = '?'; /* unknown option */
+ } else if (p[1] == ':') {
+ if (argv[opt->_i][opt->_pos] != '\0')
opt->arg = &argv[opt->_i][opt->_pos];
- else if (opt->_i < argc - 1 && argv[opt->_i + 1][0] != '-')
+ else if (opt->_i < argc - 1 && argv[opt->_i + 1][0] != '-')
opt->arg = argv[++opt->_i];
else if (p[2] != ':')
- optc = ':'; // missing option argument
- opt->_pos = -1;
- }
- }
- if (opt->_pos < 0 || argv[opt->_i][opt->_pos] == 0) {
- ++opt->_i, opt->_pos = 0;
- if (opt->_nargs > 0) /* permute */
- for (j = i0; j < opt->_i; ++j)
- _copt_permute(argv, j, opt->_nargs);
- }
- opt->ind = opt->_i - opt->_nargs;
- return optc;
+ optc = ':'; // missing option argument
+ opt->_pos = -1;
+ }
+ }
+ if (opt->_pos < 0 || argv[opt->_i][opt->_pos] == 0) {
+ ++opt->_i, opt->_pos = 0;
+ if (opt->_nargs > 0) /* permute */
+ for (j = i0; j < opt->_i; ++j)
+ _copt_permute(argv, j, opt->_nargs);
+ }
+ opt->ind = opt->_i - opt->_nargs;
+ return optc;
}
/* // demo:
|
