static VOID PrintAlias (VOID) { LPTSTR Aliases; LPTSTR ptr; DWORD len; len = GetConsoleAliasesLength(_T("cmd.exe")); if (len == 0) return; /* allocate memory for an extra \0 char to make parsing easier */ ptr = cmd_alloc(len + sizeof(TCHAR)); if (!ptr) { WARN("Cannot allocate memory for ptr!\n"); return; } Aliases = ptr; ZeroMemory(Aliases, len + sizeof(TCHAR)); if (GetConsoleAliases(Aliases, len, _T("cmd.exe")) != 0) { while (*Aliases != '\0') { ConOutPrintf(_T("%s\n"), Aliases); Aliases = Aliases + lstrlen(Aliases); Aliases++; } } cmd_free(ptr); }
//------------------------------------------------------------------------------ static int get_console_aliases(lua_State* state) { char* buffer = NULL; do { int i; int buffer_size; char* alias; lua_createtable(state, 0, 0); #if !defined(__MINGW32__) && !defined(__MINGW64__) // Get the aliases (aka. doskey macros). buffer_size = GetConsoleAliasesLength((char*)rl_readline_name); if (buffer_size == 0) { break; } buffer = malloc(buffer_size + 1); if (GetConsoleAliases(buffer, buffer_size, rl_readline_name) == 0) { break; } buffer[buffer_size] = '\0'; // Parse the result into a lua table. alias = buffer; i = 1; while (*alias != '\0') { char* c = strchr(alias, '='); if (c == NULL) { break; } *c = '\0'; lua_pushstring(state, alias); lua_rawseti(state, -2, i++); ++c; alias = c + strlen(c) + 1; } #endif // !__MINGW32__ && !__MINGW64__ } while (0); free(buffer); return 1; }
bool GetAliases(wchar_t* asExeName, wchar_t** rsAliases, LPDWORD rnAliasesSize) { bool lbRc = false; DWORD nAliasRC, nAliasErr, nAliasAErr = 0, nSizeA = 0; _ASSERTE(asExeName && rsAliases && rnAliasesSize); _ASSERTE(*rsAliases == NULL); *rnAliasesSize = GetConsoleAliasesLength(asExeName); if (*rnAliasesSize == 0) { lbRc = true; } else { *rsAliases = (wchar_t*)calloc(*rnAliasesSize+2,1); nAliasRC = GetConsoleAliases(*rsAliases,*rnAliasesSize,asExeName); if (nAliasRC) { lbRc = true; } else { nAliasErr = GetLastError(); if (nAliasErr == ERROR_NOT_ENOUGH_MEMORY) { // Попробовать ANSI функции UINT nCP = CP_OEMCP; char szExeName[MAX_PATH+1]; char *pszAliases = NULL; WideCharToMultiByte(nCP,0,asExeName,-1,szExeName,MAX_PATH+1,0,0); nSizeA = GetConsoleAliasesLengthA(szExeName); if (nSizeA) { pszAliases = (char*)calloc(nSizeA+1,1); nAliasRC = GetConsoleAliasesA(pszAliases,nSizeA,szExeName); if (nAliasRC) { lbRc = true; MultiByteToWideChar(nCP,0,pszAliases,nSizeA,*rsAliases,((*rnAliasesSize)/2)+1); } else { nAliasAErr = GetLastError(); } free(pszAliases); } } if (!nAliasRC) { if ((*rnAliasesSize) < 255) { free(*rsAliases); *rsAliases = (wchar_t*)calloc(128,2); } _wsprintf(*rsAliases, SKIPLEN(127) L"\nConEmuC: GetConsoleAliases failed, ErrCode=0x%08X(0x%08X), AliasesLength=%i(%i)\n\n", nAliasErr, nAliasAErr, *rnAliasesSize, nSizeA); } } } return lbRc; }
DWORD DisplayAliases( char *ExeName ) { DWORD cb, rc, nExeNames, nAliases, iExeName, iAlias; LPSTR FreeMem1, FreeMem2, AliasName, AliasValue, s, *SortedExeNames, *SortedAliasNames; if (ExeName == NULL) { cb = GetConsoleAliasExesLength(); if (cb == 0) { return ERROR_ENVVAR_NOT_FOUND; } if (!(FreeMem1 = malloc( cb+2 ))) { fprintf( stderr, "ALIAS: Not enough memory for EXE names.\n" ); return ERROR_NOT_ENOUGH_MEMORY; } ExeName = FreeMem1; if (!GetConsoleAliasExes( ExeName, cb )) { fprintf( stderr, "ALIAS: Unable to read alias EXE names.\n" ); return ERROR_ENVVAR_NOT_FOUND; } ExeName[ cb ] = '\0'; ExeName[ cb+1 ] = '\0'; nExeNames = 0; s = ExeName; while (*s) { _strupr( s ); nExeNames++; while (*s++) { } } SortedExeNames = malloc( nExeNames * sizeof( LPSTR ) ); if (SortedExeNames == NULL) { fprintf( stderr, "ALIAS: Not enough memory to sort .EXE names.\n" ); } else { iExeName = 0; s = ExeName; while (*s) { SortedExeNames[ iExeName++ ] = s; while (*s++) { } } qsort( SortedExeNames, nExeNames, sizeof( LPSTR ), CmpNamesRoutine ); iExeName = 0; } } else { SortedExeNames = NULL; FreeMem1 = NULL; } rc = NO_ERROR; while (rc == NO_ERROR && *ExeName) { if (SortedExeNames != NULL) { ExeName = SortedExeNames[ iExeName++ ]; } cb = GetConsoleAliasesLength(ExeName); if (cb == 0) { printf( "No aliases defined for %s in current console.\n", ExeName ); } else { if (!(FreeMem2 = malloc( cb+2 ))) { fprintf( stderr, "ALIAS: Not enough memory for alias names.\n" ); rc = ERROR_NOT_ENOUGH_MEMORY; break; } SortedAliasNames = NULL; AliasName = FreeMem2; if (GetConsoleAliases( AliasName, cb, ExeName )) { AliasName[ cb ] = '\0'; AliasName[ cb+1 ] = '\0'; nAliases = 0; s = AliasName; while (*s) { nAliases++; while (*s++) { } } SortedAliasNames = malloc( nAliases * sizeof( LPSTR ) ); if (SortedAliasNames == NULL) { fprintf( stderr, "ALIAS: Not enough memory to sort alias names.\n" ); } else { iAlias = 0; s = AliasName; while (*s) { SortedAliasNames[ iAlias++ ] = s; while (*s++) { } } qsort( SortedAliasNames, nAliases, sizeof( LPSTR ), CmpNamesRoutine ); iAlias = 0; } printf( "Dumping all defined aliases for %s.\n", ExeName ); while (*AliasName) { if (SortedAliasNames != NULL) { AliasName = SortedAliasNames[ iAlias++ ]; } AliasValue = AliasName; while (*AliasValue) { if (*AliasValue == '=') { *AliasValue++ = '\0'; break; } else { AliasValue++; } } printf( " %-16s=%s\n", AliasName, AliasValue ); if (SortedAliasNames != NULL) { if (iAlias < nAliases) { AliasName = " "; } else { AliasName = ""; } } else { AliasName = AliasValue; while (*AliasName++) { ; } } } } else { fprintf( stderr, "ALIAS: unable to read aliases for %s.\n", ExeName ); rc = ERROR_ENVVAR_NOT_FOUND; } free( FreeMem2 ); if (SortedAliasNames != NULL) { free( SortedAliasNames ); } } if (SortedExeNames != NULL) { if (iExeName < nExeNames) { ExeName = " "; } else { ExeName = ""; } } else { while (*ExeName++) { ; } } } if (SortedExeNames != NULL) { free( SortedExeNames ); } if (FreeMem1) { free( FreeMem1 ); } return rc; }