- Fix build by converting everything to use TCHAR's
[reactos.git] / reactos / base / applications / cmdutils / doskey / doskey.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <tchar.h>
4
5 static VOID
6 partstrlwr (LPTSTR str)
7 {
8 LPTSTR c = str;
9 while (*c && !_istspace (*c) && *c != _T('='))
10 {
11 *c = _totlower (*c);
12 c++;
13 }
14 }
15
16 static VOID
17 PrintAlias (VOID)
18 {
19 LPTSTR Aliases;
20 LPTSTR ptr;
21 DWORD len;
22
23 len = GetConsoleAliasesLength(_T("cmd.exe"));
24 if (len <= 0)
25 return;
26
27 /* allocate memory for an extra \0 char to make parsing easier */
28 ptr = HeapAlloc(GetProcessHeap(), 0, (len + sizeof(TCHAR)));
29 if (!ptr)
30 return;
31
32 Aliases = ptr;
33
34 ZeroMemory(Aliases, len + sizeof(TCHAR));
35
36 if (GetConsoleAliases(Aliases, len, _T("cmd.exe")) != 0)
37 {
38 while (*Aliases != '\0')
39 {
40 _tprintf(_T("%s\n"), Aliases);
41 Aliases = Aliases + lstrlen(Aliases);
42 Aliases++;
43 }
44 }
45 HeapFree(GetProcessHeap(), 0 , ptr);
46 }
47
48 INT SetMacro (LPTSTR param)
49 {
50 LPTSTR ptr;
51
52 while (*param == _T(' '))
53 param++;
54
55 /* error if no '=' found */
56 if ((ptr = _tcschr (param, _T('='))) == 0)
57 return 1;
58
59 while (*param == _T(' '))
60 param++;
61
62 while (*ptr == _T(' '))
63 ptr--;
64
65 /* Split rest into name and substitute */
66 *ptr++ = _T('\0');
67
68 partstrlwr (param);
69
70 _tprintf(_T("%s, %s\n"), ptr, param);
71
72 if (ptr[0] == _T('\0'))
73 AddConsoleAlias(param, NULL, _T("cmd.exe"));
74 else
75 AddConsoleAlias(param, ptr, _T("cmd.exe"));
76
77 return 0;
78 }
79
80 static VOID ReadFromFile(LPTSTR param)
81 {
82 FILE* fp;
83 TCHAR line[MAX_PATH];
84
85 /* Skip the "/macrofile=" prefix */
86 param += 11;
87
88 fp = _tfopen(param, _T("r"));
89
90 while ( _fgetts(line, MAX_PATH, fp) != NULL)
91 SetMacro(line);
92
93 fclose(fp);
94 return;
95 }
96
97 int
98 _tmain (int argc, LPTSTR argv[])
99 {
100 if (argc < 2)
101 return 0;
102
103 if (argv[1][0] == '/')
104 {
105 if (_tcsnicmp(argv[1], _T("/macrofile"), 10) == 0)
106 ReadFromFile(argv[1]);
107 if (_tcscmp(argv[1], _T("/macros")) == 0)
108 PrintAlias();
109 }
110 else
111 {
112 /* Get the full command line using GetCommandLine().
113 We can't just pass argv[1] here, because then a parameter like "gotoroot=cd \" wouldn't be passed completely. */
114 TCHAR* szCommandLine = GetCommandLine();
115
116 /* Skip the application name */
117 if(*szCommandLine == '\"')
118 {
119 do
120 {
121 szCommandLine++;
122 }
123 while(*szCommandLine != '\"');
124 }
125 else
126 {
127 do
128 {
129 szCommandLine++;
130 }
131 while(*szCommandLine != ' ');
132 }
133
134 /* Skip the trailing quotation mark/whitespace and pass the command line to SetMacro */
135 SetMacro(++szCommandLine);
136 }
137
138 return 0;
139 }