ReactOS 0.4.15-dev-5672-gf73ac17
getopt.h
Go to the documentation of this file.
1
2// Copyright (C) Alexander Telyatnikov, Ivan Keliukh, Yegor Anchishkin, SKIF Software, 1999-2013. Kiev, Ukraine
3// All rights reserved
4// This file was released under the GPLv2 on June 2015.
6
7#ifndef _GETOPT_H
8#define _GETOPT_H
9
10#ifdef __cplusplus
11extern "C" {
12#endif
13
14#define BAD_OPTION '\0'
15
16typedef struct _optarg_ctx {
17 /* For communication from `getopt' to the caller.
18 When `getopt' finds an option that takes an argument,
19 the argument value is returned here.
20 Also, when `ordering' is RETURN_IN_ORDER,
21 each non-option ARGV-element is returned here. */
22
24
25 /* Index in ARGV of the next element to be scanned.
26 This is used for communication to and from the caller
27 and for communication between successive calls to `getopt'.
28
29 On entry to `getopt', zero means this is the first call; initialize.
30
31 When `getopt' returns EOF, this is the index of the first of the
32 non-option elements that the caller should itself scan.
33
34 Otherwise, `optind' communicates from one call to the next
35 how much of ARGV has been scanned so far. */
36
37 int optind;
38
39 /* The next char to be scanned in the option-element
40 in which the last option character we returned was found.
41 This allows us to pick up the scan where we left off.
42
43 If this is zero, or a null string, it means resume the scan
44 by advancing to the next ARGV-element. */
45
47
48 /* Callers store zero here to inhibit the error message
49 for unrecognized options. */
50
51 int opterr;
52
53 /* Set to an option character which was unrecognized.
54 This must be initialized on some systems to avoid linking in the
55 system's own getopt implementation. */
56
57 int optopt;
58
59 /* Describe the part of ARGV that contains non-options that have
60 been skipped. `first_nonopt' is the index in ARGV of the first of them;
61 `last_nonopt' is the index after the last of them. */
62
65
66 /* Exchange two adjacent subsequences of ARGV.
67 One subsequence is elements [first_nonopt,last_nonopt)
68 which contains all the non-options that have been skipped so far.
69 The other is elements [last_nonopt,optind), which contains all
70 the options processed since those non-options were skipped.
71
72 `first_nonopt' and `last_nonopt' are relocated so that they describe
73 the new indices of the non-options in ARGV after they are moved.
74
75 To perform the swap, we first reverse the order of all elements. So
76 all options now come before all non options, but they are in the
77 wrong order. So we put back the options and non options in original
78 order by reversing them again. For example:
79 original input: a b c -x -y
80 reverse all: -y -x c b a
81 reverse options: -x -y c b a
82 reverse non options: -x -y a b c
83 */
84
86
87/* Describe the long-named options requested by the application.
88 The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector
89 of `struct option' terminated by an element containing a name which is
90 zero.
91
92 The field `has_arg' is:
93 no_argument (or 0) if the option does not take an argument,
94 required_argument (or 1) if the option requires an argument,
95 optional_argument (or 2) if the option takes an optional argument.
96
97 If the field `flag' is not NULL, it points to a variable that is set
98 to the value given in the field `val' when the option is found, but
99 left unchanged if the option is not found.
100
101 To have a long-named option do something other than set an `int' to
102 a compiled-in constant, such as set a value from `optarg', set the
103 option's `flag' field to zero and its `val' field to a nonzero
104 value (the equivalent single-letter option character, if there is
105 one). For long options that have a zero `flag' field, `getopt'
106 returns the contents of the `val' field. */
107
108struct option
109{
111 /* has_arg can't be an enum because some compilers complain about
112 type mismatches in all the code that assumes it is an int. */
114 int *flag;
115 int val;
116};
117
118extern void getopt_init(optarg_ctx* o);
119
120/* Names for the values of the `has_arg' field of `struct option'. */
121
122#define no_argument 0
123#define required_argument 1
124#define optional_argument 2
125
126extern int getopt (optarg_ctx* o, int argc, WCHAR *const *argv, const WCHAR *shortopts);
127extern int getopt_long (optarg_ctx* o, int argc, WCHAR *const *argv, const WCHAR *shortopts,
128 const struct option *longopts, int *longind);
129extern int getopt_long_only (optarg_ctx* o, int argc, WCHAR *const *argv,
130 const WCHAR *shortopts,
131 const struct option *longopts, int *longind);
132
133/* Internal only. Users should not call this directly. */
134extern int _getopt_internal (optarg_ctx* o, int argc, WCHAR *const *argv,
135 const WCHAR *shortopts,
136 const struct option *longopts, int *longind,
137 int long_only);
138
139#ifdef __cplusplus
140}
141#endif
142
143#endif /* _GETOPT_H */
static int argc
Definition: ServiceArgs.c:12
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:55
void getopt_init(optarg_ctx *o)
Definition: getopt.cpp:61
int getopt_long(optarg_ctx *o, int argc, WCHAR *const *argv, const WCHAR *shortopts, const struct option *longopts, int *longind)
Definition: getopt.cpp:472
struct _optarg_ctx * P_optarg_ctx
struct _optarg_ctx optarg_ctx
int getopt_long_only(optarg_ctx *o, int argc, WCHAR *const *argv, const WCHAR *shortopts, const struct option *longopts, int *longind)
int _getopt_internal(optarg_ctx *o, int argc, WCHAR *const *argv, const WCHAR *shortopts, const struct option *longopts, int *longind, int long_only)
Definition: getopt.cpp:157
#define argv
Definition: mplay32.c:18
int optind
Definition: getopt.h:37
int opterr
Definition: getopt.h:51
WCHAR * optarg
Definition: getopt.h:23
WCHAR * nextchar
Definition: getopt.h:46
int last_nonopt
Definition: getopt.h:64
int first_nonopt
Definition: getopt.h:63
int optopt
Definition: getopt.h:57
Definition: getopt.h:109
int val
Definition: getopt.h:115
int * flag
Definition: getopt.h:114
int has_arg
Definition: getopt.h:113
WCHAR * name
Definition: getopt.h:110
__wchar_t WCHAR
Definition: xmlstorage.h:180