-
Notifications
You must be signed in to change notification settings - Fork 638
Expand file tree
/
Copy pathCommandLine.cc
More file actions
169 lines (147 loc) · 4.34 KB
/
CommandLine.cc
File metadata and controls
169 lines (147 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (C) 1996-2026 The Squid Software Foundation and contributors
*
* Squid software is distributed under GPLv2+ license and includes
* contributions from numerous individuals and organizations.
* Please see the COPYING and CONTRIBUTORS files for details.
*/
#include "squid.h"
#include "CommandLine.h"
#include "sbuf/SBuf.h"
static void
ResetGetopt(const bool allowStderrWarnings)
{
opterr = allowStderrWarnings;
// Resetting optind to zero instead of conventional '1' has an
// advantage, since it also resets getopt(3) global state.
// getopt(3) always skips argv[0], even if optind is zero
optind = 0;
}
CommandLine::CommandLine(int argC, char *argV[], const char *shortRules, const RawLongOption *longRules):
argv_(),
shortOptions_(shortRules ? xstrdup(shortRules) : ""),
longOptions_()
{
assert(argC > 0); // C++ main() requirement that makes our arg0() safe
assert(shortRules);
/* copy argV items */
argv_.reserve(argC+1);
for (int i = 0; i < argC; ++i)
argv_.push_back(xstrdup(argV[i]));
argv_.push_back(nullptr); // POSIX argv "must be terminated by a null pointer"
/* copy grammar rules for the long options */
if (longRules) {
for (auto longOption = longRules; longOption->name; ++longOption)
longOptions_.emplace_back(*longOption);
longOptions_.emplace_back();
}
}
CommandLine::CommandLine(const CommandLine &them):
CommandLine(them.argc(), them.argv(), them.shortOptions_, them.longOptions())
{
}
CommandLine &
CommandLine::operator =(const CommandLine &them)
{
// cannot just swap(*this, them): std::swap(T,T) may call this assignment op
CommandLine tmp(them);
std::swap(argv_, tmp.argv_);
std::swap(shortOptions_, tmp.shortOptions_);
std::swap(longOptions_, tmp.longOptions_);
return *this;
}
CommandLine::~CommandLine()
{
for (auto arg: argv_)
xfree(arg);
xfree(shortOptions_);
}
bool
CommandLine::hasOption(const int optIdToFind, const char **optValue) const
{
ResetGetopt(false); // avoid duped warnings; forEachOption() will complain
int optId = 0;
while (nextOption(optId)) {
if (optId == optIdToFind) {
if (optValue) {
// do not need to copy the optarg string because it is a pointer into the original
// argv array (https://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html)
*optValue = optarg;
}
return true;
}
}
return false;
}
void
CommandLine::forEachOption(Visitor visitor) const
{
ResetGetopt(true);
int optId = 0;
while (nextOption(optId))
visitor(optId, optarg);
}
/// extracts the next option (if any)
/// \returns whether the option was extracted
/// throws on unknown option or missing required argument
bool
CommandLine::nextOption(int &optId) const
{
optId = getopt_long(argc(), argv(), shortOptions_, longOptions(), nullptr);
if ((optId == ':' && shortOptions_[0] == ':') || optId == '?') {
assert(optind > 0 && static_cast<unsigned int>(optind) < argv_.size());
SBuf errMsg;
errMsg.Printf("'%s': %s", argv_[optind - 1], optId == '?' ?
"unrecognized option or missing required argument" : "missing required argument");
throw TexcHere(errMsg);
}
return optId != -1;
}
void
CommandLine::resetArg0(const char *programName)
{
assert(programName);
xfree(argv_[0]);
argv_[0] = xstrdup(programName);
}
void
CommandLine::pushFrontOption(const char *name, const char *value)
{
assert(name);
argv_.insert(argv_.begin() + 1, xstrdup(name));
if (value)
argv_.insert(argv_.begin() + 2, xstrdup(value));
}
LongOption::LongOption() :
option({nullptr, 0, nullptr, 0})
{
}
LongOption::LongOption(const RawLongOption &opt) :
option({nullptr, 0, nullptr, 0})
{
copy(opt);
}
LongOption::LongOption(const LongOption &opt):
LongOption(static_cast<const RawLongOption &>(opt))
{
}
LongOption::~LongOption()
{
xfree(name);
}
LongOption &
LongOption::operator =(const LongOption &opt)
{
if (this != &opt)
copy(static_cast<const RawLongOption &>(opt));
return *this;
}
void
LongOption::copy(const RawLongOption &opt)
{
xfree(name);
name = opt.name ? xstrdup(opt.name) : nullptr;
has_arg = opt.has_arg;
flag = opt.flag;
val = opt.val;
}