2013-12-19 17:10:14 +00:00
/*
Command line parsing
~ yay ~
Nothing too interesting here , really
*/
# include <stdio.h>
# include <ctype.h>
2018-08-21 03:31:37 +00:00
# include <string.h>
2013-12-19 17:10:14 +00:00
# include "cfg/cfg.h"
wchar * trim_ws ( wchar * str )
{
if ( str = = 0 | | strlen ( str ) = = 0 )
return 0 ;
while ( * str )
{
if ( ! isspace ( * str ) )
break ;
str + + ;
}
size_t l = strlen ( str ) ;
if ( l = = 0 )
return 0 ;
while ( l > 0 )
{
if ( ! isspace ( str [ l - 1 ] ) )
break ;
str [ l - 1 ] = 0 ;
l - - ;
}
if ( l = = 0 )
return 0 ;
return str ;
}
int setconfig ( wchar * * arg , int cl )
{
int rv = 0 ;
for ( ; ; )
{
if ( cl < 1 )
{
2019-07-01 16:23:10 +00:00
WARN_LOG ( COMMON , " -config : invalid number of parameters, format is section:key=value " ) ;
2013-12-19 17:10:14 +00:00
return rv ;
}
wchar * sep = strstr ( arg [ 1 ] , " : " ) ;
if ( sep = = 0 )
{
2019-07-01 16:23:10 +00:00
WARN_LOG ( COMMON , " -config : invalid parameter %s, format is section:key=value " , arg [ 1 ] ) ;
2013-12-19 17:10:14 +00:00
return rv ;
}
wchar * value = strstr ( sep + 1 , " = " ) ;
if ( value = = 0 )
{
2019-07-01 16:23:10 +00:00
WARN_LOG ( COMMON , " -config : invalid parameter %s, format is section:key=value " , arg [ 1 ] ) ;
2013-12-19 17:10:14 +00:00
return rv ;
}
* sep + + = 0 ;
* value + + = 0 ;
wchar * sect = trim_ws ( arg [ 1 ] ) ;
wchar * key = trim_ws ( sep ) ;
value = trim_ws ( value ) ;
if ( sect = = 0 | | key = = 0 )
{
2019-07-01 16:23:10 +00:00
WARN_LOG ( COMMON , " -config : invalid parameter, format is section:key=value " ) ;
2013-12-19 17:10:14 +00:00
return rv ;
}
2015-07-29 01:23:08 +00:00
const wchar * constval = value ;
if ( constval = = 0 )
constval = " " ;
2019-07-01 16:23:10 +00:00
INFO_LOG ( COMMON , " Virtual cfg %s:%s=%s " , sect , key , value ) ;
2013-12-19 17:10:14 +00:00
2015-07-17 21:56:51 +00:00
cfgSetVirtual ( sect , key , value ) ;
2013-12-19 17:10:14 +00:00
rv + + ;
if ( cl > = 3 & & stricmp ( arg [ 2 ] , " , " ) = = 0 )
{
cl - = 2 ;
arg + = 2 ;
rv + + ;
continue ;
}
else
break ;
}
return rv ;
}
int showhelp ( wchar * * arg , int cl )
{
2019-07-01 16:23:10 +00:00
NOTICE_LOG ( COMMON , " Available commands: " ) ;
2013-12-19 17:10:14 +00:00
2019-07-01 16:23:10 +00:00
NOTICE_LOG ( COMMON , " -config section:key=value [, ..]: add a virtual config value \n Virtual config values won't be saved to the .cfg file \n unless a different value is written to em \n Note : \n You can specify many settings in the xx:yy=zz , gg:hh=jj , ... \n format.The spaces between the values and ',' are needed. " ) ;
NOTICE_LOG ( COMMON , " -help: show help info " ) ;
2018-08-21 03:31:37 +00:00
return 0 ;
}
2013-12-19 17:10:14 +00:00
2018-08-21 03:31:37 +00:00
bool ParseCommandLine ( int argc , wchar * argv [ ] )
{
2019-03-13 20:54:04 +00:00
cfgSetVirtual ( " config " , " image " , " " ) ;
2013-12-19 17:10:14 +00:00
int cl = argc - 2 ;
wchar * * arg = argv + 1 ;
while ( cl > = 0 )
{
2018-08-21 03:31:37 +00:00
if ( stricmp ( * arg , " -help " ) = = 0 | | stricmp ( * arg , " --help " ) = = 0 )
2013-12-19 17:10:14 +00:00
{
2018-08-21 03:31:37 +00:00
showhelp ( arg , cl ) ;
return true ;
}
else if ( stricmp ( * arg , " -config " ) = = 0 | | stricmp ( * arg , " --config " ) = = 0 )
2013-12-19 17:10:14 +00:00
{
int as = setconfig ( arg , cl ) ;
cl - = as ;
arg + = as ;
}
else
{
2015-08-17 22:28:01 +00:00
char * extension = strrchr ( * arg , ' . ' ) ;
if ( extension
& & ( stricmp ( extension , " .cdi " ) = = 0 | | stricmp ( extension , " .chd " ) = = 0
2019-07-09 21:52:19 +00:00
| | stricmp ( extension , " .gdi " ) = = 0 | | stricmp ( extension , " .cue " ) = = 0 ) )
2015-08-17 22:28:01 +00:00
{
2019-07-01 16:23:10 +00:00
INFO_LOG ( COMMON , " Using '%s' as cd image " , * arg ) ;
2015-08-17 22:28:01 +00:00
cfgSetVirtual ( " config " , " image " , * arg ) ;
}
else if ( extension & & stricmp ( extension , " .elf " ) = = 0 )
{
2019-07-01 16:23:10 +00:00
INFO_LOG ( COMMON , " Using '%s' as reios elf file " , * arg ) ;
2015-08-17 22:28:01 +00:00
cfgSetVirtual ( " config " , " reios.enabled " , " 1 " ) ;
cfgSetVirtual ( " reios " , " ElfFile " , * arg ) ;
}
else
{
2019-07-01 16:23:10 +00:00
INFO_LOG ( COMMON , " Using '%s' as rom " , * arg ) ;
2018-10-21 20:16:28 +00:00
cfgSetVirtual ( " config " , " image " , * arg ) ;
2015-08-17 22:28:01 +00:00
}
2013-12-19 17:10:14 +00:00
}
arg + + ;
cl - - ;
}
return false ;
2015-07-17 21:56:51 +00:00
}