libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

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

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