-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_label.c
More file actions
59 lines (51 loc) · 1.58 KB
/
splinter_cli_cmd_label.c
File metadata and controls
59 lines (51 loc) · 1.58 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
/**
* @file splinter_cli_cmd_label.c
* @brief Implements the CLI 'label' command to tag keys via Bloom filter.
* License: Apache 2
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "splinter_cli.h"
static const char *modname = "label";
void help_cmd_label(unsigned int level) {
(void) level;
printf("Usage: %s <key> <label_name>\n", modname);
printf("Labels are defined in ~/.splinterrc and apply to the 64-bit Bloom filter.\n");
puts("");
}
int cmd_label(int argc, char *argv[]) {
if (argc < 3) {
help_cmd_label(1);
return 1;
}
const char *key = argv[1];
const char *label_name = argv[2];
uint64_t mask = 0;
bool found = false;
// resolve Label from .splinterrc
for (int i = 0; i < thisuser.label_count; i++) {
if (!strcasecmp(label_name, thisuser.labels[i].name)) {
mask = thisuser.labels[i].mask;
found = true;
break;
}
}
if (!found) {
// fallback to hex parsing if not a named label
char *endptr;
mask = strtoull(label_name, &endptr, 0);
if (*endptr != '\0' || mask == 0) {
fprintf(stderr, "%s: unknown label or invalid mask '%s'\n", modname, label_name);
return 1;
}
}
if (splinter_set_label(key, mask) == 0) {
printf("Label '%s' (0x%lx) applied to '%s'.\n", label_name, (unsigned long)mask, key);
return 0;
} else {
fprintf(stderr, "%s: failed to apply label to '%s' (errno: %d)\n", modname, key, errno);
return 1;
}
}