Changeset 76f382b in mainline for uspace/app/sysinfo/sysinfo.c


Ignore:
Timestamp:
2012-03-02T14:50:42Z (13 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8b9a443
Parents:
560d79f
Message:

support for listing sysinfo from uspace

File:
1 edited

Legend:

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

    r560d79f r76f382b  
    3737#include <stdio.h>
    3838#include <sysinfo.h>
     39#include <malloc.h>
    3940#include <sys/types.h>
    4041
    41 static int print_item_val(char *ipath);
    42 static int print_item_data(char *ipath);
     42static void dump_bytes_hex(char *data, size_t size)
     43{
     44        for (size_t i = 0; i < size; i++) {
     45                if (i > 0)
     46                        putchar(' ');
     47                printf("0x%02x", (uint8_t) data[i]);
     48        }
     49}
    4350
    44 static void dump_bytes_hex(char *data, size_t size);
    45 static void dump_bytes_text(char *data, size_t size);
     51static void dump_bytes_text(char *data, size_t size)
     52{
     53        size_t offset = 0;
     54       
     55        while (offset < size) {
     56                wchar_t c = str_decode(data, &offset, size);
     57                printf("%lc", (wint_t) c);
     58        }
     59}
    4660
    47 static void print_syntax(void);
     61static int print_item_val(char *ipath)
     62{
     63        sysarg_t value;
     64        int rc = sysinfo_get_value(ipath, &value);
     65        if (rc != EOK) {
     66                printf("Error reading item '%s'.\n", ipath);
     67                return rc;
     68        }
     69       
     70        printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
     71            (uint64_t) value, (uint64_t) value);
     72       
     73        return EOK;
     74}
     75
     76static int print_item_data(char *ipath)
     77{
     78        size_t size;
     79        void *data = sysinfo_get_data(ipath, &size);
     80        if (data == NULL) {
     81                printf("Error reading item '%s'.\n", ipath);
     82                return -1;
     83        }
     84       
     85        printf("%s -> ", ipath);
     86        dump_bytes_hex(data, size);
     87        fputs(" ('", stdout);
     88        dump_bytes_text(data, size);
     89        fputs("')\n", stdout);
     90       
     91        return EOK;
     92}
     93
     94static void print_keys(const char *path)
     95{
     96        size_t size;
     97        char *keys = sysinfo_get_keys(path, &size);
     98        if ((keys == NULL) || (size == 0))
     99                return;
     100       
     101        size_t pos = 0;
     102        while (pos < size) {
     103                /* Process each key with sanity checks */
     104                size_t cur_size = str_nsize(keys + pos, size - pos);
     105                size_t path_size = str_size(path) + cur_size + 2;
     106                char *cur_path = (char *) malloc(path_size);
     107                if (cur_path == NULL)
     108                        break;
     109               
     110                if (path[0] != 0)
     111                        snprintf(cur_path, path_size, "%s.%s", path, keys + pos);
     112                else
     113                        snprintf(cur_path, path_size, "%s", keys + pos);
     114               
     115                printf("%s\n", cur_path);
     116                print_keys(cur_path);
     117               
     118                free(cur_path);
     119                pos += cur_size + 1;
     120        }
     121       
     122        free(keys);
     123}
    48124
    49125int main(int argc, char *argv[])
    50126{
    51         int rc;
    52         char *ipath;
    53         sysinfo_item_val_type_t tag;
    54 
    55127        if (argc != 2) {
    56                 print_syntax();
    57                 return 1;
     128                /* Print keys */
     129                print_keys("");
     130                return 0;
    58131        }
    59 
    60         ipath = argv[1];
    61 
    62         tag = sysinfo_get_val_type(ipath);
    63 
     132       
     133        char *ipath = argv[1];
     134        sysinfo_item_val_type_t tag = sysinfo_get_val_type(ipath);
     135       
    64136        /* Silence warning */
    65         rc = EOK;
    66 
     137        int rc = 0;
     138       
    67139        switch (tag) {
    68140        case SYSINFO_VAL_UNDEFINED:
     
    82154                break;
    83155        }
    84 
     156       
    85157        return rc;
    86 }
    87 
    88 static 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 
    105 static 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 }
    124 
    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");
    152158}
    153159
Note: See TracChangeset for help on using the changeset viewer.