[MODE] Improve the way the DOS device statuses are enumerated via the "mode" command (without parameters).

This commit is contained in:
Hermès Bélusca-Maïto 2018-04-29 19:33:49 +02:00
parent 37deb7593e
commit 3055afce81
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
8 changed files with 155 additions and 138 deletions

View file

@ -59,6 +59,7 @@ BEGIN
IDS_ERROR_SET_PARALLEL_STATE "ERROR: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n" IDS_ERROR_SET_PARALLEL_STATE "ERROR: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n"
IDS_ERROR_INVALID_PARAMETER "Unzulässiger Parameter - %s\n" IDS_ERROR_INVALID_PARAMETER "Unzulässiger Parameter - %s\n"
IDS_ERROR_ILLEGAL_DEVICE_NAME "Unzulässiger Gerätename - %s (0x%lx)\n" IDS_ERROR_ILLEGAL_DEVICE_NAME "Unzulässiger Gerätename - %s (0x%lx)\n"
IDS_ERROR_DEVICE_NOT_AVAILABLE "The device %s is not currently available.\n"
IDS_ERROR_STATUS_GET_DEVICE "Failed to get the status for device %s:\n" IDS_ERROR_STATUS_GET_DEVICE "Failed to get the status for device %s:\n"
IDS_ERROR_STATUS_SET_DEVICE "Failed to set the status for device %s:\n" IDS_ERROR_STATUS_SET_DEVICE "Failed to set the status for device %s:\n"
IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n" IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n"

View file

@ -59,6 +59,7 @@ BEGIN
IDS_ERROR_SET_PARALLEL_STATE "ERROR: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n" IDS_ERROR_SET_PARALLEL_STATE "ERROR: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n"
IDS_ERROR_INVALID_PARAMETER "Invalid parameter - %s\n" IDS_ERROR_INVALID_PARAMETER "Invalid parameter - %s\n"
IDS_ERROR_ILLEGAL_DEVICE_NAME "Illegal device name - %s (0x%lx)\n" IDS_ERROR_ILLEGAL_DEVICE_NAME "Illegal device name - %s (0x%lx)\n"
IDS_ERROR_DEVICE_NOT_AVAILABLE "The device %s is not currently available.\n"
IDS_ERROR_STATUS_GET_DEVICE "Failed to get the status for device %s:\n" IDS_ERROR_STATUS_GET_DEVICE "Failed to get the status for device %s:\n"
IDS_ERROR_STATUS_SET_DEVICE "Failed to set the status for device %s:\n" IDS_ERROR_STATUS_SET_DEVICE "Failed to set the status for device %s:\n"
IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n" IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n"

View file

@ -67,6 +67,7 @@ BEGIN
IDS_ERROR_SET_PARALLEL_STATE "EROARE: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n" IDS_ERROR_SET_PARALLEL_STATE "EROARE: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n"
IDS_ERROR_INVALID_PARAMETER "Parametru nevalid - %s\n" IDS_ERROR_INVALID_PARAMETER "Parametru nevalid - %s\n"
IDS_ERROR_ILLEGAL_DEVICE_NAME "Nume eronat de dispozitiv - %s (0x%lx)\n" IDS_ERROR_ILLEGAL_DEVICE_NAME "Nume eronat de dispozitiv - %s (0x%lx)\n"
IDS_ERROR_DEVICE_NOT_AVAILABLE "The device %s is not currently available.\n"
IDS_ERROR_STATUS_GET_DEVICE "Nu poate fi determinată starea dispozitivului %s:\n" IDS_ERROR_STATUS_GET_DEVICE "Nu poate fi determinată starea dispozitivului %s:\n"
IDS_ERROR_STATUS_SET_DEVICE "Nu poate fi schimbată starea pentru dispozitivul %s:\n" IDS_ERROR_STATUS_SET_DEVICE "Nu poate fi schimbată starea pentru dispozitivul %s:\n"
IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n" IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n"

View file

