Usermode/GUITerminal - VT100 extended parsing
[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  int    Term_HandleVT100_Long(int Len, const char *Buf);
16
17 static inline int min(int a, int b)
18 {
19         return a < b ? a : b;
20 }
21
22 int Term_HandleVT100(int Len, const char *Buf)
23 {
24         #define MAX_VT100_ESCAPE_LEN    16
25         static char     inc_buf[MAX_VT100_ESCAPE_LEN];
26         static int      inc_len = 0;
27
28         if( inc_len > 0 || *Buf == '\x1b' )
29         {
30                  int    new_bytes = min(MAX_VT100_ESCAPE_LEN - inc_len, Len);
31                  int    ret = 0;
32                 memcpy(inc_buf + inc_len, Buf, new_bytes);
33                 inc_len += new_bytes;
34                 // Handle VT100 (like) escape sequence
35
36                 _SysDebug("inc_len = %i, new_bytes = %i", inc_len, new_bytes);
37
38                 if( inc_len <= 1 )
39                         return 1;       // Skip 1 character (the '\x1b')
40
41                 switch(inc_buf[1])
42                 {
43                 case '[':       // Multibyte, funtime starts    
44                         ret = Term_HandleVT100_Long(inc_len-2, inc_buf+2);
45                         if( ret > 0 )
46                                 ret += 2;
47                         break;
48                 default:
49                         ret = 2;
50                         break;
51                 }       
52
53                 if( ret != 0 )
54                         inc_len = 0;
55                 return ret;
56         }
57
58         switch( *Buf )
59         {
60         case '\b':
61                 // TODO: Backspace
62                 Display_MoveCursor(-1, 0);
63                 return 1;
64         case '\t':
65                 // TODO: tab (get current cursor pos, space until multiple of 8)
66                 return 1;
67         case '\n':
68                 Display_Newline(1);
69                 return 1;
70         case '\r':
71                 // TODO: Carriage return
72                 Display_MoveCursor(INT_MIN, 0);
73                 return 1;
74         }
75
76          int    ret = 0;
77         while( ret < Len )
78         {
79                 if( *Buf == '\n' )
80                         break;
81                 if( *Buf == '\x1b' )
82                         break;
83                 ret ++;
84                 Buf ++;
85         }
86         return -ret;
87 }
88
89 int Term_HandleVT100_Long(int Len, const char *Buffer)
90 {
91         char    c;
92          int    argc = 0, j = 0;
93          int    args[6] = {0,0,0,0,0,0};
94          int    bQuestionMark = 0;
95         
96         // Get Arguments
97         if(j == Len)    return 0;
98         c = Buffer[j++];
99         if(c == '?') {
100                 bQuestionMark = 1;
101                 if(j == Len)    return 0;
102                 c = Buffer[j++];
103         }
104         if( '0' <= c && c <= '9' )
105         {
106                 do {
107                         if(c == ';') {
108                                 if(j == Len)    return 0;
109                                 c = Buffer[j++];
110                         }
111                         while('0' <= c && c <= '9') {
112                                 args[argc] *= 10;
113                                 args[argc] += c-'0';
114                                 if(j == Len)    return 0;
115                                 c = Buffer[j++];
116                         }
117                         argc ++;
118                 } while(c == ';');
119         }
120         
121         // Get Command
122         if( !isalpha(c) ) {
123                 // Bother.
124                 _SysDebug("Unexpected char 0x%x in VT100 escape code", c);
125                 return -1;
126         }
127
128         if( bQuestionMark )
129         {
130                 // Special commands
131                 switch( c )
132                 {
133                 default:
134                         _SysDebug("Unknown VT100 extended escape char 0x%x", c);
135                         break;
136                 }
137         }
138         else
139         {
140                 // Standard commands
141                 switch( c )
142                 {
143                 case 'J':
144                         if( argc == 0 )
145                                 Display_ClearLine(0);
146                         else if( args[0] == 2 )
147                                 Display_ClearLines(0);  // Entire screen!
148                         else
149                                 _SysDebug("TODO: VT100 %i J", args[0]);
150                         break;
151                 default:
152                         _SysDebug("Unknown VT100 escape char 0x%x", c);
153                         break;
154                 }
155         }
156         return j;
157 }

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