Changeset 98cb0495 in mainline for uspace/app/sysinfo/sysinfo.c


Ignore:
Timestamp:
2012-03-04T18:52:28Z (12 years ago)
Author:
Jan Vesely <jano.vesely@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
38b950f
Parents:
6561a8e (diff), 0499235 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

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

    r6561a8e r98cb0495  
    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);
    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;
     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}
     50
     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        }
    8659}
    8760
     
    8962{
    9063        sysarg_t value;
    91         int rc;
    92 
    93         rc = sysinfo_get_value(ipath, &value);
     64        int rc = sysinfo_get_value(ipath, &value);
    9465        if (rc != EOK) {
    9566                printf("Error reading item '%s'.\n", ipath);
    9667                return rc;
    9768        }
    98 
     69       
    9970        printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
    10071            (uint64_t) value, (uint64_t) value);
    101 
     72       
    10273        return EOK;
    10374}
     
    10576static int print_item_data(char *ipath)
    10677{
    107         void *data;
    10878        size_t size;
    109 
    110         data = sysinfo_get_data(ipath, &size);
     79        void *data = sysinfo_get_data(ipath, &size);
    11180        if (data == NULL) {
    11281                printf("Error reading item '%s'.\n", ipath);
    11382                return -1;
    11483        }
    115 
     84       
    11685        printf("%s -> ", ipath);
    11786        dump_bytes_hex(data, size);
     
    11988        dump_bytes_text(data, size);
    12089        fputs("')\n", stdout);
    121 
     90       
    12291        return EOK;
    12392}
    12493
    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");
     94static int print_item_property(char *ipath, char *iprop)
     95{
     96        size_t size;
     97        void *data = sysinfo_get_property(ipath, iprop, &size);
     98        if (data == NULL) {
     99                printf("Error reading property '%s' of item '%s'.\n", iprop,
     100                    ipath);
     101                return -1;
     102        }
     103       
     104        printf("%s property %s -> ", ipath, iprop);
     105        dump_bytes_hex(data, size);
     106        fputs(" ('", stdout);
     107        dump_bytes_text(data, size);
     108        fputs("')\n", stdout);
     109       
     110        return EOK;
     111}
     112
     113static void print_spaces(size_t spaces)
     114{
     115        for (size_t i = 0; i < spaces; i++)
     116                printf(" ");
     117}
     118
     119static void print_keys(const char *path, size_t spaces)
     120{
     121        size_t size;
     122        char *keys = sysinfo_get_keys(path, &size);
     123        if ((keys == NULL) || (size == 0))
     124                return;
     125       
     126        size_t pos = 0;
     127        while (pos < size) {
     128                /* Process each key with sanity checks */
     129                size_t cur_size = str_nsize(keys + pos, size - pos);
     130                if (keys[pos + cur_size] != 0)
     131                        break;
     132               
     133                size_t path_size = str_size(path) + cur_size + 2;
     134                char *cur_path = (char *) malloc(path_size);
     135                if (cur_path == NULL)
     136                        break;
     137               
     138                size_t length;
     139               
     140                if (path[0] != 0) {
     141                        print_spaces(spaces);
     142                        printf(".%s\n", keys + pos);
     143                        length = str_length(keys + pos) + 1;
     144                       
     145                        snprintf(cur_path, path_size, "%s.%s", path, keys + pos);
     146                } else {
     147                        printf("%s\n", keys + pos);
     148                        length = str_length(keys + pos);
     149                       
     150                        snprintf(cur_path, path_size, "%s", keys + pos);
     151                }
     152               
     153                print_keys(cur_path, spaces + length);
     154               
     155                free(cur_path);
     156                pos += cur_size + 1;
     157        }
     158       
     159        free(keys);
     160}
     161
     162int main(int argc, char *argv[])
     163{
     164        int rc = 0;
     165       
     166        if (argc < 2) {
     167                /* Print keys */
     168                print_keys("", 0);
     169                return rc;
     170        }
     171       
     172        char *ipath = argv[1];
     173       
     174        if (argc < 3) {
     175                sysinfo_item_val_type_t tag = sysinfo_get_val_type(ipath);
     176               
     177                switch (tag) {
     178                case SYSINFO_VAL_UNDEFINED:
     179                        printf("Error: Sysinfo item '%s' not defined.\n", ipath);
     180                        rc = 2;
     181                        break;
     182                case SYSINFO_VAL_VAL:
     183                        rc = print_item_val(ipath);
     184                        break;
     185                case SYSINFO_VAL_DATA:
     186                        rc = print_item_data(ipath);
     187                        break;
     188                default:
     189                        printf("Error: Sysinfo item '%s' with unknown value type.\n",
     190                            ipath);
     191                        rc = 2;
     192                        break;
     193                }
     194               
     195                return rc;
     196        }
     197       
     198        char *iprop = argv[2];
     199        rc = print_item_property(ipath, iprop);
     200        return rc;
    152201}
    153202
Note: See TracChangeset for help on using the changeset viewer.