CmdLine.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (c) 2002-2012 Croteam Ltd.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of version 2 of the GNU General Public License as published by
  4. the Free Software Foundation
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along
  10. with this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
  12. #include "StdH.h"
  13. #include <Engine/CurrentVersion.h>
  14. #include "CmdLine.h"
  15. extern CTString cmd_strWorld = ""; // world to load
  16. extern INDEX cmd_iGoToMarker = -1; // marker to go to
  17. extern CTString cmd_strScript = ""; // script to execute
  18. extern CTString cmd_strServer = ""; // server to connect to
  19. extern INDEX cmd_iPort = -1; // port to connect to
  20. extern CTString cmd_strPassword = ""; // network password
  21. extern CTString cmd_strOutput = ""; // output from parsing command line
  22. extern BOOL cmd_bServer = FALSE; // set to run as server
  23. extern BOOL cmd_bQuickJoin = FALSE; // do not ask for players and network settings
  24. static CTString _strCmd;
  25. // get first next word or quoted string
  26. CTString GetNextParam(void)
  27. {
  28. // strip leading spaces/tabs
  29. _strCmd.TrimSpacesLeft();
  30. // if nothing left
  31. if (_strCmd=="") {
  32. // no word to return
  33. return "";
  34. }
  35. // if the first char is quote
  36. if (_strCmd[0]=='"') {
  37. // find first next quote
  38. const char *pchClosingQuote = strchr(_strCmd+1, '"');
  39. // if not found
  40. if (pchClosingQuote==NULL) {
  41. // error in command line
  42. cmd_strOutput+=CTString(0, TRANS("Command line error!\n"));
  43. // finish parsing
  44. _strCmd = "";
  45. return "";
  46. }
  47. INDEX iQuote = pchClosingQuote-_strCmd;
  48. // get the quoted string
  49. CTString strWord;
  50. CTString strRest;
  51. _strCmd.Split(iQuote, strWord, strRest);
  52. // remove the quotes
  53. strWord.DeleteChar(0);
  54. strRest.DeleteChar(0);
  55. // get the word
  56. _strCmd = strRest;
  57. return strWord;
  58. // if the first char is not quote
  59. } else {
  60. // find first next space
  61. INDEX iSpace;
  62. INDEX ctChars = strlen(_strCmd);
  63. for(iSpace=0; iSpace<ctChars; iSpace++) {
  64. if (isspace(_strCmd[iSpace])) {
  65. break;
  66. }
  67. }
  68. // get the word string
  69. CTString strWord;
  70. CTString strRest;
  71. _strCmd.Split(iSpace, strWord, strRest);
  72. // remove the space
  73. strRest.DeleteChar(0);
  74. // get the word
  75. _strCmd = strRest;
  76. return strWord;
  77. }
  78. }
  79. // parse command line parameters and set results to static variables
  80. void ParseCommandLine(CTString strCmd)
  81. {
  82. cmd_strOutput = "";
  83. cmd_strOutput+=CTString(0, TRANS("Command line: '%s'\n"), strCmd);
  84. // if no command line
  85. if (strlen(strCmd) == 0) {
  86. // do nothing
  87. return;
  88. }
  89. _strCmd = strCmd;
  90. FOREVER {
  91. CTString strWord = GetNextParam();
  92. if (strWord=="") {
  93. cmd_strOutput+="\n";
  94. return;
  95. } else if (strWord=="+level") {
  96. cmd_strWorld = GetNextParam();
  97. } else if (strWord=="+server") {
  98. cmd_bServer = TRUE;
  99. } else if (strWord=="+quickjoin") {
  100. cmd_bQuickJoin = TRUE;
  101. } else if (strWord=="+game") {
  102. CTString strMod = GetNextParam();
  103. if (strMod!="SeriousSam") { // (we ignore default mod - always use base dir in that case)
  104. _fnmMod = "Mods\\"+strMod+"\\";
  105. }
  106. } else if (strWord=="+cdpath") {
  107. _fnmCDPath = GetNextParam();
  108. } else if (strWord=="+password") {
  109. cmd_strPassword = GetNextParam();
  110. } else if (strWord=="+connect") {
  111. cmd_strServer = GetNextParam();
  112. const char *pcColon = strchr(cmd_strServer, ':');
  113. if (pcColon!=NULL) {
  114. CTString strServer;
  115. CTString strPort;
  116. cmd_strServer.Split(pcColon-cmd_strServer, strServer, strPort);
  117. cmd_strServer = strServer;
  118. strPort.ScanF(":%d", &cmd_iPort);
  119. }
  120. } else if (strWord=="+script") {
  121. cmd_strScript = GetNextParam();
  122. } else if (strWord=="+goto") {
  123. GetNextParam().ScanF("%d", &cmd_iGoToMarker);
  124. } else if (strWord=="+logfile") {
  125. _strLogFile = GetNextParam();
  126. } else {
  127. cmd_strOutput+=CTString(0, TRANS(" Unknown option: '%s'\n"), strWord);
  128. }
  129. }
  130. }