2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2020 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.
53 static bool bc_read_binary(const char *buf, size_t size) {
57 for (i = 0; i < size; ++i) {
58 if (BC_ERR(BC_READ_BIN_CHAR(buf[i]))) return true;
64 bool bc_read_buf(BcVec *vec, char *buf, size_t *buf_len) {
68 if (!*buf_len) return false;
70 nl = strchr(buf, '\n');
74 size_t nllen = (size_t) ((nl + 1) - buf);
76 nllen = *buf_len >= nllen ? nllen : *buf_len;
78 bc_vec_npush(vec, nllen, buf);
80 memmove(buf, nl + 1, *buf_len + 1);
85 bc_vec_npush(vec, *buf_len, buf);
91 BcStatus bc_read_chars(BcVec *vec, const char *prompt) {
95 assert(vec != NULL && vec->size == sizeof(char));
97 BC_SIG_ASSERT_NOT_LOCKED;
99 bc_vec_npop(vec, vec->len);
103 bc_file_puts(&vm.fout, prompt);
104 bc_file_flush(&vm.fout);
106 #endif // BC_ENABLE_PROMPT
108 if (bc_read_buf(vec, vm.buf, &vm.buf_len)) {
109 bc_vec_pushByte(vec, '\0');
110 return BC_STATUS_SUCCESS;
119 r = read(STDIN_FILENO, vm.buf + vm.buf_len,
120 BC_VM_STDIN_BUF_SIZE - vm.buf_len);
122 if (BC_UNLIKELY(r < 0)) {
124 if (errno == EINTR) {
126 if (vm.status == (sig_atomic_t) BC_STATUS_QUIT) {
128 return BC_STATUS_QUIT;
133 vm.status = (sig_atomic_t) BC_STATUS_SUCCESS;
135 if (BC_USE_PROMPT) bc_file_puts(&vm.fout, prompt);
136 #endif // BC_ENABLE_PROMPT
137 bc_file_flush(&vm.fout);
146 bc_vm_err(BC_ERROR_FATAL_IO_ERR);
152 bc_vec_pushByte(vec, '\0');
153 return BC_STATUS_EOF;
156 vm.buf_len += (size_t) r;
157 vm.buf[vm.buf_len] = '\0';
159 done = bc_read_buf(vec, vm.buf, &vm.buf_len);
162 bc_vec_pushByte(vec, '\0');
164 return BC_STATUS_SUCCESS;
167 BcStatus bc_read_line(BcVec *vec, const char *prompt) {
171 #if BC_ENABLE_HISTORY
172 if (BC_TTY && !vm.history.badTerm)
173 s = bc_history_line(&vm.history, vec, prompt);
174 else s = bc_read_chars(vec, prompt);
175 #else // BC_ENABLE_HISTORY
176 s = bc_read_chars(vec, prompt);
177 #endif // BC_ENABLE_HISTORY
179 if (BC_ERR(bc_read_binary(vec->v, vec->len - 1)))
180 bc_vm_verr(BC_ERROR_FATAL_BIN_FILE, bc_program_stdin_name);
185 void bc_read_file(const char *path, char **buf) {
187 BcError e = BC_ERROR_FATAL_IO_ERR;
192 BC_SIG_ASSERT_LOCKED;
194 assert(path != NULL);
196 fd = open(path, O_RDONLY);
197 if (BC_ERR(fd < 0)) bc_vm_verr(BC_ERROR_FATAL_FILE_ERR, path);
198 if (BC_ERR(fstat(fd, &pstat) == -1)) goto malloc_err;
200 if (BC_ERR(S_ISDIR(pstat.st_mode))) {
201 e = BC_ERROR_FATAL_PATH_DIR;
205 size = (size_t) pstat.st_size;
206 *buf = bc_vm_malloc(size + 1);
208 r = (size_t) read(fd, *buf, size);
209 if (BC_ERR(r != size)) goto read_err;
213 if (BC_ERR(bc_read_binary(*buf, size))) {
214 e = BC_ERROR_FATAL_BIN_FILE;