1 /* $Id: ansi.cpp,v 1.1 2001/01/27 22:38:43 ea Exp $
4 * AUTHOR : unknown (sources found on www.telnet.org)
5 * PROJECT : ReactOS Operating System
6 * DESCRIPTION: telnet client for the W32 subsystem
9 * 2001-02-21 ea Modified to compile under 0.0.16 src tree
16 // Need to implement a Keymapper.
17 // here are some example key maps
36 ///////////////////////////////////////////////////////////////////////////////
37 // SET SCREEN ATTRIBUTE
39 ESC [ Ps..Ps m Ps refers to selective parameter. Multiple parameters are
40 separated by the semicolon character (073 octal). The param-
41 eters are executed in order and have the following meaning:
43 0 or none All attributes off
49 3x set foreground color to x
50 nx set background color to x
52 Any other parameters are ignored.
54 static int sa
= FOREGROUND_RED
| FOREGROUND_GREEN
| FOREGROUND_BLUE
;
56 void ansi_set_screen_attribute(char* buffer
)
63 sa
= FOREGROUND_RED
| FOREGROUND_GREEN
| FOREGROUND_BLUE
;
65 case '1': //Hign Intensity
66 sa
|= FOREGROUND_INTENSITY
;
68 case '4': //Underscore
71 sa
|= BACKGROUND_INTENSITY
;
74 sa
= BACKGROUND_RED
| BACKGROUND_GREEN
| BACKGROUND_BLUE
;
80 sa
= sa
& (BACKGROUND_BLUE
| BACKGROUND_GREEN
| BACKGROUND_RED
| BACKGROUND_INTENSITY
) |
81 (*buffer
& 1)?FOREGROUND_RED
:0 |
82 (*buffer
& 2)?FOREGROUND_GREEN
:0 |
83 (*buffer
& 4)?FOREGROUND_BLUE
:0;
88 sa
= sa
& (FOREGROUND_BLUE
| FOREGROUND_GREEN
| FOREGROUND_RED
| FOREGROUND_INTENSITY
) |
89 (*buffer
& 1)?BACKGROUND_RED
:0 |
90 (*buffer
& 2)?BACKGROUND_GREEN
:0 |
91 (*buffer
& 4)?BACKGROUND_BLUE
:0;
96 if(*buffer
&& *buffer
== ';')
99 SetConsoleTextAttribute(StandardOutput
,sa
);
102 ///////////////////////////////////////////////////////////////////////////////
105 ESC [ 0K Same *default*
106 ESC [ 1K Erase from beginning of line to cursor
107 ESC [ 2K Erase line containing cursor
110 void ansi_erase_line(char* buffer
)
115 act
= (*buffer
++) - '0';
118 CONSOLE_SCREEN_BUFFER_INFO csbi
;
119 GetConsoleScreenBufferInfo(StandardOutput
,&csbi
);
126 case 0: //erase to end of line
127 pos
.X
= csbi
.dwCursorPosition
.X
;
128 pos
.Y
= csbi
.dwCursorPosition
.Y
;
129 n
= csbi
.dwSize
.X
- csbi
.dwCursorPosition
.X
;
131 case 1: //erase from beginning
133 pos
.Y
= csbi
.dwCursorPosition
.Y
;
134 n
= csbi
.dwCursorPosition
.X
;
136 case 2: // erase whole line
138 pos
.Y
= csbi
.dwCursorPosition
.Y
;
144 FillConsoleOutputCharacter(StandardOutput
,' ',n
,pos
,&w
);
148 ///////////////////////////////////////////////////////////////////////////////
150 // ESC [ Pl;PcH Direct cursor addressing, where Pl is line#, Pc is column#
153 void ansi_set_position(char* buffer
)
158 while(*buffer
&& *buffer
!= ';')
159 pos
.Y
= pos
.Y
*10 + *buffer
++ - '0';
165 while(*buffer
&& *buffer
!= ';')
166 pos
.X
= pos
.X
*10 + *buffer
++ - '0';
171 SetConsoleCursorPosition(StandardOutput
,pos
);
175 ///////////////////////////////////////////////////////////////////////////////
178 ESC [ 0J Same *default*
179 ESC [ 2J Erase entire screen
182 void ansi_erase_screen(char* buffer
)
187 act
= (*buffer
++) - '0';
190 CONSOLE_SCREEN_BUFFER_INFO csbi
;
191 GetConsoleScreenBufferInfo(StandardOutput
,&csbi
);
199 pos
.X
= csbi
.dwCursorPosition
.X
;
200 pos
.Y
= csbi
.dwCursorPosition
.Y
;
201 n
= csbi
.dwSize
.X
*csbi
.dwSize
.Y
;
206 n
= csbi
.dwSize
.X
*csbi
.dwSize
.Y
;
211 FillConsoleOutputCharacter(StandardOutput
,' ',n
,pos
,&w
);
212 SetConsoleCursorPosition(StandardOutput
,pos
);
215 ///////////////////////////////////////////////////////////////////////////////
217 // ESC [ Pn A Cursor up Pn lines (Pn default=1)
219 void ansi_move_up(char* buffer
)
221 int cnt
= *buffer
?0:1;
224 cnt
= cnt
*10 + (*buffer
++) - '0';
229 CONSOLE_SCREEN_BUFFER_INFO csbi
;
230 GetConsoleScreenBufferInfo(StandardOutput
,&csbi
);
232 pos
.X
= csbi
.dwCursorPosition
.X
;
233 pos
.Y
= ((csbi
.dwCursorPosition
.Y
-cnt
)>=0)?(csbi
.dwCursorPosition
.Y
-cnt
):0;
235 SetConsoleCursorPosition(StandardOutput
,pos
);
238 ///////////////////////////////////////////////////////////////////////////////
240 unsigned char codeptr
;
244 typedef void (*LPCODEPROC
)(char*);
250 } codec
[NUM_CODEC
] = {
251 {'m',ansi_set_screen_attribute
},
252 {'H',ansi_set_position
},
253 {'K',ansi_erase_line
},
254 {'J',ansi_erase_screen
},
259 void ansi(SOCKET server
,unsigned char data
)
261 static _ansi_state state
= as_normal
;
268 case 0: //eat null codes.
273 default: //Send all else to the console.
274 WriteConsole(StandardOutput
,&data
,1,&z
,NULL
);
281 codebuf
[codeptr
] = 0;
287 codebuf
[codeptr
] = 0;
288 for(i
=0; codec
[i
].cmd
&& codec
[i
].cmd
!= data
; i
++);
290 codec
[i
].proc(codebuf
);
295 wsprintf(buf
,"Unknown Ansi code:'%c' (%s)\n",data
,codebuf
);
296 OutputDebugString(buf
);
302 codebuf
[codeptr
++] = data
;