-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcom_shell.c
205 lines (176 loc) · 4.19 KB
/
com_shell.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
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include "db.h"
#include "xwin.h"
#include "token.h"
#include "rlgetc.h"
#include "ev.h"
#define UNUSED(x) (void)(x)
typedef void (*sighandler_t)(int);
/* Execute a command with /bin/sh after setting stdin,stdout,stderr = /dev/tty */
int pig_system(char *s) {
int status, pid, w, tty;
sighandler_t istat, qstat;
fflush(NULL); /* clear out any i/o from parent */
usleep(500);
tty = open("/dev/tty", O_RDWR);
if (tty == -1) {
fprintf(stderr, "error: can't open /det/tty\n");
return(-1);
}
if ((pid = fork()) == 0) { /* in child */
close(0); dup(tty);
close(1); dup(tty);
close(2); dup(tty);
close(tty);
EVupdate();
execlp("sh", "sh", "-c", s, (char *) 0);
exit(127);
}
close(tty); /* in parent */
istat = signal(SIGINT, SIG_IGN); /* save sighandlers */
qstat = signal(SIGQUIT, SIG_IGN);
while ((w = wait(&status)) != pid && w != -1) {
;
}
if (w == -1) {
status = -1;
}
signal(SIGINT, istat); /* restore sighandlers */
signal(SIGQUIT, qstat);
return(status);
}
int com_echo(LEXER *lp, char *arg) /* echo variables */
{
UNUSED(arg);
char *word;
TOKEN token;
int debug=0;
if (debug) printf(" com_echo\n");
while((token=token_get(lp, &word)) != EOF && token != EOL) {
printf("%s ",word);
}
printf("\n");
return (0);
}
int com_define(LEXER *lp, char *arg) /* set macro definition */
{
UNUSED(arg);
int debug = 0;
char *word;
char var[128];
char val[128];
char *str;
TOKEN token;
int i;
if (debug) printf(" com_def\n");
i=0;
val[0]='\0';
while((token=token_get(lp, &word)) != EOF && token != EOL && token != EOC) {
if (i==0) {
if (debug) printf("setting var %s\n", word);
strncpy(var,word, 128);
} else {
if (debug) printf("concatenating val %s\n", word);
strncat(val,word, 100);
}
i++;
}
if (i==0) {
EVprint(4);
} else if (i==1) {
if ((str=Macroget(var)) == NULL) {
str="";
}
printf("\"%s\"\n", str);
} else if (i>1) {
Macroset(var,val);
}
return (0);
}
int com_set(LEXER *lp, char *arg) /* set environment variables */
{
UNUSED(arg);
int debug = 0;
char *word;
char var[128];
char val[128];
char *str;
TOKEN token;
int mode=0;
int n=0;
int i;
if (debug) printf(" com_set\n");
while((token=token_get(lp, &word)) == OPT) {
switch (toupper((unsigned char)word[1])) {
case 'E':
mode |= 1; /* limit to exported vars */
break;
case 'P':
mode |= 2; /* limit to private vars */
break;
case 'M':
mode |= 4; /* limit to macro definitions */
break;
default:
printf("SET: bad option: %s\n", word);
return(1);
}
n++;
}
token_unget(lp, token, word);
i=0;
while((token=token_get(lp, &word)) == IDENT || token == QUOTE) {
if (i==0) strncpy(var,word, 128);
if (i==1) strncpy(val,word, 128);
i++;
}
if (debug) printf("got var=%s, val=%s\n", var, val);
if ((token != EOF && token != EOL && token != EOC) || i>2) {
printf("SET: bad number of arguments\n");
} else if (i==0) {
if (n==0) { /* default is to print all env variables */
mode = 3;
}
EVprint(mode);
} else if (i==1) {
if ((str=EVget(var)) == NULL) {
str="";
}
printf("%s\n", str);
} else if (i==2) {
EVset(var,val);
if (mode & 1) {
EVexport(var);
}
}
return (0);
}
int com_shell(LEXER *lp, char *arg) /* run a program from within the editor */
{
UNUSED(arg);
int debug = 0;
// char *shell;
char cmd[1024];
char *word;
TOKEN token;
if (debug) printf(" com_shell\n");
// if ((shell=EVget("SHELL")) == NULL) {
// shell="/bin/sh";
// }
cmd[0] = '\0';
while((token=token_get(lp, &word)) != EOF && token != EOL) {
strncat(cmd, word, 1000);
strncat(cmd, " ", 1000);
}
// if (token == EOL) { token_unget(lp, token, word); }
pig_system(cmd);
return (0);
}