PatchworkOS
Loading...
Searching...
No Matches
ansi.c
Go to the documentation of this file.
1#include "ansi.h"
2
3#include <errno.h>
4#include <string.h>
5
6void ansi_init(ansi_t* ansi)
7{
8 memset(ansi, 0, sizeof(ansi_t));
9}
10
11uint64_t ansi_parse(ansi_t* ansi, char input, ansi_result_t* result)
12{
13 result->type = ANSI_STILL_PARSING;
14
15 ansi->buffer[ansi->length++] = input;
16 ansi->buffer[ansi->length] = '\0';
17 if (ansi->length >= ANSI_MAX_LENGTH - 1)
18 {
19 ansi->length = 0;
20 return ERR;
21 }
22
23 if (ansi->length == 1)
24 {
25 if (ansi->buffer[0] >= 32 && ansi->buffer[0] <= 126)
26 {
27 result->type = ANSI_PRINTABLE;
28 result->printable = ansi->buffer[0];
29 ansi->length = 0;
30 return 0;
31 }
32
33 switch (ansi->buffer[0])
34 {
35 case '\033':
36 return 0;
37 case '\b':
38 result->type = ANSI_BACKSPACE;
39 ansi->length = 0;
40 return 0;
41 case '\n':
42 result->type = ANSI_NEWLINE;
43 ansi->length = 0;
44 return 0;
45 case '\t':
46 result->type = ANSI_TAB;
47 ansi->length = 0;
48 return 0;
49 case '\003':
50 result->type = ANSI_CTRL_C;
51 ansi->length = 0;
52 return 0;
53 default:
54 ansi->length = 0;
55 return ERR;
56 }
57 }
58
59 if (ansi->length == 2)
60 {
61 if (ansi->buffer[1] == '[')
62 {
63 return 0;
64 }
65 else
66 {
67 ansi->length = 0;
68 return ERR;
69 }
70 }
71
72 if (ansi->length == 3)
73 {
74 if (strcmp(ansi->buffer, "\033[A") == 0)
75 {
76 result->type = ANSI_ARROW_UP;
77 ansi->length = 0;
78 return 0;
79 }
80 else if (strcmp(ansi->buffer, "\033[B") == 0)
81 {
82 result->type = ANSI_ARROW_DOWN;
83 ansi->length = 0;
84 return 0;
85 }
86 else if (strcmp(ansi->buffer, "\033[C") == 0)
87 {
88 result->type = ANSI_ARROW_RIGHT;
89 ansi->length = 0;
90 return 0;
91 }
92 else if (strcmp(ansi->buffer, "\033[D") == 0)
93 {
94 result->type = ANSI_ARROW_LEFT;
95 ansi->length = 0;
96 return 0;
97 }
98 }
99
100 if (ansi->length == 4)
101 {
102 if (strcmp(ansi->buffer, "\033[3~") == 0)
103 {
104 result->type = ANSI_DELETE;
105 ansi->length = 0;
106 return 0;
107 }
108 else if (strcmp(ansi->buffer, "\033[5~") == 0)
109 {
110 result->type = ANSI_PAGE_UP;
111 ansi->length = 0;
112 return 0;
113 }
114 else if (strcmp(ansi->buffer, "\033[6~") == 0)
115 {
116 result->type = ANSI_PAGE_DOWN;
117 ansi->length = 0;
118 return 0;
119 }
120 else if (strcmp(ansi->buffer, "\033[7~") == 0)
121 {
122 result->type = ANSI_HOME;
123 ansi->length = 0;
124 return 0;
125 }
126 else if (strcmp(ansi->buffer, "\033[8~") == 0)
127 {
128 result->type = ANSI_END;
129 ansi->length = 0;
130 return 0;
131 }
132 // TODO: Function keys.
133 }
134
135 ansi->length = 0;
136 return ERR;
137}
void ansi_init(ansi_t *ansi)
Definition ansi.c:6
uint64_t ansi_parse(ansi_t *ansi, char input, ansi_result_t *result)
Definition ansi.c:11
#define ERR
Integer error value.
Definition ERR.h:17
@ ANSI_BACKSPACE
Definition ansi.h:20
@ ANSI_PAGE_UP
Definition ansi.h:27
@ ANSI_CTRL_C
Definition ansi.h:32
@ ANSI_HOME
Definition ansi.h:29
@ ANSI_PAGE_DOWN
Definition ansi.h:28
@ ANSI_ARROW_DOWN
Definition ansi.h:24
@ ANSI_NEWLINE
Definition ansi.h:21
@ ANSI_END
Definition ansi.h:30
@ ANSI_TAB
Definition ansi.h:22
@ ANSI_STILL_PARSING
Definition ansi.h:18
@ ANSI_PRINTABLE
Definition ansi.h:19
@ ANSI_ARROW_UP
Definition ansi.h:23
@ ANSI_DELETE
Definition ansi.h:31
@ ANSI_ARROW_RIGHT
Definition ansi.h:26
@ ANSI_ARROW_LEFT
Definition ansi.h:25
#define ANSI_MAX_LENGTH
Definition ansi.h:6
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
_PUBLIC int strcmp(const char *s1, const char *s2)
Definition strcmp.c:3
_PUBLIC void * memset(void *s, int c, size_t n)
Definition memset.c:4
char printable
Definition ansi.h:38
ansi_result_type_t type
Definition ansi.h:37
Definition ansi.h:9
uint8_t length
Definition ansi.h:11
char buffer[ANSI_MAX_LENGTH]
Definition ansi.h:10