50855c2a3bcef6ae9d86ac0dda3900e78b462682
[reactos.git] / rosapps / cmdutils / more.c
1 /*
2 * MORE.C - external command.
3 *
4 * clone from 4nt more command
5 *
6 * 26 Sep 1999 - Paolo Pantaleo <paolopan@freemail.it>
7 * started
8 */
9
10 #include <windows.h>
11 #include <malloc.h>
12 #include <tchar.h>
13
14
15 DWORD len;
16 LPTSTR msg = "--- continue ---";
17
18
19 /*handle for file and console*/
20 HANDLE hStdIn;
21 HANDLE hStdOut;
22 HANDLE hStdErr;
23 HANDLE hKeyboard;
24
25
26 static VOID
27 GetScreenSize (PSHORT maxx, PSHORT maxy)
28 {
29 CONSOLE_SCREEN_BUFFER_INFO csbi;
30
31 GetConsoleScreenBufferInfo (hStdOut, &csbi);
32
33 if (maxx)
34 *maxx = csbi.dwSize.X;
35 if (maxy)
36 *maxy = csbi.dwSize.Y;
37 }
38
39
40 static
41 VOID ConOutPuts (LPTSTR szText)
42 {
43 DWORD dwWritten;
44
45 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), szText, _tcslen(szText), &dwWritten, NULL);
46 WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), "\n", 1, &dwWritten, NULL);
47 }
48
49
50 static VOID
51 ConInKey (VOID)
52 {
53 INPUT_RECORD ir;
54 DWORD dwRead;
55
56 do
57 {
58 ReadConsoleInput (hKeyboard, &ir, 1, &dwRead);
59 if ((ir.EventType == KEY_EVENT) &&
60 (ir.Event.KeyEvent.bKeyDown == TRUE))
61 return;
62 }
63 while (TRUE);
64 }
65
66
67 static VOID
68 WaitForKey (VOID)
69 {
70 DWORD dwWritten;
71
72 WriteFile (hStdErr,msg , len, &dwWritten, NULL);
73
74 ConInKey();
75
76 WriteFile (hStdErr, _T("\n"), 1, &dwWritten, NULL);
77
78 // FlushConsoleInputBuffer (hConsoleIn);
79 }
80
81
82 //INT CommandMore (LPTSTR cmd, LPTSTR param)
83 int main (int argc, char **argv)
84 {
85 SHORT maxx,maxy;
86 SHORT line_count=0,ch_count=0;
87 INT i, last;
88
89 /*reading/writing buffer*/
90 TCHAR *buff;
91
92 /*bytes written by WriteFile and ReadFile*/
93 DWORD dwRead,dwWritten;
94
95 /*ReadFile() return value*/
96 BOOL bRet;
97
98 len = _tcslen (msg);
99 hStdIn = GetStdHandle(STD_INPUT_HANDLE);
100 hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
101 hStdErr = GetStdHandle(STD_ERROR_HANDLE);
102
103 if (argc > 1 && _tcsncmp (argv[1], _T("/?"), 2) == 0)
104 {
105 ConOutPuts(_T("Help text still missing!!"));
106 return 0;
107 }
108
109 hKeyboard = CreateFile ("CONIN$", GENERIC_READ,
110 0,NULL,OPEN_ALWAYS,0,0);
111
112 GetScreenSize(&maxx,&maxy);
113
114 buff=malloc(4096);
115
116 FlushConsoleInputBuffer (hKeyboard);
117
118 do
119 {
120 bRet = ReadFile(hStdIn,buff,4096,&dwRead,NULL);
121
122 for(last=i=0;i<dwRead && bRet;i++)
123 {
124 ch_count++;
125 if(buff[i] == _T('\n') || ch_count == maxx)
126 {
127 ch_count=0;
128 line_count++;
129 if (line_count == maxy-1)
130 {
131 line_count = 0;
132 WriteFile(hStdOut,&buff[last], i-last+1, &dwWritten, NULL);
133 last=i+1;
134 FlushFileBuffers (hStdOut);
135 WaitForKey ();
136 }
137 }
138 }
139 if (last<dwRead && bRet)
140 WriteFile(hStdOut,&buff[last], dwRead-last, &dwWritten, NULL);
141
142 }
143 while(dwRead>0 && bRet);
144
145 free (buff);
146 CloseHandle (hKeyboard);
147
148 return 0;
149 }
150
151 /* EOF */