fork of PCE focusing on macplus, supporting DaynaPort SCSI network emulation
1/*****************************************************************************
2 * pce *
3 *****************************************************************************/
4
5/*****************************************************************************
6 * File name: src/lib/console.c *
7 * Created: 2006-06-19 by Hampa Hug <hampa@hampa.ch> *
8 * Copyright: (C) 2006-2024 Hampa Hug <hampa@hampa.ch> *
9 *****************************************************************************/
10
11/*****************************************************************************
12 * This program is free software. You can redistribute it and / or modify it *
13 * under the terms of the GNU General Public License version 2 as published *
14 * by the Free Software Foundation. *
15 * *
16 * This program is distributed in the hope that it will be useful, but *
17 * WITHOUT ANY WARRANTY, without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
19 * Public License for more details. *
20 *****************************************************************************/
21
22
23#include <config.h>
24
25#include "console.h"
26
27#include <stdarg.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
32#ifdef PCE_ENABLE_READLINE
33#include <readline/readline.h>
34#include <readline/history.h>
35#endif
36
37
38static FILE *pce_fp_inp = NULL;
39static FILE *pce_fp_out = NULL;
40
41static FILE *pce_redir_inp = NULL;
42static FILE *pce_redir_out = NULL;
43
44
45FILE *pce_get_redir_inp (void)
46{
47 return (pce_redir_inp);
48}
49
50FILE *pce_get_redir_out (void)
51{
52 return (pce_redir_out);
53}
54
55FILE *pce_get_fp_inp (void)
56{
57 return (pce_fp_inp);
58}
59
60FILE *pce_get_fp_out (void)
61{
62 return (pce_fp_out);
63}
64
65int pce_set_redir_inp (const char *fname)
66{
67 if (pce_redir_inp != NULL) {
68 fclose (pce_redir_inp);
69 pce_redir_inp = NULL;
70 }
71
72 if (fname == NULL) {
73 return (0);
74 }
75
76 pce_redir_inp = fopen (fname, "r");
77
78 if (pce_redir_inp == NULL) {
79 return (1);
80 }
81
82 return (0);
83}
84
85int pce_set_redir_out (const char *fname, const char *mode)
86{
87 if (pce_redir_out != NULL) {
88 fclose (pce_redir_out);
89 pce_redir_out = NULL;
90 }
91
92 if (fname == NULL) {
93 return (0);
94 }
95
96 pce_redir_out = fopen (fname, mode);
97
98 if (pce_redir_out == NULL) {
99 return (1);
100 }
101
102 return (0);
103}
104
105int pce_gets (const char *prompt, char *str, unsigned max)
106{
107 str[0] = 0;
108
109 if (pce_redir_inp != NULL) {
110 if (fgets (str, max, pce_redir_inp) == NULL) {
111 str[0] = 0;
112 }
113
114 if (str[0] != 0) {
115 pce_puts (str);
116 return (0);
117 }
118
119 fclose (pce_redir_inp);
120 pce_redir_inp = NULL;
121 }
122
123#ifdef PCE_ENABLE_READLINE
124 {
125 char *tmp;
126
127 if (pce_redir_out != NULL) {
128 fputs (prompt, pce_redir_out);
129 }
130
131 tmp = readline (prompt);
132
133 if (tmp == NULL) {
134 return (1);
135 }
136 else if (strlen (tmp) < max) {
137 strcpy (str, tmp);
138 }
139
140 if (*str != 0) {
141 add_history (str);
142 }
143
144 free (tmp);
145 }
146#else
147 if (pce_fp_inp == NULL) {
148 pce_fp_inp = stdin;
149 }
150
151 if (prompt != NULL) {
152 pce_puts (prompt);
153 }
154
155 if (fgets (str, max, pce_fp_inp) == NULL) {
156 str[0] = 0;
157 }
158#endif
159
160 if (pce_redir_out != NULL) {
161 fputs (str, pce_redir_out);
162 putc ('\n', pce_redir_out);
163 }
164
165 return (0);
166}
167
168int pce_putc (int c)
169{
170 if (pce_fp_out == NULL) {
171 pce_fp_out = stdout;
172 }
173
174 fputc (c, pce_fp_out);
175 fflush (pce_fp_out);
176
177 if (pce_redir_out != NULL) {
178 fputc (c, pce_redir_out);
179 fflush (pce_redir_out);
180 }
181
182 return (c);
183}
184
185int pce_puts (const char *str)
186{
187 int r;
188
189 if (pce_fp_out == NULL) {
190 pce_fp_out = stdout;
191 }
192
193 r = fputs (str, pce_fp_out);
194 fflush (pce_fp_out);
195
196 if (pce_redir_out != NULL) {
197 fputs (str, pce_redir_out);
198 fflush (pce_redir_out);
199 }
200
201 return (r);
202}
203
204int pce_printf (const char *msg, ...)
205{
206 int r;
207 va_list va;
208
209 if (pce_fp_out == NULL) {
210 pce_fp_out = stdout;
211 }
212
213 va_start (va, msg);
214 r = vfprintf (pce_fp_out, msg, va);
215 fflush (pce_fp_out);
216 va_end (va);
217
218 if (pce_redir_out != NULL) {
219 va_start (va, msg);
220 vfprintf (pce_redir_out, msg, va);
221 fflush (pce_redir_out);
222 va_end (va);
223 }
224
225 return (r);
226}
227
228int pce_vprintf (const char *msg, va_list va)
229{
230 int r;
231
232 if (pce_fp_out == NULL) {
233 pce_fp_out = stdout;
234 }
235
236 r = vfprintf (pce_fp_out, msg, va);
237 fflush (pce_fp_out);
238
239 if (pce_redir_out != NULL) {
240 vfprintf (pce_redir_out, msg, va);
241 fflush (pce_redir_out);
242 }
243
244 return (r);
245}
246
247void pce_prt_sep (const char *str)
248{
249 unsigned i, n;
250
251 n = strlen (str);
252
253 pce_puts ("-");
254 pce_puts (str);
255
256 for (i = n + 1; i < 78; i++) {
257 pce_puts ("-");
258 }
259
260 pce_puts ("\n");
261}
262
263void pce_console_init (FILE *inp, FILE *out)
264{
265 pce_fp_inp = inp;
266 pce_fp_out = out;
267
268#ifdef PCE_ENABLE_READLINE
269 using_history();
270#endif
271}
272
273void pce_console_done (void)
274{
275 pce_set_redir_out (NULL, NULL);
276 pce_set_redir_inp (NULL);
277}