00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00036 #include <errno.h>
00037 #include <stdio.h>
00038 #include <sysinfo.h>
00039 #include <sys/types.h>
00040 
00041 static int print_item_val(char *ipath);
00042 static int print_item_data(char *ipath);
00043 
00044 static void dump_bytes_hex(char *data, size_t size);
00045 static void dump_bytes_text(char *data, size_t size);
00046 
00047 static void print_syntax(void);
00048 
00049 int main(int argc, char *argv[])
00050 {
00051         int rc;
00052         char *ipath;
00053         sysinfo_item_tag_t tag;
00054 
00055         if (argc != 2) {
00056                 print_syntax();
00057                 return 1;
00058         }
00059 
00060         ipath = argv[1];
00061 
00062         tag = sysinfo_get_tag(ipath);
00063 
00064         
00065         rc = EOK;
00066 
00067         switch (tag) {
00068         case SYSINFO_VAL_UNDEFINED:
00069                 printf("Error: Sysinfo item '%s' not defined.\n", ipath);
00070                 rc = 2;
00071                 break;
00072         case SYSINFO_VAL_VAL:
00073                 rc = print_item_val(ipath);
00074                 break;
00075         case SYSINFO_VAL_DATA:
00076                 rc = print_item_data(ipath);
00077                 break;
00078         }
00079 
00080         return rc;
00081 }
00082 
00083 static int print_item_val(char *ipath)
00084 {
00085         sysarg_t value;
00086         int rc;
00087 
00088         rc = sysinfo_get_value(ipath, &value);
00089         if (rc != EOK) {
00090                 printf("Error reading item '%s'.\n", ipath);
00091                 return rc;
00092         }
00093 
00094         printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
00095             (uint64_t) value, (uint64_t) value);
00096 
00097         return EOK;
00098 }
00099 
00100 static int print_item_data(char *ipath)
00101 {
00102         void *data;
00103         size_t size;
00104 
00105         data = sysinfo_get_data(ipath, &size);
00106         if (data == NULL) {
00107                 printf("Error reading item '%s'.\n", ipath);
00108                 return -1;
00109         }
00110 
00111         printf("%s -> ", ipath);
00112         dump_bytes_hex(data, size);
00113         fputs(" ('", stdout);
00114         dump_bytes_text(data, size);
00115         fputs("')\n", stdout);
00116 
00117         return EOK;
00118 }
00119 
00120 static void dump_bytes_hex(char *data, size_t size)
00121 {
00122         size_t i;
00123 
00124         for (i = 0; i < size; ++i) {
00125                 if (i > 0) putchar(' ');
00126                 printf("0x%02x", (uint8_t) data[i]);
00127         }
00128 }
00129 
00130 static void dump_bytes_text(char *data, size_t size)
00131 {
00132         wchar_t c;
00133         size_t offset;
00134 
00135         offset = 0;
00136 
00137         while (offset < size) {
00138                 c = str_decode(data, &offset, size);
00139                 printf("%lc", (wint_t) c);
00140         }
00141 }
00142 
00143 
00144 static void print_syntax(void)
00145 {
00146         printf("Syntax: sysinfo <item_path>\n");
00147 }
00148