1 /* 2 * ***************************************************************************** 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * * Redistributions of source code must retain the above copyright notice, this 12 * list of conditions and the following disclaimer. 13 * 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. 17 * 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. 29 * 30 * ***************************************************************************** 31 * 32 * Code to handle special I/O for bc. 33 * 34 */ 35 36 #include <assert.h> 37 #include <ctype.h> 38 #include <errno.h> 39 #include <stdlib.h> 40 #include <string.h> 41 42 #include <signal.h> 43 44 #include <fcntl.h> 45 #include <sys/stat.h> 46 #include <unistd.h> 47 48 #include <read.h> 49 #include <history.h> 50 #include <program.h> 51 #include <vm.h> 52 53 static bool bc_read_binary(const char *buf, size_t size) { 54 55 size_t i; 56 57 for (i = 0; i < size; ++i) { 58 if (BC_ERR(BC_READ_BIN_CHAR(buf[i]))) return true; 59 } 60 61 return false; 62 } 63 64 bool bc_read_buf(BcVec *vec, char *buf, size_t *buf_len) { 65 66 char *nl; 67 68 if (!*buf_len) return false; 69 70 nl = strchr(buf, '\n'); 71 72 if (nl != NULL) { 73 74 size_t nllen = (size_t) ((nl + 1) - buf); 75 76 nllen = *buf_len >= nllen ? nllen : *buf_len; 77 78 bc_vec_npush(vec, nllen, buf); 79 *buf_len -= nllen; 80 memmove(buf, nl + 1, *buf_len + 1); 81 82 return true; 83 } 84 85 bc_vec_npush(vec, *buf_len, buf); 86 *buf_len = 0; 87 88 return false; 89 } 90 91 BcStatus bc_read_chars(BcVec *vec, const char *prompt) { 92 93 bool done = false; 94 95 assert(vec != NULL && vec->size == sizeof(char)); 96 97 BC_SIG_ASSERT_NOT_LOCKED; 98 99 bc_vec_popAll(vec); 100 101 #if BC_ENABLE_PROMPT 102 if (BC_USE_PROMPT) { 103 bc_file_puts(&vm.fout, prompt); 104 bc_file_flush(&vm.fout); 105 } 106 #endif // BC_ENABLE_PROMPT 107 108 if (bc_read_buf(vec, vm.buf, &vm.buf_len)) { 109 bc_vec_pushByte(vec, '\0'); 110 return BC_STATUS_SUCCESS; 111 } 112 113 while (!done) { 114 115 ssize_t r; 116 117 BC_SIG_LOCK; 118 119 r = read(STDIN_FILENO, vm.buf + vm.buf_len, 120 BC_VM_STDIN_BUF_SIZE - vm.buf_len); 121 122 if (BC_UNLIKELY(r < 0)) { 123 124 if (errno == EINTR) { 125 126 if (vm.status == (sig_atomic_t) BC_STATUS_QUIT) { 127 BC_SIG_UNLOCK; 128 return BC_STATUS_QUIT; 129 } 130 131 assert(vm.sig); 132 133 vm.status = (sig_atomic_t) BC_STATUS_SUCCESS; 134 #if BC_ENABLE_PROMPT 135 if (BC_USE_PROMPT) bc_file_puts(&vm.fout, prompt); 136 #endif // BC_ENABLE_PROMPT 137 bc_file_flush(&vm.fout); 138 139 BC_SIG_UNLOCK; 140 141 continue; 142 } 143 144 BC_SIG_UNLOCK; 145 146 bc_vm_fatalError(BC_ERR_FATAL_IO_ERR); 147 } 148 149 BC_SIG_UNLOCK; 150 151 if (r == 0) { 152 bc_vec_pushByte(vec, '\0'); 153 return BC_STATUS_EOF; 154 } 155 156 vm.buf_len += (size_t) r; 157 vm.buf[vm.buf_len] = '\0'; 158 159 done = bc_read_buf(vec, vm.buf, &vm.buf_len); 160 } 161 162 bc_vec_pushByte(vec, '\0'); 163 164 return BC_STATUS_SUCCESS; 165 } 166 167 BcStatus bc_read_line(BcVec *vec, const char *prompt) { 168 169 BcStatus s; 170 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 178 179 if (BC_ERR(bc_read_binary(vec->v, vec->len - 1))) 180 bc_vm_verr(BC_ERR_FATAL_BIN_FILE, bc_program_stdin_name); 181 182 return s; 183 } 184 185 void bc_read_file(const char *path, char **buf) { 186 187 BcErr e = BC_ERR_FATAL_IO_ERR; 188 size_t size, r; 189 struct stat pstat; 190 int fd; 191 192 BC_SIG_ASSERT_LOCKED; 193 194 assert(path != NULL); 195 196 fd = open(path, O_RDONLY); 197 if (BC_ERR(fd < 0)) bc_vm_verr(BC_ERR_FATAL_FILE_ERR, path); 198 if (BC_ERR(fstat(fd, &pstat) == -1)) goto malloc_err; 199 200 if (BC_ERR(S_ISDIR(pstat.st_mode))) { 201 e = BC_ERR_FATAL_PATH_DIR; 202 goto malloc_err; 203 } 204 205 size = (size_t) pstat.st_size; 206 *buf = bc_vm_malloc(size + 1); 207 208 r = (size_t) read(fd, *buf, size); 209 if (BC_ERR(r != size)) goto read_err; 210 211 (*buf)[size] = '\0'; 212 213 if (BC_ERR(bc_read_binary(*buf, size))) { 214 e = BC_ERR_FATAL_BIN_FILE; 215 goto read_err; 216 } 217 218 close(fd); 219 220 return; 221 222 read_err: 223 free(*buf); 224 malloc_err: 225 close(fd); 226 bc_vm_verr(e, path); 227 } 228