Changeset 68b5dd11 in mainline for uspace/lib/clui/nchoice.c


Ignore:
Timestamp:
2015-10-20T10:08:15Z (10 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b598460a
Parents:
44fe800
Message:

Provide some default options.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/clui/nchoice.c

    r44fe800 r68b5dd11  
    3535#include <errno.h>
    3636#include <nchoice.h>
     37#include <stdio.h>
    3738#include <stdlib.h>
    3839#include <str.h>
     
    9495}
    9596
    96 #include <stdio.h>
    9797/** Add option to numerical choice */
    98 int nchoice_add(nchoice_t *choice, const char *opttext, void *arg)
     98int nchoice_add(nchoice_t *choice, const char *opttext, void *arg,
     99    nchoice_flag_t flags)
    99100{
    100101        nchoice_opt_t *opt;
     
    114115        opt->arg = arg;
    115116        list_append(&opt->lchoice, &choice->opts);
     117
     118        if ((flags & ncf_default) != 0) {
     119                /* Set this option as the default */
     120                assert(choice->def_opt == NULL);
     121
     122                choice->def_opt = opt;
     123        }
     124
    116125        return EOK;
    117126}
     
    125134        unsigned long c;
    126135        char *eptr;
     136        char *istr;
     137        int def_i;
    127138
    128139again:
    129140        printf("%s\n", choice->prompt);
    130141
     142        def_i = 0;
    131143        i = 1;
    132144        list_foreach(choice->opts, lchoice, nchoice_opt_t, opt) {
    133                 printf("%d: %s\n", i, opt->text);
     145                printf("%d: %s%s\n", i, opt->text, opt == choice->def_opt ?
     146                    " [default]" : "");
     147                if (opt == choice->def_opt)
     148                        def_i = i;
    134149                ++i;
    135150        }
    136151
    137         rc = tinput_read(choice->tinput, &str);
     152        if (def_i > 0) {
     153                rc = asprintf(&istr, "%d", def_i);
     154                if (rc < 0)
     155                        return ENOMEM;
     156        } else {
     157                istr = str_dup("");
     158                if (istr == NULL)
     159                        return ENOMEM;
     160        }
     161
     162        rc = tinput_read_i(choice->tinput, istr, &str);
     163        free(istr);
     164
    138165        if (rc != EOK) {
    139166                assert(rc == EIO || rc == ENOENT);
Note: See TracChangeset for help on using the changeset viewer.