Hervé Poussineau <hpoussineau@fr.st>

Add localization to ctm

svn path=/trunk/; revision=13057
This commit is contained in:
Gé van Geldorp 2005-01-15 15:28:51 +00:00
parent 274efb4364
commit e3ea6fc396
5 changed files with 279 additions and 113 deletions

View file

@ -0,0 +1,31 @@
// English (U.S.) resources
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
STRINGTABLE DISCARDABLE
BEGIN
IDS_APP_TITLE "Console TaskManager v0.1 by Aleksey Bragin <aleksey@studiocerebral.com>"
IDS_COLUMN_IMAGENAME "Image name "
IDS_COLUMN_PID "PID "
IDS_COLUMN_CPU "CPU"
IDS_COLUMN_MEM "Mem Usage"
IDS_COLUMN_MEM_UNIT "Kb"
IDS_COLUMN_PF "Page Faults"
IDS_IDLE_PROCESS "System Idle Process"
IDS_YES "y"
IDS_NO "n"
IDS_MENU "Press: q - quit, k - kill process"
IDS_MENU_QUIT "q"
IDS_MENU_KILL_PROCESS "k"
IDS_KILL_PROCESS "Are you sure you want to kill this process? (y/n)"
IDS_KILL_PROCESS_ERR1 "Unable to terminate this process..."
IDS_KILL_PROCESS_ERR2 "Unable to terminate process %3d (unable to OpenProcess)"
IDS_CTM_GENERAL_ERR1 "ctm: can't use console."
IDS_CTM_GENERAL_ERR2 "ctm: can't GetConsoleMode() for input console."
IDS_CTM_GENERAL_ERR3 "ctm: can't GetConsoleMode() for output console."
END

View file

@ -0,0 +1,31 @@
// French (France) resources
LANGUAGE LANG_FRENCH, SUBLANG_FRENCH
STRINGTABLE DISCARDABLE
BEGIN
IDS_APP_TITLE "Console TaskManager v0.1 par Aleksey Bragin <aleksey@studiocerebral.com>"
IDS_COLUMN_IMAGENAME "Processus "
IDS_COLUMN_PID "PID "
IDS_COLUMN_CPU "CPU"
IDS_COLUMN_MEM "Util. mém"
IDS_COLUMN_MEM_UNIT "Ko"
IDS_COLUMN_PF "Défauts de page"
IDS_IDLE_PROCESS "Processus inactif du système"
IDS_YES "O"
IDS_NO "N"
IDS_MENU "Menu: Quitter (q), Tuer processus (t)"
IDS_MENU_QUIT "Q"
IDS_MENU_KILL_PROCESS "T"
IDS_KILL_PROCESS "Etes-vous sûr de vouloir tuer ce processus (o/n) ?"
IDS_KILL_PROCESS_ERR1 "Impossible de terminer ce processus..."
IDS_KILL_PROCESS_ERR2 "Impossible de terminer le processus %3d (erreur lors de OpenProcess)"
IDS_CTM_GENERAL_ERR1 "ctm: impossible d'utiliser la console."
IDS_CTM_GENERAL_ERR2 "ctm: impossible de récupérer les paramètres de la console d'entrée."
IDS_CTM_GENERAL_ERR3 "ctm: impossible de récupérer les paramètres de la console de sortie."
END

View file

