Rev 1987 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1608 | runge | 1 | /* |
| 2 | * |
||
| 3 | * Copyright (C) 2010 Mattias Runge |
||
| 4 | * |
||
| 5 | * This program is free software; you can redistribute it and/or modify |
||
| 6 | * it under the terms of the GNU General Public License as published by |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 8 | * (at your option) any later version. |
||
| 9 | * |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * |
||
| 15 | * You should have received a copy of the GNU General Public License along |
||
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 18 | * |
||
| 19 | */ |
||
| 20 | |||
| 21 | #include <string> |
||
| 22 | #include <vector> |
||
| 1647 | runge | 23 | #include <iostream> |
| 1608 | runge | 24 | |
| 25 | #include <signal.h> |
||
| 26 | #include <stdio.h> |
||
| 27 | #include <stdlib.h> |
||
| 1609 | runge | 28 | #include <pwd.h> |
| 1608 | runge | 29 | |
| 30 | #include <boost/lexical_cast.hpp> |
||
| 31 | #include <boost/thread/mutex.hpp> |
||
| 32 | #include <boost/thread/condition.hpp> |
||
| 33 | #include <boost/thread/locks.hpp> |
||
| 34 | #include <boost/algorithm/string.hpp> |
||
| 1647 | runge | 35 | #include <boost/program_options.hpp> |
| 1608 | runge | 36 | |
| 1652 | runge | 37 | #include "config.h" |
| 38 | |||
| 1608 | runge | 39 | #include "net/Manager.h" |
| 40 | #include "net/Subscriber.h" |
||
| 41 | #include "net/types.h" |
||
| 42 | |||
| 1642 | runge | 43 | #include "common/common.h" |
| 1987 | runge | 44 | #include "common/log.h" |
| 1608 | runge | 45 | |
| 46 | #include <readline/readline.h> |
||
| 47 | #include <readline/history.h> |
||
| 48 | |||
| 49 | using namespace atom; |
||
| 50 | |||
| 1647 | runge | 51 | bool finish = false; |
| 52 | common::StringList autocomplete_list; |
||
| 53 | char* buffer = NULL; |
||
| 54 | boost::condition on_message_condition; |
||
| 55 | struct termios original_flags; |
||
| 56 | std::string history_filename; |
||
| 1608 | runge | 57 | |
| 58 | void Handler(int status); |
||
| 59 | void CleanUp(); |
||
| 60 | |||
| 61 | char* AutoCompleteGet(const char* text, int state); |
||
| 62 | static char** AutoComplete(const char* text, int start, int end); |
||
| 63 | |||
| 1649 | runge | 64 | std::string GetUserHomeDirectory() |
| 1647 | runge | 65 | { |
| 66 | return std::string(getpwuid(getuid())->pw_dir); |
||
| 67 | } |
||
| 1608 | runge | 68 | |
| 69 | class ConsoleClient : public net::Subscriber |
||
| 70 | { |
||
| 71 | public: |
||
| 72 | typedef boost::shared_ptr<ConsoleClient> Pointer; |
||
| 73 | |||
| 74 | ConsoleClient(std::string address, unsigned int port) |
||
| 75 | { |
||
| 1989 | runge | 76 | this->client_id_ = net::Manager::Instance()->Connect(net::TRANSPORT_PROTOCOL_TCP, address, port); |
| 1608 | runge | 77 | } |
| 78 | |||
| 79 | virtual ~ConsoleClient() |
||
| 80 | { |
||
| 81 | net::Manager::Instance()->Disconnect(this->client_id_); |
||
| 82 | |||
| 83 | this->io_service_.stop(); |
||
| 84 | } |
||
| 85 | |||
| 1647 | runge | 86 | std::string GetPrompt() |
| 1608 | runge | 87 | { |
| 1647 | runge | 88 | return this->prompt_; |
| 1608 | runge | 89 | } |
| 90 | |||
| 1647 | runge | 91 | void SendResponse(std::string payload) |
| 1608 | runge | 92 | { |
| 1647 | runge | 93 | std::string packet = "RESP"; |
| 94 | packet += common::PadNumber(payload.length() + 1, 4); |
||
| 95 | packet += payload; |
||
| 96 | |||
| 1987 | runge | 97 | net::Manager::Instance()->SendTo(this->client_id_, common::Byteset(packet.begin(), packet.end())); |
| 1608 | runge | 98 | } |
| 99 | |||
| 1647 | runge | 100 | void AutoCompleteRequest(unsigned int arg_index, std::string commandline) |
| 1620 | runge | 101 | { |
| 1647 | runge | 102 | std::string payload = common::PadNumber(arg_index, 4); |
| 103 | payload += commandline; |
||
| 104 | |||
| 105 | std::string packet = "COMP"; |
||
| 106 | packet += common::PadNumber(payload.length() + 1, 4); |
||
| 107 | packet += payload; |
||
| 108 | |||
| 1987 | runge | 109 | net::Manager::Instance()->SendTo(this->client_id_, common::Byteset(packet.begin(), packet.end())); |
| 1620 | runge | 110 | } |
| 111 | |||
| 1608 | runge | 112 | private: |
| 1959 | runge | 113 | net::SocketId client_id_; |
| 1647 | runge | 114 | std::string prompt_; |
| 1674 | runge | 115 | std::vector<unsigned char> buffer_; |
| 1608 | runge | 116 | |
| 1986 | runge | 117 | void SlotOnNewStateHandler(net::SocketId client_id, net::ClientState client_state) |
| 1608 | runge | 118 | { |
| 1647 | runge | 119 | if (client_state != net::CLIENT_STATE_CONNECTED) |
| 1608 | runge | 120 | { |
| 1664 | runge | 121 | std::cout << "Disconnected from server." << std::endl; |
| 1647 | runge | 122 | |
| 1608 | runge | 123 | this->client_id_ = 0; |
| 1647 | runge | 124 | finish = true; |
| 1664 | runge | 125 | on_message_condition.notify_all(); |
| 1608 | runge | 126 | } |
| 127 | } |
||
| 128 | |||
| 1986 | runge | 129 | void SlotOnNewClientHandler(net::SocketId id, net::SocketId server_id) |
| 1608 | runge | 130 | { |
| 1986 | runge | 131 | } |
| 132 | |||
| 133 | void SlotOnNewDataHandler(net::SocketId client_id, common::Byteset data) |
||
| 134 | { |
||
| 1987 | runge | 135 | for (unsigned int n = 0; n < data.size(); n++) |
| 1608 | runge | 136 | { |
| 1674 | runge | 137 | this->buffer_.push_back(data[n]); |
| 138 | } |
||
| 139 | |||
| 140 | while (this->buffer_.size() >= 8) |
||
| 141 | { |
||
| 142 | std::string command = ""; |
||
| 143 | command += (char)this->buffer_[0]; |
||
| 144 | command += (char)this->buffer_[1]; |
||
| 145 | command += (char)this->buffer_[2]; |
||
| 146 | command += (char)this->buffer_[3]; |
||
| 147 | |||
| 148 | std::string payload_length_str = ""; |
||
| 149 | payload_length_str += (char)this->buffer_[4]; |
||
| 150 | payload_length_str += (char)this->buffer_[5]; |
||
| 151 | payload_length_str += (char)this->buffer_[6]; |
||
| 152 | payload_length_str += (char)this->buffer_[7]; |
||
| 1609 | runge | 153 | |
| 1987 | runge | 154 | unsigned int payload_length = boost::lexical_cast<unsigned int>(payload_length_str) - 1; |
| 1674 | runge | 155 | |
| 156 | if (this->buffer_.size() - 8 < payload_length) |
||
| 157 | { |
||
| 158 | return; |
||
| 159 | } |
||
| 160 | |||
| 161 | std::string payload = ""; |
||
| 162 | for (unsigned int n = 8; n < payload_length + 8; n++) |
||
| 163 | { |
||
| 164 | payload += (char)this->buffer_[n]; |
||
| 165 | } |
||
| 166 | |||
| 167 | this->buffer_.erase(this->buffer_.begin(), this->buffer_.begin() + payload_length + 8); |
||
| 168 | |||
| 1647 | runge | 169 | if (command == "TEXT") |
| 1609 | runge | 170 | { |
| 1674 | runge | 171 | std::cout << payload << std::flush; |
| 1609 | runge | 172 | } |
| 1647 | runge | 173 | else if (command == "PROM") |
| 1608 | runge | 174 | { |
| 1674 | runge | 175 | this->prompt_ = payload; |
| 1647 | runge | 176 | on_message_condition.notify_all(); |
| 177 | } |
||
| 178 | else if (command == "COMP") |
||
| 179 | { |
||
| 180 | autocomplete_list.clear(); |
||
| 1620 | runge | 181 | |
| 1674 | runge | 182 | if (payload.length() > 0) |
| 1620 | runge | 183 | { |
| 1674 | runge | 184 | boost::algorithm::split(autocomplete_list, payload, boost::is_any_of("\n"), boost::algorithm::token_compress_on); |
| 1620 | runge | 185 | } |
| 1647 | runge | 186 | |
| 187 | on_message_condition.notify_all(); |
||
| 1608 | runge | 188 | } |
| 1647 | runge | 189 | else |
| 190 | { |
||
| 1674 | runge | 191 | std::cerr << "Could not parse package." << std::endl; |
| 1647 | runge | 192 | finish = true; |
| 193 | on_message_condition.notify_all(); |
||
| 194 | break; |
||
| 195 | } |
||
| 1608 | runge | 196 | } |
| 197 | } |
||
| 198 | }; |
||
| 199 | |||
| 200 | ConsoleClient::Pointer cc; |
||
| 201 | |||
| 1647 | runge | 202 | |
| 1608 | runge | 203 | int main(int argc, char **argv) |
| 204 | { |
||
| 1647 | runge | 205 | // Signal handlers |
| 1608 | runge | 206 | signal(SIGTERM, Handler); |
| 207 | signal(SIGINT, Handler); |
||
| 208 | signal(SIGQUIT, Handler); |
||
| 209 | signal(SIGABRT, Handler); |
||
| 210 | signal(SIGPIPE, Handler); |
||
| 211 | |||
| 1647 | runge | 212 | boost::mutex guard_mutex; |
| 213 | |||
| 214 | // Setup readline |
||
| 1649 | runge | 215 | history_filename = GetUserHomeDirectory() + "/.atomic_history"; |
| 1647 | runge | 216 | read_history(history_filename.data()); |
| 217 | |||
| 218 | rl_attempted_completion_function = AutoComplete; |
||
| 219 | |||
| 220 | // Save command line state |
||
| 1608 | runge | 221 | tcgetattr(fileno(stdin), &original_flags); |
| 222 | |||
| 1647 | runge | 223 | // Parse commandline |
| 224 | boost::program_options::options_description command_line; |
||
| 225 | boost::program_options::variables_map variable_map; |
||
| 1608 | runge | 226 | |
| 1987 | runge | 227 | //log::SetLevel(log::LOG_LEVEL_ALL); |
| 228 | |||
| 1647 | runge | 229 | command_line.add_options() |
| 230 | ("help,h", "produce help message") |
||
| 1664 | runge | 231 | ("command,c", boost::program_options::value<std::string>()->default_value(""), "command") |
| 1647 | runge | 232 | ("server,s", boost::program_options::value<std::string>()->default_value("localhost"), "server address") |
| 233 | ("port,p", boost::program_options::value<unsigned int>()->default_value(1202), "server port"); |
||
| 1608 | runge | 234 | |
| 235 | try |
||
| 236 | { |
||
| 1647 | runge | 237 | boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(command_line).run(), variable_map); |
| 1608 | runge | 238 | } |
| 1647 | runge | 239 | catch (boost::program_options::unknown_option e) |
| 1608 | runge | 240 | { |
| 1647 | runge | 241 | std::cerr << e.what() << std::endl; |
| 242 | std::cout << command_line << std::endl; |
||
| 1608 | runge | 243 | CleanUp(); |
| 244 | return EXIT_FAILURE; |
||
| 245 | } |
||
| 1647 | runge | 246 | catch (boost::program_options::invalid_syntax e) |
| 247 | { |
||
| 248 | std::cerr << e.what() << std::endl; |
||
| 249 | std::cout << command_line << std::endl; |
||
| 250 | CleanUp(); |
||
| 251 | return EXIT_FAILURE; |
||
| 252 | } |
||
| 1608 | runge | 253 | |
| 1647 | runge | 254 | if (variable_map.count("help") != 0) |
| 255 | { |
||
| 256 | std::cout << command_line << std::endl; |
||
| 257 | CleanUp(); |
||
| 258 | return EXIT_SUCCESS; |
||
| 259 | } |
||
| 1608 | runge | 260 | |
| 1647 | runge | 261 | net::Manager::Create(); |
| 1664 | runge | 262 | |
| 263 | if (variable_map["command"].as<std::string>() == "") |
||
| 264 | { |
||
| 265 | std::cout << "\033[29;1mAtom Interactive Console, version " + std::string(VERSION) + " starting...\033[0m" << std::endl; |
||
| 1679 | runge | 266 | std::cout << "\033[29;1mReleased under " + std::string(LICENSE) + ".\033[0m" << std::endl; |
| 1664 | runge | 267 | std::cout << "Written by Mattias Runge 2010." << std::endl; |
| 268 | |||
| 269 | std::cout << "Connecting to " << variable_map["server"].as<std::string>().data() << ":" << variable_map["port"].as<unsigned int>() << "..." << std::endl; |
||
| 270 | } |
||
| 1609 | runge | 271 | |
| 1647 | runge | 272 | try |
| 273 | { |
||
| 274 | cc = ConsoleClient::Pointer(new ConsoleClient(variable_map["server"].as<std::string>(), variable_map["port"].as<unsigned int>())); |
||
| 275 | } |
||
| 276 | catch (std::runtime_error& e) |
||
| 277 | { |
||
| 1664 | runge | 278 | std::cerr << "Connection error: " << e.what() << std::endl; |
| 1647 | runge | 279 | CleanUp(); |
| 280 | return EXIT_FAILURE; |
||
| 281 | } |
||
| 1609 | runge | 282 | |
| 1664 | runge | 283 | if (variable_map["command"].as<std::string>() != "") |
| 1608 | runge | 284 | { |
| 1647 | runge | 285 | boost::mutex::scoped_lock guard(guard_mutex); |
| 286 | on_message_condition.wait(guard); |
||
| 1608 | runge | 287 | |
| 1664 | runge | 288 | if (!finish) |
| 1608 | runge | 289 | { |
| 1664 | runge | 290 | cc->SendResponse(variable_map["command"].as<std::string>()); |
| 291 | |||
| 292 | if (!finish) |
||
| 293 | { |
||
| 294 | on_message_condition.wait(guard); |
||
| 295 | } |
||
| 1608 | runge | 296 | } |
| 1664 | runge | 297 | } |
| 298 | else |
||
| 299 | { |
||
| 300 | while (true) |
||
| 1620 | runge | 301 | { |
| 1664 | runge | 302 | boost::mutex::scoped_lock guard(guard_mutex); |
| 303 | on_message_condition.wait(guard); |
||
| 304 | |||
| 305 | if (finish) |
||
| 1647 | runge | 306 | { |
| 1664 | runge | 307 | break; |
| 1647 | runge | 308 | } |
| 309 | |||
| 1664 | runge | 310 | while ((buffer = readline(cc->GetPrompt().data())) != NULL) |
| 311 | { |
||
| 1665 | runge | 312 | if (finish) |
| 313 | { |
||
| 314 | break; |
||
| 315 | } |
||
| 316 | |||
| 1664 | runge | 317 | if (strlen(buffer) == 0) |
| 318 | { |
||
| 319 | continue; |
||
| 320 | } |
||
| 321 | |||
| 322 | break; |
||
| 323 | } |
||
| 324 | |||
| 1665 | runge | 325 | if (finish) |
| 326 | { |
||
| 327 | break; |
||
| 328 | } |
||
| 329 | |||
| 1664 | runge | 330 | cc->SendResponse(buffer); |
| 331 | add_history(buffer); |
||
| 1620 | runge | 332 | } |
| 1608 | runge | 333 | } |
| 334 | |||
| 335 | CleanUp(); |
||
| 336 | |||
| 337 | return EXIT_SUCCESS; |
||
| 338 | } |
||
| 339 | |||
| 340 | static char** AutoComplete(const char* text, int start, int end) |
||
| 341 | { |
||
| 342 | unsigned int count = 0; |
||
| 1647 | runge | 343 | boost::mutex guard_mutex; |
| 344 | |||
| 1914 | linlun | 345 | for (unsigned int n = 0; n < (unsigned int)start; n++) |
| 1608 | runge | 346 | { |
| 1609 | runge | 347 | if (rl_line_buffer[n] == ' ') |
| 1608 | runge | 348 | { |
| 349 | count++; |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 1647 | runge | 353 | cc->AutoCompleteRequest(count, rl_line_buffer); |
| 1608 | runge | 354 | |
| 355 | boost::mutex::scoped_lock guard(guard_mutex); |
||
| 1647 | runge | 356 | on_message_condition.wait(guard); |
| 1608 | runge | 357 | |
| 1647 | runge | 358 | /*if (autocomplete_list.size() == 0) |
| 1609 | runge | 359 | { |
| 360 | return NULL; |
||
| 1647 | runge | 361 | }*/ |
| 1609 | runge | 362 | |
| 1608 | runge | 363 | return rl_completion_matches(text, &AutoCompleteGet); |
| 364 | } |
||
| 365 | |||
| 366 | char* AutoCompleteGet(const char* text, int state) |
||
| 367 | { |
||
| 368 | static int index = 0; |
||
| 369 | int length = strlen(text); |
||
| 370 | std::string name; |
||
| 371 | |||
| 372 | if (!state) // First run |
||
| 373 | { |
||
| 374 | index = 0; |
||
| 375 | } |
||
| 376 | |||
| 1914 | linlun | 377 | while (autocomplete_list.size() > (unsigned int) index) |
| 1608 | runge | 378 | { |
| 379 | name = autocomplete_list[index]; |
||
| 380 | |||
| 381 | index++; |
||
| 382 | |||
| 383 | if (strncmp(name.data(), text, length) == 0) |
||
| 384 | { |
||
| 385 | char *result = (char*)malloc(name.size() + 1); |
||
| 386 | strcpy(result, name.data()); |
||
| 387 | |||
| 388 | return result; |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | return NULL; |
||
| 393 | } |
||
| 394 | |||
| 395 | void CleanUp() |
||
| 396 | { |
||
| 1664 | runge | 397 | std::cout << std::endl; |
| 398 | //std::cout << "Cleaning up..." << std::endl; |
||
| 1609 | runge | 399 | |
| 400 | write_history(history_filename.data()); |
||
| 1608 | runge | 401 | |
| 402 | cc.reset(); |
||
| 403 | |||
| 404 | net::Manager::Delete(); |
||
| 405 | |||
| 406 | if (buffer != NULL) |
||
| 407 | { |
||
| 408 | free(buffer); |
||
| 409 | } |
||
| 410 | |||
| 1664 | runge | 411 | //std::cout << "Thank you for using Atom. Goodbye!" << std::endl; |
| 1608 | runge | 412 | |
| 413 | tcsetattr(fileno(stdin), TCSANOW, &original_flags); // Restore |
||
| 414 | } |
||
| 415 | |||
| 416 | void Handler(int status) |
||
| 417 | { |
||
| 418 | std::string signal_name = "Unknown"; |
||
| 419 | |||
| 420 | switch (status) |
||
| 421 | { |
||
| 422 | case SIGTERM: |
||
| 423 | { |
||
| 424 | signal_name = "Terminate"; |
||
| 425 | break; |
||
| 426 | } |
||
| 427 | case SIGINT: |
||
| 428 | { |
||
| 1940 | runge | 429 | signal_name = "Interrupt"; |
| 1608 | runge | 430 | break; |
| 431 | } |
||
| 432 | case SIGQUIT: |
||
| 433 | { |
||
| 434 | signal_name = "Quit"; |
||
| 435 | break; |
||
| 436 | } |
||
| 437 | case SIGABRT: |
||
| 438 | { |
||
| 439 | signal_name = "Abort"; |
||
| 440 | break; |
||
| 441 | } |
||
| 442 | case SIGIO: |
||
| 443 | { |
||
| 444 | signal_name = "I/O"; |
||
| 445 | break; |
||
| 446 | } |
||
| 447 | case SIGPIPE: |
||
| 448 | { |
||
| 449 | signal_name = "Pipe"; |
||
| 450 | break; |
||
| 451 | } |
||
| 452 | } |
||
| 453 | |||
| 454 | |||
| 455 | if (status != SIGPIPE) |
||
| 456 | { |
||
| 457 | CleanUp(); |
||
| 458 | exit(0); |
||
| 459 | } |
||
| 1675 | arune | 460 | } |
| 461 |