Skip to content

Commit ffeeddd

Browse files
committed
[DOSKEY]
- Fix compilation with msvc by GetProcAddress'ing undocumented functions - TCHAR -> WCHAR in the process svn path=/trunk/; revision=48942
1 parent efefc97 commit ffeeddd

File tree

2 files changed

+99
-106
lines changed

2 files changed

+99
-106
lines changed

reactos/base/applications/cmdutils/doskey/doskey.c

Lines changed: 99 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
#include <windows.h>
22
#include <stdio.h>
3-
#include <tchar.h>
3+
#include <wchar.h>
4+
#include <assert.h>
45
#include "doskey.h"
56

67
#define MAX_STRING 2000
7-
TCHAR szStringBuf[MAX_STRING];
8-
LPTSTR pszExeName = _T("cmd.exe");
8+
WCHAR szStringBuf[MAX_STRING];
9+
LPWSTR pszExeName = L"cmd.exe";
10+
11+
/* Function pointers */
12+
typedef DWORD (WINAPI *GetConsoleCommandHistoryW_t) (LPWSTR sCommands, DWORD nBufferLength, LPWSTR sExeName);
13+
typedef DWORD (WINAPI *GetConsoleCommandHistoryLengthW_t) (LPWSTR sExeName);
14+
typedef BOOL (WINAPI *SetConsoleNumberOfCommandsW_t)(DWORD nNumber, LPWSTR sExeName);
15+
typedef VOID (WINAPI *ExpungeConsoleCommandHistoryW_t)(LPWSTR sExeName);
16+
17+
GetConsoleCommandHistoryW_t pGetConsoleCommandHistoryW;
18+
GetConsoleCommandHistoryLengthW_t pGetConsoleCommandHistoryLengthW;
19+
SetConsoleNumberOfCommandsW_t pSetConsoleNumberOfCommandsW;
20+
ExpungeConsoleCommandHistoryW_t pExpungeConsoleCommandHistoryW;
921

1022
static VOID SetInsert(DWORD dwFlag)
1123
{
@@ -18,79 +30,74 @@ static VOID SetInsert(DWORD dwFlag)
1830

1931
static VOID PrintHistory(VOID)
2032
{
21-
DWORD Length = GetConsoleCommandHistoryLength(pszExeName);
22-
DWORD BufferLength;
33+
DWORD Length = pGetConsoleCommandHistoryLengthW(pszExeName);
2334
PBYTE HistBuf;
24-
TCHAR *Hist;
25-
TCHAR *HistEnd;
26-
27-
/* On Windows, the ANSI version of GetConsoleCommandHistory requires
28-
* a buffer twice as large as the actual history length. */
29-
BufferLength = Length * (sizeof(WCHAR) / sizeof(TCHAR)) * sizeof(BYTE);
35+
WCHAR *Hist;
36+
WCHAR *HistEnd;
3037

3138
HistBuf = HeapAlloc(GetProcessHeap(),
3239
HEAP_ZERO_MEMORY,
33-
BufferLength);
40+
Length);
3441
if (!HistBuf) return;
35-
Hist = (TCHAR *)HistBuf;
36-
HistEnd = (TCHAR *)&HistBuf[Length];
42+
Hist = (WCHAR *)HistBuf;
43+
HistEnd = (WCHAR *)&HistBuf[Length];
3744

38-
if (GetConsoleCommandHistory(Hist, BufferLength, pszExeName))
39-
for (; Hist < HistEnd; Hist += _tcslen(Hist) + 1)
40-
_tprintf(_T("%s\n"), Hist);
45+
if (pGetConsoleCommandHistoryW(Hist, Length, pszExeName))
46+
for (; Hist < HistEnd; Hist += wcslen(Hist) + 1)
47+
wprintf(L"%s\n", Hist);
4148

4249
HeapFree(GetProcessHeap(), 0, HistBuf);
4350
}
4451

