]> gitweb.ps.run Git - iftint/blob - main2.c
fix peekchar input
[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 vt100GetScreenSize(int * v, int * h) {
82 #ifdef _WIN32
83     CONSOLE_SCREEN_BUFFER_INFO csbi;
84     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
85     *h = csbi.srWindow.Right - csbi.srWindow.Left + 1;
86     *v = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
87 #else
88     struct winsize w;
89     ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
90     *h = w.ws_row;
91     *v = w.ws_col;
92 #endif
93 }
94
95
96 // JSON
97
98 typedef enum {
99     JSONNodeKind_Nul,
100     JSONNodeKind_Int,
101     JSONNodeKind_Str,
102     JSONNodeKind_Obj,
103     JSONNodeKind_Arr,
104 } JSONNodeKind;
105
106 struct JSONNode;
107 typedef struct JSONNode {
108     JSONNodeKind kind;
109     size_t data;
110     struct JSONNode * parent;
111     struct JSONNode * children;
112     struct JSONNode * next;
113 } JSONNode;
114
115 JSONNode *
116 JSONNodeNew(JSONNodeKind kind, size_t data) {
117     JSONNode * result = NEW(JSONNode);
118     result->kind = kind;
119     result->data = data;
120     return result;
121 }
122
123 JSONNode *
124 JSONNodeNewNul() {
125     return JSONNodeNew(JSONNodeKind_Nul, (size_t)NULL);
126 }
127
128 JSONNode *
129 JSONNodeNewInt(int i) {
130     return JSONNodeNew(JSONNodeKind_Int, (size_t)i);
131 }
132
133 JSONNode *
134 JSONNodeNewStr(const char * str) {
135     return JSONNodeNew(JSONNodeKind_Str, (size_t)str);
136 }
137
138 JSONNode *
139 JSONNodeNewObj() {
140     return JSONNodeNew(JSONNodeKind_Obj, (size_t)NULL);
141 }
142
143 JSONNode *
144 JSONNodeNewArr() {
145     return JSONNodeNew(JSONNodeKind_Arr, (size_t)NULL);
146 }
147
148 JSONNode *
149 JSONNodePush(JSONNode * this, JSONNode * that) {
150     if (this->children == NULL) {
151         this->children = that;
152     }
153     else {
154         JSONNode * lastNode = this->children;
155         while (lastNode->next != NULL)
156             lastNode = lastNode->next;
157         lastNode->next = that;
158     }
159     that->parent = this;
160     that->next = NULL;
161     
162     return that;
163 }
164
165 void
166 JSONNodePop(JSONNode * this) {
167     if (this != NULL) {
168         JSONNode * ptr = this->children;
169
170         if (ptr == NULL) { // no children
171             JSONNodePop(this->parent);
172         }
173         else if (ptr->next == NULL) { // one child
174             this->children = NULL;
175             
176         }
177         else { // more than one child
178             while (ptr->next->next != NULL)
179                 ptr = ptr->next;
180             ptr->next = NULL;
181         }
182     }
183 }
184
185 void
186 Indent(int indent) {
187     for (int i = 0; i < indent; i++)
188         printf("  ");
189 }
190
191 void
192 JSONNodePrint(JSONNode * node) {
193     if (node == NULL)
194         return;
195     
196     static int indent;
197     if (node->parent == NULL)
198         indent = 0;
199     
200     switch (node->kind) {
201     case JSONNodeKind_Nul: {
202         printf("null");
203         break;
204     }
205     case JSONNodeKind_Int: {
206         int i = (int)node->data;
207         printf("%d", i);
208         break;
209     }
210     case JSONNodeKind_Str: {
211         char * str = (char *)node->data;
212         printf("\"%s\"", str == NULL ? "" : str);
213         break;
214     }
215     case JSONNodeKind_Obj: {
216         printf("{\n");
217         JSONNode * ptr = node->children;
218         indent++;
219         while (ptr != NULL) {
220             char * key = (char *)ptr->data;
221             JSONNode * value = ptr->next;
222             Indent(indent);
223             printf("\"%s\": ", key);
224             JSONNodePrint(value);
225             if (ptr->next != NULL)
226                 ptr = ptr->next->next;
227             else
228                 ptr = NULL;
229             printf("%s\n", (ptr == NULL ? "" : ","));
230         }
231         indent--;
232         Indent(indent);
233         printf("}");
234         break;
235     }
236     case JSONNodeKind_Arr: {
237         printf("[ ");
238         JSONNode * ptr = node->children;
239         while (ptr != NULL) {
240             JSONNode * value = ptr;
241             JSONNodePrint(value);
242             ptr = ptr->next;
243             printf("%s", (ptr == NULL ? "" : ", "));
244         }
245         printf(" ]");
246         break;
247     }
248     }
249 }
250
251
252 // Input
253
254 JSONNode * g_DrawNode = NULL;
255 const char * g_DrawStr = "";
256
257 void
258 Draw(void) {
259     vt100ClearScreen();
260     vt100CursorHome();
261     
262     if (g_DrawNode != NULL)
263         JSONNodePrint(g_DrawNode);
264
265     int v, h;
266     vt100GetScreenSize(&v, &h);
267     vt100CursorPos(v, 0);
268     printf("> %s", g_DrawStr);
269
270     vt100CursorPos(v, strlen(g_DrawStr) + 3);
271 }
272
273 int
274 GetChar() {
275     Draw();
276     int c = getch();
277     return c;
278 }
279
280 int
281 PeekChar() {
282     Draw();
283     int c = peekch();
284     return c;
285 }
286
287 int
288 GetInt() {
289     static char intStr[16];
290     intStr[0] = '\0';
291     int intStrLen = 0;
292     int result = 0;
293     int c;
294     g_DrawStr = intStr;
295     while ((c = GetChar()), (c != '\r') && (c != '\n')) {
296         if ((c == 8 || c == 127) && intStrLen > 0) {
297             intStrLen--;
298             intStr[intStrLen] = '\0';
299             result /= 10;
300         }
301         else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) {
302             intStr[intStrLen++] = c;
303             intStr[intStrLen] = '\0';
304             result *= 10;
305             result += c - '0';
306         }
307     }
308     g_DrawStr = "";
309     return result;
310 }
311
312 char *
313 GetStr() {
314     char * str = NEWARR(char, 16);
315     int strLen = 0;
316     int c;
317     g_DrawStr = str;
318     while ((c = GetChar()), (c != '\r') && (c != '\n')) {
319         if ((c == 8 || c == 127) && strLen > 0) {
320             strLen--;
321             str[strLen] = '\0';
322         }
323         else if (strLen < 16 - 1) {
324             str[strLen++] = c;
325             str[strLen] = '\0';
326         }
327     }
328     g_DrawStr = "";
329     return str;
330 }
331
332 JSONNode *
333 GetNode(JSONNode * parent) {
334     int c = GetChar();
335
336     JSONNode * result = JSONNodeNewNul();
337     
338     if (parent == NULL)
339         g_DrawNode = result;
340
341     if (parent != NULL && result != NULL)
342         JSONNodePush(parent, result);
343
344     switch (c) {
345     case 'i': {
346         result->kind = JSONNodeKind_Int;
347         result->data = (size_t)GetInt();
348         break;
349     }
350     case 's': {
351         result->kind = JSONNodeKind_Str;
352         result->data = (size_t)GetStr();
353         break;
354     }
355     case 'o': {
356         result->kind = JSONNodeKind_Obj;
357         while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
358             JSONNodePush(result, JSONNodeNewStr(GetStr()));
359
360             JSONNodePush(result, GetNode(result));
361         }
362         GetChar();
363         break;
364     }
365     case 'a': {
366         result->kind = JSONNodeKind_Arr;
367         while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
368             JSONNodePush(result, GetNode(result));
369         }
370         GetChar();
371         break;
372     }
373     case 8:
374     case 127:
375         JSONNodePop(parent);
376         result = GetNode(parent);
377         break;
378     case 't':
379         result->kind = JSONNodeKind_Int;
380         result->data = (size_t)GetChar();
381         break;
382     }
383
384     return result;
385 }
386
387
388
389
390 int main() {
391     Draw();
392
393     JSONNode * n = GetNode(NULL);
394     //JSONNode * n = TestNode();
395
396     vt100ClearScreen();
397     vt100CursorHome();
398     JSONNodePrint(n);
399     printf("\n");
400
401     // JSONFree(n);
402
403     return 0;
404 }