mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 2/6] complete: Add devicetree completion
Date: Mon, 19 May 2014 22:59:40 +0200	[thread overview]
Message-ID: <1400533184-668-3-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1400533184-668-1-git-send-email-s.hauer@pengutronix.de>

The of_* commands take devicetree nodes as parameters. Add a
devicetree completion function to ease passing nodes to these
commands.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/of_node.c     |  2 ++
 commands/of_property.c |  2 ++
 commands/oftree.c      |  2 ++
 common/complete.c      | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++
 include/complete.h     |  5 +++-
 5 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/commands/of_node.c b/commands/of_node.c
index 901da88..4962e52 100644
--- a/commands/of_node.c
+++ b/commands/of_node.c
@@ -21,6 +21,7 @@
 #include <environment.h>
 #include <fdt.h>
 #include <of.h>
+#include <complete.h>
 #include <command.h>
 #include <fs.h>
 #include <malloc.h>
@@ -104,5 +105,6 @@ BAREBOX_CMD_START(of_node)
 	BAREBOX_CMD_DESC("create/delete nodes in the device tree")
 	BAREBOX_CMD_OPTS("[-cd] NODE NAME")
 	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+	BAREBOX_CMD_COMPLETE(devicetree_complete)
 	BAREBOX_CMD_HELP(cmd_of_node_help)
 BAREBOX_CMD_END
diff --git a/commands/of_property.c b/commands/of_property.c
index f9f35b2..e0154fc 100644
--- a/commands/of_property.c
+++ b/commands/of_property.c
@@ -24,6 +24,7 @@
 #include <command.h>
 #include <fs.h>
 #include <malloc.h>
+#include <complete.h>
 #include <linux/ctype.h>
 #include <asm/byteorder.h>
 #include <errno.h>
@@ -317,5 +318,6 @@ BAREBOX_CMD_START(of_property)
 	BAREBOX_CMD_DESC("handle device tree properties")
 	BAREBOX_CMD_OPTS("[-sd] NODE [PROPERTY] [VALUES]")
 	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
+	BAREBOX_CMD_COMPLETE(devicetree_complete)
 	BAREBOX_CMD_HELP(cmd_of_property_help)
 BAREBOX_CMD_END
diff --git a/commands/oftree.c b/commands/oftree.c
index 3253cf1..16648d6 100644
--- a/commands/oftree.c
+++ b/commands/oftree.c
@@ -37,6 +37,7 @@
 #include <getopt.h>
 #include <init.h>
 #include <fcntl.h>
+#include <complete.h>
 
 static int do_oftree(int argc, char *argv[])
 {
@@ -207,4 +208,5 @@ BAREBOX_CMD_START(oftree)
 	BAREBOX_CMD_OPTS("[-lpfdn] [DTB]")
 	BAREBOX_CMD_GROUP(CMD_GRP_MISC)
 	BAREBOX_CMD_HELP(cmd_oftree_help)
+	BAREBOX_CMD_COMPLETE(devicetree_file_complete)
 BAREBOX_CMD_END
diff --git a/common/complete.c b/common/complete.c
index 368321f..5b71f03 100644
--- a/common/complete.c
+++ b/common/complete.c
@@ -204,6 +204,77 @@ int command_var_complete(struct string_list *sl, char *instr)
 }
 EXPORT_SYMBOL(command_var_complete);
 
+int devicetree_alias_complete(struct string_list *sl, char *instr)
+{
+	struct device_node *aliases;
+	struct property *p;
+
+	aliases = of_find_node_by_path("/aliases");
+	if (!aliases)
+		return 0;
+
+	list_for_each_entry(p, &aliases->properties, list) {
+		if (strncmp(instr, p->name, strlen(instr)))
+			continue;
+
+		string_list_add_asprintf(sl, "%s ", p->name);
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL(devicetree_alias_complete);
+
+int devicetree_nodepath_complete(struct string_list *sl, char *instr)
+{
+	struct device_node *node, *child;
+	char *dirn, *base;
+	char *path = strdup(instr);
+
+	if (*instr == '/') {
+		dirn = dirname(path);
+		base = basename(instr);
+		node = of_find_node_by_path(dirn);
+		if (!node)
+			goto out;
+	} else if (!*instr) {
+		node = of_get_root_node();
+		if (!node)
+			goto out;
+		base = "";
+	} else {
+		goto out;
+	}
+
+	for_each_child_of_node(node, child) {
+		if (strncmp(base, child->name, strlen(base)))
+			continue;
+		string_list_add_asprintf(sl, "%s/", child->full_name);
+	}
+out:
+	free(path);
+
+	return 0;
+}
+EXPORT_SYMBOL(devicetree_nodepath_complete);
+
+int devicetree_complete(struct string_list *sl, char *instr)
+{
+	devicetree_nodepath_complete(sl, instr);
+	devicetree_alias_complete(sl, instr);
+
+	return 0;
+}
+EXPORT_SYMBOL(devicetree_complete);
+
+int devicetree_file_complete(struct string_list *sl, char *instr)
+{
+	devicetree_complete(sl, instr);
+	file_complete(sl, instr, 0);
+
+	return 0;
+}
+EXPORT_SYMBOL(devicetree_file_complete);
+
 static int env_param_complete(struct string_list *sl, char *instr, int eval)
 {
 	struct device_d *dev;
diff --git a/include/complete.h b/include/complete.h
index 33b848c..491ba66 100644
--- a/include/complete.h
+++ b/include/complete.h
@@ -17,6 +17,9 @@ int empty_complete(struct string_list *sl, char *instr);
 int eth_complete(struct string_list *sl, char *instr);
 int command_var_complete(struct string_list *sl, char *instr);
 int devfs_partition_complete(struct string_list *sl, char *instr);
+int devicetree_alias_complete(struct string_list *sl, char *instr);
+int devicetree_nodepath_complete(struct string_list *sl, char *instr);
+int devicetree_complete(struct string_list *sl, char *instr);
+int devicetree_file_complete(struct string_list *sl, char *instr);
 
 #endif /* __COMPLETE_ */
-
-- 
2.0.0.rc0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2014-05-19 21:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-19 20:59 [PATCH] devicetree command changes Sascha Hauer
2014-05-19 20:59 ` [PATCH 1/6] complete: Fix completion after options Sascha Hauer
2014-05-19 20:59 ` Sascha Hauer [this message]
2014-05-19 20:59 ` [PATCH 3/6] commands: add of_dump command Sascha Hauer
2014-05-21 13:17   ` Holger Schurig
2014-05-22  6:10     ` Sascha Hauer
2014-05-19 20:59 ` [PATCH 4/6] oftree: remove dump support Sascha Hauer
2014-05-20  8:01   ` Alexander Aring
2014-05-20  8:03     ` Alexander Aring
2014-05-20  9:08       ` Sascha Hauer
2014-05-19 20:59 ` [PATCH 5/6] of: Drop devicetree merge support Sascha Hauer
2014-05-20 15:08   ` Sebastian Hesselbarth
2014-05-21  6:13     ` Sascha Hauer
2014-05-21  6:35     ` Esben Haabendal
2014-05-21 12:39       ` Sascha Hauer
2014-05-21 13:24   ` Holger Schurig
2014-05-19 20:59 ` [PATCH 6/6] oftree command: make devicetree file optargs to -l/-s Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1400533184-668-3-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox