Changeset c30a015 in mainline for uspace


Ignore:
Timestamp:
2012-03-03T10:32:47Z (14 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7689590
Parents:
d0d7afb (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 with mainline

Location:
uspace
Files:
5 edited

Legend:

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

    rd0d7afb rc30a015  
    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
  • uspace/lib/c/generic/str.c

    rd0d7afb rc30a015  
    259259       
    260260        return offset;
     261}
     262
     263/** Get size of string with size limit.
     264 *
     265 * Get the number of bytes which are used by the string @a str
     266 * (excluding the NULL-terminator), but no more than @max_size bytes.
     267 *
     268 * @param str      String to consider.
     269 * @param max_size Maximum number of bytes to measure.
     270 *
     271 * @return Number of bytes used by the string
     272 *
     273 */
     274size_t str_nsize(const char *str, size_t max_size)
     275{
     276        size_t size = 0;
     277       
     278        while ((*str++ != 0) && (size < max_size))
     279                size++;
     280       
     281        return size;
     282}
     283
     284/** Get size of wide string with size limit.
     285 *
     286 * Get the number of bytes which are used by the wide string @a str
     287 * (excluding the NULL-terminator), but no more than @max_size bytes.
     288 *
     289 * @param str      Wide string to consider.
     290 * @param max_size Maximum number of bytes to measure.
     291 *
     292 * @return Number of bytes used by the wide string
     293 *
     294 */
     295size_t wstr_nsize(const wchar_t *str, size_t max_size)
     296{
     297        return (wstr_nlength(str, max_size) * sizeof(wchar_t));
    261298}
    262299
  • uspace/lib/c/generic/sysinfo.c

    rd0d7afb rc30a015  
    4040#include <bool.h>
    4141
     42/** Get sysinfo keys size
     43 *
     44 * @param path  Sysinfo path.
     45 * @param value Pointer to store the keys size.
     46 *
     47 * @return EOK if the keys were successfully read.
     48 *
     49 */
     50static int sysinfo_get_keys_size(const char *path, size_t *size)
     51{
     52        return (int) __SYSCALL3(SYS_SYSINFO_GET_KEYS_SIZE, (sysarg_t) path,
     53            (sysarg_t) str_size(path), (sysarg_t) size);
     54}
     55
     56/** Get sysinfo keys
     57 *
     58 * @param path  Sysinfo path.
     59 * @param value Pointer to store the keys size.
     60 *
     61 * @return Keys read from sysinfo or NULL if the
     62 *         sysinfo item has no subkeys.
     63 *         The returned non-NULL pointer should be
     64 *         freed by free().
     65 *
     66 */
     67char *sysinfo_get_keys(const char *path, size_t *size)
     68{
     69        /*
     70         * The size of the keys might change during time.
     71         * Unfortunatelly we cannot allocate the buffer
     72         * and transfer the keys as a single atomic operation.
     73         */
     74       
     75        /* Get the keys size */
     76        int ret = sysinfo_get_keys_size(path, size);
     77        if ((ret != EOK) || (size == 0)) {
     78                /*
     79                 * Item with no subkeys.
     80                 */
     81                *size = 0;
     82                return NULL;
     83        }
     84       
     85        char *data = malloc(*size);
     86        if (data == NULL) {
     87                *size = 0;
     88                return NULL;
     89        }
     90       
     91        /* Get the data */
     92        size_t sz;
     93        ret = __SYSCALL5(SYS_SYSINFO_GET_KEYS, (sysarg_t) path,
     94            (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size,
     95            (sysarg_t) &sz);
     96        if (ret == EOK) {
     97                *size = sz;
     98                return data;
     99        }
     100       
     101        free(data);
     102        *size = 0;
     103        return NULL;
     104}
     105
    42106/** Get sysinfo item type
    43107 *
     
    70134/** Get sysinfo binary data size
    71135 *
    72  * @param path  Sysinfo path.
    73  * @param value Pointer to store the binary data size.
     136 * @param path Sysinfo path.
     137 * @param size Pointer to store the binary data size.
    74138 *
    75139 * @return EOK if the value was successfully read and
     
    85149/** Get sysinfo binary data
    86150 *
    87  * @param path  Sysinfo path.
    88  * @param value Pointer to store the binary data size.
     151 * @param path Sysinfo path.
     152 * @param size Pointer to store the binary data size.
    89153 *
    90154 * @return Binary data read from sysinfo or NULL if the
     
    134198}
    135199
     200/** Get sysinfo property
     201 *
     202 * @param path Sysinfo path.
     203 * @param name Property name.
     204 * @param size Pointer to store the binary data size.
     205 *
     206 * @return Property value read from sysinfo or NULL if the
     207 *         sysinfo item value type is not binary data.
     208 *         The returned non-NULL pointer should be
     209 *         freed by free().
     210 *
     211 */
     212void *sysinfo_get_property(const char *path, const char *name, size_t *size)
     213{
     214        size_t total_size;
     215        void *data = sysinfo_get_data(path, &total_size);
     216        if ((data == NULL) || (total_size == 0)) {
     217                *size = 0;
     218                return NULL;
     219        }
     220       
     221        size_t pos = 0;
     222        while (pos < total_size) {
     223                /* Process each property with sanity checks */
     224                size_t cur_size = str_nsize(data + pos, total_size - pos);
     225                if (((char *) data)[pos + cur_size] != 0)
     226                        break;
     227               
     228                bool found = (str_cmp(data + pos, name) == 0);
     229               
     230                pos += cur_size + 1;
     231                if (pos >= total_size)
     232                        break;
     233               
     234                /* Process value size */
     235                size_t value_size;
     236                memcpy(&value_size, data + pos, sizeof(value_size));
     237               
     238                pos += sizeof(value_size);
     239                if ((pos >= total_size) || (pos + value_size > total_size))
     240                        break;
     241               
     242                if (found) {
     243                        void *value = malloc(value_size);
     244                        if (value == NULL)
     245                                break;
     246                       
     247                        memcpy(value, data + pos, value_size);
     248                        free(data);
     249                       
     250                        *size = value_size;
     251                        return value;
     252                }
     253               
     254                pos += value_size;
     255        }
     256       
     257        free(data);
     258       
     259        *size = 0;
     260        return NULL;
     261}
     262
    136263/** @}
    137264 */
  • uspace/lib/c/include/str.h

    rd0d7afb rc30a015  
    6060extern size_t str_size(const char *str);
    6161extern size_t wstr_size(const wchar_t *str);
     62
     63extern size_t str_nsize(const char *str, size_t max_size);
     64extern size_t wstr_nsize(const wchar_t *str, size_t max_size);
    6265
    6366extern size_t str_lsize(const char *str, size_t max_len);
  • uspace/lib/c/include/sysinfo.h

    rd0d7afb rc30a015  
    4040#include <abi/sysinfo.h>
    4141
     42extern char *sysinfo_get_keys(const char *, size_t *);
    4243extern sysinfo_item_val_type_t sysinfo_get_val_type(const char *);
    4344extern int sysinfo_get_value(const char *, sysarg_t *);
    4445extern void *sysinfo_get_data(const char *, size_t *);
     46extern void *sysinfo_get_property(const char *, const char *, size_t *);
    4547
    4648#endif
Note: See TracChangeset for help on using the changeset viewer.