9042a29074f1140224dda87f28c6ba669cd34e6c
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!
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(':'))
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 */
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
++)
383 if(str
[i
] == _T('\"'))
386 /* Find the prefix and suffix */
387 if(nQuotes
% 2 && nQuotes
> 1)
389 /* Odd number of quotes. Just start from the last " */
390 /* THis is the way MS does it, and is an easy way out */
391 szSearch
= _tcsrchr(str
, _T('\"'));
392 /* Move to the next char past the " */
394 _tcscpy(szSuffix
,szSearch
);
395 /* Find the one closest to end */
396 szSearch1
= _tcsrchr(str
, _T('\"'));
397 szSearch2
= _tcsrchr(str
, _T('\\'));
398 if(szSearch2
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch2
))
399 szSearch
= szSearch2
;
401 szSearch
= szSearch1
;
402 /* Move one char past */
404 szSearch
[0] = _T('\0');
405 _tcscpy(szPrefix
,str
);
410 if(!_tcschr(str
, _T(' ')))
412 /* No spaces, everything goes to Suffix */
413 _tcscpy(szSuffix
,str
);
414 /* look for a slash just in case */
415 szSearch
= _tcsrchr(str
, _T('\\'));
419 szSearch
[0] = _T('\0');
420 _tcscpy(szPrefix
,str
);
424 szPrefix
[0] = _T('\0');
431 /* No quotes, and there is a space*/
432 /* Take it after the last space */
433 szSearch
= _tcsrchr(str
, _T(' '));
435 _tcscpy(szSuffix
,szSearch
);
436 /* Find the closest to the end space or \ */
438 szSearch1
= _tcsrchr(str
, _T(' '));
439 szSearch2
= _tcsrchr(str
, _T('\\'));
440 if(szSearch2
!= NULL
&& _tcslen(szSearch1
) > _tcslen(szSearch2
))
441 szSearch
= szSearch2
;
443 szSearch
= szSearch1
;
445 szSearch
[0] = _T('\0');
446 _tcscpy(szPrefix
,str
);
450 /* All else fails and there is a lot of quotes, spaces and |
451 Then we search through and find the last space or \ that is
452 not inside a quotes */
453 for(i
= 0; i
< _tcslen(str
); i
++)
455 if(str
[i
] == _T('\"'))
457 if(str
[i
] == _T(' ') && !bInside
)
459 if((str
[i
] == _T(' ') || str
[i
] == _T('\\')) && !bInside
)
465 _tcscpy(szSuffix
,&strIN
[SBreak
]);
466 strIN
[PBreak
] = _T('\0');
467 _tcscpy(szPrefix
,strIN
);
468 if(szPrefix
[_tcslen(szPrefix
) - 2] == _T('\"'))
470 /* need to remove the " right before a \ at the end to
471 allow the next stuff to stay inside one set of quotes
472 otherwise you would have multiple sets of quotes*/
473 _tcscpy(&szPrefix
[_tcslen(szPrefix
) - 2],_T("\\"));
478 int __cdecl
compare(const void *arg1
,const void *arg2
)
484 File1
= malloc(sizeof(FileName
));
485 File2
= malloc(sizeof(FileName
));
489 memcpy(File1
,arg1
,sizeof(FileName
));
490 memcpy(File2
,arg2
,sizeof(FileName
));
492 /* ret = _tcsicmp(File1->Name, File2->Name); */
493 ret
= lstrcmpi(File1
->Name
, File2
->Name
);
500 VOID
CompleteFilename (LPTSTR strIN
, BOOL bNext
, LPTSTR strOut
, UINT cusor
)
502 /* Length of string before we complete it */
504 /* Length of string after completed */
506 /* The number of chars added too it */
507 static INT DiffLength
= 0;
508 /* Used to find and assemble the string that is returned */
509 TCHAR szBaseWord
[MAX_PATH
];
510 TCHAR szPrefix
[MAX_PATH
];
511 TCHAR szOrginal
[MAX_PATH
];
512 TCHAR szSearchPath
[MAX_PATH
];
513 /* Save the strings used last time, so if they hit tab again */
514 static TCHAR LastReturned
[MAX_PATH
];
515 static TCHAR LastSearch
[MAX_PATH
];
516 static TCHAR LastPrefix
[MAX_PATH
];
517 /* Used to search for files */
519 WIN32_FIND_DATA file
;
520 /* List of all the files */
521 FileName
* FileList
= NULL
;
522 /* Number of files */
523 INT FileListSize
= 0;
526 /* Editable string of what was passed in */
528 /* Keeps track of what element was last selected */
530 BOOL NeededQuote
= FALSE
;
532 TCHAR
* line
= strIN
;
534 strOut
[0] = _T('\0');
536 while (_istspace (*line
))
538 if(!_tcsnicmp (line
, _T("rd "), 3) || !_tcsnicmp (line
, _T("cd "), 3))
541 /* Copy the string, str can be edited and orginal should not be */
543 _tcscpy(szOrginal
,strIN
);
545 /* Look to see if the cusor is not at the end of the string */
546 if((cusor
+ 1) < _tcslen(str
))
547 str
[cusor
] = _T('\0');
549 /* Look to see if they hit tab again, if so cut off the diff length */
550 if(_tcscmp(str
,LastReturned
) || !_tcslen(str
))
552 /* We need to know how many chars we added from the start */
553 StartLength
= _tcslen(str
);
555 /* no string, we need all files in that directory */
558 _tcscat(str
,_T("*"));
561 /* Zero it out first */
562 szBaseWord
[0] = _T('\0');
563 szPrefix
[0] = _T('\0');
565 /*What comes out of this needs to be:
566 szBaseWord = path no quotes to the object
567 szPrefix = what leads up to the filename
568 no quote at the END of the full name */
569 FindPrefixAndSuffix(str
,szPrefix
,szBaseWord
);
571 for(i
= 0; i
< _tcslen(szBaseWord
); )
573 if(szBaseWord
[i
] == _T('\"'))
574 memmove(&szBaseWord
[i
],&szBaseWord
[i
+ 1], _tcslen(&szBaseWord
[i
]) * sizeof(TCHAR
));
580 memset(szSearchPath
, 0, sizeof(szSearchPath
));
582 /* Start the search for all the files */
583 GetFullPathName(szBaseWord
, MAX_PATH
, szSearchPath
, NULL
);
585 _tcscat(szSearchPath
,_T("*"));
586 _tcscpy(LastSearch
,szSearchPath
);
587 _tcscpy(LastPrefix
,szPrefix
);
591 _tcscpy(szSearchPath
, LastSearch
);
592 _tcscpy(szPrefix
, LastPrefix
);
595 /* search for the files it might be */
596 hFile
= FindFirstFile (szSearchPath
, &file
);
598 /* aseemble a list of all files names */
601 if(hFile
== INVALID_HANDLE_VALUE
)
603 /* Assemble the orginal string and return */
604 _tcscpy(strOut
,szOrginal
);
611 if(!_tcscmp (file
.cFileName
, _T(".")) ||
612 !_tcscmp (file
.cFileName
, _T("..")))
615 /* Don't show files when they are doing 'cd' or 'rd' */
618 DWORD attr
= GetFileAttributes (file
.cFileName
);
619 if(attr
!= 0xFFFFFFFF && (!(attr
& FILE_ATTRIBUTE_DIRECTORY
)))
623 /* Add the file to the list of files */
627 FileList
= malloc(FileListSize
* sizeof(FileName
));
632 FileList
= realloc(FileList
, FileListSize
* sizeof(FileName
));
637 /* Assemble the orginal string and return */
638 _tcscpy(strOut
,szOrginal
);
640 ConOutFormatMessage (GetLastError());
643 /* Copies the file name into the struct */
644 _tcscpy(FileList
[FileListSize
-1].Name
,file
.cFileName
);
646 }while(FindNextFile(hFile
,&file
));
648 /* Check the size of the list to see if we
650 if(FileListSize
== 0)
652 _tcscpy(strOut
,szOrginal
);
660 qsort(FileList
,FileListSize
,sizeof(FileName
), compare
);
662 /* Find the next/previous */
663 if(!_tcscmp(szOrginal
,LastReturned
))
667 if(FileListSize
- 1 == Sel
)
675 Sel
= FileListSize
- 1;
685 /* nothing found that matched last time
686 so return the first thing in the list */
687 strOut
[0] = _T('\0');
689 /* space in the name */
690 if(_tcschr(FileList
[Sel
].Name
, _T(' ')))
694 /* It needs a " at the end */
698 /* Find the place to put the " at the start */
699 for(i
= 0; i
< _tcslen(szPrefix
); i
++)
701 if(szPrefix
[i
] == _T('\"'))
703 if(szPrefix
[i
] == _T(' ') && !bInside
)
707 /* insert the quoation and move things around */
708 if(szPrefix
[LastSpace
+ 1] != _T('\"') && LastSpace
!= -1)
710 memmove ( &szPrefix
[LastSpace
+1], &szPrefix
[LastSpace
], (_tcslen(szPrefix
)-LastSpace
+1) * sizeof(TCHAR
) );
712 if((UINT
)(LastSpace
+ 1) == _tcslen(szPrefix
))
714 _tcscat(szPrefix
,_T("\""));
716 szPrefix
[LastSpace
+ 1] = _T('\"');
718 else if(LastSpace
== -1)
720 _tcscpy(szBaseWord
,_T("\""));
721 _tcscat(szBaseWord
,szPrefix
);
722 _tcscpy(szPrefix
,szBaseWord
);
727 _tcscpy(strOut
,szPrefix
);
728 _tcscat(strOut
,FileList
[Sel
].Name
);
730 /* check for odd number of quotes means we need to close them */
733 for(i
= 0; i
< _tcslen(strOut
); i
++)
734 if(strOut
[i
] == _T('\"'))
735 NeededQuote
= !NeededQuote
;
738 if(szPrefix
[_tcslen(szPrefix
) - 1] == _T('\"') || NeededQuote
)
739 _tcscat(strOut
,_T("\""));
741 _tcscpy(LastReturned
,strOut
);
742 EndLength
= _tcslen(strOut
);
743 DiffLength
= EndLength
- StartLength
;