| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: chuck_shell.cpp |
| 28 | // desc: chuck shell implementation |
| 29 | // |
| 30 | // author: Spencer Salazar ([email protected]) |
| 31 | // date: Autumn 2005 |
| 32 | //----------------------------------------------------------------------------- |
| 33 | #include "chuck_shell.h" |
| 34 | #include "chuck_otf.h" |
| 35 | #include "chuck_errmsg.h" |
| 36 | #include "util_network.h" |
| 37 | #include "util_string.h" |
| 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <errno.h> |
| 41 | #include <ctype.h> |
| 42 | #ifndef __PLATFORM_WINDOWS__ |
| 43 | #include <unistd.h> |
| 44 | #include <sys/param.h> |
| 45 | #include <sys/types.h> |
| 46 | #include <dirent.h> |
| 47 | #endif |
| 48 | |
| 49 | using namespace std; |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | //----------------------------------------------------------------------------- |
| 55 | // name: tokenize_string |
| 56 | // desc: divides a string into space separated tokens, respecting single and |
| 57 | // double quotes |
| 58 | //----------------------------------------------------------------------------- |
| 59 | void tokenize_string( string str, vector< string > & tokens) |
| 60 | { |
| 61 | t_CKINT space = 1; |
| 62 | t_CKINT end_space = 0; |
| 63 | t_CKINT squote = 0; |
| 64 | t_CKINT dquote = 0; |
| 65 | t_CKINT i = 0, j = 0, len = str.size(); |
| 66 | |
| 67 | for( i = 0; i < len; i++ ) |
| 68 | { |
| 69 | if( isspace( str[i] ) && space ) |
| 70 | { |
| 71 | j++; |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | if( isspace( str[i] ) && end_space ) |
| 76 | { |
| 77 | tokens.push_back( string( str, j, i - j ) ); |
| 78 | j = i + 1; |
| 79 | space = 1; |
| 80 | end_space = 0; |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | if( str[i] == '\'' ) |
| 85 | { |
| 86 | if( dquote ) |
| 87 | continue; |
| 88 | if( !squote ) |
| 89 | { |
| 90 | str.erase( i, 1 ); |
| 91 | len--; |
| 92 | i--; |
| 93 | space = 0; |
| 94 | end_space = 0; |
| 95 | squote = 1; |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | else if( str[i - 1] == '\\' ) |
| 100 | { |
| 101 | str.erase( i - 1, 1 ); |
| 102 | len--; |
| 103 | i--; |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | else |
| 108 | { |
| 109 | str.erase( i, 1 ); |
| 110 | len--; |
| 111 | i--; |
| 112 | squote = 0; |
| 113 | end_space = 1; |
| 114 | space = 0; |
| 115 | continue; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if( str[i] == '"' ) //" |
| 120 | { |
| 121 | if( squote ) |
| 122 | continue; |
| 123 | if( !dquote ) |
| 124 | { |
| 125 | str.erase( i, 1 ); |
| 126 | i--; |
| 127 | len--; |
| 128 | space = 0; |
| 129 | end_space = 0; |
| 130 | dquote = 1; |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | else if( str[i - 1] == '\\' ) |
| 135 | { |
| 136 | str.erase( i - 1, 1 ); |
| 137 | len--; |
| 138 | i--; |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | else |
| 143 | { |
| 144 | str.erase( i, 1 ); |
| 145 | i--; |
| 146 | len--; |
| 147 | dquote = 0; |
| 148 | end_space = 1; |
| 149 | space = 0; |
| 150 | continue; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if( !squote && !dquote ) |
| 155 | { |
| 156 | end_space = 1; |
| 157 | space = 0; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if( i > j && end_space ) |
| 162 | { |
| 163 | tokens.push_back( string( str, j, i - j ) ); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | //----------------------------------------------------------------------------- |
| 168 | // name: win32_tmpnam() |
| 169 | // desc: replacement for broken tmpnam() on Windows Vista + 7 |
| 170 | // file_path should be at least MAX_PATH characters. |
| 171 | //----------------------------------------------------------------------------- |
| 172 | #ifdef __PLATFORM_WINDOWS__ |
| 173 | |
| 174 | #include <windows.h> |
| 175 | |
| 176 | int win32_tmpnam(char *file_path) |
| 177 | { |
| 178 | char tmp_path[MAX_PATH]; |
| 179 | |
| 180 | if(GetTempPath(256, tmp_path) == 0) |
| 181 | return 0; |
| 182 | |
| 183 | if(GetTempFileName(tmp_path, "cksh", 0, file_path) == 0) |
| 184 | return 0; |
| 185 | |
| 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | #endif |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | //----------------------------------------------------------------------------- |
| 195 | // name: Chuck_Shell() |
| 196 | // desc: ... |
| 197 | //----------------------------------------------------------------------------- |
| 198 | Chuck_Shell::Chuck_Shell() |
| 199 | { |
| 200 | ui = NULL; |
| 201 | process_vm = NULL; |
| 202 | current_vm = NULL; |
| 203 | initialized = FALSE; |
| 204 | stop = FALSE; |
| 205 | code_entry_active = FALSE; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | //----------------------------------------------------------------------------- |
| 212 | // name: ~Chuck_Shell() |
| 213 | // desc: ... |
| 214 | //----------------------------------------------------------------------------- |
| 215 | Chuck_Shell::~Chuck_Shell() |
| 216 | { |
| 217 | vector<string>::size_type i, len = allocated_commands.size(); |
| 218 | |
| 219 | //iterate through commands, delete the associated heap data |
| 220 | for( i = 0; i != len; i++ ) |
| 221 | CK_SAFE_DELETE( allocated_commands[i] ); |
| 222 | |
| 223 | // delete ui |
| 224 | CK_SAFE_DELETE( ui ); |
| 225 | |
| 226 | // flag |
| 227 | initialized = FALSE; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | //----------------------------------------------------------------------------- |
| 234 | // name: init() |
| 235 | // desc: ... |
| 236 | //----------------------------------------------------------------------------- |
| 237 | t_CKBOOL Chuck_Shell::init( Chuck_VM * vm, Chuck_Shell_UI * ui ) |
| 238 | { |
| 239 | // log |
| 240 | EM_log( CK_LOG_SYSTEM, "initializing chuck shell..." ); |
| 241 | |
| 242 | // check |
| 243 | if( initialized == TRUE ) |
| 244 | { |
| 245 | EM_log( CK_LOG_WARNING, "chuck shell already initialized" ); |
| 246 | return TRUE; |
| 247 | } |
| 248 | |
| 249 | // ui |
| 250 | if( ui == NULL ) |
| 251 | { |
| 252 | CK_FPRINTF_STDERR( "[chuck](via shell): NULL ui passed to Chuck_Shell::init..." ); |
| 253 | return FALSE; |
| 254 | } |
| 255 | |
| 256 | this->ui = ui; |
| 257 | |
| 258 | process_vm = vm; |
| 259 | |
| 260 | Chuck_Shell_Network_VM * cspv = new Chuck_Shell_Network_VM(); |
| 261 | if( !cspv->init( "localhost", 8888 ) ) |
| 262 | { |
| 263 | CK_FPRINTF_STDERR( "[chuck](via shell): error initializing process VM..." ); |
| 264 | CK_SAFE_DELETE( cspv ); |
| 265 | return FALSE; |
| 266 | } |
| 267 | |
| 268 | // chuck shell network VM |
| 269 | current_vm = cspv; |
| 270 | vms.push_back( current_vm->copy() ); |
| 271 | // chuck vm |
| 272 | process_vm = vm; |
| 273 | |
| 274 | code_entry_active = FALSE; |
| 275 | code = ""; |
| 276 | |
| 277 | // init default variables |
| 278 | variables["COMMAND_PROMPT"] = "chuck %> "; |
| 279 | variables["DEFAULT_CONTEXT"] = "shell_global"; |
| 280 | |
| 281 | // initialize prompt |
| 282 | prompt = variables["COMMAND_PROMPT"]; |
| 283 | |
| 284 | Command * temp; |
| 285 | |
| 286 | // initialize the string -> Chuck_Shell_Command map |
| 287 | temp = new Command_VM(); |
| 288 | temp->init( this ); |
| 289 | commands["vm"] = temp; |
| 290 | allocated_commands.push_back( temp ); |
| 291 | |
| 292 | temp = new Command_VMList(); |
| 293 | temp->init( this ); |
| 294 | commands["vms"] = temp; |
| 295 | allocated_commands.push_back( temp ); |
| 296 | |
| 297 | temp = new Command_Add(); |
| 298 | temp->init( this ); |
| 299 | commands["+"] = temp; |
| 300 | commands["add"] = temp; |
| 301 | allocated_commands.push_back( temp ); |
| 302 | |
| 303 | temp = new Command_Remove(); |
| 304 | temp->init( this ); |
| 305 | commands["-"] = temp; |
| 306 | commands["remove"] = temp; |
| 307 | allocated_commands.push_back( temp ); |
| 308 | |
| 309 | temp = new Command_Status(); |
| 310 | temp->init( this ); |
| 311 | commands["status"] = temp; |
| 312 | commands["^"] = temp; |
| 313 | allocated_commands.push_back( temp ); |
| 314 | |
| 315 | temp = new Command_Removeall(); |
| 316 | temp->init( this ); |
| 317 | commands["removeall"] = temp; |
| 318 | allocated_commands.push_back( temp ); |
| 319 | |
| 320 | temp = new Command_Removelast(); |
| 321 | temp->init( this ); |
| 322 | commands["--"] = temp; |
| 323 | allocated_commands.push_back( temp ); |
| 324 | |
| 325 | temp = new Command_Replace(); |
| 326 | temp->init( this ); |
| 327 | commands["replace"] = temp; |
| 328 | commands["="] = temp; |
| 329 | allocated_commands.push_back( temp ); |
| 330 | |
| 331 | temp = new Command_Close(); |
| 332 | temp->init( this ); |
| 333 | commands["close"] = temp; |
| 334 | allocated_commands.push_back( temp ); |
| 335 | |
| 336 | temp = new Command_Kill(); |
| 337 | temp->init( this ); |
| 338 | commands["kill"] = temp; |
| 339 | allocated_commands.push_back( temp ); |
| 340 | |
| 341 | temp = new Command_Exit(); |
| 342 | temp->init( this ); |
| 343 | commands["exit"] = temp; |
| 344 | commands["quit"] = temp; |
| 345 | allocated_commands.push_back( temp ); |
| 346 | |
| 347 | temp = new Command_Ls(); |
| 348 | temp->init( this ); |
| 349 | commands["ls"] = temp; |
| 350 | allocated_commands.push_back( temp ); |
| 351 | |
| 352 | temp = new Command_Pwd(); |
| 353 | temp->init( this ); |
| 354 | commands["pwd"] = temp; |
| 355 | allocated_commands.push_back( temp ); |
| 356 | |
| 357 | temp = new Command_Cd(); |
| 358 | temp->init( this ); |
| 359 | commands["cd"] = temp; |
| 360 | allocated_commands.push_back( temp ); |
| 361 | |
| 362 | temp = new Command_Alias(); |
| 363 | temp->init( this ); |
| 364 | commands["alias"] = temp; |
| 365 | allocated_commands.push_back( temp ); |
| 366 | |
| 367 | temp = new Command_Unalias(); |
| 368 | temp->init( this ); |
| 369 | commands["unalias"] = temp; |
| 370 | allocated_commands.push_back( temp ); |
| 371 | |
| 372 | temp = new Command_Source(); |
| 373 | temp->init( this ); |
| 374 | commands["source"] = temp; |
| 375 | commands["."] = temp; |
| 376 | allocated_commands.push_back( temp ); |
| 377 | |
| 378 | temp = new Command_Code(); |
| 379 | temp->init( this ); |
| 380 | commands["code"] = temp; |
| 381 | allocated_commands.push_back( temp ); |
| 382 | |
| 383 | // flag |
| 384 | initialized = TRUE; |
| 385 | |
| 386 | return TRUE; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | //----------------------------------------------------------------------------- |
| 393 | // name: run() |
| 394 | // desc: ... |
| 395 | //----------------------------------------------------------------------------- |
| 396 | void Chuck_Shell::run() |
| 397 | { |
| 398 | // log |
| 399 | EM_log( CK_LOG_SYSTEM, "running chuck shell..." ); |
| 400 | |
| 401 | // make sure |
| 402 | if(initialized == FALSE) |
| 403 | { |
| 404 | CK_FPRINTF_STDERR( "[chuck](via shell): shell not initialized...\n" ); |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | string command; |
| 409 | string result; |
| 410 | |
| 411 | ui->next_result( "welcome to chuck shell!\n" ); |
| 412 | // ui->next_result( "(type \"help\" for more information)\n" ); |
| 413 | |
| 414 | // loop |
| 415 | for( ; !stop; ) |
| 416 | { |
| 417 | // get command |
| 418 | if( ui->next_command( prompt, command ) == TRUE ) |
| 419 | { |
| 420 | // result.clear(); |
| 421 | result = ""; |
| 422 | |
| 423 | //printf( "chuck_shell::run\n" ); |
| 424 | |
| 425 | // execute the command |
| 426 | execute( command, result ); |
| 427 | |
| 428 | // command.clear(); |
| 429 | command = ""; |
| 430 | |
| 431 | // pass the result to the shell ui |
| 432 | ui->next_result( result ); |
| 433 | } |
| 434 | else |
| 435 | // done |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | // log |
| 440 | EM_log( CK_LOG_SYSTEM, "exiting chuck shell..." ); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | //----------------------------------------------------------------------------- |
| 447 | // name: execute |
| 448 | // desc: ... |
| 449 | //----------------------------------------------------------------------------- |
| 450 | t_CKBOOL Chuck_Shell::execute( string & in, string & out ) |
| 451 | { |
| 452 | if( !initialized ) |
| 453 | { |
| 454 | return FALSE; |
| 455 | } |
| 456 | |
| 457 | // vector of string tokens |
| 458 | vector< string > vec; |
| 459 | |
| 460 | // clear the response |
| 461 | out = ""; |
| 462 | |
| 463 | // first find out if this is code to run |
| 464 | if( !code_entry_active && in[in.find_first_not_of( " \t\v\n" )] == '{' ) |
| 465 | start_code(); |
| 466 | |
| 467 | if( code_entry_active ) |
| 468 | { |
| 469 | continue_code( in ); |
| 470 | |
| 471 | if( code_entry_active == FALSE ) |
| 472 | { |
| 473 | //strip opening and closing braces |
| 474 | string::size_type k = code.find( "{" ); |
| 475 | string head = string( code, 0, k ); |
| 476 | code = string( code, k + 1, code.size() - k - 1 ); |
| 477 | |
| 478 | k = code.rfind( "}" ); |
| 479 | string tail = string( code, k + 1, code.size() - k - 1 ); |
| 480 | code = string( code, 0, k ); |
| 481 | |
| 482 | do_code( code, out ); |
| 483 | |
| 484 | tokenize_string( tail, vec ); |
| 485 | |
| 486 | if( vec.size() > 0 ) |
| 487 | { |
| 488 | string temp = "code save " + vec[0]; |
| 489 | execute( temp, out ); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | return TRUE; |
| 494 | } |
| 495 | |
| 496 | // divide the string into white space separated substrings |
| 497 | tokenize_string( in, vec ); |
| 498 | |
| 499 | // if no tokens |
| 500 | if( vec.size() == 0) |
| 501 | return TRUE; |
| 502 | |
| 503 | // first check if the first token matches an alias |
| 504 | while( aliases.find( vec[0] ) != aliases.end() ) |
| 505 | // use a while loop to handle nested aliases |
| 506 | { |
| 507 | vector< string > vec2; |
| 508 | tokenize_string( aliases[vec[0]], vec2 ); |
| 509 | vec.erase( vec.begin() ); |
| 510 | vec2.insert( vec2.end(), vec.begin(), vec.end() ); |
| 511 | vec = vec2; |
| 512 | } |
| 513 | |
| 514 | // locate the command |
| 515 | if( commands.find( vec[0] ) == commands.end() ) |
| 516 | // command doesn't exist! |
| 517 | { |
| 518 | out += vec[0] + ": command not found\n"; |
| 519 | return FALSE; |
| 520 | } |
| 521 | |
| 522 | else |
| 523 | // execute the command |
| 524 | { |
| 525 | Command * command = commands[vec[0]]; |
| 526 | vec.erase( vec.begin() ); |
| 527 | if( command->execute( vec, out ) == 0 ) |
| 528 | return TRUE; |
| 529 | |
| 530 | else |
| 531 | return FALSE; |
| 532 | } |
| 533 | |
| 534 | return TRUE; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | //----------------------------------------------------------------------------- |
| 541 | // name: start_code() |
| 542 | // desc: ... |
| 543 | //----------------------------------------------------------------------------- |
| 544 | void Chuck_Shell::start_code() |
| 545 | { |
| 546 | code = ""; |
| 547 | code_entry_active = TRUE; |
| 548 | scope = 0; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | |
| 553 | |
| 554 | //----------------------------------------------------------------------------- |
| 555 | // name: continue_code() |
| 556 | // desc: ... |
| 557 | //----------------------------------------------------------------------------- |
| 558 | void Chuck_Shell::continue_code( string & in ) |
| 559 | { |
| 560 | if( in.find( "{" ) != string::npos ) |
| 561 | // increase the scope one level and change the prompt |
| 562 | { |
| 563 | char buf[16]; |
| 564 | scope++; |
| 565 | #ifndef __PLATFORM_WINDOWS__ |
| 566 | snprintf( buf, 16, "code %2d> ", (int)scope ); |
| 567 | #else |
| 568 | snprintf( buf, 16, "code %2ld> ", (long)scope); |
| 569 | #endif |
| 570 | prompt = buf; |
| 571 | } |
| 572 | |
| 573 | if( in.find( "}" ) != string::npos ) |
| 574 | // decrease the scope one level and change the prompt |
| 575 | { |
| 576 | char buf[16]; |
| 577 | scope--; |
| 578 | #ifndef __PLATFORM_WINDOWS__ |
| 579 | snprintf( buf, 16, "code %2d> ", (int)scope ); |
| 580 | #else |
| 581 | snprintf( buf, 16, "code %2ld> ", (long)scope); |
| 582 | #endif |
| 583 | prompt = buf; |
| 584 | } |
| 585 | |
| 586 | // collect this line as ChucK code |
| 587 | code += in + "\n"; |
| 588 | |
| 589 | if( scope == 0 ) |
| 590 | // the end of this code block -- lets execute it on the current_vm |
| 591 | code_entry_active = FALSE; |
| 592 | } |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | //----------------------------------------------------------------------------- |
| 598 | // name: do_code() |
| 599 | // desc: ... |
| 600 | //----------------------------------------------------------------------------- |
| 601 | void Chuck_Shell::do_code( string & code, string & out, string command ) |
| 602 | { |
| 603 | // open a temporary file |
| 604 | #if defined(__PLATFORM_LINUX__) || defined(__PLATFORM_APPLE__) |
| 605 | char tmp_dir[] = "/tmp"; |
| 606 | char * tmp_filepath = new char [32]; |
| 607 | // 1.4.1.0: changed chuck_file.XXXXXX to chuck_shell.XXXXXX |
| 608 | strncpy( tmp_filepath, "/tmp/chuck_shell.XXXXXX", 32 ); |
| 609 | int fd = mkstemp( tmp_filepath ); |
| 610 | if( fd == -1 ) |
| 611 | { |
| 612 | out += string( "shell: error: unable to create tmpfile at " ) + tmp_dir + "\n"; |
| 613 | delete[] tmp_filepath; |
| 614 | prompt = variables["COMMAND_PROMPT"]; |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | FILE * tmp_file = fdopen( fd, "w+" ); |
| 619 | if( tmp_file == NULL ) |
| 620 | { |
| 621 | out += string( "shell: error: unable to reopen tmpfile at " ) + tmp_filepath + "\n"; |
| 622 | delete[] tmp_filepath; |
| 623 | prompt = variables["COMMAND_PROMPT"]; |
| 624 | return; |
| 625 | } |
| 626 | #else |
| 627 | char tmp_filepath1[MAX_PATH]; |
| 628 | win32_tmpnam(tmp_filepath1); |
| 629 | |
| 630 | if( tmp_filepath1 == NULL ) |
| 631 | { |
| 632 | out += string( "shell: error: unable to generate tmpfile name\n" ); |
| 633 | prompt = variables["COMMAND_PROMPT"]; |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | string tmp_filepath_stl = normalize_directory_separator(string(tmp_filepath1)); |
| 638 | const char *tmp_filepath = tmp_filepath_stl.c_str(); |
| 639 | FILE * tmp_file = fopen( tmp_filepath, "w" ); |
| 640 | if( tmp_file == NULL ) |
| 641 | { |
| 642 | out += string( "shell: error: unable to open tmpfile '" ) + tmp_filepath + "'\n"; |
| 643 | prompt = variables["COMMAND_PROMPT"]; |
| 644 | return; |
| 645 | } |
| 646 | #endif |
| 647 | |
| 648 | // write the code to the temp file |
| 649 | fprintf( tmp_file, "%s", code.c_str() ); |
| 650 | fclose( tmp_file ); |
| 651 | |
| 652 | string argv = string( command ) + tmp_filepath; |
| 653 | |
| 654 | this->execute( argv, out ); |
| 655 | // if( this->execute( argv, out ) ) |
| 656 | // ; |
| 657 | |
| 658 | // delete the file |
| 659 | #ifndef __PLATFORM_WINDOWS__ |
| 660 | unlink( tmp_filepath ); |
| 661 | #else |
| 662 | DeleteFile( tmp_filepath ); |
| 663 | #endif // __PLATFORM_WINDOWS__ |
| 664 | |
| 665 | #if defined(__PLATFORM_LINUX__) |
| 666 | delete[] tmp_filepath; |
| 667 | #endif |
| 668 | |
| 669 | prompt = variables["COMMAND_PROMPT"]; |
| 670 | } |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | //----------------------------------------------------------------------------- |
| 676 | // name: do_context() |
| 677 | // desc: ... |
| 678 | //----------------------------------------------------------------------------- |
| 679 | void Chuck_Shell::do_code_context( string & ) |
| 680 | { |
| 681 | |
| 682 | } |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | //----------------------------------------------------------------------------- |
| 688 | // name: close() |
| 689 | // desc: ... |
| 690 | //----------------------------------------------------------------------------- |
| 691 | void Chuck_Shell::close() |
| 692 | { |
| 693 | stop = TRUE; |
| 694 | // |
| 695 | } |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | //----------------------------------------------------------------------------- |
| 701 | // name: kill() |
| 702 | // desc: ... |
| 703 | //----------------------------------------------------------------------------- |
| 704 | void Chuck_Shell::exit() |
| 705 | { |
| 706 | stop = TRUE; |
| 707 | |
| 708 | // REFACTOR 2017 TODO Ge: all_stop |
| 709 | /* |
| 710 | if( process_vm != NULL ) |
| 711 | all_stop(); // ge: 1.3.5.3 |
| 712 | // process_vm->stop(); |
| 713 | */ |
| 714 | } |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | //----------------------------------------------------------------------------- |
| 720 | // name: init() |
| 721 | // desc: ... |
| 722 | //----------------------------------------------------------------------------- |
| 723 | t_CKBOOL Chuck_Shell_Network_VM::init( const string & hostname, t_CKINT port ) |
| 724 | { |
| 725 | this->hostname = hostname; |
| 726 | this->port = port; |
| 727 | return TRUE; |
| 728 | } |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | //----------------------------------------------------------------------------- |
| 734 | // name: copy() |
| 735 | // desc: ... |
| 736 | //----------------------------------------------------------------------------- |
| 737 | Chuck_Shell_VM * Chuck_Shell_Network_VM::copy() |
| 738 | { |
| 739 | Chuck_Shell_Network_VM * net_vm = new Chuck_Shell_Network_VM(); |
| 740 | net_vm->init( hostname, port ); |
| 741 | return net_vm; |
| 742 | } |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | //----------------------------------------------------------------------------- |
| 748 | // name: add_shred() |
| 749 | // desc: ... |
| 750 | //----------------------------------------------------------------------------- |
| 751 | t_CKBOOL Chuck_Shell_Network_VM::add_shred( const vector< string > & files, |
| 752 | string & out ) |
| 753 | { |
| 754 | t_CKINT i = 0; |
| 755 | t_CKBOOL return_val; |
| 756 | vector<string>::size_type j, str_len, vec_len = files.size() + 1; |
| 757 | const char ** argv = new const char * [ vec_len ]; |
| 758 | |
| 759 | /* prepare an argument vector to submit to otf_send_cmd */ |
| 760 | /* first, specify an add command */ |
| 761 | char * argv0 = new char [2]; |
| 762 | strncpy( argv0, "+", 2 ); |
| 763 | argv[0] = argv0; |
| 764 | |
| 765 | /* copy file paths into argv */ |
| 766 | for( j = 1; j < vec_len; j++ ) |
| 767 | { |
| 768 | str_len = files[j - 1].size() + 1; |
| 769 | char * argvj = new char [str_len]; |
| 770 | strncpy( argvj, files[j - 1].c_str(), str_len ); |
| 771 | argv[j] = argvj; |
| 772 | } |
| 773 | |
| 774 | /* send the command */ |
| 775 | if( otf_send_cmd( vec_len, argv, i, hostname.c_str(), port ) ) |
| 776 | return_val = TRUE; |
| 777 | else |
| 778 | { |
| 779 | out += "add: error: command failed\n"; |
| 780 | return_val = FALSE; |
| 781 | } |
| 782 | |
| 783 | /* clean up heap data */ |
| 784 | for( j = 0; j < vec_len; j++ ) |
| 785 | { |
| 786 | // TODO: verify this is alright |
| 787 | char * arg = (char *)argv[j]; |
| 788 | delete[] arg; |
| 789 | } |
| 790 | delete[] argv; |
| 791 | |
| 792 | return return_val; |
| 793 | } |
| 794 | |
| 795 | |
| 796 | |
| 797 | |
| 798 | //----------------------------------------------------------------------------- |
| 799 | // name: remove_shred() |
| 800 | // desc: ... |
| 801 | //----------------------------------------------------------------------------- |
| 802 | t_CKBOOL Chuck_Shell_Network_VM::remove_shred( const vector< string > & ids, |
| 803 | string & out ) |
| 804 | { |
| 805 | t_CKINT i = 0; |
| 806 | t_CKBOOL return_val; |
| 807 | vector<string>::size_type j, str_len, vec_len = ids.size() + 1; |
| 808 | const char ** argv = new const char * [ vec_len ]; |
| 809 | |
| 810 | /* prepare an argument vector to submit to otf_send_cmd */ |
| 811 | /* first, specify an add command */ |
| 812 | char * argv0 = new char [2]; |
| 813 | strncpy( argv0, "-", 2 ); |
| 814 | argv[0] = argv0; |
| 815 | |
| 816 | /* copy ids into argv */ |
| 817 | for( j = 1; j < vec_len; j++ ) |
| 818 | { |
| 819 | str_len = ids[j - 1].size() + 1; |
| 820 | char * argvj = new char [str_len]; |
| 821 | strncpy( argvj, ids[j - 1].c_str(), str_len ); |
| 822 | argv[j] = argvj; |
| 823 | } |
| 824 | |
| 825 | /* send the command */ |
| 826 | if( otf_send_cmd( vec_len, argv, i, hostname.c_str(), port ) ) |
| 827 | return_val = TRUE; |
| 828 | else |
| 829 | { |
| 830 | out += "remove: error: command failed\n"; |
| 831 | return_val = FALSE; |
| 832 | } |
| 833 | |
| 834 | /* clean up heap data */ |
| 835 | for( j = 0; j < vec_len; j++ ) |
| 836 | { |
| 837 | // TODO: verify this is ok |
| 838 | char * arg = (char *)argv[j]; |
| 839 | delete[] arg; |
| 840 | } |
| 841 | delete[] argv; |
| 842 | |
| 843 | return return_val; |
| 844 | } |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | //----------------------------------------------------------------------------- |
| 850 | // name: remove_all() |
| 851 | // desc: ... |
| 852 | //----------------------------------------------------------------------------- |
| 853 | t_CKBOOL Chuck_Shell_Network_VM::remove_all( string & out ) |
| 854 | { |
| 855 | t_CKBOOL return_val = TRUE; |
| 856 | t_CKINT j = 0; |
| 857 | const char * argv = "--removeall"; |
| 858 | if( !otf_send_cmd( 1, &argv, j, hostname.c_str(), port ) ) |
| 859 | { |
| 860 | out += "removeall: error: command failed\n"; |
| 861 | return_val = FALSE; |
| 862 | } |
| 863 | |
| 864 | return return_val; |
| 865 | } |
| 866 | |
| 867 | |
| 868 | |
| 869 | |
| 870 | //----------------------------------------------------------------------------- |
| 871 | // name: remove_last() |
| 872 | // desc: ... |
| 873 | //----------------------------------------------------------------------------- |
| 874 | t_CKBOOL Chuck_Shell_Network_VM::remove_last( string & out ) |
| 875 | { |
| 876 | t_CKBOOL return_val = TRUE; |
| 877 | t_CKINT j = 0; |
| 878 | const char * argv = "--"; |
| 879 | if( !otf_send_cmd( 1, &argv, j, hostname.c_str(), port ) ) |
| 880 | { |
| 881 | out += "removelast: error: command failed\n"; |
| 882 | return_val = FALSE; |
| 883 | } |
| 884 | |
| 885 | return return_val; |
| 886 | } |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | //----------------------------------------------------------------------------- |
| 892 | // name: replace_shred() |
| 893 | // desc: ... |
| 894 | //----------------------------------------------------------------------------- |
| 895 | t_CKBOOL Chuck_Shell_Network_VM::replace_shred( const vector< string > &vec, |
| 896 | string & out ) |
| 897 | { |
| 898 | if( vec.size() < 2 ) |
| 899 | { |
| 900 | out += "replace: error: insufficient arguments...\n"; |
| 901 | return FALSE; |
| 902 | } |
| 903 | |
| 904 | t_CKINT i = 0; |
| 905 | t_CKBOOL return_val; |
| 906 | vector<string>::size_type j, str_len, vec_len = vec.size() + 1; |
| 907 | const char ** argv = new const char * [ vec_len ]; |
| 908 | |
| 909 | /* prepare an argument vector to submit to otf_send_cmd */ |
| 910 | /* first, specify an add command */ |
| 911 | argv[0] = "--replace"; |
| 912 | |
| 913 | /* copy ids/files into argv */ |
| 914 | for( j = 1; j < vec_len; j++ ) |
| 915 | { |
| 916 | str_len = vec[j - 1].size() + 1; |
| 917 | char * argvj = new char [str_len]; |
| 918 | strncpy( argvj, vec[j - 1].c_str(), str_len ); |
| 919 | argv[j] = argvj; |
| 920 | } |
| 921 | |
| 922 | /* send the command */ |
| 923 | if( otf_send_cmd( vec_len, argv, i, hostname.c_str(), port ) ) |
| 924 | return_val = TRUE; |
| 925 | |
| 926 | else |
| 927 | { |
| 928 | out += "replace: error: command failed\n"; |
| 929 | return_val = FALSE; |
| 930 | } |
| 931 | |
| 932 | if( vec.size() % 2 != 0 ) |
| 933 | { |
| 934 | out += "repalce: warning: ignoring excess arguments\n"; |
| 935 | return FALSE; |
| 936 | } |
| 937 | |
| 938 | /* clean up heap data */ |
| 939 | for( j = 1; j < vec_len; j++ ) |
| 940 | { |
| 941 | // TODO: verify this is ok |
| 942 | char * arg = (char *)argv[j]; |
| 943 | delete[] arg; |
| 944 | } |
| 945 | delete[] argv; |
| 946 | |
| 947 | return return_val; |
| 948 | } |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | //----------------------------------------------------------------------------- |
| 954 | // name: status() |
| 955 | // desc: ... |
| 956 | //----------------------------------------------------------------------------- |
| 957 | t_CKBOOL Chuck_Shell_Network_VM::status( string & out ) |
| 958 | { |
| 959 | const char * argv = "--status"; |
| 960 | t_CKBOOL return_val = FALSE; |
| 961 | t_CKINT j = 0; |
| 962 | |
| 963 | if( otf_send_cmd( 1, &argv, j, hostname.c_str(), port ) ) |
| 964 | return_val = TRUE; |
| 965 | else |
| 966 | { |
| 967 | return_val = FALSE; |
| 968 | out += "status: error: command failed\n"; |
| 969 | } |
| 970 | |
| 971 | return return_val; |
| 972 | } |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | //----------------------------------------------------------------------------- |
| 978 | // name: kill() |
| 979 | // desc: ... |
| 980 | //----------------------------------------------------------------------------- |
| 981 | t_CKBOOL Chuck_Shell_Network_VM::kill( string & out ) |
| 982 | { |
| 983 | const char * argv = "--kill"; |
| 984 | t_CKINT j = 0; |
| 985 | t_CKBOOL return_val; |
| 986 | |
| 987 | if( otf_send_cmd( 1, &argv, j, hostname.c_str(), port ) ) |
| 988 | return_val = TRUE; |
| 989 | |
| 990 | else |
| 991 | { |
| 992 | return_val = FALSE; |
| 993 | out += "kill: error: command failed"; |
| 994 | } |
| 995 | |
| 996 | return return_val; |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | //----------------------------------------------------------------------------- |
| 1003 | // name: fullname() |
| 1004 | // desc: returns a somewhat descriptive full name for this VM |
| 1005 | //----------------------------------------------------------------------------- |
| 1006 | string Chuck_Shell_Network_VM::fullname() |
| 1007 | { |
| 1008 | char buf[16]; |
| 1009 | snprintf( buf, 16, ":%u", (int)port ); |
| 1010 | |
| 1011 | return hostname + buf; |
| 1012 | } |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | //----------------------------------------------------------------------------- |
| 1018 | // name: init() |
| 1019 | // desc: ... |
| 1020 | //----------------------------------------------------------------------------- |
| 1021 | t_CKBOOL Chuck_Shell_UI::init() |
| 1022 | { |
| 1023 | return TRUE; |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | //----------------------------------------------------------------------------- |
| 1030 | // name: init() |
| 1031 | // desc: ... |
| 1032 | //----------------------------------------------------------------------------- |
| 1033 | t_CKBOOL Chuck_Shell::Command::init( Chuck_Shell * caller ) |
| 1034 | { |
| 1035 | this->caller = caller; |
| 1036 | return TRUE; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | |
| 1041 | |
| 1042 | //----------------------------------------------------------------------------- |
| 1043 | // name: usage() |
| 1044 | // desc: ... |
| 1045 | //----------------------------------------------------------------------------- |
| 1046 | string Chuck_Shell::Command::usage() |
| 1047 | { |
| 1048 | return "no usage specified"; |
| 1049 | } |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | |
| 1054 | //----------------------------------------------------------------------------- |
| 1055 | // name: long_usage() |
| 1056 | // desc: ... |
| 1057 | //----------------------------------------------------------------------------- |
| 1058 | string Chuck_Shell::Command::long_usage() |
| 1059 | { |
| 1060 | return "no usage specified"; |
| 1061 | } |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | //----------------------------------------------------------------------------- |
| 1067 | // name: execute() |
| 1068 | // desc: ... |
| 1069 | //----------------------------------------------------------------------------- |
| 1070 | t_CKINT Chuck_Shell::Command_Add::execute( vector< string > & argv, |
| 1071 | string & out ) |
| 1072 | { |
| 1073 | t_CKINT result = 0; |
| 1074 | |
| 1075 | if( caller->current_vm == NULL) |
| 1076 | { |
| 1077 | out += "add: error: not attached to a VM\n"; |
| 1078 | result = -1; |
| 1079 | } |
| 1080 | |
| 1081 | else if( argv.size() < 1 ) |
| 1082 | { |
| 1083 | out += "usage: " + usage() + "\n"; |
| 1084 | result = -1; |
| 1085 | } |
| 1086 | |
| 1087 | else |
| 1088 | { |
| 1089 | result = caller->current_vm->add_shred( argv, out ); |
| 1090 | } |
| 1091 | |
| 1092 | return result; |
| 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | |
| 1097 | |
| 1098 | //----------------------------------------------------------------------------- |
| 1099 | // name: usage() |
| 1100 | // desc: ... |
| 1101 | //----------------------------------------------------------------------------- |
| 1102 | string Chuck_Shell::Command_Add::usage() |
| 1103 | { |
| 1104 | return "add file..."; |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | //----------------------------------------------------------------------------- |
| 1111 | // name: execute() |
| 1112 | // desc: ... |
| 1113 | //----------------------------------------------------------------------------- |
| 1114 | t_CKINT Chuck_Shell::Command_Remove::execute( vector< string > & argv, |
| 1115 | string & out ) |
| 1116 | { |
| 1117 | t_CKINT result = 0; |
| 1118 | |
| 1119 | if( caller->current_vm == NULL) |
| 1120 | { |
| 1121 | out += "remove: error: not attached to a VM\n"; |
| 1122 | result = -1; |
| 1123 | } |
| 1124 | |
| 1125 | else if( argv.size() < 1 ) |
| 1126 | { |
| 1127 | out += "usage: " + usage() + "\n"; |
| 1128 | result = -1; |
| 1129 | } |
| 1130 | |
| 1131 | else |
| 1132 | result = caller->current_vm->remove_shred( argv, out ); |
| 1133 | |
| 1134 | return result; |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | |
| 1140 | //----------------------------------------------------------------------------- |
| 1141 | // name: usage() |
| 1142 | // desc: ... |
| 1143 | //----------------------------------------------------------------------------- |
| 1144 | string Chuck_Shell::Command_Remove::usage() |
| 1145 | { |
| 1146 | return "remove shred_number..."; |
| 1147 | } |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | //----------------------------------------------------------------------------- |
| 1153 | // name: execute() |
| 1154 | // desc: ... |
| 1155 | //----------------------------------------------------------------------------- |
| 1156 | t_CKINT Chuck_Shell::Command_Removeall::execute( vector< string > & argv, |
| 1157 | string & out ) |
| 1158 | { |
| 1159 | t_CKINT result = 0; |
| 1160 | |
| 1161 | if( caller->current_vm == NULL) |
| 1162 | { |
| 1163 | out += "removeall: error: not attached to a VM\n"; |
| 1164 | result = -1; |
| 1165 | } |
| 1166 | |
| 1167 | else if( argv.size() > 0 ) |
| 1168 | { |
| 1169 | out += "usage " + usage() + "\n"; |
| 1170 | result = -1; |
| 1171 | } |
| 1172 | |
| 1173 | else |
| 1174 | result = caller->current_vm->remove_all( out ); |
| 1175 | |
| 1176 | return result; |
| 1177 | } |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | //----------------------------------------------------------------------------- |
| 1183 | // name: usage() |
| 1184 | // desc: ... |
| 1185 | //----------------------------------------------------------------------------- |
| 1186 | string Chuck_Shell::Command_Removeall::usage() |
| 1187 | { |
| 1188 | return "removeall"; |
| 1189 | } |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | //----------------------------------------------------------------------------- |
| 1195 | // name: execute() |
| 1196 | // desc: ... |
| 1197 | //----------------------------------------------------------------------------- |
| 1198 | t_CKINT Chuck_Shell::Command_Removelast::execute( vector< string > & argv, |
| 1199 | string & out ) |
| 1200 | { |
| 1201 | t_CKINT result = -1; |
| 1202 | |
| 1203 | if( caller->current_vm == NULL) |
| 1204 | out += "removelast: error: not attached to a VM\n"; |
| 1205 | else |
| 1206 | result = caller->current_vm->remove_last( out ); |
| 1207 | |
| 1208 | if( argv.size() > 0 ) |
| 1209 | out += "removelast: warning: ignoring excess arguments...\n"; |
| 1210 | |
| 1211 | return result; |
| 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | //----------------------------------------------------------------------------- |
| 1218 | // name: usage() |
| 1219 | // desc: ... |
| 1220 | //----------------------------------------------------------------------------- |
| 1221 | string Chuck_Shell::Command_Removelast::usage() |
| 1222 | { |
| 1223 | return "removelast"; |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | //----------------------------------------------------------------------------- |
| 1230 | // name: execute() |
| 1231 | // desc: ... |
| 1232 | //----------------------------------------------------------------------------- |
| 1233 | t_CKINT Chuck_Shell::Command_Replace::execute( vector< string > & argv, |
| 1234 | string & out ) |
| 1235 | { |
| 1236 | if( caller->current_vm == NULL) |
| 1237 | { |
| 1238 | out += "replace: error: not attached to a VM\n"; |
| 1239 | return -1; |
| 1240 | } |
| 1241 | |
| 1242 | caller->current_vm->replace_shred( argv, out ); |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | //----------------------------------------------------------------------------- |
| 1251 | // name: usage() |
| 1252 | // desc: ... |
| 1253 | //----------------------------------------------------------------------------- |
| 1254 | string Chuck_Shell::Command_Replace::usage() |
| 1255 | { |
| 1256 | return "replace shred_id file..."; |
| 1257 | } |
| 1258 | |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | //----------------------------------------------------------------------------- |
| 1263 | // name: execute() |
| 1264 | // desc: ... |
| 1265 | //----------------------------------------------------------------------------- |
| 1266 | t_CKINT Chuck_Shell::Command_Status::execute( vector< string > & argv, |
| 1267 | string & out ) |
| 1268 | { |
| 1269 | t_CKINT result = 0; |
| 1270 | |
| 1271 | if( caller->current_vm == NULL) |
| 1272 | { |
| 1273 | out += "status: error: not attached to a VM\n"; |
| 1274 | result = -1; |
| 1275 | } |
| 1276 | |
| 1277 | else if( argv.size() > 0 ) |
| 1278 | { |
| 1279 | out += "usage: " + usage() + "\n"; |
| 1280 | result = -1; |
| 1281 | } |
| 1282 | |
| 1283 | else |
| 1284 | result = caller->current_vm->status( out ); |
| 1285 | |
| 1286 | return result; |
| 1287 | } |
| 1288 | |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | //----------------------------------------------------------------------------- |
| 1293 | // name: usage() |
| 1294 | // desc: ... |
| 1295 | //----------------------------------------------------------------------------- |
| 1296 | string Chuck_Shell::Command_Status::usage() |
| 1297 | { |
| 1298 | return "status"; |
| 1299 | } |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | |
| 1304 | //----------------------------------------------------------------------------- |
| 1305 | // name: execute() |
| 1306 | // desc: ... |
| 1307 | //----------------------------------------------------------------------------- |
| 1308 | t_CKINT Chuck_Shell::Command_Kill::execute( vector< string > & argv, |
| 1309 | string & out ) |
| 1310 | { |
| 1311 | caller->current_vm->kill( out ); |
| 1312 | if( argv.size() > 0 ) |
| 1313 | out += "kill: warning: ignoring excess arguments...\n"; |
| 1314 | |
| 1315 | return 0; |
| 1316 | } |
| 1317 | |
| 1318 | |
| 1319 | |
| 1320 | |
| 1321 | //----------------------------------------------------------------------------- |
| 1322 | // name: execute() |
| 1323 | // desc: ... |
| 1324 | //----------------------------------------------------------------------------- |
| 1325 | t_CKINT Chuck_Shell::Command_Close::execute( vector< string > & argv, |
| 1326 | string & out ) |
| 1327 | { |
| 1328 | caller->close(); |
| 1329 | |
| 1330 | out += "closing chuck shell...bye!\n"; |
| 1331 | |
| 1332 | // REFACTOR-2017: removed: this needs to be done outside |
| 1333 | // if( g_shell != NULL ) |
| 1334 | // out += "(note: in-process VM still running, hit ctrl-c to exit)\n"; |
| 1335 | |
| 1336 | if( argv.size() > 0 ) |
| 1337 | out += "close: warning: ignoring excess arguments...\n"; |
| 1338 | |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | |
| 1344 | |
| 1345 | //----------------------------------------------------------------------------- |
| 1346 | // name: execute() |
| 1347 | // desc: ... |
| 1348 | //----------------------------------------------------------------------------- |
| 1349 | t_CKINT Chuck_Shell::Command_Exit::execute( vector< string > & argv, |
| 1350 | string & out ) |
| 1351 | { |
| 1352 | caller->exit(); |
| 1353 | if( argv.size() > 0 ) |
| 1354 | out += "exit: warning: ignoring excess arguments...\n"; |
| 1355 | |
| 1356 | return 0; |
| 1357 | } |
| 1358 | |
| 1359 | |
| 1360 | |
| 1361 | |
| 1362 | //----------------------------------------------------------------------------- |
| 1363 | // name: execute() |
| 1364 | // desc: ... |
| 1365 | //----------------------------------------------------------------------------- |
| 1366 | t_CKINT Chuck_Shell::Command_Ls::execute( vector< string > & argv, |
| 1367 | string & out ) |
| 1368 | { |
| 1369 | #ifndef __PLATFORM_WINDOWS__ |
| 1370 | |
| 1371 | if( argv.size() == 0 ) |
| 1372 | { |
| 1373 | DIR * dir_handle = opendir( "." ); |
| 1374 | if( dir_handle == NULL ) |
| 1375 | { |
| 1376 | out += "ls: error: unable to open directory .\n"; |
| 1377 | return -1; |
| 1378 | } |
| 1379 | |
| 1380 | dirent * dir_entity = readdir( dir_handle ); |
| 1381 | |
| 1382 | while( dir_entity != NULL ) |
| 1383 | { |
| 1384 | out += string( dir_entity->d_name ) + "\n"; |
| 1385 | dir_entity = readdir( dir_handle ); |
| 1386 | } |
| 1387 | |
| 1388 | closedir( dir_handle ); |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
| 1392 | t_CKINT i, len = argv.size(); |
| 1393 | t_CKBOOL print_parent_name = len > 1 ? TRUE : FALSE; |
| 1394 | |
| 1395 | for( i = 0; i < len; i++ ) |
| 1396 | { |
| 1397 | DIR * dir_handle = opendir( argv[i].c_str() ); |
| 1398 | if( dir_handle == NULL ) |
| 1399 | { |
| 1400 | out += "ls: error: unable to open directory " + argv[i] + "\n"; |
| 1401 | continue; |
| 1402 | } |
| 1403 | |
| 1404 | dirent * dir_entity = readdir( dir_handle ); |
| 1405 | |
| 1406 | if( print_parent_name ) |
| 1407 | out += argv[i] + ":\n"; |
| 1408 | |
| 1409 | while( dir_entity != NULL ) |
| 1410 | { |
| 1411 | out += string( dir_entity->d_name ) + "\n"; |
| 1412 | dir_entity = readdir( dir_handle ); |
| 1413 | } |
| 1414 | |
| 1415 | if( print_parent_name ) |
| 1416 | out += "\n"; |
| 1417 | |
| 1418 | closedir( dir_handle ); |
| 1419 | } |
| 1420 | |
| 1421 | #else |
| 1422 | |
| 1423 | if( argv.size() == 0 ) |
| 1424 | { |
| 1425 | t_CKUINT i, k = 64; |
| 1426 | LPTSTR cwd = new char [k]; |
| 1427 | WIN32_FIND_DATA find_data; |
| 1428 | |
| 1429 | i = GetCurrentDirectory( k - 2, cwd ); |
| 1430 | |
| 1431 | if( i >= k ) |
| 1432 | { |
| 1433 | CK_SAFE_DELETE_ARRAY( cwd ); |
| 1434 | cwd = new char [i + 2]; |
| 1435 | GetCurrentDirectory( i, cwd ); |
| 1436 | } |
| 1437 | |
| 1438 | i = strlen( cwd ); |
| 1439 | cwd[i] = '\\'; |
| 1440 | cwd[i + 1] = '*'; |
| 1441 | cwd[i + 2] = 0; |
| 1442 | |
| 1443 | HANDLE find_handle = FindFirstFile( cwd, &find_data ); |
| 1444 | |
| 1445 | out += string( find_data.cFileName ) + "\n"; |
| 1446 | |
| 1447 | while( FindNextFile( find_handle, &find_data ) ) |
| 1448 | out += string( find_data.cFileName ) + "\n"; |
| 1449 | |
| 1450 | FindClose( find_handle ); |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | t_CKINT i, len = argv.size(); |
| 1455 | t_CKBOOL print_parent_name = len > 1 ? TRUE : FALSE; |
| 1456 | |
| 1457 | for( i = 0; i < len; i++ ) |
| 1458 | { |
| 1459 | t_CKINT j = argv[i].size() + 3; |
| 1460 | WIN32_FIND_DATA find_data; |
| 1461 | LPTSTR dir = new char [j]; |
| 1462 | strncpy( dir, argv[i].c_str(), j ); |
| 1463 | dir[j - 3] = '\\'; |
| 1464 | dir[j - 2] = '*'; |
| 1465 | dir[j - 1] = 0; |
| 1466 | |
| 1467 | if( print_parent_name ) |
| 1468 | out += argv[i] + ":\n"; |
| 1469 | |
| 1470 | HANDLE find_handle = FindFirstFile( dir, &find_data ); |
| 1471 | |
| 1472 | out += string( find_data.cFileName ) + "\n"; |
| 1473 | |
| 1474 | while( FindNextFile( find_handle, &find_data ) ) |
| 1475 | out += string( find_data.cFileName ) + "\n"; |
| 1476 | |
| 1477 | if( print_parent_name ) |
| 1478 | out += "\n"; |
| 1479 | |
| 1480 | FindClose( find_handle ); |
| 1481 | } |
| 1482 | |
| 1483 | |
| 1484 | |
| 1485 | #endif // __PLATFORM_WINDOWS__ |
| 1486 | return 0; |
| 1487 | } |
| 1488 | |
| 1489 | |
| 1490 | |
| 1491 | |
| 1492 | //----------------------------------------------------------------------------- |
| 1493 | // name: execute() |
| 1494 | // desc: ... |
| 1495 | //----------------------------------------------------------------------------- |
| 1496 | t_CKINT Chuck_Shell::Command_Cd::execute( vector< string > & argv, |
| 1497 | string & out ) |
| 1498 | { |
| 1499 | #ifndef __PLATFORM_WINDOWS__ |
| 1500 | if( argv.size() < 1 ) |
| 1501 | { |
| 1502 | if( chdir( getenv( "HOME" ) ) ) |
| 1503 | out += "cd: error: command failed\n"; |
| 1504 | } |
| 1505 | |
| 1506 | else |
| 1507 | { |
| 1508 | if( chdir( argv[0].c_str() ) ) |
| 1509 | out += "cd: error: command failed\n"; |
| 1510 | } |
| 1511 | |
| 1512 | #else |
| 1513 | if( argv.size() < 1 ) |
| 1514 | out += "usage: " + usage() + "\n"; |
| 1515 | |
| 1516 | else |
| 1517 | { |
| 1518 | if( !SetCurrentDirectory( argv[0].c_str() ) ) |
| 1519 | out += "cd: error: command failed\n"; |
| 1520 | } |
| 1521 | |
| 1522 | #endif //__PLATFORM_WINDOWS__ |
| 1523 | return 0; |
| 1524 | } |
| 1525 | |
| 1526 | |
| 1527 | |
| 1528 | |
| 1529 | //----------------------------------------------------------------------------- |
| 1530 | // name: execute() |
| 1531 | // desc: ... |
| 1532 | //----------------------------------------------------------------------------- |
| 1533 | t_CKINT Chuck_Shell::Command_Pwd::execute( vector< string > & argv, |
| 1534 | string & out ) |
| 1535 | { |
| 1536 | #ifndef __PLATFORM_WINDOWS__ |
| 1537 | char * cwd = getcwd( NULL, 0 ); |
| 1538 | out += string( cwd ) + "\n"; |
| 1539 | free( cwd ); |
| 1540 | #else |
| 1541 | DWORD i, k = 256; |
| 1542 | LPTSTR cwd = new char [k]; |
| 1543 | i = GetCurrentDirectory( k, cwd ); |
| 1544 | |
| 1545 | if( i >= k ) |
| 1546 | { |
| 1547 | CK_SAFE_DELETE_ARRAY( cwd ); |
| 1548 | cwd = new char [i]; |
| 1549 | GetCurrentDirectory( i, cwd ); |
| 1550 | } |
| 1551 | |
| 1552 | out += string( cwd ) + "\n"; |
| 1553 | |
| 1554 | CK_SAFE_DELETE_ARRAY( cwd ); |
| 1555 | #endif |
| 1556 | |
| 1557 | if( argv.size() > 0 ) |
| 1558 | out += "pwd: warning: ignoring excess arguments...\n"; |
| 1559 | |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | |
| 1566 | //----------------------------------------------------------------------------- |
| 1567 | // name: execute() |
| 1568 | // desc: ... |
| 1569 | //----------------------------------------------------------------------------- |
| 1570 | t_CKINT Chuck_Shell::Command_Alias::execute( vector< string > & argv, |
| 1571 | string & out ) |
| 1572 | { |
| 1573 | vector<string>::size_type i, len = argv.size(); |
| 1574 | string::size_type j; |
| 1575 | string a, b; |
| 1576 | |
| 1577 | for( i = 0; i < len; i++ ) |
| 1578 | { |
| 1579 | j = argv[i].find( "=" ); |
| 1580 | |
| 1581 | // no alias assignment specified; just print the alias value if exists |
| 1582 | if( j == string::npos ) |
| 1583 | { |
| 1584 | // see if the alias exists in the map |
| 1585 | if( caller->aliases.find( argv[i] ) == caller->aliases.end() ) |
| 1586 | out += "alias: error: alias " + argv[i] + " not found\n"; |
| 1587 | else |
| 1588 | out += "alias " + argv[i] + "='" + |
| 1589 | caller->aliases[argv[i]] + "'\n"; |
| 1590 | } |
| 1591 | // create the alias |
| 1592 | else |
| 1593 | { |
| 1594 | a = string( argv[i], 0, j ); |
| 1595 | b = string( argv[i], j + 1, string::npos ); |
| 1596 | caller->aliases[a] = b; |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | if( len == 0 ) |
| 1601 | // no arguments specified; print the entire alias map |
| 1602 | { |
| 1603 | map< string, string>::iterator i = caller->aliases.begin(), |
| 1604 | end = caller->aliases.end(); |
| 1605 | for( ; i != end; i++ ) |
| 1606 | out += "alias " + i->first + "='" + i->second + "'\n"; |
| 1607 | } |
| 1608 | |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | |
| 1613 | |
| 1614 | |
| 1615 | //----------------------------------------------------------------------------- |
| 1616 | // name: execute() |
| 1617 | // desc: ... |
| 1618 | //----------------------------------------------------------------------------- |
| 1619 | t_CKINT Chuck_Shell::Command_Unalias::execute( vector< string > & argv, |
| 1620 | string & out ) |
| 1621 | { |
| 1622 | vector<string>::size_type i, len = argv.size(); |
| 1623 | |
| 1624 | for( i = 0; i < len; i++ ) |
| 1625 | { |
| 1626 | if( caller->aliases.find( argv[i] ) == caller->aliases.end() ) |
| 1627 | out += "error: alias " + argv[i] + " not found\n"; |
| 1628 | |
| 1629 | else |
| 1630 | caller->aliases.erase( argv[i] ); |
| 1631 | } |
| 1632 | |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | |
| 1637 | |
| 1638 | |
| 1639 | //----------------------------------------------------------------------------- |
| 1640 | // name: execute() |
| 1641 | // desc: ... |
| 1642 | //----------------------------------------------------------------------------- |
| 1643 | t_CKINT Chuck_Shell::Command_Source::execute( vector< string > & argv, |
| 1644 | string & out ) |
| 1645 | { |
| 1646 | vector<string>::size_type i, len = argv.size(); |
| 1647 | char line_buf[255]; |
| 1648 | string line, temp; |
| 1649 | |
| 1650 | for( i = 0; i < len; i++ ) |
| 1651 | { |
| 1652 | FILE * source_file = fopen( argv[i].c_str(), "r" ); |
| 1653 | |
| 1654 | if( source_file == NULL ) |
| 1655 | out += "error: unable to open file " + argv[i]; |
| 1656 | |
| 1657 | else |
| 1658 | { |
| 1659 | while( fgets( line_buf, 255, source_file ) != NULL ) |
| 1660 | { |
| 1661 | line = string( line_buf ); |
| 1662 | caller->execute( line, temp ); |
| 1663 | out += temp; |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | |
| 1672 | |
| 1673 | |
| 1674 | //----------------------------------------------------------------------------- |
| 1675 | // name: execute() |
| 1676 | // desc: ... |
| 1677 | //----------------------------------------------------------------------------- |
| 1678 | t_CKINT Chuck_Shell::Command_Help::execute( vector< string > & argv, |
| 1679 | string & out ) |
| 1680 | { |
| 1681 | out += ""; |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
| 1685 | |
| 1686 | |
| 1687 | |
| 1688 | //----------------------------------------------------------------------------- |
| 1689 | // name: init() |
| 1690 | // desc: ... |
| 1691 | //----------------------------------------------------------------------------- |
| 1692 | t_CKBOOL Chuck_Shell::Command_VM::init( Chuck_Shell * caller ) |
| 1693 | { |
| 1694 | Command * temp; |
| 1695 | |
| 1696 | Command::init( caller ); |
| 1697 | |
| 1698 | temp = new Command_VMAttach(); |
| 1699 | temp->init( caller ); |
| 1700 | commands["attach"] = temp; |
| 1701 | commands["@"] = temp; |
| 1702 | allocated_commands.push_back( temp ); |
| 1703 | |
| 1704 | temp = new Command_VMAdd(); |
| 1705 | temp->init( caller ); |
| 1706 | commands["add"] = temp; |
| 1707 | commands["+"] = temp; |
| 1708 | allocated_commands.push_back( temp ); |
| 1709 | |
| 1710 | temp = new Command_VMRemove(); |
| 1711 | temp->init( caller ); |
| 1712 | commands["remove"] = temp; |
| 1713 | commands["-"] = temp; |
| 1714 | allocated_commands.push_back( temp ); |
| 1715 | |
| 1716 | temp = new Command_VMList(); |
| 1717 | temp->init( caller ); |
| 1718 | commands["list"] = temp; |
| 1719 | allocated_commands.push_back( temp ); |
| 1720 | |
| 1721 | temp = new Command_VMSwap(); |
| 1722 | temp->init( caller ); |
| 1723 | commands["swap"] = temp; |
| 1724 | commands["<"] = temp; |
| 1725 | commands["<-"] = temp; |
| 1726 | allocated_commands.push_back( temp ); |
| 1727 | |
| 1728 | temp = new Command_VMAttachAdd(); |
| 1729 | temp->init( caller ); |
| 1730 | commands["@+"] = temp; |
| 1731 | allocated_commands.push_back( temp ); |
| 1732 | |
| 1733 | return TRUE; |
| 1734 | } |
| 1735 | |
| 1736 | |
| 1737 | |
| 1738 | |
| 1739 | //----------------------------------------------------------------------------- |
| 1740 | // name: ~Command_VM() |
| 1741 | // desc: ... |
| 1742 | //----------------------------------------------------------------------------- |
| 1743 | Chuck_Shell::Command_VM::~Command_VM() |
| 1744 | { |
| 1745 | vector<string>::size_type i, len = allocated_commands.size(); |
| 1746 | |
| 1747 | //iterate through commands, delete the associated heap data |
| 1748 | for( i = 0; i != len; i++ ) |
| 1749 | CK_SAFE_DELETE( allocated_commands[i] ); |
| 1750 | } |
| 1751 | |
| 1752 | |
| 1753 | |
| 1754 | |
| 1755 | //----------------------------------------------------------------------------- |
| 1756 | // name: execute() |
| 1757 | // desc: ... |
| 1758 | //----------------------------------------------------------------------------- |
| 1759 | t_CKINT Chuck_Shell::Command_VM::execute( vector< string > & argv, |
| 1760 | string & out ) |
| 1761 | { |
| 1762 | if( argv.size() < 1) |
| 1763 | out += "usage: " + usage() + "\n"; |
| 1764 | |
| 1765 | else if( commands.find( argv[0] ) == commands.end() ) |
| 1766 | // command doesn't exist |
| 1767 | out += "error: vm " + argv[0] + ": command not found\n"; |
| 1768 | |
| 1769 | else |
| 1770 | // call the mapped command |
| 1771 | { |
| 1772 | Command * command = commands[argv[0]]; |
| 1773 | argv.erase( argv.begin() ); |
| 1774 | command->execute( argv, out ); |
| 1775 | } |
| 1776 | |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| 1780 | |
| 1781 | |
| 1782 | |
| 1783 | //----------------------------------------------------------------------------- |
| 1784 | // name: usage() |
| 1785 | // desc: ... |
| 1786 | //----------------------------------------------------------------------------- |
| 1787 | string Chuck_Shell::Command_VM::usage() |
| 1788 | { |
| 1789 | return "vm [command] [args] ..."; |
| 1790 | } |
| 1791 | |
| 1792 | |
| 1793 | |
| 1794 | |
| 1795 | //----------------------------------------------------------------------------- |
| 1796 | // name: execute() |
| 1797 | // desc: ... |
| 1798 | //----------------------------------------------------------------------------- |
| 1799 | t_CKINT Chuck_Shell::Command_VMAttach::execute( vector < string > & argv, |
| 1800 | string & out ) |
| 1801 | { |
| 1802 | string hostname; |
| 1803 | t_CKINT port; |
| 1804 | t_CKINT result = 0; |
| 1805 | |
| 1806 | /* parse the hostname:port */ |
| 1807 | if( argv.size() == 0 ) |
| 1808 | /* no hostname:port specified, use default */ |
| 1809 | { |
| 1810 | argv.push_back( "localhost:8888" ); |
| 1811 | hostname = "localhost"; |
| 1812 | port = 8888; |
| 1813 | } |
| 1814 | |
| 1815 | else |
| 1816 | { |
| 1817 | string::size_type i = argv[0].find( ":" ); |
| 1818 | |
| 1819 | if( i == string::npos ) |
| 1820 | // couldn't find a port number; use the default |
| 1821 | { |
| 1822 | hostname = argv[0]; |
| 1823 | port = 8888; |
| 1824 | argv[0] += ":8888"; |
| 1825 | } |
| 1826 | |
| 1827 | else |
| 1828 | { |
| 1829 | port = strtol( argv[0].c_str() + i + 1, NULL, 10 ); |
| 1830 | if( port == 0 ) |
| 1831 | out += string( "error: invalid port '" ) + |
| 1832 | string( argv[0].c_str() + i + 1 ) + "'\n"; |
| 1833 | |
| 1834 | else |
| 1835 | hostname = string( argv[0], 0, i ); |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | if( port != 0 ) |
| 1840 | { |
| 1841 | //string temp; |
| 1842 | |
| 1843 | Chuck_Shell_Network_VM * new_vm = new Chuck_Shell_Network_VM(); |
| 1844 | new_vm->init( hostname, port ); |
| 1845 | |
| 1846 | /*if( !new_vm->status( temp ) ) |
| 1847 | { |
| 1848 | CK_SAFE_DELETE( new_vm ); |
| 1849 | result = -1; |
| 1850 | }*/ |
| 1851 | |
| 1852 | CK_SAFE_DELETE( caller->current_vm ); |
| 1853 | caller->current_vm = new_vm; |
| 1854 | out += argv[0] + " is now current VM\n"; |
| 1855 | result = 0; |
| 1856 | } |
| 1857 | |
| 1858 | else |
| 1859 | { |
| 1860 | out += "error: unable to attach to " + argv[0] + "\n"; |
| 1861 | result = -1; |
| 1862 | } |
| 1863 | |
| 1864 | if( argv.size() > 1 ) |
| 1865 | out += "warning: ignoring excess arguments...\n"; |
| 1866 | |
| 1867 | return result; |
| 1868 | } |
| 1869 | |
| 1870 | |
| 1871 | |
| 1872 | |
| 1873 | //----------------------------------------------------------------------------- |
| 1874 | // name: execute() |
| 1875 | // desc: ... |
| 1876 | //----------------------------------------------------------------------------- |
| 1877 | t_CKINT Chuck_Shell::Command_VMAdd::execute( vector< string > & argv, |
| 1878 | string & out ) |
| 1879 | { |
| 1880 | char buf[16]; |
| 1881 | |
| 1882 | if( caller->current_vm == NULL ) |
| 1883 | { |
| 1884 | out += "error: no VM to save\n"; |
| 1885 | return -1; |
| 1886 | } |
| 1887 | |
| 1888 | caller->vms.push_back( caller->current_vm->copy() ); |
| 1889 | |
| 1890 | #ifndef __PLATFORM_WINDOWS__ |
| 1891 | snprintf( buf, 16, "%lu", caller->vms.size() - 1 ); |
| 1892 | #else |
| 1893 | snprintf( buf, 16, "%lu", (long)caller->vms.size() - 1 ); |
| 1894 | #endif // __PLATFORM_WINDOWS__ |
| 1895 | |
| 1896 | out += caller->current_vm->fullname() + " saved as VM " + buf + "\n"; |
| 1897 | |
| 1898 | if( argv.size() > 0 ) |
| 1899 | out += "warning: ignoring excess arguments...\n"; |
| 1900 | |
| 1901 | return 0; |
| 1902 | } |
| 1903 | |
| 1904 | |
| 1905 | |
| 1906 | |
| 1907 | //----------------------------------------------------------------------------- |
| 1908 | // name: execute() |
| 1909 | // desc: ... |
| 1910 | //----------------------------------------------------------------------------- |
| 1911 | t_CKINT Chuck_Shell::Command_VMRemove::execute( vector< string > & argv, |
| 1912 | string & out ) |
| 1913 | { |
| 1914 | vector<string>::size_type i = 0, vm_no, len = argv.size(); |
| 1915 | |
| 1916 | for( ; i < len; i++ ) |
| 1917 | { |
| 1918 | vm_no = strtoul( argv[i].c_str(), NULL, 10 ); |
| 1919 | if( ( vm_no == 0 && errno == EINVAL ) || caller->vms.size() <= vm_no || |
| 1920 | caller->vms[vm_no] == NULL ) |
| 1921 | { |
| 1922 | out += "error: invalid VM id: " + argv[i] + "\n"; |
| 1923 | } |
| 1924 | else |
| 1925 | { |
| 1926 | CK_SAFE_DELETE( caller->vms[vm_no] ); |
| 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | |
| 1935 | |
| 1936 | //----------------------------------------------------------------------------- |
| 1937 | // name: execute() |
| 1938 | // desc: ... |
| 1939 | //----------------------------------------------------------------------------- |
| 1940 | t_CKINT Chuck_Shell::Command_VMSwap::execute( vector< string > & argv, |
| 1941 | string & out ) |
| 1942 | { |
| 1943 | vector<string>::size_type new_vm = 0; |
| 1944 | |
| 1945 | if( argv.size() < 1 ) |
| 1946 | { |
| 1947 | out += string( "error: too few arguments...\n" ); |
| 1948 | return -1; |
| 1949 | } |
| 1950 | |
| 1951 | new_vm = strtol( argv[0].c_str(), NULL, 10 ); |
| 1952 | if( new_vm >= caller->vms.size() || caller->vms[new_vm] == NULL ) |
| 1953 | { |
| 1954 | out += string( "error: invalid VM: " ) + argv[0] + "\n"; |
| 1955 | return -1; |
| 1956 | } |
| 1957 | |
| 1958 | CK_SAFE_DELETE( caller->current_vm ); |
| 1959 | caller->current_vm = caller->vms[new_vm]->copy(); |
| 1960 | out += "current VM is now " + caller->current_vm->fullname() + "\n"; |
| 1961 | |
| 1962 | if( argv.size() > 1 ) |
| 1963 | out += "warning: ignoring excess arguments...\n"; |
| 1964 | |
| 1965 | return 0; |
| 1966 | } |
| 1967 | |
| 1968 | |
| 1969 | |
| 1970 | |
| 1971 | //----------------------------------------------------------------------------- |
| 1972 | // name: execute() |
| 1973 | // desc: ... |
| 1974 | //----------------------------------------------------------------------------- |
| 1975 | t_CKINT Chuck_Shell::Command_VMList::execute( vector< string > & argv, |
| 1976 | string & out ) |
| 1977 | { |
| 1978 | // 1.5.0.1 (ge) changed from 16 to 32; |
| 1979 | // the former not long enough for big 64-bit numbers |
| 1980 | char buf[32]; |
| 1981 | vector<string>::size_type i, len = caller->vms.size(); |
| 1982 | |
| 1983 | if( caller->current_vm != NULL ) |
| 1984 | out += string("current VM: ") + caller->current_vm->fullname() + "\n"; |
| 1985 | |
| 1986 | for( i = 0; i < len; i++ ) |
| 1987 | { |
| 1988 | if( caller->vms[i] != NULL ) |
| 1989 | { |
| 1990 | #ifndef __PLATFORM_WINDOWS__ |
| 1991 | snprintf( buf, 32, "%lu", i ); |
| 1992 | #else |
| 1993 | snprintf( buf, 32, "%lu", (long)i ); |
| 1994 | #endif // __PLATFORM_WINDOWS__ |
| 1995 | out += string( "VM " ) + buf + ": " + |
| 1996 | caller->vms[i]->fullname() + "\n"; |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | if( argv.size() > 0 ) |
| 2001 | out += "warning: ignoring excess arguments...\n"; |
| 2002 | |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
| 2006 | |
| 2007 | |
| 2008 | |
| 2009 | //----------------------------------------------------------------------------- |
| 2010 | // name: execute() |
| 2011 | // desc: ... |
| 2012 | //----------------------------------------------------------------------------- |
| 2013 | t_CKINT Chuck_Shell::Command_VMAttachAdd::execute( vector< string > & argv, |
| 2014 | string & out ) |
| 2015 | { |
| 2016 | t_CKINT result = 0; |
| 2017 | string exec = string( "vm @ " ) + ( argv.size() > 0 ? argv[0] : "" ); |
| 2018 | |
| 2019 | if( caller->execute( exec, out ) ) |
| 2020 | { |
| 2021 | exec = "vm + "; |
| 2022 | if( caller->execute( exec, out ) ) |
| 2023 | result = 0; |
| 2024 | |
| 2025 | else |
| 2026 | result = -1; |
| 2027 | } |
| 2028 | |
| 2029 | else |
| 2030 | result = -1; |
| 2031 | |
| 2032 | return result; |
| 2033 | } |
| 2034 | |
| 2035 | |
| 2036 | |
| 2037 | |
| 2038 | //----------------------------------------------------------------------------- |
| 2039 | // name: init() |
| 2040 | // desc: ... |
| 2041 | //----------------------------------------------------------------------------- |
| 2042 | t_CKBOOL Chuck_Shell::Command_Code::init( Chuck_Shell * caller ) |
| 2043 | { |
| 2044 | Command * temp; |
| 2045 | |
| 2046 | Command::init( caller ); |
| 2047 | |
| 2048 | temp = new Command_CodeSave(); |
| 2049 | temp->init( caller ); |
| 2050 | commands["save"] = temp; |
| 2051 | allocated_commands.push_back( temp ); |
| 2052 | |
| 2053 | temp = new Command_CodeList(); |
| 2054 | temp->init( caller ); |
| 2055 | commands["list"] = temp; |
| 2056 | allocated_commands.push_back( temp ); |
| 2057 | |
| 2058 | temp = new Command_CodePrint(); |
| 2059 | temp->init( caller ); |
| 2060 | commands["print"] = temp; |
| 2061 | allocated_commands.push_back( temp ); |
| 2062 | |
| 2063 | temp = new Command_CodeDelete(); |
| 2064 | temp->init( caller ); |
| 2065 | commands["delete"] = temp; |
| 2066 | allocated_commands.push_back( temp ); |
| 2067 | |
| 2068 | temp = new Command_CodeWrite(); |
| 2069 | temp->init( caller ); |
| 2070 | commands["write"] = temp; |
| 2071 | allocated_commands.push_back( temp ); |
| 2072 | |
| 2073 | temp = new Command_CodeRead(); |
| 2074 | temp->init( caller ); |
| 2075 | commands["read"] = temp; |
| 2076 | allocated_commands.push_back( temp ); |
| 2077 | |
| 2078 | temp = new Command_CodeAdd(); |
| 2079 | temp->init( caller ); |
| 2080 | commands["add"] = temp; |
| 2081 | commands["+"] = temp; |
| 2082 | allocated_commands.push_back( temp ); |
| 2083 | |
| 2084 | return TRUE; |
| 2085 | } |
| 2086 | |
| 2087 | |
| 2088 | |
| 2089 | |
| 2090 | //----------------------------------------------------------------------------- |
| 2091 | // name: ~Command_Code() |
| 2092 | // desc: ... |
| 2093 | //----------------------------------------------------------------------------- |
| 2094 | Chuck_Shell::Command_Code::~Command_Code() |
| 2095 | { |
| 2096 | vector<string>::size_type i, len = allocated_commands.size(); |
| 2097 | |
| 2098 | //iterate through commands, delete the associated heap data |
| 2099 | for( i = 0; i != len; i++ ) |
| 2100 | CK_SAFE_DELETE( allocated_commands[i] ); |
| 2101 | } |
| 2102 | |
| 2103 | |
| 2104 | |
| 2105 | |
| 2106 | //----------------------------------------------------------------------------- |
| 2107 | // name: execute() |
| 2108 | // desc: ... |
| 2109 | //----------------------------------------------------------------------------- |
| 2110 | t_CKINT Chuck_Shell::Command_Code::execute( vector< string > & argv, |
| 2111 | string & out ) |
| 2112 | { |
| 2113 | if( argv.size() < 1) |
| 2114 | out += "usage: " + usage() + "\n"; |
| 2115 | |
| 2116 | else if( commands.find( argv[0] ) == commands.end() ) |
| 2117 | // command doesn't exist |
| 2118 | out += "error: code " + argv[0] + ": command not found\n"; |
| 2119 | |
| 2120 | else |
| 2121 | { |
| 2122 | // call the mapped command |
| 2123 | Command * command = commands[argv[0]]; |
| 2124 | argv.erase( argv.begin() ); |
| 2125 | command->execute( argv, out ); |
| 2126 | } |
| 2127 | |
| 2128 | return 0; |
| 2129 | } |
| 2130 | |
| 2131 | |
| 2132 | |
| 2133 | |
| 2134 | //----------------------------------------------------------------------------- |
| 2135 | // name: usage() |
| 2136 | // desc: ... |
| 2137 | //----------------------------------------------------------------------------- |
| 2138 | string Chuck_Shell::Command_Code::usage() |
| 2139 | { |
| 2140 | return "code [command] [args] ..."; |
| 2141 | } |
| 2142 | |
| 2143 | |
| 2144 | |
| 2145 | |
| 2146 | //----------------------------------------------------------------------------- |
| 2147 | // name: execute() |
| 2148 | // desc: ... |
| 2149 | //----------------------------------------------------------------------------- |
| 2150 | t_CKINT Chuck_Shell::Command_CodeSave::execute( vector < string > & argv, |
| 2151 | string & out ) |
| 2152 | { |
| 2153 | if( argv.size() < 1 ) |
| 2154 | { |
| 2155 | out += "error: please specify a name to identify the code\n"; |
| 2156 | } |
| 2157 | else |
| 2158 | { |
| 2159 | if( caller->code == "" ) |
| 2160 | out += "error: no code to save\n"; |
| 2161 | |
| 2162 | else |
| 2163 | caller->saved_code[argv[0]] = caller->code; |
| 2164 | |
| 2165 | if( argv.size() > 1 ) |
| 2166 | out += "warning: ignoring excess arguments...\n"; |
| 2167 | } |
| 2168 | |
| 2169 | return 0; |
| 2170 | } |
| 2171 | |
| 2172 | |
| 2173 | |
| 2174 | |
| 2175 | //----------------------------------------------------------------------------- |
| 2176 | // name: execute() |
| 2177 | // desc: ... |
| 2178 | //----------------------------------------------------------------------------- |
| 2179 | t_CKINT Chuck_Shell::Command_CodeList::execute( vector < string > & argv, |
| 2180 | string & out ) |
| 2181 | { |
| 2182 | map< string, string >::iterator i = caller->saved_code.begin(), |
| 2183 | end = caller->saved_code.end(); |
| 2184 | for( ; i != end; i++ ) |
| 2185 | out += i->first + "\n"; |
| 2186 | |
| 2187 | if( argv.size() > 0 ) |
| 2188 | out += "warning: ignoring excess arguments...\n"; |
| 2189 | |
| 2190 | return 0; |
| 2191 | } |
| 2192 | |
| 2193 | |
| 2194 | |
| 2195 | |
| 2196 | //----------------------------------------------------------------------------- |
| 2197 | // name: execute() |
| 2198 | // desc: ... |
| 2199 | //----------------------------------------------------------------------------- |
| 2200 | t_CKINT Chuck_Shell::Command_CodeAdd::execute( vector < string > & argv, |
| 2201 | string & out ) |
| 2202 | { |
| 2203 | if( argv.size() == 0 ) |
| 2204 | { |
| 2205 | if( caller->code != "" ) |
| 2206 | caller->do_code( caller->code, out ); |
| 2207 | |
| 2208 | else |
| 2209 | out += "error: no code to add\n"; |
| 2210 | } |
| 2211 | |
| 2212 | else |
| 2213 | { |
| 2214 | vector<string>::size_type i, len; |
| 2215 | for( i = 0, len = argv.size(); i < len; i++ ) |
| 2216 | { |
| 2217 | // iterate through all of the arguments, add the saved codes |
| 2218 | if( caller->saved_code.find( argv[i] ) == |
| 2219 | caller->saved_code.end() ) |
| 2220 | out += "error: code " + argv[i] + " not found\n"; |
| 2221 | |
| 2222 | else |
| 2223 | caller->do_code( caller->code, out ); |
| 2224 | } |
| 2225 | } |
| 2226 | |
| 2227 | return 0; |
| 2228 | } |
| 2229 | |
| 2230 | |
| 2231 | |
| 2232 | |
| 2233 | //----------------------------------------------------------------------------- |
| 2234 | // name: execute() |
| 2235 | // desc: ... |
| 2236 | //----------------------------------------------------------------------------- |
| 2237 | t_CKINT Chuck_Shell::Command_CodePrint::execute( vector < string > & argv, |
| 2238 | string & out ) |
| 2239 | { |
| 2240 | if( argv.size() == 0 ) |
| 2241 | { |
| 2242 | if( caller->code != "" ) |
| 2243 | out += caller->code + "\n"; |
| 2244 | else |
| 2245 | out += "error: no code to print\n"; |
| 2246 | } |
| 2247 | |
| 2248 | else |
| 2249 | { |
| 2250 | vector<string>::size_type i, len; |
| 2251 | for( i = 0, len = argv.size(); i < len; i++ ) |
| 2252 | { |
| 2253 | if( caller->saved_code.find( argv[i] ) == |
| 2254 | caller->saved_code.end() ) |
| 2255 | { |
| 2256 | out += "error: code " + argv[i] + " not found\n"; |
| 2257 | } |
| 2258 | else |
| 2259 | { |
| 2260 | string code = caller->saved_code[argv[i]]; |
| 2261 | |
| 2262 | out += argv[i] + ":\n"; |
| 2263 | out += code + "\n"; |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | return 0; |
| 2269 | } |
| 2270 | |
| 2271 | |
| 2272 | |
| 2273 | |
| 2274 | //----------------------------------------------------------------------------- |
| 2275 | // name: execute() |
| 2276 | // desc: ... |
| 2277 | //----------------------------------------------------------------------------- |
| 2278 | t_CKINT Chuck_Shell::Command_CodeDelete::execute( vector < string > & argv, |
| 2279 | string & out ) |
| 2280 | { |
| 2281 | if( argv.size() == 0 ) |
| 2282 | { |
| 2283 | out += "error: specify the code to delete\n"; |
| 2284 | } |
| 2285 | else |
| 2286 | { |
| 2287 | vector<string>::size_type i, len; |
| 2288 | for( i = 0, len = argv.size(); i < len; i++ ) |
| 2289 | { |
| 2290 | if( caller->saved_code.find( argv[i] ) == |
| 2291 | caller->saved_code.end() ) |
| 2292 | out += "error: code " + argv[i] + " not found\n"; |
| 2293 | |
| 2294 | else |
| 2295 | { |
| 2296 | caller->saved_code.erase( argv[i] ); |
| 2297 | } |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | return 0; |
| 2302 | } |
| 2303 | |
| 2304 | |
| 2305 | |
| 2306 | |
| 2307 | //----------------------------------------------------------------------------- |
| 2308 | // name: execute() |
| 2309 | // desc: ... |
| 2310 | //----------------------------------------------------------------------------- |
| 2311 | t_CKINT Chuck_Shell::Command_CodeWrite::execute( vector < string > & argv, |
| 2312 | string & out ) |
| 2313 | { |
| 2314 | if( argv.size() < 1 ) |
| 2315 | { |
| 2316 | out += "error: insufficient arguments...\n"; |
| 2317 | } |
| 2318 | else |
| 2319 | { |
| 2320 | if( caller->saved_code.find( argv[0] ) == caller->saved_code.end() ) |
| 2321 | { |
| 2322 | // this code doesnt exist! |
| 2323 | out += "error: code " + argv[0] + " not found\n"; |
| 2324 | } |
| 2325 | else |
| 2326 | { |
| 2327 | string filename; |
| 2328 | string code = caller->saved_code[argv[0]]; |
| 2329 | FILE * write_file; |
| 2330 | |
| 2331 | if( argv.size() >= 2 ) |
| 2332 | { |
| 2333 | // use second argument as filename |
| 2334 | filename = argv[1]; |
| 2335 | } |
| 2336 | else |
| 2337 | { |
| 2338 | // append .ck to the code name to get the file name |
| 2339 | filename = argv[0] + ".ck"; |
| 2340 | } |
| 2341 | |
| 2342 | // open the file |
| 2343 | write_file = fopen( filename.c_str(), "w" ); |
| 2344 | |
| 2345 | if( write_file == NULL ) |
| 2346 | { |
| 2347 | out += "error: unable to open " + filename + " for writing\n"; |
| 2348 | } |
| 2349 | else |
| 2350 | { |
| 2351 | // write code to the file |
| 2352 | if( fprintf( write_file, "%s", code.c_str() ) < 0 ) |
| 2353 | out += "error: unable to write to " + filename + "\n"; |
| 2354 | |
| 2355 | fclose( write_file ); |
| 2356 | } |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | if( argv.size() > 2 ) |
| 2361 | out += "warning: ignoring excess arguments...\n"; |
| 2362 | |
| 2363 | return 0; |
| 2364 | } |
| 2365 | |
| 2366 | |
| 2367 | |
| 2368 | |
| 2369 | //----------------------------------------------------------------------------- |
| 2370 | // name: execute() |
| 2371 | // desc: ... |
| 2372 | //----------------------------------------------------------------------------- |
| 2373 | t_CKINT Chuck_Shell::Command_CodeRead::execute( vector < string > & argv, |
| 2374 | string & out ) |
| 2375 | { |
| 2376 | if( argv.size() < 1 ) |
| 2377 | { |
| 2378 | out += "error: insufficient arguments...\n"; |
| 2379 | } |
| 2380 | else |
| 2381 | { |
| 2382 | string code_name; |
| 2383 | FILE * read_file; |
| 2384 | |
| 2385 | if( argv.size() >= 2 ) |
| 2386 | { |
| 2387 | // use second argument as the code_name |
| 2388 | code_name = argv[1]; |
| 2389 | } |
| 2390 | else |
| 2391 | { |
| 2392 | // remove the file extension to get the code_name |
| 2393 | // or use the whole filename if there is no extension |
| 2394 | string::size_type k = argv[0].rfind( "." ); |
| 2395 | |
| 2396 | if( k == string::npos ) |
| 2397 | code_name = argv[1]; |
| 2398 | else |
| 2399 | code_name = string( argv[0], 0, k ); |
| 2400 | } |
| 2401 | |
| 2402 | // open the file |
| 2403 | read_file = fopen( argv[0].c_str(), "r" ); |
| 2404 | if( read_file == NULL ) |
| 2405 | out += "error: unable to open " + argv[0] + " for reading\n"; |
| 2406 | |
| 2407 | else |
| 2408 | { |
| 2409 | string code = ""; |
| 2410 | char buffer[256], * result = fgets( buffer, 256, read_file ); |
| 2411 | |
| 2412 | if( result == NULL ) |
| 2413 | out += "error: unable to read " + argv[0] + |
| 2414 | ", or file is empty\n"; |
| 2415 | |
| 2416 | else |
| 2417 | { |
| 2418 | while( result != NULL ) |
| 2419 | { |
| 2420 | code += buffer; |
| 2421 | result = fgets( buffer, 256, read_file ); |
| 2422 | } |
| 2423 | |
| 2424 | caller->saved_code[code_name] = code; |
| 2425 | } |
| 2426 | |
| 2427 | fclose( read_file ); |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | if( argv.size() > 2 ) |
| 2432 | out += "warning: ignoring excess arguments...\n"; |
| 2433 | |
| 2434 | return 0; |
| 2435 | } |