-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwconsole.c
More file actions
525 lines (432 loc) · 12.9 KB
/
Copy pathwconsole.c
File metadata and controls
525 lines (432 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include "wconsole.h"
#include "input.h"
#define SHOW_ERROR() display_error(__LINE__)
HANDLE g_original_output_handle;
DWORD g_original_console_mode;
TCHAR g_original_console_title[256];
HANDLE g_console_output_handle;
HANDLE g_console_input_handle;
struct {
CHAR_INFO buffer[4096]; /* 80 x 50 */
COORD col_row_size;
COORD upper_left_cell;
SMALL_RECT region;
} g_original_screen_contents;
/***********************************************************************
OUTPUT
***********************************************************************/
void display_error(const short line_number)
{
LPVOID lpMsgBuf;
TCHAR title[32];
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL);
wsprintf(title, "Line Number: %d", line_number);
MessageBox(NULL, (LPCSTR) lpMsgBuf, title, MB_OK | MB_ICONINFORMATION);
LocalFree(lpMsgBuf);
}
void write_console(HANDLE handle, const char * string)
{
DWORD characters_written_count;
if ( ! WriteConsole(
handle,
string,
(DWORD) lstrlen(string),
&characters_written_count,
NULL))
{
SHOW_ERROR();
}
}
void console_log(const char * string)
{
write_console(g_original_output_handle, string);
}
void error_log(const char * string)
{
write_console(GetStdHandle(STD_ERROR_HANDLE), string);
}
void set_text_cursor_visibility(int is_visible)
{
CONSOLE_CURSOR_INFO cursor_info;
GetConsoleCursorInfo(g_console_output_handle, &cursor_info);
cursor_info.bVisible = is_visible;
SetConsoleCursorInfo(g_console_output_handle, &cursor_info);
}
void set_color(enum colors color)
{
if ( ! SetConsoleTextAttribute(g_console_output_handle, (WORD) color)) {
SHOW_ERROR();
}
}
void set_cursor_position(const short row, const short col)
{
COORD coord;
coord.X = col;
coord.Y = row;
if ( ! SetConsoleCursorPosition(g_console_output_handle, coord)) {
SHOW_ERROR();
}
}
void write_attributes_at(const short row,
const short col,
unsigned short attribute_buffer[],
const unsigned long length)
{
DWORD cells_written_count;
COORD coord;
coord.X = col;
coord.Y = row;
if ( ! WriteConsoleOutputAttribute(
g_console_output_handle,
attribute_buffer,
length,
coord,
&cells_written_count))
{
SHOW_ERROR();
}
}
void write_characters_at(const short row,
const short col,
char character_buffer[],
const unsigned long length)
{
DWORD cells_written_count;
COORD coord;
coord.X = col;
coord.Y = row;
if ( ! WriteConsoleOutputCharacter(
g_console_output_handle,
character_buffer,
length,
coord,
&cells_written_count))
{
SHOW_ERROR();
}
}
void write_at(const short row, const short col, const char * string)
{
set_cursor_position(row, col);
write_console(g_console_output_handle, string);
}
#if 0
void save_screen_at(short row, short col, struct menu * menu)
{
TCHAR characters[26];
int i, j;
g_original_screen_contents.col_row_size.X = 80;
g_original_screen_contents.col_row_size.Y = 50;
g_original_screen_contents.left_upper_cell.X = 0;
g_original_screen_contents.left_upper_cell.Y = 0;
g_original_screen_contents.region = {
col,
row,
menu->width,
menu->command_count
};
ReadConsoleOutput(
g_console_output_handle,
&g_original_screen_contents.buffer,
g_original_screen_contents.col_row_size,
g_original_screen_contents.left_upper_cell,
&g_original_screen_contents.region
);
/* Print menu's top line */
if (menu->command_count > 0) {
characters[0] = '\xDA';
for (j=1; j < menu->width - 1; ++j) {
characters[j] = '\xC4';
}
characters[] = '\xBF';
}
for (i=0; i < menu->command_count; ++i) {
}
WriteConsoleOutput(
g_console_output_handle,
&char_info,
destination_buffer_size,
destination_buffer_upper_left,
®ion
);
}
#endif
void draw_window_at(const short row,
const short col,
TCHAR * character_buffer,
const short row_count,
const short col_count)
{
CHAR_INFO char_info[4096];
COORD buffer_col_row_size;
COORD buffer_left_upper_cell = { 0, 0 };
SMALL_RECT region;
int i;
buffer_col_row_size.X = col_count;
buffer_col_row_size.Y = row_count;
region.Left = row;
region.Top = col;
region.Right = col_count;
region.Bottom = row_count;
for (i=0; i < row_count * col_count; ++i) {
char_info[i].Char.AsciiChar = character_buffer[i];
char_info[i].Attributes = WHITE_BG | BLACK_FG;
}
WriteConsoleOutput(
g_console_output_handle,
char_info,
buffer_col_row_size,
buffer_left_upper_cell,
®ion
);
}
/*void erase_window_at(short row, short col, struct menu * menu)
{
g_original_screen_contents.col_row_size = { 80, 50 };
g_original_screen_contents.left_upper_cell = { 0, 0 };
g_original_screen_contents.region = {
col,
row,
menu->width,
menu->command_count
};
WriteConsoleOutput(
g_console_output_handle,
&char_info,
destination_buffer_size,
destination_buffer_upper_left,
®ion
);
}*/
/***********************************************************************
RESIZE
***********************************************************************/
void resize_console_buffer(const COORD buffer_size)
{
if ( ! SetConsoleScreenBufferSize(g_console_output_handle, buffer_size)) {
SHOW_ERROR();
}
}
void resize_console_window(SMALL_RECT * window_rect)
{
if ( ! SetConsoleWindowInfo(g_console_output_handle, TRUE, window_rect)) {
SHOW_ERROR();
}
}
void resize_console(const short row_count, const short col_count)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
SMALL_RECT window_rect;
COORD buffer_size;
if ( ! GetConsoleScreenBufferInfo(
g_console_output_handle,
&csbi))
{
SHOW_ERROR();
}
buffer_size = csbi.dwSize;
window_rect = csbi.srWindow;
window_rect.Right = window_rect.Right - window_rect.Left;
window_rect.Bottom = window_rect.Bottom - window_rect.Top;
window_rect.Left = 0;
window_rect.Top = 0;
/* 1. If new window is smaller in either dimension... */
if (row_count < window_rect.Bottom + 1) {
/* Shrink window height */
window_rect.Bottom = row_count - 1;
resize_console_window(&window_rect);
}
if (col_count < window_rect.Right + 1) {
/* Shrink window width */
window_rect.Right = col_count - 1;
resize_console_window(&window_rect);
}
/* 2. Adjust screen buffer dimensions as necessary... */
if (row_count != buffer_size.Y) {
/* Adjust buffer height */
buffer_size.Y = row_count;
resize_console_buffer(buffer_size);
}
if (col_count != buffer_size.X) {
/* Adjust buffer width */
buffer_size.X = col_count;
resize_console_buffer(buffer_size);
}
/* 3. If new window is larger in either dimension... */
if (row_count > window_rect.Bottom + 1) {
/* Grow window height */
window_rect.Bottom = row_count - 1;
resize_console_window(&window_rect);
}
if (col_count > window_rect.Right + 1) {
/* Grow window width */
window_rect.Right = col_count - 1;
resize_console_window(&window_rect);
}
}
/**********************************************************************
INPUT
**********************************************************************/
void read_color_at(const short row,
const short col,
unsigned short attribute_buffer[],
const unsigned long length)
{
DWORD cells_read_count;
COORD coord;
coord.X = col;
coord.Y = row;
if ( ! ReadConsoleOutputAttribute(
g_console_output_handle,
attribute_buffer,
length,
coord,
&cells_read_count))
{
SHOW_ERROR();
}
}
void flush_input(void)
{
if ( ! FlushConsoleInputBuffer(g_console_input_handle)) {
SHOW_ERROR();
}
}
/**
* Read input from the console.
*/
void get_console_input(struct input * p_input)
{
static INPUT_RECORD input_record;
KEY_EVENT_RECORD * p_key_event;
MOUSE_EVENT_RECORD * p_mouse_event;
DWORD records_read_count;
if ( ! ReadConsoleInput(
g_console_input_handle,
&input_record,
1,
&records_read_count))
{
SHOW_ERROR();
}
switch(input_record.EventType) {
case KEY_EVENT:
p_key_event = &input_record.Event.KeyEvent;
p_input->type = KEYBOARD;
p_input->device.keyboard.key_is_pressed = p_key_event->bKeyDown;
switch(p_key_event->wVirtualKeyCode) {
case VK_ESCAPE:
p_input->device.keyboard.key = KEY_ESCAPE;
break;
case 0x46: /* F */
if (p_key_event->bKeyDown) {
p_input->device.keyboard.key = KEY_F;
}
break;
case VK_MENU:
p_input->device.keyboard.key = KEY_ALT;
break;
default:
p_input->type = IGNORED;
}
break; /* KEY_EVENT */
case MOUSE_EVENT:
p_mouse_event = &input_record.Event.MouseEvent;
p_input->type = MOUSE;
p_input->device.mouse.row = p_mouse_event->dwMousePosition.Y;
p_input->device.mouse.col = p_mouse_event->dwMousePosition.X;
p_input->device.mouse.button = p_mouse_event->dwButtonState;
break;
default:
p_input->type = IGNORED;
}
}
/**********************************************************************
INIT
**********************************************************************/
BOOL WINAPI ctrl_handler(DWORD ctrl_type)
{
switch (ctrl_type) {
case CTRL_C_EVENT:
return TRUE;
case CTRL_CLOSE_EVENT:
return TRUE;
default:
return FALSE;
}
}
void restore_original_console(void)
{
if ( ! SetConsoleCtrlHandler((PHANDLER_ROUTINE) ctrl_handler, FALSE)) {
SHOW_ERROR();
}
if ( ! SetConsoleActiveScreenBuffer(g_original_output_handle)) {
SHOW_ERROR();
}
if ( ! SetConsoleMode(g_original_output_handle, g_original_console_mode)) {
SHOW_ERROR();
}
if ( ! SetConsoleTitle(g_original_console_title)) {
SHOW_ERROR();
}
if ( ! FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE))) {
SHOW_ERROR();
}
}
void initialize_console(const short row_count, const short col_count)
{
if ( ! GetConsoleTitle(g_original_console_title, 256)) {
SHOW_ERROR();
}
if ( ! SetConsoleTitle("NT-CMD Editor")) {
SHOW_ERROR();
}
if ((g_original_output_handle = GetStdHandle(STD_OUTPUT_HANDLE))
== INVALID_HANDLE_VALUE)
{
SHOW_ERROR();
}
if ( ! GetConsoleMode(g_original_output_handle, &g_original_console_mode)) {
SHOW_ERROR();
}
if ((g_console_output_handle = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL)) == INVALID_HANDLE_VALUE)
{
SHOW_ERROR();
}
resize_console(row_count, col_count);
/* Make screen buffer active / visible */
if ( ! SetConsoleActiveScreenBuffer(g_console_output_handle)) {
SHOW_ERROR();
}
if ((g_console_input_handle = GetStdHandle(STD_INPUT_HANDLE))
== INVALID_HANDLE_VALUE)
{
SHOW_ERROR();
}
if ( ! FlushConsoleInputBuffer(g_console_input_handle)) {
SHOW_ERROR();
}
if ( ! SetConsoleMode(
g_console_input_handle,
ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT))
{
SHOW_ERROR();
}
if ( ! SetConsoleCtrlHandler((PHANDLER_ROUTINE) ctrl_handler, TRUE)) {
SHOW_ERROR();
}
atexit(restore_original_console);
}