-
Notifications
You must be signed in to change notification settings - Fork 0
/
elastic_fd.c
278 lines (251 loc) · 6.3 KB
/
elastic_fd.c
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*-
* Copyright (c) 2005-2020 Poul-Henning Kamp
* All rights reserved.
*
* Author: Poul-Henning Kamp <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <termios.h>
#include "rc3600.h"
#include "elastic.h"
struct elastic_fd {
struct elastic *ep;
int fd;
int has_thread;
int selfdestruct;
pthread_t rxt;
struct elastic_subscriber *ws;
};
static void *
elastic_fd_rxthread(void *priv)
{
struct elastic_fd *efp = priv;
char buf[1];
ssize_t sz;
while (1) {
sz = read(efp->fd, buf, 1);
if (sz != 1)
break;
elastic_inject(efp->ep, buf, 1);
}
if (efp->selfdestruct) {
if (efp->ws != NULL)
elastic_unsubscribe(efp->ep, efp->ws);
AZ(close(efp->fd));
free(efp);
}
return (NULL);
}
static void
elastic_fd_txfunc(void *priv, const void *src, size_t len)
{
struct elastic_fd *efp = priv;
const char *p = src;
char buf[1];
if (!efp->ep->text) {
(void)write(efp->fd, src, len);
return;
}
while (len--) {
buf[0] = *p++ & 0x7f;
switch (buf[0]) {
case 0x00:
case 0x11:
case 0x13:
break;
default:
(void)write(efp->fd, buf, 1);
break;
}
}
}
struct elastic_fd *
elastic_fd_start(struct elastic *ep, int fd, int mode, int selfdestruct)
{
struct elastic_fd *efp;
AN(ep);
assert(fd > STDERR_FILENO);
if (mode == -1)
mode = ep->mode;
else
assert(ep->mode == O_RDWR || ep->mode == mode);
efp = calloc(1, sizeof *efp);
AN(efp);
efp->fd = fd;
efp->ep = ep;
efp->selfdestruct = selfdestruct;
if (mode != O_RDONLY)
efp->ws = elastic_subscribe(ep, elastic_fd_txfunc, efp);
if (mode != O_WRONLY) {
efp->has_thread = 1;
AZ(pthread_create(&efp->rxt, NULL, elastic_fd_rxthread, efp));
if (selfdestruct)
AZ(pthread_detach(efp->rxt));
}
return(efp);
}
void
elastic_fd_stop(struct elastic_fd **efpp)
{
struct elastic_fd *efp;
void *p;
AN(efpp);
efp = *efpp;
*efpp = NULL;
AZ(efp->selfdestruct);
AZ(close(efp->fd));
if (efp->ws != NULL)
elastic_unsubscribe(efp->ep, efp->ws);
if (efp->has_thread)
AZ(pthread_join(efp->rxt, &p));
free(efp);
}
static void
elastic_serial(struct elastic *ep, struct cli *cli)
{
int fd, i;
struct termios tt;
if (cli->ac < 2) {
(void)cli_n_args(cli, 1);
return;
}
fd = open(cli->av[1], O_RDWR | O_NONBLOCK);
if (fd < 0 && errno == EPERM) {
usleep(100000);
fd = open(cli->av[1], O_RDWR | O_NONBLOCK);
}
if (fd < 0) {
cli_error(cli, "Cannot open %s: %s\n",
cli->av[1], strerror(errno));
return;
}
i = fcntl(fd, F_GETFL);
if (i == -1) {
cli_error(cli, "Cannot fcntl(F_GETFL) %s: %s\n",
cli->av[1], strerror(errno));
(void)close(fd);
return;
}
i = fcntl(fd, F_SETFL, i & ~O_NONBLOCK);
if (i == -1) {
cli_error(cli, "Cannot fcntl(F_SETFL) %s: %s\n",
cli->av[1], strerror(errno));
(void)close(fd);
return;
}
if (tcgetattr(fd, &tt)) {
cli_error(cli, "Not a tty %s: %s\n",
cli->av[1], strerror(errno));
AZ(close(fd));
return;
}
cfmakeraw(&tt);
tt.c_cflag |= CLOCAL;
tt.c_cc[VMIN] = 1;
tt.c_cc[VTIME] = 0;
cli->ac -= 2;
cli->av += 2;
while (cli->ac > 0) {
if (!strcmp(*cli->av, "110")) {
cli->ac --;
cli->av ++;
tt.c_cflag &= ~CSIZE;
tt.c_cflag |= CS7;
tt.c_cflag |= CSTOPB;
tt.c_cflag |= PARENB;
tt.c_cflag &= ~PARODD;
cfsetspeed(&tt, B110);
continue;
}
cli_unknown(cli);
break;
}
assert(tcsetattr(fd, TCSAFLUSH, &tt) == 0);
(void)elastic_fd_start(ep, fd, -1, 1);
}
int v_matchproto_(cli_elastic_f)
cli_elastic_fd(struct elastic *ep, struct cli *cli)
{
int fd;
AN(cli);
if (cli->help) {
cli_printf(cli, "\t< <filename>\n");
cli_printf(cli, "\t\tRead input from file\n");
cli_printf(cli, "\t> <filename>\n");
cli_printf(cli, "\t\tWrite output to file\n");
cli_printf(cli, "\t>> <filename>\n");
cli_printf(cli, "\t\tAppend output to file\n");
cli_printf(cli, "\tserial <tty-device>\n");
cli_printf(cli, "\t\tConnect to (UNIX) tty-device\n");
return(0);
}
AN(ep);
if (!strcmp(*cli->av, ">") || !strcmp(*cli->av, ">>")) {
if (cli_n_args(cli, 1))
return(1);
if (ep->mode == O_RDONLY)
return (cli_error(cli,
"Only outputs can '%s'\n", *cli->av));
if (!strcmp(*cli->av, ">"))
fd = open(cli->av[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);
else
fd = open(cli->av[1], O_WRONLY|O_CREAT|O_APPEND, 0644);
if (fd < 0)
return (cli_error(cli, "Cannot open %s: %s\n",
cli->av[1], strerror(errno)));
if (ep->out != NULL)
elastic_fd_stop(&ep->out);
ep->out = elastic_fd_start(ep, fd, O_WRONLY, 0);
cli->av += 2;
cli->ac -= 2;
return (1);
}
if (!strcmp(*cli->av, "<")) {
if (cli_n_args(cli, 1))
return(1);
if (ep->mode == O_WRONLY)
return (cli_error(cli,
"Only inputs can '%s'\n", *cli->av));
fd = open(cli->av[1], O_RDONLY);
if (fd < 0)
return (cli_error(cli, "Cannot open %s: %s\n",
cli->av[1], strerror(errno)));
(void)elastic_fd_start(ep, fd, O_RDONLY, 1);
cli->av += 2;
cli->ac -= 2;
return (1);
}
if (!strcmp(*cli->av, "serial")) {
elastic_serial(ep, cli);
return (1);
}
return (0);
}