summaryrefslogtreecommitdiffstats
path: root/discover
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2013-09-17 21:54:38 +0800
committerJeremy Kerr <jk@ozlabs.org>2013-09-24 13:14:59 +0800
commit01294f44e244b3c3ab5e19e98a8f98d8eee93922 (patch)
tree857ab626d0f47a29b16f150e2ce828d9e732415e /discover
parent27037bcd151ff8ecad1aaca7eaa06fb5c3f9fd1f (diff)
downloadtalos-petitboot-01294f44e244b3c3ab5e19e98a8f98d8eee93922.tar.gz
talos-petitboot-01294f44e244b3c3ab5e19e98a8f98d8eee93922.zip
discover/grub2: Implement test builtin
A faily simple implementation now, and could do with some testing... Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'discover')
-rw-r--r--discover/grub2/builtins.c118
1 files changed, 117 insertions, 1 deletions
diff --git a/discover/grub2/builtins.c b/discover/grub2/builtins.c
index 4261eeb..1ce63cf 100644
--- a/discover/grub2/builtins.c
+++ b/discover/grub2/builtins.c
@@ -123,6 +123,114 @@ static int builtin_search(struct grub2_script *script,
return 0;
}
+static bool builtin_test_op(int argc, char **argv, int *consumed)
+{
+ char *op;
+
+ if (argc >= 3) {
+ const char *a1, *a2;
+
+ a1 = argv[0];
+ op = argv[1];
+ a2 = argv[2];
+
+ if (!strcmp(op, "=") || !strcmp(op, "==")) {
+ *consumed = 3;
+ return !strcmp(a1, a2);
+ }
+
+ if (!strcmp(op, "!=")) {
+ *consumed = 3;
+ return strcmp(a1, a2);
+ }
+
+ if (!strcmp(op, "<")) {
+ *consumed = 3;
+ return strcmp(a1, a2) < 0;
+ }
+
+ if (!strcmp(op, ">")) {
+ *consumed = 3;
+ return strcmp(a1, a2) > 0;
+ }
+ }
+
+ if (argc >= 2) {
+ const char *a1;
+
+ op = argv[0];
+ a1 = argv[1];
+
+ if (!strcmp(op, "-z")) {
+ *consumed = 2;
+ return strlen(a1) == 0;
+ }
+
+ if (!strcmp(op, "-n")) {
+ *consumed = 2;
+ return strlen(a1) != 0;
+ }
+
+ /* todo: implement file checks */
+ if (!strcmp(op, "-s") || !strcmp(op, "-f")) {
+ *consumed = 2;
+ return false;
+ }
+ }
+
+ *consumed = 1;
+ return strlen(op) > 0;
+}
+
+static int builtin_test(struct grub2_script *script __attribute__((unused)),
+ void *data __attribute__((unused)),
+ int argc, char *argv[])
+{
+ int consumed;
+ bool not, rc;
+
+ if (!strcmp(argv[0], "[") && !strcmp(argv[argc - 1], "]"))
+ argc--;
+
+ /* skip command name */
+ argc--;
+ argv++;
+
+ not = false;
+ rc = false;
+
+ for (consumed = 0; argc > 0; argv += consumed, argc -= consumed) {
+
+ if (!strcmp(argv[0], "!")) {
+ not = true;
+ consumed = 1;
+ continue;
+ }
+
+ if (!strcmp(argv[0], "-a")) {
+ if (!rc)
+ return 1;
+ consumed = 1;
+ continue;
+ }
+
+ if (!strcmp(argv[0], "-o")) {
+ if (rc)
+ return 0;
+ consumed = 1;
+ continue;
+ }
+
+ rc = builtin_test_op(argc, argv, &consumed);
+ if (not) {
+ rc = !rc;
+ not = false;
+ }
+ }
+
+ return rc ? 0 : 1;
+}
+
static int builtin_nop(struct grub2_script *script __attribute__((unused)),
void *data __attribute__((unused)),
int argc __attribute__((unused)),
@@ -150,7 +258,15 @@ static struct {
{
.name = "search",
.fn = builtin_search,
- }
+ },
+ {
+ .name = "[",
+ .fn = builtin_test,
+ },
+ {
+ .name = "test",
+ .fn = builtin_test,
+ },
};
static const char *nops[] = {
OpenPOWER on IntegriCloud