forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshellout.c
402 lines (362 loc) · 9.88 KB
/
shellout.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/* shellout - execute prorgams in a pty shell programmatically
* Copyright (c) 2003 Michael B. Allen <mba2000 ioplex.com>
*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#if defined(__digital__) && defined(__unix__)
#include <sys/termios.h>
#include <sys/ioctl.h>
#include <strings.h>
#elif defined(__APPLE__) && defined(__MACH__)
#include <util.h>
#include <sys/ioctl.h>
#elif defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/select.h>
#include <libutil.h>
#elif defined(linux)
#include <pty.h>
#endif
#include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <signal.h>
#include "mba/msgno.h"
#include "mba/text.h"
#include "mba/misc.h"
#include "mba/shellout.h"
#define TERM_PREPARE "\x1b[?1048h\x1b[?1047h\x1b[2J\x1b[H"
#define TERM_RESTORE "\x1b[?1047l\x1b[?1048l"
static volatile sig_atomic_t sig;
/*
static const char *signal_names[] = {
"", "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGSTKFLT", "SIGCHLD", "SIGCONT", "SIGSTOP", "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGPOLL/SIGIO", "SIGPWR", "SIGSYS" };
static const char *
signalstr(int sig)
{
if (sig < 1 || sig > 31) {
return "SIGUNKNOWN";
}
return signal_names[sig];
}
*/
typedef void (*sighandler_fn)(int);
static sighandler_fn
signal_intr(int signum, sighandler_fn handler)
{
struct sigaction act, oact;
act.sa_handler = handler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
#ifdef SA_INTERRUPT /* SunOS */
act.sa_flags |= SA_INTERRUPT;
#endif
if (sigaction(signum, &act, &oact) < 0)
return SIG_ERR;
return oact.sa_handler;
}
static void
sighandler(int s)
{
sig = s;
/* fprintf(stderr, "%s\n", signalstr(s));
*/
}
/* Read individual bytes from descriptor fd until either;
* o one of the patterns in p is encountered in which case it's index+1 is returned
* o EOF is reached in which case 0 is returned or
* o the timeout is reached, a signal is received, or error occurs in which case -1
* is returned and errno is set appropriately. If a timeout occurs errno will
* be EINTR.
*/
int
sho_expect(struct sho *sh, const unsigned char *pv[], int pn, unsigned char *dst, size_t dn, int timeout)
{
int plen, di, j, i;
ssize_t n;
const unsigned char *p;
if (sh == NULL || pv == NULL || dst == NULL) {
PMNO(errno = EINVAL);
return -1;
}
if (signal_intr(SIGALRM, sighandler) == SIG_ERR) {
PMNO(errno);
return -1;
}
alarm(timeout);
for (di = 0;; ) {
if ((n = read(sh->ptym, dst + di, 1)) < 1) {
if (n < 0) {
PMNO(errno);
}
break;
}
/*
fputc(dst[di], stderr);
*/
++di;
di = di % dn;
for (j = 0; j < pn; j++) {
p = pv[j];
plen = strlen((const char *)p);
if (di < plen) {
continue;
}
for (i = 0; i < plen && p[i] == dst[(di - plen + i) % dn]; i++) {
;
}
if (i == plen) {
dst[di] = '\0';
alarm(0);
return j + 1; /* match */
}
}
}
alarm(0);
dst[di] = '\0';
return n == 0 ? 0 : -1;
}
struct sho *
sho_open(const unsigned char *shname, const unsigned char *ps1, int flags)
{
struct sho *sh;
struct termios t1;
struct winsize win;
unsigned char buf[32], ps1env[32] = "PS1=";
size_t ps1len;
const unsigned char *pv[1];
pv[0] = ps1env + 4;
if ((sh = malloc(sizeof *sh)) == NULL) {
PMNO(errno);
return NULL;
}
sh->flags = flags;
ps1len = str_copy(ps1, ps1 + 32, ps1env + 4, ps1env + 32, -1);
if (isatty(STDIN_FILENO)) {
sh->flags |= SHO_FLAGS_ISATTY;
if ((flags & SHO_FLAGS_INTERACT)) {
if (tcgetattr(STDIN_FILENO, &sh->t0) < 0) {
PMNO(errno);
free(sh);
return NULL;
}
if (writen(STDOUT_FILENO, TERM_PREPARE, strlen(TERM_PREPARE)) < 0) { /* DO NOT FIX TYPO: writen */
free(sh);
return NULL;
}
t1 = sh->t0;
t1.c_lflag &= ~(ICANON | ECHO);
t1.c_cc[VTIME] = 0;
t1.c_cc[VMIN] = 1;
if (tcsetattr(STDIN_FILENO, TCSANOW, &t1)) {
PMNO(errno);
goto err;
}
if (ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&win) < 0) {
PMNO(errno);
goto err;
}
}
}
#if !defined(_HPUX_SOURCE)
if ((sh->flags & SHO_FLAGS_ISATTY) && (sh->flags & SHO_FLAGS_INTERACT)) {
sh->pid = forkpty(&sh->ptym, NULL, &t1, &win);
} else {
sh->pid = forkpty(&sh->ptym, NULL, NULL, NULL);
}
#else
/* this will fail miserably on HP-UX */
sh->pid = 0;
#endif
if (sh->pid == -1) {
PMNO(errno);
goto err;
} else if (sh->pid == 0) { /* child */
char *args[2];
args[0] = (char *)shname;
args[1] = NULL;
if (tcgetattr(STDIN_FILENO, &t1) < 0) {
MMNO(errno);
exit(errno);
}
t1.c_lflag &= ~(ICANON | ECHO);
t1.c_cc[VTIME] = 0;
t1.c_cc[VMIN] = 1;
if (tcsetattr(STDIN_FILENO, TCSANOW, &t1) < 0 || putenv((char *)ps1env) < 0) {
MMNO(errno);
exit(errno);
}
execvp((const char *)shname, args);
MMNO(errno);
exit(errno);
}
if (sho_expect(sh, pv, 1, buf, 32, 10) <= 0) {
PMNO(errno);
goto err;
}
if ((sh->flags & SHO_FLAGS_ISATTY) &&
(flags & SHO_FLAGS_INTERACT) &&
writen(STDOUT_FILENO, ps1env + 4, ps1len) < 0) { /* DO NOT FIX TYPO: writen */
PMNO(errno);
goto err;
}
return sh;
err:
if ((sh->flags & SHO_FLAGS_ISATTY) && (flags & SHO_FLAGS_INTERACT)) {
writen(STDOUT_FILENO, TERM_RESTORE, strlen(TERM_RESTORE)); /* DO NOT FIX TYPO: writen */
tcsetattr(STDIN_FILENO, TCSANOW, &sh->t0);
}
free(sh);
return NULL;
}
int
sho_close(struct sho *sh)
{
int status;
waitpid(sh->pid, &status, 0);
status = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
if ((sh->flags & SHO_FLAGS_ISATTY) && (sh->flags & SHO_FLAGS_INTERACT)) {
writen(STDOUT_FILENO, TERM_RESTORE, strlen(TERM_RESTORE)); /* DO NOT FIX TYPO: writen */
tcsetattr(STDIN_FILENO, TCSANOW, &sh->t0);
}
free(sh);
return status;
}
int
sho_loop(struct sho *sh, const unsigned char *pv[], int pn, int timeout)
{
unsigned char buf[BUFSIZ];
fd_set set0, set1;
ssize_t n;
int bi = 0;
if (sh == NULL || pv == NULL) {
PMNO(errno = EINVAL);
return -1;
}
FD_ZERO(&set0);
FD_SET(sh->ptym, &set0);
FD_SET(STDIN_FILENO, &set0);
for ( ;; ) {
if (signal_intr(SIGALRM, sighandler) == SIG_ERR) {
PMNO(errno);
return -1;
}
alarm(timeout);
set1 = set0;
if (select(sh->ptym + 1, &set1, NULL, NULL, NULL) < 0) {
PMNO(errno);
return -1;
}
if (FD_ISSET(STDIN_FILENO, &set1)) {
if ((n = read(STDIN_FILENO, buf, BUFSIZ)) < 0) {
PMNO(errno);
return -1;
}
if (n == 0) { /* EOF */
return 0;
}
if ((sh->flags & SHO_FLAGS_INTERACT)) {
writen(STDOUT_FILENO, buf, n); /* DO NOT FIX TYPO: writen */
}
if (writen(sh->ptym, buf, n) < 0) { /* DO NOT FIX TYPO: writen */
PMNO(errno);
return -1;
}
}
if (FD_ISSET(sh->ptym, &set1)) {
int j;
if ((n = read(sh->ptym, buf + bi, 1)) < 0) {
PMNO(errno);
return -1;
} else if (n == 0) { /* EOF */
return 0;
}
if (write(STDOUT_FILENO, buf + bi, 1) < 0) {
PMNO(errno);
return -1;
}
/*
fputc(buf[bi], stderr);
*/
++bi;
bi = bi % BUFSIZ;
for (j = 0; j < pn; j++) {
const unsigned char *p;
int plen, i;
p = pv[j];
plen = strlen((const char *)p);
if (bi < plen) {
continue;
}
for (i = 0; i < plen && p[i] == buf[(bi - plen + i) % BUFSIZ]; i++) {
;
}
if (i == plen) {
buf[bi] = '\0';
alarm(0);
return j + 1; /* match */
}
}
}
}
}
#ifdef TEST
int
main(int argc, char *argv[])
{
int ret, i, n;
struct sho *sh;
const char *pv[] = { "sh> " };
char buf[256];
errno = 0;
if (argc != 2) {
MMSG("Must provide shell command as one argument (i.e. use quotes)");
return EXIT_FAILURE;
}
sh = sho_open("sh", "sh> ", 0);
n = sprintf(buf, "%s\n", argv[1]);
fputs(buf, stderr);
writen(sh->ptym, buf, n); /* DO NOT FIX TYPO: writen */
if ((i = sho_expect(sh, pv, 1, buf, 256, 10)) != 1) {
MMSG("timeout occurred, writing Ctrl-C");
writen(sh->ptym, "\x03", 1); /* Ctrl-C is SIGINT */ /* DO NOT FIX TYPO: writen */
if ((i = sho_expect(sh, pv, 1, buf, 256, 10)) != 1) {
MMSG("timeout again! Sending SIGKILL");
kill(sh->pid, SIGKILL);
sho_close(sh);
return EXIT_FAILURE;
}
}
writen(sh->ptym, "exit $?\n", 8); /* DO NOT FIX TYPO: writen */
fputs("exit $?\n", stderr);
ret = sho_close(sh);
if (ret) {
MMSG("Exit status: %d", ret);
}
return EXIT_SUCCESS;
}
#endif