|
| 1 | +#include <fcntl.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <unistd.h> |
| 4 | + |
| 5 | +#include "config.h" |
| 6 | +#include "menu_util.h" |
| 7 | +#include "store.h" |
| 8 | +#include "util.h" |
| 9 | + |
| 10 | +struct delete_state { |
| 11 | + uint64_t hash_to_delete; |
| 12 | +}; |
| 13 | + |
| 14 | +static enum cs_remove_action _nonnull_ |
| 15 | +remove_if_hash_match(uint64_t hash, const char *line _unused_, void *private) { |
| 16 | + const struct delete_state *state = private; |
| 17 | + return hash == state->hash_to_delete ? CS_ACTION_REMOVE : CS_ACTION_KEEP; |
| 18 | +} |
| 19 | + |
| 20 | +static int _nonnull_ clipdelmenu_action(struct config *cfg, uint64_t hash) { |
| 21 | + _drop_(close) int content_dir_fd = open(get_cache_dir(cfg), O_RDONLY); |
| 22 | + _drop_(close) int snip_fd = |
| 23 | + open(get_line_cache_path(cfg), O_RDWR | O_CREAT, 0600); |
| 24 | + expect(content_dir_fd >= 0 && snip_fd >= 0); |
| 25 | + |
| 26 | + _drop_(cs_destroy) struct clip_store cs; |
| 27 | + expect(cs_init(&cs, snip_fd, content_dir_fd) == 0); |
| 28 | + |
| 29 | + struct delete_state del_state = {.hash_to_delete = hash}; |
| 30 | + expect(cs_remove(&cs, CS_ITER_NEWEST_FIRST, remove_if_hash_match, |
| 31 | + &del_state) == 0); |
| 32 | + |
| 33 | + return 0; |
| 34 | +} |
| 35 | + |
| 36 | +int main(int argc, char *argv[]) { |
| 37 | + menu_set_user_args(argc, argv); |
| 38 | + |
| 39 | + _drop_(config_free) struct config cfg = setup("clipdelmenu"); |
| 40 | + exec_man_on_help(argc, argv); |
| 41 | + |
| 42 | + return menu_prompt_and_act(&cfg, "clipdelmenu", clipdelmenu_action); |
| 43 | +} |
0 commit comments