From 637b48750f58f92c293153acee50838e4360ef96 Mon Sep 17 00:00:00 2001 From: Bradley Bell Date: Wed, 11 Feb 2015 22:34:18 -0800 Subject: [PATCH] miscellaneous fixes --- main/cli.c | 29 ++++++++++++++--------------- main/cmd.c | 16 ++++++++-------- main/cmd.h | 4 ++-- main/console.c | 1 + main/cvar.c | 16 +++++----------- main/cvar.h | 8 +++++--- 6 files changed, 35 insertions(+), 39 deletions(-) diff --git a/main/cli.c b/main/cli.c index 5ddc3a4d..cde8948f 100644 --- a/main/cli.c +++ b/main/cli.c @@ -24,13 +24,11 @@ #include "strutil.h" -#define get_msecs() approx_fsec_to_msec(timer_get_approx_seconds()) - #define CLI_HISTORY_MAX 128 // Cut the buffer line if it becomes longer than this #define CLI_CHARS_PER_LINE 128 -// Cursor blink frequency in ms -#define CLI_BLINK_RATE 500 +// Cursor blink interval +#define CLI_BLINK_RATE (F1_0/2) // Border in pixels from the most left to the first letter #define CLI_CHAR_BORDER 4 // Default prompt used at the commandline @@ -77,7 +75,7 @@ void cli_init() CommandScrollBack = 0; Prompt = d_strdup(CLI_DEFAULT_PROMPT); - memset(CommandLines, 0, CLI_HISTORY_MAX); + memset(CommandLines, 0, sizeof(CommandLines)); memset(Command, 0, CLI_CHARS_PER_LINE); memset(LCommand, 0, CLI_CHARS_PER_LINE); memset(RCommand, 0, CLI_CHARS_PER_LINE); @@ -112,16 +110,17 @@ void cli_draw(int y) int x, w, h, aw; float real_aw; int commandbuffer; - static unsigned int LastBlinkTime = 0; // Last time the cursor blinked - static int LastCursorPos = 0; // Last cursor position - static int Blink = 0; // Is the cursor currently blinking + fix cur_time = timer_get_fixed_seconds(); + static fix LastBlinkTime = 0; // Last time the cursor blinked + static int LastCursorPos = 0; // Last cursor position + static int Blink = 0; // Is the cursor currently blinking // Concatenate the left and right side to command strcpy(Command, LCommand); - strncat(Command, RCommand, strlen(RCommand)); + strncat(Command, RCommand, CLI_CHARS_PER_LINE - strlen(Command)); gr_get_string_size(Command, &w, &h, &aw); - if (w > 0 && strlen(Command)) + if (w > 0 && *Command) real_aw = (float)w/(float)strlen(Command); else real_aw = (float)aw; @@ -137,15 +136,15 @@ void cli_draw(int y) strcpy(VCommand, Prompt); // then add the visible part of the command - strncat(VCommand, &Command[Offset], strlen(&Command[Offset])); + strncat(VCommand, &Command[Offset], CLI_CHARS_PER_LINE - strlen(VCommand)); // now display the result gr_string(CLI_CHAR_BORDER, y-h, VCommand); // at last add the cursor // check if the blink period is over - if (get_msecs() > LastBlinkTime) { - LastBlinkTime = get_msecs() + CLI_BLINK_RATE; + if (cur_time > LastBlinkTime) { + LastBlinkTime = cur_time + CLI_BLINK_RATE; if(Blink) Blink = 0; else @@ -155,7 +154,7 @@ void cli_draw(int y) // check if cursor has moved - if yes display cursor anyway if (CursorPos != LastCursorPos) { LastCursorPos = CursorPos; - LastBlinkTime = get_msecs() + CLI_BLINK_RATE; + LastBlinkTime = cur_time + CLI_BLINK_RATE; Blink = 1; } @@ -196,7 +195,7 @@ void cli_execute(void) void cli_autocomplete(void) { int i, j; - char *command; + const char *command; command = cmd_complete(LCommand); diff --git a/main/cmd.c b/main/cmd.c index 4dc7f8c5..45f1615b 100644 --- a/main/cmd.c +++ b/main/cmd.c @@ -22,7 +22,7 @@ typedef struct cmd_s } cmd_t; /* The list of cmds */ -static cmd_t *cmd_list = NULL; +static cmd_t *cmd_list; #define ALIAS_NAME_MAX 32 @@ -34,7 +34,7 @@ typedef struct cmd_alias_s } cmd_alias_t; /* The list of aliases */ -static cmd_alias_t *cmd_alias_list = NULL; +static cmd_alias_t *cmd_alias_list; /* add a new console command */ @@ -71,8 +71,8 @@ typedef struct cmd_queue_s } cmd_queue_t; /* The list of commands to be executed */ -static cmd_queue_t *cmd_queue_head = NULL; -static cmd_queue_t *cmd_queue_tail = NULL; +static cmd_queue_t *cmd_queue_head; +static cmd_queue_t *cmd_queue_tail; void cvar_cmd_set(int argc, char **argv); @@ -124,8 +124,8 @@ void cmd_parse(char *input) Assert(input != NULL); /* Strip leading spaces */ - for (i=0; isspace(input[i]); i++) ; - strncpy( buffer, &input[i], CMD_MAX_LENGTH ); + while( isspace(*input) ) { ++input; } + strncpy( buffer, input, CMD_MAX_LENGTH ); //printf("lead strip \"%s\"\n",buffer); l = (int)strlen(buffer); @@ -272,7 +272,7 @@ void cmd_enqueue(int insert, char *input) } } -void cmd_enqueuef(int insert, char *fmt, ...) +void cmd_enqueuef(int insert, const char *fmt, ...) { va_list arglist; char buf[CMD_MAX_LENGTH]; @@ -286,7 +286,7 @@ void cmd_enqueuef(int insert, char *fmt, ...) /* Attempt to autocomplete an input string */ -char *cmd_complete(char *input) +const char *cmd_complete(char *input) { cmd_t *ptr; cmd_alias_t *aptr; diff --git a/main/cmd.h b/main/cmd.h index 3d286e3a..f96d5555 100644 --- a/main/cmd.h +++ b/main/cmd.h @@ -11,7 +11,7 @@ void cmd_init(void); /* Add some commands to the queue to be executed */ void cmd_enqueue(int insert, char *input); -void cmd_enqueuef(int insert, char *fmt, ...); +void cmd_enqueuef(int insert, const char *fmt, ...); #define cmd_append(input) cmd_enqueue(0, (input)) #define cmd_appendf(...) cmd_enqueuef(0, __VA_ARGS__) #define cmd_insert(input) cmd_enqueue(1, (input)) @@ -24,7 +24,7 @@ int cmd_queue_process(void); void cmd_queue_flush(void); /* Attempt to autocomplete an input string */ -char *cmd_complete(char *input); +const char *cmd_complete(char *input); typedef void (*cmd_handler_t)(int argc, char *argv[]); diff --git a/main/console.c b/main/console.c index 1ce7ffa7..5a9ebe5f 100644 --- a/main/console.c +++ b/main/console.c @@ -294,6 +294,7 @@ void con_init() cli_init(); cmd_init(); + cvar_init(); /* Initialise the cvars */ cvar_registervariable (&con_threshold); diff --git a/main/cvar.c b/main/cvar.c index ba4ee8b9..0204f599 100644 --- a/main/cvar.c +++ b/main/cvar.c @@ -18,9 +18,7 @@ /* The list of cvars */ -cvar_t *cvar_list = NULL; - -int cvar_initialized = 0; +cvar_t *cvar_list; void cvar_free(void) @@ -79,7 +77,6 @@ void cvar_init(void) "set\n" " show value of all variables\n"); atexit(cvar_free); - cvar_initialized = 1; } @@ -95,7 +92,7 @@ cvar_t *cvar_find(char *cvar_name) } -char *cvar_complete(char *text) +const char *cvar_complete(char *text) { cvar_t *ptr; int len = (int)strlen(text); @@ -112,20 +109,17 @@ char *cvar_complete(char *text) /* Register a cvar */ -void cvar_registervariable (cvar_t *cvar) +void cvar_registervariable(cvar_t *cvar) { char *stringval; cvar_t *ptr; - if (!cvar_initialized) - cvar_init(); - Assert(cvar != NULL); stringval = cvar->string; cvar->string = d_strdup(stringval); - cvar->value = fl2f(strtod(cvar->string, (char **) NULL)); + cvar->value = fl2f(strtod(cvar->string, NULL)); cvar->intval = (int)strtol(cvar->string, NULL, 10); cvar->next = NULL; @@ -155,7 +149,7 @@ void cvar_set_cvar(cvar_t *cvar, char *value) } -void cvar_set_cvarf(cvar_t *cvar, char *fmt, ...) +void cvar_set_cvarf(cvar_t *cvar, const char *fmt, ...) { va_list arglist; char buf[CVAR_MAX_LENGTH]; diff --git a/main/cvar.h b/main/cvar.h index 739c0b9f..41013a30 100644 --- a/main/cvar.h +++ b/main/cvar.h @@ -18,12 +18,14 @@ typedef struct cvar_s } cvar_t; +void cvar_init(void); + /* Register a CVar with the name and string and optionally archive elements set */ -void cvar_registervariable (cvar_t *cvar); +void cvar_registervariable(cvar_t *cvar); /* Set a CVar's value */ void cvar_set_cvar(cvar_t *cvar, char *value); -void cvar_set_cvarf(cvar_t *cvar, char *fmt, ...); +void cvar_set_cvarf(cvar_t *cvar, const char *fmt, ...); #define cvar_setint(cvar, x) cvar_set_cvarf((cvar), "%d", (x)) #define cvar_setfl(cvar, x) cvar_set_cvarf((cvar), "%f", (x)) #define cvar_toggle(cvar) cvar_setint((cvar), !(cvar)->intval) @@ -35,7 +37,7 @@ void cvar_set(char *cvar_name, char *value); cvar_t *cvar_find(char *cvar_name); /* Try to autocomplete a cvar name */ -char *cvar_complete(char *text); +const char *cvar_complete(char *text); /* Get a CVar's value */ fix cvar(char *cvar_name); -- 2.39.2