45-
static INT SetMacro(LPTSTR definition)
52+
static INT SetMacro(LPWSTR definition)
4653
{
47-
TCHAR *name, *nameend, *text, temp;
54+
WCHAR *name, *nameend, *text, temp;
4855

4956
name = definition;
50-
while (*name == _T(' '))
57+
while (*name == L' ')
5158
name++;
5259

5360
/* error if no '=' found */
54-
if ((nameend = _tcschr(name, _T('='))) != NULL)
61+
if ((nameend = wcschr(name, L'=')) != NULL)
5562
{
5663
text = nameend + 1;
57-
while (*text == _T(' '))
64+
while (*text == L' ')
5865
text++;
5966

60-
while (nameend > name && nameend[-1] == _T(' '))
67+
while (nameend > name && nameend[-1] == L' ')
6168
nameend--;
6269

6370
/* Split rest into name and substitute */
6471
temp = *nameend;
65-
*nameend = _T('\0');
72+
*nameend = L'\0';
6673
/* Don't allow spaces in the name, since such a macro would be unusable */
67-
if (!_tcschr(name, _T(' ')) && AddConsoleAlias(name, text, pszExeName))
74+
if (!wcschr(name, L' ') && AddConsoleAlias(name, text, pszExeName))
6875
return 0;
6976
*nameend = temp;
7077
}
7178

7279
LoadString(GetModuleHandle(NULL), IDS_INVALID_MACRO_DEF, szStringBuf, MAX_STRING);
73-
_tprintf(szStringBuf, definition);
80+
wprintf(szStringBuf, definition);
7481
return 1;
7582
}
7683

77-
static VOID PrintMacros(LPTSTR pszExeName, LPTSTR Indent)
84+
static VOID PrintMacros(LPWSTR pszExeName, LPWSTR Indent)
7885
{
7986
DWORD Length = GetConsoleAliasesLength(pszExeName);
8087
PBYTE AliasBuf;
81-
TCHAR *Alias;
82-
TCHAR *AliasEnd;
88+
WCHAR *Alias;
89+
WCHAR *AliasEnd;
8390

8491
AliasBuf = HeapAlloc(GetProcessHeap(),
8592
HEAP_ZERO_MEMORY,
8693
Length * sizeof(BYTE));
8794
if (!AliasBuf) return;
88-
Alias = (TCHAR *)AliasBuf;
89-
AliasEnd = (TCHAR *)&AliasBuf[Length];
95+
Alias = (WCHAR *)AliasBuf;
96+
AliasEnd = (WCHAR *)&AliasBuf[Length];
9097

9198
if (GetConsoleAliases(Alias, Length * sizeof(BYTE), pszExeName))
92-
for (; Alias < AliasEnd; Alias += _tcslen(Alias) + 1)
93-
_tprintf(_T("%s%s\n"), Indent, Alias);
99+
for (; Alias < AliasEnd; Alias += wcslen(Alias) + 1)
100+
wprintf(L"%s%s\n", Indent, Alias);
94101

95102
HeapFree(GetProcessHeap(), 0, AliasBuf);
96103
}
@@ -99,51 +106,47 @@ static VOID PrintAllMacros(VOID)
99106
{
100107
DWORD Length = GetConsoleAliasExesLength();
101108
PBYTE ExeNameBuf;
102-
TCHAR *ExeName;
103-
TCHAR *ExeNameEnd;
109+
WCHAR *ExeName;
110+
WCHAR *ExeNameEnd;
104111

105112
ExeNameBuf = HeapAlloc(GetProcessHeap(),
106113
HEAP_ZERO_MEMORY,
107114
Length * sizeof(BYTE));
108115
if (!ExeNameBuf) return;
109-
ExeName = (TCHAR *)ExeNameBuf;
110-
ExeNameEnd = (TCHAR *)&ExeNameBuf[Length];
116+
ExeName = (WCHAR *)ExeNameBuf;
117+
ExeNameEnd = (WCHAR *)&ExeNameBuf[Length];
111118

112119
if (GetConsoleAliasExes(ExeName, Length * sizeof(BYTE)))
113120
{
114-
for (; ExeName < ExeNameEnd; ExeName += _tcslen(ExeName) + 1)
121+
for (; ExeName < ExeNameEnd; ExeName += wcslen(ExeName) + 1)
115122
{
116-
_tprintf(_T("[%s]\n"), ExeName);
117-
PrintMacros(ExeName, _T(" "));
118-
_tprintf(_T("\n"));
123+
wprintf(L"[%s]\n", ExeName);
124+
PrintMacros(ExeName, L" ");
125+
wprintf(L"\n");
119126
}
120127
}
121128

122129
HeapFree(GetProcessHeap(), 0, ExeNameBuf);
123130
}
124131

