coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

coreutils/src/util/optget.c (96 lines, 2.6 KiB) -rw-r--r-- file download

cdfecb8 Jamozed 2022-03-06 15:11:25
0
// util/optget.h, version 1.6.2
b181413 Jamozed 2022-02-05 22:32:00
1
// optget source file from libutil
ca86cbe Jamozed 2020-06-26 14:06:57
2
// Copyright (C) 2020, Jakob Wakeling
cdfecb8 Jamozed 2022-03-06 15:11:25
3
// MIT Licence
ca86cbe Jamozed 2020-06-26 14:06:57
4
30cf081 Jamozed 2020-08-28 23:01:02
5
#include "error.h"
30cf081 Jamozed 2020-08-28 23:01:02
6
#include "optget.h"
ca86cbe Jamozed 2020-06-26 14:06:57
7
30cf081 Jamozed 2020-08-28 23:01:02
8
#include <stddef.h>
ca86cbe Jamozed 2020-06-26 14:06:57
9
#include <stdio.h>
ca86cbe Jamozed 2020-06-26 14:06:57
10
#include <string.h>
ca86cbe Jamozed 2020-06-26 14:06:57
11
30cf081 Jamozed 2020-08-28 23:01:02
12
#define cur av[opt->ind]
ca86cbe Jamozed 2020-06-26 14:06:57
13
30cf081 Jamozed 2020-08-28 23:01:02
14
const struct opt OPTGET_INIT = { 1, 0, 1, 0, NULL, NULL, NULL, NULL };
ca86cbe Jamozed 2020-06-26 14:06:57
15
30cf081 Jamozed 2020-08-28 23:01:02
16
static inline void permute(char **av, int i, int n);
ca86cbe Jamozed 2020-06-26 14:06:57
17
30cf081 Jamozed 2020-08-28 23:01:02
18
int optget(struct opt *opt, char *av[], int flags) {
ca86cbe Jamozed 2020-06-26 14:06:57
19
	if (flags & 1) {
ca86cbe Jamozed 2020-06-26 14:06:57
20
		for (; cur && (cur[0] != '-' || cur[1] == 0); ++opt->ind, ++opt->nop);
ca86cbe Jamozed 2020-06-26 14:06:57
21
		if (!cur) { opt->ind -= opt->nop; opt->nop = 0; return -1; }
ca86cbe Jamozed 2020-06-26 14:06:57
22
	}
ca86cbe Jamozed 2020-06-26 14:06:57
23
	else if (!cur || (cur[0] != '-' || cur[1] == 0)) { return -1; }
ca86cbe Jamozed 2020-06-26 14:06:57
24
	
ca86cbe Jamozed 2020-06-26 14:06:57
25
	int optind = opt->ind, optret;
ca86cbe Jamozed 2020-06-26 14:06:57
26
	
ca86cbe Jamozed 2020-06-26 14:06:57
27
	if (cur[1] == '-') {
ca86cbe Jamozed 2020-06-26 14:06:57
28
		if (cur[2] == 0) { if (opt->nop) {
30cf081 Jamozed 2020-08-28 23:01:02
29
				permute(av, opt->ind++, opt->nop);
ca86cbe Jamozed 2020-06-26 14:06:57
30
				opt->ind -= opt->nop; opt->nop = 0;
ca86cbe Jamozed 2020-06-26 14:06:57
31
			} else { ++opt->ind; } return -1;
ca86cbe Jamozed 2020-06-26 14:06:57
32
		}
ca86cbe Jamozed 2020-06-26 14:06:57
33
		
ca86cbe Jamozed 2020-06-26 14:06:57
34
		int optend, lop; optret = '?'; opt->opt = 0; opt->lop = cur;
ca86cbe Jamozed 2020-06-26 14:06:57
35
		if (!opt->lops) { goto nol; }
30cf081 Jamozed 2020-08-28 23:01:02
36
		for (optend = 2; cur[optend] != '=' && cur[optend] != 0; ++optend);
ca86cbe Jamozed 2020-06-26 14:06:57
37
		
ca86cbe Jamozed 2020-06-26 14:06:57
38
		for (lop = 0; opt->lops[lop].str; ++lop) {
ca86cbe Jamozed 2020-06-26 14:06:57
39
			if (strncmp(&cur[2], opt->lops[lop].str, (size_t)optend - 2) == 0) {
ca86cbe Jamozed 2020-06-26 14:06:57
40
				if (!opt->lops[lop].str[optend - 2]) {
30cf081 Jamozed 2020-08-28 23:01:02
41
					optret = opt->opt = opt->lops[lop].val; break;
ca86cbe Jamozed 2020-06-26 14:06:57
42
				}
ca86cbe Jamozed 2020-06-26 14:06:57
43
			}
ca86cbe Jamozed 2020-06-26 14:06:57
44
		}
ca86cbe Jamozed 2020-06-26 14:06:57
45
		
ca86cbe Jamozed 2020-06-26 14:06:57
46
		if (opt->lops[lop].arg > ARG_NUL) {
ca86cbe Jamozed 2020-06-26 14:06:57
47
			if (cur[optend]) { opt->arg = &cur[optend + 1]; }
30cf081 Jamozed 2020-08-28 23:01:02
48
			else if (av[opt->ind + 1]) { opt->arg = av[++opt->ind]; }
ca86cbe Jamozed 2020-06-26 14:06:57
49
			else {
ca86cbe Jamozed 2020-06-26 14:06:57
50
				if (opt->lops[lop].arg == ARG_REQ) { optret = ':'; }
ca86cbe Jamozed 2020-06-26 14:06:57
51
				opt->arg = NULL;
ca86cbe Jamozed 2020-06-26 14:06:57
52
			}
ca86cbe Jamozed 2020-06-26 14:06:57
53
		}
ca86cbe Jamozed 2020-06-26 14:06:57
54
		else { opt->arg = NULL; }
ca86cbe Jamozed 2020-06-26 14:06:57
55
		
ca86cbe Jamozed 2020-06-26 14:06:57
56
nol:	opt->pos = 0;
ca86cbe Jamozed 2020-06-26 14:06:57
57
	}
ca86cbe Jamozed 2020-06-26 14:06:57
58
	else {
ca86cbe Jamozed 2020-06-26 14:06:57
59
		optret = opt->opt = cur[opt->pos++]; opt->lop = NULL;
ca86cbe Jamozed 2020-06-26 14:06:57
60
		const char *optchr = strchr(opt->str, opt->opt);
ca86cbe Jamozed 2020-06-26 14:06:57
61
		
ca86cbe Jamozed 2020-06-26 14:06:57
62
		if (!optchr) { optret = '?'; }
ca86cbe Jamozed 2020-06-26 14:06:57
63
		else if (optchr[1] == ':') {
ca86cbe Jamozed 2020-06-26 14:06:57
64
			if (cur[opt->pos]) { opt->arg = &cur[opt->pos]; }
30cf081 Jamozed 2020-08-28 23:01:02
65
			else if (av[opt->ind + 1]) { opt->arg = av[++opt->ind]; }
ca86cbe Jamozed 2020-06-26 14:06:57
66
			else { opt->arg = NULL; optret = ':'; }
ca86cbe Jamozed 2020-06-26 14:06:57
67
			opt->pos = 0;
ca86cbe Jamozed 2020-06-26 14:06:57
68
		}
ca86cbe Jamozed 2020-06-26 14:06:57
69
		else { opt->arg = NULL; }
ca86cbe Jamozed 2020-06-26 14:06:57
70
	}
ca86cbe Jamozed 2020-06-26 14:06:57
71
	
ca86cbe Jamozed 2020-06-26 14:06:57
72
	if (!opt->pos || !cur[opt->pos]) {
ca86cbe Jamozed 2020-06-26 14:06:57
73
		++opt->ind; opt->pos = 1;
ca86cbe Jamozed 2020-06-26 14:06:57
74
		if (opt->nop) for (; optind < opt->ind; ++optind) {
30cf081 Jamozed 2020-08-28 23:01:02
75
			permute(av, optind, opt->nop);
ca86cbe Jamozed 2020-06-26 14:06:57
76
		}
ca86cbe Jamozed 2020-06-26 14:06:57
77
	}
ca86cbe Jamozed 2020-06-26 14:06:57
78
	
ca86cbe Jamozed 2020-06-26 14:06:57
79
	if (optret == '?' && opt->str[0] != ':') {
30cf081 Jamozed 2020-08-28 23:01:02
80
		if (opt->opt) { warn("%c: invalid option", opt->opt); }
30cf081 Jamozed 2020-08-28 23:01:02
81
		else if (opt->lop) { warn("%s: invalid option", opt->lop); }
ca86cbe Jamozed 2020-06-26 14:06:57
82
	}
ca86cbe Jamozed 2020-06-26 14:06:57
83
	if (optret == ':' && opt->str[0] != ':') {
30cf081 Jamozed 2020-08-28 23:01:02
84
		if (opt->opt) { warn("%c: option requires argument", opt->opt); }
30cf081 Jamozed 2020-08-28 23:01:02
85
		else if (opt->lop) { warn("%s: option requires argument", opt->lop); }
ca86cbe Jamozed 2020-06-26 14:06:57
86
	}
ca86cbe Jamozed 2020-06-26 14:06:57
87
	
ca86cbe Jamozed 2020-06-26 14:06:57
88
	return optret;
ca86cbe Jamozed 2020-06-26 14:06:57
89
}
ca86cbe Jamozed 2020-06-26 14:06:57
90
30cf081 Jamozed 2020-08-28 23:01:02
91
static inline void permute(char **av, int i, int n) {
30cf081 Jamozed 2020-08-28 23:01:02
92
	char *a = av[i]; memmove(&av[i - n + 1], &av[i - n], n * sizeof (av));
30cf081 Jamozed 2020-08-28 23:01:02
93
	av[i - n] = a; return;
ca86cbe Jamozed 2020-06-26 14:06:57
94
}
95