source: mainline/uspace/app/sysinfo/sysinfo.c@ 8e81a7e

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8e81a7e was 711e7fe5, checked in by Prutkov Alex <prutkov.alex@…>, 14 years ago

Added printf module

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