[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>
|
---|
[1d6dd2a] | 42 | #include <str.h>
|
---|
[a3eeef45] | 43 |
|
---|
[76f382b] | 44 | static void dump_bytes_hex(char *data, size_t size)
|
---|
[a3eeef45] | 45 | {
|
---|
[76f382b] | 46 | for (size_t i = 0; i < size; i++) {
|
---|
| 47 | if (i > 0)
|
---|
| 48 | putchar(' ');
|
---|
| 49 | printf("0x%02x", (uint8_t) data[i]);
|
---|
[a3eeef45] | 50 | }
|
---|
[76f382b] | 51 | }
|
---|
[a3eeef45] | 52 |
|
---|
[76f382b] | 53 | static void dump_bytes_text(char *data, size_t size)
|
---|
| 54 | {
|
---|
| 55 | size_t offset = 0;
|
---|
[a35b458] | 56 |
|
---|
[76f382b] | 57 | while (offset < size) {
|
---|
[28a5ebd] | 58 | char32_t c = str_decode(data, &offset, size);
|
---|
[76f382b] | 59 | printf("%lc", (wint_t) c);
|
---|
[a3eeef45] | 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[b7fd2a0] | 63 | static errno_t print_item_val(char *ipath)
|
---|
[a3eeef45] | 64 | {
|
---|
| 65 | sysarg_t value;
|
---|
[b7fd2a0] | 66 | errno_t rc = sysinfo_get_value(ipath, &value);
|
---|
[a3eeef45] | 67 | if (rc != EOK) {
|
---|
| 68 | printf("Error reading item '%s'.\n", ipath);
|
---|
| 69 | return rc;
|
---|
| 70 | }
|
---|
[a35b458] | 71 |
|
---|
[a3eeef45] | 72 | printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
|
---|
| 73 | (uint64_t) value, (uint64_t) value);
|
---|
[a35b458] | 74 |
|
---|
[a3eeef45] | 75 | return EOK;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static int print_item_data(char *ipath)
|
---|
| 79 | {
|
---|
| 80 | size_t size;
|
---|
[76f382b] | 81 | void *data = sysinfo_get_data(ipath, &size);
|
---|
[a3eeef45] | 82 | if (data == NULL) {
|
---|
| 83 | printf("Error reading item '%s'.\n", ipath);
|
---|
| 84 | return -1;
|
---|
| 85 | }
|
---|
[a35b458] | 86 |
|
---|
[a3eeef45] | 87 | printf("%s -> ", ipath);
|
---|
| 88 | dump_bytes_hex(data, size);
|
---|
| 89 | fputs(" ('", stdout);
|
---|
| 90 | dump_bytes_text(data, size);
|
---|
| 91 | fputs("')\n", stdout);
|
---|
[a35b458] | 92 |
|
---|
[a3eeef45] | 93 | return EOK;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[0499235] | 96 | static int print_item_property(char *ipath, char *iprop)
|
---|
| 97 | {
|
---|
| 98 | size_t size;
|
---|
| 99 | void *data = sysinfo_get_property(ipath, iprop, &size);
|
---|
| 100 | if (data == NULL) {
|
---|
| 101 | printf("Error reading property '%s' of item '%s'.\n", iprop,
|
---|
| 102 | ipath);
|
---|
| 103 | return -1;
|
---|
| 104 | }
|
---|
[a35b458] | 105 |
|
---|
[0499235] | 106 | printf("%s property %s -> ", ipath, iprop);
|
---|
| 107 | dump_bytes_hex(data, size);
|
---|
| 108 | fputs(" ('", stdout);
|
---|
| 109 | dump_bytes_text(data, size);
|
---|
| 110 | fputs("')\n", stdout);
|
---|
[a35b458] | 111 |
|
---|
[0499235] | 112 | return EOK;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[efb8d15] | 115 | static void print_spaces(size_t spaces)
|
---|
| 116 | {
|
---|
| 117 | for (size_t i = 0; i < spaces; i++)
|
---|
| 118 | printf(" ");
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | static void print_keys(const char *path, size_t spaces)
|
---|
[a3eeef45] | 122 | {
|
---|
[76f382b] | 123 | size_t size;
|
---|
| 124 | char *keys = sysinfo_get_keys(path, &size);
|
---|
| 125 | if ((keys == NULL) || (size == 0))
|
---|
| 126 | return;
|
---|
[a35b458] | 127 |
|
---|
[76f382b] | 128 | size_t pos = 0;
|
---|
| 129 | while (pos < size) {
|
---|
| 130 | /* Process each key with sanity checks */
|
---|
| 131 | size_t cur_size = str_nsize(keys + pos, size - pos);
|
---|
[efb8d15] | 132 | if (keys[pos + cur_size] != 0)
|
---|
| 133 | break;
|
---|
[a35b458] | 134 |
|
---|
[76f382b] | 135 | size_t path_size = str_size(path) + cur_size + 2;
|
---|
| 136 | char *cur_path = (char *) malloc(path_size);
|
---|
| 137 | if (cur_path == NULL)
|
---|
| 138 | break;
|
---|
[a35b458] | 139 |
|
---|
[efb8d15] | 140 | size_t length;
|
---|
[a35b458] | 141 |
|
---|
[efb8d15] | 142 | if (path[0] != 0) {
|
---|
| 143 | print_spaces(spaces);
|
---|
| 144 | printf(".%s\n", keys + pos);
|
---|
| 145 | length = str_length(keys + pos) + 1;
|
---|
[a35b458] | 146 |
|
---|
[76f382b] | 147 | snprintf(cur_path, path_size, "%s.%s", path, keys + pos);
|
---|
[efb8d15] | 148 | } else {
|
---|
| 149 | printf("%s\n", keys + pos);
|
---|
| 150 | length = str_length(keys + pos);
|
---|
[a35b458] | 151 |
|
---|
[76f382b] | 152 | snprintf(cur_path, path_size, "%s", keys + pos);
|
---|
[efb8d15] | 153 | }
|
---|
[a35b458] | 154 |
|
---|
[efb8d15] | 155 | print_keys(cur_path, spaces + length);
|
---|
[a35b458] | 156 |
|
---|
[76f382b] | 157 | free(cur_path);
|
---|
| 158 | pos += cur_size + 1;
|
---|
[a3eeef45] | 159 | }
|
---|
[a35b458] | 160 |
|
---|
[76f382b] | 161 | free(keys);
|
---|
[a3eeef45] | 162 | }
|
---|
| 163 |
|
---|
[76f382b] | 164 | int main(int argc, char *argv[])
|
---|
[a3eeef45] | 165 | {
|
---|
[0499235] | 166 | int rc = 0;
|
---|
[a35b458] | 167 |
|
---|
[0499235] | 168 | if (argc < 2) {
|
---|
[76f382b] | 169 | /* Print keys */
|
---|
[efb8d15] | 170 | print_keys("", 0);
|
---|
[0499235] | 171 | return rc;
|
---|
[a3eeef45] | 172 | }
|
---|
[a35b458] | 173 |
|
---|
[76f382b] | 174 | char *ipath = argv[1];
|
---|
[a35b458] | 175 |
|
---|
[0499235] | 176 | if (argc < 3) {
|
---|
| 177 | sysinfo_item_val_type_t tag = sysinfo_get_val_type(ipath);
|
---|
[a35b458] | 178 |
|
---|
[0499235] | 179 | switch (tag) {
|
---|
| 180 | case SYSINFO_VAL_UNDEFINED:
|
---|
| 181 | printf("Error: Sysinfo item '%s' not defined.\n", ipath);
|
---|
| 182 | rc = 2;
|
---|
| 183 | break;
|
---|
| 184 | case SYSINFO_VAL_VAL:
|
---|
| 185 | rc = print_item_val(ipath);
|
---|
| 186 | break;
|
---|
| 187 | case SYSINFO_VAL_DATA:
|
---|
| 188 | rc = print_item_data(ipath);
|
---|
| 189 | break;
|
---|
| 190 | default:
|
---|
| 191 | printf("Error: Sysinfo item '%s' with unknown value type.\n",
|
---|
| 192 | ipath);
|
---|
| 193 | rc = 2;
|
---|
| 194 | break;
|
---|
| 195 | }
|
---|
[a35b458] | 196 |
|
---|
[0499235] | 197 | return rc;
|
---|
[76f382b] | 198 | }
|
---|
[a35b458] | 199 |
|
---|
[0499235] | 200 | char *iprop = argv[2];
|
---|
| 201 | rc = print_item_property(ipath, iprop);
|
---|
[76f382b] | 202 | return rc;
|
---|
[a3eeef45] | 203 | }
|
---|
| 204 |
|
---|
| 205 | /** @}
|
---|
| 206 | */
|
---|