9 std::map
<std::string
, command
*>* commands
;
10 std::set
<std::string
> command_stack
;
11 std::map
<std::string
, std::list
<std::string
>> aliases
;
13 function_ptr_command
run_script("run-script", "run file as a script",
14 "Syntax: run-script <file>\nRuns file <file> just as it would have been entered in the command line\n",
15 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
17 throw std::runtime_error("Filename needed");
18 std::istream
* o
= NULL
;
20 o
= &open_file_relative(args
, "");
21 messages
<< "Running '" << args
<< "'" << std::endl
;
23 while(std::getline(*o
, line
))
24 command::invokeC(line
);
26 } catch(std::exception
& e
) {
33 function_ptr_command
show_aliases("show-aliases", "show aliases",
34 "Syntax: show-aliases\nShow expansions of all aliases\n",
35 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
37 throw std::runtime_error("This command does not take parameters");
38 for(auto i
= aliases
.begin(); i
!= aliases
.end(); i
++)
39 for(auto j
= i
->second
.begin(); j
!= i
->second
.end(); j
++)
40 messages
<< "alias " << i
->first
<< " " << *j
<< std::endl
;
43 function_ptr_command
unalias_command("unalias-command", "unalias a command",
44 "Syntax: unalias-command <aliasname>\nClear expansion of alias <aliasname>\n",
45 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
46 tokensplitter
t(args
);
47 std::string aliasname
= t
;
49 throw std::runtime_error("This command only takes one argument");
50 if(aliasname
.length() == 0 || aliasname
[0] == '?' || aliasname
[0] == '*')
51 throw std::runtime_error("Illegal alias name");
52 aliases
[aliasname
].clear();
53 messages
<< "Command '" << aliasname
<< "' unaliased" << std::endl
;
56 function_ptr_command
alias_command("alias-command", "alias a command",
57 "Syntax: alias-command <aliasname> <command>\nAppend <command> to expansion of alias <aliasname>\n"
58 "Valid alias names can't be empty nor start with '*' or '?'\n",
59 [](const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
) {
60 tokensplitter
t(args
);
61 std::string aliasname
= t
;
62 std::string command
= t
.tail();
64 throw std::runtime_error("Alias name and command needed");
65 if(aliasname
.length() == 0 || aliasname
[0] == '?' || aliasname
[0] == '*')
66 throw std::runtime_error("Illegal alias name");
67 aliases
[aliasname
].push_back(command
);
68 messages
<< "Command '" << aliasname
<< "' aliased to '" << command
<< "'" << std::endl
;
72 command::command(const std::string
& cmd
) throw(std::bad_alloc
)
75 commands
= new std::map
<std::string
, command
*>();
76 if(commands
->count(cmd
))
77 std::cerr
<< "WARNING: Command collision for " << cmd
<< "!" << std::endl
;
78 (*commands
)[commandname
= cmd
] = this;
81 command::~command() throw()
85 commands
->erase(commandname
);
88 void command::invokeC(const std::string
& cmd
) throw()
91 if(command_stack
.count(cmd
)) {
92 messages
<< "Can not invoke recursively: " << cmd
<< std::endl
;
95 command_stack
.insert(cmd
);
96 std::string cmd2
= cmd
;
98 //The special ? command.
100 for(auto i
= commands
->begin(); i
!= commands
->end(); ++i
)
101 messages
<< i
->first
<< ": " << i
->second
->get_short_help() << std::endl
;
103 command_stack
.erase(cmd
);
106 if(cmd2
.length() > 1 && cmd2
[0] == '?') {
108 size_t split
= cmd2
.find_first_of(" \t");
110 if(split
>= cmd2
.length())
111 rcmd
= cmd2
.substr(1);
113 rcmd
= cmd2
.substr(1, split
- 1);
114 if(rcmd
.length() > 0 && rcmd
[0] != '*') {
115 //This may be an alias.
116 std::string aname
= cmd2
.substr(1);
117 if(aliases
.count(aname
)) {
119 messages
<< aname
<< " is an alias for: " << std::endl
;
121 for(auto i
= aliases
[aname
].begin(); i
!= aliases
[aname
].end(); ++i
, ++j
)
122 messages
<< "#" + (j
+ 1) << ": " << *i
<< std::endl
;
123 command_stack
.erase(cmd
);
127 if(rcmd
.length() > 0 && rcmd
[0] == '*')
128 rcmd
= rcmd
.substr(1);
129 if(!commands
|| !commands
->count(rcmd
)) {
131 messages
<< "Unknown command '" << rcmd
<< "'" << std::endl
;
132 command_stack
.erase(cmd
);
135 messages
<< (*commands
)[rcmd
]->get_long_help() << std::endl
;
136 command_stack
.erase(cmd
);
139 bool may_be_alias_expanded
= true;
140 if(cmd2
.length() > 0 && cmd2
[0] == '*') {
141 may_be_alias_expanded
= false;
142 cmd2
= cmd2
.substr(1);
144 if(may_be_alias_expanded
&& aliases
.count(cmd2
)) {
145 for(auto i
= aliases
[cmd2
].begin(); i
!= aliases
[cmd2
].end(); ++i
)
147 command_stack
.erase(cmd
);
151 size_t split
= cmd2
.find_first_of(" \t");
153 if(split
>= cmd2
.length())
156 rcmd
= cmd2
.substr(0, split
);
157 split
= cmd2
.find_first_not_of(" \t", split
);
159 if(split
< cmd2
.length())
160 args
= cmd2
.substr(split
);
161 command
* cmdh
= NULL
;
162 if(commands
&& commands
->count(rcmd
))
163 cmdh
= (*commands
)[rcmd
];
165 messages
<< "Unknown command '" << rcmd
<< "'" << std::endl
;
166 command_stack
.erase(cmd
);
170 command_stack
.erase(cmd
);
172 } catch(std::bad_alloc
& e
) {
174 } catch(std::exception
& e
) {
175 messages
<< "Error: " << e
.what() << std::endl
;
176 command_stack
.erase(cmd
);
179 } catch(std::bad_alloc
& e
) {
184 std::string
command::get_short_help() throw(std::bad_alloc
)
186 return "No description available";
189 std::string
command::get_long_help() throw(std::bad_alloc
)
191 return "No help available on command " + commandname
;
194 tokensplitter::tokensplitter(const std::string
& _line
) throw(std::bad_alloc
)
200 tokensplitter::operator bool() throw()
202 return (position
< line
.length());
205 tokensplitter::operator std::string() throw(std::bad_alloc
)
207 size_t nextp
, oldp
= position
;
208 nextp
= line
.find_first_of(" \t", position
);
209 if(nextp
> line
.length()) {
210 position
= line
.length();
211 return line
.substr(oldp
);
214 while(position
< line
.length() && (line
[position
] == ' ' || line
[position
] == '\t'))
216 return line
.substr(oldp
, nextp
- oldp
);
220 std::string
tokensplitter::tail() throw(std::bad_alloc
)
222 return line
.substr(position
);
225 function_ptr_command::function_ptr_command(const std::string
& name
, const std::string
& _description
,
226 const std::string
& _help
, void (*_fn
)(const std::string
& arguments
)) throw(std::bad_alloc
)
229 description
= _description
;
234 void function_ptr_command::invoke(const std::string
& args
) throw(std::bad_alloc
, std::runtime_error
)
239 std::string
function_ptr_command::get_short_help() throw(std::bad_alloc
)
244 std::string
function_ptr_command::get_long_help() throw(std::bad_alloc
)
249 function_ptr_command::~function_ptr_command() throw()