]> gitweb.ps.run Git - iftint/blob - main2.c
86cc7b70e1ccf94df0682d1f1afd5f88e51ebc03
[iftint] / main2.c
1 #include <stdio.h>
2 #include <stdarg.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include <stdbool.h>
6
7 /* TODO
8 - whitelist input on GetStr/GetInt
9 */
10
11 // Memory
12
13 #define NEW(TYPE) ((TYPE *)calloc(1, sizeof(TYPE)))
14 #define NEWARR(TYPE, NUM) ((TYPE *)calloc(NUM, sizeof(TYPE)))
15
16
17 // getch
18
19 #ifdef _WIN32
20 #include <windows.h>
21 #include <conio.h>
22 #else
23 #include <sys/ioctl.h>
24 #include <termios.h>
25 #include <unistd.h>
26 #include <stdio.h>
27
28 /* reads from keypress, doesn't echo */
29 int getch(void)
30 {
31     struct termios oldattr, newattr;
32     int ch;
33     tcgetattr( STDIN_FILENO, &oldattr );
34     newattr = oldattr;
35     newattr.c_lflag &= ~( ICANON | ECHO ); // no ECHO for echo(?)
36     tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
37     ch = getchar();
38     tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
39     return ch;
40 }
41
42 /* ungets keypress */
43 void ungetch(int ch)
44 {
45     struct termios oldattr, newattr;
46     tcgetattr( STDIN_FILENO, &oldattr );
47     newattr = oldattr;
48     newattr.c_lflag &= ~( ICANON | ECHO );
49     tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
50     ungetc(ch, stdin);
51     tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
52 }
53 #endif
54
55 int
56 peekch() {
57     int c = getch();
58     //ungetc(c, stdin);
59     ungetch(c);
60     return c;
61 }
62
63
64 // VT100
65
66 #define ASCII_ESC 27
67
68 void vt100Escape(const char * str, ...) {
69     va_list args;
70     va_start(args, str);
71
72     printf("%c", ASCII_ESC);
73     vprintf(str, args);
74 }
75
76 void vt100ClearScreen() { vt100Escape("[2J"); }
77 void vt100CursorHome() { vt100Escape("[H"); }
78 void vt100CursorPos(int v, int h) { vt100Escape("[%d;%dH", v, h); }
79 void vt100SaveCursor() { vt100Escape("7"); }
80 void vt100RestoreCursor() { vt100Escape("8"); }
81 // void vt100GetCursor(int * v, int * h) {
82 //     *v = *h = 0;
83 //     printf("\033[6n");
84 //     getch(); getch();
85 //     int c;
86 //     while ((c = getch()) != ';')
87 //         *v = (10*(*v)+(c-'0'));
88 //     while ((c = getch()) != 'R')
89 //         *h = (10*(*h)+(c-'0'));
90 // }
91 void vt100GetScreenSize(int * v, int * h) {
92 #ifdef _WIN32
93     CONSOLE_SCREEN_BUFFER_INFO csbi;
94     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
95     *h = csbi.srWindow.Right - csbi.srWindow.Left + 1;
96     *v = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
97 #else
98     struct winsize w;
99     ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
100     *h = w.ws_row;
101     *v = w.ws_col;
102 #endif
103 }
104 void vt100EnableAlternateBuffer() { vt100Escape("[?1049h"); }
105 void vt100DisableAlternateBuffer() { vt100Escape("[?1049l"); }
106
107
108 // JSON
109
110 typedef enum {
111     JSONNodeKind_Nul,
112     JSONNodeKind_Int,
113     JSONNodeKind_Str,
114     JSONNodeKind_Obj,
115     JSONNodeKind_Arr,
116     JSONNodeKind_COUNT
117 } JSONNodeKind;
118
119 struct JSONNode;
120 typedef struct JSONNode {
121     JSONNodeKind kind;
122     size_t data;
123     struct JSONNode * parent;
124     struct JSONNode * children;
125     struct JSONNode * next;
126 } JSONNode;
127
128 JSONNode *
129 JSONNodeNew(JSONNodeKind kind, size_t data) {
130     JSONNode * result = NEW(JSONNode);
131     result->kind = kind;
132     result->data = data;
133     return result;
134 }
135
136 JSONNode *
137 JSONNodeNewNul() {
138     return JSONNodeNew(JSONNodeKind_Nul, (size_t)NULL);
139 }
140
141 JSONNode *
142 JSONNodeNewInt(int i) {
143     return JSONNodeNew(JSONNodeKind_Int, (size_t)i);
144 }
145
146 JSONNode *
147 JSONNodeNewStr(const char * str) {
148     return JSONNodeNew(JSONNodeKind_Str, (size_t)str);
149 }
150
151 JSONNode *
152 JSONNodeNewObj() {
153     return JSONNodeNew(JSONNodeKind_Obj, (size_t)NULL);
154 }
155
156 JSONNode *
157 JSONNodeNewArr() {
158     return JSONNodeNew(JSONNodeKind_Arr, (size_t)NULL);
159 }
160
161 JSONNode *
162 JSONNodePush(JSONNode * this, JSONNode * that) {
163     if (this->children == NULL) {
164         this->children = that;
165     }
166     else {
167         JSONNode * lastNode = this->children;
168         while (lastNode->next != NULL)
169             lastNode = lastNode->next;
170         lastNode->next = that;
171     }
172     that->parent = this;
173     that->next = NULL;
174     
175     return that;
176 }
177
178 void
179 JSONNodePop(JSONNode * this) {
180     if (this != NULL) {
181         JSONNode * ptr = this->children;
182
183         if (ptr == NULL) { // no children
184             JSONNodePop(this->parent);
185         }
186         else if (ptr->next == NULL) { // one child
187             this->children = NULL;
188             
189         }
190         else { // more than one child
191             while (ptr->next->next != NULL)
192                 ptr = ptr->next;
193             ptr->next = NULL;
194         }
195     }
196 }
197
198 void
199 Indent(int indent) {
200     for (int i = 0; i < indent; i++)
201         printf("  ");
202 }
203
204 void
205 JSONNodePrint(JSONNode * node, JSONNode * currNode) {
206     if (node == NULL)
207         return;
208     
209     static int indent;
210     if (node->parent == NULL)
211         indent = 0;
212
213     switch (node->kind) {
214     case JSONNodeKind_Nul: {
215         printf("null");
216         break;
217     }
218     case JSONNodeKind_Int: {
219         char * str = (char *)node->data;
220         printf("%s", (str == NULL || strlen(str) == 0) ? "0" : str);
221         break;
222     }
223     case JSONNodeKind_Str: {
224         char * str = (char *)node->data;
225         printf("\"%s\"", str == NULL ? "" : str);
226         break;
227     }
228     case JSONNodeKind_Obj: {
229         printf("{\n");
230         JSONNode * ptr = node->children;
231         indent++;
232         while (ptr != NULL) {
233             JSONNode * key = ptr;
234             JSONNode * value = ptr->next;
235             Indent(indent);
236             JSONNodePrint(key, currNode);
237             printf(": ");
238             JSONNodePrint(value, currNode);
239             if (ptr->next != NULL)
240                 ptr = ptr->next->next;
241             else
242                 ptr = NULL;
243             printf("%s\n", (ptr == NULL ? "" : ","));
244         }
245         indent--;
246         Indent(indent);
247         printf("}");
248         break;
249     }
250     case JSONNodeKind_Arr: {
251         printf("[ ");
252         JSONNode * ptr = node->children;
253         while (ptr != NULL) {
254             JSONNode * value = ptr;
255             JSONNodePrint(value, currNode);
256             ptr = ptr->next;
257             printf("%s", (ptr == NULL ? "" : ", "));
258         }
259         printf(" ]");
260         break;
261     }
262     }
263
264     if (currNode == node) {
265         vt100SaveCursor();
266     }
267 }
268
269
270 // Input
271
272 JSONNode * g_DrawNode = NULL;
273 JSONNode * g_CurrNode = NULL;
274
275 void
276 Draw(void) {
277     vt100ClearScreen();
278     vt100CursorHome();
279     
280     if (g_DrawNode != NULL) {
281         JSONNodePrint(g_DrawNode, g_CurrNode);
282         vt100RestoreCursor();
283     }
284 }
285
286 int
287 GetChar() {
288     Draw();
289     int c = getch();
290     return c;
291 }
292
293 int
294 PeekChar() {
295     Draw();
296     int c = peekch();
297     return c;
298 }
299
300 typedef bool(*CharPredicateFunc)(char, int);
301
302 bool predStr(char c, int i) { return c >= 'a' && c <= 'z'; }
303 bool predInt(char c, int i) { return c >= '0' && c <= '9'; }
304
305 void
306 GetStr(JSONNode * node, CharPredicateFunc charPredicate) {
307     node->data = (size_t)NEWARR(char, 16);
308     int strLen = 0;
309
310     char * str = (char *)node->data;
311     
312     int c;
313     while ((c = GetChar()), (c != '\r') && (c != '\n')) {
314         if ((c == 8 || c == 127) && strLen > 0) {
315             strLen--;
316             str[strLen] = '\0';
317         }
318         else if (strLen < 16 - 1 && charPredicate(c, strLen)) {
319             str[strLen] = c;
320             strLen++;
321             str[strLen] = '\0';
322         }
323     }
324 }
325
326 void
327 GetNode(JSONNode * parent, JSONNode * node) {
328     int c = GetChar();
329
330     g_CurrNode = node;
331
332     if (parent == NULL)
333         g_DrawNode = node;
334
335     if (parent != NULL && node != NULL)
336         JSONNodePush(parent, node);
337
338     switch (c) {
339     case 'i': {
340         node->kind = JSONNodeKind_Int;
341         GetStr(node, predInt);
342         break;
343     }
344     case 's': {
345         node->kind = JSONNodeKind_Str;
346         GetStr(node, predStr);
347         break;
348     }
349     case 'o': {
350         node->kind = JSONNodeKind_Obj;
351         while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
352             JSONNode * newNode;
353             
354             newNode = JSONNodeNewStr("");
355             g_CurrNode = newNode;
356             JSONNodePush(node, newNode);
357             GetStr(newNode, predStr);
358
359             newNode = JSONNodeNewNul();
360             JSONNodePush(node, newNode);
361             GetNode(node, newNode);
362         }
363             g_CurrNode = node;
364         getch();
365         break;
366     }
367     case 'a': {
368         node->kind = JSONNodeKind_Arr;
369         while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
370             JSONNode * newNode;
371             
372             newNode = JSONNodeNewNul();
373             //g_CurrNode = newNode;
374             JSONNodePush(node, newNode);
375             GetNode(node, newNode);
376         }
377             g_CurrNode = node;
378         getch();
379         break;
380     }
381     /*
382     case 8:
383     case 127:
384         JSONNodePop(parent);
385         JSONNode * newNode = JSONNodeNewNul();
386         GetNode(parent, newNode);
387         break;
388     */
389     case 't':
390         node->kind = JSONNodeKind_Int;
391         node->data = (size_t)GetChar();
392         break;
393     }
394 }
395
396
397
398
399 int main() {
400     vt100EnableAlternateBuffer();
401
402     Draw();
403
404     JSONNode * n = JSONNodeNewNul();
405     GetNode(NULL, n);
406     //JSONNode * n = TestNode();
407
408     vt100ClearScreen();
409     vt100CursorHome();
410     JSONNodePrint(n, NULL);
411     printf("\n");
412
413     // JSONFree(n);
414     
415     vt100DisableAlternateBuffer();
416
417     return 0;
418 }