@ -1,3 +1,6 @@
#define UNICODE
#define _UNICODE
/* Console Task Manager
ctm.c - main program file
@ -6,8 +9,11 @@
Most of the code dealing with getting system parameters is taken from
ReactOS Task Manager written by Brian Palmer (brianp@reactos.org)
Localization features added by Hervé Poussineau (hpoussineau@fr.st)
History:
24 October 2004 - added localization features
09 April 2003 - v0.1, fixed bugs, added features, ported to mingw
20 March 2003 - v0.03, works good under ReactOS, and allows process
killing
@ -42,17 +48,30 @@
#include <ntos/zwtypes.h>
#include "ctm.h"
#include "resource.h"
#define MAX_PROC 17
#define TIMES
HANDLE hStdin;
HANDLE hStdout;
HINSTANCE hInst;
DWORD inConMode;
DWORD outConMode;
//PROCNTQSI NtQuerySystemInformation= NULL;
DWORD columnRightPositions[5];
TCHAR lpSeparator[80];
TCHAR lpHeader[80];
TCHAR lpMemUnit[3];
TCHAR lpIdleProcess[80];;
TCHAR lpTitle[80];
TCHAR lpHeader[80];
TCHAR lpMenu[80];
TCHAR lpEmpty[80];
TCHAR KEY_QUIT, KEY_KILL;
TCHAR KEY_YES, KEY_NO;
const int ProcPerScreen = 17; // 17 processess are displayed on one page
ULONG ProcessCountOld = 0;
@ -70,9 +89,11 @@ PPERFDATA pPerfData = NULL; // Most recent copy of perf data
int selection=0;
int scrolled=0; // offset from which process start showing
int first = 0; // first time in DisplayScreen
#define NEW_CONSOLE
// Functions that are needed by epsapi
void *PsaiMalloc(SIZE_T size) { return malloc(size); }
void *PsaiRealloc(void *ptr, SIZE_T size) { return realloc(ptr, size); }
void PsaiFree(void *ptr) { free(ptr); }
@ -120,39 +141,33 @@ void DisplayScreen()
int posStr;
DWORD numChars;
int lines;
int idx;
static int first = 0;
int idx, i;
if (first == 0)
{
// Header
pos.X = 2; pos.Y = 2;
_tcscpy(lpStr, _T("Console TaskManager v0.1 by Aleksey Bragin <aleksey@studiocerebral.com>"));
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
// Header
pos.X = 2; pos.Y = 2;
WriteConsoleOutputCharacter(hStdout, lpTitle, _tcslen(lpTitle), pos, &numChars);
pos.X = 2; pos.Y = 3;
_tcscpy(lpStr, _T("+-------------------------------+-------+-----+-----------+-------------+"));
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
pos.X = 2; pos.Y = 3;
WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
pos.X = 2; pos.Y = 4;
_tcscpy(lpStr, _T("| Image name | PID | CPU | Mem Usage | Page Faults |"));
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
pos.X = 2; pos.Y = 4;
WriteConsoleOutputCharacter(hStdout, lpHeader, _tcslen(lpHeader), pos, &numChars);
pos.X = 2; pos.Y = 5;
_tcscpy(lpStr, _T("+-------------------------------+-------+-----+-----------+-------------+"));
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
pos.X = 2; pos.Y = 5;
WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
// Footer
pos.X = 2; pos.Y = 23;
_tcscpy(lpStr, _T("+-------------------------------+-------+-----+-----------+-------------+"));
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
// Footer
pos.X = 2; pos.Y = ProcPerScreen+6;
WriteConsoleOutputCharacter(hStdout, lpSeparator, _tcslen(lpSeparator), pos, &numChars);
// Menu
pos.X = 2; pos.Y = ProcPerScreen+7;
WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
WriteConsoleOutputCharacter(hStdout, lpMenu, _tcslen(lpMenu), pos, &numChars);
// Menu
pos.X = 2; pos.Y = 24;
_tcscpy(lpStr, _T("Press: q - quit, k - kill process "));
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
first = 1;
first = 1;
}
// Processess
@ -168,11 +183,14 @@ void DisplayScreen()
TCHAR lpMemUsg[12];
TCHAR lpPageFaults[15];
WORD wColor;
for (i = 0; i < 80; i++)
lpStr[i] = _T(' ');
// data
// image name
if (idx < lines && scrolled + idx < ProcessCount)
{
// image name
#ifdef _UNICODE
len = wcslen(pPerfData[scrolled+idx].ImageName);
#else
@ -180,77 +198,40 @@ void DisplayScreen()
imgName, MAX_PATH, NULL, NULL);
len = strlen(imgName);
#endif
if (len > 31)
{
len = 31;
}
if (len > columnRightPositions[0])
{
len = columnRightPositions[0];
}
#ifdef _UNICODE
wcsncpy(&lpStr[2], pPerfData[scrolled+idx].ImageName, len);
#else
strncpy(&lpStr[2], imgName, len);
#endif
}
else
{
len = 0;
}
if (len < 31)
{
_tcsncpy(&lpStr[2 + len], _T(" "), 31 - len);
}
// PID
if (idx < lines && scrolled + idx < ProcessCount)
{
_stprintf(lpPid, _T("%6ld "), pPerfData[scrolled+idx].ProcessId);
_tcsncpy(&lpStr[34], lpPid, 7);
}
else
{
_tcsncpy(&lpStr[34], _T(" "), 7);
}
// PID
_stprintf(lpPid, _T("%6ld"), pPerfData[scrolled+idx].ProcessId);
_tcsncpy(&lpStr[columnRightPositions[1] - 6], lpPid, 6);
// CPU
if (idx < lines && scrolled + idx < ProcessCount)
{
_stprintf(lpCpu, _T("%3d%% "), pPerfData[scrolled+idx].CPUUsage);
_tcsncpy(&lpStr[42], lpCpu, 5);
}
else
{
_tcsncpy(&lpStr[42], _T(" "), 5);
}
#ifdef TIMES
// CPU
_stprintf(lpCpu, _T("%3d%%"), pPerfData[scrolled+idx].CPUUsage);
_tcsncpy(&lpStr[columnRightPositions[2] - 4], lpCpu, 4);
#endif
// Mem usage
if (idx < lines && scrolled + idx < ProcessCount)
{
_stprintf(lpMemUsg, _T("%6ld "), pPerfData[scrolled+idx].WorkingSetSizeBytes / 1024);
_tcsncpy(&lpStr[48], lpMemUsg, 11);
}
else
{
_tcsncpy(&lpStr[48], _T(" "), 11);
}
// Mem usage
_stprintf(lpMemUsg, _T("%6ld %s"), pPerfData[scrolled+idx].WorkingSetSizeBytes / 1024, lpMemUnit);
_tcsncpy(&lpStr[columnRightPositions[3] - 9], lpMemUsg, 9);
// Page Fault
if (idx < lines && scrolled + idx < ProcessCount)
{
_stprintf(lpPageFaults, _T("%12ld "), pPerfData[scrolled+idx].PageFaultCount);
_tcsncpy(&lpStr[60], lpPageFaults, 13);
}
else
{
_tcsncpy(&lpStr[60], _T(" "), 13);
// Page Fault
_stprintf(lpPageFaults, _T("%12ld"), pPerfData[scrolled+idx].PageFaultCount);
_tcsncpy(&lpStr[columnRightPositions[4] - 12], lpPageFaults, 12);
}
// columns
lpStr[0] = _T(' ');
lpStr[1] = _T('|');
lpStr[33] = _T('|');
lpStr[41] = _T('|');
lpStr[47] = _T('|');
lpStr[59] = _T('|');
lpStr[73] = _T('|');
for (i = 0; i < 5; i++)
lpStr[columnRightPositions[i] + 1] = _T('|');
pos.X = 1; pos.Y = 6+idx;
WriteConsoleOutputCharacter(hStdout, lpStr, 74, pos, &numChars);
@ -274,7 +255,7 @@ void DisplayScreen()
FillConsoleOutputAttribute(
hStdout, // screen buffer handle
wColor, // color to fill with
31, // number of cells to fill
columnRightPositions[0] - 1, // number of cells to fill
pos, // first cell to write to
&numChars); // actual number written
}
@ -285,13 +266,14 @@ void DisplayScreen()
// returns TRUE if exiting
int ProcessKeys(int numEvents)
{
DWORD numChars;
if ((ProcessCount-scrolled < 17) && (ProcessCount > 17))
scrolled = ProcessCount-17;
TCHAR key = GetKeyPressed(numEvents);
if (key == VK_Q)
if (key == KEY_QUIT)
return TRUE;
else if (key == VK_K)
else if (key == KEY_KILL)
{
// user wants to kill some process, get his acknowledgement
DWORD pId;
@ -299,15 +281,15 @@ int ProcessKeys(int numEvents)
TCHAR lpStr[100];
pos.X = 2; pos.Y = 24;
_tcscpy(lpStr, _T("Are you sure you want to kill this process? (y/n)"));
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &pId);
if (LoadString(hInst, IDS_KILL_PROCESS, lpStr, 100))
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
do {
GetNumberOfConsoleInputEvents(hStdin, &pId);
key = GetKeyPressed(pId);
} while (key == 0);
if (key == VK_Y)
if (key == KEY_YES)
{
HANDLE hProcess;
pId = pPerfData[selection+scrolled].ProcessId;
@ -317,8 +299,11 @@ int ProcessKeys(int numEvents)
{
if (!TerminateProcess(hProcess, 0))
{
_tcscpy(lpStr, _T("Unable to terminate this process... "));
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &pId);
if (LoadString(hInst, IDS_KILL_PROCESS_ERR1, lpStr, 80))
{
WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
}
Sleep(1000);
}
@ -326,11 +311,17 @@ int ProcessKeys(int numEvents)
}
else
{
_stprintf(lpStr, _T("Unable to terminate process %3d (unable to OpenProcess) "), pId);
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &pId);
Sleep(1000);
if (LoadString(hInst, IDS_KILL_PROCESS_ERR2, lpStr, 80))
{
WriteConsoleOutputCharacter(hStdout, lpEmpty, _tcslen(lpEmpty), pos, &numChars);
_stprintf(lpStr, lpStr, pId);
WriteConsoleOutputCharacter(hStdout, lpStr, _tcslen(lpStr), pos, &numChars);
}
Sleep(1000);
}
}
first = 0;
}
else if (key == VK_UP)
{
@ -350,11 +341,6 @@ int ProcessKeys(int numEvents)
return FALSE;
}
void PerfInit()
{
// NtQuerySystemInformation = //(PROCNTQSI)GetProcAddress(GetModuleHandle(_T("ntdll.dll")), //"NtQuerySystemInformation");
}
void PerfDataRefresh()
{
LONG status;
@ -435,11 +421,9 @@ void PerfDataRefresh()
// Now alloc a new PERFDATA array and fill in the data
if (pPerfDataOld) {
//delete[] pPerfDataOld;
free(pPerfDataOld);
}
pPerfDataOld = pPerfData;
//pPerfData = new PERFDATA[ProcessCount];
pPerfData = (PPERFDATA)malloc(sizeof(PERFDATA) * ProcessCount);
pSPI = PsaWalkFirstProcess((PSYSTEM_PROCESSES)pBuffer);
for (Idx=0; Idx<ProcessCount; Idx++) {
@ -461,7 +445,13 @@ void PerfDataRefresh()
pPerfData[Idx].ImageName[pSPI->ProcessName.Length / sizeof(WCHAR)] = 0;
}
else
wcscpy(pPerfData[Idx].ImageName, L"System Idle Process");
{
#ifdef _UNICODE
wcscpy(pPerfData[Idx].ImageName, lpIdleProcess);
#else
MultiByteToWideChar(CP_ACP, 0, lpIdleProcess, strlen(lpIdleProcess), pPerfData[Idx].ImageName, MAX_PATH);
#endif
}
pPerfData[Idx].ProcessId = pSPI->ProcessId;
@ -531,7 +521,6 @@ int MultiByteToWideChar(
#endif
pSPI = PsaWalkNextProcess(pSPI);
}
//delete[] pBuffer;
PsaFreeCapture(pBuffer);
free(SysProcessorTimeInfo);
@ -565,31 +554,96 @@ unsigned int GetKeyPressed(int events)
int main(int *argc, char **argv)
{
int i;
TCHAR lpStr[80];
for (i = 0; i < 80; i++)
lpEmpty[i] = lpHeader[i] = _T(' ');
lpEmpty[79] = _T('\0');
/* Initialize global variables */
hInst = 0 /* FIXME: which value? [used with LoadString(hInst, ..., ..., ...)] */;
if (LoadString(hInst, IDS_COLUMN_IMAGENAME, lpStr, 80))
{
columnRightPositions[0] = _tcslen(lpStr);
_tcsncpy(&lpHeader[2], lpStr, _tcslen(lpStr));
}
if (LoadString(hInst, IDS_COLUMN_PID, lpStr, 80))
{
columnRightPositions[1] = columnRightPositions[0] + _tcslen(lpStr) + 3;
_tcsncpy(&lpHeader[columnRightPositions[0] + 2], lpStr, _tcslen(lpStr));
}
if (LoadString(hInst, IDS_COLUMN_CPU, lpStr, 80))
{
columnRightPositions[2] = columnRightPositions[1] + _tcslen(lpStr) + 3;
_tcsncpy(&lpHeader[columnRightPositions[1] + 2], lpStr, _tcslen(lpStr));
}
if (LoadString(hInst, IDS_COLUMN_MEM, lpStr, 80))
{
columnRightPositions[3] = columnRightPositions[2] + _tcslen(lpStr) + 3;
_tcsncpy(&lpHeader[columnRightPositions[2] + 2], lpStr, _tcslen(lpStr));
}
if (LoadString(hInst, IDS_COLUMN_PF, lpStr, 80))
{
columnRightPositions[4] = columnRightPositions[3] + _tcslen(lpStr) + 3;
_tcsncpy(&lpHeader[columnRightPositions[3] + 2], lpStr, _tcslen(lpStr));
}
for (i = 0; i < columnRightPositions[4]; i++)
lpSeparator[i] = _T('-');
lpHeader[0] = _T('|');
lpSeparator[0] = _T('+');
for (i = 0; i < 5; i++)
{
lpHeader[columnRightPositions[i]] = _T('|');
lpSeparator[columnRightPositions[i]] = _T('+');
}
lpSeparator[columnRightPositions[4] + 1] = _T('\0');
if (!LoadString(hInst, IDS_APP_TITLE, lpTitle, 80))
lpTitle[0] = _T('\0');
if (!LoadString(hInst, IDS_COLUMN_MEM_UNIT, lpMemUnit, 3))
lpMemUnit[0] = _T('\0');
if (!LoadString(hInst, IDS_MENU, lpMenu, 80))
lpMenu[0] = _T('\0');
if (!LoadString(hInst, IDS_IDLE_PROCESS, lpIdleProcess, 80))
lpIdleProcess[0] = _T('\0');
if (LoadString(hInst, IDS_MENU_QUIT, lpStr, 2))
KEY_QUIT = lpStr[0];
if (LoadString(hInst, IDS_MENU_KILL_PROCESS, lpStr, 2))
KEY_KILL = lpStr[0];
if (LoadString(hInst, IDS_YES, lpStr, 2))
KEY_YES = lpStr[0];
if (LoadString(hInst, IDS_NO, lpStr, 2))
KEY_NO = lpStr[0];
GetInputOutputHandles();
if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE)
{
printf("ctm: can't use console.");
if (LoadString(hInst, IDS_CTM_GENERAL_ERR1, lpStr, 80))
_tprintf(lpStr);
return -1;
}
if (GetConsoleMode(hStdin, &inConMode) == 0)
{
printf("ctm: can't GetConsoleMode() for input console.");
if (LoadString(hInst, IDS_CTM_GENERAL_ERR2, lpStr, 80))
_tprintf(lpStr);
return -1;
}
if (GetConsoleMode(hStdout, &outConMode) == 0)
{
printf("ctm: can't GetConsoleMode() for output console.");
if (LoadString(hInst, IDS_CTM_GENERAL_ERR3, lpStr, 80))
_tprintf(lpStr);
return -1;
}
SetConsoleMode(hStdin, 0); //FIXME: Should check for error!
SetConsoleMode(hStdout, 0); //FIXME: Should check for error!
PerfInit();
while (1)
{
DWORD numEvents;
@ -598,7 +652,7 @@ int main(int *argc, char **argv)
DisplayScreen();
//WriteConsole(hStdin, " ", 1, &numEvents, NULL); // TODO: Make another way (this is ugly, I know)
#if 1
#if 0
/* WaitForSingleObject for console handles is not implemented in ROS */
WaitForSingleObject(hStdin, 1000);
#endif
@ -610,7 +664,7 @@ int main(int *argc, char **argv)
if (ProcessKeys(numEvents) == TRUE)
break;
}
#if 0
#if 1
else
{
/* Should be removed, if WaitForSingleObject is implemented for console handles */

View file

@ -1,7 +1,13 @@
/* $Id: ctm.rc,v 1.3 2004/10/16 22:30:18 gvg Exp $ */
/* $Id$ */
#include <defines.h>
#include "resource.h"
#define REACTOS_STR_FILE_DESCRIPTION "ReactOS Console Task Manager\0"
#define REACTOS_STR_INTERNAL_NAME "ctm\0"
#define REACTOS_STR_ORIGINAL_FILENAME "ctm.exe\0"
#define REACTOS_STR_ORIGINAL_COPYRIGHT "2003, Aleksey Bragin\0"
#include <reactos/version.rc>
#include "En.rc"
#include "Fr.rc"

View file

@ -0,0 +1,44 @@
/*
* CTM resource definitions
*
* Copyright 2004 ReactOS team
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define IDS_APP_TITLE 0
#define IDS_COLUMN_IMAGENAME 10
#define IDS_COLUMN_PID 11
#define IDS_COLUMN_CPU 12
#define IDS_COLUMN_MEM 13
#define IDS_COLUMN_MEM_UNIT 14
#define IDS_COLUMN_PF 15
#define IDS_IDLE_PROCESS 100
#define IDS_YES 101
#define IDS_NO 102
#define IDS_MENU 200
#define IDS_MENU_QUIT 201
#define IDS_MENU_KILL_PROCESS 202
#define IDS_KILL_PROCESS 1000
#define IDS_KILL_PROCESS_ERR1 1001
#define IDS_KILL_PROCESS_ERR2 1002
#define IDS_CTM_GENERAL_ERR1 32768
#define IDS_CTM_GENERAL_ERR2 32769
#define IDS_CTM_GENERAL_ERR3 32770