CmdArgs.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../idlib/precompiled.h"
  21. #pragma hdrstop
  22. /*
  23. ============
  24. idCmdArgs::operator=
  25. ============
  26. */
  27. void idCmdArgs::operator=( const idCmdArgs &args ) {
  28. int i;
  29. argc = args.argc;
  30. memcpy( tokenized, args.tokenized, MAX_COMMAND_STRING );
  31. for ( i = 0; i < argc; i++ ) {
  32. argv[ i ] = tokenized + ( args.argv[ i ] - args.tokenized );
  33. }
  34. }
  35. /*
  36. ============
  37. idCmdArgs::Args
  38. ============
  39. */
  40. const char *idCmdArgs::Args( int start, int end, bool escapeArgs ) const {
  41. static char cmd_args[MAX_COMMAND_STRING];
  42. int i;
  43. if ( end < 0 ) {
  44. end = argc - 1;
  45. } else if ( end >= argc ) {
  46. end = argc - 1;
  47. }
  48. cmd_args[0] = '\0';
  49. if ( escapeArgs ) {
  50. strcat( cmd_args, "\"" );
  51. }
  52. for ( i = start; i <= end; i++ ) {
  53. if ( i > start ) {
  54. if ( escapeArgs ) {
  55. strcat( cmd_args, "\" \"" );
  56. } else {
  57. strcat( cmd_args, " " );
  58. }
  59. }
  60. if ( escapeArgs && strchr( argv[i], '\\' ) ) {
  61. char *p = argv[i];
  62. while ( *p != '\0' ) {
  63. if ( *p == '\\' ) {
  64. strcat( cmd_args, "\\\\" );
  65. } else {
  66. int l = strlen( cmd_args );
  67. cmd_args[ l ] = *p;
  68. cmd_args[ l+1 ] = '\0';
  69. }
  70. p++;
  71. }
  72. } else {
  73. strcat( cmd_args, argv[i] );
  74. }
  75. }
  76. if ( escapeArgs ) {
  77. strcat( cmd_args, "\"" );
  78. }
  79. return cmd_args;
  80. }
  81. /*
  82. ============
  83. idCmdArgs::TokenizeString
  84. Parses the given string into command line tokens.
  85. The text is copied to a separate buffer and 0 characters
  86. are inserted in the appropriate place. The argv array
  87. will point into this temporary buffer.
  88. ============
  89. */
  90. void idCmdArgs::TokenizeString( const char *text, bool keepAsStrings ) {
  91. idLexer lex;
  92. idToken token, number;
  93. int len, totalLen;
  94. // clear previous args
  95. argc = 0;
  96. if ( !text ) {
  97. return;
  98. }
  99. lex.LoadMemory( text, strlen( text ), "idCmdSystemLocal::TokenizeString" );
  100. lex.SetFlags( LEXFL_NOERRORS
  101. | LEXFL_NOWARNINGS
  102. | LEXFL_NOSTRINGCONCAT
  103. | LEXFL_ALLOWPATHNAMES
  104. | LEXFL_NOSTRINGESCAPECHARS
  105. | LEXFL_ALLOWIPADDRESSES | ( keepAsStrings ? LEXFL_ONLYSTRINGS : 0 ) );
  106. totalLen = 0;
  107. while ( 1 ) {
  108. if ( argc == MAX_COMMAND_ARGS ) {
  109. return; // this is usually something malicious
  110. }
  111. if ( !lex.ReadToken( &token ) ) {
  112. return;
  113. }
  114. // check for negative numbers
  115. if ( !keepAsStrings && ( token == "-" ) ) {
  116. if ( lex.CheckTokenType( TT_NUMBER, 0, &number ) ) {
  117. token = "-" + number;
  118. }
  119. }
  120. // check for cvar expansion
  121. if ( token == "$" ) {
  122. if ( !lex.ReadToken( &token ) ) {
  123. return;
  124. }
  125. if ( idLib::cvarSystem ) {
  126. token = idLib::cvarSystem->GetCVarString( token.c_str() );
  127. } else {
  128. token = "<unknown>";
  129. }
  130. }
  131. len = token.Length();
  132. if ( totalLen + len + 1 > sizeof( tokenized ) ) {
  133. return; // this is usually something malicious
  134. }
  135. // regular token
  136. argv[argc] = tokenized + totalLen;
  137. argc++;
  138. idStr::Copynz( tokenized + totalLen, token.c_str(), sizeof( tokenized ) - totalLen );
  139. totalLen += len + 1;
  140. }
  141. }
  142. /*
  143. ============
  144. idCmdArgs::AppendArg
  145. ============
  146. */
  147. void idCmdArgs::AppendArg( const char *text ) {
  148. if ( !argc ) {
  149. argc = 1;
  150. argv[ 0 ] = tokenized;
  151. idStr::Copynz( tokenized, text, sizeof( tokenized ) );
  152. } else {
  153. argv[ argc ] = argv[ argc-1 ] + strlen( argv[ argc-1 ] ) + 1;
  154. idStr::Copynz( argv[ argc ], text, sizeof( tokenized ) - ( argv[ argc ] - tokenized ) );
  155. argc++;
  156. }
  157. }
  158. /*
  159. ============
  160. idCmdArgs::GetArgs
  161. ============
  162. */
  163. const char **idCmdArgs::GetArgs( int *_argc ) {
  164. *_argc = argc;
  165. return (const char **)&argv[0];
  166. }