Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sysinfo/sysinfo.c

    r9a426d1f rd4d74dc  
    3636#include <errno.h>
    3737#include <stdio.h>
     38#include <unistd.h>
    3839#include <sysinfo.h>
     40#include <malloc.h>
    3941#include <sys/types.h>
    4042
    41 static int print_item_val(char *ipath);
    42 static int print_item_data(char *ipath);
    43 
    44 static void dump_bytes_hex(char *data, size_t size);
    45 static void dump_bytes_text(char *data, size_t size);
    46 
    47 static void print_syntax(void);
    48 
    49 int main(int argc, char *argv[])
    50 {
    51         int rc;
    52         char *ipath;
    53         sysinfo_item_val_type_t tag;
    54 
    55         if (argc != 2) {
    56                 print_syntax();
    57                 return 1;
    58         }
    59 
    60         ipath = argv[1];
    61 
    62         tag = sysinfo_get_val_type(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         default:
    79                 printf("Error: Sysinfo item '%s' with unknown value type.\n",
    80                     ipath);
    81                 rc = 2;
    82                 break;
    83         }
    84 
    85         return rc;
     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        }
    8660}
    8761
     
    8963{
    9064        sysarg_t value;
    91         int rc;
    92 
    93         rc = sysinfo_get_value(ipath, &value);
     65        int rc = sysinfo_get_value(ipath, &value);
    9466        if (rc != EOK) {
    9567                printf("Error reading item '%s'.\n", ipath);
    9668                return rc;
    9769        }
    98 
     70       
    9971        printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
    10072            (uint64_t) value, (uint64_t) value);
    101 
     73       
    10274        return EOK;
    10375}
     
    10577static int print_item_data(char *ipath)
    10678{
    107         void *data;
    10879        size_t size;
    109 
    110         data = sysinfo_get_data(ipath, &size);
     80        void *data = sysinfo_get_data(ipath, &size);
    11181        if (data == NULL) {
    11282                printf("Error reading item '%s'.\n", ipath);
    11383                return -1;
    11484        }
    115 
     85       
    11686        printf("%s -> ", ipath);
    11787        dump_bytes_hex(data, size);
     
    11989        dump_bytes_text(data, size);
    12090        fputs("')\n", stdout);
    121 
     91       
    12292        return EOK;
    12393}
    12494
    125 static void dump_bytes_hex(char *data, size_t size)
    126 {
    127         size_t i;
    128 
    129         for (i = 0; i < size; ++i) {
    130                 if (i > 0) putchar(' ');
    131                 printf("0x%02x", (uint8_t) data[i]);
    132         }
    133 }
    134 
    135 static void dump_bytes_text(char *data, size_t size)
    136 {
    137         wchar_t c;
    138         size_t offset;
    139 
    140         offset = 0;
    141 
    142         while (offset < size) {
    143                 c = str_decode(data, &offset, size);
    144                 printf("%lc", (wint_t) c);
    145         }
    146 }
    147 
    148 
    149 static void print_syntax(void)
    150 {
    151         printf("Syntax: sysinfo <item_path>\n");
     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;
    152202}
    153203
Note: See TracChangeset for help on using the changeset viewer.