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'))
66 ReadConsoleInput (hInput
, &irBuffer
, 1, &dwRead
);
68 if (irBuffer
.EventType
== KEY_EVENT
)
70 if (irBuffer
.Event
.KeyEvent
.dwControlKeyState
&
71 (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))
73 if (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== 'C')
75 // if (irBuffer.Event.KeyEvent.bKeyDown == TRUE)
82 else if ((irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
83 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
84 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
))
98 return irBuffer
.Event
.KeyEvent
.uChar
.AsciiChar
;
100 return irBuffer
.Event
.KeyEvent
.uChar
.UnicodeChar
;
101 #endif /* _UNICODE */
105 * Takes a path in and returns it with the correct case of the letters
107 VOID
GetPathCase( TCHAR
* Path
, TCHAR
* OutPath
)
110 TCHAR TempPath
[MAX_PATH
];
111 WIN32_FIND_DATA FindFileData
;
113 _tcscpy(TempPath
, _T(""));
114 _tcscpy(OutPath
, _T(""));
117 for(i
= 0; i
< _tcslen(Path
); i
++)
119 if(Path
[i
] != _T('\\'))
121 _tcsncat(TempPath
, &Path
[i
], 1);
122 if(i
!= _tcslen(Path
) - 1)
125 /* Handle the base part of the path different.
126 Because if you put it into findfirstfile, it will
127 return your current folder */
128 if(_tcslen(TempPath
) == 2 && TempPath
[1] == _T(':'))
130 _tcscat(OutPath
, TempPath
);
131 _tcscat(OutPath
, _T("\\"));
132 _tcscat(TempPath
, _T("\\"));
136 hFind
= FindFirstFile(TempPath
,&FindFileData
);
137 if(hFind
== INVALID_HANDLE_VALUE
)
139 _tcscpy(OutPath
, Path
);
142 _tcscat(TempPath
, _T("\\"));
143 _tcscat(OutPath
, FindFileData
.cFileName
);
144 _tcscat(OutPath
, _T("\\"));
151 * Check if Ctrl-Break was pressed during the last calls
154 BOOL
CheckCtrlBreak (INT mode
)
156 static BOOL bLeaveAll
= FALSE
; /* leave all batch files */
161 case BREAK_OUTOFBATCH
:
165 case BREAK_BATCHFILE
:
172 /* we need to be sure the string arrives on the screen! */
174 ConOutPuts (_T("\r\nCtrl-Break pressed. Cancel batch file? (Yes/No/All) "));
175 while (!_tcschr (_T("YNA\3"), c
= _totupper (cgetchar())) || !c
);
177 ConOutPuts (_T("\r\n"));
180 return bCtrlBreak
= FALSE
; /* ignore */
182 /* leave all batch files */
183 bLeaveAll
= ((c
== _T('A')) || (c
== _T('\3')));
192 /* state processed */
197 /* add new entry for new argument */
198 BOOL
add_entry (LPINT ac
, LPTSTR
**arg
, LPCTSTR entry
)
203 q
= malloc ((_tcslen(entry
) + 1) * sizeof (TCHAR
));
211 *arg
= realloc (oldarg
, (*ac
+ 2) * sizeof (LPTSTR
));
220 (*arg
)[++(*ac
)] = NULL
;
225 static BOOL
expand (LPINT ac
, LPTSTR
**arg
, LPCTSTR pattern
)
228 WIN32_FIND_DATA FindData
;
231 LPTSTR dirpart
, fullname
;
233 pathend
= _tcsrchr (pattern
, _T('\\'));
236 dirpart
= malloc((pathend
- pattern
+ 2) * sizeof(TCHAR
));
241 memcpy(dirpart
, pattern
, pathend
- pattern
+ 1);
242 dirpart
[pathend
- pattern
+ 1] = _T('\0');
248 hFind
= FindFirstFile (pattern
, &FindData
);
249 if (INVALID_HANDLE_VALUE
!= hFind
)
255 fullname
= malloc((_tcslen(dirpart
) + _tcslen(FindData
.cFileName
) + 1) * sizeof(TCHAR
));
256 if (NULL
== fullname
)
262 _tcscat (_tcscpy (fullname
, dirpart
), FindData
.cFileName
);
263 ok
= add_entry(ac
, arg
, fullname
);
269 ok
= add_entry(ac
, arg
, FindData
.cFileName
);
271 } while (FindNextFile (hFind
, &FindData
) && ok
);
276 ok
= add_entry(ac
, arg
, pattern
);
288 * split - splits a line up into separate arguments, deliminators
289 * are spaces and slashes ('/').
292 LPTSTR
*split (LPTSTR s
, LPINT args
, BOOL expand_wildcards
)
299 BOOL bQuoted
= FALSE
;
301 arg
= malloc (sizeof (LPTSTR
));
309 /* skip leading spaces */
310 while (*s
&& (_istspace (*s
) || _istcntrl (*s
)))
313 /* if quote (") then set bQuoted */
322 /* the first character can be '/' */
326 /* skip to next word delimiter or start of next option */
329 while (_istprint (*s
) && (*s
!= _T('\"')) && (*s
!= _T('/')))
334 while (_istprint (*s
) && !_istspace (*s
) && (*s
!= _T('/')))
338 /* a word was found */
341 q
= malloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
346 memcpy (q
, start
, len
* sizeof (TCHAR
));
348 if (expand_wildcards
&& _T('/') != *start
&&
349 (NULL
!= _tcschr(q
, _T('*')) || NULL
!= _tcschr(q
, _T('?'))))
351 if (! expand(&ac
, &arg
, q
))
360 if (! add_entry(&ac
, &arg
, q
))
370 /* adjust string pointer if quoted (") */
373 /* Check to make sure if there is no ending "
374 * we dont run over the null char */
391 * freep -- frees memory used for a call to split
394 VOID
freep (LPTSTR
*p
)
409 LPTSTR
_stpcpy (LPTSTR dest
, LPCTSTR src
)
412 return (dest
+ _tcslen (src
));
418 * Checks if a path is valid (accessible)
421 BOOL
IsValidPathName (LPCTSTR pszPath
)
423 TCHAR szOldPath
[MAX_PATH
];
426 GetCurrentDirectory (MAX_PATH
, szOldPath
);
427 bResult
= SetCurrentDirectory (pszPath
);
429 SetCurrentDirectory (szOldPath
);
436 * Checks if a file exists (accessible)
439 BOOL
IsExistingFile (LPCTSTR pszPath
)
441 DWORD attr
= GetFileAttributes (pszPath
);
442 return (attr
!= 0xFFFFFFFF && (! (attr
& FILE_ATTRIBUTE_DIRECTORY
)) );
446 BOOL
IsExistingDirectory (LPCTSTR pszPath
)
448 DWORD attr
= GetFileAttributes (pszPath
);
449 return (attr
!= 0xFFFFFFFF && (attr
& FILE_ATTRIBUTE_DIRECTORY
) );
453 BOOL
FileGetString (HANDLE hFile
, LPTSTR lpBuffer
, INT nBufferLength
)
460 lpString
= malloc(nBufferLength
);
465 while ((--nBufferLength
> 0) &&
466 ReadFile(hFile
, &ch
, 1, &dwRead
, NULL
) && dwRead
)
468 lpString
[len
++] = ch
;
469 if ((ch
== _T('\n')) || (ch
== _T('\r')))
471 /* break at new line*/
479 lpString
[len
++] = _T('\0');
481 MultiByteToWideChar(CP_ACP
, 0, lpString
, len
, lpBuffer
, len
);
487 INT
PagePrompt (VOID
)
491 ConOutResPuts(STRING_MISC_HELP1
);
493 RemoveBreakHandler ();
500 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
501 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
502 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
507 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
508 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== _T('C')) &&
509 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
519 INT
FilePromptYN (LPTSTR szFormat
, ...)
521 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
525 // LPTSTR szKeys = _T("yna");
530 va_start (arg_ptr
, szFormat
);
531 _vstprintf (szOut
, szFormat
, arg_ptr
);
534 ConOutPrintf (szFormat
);
536 /* preliminary fix */
537 ConInString (szIn
, 10);
538 ConOutPrintf (_T("\n"));
541 for (p
= szIn
; _istspace (*p
); p
++)
544 LoadString(CMD_ModuleHandle
, STRING_CHOICE_OPTION
, szMsg
, RC_STRING_MAX_SIZE
);
546 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
548 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
551 else if (*p
== _T('\03'))
558 /* unfinished sollution */
560 RemoveBreakHandler ();
566 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
567 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
572 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
573 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
574 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
579 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
580 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== 'C') &&
581 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
589 INT
FilePromptYNA (LPTSTR szFormat
, ...)
591 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
595 // LPTSTR szKeys = _T("yna");
600 va_start (arg_ptr
, szFormat
);
601 _vstprintf (szOut
, szFormat
, arg_ptr
);
604 ConOutPrintf (szFormat
);
606 /* preliminary fix */
607 ConInString (szIn
, 10);
608 ConOutPrintf (_T("\n"));
611 for (p
= szIn
; _istspace (*p
); p
++)
614 LoadString( CMD_ModuleHandle
, STRING_COPY_OPTION
, szMsg
, RC_STRING_MAX_SIZE
);
616 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
618 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
620 else if (_tcsncmp(p
, &szMsg
[2], 1) == 0)
624 else if (*p
== _T('\03'))
631 /* unfinished sollution */
633 RemoveBreakHandler ();
639 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
640 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
643 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
644 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
645 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
650 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
651 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== _T('C')) &&
652 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))