@ -59,6 +59,7 @@ BEGIN
IDS_ERROR_SET_PARALLEL_STATE "ОШИБКА: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n" IDS_ERROR_SET_PARALLEL_STATE "ОШИБКА: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n"
IDS_ERROR_INVALID_PARAMETER "Неверный параметр - %s\n" IDS_ERROR_INVALID_PARAMETER "Неверный параметр - %s\n"
IDS_ERROR_ILLEGAL_DEVICE_NAME "Недопустимое имя устройства - %s (0x%lx)\n" IDS_ERROR_ILLEGAL_DEVICE_NAME "Недопустимое имя устройства - %s (0x%lx)\n"
IDS_ERROR_DEVICE_NOT_AVAILABLE "The device %s is not currently available.\n"
IDS_ERROR_STATUS_GET_DEVICE "Не удалось получить состояние устройства %s:\n" IDS_ERROR_STATUS_GET_DEVICE "Не удалось получить состояние устройства %s:\n"
IDS_ERROR_STATUS_SET_DEVICE "Не удалось установить состояние устройства %s:\n" IDS_ERROR_STATUS_SET_DEVICE "Не удалось установить состояние устройства %s:\n"
IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n" IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n"

View file

@ -61,6 +61,7 @@ BEGIN
IDS_ERROR_SET_PARALLEL_STATE "YANLIŞLIK: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n" IDS_ERROR_SET_PARALLEL_STATE "YANLIŞLIK: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n"
IDS_ERROR_INVALID_PARAMETER "Geçersiz değişken - %s\n" IDS_ERROR_INVALID_PARAMETER "Geçersiz değişken - %s\n"
IDS_ERROR_ILLEGAL_DEVICE_NAME "Geçersiz aygıt adı - %s (0x%lx)\n" IDS_ERROR_ILLEGAL_DEVICE_NAME "Geçersiz aygıt adı - %s (0x%lx)\n"
IDS_ERROR_DEVICE_NOT_AVAILABLE "The device %s is not currently available.\n"
IDS_ERROR_STATUS_GET_DEVICE "%s: aygıtının durumu alınamıyor\n" IDS_ERROR_STATUS_GET_DEVICE "%s: aygıtının durumu alınamıyor\n"
IDS_ERROR_STATUS_SET_DEVICE "%s: aygıtının durumu ayarlanamıyor\n" IDS_ERROR_STATUS_SET_DEVICE "%s: aygıtının durumu ayarlanamıyor\n"
IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n" IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n"

View file

@ -59,6 +59,7 @@ BEGIN
IDS_ERROR_SET_PARALLEL_STATE "错误: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n" IDS_ERROR_SET_PARALLEL_STATE "错误: SetParallelState(%d) - DefineDosDevice(%s) (0x%lx)\n"
IDS_ERROR_INVALID_PARAMETER "无效参数 - %s\n" IDS_ERROR_INVALID_PARAMETER "无效参数 - %s\n"
IDS_ERROR_ILLEGAL_DEVICE_NAME "非法设备名 - %s (0x%lx)\n" IDS_ERROR_ILLEGAL_DEVICE_NAME "非法设备名 - %s (0x%lx)\n"
IDS_ERROR_DEVICE_NOT_AVAILABLE "The device %s is not currently available.\n"
IDS_ERROR_STATUS_GET_DEVICE "无法获得设备 %s: 的状态\n" IDS_ERROR_STATUS_GET_DEVICE "无法获得设备 %s: 的状态\n"
IDS_ERROR_STATUS_SET_DEVICE "无法设置设备 %s: 的状态\n" IDS_ERROR_STATUS_SET_DEVICE "无法设置设备 %s: 的状态\n"
IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n" IDS_ERROR_TIMEOUT_GET_DEVICE "Failed to get the timeout for device %s:\n"

View file

