2 * Copyright © 2018 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include "ao_scheme.h"
17 #ifdef AO_SCHEME_FEATURE_PORT
19 static void port_mark(void *addr)
24 static int port_size(void *addr)
27 return sizeof(struct ao_scheme_port);
30 static void port_move(void *addr)
32 struct ao_scheme_port *port = addr;
34 (void) ao_scheme_poly_move(&port->next, 0);
37 const struct ao_scheme_type ao_scheme_port_type = {
45 ao_scheme_port_write(FILE *out, ao_poly v, bool write)
48 ao_scheme_fprintf(out, "#port<%d>", fileno(ao_scheme_poly_port(v)->file));
51 ao_poly ao_scheme_stdin, ao_scheme_stdout, ao_scheme_stderr;
53 ao_poly ao_scheme_open_ports;
56 ao_scheme_port_check_references(void)
58 struct ao_scheme_port *p;
60 for (p = ao_scheme_poly_port(ao_scheme_open_ports); p; p = ao_scheme_poly_port(p->next)) {
61 if (!ao_scheme_marked(p))
62 ao_scheme_port_close(p);
66 struct ao_scheme_port *
67 ao_scheme_port_alloc(FILE *file, bool stayopen)
69 struct ao_scheme_port *p;
71 p = ao_scheme_alloc(sizeof (struct ao_scheme_port));
74 p->type = AO_SCHEME_PORT;
75 p->stayopen = stayopen;
77 p->next = ao_scheme_open_ports;
78 ao_scheme_open_ports = ao_scheme_port_poly(p);
83 ao_scheme_port_close(struct ao_scheme_port *port)
86 struct ao_scheme_port *ref;
88 if (port->file && !port->stayopen) {
91 for (prev = &ao_scheme_open_ports; (ref = ao_scheme_poly_port(*prev)); prev = &ref->next)
100 ao_scheme_do_portp(struct ao_scheme_cons *cons)
102 return ao_scheme_do_typep(_ao_scheme_atom_port3f, AO_SCHEME_PORT, cons);
106 ao_scheme_do_port_openp(struct ao_scheme_cons *cons)
108 struct ao_scheme_port *port;
110 if (!ao_scheme_parse_args(_ao_scheme_atom_port2dopen3f, cons,
111 AO_SCHEME_PORT, &port,
113 return AO_SCHEME_NIL;
114 return port->file ? _ao_scheme_bool_true : _ao_scheme_bool_false;
118 ao_scheme_do_open_file(ao_poly proc, struct ao_scheme_cons *cons, const char *mode)
121 struct ao_scheme_string *name;
123 if (!ao_scheme_parse_args(proc, cons,
124 AO_SCHEME_STRING, &name,
126 return AO_SCHEME_NIL;
127 file = fopen(name->val, mode);
129 return ao_scheme_error(AO_SCHEME_FILEERROR,
130 "%v: no such file \"%v\"",
132 return ao_scheme_port_poly(ao_scheme_port_alloc(file, false));
136 ao_scheme_do_open_input_file(struct ao_scheme_cons *cons)
138 return ao_scheme_do_open_file(_ao_scheme_atom_open2dinput2dfile, cons, "r");
142 ao_scheme_do_open_output_file(struct ao_scheme_cons *cons)
144 return ao_scheme_do_open_file(_ao_scheme_atom_open2doutput2dfile, cons, "w");
148 ao_scheme_do_close_port(struct ao_scheme_cons *cons)
150 struct ao_scheme_port *port;
152 if (!ao_scheme_parse_args(_ao_scheme_atom_port2dopen3f, cons,
153 AO_SCHEME_PORT, &port,
155 return AO_SCHEME_NIL;
156 ao_scheme_port_close(port);
157 return _ao_scheme_bool_true;
161 ao_scheme_do_current_input_port(struct ao_scheme_cons *cons)
163 if (!ao_scheme_parse_args(_ao_scheme_atom_current2dinput2dport, cons,
165 return AO_SCHEME_NIL;
166 if (!ao_scheme_stdin)
167 ao_scheme_stdin = ao_scheme_port_poly(ao_scheme_port_alloc(stdin, true));
168 return ao_scheme_stdin;
172 ao_scheme_do_current_output_port(struct ao_scheme_cons *cons)
174 if (!ao_scheme_parse_args(_ao_scheme_atom_current2doutput2dport, cons,
176 return AO_SCHEME_NIL;
177 if (!ao_scheme_stdout)
178 ao_scheme_stdout = ao_scheme_port_poly(ao_scheme_port_alloc(stdout, true));
179 return ao_scheme_stdout;
183 ao_scheme_do_current_error_port(struct ao_scheme_cons *cons)
185 if (!ao_scheme_parse_args(_ao_scheme_atom_current2derror2dport, cons,
187 return AO_SCHEME_NIL;
188 if (!ao_scheme_stderr)
189 ao_scheme_stderr = ao_scheme_port_poly(ao_scheme_port_alloc(stderr, true));
190 return ao_scheme_stderr;
193 #endif /* AO_SCHEME_FEATURE_PORT */