forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetcomm.c
More file actions
executable file
·313 lines (292 loc) · 5.48 KB
/
Copy pathgetcomm.c
File metadata and controls
executable file
·313 lines (292 loc) · 5.48 KB
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
/* Copyright (c) 1985 Ceriel J.H. Jacobs */
/*
* Command reader, also executes shell escapes
*/
# ifndef lint
static char rcsid[] = "$Header$";
# endif
# define _GETCOMM_
# include <ctype.h>
# include "in_all.h"
# include "term.h"
# include "process.h"
# include "getcomm.h"
# include "commands.h"
# include "prompt.h"
# include "main.h"
# include "output.h"
# include "getline.h"
# include "machine.h"
# include "keys.h"
# include "display.h"
# include "assert.h"
#if USG_OPEN
#include <fcntl.h>
#endif
#if POSIX_OPEN
#include <sys/types.h>
#include <fcntl.h>
#endif
char *strcpy(),
*getenv();
STATIC int killchar();
/*
* Read a line from the terminal, doing line editing.
* The parameter s contains the prompt for the line.
*/
char *
readline(s) char *s; {
static char buf[80];
register char *p = buf;
register int ch;
register int pos;
clrbline();
putline(s);
pos = strlen(s);
while ((ch = getch()) != '\n' && ch != '\r') {
if (ch == -1) {
/*
* Can only occur because of an interrupted read.
*/
ch = erasech;
interrupt = 0;
}
if (ch == erasech) {
/*
* Erase last char
*/
if (p == buf) {
/*
* There was none, so return
*/
return (char *) 0;
}
pos -= killchar(*--p);
if (*p != '\\') continue;
}
if (ch == killch) {
/*
* Erase the whole line
*/
if (!(p > buf && *(p-1) == '\\')) {
while (p > buf) {
pos -= killchar(*--p);
}
continue;
}
pos -= killchar(*--p);
}
if (p > &buf[78] || pos >= COLS - 2) {
/*
* Line does not fit.
* Simply refuse to make it any longer
*/
pos -= killchar(*--p);
}
*p++ = ch;
if (ch < ' ' || ch >= 0177) {
fputch('^');
pos++;
ch ^= 0100;
}
fputch(ch);
pos++;
}
fputch('\r');
*p++ = '\0';
flush();
return buf;
}
/*
* Erase a character from the command line.
*/
STATIC int
killchar(c) {
backspace();
putch(' ');
backspace();
if (c < ' ' || c >= 0177) {
(VOID) killchar(' ');
return 2;
}
return 1;
}
/*
* Do a shell escape, after expanding '%' and '!'.
*/
VOID
shellescape(p, esc_char) register char *p; {
register char *p2; /* walks through command */
register int id; /* procid of child */
register int cnt; /* prevent array bound errors */
register int lastc = 0; /* will contain the previous char */
# ifdef SIGTSTP
VOID (*savetstp)();
# endif
static char previous[256]; /* previous command */
char comm[256]; /* space for command */
int piped[2];
p2 = comm;
*p2++ = esc_char;
cnt = 253;
while (*p) {
/*
* expand command
*/
switch(*p++) {
case '!':
/*
* An unescaped ! expands to the previous
* command, but disappears if there is none
*/
if (lastc != '\\') {
if (*previous) {
id = strlen(previous);
if ((cnt -= id) <= 0) break;
(VOID) strcpy(p2,previous);
p2 += id;
}
}
else {
*(p2-1) = '!';
}
continue;
case '%':
/*
* An unescaped % will expand to the current
* filename, but disappears is there is none
*/
if (lastc != '\\') {
if (nopipe) {
id = strlen(currentfile);
if ((cnt -= id) <= 0) break;
(VOID) strcpy(p2,currentfile);
p2 += id;
}
}
else {
*(p2-1) = '%';
}
continue;
default:
lastc = *(p-1);
if (cnt-- <= 0) break;
*p2++ = lastc;
continue;
}
break;
}
clrbline();
*p2 = '\0';
if (!stupid) {
/*
* Display expanded command
*/
cputline(comm);
putline("\r\n");
}
flush();
(VOID) strcpy(previous,comm + 1);
resettty();
if (esc_char == '|' && pipe(piped) < 0) {
error("Cannot create pipe");
return;
}
if ((id = fork()) < 0) {
error("Cannot fork");
return;
}
if (id == 0) {
/*
* Close files, as child might need the file descriptors
*/
cls_files();
if (esc_char == '|') {
close(piped[1]);
#if USG_OPEN || POSIX_OPEN
close(0);
fcntl(piped[0], F_DUPFD, 0);
#else
dup2(piped[0], 0);
#endif
close(piped[0]);
}
execl("/bin/sh", "sh", "-c", comm + 1, (char *) 0);
exit(1);
}
(VOID) signal(SIGINT,SIG_IGN);
(VOID) signal(SIGQUIT,SIG_IGN);
# ifdef SIGTSTP
if ((savetstp = signal(SIGTSTP,SIG_IGN)) != SIG_IGN) {
(VOID) signal(SIGTSTP,SIG_DFL);
}
# endif
if (esc_char == '|') {
(VOID) close(piped[0]);
(VOID) signal(SIGPIPE, SIG_IGN);
wrt_fd(piped[1]);
(VOID) close(piped[1]);
}
while ((lastc = wait((int *) 0)) != id && lastc >= 0) {
/*
* Wait for child, making sure it is the one we expected ...
*/
}
(VOID) signal(SIGINT,catchdel);
(VOID) signal(SIGQUIT,quit);
# ifdef SIGTSTP
(VOID) signal(SIGTSTP, savetstp);
# endif
inittty();
}
/*
* Get all those commands ...
*/
int
getcomm (plong) long *plong; {
int c;
long count;
char *p;
int i;
int j;
char buf[10];
for (;;) {
count = 0;
give_prompt();
while (isdigit((c = getch()))) {
count = count * 10 + (c - '0');
}
*plong = count;
p = buf;
for (;;) {
if (c == -1) {
/*
* This should never happen, but it does,
* when the user gives a TSTP signal (^Z) or
* an interrupt while the program is trying
* to read a character from the terminal.
* In this case, the read is interrupted, so
* we end up here.
* Right, we will have to read again.
*/
if (interrupt) return 1;
break;
}
*p++ = c;
*p = 0;
if ((i = match(buf, &j, currmap->k_mach)) > 0) {
/*
* The key sequence matched. We have a command
*/
return j;
}
if (i == 0) return 0;
/*
* We have a prefix of a command.
*/
assert(i == FSM_ISPREFIX);
c = getch();
}
}
/* NOTREACHED */
}