@ -29,6 +29,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <windef.h> #include <windef.h>
#include <winbase.h> #include <winbase.h>
@ -40,7 +41,6 @@
#include "resource.h" #include "resource.h"
#define MAX_PORTNAME_LEN 20 #define MAX_PORTNAME_LEN 20
#define MAX_COMPORT_NUM 10
#define ASSERT(a) #define ASSERT(a)
@ -72,76 +72,6 @@ UnderlinedResPrintf(
ConStreamWrite(Stream, szMsgBuffer, Len); ConStreamWrite(Stream, szMsgBuffer, Len);
} }
int QueryDevices(VOID)
{
PWSTR Buffer, ptr;
DWORD dwLen = MAX_PATH;
/* Pre-allocate a buffer for QueryDosDeviceW() */
Buffer = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
if (Buffer == NULL)
{
/* We failed, bail out */
ConPuts(StdErr, L"ERROR: Not enough memory\n");
return 0;
}
for (;;)
{
*Buffer = UNICODE_NULL;
if (QueryDosDeviceW(NULL, Buffer, dwLen))
break;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
/* We failed, bail out */
ConPrintf(StdErr, L"ERROR: QueryDosDeviceW(...) failed: 0x%lx\n", GetLastError());
HeapFree(GetProcessHeap(), 0, Buffer);
return 0;
}
/* The buffer was too small, try to re-allocate it */
dwLen *= 2;
ptr = HeapReAlloc(GetProcessHeap(), 0, Buffer, dwLen * sizeof(WCHAR));
if (ptr == NULL)
{
/* We failed, bail out */
ConPuts(StdErr, L"ERROR: Not enough memory\n");
HeapFree(GetProcessHeap(), 0, Buffer);
return 0;
}
Buffer = ptr;
}
for (ptr = Buffer; *ptr != UNICODE_NULL; ptr += wcslen(ptr) + 1)
{
if (wcsstr(ptr, L"COM"))
{
ConResPrintf(StdOut, IDS_QUERY_SERIAL_FOUND, ptr);
}
else if (wcsstr(ptr, L"PRN"))
{
ConResPrintf(StdOut, IDS_QUERY_PRINTER_FOUND, ptr);
}
else if (wcsstr(ptr, L"LPT"))
{
ConResPrintf(StdOut, IDS_QUERY_PARALLEL_FOUND, ptr);
}
else if (wcsstr(ptr, L"AUX") || wcsstr(ptr, L"NUL"))
{
ConResPrintf(StdOut, IDS_QUERY_DOSDEV_FOUND, ptr);
}
else
{
// ConResPrintf(StdOut, IDS_QUERY_MISC_FOUND, ptr);
}
}
/* Free the buffer and return success */
HeapFree(GetProcessHeap(), 0, Buffer);
return 1;
}
int ShowParallelStatus(INT nPortNum) int ShowParallelStatus(INT nPortNum)
{ {
WCHAR buffer[250]; WCHAR buffer[250];
@ -174,6 +104,7 @@ int ShowParallelStatus(INT nPortNum)
{ {
ConPrintf(StdErr, L"ERROR: QueryDosDeviceW(%s) failed: 0x%lx\n", szPortName, GetLastError()); ConPrintf(StdErr, L"ERROR: QueryDosDeviceW(%s) failed: 0x%lx\n", szPortName, GetLastError());
} }
ConPuts(StdOut, L"\n");
return 1; return 1;
} }
@ -251,6 +182,8 @@ int ShowConsoleStatus(VOID)
ConResPrintf(StdOut, IDS_CONSOLE_KBD_DELAY, dwKbdDelay); ConResPrintf(StdOut, IDS_CONSOLE_KBD_DELAY, dwKbdDelay);
} }
ConResPrintf(StdOut, IDS_CONSOLE_CODEPAGE, GetConsoleOutputCP()); ConResPrintf(StdOut, IDS_CONSOLE_CODEPAGE, GetConsoleOutputCP());
ConPuts(StdOut, L"\n");
return 0; return 0;
} }
@ -261,6 +194,8 @@ int ShowConsoleCPStatus(VOID)
ConPuts(StdOut, L"\n"); ConPuts(StdOut, L"\n");
ConResPrintf(StdOut, IDS_CONSOLE_CODEPAGE, GetConsoleOutputCP()); ConResPrintf(StdOut, IDS_CONSOLE_CODEPAGE, GetConsoleOutputCP());
ConPuts(StdOut, L"\n");
return 0; return 0;
} }
@ -364,6 +299,7 @@ ResizeTextConsole(
/* Update the console screen buffer information */ /* Update the console screen buffer information */
GetConsoleScreenBufferInfo(hConOut, pcsbi); GetConsoleScreenBufferInfo(hConOut, pcsbi);
return TRUE; return TRUE;
} }
@ -570,7 +506,11 @@ SerialPortQuery(INT nPortNum, LPDCB pDCB, LPCOMMTIMEOUTS pCommTimeouts, BOOL bWr
if (hPort == INVALID_HANDLE_VALUE) if (hPort == INVALID_HANDLE_VALUE)
{ {
ConResPrintf(StdErr, IDS_ERROR_ILLEGAL_DEVICE_NAME, szPortName, GetLastError()); DWORD dwLastError = GetLastError();
if (dwLastError == ERROR_ACCESS_DENIED)
ConResPrintf(StdErr, IDS_ERROR_DEVICE_NOT_AVAILABLE, szPortName);
else
ConResPrintf(StdErr, IDS_ERROR_ILLEGAL_DEVICE_NAME, szPortName, dwLastError);
return FALSE; return FALSE;
} }
@ -654,6 +594,8 @@ int ShowSerialStatus(INT nPortNum)
control_strings[dcb.fDsrSensitivity ? 1 : 0]); control_strings[dcb.fDsrSensitivity ? 1 : 0]);
ConResPrintf(StdOut, IDS_COM_STATUS_DTR_CIRCUIT, control_strings[dcb.fDtrControl]); ConResPrintf(StdOut, IDS_COM_STATUS_DTR_CIRCUIT, control_strings[dcb.fDtrControl]);
ConResPrintf(StdOut, IDS_COM_STATUS_RTS_CIRCUIT, control_strings[dcb.fRtsControl]); ConResPrintf(StdOut, IDS_COM_STATUS_RTS_CIRCUIT, control_strings[dcb.fRtsControl]);
ConPuts(StdOut, L"\n");
return 0; return 0;
} }
@ -1108,24 +1050,100 @@ int SetSerialState(INT nPortNum, IN PCWSTR ArgStr)
static PCWSTR static PCWSTR
FindPortNum(PCWSTR argStr, PINT PortNum) FindPortNum(PCWSTR argStr, PINT PortNum)
{ {
*PortNum = -1; PWSTR endptr = NULL;
if (*argStr >= L'0' && *argStr <= L'9') *PortNum = wcstol(argStr, &endptr, 10);
{ if (endptr == argStr)
*PortNum = *argStr - L'0';
argStr++;
if (*argStr >= L'0' && *argStr <= L'9')
{
*PortNum *= 10;
*PortNum += *argStr - L'0';
}
}
else
{ {
*PortNum = -1;
return NULL; return NULL;
} }
return argStr; return endptr;
}
int EnumerateDevices(VOID)
{
PWSTR Buffer, ptr;
PCWSTR argStr;
DWORD dwLen = MAX_PATH;
INT nPortNum;
/* Pre-allocate a buffer for QueryDosDeviceW() */
Buffer = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
if (Buffer == NULL)
{
/* We failed, bail out */
ConPuts(StdErr, L"ERROR: Not enough memory\n");
return 0;
}
for (;;)
{
*Buffer = UNICODE_NULL;
if (QueryDosDeviceW(NULL, Buffer, dwLen))
break;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
/* We failed, bail out */
ConPrintf(StdErr, L"ERROR: QueryDosDeviceW(...) failed: 0x%lx\n", GetLastError());
HeapFree(GetProcessHeap(), 0, Buffer);
return 0;
}
/* The buffer was too small, try to re-allocate it */
dwLen *= 2;
ptr = HeapReAlloc(GetProcessHeap(), 0, Buffer, dwLen * sizeof(WCHAR));
if (ptr == NULL)
{
/* We failed, bail out */
ConPuts(StdErr, L"ERROR: Not enough memory\n");
HeapFree(GetProcessHeap(), 0, Buffer);
return 0;
}
Buffer = ptr;
}
for (ptr = Buffer; *ptr != UNICODE_NULL; ptr += wcslen(ptr) + 1)
{
if (_wcsnicmp(ptr, L"COM", 3) == 0)
{
argStr = FindPortNum(ptr+3, &nPortNum);
if (!argStr || *argStr || nPortNum == -1)
continue;
// ConResPrintf(StdOut, IDS_QUERY_SERIAL_FOUND, ptr);
ShowSerialStatus(nPortNum);
}
else if (_wcsicmp(ptr, L"PRN") == 0)
{
ConResPrintf(StdOut, IDS_QUERY_PRINTER_FOUND, ptr);
}
else if (_wcsnicmp(ptr, L"LPT", 3) == 0)
{
argStr = FindPortNum(ptr+3, &nPortNum);
if (!argStr || *argStr || nPortNum == -1)
continue;
// ConResPrintf(StdOut, IDS_QUERY_PARALLEL_FOUND, ptr);
ShowParallelStatus(nPortNum);
}
else if (_wcsicmp(ptr, L"AUX") == 0 || _wcsicmp(ptr, L"NUL") == 0)
{
ConResPrintf(StdOut, IDS_QUERY_DOSDEV_FOUND, ptr);
}
else
{
// ConResPrintf(StdOut, IDS_QUERY_MISC_FOUND, ptr);
}
}
ShowConsoleStatus();
/* Free the buffer and return success */
HeapFree(GetProcessHeap(), 0, Buffer);
return 1;
} }
int wmain(int argc, WCHAR* argv[]) int wmain(int argc, WCHAR* argv[])
@ -1273,15 +1291,7 @@ int wmain(int argc, WCHAR* argv[])
} }
show_status: show_status:
QueryDevices(); EnumerateDevices();
/*
ShowParallelStatus(1);
for (nPortNum = 0; nPortNum < MAX_COMPORT_NUM; nPortNum++)
{
ShowSerialStatus(nPortNum + 1);
}
ShowConsoleStatus();
*/
goto Quit; goto Quit;
invalid_parameter: invalid_parameter:

