2 * MISC.C - misc. functions.
11 * moved functions in here
13 * 27-Jul-1998 (John P Price <linux-guru@gcfl.net>)
14 * added config.h include
16 * 18-Dec-1998 (Eric Kohl)
17 * Changed split() to accept quoted arguments.
18 * Removed parse_firstarg().
20 * 23-Jan-1999 (Eric Kohl)
21 * Fixed an ugly bug in split(). In rare cases (last character
22 * of the string is a space) it ignored the NULL character and
23 * tried to add the following to the argument list.
25 * 28-Jan-1999 (Eric Kohl)
26 * FileGetString() seems to be working now.
28 * 06-Nov-1999 (Eric Kohl)
29 * Added PagePrompt() and FilePrompt().
31 * 30-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
32 * Remove all hardcoded strings in En.rc
38 * get a character out-of-band and honor Ctrl-Break characters
43 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
44 INPUT_RECORD irBuffer
;
49 ReadConsoleInput (hInput
, &irBuffer
, 1, &dwRead
);
50 if ((irBuffer
.EventType
== KEY_EVENT
) &&
51 (irBuffer
.Event
.KeyEvent
.bKeyDown
!= FALSE
))
53 if (irBuffer
.Event
.KeyEvent
.dwControlKeyState
&
54 (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))
56 if (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== 'C')
62 else if ((irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
63 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
64 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
))
77 return irBuffer
.Event
.KeyEvent
.uChar
.AsciiChar
;
79 return irBuffer
.Event
.KeyEvent
.uChar
.UnicodeChar
;
84 * Takes a path in and returns it with the correct case of the letters
86 VOID
GetPathCase( TCHAR
* Path
, TCHAR
* OutPath
)
89 TCHAR TempPath
[MAX_PATH
];
90 WIN32_FIND_DATA FindFileData
;
92 _tcscpy(TempPath
, _T(""));
93 _tcscpy(OutPath
, _T(""));
95 for(i
= 0; i
< _tcslen(Path
); i
++)
97 if (Path
[i
] != _T('\\'))
99 _tcsncat(TempPath
, &Path
[i
], 1);
100 if (i
!= _tcslen(Path
) - 1)
103 /* Handle the base part of the path different.
104 Because if you put it into findfirstfile, it will
105 return your current folder */
106 if (_tcslen(TempPath
) == 2 && TempPath
[1] == _T(':'))
108 _tcscat(OutPath
, TempPath
);
109 _tcscat(OutPath
, _T("\\"));
110 _tcscat(TempPath
, _T("\\"));
114 hFind
= FindFirstFile(TempPath
,&FindFileData
);
115 if (hFind
== INVALID_HANDLE_VALUE
)
117 _tcscpy(OutPath
, Path
);
120 _tcscat(TempPath
, _T("\\"));
121 _tcscat(OutPath
, FindFileData
.cFileName
);
122 _tcscat(OutPath
, _T("\\"));
129 * Check if Ctrl-Break was pressed during the last calls
132 BOOL
CheckCtrlBreak(INT mode
)
134 static BOOL bLeaveAll
= FALSE
; /* leave all batch files */
135 TCHAR options
[4]; /* Yes, No, All */
140 case BREAK_OUTOFBATCH
:
144 case BREAK_BATCHFILE
:
152 LoadString(CMD_ModuleHandle
, STRING_COPY_OPTION
, options
, ARRAYSIZE(options
));
154 /* we need to be sure the string arrives on the screen! */
157 ConOutResPuts(STRING_CANCEL_BATCH_FILE
);
158 c
= _totupper(cgetchar());
159 } while (!(_tcschr(options
, c
) || c
== _T('\3')) || !c
);
161 ConOutChar(_T('\n'));
165 bCtrlBreak
= FALSE
; /* ignore */
169 /* leave all batch files */
170 bLeaveAll
= ((c
== options
[2]) || (c
== _T('\3')));
180 /* state processed */
184 /* add new entry for new argument */
185 BOOL
add_entry (LPINT ac
, LPTSTR
**arg
, LPCTSTR entry
)
190 q
= cmd_alloc ((_tcslen(entry
) + 1) * sizeof (TCHAR
));
193 WARN("Cannot allocate memory for q!\n");
199 *arg
= cmd_realloc (oldarg
, (*ac
+ 2) * sizeof (LPTSTR
));
202 WARN("Cannot reallocate memory for arg!\n");
210 (*arg
)[++(*ac
)] = NULL
;
215 static BOOL
expand (LPINT ac
, LPTSTR
**arg
, LPCTSTR pattern
)
218 WIN32_FIND_DATA FindData
;
221 LPTSTR dirpart
, fullname
;
223 pathend
= _tcsrchr (pattern
, _T('\\'));
226 dirpart
= cmd_alloc((pathend
- pattern
+ 2) * sizeof(TCHAR
));
229 WARN("Cannot allocate memory for dirpart!\n");
232 memcpy(dirpart
, pattern
, pathend
- pattern
+ 1);
233 dirpart
[pathend
- pattern
+ 1] = _T('\0');
239 hFind
= FindFirstFile (pattern
, &FindData
);
240 if (INVALID_HANDLE_VALUE
!= hFind
)
246 fullname
= cmd_alloc((_tcslen(dirpart
) + _tcslen(FindData
.cFileName
) + 1) * sizeof(TCHAR
));
249 WARN("Cannot allocate memory for fullname!\n");
254 _tcscat (_tcscpy (fullname
, dirpart
), FindData
.cFileName
);
255 ok
= add_entry(ac
, arg
, fullname
);
261 ok
= add_entry(ac
, arg
, FindData
.cFileName
);
263 } while (FindNextFile (hFind
, &FindData
) && ok
);
268 ok
= add_entry(ac
, arg
, pattern
);
280 * split - splits a line up into separate arguments, delimiters
281 * are spaces and slashes ('/').
283 LPTSTR
*split (LPTSTR s
, LPINT args
, BOOL expand_wildcards
, BOOL handle_plus
)
291 arg
= cmd_alloc (sizeof (LPTSTR
));
294 WARN("Cannot allocate memory for arg!\n");
302 BOOL bQuoted
= FALSE
;
304 /* skip leading spaces */
305 while (*s
&& (_istspace(*s
) || _istcntrl(*s
)))
310 /* the first character can be '/' */
314 /* skip to next word delimiter or start of next option */
315 while (_istprint(*s
))
317 /* if quote (") then set bQuoted */
318 bQuoted
^= (*s
== _T('\"'));
320 /* Check if we have unquoted text */
323 /* check for separators */
326 (handle_plus
&& (*s
== _T('+'))))
328 /* Make length at least one character */
337 /* a word was found */
340 q
= cmd_alloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
343 WARN("Cannot allocate memory for q!\n");
346 memcpy (q
, start
, len
* sizeof (TCHAR
));
349 if (expand_wildcards
&& (_T('/') != *start
) &&
350 (NULL
!= _tcschr(q
, _T('*')) || NULL
!= _tcschr(q
, _T('?'))))
352 if (! expand(&ac
, &arg
, q
))
361 if (! add_entry(&ac
, &arg
, q
))
378 * splitspace() is a function which uses JUST spaces as delimiters. split() uses "/" AND spaces.
379 * The way it works is real similar to split(), search the difference ;)
380 * splitspace is needed for commands such as "move" where paths as C:\this/is\allowed/ are allowed
382 LPTSTR
*splitspace (LPTSTR s
, LPINT args
)
390 arg
= cmd_alloc (sizeof (LPTSTR
));
393 WARN("Cannot allocate memory for arg!\n");
401 BOOL bQuoted
= FALSE
;
403 /* skip leading spaces */
404 while (*s
&& (_istspace (*s
) || _istcntrl (*s
)))
409 /* skip to next word delimiter or start of next option */
410 while (_istprint(*s
) && (bQuoted
|| !_istspace(*s
)))
412 /* if quote (") then set bQuoted */
413 bQuoted
^= (*s
== _T('\"'));
417 /* a word was found */
420 q
= cmd_alloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
423 WARN("Cannot allocate memory for q!\n");
426 memcpy (q
, start
, len
* sizeof (TCHAR
));
429 if (! add_entry(&ac
, &arg
, q
))
445 * freep -- frees memory used for a call to split
447 VOID
freep (LPTSTR
*p
)
461 LPTSTR
_stpcpy (LPTSTR dest
, LPCTSTR src
)
464 return (dest
+ _tcslen (src
));
468 StripQuotes(TCHAR
*in
)
481 * Checks if a path is valid (accessible)
483 BOOL
IsValidPathName (LPCTSTR pszPath
)
485 TCHAR szOldPath
[MAX_PATH
];
488 GetCurrentDirectory (MAX_PATH
, szOldPath
);
489 bResult
= SetCurrentDirectory (pszPath
);
491 SetCurrentDirectory (szOldPath
);
498 * Checks if a file exists (accessible)
500 BOOL
IsExistingFile (LPCTSTR pszPath
)
502 DWORD attr
= GetFileAttributes (pszPath
);
503 return (attr
!= 0xFFFFFFFF && (! (attr
& FILE_ATTRIBUTE_DIRECTORY
)) );
507 BOOL
IsExistingDirectory (LPCTSTR pszPath
)
509 DWORD attr
= GetFileAttributes (pszPath
);
510 return (attr
!= 0xFFFFFFFF && (attr
& FILE_ATTRIBUTE_DIRECTORY
) );
514 BOOL
FileGetString (HANDLE hFile
, LPTSTR lpBuffer
, INT nBufferLength
)
520 lpString
= cmd_alloc(nBufferLength
);
525 if (ReadFile(hFile
, lpString
, nBufferLength
- 1, &dwRead
, NULL
))
527 /* break at new line*/
528 CHAR
*end
= memchr(lpString
, '\n', dwRead
);
532 len
= (INT
)(end
- lpString
) + 1;
533 SetFilePointer(hFile
, len
- dwRead
, NULL
, FILE_CURRENT
);
545 lpString
[len
++] = '\0';
547 MultiByteToWideChar(OutputCodePage
, 0, lpString
, -1, lpBuffer
, len
);
554 BOOL __stdcall
PagePrompt(PCON_PAGER Pager
, DWORD Done
, DWORD Total
)
556 SHORT iScreenWidth
, iCursorY
;
559 ConOutResPuts(STRING_MISC_HELP1
);
561 RemoveBreakHandler();
568 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
569 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
570 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
576 * Get the screen width, erase the full line where the cursor is,
577 * and move the cursor back to the beginning of the line.
579 GetScreenSize(&iScreenWidth
, NULL
);
580 iCursorY
= GetCursorY();
581 SetCursorXY(0, iCursorY
);
582 while (iScreenWidth
-- > 0) // Or call FillConsoleOutputCharacter ?
584 SetCursorXY(0, iCursorY
);
586 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
587 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== _T('C')) &&
588 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
590 /* We break, output a newline */
591 ConOutChar(_T('\n'));
601 INT
FilePromptYN (UINT resID
)
603 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
605 // LPTSTR szKeys = _T("yna");
611 ConOutResPrintf (resID
);
613 /* preliminary fix */
614 ConInString(szIn
, 10);
615 ConOutChar(_T('\n'));
618 for (p
= szIn
; _istspace (*p
); p
++)
621 LoadString(CMD_ModuleHandle
, STRING_CHOICE_OPTION
, szMsg
, ARRAYSIZE(szMsg
));
623 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
625 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
628 else if (*p
== _T('\03'))
634 /* unfinished solution */
636 RemoveBreakHandler();
642 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
643 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
648 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
649 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
650 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
655 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
656 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== 'C') &&
657 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
665 INT
FilePromptYNA (UINT resID
)
667 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
669 // LPTSTR szKeys = _T("yna");
675 ConOutResPrintf (resID
);
677 /* preliminary fix */
678 ConInString(szIn
, 10);
679 ConOutChar(_T('\n'));
682 for (p
= szIn
; _istspace (*p
); p
++)
685 LoadString( CMD_ModuleHandle
, STRING_COPY_OPTION
, szMsg
, ARRAYSIZE(szMsg
));
687 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
689 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
691 else if (_tcsncmp(p
, &szMsg
[2], 1) == 0)
694 else if (*p
== _T('\03'))
700 /* unfinished solution */
702 RemoveBreakHandler();
708 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
709 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
712 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
713 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
714 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
719 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
720 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== _T('C')) &&
721 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))