source: mainline/uspace/app/hdisk/input.c@ f4a47e52

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f4a47e52 was 0435fe41, checked in by Dominik Taborsky (AT DOT) <brembyseznamcz>, 12 years ago

polishing libmbr, libgpt, hdisk

  • Property mode set to 100644
File size: 3.1 KB
Line 
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
41typedef int (*conv_f)(const char *, char **, unsigned int, bool, void *);
42static int convert(tinput_t * in, conv_f str_f, void * val);
43
44int 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
66uint8_t get_input_uint8(tinput_t * in)
67{
68 int rc;
69 uint32_t val;
70
71 rc = convert(in, (conv_f) str_uint8_t, &val);
72 if (rc != EOK) {
73 errno = rc;
74 return 0;
75 }
76
77 return val;
78}
79
80uint32_t get_input_uint32(tinput_t * in)
81{
82 int rc;
83 uint32_t val;
84
85 rc = convert(in, (conv_f) str_uint32_t, &val);
86 if (rc != EOK) {
87 errno = rc;
88 return 0;
89 }
90
91 return val;
92}
93
94uint64_t get_input_uint64(tinput_t * in)
95{
96 int rc;
97 uint64_t val;
98
99 rc = convert(in, (conv_f) str_uint64_t, &val);
100 if (rc != EOK) {
101 errno = rc;
102 return 0;
103 }
104
105 return val;
106}
107
108size_t get_input_size_t(tinput_t * in)
109{
110 int rc;
111 size_t val;
112
113 rc = convert(in, (conv_f) str_size_t, &val);
114 if (rc != EOK) {
115 errno = rc;
116 return 0;
117 }
118
119 errno = EOK;
120 return val;
121}
122
123static int convert(tinput_t * in, conv_f str_f, void * val)
124{
125 int rc;
126 char * str;
127
128 rc = get_input_line(in, &str);
129 if (rc != EOK) {
130 printf("Error reading input.\n");
131 return rc;
132 }
133
134 rc = str_f(str, NULL, 10, true, val);
135 if (rc != EOK) {
136 printf("Invalid value.\n");
137 }
138
139 free(str);
140 return rc;
141}
142
Note: See TracBrowser for help on using the repository browser.