| 1 | /* |
| 2 | This file is part of the GVars3 Library. |
| 3 | |
| 4 | Copyright (C) 2005 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | |
| 9 | #include "gvars3/GUI.h" |
| 10 | #include "src/GUI_impl.h" |
| 11 | #include "gvars3/GStringUtil.h" |
| 12 | |
| 13 | #include <cctype> |
| 14 | #include <sstream> |
| 15 | #include <fstream> |
| 16 | #include <stdlib.h> |
| 17 | #include <algorithm> |
| 18 | |
| 19 | |
| 20 | |
| 21 | using namespace std; |
| 22 | namespace GVars3 |
| 23 | { |
| 24 | GUI_language* get_new_lang(); |
| 25 | void remove_lang(GUI_language* l); |
| 26 | |
| 27 | |
| 28 | GUI::GUI(GVars2* /* v2 */){ |
| 29 | } |
| 30 | |
| 31 | GUI_impl& GUI::I() |
| 32 | { |
| 33 | return GUI_impl_singleton<>::instance(); |
| 34 | } |
| 35 | |
| 36 | GUI::GUI() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void GUI::RegisterCommand(string cmd, GUICallbackProc c, void* p) |
| 41 | { |
| 42 | I().RegisterCommand(cmd, c, p); |
| 43 | } |
| 44 | void GUI::RegisterCommand(std::string cmd, std::function<void(string,string)> f) |
| 45 | { |
| 46 | I().RegisterCommand(cmd, f); |
| 47 | } |
| 48 | |
| 49 | void GUI::UnRegisterAllCommands(void* p) |
| 50 | { |
| 51 | I().UnRegisterAllCommands(p); |
| 52 | } |
| 53 | |
| 54 | void GUI::UnRegisterCommand(std::string sCommandName, void* thisptr) |
| 55 | { |
| 56 | I().UnRegisterCommand(sCommandName, thisptr); |
| 57 | } |
| 58 | |
| 59 | void GUI::UnRegisterCommand(std::string sCommandName) |
| 60 | { |
| 61 | I().UnRegisterCommand(sCommandName); |
| 62 | } |
| 63 | |
| 64 | void GUI::ParseLine(std::string s, bool bSilentFailure) |
| 65 | { |
| 66 | I().ParseLine(s, bSilentFailure); |
| 67 | } |
| 68 | |
| 69 | void GUI::ParseStream(std::istream& is) |
| 70 | { |
| 71 | I().ParseStream(is); |
| 72 | } |
| 73 | |
| 74 | void GUI::LoadFile(std::string sFileName) |
| 75 | { |
| 76 | I().LoadFile(sFileName); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | bool GUI::CallCallbacks(std::string sCommand, std::string sParams) |
| 81 | { |
| 82 | return I().CallCallbacks(sCommand, sParams); |
| 83 | } |
| 84 | |
| 85 | void GUI::SetupReadlineCompletion() |
| 86 | { |
| 87 | I().SetupReadlineCompletion(); |
| 88 | } |
| 89 | |
| 90 | void GUI::StartParserThread() |
| 91 | { |
| 92 | I().StartParserThread(); |
| 93 | } |
| 94 | |
| 95 | void GUI::StopParserThread() |
| 96 | { |
| 97 | I(); |
| 98 | GUI_impl::StopParserThread(); |
| 99 | } |
| 100 | |
| 101 | int GUI::parseArguments( const int argc, char * argv[], int start, const std::string prefix, const std::string execKeyword) |
| 102 | { |
| 103 | return I().parseArguments(argc, argv, start, prefix, execKeyword); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | bool setvar(string s) |
| 112 | { |
| 113 | //Execution failed. Maybe its an assignment. |
| 114 | string::size_type n; |
| 115 | n=s.find("="); |
| 116 | |
| 117 | if(n != string::npos) |
| 118 | { |
| 119 | string var = s.substr(0, n); |
| 120 | string val = s.substr(n+1); |
| 121 | |
| 122 | //Strip whitespace from around var; |
| 123 | string::size_type s=0, e = var.length()-1; |
| 124 | for(; isspace(var[s]) && s < var.length(); s++) |
| 125 | {} |
| 126 | if(s==var.length()) // All whitespace before the `='? |
| 127 | return false; |
| 128 | for(; isspace(var[e]); e--) |
| 129 | {} |
| 130 | if(e >= s) |
| 131 | { |
| 132 | var = var.substr(s, e-s+1); |
| 133 | |
| 134 | //Strip whitespace from around val; |
| 135 | s = 0, e = val.length() - 1; |
| 136 | for(; isspace(val[s]) && s < val.length(); s++) |
| 137 | {} |
| 138 | if( s < val.length()) |
| 139 | { |
| 140 | for(; isspace(val[e]); e--) |
| 141 | {} |
| 142 | val = val.substr(s, e-s+1); |
| 143 | } |
| 144 | else val = ""; |
| 145 | |
| 146 | GV3::set_var(var, val); |
| 147 | return true; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | |
| 156 | GUI_impl *GUI_impl::mpReadlineCompleterGUI=NULL; |
| 157 | |
| 158 | |
| 159 | void GUI_impl::UnRegisterCommand(string sCommandName) |
| 160 | { |
| 161 | mmCallBackMap.erase(sCommandName); |
| 162 | }; |
| 163 | |
| 164 | void GUI_impl::UnRegisterAllCommands(void* thisptr) |
| 165 | { |
| 166 | for(map<string, CallbackVector>::iterator i=mmCallBackMap.begin(); i!=mmCallBackMap.end(); i++) |
| 167 | UnRegisterCommand(i->first, thisptr); |
| 168 | }; |
| 169 | |
| 170 | void GUI_impl::UnRegisterCommand(string sCommandName, void* thisptr) |
| 171 | { |
| 172 | CallbackVector &cbv = mmCallBackMap[sCommandName]; |
| 173 | for(int i = static_cast<int>(cbv.size()) - 1; i>=0; i--) |
| 174 | if(cbv[i].thisptr == thisptr) |
| 175 | cbv.erase(cbv.begin() + i); |
| 176 | }; |
| 177 | |
| 178 | void GUI_impl::RegisterCommand(string sCommandName, std::function<void(string, string)> func) |
| 179 | { |
| 180 | callback_functions.push_back(func); |
| 181 | |
| 182 | RegisterCommand(sCommandName, [](void* ptr, std::string cmd, std::string params){ |
| 183 | (*static_cast<std::function<void(string, string)>*>(ptr))(cmd, params); |
| 184 | }, &callback_functions.back()); |
| 185 | } |
| 186 | |
| 187 | void GUI_impl::RegisterCommand(string sCommandName, GUICallbackProc callback, void* thisptr) |
| 188 | { |
| 189 | if(builtins.count(sCommandName)) |
| 190 | { |
| 191 | cerr << "!!GUI_impl::RegisterCommand: Tried to register reserved keyword " << sCommandName << "." << endl; |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | CallbackInfoStruct s; |
| 196 | s.cbp = callback; |
| 197 | s.thisptr = thisptr; |
| 198 | |
| 199 | bool bAlreadyThere = false; |
| 200 | CallbackVector *cbv = &mmCallBackMap[sCommandName]; |
| 201 | |
| 202 | |
| 203 | for(CallbackVector::iterator i = cbv->begin(); i<cbv->end();i++) |
| 204 | if( (i->cbp ==s.cbp) && (i->thisptr == s.thisptr)) |
| 205 | bAlreadyThere=true; |
| 206 | |
| 207 | if(!bAlreadyThere) |
| 208 | cbv->push_back(s); |
| 209 | }; |
| 210 | |
| 211 | |
| 212 | // Returns true if something was called; |
| 213 | // otherwise returns false |
| 214 | bool GUI_impl::CallCallbacks(string sCommand, string sParams) |
| 215 | { |
| 216 | if(mmCallBackMap.count(sCommand)==0) |
| 217 | return false; |
| 218 | |
| 219 | //Make a copy of this callback vector, since the command might call Unregister. |
| 220 | CallbackVector cbv = mmCallBackMap[sCommand]; |
| 221 | if(cbv.size() == 0) |
| 222 | return false; |
| 223 | |
| 224 | for(CallbackVector::iterator i = cbv.begin(); i<cbv.end();i++) |
| 225 | i->cbp(i->thisptr, sCommand, sParams); |
| 226 | |
| 227 | return true; |
| 228 | }; |
| 229 | |
| 230 | |
| 231 | void GUI_impl::ParseStream(istream& is) |
| 232 | { |
| 233 | string buffer; |
| 234 | while (getline(is, buffer)) { |
| 235 | // Lines ending with '\' are taken as continuing on the next line. |
| 236 | while(!buffer.empty() && buffer[buffer.length() - 1] == '\\') { |
| 237 | string buffer2; |
| 238 | if (! getline(is, buffer2)) |
| 239 | break; |
| 240 | buffer = buffer.substr(0, buffer.length() - 1) + buffer2; |
| 241 | } |
| 242 | ParseLine(buffer); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void GUI_impl::LoadFile(string sFileName) |
| 247 | { |
| 248 | ifstream ifs; |
| 249 | |
| 250 | vector<string> v = ChopAndUnquoteString(sFileName); |
| 251 | |
| 252 | if(v.size()<1) |
| 253 | return; |
| 254 | |
| 255 | ifs.open(v[0].c_str()); |
| 256 | |
| 257 | if(!ifs.good()) |
| 258 | { |
| 259 | cerr << "! GUI_impl::Loadfile: Failed to load script file \"" << sFileName << "\"."<< endl; |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | ParseStream(ifs); |
| 264 | ifs.close(); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | string::size_type FindCloseBrace(const string& s, string::size_type start, char op, char cl) |
| 272 | { |
| 273 | string::size_type open=1; |
| 274 | string::size_type i; |
| 275 | |
| 276 | for(i=start+1; i < s.size() ; i++) |
| 277 | { |
| 278 | if(s[i] == op) |
| 279 | open++; |
| 280 | else if(s[i] == cl) |
| 281 | open--; |
| 282 | |
| 283 | if(open == 0) |
| 284 | break; |
| 285 | } |
| 286 | |
| 287 | if(i == s.size()) |
| 288 | i = s.npos; |
| 289 | return i; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | void GUI_impl::ParseLine(string s, bool bSilentFailure ) |
| 294 | { |
| 295 | s = UncommentString(s); |
| 296 | if(s == "") |
| 297 | return; |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | // New for 2004! brace expansion! any line with {gvarname} in it |
| 304 | // will have the brace bit replaced with the contents of that gvar, |
| 305 | // one line per word in the gvar value... |
| 306 | // e.g. |
| 307 | // > TrackerList = Tracker FireTracker |
| 308 | // > {TrackerList}.ReloadModel |
| 309 | // would be equivalent to writing |
| 310 | // > Tracker.ReloadModel |
| 311 | // > FireTracker.ReloadModel |
| 312 | // Can use double braces so that brace expansion only occurs later; |
| 313 | // e.g. GUI_Motif.AddPushButton DoStuffToTrackerList {{TrackerList}}.do_stuff |
| 314 | // will create a single button which then exectues {TrackerList}.do_stuff |
| 315 | // (Without double braces, the it would try to make two buttons!) |
| 316 | |
| 317 | { // Brace expansion wrapper follows: |
| 318 | string::size_type nOpenBracePos = s.find("{"); |
| 319 | //int nCloseBracePos = s.rfind("}"); |
| 320 | string::size_type nCloseBracePos = FindCloseBrace(s, nOpenBracePos, '{', '}'); |
| 321 | if( (nOpenBracePos !=s.npos) && |
| 322 | (nCloseBracePos !=s.npos) && |
| 323 | (nCloseBracePos > nOpenBracePos)) |
| 324 | { // Brace Pair found!! |
| 325 | |
| 326 | string sBegin = s.substr(0,nOpenBracePos); |
| 327 | string sVarName = s.substr(nOpenBracePos+1,nCloseBracePos-nOpenBracePos-1); |
| 328 | string sEnd = s.substr(nCloseBracePos+1); |
| 329 | |
| 330 | string::size_type nLength = sVarName.size(); // Check if it's a double brace: {{foo}} in which case remove one pair, but don't expand. |
| 331 | bool bIsDoubleQuoted = false; |
| 332 | |
| 333 | if(nLength>2) |
| 334 | if((sVarName[0]=='{') && (sVarName[nLength-1]=='}')) |
| 335 | bIsDoubleQuoted = true; |
| 336 | |
| 337 | if(!bIsDoubleQuoted) |
| 338 | { |
| 339 | //vector<string> vsExpandedList = ChopAndUnquoteString(mpGV2->StringValue(sVarName, true)); |
| 340 | vector<string> vsExpandedList = ChopAndUnquoteString(GV3::get_var(sVarName)); |
| 341 | |
| 342 | //ER: look at the end of the string for '$'s to also perform the substitution |
| 343 | vector<string> chopped; |
| 344 | string end; |
| 345 | string::size_type dollarpos=s.npos, lastpos=0; |
| 346 | while((dollarpos = sEnd.find("$", dollarpos+1)) != s.npos) |
| 347 | { |
| 348 | chopped.push_back(sEnd.substr(lastpos, dollarpos - lastpos)); |
| 349 | lastpos = dollarpos+1; |
| 350 | } |
| 351 | end = sEnd.substr(lastpos); |
| 352 | |
| 353 | for(vector<string>::iterator i = vsExpandedList.begin(); i!=vsExpandedList.end();i++) |
| 354 | { |
| 355 | string line = sBegin + *i; |
| 356 | for(vector<string>::iterator s=chopped.begin(); s != chopped.end(); s++) |
| 357 | line += *s + *i; |
| 358 | line += end; |
| 359 | |
| 360 | ParseLine(line, bSilentFailure); |
| 361 | } |
| 362 | return; |
| 363 | }; |
| 364 | |
| 365 | // if it was double quoted, just parse it as normal, but replace s with the new, single-braced thingy. |
| 366 | s = sBegin + sVarName + sEnd; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Newer for 2004: Round brace expansion |
| 371 | // Expands gvar in round braces to the value |
| 372 | // e.g. A = 2 |
| 373 | // B = (A) |
| 374 | // assigns 2 to B |
| 375 | // And as before, use double brace to protect. |
| 376 | |
| 377 | { // Round expansion wrapper follows: |
| 378 | |
| 379 | string::size_type nOpenBracePos = s.find("("); |
| 380 | //int nCloseBracePos = s.rfind(")"); |
| 381 | string::size_type nCloseBracePos = FindCloseBrace(s, nOpenBracePos, '(', ')'); |
| 382 | |
| 383 | // cerr << "((( " << nOpenBracePos << " " << nCloseBracePos << endl; |
| 384 | |
| 385 | |
| 386 | if((nOpenBracePos !=s.npos) && (nCloseBracePos !=s.npos) && (nCloseBracePos > nOpenBracePos)) |
| 387 | { // Brace Pair found!! |
| 388 | //cerr << "Found (\n"; |
| 389 | //cout << "Found brace pair. " << endl; |
| 390 | string sBegin = s.substr(0,nOpenBracePos); |
| 391 | string sVarName = s.substr(nOpenBracePos+1,nCloseBracePos-nOpenBracePos-1); |
| 392 | string sEnd = s.substr(nCloseBracePos+1); |
| 393 | |
| 394 | //cerr << "varname = --" << sVarName << "--\n"; |
| 395 | |
| 396 | string::size_type nLength = sVarName.size(); // Check if it's a double brace: {{foo}} in which case remove one pair, but don't expand. |
| 397 | bool bIsDoubleQuoted = false; |
| 398 | |
| 399 | if(nLength>2) |
| 400 | if((sVarName[0]=='(') && (sVarName[nLength-1]==')')) |
| 401 | { |
| 402 | s = sBegin + sVarName + sEnd; // Just remove the first set of braces. |
| 403 | bIsDoubleQuoted = true; |
| 404 | }; |
| 405 | |
| 406 | if(!bIsDoubleQuoted) |
| 407 | { |
| 408 | //cerr << "varname = --" << sVarName << "--\n"; |
| 409 | //cerr << "***************" << GV3::get_var(sVarName) << endl; |
| 410 | //string sExpanded = mpGV2->StringValue(sVarName, true); |
| 411 | string sExpanded = GV3::get_var(sVarName); |
| 412 | s = sBegin + sExpanded + sEnd; |
| 413 | //cout << "DEBUG : xx" << s << "xx" << endl; |
| 414 | }; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | |
| 419 | |
| 420 | // Old ParseLine code follows, here no braces can be left (unless in arg.) |
| 421 | istringstream ist(s); |
| 422 | |
| 423 | string sCommand; |
| 424 | string sParams; |
| 425 | |
| 426 | //Get the first token (the command name) |
| 427 | ist >> sCommand; |
| 428 | if(sCommand == "") |
| 429 | return; |
| 430 | |
| 431 | //Get everything else (the arguments)... |
| 432 | |
| 433 | //Remove any whitespace |
| 434 | ist >> ws; |
| 435 | getline(ist, sParams); |
| 436 | |
| 437 | //Attempt to execute command |
| 438 | if (CallCallbacks(sCommand, sParams)) |
| 439 | return; |
| 440 | |
| 441 | if(setvar(s)) |
| 442 | return; |
| 443 | |
| 444 | if(!bSilentFailure) |
| 445 | cerr << "? GUI_impl::ParseLine: Unknown command \"" << sCommand << "\" or invalid assignment." << endl; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | |
| 450 | //////////////////////////////////////////////////////////////////////////////// |
| 451 | // |
| 452 | // Builtin commands |
| 453 | |
| 454 | void builtin_shell(void* /* ptr */, string /* sCommand */, string sParams) |
| 455 | { |
| 456 | vector<string> v = ChopAndUnquoteString(sParams); |
| 457 | |
| 458 | if(v.size()==0) |
| 459 | { |
| 460 | cerr <<"? GUI_impl internal shell command: No prog/args given."<< endl; |
| 461 | return; |
| 462 | } |
| 463 | |
| 464 | system(sParams.c_str()); |
| 465 | } |
| 466 | |
| 467 | void builtin_ls(void* ptr, string /*sCommand*/, string sParams) |
| 468 | { |
| 469 | GUI_impl* p = (GUI_impl*)ptr; |
| 470 | p->ParseLine("shell ls " + sParams); |
| 471 | } |
| 472 | |
| 473 | void builtin_ll(void* ptr, string /*sCommand*/, string sParams) |
| 474 | { |
| 475 | GUI_impl* p = (GUI_impl*)ptr; |
| 476 | p->ParseLine("shell ls -l " + sParams); |
| 477 | } |
| 478 | |
| 479 | void builtin_try(void* ptr, string /*sCommand*/, string sParams) |
| 480 | { |
| 481 | GUI_impl* p = (GUI_impl*)ptr; |
| 482 | p->ParseLine(sParams, true); |
| 483 | } |
| 484 | |
| 485 | void builtin_exec(void* ptr, string /*sCommand*/, string sParams) |
| 486 | { |
| 487 | GUI_impl* p = (GUI_impl*)ptr; |
| 488 | p->LoadFile(sParams); |
| 489 | } |
| 490 | |
| 491 | void builtin_echo(void* /*ptr*/, string /*sCommand*/, string sParams) |
| 492 | { |
| 493 | cout << sParams << endl; |
| 494 | } |
| 495 | |
| 496 | void builtin_qmark(void* /*ptr*/, string /*sCommand*/, string /*sParams*/) |
| 497 | { |
| 498 | cout << " Perhaps you'd like to type \"commandlist\" or \"gvarlist\"." << endl; |
| 499 | } |
| 500 | |
| 501 | void builtin_if(void* ptr, string /*sCommand*/, string sParams) |
| 502 | { |
| 503 | GUI_impl* p = (GUI_impl*)ptr; |
| 504 | vector<string> v = ChopAndUnquoteString(sParams); |
| 505 | if(v.size()<2) |
| 506 | { |
| 507 | cerr <<"? GUI_impl internal if command: not enough params (syntax: if gvar command)"<< endl; |
| 508 | return; |
| 509 | } |
| 510 | |
| 511 | string sValue = GV3::get_var(v[0]); |
| 512 | if(sValue == "(not in GVar list)") |
| 513 | return; |
| 514 | |
| 515 | // Have to do this to fix whitespace issues on non-registered gvars |
| 516 | vector<string> vv = ChopAndUnquoteString(sValue); |
| 517 | |
| 518 | if(vv.size()<1) |
| 519 | return; |
| 520 | else if(vv[0]!="0") |
| 521 | { |
| 522 | string s; |
| 523 | s = ""; |
| 524 | for(size_t i=1;i<v.size();i++) |
| 525 | s = s + " " + v[i]; |
| 526 | p->ParseLine(s); |
| 527 | } |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | void builtin_ifnot(void* ptr, string /*sCommand*/, string sParams) |
| 532 | { |
| 533 | GUI_impl* p = (GUI_impl*)ptr; |
| 534 | vector<string> v = ChopAndUnquoteString(sParams); |
| 535 | if(v.size()<2) |
| 536 | { |
| 537 | cerr <<"? GUI_impl internal ifnot command: not enough params (syntax: if gvar command)"<< endl; |
| 538 | return; |
| 539 | } |
| 540 | |
| 541 | string sValue = GV3::get_var(v[0]); |
| 542 | if(sValue == "(not in GVar list)") |
| 543 | return; |
| 544 | |
| 545 | // Have to do this to fix whitespace issues on non-registered gvars |
| 546 | vector<string> vv = ChopAndUnquoteString(sValue); |
| 547 | |
| 548 | if(vv.size()<1) |
| 549 | return; |
| 550 | else if(vv[0]=="0") |
| 551 | { |
| 552 | string s; |
| 553 | s = ""; |
| 554 | for(size_t i=1;i<v.size();i++) |
| 555 | s = s + " " + v[i]; |
| 556 | p->ParseLine(s); |
| 557 | } |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | void builtin_ifeq(void* ptr, string /*sCommand*/, string sParams) |
| 562 | { |
| 563 | GUI_impl* p = (GUI_impl*)ptr; |
| 564 | vector<string> v = ChopAndUnquoteString(sParams); |
| 565 | if(v.size()<3) |
| 566 | { |
| 567 | cerr <<"? GUI_impl internal ifeq command: not enough params (syntax: if gvar command)"<< endl; |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | string sValue = GV3::get_var(v[0]); |
| 572 | string sIfValue = v[1]; |
| 573 | if(sValue == "(not in GVar list)") |
| 574 | return; |
| 575 | |
| 576 | // Have to do this to fix whitespace issues on non-registered gvars |
| 577 | vector<string> vv = ChopAndUnquoteString(sValue); |
| 578 | |
| 579 | if(vv.size()<1) |
| 580 | return; |
| 581 | else if(vv[0]==sIfValue) |
| 582 | { |
| 583 | string s; |
| 584 | s = ""; |
| 585 | for(size_t i=2;i<v.size();i++) |
| 586 | s = s + " " + v[i]; |
| 587 | p->ParseLine(s); |
| 588 | } |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | void builtin_toggle(void* /*ptr*/, string /*sCommand*/, string sParams) |
| 593 | { |
| 594 | vector<string> v = ChopAndUnquoteString(sParams); |
| 595 | if(v.size()!=1) |
| 596 | { |
| 597 | cout <<"? GUI_impl internal toggle command: invalid num of params (syntax: toggle gvar)"<< endl; |
| 598 | return; |
| 599 | }; |
| 600 | int nValue = GV3::get<int>(v[0]); |
| 601 | if(nValue) |
| 602 | GV3::set_var(v[0],"0"); |
| 603 | else |
| 604 | GV3::set_var(v[0],"1"); |
| 605 | return; |
| 606 | |
| 607 | }; |
| 608 | |
| 609 | void builtin_set(void* /*ptr*/, string /*sCommand*/, string sParams) |
| 610 | { |
| 611 | setvar(sParams); |
| 612 | } |
| 613 | |
| 614 | |
| 615 | void builtin_gvarlist(void* /*ptr*/, string sCommand, string sParams) |
| 616 | { |
| 617 | bool error = false; |
| 618 | bool print_all = false; |
| 619 | string pattern = ""; |
| 620 | |
| 621 | vector<string> v = ChopAndUnquoteString(sParams); |
| 622 | if(v.size() > 0 && v[0][0] == '-') |
| 623 | { |
| 624 | if(v[0].size() == 2) |
| 625 | { |
| 626 | switch(v[0][1]) |
| 627 | { |
| 628 | case 'a': |
| 629 | print_all = true; |
| 630 | break; |
| 631 | default: |
| 632 | error = true; |
| 633 | } |
| 634 | if(!error) |
| 635 | v.erase(v.begin()); |
| 636 | } |
| 637 | else |
| 638 | error = true; |
| 639 | } |
| 640 | |
| 641 | if(v.size()==1) |
| 642 | pattern = v[0]; |
| 643 | else if(v.size() > 1) |
| 644 | error = true; |
| 645 | |
| 646 | if(error) |
| 647 | cout << "? GUI_impl internal " << sCommand << ": syntax is " << sCommand << " [-a] [pattern] " << endl; |
| 648 | else |
| 649 | GV3::print_var_list(cout, pattern, print_all); |
| 650 | } |
| 651 | |
| 652 | |
| 653 | void builtin_printvar(void* /*ptr*/, string /*sCommand*/, string sParams) |
| 654 | { |
| 655 | cout << sParams << "=" << GV3::get_var(sParams) << endl;; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | void builtin_commandlist(void* ptr, string /*sCommand*/, string /*sParams*/) |
| 660 | { |
| 661 | GUI_impl* p = (GUI_impl*)ptr; |
| 662 | |
| 663 | cout << " Builtin commands:" << endl; |
| 664 | |
| 665 | for(set<string>::iterator i=p->builtins.begin(); i!=p->builtins.end(); i++) |
| 666 | cout << " " << *i << endl; |
| 667 | |
| 668 | |
| 669 | cout << " Registered commands:" << endl; |
| 670 | |
| 671 | for(map<string,CallbackVector>::iterator i=p->mmCallBackMap.begin(); i!=p->mmCallBackMap.end(); i++) |
| 672 | if(p->builtins.count(i->first) == 0) |
| 673 | cout << " " << i->first << endl; |
| 674 | } |
| 675 | |
| 676 | void builtin_queue(void* ptr, string /*sCommand*/, string sParams) |
| 677 | { |
| 678 | vector<string> vs = ChopAndUnquoteString(sParams); |
| 679 | if(vs.size() < 2) |
| 680 | { |
| 681 | cout << "? GUI_impl Internal queue command syntax: queue queue-name line-to-enqueue" << endl; |
| 682 | return; |
| 683 | } |
| 684 | string &sQueueName = vs[0]; |
| 685 | sParams.erase(sParams.find(sQueueName), sQueueName.length()); |
| 686 | |
| 687 | GUI_impl* pGUI = (GUI_impl*)ptr; |
| 688 | pGUI->mmQueues[sQueueName].push_back(sParams); |
| 689 | } |
| 690 | |
| 691 | void builtin_runqueue(void* ptr, string sCommand, string sParams) |
| 692 | { |
| 693 | GUI_impl* pGUI = (GUI_impl*)ptr; |
| 694 | vector<string> vs = ChopAndUnquoteString(sParams); |
| 695 | if(vs.size() != 1) |
| 696 | { |
| 697 | cout << "? GUI_impl Internal " << sCommand << " command syntax: runqueue queue-name " << endl; |
| 698 | size_t nQueues = pGUI->mmQueues.size(); |
| 699 | |
| 700 | cout << " Currently there are " << nQueues << " queues registered." << endl; |
| 701 | if(nQueues > 0) |
| 702 | { |
| 703 | cout << " They are: "; |
| 704 | for(map<string,vector<string> >::iterator it=pGUI->mmQueues.begin(); |
| 705 | it!=pGUI->mmQueues.end(); |
| 706 | it++) |
| 707 | cout << ((it==pGUI->mmQueues.begin())?"":", ") << it->first; |
| 708 | cout << endl; |
| 709 | } |
| 710 | return; |
| 711 | } |
| 712 | string &sQueueName = vs[0]; |
| 713 | vector<string> &vQueue = pGUI->mmQueues[sQueueName]; |
| 714 | for(size_t i=0; i<vQueue.size(); i++) |
| 715 | pGUI->ParseLine(vQueue[i]); |
| 716 | if(sCommand=="runqueue") |
| 717 | vQueue.clear(); // do not clear the queue if the command was runqueue_noclear! |
| 718 | } |
| 719 | |
| 720 | int GUI_impl::parseArguments( const int argc, char * argv[], int start, const string prefix, const string execKeyword ){ |
| 721 | while(start < argc){ |
| 722 | string opt = argv[start]; |
| 723 | if(opt.size() > prefix.size() && opt.find(prefix) == 0){ |
| 724 | string name = opt.substr(2); |
| 725 | if(start < argc-1){ |
| 726 | string value = argv[start+1]; |
| 727 | if(name == execKeyword){ |
| 728 | LoadFile(value); |
| 729 | } else { |
| 730 | GV3::set_var(name, value); |
| 731 | } |
| 732 | start +=2; |
| 733 | } |
| 734 | else break; |
| 735 | } |
| 736 | else break; |
| 737 | } |
| 738 | return start; |
| 739 | } |
| 740 | |
| 741 | void GUI_impl::RegisterBuiltin(string sCommandName, GUICallbackProc callback) |
| 742 | { |
| 743 | RegisterCommand(sCommandName, callback, this); |
| 744 | builtins.insert(sCommandName); |
| 745 | } |
| 746 | |
| 747 | GUI_impl::GUI_impl() |
| 748 | { |
| 749 | do_builtins(); |
| 750 | lang=0; |
| 751 | } |
| 752 | |
| 753 | void GUI_impl::post_init() |
| 754 | { |
| 755 | lang = get_new_lang(); |
| 756 | } |
| 757 | |
| 758 | GUI_impl::GUI_impl(GVars2*) |
| 759 | { |
| 760 | do_builtins(); |
| 761 | } |
| 762 | |
| 763 | void print_history(ostream &ost); |
| 764 | void builtin_history(void* /*ptr*/, string /*sCommand*/, string /*sParams*/) |
| 765 | { |
| 766 | cout << "History: " << endl; |
| 767 | print_history(cout); |
| 768 | }; |
| 769 | |
| 770 | void builtin_save_history(void* /*ptr*/, string /*sCommand*/, string sParams) |
| 771 | { |
| 772 | vector<string> v = ChopAndUnquoteString(sParams); |
| 773 | if(v.size()!=1) |
| 774 | cout << "? GUI_impl internal savehistory command: need one param (filename)." << endl; |
| 775 | else |
| 776 | { |
| 777 | ofstream ofs; |
| 778 | ofs.open(v[0].c_str()); |
| 779 | if(!ofs.good()) |
| 780 | cout << "? GUI_impl internal savehistory command: cannot open " << v[0] << " for write." << endl; |
| 781 | else |
| 782 | { |
| 783 | print_history(ofs); |
| 784 | ofs.close(); |
| 785 | cout << " Saved to " << v[0] << endl; |
| 786 | } |
| 787 | }; |
| 788 | }; |
| 789 | |
| 790 | |
| 791 | void GUI_impl::do_builtins() |
| 792 | { |
| 793 | RegisterBuiltin("shell", builtin_shell); |
| 794 | RegisterBuiltin("ls", builtin_ls); |
| 795 | RegisterBuiltin("ll", builtin_ll); |
| 796 | RegisterBuiltin("try", builtin_try); |
| 797 | RegisterBuiltin("exec", builtin_exec); |
| 798 | RegisterBuiltin("echo", builtin_echo); |
| 799 | RegisterBuiltin("if", builtin_if); |
| 800 | RegisterBuiltin("ifnot", builtin_ifnot); |
| 801 | RegisterBuiltin("ifeq", builtin_ifeq); |
| 802 | RegisterBuiltin("toggle", builtin_toggle); |
| 803 | RegisterBuiltin("set", builtin_set); |
| 804 | RegisterBuiltin("history", builtin_history); |
| 805 | RegisterBuiltin("savehistory", builtin_save_history); |
| 806 | RegisterBuiltin("?", builtin_qmark); |
| 807 | RegisterBuiltin("gvarlist", builtin_gvarlist); |
| 808 | RegisterBuiltin("printvar", builtin_printvar); |
| 809 | RegisterBuiltin("commandlist", builtin_commandlist); |
| 810 | RegisterBuiltin("queue", builtin_queue); |
| 811 | RegisterBuiltin("runqueue", builtin_runqueue); |
| 812 | RegisterBuiltin("runqueue_noclear", builtin_runqueue); |
| 813 | }; |
| 814 | |
| 815 | |
| 816 | |
| 817 | /////////////////////////////////////// |
| 818 | /////////////////////////////////////// |
| 819 | //////// Parser/Readline stuff: |
| 820 | /////////////////////////////////////// |
| 821 | /////////////////////////////////////// |
| 822 | |
| 823 | void* GUI_impl::mpParserThread = NULL; |
| 824 | |
| 825 | |
| 826 | } |
| 827 | |
| 828 | |
| 829 |