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 hardcode string to En.rc
39 * 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
))
57 if (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== 'C')
63 else if ((irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
64 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
65 (irBuffer
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
))
79 return irBuffer
.Event
.KeyEvent
.uChar
.AsciiChar
;
81 return irBuffer
.Event
.KeyEvent
.uChar
.UnicodeChar
;
86 * Takes a path in and returns it with the correct case of the letters
88 VOID
GetPathCase( TCHAR
* Path
, TCHAR
* OutPath
)
91 TCHAR TempPath
[MAX_PATH
];
92 WIN32_FIND_DATA FindFileData
;
94 _tcscpy(TempPath
, _T(""));
95 _tcscpy(OutPath
, _T(""));
98 for(i
= 0; i
< _tcslen(Path
); i
++)
100 if(Path
[i
] != _T('\\'))
102 _tcsncat(TempPath
, &Path
[i
], 1);
103 if(i
!= _tcslen(Path
) - 1)
106 /* Handle the base part of the path different.
107 Because if you put it into findfirstfile, it will
108 return your current folder */
109 if(_tcslen(TempPath
) == 2 && TempPath
[1] == _T(':'))
111 _tcscat(OutPath
, TempPath
);
112 _tcscat(OutPath
, _T("\\"));
113 _tcscat(TempPath
, _T("\\"));
117 hFind
= FindFirstFile(TempPath
,&FindFileData
);
118 if(hFind
== INVALID_HANDLE_VALUE
)
120 _tcscpy(OutPath
, Path
);
123 _tcscat(TempPath
, _T("\\"));
124 _tcscat(OutPath
, FindFileData
.cFileName
);
125 _tcscat(OutPath
, _T("\\"));
132 * Check if Ctrl-Break was pressed during the last calls
135 BOOL
CheckCtrlBreak (INT mode
)
137 static BOOL bLeaveAll
= FALSE
; /* leave all batch files */
138 TCHAR options
[4]; /* Yes, No, All */
143 case BREAK_OUTOFBATCH
:
147 case BREAK_BATCHFILE
:
154 LoadString(CMD_ModuleHandle
, STRING_COPY_OPTION
, options
, 4);
156 /* we need to be sure the string arrives on the screen! */
159 ConOutResPuts(STRING_CANCEL_BATCH_FILE
);
160 c
= _totupper(cgetchar());
161 } while (!(_tcschr(options
, c
) || c
== _T('\3')) || !c
);
163 ConOutPuts (_T("\r\n"));
166 return bCtrlBreak
= FALSE
; /* ignore */
168 /* leave all batch files */
169 bLeaveAll
= ((c
== options
[2]) || (c
== _T('\3')));
178 /* state processed */
183 /* add new entry for new argument */
184 BOOL
add_entry (LPINT ac
, LPTSTR
**arg
, LPCTSTR entry
)
189 q
= cmd_alloc ((_tcslen(entry
) + 1) * sizeof (TCHAR
));
197 *arg
= cmd_realloc (oldarg
, (*ac
+ 2) * sizeof (LPTSTR
));
206 (*arg
)[++(*ac
)] = NULL
;
211 static BOOL
expand (LPINT ac
, LPTSTR
**arg
, LPCTSTR pattern
)
214 WIN32_FIND_DATA FindData
;
217 LPTSTR dirpart
, fullname
;
219 pathend
= _tcsrchr (pattern
, _T('\\'));
222 dirpart
= cmd_alloc((pathend
- pattern
+ 2) * sizeof(TCHAR
));
227 memcpy(dirpart
, pattern
, pathend
- pattern
+ 1);
228 dirpart
[pathend
- pattern
+ 1] = _T('\0');
234 hFind
= FindFirstFile (pattern
, &FindData
);
235 if (INVALID_HANDLE_VALUE
!= hFind
)
241 fullname
= cmd_alloc((_tcslen(dirpart
) + _tcslen(FindData
.cFileName
) + 1) * sizeof(TCHAR
));
242 if (NULL
== fullname
)
248 _tcscat (_tcscpy (fullname
, dirpart
), FindData
.cFileName
);
249 ok
= add_entry(ac
, arg
, fullname
);
255 ok
= add_entry(ac
, arg
, FindData
.cFileName
);
257 } while (FindNextFile (hFind
, &FindData
) && ok
);
262 ok
= add_entry(ac
, arg
, pattern
);
274 * split - splits a line up into separate arguments, deliminators
275 * are spaces and slashes ('/').
278 LPTSTR
*split (LPTSTR s
, LPINT args
, BOOL expand_wildcards
)
286 arg
= cmd_alloc (sizeof (LPTSTR
));
294 BOOL bQuoted
= FALSE
;
296 /* skip leading spaces */
297 while (*s
&& (_istspace(*s
) || _istcntrl(*s
)))
302 /* the first character can be '/' */
306 /* skip to next word delimiter or start of next option */
307 while (_istprint(*s
))
309 /* if quote (") then set bQuoted */
310 bQuoted
^= (*s
== _T('\"'));
312 /* Check if we have unquoted text */
315 /* check for separators */
320 /* Make length at least one character */
329 /* a word was found */
332 q
= cmd_alloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
337 memcpy (q
, start
, len
* sizeof (TCHAR
));
340 if (expand_wildcards
&& (_T('/') != *start
) &&
341 (NULL
!= _tcschr(q
, _T('*')) || NULL
!= _tcschr(q
, _T('?'))))
343 if (! expand(&ac
, &arg
, q
))
352 if (! add_entry(&ac
, &arg
, q
))
368 /* splitspace() is a function which uses JUST spaces as delimeters. split() uses "/" AND spaces.
369 * The way it works is real similar to split(), search the difference ;)
370 * splitspace is needed for commands such as "move" where paths as C:\this/is\allowed/ are allowed
372 LPTSTR
*splitspace (LPTSTR s
, LPINT args
)
380 arg
= cmd_alloc (sizeof (LPTSTR
));
388 BOOL bQuoted
= FALSE
;
390 /* skip leading spaces */
391 while (*s
&& (_istspace (*s
) || _istcntrl (*s
)))
396 /* skip to next word delimiter or start of next option */
397 while (_istprint(*s
) && (bQuoted
|| !_istspace(*s
)))
399 /* if quote (") then set bQuoted */
400 bQuoted
^= (*s
== _T('\"'));
404 /* a word was found */
407 q
= cmd_alloc (((len
= s
- start
) + 1) * sizeof (TCHAR
));
412 memcpy (q
, start
, len
* sizeof (TCHAR
));
415 if (! add_entry(&ac
, &arg
, q
))
431 * freep -- frees memory used for a call to split
434 VOID
freep (LPTSTR
*p
)
449 LPTSTR
_stpcpy (LPTSTR dest
, LPCTSTR src
)
452 return (dest
+ _tcslen (src
));
456 StripQuotes(TCHAR
*in
)
470 * Checks if a path is valid (accessible)
473 BOOL
IsValidPathName (LPCTSTR pszPath
)
475 TCHAR szOldPath
[MAX_PATH
];
478 GetCurrentDirectory (MAX_PATH
, szOldPath
);
479 bResult
= SetCurrentDirectory (pszPath
);
481 SetCurrentDirectory (szOldPath
);
488 * Checks if a file exists (accessible)
491 BOOL
IsExistingFile (LPCTSTR pszPath
)
493 DWORD attr
= GetFileAttributes (pszPath
);
494 return (attr
!= 0xFFFFFFFF && (! (attr
& FILE_ATTRIBUTE_DIRECTORY
)) );
498 BOOL
IsExistingDirectory (LPCTSTR pszPath
)
500 DWORD attr
= GetFileAttributes (pszPath
);
501 return (attr
!= 0xFFFFFFFF && (attr
& FILE_ATTRIBUTE_DIRECTORY
) );
505 BOOL
FileGetString (HANDLE hFile
, LPTSTR lpBuffer
, INT nBufferLength
)
511 lpString
= cmd_alloc(nBufferLength
);
516 if (ReadFile(hFile
, lpString
, nBufferLength
- 1, &dwRead
, NULL
))
518 /* break at new line*/
519 CHAR
*end
= memchr(lpString
, '\n', dwRead
);
523 len
= (INT
)(end
- lpString
) + 1;
524 SetFilePointer(hFile
, len
- dwRead
, NULL
, FILE_CURRENT
);
536 lpString
[len
++] = '\0';
538 MultiByteToWideChar(OutputCodePage
, 0, lpString
, -1, lpBuffer
, len
);
544 INT
PagePrompt (VOID
)
548 ConOutResPuts(STRING_MISC_HELP1
);
550 RemoveBreakHandler ();
557 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
558 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
559 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
564 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
565 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== _T('C')) &&
566 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
576 INT
FilePromptYN (UINT resID
)
578 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
580 // LPTSTR szKeys = _T("yna");
586 ConOutResPrintf (resID
);
588 /* preliminary fix */
589 ConInString (szIn
, 10);
590 ConOutPrintf (_T("\n"));
593 for (p
= szIn
; _istspace (*p
); p
++)
596 LoadString(CMD_ModuleHandle
, STRING_CHOICE_OPTION
, szMsg
, RC_STRING_MAX_SIZE
);
598 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
600 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
603 else if (*p
== _T('\03'))
610 /* unfinished sollution */
612 RemoveBreakHandler ();
618 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
619 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
624 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
625 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
626 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
631 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
632 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== 'C') &&
633 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))
641 INT
FilePromptYNA (UINT resID
)
643 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
645 // LPTSTR szKeys = _T("yna");
651 ConOutResPrintf (resID
);
653 /* preliminary fix */
654 ConInString (szIn
, 10);
655 ConOutPrintf (_T("\n"));
658 for (p
= szIn
; _istspace (*p
); p
++)
661 LoadString( CMD_ModuleHandle
, STRING_COPY_OPTION
, szMsg
, RC_STRING_MAX_SIZE
);
663 if (_tcsncmp(p
, &szMsg
[0], 1) == 0)
665 else if (_tcsncmp(p
, &szMsg
[1], 1) == 0)
667 else if (_tcsncmp(p
, &szMsg
[2], 1) == 0)
671 else if (*p
== _T('\03'))
678 /* unfinished sollution */
680 RemoveBreakHandler ();
686 cKey
= _totlower (ir
.Event
.KeyEvent
.uChar
.AsciiChar
);
687 if (_tcschr (szKeys
, cKey
[0]) == NULL
)
690 while ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_SHIFT
) ||
691 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_MENU
) ||
692 (ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_CONTROL
));
697 if ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== VK_ESCAPE
) ||
698 ((ir
.Event
.KeyEvent
.wVirtualKeyCode
== _T('C')) &&
699 (ir
.Event
.KeyEvent
.dwControlKeyState
& (LEFT_CTRL_PRESSED
| RIGHT_CTRL_PRESSED
))))