CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/cache/sources/wl/shared/miniopt.c
Views: 3959
1
/*
2
* Description.
3
*
4
* $Copyright Open Broadcom Corporation$
5
* $Id: miniopt.c 401759 2013-05-13 16:08:08Z sudhirbs $
6
*/
7
8
/* ---- Include Files ---------------------------------------------------- */
9
10
#include <typedefs.h>
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <string.h>
14
#include <miniopt.h>
15
16
17
/* ---- Public Variables ------------------------------------------------- */
18
/* ---- Private Constants and Types -------------------------------------- */
19
20
/* XXX FIXME: this should be moved to a common header file. */
21
#if defined(_CFE_)
22
#define fprintf(stream, fmt, args...) xprintf(fmt , ## args)
23
#elif defined(__IOPOS__)
24
#define fprintf(file, fmt, arg...) jtag_printf(fmt , ## arg)
25
#endif
26
27
28
/* ---- Private Variables ------------------------------------------------ */
29
/* ---- Private Function Prototypes -------------------------------------- */
30
/* ---- Functions -------------------------------------------------------- */
31
32
/* ----------------------------------------------------------------------- */
33
void
34
miniopt_init(miniopt_t *t, const char* name, const char* flags, bool longflags)
35
{
36
static const char *null_flags = "";
37
38
memset(t, 0, sizeof(miniopt_t));
39
t->name = name;
40
if (flags == NULL)
41
t->flags = null_flags;
42
else
43
t->flags = flags;
44
t->longflags = longflags;
45
}
46
47
48
/* ----------------------------------------------------------------------- */
49
int
50
miniopt(miniopt_t *t, char **argv)
51
{
52
int keylen;
53
char *p, *eq, *valstr, *endptr = NULL;
54
int err = 0;
55
56
t->consumed = 0;
57
t->positional = FALSE;
58
memset(t->key, 0, MINIOPT_MAXKEY);
59
t->opt = '\0';
60
t->valstr = NULL;
61
t->good_int = FALSE;
62
valstr = NULL;
63
64
if (*argv == NULL) {
65
err = -1;
66
goto exit;
67
}
68
69
p = *argv++;
70
t->consumed++;
71
72
if (!t->opt_end && !strcmp(p, "--")) {
73
t->opt_end = TRUE;
74
if (*argv == NULL) {
75
err = -1;
76
goto exit;
77
}
78
p = *argv++;
79
t->consumed++;
80
}
81
82
if (t->opt_end) {
83
t->positional = TRUE;
84
valstr = p;
85
}
86
else if (!strncmp(p, "--", 2)) {
87
eq = strchr(p, '=');
88
if (eq == NULL && !t->longflags) {
89
fprintf(stderr,
90
"%s: missing \" = \" in long param \"%s\"\n", t->name, p);
91
err = 1;
92
goto exit;
93
}
94
keylen = eq ? (eq - (p + 2)) : (int)strlen(p) - 2;
95
if (keylen > 63) keylen = 63;
96
memcpy(t->key, p + 2, keylen);
97
98
if (eq) {
99
valstr = eq + 1;
100
if (*valstr == '\0') {
101
fprintf(stderr,
102
"%s: missing value after \" = \" in long param \"%s\"\n",
103
t->name, p);
104
err = 1;
105
goto exit;
106
}
107
}
108
}
109
else if (!strncmp(p, "-", 1)) {
110
t->opt = p[1];
111
if (strlen(p) > 2) {
112
fprintf(stderr,
113
"%s: only single char options, error on param \"%s\"\n",
114
t->name, p);
115
err = 1;
116
goto exit;
117
}
118
if (strchr(t->flags, t->opt)) {
119
/* this is a flag option, no value expected */
120
valstr = NULL;
121
} else {
122
if (*argv == NULL) {
123
fprintf(stderr,
124
"%s: missing value parameter after \"%s\"\n", t->name, p);
125
err = 1;
126
goto exit;
127
}
128
valstr = *argv;
129
argv++;
130
t->consumed++;
131
}
132
} else {
133
t->positional = TRUE;
134
valstr = p;
135
}
136
137
/* parse valstr as int just in case */
138
if (valstr) {
139
t->uval = (uint)strtoul(valstr, &endptr, 0);
140
t->val = (int)t->uval;
141
t->good_int = (*endptr == '\0');
142
}
143
144
t->valstr = valstr;
145
146
exit:
147
if (err == 1)
148
t->opt = '?';
149
150
return err;
151
}
152
153