1999-03-20 20:28:42 +00:00
|
|
|
/*
|
|
|
|
* FILECOMP.C - handles filename completion.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Comments:
|
|
|
|
*
|
|
|
|
* 30-Jul-1998 (John P Price <linux-guru@gcfl.net>)
|
|
|
|
* moved from command.c file
|
|
|
|
* made second TAB display list of filename matches
|
|
|
|
* made filename be lower case if last character typed is lower case
|
|
|
|
*
|
|
|
|
* 25-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
|
|
|
|
* Cleanup. Unicode safe!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1999-03-21 21:32:45 +00:00
|
|
|
#include <ctype.h>
|
1999-04-14 23:48:27 +00:00
|
|
|
#include <stdio.h>
|
1999-03-20 20:28:42 +00:00
|
|
|
|
1999-12-07 18:16:24 +00:00
|
|
|
#include "cmd.h"
|
1999-03-20 20:28:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef FEATURE_UNIX_FILENAME_COMPLETION
|
|
|
|
|
|
|
|
VOID CompleteFilename (LPTSTR str, INT charcount)
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATA file;
|
|
|
|
HANDLE hFile;
|
|
|
|
INT curplace = 0;
|
|
|
|
INT start;
|
|
|
|
INT count;
|
|
|
|
BOOL found_dot = FALSE;
|
|
|
|
BOOL perfectmatch = TRUE;
|
|
|
|
TCHAR path[MAX_PATH];
|
|
|
|
TCHAR fname[MAX_PATH];
|
|
|
|
TCHAR maxmatch[MAX_PATH] = _T("");
|
|
|
|
TCHAR directory[MAX_PATH];
|
1999-12-07 18:16:24 +00:00
|
|
|
LPCOMMAND cmds_ptr;
|
|
|
|
|
1999-03-20 20:28:42 +00:00
|
|
|
/* expand current file name */
|
|
|
|
count = charcount - 1;
|
|
|
|
if (count < 0)
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* find front of word */
|
|
|
|
while (count > 0 && str[count] != _T(' '))
|
|
|
|
count--;
|
|
|
|
|
|
|
|
/* if not at beginning, go forward 1 */
|
|
|
|
if (str[count] == _T(' '))
|
|
|
|
count++;
|
|
|
|
|
|
|
|
start = count;
|
|
|
|
|
|
|
|
/* extract directory from word */
|
|
|
|
_tcscpy (directory, &str[start]);
|
|
|
|
curplace = _tcslen (directory) - 1;
|
|
|
|
while (curplace >= 0 && directory[curplace] != _T('\\') &&
|
|
|
|
directory[curplace] != _T(':'))
|
|
|
|
{
|
|
|
|
directory[curplace] = 0;
|
|
|
|
curplace--;
|
|
|
|
}
|
|
|
|
|
|
|
|
_tcscpy (path, &str[start]);
|
|
|
|
|
|
|
|
/* look for a '.' in the filename */
|
1999-03-21 21:32:45 +00:00
|
|
|
for (count = _tcslen (directory); path[count] != _T('\0'); count++)
|
1999-03-20 20:28:42 +00:00
|
|
|
{
|
|
|
|
if (path[count] == _T('.'))
|
|
|
|
{
|
1999-03-21 21:32:45 +00:00
|
|
|
found_dot = TRUE;
|
1999-03-20 20:28:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_dot)
|
|
|
|
_tcscat (path, _T("*"));
|
|
|
|
else
|
|
|
|
_tcscat (path, _T("*.*"));
|
|
|
|
|
|
|
|
/* current fname */
|
|
|
|
curplace = 0;
|
|
|
|
|
|
|
|
hFile = FindFirstFile (path, &file);
|
|
|
|
if (hFile != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* find anything */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* ignore "." and ".." */
|
1999-12-24 17:20:54 +00:00
|
|
|
if (!_tcscmp (file.cFileName, _T(".")) ||
|
1999-03-21 21:32:45 +00:00
|
|
|
!_tcscmp (file.cFileName, _T("..")))
|
1999-03-20 20:28:42 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
_tcscpy (fname, file.cFileName);
|
|
|
|
|
|
|
|
if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
_tcscat (fname, _T("\\"));
|
|
|
|
else
|
|
|
|
_tcscat (fname, _T(" "));
|
|
|
|
|
|
|
|
if (!maxmatch[0] && perfectmatch)
|
|
|
|
{
|
|
|
|
_tcscpy(maxmatch, fname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (count = 0; maxmatch[count] && fname[count]; count++)
|
|
|
|
{
|
|
|
|
if (tolower(maxmatch[count]) != tolower(fname[count]))
|
|
|
|
{
|
|
|
|
perfectmatch = FALSE;
|
|
|
|
maxmatch[count] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (FindNextFile (hFile, &file));
|
|
|
|
|
|
|
|
FindClose (hFile);
|
2000-01-17 02:17:50 +00:00
|
|
|
if( perfectmatch )
|
|
|
|
{
|
|
|
|
str[start] = '\"';
|
|
|
|
_tcscpy (&str[start+1], directory);
|
|
|
|
_tcscat (&str[start], maxmatch);
|
|
|
|
_tcscat (&str[start], "\"" );
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
1999-03-21 21:32:45 +00:00
|
|
|
#ifdef __REACTOS__
|
|
|
|
Beep (440, 50);
|
|
|
|
#else
|
1999-03-20 20:28:42 +00:00
|
|
|
MessageBeep (-1);
|
1999-03-21 21:32:45 +00:00
|
|
|
#endif
|
1999-03-20 20:28:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-12-24 17:20:54 +00:00
|
|
|
/* no match found - search for internal command */
|
|
|
|
for (cmds_ptr = cmds; cmds_ptr->name; cmds_ptr++)
|
1999-12-07 18:16:24 +00:00
|
|
|
{
|
1999-12-24 17:20:54 +00:00
|
|
|
if (!_tcsnicmp (&str[start], cmds_ptr->name,
|
|
|
|
_tcslen (&str[start])))
|
1999-12-07 18:16:24 +00:00
|
|
|
{
|
1999-12-24 17:20:54 +00:00
|
|
|
/* return the mach only if it is unique */
|
|
|
|
if (_tcsnicmp (&str[start], (cmds_ptr+1)->name, _tcslen (&str[start])))
|
|
|
|
_tcscpy (&str[start], cmds_ptr->name);
|
1999-12-07 18:16:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-21 21:32:45 +00:00
|
|
|
#ifdef __REACTOS__
|
|
|
|
Beep (440, 50);
|
|
|
|
#else
|
1999-03-20 20:28:42 +00:00
|
|
|
MessageBeep (-1);
|
1999-03-21 21:32:45 +00:00
|
|
|
#endif
|
1999-03-20 20:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* returns 1 if at least one match, else returns 0
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL ShowCompletionMatches (LPTSTR str, INT charcount)
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATA file;
|
|
|
|
HANDLE hFile;
|
|
|
|
BOOL found_dot = FALSE;
|
|
|
|
INT curplace = 0;
|
|
|
|
INT start;
|
|
|
|
INT count;
|
|
|
|
TCHAR path[MAX_PATH];
|
|
|
|
TCHAR fname[MAX_PATH];
|
|
|
|
TCHAR directory[MAX_PATH];
|
|
|
|
|
|
|
|
/* expand current file name */
|
|
|
|
count = charcount - 1;
|
|
|
|
if (count < 0)
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
/* find front of word */
|
|
|
|
while (count > 0 && str[count] != _T(' '))
|
|
|
|
count--;
|
|
|
|
|
|
|
|
/* if not at beginning, go forward 1 */
|
|
|
|
if (str[count] == _T(' '))
|
|
|
|
count++;
|
|
|
|
|
|
|
|
start = count;
|
|
|
|
|
|
|
|
/* extract directory from word */
|
|
|
|
_tcscpy (directory, &str[start]);
|
|
|
|
curplace = _tcslen (directory) - 1;
|
1999-03-21 21:32:45 +00:00
|
|
|
while (curplace >= 0 &&
|
|
|
|
directory[curplace] != _T('\\') &&
|
1999-03-20 20:28:42 +00:00
|
|
|
directory[curplace] != _T(':'))
|
|
|
|
{
|
|
|
|
directory[curplace] = 0;
|
|
|
|
curplace--;
|
|
|
|
}
|
|
|
|
|
|
|
|
_tcscpy (path, &str[start]);
|
|
|
|
|
|
|
|
/* look for a . in the filename */
|
1999-03-21 21:32:45 +00:00
|
|
|
for (count = _tcslen (directory); path[count] != _T('\0'); count++)
|
1999-03-20 20:28:42 +00:00
|
|
|
{
|
|
|
|
if (path[count] == _T('.'))
|
|
|
|
{
|
|
|
|
found_dot = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-03-21 21:32:45 +00:00
|
|
|
|
1999-03-20 20:28:42 +00:00
|
|
|
if (found_dot)
|
|
|
|
_tcscat (path, _T("*"));
|
|
|
|
else
|
|
|
|
_tcscat (path, _T("*.*"));
|
|
|
|
|
|
|
|
/* current fname */
|
|
|
|
curplace = 0;
|
|
|
|
|
|
|
|
hFile = FindFirstFile (path, &file);
|
|
|
|
if (hFile != INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
/* find anything */
|
|
|
|
ConOutChar (_T('\n'));
|
|
|
|
count = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* ignore . and .. */
|
1999-03-21 21:32:45 +00:00
|
|
|
if (!_tcscmp (file.cFileName, _T(".")) ||
|
|
|
|
!_tcscmp (file.cFileName, _T("..")))
|
1999-03-20 20:28:42 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
1999-05-11 12:40:11 +00:00
|
|
|
_stprintf (fname, _T("[%s]"), file.cFileName);
|
1999-03-20 20:28:42 +00:00
|
|
|
else
|
|
|
|
_tcscpy (fname, file.cFileName);
|
|
|
|
|
|
|
|
ConOutPrintf (_T("%-14s"), fname);
|
|
|
|
if (++count == 5)
|
|
|
|
{
|
|
|
|
ConOutChar (_T('\n'));
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (FindNextFile (hFile, &file));
|
|
|
|
|
|
|
|
FindClose (hFile);
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
ConOutChar (_T('\n'));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* no match found */
|
1999-03-21 21:32:45 +00:00
|
|
|
#ifdef __REACTOS__
|
|
|
|
Beep (440, 50);
|
|
|
|
#else
|
1999-03-20 20:28:42 +00:00
|
|
|
MessageBeep (-1);
|
1999-03-21 21:32:45 +00:00
|
|
|
#endif
|
1999-03-20 20:28:42 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FEATURE_4NT_FILENAME_COMPLETION
|
|
|
|
|
|
|
|
//static VOID BuildFilenameMatchList (...)
|
|
|
|
|
|
|
|
// VOID CompleteFilenameNext (LPTSTR, INT)
|
|
|
|
// VOID CompleteFilenamePrev (LPTSTR, INT)
|
|
|
|
|
|
|
|
// VOID RemoveFilenameMatchList (VOID)
|
|
|
|
|
1999-03-21 21:32:45 +00:00
|
|
|
#endif
|