-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathactions.c
More file actions
214 lines (177 loc) · 5.01 KB
/
actions.c
File metadata and controls
214 lines (177 loc) · 5.01 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright Nico Sonack <nsonack@herrhotzenplotz.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <gcli/cmd/actions.h>
#include <gcli/cmd/cmd.h>
#include <gcli/cmd/interactive.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static struct gcli_cmd_action const *
find_action(struct gcli_cmd_actions const *const actions,
char const *const action_name)
{
struct gcli_cmd_action const *a = NULL;
for (a = actions->defs; a->name != NULL; a += 1) {
if (strcmp(a->name, action_name) == 0) {
return a;
}
}
return NULL;
}
int
gcli_cmd_actions_handle(struct gcli_cmd_actions const *const actions,
struct gcli_path const *const path,
int *argc, char ***argv)
{
void *item = NULL;
int rc = 0;
if (*argc < 1) {
fprintf(stderr, "gcli: error: missing action\n");
return 1;
}
/* check until we don't have any more remaining arguments */
for (;;) {
/* fetch the action name */
char *const action_name = (*argv)[0];
/* look for an action definition */
struct gcli_cmd_action const *const action = find_action(
actions, action_name);
if (action == NULL) {
fprintf(stderr, "gcli: error: unknown action '%s'\n",
action_name);
rc = GCLI_EX_USAGE;
break;
}
/* check whether we need to fetch the item */
if (action->needs_item && item == NULL) {
item = calloc(1, actions->item_size);
if (item == NULL) {
err(1, "calloc failed");
}
rc = actions->fetch_item(g_clictx, path, item);
if (rc < 0) {
fprintf(stderr, "gcli: error: failed to fetch: %s\n",
gcli_get_error(g_clictx));
rc = GCLI_EX_DATAERR;
break;
}
}
/* handle the action */
rc = action->handler(path, item, argc, argv);
if (rc < 0) {
fprintf(stderr, "gcli: action %s failed\n", action_name);
break;
}
shift(argc, argv);
if (*argc == 0)
break;
fputc('\n', stdout);
}
if (item) {
actions->free_item(item);
free(item);
item = NULL;
}
return rc;
}
struct into_pager_args {
int argc;
char **argv;
struct gcli_path const *path;
void *item;
struct gcli_cmd_action const *action;
};
static int
into_pager_fn(void *data)
{
struct into_pager_args *args = data;
return args->action->handler(
args->path,
args->item,
&args->argc,
&args->argv);
}
int
gcli_cmd_action_handle(struct gcli_cmd_actions const *actions,
struct gcli_path const *path,
char *cmd_input)
{
enum { argv_size = 32 };
char *_argv[argv_size]; /* storage */
char *argfront = cmd_input;
int argc = 0, rc = 0;
struct gcli_cmd_action const *action = NULL;
void *item = NULL;
char **argv = &_argv[0]; /* pointer to storage but type is corrected */
/* Split arguments by spaces and collect into argc/argv */
for (;;) {
char *argnext = strchr(argfront, ' ');
if (argc == argv_size)
err(1, "gcli: error: too many arguments");
argv[argc++] = argfront;
if (!argnext)
break;
*argnext++ = '\0';
argfront = argnext;
}
action = find_action(actions, argv[0]);
if (action == NULL) {
fprintf(stderr, "gcli: error: no such action: %s\n", argv[0]);
return GCLI_EX_USAGE;
}
if (action->needs_item) {
item = calloc(1, actions->item_size);
if (item == NULL)
err(1, "calloc");
rc = actions->fetch_item(g_clictx, path, item);
if (rc < 0) {
fprintf(stderr, "gcli: error: failed to fetch item: %s\n",
gcli_get_error(g_clictx));
return GCLI_EX_DATAERR;
}
}
if (action->use_pager) {
struct into_pager_args args = {
.argc = argc,
.argv = argv,
.path = path,
.item = item,
.action = action,
};
rc = gcli_cmd_into_pager(into_pager_fn, &args);
} else {
rc = action->handler(path, item, &argc, &argv);
}
if (item) {
actions->free_item(item);
free(item);
item = NULL;
}
return rc;
}