1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/*
* repl-connection.c
*
* Copyright © 2019-2021 Thomas White <taw@bitwiz.me.uk>
*
* This file is part of Starlet.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <glib.h>
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
#include <libguile.h>
#include <libintl.h>
#define _(x) gettext(x)
#include "repl-connection.h"
struct _replconnection
{
GSocketConnection *conn;
char inbuf[1024];
char input[2048];
void (*process_func)(SCM sexp, void *data);
void *process_func_data;
int verbose;
};
static char *strip_crap(const char *line_orig)
{
size_t len;
char *line;
size_t outptr;
int i;
int escape_seq;
len = strlen(line_orig);
line = malloc(len+1);
if ( line == NULL ) return NULL;
/* Remove non-ASCII characters and ANSI escape sequences */
outptr = 0;
escape_seq = 0;
for ( i=0; i<len; i++ ) {
char ch = line_orig[i];
if ( (ch >= 0x1f ) && (ch < 0xff) && !escape_seq ) {
line[outptr++] = ch;
}
if ( ch == 0x1b ) escape_seq = 1;
if ( escape_seq && ch == 'm' ) escape_seq = 0;
}
line[outptr] = '\0';
return line;
}
static void process_line(const char *line_orig, ReplConnection *repl)
{
SCM port, str, sexp;
char *line = strip_crap(line_orig);
if ( repl->verbose ) {
printf("%p recv: '%s'\n", repl, line);
}
str = scm_from_utf8_string(line);
port = scm_open_input_string(str);
sexp = scm_read(port);
repl->process_func(sexp, repl->process_func_data);
scm_close_port(port);
free(line);
}
static void input_ready(GObject *source, GAsyncResult *res, gpointer vp)
{
GError *error = NULL;
ReplConnection *repl = vp;
size_t len;
int i;
char *nl_pos;
char *remaining;
len = g_input_stream_read_finish(G_INPUT_STREAM(source), res, &error);
if ( len == 0 ) {
printf("%p: EOF\n", repl);
g_object_unref(repl->conn);
repl->conn = NULL;
return;
}
if ( len == -1 ) {
printf("%p: Error: %s\n", repl, error->message);
g_object_unref(repl->conn);
repl->conn = NULL;
return;
}
repl->inbuf[len] = '\0';
for ( i=0; i<len; i++ ) {
if ( repl->inbuf[i] == '\0' ) {
repl->inbuf[i] = ' ';
fprintf(stderr, "Zero byte found in REPL output\n");
}
}
if ( strlen(repl->input) + len + 1 >= 2048 ) {
fprintf(stderr, "Too much input!\n");
} else {
strcat(repl->input, repl->inbuf);
}
nl_pos = strchr(repl->input, '\n');
while ( nl_pos != NULL ) {
size_t len_remaining = strlen(nl_pos);
nl_pos[0] = '\0';
process_line(repl->input, repl);
memmove(repl->input, nl_pos+1, len_remaining);
nl_pos = strchr(repl->input, '\n');
}
remaining = strip_crap(repl->input);
if ( strcmp(remaining, "scheme@(guile-user)> ") == 0 ) {
printf("Prompt!\n");
repl->input[0] = '\0';
}
free(remaining);
g_input_stream_read_async(g_io_stream_get_input_stream(G_IO_STREAM(repl->conn)),
repl->inbuf, 1023, G_PRIORITY_DEFAULT, NULL,
input_ready, repl);
}
ReplConnection *repl_connection_new(const char *socket,
void (*process_func)(SCM sexp, void *data),
void *data,
int verbose)
{
ReplConnection *repl;
GSocketClient *client;
GSocketAddress *addr;
GError *error = NULL;
repl = malloc(sizeof(struct _replconnection));
if ( repl == NULL ) return NULL;
addr = g_unix_socket_address_new(socket);
if ( addr == NULL ) return NULL;
client = g_socket_client_new();
repl->conn = g_socket_client_connect(client, G_SOCKET_CONNECTABLE(addr),
NULL, &error);
if ( repl->conn == NULL ) {
fprintf(stderr, "Couldn't connect: %s\n", error->message);
return NULL;
}
repl->input[0] = '\0';
scm_init_guile();
repl->process_func = process_func;
repl->process_func_data = data;
repl->verbose = verbose;
repl_send(repl, "(let loop ()"
" (let ((line (read)))"
" (unless (eq? line 'exit)"
" (let ((res (eval line (interaction-environment))))"
" (unless (unspecified? res)"
" (write res)"
" (newline)))"
" (loop))))");
g_input_stream_read_async(g_io_stream_get_input_stream(G_IO_STREAM(repl->conn)),
repl->inbuf, 1023, G_PRIORITY_DEFAULT, NULL,
input_ready, repl);
return repl;
}
int repl_send(ReplConnection *repl, const char *line)
{
GError *error = NULL;
GOutputStream *out;
if ( repl->conn == NULL ) {
fprintf(stderr, "No REPL to send to!\n");
return 1;
}
if ( repl->verbose ) {
printf("%p send: %s\n", repl, line);
}
out = g_io_stream_get_output_stream(G_IO_STREAM(repl->conn));
if ( g_output_stream_write(out, line, strlen(line), NULL, &error) == -1 ) {
fprintf(stderr, "Couldn't send: %s\n", error->message);
return 1;
}
if ( g_output_stream_write(out, "\n", 1, NULL, &error) == -1 ) {
fprintf(stderr, "Couldn't send newline: %s\n", error->message);
return 1;
}
return 0;
}
int repl_closed(ReplConnection *repl)
{
return repl->conn == NULL;
}
void repl_connection_close(ReplConnection *repl)
{
repl_send(repl, "exit");
repl_send(repl, ",q");
}
|