Usermode/Applications - Added -Wall to make config
[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                 Display_MoveCursor(-1, 0);
71                 Display_AddText(1, " ");
72                 Display_MoveCursor(-1, 0);
73                 // TODO: Need to handle \t and ^A-Z
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                 Display_MoveCursor(INT_MIN, 0);
83                 return 1;
84         }
85
86          int    ret = 0;
87         while( ret < Len )
88         {
89                 if( *Buf == '\n' )
90                         break;
91                 if( *Buf == '\x1b' )
92                         break;
93                 ret ++;
94                 Buf ++;
95         }
96         return -ret;
97 }
98
99 int Term_HandleVT100_Long(int Len, const char *Buffer)
100 {
101         char    c;
102          int    argc = 0, j = 0;
103          int    args[6] = {0,0,0,0,0,0};
104          int    bQuestionMark = 0;
105         
106         // Get Arguments
107         if(j == Len)    return 0;
108         c = Buffer[j++];
109         if(c == '?') {
110                 bQuestionMark = 1;
111                 if(j == Len)    return 0;
112                 c = Buffer[j++];
113         }
114         if( '0' <= c && c <= '9' )
115         {
116                 do {
117                         if(c == ';') {
118                                 if(j == Len)    return 0;
119                                 c = Buffer[j++];
120                         }
121                         while('0' <= c && c <= '9') {
122                                 args[argc] *= 10;
123                                 args[argc] += c-'0';
124                                 if(j == Len)    return 0;
125                                 c = Buffer[j++];
126                         }
127                         argc ++;
128                 } while(c == ';');
129         }
130         
131         // Get Command
132         if( !isalpha(c) ) {
133                 // Bother.
134                 _SysDebug("Unexpected char 0x%x in VT100 escape code", c);
135                 return 1;
136         }
137
138         if( bQuestionMark )
139         {
140                 // Special commands
141                 switch( c )
142                 {
143                 default:
144                         _SysDebug("Unknown VT100 extended escape char 0x%x", c);
145                         break;
146                 }
147         }
148         else
149         {
150                 // Standard commands
151                 switch( c )
152                 {
153                 case 'J':
154                         if( argc == 0 )
155                                 Display_ClearLine(0);
156                         else if( args[0] == 2 )
157                                 Display_ClearLines(0);  // Entire screen!
158                         else
159                                 _SysDebug("TODO: VT100 %i J", args[0]);
160                         break;
161                 case 'm':
162                         if( argc == 0 )
163                         {
164                                 // Reset
165                         }
166                         else
167                         {
168                                 int i;
169                                 for( i = 0; i < argc; i ++ )
170                                 {
171                                         if( args[i] < 8 )
172                                         {
173                                                 // TODO: Flags?
174                                         }
175                                         else if( 30 <= args[i] && args[i] <= 37 )
176                                         {
177                                                 // TODO: Bold/bright
178                                                 Display_SetForeground( caVT100Colours[ args[i]-30 ] );
179                                         } 
180                                         else if( 40 <= args[i] && args[i] <= 47 )
181                                         {
182                                                 // TODO: Bold/bright
183                                                 Display_SetBackground( caVT100Colours[ args[i]-30 ] );
184                                         } 
185                                 }
186                         }
187                         break;
188                 default:
189                         _SysDebug("Unknown VT100 escape char 0x%x", c);
190                         break;
191                 }
192         }
193         return j;
194 }

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