• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * *****************************************************************************
3  *
4  * Copyright (c) 2018-2019 Gavin D. Howard and contributors.
5  *
6  * All rights reserved.
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 <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <signal.h>
44 
45 #include <fcntl.h>
46 #include <sys/stat.h>
47 #include <unistd.h>
48 
49 #include <read.h>
50 #include <history.h>
51 #include <program.h>
52 #include <vm.h>
53 
bc_read_binary(const char * buf,size_t size)54 static bool bc_read_binary(const char *buf, size_t size) {
55 
56 	size_t i;
57 
58 	for (i = 0; i < size; ++i) {
59 		if (BC_ERR(BC_READ_BIN_CHAR(buf[i]))) return true;
60 	}
61 
62 	return false;
63 }
64 
bc_read_chars(BcVec * vec,const char * prompt)65 BcStatus bc_read_chars(BcVec *vec, const char *prompt) {
66 
67 	int i;
68 	signed char c = 0;
69 
70 	assert(vec != NULL && vec->size == sizeof(char));
71 
72 	bc_vec_npop(vec, vec->len);
73 
74 #if BC_ENABLE_PROMPT
75 	if (BC_USE_PROMPT) {
76 		bc_vm_puts(prompt, stderr);
77 		bc_vm_fflush(stderr);
78 	}
79 #endif // BC_ENABLE_PROMPT
80 
81 	while (BC_NO_SIG && c != '\n') {
82 
83 		i = fgetc(stdin);
84 
85 		if (BC_UNLIKELY(i == EOF)) {
86 
87 #if BC_ENABLE_SIGNALS
88 			if (errno == EINTR) {
89 
90 				if (BC_SIGTERM) return BC_STATUS_QUIT;
91 
92 				vm->sig_chk = vm->sig;
93 
94 				if (BC_TTYIN || BC_I) {
95 					bc_vm_puts(bc_program_ready_msg, stderr);
96 #if BC_ENABLE_PROMPT
97 					if (BC_USE_PROMPT) bc_vm_puts(prompt, stderr);
98 #endif // BC_ENABLE_PROMPT
99 					bc_vm_fflush(stderr);
100 				}
101 				else return BC_STATUS_SIGNAL;
102 
103 				continue;
104 			}
105 #endif // BC_ENABLE_SIGNALS
106 
107 			bc_vec_pushByte(vec, '\0');
108 			return BC_STATUS_EOF;
109 		}
110 
111 		c = (signed char) i;
112 		bc_vec_push(vec, &c);
113 	}
114 
115 	bc_vec_pushByte(vec, '\0');
116 
117 	return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
118 }
119 
bc_read_line(BcVec * vec,const char * prompt)120 BcStatus bc_read_line(BcVec *vec, const char *prompt) {
121 
122 	BcStatus s;
123 
124 	// We are about to output to stderr, so flush stdout to
125 	// make sure that we don't get the outputs mixed up.
126 	bc_vm_fflush(stdout);
127 
128 #if BC_ENABLE_HISTORY
129 	s = bc_history_line(&vm->history, vec, prompt);
130 #else // BC_ENABLE_HISTORY
131 	s = bc_read_chars(vec, prompt);
132 #endif // BC_ENABLE_HISTORY
133 
134 	if (BC_ERR(s && s != BC_STATUS_EOF)) return s;
135 	if (BC_ERR(bc_read_binary(vec->v, vec->len - 1)))
136 		return bc_vm_verr(BC_ERROR_FATAL_BIN_FILE, bc_program_stdin_name);
137 
138 	return s;
139 }
140 
bc_read_file(const char * path,char ** buf)141 BcStatus bc_read_file(const char *path, char **buf) {
142 
143 	BcError e = BC_ERROR_FATAL_IO_ERR;
144 	FILE *f;
145 	size_t size, read;
146 	long res;
147 	struct stat pstat;
148 
149 	assert(path != NULL);
150 
151 	f = fopen(path, "r");
152 	if (BC_ERR(f == NULL)) return bc_vm_verr(BC_ERROR_FATAL_FILE_ERR, path);
153 	if (BC_ERR(fstat(fileno(f), &pstat) == -1)) goto malloc_err;
154 
155 	if (BC_ERR(S_ISDIR(pstat.st_mode))) {
156 		e = BC_ERROR_FATAL_PATH_DIR;
157 		goto malloc_err;
158 	}
159 
160 	if (BC_ERR(fseek(f, 0, SEEK_END) == -1)) goto malloc_err;
161 	res = ftell(f);
162 	if (BC_ERR(res < 0)) goto malloc_err;
163 	if (BC_ERR(fseek(f, 0, SEEK_SET) == -1)) goto malloc_err;
164 
165 	size = (size_t) res;
166 	*buf = bc_vm_malloc(size + 1);
167 
168 	read = fread(*buf, 1, size, f);
169 	if (BC_ERR(read != size)) goto read_err;
170 
171 	(*buf)[size] = '\0';
172 
173 	if (BC_ERR(bc_read_binary(*buf, size))) {
174 		e = BC_ERROR_FATAL_BIN_FILE;
175 		goto read_err;
176 	}
177 
178 	fclose(f);
179 
180 	return BC_STATUS_SUCCESS;
181 
182 read_err:
183 	free(*buf);
184 malloc_err:
185 	fclose(f);
186 	return bc_vm_verr(e, path);
187 }
188