changes to make cmd compile (not link)
[reactos.git] / reactos / apps / utils / cmd / alias.c
1 /*
2 * ALIAS.C - alias administration module.
3 *
4 *
5 * History:
6 *
7 * 02/02/1996 (Oliver Mueller)
8 * started.
9 *
10 * 02/03/1996 (Oliver Mueller)
11 * Added sorting algorithm and case sensitive substitution by using
12 * partstrupr().
13 *
14 * 27 Jul 1998 John P. Price
15 * added config.h include
16 * added ifdef's to disable aliases
17 *
18 * 09-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
19 * Fixed crash when removing an alias in DeleteAlias().
20 * Added help text ("/?").
21 *
22 * 14-Jan-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
23 * Clean up and Unicode safe!
24 *
25 * 24-Jan-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
26 * Redirection safe!
27 */
28
29
30 #include "config.h"
31
32 #ifdef FEATURE_ALIASES
33
34 #include <windows.h>
35 #include <tchar.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39
40 #include "cmd.h"
41
42
43 typedef struct tagALIAS
44 {
45 struct tagALIAS *next;
46 LPTSTR lpName;
47 LPTSTR lpSubst;
48 WORD wUsed;
49 } ALIAS, *LPALIAS;
50
51
52 static LPALIAS lpFirst = NULL;
53 static LPALIAS lpLast = NULL;
54 static WORD wUsed = 0;
55
56
57 /* module internal functions */
58 /* strlwr only for first word in string */
59 static VOID
60 partstrlwr (LPTSTR str)
61 {
62 LPTSTR c = str;
63 while (*c && !_istspace (*c))
64 {
65 *c = _totlower (*c);
66 c++;
67 }
68 }
69
70
71 static VOID
72 PrintAlias (VOID)
73 {
74 LPALIAS ptr = lpFirst;
75 while (ptr)
76 {
77 ConOutPrintf (_T("%s=%s\n"), ptr->lpName, ptr->lpSubst);
78 ptr = ptr->next;
79 }
80 }
81
82
83 static VOID
84 DeleteAlias (LPTSTR pszName)
85 {
86 LPALIAS ptr = lpFirst;
87 LPALIAS prev = NULL;
88
89 while (ptr)
90 {
91 if (!_tcsicmp (ptr->lpName, pszName))
92 {
93 if (prev)
94 prev->next = ptr->next;
95 else
96 lpFirst = ptr->next;
97 free (ptr->lpName);
98 free (ptr->lpSubst);
99 free (ptr);
100 return;
101 }
102 prev = ptr;
103 ptr = ptr->next;
104 }
105 }
106
107
108 static INT
109 AddAlias (LPTSTR name, LPTSTR subst)
110 {
111 LPALIAS ptr = lpFirst;
112 LPALIAS prev, entry;
113 LPTSTR s;
114
115 while (ptr)
116 {
117 if (!_tcsicmp (ptr->lpName, name))
118 {
119 s = (LPTSTR)malloc ((_tcslen (subst) + 1)*sizeof(TCHAR));
120 if (!s)
121 {
122 error_out_of_memory ();
123 return 1;
124 }
125
126 free (ptr->lpSubst);
127 ptr->lpSubst = s;
128 _tcscpy (ptr->lpSubst, subst);
129 return 0;
130 }
131 ptr = ptr->next;
132 }
133
134 ptr = (LPALIAS)malloc (sizeof (ALIAS));
135 if (!ptr)
136 return 1;
137
138 ptr->next = 0;
139
140 ptr->lpName = (LPTSTR)malloc ((_tcslen (name) + 1)*sizeof(TCHAR));
141 if (!ptr->lpName)
142 {
143 error_out_of_memory ();
144 free (ptr);
145 return 1;
146 }
147 _tcscpy (ptr->lpName, name);
148
149 ptr->lpSubst = (LPTSTR)malloc ((_tcslen (subst) + 1)*sizeof(TCHAR));
150 if (!ptr->lpSubst)
151 {
152 error_out_of_memory ();
153 free (ptr->lpName);
154 free (ptr);
155 return 1;
156 }
157 _tcscpy (ptr->lpSubst, subst);
158
159 /* it's necessary for recursive substitution */
160 partstrlwr (ptr->lpSubst);
161
162 ptr->wUsed = 0;
163
164 /* Alias table must be sorted!
165 * Here a little example:
166 * command line = "ls -c"
167 * If the entries are
168 * ls=dir
169 * ls -c=ls /w
170 * command line will be expanded to "dir -c" which is not correct.
171 * If the entries are sortet as
172 * ls -c=ls /w
173 * ls=dir
174 * it will be expanded to "dir /w" which is a valid DOS command.
175 */
176 entry = lpFirst;
177 prev = 0;
178 while (entry)
179 {
180 if (_tcsicmp (ptr->lpName, entry->lpName) > 0)
181 {
182 if (prev)
183 {
184 prev->next = ptr;
185 ptr->next = entry;
186 }
187 else
188 {
189 ptr->next = entry;
190 lpFirst = ptr;
191 }
192 return 0;
193 }
194 prev = entry;
195 entry = entry->next;
196 }
197
198 /* The new entry is the smallest (or the first) and must be
199 * added to the end of the list.
200 */
201 if (!lpFirst)
202 lpFirst = ptr;
203 else
204 lpLast->next = ptr;
205 lpLast = ptr;
206
207 return 0;
208 }
209
210
211 /* specified routines */
212 VOID ExpandAlias (LPTSTR cmd, INT maxlen)
213 {
214 unsigned n = 0,
215 m,
216 i,
217 len;
218 short d = 1;
219 LPALIAS ptr = lpFirst;
220
221 wUsed++;
222 if (wUsed == 0)
223 {
224 while (ptr)
225 ptr->wUsed = 0;
226 ptr = lpFirst;
227 wUsed = 1;
228 }
229
230 /* skipping white spaces */
231 while (_istspace (cmd[n]))
232 n++;
233
234 partstrlwr (&cmd[n]);
235
236 if (!_tcsncmp (&cmd[n], _T("NOALIAS"), 7) &&
237 (_istspace (cmd[n + 7]) || cmd[n + 7] == _T('\0')))
238 {
239 memmove (cmd, &cmd[n + 7], (_tcslen (&cmd[n + 7]) + 1) * sizeof (TCHAR));
240 return;
241 }
242
243 /* substitution loop */
244 while (d)
245 {
246 d = 0;
247 while (ptr)
248 {
249 len = _tcslen (ptr->lpName);
250 if (!_tcsncmp (&cmd[n], ptr->lpName, len) &&
251 (_istspace (cmd[n + len]) || cmd[n + len] == _T('\0')) &&
252 ptr->wUsed != wUsed)
253 {
254 m = _tcslen (ptr->lpSubst);
255 if ((int)(_tcslen (cmd) - len + m - n) > maxlen)
256 {
257 ConErrPrintf (_T("Command line too long after alias expansion!\n"));
258 /* the parser won't cause any problems with an empty line */
259 cmd[0] = _T('\0');
260 }
261 else
262 {
263 memmove (&cmd[m], &cmd[n + len], (_tcslen(&cmd[n + len]) + 1) * sizeof (TCHAR));
264 for (i = 0; i < m; i++)
265 cmd[i] = ptr->lpSubst[i];
266 ptr->wUsed = wUsed;
267 /* whitespaces are removed! */
268 n = 0;
269 d = 1;
270 }
271 }
272 ptr = ptr->next;
273 }
274 }
275 }
276
277
278 INT cmd_alias (LPTSTR cmd, LPTSTR param)
279 {
280 LPTSTR ptr;
281 INT n = 0;
282
283 if (!_tcsncmp (param, _T("/?"), 2))
284 {
285 ConOutPuts (_T("Sets, removes or shows aliases.\n"
286 "\n"
287 "ALIAS [alias=[command]]\n"
288 "\n"
289 " alias Name for an alias.\n"
290 " command Text to be substituted for an alias.\n"
291 "\n"
292 // "For example:\n"
293 "To list all aliases:\n"
294 " ALIAS\n\n"
295 "To set a new or replace an existing alias:\n"
296 " ALIAS da=dir a:\n\n"
297 "To remove an alias from the alias list:\n"
298 " ALIAS da="
299 // "Type ALIAS without a parameter to display the alias list.\n"
300 ));
301 return 0;
302 }
303
304 if (param[0] == _T('\0'))
305 {
306 PrintAlias ();
307 return 0;
308 }
309
310 /* error if no '=' found */
311 if ((ptr = _tcschr (param, _T('='))) == 0)
312 return -1;
313
314 /* Split rest into name and substitute */
315 *ptr++ = _T('\0');
316
317 partstrlwr (param);
318
319 if (ptr[0] == _T('\0'))
320 DeleteAlias (param);
321 else
322 n = AddAlias (param, ptr);
323
324 return n;
325 }
326 #endif