Changeset 5e962ad in mainline


Ignore:
Timestamp:
2017-09-21T16:13:41Z (7 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
207d5da
Parents:
4c363fa2
Message:

Add a module to format command output as a table.

Location:
uspace
Files:
3 added
3 edited

Legend:

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

    r4c363fa2 r5e962ad  
    3838#include <inet/addr.h>
    3939#include <inet/inetcfg.h>
     40#include <io/table.h>
    4041#include <loc.h>
    4142#include <stdio.h>
     
    4849static void print_syntax(void)
    4950{
    50         printf("syntax:\n");
    51         printf("\t" NAME " create <addr>/<width> <link-name> <addr-name>\n");
    52         printf("\t" NAME " delete <link-name> <addr-name>\n");
    53         printf("\t" NAME " add-sr <dest-addr>/<width> <router-addr> <route-name>\n");
    54         printf("\t" NAME " del-sr <route-name>\n");
     51        printf("Syntax:\n");
     52        printf("  %s create <addr>/<width> <link-name> <addr-name>\n", NAME);
     53        printf("  %s delete <link-name> <addr-name>\n", NAME);
     54        printf("  %s add-sr <dest-addr>/<width> <router-addr> <route-name>\n", NAME);
     55        printf("  %s del-sr <route-name>\n", NAME);
    5556}
    5657
     
    242243        inet_addr_info_t ainfo;
    243244        inet_link_info_t linfo;
     245        table_t *table = NULL;
    244246
    245247        size_t count;
    246248        size_t i;
    247249        int rc;
    248         char *astr;
     250        char *astr = NULL;
     251
     252        ainfo.name = NULL;
     253        linfo.name = NULL;
    249254
    250255        rc = inetcfg_get_addr_list(&addr_list, &count);
     
    254259        }
    255260
    256         printf("Configured addresses:\n");
    257         if (count > 0)
    258                 printf("    [Addr/Width] [Link-Name] [Addr-Name] [Def-MTU]\n");
    259         ainfo.name = NULL;
    260         linfo.name = NULL;
    261         astr = NULL;
     261        rc = table_create(&table);
     262        if (rc != EOK) {
     263                printf("Memory allocation failed.\n");
     264                goto out;
     265        }
     266
     267        table_set_margin_left(table, 4);
     268
     269        printf("Configured addresses:\n\n");
     270
     271        table_header_row(table);
     272        table_printf(table, "Addr/Width\t" "Link-Name\t" "Addr-Name\t"
     273            "Def-MTU\n");
    262274
    263275        for (i = 0; i < count; i++) {
     
    285297                }
    286298
    287                 printf("    %s %s %s %zu\n", astr, linfo.name,
    288                     ainfo.name, linfo.def_mtu);
     299                table_printf(table, "%s\t" "%s\t" "%s\t" "%zu\n", astr,
     300                    linfo.name, ainfo.name, linfo.def_mtu);
    289301
    290302                free(ainfo.name);
     
    297309        }
    298310
    299         if (count == 0)
    300                 printf("    None\n");
     311        if (count == 0) {
     312                printf("None\n");
     313        } else {
     314                rc = table_print_out(table, stdout);
     315                if (rc != EOK) {
     316                        printf("Error printing table.\n");
     317                        goto out;
     318                }
     319        }
     320
     321        rc = EOK;
    301322out:
     323        table_destroy(table);
    302324        if (ainfo.name != NULL)
    303325                free(ainfo.name);
     
    309331        free(addr_list);
    310332
    311         return EOK;
     333        return rc;
    312334}
    313335
    314336static int link_list(void)
    315337{
    316         sysarg_t *link_list;
     338        sysarg_t *link_list = NULL;
    317339        inet_link_info_t linfo;
     340        table_t *table = NULL;
    318341
    319342        size_t count;
     
    327350        }
    328351
    329         printf("IP links:\n");
    330         if (count > 0)
    331                 printf("    [Link-layer Address] [Link-Name] [Def-MTU]\n");
     352        rc = table_create(&table);
     353        if (rc != EOK) {
     354                printf("Memory allocation failed.\n");
     355                goto out;
     356        }
     357
     358        table_set_margin_left(table, 4);
     359
     360        printf("IP links:\n\n");
     361        table_header_row(table);
     362        table_printf(table, "Link-layer Address\t" "Link-Name\t" "Def-MTU\n");
    332363
    333364        for (i = 0; i < count; i++) {
     
    339370                }
    340371
    341                 printf("    %02x:%02x:%02x:%02x:%02x:%02x %s %zu\n",
     372                table_printf(table, "%02x:%02x:%02x:%02x:%02x:%02x\t"
     373                    "%s\t" "%zu\n",
    342374                    linfo.mac_addr[0], linfo.mac_addr[1],
    343375                    linfo.mac_addr[2], linfo.mac_addr[3],
     
    350382        }
    351383
    352         if (count == 0)
    353                 printf("    None\n");
    354 
     384        if (count == 0) {
     385                printf("None\n");
     386        } else {
     387                rc = table_print_out(table, stdout);
     388                if (rc != EOK) {
     389                        printf("Error printing table.\n");
     390                        goto out;
     391                }
     392        }
     393
     394        rc = EOK;
     395out:
     396        table_destroy(table);
    355397        free(link_list);
    356398
    357         return EOK;
     399        return rc;
    358400}
    359401
    360402static int sroute_list(void)
    361403{
    362         sysarg_t *sroute_list;
     404        sysarg_t *sroute_list = NULL;
    363405        inet_sroute_info_t srinfo;
     406        table_t *table = NULL;
    364407
    365408        size_t count;
    366409        size_t i;
    367410        int rc;
    368         char *dest_str;
    369         char *router_str;
     411        char *dest_str = NULL;
     412        char *router_str = NULL;
     413
     414        srinfo.name = NULL;
    370415
    371416        rc = inetcfg_get_sroute_list(&sroute_list, &count);
     
    375420        }
    376421
    377         printf("Static routes:\n");
    378         if (count > 0)
    379                 printf("    [Dest/Width] [Router-Addr] [Route-Name]\n");
    380 
    381         srinfo.name = NULL;
    382         dest_str = NULL;
    383         router_str = NULL;
     422        rc = table_create(&table);
     423        if (rc != EOK) {
     424                printf("Memory allocation failed.\n");
     425                goto out;
     426        }
     427
     428        table_set_margin_left(table, 4);
     429
     430        printf("Static routes:\n\n");
     431        table_header_row(table);
     432        table_printf(table, "Dest/Width\t" "Router-Addr\t" "Route-Name\n");
    384433
    385434        for (i = 0; i < count; i++) {
     
    406455                }
    407456
    408                 printf("    %s %s %s\n", dest_str, router_str, srinfo.name);
     457                table_printf(table, "%s\t" "%s\t" "%s\n", dest_str, router_str,
     458                    srinfo.name);
    409459
    410460                free(srinfo.name);
     
    417467        }
    418468
    419         if (count == 0)
    420                 printf("    None\n");
     469        if (count == 0) {
     470                printf("None\n");
     471        } else {
     472                rc = table_print_out(table, stdout);
     473                if (rc != EOK) {
     474                        printf("Error printing table.\n");
     475                        goto out;
     476                }
     477        }
     478
     479        rc = EOK;
    421480out:
     481        table_destroy(table);
    422482        if (srinfo.name != NULL)
    423483                free(srinfo.name);
     
    429489        free(sroute_list);
    430490
    431         return EOK;
     491        return rc;
    432492}
    433493
  • uspace/lib/c/Makefile

    r4c363fa2 r5e962ad  
    126126        generic/io/con_srv.c \
    127127        generic/io/console.c \
     128        generic/io/table.c \
    128129        generic/io/visualizer.c \
    129130        generic/io/window.c \
     
    183184        test/fibril/timer.c \
    184185        test/main.c \
     186        test/io/table.c \
    185187        test/odict.c \
    186188        test/qsort.c \
  • uspace/lib/c/test/main.c

    r4c363fa2 r5e962ad  
    3737PCUT_IMPORT(sprintf);
    3838PCUT_IMPORT(str);
     39PCUT_IMPORT(table);
    3940
    4041PCUT_MAIN()
Note: See TracChangeset for help on using the changeset viewer.