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
:
151 LoadString(CMD_ModuleHandle
, STRING_COPY_OPTION
, options
, ARRAYSIZE(options
));
153 /* we need to be sure the string arrives on the screen! */
156 ConOutResPuts(STRING_CANCEL_BATCH_FILE
);
157 c
= _totupper(cgetchar());
158 } while (!(_tcschr(options
, c
) || c
== _T('\3')) || !c
);
160 ConOutChar(_T('\n'));
163 return bCtrlBreak
= FALSE
; /* ignore */
165 /* leave all batch files */
166 bLeaveAll
= ((c
== options
[2]) || (c
== _T('\3')));
175 /* state processed */
180 /* add new entry for new argument */
181 BOOL
add_entry (LPINT ac
, LPTSTR
**arg
, LPCTSTR entry
)
186 q
= cmd_alloc ((_tcslen(entry
) + 1) * sizeof (TCHAR
));
194 *arg
= cmd_realloc (oldarg
, (*ac
+ 2) * sizeof (LPTSTR
));
204 (*arg
)[++(*ac
)] = NULL
;
209 static BOOL
expand (LPINT ac
, LPTSTR
**arg
, LPCTSTR pattern
)
212 WIN32_FIND_DATA FindData
;
215 LPTSTR dirpart
, fullname
;
217 pathend
= _tcsrchr (pattern
, _T('\\'));
220 dirpart
= cmd_alloc((pathend
- pattern
+ 2) * sizeof(TCHAR
));
225 memcpy(dirpart
, pattern
, pathend
- pattern
+ 1);
226 dirpart
[pathend
- pattern
+ 1] = _T('\0');
232 hFind
= FindFirstFile (pattern
, &FindData
);
233 if (INVALID_HANDLE_VALUE
!= hFind
)
239 fullname
= cmd_alloc((_tcslen(dirpart
) + _tcslen(FindData
.cFileName
) + 1) * sizeof(TCHAR
));
240 if (NULL
== fullname
)
246 _tcscat (_tcscpy (fullname
, dirpart
), FindData
.cFileName
);
247 ok
= add_entry(ac
, arg
, fullname
);
253 ok
= add_entry(ac
, arg
, FindData
.cFileName
);
255 } while (FindNextFile (hFind
, &FindData
) && ok
);
260 ok
= add_entry(ac
, arg
, pattern
);
272 * split - splits a line up into separate arguments, delimiters
273 * are spaces and slashes ('/').
275 LPTSTR
*split (LPTSTR s
, LPINT args
, BOOL expand_wildcards
, BOOL handle_plus
)
283 arg
= cmd_alloc (sizeof (LPTSTR
));
291 BOOL bQuoted
= FALSE
;
293 /* skip leading spaces */
294 while (*s
&& (_istspace(*s
) || _istcntrl(*s
)))
299 /* the first character can be '/' */
303 /* skip to next word delimiter or start of next option */
304 while (_istprint(*s
))
306 /* if quote (") then set bQuoted */
307 bQuoted
^= (*s
== _T('\"'));
309 /* Check if we have unquoted text */
312 /* check for separators */
315 (handle_plus
&& (*s
== _T('+'))))
317 /* Make length at least one character */
326 /* a word was found */
329 q
= cmd_alloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
334 memcpy (q
, start
, len
* sizeof (TCHAR
));
337 if (expand_wildcards
&& (_T('/') != *start
) &&
338 (NULL
!= _tcschr(q
, _T('*')) || NULL
!= _tcschr(q
, _T('?'))))
340 if (! expand(&ac
, &arg
, q
))
349 if (! add_entry(&ac
, &arg
, q
))
366 * splitspace() is a function which uses JUST spaces as delimiters. split() uses "/" AND spaces.
367 * The way it works is real similar to split(), search the difference ;)
368 * splitspace is needed for commands such as "move" where paths as C:\this/is\allowed/ are allowed
370 LPTSTR
*splitspace (LPTSTR s
, LPINT args
)
378 arg
= cmd_alloc (sizeof (LPTSTR
));
386 BOOL bQuoted
= FALSE
;
388 /* skip leading spaces */
389 while (*s
&& (_istspace (*s
) || _istcntrl (*s
)))
394 /* skip to next word delimiter or start of next option */
395 while (_istprint(*s
) && (bQuoted
|| !_istspace(*s
)))
397 /* if quote (") then set bQuoted */
398 bQuoted
^= (*s
== _T('\"'));
402 /* a word was found */
405 q
= cmd_alloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
410 memcpy (q
, start
, len
* sizeof (TCHAR
));
413 if (! add_entry(&ac
, &arg
, q
))
429 * freep -- frees memory used for a call to split
431 VOID
freep (LPTSTR
*p
)
445 LPTSTR
_stpcpy (LPTSTR dest
, LPCTSTR src
)
448 return (dest
+ _tcslen (src
));
452 StripQuotes(TCHAR
*in
)
465 * Checks if a path is valid (accessible)
467 BOOL
IsValidPathName (LPCTSTR pszPath
)
469 TCHAR szOldPath
[MAX_PATH
];
472 GetCurrentDirectory (MAX_PATH
, szOldPath
);
473 bResult
= SetCurrentDirectory (pszPath
);
475 SetCurrentDirectory (szOldPath
);
482 * Checks if a file exists (accessible)
484 BOOL
IsExistingFile (LPCTSTR pszPath
)
486 DWORD attr
= GetFileAttributes (pszPath
);
487 return (attr
!= 0xFFFFFFFF && (! (attr
& FILE_ATTRIBUTE_DIRECTORY
)) );
491 BOOL
IsExistingDirectory (LPCTSTR pszPath
)
493 DWORD attr
= GetFileAttributes (pszPath
);
494 return (attr
!= 0xFFFFFFFF && (attr
& FILE_ATTRIBUTE_DIRECTORY
) );
498 BOOL
FileGetString (HANDLE hFile
, LPTSTR lpBuffer
, INT nBufferLength
)
504 lpString
= cmd_alloc(nBufferLength
);
509 if (ReadFile(hFile
, lpString
, nBufferLength
- 1, &dwRead
, NULL
))
511 /* break at new line*/
512 CHAR
*end
= memchr(lpString
, '\n', dwRead
);
516 len
= (INT
)(end
- lpString
) + 1;
517 SetFilePointer(hFile
, len
- dwRead
, NULL
, FILE_CURRENT
);
529 lpString
[len
++] = '\0';
531 MultiByteToWideChar(OutputCodePage
, 0, lpString
, -1, lpBuffer
, len
);
538 BOOL __stdcall
PagePrompt(PCON_PAGER Pager
, DWORD Done
, DWORD Total
)
540 SHORT iScreenWidth
, iCursorY
;
543 ConOutResPuts(STRING_MISC_HELP1
);
545 RemoveBreakHandler();
552 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
553 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
554 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
560 * Get the screen width, erase the full line where the cursor is,
561 * and move the cursor back to the beginning of the line.
563 GetScreenSize(&iScreenWidth
, NULL
);
564 iCursorY
= GetCursorY();
565 SetCursorXY(0, iCursorY
);
566 while (iScreenWidth
-- > 0) // Or call FillConsoleOutputCharacter ?
568 SetCursorXY(0, iCursorY
);
570 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
571 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== _T('C')) &&
572 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
574 /* We break, output a newline */
575 ConOutChar(_T('\n'));
585 INT
FilePromptYN (UINT resID
)
587 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
589 // LPTSTR szKeys = _T("yna");
595 ConOutResPrintf (resID
);
597 /* preliminary fix */
598 ConInString(szIn
, 10);
599 ConOutChar(_T('\n'));
602 for (p
= szIn
; _istspace (*p
); p
++)
605 LoadString(CMD_ModuleHandle
, STRING_CHOICE_OPTION
, szMsg
, ARRAYSIZE(szMsg
));
607 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
609 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
612 else if (*p
== _T('\03'))
618 /* unfinished solution */
620 RemoveBreakHandler();
626 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
627 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
632 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
633 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
634 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
639 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
640 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== 'C') &&
641 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
649 INT
FilePromptYNA (UINT resID
)
651 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
653 // LPTSTR szKeys = _T("yna");
659 ConOutResPrintf (resID
);
661 /* preliminary fix */
662 ConInString(szIn
, 10);
663 ConOutChar(_T('\n'));
666 for (p
= szIn
; _istspace (*p
); p
++)
669 LoadString( CMD_ModuleHandle
, STRING_COPY_OPTION
, szMsg
, ARRAYSIZE(szMsg
));
671 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
673 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
675 else if (_tcsncmp(p
, &szMsg
[2], 1) == 0)
678 else if (*p
== _T('\03'))
684 /* unfinished solution */
686 RemoveBreakHandler();
692 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
693 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
696 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
697 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
698 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
703 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
704 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== _T('C')) &&
705 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))