65231823c97dbba26a4c4c85cb9ca546b831cc13
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.
41 * get a character out-of-band and honor Ctrl-Break characters
45 HANDLE hInput
= GetStdHandle (STD_INPUT_HANDLE
);
46 INPUT_RECORD irBuffer
;
51 ReadConsoleInput (hInput
, &irBuffer
, 1, &dwRead
);
52 if ((irBuffer
.EventType
== KEY_EVENT
) &&
53 (irBuffer
.Event
.KeyEvent
.bKeyDown
== TRUE
))
55 if ((irBuffer
.Event
.KeyEvent
.dwControlKeyState
&
56 (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
)) &
57 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== 'C'))
66 return irBuffer
.Event
.KeyEvent
.uChar
.AsciiChar
;
68 return irBuffer
.Event
.KeyEvent
.uChar
.UnicodeChar
;
74 * Check if Ctrl-Break was pressed during the last calls
77 BOOL
CheckCtrlBreak (INT mode
)
79 static BOOL bLeaveAll
= FALSE
; /* leave all batch files */
84 case BREAK_OUTOFBATCH
:
95 /* we need to be sure the string arrives on the screen! */
97 ConOutPuts (_T("\r\nCtrl-Break pressed. Cancel batch file? (Yes/No/All) "));
98 while (!_tcschr ("YNA\3", c
= _totupper (cgetchar())) || !c
);
100 ConOutPuts (_T("\r\n"));
103 return bCtrlBreak
= FALSE
; /* ignore */
105 /* leave all batch files */
106 bLeaveAll
= ((c
== _T('A')) || (c
== _T('\3')));
115 /* state processed */
122 * split - splits a line up into separate arguments, deliminators
123 * are spaces and slashes ('/').
126 LPTSTR
*split (LPTSTR s
, LPINT args
)
134 BOOL bQuoted
= FALSE
;
136 arg
= malloc (sizeof (LPTSTR
));
144 /* skip leading spaces */
145 while (*s
&& (_istspace (*s
) || _istcntrl (*s
)))
148 /* if quote (") then set bQuoted */
157 /* the first character can be '/' */
161 /* skip to next word delimiter or start of next option */
164 while (_istprint (*s
) && (*s
!= _T('\"')) && (*s
!= _T('/')))
169 while (_istprint (*s
) && !_istspace (*s
) && (*s
!= _T('/')))
173 /* a word was found */
176 /* add new entry for new argument */
177 arg
= realloc (p
= arg
, (ac
+ 2) * sizeof (LPTSTR
));
184 /* create new entry */
185 q
= arg
[ac
] = malloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
192 memcpy (q
, start
, len
* sizeof (TCHAR
));
196 /* adjust string pointer if quoted (") */
211 * freep -- frees memory used for a call to split
214 VOID
freep (LPTSTR
*p
)
229 LPTSTR
stpcpy (LPTSTR dest
, LPTSTR src
)
232 return (dest
+ _tcslen (src
));
238 * Checks if a path is valid (accessible)
241 BOOL
IsValidPathName (LPCTSTR pszPath
)
243 TCHAR szOldPath
[MAX_PATH
];
246 GetCurrentDirectory (MAX_PATH
, szOldPath
);
247 bResult
= SetCurrentDirectory (pszPath
);
249 SetCurrentDirectory (szOldPath
);
256 * Checks if a file exists (accessible)
259 BOOL
IsValidFileName (LPCTSTR pszPath
)
261 return (GetFileAttributes (pszPath
) != 0xFFFFFFFF);
265 BOOL
IsValidDirectory (LPCTSTR pszPath
)
267 return (GetFileAttributes (pszPath
) & FILE_ATTRIBUTE_DIRECTORY
);
271 BOOL
FileGetString (HANDLE hFile
, LPTSTR lpBuffer
, INT nBufferLength
)
279 while ((--nBufferLength
> 0) &&
280 ReadFile(hFile
, &ch
, 1, &dwRead
, NULL
) && dwRead
)
286 ReadFile (hFile
, &ch
, 1, &dwRead
, NULL
);
291 if (!dwRead
&& lpString
== lpBuffer
)
294 *lpString
++ = _T('\0');
301 * GetConsoleWindow - returns the handle to the current console window
303 HWND
GetConsoleWindow(VOID
)
305 TCHAR original
[256]; /* stores original title*/
306 TCHAR temp
[256]; /* stores temp title*/
310 GetConsoleTitle(original
,sizeof(original
));
312 _tcscpy(temp
,original
);
313 _tcscat(temp
,_T("-xxx "));
315 if((h
= FindWindow("tty",temp
)) == NULL
)
317 SetConsoleTitle(temp
);
318 h
=FindWindow("tty",temp
);
319 SetConsoleTitle(original
);