source: mainline/uspace/app/sbi/src/input.c@ 12d7710

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 12d7710 was 37f527b, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Update SBI to rev. 144.

  • Property mode set to 100644
File size: 4.6 KB
Line 
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 *
31 * Reads source code from a file.
32 */
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include "mytypes.h"
38#include "strtab.h"
39
40#include "input.h"
41
42#define INPUT_BUFFER_SIZE 256
43
44static int input_init_file(input_t *input, char *fname);
45static void input_init_interactive(input_t *input);
46static void input_init_string(input_t *input, const char *str);
47
48/** Create new input object for reading from file. */
49int input_new_file(input_t **input, char *fname)
50{
51 *input = malloc(sizeof(input_t));
52 if (*input == NULL)
53 return ENOMEM;
54
55 return input_init_file(*input, fname);
56}
57
58/** Create new input object for reading from interactive input. */
59int input_new_interactive(input_t **input)
60{
61 *input = malloc(sizeof(input_t));
62 if (*input == NULL)
63 return ENOMEM;
64
65 input_init_interactive(*input);
66 return EOK;
67}
68
69/** Create new input object for reading from string. */
70int input_new_string(input_t **input, const char *str)
71{
72 *input = malloc(sizeof(input_t));
73 if (*input == NULL)
74 return ENOMEM;
75
76 input_init_string(*input, str);
77 return EOK;
78}
79
80/** Initialize input object for reading from file. */
81static int input_init_file(input_t *input, char *fname)
82{
83 FILE *f;
84
85 f = fopen(fname, "rt");
86 if (f == NULL)
87 return ENOENT;
88
89 input->buffer = malloc(INPUT_BUFFER_SIZE);
90 if (input->buffer == NULL) {
91 printf("Memory allocation failed.\n");
92 exit(1);
93 }
94
95 input->line_no = 0;
96 input->fin = f;
97 return EOK;
98}
99
100/** Initialize input object for reading from interactive input. */
101static void input_init_interactive(input_t *input)
102{
103 input->buffer = malloc(INPUT_BUFFER_SIZE);
104 if (input->buffer == NULL) {
105 printf("Memory allocation failed.\n");
106 exit(1);
107 }
108
109 input->line_no = 0;
110 input->fin = NULL;
111}
112
113/** Initialize input object for reading from string. */
114static void input_init_string(input_t *input, const char *str)
115{
116 input->buffer = malloc(INPUT_BUFFER_SIZE);
117 if (input->buffer == NULL) {
118 printf("Memory allocation failed.\n");
119 exit(1);
120 }
121
122 input->str = str;
123 input->line_no = 0;
124 input->fin = NULL;
125}
126
127/** Get next line of input. */
128int input_get_line(input_t *input, char **line)
129{
130 const char *sp;
131 char *dp;
132 size_t cnt;
133
134 if (input->fin != NULL) {
135 /* Reading from file. */
136 if (fgets(input->buffer, INPUT_BUFFER_SIZE, input->fin) == NULL)
137 input->buffer[0] = '\0';
138
139 if (ferror(input->fin))
140 return EIO;
141
142 *line = input->buffer;
143 } else if (input->str != NULL) {
144 /* Reading from a string constant. */
145
146 /* Copy one line. */
147 sp = input->str;
148 dp = input->buffer;
149 cnt = 0;
150 while (*sp != '\n' && *sp != '\0' &&
151 cnt < INPUT_BUFFER_SIZE - 2) {
152 *dp++ = *sp++;
153 }
154
155 /* Advance to start of next line. */
156 if (*sp == '\n')
157 *dp++ = *sp++;
158
159 *dp++ = '\0';
160 input->str = sp;
161 *line = input->buffer;
162 } else {
163 /* Interactive mode */
164 if (input->line_no == 0)
165 printf("sbi> ");
166 else
167 printf("... ");
168
169 if (fgets(input->buffer, INPUT_BUFFER_SIZE, stdin) == NULL)
170 input->buffer[0] = '\0';
171
172 if (ferror(stdin))
173 return EIO;
174
175 *line = input->buffer;
176 }
177
178 ++input->line_no;
179 return EOK;
180}
181
182/** Get number of the last provided line of input. */
183int input_get_line_no(input_t *input)
184{
185 return input->line_no;
186}
Note: See TracBrowser for help on using the repository browser.