| 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>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <str.h>
|
|---|
| 39 | #include <tinput.h>
|
|---|
| 40 |
|
|---|
| 41 | /** Create numerical choice
|
|---|
| 42 | *
|
|---|
| 43 | */
|
|---|
| 44 | int nchoice_create(nchoice_t **rchoice)
|
|---|
| 45 | {
|
|---|
| 46 | nchoice_t *choice = NULL;
|
|---|
| 47 | int rc;
|
|---|
| 48 |
|
|---|
| 49 | choice = calloc(1, sizeof(nchoice_t));
|
|---|
| 50 | if (choice == NULL)
|
|---|
| 51 | goto error;
|
|---|
| 52 |
|
|---|
| 53 | list_initialize(&choice->opts);
|
|---|
| 54 |
|
|---|
| 55 | choice->tinput = tinput_new();
|
|---|
| 56 | if (choice->tinput == NULL)
|
|---|
| 57 | goto error;
|
|---|
| 58 |
|
|---|
| 59 | rc = tinput_set_prompt(choice->tinput, "Choice> ");
|
|---|
| 60 | if (rc != EOK) {
|
|---|
| 61 | assert(rc == ENOMEM);
|
|---|
| 62 | goto error;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | *rchoice = choice;
|
|---|
| 66 | return EOK;
|
|---|
| 67 | error:
|
|---|
| 68 | nchoice_destroy(choice);
|
|---|
| 69 | return ENOMEM;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /** Destroy numerical choice */
|
|---|
| 73 | void nchoice_destroy(nchoice_t *choice)
|
|---|
| 74 | {
|
|---|
| 75 | if (choice == NULL)
|
|---|
| 76 | return;
|
|---|
| 77 |
|
|---|
| 78 | tinput_destroy(choice->tinput);
|
|---|
| 79 | free(choice);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | /** Set numerica choice prompt text */
|
|---|
| 83 | int nchoice_set_prompt(nchoice_t *choice, const char *prompt)
|
|---|
| 84 | {
|
|---|
| 85 | char *pstr;
|
|---|
| 86 |
|
|---|
| 87 | pstr = str_dup(prompt);
|
|---|
| 88 | if (pstr == NULL)
|
|---|
| 89 | return ENOMEM;
|
|---|
| 90 |
|
|---|
| 91 | free(choice->prompt);
|
|---|
| 92 | choice->prompt = pstr;
|
|---|
| 93 | return EOK;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | #include <stdio.h>
|
|---|
| 97 | /** Add option to numerical choice */
|
|---|
| 98 | int nchoice_add(nchoice_t *choice, const char *opttext, void *arg)
|
|---|
| 99 | {
|
|---|
| 100 | nchoice_opt_t *opt;
|
|---|
| 101 | char *ptext;
|
|---|
| 102 |
|
|---|
| 103 | opt = calloc(1, sizeof(nchoice_opt_t));
|
|---|
| 104 | if (opt == NULL)
|
|---|
| 105 | return ENOMEM;
|
|---|
| 106 |
|
|---|
| 107 | ptext = str_dup(opttext);
|
|---|
| 108 | if (ptext == NULL) {
|
|---|
| 109 | free(opt);
|
|---|
| 110 | return ENOMEM;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | opt->text = ptext;
|
|---|
| 114 | opt->arg = arg;
|
|---|
| 115 | list_append(&opt->lchoice, &choice->opts);
|
|---|
| 116 | return EOK;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | /** Get numerical choice from user */
|
|---|
| 120 | int nchoice_get(nchoice_t *choice, void **rarg)
|
|---|
| 121 | {
|
|---|
| 122 | int i;
|
|---|
| 123 | int rc;
|
|---|
| 124 | char *str;
|
|---|
| 125 | unsigned long c;
|
|---|
| 126 | char *eptr;
|
|---|
| 127 |
|
|---|
| 128 | again:
|
|---|
| 129 | printf("%s\n", choice->prompt);
|
|---|
| 130 |
|
|---|
| 131 | i = 1;
|
|---|
| 132 | list_foreach(choice->opts, lchoice, nchoice_opt_t, opt) {
|
|---|
| 133 | printf("%d: %s\n", i, opt->text);
|
|---|
| 134 | ++i;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | rc = tinput_read(choice->tinput, &str);
|
|---|
| 138 | if (rc != EOK) {
|
|---|
| 139 | assert(rc == EIO || rc == ENOENT);
|
|---|
| 140 |
|
|---|
| 141 | if (rc == ENOENT) {
|
|---|
| 142 | return ENOENT;
|
|---|
| 143 | } else {
|
|---|
| 144 | /* rc == EIO */
|
|---|
| 145 | return EIO;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | c = strtoul(str, &eptr, 10);
|
|---|
| 150 | if (*eptr != '\0' || c < 1 || c > (unsigned long)i - 1) {
|
|---|
| 151 | printf("Invalid choice. Try again.\n");
|
|---|
| 152 | free(str);
|
|---|
| 153 | goto again;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | free(str);
|
|---|
| 157 |
|
|---|
| 158 | i = 1;
|
|---|
| 159 | list_foreach(choice->opts, lchoice, nchoice_opt_t, opt) {
|
|---|
| 160 | if ((unsigned long)i == c) {
|
|---|
| 161 | *rarg = opt->arg;
|
|---|
| 162 | return EOK;
|
|---|
| 163 | }
|
|---|
| 164 | ++i;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | assert(false);
|
|---|
| 168 | return ENOENT;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /** @}
|
|---|
| 172 | */
|
|---|