diff mbox

[U-Boot] dm_test: improve the appearance shown by "dm tree" command

Message ID 1417248116-1767-1-git-send-email-yamada.m@jp.panasonic.com
State Accepted
Delegated to: Simon Glass
Headers show

Commit Message

Masahiro Yamada Nov. 29, 2014, 8:01 a.m. UTC
The command "dm tree" lists devices in a tree-like format.
This commit makes it look more like what the Unix command "tree"
shows.

=> dm tree
 Class       Probed   Name
----------------------------------------
 root        [ + ]    root_driver
 demo        [   ]    |-- demo_shape_drv
 demo        [   ]    |-- demo_simple_drv
 demo        [   ]    |-- demo_shape_drv
 demo        [   ]    |-- demo_simple_drv
 demo        [   ]    |-- demo_shape_drv
 test        [   ]    |-- test_drv
 test        [   ]    |-- test_drv
 test        [   ]    |-- test_drv
 gpio        [   ]    |-- gpio_sandbox
 serial      [   ]    |-- serial_sandbox
 serial      [ + ]    |-- serial
 demo        [   ]    |-- triangle
 demo        [   ]    |-- square
 demo        [   ]    |-- hexagon
 gpio        [   ]    |-- gpios
 spi         [   ]    |-- spi@0
 spi_emul    [   ]    |   `-- flash@0
 cros_ec     [ + ]    `-- cros-ec@0

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
---

This commit uses strlcpy().

Please apply the following first:
http://patchwork.ozlabs.org/patch/412670/


 test/dm/cmd_dm.c | 103 ++++++++++++++++++++++++++++---------------------------
 1 file changed, 52 insertions(+), 51 deletions(-)

Comments

Simon Glass Nov. 30, 2014, 3:22 a.m. UTC | #1
On 29 November 2014 at 01:01, Masahiro Yamada <yamada.m@jp.panasonic.com> wrote:
> The command "dm tree" lists devices in a tree-like format.
> This commit makes it look more like what the Unix command "tree"
> shows.
>
> => dm tree
>  Class       Probed   Name
> ----------------------------------------
>  root        [ + ]    root_driver
>  demo        [   ]    |-- demo_shape_drv
>  demo        [   ]    |-- demo_simple_drv
>  demo        [   ]    |-- demo_shape_drv
>  demo        [   ]    |-- demo_simple_drv
>  demo        [   ]    |-- demo_shape_drv
>  test        [   ]    |-- test_drv
>  test        [   ]    |-- test_drv
>  test        [   ]    |-- test_drv
>  gpio        [   ]    |-- gpio_sandbox
>  serial      [   ]    |-- serial_sandbox
>  serial      [ + ]    |-- serial
>  demo        [   ]    |-- triangle
>  demo        [   ]    |-- square
>  demo        [   ]    |-- hexagon
>  gpio        [   ]    |-- gpios
>  spi         [   ]    |-- spi@0
>  spi_emul    [   ]    |   `-- flash@0
>  cros_ec     [ + ]    `-- cros-ec@0
>
> Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
> ---
>
> This commit uses strlcpy().
>
> Please apply the following first:
> http://patchwork.ozlabs.org/patch/412670/

Acked-by: Simon Glass <sjg@chromium.org>
Simon Glass Nov. 30, 2014, 3:29 a.m. UTC | #2
On 29 November 2014 at 20:22, Simon Glass <sjg@chromium.org> wrote:
> On 29 November 2014 at 01:01, Masahiro Yamada <yamada.m@jp.panasonic.com> wrote:
>> The command "dm tree" lists devices in a tree-like format.
>> This commit makes it look more like what the Unix command "tree"
>> shows.
>>
>> => dm tree
>>  Class       Probed   Name
>> ----------------------------------------
>>  root        [ + ]    root_driver
>>  demo        [   ]    |-- demo_shape_drv
>>  demo        [   ]    |-- demo_simple_drv
>>  demo        [   ]    |-- demo_shape_drv
>>  demo        [   ]    |-- demo_simple_drv
>>  demo        [   ]    |-- demo_shape_drv
>>  test        [   ]    |-- test_drv
>>  test        [   ]    |-- test_drv
>>  test        [   ]    |-- test_drv
>>  gpio        [   ]    |-- gpio_sandbox
>>  serial      [   ]    |-- serial_sandbox
>>  serial      [ + ]    |-- serial
>>  demo        [   ]    |-- triangle
>>  demo        [   ]    |-- square
>>  demo        [   ]    |-- hexagon
>>  gpio        [   ]    |-- gpios
>>  spi         [   ]    |-- spi@0
>>  spi_emul    [   ]    |   `-- flash@0
>>  cros_ec     [ + ]    `-- cros-ec@0
>>
>> Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>
>> ---
>>
>> This commit uses strlcpy().
>>
>> Please apply the following first:
>> http://patchwork.ozlabs.org/patch/412670/
>
> Acked-by: Simon Glass <sjg@chromium.org>

