Rev 1657 | Rev 1665 | 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_; |
| 1608 | runge | 114 | |
| 1620 | runge | 115 | |
| 1608 | runge | 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 | { |
| 130 | std::string s(data.ToCharString()); |
||
| 1647 | runge | 131 | |
| 132 | while (s.length() > 0) |
||
| 1608 | runge | 133 | { |
| 1647 | runge | 134 | std::string command = s.substr(0, 4); |
| 135 | unsigned int payload_length = boost::lexical_cast<unsigned int>(s.substr(4, 4)); |
||
| 1609 | runge | 136 | |
| 1647 | runge | 137 | if (command == "TEXT") |
| 1609 | runge | 138 | { |
| 1647 | runge | 139 | std::cout << s.substr(8, payload_length - 1) << std::flush; |
| 1609 | runge | 140 | } |
| 1647 | runge | 141 | else if (command == "PROM") |
| 1608 | runge | 142 | { |
| 1647 | runge | 143 | this->prompt_ = s.substr(8, payload_length - 1); |
| 144 | on_message_condition.notify_all(); |
||
| 145 | } |
||
| 146 | else if (command == "COMP") |
||
| 147 | { |
||
| 148 | autocomplete_list.clear(); |
||
| 1620 | runge | 149 | |
| 1647 | runge | 150 | if (payload_length > 0) |
| 1620 | runge | 151 | { |
| 1647 | runge | 152 | std::string payload = s.substr(8, payload_length - 1); |
| 1620 | runge | 153 | |
| 1647 | runge | 154 | if (payload.length() > 0) |
| 1620 | runge | 155 | { |
| 1647 | runge | 156 | boost::algorithm::split(autocomplete_list, payload, boost::is_any_of("\n"), boost::algorithm::token_compress_on); |
| 1620 | runge | 157 | } |
| 158 | } |
||
| 1647 | runge | 159 | |
| 160 | on_message_condition.notify_all(); |
||
| 1608 | runge | 161 | } |
| 1647 | runge | 162 | else |
| 163 | { |
||
| 164 | std::cerr << "Unknown data received: " << s << std::endl; |
||
| 165 | finish = true; |
||
| 166 | on_message_condition.notify_all(); |
||
| 167 | break; |
||
| 168 | } |
||
| 169 | |||
| 170 | if (8 + payload_length >= s.length()) |
||
| 171 | { |
||
| 172 | break; |
||
| 173 | } |
||
| 174 | |||
| 175 | s = s.substr(8 + payload_length); |
||
| 1608 | runge | 176 | } |
| 177 | } |
||
| 178 | }; |
||
| 179 | |||
| 180 | ConsoleClient::Pointer cc; |
||
| 181 | |||
| 1647 | runge | 182 | |
| 1608 | runge | 183 | int main(int argc, char **argv) |
| 184 | { |
||
| 1647 | runge | 185 | // Signal handlers |
| 1608 | runge | 186 | signal(SIGTERM, Handler); |
| 187 | signal(SIGINT, Handler); |
||
| 188 | signal(SIGQUIT, Handler); |
||
| 189 | signal(SIGABRT, Handler); |
||
| 190 | signal(SIGPIPE, Handler); |
||
| 191 | |||
| 1647 | runge | 192 | boost::mutex guard_mutex; |
| 193 | |||
| 194 | // Setup readline |
||
| 1649 | runge | 195 | history_filename = GetUserHomeDirectory() + "/.atomic_history"; |
| 1647 | runge | 196 | read_history(history_filename.data()); |
| 197 | |||
| 198 | rl_attempted_completion_function = AutoComplete; |
||
| 199 | |||
| 200 | // Save command line state |
||
| 1608 | runge | 201 | tcgetattr(fileno(stdin), &original_flags); |
| 202 | |||
| 1647 | runge | 203 | // Parse commandline |
| 204 | boost::program_options::options_description command_line; |
||
| 205 | boost::program_options::variables_map variable_map; |
||
| 1608 | runge | 206 | |
| 1647 | runge | 207 | command_line.add_options() |
| 208 | ("help,h", "produce help message") |
||
| 1664 | runge | 209 | ("command,c", boost::program_options::value<std::string>()->default_value(""), "command") |
| 1647 | runge | 210 | ("server,s", boost::program_options::value<std::string>()->default_value("localhost"), "server address") |
| 211 | ("port,p", boost::program_options::value<unsigned int>()->default_value(1202), "server port"); |
||
| 1608 | runge | 212 | |
| 213 | try |
||
| 214 | { |
||
| 1647 | runge | 215 | boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(command_line).run(), variable_map); |
| 1608 | runge | 216 | } |
| 1647 | runge | 217 | catch (boost::program_options::unknown_option e) |
| 1608 | runge | 218 | { |
| 1647 | runge | 219 | std::cerr << e.what() << std::endl; |
| 220 | std::cout << command_line << std::endl; |
||
| 1608 | runge | 221 | CleanUp(); |
| 222 | return EXIT_FAILURE; |
||
| 223 | } |
||
| 1647 | runge | 224 | catch (boost::program_options::invalid_syntax e) |
| 225 | { |
||
| 226 | std::cerr << e.what() << std::endl; |
||
| 227 | std::cout << command_line << std::endl; |
||
| 228 | CleanUp(); |
||
| 229 | return EXIT_FAILURE; |
||
| 230 | } |
||
| 1608 | runge | 231 | |
| 1647 | runge | 232 | if (variable_map.count("help") != 0) |
| 233 | { |
||
| 234 | std::cout << command_line << std::endl; |
||
| 235 | CleanUp(); |
||
| 236 | return EXIT_SUCCESS; |
||
| 237 | } |
||
| 1608 | runge | 238 | |
| 1647 | runge | 239 | net::Manager::Create(); |
| 1664 | runge | 240 | |
| 241 | if (variable_map["command"].as<std::string>() == "") |
||
| 242 | { |
||
| 243 | std::cout << "\033[29;1mAtom Interactive Console, version " + std::string(VERSION) + " starting...\033[0m" << std::endl; |
||
| 244 | std::cout << "\033[29;1mReleased under GPL version 2.\033[0m" << std::endl; |
||
| 245 | std::cout << "Written by Mattias Runge 2010." << std::endl; |
||
| 246 | |||
| 247 | std::cout << "Connecting to " << variable_map["server"].as<std::string>().data() << ":" << variable_map["port"].as<unsigned int>() << "..." << std::endl; |
||
| 248 | } |
||
| 1609 | runge | 249 | |
| 1647 | runge | 250 | try |
| 251 | { |
||
| 252 | cc = ConsoleClient::Pointer(new ConsoleClient(variable_map["server"].as<std::string>(), variable_map["port"].as<unsigned int>())); |
||
| 253 | } |
||
| 254 | catch (std::runtime_error& e) |
||
| 255 | { |
||
| 1664 | runge | 256 | std::cerr << "Connection error: " << e.what() << std::endl; |
| 1647 | runge | 257 | CleanUp(); |
| 258 | return EXIT_FAILURE; |
||
| 259 | } |
||
| 1609 | runge | 260 | |
| 1664 | runge | 261 | if (variable_map["command"].as<std::string>() != "") |
| 1608 | runge | 262 | { |
| 1647 | runge | 263 | boost::mutex::scoped_lock guard(guard_mutex); |
| 264 | on_message_condition.wait(guard); |
||
| 1608 | runge | 265 | |
| 1664 | runge | 266 | if (!finish) |
| 1608 | runge | 267 | { |
| 1664 | runge | 268 | cc->SendResponse(variable_map["command"].as<std::string>()); |
| 269 | |||
| 270 | if (!finish) |
||
| 271 | { |
||
| 272 | on_message_condition.wait(guard); |
||
| 273 | } |
||
| 1608 | runge | 274 | } |
| 1664 | runge | 275 | } |
| 276 | else |
||
| 277 | { |
||
| 278 | while (true) |
||
| 1620 | runge | 279 | { |
| 1664 | runge | 280 | boost::mutex::scoped_lock guard(guard_mutex); |
| 281 | on_message_condition.wait(guard); |
||
| 282 | |||
| 283 | if (finish) |
||
| 1647 | runge | 284 | { |
| 1664 | runge | 285 | break; |
| 1647 | runge | 286 | } |
| 287 | |||
| 1664 | runge | 288 | while ((buffer = readline(cc->GetPrompt().data())) != NULL) |
| 289 | { |
||
| 290 | if (strlen(buffer) == 0) |
||
| 291 | { |
||
| 292 | continue; |
||
| 293 | } |
||
| 294 | |||
| 295 | break; |
||
| 296 | } |
||
| 297 | |||
| 298 | cc->SendResponse(buffer); |
||
| 299 | add_history(buffer); |
||
| 1620 | runge | 300 | } |
| 1608 | runge | 301 | } |
| 302 | |||
| 303 | CleanUp(); |
||
| 304 | |||
| 305 | return EXIT_SUCCESS; |
||
| 306 | } |
||
| 307 | |||
| 308 | static char** AutoComplete(const char* text, int start, int end) |
||
| 309 | { |
||
| 310 | unsigned int count = 0; |
||
| 1647 | runge | 311 | boost::mutex guard_mutex; |
| 312 | |||
| 1608 | runge | 313 | for (unsigned int n = 0; n < start; n++) |
| 314 | { |
||
| 1609 | runge | 315 | if (rl_line_buffer[n] == ' ') |
| 1608 | runge | 316 | { |
| 317 | count++; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 1647 | runge | 321 | cc->AutoCompleteRequest(count, rl_line_buffer); |
| 1608 | runge | 322 | |
| 323 | boost::mutex::scoped_lock guard(guard_mutex); |
||
| 1647 | runge | 324 | on_message_condition.wait(guard); |
| 1608 | runge | 325 | |
| 1647 | runge | 326 | /*if (autocomplete_list.size() == 0) |
| 1609 | runge | 327 | { |
| 328 | return NULL; |
||
| 1647 | runge | 329 | }*/ |
| 1609 | runge | 330 | |
| 1608 | runge | 331 | return rl_completion_matches(text, &AutoCompleteGet); |
| 332 | } |
||
| 333 | |||
| 334 | char* AutoCompleteGet(const char* text, int state) |
||
| 335 | { |
||
| 336 | static int index = 0; |
||
| 337 | int length = strlen(text); |
||
| 338 | std::string name; |
||
| 339 | |||
| 340 | if (!state) // First run |
||
| 341 | { |
||
| 342 | index = 0; |
||
| 343 | } |
||
| 344 | |||
| 345 | while (autocomplete_list.size() > index) |
||
| 346 | { |
||
| 347 | name = autocomplete_list[index]; |
||
| 348 | |||
| 349 | index++; |
||
| 350 | |||
| 351 | if (strncmp(name.data(), text, length) == 0) |
||
| 352 | { |
||
| 353 | char *result = (char*)malloc(name.size() + 1); |
||
| 354 | strcpy(result, name.data()); |
||
| 355 | |||
| 356 | return result; |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | return NULL; |
||
| 361 | } |
||
| 362 | |||
| 363 | void CleanUp() |
||
| 364 | { |
||
| 1664 | runge | 365 | std::cout << std::endl; |
| 366 | //std::cout << "Cleaning up..." << std::endl; |
||
| 1609 | runge | 367 | |
| 368 | write_history(history_filename.data()); |
||
| 1608 | runge | 369 | |
| 370 | cc.reset(); |
||
| 371 | |||
| 372 | net::Manager::Delete(); |
||
| 373 | |||
| 374 | if (buffer != NULL) |
||
| 375 | { |
||
| 376 | free(buffer); |
||
| 377 | } |
||
| 378 | |||
| 1664 | runge | 379 | //std::cout << "Thank you for using Atom. Goodbye!" << std::endl; |
| 1608 | runge | 380 | |
| 381 | tcsetattr(fileno(stdin), TCSANOW, &original_flags); // Restore |
||
| 382 | } |
||
| 383 | |||
| 384 | void Handler(int status) |
||
| 385 | { |
||
| 386 | std::string signal_name = "Unknown"; |
||
| 387 | |||
| 388 | switch (status) |
||
| 389 | { |
||
| 390 | case SIGTERM: |
||
| 391 | { |
||
| 392 | signal_name = "Terminate"; |
||
| 393 | break; |
||
| 394 | } |
||
| 395 | case SIGINT: |
||
| 396 | { |
||
| 397 | signal_name = "Interupt"; |
||
| 398 | break; |
||
| 399 | } |
||
| 400 | case SIGQUIT: |
||
| 401 | { |
||
| 402 | signal_name = "Quit"; |
||
| 403 | break; |
||
| 404 | } |
||
| 405 | case SIGABRT: |
||
| 406 | { |
||
| 407 | signal_name = "Abort"; |
||
| 408 | break; |
||
| 409 | } |
||
| 410 | case SIGIO: |
||
| 411 | { |
||
| 412 | signal_name = "I/O"; |
||
| 413 | break; |
||
| 414 | } |
||
| 415 | case SIGPIPE: |
||
| 416 | { |
||
| 417 | signal_name = "Pipe"; |
||
| 418 | break; |
||
| 419 | } |
||
| 420 | } |
||
| 421 | |||
| 422 | |||
| 423 | if (status != SIGPIPE) |
||
| 424 | { |
||
| 425 | CleanUp(); |
||
| 426 | exit(0); |
||
| 427 | } |
||
| 428 | } |