diff options
Diffstat (limited to 'src/prompt.c')
-rw-r--r-- | src/prompt.c | 382 |
1 files changed, 197 insertions, 185 deletions
diff --git a/src/prompt.c b/src/prompt.c index 2bb7a7e..04e4350 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -8,7 +8,7 @@ of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. @@ -30,8 +30,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. static cvar_t *com_completion_mode; static cvar_t *com_completion_treshold; -static void Prompt_ShowMatches( commandPrompt_t *prompt, char **matches, - int start, int end ) +static void Prompt_ShowMatches(commandPrompt_t *prompt, char **matches, + int start, int end) { int count = end - start; int numCols = 7, numLines; @@ -43,49 +43,49 @@ static void Prompt_ShowMatches( commandPrompt_t *prompt, char **matches, // determine number of columns needed do { numCols--; - numLines = ceil( ( float )count / numCols ); + numLines = ceil((float)count / numCols); total = 0; - for( i = 0; i < numCols; i++ ) { + for (i = 0; i < numCols; i++) { k = start + numLines * i; - if( k >= end ) { + if (k >= end) { break; } maxlen = 0; - for( j = k; j < k + numLines && j < end; j++ ) { - len = strlen( matches[j] ); - if( maxlen < len ) { + for (j = k; j < k + numLines && j < end; j++) { + len = strlen(matches[j]); + if (maxlen < len) { maxlen = len; } } maxlen += 2; // account for intercolumn spaces - if( maxlen > prompt->widthInChars ) { + if (maxlen > prompt->widthInChars) { maxlen = prompt->widthInChars; } colwidths[i] = maxlen; total += maxlen; } - if( total < prompt->widthInChars ) { + if (total < prompt->widthInChars) { break; // this number of columns does fit } - } while( numCols > 1 ); + } while (numCols > 1); - for( i = 0; i < numLines; i++ ) { - for( j = 0; j < numCols; j++ ) { + for (i = 0; i < numLines; i++) { + for (j = 0; j < numCols; j++) { k = start + j * numLines + i; - if( k >= end ) { + if (k >= end) { break; } match = matches[k]; - prompt->printf( "%s", match ); - len = strlen( match ); - if( len < colwidths[j] ) { + prompt->printf("%s", match); + len = strlen(match); + if (len < colwidths[j]) { // pad with spaces - for( k = 0; k < colwidths[j] - len; k++ ) { - prompt->printf( " " ); + for (k = 0; k < colwidths[j] - len; k++) { + prompt->printf(" "); } } } - prompt->printf( "\n" ); + prompt->printf("\n"); } } @@ -94,84 +94,87 @@ static void Prompt_ShowIndividualMatches( char **matches, int numCommands, int numAliases, - int numCvars ) + int numCvars) { int offset = 0; - if( numCommands ) { - qsort( matches + offset, numCommands, sizeof( matches[0] ), SortStrcmp ); + if (numCommands) { + qsort(matches + offset, numCommands, sizeof(matches[0]), SortStrcmp); - prompt->printf( "\n%i possible command%s:\n", - numCommands, numCommands != 1 ? "s" : "" ); + prompt->printf("\n%i possible command%s:\n", + numCommands, numCommands != 1 ? "s" : ""); - Prompt_ShowMatches( prompt, matches, offset, offset + numCommands ); + Prompt_ShowMatches(prompt, matches, offset, offset + numCommands); offset += numCommands; } - if( numCvars ) { - qsort( matches + offset, numCvars, sizeof( matches[0] ), SortStrcmp ); + if (numCvars) { + qsort(matches + offset, numCvars, sizeof(matches[0]), SortStrcmp); - prompt->printf( "\n%i possible variable%s:\n", - numCvars, numCvars != 1 ? "s" : "" ); + prompt->printf("\n%i possible variable%s:\n", + numCvars, numCvars != 1 ? "s" : ""); - Prompt_ShowMatches( prompt, matches, offset, offset + numCvars ); + Prompt_ShowMatches(prompt, matches, offset, offset + numCvars); offset += numCvars; } - if( numAliases ) { - qsort( matches + offset, numAliases, sizeof( matches[0] ), SortStrcmp ); + if (numAliases) { + qsort(matches + offset, numAliases, sizeof(matches[0]), SortStrcmp); - prompt->printf( "\n%i possible alias%s:\n", - numAliases, numAliases != 1 ? "es" : "" ); + prompt->printf("\n%i possible alias%s:\n", + numAliases, numAliases != 1 ? "es" : ""); - Prompt_ShowMatches( prompt, matches, offset, offset + numAliases ); + Prompt_ShowMatches(prompt, matches, offset, offset + numAliases); offset += numAliases; } } -static qboolean find_dup( genctx_t *ctx, const char *s ) { +static qboolean find_dup(genctx_t *ctx, const char *s) +{ int i, r; - for( i = 0; i < ctx->count; i++ ) { - if( ctx->ignorecase ) - r = Q_strcasecmp( ctx->matches[i], s ); + for (i = 0; i < ctx->count; i++) { + if (ctx->ignorecase) + r = Q_strcasecmp(ctx->matches[i], s); else - r = strcmp( ctx->matches[i], s ); + r = strcmp(ctx->matches[i], s); - if( !r ) + if (!r) return qtrue; } return qfalse; } -qboolean Prompt_AddMatch( genctx_t *ctx, const char *s ) { +qboolean Prompt_AddMatch(genctx_t *ctx, const char *s) +{ int r; - if( ctx->count >= ctx->size ) + if (ctx->count >= ctx->size) return qfalse; - if( ctx->ignorecase ) - r = Q_strncasecmp( ctx->partial, s, ctx->length ); + if (ctx->ignorecase) + r = Q_strncasecmp(ctx->partial, s, ctx->length); else - r = strncmp( ctx->partial, s, ctx->length ); + r = strncmp(ctx->partial, s, ctx->length); - if( r ) + if (r) return qtrue; - if( ctx->ignoredups && find_dup( ctx, s ) ) + if (ctx->ignoredups && find_dup(ctx, s)) return qtrue; - ctx->matches[ctx->count++] = Z_CopyString( s ); + ctx->matches[ctx->count++] = Z_CopyString(s); return qtrue; } -static qboolean needs_quotes( const char *s ) { +static qboolean needs_quotes(const char *s) +{ int c; - while( *s ) { + while (*s) { c = *s++; - if( c == '$' || c == ';' || !Q_isgraph( c ) ) { + if (c == '$' || c == ';' || !Q_isgraph(c)) { return qtrue; } } @@ -184,7 +187,8 @@ static qboolean needs_quotes( const char *s ) { Prompt_CompleteCommand ==================== */ -void Prompt_CompleteCommand( commandPrompt_t *prompt, qboolean backslash ) { +void Prompt_CompleteCommand(commandPrompt_t *prompt, qboolean backslash) +{ inputField_t *inputLine = &prompt->inputLine; char *text, *partial, *s; int i, argc, currentArg, argnum; @@ -199,9 +203,9 @@ void Prompt_CompleteCommand( commandPrompt_t *prompt, qboolean backslash ) { pos = inputLine->cursorPos; // prepend backslash if missing - if( backslash ) { - if( inputLine->text[0] != '\\' && inputLine->text[0] != '/' ) { - memmove( inputLine->text + 1, inputLine->text, size - 1 ); + if (backslash) { + if (inputLine->text[0] != '\\' && inputLine->text[0] != '/') { + memmove(inputLine->text + 1, inputLine->text, size - 1); inputLine->text[0] = '\\'; } text++; @@ -210,94 +214,94 @@ void Prompt_CompleteCommand( commandPrompt_t *prompt, qboolean backslash ) { } // parse the input line into tokens - Cmd_TokenizeString( text, qfalse ); + Cmd_TokenizeString(text, qfalse); argc = Cmd_Argc(); // determine absolute argument number to be completed - currentArg = Cmd_FindArgForOffset( pos ); - if( currentArg == argc - 1 && Cmd_WhiteSpaceTail() ) { + currentArg = Cmd_FindArgForOffset(pos); + if (currentArg == argc - 1 && Cmd_WhiteSpaceTail()) { // start completing new argument if command line has trailing whitespace currentArg++; } // determine relative argument number to be completed argnum = 0; - for( i = 0; i < currentArg; i++ ) { - s = Cmd_Argv( i ); + for (i = 0; i < currentArg; i++) { + s = Cmd_Argv(i); argnum++; - if( *s == ';' ) { + if (*s == ';') { // semicolon starts a new command argnum = 0; } } // get the partial argument string to be completed - partial = Cmd_Argv( currentArg ); - if( *partial == ';' ) { + partial = Cmd_Argv(currentArg); + if (*partial == ';') { // semicolon starts a new command currentArg++; - partial = Cmd_Argv( currentArg ); + partial = Cmd_Argv(currentArg); argnum = 0; } // generate matches - memset( &ctx, 0, sizeof( ctx ) ); + memset(&ctx, 0, sizeof(ctx)); ctx.partial = partial; - ctx.length = strlen( partial ); + ctx.length = strlen(partial); ctx.argnum = currentArg; ctx.matches = matches; ctx.size = MAX_MATCHES; - if( argnum ) { + if (argnum) { // complete a command/cvar argument - Com_Generic_c( &ctx, argnum ); + Com_Generic_c(&ctx, argnum); numCommands = numCvars = numAliases = 0; } else { // complete a command/cvar/alias name - Cmd_Command_g( &ctx ); + Cmd_Command_g(&ctx); numCommands = ctx.count; - Cvar_Variable_g( &ctx ); + Cvar_Variable_g(&ctx); numCvars = ctx.count - numCommands; - Cmd_Alias_g( &ctx ); + Cmd_Alias_g(&ctx); numAliases = ctx.count - numCvars - numCommands; } - if( !ctx.count ) { - pos = strlen( inputLine->text ); + if (!ctx.count) { + pos = strlen(inputLine->text); prompt->tooMany = qfalse; goto finish2; // nothing found } - pos = Cmd_ArgOffset( currentArg ); + pos = Cmd_ArgOffset(currentArg); text += pos; size -= pos; // append whitespace since Cmd_TokenizeString eats it - if( currentArg == argc && Cmd_WhiteSpaceTail() ) { + if (currentArg == argc && Cmd_WhiteSpaceTail()) { *text++ = ' '; pos++; size--; } - if( ctx.count == 1 ) { + if (ctx.count == 1) { // we have finished completion! - s = Cmd_RawArgsFrom( currentArg + 1 ); - if( needs_quotes( matches[0] ) ) { - pos += Q_concat( text, size, "\"", matches[0], "\" ", s, NULL ); + s = Cmd_RawArgsFrom(currentArg + 1); + if (needs_quotes(matches[0])) { + pos += Q_concat(text, size, "\"", matches[0], "\" ", s, NULL); } else { - pos += Q_concat( text, size, matches[0], " ", s, NULL ); + pos += Q_concat(text, size, matches[0], " ", s, NULL); } pos++; prompt->tooMany = qfalse; goto finish1; } - if( ctx.count > com_completion_treshold->integer && !prompt->tooMany ) { - prompt->printf( "Press TAB again to display all %d possibilities.\n", ctx.count ); - pos = strlen( inputLine->text ); + if (ctx.count > com_completion_treshold->integer && !prompt->tooMany) { + prompt->printf("Press TAB again to display all %d possibilities.\n", ctx.count); + pos = strlen(inputLine->text); prompt->tooMany = qtrue; goto finish1; } @@ -305,101 +309,102 @@ void Prompt_CompleteCommand( commandPrompt_t *prompt, qboolean backslash ) { prompt->tooMany = qfalse; // sort matches alphabethically - for( i = 0; i < ctx.count; i++ ) { + for (i = 0; i < ctx.count; i++) { sortedMatches[i] = matches[i]; } - qsort( sortedMatches, ctx.count, sizeof( sortedMatches[0] ), - ctx.ignorecase ? SortStricmp : SortStrcmp ); + qsort(sortedMatches, ctx.count, sizeof(sortedMatches[0]), + ctx.ignorecase ? SortStricmp : SortStrcmp); // copy matching part first = sortedMatches[0]; - last = sortedMatches[ ctx.count - 1 ]; + last = sortedMatches[ctx.count - 1]; len = 0; do { - if( *first != *last ) { - if( !ctx.ignorecase || Q_tolower( *first ) != Q_tolower( *last ) ) { + if (*first != *last) { + if (!ctx.ignorecase || Q_tolower(*first) != Q_tolower(*last)) { break; } } text[len++] = *first; - if( len == size - 1 ) { + if (len == size - 1) { break; } first++; last++; - } while( *first ); + } while (*first); text[len] = 0; pos += len; size -= len; // copy trailing arguments - if( currentArg + 1 < argc ) { - s = Cmd_RawArgsFrom( currentArg + 1 ); - pos += Q_concat( text + len, size, " ", s, NULL ); + if (currentArg + 1 < argc) { + s = Cmd_RawArgsFrom(currentArg + 1); + pos += Q_concat(text + len, size, " ", s, NULL); } pos++; - prompt->printf( "]\\%s\n", Cmd_ArgsFrom( 0 ) ); - if( argnum ) { + prompt->printf("]\\%s\n", Cmd_ArgsFrom(0)); + if (argnum) { goto multi; } - - switch( com_completion_mode->integer ) { + + switch (com_completion_mode->integer) { case 0: // print in solid list - for( i = 0 ; i < ctx.count; i++ ) { - prompt->printf( "%s\n", sortedMatches[i] ); + for (i = 0; i < ctx.count; i++) { + prompt->printf("%s\n", sortedMatches[i]); } break; case 1: - multi: +multi: // print in multiple columns - Prompt_ShowMatches( prompt, sortedMatches, 0, ctx.count ); + Prompt_ShowMatches(prompt, sortedMatches, 0, ctx.count); break; case 2: default: // resort matches by type and print in multiple columns - Prompt_ShowIndividualMatches( prompt, matches, numCommands, numAliases, numCvars ); + Prompt_ShowIndividualMatches(prompt, matches, numCommands, numAliases, numCvars); break; } finish1: // free matches - for( i = 0; i < ctx.count; i++ ) { - Z_Free( matches[i] ); + for (i = 0; i < ctx.count; i++) { + Z_Free(matches[i]); } finish2: // move cursor - if( pos >= inputLine->maxChars ) { + if (pos >= inputLine->maxChars) { pos = inputLine->maxChars - 1; } inputLine->cursorPos = pos; } -void Prompt_CompleteHistory( commandPrompt_t *prompt, qboolean forward ) { +void Prompt_CompleteHistory(commandPrompt_t *prompt, qboolean forward) +{ char *s, *m = NULL; int i, j; - if( !prompt->search ) { + if (!prompt->search) { s = prompt->inputLine.text; - if( *s == '/' || *s == '\\' ) { + if (*s == '/' || *s == '\\') { s++; } - if( !*s ) { + if (!*s) { return; } - prompt->search = Z_CopyString( s ); + prompt->search = Z_CopyString(s); } - if( forward ) { - for( i = prompt->historyLineNum + 1; i < prompt->inputLineNum; i++ ) { + if (forward) { + for (i = prompt->historyLineNum + 1; i < prompt->inputLineNum; i++) { s = prompt->history[i & HISTORY_MASK]; - if( s && strstr( s, prompt->search ) ) { - if( strcmp( s, prompt->inputLine.text ) ) { + if (s && strstr(s, prompt->search)) { + if (strcmp(s, prompt->inputLine.text)) { m = s; break; } @@ -407,13 +412,13 @@ void Prompt_CompleteHistory( commandPrompt_t *prompt, qboolean forward ) { } } else { j = prompt->inputLineNum - HISTORY_SIZE; - if( j < 0 ) { + if (j < 0) { j = 0; } - for( i = prompt->historyLineNum - 1; i >= j; i-- ) { + for (i = prompt->historyLineNum - 1; i >= j; i--) { s = prompt->history[i & HISTORY_MASK]; - if( s && strstr( s, prompt->search ) ) { - if( strcmp( s, prompt->inputLine.text ) ) { + if (s && strstr(s, prompt->search)) { + if (strcmp(s, prompt->inputLine.text)) { m = s; break; } @@ -421,18 +426,19 @@ void Prompt_CompleteHistory( commandPrompt_t *prompt, qboolean forward ) { } } - if( !m ) { + if (!m) { return; } prompt->historyLineNum = i; - IF_Replace( &prompt->inputLine, prompt->history[i & HISTORY_MASK] ); + IF_Replace(&prompt->inputLine, prompt->history[i & HISTORY_MASK]); } -void Prompt_ClearState( commandPrompt_t *prompt ) { +void Prompt_ClearState(commandPrompt_t *prompt) +{ prompt->tooMany = qfalse; - if( prompt->search ) { - Z_Free( prompt->search ); + if (prompt->search) { + Z_Free(prompt->search); prompt->search = NULL; } } @@ -444,24 +450,25 @@ Prompt_Action User just pressed enter ==================== */ -char *Prompt_Action( commandPrompt_t *prompt ) { +char *Prompt_Action(commandPrompt_t *prompt) +{ char *s = prompt->inputLine.text; int i, j; - - Prompt_ClearState( prompt ); - if( s[0] == 0 || ( ( s[0] == '/' || s[0] == '\\' ) && s[1] == 0 ) ) { - IF_Clear( &prompt->inputLine ); + + Prompt_ClearState(prompt); + if (s[0] == 0 || ((s[0] == '/' || s[0] == '\\') && s[1] == 0)) { + IF_Clear(&prompt->inputLine); return NULL; // empty line } // save current line in history i = prompt->inputLineNum & HISTORY_MASK; - j = ( prompt->inputLineNum - 1 ) & HISTORY_MASK; - if( !prompt->history[j] || strcmp( prompt->history[j], s ) ) { - if( prompt->history[i] ) { - Z_Free( prompt->history[i] ); + j = (prompt->inputLineNum - 1) & HISTORY_MASK; + if (!prompt->history[j] || strcmp(prompt->history[j], s)) { + if (prompt->history[i]) { + Z_Free(prompt->history[i]); } - prompt->history[i] = Z_CopyString( s ); + prompt->history[i] = Z_CopyString(s); prompt->inputLineNum++; } else { i = j; @@ -469,8 +476,8 @@ char *Prompt_Action( commandPrompt_t *prompt ) { // stop history search prompt->historyLineNum = prompt->inputLineNum; - - IF_Clear( &prompt->inputLine ); + + IF_Clear(&prompt->inputLine); return prompt->history[i]; } @@ -480,28 +487,28 @@ char *Prompt_Action( commandPrompt_t *prompt ) { Prompt_HistoryUp ==================== */ -void Prompt_HistoryUp( commandPrompt_t *prompt ) { +void Prompt_HistoryUp(commandPrompt_t *prompt) +{ int i; - Prompt_ClearState( prompt ); + Prompt_ClearState(prompt); - if( prompt->historyLineNum == prompt->inputLineNum ) { + if (prompt->historyLineNum == prompt->inputLineNum) { // save current line in history i = prompt->inputLineNum & HISTORY_MASK; - if( prompt->history[i] ) { - Z_Free( prompt->history[i] ); + if (prompt->history[i]) { + Z_Free(prompt->history[i]); } - prompt->history[i] = Z_CopyString( prompt->inputLine.text ); + prompt->history[i] = Z_CopyString(prompt->inputLine.text); } - if( prompt->inputLineNum - prompt->historyLineNum < HISTORY_SIZE && - prompt->historyLineNum > 0 ) - { + if (prompt->inputLineNum - prompt->historyLineNum < HISTORY_SIZE && + prompt->historyLineNum > 0) { prompt->historyLineNum--; } i = prompt->historyLineNum & HISTORY_MASK; - IF_Replace( &prompt->inputLine, prompt->history[i] ); + IF_Replace(&prompt->inputLine, prompt->history[i]); } /* @@ -509,19 +516,20 @@ void Prompt_HistoryUp( commandPrompt_t *prompt ) { Prompt_HistoryDown ==================== */ -void Prompt_HistoryDown( commandPrompt_t *prompt ) { +void Prompt_HistoryDown(commandPrompt_t *prompt) +{ int i; - Prompt_ClearState( prompt ); + Prompt_ClearState(prompt); - if( prompt->historyLineNum == prompt->inputLineNum ) { + if (prompt->historyLineNum == prompt->inputLineNum) { return; } - + prompt->historyLineNum++; - + i = prompt->historyLineNum & HISTORY_MASK; - IF_Replace( &prompt->inputLine, prompt->history[i] ); + IF_Replace(&prompt->inputLine, prompt->history[i]); } /* @@ -529,74 +537,77 @@ void Prompt_HistoryDown( commandPrompt_t *prompt ) { Prompt_Clear ==================== */ -void Prompt_Clear( commandPrompt_t *prompt ) { +void Prompt_Clear(commandPrompt_t *prompt) +{ int i; - Prompt_ClearState( prompt ); - - for( i = 0; i < HISTORY_SIZE; i++ ) { - if( prompt->history[i] ) { - Z_Free( prompt->history[i] ); + Prompt_ClearState(prompt); + + for (i = 0; i < HISTORY_SIZE; i++) { + if (prompt->history[i]) { + Z_Free(prompt->history[i]); prompt->history[i] = NULL; } } - + prompt->historyLineNum = 0; prompt->inputLineNum = 0; - - IF_Clear( &prompt->inputLine ); + + IF_Clear(&prompt->inputLine); } -void Prompt_SaveHistory( commandPrompt_t *prompt, const char *filename, int lines ) { +void Prompt_SaveHistory(commandPrompt_t *prompt, const char *filename, int lines) +{ qhandle_t f; char *s; int i; - FS_FOpenFile( filename, &f, FS_MODE_WRITE|FS_PATH_BASE ); - if( !f ) { + FS_FOpenFile(filename, &f, FS_MODE_WRITE | FS_PATH_BASE); + if (!f) { return; } - if( lines > HISTORY_SIZE ) { + if (lines > HISTORY_SIZE) { lines = HISTORY_SIZE; } i = prompt->inputLineNum - lines; - if( i < 0 ) { + if (i < 0) { i = 0; } - for( ; i < prompt->inputLineNum; i++ ) { + for (; i < prompt->inputLineNum; i++) { s = prompt->history[i & HISTORY_MASK]; - if( s ) { - FS_FPrintf( f, "%s\n", s ); + if (s) { + FS_FPrintf(f, "%s\n", s); } } - FS_FCloseFile( f ); + FS_FCloseFile(f); } -void Prompt_LoadHistory( commandPrompt_t *prompt, const char *filename ) { +void Prompt_LoadHistory(commandPrompt_t *prompt, const char *filename) +{ char buffer[MAX_FIELD_TEXT]; qhandle_t f; int i; ssize_t len; - FS_FOpenFile( filename, &f, FS_MODE_READ|FS_TYPE_REAL|FS_PATH_BASE ); - if( !f ) { + FS_FOpenFile(filename, &f, FS_MODE_READ | FS_TYPE_REAL | FS_PATH_BASE); + if (!f) { return; } - for( i = 0; i < HISTORY_SIZE; i++ ) { - if( ( len = FS_ReadLine( f, buffer, sizeof( buffer ) ) ) < 1 ) { + for (i = 0; i < HISTORY_SIZE; i++) { + if ((len = FS_ReadLine(f, buffer, sizeof(buffer))) < 1) { break; } - if( prompt->history[i] ) { - Z_Free( prompt->history[i] ); + if (prompt->history[i]) { + Z_Free(prompt->history[i]); } - prompt->history[i] = memcpy( Z_Malloc( len + 1 ), buffer, len + 1 ); + prompt->history[i] = memcpy(Z_Malloc(len + 1), buffer, len + 1); } - FS_FCloseFile( f ); + FS_FCloseFile(f); prompt->historyLineNum = i; prompt->inputLineNum = i; @@ -607,8 +618,9 @@ void Prompt_LoadHistory( commandPrompt_t *prompt, const char *filename ) { Prompt_Init ==================== */ -void Prompt_Init( void ) { - com_completion_mode = Cvar_Get( "com_completion_mode", "1", 0 ); - com_completion_treshold = Cvar_Get( "com_completion_treshold", "50", 0 ); +void Prompt_Init(void) +{ + com_completion_mode = Cvar_Get("com_completion_mode", "1", 0); + com_completion_treshold = Cvar_Get("com_completion_treshold", "50", 0); } |