Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable directory completion for \exec command #305

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions code/qcommon/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,21 @@ Cmd_CompleteCfgName
==================
*/
static void Cmd_CompleteCfgName( const char *args, int argNum ) {
if( argNum == 2 ) {
Field_CompleteFilename( "", "cfg", qfalse, FS_MATCH_ANY | FS_MATCH_STICK );
if (argNum == 2) {
char path[MAX_OSPATH] = "";
const char *arg = Cmd_Argv(1);

// Look for the last forward slash to determine the
// directory portion of the path for file completion.
char *lastSlash = strrchr(arg, '/');

if (lastSlash) {
int newLength = lastSlash - arg + 1;
strncpy(path, arg, newLength);
path[newLength] = '\0';
}

Field_CompleteFilename(path, "cfg", qfalse, FS_MATCH_ANY | FS_MATCH_STICK);
}
}

Expand Down
11 changes: 10 additions & 1 deletion code/qcommon/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4607,7 +4607,8 @@ static qboolean Field_Complete( void )

if( matchCount == 1 )
{
Field_AddSpace();
if(completionField->buffer[completionField->cursor - 1] != '/')
Field_AddSpace();
return qtrue;
}

Expand Down Expand Up @@ -4764,7 +4765,15 @@ void Field_CompleteCommand( const char *cmd, qboolean doCommands, qboolean doCva
completionArgument++;
}
else
{
completionString = Cmd_Argv( completionArgument - 1 );
char *lastSlash = strrchr(completionString, '/');
if (lastSlash) {
// If a slash is found, set the completion string to everything after the last slash
// This is done to ensure that the completion function works with the correct file or directory name
completionString = lastSlash + 1;
}
}

#ifndef DEDICATED
// Unconditionally add a '\' to the start of the buffer
Expand Down
31 changes: 27 additions & 4 deletions code/qcommon/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,7 @@ Returns a unique list of files that match the given criteria
from all search paths
===============
*/
static char **FS_ListFilteredFiles( const char *path, const char *extension, const char *filter, int *numfiles, int flags ) {
static char **FS_ListFilteredFiles( const char *path, const char *extension, const char *filter, int *numfiles, int flags, qboolean includeDirs ) {
int nfiles;
char **listCopy;
char *list[MAX_FOUND_FILES];
Expand All @@ -3334,6 +3334,7 @@ static char **FS_ListFilteredFiles( const char *path, const char *extension, con
char zpath[MAX_ZPATH];
qboolean hasPatterns;
const char *x;
char dirname[MAX_OSPATH*2];

if ( !fs_searchpaths ) {
Com_Error( ERR_FATAL, "Filesystem call made without initialization" );
Expand Down Expand Up @@ -3441,6 +3442,8 @@ static char **FS_ListFilteredFiles( const char *path, const char *extension, con
const char *netpath;
int numSysFiles;
char **sysFiles;
int numSysDirs;
char **sysDirs;
const char *name;

netpath = FS_BuildOSPath( search->dir->path, search->dir->gamedir, path );
Expand All @@ -3458,6 +3461,26 @@ static char **FS_ListFilteredFiles( const char *path, const char *extension, con
nfiles = FS_AddFileToList( name, list, nfiles );
}
Sys_FreeFileList( sysFiles );

if(includeDirs) {
sysDirs = Sys_ListFiles( netpath, "", filter, &numSysDirs, qtrue );
for ( i = 0 ; i < numSysDirs ; i++ ) {
name = sysDirs[ i ];
// ignore . and ..
if ( strcmp(name, "." ) == 0 || strcmp( name, ".." ) == 0 ) {
continue;
}
Com_sprintf( dirname, sizeof(dirname), "%s/", name);
if ( fnamecallback ) {
// use custom filter
if ( !fnamecallback( dirname, strlen(dirname) ) )
continue;
}

nfiles = FS_AddFileToList( dirname, list, nfiles );
}
Sys_FreeFileList( sysDirs );
}
}
}

Expand Down Expand Up @@ -3485,7 +3508,7 @@ FS_ListFiles
*/
char **FS_ListFiles( const char *path, const char *extension, int *numfiles )
{
return FS_ListFilteredFiles( path, extension, NULL, numfiles, FS_MATCH_ANY );
return FS_ListFilteredFiles( path, extension, NULL, numfiles, FS_MATCH_ANY, qfalse );
}


Expand Down Expand Up @@ -3929,7 +3952,7 @@ static void FS_NewDir_f( void ) {

Com_Printf( "---------------\n" );

dirnames = FS_ListFilteredFiles( "", "", filter, &ndirs, FS_MATCH_ANY );
dirnames = FS_ListFilteredFiles( "", "", filter, &ndirs, FS_MATCH_ANY, qfalse );

if ( ndirs >= 2 )
FS_SortFileList( dirnames, ndirs - 1 );
Expand Down Expand Up @@ -5579,7 +5602,7 @@ void FS_FilenameCompletion( const char *dir, const char *ext,
int nfiles;
int i;

filenames = FS_ListFilteredFiles( dir, ext, NULL, &nfiles, flags );
filenames = FS_ListFilteredFiles( dir, ext, NULL, &nfiles, flags, qtrue );

if ( nfiles >= 2 )
FS_SortFileList( filenames, nfiles-1 );
Expand Down