doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CmdArgs.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 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.
23 
24 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.
25 
26 ===========================================================================
27 */
28 
29 #include "../idlib/precompiled.h"
30 #pragma hdrstop
31 
32 /*
33 ============
34 idCmdArgs::operator=
35 ============
36 */
37 void idCmdArgs::operator=( const idCmdArgs &args ) {
38  int i;
39 
40  argc = args.argc;
41  memcpy( tokenized, args.tokenized, MAX_COMMAND_STRING );
42  for ( i = 0; i < argc; i++ ) {
43  argv[ i ] = tokenized + ( args.argv[ i ] - args.tokenized );
44  }
45 }
46 
47 /*
48 ============
49 idCmdArgs::Args
50 ============
51 */
52 const char *idCmdArgs::Args( int start, int end, bool escapeArgs ) const {
53  static char cmd_args[MAX_COMMAND_STRING];
54  int i;
55 
56  if ( end < 0 ) {
57  end = argc - 1;
58  } else if ( end >= argc ) {
59  end = argc - 1;
60  }
61  cmd_args[0] = '\0';
62  if ( escapeArgs ) {
63  strcat( cmd_args, "\"" );
64  }
65  for ( i = start; i <= end; i++ ) {
66  if ( i > start ) {
67  if ( escapeArgs ) {
68  strcat( cmd_args, "\" \"" );
69  } else {
70  strcat( cmd_args, " " );
71  }
72  }
73  if ( escapeArgs && strchr( argv[i], '\\' ) ) {
74  char *p = argv[i];
75  while ( *p != '\0' ) {
76  if ( *p == '\\' ) {
77  strcat( cmd_args, "\\\\" );
78  } else {
79  int l = strlen( cmd_args );
80  cmd_args[ l ] = *p;
81  cmd_args[ l+1 ] = '\0';
82  }
83  p++;
84  }
85  } else {
86  strcat( cmd_args, argv[i] );
87  }
88  }
89  if ( escapeArgs ) {
90  strcat( cmd_args, "\"" );
91  }
92 
93  return cmd_args;
94 }
95 
96 /*
97 ============
98 idCmdArgs::TokenizeString
99 
100 Parses the given string into command line tokens.
101 The text is copied to a separate buffer and 0 characters
102 are inserted in the appropriate place. The argv array
103 will point into this temporary buffer.
104 ============
105 */
106 void idCmdArgs::TokenizeString( const char *text, bool keepAsStrings ) {
107  idLexer lex;
108  idToken token, number;
109  int len, totalLen;
110 
111  // clear previous args
112  argc = 0;
113 
114  if ( !text ) {
115  return;
116  }
117 
118  lex.LoadMemory( text, strlen( text ), "idCmdSystemLocal::TokenizeString" );
124  | LEXFL_ALLOWIPADDRESSES | ( keepAsStrings ? LEXFL_ONLYSTRINGS : 0 ) );
125 
126  totalLen = 0;
127 
128  while ( 1 ) {
129  if ( argc == MAX_COMMAND_ARGS ) {
130  return; // this is usually something malicious
131  }
132 
133  if ( !lex.ReadToken( &token ) ) {
134  return;
135  }
136 
137  // check for negative numbers
138  if ( !keepAsStrings && ( token == "-" ) ) {
139  if ( lex.CheckTokenType( TT_NUMBER, 0, &number ) ) {
140  token = "-" + number;
141  }
142  }
143 
144  // check for cvar expansion
145  if ( token == "$" ) {
146  if ( !lex.ReadToken( &token ) ) {
147  return;
148  }
149  if ( idLib::cvarSystem ) {
150  token = idLib::cvarSystem->GetCVarString( token.c_str() );
151  } else {
152  token = "<unknown>";
153  }
154  }
155 
156  len = token.Length();
157 
158  if ( totalLen + len + 1 > sizeof( tokenized ) ) {
159  return; // this is usually something malicious
160  }
161 
162  // regular token
163  argv[argc] = tokenized + totalLen;
164  argc++;
165 
166  idStr::Copynz( tokenized + totalLen, token.c_str(), sizeof( tokenized ) - totalLen );
167 
168  totalLen += len + 1;
169  }
170 }
171 
172 /*
173 ============
174 idCmdArgs::AppendArg
175 ============
176 */
177 void idCmdArgs::AppendArg( const char *text ) {
178  if ( !argc ) {
179  argc = 1;
180  argv[ 0 ] = tokenized;
181  idStr::Copynz( tokenized, text, sizeof( tokenized ) );
182  } else {
183  argv[ argc ] = argv[ argc-1 ] + strlen( argv[ argc-1 ] ) + 1;
184  idStr::Copynz( argv[ argc ], text, sizeof( tokenized ) - ( argv[ argc ] - tokenized ) );
185  argc++;
186  }
187 }
188 
189 /*
190 ============
191 idCmdArgs::GetArgs
192 ============
193 */
194 const char **idCmdArgs::GetArgs( int *_argc ) {
195  *_argc = argc;
196  return (const char **)&argv[0];
197 }
198 
int Length(void) const
Definition: Str.h:702
static const int MAX_COMMAND_ARGS
Definition: CmdArgs.h:65
static const int MAX_COMMAND_STRING
Definition: CmdArgs.h:66
const char ** GetArgs(int *argc)
Definition: CmdArgs.cpp:194
const char * Args(int start=1, int end=-1, bool escapeArgs=false) const
Definition: CmdArgs.cpp:52
int CheckTokenType(int type, int subtype, idToken *token)
Definition: Lexer.cpp:1028
Definition: Token.h:71
void SetFlags(int flags)
Definition: Lexer.h:298
GLenum GLsizei len
Definition: glext.h:3472
int i
Definition: process.py:33
#define TT_NUMBER
Definition: Token.h:43
list l
Definition: prepare.py:17
Definition: Lexer.h:137
void AppendArg(const char *text)
Definition: CmdArgs.cpp:177
char * argv[MAX_COMMAND_ARGS]
Definition: CmdArgs.h:69
int argc
Definition: CmdArgs.h:68
GLuint GLuint end
Definition: glext.h:2845
virtual const char * GetCVarString(const char *name) const =0
static void Copynz(char *dest, const char *src, int destsize)
Definition: Str.cpp:1376
int LoadMemory(const char *ptr, int length, const char *name, int startLine=1)
Definition: Lexer.cpp:1646
void TokenizeString(const char *text, bool keepAsStrings)
Definition: CmdArgs.cpp:106
void operator=(const idCmdArgs &args)
Definition: CmdArgs.cpp:37
static class idCVarSystem * cvarSystem
Definition: Lib.h:54
const char * c_str(void) const
Definition: Str.h:487
char tokenized[MAX_COMMAND_STRING]
Definition: CmdArgs.h:70
GLfloat GLfloat p
Definition: glext.h:4674
int ReadToken(idToken *token)
Definition: Lexer.cpp:820
GLuint start
Definition: glext.h:2845