This repository has been archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCONSOLE.c
1225 lines (972 loc) · 29.3 KB
/
CONSOLE.c
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* console.c
*
* AUTHOR: John L. Miller, [email protected] / [email protected]
* DATE: 8/4/96
*
* Copyright (c) 1996 John L. Miller
*
* Full permission is granted to use, modify and distribute
* this code, provided:
* 1) This comment field is included in its entirity
* 2) No money is charged for any work including or based on
* portions of this code.
*
* If you're a nice person and find this useful, I'd appreciate a
* note letting me know about it. e-mail is usually what spurs me
* on to improve and support software I've written.
*
* This file contains functions intended to provide the back
* end to a console window for my semi-vt100 emulator.
*/
/* Note - one HUGE difference between console windows and terminal
* windows. Console windows displays start at (0,0). Terminal displays
* start at (1,1). YUCK!
*/
#ifdef _WIN32
/* FIX - MDobak
* Prevent to run windows.h in UNICODE mode.
*/
#undef UNICODE
#include <windows.h>
#include "vt100.h"
int topScrollRow=TOP_EDGE;
int bottomScrollRow=BOTTOM_EDGE;
/* This variable will contain terminal configuration flags, such as
* reverse/standard video, whether wrapping is enabled, and so on.
*/
int conTermMode;
/* Variable to hold the cursor position for save/restore cursor calls */
COORD cursorPosSave= {1,1};
/* Handles to the current console for input and output */
HANDLE hConIn, hConOut;
/* Array of all the tabs which are currently set. Ironically, I think the
* primary emulator can CLEAR tags, but not set them.
*/
int tabSet[132]= {0};
int numTabs = 0;
/* Default settings. - Mdobak */
CONSOLE_SCREEN_BUFFER_INFO defaultSettings;
/* This section contains console-specific color information. NT consoles can
* have Red, blue, green, and intensity flags set. Hence, 4 con_colors.
*/
#define NUM_CON_COLORS 4
/* Foreground and background colors are separated out */
int conForeColors, conBackColors;
/* mapping between foreground and background console colors: needed
* when reverse video is being used
*/
int conColorMapping[NUM_CON_COLORS][2] = {
{FOREGROUND_RED, BACKGROUND_RED},
{FOREGROUND_BLUE, BACKGROUND_BLUE},
{FOREGROUND_GREEN, BACKGROUND_GREEN},
{FOREGROUND_INTENSITY, BACKGROUND_INTENSITY}
};
/* Device-independant foreground and background flags stored here.
* probably a bad division of labor, but hey, since we don't use
* all of their flags in our console stuff (and hence can't retrieve
* them), the information has to live SOMEWHERE.
*/
int scForeFlags, scBackFlags;
/* Defines for array indexing for translation of flags */
#define SC_FLAG 0
#define CONSOLE_FLAG 1
/* Color mapping between SC (the vt-100 emulator device independant
* flags) and NT console character specific flags. Flags which have no analog
* are set to 0. Note that all global character attributes (character set
* underline, bold, reverse) are all stored in foreground only
*/
const int scForeMapping[NUM_SC_ATTRIBUTES][2] = {
{SC_RED,FOREGROUND_RED},
{SC_GREEN,FOREGROUND_GREEN},
{SC_BLUE,FOREGROUND_BLUE},
{SC_BOLD,FOREGROUND_INTENSITY},
{SC_UL,0},
{SC_BL,0},
{SC_RV,0},
{SC_ASCII,0},
{SC_G0,0},
{SC_G1,0},
{SC_GRAPHICS,0}
};
/* Background color mapping between SC and console */
const int scBackMapping[NUM_SC_ATTRIBUTES][2] = {
{SC_RED,BACKGROUND_RED},
{SC_GREEN,BACKGROUND_GREEN},
{SC_BLUE,BACKGROUND_BLUE},
{SC_BOLD,BACKGROUND_INTENSITY},
{SC_UL,0},
{SC_BL,0},
{SC_RV,0},
{SC_ASCII,0},
{SC_G0,0},
{SC_G1,0},
{SC_GRAPHICS,0}
};
/* These arrays map character vals 0-255 to new values.
* Since the G0 and G1 character sets don't have a direct analog in
* NT, I'm settling for replacing the ones I know what to set them
* to.
*/
char G0Chars[256];
char G1Chars[256];
/* These four sets of variables are just precomputed combinations of
* all the possible flags to save time for masking.
*/
int allFore[2], allBack[2];
int bothFore[2], bothBack[2];
/* FORWARD DECLARATIONS */
int
RawPrintLine(
char *text,
int len,
int scrollAtEnd
);
int
Scroll(
int row
);
/* END FORWARD DECLARATIONS */
/* beInitVT100Terminal() -
*
* This function is called by the VT100 emulator as soon as the
* front-end terminal is initialized. It's responsible for setting
* initial state of the terminal, and initing our many wacky variables.
*/
int
beInitVT100Terminal()
{
int i;
CONSOLE_SCREEN_BUFFER_INFO csbi;
/* Set tabs to every 8 spaces initially */
numTabs = 0;
for (numTabs=0; numTabs < 132/8; numTabs++)
tabSet[numTabs] = (numTabs+1)*8;
/* Init the cursor save position to HOME */
cursorPosSave.X = 1;
cursorPosSave.Y = 1;
/* Disable scrolling window limits */
topScrollRow=TOP_EDGE;
bottomScrollRow=BOTTOM_EDGE;
conTermMode = ANSI_MODE|WRAP_MODE|REPEAT_MODE;
hConIn = GetStdHandle(STD_INPUT_HANDLE);
hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
/* Init our time-saving mask variables */
allFore[SC_FLAG] = allBack[SC_FLAG] = 0;
allFore[CONSOLE_FLAG] = allBack[CONSOLE_FLAG] = 0;
bothFore[SC_FLAG] = bothBack[SC_FLAG] = 0;
bothFore[CONSOLE_FLAG] = bothBack[CONSOLE_FLAG] = 0;
for (i=0; i<NUM_SC_ATTRIBUTES; i++) {
allFore[SC_FLAG] |= scForeMapping[i][SC_FLAG];
allFore[CONSOLE_FLAG] |= scForeMapping[i][CONSOLE_FLAG];
allBack[SC_FLAG] |= scBackMapping[i][SC_FLAG];
allBack[CONSOLE_FLAG] |= scBackMapping[i][CONSOLE_FLAG];
if (scForeMapping[i][SC_FLAG] && scForeMapping[i][CONSOLE_FLAG]) {
bothFore[SC_FLAG] |= scForeMapping[i][SC_FLAG];
bothFore[CONSOLE_FLAG] |= scForeMapping[i][CONSOLE_FLAG];
}
if (scBackMapping[i][SC_FLAG] && scBackMapping[i][CONSOLE_FLAG]) {
bothBack[SC_FLAG] |= scBackMapping[i][SC_FLAG];
bothBack[CONSOLE_FLAG] |= scBackMapping[i][CONSOLE_FLAG];
}
}
conForeColors = conBackColors = 0;
for (i=0; i<NUM_CON_COLORS; i++) {
conForeColors |= conColorMapping[i][0];
conBackColors |= conColorMapping[i][1];
}
/* Do initial settings for device-independant flags */
scForeFlags = SC_ASCII;
scBackFlags = 0;
GetConsoleScreenBufferInfo(hConOut, &csbi);
GetConsoleScreenBufferInfo(hConOut, &defaultSettings);
for (i=0; i<NUM_SC_ATTRIBUTES; i++) {
if (csbi.wAttributes & scForeMapping[i][CONSOLE_FLAG])
scForeFlags |= scForeMapping[i][SC_FLAG];
if (csbi.wAttributes & scBackMapping[i][CONSOLE_FLAG])
scBackFlags |= scBackMapping[i][SC_FLAG];
}
/* Since the G0/G1 character sets don't really map to
* USASCII, So come as close as we can. By default, it'll
* just print the ascii character. For the graphics characters
* I was able to identify, change that mapping.
*/
for (i=0; i<256; i++) {
G0Chars[i] = i;
G1Chars[i] = i;
}
G1Chars['a']=(char)177;
G1Chars['f']=(char)248;
G1Chars['g']=(char)241;
G1Chars['j']=(char)217;
G1Chars['k']=(char)191;
G1Chars['l']=(char)218;
G1Chars['m']=(char)192;
G1Chars['n']=(char)197;
G1Chars['o']=(char)196;
G1Chars['p']=(char)196;
G1Chars['q']=(char)196;
G1Chars['r']=(char)196;
G1Chars['s']=(char)196;
G1Chars['t']=(char)195;
G1Chars['u']=(char)180;
G1Chars['v']=(char)193;
G1Chars['w']=(char)194;
G1Chars['x']=(char)179;
G1Chars['y']=(char)243;
G1Chars['z']=(char)242;
return(0);
}
/* beAbsoluteCursor -
*
* Given an input row and column, move the cursor to the
* absolute screen coordinates requested. Note that if the
* display window has scrollbars, the column is adjusted
* to take that into account, but the row is not. This allows
* for large scrollback in terminal windows.
*
* ROW must be able to accept CUR_ROW, TOP_EDGE, BOTTOM_EDGE,
* or a row number.
*
* COLUMN must be able to accept CUR_COL, LEFT_EDGE, RIGHT_EDGE,
* or a column number.
*/
int
beAbsoluteCursor(
int row,
int col
)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD cursorPos;
GetConsoleScreenBufferInfo(hConOut, &csbi);
if (row == CUR_ROW)
row = csbi.dwCursorPosition.Y;
else if (row == TOP_EDGE)
row = csbi.srWindow.Top;
else if (row == BOTTOM_EDGE)
row = csbi.srWindow.Bottom;
else
row += csbi.srWindow.Top - 1;
if (col == CUR_COL)
col = csbi.dwCursorPosition.X;
else if (col == LEFT_EDGE)
col = 0;
else if (col == RIGHT_EDGE)
col = csbi.dwSize.X-1;
else
col -= 1;
cursorPos.X = col;
cursorPos.Y = row;
SetConsoleCursorPosition(hConOut, cursorPos);
return(0);
}
/* beOffsetCursor -
*
* Given an input row and column offset, move the cursor by that
* many positions. For instance, row=0 and column=-1 would move
* the cursor left a single column.
*
* If the cursor can't move the requested amount, results are
* unpredictable.
*/
int
beOffsetCursor(
int row,
int column
)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD cursorPos;
GetConsoleScreenBufferInfo(hConOut, &csbi);
cursorPos = csbi.dwCursorPosition;
cursorPos.X += column;
cursorPos.Y += row;
if (cursorPos.X < 0)
cursorPos.X = 0;
if (cursorPos.X >= csbi.dwSize.X) {
cursorPos.X -= csbi.dwSize.X;
cursorPos.Y += 1;
}
if (cursorPos.Y < 0)
cursorPos.Y = 0;
SetConsoleCursorPosition(hConOut, cursorPos);
return(0);
}
/* beRestoreCursor -
*
* Saved cursor position should be stored in a static
* variable in the back end. This function restores the
* cursor to the position stored in that variable.
*/
int
beRestoreCursor(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD cursorPos;
GetConsoleScreenBufferInfo(hConOut, &csbi);
cursorPos = csbi.dwCursorPosition;
cursorPos.Y += cursorPosSave.Y;
SetConsoleCursorPosition(hConOut, cursorPos);
return(0);
}
/* beSaveCursor -
*
* The back-end should maintain a static variable with the
* last STORED cursor position in it. This function replaces
* the contents of that variable with the current cursor position.
* The cursor may be restored to this position by using the
* beRestoreCursor function.
*/
int
beSaveCursor(void)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConOut, &csbi);
cursorPosSave = csbi.dwCursorPosition;
cursorPosSave.Y -= csbi.srWindow.Top;
return(0);
}
/* beGetTextAttributes -
*
* given a pointer to 'fore'ground and 'back'ground ints,
* fill them with a device-independant description of the
* current foreground and background colors, as well as any
* font information in the foreground variable.
*/
int
beGetTextAttributes(
int *fore,
int *back
)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
int i;
/* Since it's entirely possible that the text attributes were
* changed without our terminal being notified, we might as well
* make sure they're accurate.
*/
/* First, strip out everything in the screen buffer variables
* that we can detect
*/
scForeFlags &= ~bothFore[SC_FLAG];
scBackFlags &= ~bothBack[SC_FLAG];
/* Now, find out what the real settings are, and set the
* flag values accordingly.
*/
GetConsoleScreenBufferInfo(hConOut, &csbi);
/* If reverse video is set, we need to reverse our color mappings
* before any calculations get made.
*/
if (scForeFlags & SC_RV) {
int tmpFore, tmpBack;
tmpFore = csbi.wAttributes & conForeColors;
tmpBack = csbi.wAttributes & conBackColors;
csbi.wAttributes &= ~(conForeColors | conBackColors);
for (i=0; i<NUM_CON_COLORS; i++) {
if (tmpFore & conColorMapping[i][0])
csbi.wAttributes |= conColorMapping[i][1];
if (tmpBack & conColorMapping[i][1])
csbi.wAttributes |= conColorMapping[i][0];
}
}
/* Now, do the actual translation between our detectable
* console text attributes and the corresponding device-independant
* attributes.
*/
for (i=0; i<NUM_SC_ATTRIBUTES; i++) {
if (csbi.wAttributes & scForeMapping[i][CONSOLE_FLAG])
scForeFlags |= scForeMapping[i][SC_FLAG];
if (csbi.wAttributes & scBackMapping[i][CONSOLE_FLAG])
scBackFlags |= scBackMapping[i][SC_FLAG];
}
/* Finally, copy our updated sc flags into the variables
* passed in
*/
if (fore != NULL)
*fore = scForeFlags;
if (back != NULL)
*back = scBackFlags;
return(0);
}
/* beSetTextAttributes -
*
* Given a foreground and a background device independant (SC) color and font
* specification, apply these to the display, and save the state in the
* static screen variables.
*
* Note that many font-specific constants (bold/underline/reverse, G0/G1/ASCII)
* are stored ONLY in the foreground specification.
*/
int
beSetTextAttributes(
int fore,
int back
)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
int i;
WORD wAttributes;
/* First off, let's assign these settings into our
* device-independant holder.
*/
scForeFlags = fore;
scBackFlags = back;
/* Next, determine the console's actual current settings */
GetConsoleScreenBufferInfo(hConOut, &csbi);
/* Mask out any of the attributes which can be set via
* our device-independant options. Since the console settings
* have additional options, we need to retain those so we don't
* do something unpleasant to our I/O abilities, for instance.
*/
wAttributes = csbi.wAttributes;
wAttributes &= ~(bothFore[CONSOLE_FLAG] | bothBack[CONSOLE_FLAG]);
/* Now, loop through the device-independant possibilities for
* flags, and modify our console flags as appropriate.
*/
for (i=0; i<NUM_SC_ATTRIBUTES; i++) {
if (scForeFlags & scForeMapping[i][SC_FLAG])
wAttributes |= scForeMapping[i][CONSOLE_FLAG];
if (scBackFlags & scBackMapping[i][SC_FLAG])
wAttributes |= scBackMapping[i][CONSOLE_FLAG];
}
/* One last unpleasantry: if reverse video is set, then we should
* reverse the foreground and background colors
*/
if (scForeFlags & SC_RV) {
int tmpFore, tmpBack;
tmpFore = wAttributes & conForeColors;
tmpBack = wAttributes & conBackColors;
wAttributes &= ~(conForeColors | conBackColors);
for (i=0; i<NUM_CON_COLORS; i++) {
if (tmpFore & conColorMapping[i][0])
wAttributes |= conColorMapping[i][1];
if (tmpBack & conColorMapping[i][1])
wAttributes |= conColorMapping[i][0];
}
}
/* The appropriate colors, etc. should be set in
* the wAttributes variable now. Apply them to the
* current console.
*/
SetConsoleTextAttribute(hConOut, wAttributes);
return(0);
}
/* beRawTextOut-
*
* The name of this function is misleading. Given a pointer to
* ascii text and a count of bytes to print, print them to the
* display device. If wrapping is enabled, wrap text. If there is a
* scrolling region set and the cursor is in it,
* scroll only within that region. 'beRawTextOut' means that it's guaranteed
* not to have control sequences within the text.
*/
int
beRawTextOut(
char *text,
int len
)
{
int i,j;
/* If there's no work to do, return immediately. */
if ((text == NULL)||(len == 0))
return(0);
i=0;
/* Otherwise, loop through the text until all of it has been output */
while (i < len) {
/* This inner loop serves to divide the raw text to output into
* explicit lines. While the 'RawPrintLine' may still have to
* break lines to do text wrapping, explicit line breaks are
* handled right here.
*/
j=i;
while ((text[j] != '\n')&&(j<len)) {
j++;
}
RawPrintLine(text+i, j-i, (text[j] == '\n'));
i = j+1;
}
return(0);
}
/* RawPrintLine -
*
* This routine is a helper for beRawTextOut. It is given a
* line of text which is guaranteed not to have any newlines
* or control characters (which need to be interpreted) in it.
* It prints out the text, wrapping if necessary, and handles
* scrolling or truncation.
*
* If scrollAtEnd is true, an extra carriage return (scroll) is
* performed after the text has been printed out.
*/
int
RawPrintLine(
char *text,
int len,
int scrollAtEnd
)
{
int i, end;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwWritten;
if ((scrollAtEnd == FALSE) && ((text == NULL)||(len == 0)))
return(0);
GetConsoleScreenBufferInfo(hConOut, &csbi);
/* find out how far to the first tab or end of text */
if (text != NULL) {
for (end=0; end<len; end++) {
if (text[end] == '\t')
break;
}
if (end > (csbi.dwSize.X - csbi.dwCursorPosition.X))
end = (csbi.dwSize.X - csbi.dwCursorPosition.X);
/* If we're in non-ascii mode, we need to do a little
* magic to get the right characters out.
*/
if (scForeFlags & SC_G1) {
for (i=0; i<end; i++) {
text[i] = G1Chars[(int)text[i]];
}
}
/* actually print out the text. */
WriteConsole(hConOut,text,(DWORD)end,&dwWritten,NULL);
if (end == (csbi.dwSize.X - csbi.dwCursorPosition.X))
Scroll(CUR_ROW);
if ( (!(conTermMode & WRAP_MODE))
&& (end == (csbi.dwSize.X - csbi.dwCursorPosition.X))
)
end = len;
if (end != len) {
if (text[end] == '\t') {
beAdvanceToTab();
RawPrintLine(text+end+1,len - (end+1), FALSE);
} else {
RawPrintLine(text+end, len-end, FALSE);
beAbsoluteCursor(CUR_ROW,1);
beOffsetCursor(1,0);
}
}
}
/* Now that we've printed this line, scroll if we need to.
* Note that a scroll implies a newline.
*/
if (scrollAtEnd) {
Scroll(CUR_ROW);
beAbsoluteCursor(CUR_ROW,1);
beOffsetCursor(1,0);
}
return(0);
}
/* Scroll -
*
* Given a row specification, calculate a scroll executed in that
* row. It could be within a scroll range, or outside of it.
*
* For some ungodly reason, I made this routine handle the TOP_EDGE,
* BOTTOM_EDGE, and CUR_ROW specifiers as well as a real row.
*/
int
Scroll(
int row
)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
SMALL_RECT scrollRect;
COORD dest;
CHAR_INFO fillChar;
GetConsoleScreenBufferInfo(hConOut, &csbi);
if (row == TOP_EDGE)
row = csbi.srWindow.Top;
else if (row == BOTTOM_EDGE)
row = csbi.srWindow.Bottom;
else if (row == CUR_ROW)
row = csbi.dwCursorPosition.Y;
else
row += csbi.srWindow.Top;
/* Escape out if we don't really need to scroll */
if ( (row < (csbi.dwSize.Y-1))
&& ((row-csbi.srWindow.Top + 1) < bottomScrollRow)
)
return(0);
/* NT console requires a fill character for scrolling. */
fillChar.Char.AsciiChar=' ';
fillChar.Attributes = csbi.wAttributes;
/* Determine the rectangle of text to scroll. Under NT this
* is actually an overlap-safe block-copy.
*/
scrollRect.Left = 0;
scrollRect.Top = 1;
scrollRect.Right = csbi.dwSize.X;
scrollRect.Bottom = row;
if (topScrollRow != TOP_EDGE) {
scrollRect.Top = csbi.srWindow.Top + topScrollRow + 1;
}
if ( (bottomScrollRow != BOTTOM_EDGE)
&& ((csbi.srWindow.Top+bottomScrollRow) < scrollRect.Bottom)
) {
scrollRect.Bottom = csbi.srWindow.Top + bottomScrollRow;
}
dest.X = 0;
dest.Y = scrollRect.Top - 1;
ScrollConsoleScreenBuffer(hConOut,&scrollRect,NULL,
dest, &fillChar);
return(0);
}
/* beEraseText -
*
* Given a 'from' and a 'to' position in display coordinates,
* this function will fill in all characters between the two
* (inclusive) with spaces. Note that the coordinates do NOT
* specify a rectangle. erasing from (1,1) to (2,2) erases
* all of the first row, and the first two characters of the
* second.
*
* Note that this routine must be able to handle TOP_EDGE,
* BOTTOM_EDGE, LEFT_EDGE, RIGHT_EDGE, CUR_ROW, and CUR_COL
* in the appropriate parameters.
*/
int
beEraseText(
int rowFrom,
int colFrom,
int rowTo,
int colTo
)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD dest, saveCursor;
DWORD nLength, dwWritten;
GetConsoleScreenBufferInfo(hConOut, &csbi);
saveCursor = csbi.dwCursorPosition;
/* Convert the row and column specifications into
* buffer coordinates
*/
if (rowFrom == CUR_ROW)
rowFrom = csbi.dwCursorPosition.Y;
else if (rowFrom == TOP_EDGE)
rowFrom = csbi.srWindow.Top;
else
rowFrom += csbi.srWindow.Top -1;
if (colFrom == CUR_COL)
colFrom = csbi.dwCursorPosition.X;
else if (colFrom == LEFT_EDGE)
colFrom = 0;
else
colFrom -= 1;
if (rowTo == CUR_ROW)
rowTo = csbi.dwCursorPosition.Y;
else if (rowTo == BOTTOM_EDGE)
rowTo = csbi.srWindow.Bottom;
else
rowTo += csbi.srWindow.Top-1;
if (colTo == CUR_COL)
colTo = csbi.dwCursorPosition.X;
else if (colTo == RIGHT_EDGE)
colTo = csbi.dwSize.X;
else
colTo -= 1;
/* We're going to erase by filling a continuous range of
* character cells with spaces. Note that this has displeasing
* asthetics under NT, as highlighting appears to be immune.
*/
nLength = (rowTo - rowFrom)*csbi.dwSize.X;
nLength += colTo - colFrom;
dest.X = colFrom;
dest.Y = rowFrom;
FillConsoleOutputCharacter(hConOut, ' ', nLength, dest, &dwWritten);
FillConsoleOutputAttribute(hConOut, csbi.wAttributes, nLength, dest, &dwWritten);
SetConsoleCursorPosition(hConOut, saveCursor);
return(0);
}
/* beDeleteText -
*
* Given a screen cursor 'from' and 'to' position, this function
* will delete all text between the two. Text will be scrolled
* up as appropriate to fill the deleted space. Note that, as in
* beEraseText, the two coordinates don't specify a rectangle, but
* rather a starting position and ending position. In other words,
* deleting from (1,1) to (2,2) should move the text from (2,3) to the
* end of the second row to (1,1), move line 3 up to line 2, and so on.
*
* This function must be able to process TOP_EDGE, BOTTOM_EDGE, LEFT_EDGE,
* RIGHT_EDGE, CUR_ROW, and CUR_COL specifications in the appropriate
* variables as well as regular row and column specifications.
*/
int
beDeleteText(
int rowFrom,
int colFrom,
int rowTo,
int colTo
)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD dest, saveCursor;
CHAR_INFO fillChar;
GetConsoleScreenBufferInfo(hConOut, &csbi);
saveCursor = csbi.dwCursorPosition;
if (rowFrom == CUR_ROW)
rowFrom = csbi.dwCursorPosition.Y;
else if (rowFrom == TOP_EDGE)
rowFrom = csbi.srWindow.Top;
else
rowFrom += csbi.srWindow.Top -1;
if (colFrom == CUR_COL)
colFrom = csbi.dwCursorPosition.X;
else if (colFrom == LEFT_EDGE)
colFrom = 0;
else
colFrom -= 1;
if (rowTo == CUR_ROW)
rowTo = csbi.dwCursorPosition.Y;
else if (rowTo == BOTTOM_EDGE)
rowTo = csbi.srWindow.Bottom;
else
rowTo += csbi.srWindow.Top-1;
if (colTo == CUR_COL)
colTo = csbi.dwCursorPosition.X;
else if (colTo == RIGHT_EDGE)
colTo = csbi.dwSize.X;
else
colTo -= 1;
fillChar.Char.AsciiChar=' ';
fillChar.Attributes = csbi.wAttributes;
/* Now that we've got the from and to positions
* set correctly, we need to delete appropriate
* rows and columns.
*/
dest.X = colFrom;
dest.Y = rowFrom;
/* BUGBUG - need to implement this. What can I say, I'm lazy :) */
return(0);
}
/* beInsertRow -
*
* Given a row number or CUR_ROW, TOP_EDGE or BOTTOM_EDGE as an input,
* this function will scroll all text from the current row down down by one,
* and create a blank row under the cursor.
*/
int
beInsertRow(
int row
)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
COORD dest;
CHAR_INFO fillChar;
SMALL_RECT scrollRect;
GetConsoleScreenBufferInfo(hConOut, &csbi);
fillChar.Char.AsciiChar=' ';
fillChar.Attributes = csbi.wAttributes;
if (row == CUR_ROW)
row = csbi.dwCursorPosition.Y;
else if (row == TOP_EDGE)
row = csbi.srWindow.Top;
else if (row == BOTTOM_EDGE)
row = csbi.srWindow.Bottom;
else
row += csbi.srWindow.Top-1;
dest.X = 0;
dest.Y = row+1;
scrollRect.Top = row;
scrollRect.Left = 0;
scrollRect.Right = csbi.dwSize.X;
scrollRect.Bottom = csbi.srWindow.Bottom;
ScrollConsoleScreenBuffer(hConOut, &scrollRect, NULL, dest, &fillChar);
return(0);
}
/* beTransmitText -
*
* Given a pointer to text and byte count, this routine should transmit data
* to whatever host made the request it's responding to. Typically this routin
* should transmit data as though the user had typed it in.
*/
int
beTransmitText(
char *text,
int len
)
{
if ((text == NULL) || (len < 1))
return(0);
/* BUGBUG - need to implement this. */