2 * ALIAS.C - alias administration module.
7 * 02/02/1996 (Oliver Mueller)
10 * 02/03/1996 (Oliver Mueller)
11 * Added sorting algorithm and case sensitive substitution by using
14 * 27 Jul 1998 John P. Price
15 * added config.h include
16 * added ifdef's to disable aliases
18 * 09-Dec-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
19 * Fixed crash when removing an alias in DeleteAlias().
20 * Added help text ("/?").
22 * 14-Jan-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
23 * Clean up and Unicode safe!
25 * 24-Jan-1998 (Eric Kohl <ekohl@abo.rhein-zeitung.de>)
32 #ifdef FEATURE_ALIASES
43 typedef struct tagALIAS
45 struct tagALIAS
*next
;
52 static LPALIAS lpFirst
= NULL
;
53 static LPALIAS lpLast
= NULL
;
54 static WORD wUsed
= 0;
57 /* module internal functions */
58 /* strlwr only for first word in string */
60 partstrlwr (LPTSTR str
)
63 while (*c
&& !_istspace (*c
))
74 LPALIAS ptr
= lpFirst
;
77 ConOutPrintf (_T("%s=%s\n"), ptr
->lpName
, ptr
->lpSubst
);
84 DeleteAlias (LPTSTR pszName
)
86 LPALIAS ptr
= lpFirst
;
91 if (!_tcsicmp (ptr
->lpName
, pszName
))
94 prev
->next
= ptr
->next
;
109 AddAlias (LPTSTR name
, LPTSTR subst
)
111 LPALIAS ptr
= lpFirst
;
117 if (!_tcsicmp (ptr
->lpName
, name
))
119 s
= (LPTSTR
)malloc ((_tcslen (subst
) + 1)*sizeof(TCHAR
));
122 error_out_of_memory ();
128 _tcscpy (ptr
->lpSubst
, subst
);
134 ptr
= (LPALIAS
)malloc (sizeof (ALIAS
));
140 ptr
->lpName
= (LPTSTR
)malloc ((_tcslen (name
) + 1)*sizeof(TCHAR
));
143 error_out_of_memory ();
147 _tcscpy (ptr
->lpName
, name
);
149 ptr
->lpSubst
= (LPTSTR
)malloc ((_tcslen (subst
) + 1)*sizeof(TCHAR
));
152 error_out_of_memory ();
157 _tcscpy (ptr
->lpSubst
, subst
);
159 /* it's necessary for recursive substitution */
160 partstrlwr (ptr
->lpSubst
);
164 /* Alias table must be sorted!
165 * Here a little example:
166 * command line = "ls -c"
170 * command line will be expanded to "dir -c" which is not correct.
171 * If the entries are sortet as
174 * it will be expanded to "dir /w" which is a valid DOS command.
180 if (_tcsicmp (ptr
->lpName
, entry
->lpName
) > 0)
198 /* The new entry is the smallest (or the first) and must be
199 * added to the end of the list.
211 /* specified routines */
212 VOID
ExpandAlias (LPTSTR cmd
, INT maxlen
)
219 LPALIAS ptr
= lpFirst
;
230 /* skipping white spaces */
231 while (_istspace (cmd
[n
]))
234 partstrlwr (&cmd
[n
]);
236 if (!_tcsncmp (&cmd
[n
], _T("NOALIAS"), 7) &&
237 (_istspace (cmd
[n
+ 7]) || cmd
[n
+ 7] == _T('\0')))
239 memmove (cmd
, &cmd
[n
+ 7], (_tcslen (&cmd
[n
+ 7]) + 1) * sizeof (TCHAR
));
243 /* substitution loop */
249 len
= _tcslen (ptr
->lpName
);
250 if (!_tcsncmp (&cmd
[n
], ptr
->lpName
, len
) &&
251 (_istspace (cmd
[n
+ len
]) || cmd
[n
+ len
] == _T('\0')) &&
254 m
= _tcslen (ptr
->lpSubst
);
255 if ((int)(_tcslen (cmd
) - len
+ m
- n
) > maxlen
)
257 ConErrPrintf (_T("Command line too long after alias expansion!\n"));
258 /* the parser won't cause any problems with an empty line */
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
];
267 /* whitespaces are removed! */
278 INT
cmd_alias (LPTSTR cmd
, LPTSTR param
)
283 if (!_tcsncmp (param
, _T("/?"), 2))
285 ConOutPuts (_T("Sets, removes or shows aliases.\n"
287 "ALIAS [alias=[command]]\n"
289 " alias Name for an alias.\n"
290 " command Text to be substituted for an alias.\n"
293 "To list all aliases:\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"
299 // "Type ALIAS without a parameter to display the alias list.\n"
304 if (param
[0] == _T('\0'))
310 /* error if no '=' found */
311 if ((ptr
= _tcschr (param
, _T('='))) == 0)
314 /* Split rest into name and substitute */
319 if (ptr
[0] == _T('\0'))
322 n
= AddAlias (param
, ptr
);