adacf9092fbcec622bcffad0139702a5f8650041
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.
25 #ifdef FEATURE_UNIX_FILENAME_COMPLETION
27 VOID
CompleteFilename (LPTSTR str
, UINT charcount
)
36 BOOL found_dot
= FALSE
;
37 BOOL perfectmatch
= TRUE
;
39 TCHAR fname
[MAX_PATH
];
40 TCHAR maxmatch
[MAX_PATH
] = _T("");
41 TCHAR directory
[MAX_PATH
];
44 /* expand current file name */
45 count
= charcount
- 1;
49 /* find how many '"'s there is typed already. */
53 if (str
[step
] == _T('"'))
57 /* if c is odd, then user typed " before name, else not. */
59 /* find front of word */
60 if (str
[count
] == _T('"') || (c
% 2))
63 while (count
> 0 && str
[count
] != _T('"'))
68 while (count
> 0 && str
[count
] != _T(' '))
72 /* if not at beginning, go forward 1 */
73 if (str
[count
] == _T(' '))
78 if (str
[count
] == _T('"'))
79 count
++; /* don't increment start */
81 /* extract directory from word */
82 _tcscpy (directory
, &str
[count
]);
83 curplace
= _tcslen (directory
) - 1;
85 if (curplace
>= 0 && directory
[curplace
] == _T('"'))
86 directory
[curplace
--] = _T('\0');
88 _tcscpy (path
, directory
);
90 while (curplace
>= 0 && directory
[curplace
] != _T('\\') &&
91 directory
[curplace
] != _T('/') &&
92 directory
[curplace
] != _T(':'))
94 directory
[curplace
] = 0;
98 /* look for a '.' in the filename */
99 for (count
= _tcslen (directory
); path
[count
] != _T('\0'); count
++)
101 if (path
[count
] == _T('.'))
109 _tcscat (path
, _T("*"));
111 _tcscat (path
, _T("*.*"));
116 hFile
= FindFirstFile (path
, &file
);
117 if (hFile
!= INVALID_HANDLE_VALUE
)
122 /* ignore "." and ".." */
123 if (!_tcscmp (file
.cFileName
, _T(".")) ||
124 !_tcscmp (file
.cFileName
, _T("..")))
127 _tcscpy (fname
, file
.cFileName
);
129 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
130 _tcscat (fname
, _T("\\"));
132 if (!maxmatch
[0] && perfectmatch
)
134 _tcscpy(maxmatch
, fname
);
138 for (count
= 0; maxmatch
[count
] && fname
[count
]; count
++)
140 if (tolower(maxmatch
[count
]) != tolower(fname
[count
]))
142 perfectmatch
= FALSE
;
148 if (maxmatch
[count
] == _T('\0') &&
149 fname
[count
] != _T('\0'))
150 perfectmatch
= FALSE
;
153 while (FindNextFile (hFile
, &file
));
157 /* only quote if the filename contains spaces */
158 if (_tcschr(directory
, _T(' ')) ||
159 _tcschr(maxmatch
, _T(' ')))
161 str
[start
] = _T('\"');
162 _tcscpy (&str
[start
+1], directory
);
163 _tcscat (&str
[start
], maxmatch
);
164 _tcscat (&str
[start
], _T("\"") );
168 _tcscpy (&str
[start
], directory
);
169 _tcscat (&str
[start
], maxmatch
);
179 /* no match found - search for internal command */
180 for (cmds_ptr
= cmds
; cmds_ptr
->name
; cmds_ptr
++)
182 if (!_tcsnicmp (&str
[start
], cmds_ptr
->name
,
183 _tcslen (&str
[start
])))
185 /* return the mach only if it is unique */
186 if (_tcsnicmp (&str
[start
], (cmds_ptr
+1)->name
, _tcslen (&str
[start
])))
187 _tcscpy (&str
[start
], cmds_ptr
->name
);
198 * returns 1 if at least one match, else returns 0
200 BOOL
ShowCompletionMatches (LPTSTR str
, INT charcount
)
202 WIN32_FIND_DATA file
;
204 BOOL found_dot
= FALSE
;
208 TCHAR path
[MAX_PATH
];
209 TCHAR fname
[MAX_PATH
];
210 TCHAR directory
[MAX_PATH
];
213 /* expand current file name */
214 count
= charcount
- 1;
218 /* find front of word */
219 if (str
[count
] == _T('"'))
222 while (count
> 0 && str
[count
] != _T('"'))
227 while (count
> 0 && str
[count
] != _T(' '))
231 /* if not at beginning, go forward 1 */
232 if (str
[count
] == _T(' '))
237 if (str
[count
] == _T('"'))
238 count
++; /* don't increment start */
240 /* extract directory from word */
241 _tcscpy (directory
, &str
[count
]);
242 curplace
= _tcslen (directory
) - 1;
244 if (curplace
>= 0 && directory
[curplace
] == _T('"'))
245 directory
[curplace
--] = _T('\0');
247 _tcscpy (path
, directory
);
249 while (curplace
>= 0 &&
250 directory
[curplace
] != _T('\\') &&
251 directory
[curplace
] != _T(':'))
253 directory
[curplace
] = 0;
257 /* look for a . in the filename */
258 for (count
= _tcslen (directory
); path
[count
] != _T('\0'); count
++)
260 if (path
[count
] == _T('.'))
268 _tcscat (path
, _T("*"));
270 _tcscat (path
, _T("*.*"));
275 hFile
= FindFirstFile (path
, &file
);
276 if (hFile
!= INVALID_HANDLE_VALUE
)
278 UINT longestfname
= 0;
279 /* Get the size of longest filename first. */
282 if (_tcslen(file
.cFileName
) > longestfname
)
284 longestfname
= _tcslen(file
.cFileName
);
285 /* Directories get extra brackets around them. */
286 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
290 while (FindNextFile (hFile
, &file
));
293 hFile
= FindFirstFile (path
, &file
);
295 /* Count the highest number of columns */
296 GetScreenSize(&screenwidth
, 0);
298 /* For counting columns of output */
301 /* Increase by the number of spaces behind file name */
305 ConOutChar(_T('\n'));
308 /* ignore . and .. */
309 if (!_tcscmp (file
.cFileName
, _T(".")) ||
310 !_tcscmp (file
.cFileName
, _T("..")))
313 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
314 _stprintf (fname
, _T("[%s]"), file
.cFileName
);
316 _tcscpy (fname
, file
.cFileName
);
318 ConOutPrintf (_T("%*s"), - longestfname
, fname
);
320 /* output as much columns as fits on the screen */
321 if (count
>= (screenwidth
/ longestfname
))
323 /* print the new line only if we aren't on the
324 * last column, in this case it wraps anyway */
325 if (count
* longestfname
!= (UINT
)screenwidth
)
326 ConOutChar(_T('\n'));
330 while (FindNextFile (hFile
, &file
));
335 ConOutChar(_T('\n'));
348 #ifdef FEATURE_4NT_FILENAME_COMPLETION
350 typedef struct _FileName
352 TCHAR Name
[MAX_PATH
];
355 VOID
FindPrefixAndSuffix(LPTSTR strIN
, LPTSTR szPrefix
, LPTSTR szSuffix
)
357 /* String that is to be examined */
359 /* temp pointers to used to find needed parts */
364 /* number of quotes in the string */
366 /* used in for loops */
368 /* Char number to break the string at */
371 /* when phrasing a string, this tells weather
372 you are inside quotes ot not. */
373 BOOL bInside
= FALSE
;
375 szPrefix
[0] = _T('\0');
376 szSuffix
[0] = _T('\0');
378 /* Copy over the string to later be edited */
381 /* Count number of " */
382 for(i
= 0; i
< _tcslen(str
); i
++)
384 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
)
472 _tcscpy(szSuffix
,&strIN
[SBreak
]);
473 strIN
[PBreak
] = _T('\0');
474 _tcscpy(szPrefix
,strIN
);
475 if (szPrefix
[_tcslen(szPrefix
) - 2] == _T('\"') &&
476 szPrefix
[_tcslen(szPrefix
) - 1] != _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("\\"));
485 int __cdecl
compare(const void *arg1
,const void *arg2
)
491 File1
= cmd_alloc(sizeof(FileName
));
495 File2
= cmd_alloc(sizeof(FileName
));
502 memcpy(File1
,arg1
,sizeof(FileName
));
503 memcpy(File2
,arg2
,sizeof(FileName
));
505 /* ret = _tcsicmp(File1->Name, File2->Name); */
506 ret
= lstrcmpi(File1
->Name
, File2
->Name
);
514 FileNameContainsSpecialCharacters(LPTSTR pszFileName
)
518 while ((chr
= *pszFileName
++) != _T('\0'))
520 if ((chr
== _T(' ')) ||
538 (chr
== 0xB4)) // '´'
548 VOID
CompleteFilename (LPTSTR strIN
, BOOL bNext
, LPTSTR strOut
, UINT cusor
)
550 /* Length of string before we complete it */
552 /* Length of string after completed */
554 /* The number of chars added too it */
555 //static INT DiffLength = 0;
556 /* Used to find and assemble the string that is returned */
557 TCHAR szBaseWord
[MAX_PATH
];
558 TCHAR szPrefix
[MAX_PATH
];
559 TCHAR szOriginal
[MAX_PATH
];
560 TCHAR szSearchPath
[MAX_PATH
];
561 /* Save the strings used last time, so if they hit tab again */
562 static TCHAR LastReturned
[MAX_PATH
];
563 static TCHAR LastSearch
[MAX_PATH
];
564 static TCHAR LastPrefix
[MAX_PATH
];
565 /* Used to search for files */
567 WIN32_FIND_DATA file
;
568 /* List of all the files */
569 FileName
* FileList
= NULL
;
570 /* Number of files */
571 INT FileListSize
= 0;
574 /* Editable string of what was passed in */
576 /* Keeps track of what element was last selected */
578 BOOL NeededQuote
= FALSE
;
580 TCHAR
* line
= strIN
;
582 strOut
[0] = _T('\0');
584 while (_istspace (*line
))
586 if (!_tcsnicmp (line
, _T("rd "), 3) || !_tcsnicmp (line
, _T("cd "), 3))
589 /* Copy the string, str can be edited and original should not be */
591 _tcscpy(szOriginal
,strIN
);
593 /* Look to see if the cusor is not at the end of the string */
594 if ((cusor
+ 1) < _tcslen(str
))
595 str
[cusor
] = _T('\0');
597 /* Look to see if they hit tab again, if so cut off the diff length */
598 if (_tcscmp(str
,LastReturned
) || !_tcslen(str
))
600 /* We need to know how many chars we added from the start */
601 StartLength
= _tcslen(str
);
603 /* no string, we need all files in that directory */
606 _tcscat(str
,_T("*"));
609 /* Zero it out first */
610 szBaseWord
[0] = _T('\0');
611 szPrefix
[0] = _T('\0');
613 /*What comes out of this needs to be:
614 szBaseWord = path no quotes to the object
615 szPrefix = what leads up to the filename
616 no quote at the END of the full name */
617 FindPrefixAndSuffix(str
,szPrefix
,szBaseWord
);
619 for(i
= 0; i
< _tcslen(szBaseWord
); )
621 if (szBaseWord
[i
] == _T('\"'))
622 memmove(&szBaseWord
[i
],&szBaseWord
[i
+ 1], _tcslen(&szBaseWord
[i
]) * sizeof(TCHAR
));
628 memset(szSearchPath
, 0, sizeof(szSearchPath
));
630 /* Start the search for all the files */
631 GetFullPathName(szBaseWord
, MAX_PATH
, szSearchPath
, NULL
);
633 /* Got a device path? Fallback to the the current dir plus the short path */
634 if (szSearchPath
[0] == _T('\\') && szSearchPath
[1] == _T('\\') &&
635 szSearchPath
[2] == _T('.') && szSearchPath
[3] == _T('\\'))
637 GetCurrentDirectory(MAX_PATH
, szSearchPath
);
638 _tcscat(szSearchPath
, _T("\\"));
639 _tcscat(szSearchPath
, szBaseWord
);
644 _tcscat(szSearchPath
,_T("*"));
646 _tcscpy(LastSearch
,szSearchPath
);
647 _tcscpy(LastPrefix
,szPrefix
);
651 _tcscpy(szSearchPath
, LastSearch
);
652 _tcscpy(szPrefix
, LastPrefix
);
655 /* search for the files it might be */
656 hFile
= FindFirstFile (szSearchPath
, &file
);
657 if (hFile
== INVALID_HANDLE_VALUE
)
659 /* Assemble the original string and return */
660 _tcscpy(strOut
,szOriginal
);
664 /* aseemble a list of all files names */
667 FileName
* oldFileList
= FileList
;
669 if (!_tcscmp (file
.cFileName
, _T(".")) ||
670 !_tcscmp (file
.cFileName
, _T("..")))
673 /* Don't show files when they are doing 'cd' or 'rd' */
675 file
.dwFileAttributes
!= 0xFFFFFFFF &&
676 !(file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
681 /* Add the file to the list of files */
682 FileList
= cmd_realloc(FileList
, ++FileListSize
* sizeof(FileName
));
684 if (FileList
== NULL
)
686 /* Don't leak old buffer */
687 cmd_free(oldFileList
);
688 /* Assemble the original string and return */
689 _tcscpy(strOut
,szOriginal
);
691 ConOutFormatMessage (GetLastError());
694 /* Copies the file name into the struct */
695 _tcscpy(FileList
[FileListSize
-1].Name
,file
.cFileName
);
697 } while(FindNextFile(hFile
,&file
));
701 /* Check the size of the list to see if we found any matches */
702 if (FileListSize
== 0)
704 _tcscpy(strOut
,szOriginal
);
705 if (FileList
!= NULL
)
711 qsort(FileList
,FileListSize
,sizeof(FileName
), compare
);
713 /* Find the next/previous */
714 if (_tcslen(szOriginal
) && !_tcscmp(szOriginal
,LastReturned
))
718 if (FileListSize
- 1 == Sel
)
726 Sel
= FileListSize
- 1;
736 /* nothing found that matched last time so return the first thing in the list */
737 strOut
[0] = _T('\0');
739 /* Special character in the name */
740 if (FileNameContainsSpecialCharacters(FileList
[Sel
].Name
))
744 /* It needs a " at the end */
748 /* Find the place to put the " at the start */
749 for(i
= 0; i
< _tcslen(szPrefix
); i
++)
751 if (szPrefix
[i
] == _T('\"'))
753 if (szPrefix
[i
] == _T(' ') && !bInside
)
757 /* insert the quotation and move things around */
758 if (szPrefix
[LastSpace
+ 1] != _T('\"') && LastSpace
!= -1)
760 memmove ( &szPrefix
[LastSpace
+1], &szPrefix
[LastSpace
], (_tcslen(szPrefix
)-LastSpace
+1) * sizeof(TCHAR
) );
762 if ((UINT
)(LastSpace
+ 1) == _tcslen(szPrefix
))
764 _tcscat(szPrefix
,_T("\""));
766 szPrefix
[LastSpace
+ 1] = _T('\"');
768 else if (LastSpace
== -1)
770 /* Add quotation only if none exists already */
771 if (szPrefix
[0] != _T('\"'))
773 _tcscpy(szBaseWord
,_T("\""));
774 _tcscat(szBaseWord
,szPrefix
);
775 _tcscpy(szPrefix
,szBaseWord
);
780 _tcscpy(strOut
,szPrefix
);
781 _tcscat(strOut
,FileList
[Sel
].Name
);
783 /* check for odd number of quotes means we need to close them */
786 for(i
= 0; i
< _tcslen(strOut
); i
++)
788 if (strOut
[i
] == _T('\"'))
789 NeededQuote
= !NeededQuote
;
793 if (NeededQuote
|| (_tcslen(szPrefix
) && szPrefix
[_tcslen(szPrefix
) - 1] == _T('\"')))
794 _tcscat(strOut
,_T("\""));
796 _tcscpy(LastReturned
,strOut
);
797 //EndLength = _tcslen(strOut);
798 //DiffLength = EndLength - StartLength;
799 if (FileList
!= NULL
)