Modules/Ext2 - Fixed zero-length filenames and memory leaks
[tpg/acess2.git] / KernelLand / Kernel / drv / vterm_vt100.c
1 /*
2  * Acess2 Kernel
3  * - By John Hodge (thePowersGang)
4  *
5  * drv/vterm_vt100.c
6  * - Virtual Terminal - VT100 (Kinda) Emulation
7  */
8 #include "vterm.h"
9
10 // === CONSTANTS ===
11 const Uint16    caVT100Colours[] = {
12                 // Black, Red, Green, Yellow, Blue, Purple, Cyan, Gray
13                 // Same again, but bright
14                 VT_COL_BLACK, 0x700, 0x070, 0x770, 0x007, 0x707, 0x077, 0xAAA,
15                 VT_COL_GREY, 0xF00, 0x0F0, 0xFF0, 0x00F, 0xF0F, 0x0FF, VT_COL_WHITE
16         };
17
18 // === CODE ===
19 /**
20  * \brief Handle a standard large escape code
21  * 
22  * Handles any escape code of the form \x1B[n,...A where n is an integer
23  * and A is any letter.
24  */
25 void VT_int_ParseEscape_StandardLarge(tVTerm *Term, char CmdChar, int argc, int *args)
26 {
27          int    tmp = 1;
28         switch(CmdChar)
29         {
30         // Left
31         case 'D':
32                 tmp = -1;
33         // Right
34         case 'C':
35                 if(argc == 1)   tmp *= args[0];
36                 if( Term->Flags & VT_FLAG_ALTBUF )
37                 {
38                         if( (Term->AltWritePos + tmp) % Term->TextWidth == 0 ) {
39                                 Term->AltWritePos -= Term->AltWritePos % Term->TextWidth;
40                                 Term->AltWritePos += Term->TextWidth - 1;
41                         }
42                         else
43                                 Term->AltWritePos += tmp;
44                 }
45                 else
46                 {
47                         if( (Term->WritePos + tmp) % Term->TextWidth == 0 ) {
48                                 Term->WritePos -= Term->WritePos % Term->TextWidth;
49                                 Term->WritePos += Term->TextWidth - 1;
50                         }
51                         else
52                                 Term->WritePos += tmp;
53                 }
54                 break;
55         
56         // Erase
57         case 'J':
58                 switch(args[0])
59                 {
60                 case 0: // Erase below
61                         break;
62                 case 1: // Erase above
63                         break;
64                 case 2: // Erase all
65                         if( Term->Flags & VT_FLAG_ALTBUF )
66                         {
67                                  int    i = Term->TextHeight;
68                                 while( i-- )    VT_int_ClearLine(Term, i);
69                                 Term->AltWritePos = 0;
70                                 VT_int_UpdateScreen(Term, 1);
71                         }
72                         else
73                         {
74                                  int    i = Term->TextHeight * (giVT_Scrollback + 1);
75                                 while( i-- )    VT_int_ClearLine(Term, i);
76                                 Term->WritePos = 0;
77                                 Term->ViewPos = 0;
78                                 VT_int_UpdateScreen(Term, 1);
79                         }
80                         break;
81                 }
82                 break;
83         
84         // Erase in line
85         case 'K':
86                 switch(args[0])
87                 {
88                 case 0: // Erase to right
89                         if( Term->Flags & VT_FLAG_ALTBUF )
90                         {
91                                  int    i, max;
92                                 max = Term->Width - Term->AltWritePos % Term->Width;
93                                 for( i = 0; i < max; i ++ )
94                                         Term->AltBuf[Term->AltWritePos+i].Ch = 0;
95                         }
96                         else
97                         {
98                                  int    i, max;
99                                 max = Term->Width - Term->WritePos % Term->Width;
100                                 for( i = 0; i < max; i ++ )
101                                         Term->Text[Term->WritePos+i].Ch = 0;
102                         }
103                         VT_int_UpdateScreen(Term, 0);
104                         break;
105                 case 1: // Erase to left
106                         if( Term->Flags & VT_FLAG_ALTBUF )
107                         {
108                                  int    i = Term->AltWritePos % Term->Width;
109                                 while( i -- )
110                                         Term->AltBuf[Term->AltWritePos++].Ch = 0;
111                         }
112                         else
113                         {
114                                  int    i = Term->WritePos % Term->Width;
115                                 while( i -- )
116                                         Term->Text[Term->WritePos++].Ch = 0;
117                         }
118                         VT_int_UpdateScreen(Term, 0);
119                         break;
120                 case 2: // Erase all
121                         if( Term->Flags & VT_FLAG_ALTBUF )
122                         {
123                                 VT_int_ClearLine(Term, Term->AltWritePos / Term->Width);
124                         }
125                         else
126                         {
127                                 VT_int_ClearLine(Term, Term->WritePos / Term->Width);
128                         }
129                         VT_int_UpdateScreen(Term, 0);
130                         break;
131                 }
132                 break;
133         
134         // Set cursor position
135         case 'H':
136                 if( Term->Flags & VT_FLAG_ALTBUF )
137                         Term->AltWritePos = args[0] + args[1]*Term->TextWidth;
138                 else
139                         Term->WritePos = args[0] + args[1]*Term->TextWidth;
140                 //Log_Debug("VTerm", "args = {%i, %i}", args[0], args[1]);
141                 break;
142         
143         // Scroll up `n` lines
144         case 'S':
145                 tmp = -1;
146         // Scroll down `n` lines
147         case 'T':
148                 if(argc == 1)   tmp *= args[0];
149                 if( Term->Flags & VT_FLAG_ALTBUF )
150                         VT_int_ScrollText(Term, tmp);
151                 else
152                 {
153                         if(Term->ViewPos/Term->TextWidth + tmp < 0)
154                                 break;
155                         if(Term->ViewPos/Term->TextWidth + tmp  > Term->TextHeight * (giVT_Scrollback + 1))
156                                 break;
157                         
158                         Term->ViewPos += Term->TextWidth*tmp;
159                 }
160                 break;
161         
162         // Set Font flags
163         case 'm':
164                 for( ; argc--; )
165                 {
166                          int    colour_idx;
167                         // Flags
168                         if( 0 <= args[argc] && args[argc] <= 8)
169                         {
170                                 switch(args[argc])
171                                 {
172                                 case 0: Term->CurColour = DEFAULT_COLOUR;       break;  // Reset
173                                 case 1: Term->CurColour |= 0x80000000;  break;  // Bright
174                                 case 2: Term->CurColour &= ~0x80000000; break;  // Dim
175                                 }
176                         }
177                         // Foreground Colour
178                         else if(30 <= args[argc] && args[argc] <= 37) {
179                                 // Get colour index, accounting for bright bit
180                                 colour_idx = args[argc]-30 + ((Term->CurColour>>28) & 8);
181                                 Term->CurColour &= 0x8000FFFF;
182                                 Term->CurColour |= (Uint32)caVT100Colours[ colour_idx ] << 16;
183                         }
184                         // Background Colour
185                         else if(40 <= args[argc] && args[argc] <= 47) {
186                                 // Get colour index, accounting for bright bit
187                                 colour_idx = args[argc]-40 + ((Term->CurColour>>12) & 8);
188                                 Term->CurColour &= 0xFFFF8000;
189                                 Term->CurColour |= caVT100Colours[ colour_idx ];
190                         }
191                         else {
192                                 Log_Warning("VTerm", "Unknown font flag %i", args[argc]);
193                         }
194                 }
195                 break;
196         
197         // Set scrolling region
198         case 'r':
199                 if( argc != 2 ) break;
200                 Term->ScrollTop = args[0];
201                 Term->ScrollHeight = args[1] - args[0];
202                 break;
203         
204         default:
205                 Log_Warning("VTerm", "Unknown control sequence '\\x1B[%c'", CmdChar);
206                 break;
207         }
208 }
209
210 /**
211  * \fn int VT_int_ParseEscape(tVTerm *Term, const char *Buffer)
212  * \brief Parses a VT100 Escape code
213  */
214 int VT_int_ParseEscape(tVTerm *Term, const char *Buffer)
215 {
216         char    c;
217          int    argc = 0, j = 1;
218          int    args[6] = {0,0,0,0};
219          int    bQuestionMark = 0;
220         
221         switch(Buffer[0])
222         {
223         //Large Code
224         case '[':
225                 // Get Arguments
226                 c = Buffer[j++];
227                 if(c == '?') {
228                         bQuestionMark = 1;
229                         c = Buffer[j++];
230                 }
231                 if( '0' <= c && c <= '9' )
232                 {
233                         do {
234                                 if(c == ';')    c = Buffer[j++];
235                                 while('0' <= c && c <= '9') {
236                                         args[argc] *= 10;
237                                         args[argc] += c-'0';
238                                         c = Buffer[j++];
239                                 }
240                                 argc ++;
241                         } while(c == ';');
242                 }
243                 
244                 // Get Command
245                 if( ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
246                 {
247                         if( bQuestionMark )
248                         {
249                                 switch(c)
250                                 {
251                                 // DEC Private Mode Set
252                                 case 'h':
253                                         if(argc != 1)   break;
254                                         switch(args[0])
255                                         {
256                                         case 25:
257                                                 Term->Flags &= ~VT_FLAG_HIDECSR;
258                                                 break;
259                                         case 1047:
260                                                 VT_int_ToggleAltBuffer(Term, 1);
261                                                 break;
262                                         }
263                                         break;
264                                 case 'l':
265                                         if(argc != 1)   break;
266                                         switch(args[0])
267                                         {
268                                         case 25:
269                                                 Term->Flags |= VT_FLAG_HIDECSR;
270                                                 break;
271                                         case 1047:
272                                                 VT_int_ToggleAltBuffer(Term, 0);
273                                                 break;
274                                         }
275                                         break;
276                                 default:
277                                         Log_Warning("VTerm", "Unknown control sequence '\\x1B[?%c'", c);
278                                         break;
279                                 }
280                         }
281                         else
282                         {
283                                 VT_int_ParseEscape_StandardLarge(Term, c, argc, args);
284                         }
285                 }
286                 break;
287                 
288         default:
289                 Log_Notice("VTerm", "TODO: Handle short escape codes");
290                 break;
291         }
292         
293         //Log_Debug("VTerm", "j = %i, Buffer = '%s'", j, Buffer);
294         return j;
295 }

UCC git Repository :: git.ucc.asn.au