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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8fdb18e was d4d74dc, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

Less includes in library headers

There is no need for errno.h to include fibril.h.
Similarly, tinput.h does not need list.h or async.h.

Unfortunately, many programs depended on the fact that including
errno.h would (recursively) include unistd.h and NULL would be
defined. Most of the fixes remedy this problem.

  • Property mode set to 100644
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 <unistd.h>
39#include <sysinfo.h>
40#include <malloc.h>
41#include <sys/types.h>
42
43static void dump_bytes_hex(char *data, size_t size)
44{
45 for (size_t i = 0; i < size; i++) {
46 if (i > 0)
47 putchar(' ');
48 printf("0x%02x", (uint8_t) data[i]);
49 }
50}
51
52static 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);
59 }
60}
61
62static int print_item_val(char *ipath)
63{
64 sysarg_t value;
65 int rc = sysinfo_get_value(ipath, &value);
66 if (rc != EOK) {
67 printf("Error reading item '%s'.\n", ipath);
68 return rc;
69 }
70
71 printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
72 (uint64_t) value, (uint64_t) value);
73
74 return EOK;
75}
76
77static int print_item_data(char *ipath)
78{
79 size_t size;
80 void *data = sysinfo_get_data(ipath, &size);
81 if (data == NULL) {
82 printf("Error reading item '%s'.\n", ipath);
83 return -1;
84 }
85
86 printf("%s -> ", ipath);
87 dump_bytes_hex(data, size);
88 fputs(" ('", stdout);
89 dump_bytes_text(data, size);
90 fputs("')\n", stdout);
91
92 return EOK;
93}
94
95static 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
111 return EOK;
112}
113
114static void print_spaces(size_t spaces)
115{
116 for (size_t i = 0; i < spaces; i++)
117 printf(" ");
118}
119
120static void print_keys(const char *path, size_t spaces)
121{
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);
131 if (keys[pos + cur_size] != 0)
132 break;
133
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
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
146 snprintf(cur_path, path_size, "%s.%s", path, keys + pos);
147 } else {
148 printf("%s\n", keys + pos);
149 length = str_length(keys + pos);
150
151 snprintf(cur_path, path_size, "%s", keys + pos);
152 }
153
154 print_keys(cur_path, spaces + length);
155
156 free(cur_path);
157 pos += cur_size + 1;
158 }
159
160 free(keys);
161}
162
163int main(int argc, char *argv[])
164{
165 int rc = 0;
166
167 if (argc < 2) {
168 /* Print keys */
169 print_keys("", 0);
170 return rc;
171 }
172
173 char *ipath = argv[1];
174
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:
184 rc = print_item_val(ipath);
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;
197 }
198
199 char *iprop = argv[2];
200 rc = print_item_property(ipath, iprop);
201 return rc;
202}
203
204/** @}
205 */
Note: See TracBrowser for help on using the repository browser.