2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 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. This is copied to gen/strgen.c. Make sure
58 * to update that if this changes.
59 * @param path The path to the file to open.
60 * @param mode The mode to open in.
63 bc_read_open(const char* path, int mode)
68 fd = open(path, mode);
71 open(&fd, path, mode);
78 * Returns true if the buffer data is non-text.
79 * @param buf The buffer to test.
80 * @param size The size of the buffer.
83 bc_read_binary(const char* buf, size_t size)
87 for (i = 0; i < size; ++i)
89 if (BC_ERR(BC_READ_BIN_CHAR(buf[i]))) return true;
96 bc_read_buf(BcVec* vec, char* buf, size_t* buf_len)
100 // If nothing there, return.
101 if (!*buf_len) return false;
104 nl = strchr(buf, '\n');
106 // If a newline exists...
109 // Get the size of the data up to, and including, the newline.
110 size_t nllen = (size_t) ((nl + 1) - buf);
112 nllen = *buf_len >= nllen ? nllen : *buf_len;
114 // Move data into the vector, and move the rest of the data in the
116 bc_vec_npush(vec, nllen, buf);
119 memmove(buf, nl + 1, *buf_len + 1);
124 // Just put the data into the vector.
125 bc_vec_npush(vec, *buf_len, buf);
132 bc_read_chars(BcVec* vec, const char* prompt)
136 assert(vec != NULL && vec->size == sizeof(char));
138 BC_SIG_ASSERT_NOT_LOCKED;
143 // Handle the prompt, if desired.
146 bc_file_puts(&vm->fout, bc_flush_none, prompt);
147 bc_file_flush(&vm->fout, bc_flush_none);
150 // Try reading from the buffer, and if successful, just return.
151 if (bc_read_buf(vec, vm->buf, &vm->buf_len))
153 bc_vec_pushByte(vec, '\0');
154 return BC_STATUS_SUCCESS;
157 // Loop until we have something.
164 // Read data from stdin.
165 r = read(STDIN_FILENO, vm->buf + vm->buf_len,
166 BC_VM_STDIN_BUF_SIZE - vm->buf_len);
168 // If there was an error...
169 if (BC_UNLIKELY(r < 0))
176 // Jump out if we are supposed to quit, which certain signals
178 if (vm->status == (sig_atomic_t) BC_STATUS_QUIT) BC_JMP;
180 assert(vm->sig != 0);
184 // Clear the signal and status.
186 vm->status = (sig_atomic_t) BC_STATUS_SUCCESS;
189 // We don't want to print anything on a SIGWINCH.
193 // Print the ready message and prompt again.
194 bc_file_puts(&vm->fout, bc_flush_none,
195 bc_program_ready_msg);
198 bc_file_puts(&vm->fout, bc_flush_none, prompt);
200 bc_file_flush(&vm->fout, bc_flush_none);
210 // If we get here, it's bad. Barf.
211 bc_vm_fatalError(BC_ERR_FATAL_IO_ERR);
216 // If we read nothing, make sure to terminate the string and return EOF.
219 bc_vec_pushByte(vec, '\0');
220 return BC_STATUS_EOF;
225 // Add to the buffer.
226 vm->buf_len += (size_t) r;
227 vm->buf[vm->buf_len] = '\0';
229 // Read from the buffer.
230 done = bc_read_buf(vec, vm->buf, &vm->buf_len);
235 // Terminate the string.
236 bc_vec_pushByte(vec, '\0');
238 return BC_STATUS_SUCCESS;
242 bc_read_line(BcVec* vec, const char* prompt)
246 #if BC_ENABLE_HISTORY
247 // Get a line from either history or manual reading.
248 if (BC_TTY && !vm->history.badTerm)
250 s = bc_history_line(&vm->history, vec, prompt);
252 else s = bc_read_chars(vec, prompt);
253 #else // BC_ENABLE_HISTORY
254 s = bc_read_chars(vec, prompt);
255 #endif // BC_ENABLE_HISTORY
257 if (BC_ERR(bc_read_binary(vec->v, vec->len - 1)))
259 bc_verr(BC_ERR_FATAL_BIN_FILE, bc_program_stdin_name);
266 bc_read_file(const char* path)
268 BcErr e = BC_ERR_FATAL_IO_ERR;
269 size_t size, to_read;
275 // This has been copied to gen/strgen.c. Make sure to change that if this
278 BC_SIG_ASSERT_LOCKED;
280 assert(path != NULL);
283 // Need this to quiet MSan.
285 memset(&pstat, 0, sizeof(struct stat));
288 fd = bc_read_open(path, O_RDONLY);
290 // If we can't read a file, we just barf.
291 if (BC_ERR(fd < 0)) bc_verr(BC_ERR_FATAL_FILE_ERR, path);
293 // The reason we call fstat is to eliminate TOCTOU race conditions. This
294 // way, we have an open file, so it's not going anywhere.
295 if (BC_ERR(fstat(fd, &pstat) == -1)) goto malloc_err;
297 // Make sure it's not a directory.
298 if (BC_ERR(S_ISDIR(pstat.st_mode)))
300 e = BC_ERR_FATAL_PATH_DIR;
304 // Get the size of the file and allocate that much.
305 size = (size_t) pstat.st_size;
306 buf = bc_vm_malloc(size + 1);
312 // Read the file. We just bail if a signal interrupts. This is so that
313 // users can interrupt the reading of big files if they want.
314 ssize_t r = read(fd, buf2, to_read);
315 if (BC_ERR(r < 0)) goto read_err;
316 to_read -= (size_t) r;
321 // Got to have a nul byte.
324 if (BC_ERR(bc_read_binary(buf, size)))
326 e = BC_ERR_FATAL_BIN_FILE;