Improve OSD_Dispatch() a little

git-svn-id: https://svn.eduke32.com/eduke32@7143 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2018-11-18 18:05:10 +00:00
parent 9ab8ebce20
commit f60c353810
3 changed files with 28 additions and 33 deletions

View file

@ -114,7 +114,7 @@ extern int32_t gridlock;
extern int32_t g_maxCacheSize; extern int32_t g_maxCacheSize;
extern int32_t g_lazy_tileselector; extern int32_t g_lazy_tileselector;
extern int32_t m32_osd_tryscript; extern bool m32_osd_tryscript;
extern int32_t showheightindicators; extern int32_t showheightindicators;
extern int32_t showambiencesounds; extern int32_t showambiencesounds;

View file

@ -278,7 +278,7 @@ void OSD_Puts(const char *str);
void OSD_DispatchQueued(void); void OSD_DispatchQueued(void);
// executes a string // executes a string
int32_t OSD_Dispatch(const char *cmd); void OSD_Dispatch(const char *cmd);
// registers a function // registers a function
// name = name of the function // name = name of the function

View file

@ -63,7 +63,7 @@ static int32_t (*_getcolumnwidth)(int32_t) = _internal_getcolumnwidth;
static int32_t (*_getrowheight)(int32_t) = _internal_getrowheight; static int32_t (*_getrowheight)(int32_t) = _internal_getrowheight;
static hashtable_t h_cvars = { OSDMAXSYMBOLS<<1, NULL }; static hashtable_t h_cvars = { OSDMAXSYMBOLS<<1, NULL };
int32_t m32_osd_tryscript=0; // whether to try executing m32script on unkown command in the osd bool m32_osd_tryscript = false; // whether to try executing m32script on unkown command in the osd
void OSD_RegisterCvar(osdcvardata_t * const cvar, int32_t (*func)(osdfuncparm_t const * const)) void OSD_RegisterCvar(osdcvardata_t * const cvar, int32_t (*func)(osdfuncparm_t const * const))
{ {
@ -1711,8 +1711,7 @@ void OSD_DispatchQueued(void)
// //
// OSD_Dispatch() -- Executes a command string // OSD_Dispatch() -- Executes a command string
// //
static char *osd_strtoken(char *s, char **ptrptr, int *restart)
static char *strtoken(char *s, char **ptrptr, int32_t *restart)
{ {
*restart = 0; *restart = 0;
if (!ptrptr) return NULL; if (!ptrptr) return NULL;
@ -1806,69 +1805,67 @@ static char *strtoken(char *s, char **ptrptr, int32_t *restart)
} }
#define MAXPARMS 256 #define MAXPARMS 256
int32_t OSD_Dispatch(const char *cmd) void OSD_Dispatch(const char *cmd)
{ {
char *workbuf, *wtp, *state; char *workbuf, *wtp, *state;
char const *pszParam; char const *token;
int32_t restart = 0; int32_t restart = 0;
workbuf = state = Xstrdup(cmd); workbuf = state = Xstrdup(cmd);
do do
{ {
if ((pszParam = strtoken(state, &wtp, &restart)) == NULL) if ((token = osd_strtoken(state, &wtp, &restart)) == NULL)
{ {
state = wtp; state = wtp;
continue; continue;
} }
// cheap hack for comments in cfgs // cheap hack for comments in cfgs
if (pszParam[0] == '/' && pszParam[1] == '/') if (token[0] == '/' && token[1] == '/')
{ {
Bfree(workbuf); Bfree(workbuf);
return -1; return;
} }
osdsymbol_t const * const pSymbol = osd_findexactsymbol(pszParam); auto const *symbol = osd_findexactsymbol(token);
if (pSymbol == NULL) if (symbol == NULL)
{ {
static char const s_gamefunc_[] = "gamefunc_"; static char const s_gamefunc_[] = "gamefunc_";
size_t const strlen_gamefunc_ = ARRAY_SIZE(s_gamefunc_) - 1; size_t constexpr strlen_gamefunc_ = ARRAY_SIZE(s_gamefunc_) - 1;
size_t const strlen_token = Bstrlen(pszParam); size_t const strlen_token = Bstrlen(token);
if ((strlen_gamefunc_ >= strlen_token || Bstrncmp(pszParam, s_gamefunc_, strlen_gamefunc_)) && !m32_osd_tryscript) if ((strlen_gamefunc_ >= strlen_token || Bstrncmp(token, s_gamefunc_, strlen_gamefunc_)) && !m32_osd_tryscript)
OSD_Printf("%s\"%s\" is not a valid command or cvar\n", osd->draw.highlight, pszParam); OSD_Printf("%s\"%s\" is not a valid command or cvar\n", osd->draw.highlight, token);
else if (m32_osd_tryscript) else if (m32_osd_tryscript)
M32RunScript(cmd); M32RunScript(cmd);
Bfree(workbuf); Bfree(workbuf);
return -1; return;
} }
char const * const pszName = pszParam; auto *name = token;
char const *parms[MAXPARMS]; char const *parms[MAXPARMS] = {};
int numparms = 0;
Bmemset(parms, 0, sizeof(parms));
int32_t numparms = 0;
while (wtp && !restart) while (wtp && !restart)
{ {
pszParam = strtoken(NULL, &wtp, &restart); token = osd_strtoken(NULL, &wtp, &restart);
if (pszParam && numparms < MAXPARMS) parms[numparms++] = pszParam; if (token && numparms < MAXPARMS) parms[numparms++] = token;
} }
osdfuncparm_t const ofp ={ numparms, pszName, (const char **) parms, cmd }; osdfuncparm_t const ofp = { numparms, name, parms, cmd };
if (pSymbol->func == OSD_ALIAS) if (symbol->func == OSD_ALIAS)
OSD_Dispatch(pSymbol->help); OSD_Dispatch(symbol->help);
else if (pSymbol->func != OSD_UNALIASED) else if (symbol->func != OSD_UNALIASED)
{ {
switch (pSymbol->func(&ofp)) switch (symbol->func(&ofp))
{ {
default:
case OSDCMD_OK: break; case OSDCMD_OK: break;
case OSDCMD_SHOWHELP: OSD_Printf("%s\n", pSymbol->help); break; case OSDCMD_SHOWHELP: OSD_Printf("%s\n", symbol->help); break;
} }
} }
@ -1877,8 +1874,6 @@ int32_t OSD_Dispatch(const char *cmd)
while (wtp && restart); while (wtp && restart);
Bfree(workbuf); Bfree(workbuf);
return 0;
} }