-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_list.c
More file actions
154 lines (128 loc) · 4.04 KB
/
splinter_cli_cmd_list.c
File metadata and controls
154 lines (128 loc) · 4.04 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
/**
* Copyright 2025 Tim Post
* License: Apache 2 (MIT available upon request to timthepost@protonmail.com)
*
* @file splinter_cli_cmd_list.c
* @brief Implements the CLI 'list' 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 = "list";
static splinter_header_snapshot_t snap = {0};
void help_cmd_list(unsigned int level) {
(void) level;
printf("%s lists keys in the currently selected store.\n", modname);
printf("Usage: %s [pattern] [max_lines]\n", modname);
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;
}
int cmd_list(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++;
}
}
}
}
// Sort so the most-updated keys are at the top of the list
qsort(slots, x, sizeof(splinter_slot_snapshot_t), compare_slots_by_epoch);
printf("%-44s %-6s %-6s %s\n",
"Name",
"Epoch",
"Len",
"Type"
);
for (i = 0; i < x && slots[i].epoch > 0; i++) {
printf("%-44s %-6lu %-6u %s\n",
slots[i].key,
slots[i].epoch,
slots[i].val_len,
cli_show_key_type(slots[i].type_flag)
);
}
// 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;
}