-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspawn.c
414 lines (322 loc) · 7.5 KB
/
spawn.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
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Spawn.
*/
#include "def.h"
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
/*
* Mg3a: And finally, an attempt at suspending and resuming. ttinit(),
* called from vtinit(), has been adapted for the restart.
*/
INT
spawncli(INT f, INT n)
{
sigset_t oldset;
vttidy();
sigprocmask(SIG_SETMASK, NULL, &oldset);
kill(0, SIGTSTP);
sigprocmask(SIG_SETMASK, &oldset, NULL);
vtinit();
return TRUE;
}
/*
* Mg3a: paraphernalia to allow interrupting subprocess
*/
static volatile sig_atomic_t subprocess_alive = 0;
static pid_t subprocess_pid;
static void
sigint(int dummy)
{
if (subprocess_alive) {
if (subprocess_alive == 1) {
kill(subprocess_pid, SIGINT);
subprocess_alive = 2;
} else {
kill(subprocess_pid, SIGKILL);
subprocess_alive = 0;
}
}
}
/*
* Mg3a: Do the setup so that we can interrupt subprocess. Return
* success status when turning on interrupts.
*/
static int
set_subprocess_alive(int on)
{
struct sigaction sa;
if (on) {
subprocess_alive = 1;
sa.sa_handler = sigint;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART; // Don't break pipe IO
if (sigaction(SIGINT, &sa, NULL) != 0 || !setctrlg_interrupt(1)) return 0;
} else {
setctrlg_interrupt(0);
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGINT, &sa, NULL);
subprocess_alive = 0;
}
return 1;
}
/*
* Mg3a: shell with override
*/
static char shellfile[NFILEN] = "";
static char *
getshell()
{
char *cp;
if (shellfile[0]) return shellfile;
else if ((cp = getenv("SHELL"))) return cp;
else return "/bin/sh";
}
INT
setshell(INT f, INT n)
{
char input[NFILEN];
if (eread("Set shell: ", input, sizeof(input), EFFILE) == ABORT) return ABORT;
stringcopy(shellfile, input, sizeof(shellfile));
if (input[0]) {
ewprintf("Shell set to %s", input);
} else {
ewprintf("Shell reverted to %s", getshell());
}
return TRUE;
}
/*
* Mg3a: Do this to replace popen() to redirect stderr.
*/
FILE *
pipe_open(char *cmd)
{
int fds[2], infd;
FILE *f;
struct sigaction sa;
char *shell = getshell();
if (pipe(fds) < 0) {
ewprintf("Error opening pipe: %s", strerror(errno));
return NULL;
}
// Ignore children
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGCHLD, &sa, NULL);
switch ((subprocess_pid = fork())) {
case -1:
ewprintf("Fork error: %s", strerror(errno));
return NULL;
case 0:
if ( dup2(fds[1], 2) < 0 || dup2(fds[1], 1) < 0 ||
close(fds[0]) < 0 || (infd = open("/dev/null", O_RDONLY)) < 0 ||
dup2(infd, 0) < 0 || chdir(dirnameofbuffer(curbp)) < 0)
{
panic("Error in subprocess", errno);
}
if (execl(shell, shell, "-c", cmd, NULL) < 0) {
printf("(Couldn't execute shell %s: %s)\n", shell, strerror(errno));
exit(1);
}
return NULL;
default:
if ((f = fdopen(fds[0], "r")) == NULL || close(fds[1]) < 0) {
ewprintf("Error modifying pipe: %s", strerror(errno));
if (f) fclose(f); else close(fds[1]);
return NULL;
}
return f;
}
}
/*
* Mg3a: Inspired by POSIX getline()
*
- Must be called with *lineptr == NULL first time.
- errno is not explicitly set.
- If maxlen > SSIZE_MAX, max SSIZE_MAX bytes are returned anyway.
- Out of memory is reported by a return value.
- Can only be used on one stream at a time.
- feof() is not reliable. You have to check the return value and
see that ferror() isn't set.
*/
#define GETLINE_EOF (-1)
#define GETLINE_OUTOFMEM (-2)
ssize_t
mg_getline(char **lineptr, size_t *n, size_t maxlen, FILE *stream)
{
char *line = NULL, *line2, *nl;
size_t len = 0, size = 0, copylen, newsize;
static char buf[BUFSIZ];
static size_t bufpos, buflen;
int returnresult = 0;
if (!*lineptr) {
bufpos = 0;
buflen = 0;
size = 2*BUFSIZ;
line = malloc(size);
if (!line) goto outofmemory;
} else {
line = *lineptr;
size = *n;
}
while (1) {
if (bufpos == buflen) {
buflen = fread(buf, 1, sizeof(buf), stream);
bufpos = 0;
if (buflen == 0) break; // EOF or error
}
copylen = buflen - bufpos;
if (len + copylen > maxlen) {
copylen = maxlen - len;
returnresult = 1;
}
nl = memchr(buf + bufpos, '\n', copylen);
if (nl) {
copylen = nl - buf - bufpos + 1;
returnresult = 1;
}
if (len + copylen >= size) {
if (len + copylen >= SSIZE_MAX) {
goto outofmemory;
} else if (size <= BUFSIZ) {
newsize = 2*BUFSIZ;
} else if (size > SSIZE_MAX - size) {
newsize = SSIZE_MAX;
} else {
newsize = size + size;
}
line2 = realloc(line, newsize);
if (!line2) goto outofmemory;
line = line2;
size = newsize;
}
memcpy(line + len, buf + bufpos, copylen);
len += copylen;
bufpos += copylen;
if (returnresult) break;
}
line[len] = 0;
*lineptr = line;
*n = size;
if (len == 0) return GETLINE_EOF;
return len;
outofmemory:
free(line); // Note that line is the current one, not *lineptr
*lineptr = NULL;
*n = 0;
return GETLINE_OUTOFMEM;
}
/* Limit on shell command data */
INT shell_command_limit = 10000000;
/*
* Mg3a: As Emacs "shell-command" without asynch
*/
INT
shellcommand(INT f, INT n)
{
char cmd[EXCLEN], *line = NULL;
INT s, savedoffset = 0, len, ret = FALSE, yankp, inthisbuffer;
FILE *pipef;
int first = 1, nl = 0;
BUFFER *bp = NULL;
size_t pos = 0, linesize = 0, range;
LINE *prevline = NULL, *lp;
const INT overhead = sizeof(LINE) + sizeof(void*);
INT budget;
if ((s = ereply("Shell command: ", cmd, sizeof(cmd))) != TRUE) return s;
ttmove(nrow - 1, 0); ttflush(); /* Like Emacs */
if ((pipef = pipe_open(cmd)) == NULL) return FALSE;
yankp = (f & FFRAND);
inthisbuffer = (f & FFARG) || yankp;
if (inthisbuffer) {
u_modify();
if (lnewline_noundo() == FALSE) {
fclose(pipef);
return FALSE;
}
endofpreviousline();
isetmark();
prevline = lback(curwp->w_dotp);
savedoffset = curwp->w_doto;
pos = curwp->w_dotpos;
} else {
if ((bp = bfind("*Shell Command Output*", TRUE)) == NULL) {
fclose(pipef);
return FALSE;
}
resetbuffer(bp);
u_clear(bp);
bp->b_flag &= ~BFCHG;
bclear(bp);
}
if (!set_subprocess_alive(1)) {
ewprintf("Failed to set signal handler: %s", strerror(errno));
goto error;
}
budget = shell_command_limit;
while (1) {
if (budget <= overhead) {
ewprintf("Data overflowing \"shell-command-limit\"");
goto error;
}
len = mg_getline(&line, &linesize, budget - overhead, pipef);
if (len == GETLINE_OUTOFMEM) {
ewprintf("Out of memory");
goto error;
} else if (len < 0) {
break;
}
budget -= len + overhead;
nl = 0;
if (len && line[len - 1] == '\n') {
nl = 1;
len--;
if (len && line[len - 1] == '\r') len--;
}
if (inthisbuffer) {
if (first) {
if (linsert_str_noundo(line, len) == FALSE) goto error;
first = 0;
} else {
if (laddline(line, len) == FALSE) goto error;
}
} else {
if ((lp = lalloc(len)) == NULL) goto error;
memcpy(ltext(lp), line, len);
addlast(bp, lp);
}
}
ret = TRUE;
error:
free(line);
set_subprocess_alive(0);
fclose(pipef);
if (inthisbuffer) {
if (nl && !yankp) adjustpos(lforw(curwp->w_dotp), 0);
else ldelnewline_noundo();
if ((range = curwp->w_dotpos - pos) > MAXINT) {
u_clear(curbp);
} else if (!u_entry_range(UFL_INSERT, lforw(prevline), savedoffset, range, pos)) {
ret = FALSE;
}
if (!yankp) {
isetmark();
position(pos);
}
} else {
// setcharsetfromcontent(bp); // ?
if (popbuftop(bp) == FALSE) return FALSE;
}
return ret;
}
/*
* Mg3a: A variant of "shell-command". Not in Emacs.
*/
INT
yank_process(INT f, INT n)
{
return shellcommand(f | FFRAND, n);
}