-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexec.c
198 lines (163 loc) · 4.07 KB
/
exec.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include "argv.h"
#include "main.h"
#include "common.h"
static char* exec_command = 0;
void
exec_cleanup(void)
{
if (exec_command) {
free(exec_command);
exec_command = 0;
}
}
int
exec_cmd(int argc, char** argv)
{
uint8_t commandCode;
int commandLength = 0;
exec_command = 0;
if (argc == 2 && strcmp("cd", argv[0]) == 0) {
commandLength = strlen(argv[1]);
exec_command = malloc(commandLength+1);
strcpy(exec_command, argv[1]);
commandCode = SQUIRT_COMMAND_CD;
} else {
for (int i = 0; i < argc; i++) {
commandLength += strlen(argv[i]);
commandLength++;
}
exec_command = malloc(commandLength+1);
strcpy(exec_command, argv[0]);
for (int i = 1; i < argc; i++) {
strcat(exec_command, " ");
strcat(exec_command, argv[i]);
}
commandCode = SQUIRT_COMMAND_CLI;
}
if (util_sendCommand(main_socketFd, commandCode) != 0) {
fatalError("failed to connect to squirtd server");
}
if (util_sendLengthAndUtf8StringAsLatin1(main_socketFd, exec_command) != 0) {
fatalError("send() command failed");
}
if (commandCode != SQUIRT_COMMAND_CD) {
uint8_t c;
char buffer[1024];
int bindex = 0;
int exitState = 0;
while (util_recv(main_socketFd, &c, 1, 0)) {
if (c == 0) {
exitState++;
if (exitState == 4) {
break;
}
} else if (c == 0x9B) {
//write(1, buffer, bindex);
buffer[bindex] = 0;
char* utf8 = util_latin1ToUtf8(buffer);
size_t ignored __attribute__((unused)) = write(1, utf8, strlen(utf8));
free(utf8);
bindex = 0;
fprintf(stdout, "%c[", 27);
fflush(stdout);
} else {
buffer[bindex++] = c;
if (c == '\n' || bindex == sizeof(buffer)) {
//write(1, buffer, bindex);
buffer[bindex] = 0;
char* utf8 = util_latin1ToUtf8(buffer);
size_t ignored __attribute__((unused)) = write(1, utf8, strlen(utf8));
free(utf8);
bindex = 0;
}
}
}
if (bindex) {
size_t ignored __attribute__((unused)) = write(1, buffer, bindex);
}
}
uint32_t error;
if (util_recvU32(main_socketFd, &error) != 0) {
fatalError("exec: failed to read remote status");
}
exec_cleanup();
return error;
}
char*
exec_captureCmd(uint32_t* errorCode, int argc, char** argv)
{
uint8_t commandCode;
int commandLength = 0;
exec_command = 0;
int outputLength = 255;
int outputSize = 0;
char* output = malloc(outputLength);
for (int i = 0; i < argc; i++) {
commandLength += strlen(argv[i]);
commandLength++;
}
exec_command = malloc(commandLength+1);
strcpy(exec_command, argv[0]);
for (int i = 1; i < argc; i++) {
strcat(exec_command, " ");
strcat(exec_command, argv[i]);
}
commandCode = SQUIRT_COMMAND_CLI;
if (util_sendCommand(main_socketFd, commandCode) != 0) {
fatalError("failed to connect to squirtd server");
}
if (util_sendLengthAndUtf8StringAsLatin1(main_socketFd, exec_command) != 0) {
fatalError("send() command failed");
}
if (commandCode != SQUIRT_COMMAND_CD) {
uint8_t c;
int exitState = 0;
while (util_recv(main_socketFd, &c, 1, 0)) {
if (c == 0) {
exitState++;
if (exitState == 4) {
if (outputSize >= outputLength) {
outputLength = outputLength*2;
output = realloc(output, outputLength);
}
output[outputSize++] = 0;
break;
}
} else {
if (outputSize >= outputLength) {
outputLength = outputLength*2;
output = realloc(output, outputLength);
}
output[outputSize++] = c;
}
}
}
uint32_t error;
if (util_recvU32(main_socketFd, &error) != 0) {
fatalError("exec: failed to read remote status");
}
exec_cleanup();
*errorCode = error;
return output;
}
void
exec_main(int argc, char* argv[])
{
if (argc < 3) {
fatalError("incorrect number of arguments\nusage: %s hostname command to be executed", argv[0]);
}
util_connect(argv[1]);
for (int i = 0; i < argc-2; i++) {
argv[i] = argv[i+2];
}
argc-=2;
uint32_t error = exec_cmd(argc, argv);
if (error != 0) {
fatalError("%s", util_getErrorString(error));
}
}