xref: /freebsd/contrib/bc/include/read.h (revision d101cdd6)
1252884aeSStefan Eßer /*
2252884aeSStefan Eßer  * *****************************************************************************
3252884aeSStefan Eßer  *
43aa99676SStefan Eßer  * SPDX-License-Identifier: BSD-2-Clause
5252884aeSStefan Eßer  *
6d101cdd6SStefan Eßer  * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
7252884aeSStefan Eßer  *
8252884aeSStefan Eßer  * Redistribution and use in source and binary forms, with or without
9252884aeSStefan Eßer  * modification, are permitted provided that the following conditions are met:
10252884aeSStefan Eßer  *
11252884aeSStefan Eßer  * * Redistributions of source code must retain the above copyright notice, this
12252884aeSStefan Eßer  *   list of conditions and the following disclaimer.
13252884aeSStefan Eßer  *
14252884aeSStefan Eßer  * * Redistributions in binary form must reproduce the above copyright notice,
15252884aeSStefan Eßer  *   this list of conditions and the following disclaimer in the documentation
16252884aeSStefan Eßer  *   and/or other materials provided with the distribution.
17252884aeSStefan Eßer  *
18252884aeSStefan Eßer  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19252884aeSStefan Eßer  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20252884aeSStefan Eßer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21252884aeSStefan Eßer  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22252884aeSStefan Eßer  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23252884aeSStefan Eßer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24252884aeSStefan Eßer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25252884aeSStefan Eßer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26252884aeSStefan Eßer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27252884aeSStefan Eßer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28252884aeSStefan Eßer  * POSSIBILITY OF SUCH DAMAGE.
29252884aeSStefan Eßer  *
30252884aeSStefan Eßer  * *****************************************************************************
31252884aeSStefan Eßer  *
32252884aeSStefan Eßer  * Code to handle special I/O for bc.
33252884aeSStefan Eßer  *
34252884aeSStefan Eßer  */
35252884aeSStefan Eßer 
363aa99676SStefan Eßer #ifndef BC_READ_H
373aa99676SStefan Eßer #define BC_READ_H
38252884aeSStefan Eßer 
39252884aeSStefan Eßer #include <stdlib.h>
40252884aeSStefan Eßer 
41252884aeSStefan Eßer #include <status.h>
42252884aeSStefan Eßer #include <vector.h>
43252884aeSStefan Eßer 
4444d4804dSStefan Eßer /**
4544d4804dSStefan Eßer  * Returns true if @a c is a non-ASCII (invalid) char.
4644d4804dSStefan Eßer  * @param c  The character to test.
4744d4804dSStefan Eßer  * @return   True if @a c is an invalid char.
4844d4804dSStefan Eßer  */
4944d4804dSStefan Eßer #define BC_READ_BIN_CHAR(c) (!(c))
50252884aeSStefan Eßer 
5144d4804dSStefan Eßer /**
5244d4804dSStefan Eßer  * Reads a line from stdin after printing prompt, if desired.
5344d4804dSStefan Eßer  * @param vec     The vector to put the stdin data into.
5444d4804dSStefan Eßer  * @param prompt  The prompt to print, if desired.
5544d4804dSStefan Eßer  */
5678bc019dSStefan Eßer BcStatus
5778bc019dSStefan Eßer bc_read_line(BcVec* vec, const char* prompt);
5844d4804dSStefan Eßer 
5944d4804dSStefan Eßer /**
6044d4804dSStefan Eßer  * Read a file and return a buffer with the data. The buffer must be freed by
6144d4804dSStefan Eßer  * the caller.
6244d4804dSStefan Eßer  * @param path  The path to the file to read.
6344d4804dSStefan Eßer  */
6478bc019dSStefan Eßer char*
6578bc019dSStefan Eßer bc_read_file(const char* path);
6644d4804dSStefan Eßer 
6744d4804dSStefan Eßer /**
6844d4804dSStefan Eßer  * Helper function for reading characters from stdin. This takes care of a bunch
6944d4804dSStefan Eßer  * of complex error handling. Thus, it returns a status instead of throwing an
7044d4804dSStefan Eßer  * error, except for fatal errors.
7144d4804dSStefan Eßer  * @param vec     The vec to put the stdin into.
7244d4804dSStefan Eßer  * @param prompt  The prompt to print, if desired.
7344d4804dSStefan Eßer  */
7478bc019dSStefan Eßer BcStatus
7578bc019dSStefan Eßer bc_read_chars(BcVec* vec, const char* prompt);
7644d4804dSStefan Eßer 
7744d4804dSStefan Eßer /**
7844d4804dSStefan Eßer  * Read a line from buf into vec.
7944d4804dSStefan Eßer  * @param vec      The vector to read data into.
8044d4804dSStefan Eßer  * @param buf      The buffer to read from.
8144d4804dSStefan Eßer  * @param buf_len  The length of the buffer.
8244d4804dSStefan Eßer  */
8378bc019dSStefan Eßer bool
8478bc019dSStefan Eßer bc_read_buf(BcVec* vec, char* buf, size_t* buf_len);
85252884aeSStefan Eßer 
863aa99676SStefan Eßer #endif // BC_READ_H
87