-
Notifications
You must be signed in to change notification settings - Fork 0
/
tol.cpp
212 lines (177 loc) · 4.13 KB
/
tol.cpp
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* This file is part of Tcl One-Liner (tol).
* See tol.h for the COPYRIGHT notice.
*/
#include <algorithm>
#include <cstring>
#include <iostream>
#include <ostream>
#include "tol.h"
Tol::Tol(int& argc, char** argv)
: m_interp{ nullptr }
, m_flags{ 0 }
{
Tcl_FindExecutable(argv[0]);
CreateInterp();
std::copy(argv + 1, argv + argc, std::back_inserter(m_args));
}
Tol::~Tol()
{
DeleteInterp();
}
void
Tol::CreateInterp()
{
m_interp = Tcl_CreateInterp();
Tcl_Init(m_interp);
}
void
Tol::DeleteInterp()
{
if (m_interp != nullptr)
{
Tcl_DeleteInterp(m_interp);
m_interp = nullptr;
}
}
void
Tol::ResetState()
{
ResetFlags();
DeleteInterp();
CreateInterp();
}
int
Tol::Evaluate(const std::string& command)
{
Tcl_ResetResult(m_interp);
int res{ Tcl_Eval(m_interp, command.c_str()) };
std::string out{ Tcl_GetStringFromObj(Tcl_GetObjResult(m_interp), NULL) };
if (res != TCL_OK)
{
if (!IsFlagged(Flag::IgnoreErrors))
{
std::cerr << Tcl_GetVar(m_interp, "::errorInfo", 0) << std::endl;
return res;
}
res = TCL_OK;
}
/*
* Avoid printing blank lines by skipping over empty results
*/
if (IsFlagged(Flag::PrintResult) && !out.empty())
{
std::cout << out << std::endl;
}
return res;
}
int
Tol::Run()
{
if (m_args.size() == 0)
{
return 0;
}
std::string cmd;
/*
* Unfortunately I can't iterate over arguments with a more
* convenient range-for because I have to manually increment
* the iterator to skip over options.
*/
for (auto i = m_args.cbegin(); i != m_args.cend(); ++i)
{
const auto& s = *i;
if (s.empty())
{
continue;
}
if (s.length() > 1 && s[0] == '-')
{
bool handled = true;
/*
* Handle options
*/
switch (s[1])
{
case 'a':
SetFlag(Flag::AccumulateCommands);
break;
case 'c':
SetFlag(Flag::Continue);
break;
case 'e':
PrintExamples();
break;
case 'i':
SetFlag(Flag::IgnoreErrors);
break;
case 'p':
SetFlag(Flag::PrintResult);
break;
case 'r':
ResetState();
break;
case 's':
if (i + 2 >= m_args.end())
{
std::cerr << "Not enough arguments given." << std::endl;
return 1;
}
Tcl_SetVar(m_interp, (i + 1)->c_str(), (i + 2)->c_str(), 0);
i += 2;
break;
case 'h':
case 'v':
PrintUsage();
break;
default:
handled = false;
break;
}
if (handled)
continue;
}
/*
* Options are done. Assume the current argument is part of a
* command to be evaluated.
*/
cmd.append(s);
/*
* Skip evaluation of the command if Continue flag was used and
* the command is not yet complete.
*/
if (IsFlagged(Flag::Continue) && !Tcl_CommandComplete(cmd.c_str()))
{
cmd.append(" ");
ResetFlags();
continue;
}
/*
* Skip evaluation of the command if AccumulateCommands flag was used.
*/
if (IsFlagged(Flag::AccumulateCommands))
{
cmd.append(" ");
continue;
}
/*
* Finally evaluate the command. Only report errors if the
* IgnoreErrors flag wasn't used.
*/
auto res = Evaluate(cmd);
if (res != TCL_OK)
{
return 1;
}
ResetFlags();
cmd.clear();
}
/*
* Evaluate any remaining commands.
*/
if (!cmd.empty())
{
return Evaluate(cmd);
}
return 0;
}