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>)
28 * 02-Apr-2005 (Magnus Olsen) <magnus@greatlord.com>)
29 * Remove all hardcode string to En.rc
36 #ifdef FEATURE_ALIASES
38 typedef struct tagALIAS
40 struct tagALIAS
*next
;
47 static LPALIAS lpFirst
= NULL
;
48 static LPALIAS lpLast
= NULL
;
49 static DWORD dwUsed
= 0;
52 /* module internal functions */
53 /* strlwr only for first word in string */
55 partstrlwr (LPTSTR str
)
58 while (*c
&& !_istspace (*c
) && *c
!= _T('='))
69 LPALIAS ptr
= lpFirst
;
72 ConOutPrintf (_T("%s=%s\n"), ptr
->lpName
, ptr
->lpSubst
);
79 DeleteAlias (LPTSTR pszName
)
81 LPALIAS ptr
= lpFirst
;
86 if (!_tcsicmp (ptr
->lpName
, pszName
))
89 prev
->next
= ptr
->next
;
104 AddAlias (LPTSTR name
, LPTSTR subst
)
106 LPALIAS ptr
= lpFirst
;
112 if (!_tcsicmp (ptr
->lpName
, name
))
114 s
= (LPTSTR
)malloc ((_tcslen (subst
) + 1)*sizeof(TCHAR
));
117 error_out_of_memory ();
123 _tcscpy (ptr
->lpSubst
, subst
);
129 ptr
= (LPALIAS
)malloc (sizeof (ALIAS
));
135 ptr
->lpName
= (LPTSTR
)malloc ((_tcslen (name
) + 1)*sizeof(TCHAR
));
138 error_out_of_memory ();
142 _tcscpy (ptr
->lpName
, name
);
144 ptr
->lpSubst
= (LPTSTR
)malloc ((_tcslen (subst
) + 1)*sizeof(TCHAR
));
147 error_out_of_memory ();
152 _tcscpy (ptr
->lpSubst
, subst
);
154 /* it's necessary for recursive substitution */
155 partstrlwr (ptr
->lpSubst
);
159 /* Alias table must be sorted!
160 * Here a little example:
161 * command line = "ls -c"
165 * command line will be expanded to "dir -c" which is not correct.
166 * If the entries are sortet as
169 * it will be expanded to "dir /w" which is a valid DOS command.
175 if (_tcsicmp (ptr
->lpName
, entry
->lpName
) > 0)
193 /* The new entry is the smallest (or the first) and must be
194 * added to the end of the list.
206 VOID
InitializeAlias (VOID
)
213 VOID
DestroyAlias (VOID
)
218 while (lpFirst
->next
!= NULL
)
221 lpFirst
= lpLast
->next
;
223 free (lpLast
->lpName
);
224 free (lpLast
->lpSubst
);
228 free (lpFirst
->lpName
);
229 free (lpFirst
->lpSubst
);
237 /* specified routines */
238 VOID
ExpandAlias (LPTSTR cmd
, INT maxlen
)
240 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
246 LPALIAS ptr
= lpFirst
;
257 /* skipping white spaces */
258 while (_istspace (cmd
[n
]))
261 partstrlwr (&cmd
[n
]);
263 if (!_tcsncmp (&cmd
[n
], _T("NOALIAS"), 7) &&
264 (_istspace (cmd
[n
+ 7]) || cmd
[n
+ 7] == _T('\0')))
266 memmove (cmd
, &cmd
[n
+ 7], (_tcslen (&cmd
[n
+ 7]) + 1) * sizeof (TCHAR
));
270 /* substitution loop */
276 len
= _tcslen (ptr
->lpName
);
277 if (!_tcsncmp (&cmd
[n
], ptr
->lpName
, len
) &&
278 (_istspace (cmd
[n
+ len
]) || cmd
[n
+ len
] == _T('\0')) &&
279 ptr
->dwUsed
!= dwUsed
)
281 m
= _tcslen (ptr
->lpSubst
);
282 if ((int)(_tcslen (cmd
) - len
+ m
- n
) > maxlen
)
284 LoadString(GetModuleHandle(NULL
), STRING_ALIAS_ERROR
, szMsg
, RC_STRING_MAX_SIZE
);
287 /* the parser won't cause any problems with an empty line */
292 memmove (&cmd
[m
], &cmd
[n
+ len
], (_tcslen(&cmd
[n
+ len
]) + 1) * sizeof (TCHAR
));
293 for (i
= 0; i
< m
; i
++)
294 cmd
[i
] = ptr
->lpSubst
[i
];
295 ptr
->dwUsed
= dwUsed
;
296 /* whitespaces are removed! */
307 INT
CommandAlias (LPTSTR cmd
, LPTSTR param
)
309 TCHAR szMsg
[RC_STRING_MAX_SIZE
];
312 if (!_tcsncmp (param
, _T("/?"), 2))
314 LoadString(GetModuleHandle(NULL
), STRING_ALIAS_HELP
, szMsg
, RC_STRING_MAX_SIZE
);
319 if (param
[0] == _T('\0'))
325 /* error if no '=' found */
326 if ((ptr
= _tcschr (param
, _T('='))) == 0)
329 /* Split rest into name and substitute */
334 if (ptr
[0] == _T('\0'))
337 AddAlias (param
, ptr
);