125-
static VOID ReadFromFile(LPTSTR param)
132+
static VOID ReadFromFile(LPWSTR param)
126133
{
127134
FILE* fp;
128-
TCHAR line[MAX_PATH];
135+
WCHAR line[MAX_PATH];
129136

130-
fp = _tfopen(param, _T("r"));
137+
fp = _wfopen(param, L"r");
131138
if (!fp)
132139
{
133-
#ifdef UNICODE
134140
_wperror(param);
135-
#else
136-
perror(param);
137-
#endif
138141
return;
139142
}
140143

141-
while ( _fgetts(line, MAX_PATH, fp) != NULL)
144+
while ( fgetws(line, MAX_PATH, fp) != NULL)
142145
{
143146
/* Remove newline character */
144-
TCHAR *end = &line[_tcslen(line) - 1];
145-
if (*end == _T('\n'))
146-
*end = _T('\0');
147+
WCHAR *end = &line[wcslen(line) - 1];
148+
if (*end == L'\n')
149+
*end = L'\0';
147150

148151
if (*line)
149152
SetMacro(line);
@@ -154,99 +157,109 @@ static VOID ReadFromFile(LPTSTR param)
154157
}
155158

156159
/* Get the start and end of the next command-line argument. */
157-
static BOOL GetArg(TCHAR **pStart, TCHAR **pEnd)
160+
static BOOL GetArg(WCHAR **pStart, WCHAR **pEnd)
158161
{
159162
BOOL bInQuotes = FALSE;
160-
TCHAR *p = *pEnd;
161-
p += _tcsspn(p, _T(" \t"));
163+
WCHAR *p = *pEnd;
164+
p += wcsspn(p, L" \t");
162165
if (!*p)
163166
return FALSE;
164167
*pStart = p;
165168
do
166169
{
167-
if (!bInQuotes && (*p == _T(' ') || *p == _T('\t')))
170+
if (!bInQuotes && (*p == L' ' || *p == L'\t'))
168171
break;
169-
bInQuotes ^= (*p++ == _T('"'));
172+
bInQuotes ^= (*p++ == L'"');
170173
} while (*p);
171174
*pEnd = p;
172175
return TRUE;
173176
}
174177

175178
/* Remove starting and ending quotes from a string, if present */
176-
static LPTSTR RemoveQuotes(LPTSTR str)
179+
static LPWSTR RemoveQuotes(LPWSTR str)
177180
{
178-
TCHAR *end;
179-
if (*str == _T('"') && *(end = str + _tcslen(str) - 1) == _T('"'))
181+
WCHAR *end;
182+
if (*str == L'"' && *(end = str + wcslen(str) - 1) == L'"')
180183
{
181184
str++;
182-
*end = _T('\0');
185+
*end = L'\0';
183186
}
184187
return str;
185188
}
186189

187190
int
188-
_tmain(VOID)
191+
wmain(VOID)
189192
{
190193
/* Get the full command line using GetCommandLine(). We can't just use argv,
191194
* because then a parameter like "gotoroot=cd \" wouldn't be passed completely. */
192-
TCHAR *pArgStart;
193-
TCHAR *pArgEnd = GetCommandLine();
195+
WCHAR *pArgStart;
196+
WCHAR *pArgEnd = GetCommandLine();
197+
HMODULE hKernel32 = LoadLibraryW(L"kernel32.dll");
198+
199+
/* Get function pointers */
200+
pGetConsoleCommandHistoryW = (GetConsoleCommandHistoryW_t)GetProcAddress( hKernel32, "GetConsoleCommandHistoryW");
201+
pGetConsoleCommandHistoryLengthW = (GetConsoleCommandHistoryLengthW_t)GetProcAddress( hKernel32, "GetConsoleCommandHistoryLengthW");
202+
pSetConsoleNumberOfCommandsW = (SetConsoleNumberOfCommandsW_t)GetProcAddress( hKernel32, "SetConsoleNumberOfCommandsW");
203+
pExpungeConsoleCommandHistoryW = (ExpungeConsoleCommandHistoryW_t)GetProcAddress( hKernel32, "ExpungeConsoleCommandHistoryW");
204+
205+
assert(pGetConsoleCommandHistoryW && pGetConsoleCommandHistoryLengthW &&
206+
pSetConsoleNumberOfCommandsW && pSetConsoleNumberOfCommandsW);
194207

195208
/* Skip the application name */
196209
GetArg(&pArgStart, &pArgEnd);
197210

198211
while (GetArg(&pArgStart, &pArgEnd))
199212
{
200213
/* NUL-terminate this argument to make processing easier */
201-
TCHAR tmp = *pArgEnd;
202-
*pArgEnd = _T('\0');
214+
WCHAR tmp = *pArgEnd;
215+
*pArgEnd = L'\0';
203216

204-
if (!_tcscmp(pArgStart, _T("/?")))
217+
if (!wcscmp(pArgStart, L"/?"))
205218
{
206219
LoadString(GetModuleHandle(NULL), IDS_HELP, szStringBuf, MAX_STRING);
207-
_tprintf(szStringBuf);
220+
wprintf(szStringBuf);
208221
break;
209222
}
210-
else if (!_tcsnicmp(pArgStart, _T("/EXENAME="), 9))
223+
else if (!_wcsnicmp(pArgStart, L"/EXENAME=", 9))
211224
{
212225
pszExeName = RemoveQuotes(pArgStart + 9);
213226
}
214-
else if (!_tcsicmp(pArgStart, _T("/H")) ||
215-
!_tcsicmp(pArgStart, _T("/HISTORY")))
227+
else if (!wcsicmp(pArgStart, L"/H") ||
228+
!wcsicmp(pArgStart, L"/HISTORY"))
216229
{
217230
PrintHistory();
218231
}
219-
else if (!_tcsnicmp(pArgStart, _T("/LISTSIZE="), 10))
232+
else if (!_wcsnicmp(pArgStart, L"/LISTSIZE=", 10))
220233
{
221-
SetConsoleNumberOfCommands(_ttoi(pArgStart + 10), pszExeName);
234+
pSetConsoleNumberOfCommandsW(_wtoi(pArgStart + 10), pszExeName);
222235
}
223-
else if (!_tcsicmp(pArgStart, _T("/REINSTALL")))
236+
else if (!wcsicmp(pArgStart, L"/REINSTALL"))
224237
{
225-
ExpungeConsoleCommandHistory(pszExeName);
238+
pExpungeConsoleCommandHistoryW(pszExeName);
226239
}
227-
else if (!_tcsicmp(pArgStart, _T("/INSERT")))
240+
else if (!wcsicmp(pArgStart, L"/INSERT"))
228241
{
229242
SetInsert(ENABLE_INSERT_MODE);
230243
}
231-
else if (!_tcsicmp(pArgStart, _T("/OVERSTRIKE")))
244+
else if (!wcsicmp(pArgStart, L"/OVERSTRIKE"))
232245
{
233246
SetInsert(0);
234247
}
235-
else if (!_tcsicmp(pArgStart, _T("/M")) ||
236-
!_tcsicmp(pArgStart, _T("/MACROS")))
248+
else if (!wcsicmp(pArgStart, L"/M") ||
249+
!wcsicmp(pArgStart, L"/MACROS"))
237250
{
238-
PrintMacros(pszExeName, _T(""));
251+
PrintMacros(pszExeName, L"");
239252
}
240-
else if (!_tcsnicmp(pArgStart, _T("/M:"), 3) ||
241-
!_tcsnicmp(pArgStart, _T("/MACROS:"), 8))
253+
else if (!_wcsnicmp(pArgStart, L"/M:", 3) ||
254+
!_wcsnicmp(pArgStart, L"/MACROS:", 8))
242255
{
243-
LPTSTR exe = RemoveQuotes(_tcschr(pArgStart, _T(':')) + 1);
244-
if (!_tcsicmp(exe, _T("ALL")))
256+
LPWSTR exe = RemoveQuotes(wcschr(pArgStart, L':') + 1);
257+
if (!wcsicmp(exe, L"ALL"))
245258
PrintAllMacros();
246259
else
247-
PrintMacros(exe, _T(""));
260+
PrintMacros(exe, L"");
248261
}
249-
else if (!_tcsnicmp(pArgStart, _T("/MACROFILE="), 11))
262+
else if (!_wcsnicmp(pArgStart, L"/MACROFILE=", 11))
250263
{
251264
ReadFromFile(RemoveQuotes(pArgStart + 11));
252265
}

reactos/base/applications/cmdutils/doskey/doskey.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ BOOL WINAPI AddConsoleAliasA(LPSTR, LPSTR, LPSTR);
2323
BOOL WINAPI AddConsoleAliasW(LPWSTR, LPWSTR, LPWSTR);
2424
#define AddConsoleAlias TNAME(AddConsoleAlias)
2525
#endif
26-
#ifndef ExpungeConsoleCommandHistory
27-
BOOL WINAPI ExpungeConsoleCommandHistoryA(LPSTR);
28-
BOOL WINAPI ExpungeConsoleCommandHistoryW(LPWSTR);
29-
#define ExpungeConsoleCommandHistory TNAME(ExpungeConsoleCommandHistory)
30-
#endif
3126
#ifndef GetConsoleAliases
3227
DWORD WINAPI GetConsoleAliasesA(LPSTR, DWORD, LPSTR);
3328
DWORD WINAPI GetConsoleAliasesW(LPWSTR, DWORD, LPWSTR);
@@ -48,20 +43,5 @@ DWORD WINAPI GetConsoleAliasExesLengthA(VOID);
4843
DWORD WINAPI GetConsoleAliasExesLengthW(VOID);
4944
#define GetConsoleAliasExesLength TNAME(GetConsoleAliasExesLength)
5045
#endif
51-
#ifndef GetConsoleCommandHistory
52-
DWORD WINAPI GetConsoleCommandHistoryA(LPSTR, DWORD, LPSTR);
53-
DWORD WINAPI GetConsoleCommandHistoryW(LPWSTR, DWORD, LPWSTR);
54-
#define GetConsoleCommandHistory TNAME(GetConsoleCommandHistory)
55-
#endif
56-
#ifndef GetConsoleCommandHistoryLength
57-
DWORD WINAPI GetConsoleCommandHistoryLengthA(LPSTR);
58-
DWORD WINAPI GetConsoleCommandHistoryLengthW(LPWSTR);
59-
#define GetConsoleCommandHistoryLength TNAME(GetConsoleCommandHistoryLength)
60-
#endif
61-
#ifndef SetConsoleNumberOfCommands
62-
BOOL WINAPI SetConsoleNumberOfCommandsA(DWORD, LPSTR);
63-
BOOL WINAPI SetConsoleNumberOfCommandsW(DWORD, LPWSTR);
64-
#define SetConsoleNumberOfCommands TNAME(SetConsoleNumberOfCommands)
65-
#endif
6646

6747
#endif /* RC_INVOKED */

0 commit comments

Comments
 (0)