2008-02-02 21:23:38 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
2010-09-30 18:45:58 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <assert.h>
|
2011-04-14 12:13:38 +00:00
|
|
|
#include <locale.h>
|
2009-03-12 04:37:01 +00:00
|
|
|
#include "doskey.h"
|
2008-02-02 21:23:38 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
#define MAX_STRING 2000
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR szStringBuf[MAX_STRING];
|
|
|
|
LPWSTR pszExeName = L"cmd.exe";
|
|
|
|
|
|
|
|
/* Function pointers */
|
2011-05-13 17:16:08 +00:00
|
|
|
typedef DWORD (WINAPI *GetConsoleCommandHistoryW_t) (LPWSTR sCommands, DWORD nBufferLength, LPWSTR sExeName);
|
2010-09-30 18:45:58 +00:00
|
|
|
typedef DWORD (WINAPI *GetConsoleCommandHistoryLengthW_t) (LPWSTR sExeName);
|
|
|
|
typedef BOOL (WINAPI *SetConsoleNumberOfCommandsW_t)(DWORD nNumber, LPWSTR sExeName);
|
|
|
|
typedef VOID (WINAPI *ExpungeConsoleCommandHistoryW_t)(LPWSTR sExeName);
|
|
|
|
|
|
|
|
GetConsoleCommandHistoryW_t pGetConsoleCommandHistoryW;
|
|
|
|
GetConsoleCommandHistoryLengthW_t pGetConsoleCommandHistoryLengthW;
|
|
|
|
SetConsoleNumberOfCommandsW_t pSetConsoleNumberOfCommandsW;
|
|
|
|
ExpungeConsoleCommandHistoryW_t pExpungeConsoleCommandHistoryW;
|
2008-05-08 18:11:56 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
static VOID SetInsert(DWORD dwFlag)
|
|
|
|
{
|
|
|
|
DWORD dwMode;
|
|
|
|
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
|
|
|
|
GetConsoleMode(hConsole, &dwMode);
|
|
|
|
dwMode |= ENABLE_EXTENDED_FLAGS;
|
|
|
|
SetConsoleMode(hConsole, (dwMode & ~ENABLE_INSERT_MODE) | dwFlag);
|
|
|
|
}
|
2008-05-08 18:11:56 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
static VOID PrintHistory(VOID)
|
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
DWORD Length = pGetConsoleCommandHistoryLengthW(pszExeName);
|
2009-08-02 17:38:27 +00:00
|
|
|
PBYTE HistBuf;
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR *Hist;
|
|
|
|
WCHAR *HistEnd;
|
2009-08-02 17:38:27 +00:00
|
|
|
|
|
|
|
HistBuf = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
2010-09-30 18:45:58 +00:00
|
|
|
Length);
|
2009-08-02 17:38:27 +00:00
|
|
|
if (!HistBuf) return;
|
2010-09-30 18:45:58 +00:00
|
|
|
Hist = (WCHAR *)HistBuf;
|
|
|
|
HistEnd = (WCHAR *)&HistBuf[Length];
|
2009-03-12 04:37:01 +00:00
|
|
|
|
2010-09-30 18:45:58 +00:00
|
|
|
if (pGetConsoleCommandHistoryW(Hist, Length, pszExeName))
|
|
|
|
for (; Hist < HistEnd; Hist += wcslen(Hist) + 1)
|
|
|
|
wprintf(L"%s\n", Hist);
|
2009-08-02 17:38:27 +00:00
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, HistBuf);
|
2008-02-02 21:23:38 +00:00
|
|
|
}
|
|
|
|
|
2010-09-30 18:45:58 +00:00
|
|
|
static INT SetMacro(LPWSTR definition)
|
2008-02-02 21:32:11 +00:00
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR *name, *nameend, *text, temp;
|
2008-02-02 21:32:11 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
name = definition;
|
2010-09-30 18:45:58 +00:00
|
|
|
while (*name == L' ')
|
2009-03-12 04:37:01 +00:00
|
|
|
name++;
|
2008-02-02 21:52:46 +00:00
|
|
|
|
2008-05-08 18:11:56 +00:00
|
|
|
/* error if no '=' found */
|
2010-09-30 18:45:58 +00:00
|
|
|
if ((nameend = wcschr(name, L'=')) != NULL)
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
|
|
|
text = nameend + 1;
|
2010-09-30 18:45:58 +00:00
|
|
|
while (*text == L' ')
|
2009-03-12 04:37:01 +00:00
|
|
|
text++;
|
|
|
|
|
2010-09-30 18:45:58 +00:00
|
|
|
while (nameend > name && nameend[-1] == L' ')
|
2009-03-12 04:37:01 +00:00
|
|
|
nameend--;
|
|
|
|
|
|
|
|
/* Split rest into name and substitute */
|
|
|
|
temp = *nameend;
|
2010-09-30 18:45:58 +00:00
|
|
|
*nameend = L'\0';
|
2009-03-12 04:37:01 +00:00
|
|
|
/* Don't allow spaces in the name, since such a macro would be unusable */
|
2010-09-30 18:45:58 +00:00
|
|
|
if (!wcschr(name, L' ') && AddConsoleAlias(name, text, pszExeName))
|
2009-03-12 04:37:01 +00:00
|
|
|
return 0;
|
|
|
|
*nameend = temp;
|
|
|
|
}
|
2008-02-02 21:32:11 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
LoadString(GetModuleHandle(NULL), IDS_INVALID_MACRO_DEF, szStringBuf, MAX_STRING);
|
2010-09-30 18:45:58 +00:00
|
|
|
wprintf(szStringBuf, definition);
|
2009-03-12 04:37:01 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2008-02-02 21:32:11 +00:00
|
|
|
|
2010-09-30 18:45:58 +00:00
|
|
|
static VOID PrintMacros(LPWSTR pszExeName, LPWSTR Indent)
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
|
|
|
DWORD Length = GetConsoleAliasesLength(pszExeName);
|
2009-08-02 17:38:27 +00:00
|
|
|
PBYTE AliasBuf;
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR *Alias;
|
|
|
|
WCHAR *AliasEnd;
|
2009-08-02 17:38:27 +00:00
|
|
|
|
|
|
|
AliasBuf = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
|
|
|
Length * sizeof(BYTE));
|
|
|
|
if (!AliasBuf) return;
|
2010-09-30 18:45:58 +00:00
|
|
|
Alias = (WCHAR *)AliasBuf;
|
|
|
|
AliasEnd = (WCHAR *)&AliasBuf[Length];
|
2009-08-02 17:38:27 +00:00
|
|
|
|
|
|
|
if (GetConsoleAliases(Alias, Length * sizeof(BYTE), pszExeName))
|
2010-09-30 18:45:58 +00:00
|
|
|
for (; Alias < AliasEnd; Alias += wcslen(Alias) + 1)
|
|
|
|
wprintf(L"%s%s\n", Indent, Alias);
|
2009-08-02 17:38:27 +00:00
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, AliasBuf);
|
2009-03-12 04:37:01 +00:00
|
|
|
}
|
2008-02-02 22:49:24 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
static VOID PrintAllMacros(VOID)
|
|
|
|
{
|
|
|
|
DWORD Length = GetConsoleAliasExesLength();
|
2009-08-02 17:38:27 +00:00
|
|
|
PBYTE ExeNameBuf;
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR *ExeName;
|
|
|
|
WCHAR *ExeNameEnd;
|
2009-08-02 17:38:27 +00:00
|
|
|
|
|
|
|
ExeNameBuf = HeapAlloc(GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
|
|
|
Length * sizeof(BYTE));
|
|
|
|
if (!ExeNameBuf) return;
|
2010-09-30 18:45:58 +00:00
|
|
|
ExeName = (WCHAR *)ExeNameBuf;
|
|
|
|
ExeNameEnd = (WCHAR *)&ExeNameBuf[Length];
|
2009-08-02 17:38:27 +00:00
|
|
|
|
|
|
|
if (GetConsoleAliasExes(ExeName, Length * sizeof(BYTE)))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
for (; ExeName < ExeNameEnd; ExeName += wcslen(ExeName) + 1)
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
wprintf(L"[%s]\n", ExeName);
|
|
|
|
PrintMacros(ExeName, L" ");
|
|
|
|
wprintf(L"\n");
|
2009-03-12 04:37:01 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-02 17:38:27 +00:00
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, ExeNameBuf);
|
2008-02-02 21:32:11 +00:00
|
|
|
}
|
|
|
|
|
2010-09-30 18:45:58 +00:00
|
|
|
static VOID ReadFromFile(LPWSTR param)
|
2008-02-02 21:52:46 +00:00
|
|
|
{
|
2008-05-08 18:11:56 +00:00
|
|
|
FILE* fp;
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR line[MAX_PATH];
|
2008-05-08 18:11:56 +00:00
|
|
|
|
2010-09-30 18:45:58 +00:00
|
|
|
fp = _wfopen(param, L"r");
|
2009-03-12 04:37:01 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
_wperror(param);
|
|
|
|
return;
|
|
|
|
}
|
2008-05-08 18:11:56 +00:00
|
|
|
|
2011-05-13 17:16:08 +00:00
|
|
|
while ( fgetws(line, MAX_PATH, fp) != NULL)
|
2009-03-01 19:11:55 +00:00
|
|
|
{
|
|
|
|
/* Remove newline character */
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR *end = &line[wcslen(line) - 1];
|
|
|
|
if (*end == L'\n')
|
|
|
|
*end = L'\0';
|
2009-03-01 19:11:55 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
if (*line)
|
|
|
|
SetMacro(line);
|
2009-03-01 19:11:55 +00:00
|
|
|
}
|
2008-05-08 18:11:56 +00:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
2008-02-02 21:52:46 +00:00
|
|
|
}
|
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
/* Get the start and end of the next command-line argument. */
|
2010-09-30 18:45:58 +00:00
|
|
|
static BOOL GetArg(WCHAR **pStart, WCHAR **pEnd)
|
2008-02-02 21:23:38 +00:00
|
|
|
{
|
2009-03-12 04:37:01 +00:00
|
|
|
BOOL bInQuotes = FALSE;
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR *p = *pEnd;
|
|
|
|
p += wcsspn(p, L" \t");
|
2009-03-12 04:37:01 +00:00
|
|
|
if (!*p)
|
|
|
|
return FALSE;
|
|
|
|
*pStart = p;
|
|
|
|
do
|
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
if (!bInQuotes && (*p == L' ' || *p == L'\t'))
|
2009-03-12 04:37:01 +00:00
|
|
|
break;
|
2010-09-30 18:45:58 +00:00
|
|
|
bInQuotes ^= (*p++ == L'"');
|
2009-03-12 04:37:01 +00:00
|
|
|
} while (*p);
|
|
|
|
*pEnd = p;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2008-05-08 18:11:56 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
/* Remove starting and ending quotes from a string, if present */
|
2010-09-30 18:45:58 +00:00
|
|
|
static LPWSTR RemoveQuotes(LPWSTR str)
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR *end;
|
|
|
|
if (*str == L'"' && *(end = str + wcslen(str) - 1) == L'"')
|
2008-05-08 18:11:56 +00:00
|
|
|
{
|
2009-03-12 04:37:01 +00:00
|
|
|
str++;
|
2010-09-30 18:45:58 +00:00
|
|
|
*end = L'\0';
|
2008-05-08 18:11:56 +00:00
|
|
|
}
|
2009-03-12 04:37:01 +00:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2010-09-30 18:45:58 +00:00
|
|
|
wmain(VOID)
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
2011-05-13 17:16:08 +00:00
|
|
|
WCHAR *pArgStart;
|
|
|
|
WCHAR *pArgEnd;
|
|
|
|
HMODULE hKernel32;
|
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
2011-04-14 12:13:38 +00:00
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
/* Get the full command line using GetCommandLine(). We can't just use argv,
|
|
|
|
* because then a parameter like "gotoroot=cd \" wouldn't be passed completely. */
|
2011-05-13 17:16:08 +00:00
|
|
|
pArgEnd = GetCommandLine();
|
|
|
|
hKernel32 = LoadLibraryW(L"kernel32.dll");
|
2010-09-30 18:45:58 +00:00
|
|
|
|
|
|
|
/* Get function pointers */
|
|
|
|
pGetConsoleCommandHistoryW = (GetConsoleCommandHistoryW_t)GetProcAddress( hKernel32, "GetConsoleCommandHistoryW");
|
|
|
|
pGetConsoleCommandHistoryLengthW = (GetConsoleCommandHistoryLengthW_t)GetProcAddress( hKernel32, "GetConsoleCommandHistoryLengthW");
|
|
|
|
pSetConsoleNumberOfCommandsW = (SetConsoleNumberOfCommandsW_t)GetProcAddress( hKernel32, "SetConsoleNumberOfCommandsW");
|
|
|
|
pExpungeConsoleCommandHistoryW = (ExpungeConsoleCommandHistoryW_t)GetProcAddress( hKernel32, "ExpungeConsoleCommandHistoryW");
|
|
|
|
|
|
|
|
assert(pGetConsoleCommandHistoryW && pGetConsoleCommandHistoryLengthW &&
|
2011-06-20 09:18:42 +00:00
|
|
|
pSetConsoleNumberOfCommandsW && pExpungeConsoleCommandHistoryW);
|
2009-03-12 04:37:01 +00:00
|
|
|
|
|
|
|
/* Skip the application name */
|
|
|
|
GetArg(&pArgStart, &pArgEnd);
|
|
|
|
|
|
|
|
while (GetArg(&pArgStart, &pArgEnd))
|
2008-05-08 18:11:56 +00:00
|
|
|
{
|
2009-03-12 04:37:01 +00:00
|
|
|
/* NUL-terminate this argument to make processing easier */
|
2010-09-30 18:45:58 +00:00
|
|
|
WCHAR tmp = *pArgEnd;
|
|
|
|
*pArgEnd = L'\0';
|
2008-05-08 18:11:56 +00:00
|
|
|
|
2010-09-30 18:45:58 +00:00
|
|
|
if (!wcscmp(pArgStart, L"/?"))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
|
|
|
LoadString(GetModuleHandle(NULL), IDS_HELP, szStringBuf, MAX_STRING);
|
2010-09-30 18:45:58 +00:00
|
|
|
wprintf(szStringBuf);
|
2009-03-12 04:37:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!_wcsnicmp(pArgStart, L"/EXENAME=", 9))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
|
|
|
pszExeName = RemoveQuotes(pArgStart + 9);
|
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!wcsicmp(pArgStart, L"/H") ||
|
|
|
|
!wcsicmp(pArgStart, L"/HISTORY"))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
|
|
|
PrintHistory();
|
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!_wcsnicmp(pArgStart, L"/LISTSIZE=", 10))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
pSetConsoleNumberOfCommandsW(_wtoi(pArgStart + 10), pszExeName);
|
2009-03-12 04:37:01 +00:00
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!wcsicmp(pArgStart, L"/REINSTALL"))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
pExpungeConsoleCommandHistoryW(pszExeName);
|
2009-03-12 04:37:01 +00:00
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!wcsicmp(pArgStart, L"/INSERT"))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
|
|
|
SetInsert(ENABLE_INSERT_MODE);
|
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!wcsicmp(pArgStart, L"/OVERSTRIKE"))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
|
|
|
SetInsert(0);
|
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!wcsicmp(pArgStart, L"/M") ||
|
|
|
|
!wcsicmp(pArgStart, L"/MACROS"))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
PrintMacros(pszExeName, L"");
|
2009-03-12 04:37:01 +00:00
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!_wcsnicmp(pArgStart, L"/M:", 3) ||
|
|
|
|
!_wcsnicmp(pArgStart, L"/MACROS:", 8))
|
2009-03-12 04:37:01 +00:00
|
|
|
{
|
2010-09-30 18:45:58 +00:00
|
|
|
LPWSTR exe = RemoveQuotes(wcschr(pArgStart, L':') + 1);
|
|
|
|
if (!wcsicmp(exe, L"ALL"))
|
2009-03-12 04:37:01 +00:00
|
|
|
PrintAllMacros();
|
|
|
|
else
|
2010-09-30 18:45:58 +00:00
|
|
|
PrintMacros(exe, L"");
|
2009-03-12 04:37:01 +00:00
|
|
|
}
|
2010-09-30 18:45:58 +00:00
|
|
|
else if (!_wcsnicmp(pArgStart, L"/MACROFILE=", 11))
|
2008-05-08 18:11:56 +00:00
|
|
|
{
|
2009-03-12 04:37:01 +00:00
|
|
|
ReadFromFile(RemoveQuotes(pArgStart + 11));
|
2008-05-08 18:11:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-12 04:37:01 +00:00
|
|
|
/* This is the beginning of a macro definition. It includes
|
|
|
|
* the entire remainder of the line, so first put back the
|
|
|
|
* character that we replaced with NUL. */
|
|
|
|
*pArgEnd = tmp;
|
|
|
|
return SetMacro(pArgStart);
|
2008-05-08 18:11:56 +00:00
|
|
|
}
|
|
|
|
|
2009-03-12 04:37:01 +00:00
|
|
|
if (!tmp) break;
|
|
|
|
pArgEnd++;
|
2008-05-08 18:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2008-02-02 21:23:38 +00:00
|
|
|
}
|