-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplinter_cli_cmd_hist.c
More file actions
73 lines (62 loc) · 1.58 KB
/
splinter_cli_cmd_hist.c
File metadata and controls
73 lines (62 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* Copyright 2025 Tim Post
* License: Apache 2 (MIT available upon request to timthepost@protonmail.com)
*
* @file splinter_cli_cmd_hist.c
* @brief Implements the CLI 'hist' command.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "splinter_cli.h"
#include "3rdparty/grawk.h"
#include "3rdparty/linenoise.h"
static const char *modname = "hist";
extern char **history;
extern void freeHistory(void);
void help_cmd_hist(unsigned int level) {
(void) level;
printf("%s shows (filtered by [pattern]) the command history.\n", modname);
printf("Usage: %s [pattern]\n", modname);
return;
}
int cmd_hist(int argc, char *argv[]) {
grawk_t *g = NULL;
awk_pat_t *filter = NULL;
int i;
grawk_opts_t opts = {
.ignore_case = 0,
.invert_match = 0,
.quiet = 1
};
if (argc > 2) {
help_cmd_hist(1);
return -1;
}
g = grawk_init();
if (g == NULL) {
fprintf(stderr, "%s: unable to allocate memory to filter entries.\n", modname);
errno = ENOMEM;
return -1;
}
grawk_set_options(g, &opts);
if (argc == 2) {
filter = grawk_build_pattern(argv[1]);
grawk_set_pattern(g, filter);
}
for (i = 0; history[i]; i++) {
if (filter != NULL) {
if (grawk_match(g, history[i])) {
printf("%d: %s\n", i+1, history[i]);
}
} else {
printf("%d: %s\n", i+1, history[i]);
}
}
puts("");
if (g != NULL)
grawk_free(g);
return 0;
}