8 - whitelist input on GetStr/GetInt
13 #define NEW(TYPE) ((TYPE *)calloc(1, sizeof(TYPE)))
14 #define NEWARR(TYPE, NUM) ((TYPE *)calloc(NUM, sizeof(TYPE)))
23 #include <sys/ioctl.h>
28 /* reads from keypress, doesn't echo */
31 struct termios oldattr, newattr;
33 tcgetattr( STDIN_FILENO, &oldattr );
35 newattr.c_lflag &= ~( ICANON | ECHO ); // no ECHO for echo(?)
36 tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
38 tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
45 struct termios oldattr, newattr;
46 tcgetattr( STDIN_FILENO, &oldattr );
48 newattr.c_lflag &= ~( ICANON | ECHO );
49 tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
51 tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
68 void vt100Escape(const char * str, ...) {
72 printf("%c", ASCII_ESC);
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) {
86 // while ((c = getch()) != ';')
87 // *v = (10*(*v)+(c-'0'));
88 // while ((c = getch()) != 'R')
89 // *h = (10*(*h)+(c-'0'));
91 void vt100GetScreenSize(int * v, int * h) {
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;
99 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
104 void vt100EnableAlternateBuffer() { vt100Escape("[?1049h"); }
105 void vt100DisableAlternateBuffer() { vt100Escape("[?1049l"); }
120 typedef struct JSONNode {
123 struct JSONNode * parent;
124 struct JSONNode * children;
125 struct JSONNode * next;
129 JSONNodeNew(JSONNodeKind kind, size_t data) {
130 JSONNode * result = NEW(JSONNode);
138 return JSONNodeNew(JSONNodeKind_Nul, (size_t)NULL);
142 JSONNodeNewInt(int i) {
143 return JSONNodeNew(JSONNodeKind_Int, (size_t)i);
147 JSONNodeNewStr(const char * str) {
148 return JSONNodeNew(JSONNodeKind_Str, (size_t)str);
153 return JSONNodeNew(JSONNodeKind_Obj, (size_t)NULL);
158 return JSONNodeNew(JSONNodeKind_Arr, (size_t)NULL);
162 JSONNodePush(JSONNode * this, JSONNode * that) {
163 if (this->children == NULL) {
164 this->children = that;
167 JSONNode * lastNode = this->children;
168 while (lastNode->next != NULL)
169 lastNode = lastNode->next;
170 lastNode->next = that;
179 JSONNodePop(JSONNode * this) {
181 JSONNode * ptr = this->children;
183 if (ptr == NULL) { // no children
184 JSONNodePop(this->parent);
186 else if (ptr->next == NULL) { // one child
187 this->children = NULL;
190 else { // more than one child
191 while (ptr->next->next != NULL)
200 for (int i = 0; i < indent; i++)
205 JSONNodePrint(JSONNode * node, JSONNode * currNode) {
210 if (node->parent == NULL)
213 switch (node->kind) {
214 case JSONNodeKind_Nul: {
218 case JSONNodeKind_Int: {
219 char * str = (char *)node->data;
220 printf("%s", (str == NULL || strlen(str) == 0) ? "0" : str);
223 case JSONNodeKind_Str: {
224 char * str = (char *)node->data;
225 printf("\"%s\"", str == NULL ? "" : str);
228 case JSONNodeKind_Obj: {
230 JSONNode * ptr = node->children;
232 while (ptr != NULL) {
233 JSONNode * key = ptr;
234 JSONNode * value = ptr->next;
236 JSONNodePrint(key, currNode);
238 JSONNodePrint(value, currNode);
239 if (ptr->next != NULL)
240 ptr = ptr->next->next;
243 printf("%s\n", (ptr == NULL ? "" : ","));
250 case JSONNodeKind_Arr: {
252 JSONNode * ptr = node->children;
253 while (ptr != NULL) {
254 JSONNode * value = ptr;
255 JSONNodePrint(value, currNode);
257 printf("%s", (ptr == NULL ? "" : ", "));
264 if (currNode == node) {
272 JSONNode * g_DrawNode = NULL;
273 JSONNode * g_CurrNode = NULL;
280 if (g_DrawNode != NULL) {
281 JSONNodePrint(g_DrawNode, g_CurrNode);
282 vt100RestoreCursor();
300 typedef bool(*CharPredicateFunc)(char, int);
302 bool predStr(char c, int i) { return c >= 'a' && c <= 'z'; }
303 bool predInt(char c, int i) { return c >= '0' && c <= '9'; }
306 GetStr(JSONNode * node, CharPredicateFunc charPredicate) {
307 node->data = (size_t)NEWARR(char, 16);
310 char * str = (char *)node->data;
313 while ((c = GetChar()), (c != '\r') && (c != '\n')) {
314 if ((c == 8 || c == 127) && strLen > 0) {
318 else if (strLen < 16 - 1 && charPredicate(c, strLen)) {
327 GetNode(JSONNode * parent, JSONNode * node) {
335 if (parent != NULL && node != NULL)
336 JSONNodePush(parent, node);
340 node->kind = JSONNodeKind_Int;
341 GetStr(node, predInt);
345 node->kind = JSONNodeKind_Str;
346 GetStr(node, predStr);
350 node->kind = JSONNodeKind_Obj;
351 while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
354 newNode = JSONNodeNewStr("");
355 g_CurrNode = newNode;
356 JSONNodePush(node, newNode);
357 GetStr(newNode, predStr);
359 newNode = JSONNodeNewNul();
360 JSONNodePush(node, newNode);
361 GetNode(node, newNode);
368 node->kind = JSONNodeKind_Arr;
369 while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
372 newNode = JSONNodeNewNul();
373 //g_CurrNode = newNode;
374 JSONNodePush(node, newNode);
375 GetNode(node, newNode);
385 JSONNode * newNode = JSONNodeNewNul();
386 GetNode(parent, newNode);
390 node->kind = JSONNodeKind_Int;
391 node->data = (size_t)GetChar();
400 vt100EnableAlternateBuffer();
404 JSONNode * n = JSONNodeNewNul();
406 //JSONNode * n = TestNode();
410 JSONNodePrint(n, NULL);
415 vt100DisableAlternateBuffer();