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 DWORD dwUsed
= 0;
57 /* module internal functions */
58 /* strlwr only for first word in string */
60 partstrlwr (LPTSTR str
)
63 while (*c
&& !_istspace (*c
) && *c
!= _T('='))
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 VOID
InitializeAlias (VOID
)
218 VOID
DestroyAlias (VOID
)
223 while (lpFirst
->next
!= NULL
)
226 lpFirst
= lpLast
->next
;
228 free (lpLast
->lpName
);
229 free (lpLast
->lpSubst
);
233 free (lpFirst
->lpName
);
234 free (lpFirst
->lpSubst
);
242 /* specified routines */
243 VOID
ExpandAlias (LPTSTR cmd
, INT maxlen
)
250 LPALIAS ptr
= lpFirst
;
261 /* skipping white spaces */
262 while (_istspace (cmd
[n
]))
265 partstrlwr (&cmd
[n
]);
267 if (!_tcsncmp (&cmd
[n
], _T("NOALIAS"), 7) &&
268 (_istspace (cmd
[n
+ 7]) || cmd
[n
+ 7] == _T('\0')))
270 memmove (cmd
, &cmd
[n
+ 7], (_tcslen (&cmd
[n
+ 7]) + 1) * sizeof (TCHAR
));
274 /* substitution loop */
280 len
= _tcslen (ptr
->lpName
);
281 if (!_tcsncmp (&cmd
[n
], ptr
->lpName
, len
) &&
282 (_istspace (cmd
[n
+ len
]) || cmd
[n
+ len
] == _T('\0')) &&
283 ptr
->dwUsed
!= dwUsed
)
285 m
= _tcslen (ptr
->lpSubst
);
286 if ((int)(_tcslen (cmd
) - len
+ m
- n
) > maxlen
)
288 ConErrPrintf (_T("Command line too long after alias expansion!\n"));
289 /* the parser won't cause any problems with an empty line */
294 memmove (&cmd
[m
], &cmd
[n
+ len
], (_tcslen(&cmd
[n
+ len
]) + 1) * sizeof (TCHAR
));
295 for (i
= 0; i
< m
; i
++)
296 cmd
[i
] = ptr
->lpSubst
[i
];
297 ptr
->dwUsed
= dwUsed
;
298 /* whitespaces are removed! */
309 INT
CommandAlias (LPTSTR cmd
, LPTSTR param
)
313 if (!_tcsncmp (param
, _T("/?"), 2))
315 ConOutPuts (_T("Sets, removes or shows aliases.\n"
317 "ALIAS [alias=[command]]\n"
319 " alias Name for an alias.\n"
320 " command Text to be substituted for an alias.\n"
323 "To list all aliases:\n"
325 "To set a new or replace an existing alias:\n"
326 " ALIAS da=dir a:\n\n"
327 "To remove an alias from the alias list:\n"
329 // "Type ALIAS without a parameter to display the alias list.\n"
334 if (param
[0] == _T('\0'))
340 /* error if no '=' found */
341 if ((ptr
= _tcschr (param
, _T('='))) == 0)
344 /* Split rest into name and substitute */
349 if (ptr
[0] == _T('\0'))
352 AddAlias (param
, ptr
);