Get input file as one long string in global input. Returns number of bytes read. */
| 188 | Returns number of bytes read. |
| 189 | */ |
| 190 | static size_t read_input(void) |
| 191 | { |
| 192 | size_t nread = 0; |
| 193 | size_t nalloc = 1024; |
| 194 | input = malloc(nalloc); |
| 195 | char *p = (char *)input; |
| 196 | int cc; |
| 197 | |
| 198 | while ((cc = getchar()) != EOF) { |
| 199 | if (nread == nalloc) { |
| 200 | nalloc <<= 1; |
| 201 | input = realloc((void *)input, nalloc); |
| 202 | /* reset p to correct spot in input: */ |
| 203 | p = (char *)input+nread; |
| 204 | } |
| 205 | *p++ = cc; |
| 206 | nread++; |
| 207 | } |
| 208 | /* No need for terminating \0 char. */ |
| 209 | if (debug) fprintf(stderr, "(D): Read %u bytes.\n", nread); |
| 210 | return nread; |
| 211 | } |
| 212 | |
| 213 | /** Parse the JSON input (global) into an array of tokens. |
| 214 | Returns the allocated token array. |