View file

@ -38,13 +38,14 @@
#define IDS_ERROR_SET_PARALLEL_STATE 26 #define IDS_ERROR_SET_PARALLEL_STATE 26
#define IDS_ERROR_INVALID_PARAMETER 27 #define IDS_ERROR_INVALID_PARAMETER 27
#define IDS_ERROR_ILLEGAL_DEVICE_NAME 28 #define IDS_ERROR_ILLEGAL_DEVICE_NAME 28
#define IDS_ERROR_STATUS_GET_DEVICE 29 #define IDS_ERROR_DEVICE_NOT_AVAILABLE 29
#define IDS_ERROR_STATUS_SET_DEVICE 30 #define IDS_ERROR_STATUS_GET_DEVICE 30
#define IDS_ERROR_TIMEOUT_GET_DEVICE 31 #define IDS_ERROR_STATUS_SET_DEVICE 31
#define IDS_ERROR_TIMEOUT_SET_DEVICE 32 #define IDS_ERROR_TIMEOUT_GET_DEVICE 32
#define IDS_ERROR_INVALID_PARITY_BITS 33 #define IDS_ERROR_TIMEOUT_SET_DEVICE 33
#define IDS_ERROR_INVALID_STOP_BITS 34 #define IDS_ERROR_INVALID_PARITY_BITS 34
#define IDS_ERROR_NO_MEMORY 35 #define IDS_ERROR_INVALID_STOP_BITS 35
#define IDS_ERROR_SCREEN_LINES_COL 36 #define IDS_ERROR_NO_MEMORY 36
#define IDS_ERROR_SCREEN_LINES_COL 37
#endif /* RESOURCE_H */ #endif /* RESOURCE_H */