Ignore:
File:
1 edited

Legend:

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

    rd4d74dc r9a426d1f  
    3636#include <errno.h>
    3737#include <stdio.h>
    38 #include <unistd.h>
    3938#include <sysinfo.h>
    40 #include <malloc.h>
    4139#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_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;
     86}
     87
     88static int print_item_val(char *ipath)
     89{
     90        sysarg_t value;
     91        int rc;
     92
     93        rc = sysinfo_get_value(ipath, &value);
     94        if (rc != EOK) {
     95                printf("Error reading item '%s'.\n", ipath);
     96                return rc;
     97        }
     98
     99        printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
     100            (uint64_t) value, (uint64_t) value);
     101
     102        return EOK;
     103}
     104
     105static int print_item_data(char *ipath)
     106{
     107        void *data;
     108        size_t size;
     109
     110        data = sysinfo_get_data(ipath, &size);
     111        if (data == NULL) {
     112                printf("Error reading item '%s'.\n", ipath);
     113                return -1;
     114        }
     115
     116        printf("%s -> ", ipath);
     117        dump_bytes_hex(data, size);
     118        fputs(" ('", stdout);
     119        dump_bytes_text(data, size);
     120        fputs("')\n", stdout);
     121
     122        return EOK;
     123}
    42124
    43125static void dump_bytes_hex(char *data, size_t size)
    44126{
    45         for (size_t i = 0; i < size; i++) {
    46                 if (i > 0)
    47                         putchar(' ');
     127        size_t i;
     128
     129        for (i = 0; i < size; ++i) {
     130                if (i > 0) putchar(' ');
    48131                printf("0x%02x", (uint8_t) data[i]);
    49132        }
     
    52135static void dump_bytes_text(char *data, size_t size)
    53136{
    54         size_t offset = 0;
    55        
     137        wchar_t c;
     138        size_t offset;
     139
     140        offset = 0;
     141
    56142        while (offset < size) {
    57                 wchar_t c = str_decode(data, &offset, size);
     143                c = str_decode(data, &offset, size);
    58144                printf("%lc", (wint_t) c);
    59145        }
    60146}
    61147
    62 static int print_item_val(char *ipath)
     148
     149static void print_syntax(void)
    63150{
    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 
    77 static 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 
    95 static 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 
    114 static void print_spaces(size_t spaces)
    115 {
    116         for (size_t i = 0; i < spaces; i++)
    117                 printf(" ");
    118 }
    119 
    120 static 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 
    163 int 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;
     151        printf("Syntax: sysinfo <item_path>\n");
    202152}
    203153
Note: See TracChangeset for help on using the changeset viewer.