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 WaitForSingleObject (hInput
, INFINITE
);
52 ReadConsoleInput (hInput
, &irBuffer
, 1, &dwRead
);
53 if ((irBuffer
.EventType
== KEY_EVENT
) &&
54 (irBuffer
.Event
.KeyEvent
.bKeyDown
== TRUE
))
56 if ((irBuffer
.Event
.KeyEvent
.dwControlKeyState
&
57 (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
)) &
58 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== 'C'))
67 return irBuffer
.Event
.KeyEvent
.AsciiChar
;
70 return irBuffer
.Event
.KeyEvent
.uChar
.AsciiChar
;
72 return irBuffer
.Event
.KeyEvent
.uChar
.UnicodeChar
;
74 #endif /* __REACTOS__ */
79 * Check if Ctrl-Break was pressed during the last calls
82 BOOL
CheckCtrlBreak (INT mode
)
84 static BOOL bLeaveAll
= FALSE
; /* leave all batch files */
89 case BREAK_OUTOFBATCH
:
100 /* we need to be sure the string arrives on the screen! */
102 ConOutPuts (_T("\r\nCtrl-Break pressed. Cancel batch file? (Yes/No/All) "));
103 while (!_tcschr ("YNA\3", c
= _totupper (cgetchar())) || !c
);
105 ConOutPuts (_T("\r\n"));
108 return bCtrlBreak
= FALSE
; /* ignore */
110 /* leave all batch files */
111 bLeaveAll
= ((c
== _T('A')) || (c
== _T('\3')));
120 /* state processed */
127 * split - splits a line up into separate arguments, deliminators
128 * are spaces and slashes ('/').
131 LPTSTR
*split (LPTSTR s
, LPINT args
)
139 BOOL bQuoted
= FALSE
;
141 arg
= malloc (sizeof (LPTSTR
));
149 /* skip leading spaces */
150 while (*s
&& (_istspace (*s
) || _istcntrl (*s
)))
153 /* if quote (") then set bQuoted */
162 /* the first character can be '/' */
166 /* skip to next word delimiter or start of next option */
169 while (_istprint (*s
) && (*s
!= _T('\"')) && (*s
!= _T('/')))
174 while (_istprint (*s
) && !_istspace (*s
) && (*s
!= _T('/')))
178 /* a word was found */
181 /* add new entry for new argument */
182 arg
= realloc (p
= arg
, (ac
+ 2) * sizeof (LPTSTR
));
189 /* create new entry */
190 q
= arg
[ac
] = malloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
197 memcpy (q
, start
, len
* sizeof (TCHAR
));
201 /* adjust string pointer if quoted (") */
216 * freep -- frees memory used for a call to split
219 VOID
freep (LPTSTR
*p
)
234 LPTSTR
stpcpy (LPTSTR dest
, LPTSTR src
)
237 return (dest
+ _tcslen (src
));
243 * Checks if a path is valid (accessible)
246 BOOL
IsValidPathName (LPCTSTR pszPath
)
248 TCHAR szOldPath
[MAX_PATH
];
251 GetCurrentDirectory (MAX_PATH
, szOldPath
);
252 bResult
= SetCurrentDirectory (pszPath
);
254 SetCurrentDirectory (szOldPath
);
261 * Checks if a file exists (accessible)
264 BOOL
IsValidFileName (LPCTSTR pszPath
)
266 return (GetFileAttributes (pszPath
) != 0xFFFFFFFF);
270 BOOL
IsValidDirectory (LPCTSTR pszPath
)
272 return (GetFileAttributes (pszPath
) & FILE_ATTRIBUTE_DIRECTORY
);
276 BOOL
FileGetString (HANDLE hFile
, LPTSTR lpBuffer
, INT nBufferLength
)
284 while ((--nBufferLength
> 0) &&
285 ReadFile(hFile
, &ch
, 1, &dwRead
, NULL
) && dwRead
)
291 ReadFile (hFile
, &ch
, 1, &dwRead
, NULL
);
296 if (!dwRead
&& lpString
== lpBuffer
)
299 *lpString
++ = _T('\0');