2 * FILECOMP.C - handles filename completion.
7 * 30-Jul-1998 (John P Price <linux-guru@gcfl.net>)
8 * moved from command.c file
9 * made second TAB display list of filename matches
10 * made filename be lower case if last character typed is lower case
12 * 25-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
13 * Cleanup. Unicode safe!
28 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
30 VOID CompleteFilename (LPTSTR str, INT charcount)
37 BOOL found_dot = FALSE;
38 BOOL perfectmatch = TRUE;
40 TCHAR fname[MAX_PATH];
41 TCHAR maxmatch[MAX_PATH] = _T("");
42 TCHAR directory[MAX_PATH];
45 /* expand current file name */
46 count = charcount - 1;
50 /* find front of word */
51 if (str[count] == _T('"'))
54 while (count > 0 && str[count] != _T('"'))
59 while (count > 0 && str[count] != _T(' '))
63 /* if not at beginning, go forward 1 */
64 if (str[count] == _T(' '))
69 if (str[count] == _T('"'))
70 count++; /* don't increment start */
72 /* extract directory from word */
73 _tcscpy (directory, &str[count]);
74 curplace = _tcslen (directory) - 1;
76 if (curplace >= 0 && directory[curplace] == _T('"'))
77 directory[curplace--] = _T('\0');
79 _tcscpy (path, directory);
81 while (curplace >= 0 && directory[curplace] != _T('\\') &&
82 directory[curplace] != _T(':'))
84 directory[curplace] = 0;
88 /* look for a '.' in the filename */
89 for (count = _tcslen (directory); path[count] != _T('\0'); count++)
91 if (path[count] == _T('.'))
99 _tcscat (path, _T("*"));
101 _tcscat (path, _T("*.*"));
106 hFile = FindFirstFile (path, &file);
107 if (hFile != INVALID_HANDLE_VALUE)
112 /* ignore "." and ".." */
113 if (!_tcscmp (file.cFileName, _T(".")) ||
114 !_tcscmp (file.cFileName, _T("..")))
117 _tcscpy (fname, file.cFileName);
119 if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
120 _tcscat (fname, _T("\\"));
122 if (!maxmatch[0] && perfectmatch)
124 _tcscpy(maxmatch, fname);
128 for (count = 0; maxmatch[count] && fname[count]; count++)
130 if (tolower(maxmatch[count]) != tolower(fname[count]))
132 perfectmatch = FALSE;
138 if (maxmatch[count] == _T('\0') &&
139 fname[count] != _T('\0'))
140 perfectmatch = FALSE;
143 while (FindNextFile (hFile, &file));
147 /* only quote if the filename contains spaces */
148 if (_tcschr(directory, _T(' ')) ||
149 _tcschr(maxmatch, _T(' ')))
151 str[start] = _T('\"');
152 _tcscpy (&str[start+1], directory);
153 _tcscat (&str[start], maxmatch);
154 _tcscat (&str[start], _T("\"") );
158 _tcscpy (&str[start], directory);
159 _tcscat (&str[start], maxmatch);
162 /* append a space if last word is not a directory */
165 curplace = _tcslen(&str[start]);
166 if(str[start+curplace-1] == _T('"'))
169 if(str[start+curplace-1] != _T('\\'))
170 _tcscat(&str[start], _T(" "));
183 /* no match found - search for internal command */
184 for (cmds_ptr = cmds; cmds_ptr->name; cmds_ptr++)
186 if (!_tcsnicmp (&str[start], cmds_ptr->name,
187 _tcslen (&str[start])))
189 /* return the mach only if it is unique */
190 if (_tcsnicmp (&str[start], (cmds_ptr+1)->name, _tcslen (&str[start])))
191 _tcscpy (&str[start], cmds_ptr->name);
206 * returns 1 if at least one match, else returns 0
209 BOOL ShowCompletionMatches (LPTSTR str, INT charcount)
211 WIN32_FIND_DATA file;
213 BOOL found_dot = FALSE;
217 TCHAR path[MAX_PATH];
218 TCHAR fname[MAX_PATH];
219 TCHAR directory[MAX_PATH];
221 /* expand current file name */
222 count = charcount - 1;
226 /* find front of word */
227 if (str[count] == _T('"'))
230 while (count > 0 && str[count] != _T('"'))
235 while (count > 0 && str[count] != _T(' '))
239 /* if not at beginning, go forward 1 */
240 if (str[count] == _T(' '))
245 if (str[count] == _T('"'))
246 count++; /* don't increment start */
248 /* extract directory from word */
249 _tcscpy (directory, &str[count]);
250 curplace = _tcslen (directory) - 1;
252 if (curplace >= 0 && directory[curplace] == _T('"'))
253 directory[curplace--] = _T('\0');
255 _tcscpy (path, directory);
257 while (curplace >= 0 &&
258 directory[curplace] != _T('\\') &&
259 directory[curplace] != _T(':'))
261 directory[curplace] = 0;
265 /* look for a . in the filename */
266 for (count = _tcslen (directory); path[count] != _T('\0'); count++)
268 if (path[count] == _T('.'))
276 _tcscat (path, _T("*"));
278 _tcscat (path, _T("*.*"));
283 hFile = FindFirstFile (path, &file);
284 if (hFile != INVALID_HANDLE_VALUE)
287 ConOutChar (_T('\n'));
291 /* ignore . and .. */
292 if (!_tcscmp (file.cFileName, _T(".")) ||
293 !_tcscmp (file.cFileName, _T("..")))
296 if (file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
297 _stprintf (fname, _T("[%s]"), file.cFileName);
299 _tcscpy (fname, file.cFileName);
301 ConOutPrintf (_T("%-14s"), fname);
304 ConOutChar (_T('\n'));
308 while (FindNextFile (hFile, &file));
313 ConOutChar (_T('\n'));
330 #ifdef FEATURE_4NT_FILENAME_COMPLETION
332 //static VOID BuildFilenameMatchList (...)
334 // VOID CompleteFilenameNext (LPTSTR, INT)
335 // VOID CompleteFilenamePrev (LPTSTR, INT)
337 // VOID RemoveFilenameMatchList (VOID)