-
Notifications
You must be signed in to change notification settings - Fork 2
/
EscapeSequence.c
715 lines (680 loc) · 20.5 KB
/
EscapeSequence.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
/*------------------------------------------------------------------------------
-- SOURCE FILE: EscapeSequence.c - This contains all the functions necessary
-- to parse an escaped control sequence.
--
-- PROGRAM: Advanced Terminal Emulator Pro
--
-- FUNCTIONS:
-- BOOL CheckDigits(HWND hWnd, CHAR* psBuffer,
-- DWORD length, DWORD *i);
-- BOOL CheckDigitsQ(HWND hWnd, CHAR* psBuffer,
-- DWORD length, DWORD *i);
-- BOOL CheckDigitsSemi(HWND hWnd, CHAR* psBuffer,
-- DWORD length, DWORD *i);
-- DWORD GetDigit(CHAR* psBuffer, DWORD length, DWORD *i);
-- VOID ProcessEsc(HWND hWnd, CHAR* psBuffer,
-- DWORD dwBytesRead);
-- VOID ProcessFont(HWND hWnd);
-- BOOL ProcessParen(CHAR* psBuffer, DWORD length, DWORD *i);
-- BOOL ProcessSquare(HWND hWnd, CHAR* psBuffer,
-- DWORD length, DWORD *i);
--
--
-- DATE: Oct 19, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- NOTES:
-- Receives a buffer from a port read and processes it. Displayable characters
-- and special characters such as line feed are handled character by character.
-- If an escape character is received, the buffer is stepped through until one
-- of three things happens:
--
-- 1. A valid sequence is completed:
-- The remainder of the buffer is then once again processed character
-- by character.
--
-- 2. An sequence is found that cannot be valid:
-- In this case, the buffer starting at the character that made the
-- sequence invalid is processed character by character. For example,
-- if the buffer contains " ESC[0;$H ", then $H would be added to the
-- display buffer and displayed on screen.
--
-- 3. The buffer contains a partial sequence:
-- The incomplete sequence is stored in the window extra structure, and
-- the next buffer from a port read is appended to the end of it.
------------------------------------------------------------------------------*/
#include "EscapeSequence.h"
/*------------------------------------------------------------------------------
-- FUNCTION: ProcessEsc
--
-- DATE: Oct 17, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- INTERFACE: VOID ProcessEsc(HWND hWnd, CHAR* psBuffer, DWORD length)
-- hWnd - Handle to the window
-- psBuffer - Array of chars to be processed
-- length - Number of chars to process
--
-- RETURNS: VOID
--
-- NOTES:
-- Processes an Escape sequence. If a valid sequence is encountered
-- it will either end or send the rest of the array to ProcessRead.
-- If it is invalid it will send the invalid character plus the
-- rest of the array to ProcessRead. If it is incomplete it will
-- save the current sequence and another Read will be done to later
-- process the rest of the sequence.
------------------------------------------------------------------------------*/
VOID ProcessEsc(HWND hWnd, CHAR* psBuffer, DWORD length) {
PWNDDATA pwd = NULL;
DWORD i = 0;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
ESC_VAL(0) = 0;
if (i++ == length - 1) {
pwd->psIncompleteEsc = (CHAR*)malloc(sizeof(CHAR)*length);
strncpy(pwd->psIncompleteEsc, psBuffer, length);
pwd->dwIncompleteLength = length;
return;
}
switch (psBuffer[i++]) {
case '[':
if (i == length) {
pwd->psIncompleteEsc = (CHAR*)malloc(sizeof(CHAR)*length);
strncpy(pwd->psIncompleteEsc, psBuffer, length);
pwd->dwIncompleteLength = length;
return;
}
if (!ProcessSquare(hWnd, psBuffer, length, &i))
break;
else {
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
}
case '(':
if (i == length) {
pwd->psIncompleteEsc = (CHAR*)malloc(sizeof(CHAR)*length);
strncpy(pwd->psIncompleteEsc, psBuffer, length);
pwd->dwIncompleteLength = length;
return;
}
if (!ProcessParen(psBuffer, length, &i))
break;
else {
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
}
break;
case ')':
if (i == length) {
pwd->psIncompleteEsc = (CHAR*)malloc(sizeof(CHAR)*length);
strncpy(pwd->psIncompleteEsc, psBuffer, length);
pwd->dwIncompleteLength = length;
return;
}
if (!ProcessParen(psBuffer, length, &i))
break;
else {
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
}
break;
case '>':
//DISPLAY_ERROR(">");
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
case '=':
//DISPLAY_ERROR("=");
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
case 'I':
case 'M': // move up one line, scrolling if necessary
MoveCursor(hWnd, X + 1, --Y + 1, TRUE);
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
case 'H':
//DISPLAY_ERROR("H");
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
case 'E': // move down one line to 1st position,
// scrolling if necessary
MoveCursor(hWnd, 1, ++Y + 1, TRUE);
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
case 'D': // move down one line, scrolling if necessary
MoveCursor(hWnd, X + 1, ++Y + 1, TRUE);
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
case '#':
if (i >= length)
break;
if (isdigit(psBuffer[i])) {
if (psBuffer[i] == '8') {
ScreenAlignment(hWnd);
}
if (i >= length - 1)
return;
}
i++;
ProcessRead(hWnd, psBuffer + i, length - i);
case 'c':
FormFeed(hWnd);
if (i == length)
return;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
case 'F':
case 'G':
case '7':
case '8':
break;
default :
i--;
ProcessRead(hWnd, psBuffer + i, length - i);
return;
}
pwd->psIncompleteEsc = (CHAR*)malloc(sizeof(CHAR)*length);
strncpy(pwd->psIncompleteEsc, psBuffer, length);
pwd->dwIncompleteLength = length;
}
/*------------------------------------------------------------------------------
-- FUNCTION: GetDigit
--
-- DATE: Oct 17, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- INTERFACE: DWORD GetDigit(CHAR* psBuffer, DWORD length, DWORD *i)
-- hWnd - Handle to the window
-- psBuffer - Array of chars to be processed
-- length - Number of chars to process
--
-- RETURNS: DWORD digit - The number currently being checked in the array of
-- chars.
--
-- NOTES:
-- Processes digits after a square bracket. It decrements i
-- if the sequence is invalid so it can send the invalid character.
------------------------------------------------------------------------------*/
DWORD GetDigit(CHAR* psBuffer, DWORD length, DWORD *i) {
DWORD j = 0;
CHAR *psDigits = NULL;
DWORD digit;
psDigits = (CHAR*)malloc(sizeof(CHAR)*(length + 1));
strncpy(psDigits, psBuffer + *i, length - *i + 1);
psDigits[length - *i] = '\0';
j = strspn(psDigits, "0123456789");
strncpy(psDigits, psBuffer + *i, j);
digit = atoi(psDigits);
*i += j - 1;
return digit;
}
/*------------------------------------------------------------------------------
-- FUNCTION: CheckDigits
--
-- DATE: Oct 17, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- INTERFACE: BOOL CheckDigits(HWND hWnd, CHAR* psBuffer, DWORD length)
-- hWnd - Handle to the window
-- psBuffer - Array of chars to be processed
-- length - Number of chars to process
--
-- RETURNS: BOOL - TRUE if the sequence is valid or invalid
-- - FALSE if the sequence is incomplete
--
-- NOTES:
-- Processes digits after a square bracket. It decrements i
-- if the sequence is invalid so it can send the invalid character.
------------------------------------------------------------------------------*/
BOOL CheckDigits(HWND hWnd, CHAR* psBuffer, DWORD length, DWORD *i) {
PWNDDATA pwd = NULL;
DWORD digit = 0;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
if (*i >= length)
return FALSE;
if (isdigit(psBuffer[--(*i)])) {
digit = GetDigit(psBuffer, length, i);
ESC_VAL(0)++;
ESC_VAL(1) = digit;
if ((*i)++ == length - 1)
return FALSE;
if (digit >= 0) {
switch (psBuffer[(*i)++]) {
case 'A': // Esc[0A
MoveCursor(hWnd, X + 1, Y - ESC_VAL(1) + 1, FALSE);
break;
case 'B': // Esc[0B
MoveCursor(hWnd, X + 1, Y + ESC_VAL(1) + 1, FALSE);
break;
case 'C': // Esc[0C
MoveCursor(hWnd, X + ESC_VAL(1) + 1, Y + 1, FALSE);
break;
case 'D': // Esc[0D
MoveCursor(hWnd, X - ESC_VAL(1) + 1, Y + 1, FALSE);
break;
case 'g':
//DISPLAY_ERROR("num g");
break;
case 'K': // Esc[0K
if (ESC_VAL(1) == 0) {
ClearLine(hWnd, X, Y, CLR_RIGHT);
} else if (ESC_VAL(1) == 1) {
ClearLine(hWnd, X, Y, CLR_LEFT);
} else {
ClearLine(hWnd, X, Y, CLR_LEFT);
ClearLine(hWnd, X, Y, CLR_RIGHT);
}
break;
case 'J': // Esc[0J
if (ESC_VAL(1) == 0) {
ClearScreen(hWnd, X, Y, CLR_DOWN);
} else if (ESC_VAL(1) == 1) {
ClearScreen(hWnd, X, Y, CLR_UP);
} else {
ClearScreen(hWnd, X, Y, CLR_UP);
ClearScreen(hWnd, X, Y, CLR_DOWN);
}
break;
case 'm':
ProcessFont(hWnd);
break;
case ';':
if (!CheckDigitsSemi(hWnd, psBuffer, length, i))
return FALSE;
break;
case 'c':
ProcessWrite(hWnd, '0', FALSE);
case 'q':
break;
default:
(*i)--;
}
} else {
(*i)--;
}
}
return TRUE;
}
/*------------------------------------------------------------------------------
-- FUNCTION: CheckDigitsSemi
--
-- DATE: Oct 17, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- INTERFACE: BOOL CheckDigitsSemi(HWND hWnd, CHAR* psBuffer, DWORD length)
-- hWnd - Handle to the window
-- psBuffer - Array of chars to be processed
-- length - Number of chars to process
--
-- RETURNS: BOOL - TRUE if the sequence is valid or invalid
-- - FALSE if the sequence is incomplete
--
-- NOTES:
-- Processes digits after a semi colon. Loops with digit and
-- semicolon for the font changes. If the sequence is invalid
-- i is decremented.
------------------------------------------------------------------------------*/
BOOL CheckDigitsSemi(HWND hWnd, CHAR* psBuffer, DWORD length, DWORD *i) {
PWNDDATA pwd = NULL;
DWORD digit = 0;
DWORD count = 0;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
if (*i >= length)
return FALSE;
if (isdigit(psBuffer[*i])) {
digit = GetDigit(psBuffer, length, i);
ESC_VAL(0)++;
ESC_VAL(2) = digit;
if ((*i)++ == length - 1)
return FALSE;
if (digit >= 0) {
switch (psBuffer[(*i)++]) {
case 'r': // Esc0;0r
SetScrollRegion(hWnd, ESC_VAL(1), ESC_VAL(2));
break;
case 'H': // Esc0;0H
case 'f': // Esc0;0f
MoveCursor(hWnd, ESC_VAL(2), ESC_VAL(1), FALSE);
break;
case 'm':
ProcessFont(hWnd);
break;
case ';':
do {
if (*i >= length)
return FALSE;
if (isdigit(psBuffer[*i])) {
digit = GetDigit(psBuffer, length, i);
ESC_VAL(0)++;
ESC_VAL(3 + count++) = digit;
//validate digit
if ((*i)++ == length-1)
return FALSE;
} else {
(*i)--;
return TRUE;
}
} while (psBuffer[(*i)++] == ';');
if (psBuffer[(*i)-1] == 'm') {
ProcessFont(hWnd);
} else {
(*i)--;
}
break;
case 'q':
case 'y':
case 'R':
break;
default:
(*i)--;
}
} else {
(*i)--;
}
}
return TRUE;
}
/*------------------------------------------------------------------------------
-- FUNCTION: CheckDigitsQ
--
-- DATE: Oct 17, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- INTERFACE: BOOL CheckDigitsQ(HWND hWnd, CHAR* psBuffer, DWORD length)
-- hWnd - Handle to the window
-- psBuffer - Array of chars to be processed
-- length - Number of chars to process
--
-- RETURNS: BOOL - TRUE if the sequence is valid or invalid
-- - FALSE if the sequence is incomplete
--
-- NOTES:
-- Processes digits after a question mark. It decrements i
-- if the sequence is valid so it can send the invalid character.
------------------------------------------------------------------------------*/
BOOL CheckDigitsQ(HWND hWnd, CHAR* psBuffer, DWORD length, DWORD *i) {
PWNDDATA pwd = NULL;
DWORD digit = 0;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
if (*i == length)
return FALSE;
if (isdigit(psBuffer[*i])) {
digit = GetDigit(psBuffer, length, i);
ESC_VAL(0)++;
ESC_VAL(1) = digit;
if ((*i)++ == length - 1)
return FALSE;
if (digit >= 0) {
switch (psBuffer[(*i)++]) {
case 'h':
if (digit == 1) {
pwd->cursorMode = FALSE;
} else if (digit == 25) {
// DISPLAY_ERROR("Show cursor")
} else if (digit == 7) {
pwd->wordWrap = TRUE;
} else if (digit == 6) {
pwd->relOrigin = TRUE;
}
break;
case 'l':
if (digit == 1) {
pwd->cursorMode = TRUE;
} else if (digit == 25) {
// DISPLAY_ERROR("Hide cursor")
} else if (digit == 7) {
pwd->wordWrap = FALSE;
} else if (digit == 6) {
pwd->relOrigin = FALSE;
}
break;
case 'c':
break;
default:
(*i)--;
break;
}
} else {
(*i)--;
}
}
return TRUE;
}
/*------------------------------------------------------------------------------
-- FUNCTION: ProcessSquare
--
-- DATE: Oct 17, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- INTERFACE: BOOL ProcessSquare(HWND hWnd, CHAR* psBuffer, DWORD length)
-- hWnd - Handle to the window
-- psBuffer - Array of chars to be processed
-- length - Number of chars to process
--
-- RETURNS: BOOL - TRUE if the sequence is valid or invalid
-- - FALSE if the sequence is incomplete
--
-- NOTES:
-- Processes a square bracket from an escape sequence. It decrements i
-- if the sequence is valid so it can send the invalid character.
------------------------------------------------------------------------------*/
BOOL ProcessSquare(HWND hWnd, CHAR* psBuffer, DWORD length, DWORD *i) {
PWNDDATA pwd = NULL;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
switch (psBuffer[(*i)++]) {
case 'A': // Esc[A
MoveCursor(hWnd, X + 1, --Y + 1, FALSE);
break;
case 'B': // Esc[B
MoveCursor(hWnd, X + 1, ++Y + 1, FALSE);
break;
case 'C': // Esc[C
MoveCursor(hWnd, ++X + 1, Y + 1, FALSE);
break;
case 'D': // Esc[D
MoveCursor(hWnd, --X + 1, Y + 1, FALSE);
break;
case 'H': // Esc[H
case 'f': // Esc[f
MoveCursor(hWnd, 1, 1, FALSE);
break;
case 'g':
//DISPLAY_ERROR("g");
break;
case 'K': // Esc[K
ClearLine(hWnd, X, Y, CLR_RIGHT);
break;
case 'J': // Esc[J
ClearScreen(hWnd, X, Y, CLR_DOWN);
break;
case 'm':
ESC_VAL(0)++;
ESC_VAL(1) = 0;
ProcessFont(hWnd);
break;
case '?':
if (!CheckDigitsQ(hWnd, psBuffer, length, i)) {
return FALSE;
}
break;
case ';': // Esc[;H
if (*i >= length) {
return FALSE;
}
if (psBuffer[*i] == 'H' || psBuffer[*i] == 'f') {
(*i)++;
MoveCursor(hWnd, 1, 1, FALSE);
} else {
*i;
}
break;
default:
if (!CheckDigits(hWnd, psBuffer, length, i)) {
return FALSE;
}
break;
}
return TRUE;
}
/*------------------------------------------------------------------------------
-- FUNCTION: ProcessParen
--
-- DATE: Oct 17, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- INTERFACE: BOOL ProcessParen(HWND hWnd, CHAR* psBuffer, DWORD length)
-- hWnd - Handle to the window
-- psBuffer - Array of chars to be processed
-- length - Number of chars to process
--
-- RETURNS: BOOL - TRUE if the sequence is valid or invalid
--
-- NOTES:
-- Processes a parantheses from an escape sequence. It decrements i
-- if the sequence is invalid so it can send the invalid character.
------------------------------------------------------------------------------*/
BOOL ProcessParen(CHAR* psBuffer, DWORD length, DWORD *i) {
switch (psBuffer[(*i)++]) {
case 'A':
//DISPLAY_ERROR("paran A");
break;
case 'B':
//DISPLAY_ERROR("paran B");
break;
case '0':
//DISPLAY_ERROR("paran 0");
break;
case '1':
//DISPLAY_ERROR("paran 1");
break;
case '2':
//DISPLAY_ERROR("paran 2");
break;
default:
(*i)--;
break;
}
return TRUE;
}
/*------------------------------------------------------------------------------
-- FUNCTION: ProcessFont
--
-- DATE: Oct 17, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Marcel Vangrootheest
--
-- PROGRAMMER: Marcel Vangrootheest
--
-- INTERFACE: VOID ProcessFont(HWND hWnd)
-- hWnd - Handle to the window
--
-- RETURNS: VOID
--
-- NOTES:
-- Process a Font change from a sequence ending in m.
-- Changes color information based on color array defined in
-- Application.h. Style is set to 1 for underline.
------------------------------------------------------------------------------*/
VOID ProcessFont(HWND hWnd) {
UINT i = 0;
UINT temp = 0;
PWNDDATA pwd = NULL;
pwd = (PWNDDATA) GetWindowLongPtr(hWnd, 0);
for (i = 1; i <= ESC_VAL(0); i++) {
switch (ESC_VAL(i)) {
case 0: // default
CUR_FG_COLOR = 7;
CUR_BG_COLOR = 0;
CUR_STYLE = 0;
break;
case 1: // bright
if (CUR_FG_COLOR <= 7)
CUR_FG_COLOR += 8;
BRIGHTNESS = 8;
break;
case 2: // dim
if (CUR_FG_COLOR > 7)
CUR_FG_COLOR -= 8;
BRIGHTNESS = 0;
break;
case 4: // underline
CUR_STYLE = 1;
break;
case 5: // blink
// not implemented
break;
case 7: // inverse
temp = CUR_FG_COLOR;
CUR_FG_COLOR = CUR_BG_COLOR;
CUR_BG_COLOR = temp;
break;
case 8: // hide
CUR_FG_COLOR = CUR_BG_COLOR;
break;
default: // set colors by value
if (ESC_VAL(i) / 10 == 3)
CUR_FG_COLOR = (ESC_VAL(i) % 10) + BRIGHTNESS;
if (ESC_VAL(i) / 10 == 4)
CUR_BG_COLOR = (ESC_VAL(i) % 10) + BRIGHTNESS;
}
}
}