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 <ekohl@abo.rhein-zeitung.de>)
17 * Changed split() to accept quoted arguments.
18 * Removed parse_firstarg().
20 * 23-Jan-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
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 <ekohl@abo.rhein-zeitung.de>)
26 * FileGetString() seems to be working now.
28 * 06-Nov-1999 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
29 * Added PagePrompt() and FilePrompt().
31 * 30-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
32 * Remove all hardcode string to En.rc
40 * get a character out-of-band and honor Ctrl-Break characters
44 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
45 INPUT_RECORD irBuffer
;
50 ReadConsoleInput (hInput
, &irBuffer
, 1, &dwRead
);
51 if ((irBuffer
.EventType
== KEY_EVENT
) &&
52 (irBuffer
.Event
.KeyEvent
.bKeyDown
== TRUE
))
54 if ((irBuffer
.Event
.KeyEvent
.dwControlKeyState
&
55 (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
)) &
56 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== 'C'))
65 return irBuffer
.Event
.KeyEvent
.uChar
.AsciiChar
;
67 return irBuffer
.Event
.KeyEvent
.uChar
.UnicodeChar
;
73 * Check if Ctrl-Break was pressed during the last calls
76 BOOL
CheckCtrlBreak (INT mode
)
78 static BOOL bLeaveAll
= FALSE
; /* leave all batch files */
83 case BREAK_OUTOFBATCH
:
94 /* we need to be sure the string arrives on the screen! */
96 ConOutPuts (_T("\r\nCtrl-Break pressed. Cancel batch file? (Yes/No/All) "));
97 while (!_tcschr (_T("YNA\3"), c
= _totupper (cgetchar())) || !c
);
99 ConOutPuts (_T("\r\n"));
102 return bCtrlBreak
= FALSE
; /* ignore */
104 /* leave all batch files */
105 bLeaveAll
= ((c
== _T('A')) || (c
== _T('\3')));
114 /* state processed */
119 /* add new entry for new argument */
120 static BOOL
add_entry (LPINT ac
, LPTSTR
**arg
, LPCTSTR entry
)
125 q
= malloc ((_tcslen(entry
) + 1) * sizeof (TCHAR
));
133 *arg
= realloc (oldarg
, (*ac
+ 2) * sizeof (LPTSTR
));
142 (*arg
)[++(*ac
)] = NULL
;
147 static BOOL
expand (LPINT ac
, LPTSTR
**arg
, LPCTSTR pattern
)
150 WIN32_FIND_DATA FindData
;
153 LPTSTR dirpart
, fullname
;
155 pathend
= _tcsrchr (pattern
, _T('\\'));
158 dirpart
= malloc((pathend
- pattern
+ 2) * sizeof(TCHAR
));
163 memcpy(dirpart
, pattern
, pathend
- pattern
+ 1);
164 dirpart
[pathend
- pattern
+ 1] = '\0';
170 hFind
= FindFirstFile (pattern
, &FindData
);
171 if (INVALID_HANDLE_VALUE
!= hFind
)
177 fullname
= malloc((_tcslen(dirpart
) + _tcslen(FindData
.cFileName
) + 1) * sizeof(TCHAR
));
178 if (NULL
== fullname
)
184 _tcscat (_tcscpy (fullname
, dirpart
), FindData
.cFileName
);
185 ok
= add_entry(ac
, arg
, fullname
);
191 ok
= add_entry(ac
, arg
, FindData
.cFileName
);
193 } while (FindNextFile (hFind
, &FindData
) && ok
);
198 ok
= add_entry(ac
, arg
, pattern
);
210 * split - splits a line up into separate arguments, deliminators
211 * are spaces and slashes ('/').
214 LPTSTR
*split (LPTSTR s
, LPINT args
, BOOL expand_wildcards
)
221 BOOL bQuoted
= FALSE
;
223 arg
= malloc (sizeof (LPTSTR
));
231 /* skip leading spaces */
232 while (*s
&& (_istspace (*s
) || _istcntrl (*s
)))
235 /* if quote (") then set bQuoted */
244 /* the first character can be '/' */
248 /* skip to next word delimiter or start of next option */
251 while (_istprint (*s
) && (*s
!= _T('\"')) && (*s
!= _T('/')))
256 while (_istprint (*s
) && !_istspace (*s
) && (*s
!= _T('/')))
260 /* a word was found */
263 q
= malloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
268 memcpy (q
, start
, len
* sizeof (TCHAR
));
270 if (expand_wildcards
&& _T('/') != *start
&&
271 (NULL
!= _tcschr(q
, _T('*')) || NULL
!= _tcschr(q
, _T('?'))))
273 if (! expand(&ac
, &arg
, q
))
282 if (! add_entry(&ac
, &arg
, q
))
292 /* adjust string pointer if quoted (") */
307 * freep -- frees memory used for a call to split
310 VOID
freep (LPTSTR
*p
)
325 LPTSTR
_stpcpy (LPTSTR dest
, LPTSTR src
)
328 return (dest
+ _tcslen (src
));
334 * Checks if a path is valid (accessible)
337 BOOL
IsValidPathName (LPCTSTR pszPath
)
339 TCHAR szOldPath
[MAX_PATH
];
342 GetCurrentDirectory (MAX_PATH
, szOldPath
);
343 bResult
= SetCurrentDirectory (pszPath
);
345 SetCurrentDirectory (szOldPath
);
352 * Checks if a file exists (accessible)
355 BOOL
IsExistingFile (LPCTSTR pszPath
)
357 DWORD attr
= GetFileAttributes (pszPath
);
358 return (attr
!= 0xFFFFFFFF && (! (attr
& FILE_ATTRIBUTE_DIRECTORY
)) );
362 BOOL
IsExistingDirectory (LPCTSTR pszPath
)
364 DWORD attr
= GetFileAttributes (pszPath
);
365 return (attr
!= 0xFFFFFFFF && (attr
& FILE_ATTRIBUTE_DIRECTORY
) );
369 BOOL
FileGetString (HANDLE hFile
, LPTSTR lpBuffer
, INT nBufferLength
)
376 lpString
= malloc(nBufferLength
);
381 while ((--nBufferLength
> 0) &&
382 ReadFile(hFile
, &ch
, 1, &dwRead
, NULL
) && dwRead
)
387 ReadFile (hFile
, &ch
, 1, &dwRead
, NULL
);
390 lpString
[len
++] = ch
;
396 lpString
[len
++] = '\0';
398 MultiByteToWideChar(CP_ACP
, 0, lpString
, len
, lpBuffer
, len
);
406 * GetConsoleWindow - returns the handle to the current console window
408 HWND
GetConsoleWindow (VOID
)
417 GetConsoleTitle (original
, sizeof(original
) / sizeof(TCHAR
));
419 _tcscpy (temp
, original
);
420 _tcscat (temp
, _T("-xxx "));
422 if (FindWindow (0, temp
) == NULL
)
424 SetConsoleTitle (temp
);
427 while(!(h
= FindWindow (0, temp
)))
430 SetConsoleTitle (original
);
438 INT
PagePrompt (VOID
)
442 ConOutResPuts(STRING_MISC_HELP1
);
444 RemoveBreakHandler ();
451 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
452 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
453 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
458 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
459 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== 'C') &&
460 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
467 INT
FilePromptYN (LPTSTR szFormat
, ...)
469 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
473 // LPTSTR szKeys = _T("yna");
478 va_start (arg_ptr
, szFormat
);
479 _vstprintf (szOut
, szFormat
, arg_ptr
);
482 ConOutPrintf (szFormat
);
484 /* preliminary fix */
485 ConInString (szIn
, 10);
486 ConOutPrintf (_T("\n"));
489 for (p
= szIn
; _istspace (*p
); p
++)
492 LoadString(CMD_ModuleHandle
, STRING_CHOICE_OPTION
, szMsg
, RC_STRING_MAX_SIZE
);
494 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
496 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
499 else if (*p
== _T('\03'))
506 /* unfinished sollution */
508 RemoveBreakHandler ();
514 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
515 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
520 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
521 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
522 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
527 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
528 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== 'C') &&
529 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
537 INT
FilePromptYNA (LPTSTR szFormat
, ...)
539 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
543 // LPTSTR szKeys = _T("yna");
548 va_start (arg_ptr
, szFormat
);
549 _vstprintf (szOut
, szFormat
, arg_ptr
);
552 ConOutPrintf (szFormat
);
554 /* preliminary fix */
555 ConInString (szIn
, 10);
556 ConOutPrintf (_T("\n"));
559 for (p
= szIn
; _istspace (*p
); p
++)
562 LoadString( CMD_ModuleHandle
, STRING_COPY_OPTION
, szMsg
, RC_STRING_MAX_SIZE
);
564 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
566 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
568 else if (_tcsncmp(p
, &szMsg
[2], 1) == 0)
572 else if (*p
== _T('\03'))
579 /* unfinished sollution */
581 RemoveBreakHandler ();
587 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
588 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
591 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
592 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
593 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
598 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
599 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== 'C') &&
600 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))