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 while (count
> 0 && str
[count
] != _T(' '))
54 /* if not at beginning, go forward 1 */
55 if (str
[count
] == _T(' '))
60 /* extract directory from word */
61 _tcscpy (directory
, &str
[start
]);
62 curplace
= _tcslen (directory
) - 1;
63 while (curplace
>= 0 && directory
[curplace
] != _T('\\') &&
64 directory
[curplace
] != _T(':'))
66 directory
[curplace
] = 0;
70 _tcscpy (path
, &str
[start
]);
72 /* look for a '.' in the filename */
73 for (count
= _tcslen (directory
); path
[count
] != _T('\0'); count
++)
75 if (path
[count
] == _T('.'))
83 _tcscat (path
, _T("*"));
85 _tcscat (path
, _T("*.*"));
90 hFile
= FindFirstFile (path
, &file
);
91 if (hFile
!= INVALID_HANDLE_VALUE
)
96 /* ignore "." and ".." */
97 if (!_tcscmp (file
.cFileName
, _T(".")) ||
98 !_tcscmp (file
.cFileName
, _T("..")))
101 _tcscpy (fname
, file
.cFileName
);
103 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
104 _tcscat (fname
, _T("\\"));
106 _tcscat (fname
, _T(" "));
108 if (!maxmatch
[0] && perfectmatch
)
110 _tcscpy(maxmatch
, fname
);
114 for (count
= 0; maxmatch
[count
] && fname
[count
]; count
++)
116 if (tolower(maxmatch
[count
]) != tolower(fname
[count
]))
118 perfectmatch
= FALSE
;
125 while (FindNextFile (hFile
, &file
));
131 _tcscpy (&str
[start
+1], directory
);
132 _tcscat (&str
[start
], maxmatch
);
133 _tcscat (&str
[start
], "\"" );
145 /* no match found - search for internal command */
146 for (cmds_ptr
= cmds
; cmds_ptr
->name
; cmds_ptr
++)
148 if (!_tcsnicmp (&str
[start
], cmds_ptr
->name
,
149 _tcslen (&str
[start
])))
151 /* return the mach only if it is unique */
152 if (_tcsnicmp (&str
[start
], (cmds_ptr
+1)->name
, _tcslen (&str
[start
])))
153 _tcscpy (&str
[start
], cmds_ptr
->name
);
168 * returns 1 if at least one match, else returns 0
171 BOOL
ShowCompletionMatches (LPTSTR str
, INT charcount
)
173 WIN32_FIND_DATA file
;
175 BOOL found_dot
= FALSE
;
179 TCHAR path
[MAX_PATH
];
180 TCHAR fname
[MAX_PATH
];
181 TCHAR directory
[MAX_PATH
];
183 /* expand current file name */
184 count
= charcount
- 1;
188 /* find front of word */
189 while (count
> 0 && str
[count
] != _T(' '))
192 /* if not at beginning, go forward 1 */
193 if (str
[count
] == _T(' '))
198 /* extract directory from word */
199 _tcscpy (directory
, &str
[start
]);
200 curplace
= _tcslen (directory
) - 1;
201 while (curplace
>= 0 &&
202 directory
[curplace
] != _T('\\') &&
203 directory
[curplace
] != _T(':'))
205 directory
[curplace
] = 0;
209 _tcscpy (path
, &str
[start
]);
211 /* look for a . in the filename */
212 for (count
= _tcslen (directory
); path
[count
] != _T('\0'); count
++)
214 if (path
[count
] == _T('.'))
222 _tcscat (path
, _T("*"));
224 _tcscat (path
, _T("*.*"));
229 hFile
= FindFirstFile (path
, &file
);
230 if (hFile
!= INVALID_HANDLE_VALUE
)
233 ConOutChar (_T('\n'));
237 /* ignore . and .. */
238 if (!_tcscmp (file
.cFileName
, _T(".")) ||
239 !_tcscmp (file
.cFileName
, _T("..")))
242 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
243 _stprintf (fname
, _T("[%s]"), file
.cFileName
);
245 _tcscpy (fname
, file
.cFileName
);
247 ConOutPrintf (_T("%-14s"), fname
);
250 ConOutChar (_T('\n'));
254 while (FindNextFile (hFile
, &file
));
259 ConOutChar (_T('\n'));
276 #ifdef FEATURE_4NT_FILENAME_COMPLETION
278 //static VOID BuildFilenameMatchList (...)
280 // VOID CompleteFilenameNext (LPTSTR, INT)
281 // VOID CompleteFilenamePrev (LPTSTR, INT)
283 // VOID RemoveFilenameMatchList (VOID)