Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

verify command line options #650

Merged
merged 26 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ include(CPack)
add_subdirectory(data)
add_subdirectory(opl)
add_subdirectory(textscreen)
add_subdirectory(man)
add_subdirectory(src)
add_subdirectory(toolsrc)
add_subdirectory(setup)
add_subdirectory(man)
10 changes: 10 additions & 0 deletions man/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@

if(Python3_EXECUTABLE)
add_custom_target(GenParams
COMMAND "${Python3_EXECUTABLE}" docgen -a
"${PROJECT_SOURCE_DIR}/src" > "${PROJECT_SOURCE_DIR}/src/params.h"
DEPENDS "${PROJECT_SOURCE_DIR}/src"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
endif()

configure_file(WoofInstall.cmake.in WoofInstall.cmake ESCAPE_QUOTES @ONLY)
install(SCRIPT "${CMAKE_CURRENT_BINARY_DIR}/WoofInstall.cmake")

Expand Down
49 changes: 48 additions & 1 deletion man/docgen
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ class Category:

return result

def carray_output(self):
result = ""

self.params.sort()

for p in self.params:
if p.should_show() and not p.args:
result += "\"" + p.carray_output(0) + "\",\n"

return result

def carray_output_args(self):
result = ""

self.params.sort()

for p in self.params:
if p.should_show() and p.args:
result += "\"" + p.carray_output(0) + "\",\n"

return result

def manpage_output(self):
result = ".SH " + self.description.upper() + "\n"

Expand Down Expand Up @@ -336,6 +358,12 @@ class Parameter:

return result

def carray_output(self, w):

result = self.name

return result

# Read list of wiki pages

def read_wikipages():
Expand Down Expand Up @@ -531,6 +559,22 @@ def completion_output(targets, substs, template_file):

print_template(template_file, substs, content)

def carray_output(targets, substs, template):

content = "\nstatic const char *params[] = {\n"

for t in targets:
content += t.carray_output()

content += "};\n\nstatic const char *params_with_args[] = {\n"

for t in targets:
content += t.carray_output_args()

content += "};\n"

print(content)

def usage():
print("Usage: %s [-V] [-c tag] -s project_name [ -z shortname ] ( -M | -m | -w | -p ) <dir>..." \
% sys.argv[0])
Expand All @@ -544,13 +588,14 @@ def usage():
print(" -w : Wikitext output")
print(" -p : Plaintext output")
print(" -b : Bash-Completion output")
print(" -a : C array output")
print(" -V : Don't show Vanilla Doom options")
print(" -g : Only document options for specified game.")
sys.exit(0)

# Parse command line

opts, args = getopt.getopt(sys.argv[1:], "s:z:M:m:wp:b:c:g:V")
opts, args = getopt.getopt(sys.argv[1:], "s:z:M:m:wp:b:ap:c:g:V")

output_function = None
template = None
Expand All @@ -577,6 +622,8 @@ for opt in opts:
elif opt[0] == "-b":
output_function = completion_output
template = opt[1]
elif opt[0] == "-a":
output_function = carray_output
elif opt[0] == "-V":
show_vanilla_options = False
elif opt[0] == "-c":
Expand Down
9 changes: 6 additions & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ set(WOOF_SOURCES
z_zone.c z_zone.h
../miniz/miniz.c ../miniz/miniz.h)

list(APPEND WOOF_LIBRARIES textscreen)

if(WIN32)
list(APPEND
WOOF_SOURCES
Expand Down Expand Up @@ -156,13 +154,18 @@ add_executable(woof WIN32 ${WOOF_SOURCES})
target_woof_settings(woof)
target_include_directories(woof PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/../")

if(Python3_EXECUTABLE)
add_dependencies(woof GenParams)
target_compile_definitions(woof PRIVATE HAVE_GEN_PARAMS)
endif()

if (FluidSynth_FOUND)
list(APPEND WOOF_LIBRARIES FluidSynth::FluidSynth)
target_compile_definitions(woof PRIVATE HAVE_FLUIDSYNTH)
endif()

target_link_libraries(woof PRIVATE ${WOOF_LIBRARIES}
SDL2::SDL2 SDL2::mixer SDL2::net opl)
SDL2::SDL2 SDL2::mixer SDL2::net opl textscreen)

if(MSVC)
# MSVC tries to supply a default manifest and complains when it finds ours
Expand Down
41 changes: 31 additions & 10 deletions src/d_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,10 @@ void D_DoomMain(void)

I_AtExitPrio(I_ErrorMsg, true, "I_ErrorMsg", exit_priority_verylast);

#if defined(HAVE_GEN_PARAMS)
M_CheckCommandLine();
#endif

dsdh_InitTables();

#if defined(_WIN32)
Expand Down Expand Up @@ -2133,10 +2137,20 @@ void D_DoomMain(void)
//

if ((p = M_CheckParm ("-skill")) && p < myargc-1)
{
startskill = myargv[p+1][0]-'1';
autostart = true;
}
{
boolean check = M_ParmStrToInt(myargv[p+1], (int *)&startskill);
startskill--;
if (check && startskill >= sk_none && startskill <= sk_nightmare)
{
autostart = true;
}
else
{
I_Error("Wrong -skill parameter '%s', valid values are 1-5 "
"(1: easiest, 5: hardest). "
"A skill of 0 disables all monsters.", myargv[p+1]);
}
}

//!
// @category game
Expand All @@ -2148,9 +2162,16 @@ void D_DoomMain(void)

if ((p = M_CheckParm ("-episode")) && p < myargc-1)
{
startepisode = myargv[p+1][0]-'0';
startmap = 1;
autostart = true;
if (M_ParmStrToInt(myargv[p+1], &startepisode) &&
startepisode >= 1 && startepisode <= 4)
{
startmap = 1;
autostart = true;
}
else
{
I_Error("Wrong -episode parameter '%s', should be 1-4", myargv[p+1]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we have any episode from 0 to 9 now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, in Woof we even support episodes 0-99.
@fabiangreffrath BTW -warp 9999 doesn't currently work, only -warp 99 99. Not sure how to validate -warp 😄

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's okay as it is. No algorithm can know if -warp 999 means to warp to E99M9 or E9M99. 😉

}
}

//!
Expand All @@ -2163,9 +2184,9 @@ void D_DoomMain(void)

if ((p = M_CheckParm ("-timer")) && p < myargc-1 && deathmatch)
{
int time = atoi(myargv[p+1]);
timelimit = time;
printf("Levels will end after %d minute%s.\n", time, time>1 ? "s" : "");
if (!M_ParmStrToInt(myargv[p+1], &timelimit))
I_Error("Wrong -timer parameter '%s', valid value n minutes", myargv[p+1]);
printf("Levels will end after %d minute%s.\n", timelimit, timelimit>1 ? "s" : "");
}

//!
Expand Down
3 changes: 3 additions & 0 deletions src/g_game.c
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,9 @@ void G_ReloadDefaults(void)
int l = G_GetNamedComplevel(myargv[i+1]);
if (l > -1)
demo_version = l;
else
I_Error("Wrong -complevel parameter '%s', "
"valid values are 'vanilla', 'boom', 'mbf', 'mbf21'", myargv[i+1]);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/i_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ int main(int argc, char **argv)
myargc = argc;
myargv = argv;

// print the program version and exit
//!
//
// Print the program version and exit.
//

if (M_ParmExists("-version") || M_ParmExists("--version"))
{
puts(PROJECT_STRING);
Expand Down
85 changes: 85 additions & 0 deletions src/m_argv.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
//-----------------------------------------------------------------------------

#include "doomtype.h"
#include <stdio.h>
#include <string.h>

int myargc;
Expand Down Expand Up @@ -69,6 +70,90 @@ boolean M_ParmExists(const char *check)
return M_CheckParm(check) != 0;
}

boolean M_ParmStrToInt(const char *str, int *result)
{
return sscanf(str, " %d", result) == 1;
}

#if defined(HAVE_GEN_PARAMS)
#include "params.h"
#include "i_system.h"

static int CheckArgs(int p, int num_args)
{
int i;

++p;

for (i = p; i < p + num_args && i < myargc; ++i)
{
if (myargv[i][0] == '-')
break;
}

if (i > p)
return i;

return 0;
}

void M_CheckCommandLine(void)
{
int p = 1;

while (p < myargc)
{
int i;
int args = -1;

for (i = 0; i < arrlen(params_with_args); ++i)
{
if (!strcasecmp(myargv[p], "-file") ||
!strcasecmp(myargv[p], "-deh") ||
!strcasecmp(myargv[p], "-bex"))
{
args = myargc;
break;
}
else if (!strcasecmp(myargv[p], "-warp") ||
!strcasecmp(myargv[p], "-recordfrom"))
{
args = 2;
break;
}
else if (!strcasecmp(myargv[p], params_with_args[i]))
{
args = 1;
break;
}
}

if (args > 0)
{
int check = CheckArgs(p, args);

if (!check)
I_Error("No parameter for %s", myargv[p]);
else
p = check;

continue;
}

for (i = 0; i < arrlen(params); ++i)
{
if (!strcasecmp(myargv[p], params[i]))
break;
}

if (i == arrlen(params))
I_Error("No such option %s", myargv[p]);

++p;
}
}
#endif

//----------------------------------------------------------------------------
//
// $Log: m_argv.c,v $
Expand Down
6 changes: 6 additions & 0 deletions src/m_argv.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ int M_CheckParmWithArgs(const char *check, int num_args);
// line arguments, false if not.
boolean M_ParmExists(const char *check);

#if defined(HAVE_GEN_PARAMS)
boolean M_CheckCommandLine(void);
#endif

boolean M_ParmStrToInt(const char *str, int *result);

#endif

//----------------------------------------------------------------------------
Expand Down