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