Applied to u-boot-dm, thanks!
diff mbox

Patch

diff --git a/test/dm/cmd_dm.c b/test/dm/cmd_dm.c
index 26980d2..79a674e 100644
--- a/test/dm/cmd_dm.c
+++ b/test/dm/cmd_dm.c
@@ -16,17 +16,65 @@ 
 #include <dm/test.h>
 #include <dm/uclass-internal.h>
 
+static void show_devices(struct udevice *dev, int depth, int last_flag)
+{
+	int i, is_last;
+	struct udevice *child;
+	char class_name[12];
+
+	/* print the first 11 characters to not break the tree-format. */
+	strlcpy(class_name, dev->uclass->uc_drv->name, sizeof(class_name));
+	printf(" %-11s [ %c ]    ", class_name,
+	       dev->flags & DM_FLAG_ACTIVATED ? '+' : ' ');
+
+	for (i = depth; i >= 0; i--) {
+		is_last = (last_flag >> i) & 1;
+		if (i) {
+			if (is_last)
+				printf("    ");
+			else
+				printf("|   ");
+		} else {
+			if (is_last)
+				printf("`-- ");
+			else
+				printf("|-- ");
+		}
+	}
+
+	printf("%s\n", dev->name);
+
+	list_for_each_entry(child, &dev->child_head, sibling_node) {
+		is_last = list_is_last(&child->sibling_node, &dev->child_head);
+		show_devices(child, depth + 1, (last_flag << 1) | is_last);
+	}
+}
+
+static int do_dm_dump_all(cmd_tbl_t *cmdtp, int flag, int argc,
+			  char * const argv[])
+{
+	struct udevice *root;
+
+	root = dm_root();
+	if (root) {
+		printf(" Class       Probed   Name\n");
+		printf("----------------------------------------\n");
+		show_devices(root, -1, 0);
+	}
+
+	return 0;
+}
+
 /**
  * dm_display_line() - Display information about a single device
  *
  * Displays a single line of information with an option prefix
  *
  * @dev:	Device to display
- * @buf:	Prefix to display at the start of the line
  */
-static void dm_display_line(struct udevice *dev, char *buf)
+static void dm_display_line(struct udevice *dev)
 {
-	printf("%s- %c %s @ %08lx", buf,
+	printf("- %c %s @ %08lx",
 	       dev->flags & DM_FLAG_ACTIVATED ? '*' : ' ',
 	       dev->name, (ulong)map_to_sysmem(dev));
 	if (dev->req_seq != -1)
@@ -34,53 +82,6 @@  static void dm_display_line(struct udevice *dev, char *buf)
 	puts("\n");
 }
 
-static int display_succ(struct udevice *in, char *buf)
-{
-	int len;
-	int ip = 0;
-	char local[16];
-	struct udevice *pos, *n, *prev = NULL;
-
-	dm_display_line(in, buf);
-
-	if (list_empty(&in->child_head))
-		return 0;
-
-	len = strlen(buf);
-	strncpy(local, buf, sizeof(local));
-	snprintf(local + len, 2, "|");
-	if (len && local[len - 1] == '`')
-		local[len - 1] = ' ';
-
-	list_for_each_entry_safe(pos, n, &in->child_head, sibling_node) {
-		if (ip++)
-			display_succ(prev, local);
-		prev = pos;
-	}
-
-	snprintf(local + len, 2, "`");
-	display_succ(prev, local);
-
-	return 0;
-}
-
-static int dm_dump(struct udevice *dev)
-{
-	if (!dev)
-		return -EINVAL;
-	return display_succ(dev, "");
-}
-
-static int do_dm_dump_all(cmd_tbl_t *cmdtp, int flag, int argc,
-			  char * const argv[])
-{
-	struct udevice *root;
-
-	root = dm_root();
-	printf("ROOT %08lx\n", (ulong)map_to_sysmem(root));
-	return dm_dump(root);
-}
-
 static int do_dm_dump_uclass(cmd_tbl_t *cmdtp, int flag, int argc,
 			     char * const argv[])
 {
@@ -99,7 +100,7 @@  static int do_dm_dump_uclass(cmd_tbl_t *cmdtp, int flag, int argc,
 		if (list_empty(&uc->dev_head))
 			continue;
 		list_for_each_entry(dev, &uc->dev_head, uclass_node) {
-			dm_display_line(dev, "");
+			dm_display_line(dev);
 		}
 		puts("\n");
 	}