forked from h4yde/team-comtress-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandline.cpp
696 lines (557 loc) · 16.6 KB
/
commandline.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $Workfile: $
// $NoKeywords: $
//===========================================================================//
#include "pch_tier0.h"
#include "tier0/icommandline.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tier0/dbg.h"
#include "tier0/memdbgon.h"
#ifdef POSIX
#include <limits.h>
#define _MAX_PATH PATH_MAX
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
static const int MAX_PARAMETER_LEN = 128;
//-----------------------------------------------------------------------------
// Purpose: Implements ICommandLine
//-----------------------------------------------------------------------------
class CCommandLine : public ICommandLine
{
public:
// Construction
CCommandLine( void );
virtual ~CCommandLine( void );
// Implements ICommandLine
virtual void CreateCmdLine( const char *commandline );
virtual void CreateCmdLine( int argc, char **argv );
virtual const char *GetCmdLine( void ) const;
virtual const char *CheckParm( const char *psz, const char **ppszValue = 0 ) const;
// A bool return of whether param exists, useful for just checking if param that is just a flag is set
virtual bool HasParm( const char *psz ) const;
virtual void RemoveParm( const char *parm );
virtual void AppendParm( const char *pszParm, const char *pszValues );
virtual int ParmCount() const;
virtual int FindParm( const char *psz ) const;
virtual const char* GetParm( int nIndex ) const;
virtual const char *ParmValue( const char *psz, const char *pDefaultVal = NULL ) const OVERRIDE;
virtual int ParmValue( const char *psz, int nDefaultVal ) const OVERRIDE;
virtual float ParmValue( const char *psz, float flDefaultVal ) const OVERRIDE;
virtual const char *ParmValueByIndex( int nIndex, const char *pDefaultVal = 0 ) const OVERRIDE;
virtual void SetParm( int nIndex, char const *pParm );
private:
enum
{
MAX_PARAMETER_LEN = 128,
MAX_PARAMETERS = 256,
};
// When the commandline contains @name, it reads the parameters from that file
void LoadParametersFromFile( const char *&pSrc, char *&pDst, int maxDestLen, bool bInQuotes );
// Parse command line...
void ParseCommandLine();
// Frees the command line arguments
void CleanUpParms();
// Adds an argument..
void AddArgument( const char *pFirst, const char *pLast );
// Copy of actual command line
char *m_pszCmdLine;
// Pointers to each argument...
int m_nParmCount;
char *m_ppParms[MAX_PARAMETERS];
};
//-----------------------------------------------------------------------------
// Instance singleton and expose interface to rest of code
//-----------------------------------------------------------------------------
static CCommandLine g_CmdLine;
ICommandLine *CommandLine()
{
return &g_CmdLine;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CCommandLine::CCommandLine( void )
{
m_pszCmdLine = NULL;
m_nParmCount = 0;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CCommandLine::~CCommandLine( void )
{
CleanUpParms();
delete[] m_pszCmdLine;
}
//-----------------------------------------------------------------------------
// Read commandline from file instead...
//-----------------------------------------------------------------------------
void CCommandLine::LoadParametersFromFile( const char *&pSrc, char *&pDst, int maxDestLen, bool bInQuotes )
{
// Suck out the file name
char szFileName[ _MAX_PATH ];
char *pOut;
char *pDestStart = pDst;
if ( maxDestLen < 3 )
return;
// Skip the @ sign
pSrc++;
pOut = szFileName;
char terminatingChar = ' ';
if ( bInQuotes )
terminatingChar = '\"';
while ( *pSrc && *pSrc != terminatingChar )
{
*pOut++ = *pSrc++;
if ( (pOut - szFileName) >= (_MAX_PATH-1) )
break;
}
*pOut = '\0';
// Skip the space after the file name
if ( *pSrc )
pSrc++;
// Now read in parameters from file
FILE *fp = fopen( szFileName, "r" );
if ( fp )
{
char c;
c = (char)fgetc( fp );
while ( c != EOF )
{
// Turn return characters into spaces
if ( c == '\n' )
c = ' ';
*pDst++ = c;
// Don't go past the end, and allow for our terminating space character AND a terminating null character.
if ( (pDst - pDestStart) >= (maxDestLen-2) )
break;
// Get the next character, if there are more
c = (char)fgetc( fp );
}
// Add a terminating space character
*pDst++ = ' ';
fclose( fp );
}
else
{
printf( "Parameter file '%s' not found, skipping...", szFileName );
}
}
//-----------------------------------------------------------------------------
// Creates a command line from the arguments passed in
//-----------------------------------------------------------------------------
void CCommandLine::CreateCmdLine( int argc, char **argv )
{
char cmdline[ 2048 ];
cmdline[ 0 ] = 0;
char *dest = cmdline;
size_t size = sizeof( cmdline );
const char *space = "";
for ( int i = 0; i < argc; ++i )
{
// We need room for: space, arg, 2 quotes, and a nil.
Assert( strlen( space ) + strlen( argv[ i ] ) + 2 + 1 <= size );
if ( size )
{
_snprintf( dest, size, "%s\"%s\"", space, argv[ i ] );
dest[ size - 1 ] = 0;
}
size_t len = strlen( dest );
size -= len;
dest += len;
space = " ";
}
CreateCmdLine( cmdline );
}
//-----------------------------------------------------------------------------
// Purpose: Create a command line from the passed in string
// Note that if you pass in a @filename, then the routine will read settings
// from a file instead of the command line
//-----------------------------------------------------------------------------
void CCommandLine::CreateCmdLine( const char *commandline )
{
if ( m_pszCmdLine )
{
delete[] m_pszCmdLine;
}
char szFull[ 4096 ];
szFull[0] = '\0';
char *pDst = szFull;
const char *pSrc = commandline;
bool bInQuotes = false;
const char *pInQuotesStart = 0;
while ( *pSrc )
{
// Is this an unslashed quote?
if ( *pSrc == '"' )
{
if ( pSrc == commandline || ( pSrc[-1] != '/' && pSrc[-1] != '\\' ) )
{
bInQuotes = !bInQuotes;
pInQuotesStart = pSrc + 1;
}
}
if ( *pSrc == '@' )
{
if ( pSrc == commandline || (!bInQuotes && isspace( pSrc[-1] )) || (bInQuotes && pSrc == pInQuotesStart) )
{
LoadParametersFromFile( pSrc, pDst, sizeof( szFull ) - (pDst - szFull), bInQuotes );
continue;
}
}
// Don't go past the end.
if ( (pDst - szFull) >= (sizeof( szFull ) - 1) )
break;
*pDst++ = *pSrc++;
}
*pDst = '\0';
int len = strlen( szFull ) + 1;
m_pszCmdLine = new char[len];
memcpy( m_pszCmdLine, szFull, len );
ParseCommandLine();
}
//-----------------------------------------------------------------------------
// Finds a string in another string with a case insensitive test
//-----------------------------------------------------------------------------
static char * _stristr( char * pStr, const char * pSearch )
{
AssertValidStringPtr(pStr);
AssertValidStringPtr(pSearch);
if (!pStr || !pSearch)
return 0;
char* pLetter = pStr;
// Check the entire string
while (*pLetter != 0)
{
// Skip over non-matches
if (tolower((unsigned char)*pLetter) == tolower((unsigned char)*pSearch))
{
// Check for match
char const* pMatch = pLetter + 1;
char const* pTest = pSearch + 1;
while (*pTest != 0)
{
// We've run off the end; don't bother.
if (*pMatch == 0)
return 0;
if (tolower((unsigned char)*pMatch) != tolower((unsigned char)*pTest))
break;
++pMatch;
++pTest;
}
// Found a match!
if (*pTest == 0)
return pLetter;
}
++pLetter;
}
return 0;
}
//-----------------------------------------------------------------------------
// Purpose: Remove specified string ( and any args attached to it ) from command line
// Input : *pszParm -
//-----------------------------------------------------------------------------
void CCommandLine::RemoveParm( const char *pszParm )
{
if ( !m_pszCmdLine )
return;
// Search for first occurrence of pszParm
char *p, *found;
char *pnextparam;
int n;
int curlen;
p = m_pszCmdLine;
while ( *p )
{
curlen = strlen( p );
found = _stristr( p, pszParm );
if ( !found )
break;
pnextparam = found + 1;
bool bHadQuote = false;
if ( found > m_pszCmdLine && found[-1] == '\"' )
bHadQuote = true;
while ( pnextparam && *pnextparam && (*pnextparam != ' ') && (*pnextparam != '\"') )
pnextparam++;
if ( pnextparam && ( static_cast<size_t>( pnextparam - found ) > strlen( pszParm ) ) )
{
p = pnextparam;
continue;
}
while ( pnextparam && *pnextparam && (*pnextparam != '-') && (*pnextparam != '+') )
pnextparam++;
if ( bHadQuote )
{
found--;
}
if ( pnextparam && *pnextparam )
{
// We are either at the end of the string, or at the next param. Just chop out the current param.
n = curlen - ( pnextparam - p ); // # of characters after this param.
memmove( found, pnextparam, n );
found[n] = '\0';
}
else
{
// Clear out rest of string.
n = pnextparam - found;
memset( found, 0, n );
}
}
// Strip and trailing ' ' characters left over.
while ( 1 )
{
int len = strlen( m_pszCmdLine );
if ( len == 0 || m_pszCmdLine[ len - 1 ] != ' ' )
break;
m_pszCmdLine[len - 1] = '\0';
}
ParseCommandLine();
}
//-----------------------------------------------------------------------------
// Purpose: Append parameter and argument values to command line
// Input : *pszParm -
// *pszValues -
//-----------------------------------------------------------------------------
void CCommandLine::AppendParm( const char *pszParm, const char *pszValues )
{
int nNewLength = 0;
char *pCmdString;
nNewLength = strlen( pszParm ); // Parameter.
if ( pszValues )
nNewLength += strlen( pszValues ) + 1; // Values + leading space character.
nNewLength++; // Terminal 0;
if ( !m_pszCmdLine )
{
m_pszCmdLine = new char[ nNewLength ];
strcpy( m_pszCmdLine, pszParm );
if ( pszValues )
{
strcat( m_pszCmdLine, " " );
strcat( m_pszCmdLine, pszValues );
}
ParseCommandLine();
return;
}
// Remove any remnants from the current Cmd Line.
RemoveParm( pszParm );
nNewLength += strlen( m_pszCmdLine ) + 1 + 1;
pCmdString = new char[ nNewLength ];
memset( pCmdString, 0, nNewLength );
strcpy ( pCmdString, m_pszCmdLine ); // Copy old command line.
strcat ( pCmdString, " " ); // Put in a space
strcat ( pCmdString, pszParm );
if ( pszValues )
{
strcat( pCmdString, " " );
strcat( pCmdString, pszValues );
}
// Kill off the old one
delete[] m_pszCmdLine;
// Point at the new command line.
m_pszCmdLine = pCmdString;
ParseCommandLine();
}
//-----------------------------------------------------------------------------
// Purpose: Return current command line
// Output : const char
//-----------------------------------------------------------------------------
const char *CCommandLine::GetCmdLine( void ) const
{
return m_pszCmdLine;
}
//-----------------------------------------------------------------------------
// Purpose: Search for the parameter in the current commandline
// Input : *psz -
// **ppszValue -
// Output : char
//-----------------------------------------------------------------------------
const char *CCommandLine::CheckParm( const char *psz, const char **ppszValue ) const
{
if ( ppszValue )
*ppszValue = NULL;
int i = FindParm( psz );
if ( i == 0 )
return NULL;
if ( ppszValue )
{
if ( (i+1) >= m_nParmCount )
{
*ppszValue = NULL;
}
else
{
*ppszValue = m_ppParms[i+1];
}
}
return m_ppParms[i];
}
//-----------------------------------------------------------------------------
// Adds an argument..
//-----------------------------------------------------------------------------
void CCommandLine::AddArgument( const char *pFirst, const char *pLast )
{
if ( pLast <= pFirst )
return;
if ( m_nParmCount >= MAX_PARAMETERS )
Error( "CCommandLine::AddArgument: exceeded %d parameters", MAX_PARAMETERS );
size_t nLen = pLast - pFirst + 1;
m_ppParms[m_nParmCount] = new char[nLen];
memcpy( m_ppParms[m_nParmCount], pFirst, nLen - 1 );
m_ppParms[m_nParmCount][nLen - 1] = 0;
++m_nParmCount;
}
//-----------------------------------------------------------------------------
// Parse command line...
//-----------------------------------------------------------------------------
void CCommandLine::ParseCommandLine()
{
CleanUpParms();
if (!m_pszCmdLine)
return;
const char *pChar = m_pszCmdLine;
while ( *pChar && isspace(*pChar) )
{
++pChar;
}
bool bInQuotes = false;
const char *pFirstLetter = NULL;
for ( ; *pChar; ++pChar )
{
if ( bInQuotes )
{
if ( *pChar != '\"' )
continue;
AddArgument( pFirstLetter, pChar );
pFirstLetter = NULL;
bInQuotes = false;
continue;
}
// Haven't started a word yet...
if ( !pFirstLetter )
{
if ( *pChar == '\"' )
{
bInQuotes = true;
pFirstLetter = pChar + 1;
continue;
}
if ( isspace( *pChar ) )
continue;
pFirstLetter = pChar;
continue;
}
// Here, we're in the middle of a word. Look for the end of it.
if ( isspace( *pChar ) )
{
AddArgument( pFirstLetter, pChar );
pFirstLetter = NULL;
}
}
if ( pFirstLetter )
{
AddArgument( pFirstLetter, pChar );
}
}
//-----------------------------------------------------------------------------
// Individual command line arguments
//-----------------------------------------------------------------------------
void CCommandLine::CleanUpParms()
{
for ( int i = 0; i < m_nParmCount; ++i )
{
delete [] m_ppParms[i];
m_ppParms[i] = NULL;
}
m_nParmCount = 0;
}
//-----------------------------------------------------------------------------
// Returns individual command line arguments
//-----------------------------------------------------------------------------
int CCommandLine::ParmCount() const
{
return m_nParmCount;
}
int CCommandLine::FindParm( const char *psz ) const
{
// Start at 1 so as to not search the exe name
for ( int i = 1; i < m_nParmCount; ++i )
{
if ( !_stricmp( psz, m_ppParms[i] ) )
return i;
}
return 0;
}
bool CCommandLine::HasParm( const char *psz ) const
{
return ( FindParm( psz ) != 0 );
}
const char* CCommandLine::GetParm( int nIndex ) const
{
Assert( (nIndex >= 0) && (nIndex < m_nParmCount) );
if ( (nIndex < 0) || (nIndex >= m_nParmCount) )
return "";
return m_ppParms[nIndex];
}
void CCommandLine::SetParm( int nIndex, char const *pParm )
{
if ( pParm )
{
Assert( (nIndex >= 0) && (nIndex < m_nParmCount) );
if ( (nIndex >= 0) && (nIndex < m_nParmCount) )
{
if ( m_ppParms[nIndex] )
delete[] m_ppParms[nIndex];
m_ppParms[nIndex] = strdup( pParm );
}
}
}
//-----------------------------------------------------------------------------
// Returns the argument after the one specified, or the default if not found
//-----------------------------------------------------------------------------
const char *CCommandLine::ParmValue( const char *psz, const char *pDefaultVal ) const
{
int nIndex = FindParm( psz );
if (( nIndex == 0 ) || (nIndex == m_nParmCount - 1))
return pDefaultVal;
// Probably another cmdline parameter instead of a valid arg if it starts with '+' or '-'
if ( m_ppParms[nIndex + 1][0] == '-' || m_ppParms[nIndex + 1][0] == '+' )
return pDefaultVal;
return m_ppParms[nIndex + 1];
}
int CCommandLine::ParmValue( const char *psz, int nDefaultVal ) const
{
int nIndex = FindParm( psz );
if (( nIndex == 0 ) || (nIndex == m_nParmCount - 1))
return nDefaultVal;
// Probably another cmdline parameter instead of a valid arg if it starts with '+' or '-'
if ( m_ppParms[nIndex + 1][0] == '-' || m_ppParms[nIndex + 1][0] == '+' )
return nDefaultVal;
return atoi( m_ppParms[nIndex + 1] );
}
float CCommandLine::ParmValue( const char *psz, float flDefaultVal ) const
{
int nIndex = FindParm( psz );
if (( nIndex == 0 ) || (nIndex == m_nParmCount - 1))
return flDefaultVal;
// Probably another cmdline parameter instead of a valid arg if it starts with '+' or '-'
if ( m_ppParms[nIndex + 1][0] == '-' || m_ppParms[nIndex + 1][0] == '+' )
return flDefaultVal;
return atof( m_ppParms[nIndex + 1] );
}
const char *CCommandLine::ParmValueByIndex( int nIndex, const char *pDefaultVal ) const
{
if (( nIndex == 0 ) || (nIndex == m_nParmCount - 1))
return pDefaultVal;
// Probably another cmdline parameter instead of a valid arg if it starts with '+' or '-'
if ( m_ppParms[nIndex + 1][0] == '-' || m_ppParms[nIndex + 1][0] == '+' )
return pDefaultVal;
return m_ppParms[nIndex + 1];
}