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);
118 typedef struct JSONNode {
121 struct JSONNode * parent;
122 struct JSONNode * children;
123 struct JSONNode * next;
127 JSONNodeNew(JSONNodeKind kind, size_t data) {
128 JSONNode * result = NEW(JSONNode);
136 return JSONNodeNew(JSONNodeKind_Nul, (size_t)NULL);
140 JSONNodeNewInt(int i) {
141 return JSONNodeNew(JSONNodeKind_Int, (size_t)i);
145 JSONNodeNewStr(const char * str) {
146 return JSONNodeNew(JSONNodeKind_Str, (size_t)str);
151 return JSONNodeNew(JSONNodeKind_Obj, (size_t)NULL);
156 return JSONNodeNew(JSONNodeKind_Arr, (size_t)NULL);
160 JSONNodePush(JSONNode * this, JSONNode * that) {
161 if (this->children == NULL) {
162 this->children = that;
165 JSONNode * lastNode = this->children;
166 while (lastNode->next != NULL)
167 lastNode = lastNode->next;
168 lastNode->next = that;
177 JSONNodePop(JSONNode * this) {
179 JSONNode * ptr = this->children;
181 if (ptr == NULL) { // no children
182 JSONNodePop(this->parent);
184 else if (ptr->next == NULL) { // one child
185 this->children = NULL;
188 else { // more than one child
189 while (ptr->next->next != NULL)
198 for (int i = 0; i < indent; i++)
202 static int currV = 0;
203 static int currH = 0;
206 JSONNodePrint(JSONNode * node, JSONNode * currNode) {
211 if (node->parent == NULL)
214 switch (node->kind) {
215 case JSONNodeKind_Nul: {
219 case JSONNodeKind_Int: {
220 int i = (int)node->data;
224 case JSONNodeKind_Str: {
225 char * str = (char *)node->data;
226 printf("\"%s\"", str == NULL ? "" : str);
229 case JSONNodeKind_Obj: {
231 JSONNode * ptr = node->children;
233 while (ptr != NULL) {
234 char * key = (char *)ptr->data;
235 JSONNode * value = ptr->next;
237 printf("\"%s\": ", key);
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) {
265 int currOffsets[JSONNodeKind_COUNT];
266 currOffsets[JSONNodeKind_Nul] = 0;
267 currOffsets[JSONNodeKind_Int] = 0;
268 currOffsets[JSONNodeKind_Str] = 1;
269 currOffsets[JSONNodeKind_Obj] = 1;
270 currOffsets[JSONNodeKind_Arr] = 2;
271 vt100GetCursor(&currV, &currH);
272 currH -= currOffsets[node->kind];
279 JSONNode * g_DrawNode = NULL;
280 JSONNode * g_CurrNode = NULL;
287 if (g_DrawNode != NULL) {
288 JSONNodePrint(g_DrawNode, g_CurrNode);
289 vt100CursorPos(currV, currH);
308 GetInt(JSONNode * node) {
309 char intStr[16] = "";
312 size_t * i = &node->data;
315 while ((c = GetChar()), (c != '\r') && (c != '\n')) {
316 if ((c == 8 || c == 127) && intStrLen > 0) {
318 intStr[intStrLen] = '\0';
321 else if (intStrLen < 16 - 1 && (c >= '0' && c <= '9')) {
322 intStr[intStrLen++] = c;
323 intStr[intStrLen] = '\0';
331 GetStr(JSONNode * node) {
332 node->data = (size_t)NEWARR(char, 16);
335 char * str = (char *)node->data;
338 while ((c = GetChar()), (c != '\r') && (c != '\n')) {
339 if ((c == 8 || c == 127) && strLen > 0) {
343 else if (strLen < 16 - 1) {
352 GetNode(JSONNode * parent, JSONNode * node) {
355 JSONNode * result = node;
361 if (parent != NULL && result != NULL)
362 JSONNodePush(parent, result);
366 result->kind = JSONNodeKind_Int;
371 result->kind = JSONNodeKind_Str;
376 result->kind = JSONNodeKind_Obj;
377 while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
380 newNode = JSONNodeNewStr("");
381 g_CurrNode = newNode;
382 JSONNodePush(result, newNode);
385 newNode = JSONNodeNewNul();
386 JSONNodePush(result, newNode);
387 GetNode(result, newNode);
394 result->kind = JSONNodeKind_Arr;
395 while ((c = PeekChar()), (c != '\r') && (c != '\n')) {
396 JSONNode * newNode = JSONNodeNewNul();
397 g_CurrNode = newNode;
398 JSONNodePush(result, newNode);
399 GetNode(result, newNode);
409 JSONNode * newNode = JSONNodeNewNul();
410 GetNode(parent, newNode);
414 result->kind = JSONNodeKind_Int;
415 result->data = (size_t)GetChar();
426 JSONNode * n = JSONNodeNewNul();
428 //JSONNode * n = TestNode();
432 JSONNodePrint(n, NULL);