[e96047c] | 1 | /*
|
---|
| 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 | /** @addtogroup libclui
|
---|
| 29 | * @{
|
---|
| 30 | */
|
---|
| 31 | /**
|
---|
| 32 | * @file Numerical choice
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <errno.h>
|
---|
| 36 | #include <nchoice.h>
|
---|
[68b5dd11] | 37 | #include <stdio.h>
|
---|
[e96047c] | 38 | #include <stdlib.h>
|
---|
| 39 | #include <str.h>
|
---|
| 40 | #include <tinput.h>
|
---|
| 41 |
|
---|
| 42 | /** Create numerical choice
|
---|
| 43 | *
|
---|
| 44 | */
|
---|
[b7fd2a0] | 45 | errno_t nchoice_create(nchoice_t **rchoice)
|
---|
[e96047c] | 46 | {
|
---|
| 47 | nchoice_t *choice = NULL;
|
---|
[b7fd2a0] | 48 | errno_t rc;
|
---|
[e96047c] | 49 |
|
---|
| 50 | choice = calloc(1, sizeof(nchoice_t));
|
---|
| 51 | if (choice == NULL)
|
---|
| 52 | goto error;
|
---|
| 53 |
|
---|
| 54 | list_initialize(&choice->opts);
|
---|
| 55 |
|
---|
| 56 | choice->tinput = tinput_new();
|
---|
| 57 | if (choice->tinput == NULL)
|
---|
| 58 | goto error;
|
---|
| 59 |
|
---|
| 60 | rc = tinput_set_prompt(choice->tinput, "Choice> ");
|
---|
| 61 | if (rc != EOK) {
|
---|
| 62 | assert(rc == ENOMEM);
|
---|
| 63 | goto error;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | *rchoice = choice;
|
---|
| 67 | return EOK;
|
---|
| 68 | error:
|
---|
| 69 | nchoice_destroy(choice);
|
---|
| 70 | return ENOMEM;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /** Destroy numerical choice */
|
---|
| 74 | void nchoice_destroy(nchoice_t *choice)
|
---|
| 75 | {
|
---|
| 76 | if (choice == NULL)
|
---|
| 77 | return;
|
---|
| 78 |
|
---|
| 79 | tinput_destroy(choice->tinput);
|
---|
| 80 | free(choice);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | /** Set numerica choice prompt text */
|
---|
[b7fd2a0] | 84 | errno_t nchoice_set_prompt(nchoice_t *choice, const char *prompt)
|
---|
[e96047c] | 85 | {
|
---|
| 86 | char *pstr;
|
---|
| 87 |
|
---|
| 88 | pstr = str_dup(prompt);
|
---|
| 89 | if (pstr == NULL)
|
---|
| 90 | return ENOMEM;
|
---|
| 91 |
|
---|
| 92 | free(choice->prompt);
|
---|
| 93 | choice->prompt = pstr;
|
---|
| 94 | return EOK;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /** Add option to numerical choice */
|
---|
[b7fd2a0] | 98 | errno_t nchoice_add(nchoice_t *choice, const char *opttext, void *arg,
|
---|
[68b5dd11] | 99 | nchoice_flag_t flags)
|
---|
[e96047c] | 100 | {
|
---|
| 101 | nchoice_opt_t *opt;
|
---|
| 102 | char *ptext;
|
---|
| 103 |
|
---|
| 104 | opt = calloc(1, sizeof(nchoice_opt_t));
|
---|
| 105 | if (opt == NULL)
|
---|
| 106 | return ENOMEM;
|
---|
| 107 |
|
---|
| 108 | ptext = str_dup(opttext);
|
---|
| 109 | if (ptext == NULL) {
|
---|
| 110 | free(opt);
|
---|
| 111 | return ENOMEM;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | opt->text = ptext;
|
---|
| 115 | opt->arg = arg;
|
---|
| 116 | list_append(&opt->lchoice, &choice->opts);
|
---|
[68b5dd11] | 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 |
|
---|
[e96047c] | 125 | return EOK;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /** Get numerical choice from user */
|
---|
[b7fd2a0] | 129 | errno_t nchoice_get(nchoice_t *choice, void **rarg)
|
---|
[e96047c] | 130 | {
|
---|
| 131 | int i;
|
---|
[b7fd2a0] | 132 | errno_t rc;
|
---|
[d5c1051] | 133 | int ret;
|
---|
[e96047c] | 134 | char *str;
|
---|
| 135 | unsigned long c;
|
---|
| 136 | char *eptr;
|
---|
[68b5dd11] | 137 | char *istr;
|
---|
| 138 | int def_i;
|
---|
[e96047c] | 139 |
|
---|
| 140 | again:
|
---|
| 141 | printf("%s\n", choice->prompt);
|
---|
| 142 |
|
---|
[68b5dd11] | 143 | def_i = 0;
|
---|
[e96047c] | 144 | i = 1;
|
---|
| 145 | list_foreach(choice->opts, lchoice, nchoice_opt_t, opt) {
|
---|
[68b5dd11] | 146 | printf("%d: %s%s\n", i, opt->text, opt == choice->def_opt ?
|
---|
| 147 | " [default]" : "");
|
---|
| 148 | if (opt == choice->def_opt)
|
---|
| 149 | def_i = i;
|
---|
[e96047c] | 150 | ++i;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[68b5dd11] | 153 | if (def_i > 0) {
|
---|
[d5c1051] | 154 | ret = asprintf(&istr, "%d", def_i);
|
---|
| 155 | if (ret < 0)
|
---|
[68b5dd11] | 156 | return ENOMEM;
|
---|
| 157 | } else {
|
---|
| 158 | istr = str_dup("");
|
---|
| 159 | if (istr == NULL)
|
---|
| 160 | return ENOMEM;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | rc = tinput_read_i(choice->tinput, istr, &str);
|
---|
| 164 | free(istr);
|
---|
| 165 |
|
---|
[e96047c] | 166 | if (rc != EOK) {
|
---|
| 167 | assert(rc == EIO || rc == ENOENT);
|
---|
| 168 |
|
---|
| 169 | if (rc == ENOENT) {
|
---|
| 170 | return ENOENT;
|
---|
| 171 | } else {
|
---|
| 172 | /* rc == EIO */
|
---|
| 173 | return EIO;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | c = strtoul(str, &eptr, 10);
|
---|
| 178 | if (*eptr != '\0' || c < 1 || c > (unsigned long)i - 1) {
|
---|
| 179 | printf("Invalid choice. Try again.\n");
|
---|
| 180 | free(str);
|
---|
| 181 | goto again;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | free(str);
|
---|
| 185 |
|
---|
| 186 | i = 1;
|
---|
| 187 | list_foreach(choice->opts, lchoice, nchoice_opt_t, opt) {
|
---|
| 188 | if ((unsigned long)i == c) {
|
---|
| 189 | *rarg = opt->arg;
|
---|
| 190 | return EOK;
|
---|
| 191 | }
|
---|
| 192 | ++i;
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | assert(false);
|
---|
| 196 | return ENOENT;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /** @}
|
---|
| 200 | */
|
---|