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
201 BOOL
ShowCompletionMatches (LPTSTR str
, INT charcount
)
203 WIN32_FIND_DATA file
;
205 BOOL found_dot
= FALSE
;
209 TCHAR path
[MAX_PATH
];
210 TCHAR fname
[MAX_PATH
];
211 TCHAR directory
[MAX_PATH
];
212 UINT longestfname
= 0;
215 /* expand current file name */
216 count
= charcount
- 1;
220 /* find front of word */
221 if (str
[count
] == _T('"'))
224 while (count
> 0 && str
[count
] != _T('"'))
229 while (count
> 0 && str
[count
] != _T(' '))
233 /* if not at beginning, go forward 1 */
234 if (str
[count
] == _T(' '))
239 if (str
[count
] == _T('"'))
240 count
++; /* don't increment start */
242 /* extract directory from word */
243 _tcscpy (directory
, &str
[count
]);
244 curplace
= _tcslen (directory
) - 1;
246 if (curplace
>= 0 && directory
[curplace
] == _T('"'))
247 directory
[curplace
--] = _T('\0');
249 _tcscpy (path
, directory
);
251 while (curplace
>= 0 &&
252 directory
[curplace
] != _T('\\') &&
253 directory
[curplace
] != _T(':'))
255 directory
[curplace
] = 0;
259 /* look for a . in the filename */
260 for (count
= _tcslen (directory
); path
[count
] != _T('\0'); count
++)
262 if (path
[count
] == _T('.'))
270 _tcscat (path
, _T("*"));
272 _tcscat (path
, _T("*.*"));
277 hFile
= FindFirstFile (path
, &file
);
278 if (hFile
!= INVALID_HANDLE_VALUE
)
280 /* Get the size of longest filename first. */
283 if (_tcslen(file
.cFileName
) > longestfname
)
285 longestfname
= _tcslen(file
.cFileName
);
286 /* Directories get extra brackets around them. */
287 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
291 while (FindNextFile (hFile
, &file
));
294 hFile
= FindFirstFile (path
, &file
);
296 /* Count the highest number of columns */
297 GetScreenSize(&screenwidth
, 0);
299 /* For counting columns of output */
302 /* Increase by the number of spaces behind file name */
306 ConOutChar (_T('\n'));
309 /* ignore . and .. */
310 if (!_tcscmp (file
.cFileName
, _T(".")) ||
311 !_tcscmp (file
.cFileName
, _T("..")))
314 if (file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
315 _stprintf (fname
, _T("[%s]"), file
.cFileName
);
317 _tcscpy (fname
, file
.cFileName
);
319 ConOutPrintf (_T("%*s"), - longestfname
, fname
);
321 /* output as much columns as fits on the screen */
322 if (count
>= (screenwidth
/ longestfname
))
324 /* print the new line only if we aren't on the
325 * last column, in this case it wraps anyway */
326 if (count
* longestfname
!= (UINT
)screenwidth
)
327 ConOutPrintf (_T("\n"));
331 while (FindNextFile (hFile
, &file
));
336 ConOutChar (_T('\n'));
349 #ifdef FEATURE_4NT_FILENAME_COMPLETION
351 typedef struct _FileName
353 TCHAR Name
[MAX_PATH
];
356 VOID
FindPrefixAndSuffix(LPTSTR strIN
, LPTSTR szPrefix
, LPTSTR szSuffix
)
358 /* String that is to be examined */
360 /* temp pointers to used to find needed parts */
365 /* number of quotes in the string */
367 /* used in for loops */
369 /* Char number to break the string at */
372 /* when phrasing a string, this tells weather
373 you are inside quotes ot not. */
374 BOOL bInside
= FALSE
;
376 szPrefix
[0] = _T('\0');
377 szSuffix
[0] = _T('\0');
379 /* Copy over the string to later be edited */
382 /* Count number of " */
383 for(i
= 0; i
< _tcslen(str
); i
++)
384 if(str
[i
] == _T('\"'))
387 /* Find the prefix and suffix */
388 if(nQuotes
% 2 && nQuotes
>= 1)
390 /* Odd number of quotes. Just start from the last " */
391 /* THis is the way MS does it, and is an easy way out */
392 szSearch
= _tcsrchr(str
, _T('\"'));
393 /* Move to the next char past the " */
395 _tcscpy(szSuffix
,szSearch
);
396 /* Find the one closest to end */
397 szSearch1
= _tcsrchr(str
, _T('\"'));
398 szSearch2
= _tcsrchr(str
, _T('\\'));
399 szSearch3
= _tcsrchr(str
, _T('.'));
400 if(szSearch2
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch2
))
401 szSearch
= szSearch2
;
402 else if(szSearch3
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch3
))
403 szSearch
= szSearch3
;
405 szSearch
= szSearch1
;
406 /* Move one char past */
408 szSearch
[0] = _T('\0');
409 _tcscpy(szPrefix
,str
);
414 if(!_tcschr(str
, _T(' ')))
416 /* No spaces, everything goes to Suffix */
417 _tcscpy(szSuffix
,str
);
418 /* look for a slash just in case */
419 szSearch
= _tcsrchr(str
, _T('\\'));
423 szSearch
[0] = _T('\0');
424 _tcscpy(szPrefix
,str
);
428 szPrefix
[0] = _T('\0');
435 /* No quotes, and there is a space*/
436 /* Take it after the last space */
437 szSearch
= _tcsrchr(str
, _T(' '));
439 _tcscpy(szSuffix
,szSearch
);
440 /* Find the closest to the end space or \ */
442 szSearch1
= _tcsrchr(str
, _T(' '));
443 szSearch2
= _tcsrchr(str
, _T('\\'));
444 szSearch3
= _tcsrchr(str
, _T('/'));
445 if(szSearch2
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch2
))
446 szSearch
= szSearch2
;
447 else if(szSearch3
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch3
))
448 szSearch
= szSearch3
;
450 szSearch
= szSearch1
;
452 szSearch
[0] = _T('\0');
453 _tcscpy(szPrefix
,str
);
457 /* All else fails and there is a lot of quotes, spaces and |
458 Then we search through and find the last space or \ that is
459 not inside a quotes */
460 for(i
= 0; i
< _tcslen(str
); i
++)
462 if(str
[i
] == _T('\"'))
464 if(str
[i
] == _T(' ') && !bInside
)
466 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("\\"));
486 int __cdecl
compare(const void *arg1
,const void *arg2
)
492 File1
= cmd_alloc(sizeof(FileName
));
493 File2
= cmd_alloc(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
);
607 if(hFile
== INVALID_HANDLE_VALUE
)
609 /* Assemble the orginal string and return */
610 _tcscpy(strOut
,szOrginal
);
614 /* aseemble a list of all files names */
617 if(!_tcscmp (file
.cFileName
, _T(".")) ||
618 !_tcscmp (file
.cFileName
, _T("..")))
621 /* Don't show files when they are doing 'cd' or 'rd' */
623 file
.dwFileAttributes
!= 0xFFFFFFFF &&
624 !(file
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
))
629 /* Add the file to the list of files */
630 FileList
= cmd_realloc(FileList
, ++FileListSize
* sizeof(FileName
));
634 /* Assemble the orginal string and return */
635 _tcscpy(strOut
,szOrginal
);
637 ConOutFormatMessage (GetLastError());
640 /* Copies the file name into the struct */
641 _tcscpy(FileList
[FileListSize
-1].Name
,file
.cFileName
);
643 } while(FindNextFile(hFile
,&file
));
647 /* Check the size of the list to see if we
649 if(FileListSize
== 0)
651 _tcscpy(strOut
,szOrginal
);
658 qsort(FileList
,FileListSize
,sizeof(FileName
), compare
);
660 /* Find the next/previous */
661 if(!_tcscmp(szOrginal
,LastReturned
))
665 if(FileListSize
- 1 == Sel
)
673 Sel
= FileListSize
- 1;
683 /* nothing found that matched last time
684 so return the first thing in the list */
685 strOut
[0] = _T('\0');
687 /* space in the name */
688 if(_tcschr(FileList
[Sel
].Name
, _T(' ')))
692 /* It needs a " at the end */
696 /* Find the place to put the " at the start */
697 for(i
= 0; i
< _tcslen(szPrefix
); i
++)
699 if(szPrefix
[i
] == _T('\"'))
701 if(szPrefix
[i
] == _T(' ') && !bInside
)
705 /* insert the quoation and move things around */
706 if(szPrefix
[LastSpace
+ 1] != _T('\"') && LastSpace
!= -1)
708 memmove ( &szPrefix
[LastSpace
+1], &szPrefix
[LastSpace
], (_tcslen(szPrefix
)-LastSpace
+1) * sizeof(TCHAR
) );
710 if((UINT
)(LastSpace
+ 1) == _tcslen(szPrefix
))
712 _tcscat(szPrefix
,_T("\""));
714 szPrefix
[LastSpace
+ 1] = _T('\"');
716 else if(LastSpace
== -1)
718 _tcscpy(szBaseWord
,_T("\""));
719 _tcscat(szBaseWord
,szPrefix
);
720 _tcscpy(szPrefix
,szBaseWord
);
725 _tcscpy(strOut
,szPrefix
);
726 _tcscat(strOut
,FileList
[Sel
].Name
);
728 /* check for odd number of quotes means we need to close them */
731 for(i
= 0; i
< _tcslen(strOut
); i
++)
732 if(strOut
[i
] == _T('\"'))
733 NeededQuote
= !NeededQuote
;
736 if(szPrefix
[_tcslen(szPrefix
) - 1] == _T('\"') || NeededQuote
)
737 _tcscat(strOut
,_T("\""));
739 _tcscpy(LastReturned
,strOut
);
740 EndLength
= _tcslen(strOut
);
741 DiffLength
= EndLength
- StartLength
;