-
Notifications
You must be signed in to change notification settings - Fork 0
/
playio.c
281 lines (223 loc) · 8.28 KB
/
playio.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
#define _GNU_SOURCE
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <time.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#define PIPE_READ 0
#define PIPE_WRITE 1
// semantic version parts
#define VERSION_MAJOR 1
#define VERSION_MINOR 1
#define VERSION_PATCH 4
int hascmdparam(const char *str);
char *getcmdparam(char *str);
char *clipnewline(char *str);
void runscript(int childStdinFD, int childStdoutFD);
// not in #define but variable, because of use in write()
const char newline = '\n';
int main(int argc, const char* argv[]) {
// no arguments given, so let's explain how we roll
if (argc < 2) {
printf("Usage:\n");
printf("%s <program> [program options]\n", argv[0]);
printf("%s -V\n", argv[0]);
return 1;
}
if (!strcmp(argv[1],"-V")) {
printf("PlayIO version %d.%d.%d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
return 0;
}
// create pipe for child stdin
int stdinPipe[2];
if (pipe(stdinPipe) < 0) {
perror("allocating pipe for child input redirect");
return 1;
}
// create pipe for child stdout
int stdoutPipe[2];
if (pipe(stdoutPipe) < 0) {
perror("allocating pipe for child output redirect");
// clean up stdin pipe we already have
close(stdinPipe[PIPE_READ]);
close(stdinPipe[PIPE_WRITE]);
return 1;
}
pid_t childPid = fork();
if (childPid == 0) {
// child continues here
// redirect stdin
if (dup2(stdinPipe[PIPE_READ], STDIN_FILENO) == -1) {
perror("redirecting child stdin");
return 1;
}
// redirect stdout...
if (dup2(stdoutPipe[PIPE_WRITE], STDOUT_FILENO) == -1) {
perror("redirecting child stdout");
return 1;
}
// ...and stderr to the same pipe
if (dup2(stdoutPipe[PIPE_WRITE], STDERR_FILENO) == -1) {
perror("redirecting child stderr");
return 1;
}
// all these are for use by parent only
close(stdinPipe[PIPE_READ]);
close(stdinPipe[PIPE_WRITE]);
close(stdoutPipe[PIPE_READ]);
close(stdoutPipe[PIPE_WRITE]);
// command line arguments denote program to run, and its arguments
char *childArgs[argc];
// copy arguments for child program, because execvp demands char instead of const char
for (int i = 1; i < argc; i++) {
int argLen = strlen(argv[i]);
childArgs[i - 1] = malloc(argLen + 1);
strncpy(childArgs[i - 1], argv[i], argLen);
childArgs[i - 1][argLen] = 0;
}
childArgs[argc - 1] = NULL;
// replace ourselves with the program we're supposed to run
execvp(argv[1], childArgs);
// we only reach this point if we couldn't load the child program
return 127;
}
else if (childPid > 0) {
// parent continues here
// ignore broken pipes; we're sticking to our script
signal(SIGPIPE, SIG_IGN);
// close unused file descriptors, these are for child only
close(stdinPipe[PIPE_READ]);
close(stdoutPipe[PIPE_WRITE]);
runscript(stdinPipe[PIPE_WRITE], stdoutPipe[PIPE_READ]);
// close remaining pipes now that we're done
close(stdinPipe[PIPE_WRITE]);
close(stdoutPipe[PIPE_READ]);
// exit with the most informative exit status we can give
int status;
return waitpid(childPid, &status, 0) != -1
? (WIFEXITED(status) ? WEXITSTATUS(status) : 1)
: 1;
}
else {
// failed to create child
perror("creating child process");
// clean up pipes
close(stdinPipe[PIPE_READ]);
close(stdinPipe[PIPE_WRITE]);
close(stdoutPipe[PIPE_READ]);
close(stdoutPipe[PIPE_WRITE]);
return 1;
}
return 0;
}
// read our I/O script from our own stdin, and execute it
void runscript(int childStdinFD, int childStdoutFD) {
char *stdinLine = NULL;
size_t stdinBufLen = 0;
ssize_t stdinReadLen = -1;
char *childLine = NULL;
size_t childBufLen = 0;
ssize_t childReadLen = -1;
FILE *childStdout = fdopen(childStdoutFD, "r");
// read lines from our own stdin (not the child's, yet)
while((stdinReadLen = getline(&stdinLine, &stdinBufLen, stdin)) != -1) {
// skip empty lines
if (stdinReadLen == 0)
continue;
// first char is our command
switch(stdinLine[0]) {
// output timestamp (μs since epoch)
case 't': {
struct timespec tms;
if (timespec_get(&tms, TIME_UTC)) {
// we got our time in seconds and nanoseconds, so we need to convert
int64_t micros = tms.tv_sec * 1000000;
micros += tms.tv_nsec / 1000;
// round off microseconds mathematically correct
if (tms.tv_nsec % 1000 >= 500)
++micros;
// output command parameter first, if there is one
const char *param = hascmdparam(stdinLine) ? getcmdparam(stdinLine) : "";
printf("%s%"PRId64"\n", param, micros);
}
}
break;
// read line(s) from child stdout, optionally until a specific string is found
case 'f': {
const char *param = hascmdparam(stdinLine) ? getcmdparam(stdinLine) : NULL;
while((childReadLen = getline(&childLine, &childBufLen, childStdout)) != -1) {
// try to find the search string in the child output, if we have one
if (param == NULL || strstr(childLine, param) != NULL)
break;
}
}
break;
// output string or last line read from child stdout
case 'o': {
if (hascmdparam(stdinLine)) {
printf("%s\n", getcmdparam(stdinLine));
break;
}
// bail out if no child output is available
if (childReadLen == -1)
break;
printf("%s\n", clipnewline(childLine));
}
break;
// pause for a specified number of seconds; fractional numbers are supported
case 'p': {
if (!hascmdparam(stdinLine))
break;
// we need to pause in seconds + nanoseconds
double duration = atof(getcmdparam(stdinLine));
struct timespec tms = {
.tv_sec = (time_t)duration,
.tv_nsec = (long)((duration - (long)duration) * 1000000000)
};
nanosleep(&tms, NULL);
}
break;
// write (optional) string to child stdin
case 'w': {
if (hascmdparam(stdinLine)) {
const char *param = getcmdparam(stdinLine);
write(childStdinFD, param, strlen(param));
}
write(childStdinFD, &newline, 1);
}
break;
// we don't know this command character so we ignore it
default:
break;
} // end of switch
} // end of while
// release buffers that were allocated by getline()
free(stdinLine);
free(childLine);
}
// check if a script command has a parameter: "c" = no, "c " = no, "c p..." = yes
// note that the second character is ignored whether it is a space or not!
inline int hascmdparam(const char *str) {
size_t strLen = strlen(str);
return strLen > 3 || (strLen == 3 && str[2] != newline);
}
// get pointer to script command parameter without trailing newline
// it doesn't check if there is a parameter to point to!
inline char *getcmdparam(char *str) {
return clipnewline(&str[2]);
}
// cut last newline off a string if there is one
// this modifies the string passed as a parameter!
inline char *clipnewline(char *str) {
size_t strLen = strlen(str);
if (strLen == 0)
return str;
if (str[strLen - 1] == newline)
str[strLen - 1] = 0;
return str;
}