beachcomber 0.4.0

A centralized daemon that caches shell state (git, battery, hostname, etc.) so every consumer reads from one fast cache instead of independently forking shells
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * json.c — minimal JSON parser for beachcomber C SDK
 */

#include "json.h"

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <math.h>   /* HUGE_VAL */

/* -------------------------------------------------------------------------
 * Internal parser state
 * ---------------------------------------------------------------------- */

typedef struct {
    const char *src;
    size_t      pos;
    size_t      len;
} parser_t;

/* Forward declarations */
static json_node_t *parse_value(parser_t *p);
static json_node_t *parse_object(parser_t *p);
static json_node_t *parse_array(parser_t *p);
static json_node_t *parse_string_node(parser_t *p);
static json_node_t *parse_number(parser_t *p);
static json_node_t *parse_literal(parser_t *p, const char *lit,
                                   json_type_t type, int bool_val);
static void skip_ws(parser_t *p);
static char peek(const parser_t *p);
static char next_char(parser_t *p);

/* -------------------------------------------------------------------------
 * Node allocation helpers
 * ---------------------------------------------------------------------- */

static json_node_t *node_alloc(json_type_t type) {
    json_node_t *n = (json_node_t *)calloc(1, sizeof(json_node_t));
    if (n) n->type = type;
    return n;
}

static char *str_dup_n(const char *s, size_t len) {
    char *out = (char *)malloc(len + 1);
    if (!out) return NULL;
    memcpy(out, s, len);
    out[len] = '\0';
    return out;
}

/* -------------------------------------------------------------------------
 * Public API
 * ---------------------------------------------------------------------- */

json_node_t *json_parse(const char *src) {
    if (!src) return NULL;
    parser_t p;
    p.src = src;
    p.pos = 0;
    p.len = strlen(src);
    skip_ws(&p);
    json_node_t *root = parse_value(&p);
    return root;
}

void json_free(json_node_t *node) {
    if (!node) return;

    /* Free sibling chain first */
    json_free(node->next);

    /* Free children */
    json_free(node->children);

    /* Free owned strings */
    free(node->key);
    if (node->type == JSON_STRING) {
        free(node->val.string);
    }

    free(node);
}

json_node_t *json_get(const json_node_t *node, const char *key) {
    if (!node || node->type != JSON_OBJECT || !key) return NULL;
    for (json_node_t *c = node->children; c; c = c->next) {
        if (c->key && strcmp(c->key, key) == 0) return c;
    }
    return NULL;
}

const char *json_as_str(const json_node_t *node) {
    if (!node || node->type != JSON_STRING) return NULL;
    return node->val.string;
}

int64_t json_as_int(const json_node_t *node, int *ok) {
    if (ok) *ok = 0;
    if (!node) return 0;
    if (node->type == JSON_INT) {
        if (ok) *ok = 1;
        return node->val.integer;
    }
    /* Accept float that is a whole number */
    if (node->type == JSON_FLOAT) {
        double d = node->val.floating;
        if (d == (double)(int64_t)d) {
            if (ok) *ok = 1;
            return (int64_t)d;
        }
    }
    return 0;
}

double json_as_float(const json_node_t *node, int *ok) {
    if (ok) *ok = 0;
    if (!node) return 0.0;
    if (node->type == JSON_FLOAT) {
        if (ok) *ok = 1;
        return node->val.floating;
    }
    if (node->type == JSON_INT) {
        if (ok) *ok = 1;
        return (double)node->val.integer;
    }
    return 0.0;
}

int json_as_bool(const json_node_t *node, int *ok) {
    if (ok) *ok = 0;
    if (!node || node->type != JSON_BOOL) return 0;
    if (ok) *ok = 1;
    return node->val.boolean;
}

const char *json_type_name(json_type_t type) {
    switch (type) {
        case JSON_NULL:   return "null";
        case JSON_BOOL:   return "bool";
        case JSON_INT:    return "int";
        case JSON_FLOAT:  return "float";
        case JSON_STRING: return "string";
        case JSON_ARRAY:  return "array";
        case JSON_OBJECT: return "object";
        default:          return "unknown";
    }
}

/* -------------------------------------------------------------------------
 * Internal parser implementation
 * ---------------------------------------------------------------------- */

static void skip_ws(parser_t *p) {
    while (p->pos < p->len && isspace((unsigned char)p->src[p->pos]))
        p->pos++;
}

