summaryrefslogtreecommitdiff
path: root/src/cmd.c
blob: 5fdc59f6d8ccc5e089a5e156a82d95cf78508c98 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
 * @file src/cmd.c  Command Interface
 *
 * Copyright (C) 2010 Creytiv.com
 */
#include <ctype.h>
#include <string.h>
#include <re.h>
#include <baresip.h>
#include "core.h"


enum {
	REL = 0x00,
	ESC = 0x1b,
	DEL = 0x7f,
};


struct cmds {
	struct le le;
	const struct cmd *cmdv;
	size_t cmdc;
};

struct cmd_ctx {
	struct mbuf *mb;
	const struct cmd *cmd;
};


static struct list cmdl;           /**< List of command blocks (struct cmds) */


static void destructor(void *arg)
{
	struct cmds *cmds = arg;

	list_unlink(&cmds->le);
}


static void ctx_destructor(void *arg)
{
	struct cmd_ctx *ctx = arg;

	mem_deref(ctx->mb);
}


static int ctx_alloc(struct cmd_ctx **ctxp, const struct cmd *cmd)
{
	struct cmd_ctx *ctx;

	ctx = mem_zalloc(sizeof(*ctx), ctx_destructor);
	if (!ctx)
		return ENOMEM;

	ctx->mb = mbuf_alloc(32);
	if (!ctx->mb) {
		mem_deref(ctx);
		return ENOMEM;
	}

	ctx->cmd = cmd;

	*ctxp = ctx;

	return 0;
}


static struct cmds *cmds_find(const struct cmd *cmdv)
{
	struct le *le;

	if (!cmdv)
		return NULL;

	for (le = cmdl.head; le; le = le->next) {
		struct cmds *cmds = le->data;

		if (cmds->cmdv == cmdv)
			return cmds;
	}

	return NULL;
}


static const struct cmd *cmd_find_by_key(char key)
{
	struct le *le;

	for (le = cmdl.tail; le; le = le->prev) {

		struct cmds *cmds = le->data;
		size_t i;

		for (i=0; i<cmds->cmdc; i++) {

			const struct cmd *cmd = &cmds->cmdv[i];

			if (cmd->key == key && cmd->h)
				return cmd;
		}
	}

	return NULL;
}


static const char *cmd_name(char *buf, size_t sz, const struct cmd *cmd)
{
	switch (cmd->key) {

	case ' ':   return "SPACE";
	case '\n':  return "ENTER";
	case ESC:   return "ESC";
	}

	buf[0] = cmd->key;
	buf[1] = '\0';

	if (cmd->flags & CMD_PRM)
		strncat(buf, " ..", sz-1);

	return buf;
}


static int editor_input(struct mbuf *mb, char key,
			struct re_printf *pf, bool *del)
{
	int err = 0;

	switch (key) {

	case ESC:
		*del = true;
		return re_hprintf(pf, "\nCancel\n");

	case REL:
		break;

	case '\n':
		*del = true;
		return re_hprintf(pf, "\n");

	case '\b':
	case DEL:
		if (mb->pos > 0)
			mb->pos = mb->end = (mb->pos - 1);
		break;

	default:
		err = mbuf_write_u8(mb, key);
		break;
	}

	err |= re_hprintf(pf, "\r> %32b", mb->buf, mb->end);

	return err;
}


static int cmd_report(const struct cmd *cmd, struct re_printf *pf,
		      struct mbuf *mb, bool compl, void *data)
{
	struct cmd_arg arg;
	int err;

	mb->pos = 0;
	err = mbuf_strdup(mb, &arg.prm, mb->end);
	if (err)
		return err;

	arg.key      = cmd->key;
	arg.complete = compl;
	arg.data     = data;

	err = cmd->h(pf, &arg);

	mem_deref(arg.prm);

	return err;
}


static int cmd_process_edit(struct cmd_ctx **ctxp, char key,
			    struct re_printf *pf, void *data)
{
	struct cmd_ctx *ctx;
	bool compl = (key == '\n'), del = false;
	int err;

	if (!ctxp)
		return EINVAL;

	ctx = *ctxp;

	err = editor_input(ctx->mb, key, pf, &del);
	if (err)
		return err;

	if (compl || ctx->cmd->flags & CMD_PROG)
		err = cmd_report(ctx->cmd, pf, ctx->mb, compl, data);

	if (del)
		*ctxp = mem_deref(*ctxp);

	return err;
}


/**
 * Register commands
 *
 * @param cmdv Array of commands
 * @param cmdc Number of commands
 *
 * @return 0 if success, otherwise errorcode
 */
int cmd_register(const struct cmd *cmdv, size_t cmdc)
{
	struct cmds *cmds;

	if (!cmdv || !cmdc)
		return EINVAL;

	cmds = cmds_find(cmdv);
	if (cmds)
		return EALREADY;

	cmds = mem_zalloc(sizeof(*cmds), destructor);
	if (!cmds)
		return ENOMEM;

	cmds->cmdv = cmdv;
	cmds->cmdc = cmdc;

	list_append(&cmdl, &cmds->le, cmds);

	return 0;
}


/**
 * Unregister commands
 *
 * @param cmdv Array of commands
 */
void cmd_unregister(const struct cmd *cmdv)
{
	mem_deref(cmds_find(cmdv));
}


/**
 * Process input characters to the command system
 *
 * @param ctxp Pointer to context for editor (optional)
 * @param key  Input character
 * @param pf   Print function
 * @param data Application data
 *
 * @return 0 if success, otherwise errorcode
 */
int cmd_process(struct cmd_ctx **ctxp, char key, struct re_printf *pf,
		void *data)
{
	const struct cmd *cmd;

	/* are we in edit-mode? */
	if (ctxp && *ctxp) {

		if (key == REL)
			return 0;

		return cmd_process_edit(ctxp, key, pf, data);
	}

	cmd = cmd_find_by_key(key);
	if (cmd) {
		struct cmd_arg arg;

		/* check for parameters */
		if (cmd->flags & CMD_PRM) {

			if (ctxp) {
				int err = ctx_alloc(ctxp, cmd);
				if (err)
					return err;
			}

			return cmd_process_edit(ctxp,
						isdigit(key) ? key : 0,
						pf, data);
		}

		arg.key      = key;
		arg.prm      = NULL;
		arg.complete = true;
		arg.data     = data;

		return cmd->h(pf, &arg);
	}

	if (key == REL)
		return 0;

	return cmd_print(pf, NULL);
}


/**
 * Print a list of available commands
 *
 * @param pf     Print function
 * @param unused Unused variable
 *
 * @return 0 if success, otherwise errorcode
 */
int cmd_print(struct re_printf *pf, void *unused)
{
	size_t width = 5;
	char fmt[32], buf[8];
	int err = 0;
	int key;

	(void)unused;

	if (!pf)
		return EINVAL;

	(void)re_snprintf(fmt, sizeof(fmt), " %%-%zus   %%s\n", width);

	err |= re_hprintf(pf, "--- Help ---\n");

	/* print in alphabetical order */
	for (key = 1; key <= 0x80; key++) {

		const struct cmd *cmd = cmd_find_by_key(key);
		if (!cmd || !str_isset(cmd->desc))
			continue;

		err |= re_hprintf(pf, fmt, cmd_name(buf, sizeof(buf), cmd),
				  cmd->desc);

	}

	err |= re_hprintf(pf, "\n");

	return err;
}