source: mainline/uspace/app/sysinfo/sysinfo.c@ f841608

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f841608 was a3eeef45, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Add sysinfo command to print sysinfo items from userspace.

  • Property mode set to 100644
File size: 3.4 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 <sys/types.h>
40
41static int print_item_val(char *ipath);
42static int print_item_data(char *ipath);
43
44static void dump_bytes_hex(char *data, size_t size);
45static void dump_bytes_text(char *data, size_t size);
46
47static void print_syntax(void);
48
49int main(int argc, char *argv[])
50{
51 int rc;
52 char *ipath;
53 sysinfo_item_tag_t tag;
54
55 if (argc != 2) {
56 print_syntax();
57 return 1;
58 }
59
60 ipath = argv[1];
61
62 tag = sysinfo_get_tag(ipath);
63
64 /* Silence warning */
65 rc = EOK;
66
67 switch (tag) {
68 case SYSINFO_VAL_UNDEFINED:
69 printf("Error: Sysinfo item '%s' not defined.\n", ipath);
70 rc = 2;
71 break;
72 case SYSINFO_VAL_VAL:
73 rc = print_item_val(ipath);
74 break;
75 case SYSINFO_VAL_DATA:
76 rc = print_item_data(ipath);
77 break;
78 }
79
80 return rc;
81}
82
83static int print_item_val(char *ipath)
84{
85 sysarg_t value;
86 int rc;
87
88 rc = sysinfo_get_value(ipath, &value);
89 if (rc != EOK) {
90 printf("Error reading item '%s'.\n", ipath);
91 return rc;
92 }
93
94 printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
95 (uint64_t) value, (uint64_t) value);
96
97 return EOK;
98}
99
100static int print_item_data(char *ipath)
101{
102 void *data;
103 size_t size;
104
105 data = sysinfo_get_data(ipath, &size);
106 if (data == NULL) {
107 printf("Error reading item '%s'.\n", ipath);
108 return -1;
109 }
110
111 printf("%s -> ", ipath);
112 dump_bytes_hex(data, size);
113 fputs(" ('", stdout);
114 dump_bytes_text(data, size);
115 fputs("')\n", stdout);
116
117 return EOK;
118}
119
120static void dump_bytes_hex(char *data, size_t size)
121{
122 size_t i;
123
124 for (i = 0; i < size; ++i) {
125 if (i > 0) putchar(' ');
126 printf("0x%02x", (uint8_t) data[i]);
127 }
128}
129
130static void dump_bytes_text(char *data, size_t size)
131{
132 wchar_t c;
133 size_t offset;
134
135 offset = 0;
136
137 while (offset < size) {
138 c = str_decode(data, &offset, size);
139 printf("%lc", (wint_t) c);
140 }
141}
142
143
144static void print_syntax(void)
145{
146 printf("Syntax: sysinfo <item_path>\n");
147}
148
149/** @}
150 */
Note: See TracBrowser for help on using the repository browser.