static char peek(const parser_t *p) {
    if (p->pos >= p->len) return '\0';
    return p->src[p->pos];
}

static char next_char(parser_t *p) {
    if (p->pos >= p->len) return '\0';
    return p->src[p->pos++];
}

/* Parse a 4-hex-digit Unicode escape, writing UTF-8 into buf.
 * Returns number of bytes written, or 0 on error. */
static int parse_unicode_escape(parser_t *p, char *buf) {
    if (p->pos + 4 > p->len) return 0;
    unsigned int cp = 0;
    for (int i = 0; i < 4; i++) {
        char c = p->src[p->pos++];
        unsigned int digit;
        if      (c >= '0' && c <= '9') digit = (unsigned int)(c - '0');
        else if (c >= 'a' && c <= 'f') digit = (unsigned int)(c - 'a' + 10);
        else if (c >= 'A' && c <= 'F') digit = (unsigned int)(c - 'A' + 10);
        else return 0;
        cp = (cp << 4) | digit;
    }
    /* Encode as UTF-8 */
    if (cp < 0x80) {
        buf[0] = (char)cp;
        return 1;
    } else if (cp < 0x800) {
        buf[0] = (char)(0xC0 | (cp >> 6));
        buf[1] = (char)(0x80 | (cp & 0x3F));
        return 2;
    } else {
        buf[0] = (char)(0xE0 | (cp >> 12));
        buf[1] = (char)(0x80 | ((cp >> 6) & 0x3F));
        buf[2] = (char)(0x80 | (cp & 0x3F));
        return 3;
    }
}

/*
 * Parse a JSON string value (the quotes are consumed here).
 * Returns a malloc'd NUL-terminated string, or NULL on error.
 */
static char *parse_string_raw(parser_t *p) {
    if (peek(p) != '"') return NULL;
    next_char(p); /* consume opening quote */

    /* We accumulate the decoded string in a dynamically-grown buffer. */
    size_t cap = 64;
    size_t len = 0;
    char *buf = (char *)malloc(cap);
    if (!buf) return NULL;

#define APPEND(c) do {                             \
    if (len + 1 >= cap) {                         \
        cap *= 2;                                  \
        char *nb = (char *)realloc(buf, cap);      \
        if (!nb) { free(buf); return NULL; }       \
        buf = nb;                                  \
    }                                              \
    buf[len++] = (c);                              \
} while (0)

#define APPEND_N(s, n) do {                        \
    while (len + (n) + 1 >= cap) {                \
        cap *= 2;                                  \
        char *nb = (char *)realloc(buf, cap);      \
        if (!nb) { free(buf); return NULL; }       \
        buf = nb;                                  \
    }                                              \
    memcpy(buf + len, s, n);                       \
    len += (n);                                    \
} while (0)

    for (;;) {
        if (p->pos >= p->len) { free(buf); return NULL; } /* unterminated */
        char c = next_char(p);
        if (c == '"') break; /* end of string */
        if (c != '\\') {
            APPEND(c);
            continue;
        }
        /* Escape sequence */
        if (p->pos >= p->len) { free(buf); return NULL; }
        char esc = next_char(p);
        switch (esc) {
            case '"':  APPEND('"');  break;
            case '\\': APPEND('\\'); break;
            case '/':  APPEND('/');  break;
            case 'b':  APPEND('\b'); break;
            case 'f':  APPEND('\f'); break;
            case 'n':  APPEND('\n'); break;
            case 'r':  APPEND('\r'); break;
            case 't':  APPEND('\t'); break;
            case 'u': {
                char ubuf[4];
                int nb = parse_unicode_escape(p, ubuf);
                if (nb == 0) { free(buf); return NULL; }
                APPEND_N(ubuf, (size_t)nb);
                break;
            }
            default: { free(buf); return NULL; }
        }
    }

#undef APPEND
#undef APPEND_N

    buf[len] = '\0';
    return buf;
}

static json_node_t *parse_string_node(parser_t *p) {
    char *s = parse_string_raw(p);
    if (!s) return NULL;
    json_node_t *n = node_alloc(JSON_STRING);
    if (!n) { free(s); return NULL; }
    n->val.string = s;
    return n;
}

