diff options
Diffstat (limited to 'src/common/console.c')
-rw-r--r-- | src/common/console.c | 156 |
1 files changed, 99 insertions, 57 deletions
diff --git a/src/common/console.c b/src/common/console.c index 0be33e5c3..5b4dbeb1a 100644 --- a/src/common/console.c +++ b/src/common/console.c @@ -2,8 +2,8 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team - * Copyright (C) Athena Dev Teams + * Copyright (C) 2012-2020 Hercules Dev Team + * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,23 +23,21 @@ #include "config/core.h" // CONSOLE_INPUT, MAX_CONSOLE_INPUT #include "console.h" +#include "common/atomic.h" #include "common/cbasetypes.h" #include "common/core.h" +#include "common/ers.h" +#include "common/memmgr.h" +#include "common/mmo.h" +#include "common/mutex.h" #include "common/nullpo.h" #include "common/showmsg.h" +#include "common/spinlock.h" +#include "common/sql.h" +#include "common/strlib.h" #include "common/sysinfo.h" - -#ifndef MINICORE -# include "common/atomic.h" -# include "common/ers.h" -# include "common/memmgr.h" -# include "common/mutex.h" -# include "common/spinlock.h" -# include "common/sql.h" -# include "common/strlib.h" -# include "common/thread.h" -# include "common/timer.h" -#endif +#include "common/thread.h" +#include "common/timer.h" #include <stdio.h> #include <stdlib.h> @@ -53,12 +51,13 @@ # endif #endif -struct console_interface console_s; +static struct console_interface console_s; struct console_interface *console; #ifdef CONSOLE_INPUT -struct console_input_interface console_input_s; +static struct console_input_interface console_input_s; +static struct spin_lock console_ptlock_s; -struct { +static struct { char queue[CONSOLE_PARSE_SIZE][MAX_CONSOLE_INPUT]; unsigned short count; } cinput; @@ -67,7 +66,8 @@ struct { /*====================================== * CORE : Display title *--------------------------------------*/ -void display_title(void) { +static void display_title(void) +{ const char *vcstype = sysinfo->vcstype(); ShowMessage("\n"); @@ -91,19 +91,20 @@ void display_title(void) { ShowInfo("Compiled with %s\n", sysinfo->compiler()); ShowInfo("Compile Flags: %s\n", sysinfo->cflags()); ShowInfo("Timer Function Type: %s\n", sysinfo->time()); + ShowInfo("Packet version: %d " PACKETTYPE "\n", PACKETVER); } /** * Shows a license notice as per GNU GPL recommendation. */ -void display_gplnotice(void) +static void display_gplnotice(void) { - ShowInfo("Hercules, Copyright (C) 2012-2015, Hercules Dev Team and others.\n"); + ShowInfo("Hercules, Copyright (C) 2012-2018, Hercules Dev Team and others.\n"); ShowInfo("Licensed under the GNU General Public License, version 3 or later.\n"); } #ifdef CONSOLE_INPUT -int console_parse_key_pressed(void) +static int console_parse_key_pressed(void) { #ifdef WIN32 return _kbhit(); @@ -129,21 +130,27 @@ int console_parse_key_pressed(void) /** * Stops server **/ -CPCMD_C(exit,server) { - core->runflag = 0; +static CPCMD_C(exit, server) +{ + if (core->shutdown_callback != NULL) + core->shutdown_callback(); + else + core->runflag = CORE_ST_STOP; } /** * Displays ERS-related statistics (Entry Reusage System) **/ -CPCMD_C(ers_report,server) { +static CPCMD_C(ers_report, server) +{ ers_report(); } /** * Displays memory usage **/ -CPCMD_C(mem_report,server) { +static CPCMD_C(mem_report, server) +{ #ifdef USE_MEMMGR memmgr_report(line?atoi(line):0); #endif @@ -152,7 +159,7 @@ CPCMD_C(mem_report,server) { /** * Displays command list **/ -CPCMD(help) +static CPCMD(help) { int i; for (i = 0; i < VECTOR_LENGTH(console->input->command_list); i++) { @@ -170,7 +177,8 @@ CPCMD(help) * [Ind/Hercules] * Displays current malloc usage */ -CPCMD_C(malloc_usage,server) { +static CPCMD_C(malloc_usage, server) +{ unsigned int val = (unsigned int)iMalloc->usage(); ShowInfo("malloc_usage: %.2f MB\n",(double)(val)/1024); } @@ -179,7 +187,8 @@ CPCMD_C(malloc_usage,server) { * Skips an sql update * Usage: sql update skip UPDATE-FILE.sql **/ -CPCMD_C(skip,update) { +static CPCMD_C(skip, update) +{ if( !line ) { ShowDebug("usage example: sql update skip 2013-02-14--16-15.sql\n"); return; @@ -190,7 +199,7 @@ CPCMD_C(skip,update) { /** * Loads console commands list **/ -void console_load_defaults(void) +static void console_load_defaults(void) { /** * Defines a main category. @@ -303,15 +312,17 @@ void console_load_defaults(void) * @param name The command name. * @param func The command callback. */ -void console_parse_create(char *name, CParseFunc func) +static void console_parse_create(char *name, CParseFunc func) { int i; char *tok; char sublist[CP_CMD_LENGTH * 5]; struct CParseEntry *cmd; + nullpo_retv(name); safestrncpy(sublist, name, CP_CMD_LENGTH * 5); tok = strtok(sublist,":"); + nullpo_retv(tok); ARR_FIND(0, VECTOR_LENGTH(console->input->command_list), i, strcmpi(tok, VECTOR_INDEX(console->input->command_list, i)->cmd) == 0); @@ -359,10 +370,11 @@ void console_parse_create(char *name, CParseFunc func) * @param cmd The command entry. * @param depth The current tree depth (for display purposes). */ -void console_parse_list_subs(struct CParseEntry *cmd, unsigned char depth) +static void console_parse_list_subs(struct CParseEntry *cmd, unsigned char depth) { int i; char msg[CP_CMD_LENGTH * 2]; + nullpo_retv(cmd); Assert_retv(cmd->type == CPET_CATEGORY); for (i = 0; i < VECTOR_LENGTH(cmd->u.children); i++) { struct CParseEntry *child = VECTOR_INDEX(cmd->u.children, i); @@ -382,7 +394,7 @@ void console_parse_list_subs(struct CParseEntry *cmd, unsigned char depth) * * @param line The input line. */ -void console_parse_sub(char *line) +static void console_parse_sub(char *line) { struct CParseEntry *cmd; char bline[200]; @@ -390,8 +402,13 @@ void console_parse_sub(char *line) char sublist[CP_CMD_LENGTH * 5]; int i; + nullpo_retv(line); memcpy(bline, line, 200); tok = strtok(line, " "); + if (tok == NULL) { + // Ignore empty commands + return; + } ARR_FIND(0, VECTOR_LENGTH(console->input->command_list), i, strcmpi(tok, VECTOR_INDEX(console->input->command_list, i)->cmd) == 0); if (i == VECTOR_LENGTH(console->input->command_list)) { @@ -405,6 +422,12 @@ void console_parse_sub(char *line) if (cmd->type == CPET_FUNCTION) { tok = strtok(NULL, ""); + if (tok != NULL) { + while (tok[0] == ' ') + tok++; + if (tok[0] == '\0') + tok = NULL; + } cmd->u.func(tok); return; } @@ -432,6 +455,12 @@ void console_parse_sub(char *line) entry = VECTOR_INDEX(cmd->u.children, i); if (entry->type == CPET_FUNCTION) { tok = strtok(NULL, ""); + if (tok != NULL) { + while (tok[0] == ' ') + tok++; + if (tok[0] == '\0') + tok = NULL; + } entry->u.func(tok); return; } @@ -443,9 +472,12 @@ void console_parse_sub(char *line) } ShowError("Is only a category, type '"CL_WHITE"%s help"CL_RESET"' to list its subcommands\n",sublist); } -void console_parse(char* line) { + +static void console_parse(char *line) +{ int c, i = 0, len = MAX_CONSOLE_INPUT - 1;/* we leave room for the \0 :P */ + nullpo_retv(line); while( (c = fgetc(stdin)) != EOF ) { if( --len == 0 ) break; @@ -457,65 +489,73 @@ void console_parse(char* line) { line[i++] = '\0'; } -void *cThread_main(void *x) { + +static void *cThread_main(void *x) +{ while( console->input->ptstate ) {/* loopx */ if( console->input->key_pressed() ) { char input[MAX_CONSOLE_INPUT]; console->input->parse(input); if( input[0] != '\0' ) {/* did we get something? */ - EnterSpinLock(&console->input->ptlock); + EnterSpinLock(console->input->ptlock); if( cinput.count == CONSOLE_PARSE_SIZE ) { - LeaveSpinLock(&console->input->ptlock); + LeaveSpinLock(console->input->ptlock); continue;/* drop */ } safestrncpy(cinput.queue[cinput.count++],input,MAX_CONSOLE_INPUT); - LeaveSpinLock(&console->input->ptlock); + LeaveSpinLock(console->input->ptlock); } } - ramutex_lock( console->input->ptmutex ); - racond_wait( console->input->ptcond, console->input->ptmutex, -1 ); - ramutex_unlock( console->input->ptmutex ); + mutex->lock(console->input->ptmutex); + mutex->cond_wait(console->input->ptcond, console->input->ptmutex, -1); + mutex->unlock(console->input->ptmutex); } return NULL; } -int console_parse_timer(int tid, int64 tick, int id, intptr_t data) { + +static int console_parse_timer(int tid, int64 tick, int id, intptr_t data) +{ int i; - EnterSpinLock(&console->input->ptlock); + EnterSpinLock(console->input->ptlock); for(i = 0; i < cinput.count; i++) { console->input->parse_sub(cinput.queue[i]); } cinput.count = 0; - LeaveSpinLock(&console->input->ptlock); - racond_signal(console->input->ptcond); + LeaveSpinLock(console->input->ptlock); + mutex->cond_signal(console->input->ptcond); return 0; } -void console_parse_final(void) { + +static void console_parse_final(void) +{ if( console->input->ptstate ) { InterlockedDecrement(&console->input->ptstate); - racond_signal(console->input->ptcond); + mutex->cond_signal(console->input->ptcond); /* wait for thread to close */ - rathread_wait(console->input->pthread, NULL); + thread->wait(console->input->pthread, NULL); - racond_destroy(console->input->ptcond); - ramutex_destroy(console->input->ptmutex); + mutex->cond_destroy(console->input->ptcond); + mutex->destroy(console->input->ptmutex); } } -void console_parse_init(void) { + +static void console_parse_init(void) +{ cinput.count = 0; console->input->ptstate = 1; - InitializeSpinLock(&console->input->ptlock); + InitializeSpinLock(console->input->ptlock); - console->input->ptmutex = ramutex_create(); - console->input->ptcond = racond_create(); + console->input->ptmutex = mutex->create(); + console->input->ptcond = mutex->cond_create(); - if( (console->input->pthread = rathread_create(console->input->pthread_main, NULL)) == NULL ){ + if( (console->input->pthread = thread->create(console->input->pthread_main, NULL)) == NULL ){ ShowFatalError("console_parse_init: failed to spawn console_parse thread.\n"); exit(EXIT_FAILURE); } @@ -523,13 +563,14 @@ void console_parse_init(void) { timer->add_func_list(console->input->parse_timer, "console_parse_timer"); timer->add_interval(timer->gettick() + 1000, console->input->parse_timer, 0, 0, 500);/* start listening in 1s; re-try every 0.5s */ } -void console_setSQL(struct Sql *SQL_handle) + +static void console_setSQL(struct Sql *SQL_handle) { console->input->SQL = SQL_handle; } #endif /* CONSOLE_INPUT */ -void console_init(void) +static void console_init(void) { #ifdef CONSOLE_INPUT VECTOR_INIT(console->input->command_list); @@ -539,7 +580,7 @@ void console_init(void) #endif } -void console_final(void) +static void console_final(void) { #ifdef CONSOLE_INPUT console->input->parse_final(); @@ -563,6 +604,7 @@ void console_defaults(void) console->display_gplnotice = display_gplnotice; #ifdef CONSOLE_INPUT console->input = &console_input_s; + console->input->ptlock = &console_ptlock_s; console->input->parse_init = console_parse_init; console->input->parse_final = console_parse_final; console->input->parse_timer = console_parse_timer; |