source: mainline/uspace/app/sbi/src/input.c@ 28ca31ed

Last change on this file since 28ca31ed was ae7d03c, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Selected ccheck-proposed comment fixes.

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[09ababb7]1/*
2 * Copyright (c) 2010 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
29/** @file Input module
30 *
[1ebc1a62]31 * Reads source code. Currently input can be read from a file (standard
32 * case), from a string literal (when parsing builtin code) or interactively
33 * from the user.
[09ababb7]34 */
35
36#include <stdio.h>
37#include <stdlib.h>
38#include "mytypes.h"
[1ebc1a62]39#include "os/os.h"
[09ababb7]40#include "strtab.h"
41
42#include "input.h"
43
[1ebc1a62]44/** Size of input buffer. XXX This limits the maximum line length. */
[09ababb7]45#define INPUT_BUFFER_SIZE 256
46
[b7fd2a0]47static errno_t input_init_file(input_t *input, const char *fname);
[37f527b]48static void input_init_interactive(input_t *input);
49static void input_init_string(input_t *input, const char *str);
[09ababb7]50
[1ebc1a62]51/** Create new input object for reading from file.
52 *
53 * @param input Place to store pointer to new input object.
54 * @param fname Name of file to read from.
55 *
56 * @return EOK on success, ENOMEM when allocation fails,
57 * ENOENT when opening file fails.
58 */
[b7fd2a0]59errno_t input_new_file(input_t **input, const char *fname)
[09ababb7]60{
61 *input = malloc(sizeof(input_t));
62 if (*input == NULL)
63 return ENOMEM;
64
[37f527b]65 return input_init_file(*input, fname);
[09ababb7]66}
67
[1ebc1a62]68/** Create new input object for reading from interactive input.
69 *
70 * @param input Place to store pointer to new input object.
71 * @return EOK on success, ENOMEM when allocation fails.
72 */
[b7fd2a0]73errno_t input_new_interactive(input_t **input)
[37f527b]74{
75 *input = malloc(sizeof(input_t));
76 if (*input == NULL)
77 return ENOMEM;
78
79 input_init_interactive(*input);
80 return EOK;
81}
82
[1ebc1a62]83/** Create new input object for reading from string.
84 *
85 * @param input Place to store pointer to new input object.
86 * @param str String literal from which to read input.
87 * @return EOK on success, ENOMEM when allocation fails.
88 */
[b7fd2a0]89errno_t input_new_string(input_t **input, const char *str)
[37f527b]90{
91 *input = malloc(sizeof(input_t));
92 if (*input == NULL)
93 return ENOMEM;
94
95 input_init_string(*input, str);
96 return EOK;
97}
98
[1ebc1a62]99/** Initialize input object for reading from file.
100 *
101 * @param input Input object.
102 * @param fname Name of file to read from.
103 *
104 * @return EOK on success, ENOENT when opening file fails.
[ae7d03c]105 */
[b7fd2a0]106static errno_t input_init_file(input_t *input, const char *fname)
[09ababb7]107{
108 FILE *f;
109
110 f = fopen(fname, "rt");
111 if (f == NULL)
112 return ENOENT;
113
114 input->buffer = malloc(INPUT_BUFFER_SIZE);
[fa36f29]115 if (input->buffer == NULL) {
116 printf("Memory allocation failed.\n");
117 exit(1);
118 }
[09ababb7]119
[051bc69a]120 input->name = os_str_dup(fname);
[1ebc1a62]121 input->str = NULL;
[09ababb7]122 input->line_no = 0;
123 input->fin = f;
124 return EOK;
125}
126
[1ebc1a62]127/** Initialize input object for reading from interactive input.
128 *
129 * @param input Input object.
130 */
[37f527b]131static void input_init_interactive(input_t *input)
132{
133 input->buffer = malloc(INPUT_BUFFER_SIZE);
134 if (input->buffer == NULL) {
135 printf("Memory allocation failed.\n");
136 exit(1);
137 }
138
[051bc69a]139 input->name = "<user-input>";
[1ebc1a62]140 input->str = NULL;
[37f527b]141 input->line_no = 0;
142 input->fin = NULL;
143}
144
[1ebc1a62]145/** Initialize input object for reading from string.
146 *
147 * @param input Input object.
148 * @param str String literal from which to read input.
149 */
[37f527b]150static void input_init_string(input_t *input, const char *str)
151{
152 input->buffer = malloc(INPUT_BUFFER_SIZE);
153 if (input->buffer == NULL) {
154 printf("Memory allocation failed.\n");
155 exit(1);
156 }
157
[051bc69a]158 input->name = "<builtin>";
[37f527b]159 input->str = str;
160 input->line_no = 0;
161 input->fin = NULL;
162}
163
[1ebc1a62]164/** Get next line of input.
165 *
166 * The pointer stored in @a line is owned by @a input and is valid until the
167 * next call to input_get_line(). The caller is not to free it. The returned
168 * line is terminated with '\n' if another line is coming (potentially empty).
169 * An empty line ("") signals end of input.
170 *
171 * @param input Input object.
172 * @param line Place to store pointer to next line.
173 *
174 * @return EOK on success, EIO on failure.
175 */
[b7fd2a0]176errno_t input_get_line(input_t *input, char **line)
[09ababb7]177{
[9be9c4d]178 const char *prompt;
[37f527b]179 const char *sp;
180 char *dp;
[1ebc1a62]181 char *line_p;
[37f527b]182 size_t cnt;
183
184 if (input->fin != NULL) {
185 /* Reading from file. */
186 if (fgets(input->buffer, INPUT_BUFFER_SIZE, input->fin) == NULL)
187 input->buffer[0] = '\0';
188
189 if (ferror(input->fin))
190 return EIO;
191
192 *line = input->buffer;
193 } else if (input->str != NULL) {
194 /* Reading from a string constant. */
195
196 /* Copy one line. */
197 sp = input->str;
198 dp = input->buffer;
199 cnt = 0;
200 while (*sp != '\n' && *sp != '\0' &&
201 cnt < INPUT_BUFFER_SIZE - 2) {
202 *dp++ = *sp++;
203 }
[09ababb7]204
[37f527b]205 /* Advance to start of next line. */
206 if (*sp == '\n')
207 *dp++ = *sp++;
208
[c5cb943d]209 *dp = '\0';
[37f527b]210 input->str = sp;
211 *line = input->buffer;
212 } else {
213 /* Interactive mode */
214 if (input->line_no == 0)
[9be9c4d]215 prompt = "sbi> ";
[37f527b]216 else
[9be9c4d]217 prompt = "... ";
[37f527b]218
[1ebc1a62]219 fflush(stdout);
[9be9c4d]220 if (os_input_line(prompt, &line_p) != EOK)
[37f527b]221 return EIO;
222
[1ebc1a62]223 *line = line_p;
[37f527b]224 }
[09ababb7]225
226 ++input->line_no;
227 return EOK;
228}
229
[1ebc1a62]230/** Get number of the last provided line of input.
231 *
[051bc69a]232 * @param input Input object.
[1ebc1a62]233 * @return Line number of the last provided input line (counting
234 * from 1 up).
235 */
[09ababb7]236int input_get_line_no(input_t *input)
237{
238 return input->line_no;
239}
Note: See TracBrowser for help on using the repository browser.