1 | /*
|
---|
2 | * Copyright (c) 2012, 2013 Dominik Taborsky
|
---|
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 |
|
---|
29 | /** @addtogroup hdisk
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <str.h>
|
---|
36 | #include <errno.h>
|
---|
37 | #include <stdlib.h>
|
---|
38 |
|
---|
39 | #include "input.h"
|
---|
40 |
|
---|
41 | typedef int (*conv_f)(const char *, char **, unsigned int, bool, void *);
|
---|
42 | static int convert(tinput_t * in, conv_f str_f, void * val);
|
---|
43 |
|
---|
44 | int get_input_line(tinput_t * in, char ** str)
|
---|
45 | {
|
---|
46 | int rc;
|
---|
47 | rc = tinput_read(in, str);
|
---|
48 | if (rc == ENOENT) {
|
---|
49 | /* User requested exit */
|
---|
50 | return EINTR;
|
---|
51 | }
|
---|
52 | if (rc != EOK) {
|
---|
53 | /* Error in communication with console */
|
---|
54 | return rc;
|
---|
55 | }
|
---|
56 | /* Check for empty input. */
|
---|
57 | if (str_cmp(*str, "") == 0) {
|
---|
58 | free(*str);
|
---|
59 | *str = NULL;
|
---|
60 | return EINVAL;
|
---|
61 | }
|
---|
62 |
|
---|
63 | return EOK;
|
---|
64 | }
|
---|
65 |
|
---|
66 | uint8_t get_input_uint8(tinput_t * in)
|
---|
67 | {
|
---|
68 | int rc;
|
---|
69 | uint32_t val;
|
---|
70 | /*char * str;
|
---|
71 |
|
---|
72 | rc = get_input_line(in, &str);
|
---|
73 | if (rc != EOK) {
|
---|
74 | printf("Error reading input.\n");
|
---|
75 | return 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | rc = str_uint8_t(str, NULL, 10, true, &val);
|
---|
79 | if( != EOK) {
|
---|
80 | printf("Invalid value.\n");
|
---|
81 | return 0;
|
---|
82 | }
|
---|
83 |
|
---|
84 | free(str);*/
|
---|
85 |
|
---|
86 | rc = convert(in, (conv_f) str_uint8_t, &val);
|
---|
87 | if (rc != EOK) {
|
---|
88 | errno = rc;
|
---|
89 | return 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 | return val;
|
---|
93 | }
|
---|
94 |
|
---|
95 | uint32_t get_input_uint32(tinput_t * in)
|
---|
96 | {
|
---|
97 | int rc;
|
---|
98 | uint32_t val;
|
---|
99 | /*char * str;
|
---|
100 |
|
---|
101 | rc = get_input_line(in, &str);
|
---|
102 | if (rc != EOK) {
|
---|
103 | printf("Error reading input.\n");
|
---|
104 | return 0;
|
---|
105 | }
|
---|
106 |
|
---|
107 | rc = str_uint32_t(str, NULL, 10, true, &val);
|
---|
108 | if( != EOK) {
|
---|
109 | printf("Invalid value.\n");
|
---|
110 | return 0;
|
---|
111 | }
|
---|
112 |
|
---|
113 | free(str);*/
|
---|
114 |
|
---|
115 | rc = convert(in, (conv_f) str_uint32_t, &val);
|
---|
116 | if (rc != EOK) {
|
---|
117 | errno = rc;
|
---|
118 | return 0;
|
---|
119 | }
|
---|
120 |
|
---|
121 | return val;
|
---|
122 | }
|
---|
123 |
|
---|
124 | uint64_t get_input_uint64(tinput_t * in)
|
---|
125 | {
|
---|
126 | int rc;
|
---|
127 | uint64_t val;
|
---|
128 | /*char * str;
|
---|
129 |
|
---|
130 | rc = get_input_line(in, &str);
|
---|
131 | if (rc != EOK) {
|
---|
132 | printf("Error reading input.\n");
|
---|
133 | return 0;
|
---|
134 | }
|
---|
135 |
|
---|
136 | rc = str_uint32_t(str, NULL, 10, true, &val);
|
---|
137 | if( != EOK) {
|
---|
138 | printf("Invalid value.\n");
|
---|
139 | return 0;
|
---|
140 | }
|
---|
141 |
|
---|
142 | free(str);*/
|
---|
143 |
|
---|
144 | rc = convert(in, (conv_f) str_uint64_t, &val);
|
---|
145 | if (rc != EOK) {
|
---|
146 | errno = rc;
|
---|
147 | return 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | return val;
|
---|
151 | }
|
---|
152 |
|
---|
153 | size_t get_input_size_t(tinput_t * in)
|
---|
154 | {
|
---|
155 | int rc;
|
---|
156 | size_t val;
|
---|
157 |
|
---|
158 | /*char * str;
|
---|
159 | rc = get_input_line(in, &str);
|
---|
160 | if (rc != EOK) {
|
---|
161 | printf("Error reading input.\n");
|
---|
162 | return 0;
|
---|
163 | }
|
---|
164 |
|
---|
165 | rc = str_size_t(str, NULL, 10, true, &val);
|
---|
166 | if (rc != EOK) {
|
---|
167 | printf("Invalid value.\n");
|
---|
168 | return 0;
|
---|
169 | }
|
---|
170 | free(str);*/
|
---|
171 |
|
---|
172 | rc = convert(in, (conv_f) str_size_t, &val);
|
---|
173 | if (rc != EOK) {
|
---|
174 | errno = rc;
|
---|
175 | return 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 | errno = EOK;
|
---|
179 | return val;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static int convert(tinput_t * in, conv_f str_f, void * val)
|
---|
183 | {
|
---|
184 | int rc;
|
---|
185 | char * str;
|
---|
186 |
|
---|
187 | rc = get_input_line(in, &str);
|
---|
188 | if (rc != EOK) {
|
---|
189 | printf("Error reading input.\n");
|
---|
190 | return rc;
|
---|
191 | }
|
---|
192 |
|
---|
193 | rc = str_f(str, NULL, 10, true, val);
|
---|
194 | if(rc != EOK) {
|
---|
195 | printf("Invalid value.\n");
|
---|
196 | }
|
---|
197 |
|
---|
198 | free(str);
|
---|
199 | return rc;
|
---|
200 | }
|
---|
201 |
|
---|