2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Code to handle special I/O for bc.
57 * A portability file open function.
58 * @param path The path to the file to open.
59 * @param mode The mode to open in.
61 static int bc_read_open(const char* path, int mode) {
66 fd = open(path, mode);
69 open(&fd, path, mode);
76 * Returns true if the buffer data is non-text.
77 * @param buf The buffer to test.
78 * @param size The size of the buffer.
80 static bool bc_read_binary(const char *buf, size_t size) {
84 for (i = 0; i < size; ++i) {
85 if (BC_ERR(BC_READ_BIN_CHAR(buf[i]))) return true;
91 bool bc_read_buf(BcVec *vec, char *buf, size_t *buf_len) {
95 // If nothing there, return.
96 if (!*buf_len) return false;
99 nl = strchr(buf, '\n');
101 // If a newline exists...
104 // Get the size of the data up to, and including, the newline.
105 size_t nllen = (size_t) ((nl + 1) - buf);
107 nllen = *buf_len >= nllen ? nllen : *buf_len;
109 // Move data into the vector, and move the rest of the data in the
111 bc_vec_npush(vec, nllen, buf);
113 memmove(buf, nl + 1, *buf_len + 1);
118 // Just put the data into the vector.
119 bc_vec_npush(vec, *buf_len, buf);
125 BcStatus bc_read_chars(BcVec *vec, const char *prompt) {
129 assert(vec != NULL && vec->size == sizeof(char));
131 BC_SIG_ASSERT_NOT_LOCKED;
136 // Handle the prompt, if desired.
138 bc_file_puts(&vm.fout, bc_flush_none, prompt);
139 bc_file_flush(&vm.fout, bc_flush_none);
142 // Try reading from the buffer, and if successful, just return.
143 if (bc_read_buf(vec, vm.buf, &vm.buf_len)) {
144 bc_vec_pushByte(vec, '\0');
145 return BC_STATUS_SUCCESS;
148 // Loop until we have something.
155 // Read data from stdin.
156 r = read(STDIN_FILENO, vm.buf + vm.buf_len,
157 BC_VM_STDIN_BUF_SIZE - vm.buf_len);
159 // If there was an error...
160 if (BC_UNLIKELY(r < 0)) {
163 if (errno == EINTR) {
165 // Jump out if we are supposed to quit, which certain signals
167 if (vm.status == (sig_atomic_t) BC_STATUS_QUIT) BC_JMP;
171 // Clear the signal and status.
173 vm.status = (sig_atomic_t) BC_STATUS_SUCCESS;
175 // Print the ready message and prompt again.
176 bc_file_puts(&vm.fout, bc_flush_none, bc_program_ready_msg);
177 if (BC_PROMPT) bc_file_puts(&vm.fout, bc_flush_none, prompt);
178 bc_file_flush(&vm.fout, bc_flush_none);
187 // If we get here, it's bad. Barf.
188 bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);
193 // If we read nothing, make sure to terminate the string and return EOF.
195 bc_vec_pushByte(vec, '\0');
196 return BC_STATUS_EOF;
199 // Add to the buffer.
200 vm.buf_len += (size_t) r;
201 vm.buf[vm.buf_len] = '\0';
203 // Read from the buffer.
204 done = bc_read_buf(vec, vm.buf, &vm.buf_len);
207 // Terminate the string.
208 bc_vec_pushByte(vec, '\0');
210 return BC_STATUS_SUCCESS;
213 BcStatus bc_read_line(BcVec *vec, const char *prompt) {
217 #if BC_ENABLE_HISTORY
218 // Get a line from either history or manual reading.
219 if (BC_TTY && !vm.history.badTerm)
220 s = bc_history_line(&vm.history, vec, prompt);
221 else s = bc_read_chars(vec, prompt);
222 #else // BC_ENABLE_HISTORY
223 s = bc_read_chars(vec, prompt);
224 #endif // BC_ENABLE_HISTORY
226 if (BC_ERR(bc_read_binary(vec->v, vec->len - 1)))
227 bc_verr(BC_ERR_FATAL_BIN_FILE, bc_program_stdin_name);
232 char* bc_read_file(const char *path) {
234 BcErr e = BC_ERR_FATAL_IO_ERR;
235 size_t size, to_read;
241 BC_SIG_ASSERT_LOCKED;
243 assert(path != NULL);
246 // Need this to quiet MSan.
247 memset(&pstat, 0, sizeof(struct stat));
250 fd = bc_read_open(path, O_RDONLY);
252 // If we can't read a file, we just barf.
253 if (BC_ERR(fd < 0)) bc_verr(BC_ERR_FATAL_FILE_ERR, path);
255 // The reason we call fstat is to eliminate TOCTOU race conditions. This
256 // way, we have an open file, so it's not going anywhere.
257 if (BC_ERR(fstat(fd, &pstat) == -1)) goto malloc_err;
259 // Make sure it's not a directory.
260 if (BC_ERR(S_ISDIR(pstat.st_mode))) {
261 e = BC_ERR_FATAL_PATH_DIR;
265 // Get the size of the file and allocate that much.
266 size = (size_t) pstat.st_size;
267 buf = bc_vm_malloc(size + 1);
273 // Read the file. We just bail if a signal interrupts. This is so that
274 // users can interrupt the reading of big files if they want.
275 ssize_t r = read(fd, buf2, to_read);
276 if (BC_ERR(r < 0)) goto read_err;
277 to_read -= (size_t) r;
281 // Got to have a nul byte.
284 if (BC_ERR(bc_read_binary(buf, size))) {
285 e = BC_ERR_FATAL_BIN_FILE;