/* ============ Cvar_Reset_f ============ */ void Cvar_Reset_f( void ) { if ( Cmd_Argc() != 2 ) { Cmd_PrintUsage(_("<variable>"), NULL); return; } Cvar_Reset( Cmd_Argv( 1 ) ); }
/* ================ Con_Dump_f Save the console contents out to a file ================ */ void Con_Dump_f( void ) { int l; fileHandle_t f; char name[ MAX_STRING_CHARS ]; l = Cmd_Argc(); if ( l > 2 ) { Cmd_PrintUsage(_("[<filename>]"), NULL); return; } if ( l == 1 ) { time_t now = time( NULL ); strftime( name, sizeof( name ), "condump/%Y%m%d-%H%M%S%z.txt", localtime( &now ) ); } else { Q_snprintf( name, sizeof( name ), "condump/%s", Cmd_Argv( 1 ) ); } f = FS_FOpenFileWrite( name ); if ( !f ) { Com_Log(LOG_ERROR, _( "couldn't open." )); return; } Com_Printf(_( "Dumped console text to %s.\n"), name ); // skip empty lines for ( l = consoleState.currentLine - consoleState.maxScrollbackLengthInLines + 1; l <= consoleState.currentLine; l++ ) { if ( consoleState.text[ CON_LINE( l ) ].ch ) { break; } } // write the remaining lines for ( ; l <= consoleState.currentLine; l++ ) { const char *buffer = Con_LineToString( l, qtrue ); FS_Write( buffer, strlen( buffer ), f ); } FS_FCloseFile( f ); }
/* ============ Cvar_Cycle_f - ydnar Cycles a cvar for easy single key binding ============ */ void Cvar_Cycle_f( void ) { int start, end, step, oldvalue, value; if ( Cmd_Argc() < 4 || Cmd_Argc() > 5 ) { Cmd_PrintUsage(_("<variable> <start> <end> [<step>]"), NULL); return; } oldvalue = value = Cvar_VariableValue( Cmd_Argv( 1 ) ); start = atoi( Cmd_Argv( 2 ) ); end = atoi( Cmd_Argv( 3 ) ); if ( Cmd_Argc() == 5 ) { step = abs( atoi( Cmd_Argv( 4 ) ) ); } else { step = 1; } if ( abs( end - start ) < step ) { step = 1; } if ( end < start ) { value -= step; if ( value < end ) { value = start - ( step - ( oldvalue - end + 1 ) ); } } else { value += step; if ( value > end ) { value = start + ( step - ( end - oldvalue + 1 ) ); } } Cvar_Set2( Cmd_Argv( 1 ), va( "%i", value ), qfalse ); }
static void Cvar_Set_Flagged( int flag ) { cvar_t *v; if ( Cmd_Argc() < 3 ) { Cmd_PrintUsage(_("<variable> <value> [unsafe]"), NULL); return; } Cvar_Set_f(); v = Cvar_FindVar( Cmd_Argv( 1 ) ); if ( !v ) { return; } v->flags |= flag; }
/* ============ Cvar_Toggle_f Toggles a cvar for easy single key binding, optionally through a list of given values ============ */ void Cvar_Toggle_f( void ) { int i, c; const char *varname, *curval; c = Cmd_Argc(); if ( c < 2 ) { Cmd_PrintUsage(_("<variable> [<value> …]"), NULL); return; } varname = Cmd_Argv( 1 ); if ( c == 2 ) { Cvar_Set2( varname, va( "%d", !Cvar_VariableValue( varname ) ), qfalse ); return; } curval = Cvar_VariableString( Cmd_Argv( 1 ) ); // don't bother checking the last value for a match, since the desired // behaviour is the same as if the last value didn't match: // set the variable to the first value for ( i = 2; i < c - 1; ++i ) { if ( !strcmp( curval, Cmd_Argv( i ) ) ) { Cvar_Set2( varname, Cmd_Argv( i + 1 ), qfalse ); return; } } // fallback Cvar_Set2( varname, Cmd_Argv( 2 ), qfalse ); }
/* ================ Con_Search_f Scroll up to the first console line containing a string ================ */ void Con_Search_f( void ) { int l, i; int direction; int c = Cmd_Argc(); if ( c < 2 ) { Cmd_PrintUsage(_("<string>…"), NULL); return; } direction = Q_stricmp( Cmd_Argv( 0 ), "searchDown" ) ? -1 : 1; // check the lines for ( l = consoleState.scrollLineIndex - 1 + direction; l <= consoleState.currentLine && consoleState.currentLine - l < consoleState.maxScrollbackLengthInLines; l += direction ) { const char *buffer = Con_LineToString( l, qtrue ); // Don't search commands for ( i = 1; i < c; i++ ) { if ( Q_stristr( buffer, Cmd_Argv( i ) ) ) { consoleState.scrollLineIndex = l + 1; if ( consoleState.scrollLineIndex > consoleState.currentLine ) { consoleState.bottomDisplayedLine = consoleState.currentLine; } return; } } } }
/* ============ Cvar_Set_f Allows setting and defining of arbitrary cvars from console, even if they weren't declared in C code. ============ */ void Cvar_Set_f( void ) { int c, unsafe = 0; char *value; c = Cmd_Argc(); if ( c < 3 ) { Cmd_PrintUsage(_("<variable> <value> [unsafe]"), NULL); return; } // ydnar: handle unsafe vars if ( c >= 4 && !strcmp( Cmd_Argv( c - 1 ), "unsafe" ) ) { c--; unsafe = 1; if ( com_crashed != NULL && com_crashed->integer ) { Com_Printf(_( "%s is unsafe. Check com_crashed.\n"), Cmd_Argv( 1 ) ); return; } } value = strdup( Cmd_Cmd_FromNth( 2 ) ); // 3rd arg onwards, raw if ( unsafe ) { char *end = value + strlen( value ); // skip spaces while ( --end > value ) { if ( *end != ' ' ) { break; } } ++end; // skip "unsafe" (may be quoted, so just scan it) while ( --end > value ) { if ( *end == ' ' ) { break; } } ++end; // skip spaces while ( --end > value ) { if ( *end != ' ' ) { break; } } end[ 1 ] = 0; // end of string :-) } Cvar_Set2( Cmd_Argv( 1 ), Cmd_UnquoteString( value ), qfalse ); free( value ); }
/* ================ Con_Grep_f Find all console lines containing a string ================ */ void Con_Grep_f( void ) { int l; char *search; char *printbuf = NULL; size_t pbAlloc = 0, pbLength = 0; if ( Cmd_Argc() != 2 ) { Cmd_PrintUsage(_("<string>"), NULL); return; } // skip empty lines for ( l = consoleState.currentLine - consoleState.maxScrollbackLengthInLines + 1; l <= consoleState.currentLine; l++ ) { if ( consoleState.text[ CON_LINE( l ) ].ch ) { break; } } // check the remaining lines search = Cmd_Argv( 1 ); for ( ; l <= consoleState.currentLine; l++ ) { const char *buffer = Con_LineToString( l, qfalse ); if ( Q_stristr( buffer, search ) ) { size_t i; buffer = Con_LineToColouredString( l, qtrue ); i = strlen( buffer ); if ( pbLength + i >= pbAlloc ) { char *nb; // allocate in 16K chunks - more than adequate pbAlloc = ( pbLength + i + 1 + 16383) & ~16383; nb = (char*) Z_Malloc( pbAlloc ); if( printbuf ) { strcpy( nb, printbuf ); Z_Free( printbuf ); } printbuf = nb; } Q_strcat( printbuf, pbAlloc, buffer ); pbLength += i; } } if( printbuf ) { char tmpbuf[ MAXPRINTMSG ]; int i; // print out in chunks so we don't go over the MAXPRINTMSG limit for ( i = 0; i < pbLength; i += MAXPRINTMSG - 1 ) { Q_strncpyz( tmpbuf, printbuf + i, sizeof( tmpbuf ) ); Com_Printf( "%s", tmpbuf ); } Z_Free( printbuf ); } }