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)
13 * Cleanup. Unicode safe!
15 * 30-Apr-2004 (Filip Navara <xnavara@volny.cz>)
16 * Make the file listing readable when there is a lot of long names.
19 * 05-Jul-2004 (Jens Collin <jens.collin@lakhei.com>)
20 * Now expands lfn even when trailing " is omitted.
26 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
28 VOID
CompleteFilename (LPTSTR str
, UINT 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 how many '"'s there is typed already.*/
54 if (str
[step
] == _T('"'))
58 /* if c is odd, then user typed " before name, else not.*/
60 /* find front of word */
61 if (str
[count
] == _T('"') || (c
% 2))
64 while (count
> 0 && str
[count
] != _T('"'))
69 while (count
> 0 && str
[count
] != _T(' '))
73 /* if not at beginning, go forward 1 */
74 if (str
[count
] == _T(' '))
79 if (str
[count
] == _T('"'))
80 count
++; /* don't increment start */
82 /* extract directory from word */
83 _tcscpy (directory
, &str
[count
]);
84 curplace
= _tcslen (directory
) - 1;
86 if (curplace
>= 0 && directory
[curplace
] == _T('"'))
87 directory
[curplace
--] = _T('\0');
89 _tcscpy (path
, directory
);
91 while (curplace
>= 0 && directory
[curplace
] != _T('\\') &&
92 directory
[curplace
] != _T('/') &&
93 directory
[curplace
] != _T(':'))
95 directory
[curplace
] = 0;
99 /* look for a '.' in the filename */
100 for (count
= _tcslen (directory
); path
[count
] != _T('\0'); count
++)
102 if (path
[count
] == _T('.'))
110 _tcscat (path
, _T("*"));
112 _tcscat (path
, _T("*.*"));
117 hFile
= FindFirstFile (path
, &file
);
118 if (hFile
!= INVALID_HANDLE_VALUE
)
123 /* ignore "." and ".." */
124 if (!_tcscmp (file
.cFileName
, _T(".")) ||
125 !_tcscmp (file
.cFileName
, _T("..")))
128 _tcscpy (fname
, file
.cFileName
);
130 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
131 _tcscat (fname
, _T("\\"));
133 if (!maxmatch
[0] && perfectmatch
)
135 _tcscpy(maxmatch
, fname
);
139 for (count
= 0; maxmatch
[count
] && fname
[count
]; count
++)
141 if (tolower(maxmatch
[count
]) != tolower(fname
[count
]))
143 perfectmatch
= FALSE
;
149 if (maxmatch
[count
] == _T('\0') &&
150 fname
[count
] != _T('\0'))
151 perfectmatch
= FALSE
;
154 while (FindNextFile (hFile
, &file
));
158 /* only quote if the filename contains spaces */
159 if (_tcschr(directory
, _T(' ')) ||
160 _tcschr(maxmatch
, _T(' ')))
162 str
[start
] = _T('\"');
163 _tcscpy (&str
[start
+1], directory
);
164 _tcscat (&str
[start
], maxmatch
);
165 _tcscat (&str
[start
], _T("\"") );
169 _tcscpy (&str
[start
], directory
);
170 _tcscat (&str
[start
], maxmatch
);
180 /* no match found - search for internal command */
181 for (cmds_ptr
= cmds
; cmds_ptr
->name
; cmds_ptr
++)
183 if (!_tcsnicmp (&str
[start
], cmds_ptr
->name
,
184 _tcslen (&str
[start
])))
186 /* return the mach only if it is unique */
187 if (_tcsnicmp (&str
[start
], (cmds_ptr
+1)->name
, _tcslen (&str
[start
])))
188 _tcscpy (&str
[start
], cmds_ptr
->name
);
199 * returns 1 if at least one match, else returns 0
202 BOOL
ShowCompletionMatches (LPTSTR str
, INT charcount
)
204 WIN32_FIND_DATA file
;
206 BOOL found_dot
= FALSE
;
210 TCHAR path
[MAX_PATH
];
211 TCHAR fname
[MAX_PATH
];
212 TCHAR directory
[MAX_PATH
];
213 UINT longestfname
= 0;
216 /* expand current file name */
217 count
= charcount
- 1;
221 /* find front of word */
222 if (str
[count
] == _T('"'))
225 while (count
> 0 && str
[count
] != _T('"'))
230 while (count
> 0 && str
[count
] != _T(' '))
234 /* if not at beginning, go forward 1 */
235 if (str
[count
] == _T(' '))
240 if (str
[count
] == _T('"'))
241 count
++; /* don't increment start */
243 /* extract directory from word */
244 _tcscpy (directory
, &str
[count
]);
245 curplace
= _tcslen (directory
) - 1;
247 if (curplace
>= 0 && directory
[curplace
] == _T('"'))
248 directory
[curplace
--] = _T('\0');
250 _tcscpy (path
, directory
);
252 while (curplace
>= 0 &&
253 directory
[curplace
] != _T('\\') &&
254 directory
[curplace
] != _T(':'))
256 directory
[curplace
] = 0;
260 /* look for a . in the filename */
261 for (count
= _tcslen (directory
); path
[count
] != _T('\0'); count
++)
263 if (path
[count
] == _T('.'))
271 _tcscat (path
, _T("*"));
273 _tcscat (path
, _T("*.*"));
278 hFile
= FindFirstFile (path
, &file
);
279 if (hFile
!= INVALID_HANDLE_VALUE
)
281 /* Get the size of longest filename first. */
284 if (_tcslen(file
.cFileName
) > longestfname
)
286 longestfname
= _tcslen(file
.cFileName
);
287 /* Directories get extra brackets around them. */
288 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
292 while (FindNextFile (hFile
, &file
));
295 hFile
= FindFirstFile (path
, &file
);
297 /* Count the highest number of columns */
298 GetScreenSize(&screenwidth
, 0);
300 /* For counting columns of output */
303 /* Increase by the number of spaces behind file name */
307 ConOutChar (_T('\n'));
310 /* ignore . and .. */
311 if (!_tcscmp (file
.cFileName
, _T(".")) ||
312 !_tcscmp (file
.cFileName
, _T("..")))
315 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
316 _stprintf (fname
, _T("[%s]"), file
.cFileName
);
318 _tcscpy (fname
, file
.cFileName
);
320 ConOutPrintf (_T("%*s"), - longestfname
, fname
);
322 /* output as much columns as fits on the screen */
323 if (count
>= (screenwidth
/ longestfname
))
325 /* print the new line only if we aren't on the
326 * last column, in this case it wraps anyway */
327 if (count
* longestfname
!= (UINT
)screenwidth
)
328 ConOutPrintf (_T("\n"));
332 while (FindNextFile (hFile
, &file
));
337 ConOutChar (_T('\n'));
350 #ifdef FEATURE_4NT_FILENAME_COMPLETION
352 typedef struct _FileName
354 TCHAR Name
[MAX_PATH
];
357 VOID
FindPrefixAndSuffix(LPTSTR strIN
, LPTSTR szPrefix
, LPTSTR szSuffix
)
359 /* String that is to be examined */
361 /* temp pointers to used to find needed parts */
366 /* number of quotes in the string */
368 /* used in for loops */
370 /* Char number to break the string at */
373 /* when phrasing a string, this tells weather
374 you are inside quotes ot not. */
375 BOOL bInside
= FALSE
;
377 szPrefix
[0] = _T('\0');
378 szSuffix
[0] = _T('\0');
380 /* Copy over the string to later be edited */
383 /* Count number of " */
384 for(i
= 0; i
< _tcslen(str
); i
++)
385 if(str
[i
] == _T('\"'))
388 /* Find the prefix and suffix */
389 if(nQuotes
% 2 && nQuotes
>= 1)
391 /* Odd number of quotes. Just start from the last " */
392 /* THis is the way MS does it, and is an easy way out */
393 szSearch
= _tcsrchr(str
, _T('\"'));
394 /* Move to the next char past the " */
396 _tcscpy(szSuffix
,szSearch
);
397 /* Find the one closest to end */
398 szSearch1
= _tcsrchr(str
, _T('\"'));
399 szSearch2
= _tcsrchr(str
, _T('\\'));
400 szSearch3
= _tcsrchr(str
, _T('.'));
401 if(szSearch2
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch2
))
402 szSearch
= szSearch2
;
403 else if(szSearch3
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch3
))
404 szSearch
= szSearch3
;
406 szSearch
= szSearch1
;
407 /* Move one char past */
409 szSearch
[0] = _T('\0');
410 _tcscpy(szPrefix
,str
);
415 if(!_tcschr(str
, _T(' ')))
417 /* No spaces, everything goes to Suffix */
418 _tcscpy(szSuffix
,str
);
419 /* look for a slash just in case */
420 szSearch
= _tcsrchr(str
, _T('\\'));
424 szSearch
[0] = _T('\0');
425 _tcscpy(szPrefix
,str
);
429 szPrefix
[0] = _T('\0');
436 /* No quotes, and there is a space*/
437 /* Take it after the last space */
438 szSearch
= _tcsrchr(str
, _T(' '));
440 _tcscpy(szSuffix
,szSearch
);
441 /* Find the closest to the end space or \ */
443 szSearch1
= _tcsrchr(str
, _T(' '));
444 szSearch2
= _tcsrchr(str
, _T('\\'));
445 szSearch3
= _tcsrchr(str
, _T('/'));
446 if(szSearch2
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch2
))
447 szSearch
= szSearch2
;
448 else if(szSearch3
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch3
))
449 szSearch
= szSearch3
;
451 szSearch
= szSearch1
;
453 szSearch
[0] = _T('\0');
454 _tcscpy(szPrefix
,str
);
458 /* All else fails and there is a lot of quotes, spaces and |
459 Then we search through and find the last space or \ that is
460 not inside a quotes */
461 for(i
= 0; i
< _tcslen(str
); i
++)
463 if(str
[i
] == _T('\"'))
465 if(str
[i
] == _T(' ') && !bInside
)
467 if((str
[i
] == _T(' ') || str
[i
] == _T('\\')) && !bInside
)
473 _tcscpy(szSuffix
,&strIN
[SBreak
]);
474 strIN
[PBreak
] = _T('\0');
475 _tcscpy(szPrefix
,strIN
);
476 if(szPrefix
[_tcslen(szPrefix
) - 2] == _T('\"'))
478 /* need to remove the " right before a \ at the end to
479 allow the next stuff to stay inside one set of quotes
480 otherwise you would have multiple sets of quotes*/
481 _tcscpy(&szPrefix
[_tcslen(szPrefix
) - 2],_T("\\"));
486 int __cdecl
compare(const void *arg1
,const void *arg2
)
492 File1
= malloc(sizeof(FileName
));
493 File2
= malloc(sizeof(FileName
));
497 memcpy(File1
,arg1
,sizeof(FileName
));
498 memcpy(File2
,arg2
,sizeof(FileName
));
500 /* ret = _tcsicmp(File1->Name, File2->Name); */
501 ret
= lstrcmpi(File1
->Name
, File2
->Name
);
508 VOID
CompleteFilename (LPTSTR strIN
, BOOL bNext
, LPTSTR strOut
, UINT cusor
)
510 /* Length of string before we complete it */
512 /* Length of string after completed */
514 /* The number of chars added too it */
515 static INT DiffLength
= 0;
516 /* Used to find and assemble the string that is returned */
517 TCHAR szBaseWord
[MAX_PATH
];
518 TCHAR szPrefix
[MAX_PATH
];
519 TCHAR szOrginal
[MAX_PATH
];
520 TCHAR szSearchPath
[MAX_PATH
];
521 /* Save the strings used last time, so if they hit tab again */
522 static TCHAR LastReturned
[MAX_PATH
];
523 static TCHAR LastSearch
[MAX_PATH
];
524 static TCHAR LastPrefix
[MAX_PATH
];
525 /* Used to search for files */
527 WIN32_FIND_DATA file
;
528 /* List of all the files */
529 FileName
* FileList
= NULL
;
530 /* Number of files */
531 INT FileListSize
= 0;
534 /* Editable string of what was passed in */
536 /* Keeps track of what element was last selected */
538 BOOL NeededQuote
= FALSE
;
540 TCHAR
* line
= strIN
;
542 strOut
[0] = _T('\0');
544 while (_istspace (*line
))
546 if(!_tcsnicmp (line
, _T("rd "), 3) || !_tcsnicmp (line
, _T("cd "), 3))
549 /* Copy the string, str can be edited and orginal should not be */
551 _tcscpy(szOrginal
,strIN
);
553 /* Look to see if the cusor is not at the end of the string */
554 if((cusor
+ 1) < _tcslen(str
))
555 str
[cusor
] = _T('\0');
557 /* Look to see if they hit tab again, if so cut off the diff length */
558 if(_tcscmp(str
,LastReturned
) || !_tcslen(str
))
560 /* We need to know how many chars we added from the start */
561 StartLength
= _tcslen(str
);
563 /* no string, we need all files in that directory */
566 _tcscat(str
,_T("*"));
569 /* Zero it out first */
570 szBaseWord
[0] = _T('\0');
571 szPrefix
[0] = _T('\0');
573 /*What comes out of this needs to be:
574 szBaseWord = path no quotes to the object
575 szPrefix = what leads up to the filename
576 no quote at the END of the full name */
577 FindPrefixAndSuffix(str
,szPrefix
,szBaseWord
);
579 for(i
= 0; i
< _tcslen(szBaseWord
); )
581 if(szBaseWord
[i
] == _T('\"'))
582 memmove(&szBaseWord
[i
],&szBaseWord
[i
+ 1], _tcslen(&szBaseWord
[i
]) * sizeof(TCHAR
));
588 memset(szSearchPath
, 0, sizeof(szSearchPath
));
590 /* Start the search for all the files */
591 GetFullPathName(szBaseWord
, MAX_PATH
, szSearchPath
, NULL
);
594 _tcscat(szSearchPath
,_T("*"));
596 _tcscpy(LastSearch
,szSearchPath
);
597 _tcscpy(LastPrefix
,szPrefix
);
601 _tcscpy(szSearchPath
, LastSearch
);
602 _tcscpy(szPrefix
, LastPrefix
);
605 /* search for the files it might be */
606 hFile
= FindFirstFile (szSearchPath
, &file
);
608 /* aseemble a list of all files names */
611 if(hFile
== INVALID_HANDLE_VALUE
)
613 /* Assemble the orginal string and return */
614 _tcscpy(strOut
,szOrginal
);
621 if(!_tcscmp (file
.cFileName
, _T(".")) ||
622 !_tcscmp (file
.cFileName
, _T("..")))
625 /* Don't show files when they are doing 'cd' or 'rd' */
627 file
.dwFileAttributes
!= 0xFFFFFFFF &&
628 !(file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
633 /* Add the file to the list of files */
637 FileList
= malloc(FileListSize
* sizeof(FileName
));
642 FileList
= realloc(FileList
, FileListSize
* sizeof(FileName
));
647 /* Assemble the orginal string and return */
648 _tcscpy(strOut
,szOrginal
);
650 ConOutFormatMessage (GetLastError());
653 /* Copies the file name into the struct */
654 _tcscpy(FileList
[FileListSize
-1].Name
,file
.cFileName
);
656 }while(FindNextFile(hFile
,&file
));
658 /* Check the size of the list to see if we
660 if(FileListSize
== 0)
662 _tcscpy(strOut
,szOrginal
);
670 qsort(FileList
,FileListSize
,sizeof(FileName
), compare
);
672 /* Find the next/previous */
673 if(!_tcscmp(szOrginal
,LastReturned
))
677 if(FileListSize
- 1 == Sel
)
685 Sel
= FileListSize
- 1;
695 /* nothing found that matched last time
696 so return the first thing in the list */
697 strOut
[0] = _T('\0');
699 /* space in the name */
700 if(_tcschr(FileList
[Sel
].Name
, _T(' ')))
704 /* It needs a " at the end */
708 /* Find the place to put the " at the start */
709 for(i
= 0; i
< _tcslen(szPrefix
); i
++)
711 if(szPrefix
[i
] == _T('\"'))
713 if(szPrefix
[i
] == _T(' ') && !bInside
)
717 /* insert the quoation and move things around */
718 if(szPrefix
[LastSpace
+ 1] != _T('\"') && LastSpace
!= -1)
720 memmove ( &szPrefix
[LastSpace
+1], &szPrefix
[LastSpace
], (_tcslen(szPrefix
)-LastSpace
+1) * sizeof(TCHAR
) );
722 if((UINT
)(LastSpace
+ 1) == _tcslen(szPrefix
))
724 _tcscat(szPrefix
,_T("\""));
726 szPrefix
[LastSpace
+ 1] = _T('\"');
728 else if(LastSpace
== -1)
730 _tcscpy(szBaseWord
,_T("\""));
731 _tcscat(szBaseWord
,szPrefix
);
732 _tcscpy(szPrefix
,szBaseWord
);
737 _tcscpy(strOut
,szPrefix
);
738 _tcscat(strOut
,FileList
[Sel
].Name
);
740 /* check for odd number of quotes means we need to close them */
743 for(i
= 0; i
< _tcslen(strOut
); i
++)
744 if(strOut
[i
] == _T('\"'))
745 NeededQuote
= !NeededQuote
;
748 if(szPrefix
[_tcslen(szPrefix
) - 1] == _T('\"') || NeededQuote
)
749 _tcscat(strOut
,_T("\""));
751 _tcscpy(LastReturned
,strOut
);
752 EndLength
= _tcslen(strOut
);
753 DiffLength
= EndLength
- StartLength
;