-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbh-find-iosea.c
145 lines (121 loc) · 3.51 KB
/
rbh-find-iosea.c
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
/* This file is part of rbh-find-iosea
* Copyright (C) 2022 Commissariat a l'energie atomique et aux energies
* alternatives
*
* SPDX-License-Identifer: LGPL-3.0-or-later
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <assert.h>
#include <dlfcn.h>
#include <errno.h>
#include <error.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <robinhood.h>
#include <robinhood/utils.h>
#include <rbh-find/actions.h>
#include <rbh-find/core.h>
#include <rbh-find/find_cb.h>
#include "filters.h"
#include "parser.h"
static struct find_context ctx;
static void __attribute__((destructor))
on_find_exit(void)
{
ctx_finish(&ctx);
}
enum command_line_token
iosea_predicate_or_action(const char *string)
{
switch (string[0]) {
case '-':
switch (string[1]) {
case 'e':
if (!strcmp(&string[2], "xtent-size"))
return CLT_PREDICATE;
break;
case 't':
if (!strcmp(&string[2], "ier"))
return CLT_PREDICATE;
break;
}
break;
}
return find_predicate_or_action(string);
}
static struct rbh_filter *
iosea_parse_predicate(struct find_context *ctx, int *arg_idx)
{
struct rbh_filter *filter;
int i = *arg_idx;
int predicate;
predicate = str2iosea_predicate(ctx->argv[i]);
if (i + 1 >= ctx->argc)
error(EX_USAGE, 0, "missing argument to '%s'", ctx->argv[i]);
/* In the following block, functions should call error() themselves rather
* than returning.
*
* Errors are most likely fatal (not recoverable, and this allows for
* precise and meaningful error messages.
*/
switch (predicate) {
case IPRED_EXTENT_SIZE:
filter = extent_size2filter(ctx->argv[++i]);
break;
case IPRED_TIER:
filter = tier2filter(ctx->argv[++i]);
break;
default:
filter = find_parse_predicate(ctx, &i);
break;
}
assert(filter != NULL);
*arg_idx = i;
return filter;
}
int
main(int _argc, char *_argv[])
{
struct rbh_filter_sort *sorts = NULL;
struct rbh_filter *filter;
size_t sorts_count = 0;
int index;
/* Discard the program's name */
ctx.argc = _argc - 1;
ctx.argv = &_argv[1];
ctx.pre_action_callback = &find_pre_action;
ctx.exec_action_callback = &find_exec_action;
ctx.post_action_callback = &find_post_action;
ctx.parse_predicate_callback = &iosea_parse_predicate;
ctx.pred_or_action_callback = &iosea_predicate_or_action;
/* Parse the command line */
for (index = 0; index < ctx.argc; index++) {
if (str2command_line_token(&ctx, ctx.argv[index]) != CLT_URI)
break;
}
if (index == 0)
error(EX_USAGE, 0, "missing at least one robinhood URI");
ctx.backends = malloc(index * sizeof(*ctx.backends));
if (ctx.backends == NULL)
error(EXIT_FAILURE, errno, "malloc");
ctx.uris = malloc(index * sizeof(*ctx.uris));
if (!ctx.uris)
error(EXIT_FAILURE, errno, "malloc");
for (int i = 0; i < index; i++) {
ctx.backends[i] = rbh_backend_from_uri(ctx.argv[i]);
ctx.uris[i] = ctx.argv[i];
ctx.backend_count++;
}
filter = parse_expression(&ctx, &index, NULL, &sorts, &sorts_count);
if (index != ctx.argc)
error(EX_USAGE, 0, "you have too many ')'");
if (!ctx.action_done)
find(&ctx, ACT_PRINT, &index, filter, sorts, sorts_count);
free(filter);
return EXIT_SUCCESS;
}