-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_export.c
More file actions
244 lines (209 loc) · 7.43 KB
/
splinter_cli_cmd_export.c
File metadata and controls
244 lines (209 loc) · 7.43 KB
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
/**
* Copyright 2025 Tim Post
* License: Apache 2 (MIT available upon request to timthepost@protonmail.com)
*
* @file splinter_cli_cmd_export.c
* @brief Implements the CLI 'export' command.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "splinter_cli.h"
#include "3rdparty/grawk.h"
static const char *modname = "export";
static splinter_header_snapshot_t snap = {0};
void help_cmd_export(unsigned int level) {
(void) level;
printf("%s exports the store in various formats to standard output.\n", modname);
printf("Usage: %s [format (default=json)] [max_lines (default=0/unlimited)]\n", modname);
printf("Format can be one of: json (more coming soon)\n");
return;
}
static int compare_slots_by_epoch(const void *a, const void *b) {
const splinter_slot_snapshot_t *slot_a = (const splinter_slot_snapshot_t *)a;
const splinter_slot_snapshot_t *slot_b = (const splinter_slot_snapshot_t *)b;
// Descending order: b - a
if (slot_b->epoch > slot_a->epoch) return 1;
if (slot_b->epoch < slot_a->epoch) return -1;
return 0;
}
/**
* @brief Emit a buffer as a JSON-escaped single-line string (no surrounding quotes).
*
* Handles RFC 8259 mandatory escapes (", \, control chars), turns newlines/tabs
* into their short forms, and emits remaining control bytes as \u00XX. Bytes
* >= 0x80 are passed through verbatim — VARTEXT is expected to be UTF-8.
*/
static void json_print_escaped(const char *buf, size_t len) {
for (size_t i = 0; i < len; i++) {
unsigned char c = (unsigned char)buf[i];
switch (c) {
case '"': fputs("\\\"", stdout); break;
case '\\': fputs("\\\\", stdout); break;
case '\b': fputs("\\b", stdout); break;
case '\f': fputs("\\f", stdout); break;
case '\n': fputs("\\n", stdout); break;
case '\r': fputs("\\r", stdout); break;
case '\t': fputs("\\t", stdout); break;
default:
if (c < 0x20) {
printf("\\u%04x", c);
} else {
fputc(c, stdout);
}
break;
}
}
}
/**
* @brief Prints slot snapshots in JSON format
* @param slots Sorted array of slot snapshots
* @param slot_count Number of valid slots in the array
* @param snap Pointer to bus header snapshot
*
* For VARTEXT-typed slots, the slot's value is fetched and emitted as a
* single-line JSON string under the "value" field. Other types omit the
* field — printability isn't guaranteed and the consumer can re-fetch
* by key if needed.
*/
static void print_json(const splinter_slot_snapshot_t *slots, size_t slot_count,
const splinter_header_snapshot_t *snap) {
size_t i;
/* Reusable buffer for VARTEXT value reads. Sized to max_val_sz so any
* slot fits; allocated once, freed at the end. */
char *valbuf = NULL;
size_t valbuf_sz = (snap->max_val_sz > 0) ? snap->max_val_sz : 0;
if (valbuf_sz > 0) {
valbuf = (char *)malloc(valbuf_sz);
/* Falling through with valbuf == NULL just means we skip value
* emission rather than failing the whole export. */
}
printf("{\n");
printf(" \"store\": {\n");
printf(" \"total_slots\": %u,\n", snap->slots);
printf(" \"active_keys\": %zu\n", slot_count);
printf(" },\n");
printf(" \"keys\": [\n");
for (i = 0; i < slot_count; i++) {
// Skip empty/invalid entries
if (slots[i].epoch == 0) {
continue;
}
int is_vartext = (slots[i].type_flag & SPL_SLOT_TYPE_VARTEXT) != 0;
int emit_value = (is_vartext && valbuf != NULL && slots[i].val_len > 0);
printf(" {\n");
printf(" \"key\": \"%s\",\n", slots[i].key);
printf(" \"type\": \"%s\",\n", cli_show_key_type(slots[i].type_flag));
printf(" \"epoch\": %lu,\n", slots[i].epoch);
if (emit_value) {
printf(" \"value_length\": %u,\n", slots[i].val_len);
size_t got = 0;
if (splinter_get(slots[i].key, valbuf, valbuf_sz, &got) == 0) {
printf(" \"value\": \"");
json_print_escaped(valbuf, got);
printf("\"\n");
} else {
/* Read failed (slot moved, key missing, etc.) — degrade to null. */
printf(" \"value\": null\n");
}
} else {
printf(" \"value_length\": %u\n", slots[i].val_len);
}
// Add comma unless this is the last entry
if (i < slot_count - 1 && slots[i + 1].epoch > 0) {
printf(" },\n");
} else {
printf(" }\n");
}
}
printf(" ]\n");
printf("}\n");
free(valbuf);
}
int cmd_export(int argc, char *argv[]) {
grawk_t *g = NULL;
awk_pat_t *filter = NULL;
grawk_opts_t opts = {
.ignore_case = 0,
.invert_match = 0,
.quiet = 1
};
splinter_slot_snapshot_t *slots = NULL;
char **keynames = NULL;
size_t entry_count = 0;
size_t max_keys = 0;
int rc = -1, i, x = 0;
if (argc > 2) {
help_cmd_list(1);
return -1;
}
// Get header snapshot to determine allocation size
splinter_get_header_snapshot(&snap);
max_keys = snap.slots;
if (max_keys == 0) {
fprintf(stderr, "%s: no slots available in current store.\n", modname);
return -1;
}
keynames = (char **)calloc(max_keys, sizeof(char *));
if (keynames == NULL) {
fprintf(stderr, "%s: unable to allocate memory for key names.\n", modname);
errno = ENOMEM;
return -1;
}
slots = (splinter_slot_snapshot_t *)calloc(max_keys, sizeof(splinter_slot_snapshot_t));
if (slots == NULL) {
fprintf(stderr, "%s: unable to allocate memory for slot snapshots.\n", modname);
errno = ENOMEM;
free(keynames);
return -1;
}
rc = splinter_list(keynames, max_keys, &entry_count);
if (rc == 0) {
g = grawk_init();
if (g == NULL) {
fprintf(stderr, "%s: unable to allocate memory to filter keys.\n", modname);
errno = ENOMEM;
rc = -1;
goto cleanup;
}
grawk_set_options(g, &opts);
if (argc == 2) {
filter = grawk_build_pattern(argv[1]);
grawk_set_pattern(g, filter);
}
for (i = 0; keynames[i]; i++) {
if (keynames[i][0] != '\0') {
// if there's no filter, just add it
if (filter == NULL) {
splinter_get_slot_snapshot(keynames[i], &slots[x]);
x++;
} else {
// only add if filter matches
if (grawk_match(g, keynames[i])) {
splinter_get_slot_snapshot(keynames[i], &slots[x]);
x++;
}
}
}
}
qsort(slots, x, sizeof(splinter_slot_snapshot_t), compare_slots_by_epoch);
// TODO: Other formats / arguments
print_json(slots, x, &snap);
// Empty line is intentional (and uniform throughout commands)
puts("");
rc = 0;
}
cleanup:
// Free grawk resources
if (g != NULL) {
grawk_free(g);
}
if (slots != NULL) {
free(slots);
}
if (keynames != NULL) {
free(keynames);
}
return rc;
}