static json_node_t *parse_number(parser_t *p) {
    size_t start = p->pos;
    int is_float = 0;

    if (peek(p) == '-') p->pos++;

    if (peek(p) == '0') {
        p->pos++;
    } else {
        if (!isdigit((unsigned char)peek(p))) return NULL;
        while (p->pos < p->len && isdigit((unsigned char)p->src[p->pos]))
            p->pos++;
    }

    /* Fractional part */
    if (peek(p) == '.') {
        is_float = 1;
        p->pos++;
        if (!isdigit((unsigned char)peek(p))) return NULL;
        while (p->pos < p->len && isdigit((unsigned char)p->src[p->pos]))
            p->pos++;
    }

    /* Exponent */
    if (peek(p) == 'e' || peek(p) == 'E') {
        is_float = 1;
        p->pos++;
        if (peek(p) == '+' || peek(p) == '-') p->pos++;
        if (!isdigit((unsigned char)peek(p))) return NULL;
        while (p->pos < p->len && isdigit((unsigned char)p->src[p->pos]))
            p->pos++;
    }

    size_t num_len = p->pos - start;
    char *tmp = str_dup_n(p->src + start, num_len);
    if (!tmp) return NULL;

    json_node_t *n;
    if (is_float) {
        n = node_alloc(JSON_FLOAT);
        if (n) n->val.floating = strtod(tmp, NULL);
    } else {
        n = node_alloc(JSON_INT);
        if (n) n->val.integer = (int64_t)strtoll(tmp, NULL, 10);
    }
    free(tmp);
    return n;
}

static json_node_t *parse_literal(parser_t *p, const char *lit,
                                   json_type_t type, int bool_val) {
    size_t lit_len = strlen(lit);
    if (p->pos + lit_len > p->len) return NULL;
    if (strncmp(p->src + p->pos, lit, lit_len) != 0) return NULL;
    p->pos += lit_len;
    json_node_t *n = node_alloc(type);
    if (n && type == JSON_BOOL) n->val.boolean = bool_val;
    return n;
}

static json_node_t *parse_object(parser_t *p) {
    if (peek(p) != '{') return NULL;
    next_char(p);

    json_node_t *obj = node_alloc(JSON_OBJECT);
    if (!obj) return NULL;

    skip_ws(p);
    if (peek(p) == '}') {
        next_char(p);
        return obj;
    }

    json_node_t *tail = NULL;

    for (;;) {
        skip_ws(p);
        if (peek(p) != '"') { json_free(obj); return NULL; }

        /* Parse key */
        char *key = parse_string_raw(p);
        if (!key) { json_free(obj); return NULL; }

        skip_ws(p);
        if (next_char(p) != ':') { free(key); json_free(obj); return NULL; }
        skip_ws(p);

        /* Parse value */
        json_node_t *val = parse_value(p);
        if (!val) { free(key); json_free(obj); return NULL; }
        val->key = key;

        /* Append to children list */
        if (!obj->children) {
            obj->children = val;
        } else {
            tail->next = val;
        }
        tail = val;
        obj->n_children++;

        skip_ws(p);
        char sep = peek(p);
        if (sep == '}') { next_char(p); break; }
        if (sep == ',') { next_char(p); continue; }
        json_free(obj);
        return NULL;
    }

    return obj;
}

static json_node_t *parse_array(parser_t *p) {
    if (peek(p) != '[') return NULL;
    next_char(p);

    json_node_t *arr = node_alloc(JSON_ARRAY);
    if (!arr) return NULL;

    skip_ws(p);
    if (peek(p) == ']') {
        next_char(p);
        return arr;
    }

    json_node_t *tail = NULL;

    for (;;) {
        skip_ws(p);
        json_node_t *val = parse_value(p);
        if (!val) { json_free(arr); return NULL; }

        if (!arr->children) {
            arr->children = val;
        } else {
            tail->next = val;
        }
        tail = val;
        arr->n_children++;

        skip_ws(p);
        char sep = peek(p);
        if (sep == ']') { next_char(p); break; }
        if (sep == ',') { next_char(p); continue; }
        json_free(arr);
        return NULL;
    }

    return arr;
}

static json_node_t *parse_value(parser_t *p) {
    skip_ws(p);
    char c = peek(p);

    if (c == '{') return parse_object(p);
    if (c == '[') return parse_array(p);
    if (c == '"') return parse_string_node(p);
    if (c == 't') return parse_literal(p, "true",  JSON_BOOL, 1);
    if (c == 'f') return parse_literal(p, "false", JSON_BOOL, 0);
    if (c == 'n') return parse_literal(p, "null",  JSON_NULL, 0);
    if (c == '-' || isdigit((unsigned char)c)) return parse_number(p);

    return NULL; /* unrecognised character */
}