aboutsummaryrefslogtreecommitdiff
path: root/src/piesctl.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-01-05 14:56:11 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2016-01-05 14:56:11 +0200
commit32a337f3b35c51ceb1d9320c7ff67fa1d5726c2a (patch)
tree098d41b0d07d62144f8398dc3c5f80a295369f98 /src/piesctl.c
parent2c03055b8ed023e7d8d65f2dbd51cd128040fcfb (diff)
downloadpies-32a337f3b35c51ceb1d9320c7ff67fa1d5726c2a.tar.gz
pies-32a337f3b35c51ceb1d9320c7ff67fa1d5726c2a.tar.bz2
Select components to operate upon using conditional expression
* grecs: Upgrade. * src/ctl.c (restab): Split /programs in two endpoints. (eval_env): New members allowed_state, fun, total_count, success_count, and noperm_count. (pcond_type): pcond_and and pcond_or can take arbitrary number of arguments. All uses updated. (selector): Rewrite. * src/piesctl.c: All commands that operate on components take conditional expression as argument. (pcond_parse_and) (pcond_parse_or): Optimize consecutive operations.
Diffstat (limited to 'src/piesctl.c')
-rw-r--r--src/piesctl.c244
1 files changed, 156 insertions, 88 deletions
diff --git a/src/piesctl.c b/src/piesctl.c
index 8c4e2e1..4b448ad 100644
--- a/src/piesctl.c
+++ b/src/piesctl.c
@@ -1228,15 +1228,6 @@ peek_token (struct pcond_parser_state *state)
}
static struct json_value *
-json_new_array2 (struct json_value *a, struct json_value *b)
-{
- struct json_value *ret = json_new_array ();
- json_array_append (ret, a);
- json_array_append (ret, b);
- return ret;
-}
-
-static struct json_value *
json_encode_op (char const *op, struct json_value *arg)
{
struct json_value *ret = json_new_object ();
@@ -1300,11 +1291,48 @@ pcond_parse_unary (struct pcond_parser_state *state, struct json_value **ret)
}
}
+static int
+is_op (struct json_value *val, char const *opcode)
+{
+ struct json_value *op;
+
+ if (json_object_get (val, "op", &op) == 0)
+ {
+ if (op->type == json_string && strcmp (op->v.s, opcode) == 0)
+ return 1;
+ }
+ return 0;
+}
+
+static void
+binop_append_optimized (struct json_value *ar, struct json_value *val,
+ char const *opcode)
+{
+ if (is_op (val, opcode))
+ {
+ size_t i, n;
+ struct json_value *arg;
+
+ json_object_get (val, "arg", &arg);
+ n = json_array_size (arg);
+ for (i = 0; i < n; i++)
+ {
+ struct json_value *elt;
+ json_array_get (arg, i, &elt);
+ json_array_set (arg, i, NULL);
+ json_array_append (ar, elt);
+ }
+ json_value_free (val);
+ }
+ else
+ json_array_append (ar, val);
+}
+
static void
pcond_parse_and (struct pcond_parser_state *state, struct json_value **ret)
{
char const *token;
- struct json_value *left, *right;
+ struct json_value *left, *right, *ar;
pcond_parse_unary (state, &left);
token = peek_token (state);
@@ -1315,14 +1343,17 @@ pcond_parse_and (struct pcond_parser_state *state, struct json_value **ret)
}
next_token (state);
pcond_parse_and (state, &right);
- *ret = json_encode_op (token, json_new_array2 (left, right));
+ ar = json_new_array ();
+ json_array_append (ar, left);
+ binop_append_optimized (ar, right, token);
+ *ret = json_encode_op (token, ar);
}
static void
pcond_parse_or (struct pcond_parser_state *state, struct json_value **ret)
{
char const *token;
- struct json_value *left, *right;
+ struct json_value *left, *right, *ar;
pcond_parse_and (state, &left);
token = peek_token (state);
@@ -1333,7 +1364,10 @@ pcond_parse_or (struct pcond_parser_state *state, struct json_value **ret)
}
next_token (state);
pcond_parse_or (state, &right);
- *ret = json_encode_op (token, json_new_array2 (left, right));
+ ar = json_new_array ();
+ json_array_append (ar, left);
+ binop_append_optimized (ar, right, token);
+ *ret = json_encode_op (token, ar);
}
static struct json_value *
@@ -1369,34 +1403,21 @@ acc_writer (void *closure, char const *text, size_t len)
}
static char *
-json_to_string (struct json_value *val)
-{
- struct grecs_txtacc *acc = grecs_txtacc_create ();
- struct json_format fmt = {
- .indent = 0,
- .precision = 0,
- .write = acc_writer,
- .data = acc
- };
- char *ret;
-
- json_format_value (val, &fmt);
- grecs_txtacc_grow_char (acc, 0);
- ret = grecs_txtacc_finish (acc, 1);
- grecs_txtacc_free (acc);
-
- return ret;
-}
-
-static char *
-parse_condition_to_uri (char const *base, int argc, char **argv)
+parse_condition_to_uri (char const *base, int argc, char **argv, int mandatory)
{
char *ret = NULL;
struct grecs_txtacc *acc;
struct json_value *val;
+ if (mandatory && argc == 1)
+ {
+ grecs_error (NULL, 0, _("condition must be specified"));
+ exit (EX_USAGE);
+ }
+
acc = grecs_txtacc_create ();
- grecs_txtacc_grow_string (acc, base);
+ if (base)
+ grecs_txtacc_grow_string (acc, base);
val = parse_condition (argc, argv);
if (val)
@@ -1407,7 +1428,8 @@ parse_condition_to_uri (char const *base, int argc, char **argv)
.write = acc_writer,
.data = acc
};
- grecs_txtacc_grow_char (acc, '/');
+ if (base)
+ grecs_txtacc_grow_char (acc, '/');
json_format_value (val, &fmt);
json_value_free (val);
}
@@ -1415,14 +1437,13 @@ parse_condition_to_uri (char const *base, int argc, char **argv)
ret = grecs_txtacc_finish (acc, 1);
grecs_txtacc_free (acc);
-
return ret;
}
static int
com_list (struct shttp_connection *conn, int argc, char **argv)
{
- char *uri = parse_condition_to_uri ("/programs", argc, argv);
+ char *uri = parse_condition_to_uri ("/programs/select", argc, argv, 0);
shttp_io_init (&conn->req);
shttp_process (conn, METH_GET, uri);
@@ -1443,68 +1464,97 @@ com_list (struct shttp_connection *conn, int argc, char **argv)
}
static int
-com_stop (struct shttp_connection *conn, int argc, char **argv)
+json_object_get_type (struct json_value *obj, char const *name,
+ enum json_value_type type, struct json_value **ret)
{
- char *buf = NULL;
- size_t len = 0;
- size_t i;
-
- for (i = 1; i < argc; i++)
+ struct json_value *v;
+ int rc = json_object_get (obj, name, &v);
+ if (rc)
+ return rc;
+ if (v->type != type)
{
- struct json_value *v;
- grecs_asprintf (&buf, &len, "/programs/%s", argv[i]);
- shttp_io_init (&conn->req);
- shttp_process (conn, METH_DELETE, buf);
- v = shttp_getval (conn, "error_message", json_string);
- if (v)
- printf ("%s: %s\n", argv[i], v->v.s);
+ errno = EACCES;
+ return -1;
}
- free (buf);
+ *ret = v;
+ return 0;
+}
+
+static void
+shttp_print_response_status (struct shttp_connection *conn)
+{
+ if (!dump && conn->result && conn->result->type == json_arr)
+ {
+ size_t i, n = json_array_size (conn->result);
+
+ for (i = 0; i < n; i++)
+ {
+ struct json_value *elt, *v;
+
+ if (json_array_get (conn->result, i, &elt) == 0)
+ {
+ if (elt->type != json_object)
+ {
+ grecs_error (NULL, 0, _("%lu: unexpected value type"),
+ (unsigned long) i);
+ print_json (stderr, v);
+ continue;
+ }
+ if (json_object_get_type (elt, "tag", json_string, &v) == 0)
+ {
+ printf ("%s: ", v->v.s);
+ if (json_object_get_type (elt, "status", json_string, &v)
+ == 0)
+ {
+ if (strcmp (v->v.s, "OK") == 0)
+ fputs (v->v.s, stdout);
+ else if (json_object_get_type (elt, "error_message",
+ json_string, &v) == 0)
+ fputs (v->v.s, stdout);
+ else
+ printf ("unknown error");
+ }
+ else
+ printf ("unknown status");
+ fputc ('\n', stdout);
+ }
+ }
+ }
+ }
+}
+
+static int
+com_stop (struct shttp_connection *conn, int argc, char **argv)
+{
+ char *uri = parse_condition_to_uri ("/programs/select", argc, argv, 1);
+
+ shttp_io_init (&conn->req);
+ shttp_process (conn, METH_DELETE, uri);
+ grecs_free (uri);
+ shttp_print_response_status (conn);
return 0;
}
static int
com_start (struct shttp_connection *conn, int argc, char **argv)
{
- char *buf = NULL;
- size_t len = 0;
- size_t i;
-
- for (i = 1; i < argc; i++)
- {
- struct json_value *v;
- grecs_asprintf (&buf, &len, "/programs/%s", argv[i]);
- shttp_io_init (&conn->req);
- shttp_process (conn, METH_PUT, buf);
- v = shttp_getval (conn, "error_message", json_string);
- if (v)
- printf ("%s: %s\n", argv[i], v->v.s);
- }
- free (buf);
+ char *uri = parse_condition_to_uri ("/programs/select", argc, argv, 1);
+
+ shttp_io_init (&conn->req);
+ shttp_process (conn, METH_PUT, uri);
+ grecs_free (uri);
+ shttp_print_response_status (conn);
return 0;
}
static int
com_restart (struct shttp_connection *conn, int argc, char **argv)
{
- struct json_value *val = json_new_array ();
- size_t i;
-
- if (argc == 1)
- {
- grecs_error (NULL, 0, _("missing component names"));
- exit (EX_USAGE);
- }
- for (i = 1; i < argc; i++)
- json_array_append (val, json_new_string (argv[i]));
-
shttp_io_init (&conn->req);
- conn->req.content = json_to_string (val);
+ conn->req.content = parse_condition_to_uri (NULL, argc, argv, 1);
conn->req.content_length = strlen (conn->req.content);
- json_value_free (val);
-
- shttp_process (conn, METH_POST, "/programs");
-
+ shttp_process (conn, METH_POST, "/programs/select");
+ shttp_print_response_status (conn);
return 0;
}
@@ -1585,10 +1635,10 @@ struct comtab
};
static struct comtab comtab[] = {
- { "list", N_("[EXPR]"), N_("list configured components"), com_list },
- { "stop", N_("TAG [TAG...]"), N_("stop components"), com_stop },
- { "start", N_("TAG [TAG...]"), N_("start components"), com_start },
- { "restart", N_("TAG [TAG...]"), N_("restart components"), com_restart },
+ { "list", N_("[CONDITION]"), N_("list configured components"), com_list },
+ { "stop", N_("CONDITION"), N_("stop components"), com_stop },
+ { "start", N_("CONDITION"), N_("start components"), com_start },
+ { "restart", N_("CONDITION"), N_("restart components"), com_restart },
{ "id", N_("[KEYWORDS...]"),
N_("show info about the running GNU Pies instance"),
com_id },
@@ -1639,7 +1689,7 @@ command_help (FILE *fp)
{
struct comtab *cp;
- fputs (_("Available commands are:"), fp);
+ fputs (_("Available commands with their arguments are:"), fp);
fputc ('\n', fp);
fputc ('\n', fp);
@@ -1655,6 +1705,24 @@ command_help (FILE *fp)
fprintf (fp, " %s\n", gettext (cp->docstr));
}
fputc ('\n', fp);
+ fputs (_("Condition is defined as:"), fp);
+ fputc ('\n', fp);
+ fputc ('\n', fp);
+ fputs ("\
+ <condition> ::= <disjunction>\n\
+ <disjunction> ::= <conjunction> | <conjunction> \"or\" <disjunction>\n\
+ <conjunction> ::= <unary> | <unary> \"and\" <conjunction>\n\
+ <unary> ::= <term> | \"not\" <condition> | \"(\" <condition> \")\"\n\
+ <term> ::= \"all\" | <keyword> <value>\n\
+ <keyword> ::= \"type\" | \"mode\" | \"status\" | \"component\"\n\
+ <value> ::= <word> | <quoted-string>\n\
+ <word> ::= <printable> | <word> <printable>\n\
+ <printable> ::= \"A\" - \"Z\" | \"a\" - \"z\" | \"0\" - \"9\" |\n\
+ \"_\" | \".\" | \"*\" | \":\" | \"@\" | \"[\" | \"]\" | \"-\" | \"/\"\n\
+ <quoted-string> ::= \"\"\" <string> \"\"\"\n\
+ <string> ::= <char> | <string> <char>\n\
+ <char> ::= <any character except \"\\\" and \"\"\"> | \"\\\\\" | \"\\\"\"\n\n", fp);
+
}
int

Return to:

Send suggestions and report system problems to the System administrator.