[a3eeef45] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup sysinfo
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file sysinfo.c
|
---|
| 33 | * @brief Print value of item from sysinfo tree.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <stdio.h>
|
---|
[582a0b8] | 38 | #include <stddef.h>
|
---|
[8d2dd7f2] | 39 | #include <stdint.h>
|
---|
[a3eeef45] | 40 | #include <sysinfo.h>
|
---|
[38d150e] | 41 | #include <stdlib.h>
|
---|
[a3eeef45] | 42 |
|
---|
[76f382b] | 43 | static void dump_bytes_hex(char *data, size_t size)
|
---|
[a3eeef45] | 44 | {
|
---|
[76f382b] | 45 | for (size_t i = 0; i < size; i++) {
|
---|
| 46 | if (i > 0)
|
---|
| 47 | putchar(' ');
|
---|
| 48 | printf("0x%02x", (uint8_t) data[i]);
|
---|
[a3eeef45] | 49 | }
|
---|
[76f382b] | 50 | }
|
---|
[a3eeef45] | 51 |
|
---|
[76f382b] | 52 | static void dump_bytes_text(char *data, size_t size)
|
---|
| 53 | {
|
---|
| 54 | size_t offset = 0;
|
---|
| 55 |
|
---|
| 56 | while (offset < size) {
|
---|
| 57 | wchar_t c = str_decode(data, &offset, size);
|
---|
| 58 | printf("%lc", (wint_t) c);
|
---|
[a3eeef45] | 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[46577995] | 62 | static errno_t print_item_val(char *ipath)
|
---|
[a3eeef45] | 63 | {
|
---|
| 64 | sysarg_t value;
|
---|
[46577995] | 65 | errno_t rc = sysinfo_get_value(ipath, &value);
|
---|
[a3eeef45] | 66 | if (rc != EOK) {
|
---|
| 67 | printf("Error reading item '%s'.\n", ipath);
|
---|
| 68 | return rc;
|
---|
| 69 | }
|
---|
[76f382b] | 70 |
|
---|
[a3eeef45] | 71 | printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
|
---|
| 72 | (uint64_t) value, (uint64_t) value);
|
---|
[76f382b] | 73 |
|
---|
[a3eeef45] | 74 | return EOK;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | static int print_item_data(char *ipath)
|
---|
| 78 | {
|
---|
| 79 | size_t size;
|
---|
[76f382b] | 80 | void *data = sysinfo_get_data(ipath, &size);
|
---|
[a3eeef45] | 81 | if (data == NULL) {
|
---|
| 82 | printf("Error reading item '%s'.\n", ipath);
|
---|
| 83 | return -1;
|
---|
| 84 | }
|
---|
[76f382b] | 85 |
|
---|
[a3eeef45] | 86 | printf("%s -> ", ipath);
|
---|
| 87 | dump_bytes_hex(data, size);
|
---|
| 88 | fputs(" ('", stdout);
|
---|
| 89 | dump_bytes_text(data, size);
|
---|
| 90 | fputs("')\n", stdout);
|
---|
[76f382b] | 91 |
|
---|
[84a1a54] | 92 | return 0;
|
---|
[a3eeef45] | 93 | }
|
---|
| 94 |
|
---|
[0499235] | 95 | static int print_item_property(char *ipath, char *iprop)
|
---|
| 96 | {
|
---|
| 97 | size_t size;
|
---|
| 98 | void *data = sysinfo_get_property(ipath, iprop, &size);
|
---|
| 99 | if (data == NULL) {
|
---|
| 100 | printf("Error reading property '%s' of item '%s'.\n", iprop,
|
---|
| 101 | ipath);
|
---|
| 102 | return -1;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | printf("%s property %s -> ", ipath, iprop);
|
---|
| 106 | dump_bytes_hex(data, size);
|
---|
| 107 | fputs(" ('", stdout);
|
---|
| 108 | dump_bytes_text(data, size);
|
---|
| 109 | fputs("')\n", stdout);
|
---|
| 110 |
|
---|
[84a1a54] | 111 | return 0;
|
---|
[0499235] | 112 | }
|
---|
| 113 |
|
---|
[efb8d15] | 114 | static void print_spaces(size_t spaces)
|
---|
| 115 | {
|
---|
| 116 | for (size_t i = 0; i < spaces; i++)
|
---|
| 117 | printf(" ");
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | static void print_keys(const char *path, size_t spaces)
|
---|
[a3eeef45] | 121 | {
|
---|
[76f382b] | 122 | size_t size;
|
---|
| 123 | char *keys = sysinfo_get_keys(path, &size);
|
---|
| 124 | if ((keys == NULL) || (size == 0))
|
---|
| 125 | return;
|
---|
| 126 |
|
---|
| 127 | size_t pos = 0;
|
---|
| 128 | while (pos < size) {
|
---|
| 129 | /* Process each key with sanity checks */
|
---|
| 130 | size_t cur_size = str_nsize(keys + pos, size - pos);
|
---|
[efb8d15] | 131 | if (keys[pos + cur_size] != 0)
|
---|
| 132 | break;
|
---|
| 133 |
|
---|
[76f382b] | 134 | size_t path_size = str_size(path) + cur_size + 2;
|
---|
| 135 | char *cur_path = (char *) malloc(path_size);
|
---|
| 136 | if (cur_path == NULL)
|
---|
| 137 | break;
|
---|
| 138 |
|
---|
[efb8d15] | 139 | size_t length;
|
---|
| 140 |
|
---|
| 141 | if (path[0] != 0) {
|
---|
| 142 | print_spaces(spaces);
|
---|
| 143 | printf(".%s\n", keys + pos);
|
---|
| 144 | length = str_length(keys + pos) + 1;
|
---|
| 145 |
|
---|
[76f382b] | 146 | snprintf(cur_path, path_size, "%s.%s", path, keys + pos);
|
---|
[efb8d15] | 147 | } else {
|
---|
| 148 | printf("%s\n", keys + pos);
|
---|
| 149 | length = str_length(keys + pos);
|
---|
| 150 |
|
---|
[76f382b] | 151 | snprintf(cur_path, path_size, "%s", keys + pos);
|
---|
[efb8d15] | 152 | }
|
---|
[76f382b] | 153 |
|
---|
[efb8d15] | 154 | print_keys(cur_path, spaces + length);
|
---|
[76f382b] | 155 |
|
---|
| 156 | free(cur_path);
|
---|
| 157 | pos += cur_size + 1;
|
---|
[a3eeef45] | 158 | }
|
---|
[76f382b] | 159 |
|
---|
| 160 | free(keys);
|
---|
[a3eeef45] | 161 | }
|
---|
| 162 |
|
---|
[76f382b] | 163 | int main(int argc, char *argv[])
|
---|
[a3eeef45] | 164 | {
|
---|
[0499235] | 165 | int rc = 0;
|
---|
| 166 |
|
---|
| 167 | if (argc < 2) {
|
---|
[76f382b] | 168 | /* Print keys */
|
---|
[efb8d15] | 169 | print_keys("", 0);
|
---|
[0499235] | 170 | return rc;
|
---|
[a3eeef45] | 171 | }
|
---|
[76f382b] | 172 |
|
---|
| 173 | char *ipath = argv[1];
|
---|
| 174 |
|
---|
[0499235] | 175 | if (argc < 3) {
|
---|
| 176 | sysinfo_item_val_type_t tag = sysinfo_get_val_type(ipath);
|
---|
| 177 |
|
---|
| 178 | switch (tag) {
|
---|
| 179 | case SYSINFO_VAL_UNDEFINED:
|
---|
| 180 | printf("Error: Sysinfo item '%s' not defined.\n", ipath);
|
---|
| 181 | rc = 2;
|
---|
| 182 | break;
|
---|
| 183 | case SYSINFO_VAL_VAL:
|
---|
[84a1a54] | 184 | rc = EXIT_RC(print_item_val(ipath));
|
---|
[0499235] | 185 | break;
|
---|
| 186 | case SYSINFO_VAL_DATA:
|
---|
| 187 | rc = print_item_data(ipath);
|
---|
| 188 | break;
|
---|
| 189 | default:
|
---|
| 190 | printf("Error: Sysinfo item '%s' with unknown value type.\n",
|
---|
| 191 | ipath);
|
---|
| 192 | rc = 2;
|
---|
| 193 | break;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | return rc;
|
---|
[76f382b] | 197 | }
|
---|
| 198 |
|
---|
[0499235] | 199 | char *iprop = argv[2];
|
---|
| 200 | rc = print_item_property(ipath, iprop);
|
---|
[76f382b] | 201 | return rc;
|
---|
[a3eeef45] | 202 | }
|
---|
| 203 |
|
---|
| 204 | /** @}
|
---|
| 205 | */
|
---|