DWORD DisplayAlias( LPSTR AliasName, LPSTR ExeName ) { DWORD cb; CHAR AliasBuffer[512]; if (cb = GetConsoleAlias( AliasName, AliasBuffer, sizeof( AliasBuffer ), ExeName )) { printf( "%-16s=%s\n", AliasName, AliasBuffer ); return NO_ERROR; } else { printf( "%-16s *** Unable to read value of alias ***\n", AliasName ); return ERROR_ENVVAR_NOT_FOUND; } }
/* specified routines */ VOID ExpandAlias (LPTSTR cmd, INT maxlen) { LPTSTR buffer; TCHAR *position, *in, *out; LPTSTR Token; LPTSTR tmp; tmp = cmd_dup(cmd); if (!tmp) return; /* first part is the macro name */ position = tmp + _tcscspn(tmp, _T(" \n")); if (position == tmp) { cmd_free(tmp); return; } *position++ = _T('\0'); position += _tcsspn(position, _T(" ")); buffer = cmd_alloc(maxlen); if (!buffer) { WARN("Cannot allocate memory for alias buffer!\n"); cmd_free(tmp); return; } if (GetConsoleAlias(tmp, buffer, maxlen, _T("cmd.exe")) == 0) { cmd_free(tmp); cmd_free(buffer); return; } in = buffer; out = cmd; while (*in) { if (*in == _T('$')) { Token = position; if (in[1] >= _T('1') && in[1] <= _T('9')) { /* Copy a single space-delimited token from the input line */ INT num; for (num = in[1] - _T('1'); num > 0; num--) { Token += _tcscspn(Token, _T(" \n")); Token += _tcsspn(Token, _T(" ")); } while (!_tcschr(_T(" \n"), *Token)) { if (out >= &cmd[maxlen - 1]) break; *out++ = *Token++; } in += 2; continue; } else if (in[1] == _T('*')) { /* Copy the entire remainder of the line */ while (*Token && *Token != _T('\n')) { if (out >= &cmd[maxlen - 1]) break; *out++ = *Token++; } in += 2; continue; } } if (out >= &cmd[maxlen - 1]) break; *out++ = *in++; } *out++ = _T('\n'); *out = _T('\0'); cmd_free(buffer); cmd_free(tmp); }