/*1* *****************************************************************************2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2018-2025 Gavin D. Howard and contributors.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions are met:9*10* * Redistributions of source code must retain the above copyright notice, this11* list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright notice,14* this list of conditions and the following disclaimer in the documentation15* and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*29* *****************************************************************************30*31* Code to handle special I/O for bc.32*33*/3435#include <assert.h>36#include <ctype.h>37#include <errno.h>38#include <stdlib.h>39#include <string.h>4041#include <signal.h>4243#include <fcntl.h>44#include <sys/stat.h>4546#ifndef _WIN3247#include <unistd.h>48#endif // _WIN324950#include <read.h>51#include <history.h>52#include <program.h>53#include <vm.h>5455/**56* A portability file open function. This is copied to gen/strgen.c. Make sure57* to update that if this changes.58* @param path The path to the file to open.59* @param mode The mode to open in.60*/61static int62bc_read_open(const char* path, int mode)63{64int fd;6566#ifndef _WIN3267fd = open(path, mode);68#else // _WIN3269fd = -1;70open(&fd, path, mode);71#endif7273return fd;74}7576/**77* Returns true if the buffer data is non-text.78* @param buf The buffer to test.79* @param size The size of the buffer.80*/81static bool82bc_read_binary(const char* buf, size_t size)83{84size_t i;8586for (i = 0; i < size; ++i)87{88if (BC_ERR(BC_READ_BIN_CHAR(buf[i]))) return true;89}9091return false;92}9394bool95bc_read_buf(BcVec* vec, char* buf, size_t* buf_len)96{97char* nl;9899// If nothing there, return.100if (!*buf_len) return false;101102// Find the newline.103nl = strchr(buf, '\n');104105// If a newline exists...106if (nl != NULL)107{108// Get the size of the data up to, and including, the newline.109size_t nllen = (size_t) ((nl + 1) - buf);110111nllen = *buf_len >= nllen ? nllen : *buf_len;112113// Move data into the vector, and move the rest of the data in the114// buffer up.115bc_vec_npush(vec, nllen, buf);116*buf_len -= nllen;117// NOLINTNEXTLINE118memmove(buf, nl + 1, *buf_len + 1);119120return true;121}122123// Just put the data into the vector.124bc_vec_npush(vec, *buf_len, buf);125*buf_len = 0;126127return false;128}129130BcStatus131bc_read_chars(BcVec* vec, const char* prompt)132{133bool done = false;134135assert(vec != NULL && vec->size == sizeof(char));136137BC_SIG_ASSERT_NOT_LOCKED;138139// Clear the vector.140bc_vec_popAll(vec);141142// Handle the prompt, if desired.143if (BC_PROMPT)144{145bc_file_puts(&vm->fout, bc_flush_none, prompt);146bc_file_flush(&vm->fout, bc_flush_none);147}148149// Try reading from the buffer, and if successful, just return.150if (bc_read_buf(vec, vm->buf, &vm->buf_len))151{152bc_vec_pushByte(vec, '\0');153return BC_STATUS_SUCCESS;154}155156// Loop until we have something.157while (!done)158{159ssize_t r;160161BC_SIG_LOCK;162163// Read data from stdin.164r = read(STDIN_FILENO, vm->buf + vm->buf_len,165BC_VM_STDIN_BUF_SIZE - vm->buf_len);166167// If there was an error...168if (BC_UNLIKELY(r < 0))169{170// If interupted...171if (errno == EINTR)172{173// Jump out if we are supposed to quit, which certain signals174// will require.175if (vm->status == (sig_atomic_t) BC_STATUS_QUIT) BC_JMP;176177assert(vm->sig != 0);178179// Clear the signal and status.180vm->sig = 0;181vm->status = (sig_atomic_t) BC_STATUS_SUCCESS;182183// Print the ready message and prompt again.184bc_file_puts(&vm->fout, bc_flush_none, bc_program_ready_msg);185if (BC_PROMPT)186{187bc_file_puts(&vm->fout, bc_flush_none, prompt);188}189bc_file_flush(&vm->fout, bc_flush_none);190191BC_SIG_UNLOCK;192193continue;194}195196BC_SIG_UNLOCK;197198// If we get here, it's bad. Barf.199bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);200}201202BC_SIG_UNLOCK;203204// If we read nothing, make sure to terminate the string and return EOF.205if (r == 0)206{207bc_vec_pushByte(vec, '\0');208return BC_STATUS_EOF;209}210211BC_SIG_LOCK;212213// Add to the buffer.214vm->buf_len += (size_t) r;215vm->buf[vm->buf_len] = '\0';216217// Read from the buffer.218done = bc_read_buf(vec, vm->buf, &vm->buf_len);219220BC_SIG_UNLOCK;221}222223// Terminate the string.224bc_vec_pushByte(vec, '\0');225226return BC_STATUS_SUCCESS;227}228229BcStatus230bc_read_line(BcVec* vec, const char* prompt)231{232BcStatus s;233234#if BC_ENABLE_HISTORY235// Get a line from either history or manual reading.236if (BC_TTY && !vm->history.badTerm)237{238s = bc_history_line(&vm->history, vec, prompt);239}240else s = bc_read_chars(vec, prompt);241#else // BC_ENABLE_HISTORY242s = bc_read_chars(vec, prompt);243#endif // BC_ENABLE_HISTORY244245if (BC_ERR(bc_read_binary(vec->v, vec->len - 1)))246{247bc_verr(BC_ERR_FATAL_BIN_FILE, bc_program_stdin_name);248}249250return s;251}252253char*254bc_read_file(const char* path)255{256BcErr e = BC_ERR_FATAL_IO_ERR;257size_t size, to_read;258struct stat pstat;259int fd;260char* buf;261char* buf2;262263// This has been copied to gen/strgen.c. Make sure to change that if this264// changes.265266BC_SIG_ASSERT_LOCKED;267268assert(path != NULL);269270#if BC_DEBUG271// Need this to quiet MSan.272// NOLINTNEXTLINE273memset(&pstat, 0, sizeof(struct stat));274#endif // BC_DEBUG275276fd = bc_read_open(path, O_RDONLY);277278// If we can't read a file, we just barf.279if (BC_ERR(fd < 0)) bc_verr(BC_ERR_FATAL_FILE_ERR, path);280281// The reason we call fstat is to eliminate TOCTOU race conditions. This282// way, we have an open file, so it's not going anywhere.283if (BC_ERR(fstat(fd, &pstat) == -1)) goto malloc_err;284285// Make sure it's not a directory.286if (BC_ERR(S_ISDIR(pstat.st_mode)))287{288e = BC_ERR_FATAL_PATH_DIR;289goto malloc_err;290}291292// Get the size of the file and allocate that much.293size = (size_t) pstat.st_size;294buf = bc_vm_malloc(size + 1);295buf2 = buf;296to_read = size;297298do299{300// Read the file. We just bail if a signal interrupts. This is so that301// users can interrupt the reading of big files if they want.302ssize_t r = read(fd, buf2, to_read);303if (BC_ERR(r < 0)) goto read_err;304to_read -= (size_t) r;305buf2 += (size_t) r;306}307while (to_read);308309// Got to have a nul byte.310buf[size] = '\0';311312if (BC_ERR(bc_read_binary(buf, size)))313{314e = BC_ERR_FATAL_BIN_FILE;315goto read_err;316}317318close(fd);319320return buf;321322read_err:323free(buf);324malloc_err:325close(fd);326bc_verr(e, path);327return NULL;328}329330331