Cleaning up clang warnings
[tpg/acess2.git] / Usermode / Applications / gui_shell_src / vt100.c
1 /*
2  * Acess GUI Terminal
3  * - By John Hodge (thePowersGang)
4  *
5  * vt100.c
6  * - VT100/xterm Emulation
7  */
8 #include <string.h>
9 #include <limits.h>
10 #include "include/vt100.h"
11 #include "include/display.h"
12 #include <ctype.h>      // isalpha
13 #include <acess/sys.h>  // _SysDebug
14
15 const uint32_t  caVT100Colours[] = {
16         // Black, Red, Green, Yellow, Blue, Purple, Cyan, Gray
17         // Same again, but bright
18         0x000000, 0x770000, 0x007700, 0x777700, 0x000077, 0x770077, 0x007777, 0xAAAAAAA,
19         0xCCCCCC, 0xFF0000, 0x00FF00, 0xFFFF00, 0x0000FF, 0xFF00FF, 0x00FFFF, 0xFFFFFFF
20 };
21
22  int    Term_HandleVT100_Long(int Len, const char *Buf);
23
24 static inline int min(int a, int b)
25 {
26         return a < b ? a : b;
27 }
28
29 int Term_HandleVT100(int Len, const char *Buf)
30 {
31         #define MAX_VT100_ESCAPE_LEN    16
32         static char     inc_buf[MAX_VT100_ESCAPE_LEN];
33         static int      inc_len = 0;
34
35         if( inc_len > 0 || *Buf == '\x1b' )
36         {
37                 // Handle VT100 (like) escape sequence
38                  int    new_bytes = min(MAX_VT100_ESCAPE_LEN - inc_len, Len);
39                  int    ret = 0, old_inc_len = inc_len;
40                 memcpy(inc_buf + inc_len, Buf, new_bytes);
41
42                 inc_len += new_bytes;
43
44                 if( inc_len <= 1 )
45                         return 1;       // Skip 1 character (the '\x1b')
46
47                 switch(inc_buf[1])
48                 {
49                 case '[':       // Multibyte, funtime starts    
50                         ret = Term_HandleVT100_Long(inc_len-2, inc_buf+2);
51                         if( ret > 0 ) {
52                                 ret += 2;
53                         }
54                         break;
55                 default:
56                         ret = 2;
57                         break;
58                 }       
59
60                 if( ret != 0 ) {
61                         inc_len = 0;
62                         ret -= old_inc_len;     // counter cached bytes
63                 }
64                 return ret;
65         }
66
67         switch( *Buf )
68         {
69         case '\b':
70                 // TODO: Backspace
71                 Display_MoveCursor(-1, 0);
72                 Display_AddText(1, " ");
73                 Display_MoveCursor(-1, 0);
74                 return 1;
75         case '\t':
76                 // TODO: tab (get current cursor pos, space until multiple of 8)
77                 return 1;
78         case '\n':
79                 Display_Newline(1);
80                 return 1;
81         case '\r':
82                 // TODO: Carriage return
83                 Display_MoveCursor(INT_MIN, 0);
84                 return 1;
85         }
86
87          int    ret = 0;
88         while( ret < Len )
89         {
90                 if( *Buf == '\n' )
91                         break;
92                 if( *Buf == '\x1b' )
93                         break;
94                 ret ++;
95                 Buf ++;
96         }
97         return -ret;
98 }
99
100 int Term_HandleVT100_Long(int Len, const char *Buffer)
101 {
102         char    c;
103          int    argc = 0, j = 0;
104          int    args[6] = {0,0,0,0,0,0};
105          int    bQuestionMark = 0;
106         
107         // Get Arguments
108         if(j == Len)    return 0;
109         c = Buffer[j++];
110         if(c == '?') {
111                 bQuestionMark = 1;
112                 if(j == Len)    return 0;
113                 c = Buffer[j++];
114         }
115         if( '0' <= c && c <= '9' )
116         {
117                 do {
118                         if(c == ';') {
119                                 if(j == Len)    return 0;
120                                 c = Buffer[j++];
121                         }
122                         while('0' <= c && c <= '9') {
123                                 args[argc] *= 10;
124                                 args[argc] += c-'0';
125                                 if(j == Len)    return 0;
126                                 c = Buffer[j++];
127                         }
128                         argc ++;
129                 } while(c == ';');
130         }
131         
132         // Get Command
133         if( !isalpha(c) ) {
134                 // Bother.
135                 _SysDebug("Unexpected char 0x%x in VT100 escape code", c);
136                 return 1;
137         }
138
139         if( bQuestionMark )
140         {
141                 // Special commands
142                 switch( c )
143                 {
144                 default:
145                         _SysDebug("Unknown VT100 extended escape char 0x%x", c);
146                         break;
147                 }
148         }
149         else
150         {
151                 // Standard commands
152                 switch( c )
153                 {
154                 case 'J':
155                         if( argc == 0 )
156                                 Display_ClearLine(0);
157                         else if( args[0] == 2 )
158                                 Display_ClearLines(0);  // Entire screen!
159                         else
160                                 _SysDebug("TODO: VT100 %i J", args[0]);
161                         break;
162                 case 'm':
163                         if( argc == 0 )
164                         {
165                                 // Reset
166                         }
167                         else
168                         {
169                                 int i;
170                                 for( i = 0; i < argc; i ++ )
171                                 {
172                                         if( args[i] < 8 )
173                                         {
174                                                 // Flags?
175                                         }
176                                         else if( 30 <= args[i] && args[i] <= 37 )
177                                         {
178                                                 // TODO: Bold/bright
179                                                 Display_SetForeground( caVT100Colours[ args[i]-30 ] );
180                                         } 
181                                         else if( 40 <= args[i] && args[i] <= 47 )
182                                         {
183                                                 // TODO: Bold/bright
184                                                 Display_SetBackground( caVT100Colours[ args[i]-30 ] );
185                                         } 
186                                 }
187                         }
188                         break;
189                 default:
190                         _SysDebug("Unknown VT100 escape char 0x%x", c);
191                         break;
192                 }
193         }
194         return j;
195 }

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