ReactOS  0.4.13-dev-563-g0561610
Go to the documentation of this file.
1 // 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.
7 #ifndef _GETOPT_H
8 #define _GETOPT_H
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
14 #define BAD_OPTION '\0'
16 typedef 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. */
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'.
29  On entry to `getopt', zero means this is the first call; initialize.
31  When `getopt' returns EOF, this is the index of the first of the
32  non-option elements that the caller should itself scan.
34  Otherwise, `optind' communicates from one call to the next
35  how much of ARGV has been scanned so far. */
37  int optind;
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.
43  If this is zero, or a null string, it means resume the scan
44  by advancing to the next ARGV-element. */
48  /* Callers store zero here to inhibit the error message
49  for unrecognized options. */
51  int opterr;
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. */
57  int optopt;
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. */
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.
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.
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  */
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.
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.
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.
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. */
108 struct 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. */
113  int has_arg;
114  int *flag;
115  int val;
116 };
118 extern void getopt_init(optarg_ctx* o);
120 /* Names for the values of the `has_arg' field of `struct option'. */
122 #define no_argument 0
123 #define required_argument 1
124 #define optional_argument 2
126 extern int getopt (optarg_ctx* o, int argc, WCHAR *const *argv, const WCHAR *shortopts);
127 extern int getopt_long (optarg_ctx* o, int argc, WCHAR *const *argv, const WCHAR *shortopts,
128  const struct option *longopts, int *longind);
129 extern int getopt_long_only (optarg_ctx* o, int argc, WCHAR *const *argv,
130  const WCHAR *shortopts,
131  const struct option *longopts, int *longind);
133 /* Internal only. Users should not call this directly. */
134 extern 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);
139 #ifdef __cplusplus
140 }
141 #endif
143 #endif /* _GETOPT_H */
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
static int argc
Definition: ServiceArgs.c:12
int getopt_long_only(optarg_ctx *o, int argc, WCHAR *const *argv, const WCHAR *shortopts, const struct option *longopts, int *longind)
int getopt_long(optarg_ctx *o, int argc, WCHAR *const *argv, const WCHAR *shortopts, const struct option *longopts, int *longind)
Definition: getopt.cpp:472
int opterr
Definition: getopt.h:51
int val
Definition: getopt.h:115
struct _optarg_ctx * P_optarg_ctx
int * flag
Definition: getopt.h:114
#define argv
Definition: mplay32.c:18
int last_nonopt
Definition: getopt.h:64
WCHAR * name
Definition: getopt.h:110
Definition: getopt.h:108
int optind
Definition: getopt.h:37
__wchar_t WCHAR
Definition: xmlstorage.h:180
void getopt_init(optarg_ctx *o)
Definition: getopt.cpp:61
WCHAR * nextchar
Definition: getopt.h:46
struct _optarg_ctx optarg_ctx
int first_nonopt
Definition: getopt.h:63
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:55
WCHAR * optarg
Definition: getopt.h:23
int has_arg
Definition: getopt.h:113
int optopt
Definition: getopt.h:57