-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.c
84 lines (69 loc) · 1.5 KB
/
run.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
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2022 Codethink
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "cmd/cmd.h"
#include "cmd/private.h"
#include "msg/msg.h"
#include "util/cli.h"
#include "util/log.h"
#include "util/util.h"
static struct run_ctx {
const char *script;
} run_ctx;
static const struct cli_table_entry cli_entries[] = {
CMD_CLI_COMMON("run"),
{
.p = true,
.l = "SCRIPT",
.t = CLI_STRING,
.v.s = &run_ctx.script,
.d = "JSON-escaped JavaScript."
},
};
static const struct cli_table cli = {
.entries = cli_entries,
.count = (sizeof(cli_entries))/(sizeof(*cli_entries)),
.min_positional = 3,
};
static bool cmd_run_init(int argc, const char **argv,
struct cmd_options *options, void **pw_out)
{
int id;
if (!cmd_cli_parse(argc, argv, &cli, options)) {
return false;
}
msg_queue_for_send(&(const struct msg)
{
.type = MSG_TYPE_EVALUATE,
.data = {
.evaluate = {
.expression = run_ctx.script,
},
},
}, &id);
*pw_out = NULL;
return true;
}
static void cmd_run_msg(void *pw, int id, const char *msg, size_t len)
{
(void)(pw);
cdt_log(CDT_LOG_NOTICE, "Received message with id %i: %*s",
id, (int)len, msg);
}
static void cmd_run_help(int argc, const char **argv);
const struct cmd_table cmd_run = {
.cmd = "run",
.init = cmd_run_init,
.help = cmd_run_help,
.msg = cmd_run_msg,
};
static void cmd_run_help(int argc, const char **argv)
{
cli_help(&cli, (argc > 0) ? argv[0] : "cdt");
}