-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand.cpp
198 lines (173 loc) · 4.27 KB
/
command.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
//@ {"targets":[{"name":"command.o","type":"object"}]}
#include "command.hpp"
#include "variant.hpp"
#include "stringkey.hpp"
#include "exceptionhandler.hpp"
#include "errormessage.hpp"
#include "resourceobject.hpp"
#include "pipe.hpp"
#include "parameterset.hpp"
using namespace Maike;
Command::Command(){}
Command::Command(const ResourceObject& cmd)
{
m_name=std::string( static_cast<const char*>( cmd.objectGet("name") ) );
auto args=cmd.objectGet("args");
auto N_args=args.objectCountGet();
for(decltype(N_args) k=0;k<N_args;++k)
{
m_args.push_back(std::string(static_cast<const char*>(args.objectGet(k))));
}
}
static std::vector<const char*> cstr(Twins<const std::string*> strings)
{
std::vector<const char*> ret;
while(strings.first!=strings.second)
{
ret.push_back(strings.first->c_str());
++strings.first;
}
return std::move(ret);
}
Pipe Command::execute(unsigned int redirection) const
{
auto args=cstr({m_args.data(),m_args.data() + m_args.size()});
return Pipe(m_name.c_str(),{args.data(),args.data() + args.size()},redirection);
}
namespace
{
class ParamExtractor:public ParameterSet::ParameterProcessor
{
public:
ParamExtractor(const std::string& prefix
,std::vector<std::string>& result):r_prefix(prefix),r_result(result)
{}
void operator()(const char* value)
{r_result.push_back(r_prefix + value);}
private:
const std::string& r_prefix;
std::vector<std::string>& r_result;
};
}
static void substitutesAppend(const std::string& prefix,const Stringkey& key
,Twins<const ParameterSet* const*> substitutes
,std::vector<std::string>& result)
{
auto n_0=result.size();
while(substitutes.first!=substitutes.second)
{
--substitutes.second;
(*substitutes.second)->parameterGet(key,ParamExtractor(prefix,result));
auto n=result.size();
if(n_0!=n) //Parameter found
{return;}
n_0=n;
}
}
static void varsSubstitute(const char* str
,Twins<const ParameterSet* const*> substitutes
,std::vector<std::string>& result)
{
std::string temp;
std::string name_old;
enum class State:unsigned int{NORMAL,VARIABLE,ESCAPE};
auto state=State::NORMAL;
while(true)
{
auto ch_in=*str;
switch(state)
{
case State::NORMAL:
switch(ch_in)
{
case '{':
name_old=temp;
temp.clear();
state=State::VARIABLE;
break;
case '\\':
state=State::ESCAPE;
break;
case '\0':
if(temp.size()!=0)
{result.push_back(temp);}
return;
default:
temp+=ch_in;
}
break;
case State::VARIABLE:
switch(ch_in)
{
case '}':
//FIXME: How to treat fixed content together with array argument
state=State::NORMAL;
substitutesAppend(name_old,Stringkey(temp.c_str()),substitutes,result);
name_old.clear();
temp.clear();
break;
case '\0':
exceptionRaise(ErrorMessage("Expansion error: Unterminated variable.",{}));
break;
default:
temp+=ch_in;
}
break;
case State::ESCAPE:
switch(ch_in)
{
case '\0':
exceptionRaise(ErrorMessage("Expansion error: Lonely escape character.",{}));
break;
default:
temp+=ch_in;
state=State::NORMAL;
}
break;
}
++str;
}
}
Pipe Command::execute(unsigned int redirection
,Twins<const ParameterSet* const*> substitutes) const
{
std::vector<std::string> args_temp;
auto ptr_args=m_args.data();
auto ptr_end=ptr_args + m_args.size();
while(ptr_args!=ptr_end)
{
varsSubstitute(ptr_args->c_str(),substitutes,args_temp);
++ptr_args;
}
auto args=cstr({args_temp.data(),args_temp.data() + args_temp.size()});
return Pipe(m_name.c_str(),{args.data(),args.data() + args.size()},redirection);
}
Command& Command::nameSet(const char* name)
{
m_name=std::string(name);
return *this;
}
Command& Command::argumentAppend(const char* arg)
{
m_args.push_back(arg);
return *this;
}
void Command::argumentsClear() noexcept
{
m_args.clear();
}
void Command::configDump(ResourceObject& cmd) const
{
cmd.objectSet("name",cmd.create(m_name.c_str()));
{
auto args=cmd.createArray();
auto ptr_args=m_args.data();
auto ptr_end=ptr_args + m_args.size();
while(ptr_args!=ptr_end)
{
args.objectAppend(args.create(ptr_args->c_str()));
++ptr_args;
}
cmd.objectSet("args",std::move(args));
}
}