HelenOS sources
This source file includes following definitions.
- main
- syntax_print
- ofw_print_subtree
- ofw_print_properties
#include <errno.h>
#include <ofw.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <str.h>
#define NAME "ofw"
#define MAX_NAME_LENGTH 1024
static void syntax_print(void);
static errno_t ofw_print_subtree(const char *, bool);
static errno_t ofw_print_properties(const char *);
int main(int argc, char **argv)
{
errno_t rc;
const char *path = "/";
bool verbose = false;
--argc;
++argv;
while (argc > 0 && argv[0][0] == '-') {
if (str_cmp(argv[0], "-p") == 0) {
--argc;
++argv;
if (argc < 1) {
printf("Option argument missing.\n");
return 1;
}
path = argv[0];
--argc;
++argv;
} else if (str_cmp(argv[0], "-v") == 0) {
--argc;
++argv;
verbose = true;
} else {
syntax_print();
return 1;
}
}
if (argc != 0) {
syntax_print();
return 1;
}
rc = ofw_print_subtree(path, verbose);
if (rc != EOK)
return 1;
return 0;
}
static void syntax_print(void)
{
printf("syntax: %s [<options>]\n", NAME);
printf("options:\n"
"\t-v Verbose mode (print properties and their values)\n"
"\t-p <path> Only print devices under <path>\n");
}
static errno_t ofw_print_subtree(const char *path, bool verbose)
{
char *subpath;
ofw_child_it_t it;
errno_t rc;
printf("%s\n", path);
if (verbose) {
rc = ofw_print_properties(path);
if (rc != EOK)
return rc;
}
rc = ofw_child_it_first(&it, path);
while (!ofw_child_it_end(&it)) {
rc = ofw_child_it_get_path(&it, &subpath);
if (rc != EOK)
goto error;
rc = ofw_print_subtree(subpath, verbose);
if (rc != EOK)
goto error;
free(subpath);
subpath = NULL;
ofw_child_it_next(&it);
}
ofw_child_it_fini(&it);
return EOK;
error:
if (subpath != NULL)
free(subpath);
ofw_child_it_fini(&it);
return rc;
}
static errno_t ofw_print_properties(const char *ofwpath)
{
const char *propname;
const uint8_t *propval;
size_t val_sz;
ofw_prop_it_t it;
errno_t rc;
size_t i;
rc = ofw_prop_it_first(&it, ofwpath);
if (rc != EOK)
return rc;
while (!ofw_prop_it_end(&it)) {
propname = ofw_prop_it_get_name(&it);
printf("'%s' =", propname);
propval = ofw_prop_it_get_data(&it, &val_sz);
for (i = 0; i < val_sz; i++)
printf(" %02x", propval[i]);
printf(" ('%*s')\n", (int)val_sz - 1, (char *)propval);
ofw_prop_it_next(&it);
}
ofw_prop_it_fini(&it);
return EOK;
}
HelenOS homepage, sources at GitHub