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
52
57
58
59
65
66
67
68
69
70
71
79
80
81
82
83
89
90
91
92
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
117
127
128
132
133
134
135
136
137
138
141
142
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
179
180
184
185
186
187
191
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
226
232
233
234
235
236
237
242
247
248
249
250
251
252
257
262
263
264
265
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
296
297
301
302
303
308
309
310
316
317
324
325
326
327
328
329
338
339
340
341
342
343
344
345
346
347
348
349
354
355
356
357
358
359
368
369
370
371
374
375
376
377
378
379
380
381
382
383
384
385
389
390
391
392
393
394
395
398
399
400
403
404
405
406
407
408
409
412
413
414
415
418
419
420
421
422
423
424
430
431
432
433
434
435
438
439
445
458
459
463
464
465
466
467
468
469
470
471
472
473
474
481
492
493
498
513
514
517
518
519
520
521
522
523
524
530
531
532
533
534
535
536
537
538
539
542
543
544
545
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
601
602
605
609
612
613
614
615
616
617
621
622
623
624
625
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
670
672
674
677
678
679
680
681
682
683
684
688
689
694
699
701
703
704
707
708
711
712
715
716
719
720
722
723
724
725
726
727
728
729
730
731
732
733
734
735
738
741
744
745
746
753
754
755
756
757
758
759
760
761
766
770
771
774
775
776
777
778
779
780
781
785
790
794
797
801
802
803
804
805
806
810
814
817
818
831
832
836
837
838
844
845
846
847
848
852
888
897
898
902
921
933
934
965
966
967
968
969
970
971
972
973
974
975
978
979
980
981
985
986
987
988
989
990
991
1000
1001
1005
1006
1007
1008
1012
1019
1020
1021
1024
1025
1026
1027
1028
1034
1035
1036
1037
1041
1042
1046
1047
1048
1049
1053
1059
1063
1064
1067
1070
1073
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1090
1091
1092
1093
1094
1095
1096
1097
1101
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1119
1120
1123
1126
1127
1128
1129
1130
1131
1134
1135
1136
1139
1142
1145
1146
1147
1148
1149
1150
1151
1156
1157
1158
1162
1166
1170
1173
1176
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1258
1261
1262
1263
1264
1265
1266
1267
1271
1272
1276
1277
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1294
1295
1298
1307
1310
1311
1312
1313
1314
1315
1316
1317
1332
1333
1334
1335
1345
1348
1349
1350
1351
1354
1355
1356
1357
1358
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1375
1376
1377
1378
1379
1380
1385
1386
1387
1388
1389
1390
1391
1392
1393
1398
1401
1404
1405
1406
1409
1410
1413
1416
1417
1418
1421
1422
1423
1426
1427
1428
1431
1432
1436
1440
1443
1446
1447
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1471
1474
1477
1478
1479
1480
1481
1482
1486
1487
1493
1494
1499
1500
/* ... */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <string.h>
#include <ctype.h>
#include "jimautoconf.h"
#include <jim.h>
#if (!defined(HAVE_VFORK) || !defined(HAVE_WAITPID)) && !defined(__MINGW32__)
/* ... */
static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
Jim_Obj *cmdlineObj = Jim_NewEmptyStringObj(interp);
int i, j;
int rc;
for (i = 1; i < argc; i++) {
int len;
const char *arg = Jim_GetString(argv[i], &len);
if (i > 1) {
Jim_AppendString(interp, cmdlineObj, " ", 1);
}if (i > 1) { ... }
if (strpbrk(arg, "\\\" ") == NULL) {
Jim_AppendString(interp, cmdlineObj, arg, len);
continue;
}if (strpbrk(arg, "\\\" ") == NULL) { ... }
Jim_AppendString(interp, cmdlineObj, "\"", 1);
for (j = 0; j < len; j++) {
if (arg[j] == '\\' || arg[j] == '"') {
Jim_AppendString(interp, cmdlineObj, "\\", 1);
}if (arg[j] == '\\' || arg[j] == '"') { ... }
Jim_AppendString(interp, cmdlineObj, &arg[j], 1);
}for (j = 0; j < len; j++) { ... }
Jim_AppendString(interp, cmdlineObj, "\"", 1);
}for (i = 1; i < argc; i++) { ... }
rc = system(Jim_String(cmdlineObj));
Jim_FreeNewObj(interp, cmdlineObj);
if (rc) {
Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0);
Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1));
Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, 0));
Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, rc));
Jim_SetGlobalVariableStr(interp, "errorCode", errorCode);
return JIM_ERR;
}if (rc) { ... }
return JIM_OK;
}Jim_ExecCmd (Jim_Interp *interp, int argc, Jim_Obj *const *argv) { ... }
int Jim_execInit(Jim_Interp *interp)
{
Jim_PackageProvideCheck(interp, "exec");
Jim_CreateCommand(interp, "exec", Jim_ExecCmd, NULL, NULL);
return JIM_OK;
}Jim_execInit (Jim_Interp *interp) { ... }
/* ... */#else
#include <errno.h>
#include <signal.h>
#include "jim-signal.h"
#include "jimiocompat.h"
#include <sys/stat.h>
5 includes
struct WaitInfoTable;
static char **JimOriginalEnviron(void);
static char **JimSaveEnv(char **env);
static void JimRestoreEnv(char **env);
static int JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv,
pidtype **pidArrayPtr, int *inPipePtr, int *outPipePtr, int *errFilePtr);
static void JimDetachPids(struct WaitInfoTable *table, int numPids, const pidtype *pidPtr);
static int JimCleanupChildren(Jim_Interp *interp, int numPids, pidtype *pidPtr, Jim_Obj *errStrObj);
static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
#if defined(__MINGW32__)
static pidtype JimStartWinProcess(Jim_Interp *interp, char **argv, char **env, int inputId, int outputId, int errorId);
#endif
/* ... */
static void Jim_RemoveTrailingNewline(Jim_Obj *objPtr)
{
int len;
const char *s = Jim_GetString(objPtr, &len);
if (len > 0 && s[len - 1] == '\n') {
objPtr->length--;
objPtr->bytes[objPtr->length] = '\0';
}if (len > 0 && s[len - 1] == '\n') { ... }
}{ ... }
/* ... */
static int JimAppendStreamToString(Jim_Interp *interp, int fd, Jim_Obj *strObj)
{
char buf[256];
FILE *fh = fdopen(fd, "r");
int ret = 0;
if (fh == NULL) {
return -1;
}if (fh == NULL) { ... }
while (1) {
int retval = fread(buf, 1, sizeof(buf), fh);
if (retval > 0) {
ret = 1;
Jim_AppendString(interp, strObj, buf, retval);
}if (retval > 0) { ... }
if (retval != sizeof(buf)) {
break;
}if (retval != sizeof(buf)) { ... }
}while (1) { ... }
fclose(fh);
return ret;
}{ ... }
/* ... */
static char **JimBuildEnv(Jim_Interp *interp)
{
int i;
int size;
int num;
int n;
char **envptr;
char *envdata;
Jim_Obj *objPtr = Jim_GetGlobalVariableStr(interp, "env", JIM_NONE);
if (!objPtr) {
return JimOriginalEnviron();
}if (!objPtr) { ... }
/* ... */
num = Jim_ListLength(interp, objPtr);
if (num % 2) {
num--;
}if (num % 2) { ... }
/* ... */
size = Jim_Length(objPtr) + 2;
envptr = Jim_Alloc(sizeof(*envptr) * (num / 2 + 1) + size);
envdata = (char *)&envptr[num / 2 + 1];
n = 0;
for (i = 0; i < num; i += 2) {
const char *s1, *s2;
Jim_Obj *elemObj;
Jim_ListIndex(interp, objPtr, i, &elemObj, JIM_NONE);
s1 = Jim_String(elemObj);
Jim_ListIndex(interp, objPtr, i + 1, &elemObj, JIM_NONE);
s2 = Jim_String(elemObj);
envptr[n] = envdata;
envdata += sprintf(envdata, "%s=%s", s1, s2);
envdata++;
n++;
}for (i = 0; i < num; i += 2) { ... }
envptr[n] = NULL;
*envdata = 0;
return envptr;
}{ ... }
/* ... */
static void JimFreeEnv(char **env, char **original_environ)
{
if (env != original_environ) {
Jim_Free(env);
}if (env != original_environ) { ... }
}{ ... }
static Jim_Obj *JimMakeErrorCode(Jim_Interp *interp, pidtype pid, int waitStatus, Jim_Obj *errStrObj)
{
Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0);
if (pid == JIM_BAD_PID || pid == JIM_NO_PID) {
Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "NONE", -1));
Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, (long)pid));
Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, -1));
}if (pid == JIM_BAD_PID || pid == JIM_NO_PID) { ... }
else if (WIFEXITED(waitStatus)) {
Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1));
Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, (long)pid));
Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WEXITSTATUS(waitStatus)));
}else if (WIFEXITED(waitStatus)) { ... }
else {
const char *type;
const char *action;
const char *signame;
if (WIFSIGNALED(waitStatus)) {
type = "CHILDKILLED";
action = "killed";
signame = Jim_SignalId(WTERMSIG(waitStatus));
}if (WIFSIGNALED(waitStatus)) { ... }
else {
type = "CHILDSUSP";
action = "suspended";
signame = "none";
}else { ... }
Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, type, -1));
if (errStrObj) {
/* ... */
Jim_AppendStrings(interp, errStrObj, "child ", action, " by signal ", Jim_SignalId(WTERMSIG(waitStatus)), "\n", NULL);
}if (errStrObj) { ... }
Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, (long)pid));
Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, signame, -1));
}else { ... }
return errorCode;
}{ ... }
/* ... */
static int JimCheckWaitStatus(Jim_Interp *interp, pidtype pid, int waitStatus, Jim_Obj *errStrObj)
{
if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) {
return JIM_OK;
}if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) { ... }
Jim_SetGlobalVariableStr(interp, "errorCode", JimMakeErrorCode(interp, pid, waitStatus, errStrObj));
return JIM_ERR;
}{ ... }
/* ... */
struct WaitInfo
{
pidtype pid;
int status;
int flags;
...};
struct WaitInfoTable {
struct WaitInfo *info;
int size;
int used;
int refcount;
...};
/* ... */
#define WI_DETACHED 2
#define WAIT_TABLE_GROW_BY 4
static void JimFreeWaitInfoTable(struct Jim_Interp *interp, void *privData)
{
struct WaitInfoTable *table = privData;
if (--table->refcount == 0) {
Jim_Free(table->info);
Jim_Free(table);
}if (--table->refcount == 0) { ... }
}{ ... }
static struct WaitInfoTable *JimAllocWaitInfoTable(void)
{
struct WaitInfoTable *table = Jim_Alloc(sizeof(*table));
table->info = NULL;
table->size = table->used = 0;
table->refcount = 1;
return table;
}{ ... }
/* ... */
static int JimWaitRemove(struct WaitInfoTable *table, pidtype pid)
{
int i;
for (i = 0; i < table->used; i++) {
if (pid == table->info[i].pid) {
if (i != table->used - 1) {
table->info[i] = table->info[table->used - 1];
}if (i != table->used - 1) { ... }
table->used--;
return 0;
}if (pid == table->info[i].pid) { ... }
}for (i = 0; i < table->used; i++) { ... }
return -1;
}{ ... }
/* ... */
static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
int outputId;
int errorId;
pidtype *pidPtr;
int numPids, result;
int child_siginfo = 1;
Jim_Obj *childErrObj;
Jim_Obj *errStrObj;
struct WaitInfoTable *table = Jim_CmdPrivData(interp);
/* ... */
if (argc > 1 && Jim_CompareStringImmediate(interp, argv[argc - 1], "&")) {
Jim_Obj *listObj;
int i;
argc--;
numPids = JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, NULL, NULL);
if (numPids < 0) {
return JIM_ERR;
}if (numPids < 0) { ... }
listObj = Jim_NewListObj(interp, NULL, 0);
for (i = 0; i < numPids; i++) {
Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, (long)pidPtr[i]));
}for (i = 0; i < numPids; i++) { ... }
Jim_SetResult(interp, listObj);
JimDetachPids(table, numPids, pidPtr);
Jim_Free(pidPtr);
return JIM_OK;
}if (argc > 1 && Jim_CompareStringImmediate(interp, argv[argc - 1], "&")) { ... }
/* ... */
numPids =
JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, &outputId, &errorId);
if (numPids < 0) {
return JIM_ERR;
}if (numPids < 0) { ... }
result = JIM_OK;
errStrObj = Jim_NewStringObj(interp, "", 0);
if (outputId != -1) {
if (JimAppendStreamToString(interp, outputId, errStrObj) < 0) {
result = JIM_ERR;
Jim_SetResultErrno(interp, "error reading from output pipe");
}if (JimAppendStreamToString(interp, outputId, errStrObj) < 0) { ... }
}if (outputId != -1) { ... }
childErrObj = Jim_NewStringObj(interp, "", 0);
Jim_IncrRefCount(childErrObj);
if (JimCleanupChildren(interp, numPids, pidPtr, childErrObj) != JIM_OK) {
result = JIM_ERR;
}if (JimCleanupChildren(interp, numPids, pidPtr, childErrObj) != JIM_OK) { ... }
/* ... */
if (errorId != -1) {
int ret;
lseek(errorId, 0, SEEK_SET);
ret = JimAppendStreamToString(interp, errorId, errStrObj);
if (ret < 0) {
Jim_SetResultErrno(interp, "error reading from error pipe");
result = JIM_ERR;
}if (ret < 0) { ... }
else if (ret > 0) {
child_siginfo = 0;
}else if (ret > 0) { ... }
}if (errorId != -1) { ... }
if (child_siginfo) {
Jim_AppendObj(interp, errStrObj, childErrObj);
}if (child_siginfo) { ... }
Jim_DecrRefCount(interp, childErrObj);
Jim_RemoveTrailingNewline(errStrObj);
Jim_SetResult(interp, errStrObj);
return result;
}{ ... }
/* ... */
static pidtype JimWaitForProcess(struct WaitInfoTable *table, pidtype pid, int *statusPtr)
{
if (JimWaitRemove(table, pid) == 0) {
waitpid(pid, statusPtr, 0);
return pid;
}if (JimWaitRemove(table, pid) == 0) { ... }
return JIM_BAD_PID;
}{ ... }
/* ... */
static void JimDetachPids(struct WaitInfoTable *table, int numPids, const pidtype *pidPtr)
{
int j;
for (j = 0; j < numPids; j++) {
int i;
for (i = 0; i < table->used; i++) {
if (pidPtr[j] == table->info[i].pid) {
table->info[i].flags |= WI_DETACHED;
break;
}if (pidPtr[j] == table->info[i].pid) { ... }
}for (i = 0; i < table->used; i++) { ... }
}for (j = 0; j < numPids; j++) { ... }
}{ ... }
/* ... */
static int JimGetChannelFd(Jim_Interp *interp, const char *name)
{
Jim_Obj *objv[2];
objv[0] = Jim_NewStringObj(interp, name, -1);
objv[1] = Jim_NewStringObj(interp, "getfd", -1);
if (Jim_EvalObjVector(interp, 2, objv) == JIM_OK) {
jim_wide fd;
if (Jim_GetWide(interp, Jim_GetResult(interp), &fd) == JIM_OK) {
return fd;
}if (Jim_GetWide(interp, Jim_GetResult(interp), &fd) == JIM_OK) { ... }
}if (Jim_EvalObjVector(interp, 2, objv) == JIM_OK) { ... }
return -1;
}{ ... }
static void JimReapDetachedPids(struct WaitInfoTable *table)
{
struct WaitInfo *waitPtr;
int count;
int dest;
if (!table) {
return;
}if (!table) { ... }
waitPtr = table->info;
dest = 0;
for (count = table->used; count > 0; waitPtr++, count--) {
if (waitPtr->flags & WI_DETACHED) {
int status;
pidtype pid = waitpid(waitPtr->pid, &status, WNOHANG);
if (pid == waitPtr->pid) {
table->used--;
continue;
}if (pid == waitPtr->pid) { ... }
}if (waitPtr->flags & WI_DETACHED) { ... }
if (waitPtr != &table->info[dest]) {
table->info[dest] = *waitPtr;
}if (waitPtr != &table->info[dest]) { ... }
dest++;
}for (count = table->used; count > 0; waitPtr++, count--) { ... }
}{ ... }
/* ... */
static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
struct WaitInfoTable *table = Jim_CmdPrivData(interp);
int nohang = 0;
pidtype pid;
long pidarg;
int status;
Jim_Obj *errCodeObj;
if (argc == 1) {
JimReapDetachedPids(table);
return JIM_OK;
}if (argc == 1) { ... }
if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-nohang")) {
nohang = 1;
}if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-nohang")) { ... }
if (argc != nohang + 2) {
Jim_WrongNumArgs(interp, 1, argv, "?-nohang? ?pid?");
return JIM_ERR;
}if (argc != nohang + 2) { ... }
if (Jim_GetLong(interp, argv[nohang + 1], &pidarg) != JIM_OK) {
return JIM_ERR;
}if (Jim_GetLong(interp, argv[nohang + 1], &pidarg) != JIM_OK) { ... }
pid = waitpid((pidtype)pidarg, &status, nohang ? WNOHANG : 0);
errCodeObj = JimMakeErrorCode(interp, pid, status, NULL);
if (pid != JIM_BAD_PID && (WIFEXITED(status) || WIFSIGNALED(status))) {
JimWaitRemove(table, pid);
}if (pid != JIM_BAD_PID && (WIFEXITED(status) || WIFSIGNALED(status))) { ... }
Jim_SetResult(interp, errCodeObj);
return JIM_OK;
}{ ... }
static int Jim_PidCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
if (argc != 1) {
Jim_WrongNumArgs(interp, 1, argv, "");
return JIM_ERR;
}if (argc != 1) { ... }
Jim_SetResultInt(interp, (jim_wide)getpid());
return JIM_OK;
}{ ... }
/* ... */
static int
JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, pidtype **pidArrayPtr,
int *inPipePtr, int *outPipePtr, int *errFilePtr)
{
pidtype *pidPtr = NULL;
/* ... */
int numPids = 0;
/* ... */
int cmdCount;
/* ... */
const char *input = NULL;
/* ... */
int input_len = 0;
#define FILE_NAME 0
#define FILE_APPEND 1
#define FILE_HANDLE 2
#define FILE_TEXT 3
int inputFile = FILE_NAME;
/* ... */
int outputFile = FILE_NAME;
/* ... */
int errorFile = FILE_NAME;
/* ... */
const char *output = NULL;
/* ... */
const char *error = NULL;
/* ... */
int inputId = -1;
/* ... */
int outputId = -1;
/* ... */
int errorId = -1;
/* ... */
int lastOutputId = -1;
/* ... */
int pipeIds[2];
int firstArg, lastArg;
/* ... */
int lastBar;
int i;
pidtype pid;
char **save_environ;
#ifndef __MINGW32__
char **child_environ;
#endif
struct WaitInfoTable *table = Jim_CmdPrivData(interp);
char **arg_array = Jim_Alloc(sizeof(*arg_array) * (argc + 1));
int arg_count = 0;
if (inPipePtr != NULL) {
*inPipePtr = -1;
}if (inPipePtr != NULL) { ... }
if (outPipePtr != NULL) {
*outPipePtr = -1;
}if (outPipePtr != NULL) { ... }
if (errFilePtr != NULL) {
*errFilePtr = -1;
}if (errFilePtr != NULL) { ... }
pipeIds[0] = pipeIds[1] = -1;
/* ... */
cmdCount = 1;
lastBar = -1;
for (i = 0; i < argc; i++) {
const char *arg = Jim_String(argv[i]);
if (arg[0] == '<') {
inputFile = FILE_NAME;
input = arg + 1;
if (*input == '<') {
inputFile = FILE_TEXT;
input_len = Jim_Length(argv[i]) - 2;
input++;
}if (*input == '<') { ... }
else if (*input == '@') {
inputFile = FILE_HANDLE;
input++;
}else if (*input == '@') { ... }
if (!*input && ++i < argc) {
input = Jim_GetString(argv[i], &input_len);
}if (!*input && ++i < argc) { ... }
}if (arg[0] == '<') { ... }
else if (arg[0] == '>') {
int dup_error = 0;
outputFile = FILE_NAME;
output = arg + 1;
if (*output == '>') {
outputFile = FILE_APPEND;
output++;
}if (*output == '>') { ... }
if (*output == '&') {
output++;
dup_error = 1;
}if (*output == '&') { ... }
if (*output == '@') {
outputFile = FILE_HANDLE;
output++;
}if (*output == '@') { ... }
if (!*output && ++i < argc) {
output = Jim_String(argv[i]);
}if (!*output && ++i < argc) { ... }
if (dup_error) {
errorFile = outputFile;
error = output;
}if (dup_error) { ... }
}else if (arg[0] == '>') { ... }
else if (arg[0] == '2' && arg[1] == '>') {
error = arg + 2;
errorFile = FILE_NAME;
if (*error == '@') {
errorFile = FILE_HANDLE;
error++;
}if (*error == '@') { ... }
else if (*error == '>') {
errorFile = FILE_APPEND;
error++;
}else if (*error == '>') { ... }
if (!*error && ++i < argc) {
error = Jim_String(argv[i]);
}if (!*error && ++i < argc) { ... }
}else if (arg[0] == '2' && arg[1] == '>') { ... }
else {
if (strcmp(arg, "|") == 0 || strcmp(arg, "|&") == 0) {
if (i == lastBar + 1 || i == argc - 1) {
Jim_SetResultString(interp, "illegal use of | or |& in command", -1);
goto badargs;
}if (i == lastBar + 1 || i == argc - 1) { ... }
lastBar = i;
cmdCount++;
}if (strcmp(arg, "|") == 0 || strcmp(arg, "|&") == 0) { ... }
arg_array[arg_count++] = (char *)arg;
continue;
}else { ... }
if (i >= argc) {
Jim_SetResultFormatted(interp, "can't specify \"%s\" as last word in command", arg);
goto badargs;
}if (i >= argc) { ... }
}for (i = 0; i < argc; i++) { ... }
if (arg_count == 0) {
Jim_SetResultString(interp, "didn't specify command to execute", -1);
badargs:
Jim_Free(arg_array);
return -1;
}if (arg_count == 0) { ... }
save_environ = JimSaveEnv(JimBuildEnv(interp));
/* ... */
if (input != NULL) {
if (inputFile == FILE_TEXT) {
/* ... */
inputId = Jim_MakeTempFile(interp, NULL, 1);
if (inputId == -1) {
goto error;
}if (inputId == -1) { ... }
if (write(inputId, input, input_len) != input_len) {
Jim_SetResultErrno(interp, "couldn't write temp file");
close(inputId);
goto error;
}if (write(inputId, input, input_len) != input_len) { ... }
lseek(inputId, 0L, SEEK_SET);
}if (inputFile == FILE_TEXT) { ... }
else if (inputFile == FILE_HANDLE) {
int fd = JimGetChannelFd(interp, input);
if (fd < 0) {
goto error;
}if (fd < 0) { ... }
inputId = dup(fd);
}else if (inputFile == FILE_HANDLE) { ... }
else {
/* ... */
inputId = Jim_OpenForRead(input);
if (inputId == -1) {
Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", input, strerror(Jim_Errno()));
goto error;
}if (inputId == -1) { ... }
}else { ... }
}if (input != NULL) { ... }
else if (inPipePtr != NULL) {
if (pipe(pipeIds) != 0) {
Jim_SetResultErrno(interp, "couldn't create input pipe for command");
goto error;
}if (pipe(pipeIds) != 0) { ... }
inputId = pipeIds[0];
*inPipePtr = pipeIds[1];
pipeIds[0] = pipeIds[1] = -1;
}else if (inPipePtr != NULL) { ... }
/* ... */
if (output != NULL) {
if (outputFile == FILE_HANDLE) {
int fd = JimGetChannelFd(interp, output);
if (fd < 0) {
goto error;
}if (fd < 0) { ... }
lastOutputId = dup(fd);
}if (outputFile == FILE_HANDLE) { ... }
else {
/* ... */
lastOutputId = Jim_OpenForWrite(output, outputFile == FILE_APPEND);
if (lastOutputId == -1) {
Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", output, strerror(Jim_Errno()));
goto error;
}if (lastOutputId == -1) { ... }
}else { ... }
}if (output != NULL) { ... }
else if (outPipePtr != NULL) {
/* ... */
if (pipe(pipeIds) != 0) {
Jim_SetResultErrno(interp, "couldn't create output pipe");
goto error;
}if (pipe(pipeIds) != 0) { ... }
lastOutputId = pipeIds[1];
*outPipePtr = pipeIds[0];
pipeIds[0] = pipeIds[1] = -1;
}else if (outPipePtr != NULL) { ... }
if (error != NULL) {
if (errorFile == FILE_HANDLE) {
if (strcmp(error, "1") == 0) {
if (lastOutputId != -1) {
errorId = dup(lastOutputId);
}if (lastOutputId != -1) { ... }
else {
error = "stdout";
}else { ... }
}if (strcmp(error, "1") == 0) { ... }
if (errorId == -1) {
int fd = JimGetChannelFd(interp, error);
if (fd < 0) {
goto error;
}if (fd < 0) { ... }
errorId = dup(fd);
}if (errorId == -1) { ... }
}if (errorFile == FILE_HANDLE) { ... }
else {
/* ... */
errorId = Jim_OpenForWrite(error, errorFile == FILE_APPEND);
if (errorId == -1) {
Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", error, strerror(Jim_Errno()));
goto error;
}if (errorId == -1) { ... }
}else { ... }
}if (error != NULL) { ... }
else if (errFilePtr != NULL) {
/* ... */
errorId = Jim_MakeTempFile(interp, NULL, 1);
if (errorId == -1) {
goto error;
}if (errorId == -1) { ... }
*errFilePtr = dup(errorId);
}else if (errFilePtr != NULL) { ... }
/* ... */
pidPtr = Jim_Alloc(cmdCount * sizeof(*pidPtr));
for (firstArg = 0; firstArg < arg_count; numPids++, firstArg = lastArg + 1) {
int pipe_dup_err = 0;
int origErrorId = errorId;
for (lastArg = firstArg; lastArg < arg_count; lastArg++) {
if (strcmp(arg_array[lastArg], "|") == 0) {
break;
}if (strcmp(arg_array[lastArg], "|") == 0) { ... }
if (strcmp(arg_array[lastArg], "|&") == 0) {
pipe_dup_err = 1;
break;
}if (strcmp(arg_array[lastArg], "|&") == 0) { ... }
}for (lastArg = firstArg; lastArg < arg_count; lastArg++) { ... }
if (lastArg == firstArg) {
Jim_SetResultString(interp, "missing command to exec", -1);
goto error;
}if (lastArg == firstArg) { ... }
arg_array[lastArg] = NULL;
if (lastArg == arg_count) {
outputId = lastOutputId;
lastOutputId = -1;
}if (lastArg == arg_count) { ... }
else {
if (pipe(pipeIds) != 0) {
Jim_SetResultErrno(interp, "couldn't create pipe");
goto error;
}if (pipe(pipeIds) != 0) { ... }
outputId = pipeIds[1];
}else { ... }
if (pipe_dup_err) {
errorId = outputId;
}if (pipe_dup_err) { ... }
#ifdef __MINGW32__
pid = JimStartWinProcess(interp, &arg_array[firstArg], save_environ, inputId, outputId, errorId);
if (pid == JIM_BAD_PID) {
Jim_SetResultFormatted(interp, "couldn't exec \"%s\"", arg_array[firstArg]);
goto error;
}if (pid == JIM_BAD_PID) { ... }
/* ... */#else
i = strlen(arg_array[firstArg]);
child_environ = Jim_GetEnviron();
/* ... */
pid = vfork();
if (pid < 0) {
Jim_SetResultErrno(interp, "couldn't fork child process");
goto error;
}if (pid < 0) { ... }
if (pid == 0) {
if (inputId != -1 && inputId != fileno(stdin)) {
dup2(inputId, fileno(stdin));
close(inputId);
}if (inputId != -1 && inputId != fileno(stdin)) { ... }
if (outputId != -1 && outputId != fileno(stdout)) {
dup2(outputId, fileno(stdout));
if (outputId != errorId) {
close(outputId);
}if (outputId != errorId) { ... }
}if (outputId != -1 && outputId != fileno(stdout)) { ... }
if (errorId != -1 && errorId != fileno(stderr)) {
dup2(errorId, fileno(stderr));
close(errorId);
}if (errorId != -1 && errorId != fileno(stderr)) { ... }
if (outPipePtr && *outPipePtr != -1) {
close(*outPipePtr);
}if (outPipePtr && *outPipePtr != -1) { ... }
if (errFilePtr && *errFilePtr != -1) {
close(*errFilePtr);
}if (errFilePtr && *errFilePtr != -1) { ... }
if (pipeIds[0] != -1) {
close(pipeIds[0]);
}if (pipeIds[0] != -1) { ... }
if (lastOutputId != -1) {
close(lastOutputId);
}if (lastOutputId != -1) { ... }
execvpe(arg_array[firstArg], &arg_array[firstArg], child_environ);
if (write(fileno(stderr), "couldn't exec \"", 15) &&
write(fileno(stderr), arg_array[firstArg], i) &&
write(fileno(stderr), "\"\n", 2)) {
}if (write(fileno(stderr), "couldn't exec \"", 15) && write(fileno(stderr), arg_array[firstArg], i) && write(fileno(stderr), "\"\n", 2)) { ... }
#ifdef JIM_MAINTAINER
{
static char *const false_argv[2] = {"false", NULL};
execvp(false_argv[0],false_argv);
}/* ... */
if (write(fileno(stderr), "couldn't exec \"", 15) && write(fileno(stderr), arg_array[firstArg], i) && write(fileno(stderr), "\"\n", 2)) { ... }#endif
_exit(127);
}if (pid == 0) { ... }
/* ... */#endif
/* ... */
if (table->used == table->size) {
table->size += WAIT_TABLE_GROW_BY;
table->info = Jim_Realloc(table->info, table->size * sizeof(*table->info));
}if (table->used == table->size) { ... }
table->info[table->used].pid = pid;
table->info[table->used].flags = 0;
table->used++;
pidPtr[numPids] = pid;
errorId = origErrorId;
/* ... */
if (inputId != -1) {
close(inputId);
}if (inputId != -1) { ... }
if (outputId != -1) {
close(outputId);
}if (outputId != -1) { ... }
inputId = pipeIds[0];
pipeIds[0] = pipeIds[1] = -1;
}for (firstArg = 0; firstArg < arg_count; numPids++, firstArg = lastArg + 1) { ... }
*pidArrayPtr = pidPtr;
/* ... */
cleanup:
if (inputId != -1) {
close(inputId);
}if (inputId != -1) { ... }
if (lastOutputId != -1) {
close(lastOutputId);
}if (lastOutputId != -1) { ... }
if (errorId != -1) {
close(errorId);
}if (errorId != -1) { ... }
Jim_Free(arg_array);
JimRestoreEnv(save_environ);
return numPids;
/* ... */
error:
if ((inPipePtr != NULL) && (*inPipePtr != -1)) {
close(*inPipePtr);
*inPipePtr = -1;
}if ((inPipePtr != NULL) && (*inPipePtr != -1)) { ... }
if ((outPipePtr != NULL) && (*outPipePtr != -1)) {
close(*outPipePtr);
*outPipePtr = -1;
}if ((outPipePtr != NULL) && (*outPipePtr != -1)) { ... }
if ((errFilePtr != NULL) && (*errFilePtr != -1)) {
close(*errFilePtr);
*errFilePtr = -1;
}if ((errFilePtr != NULL) && (*errFilePtr != -1)) { ... }
if (pipeIds[0] != -1) {
close(pipeIds[0]);
}if (pipeIds[0] != -1) { ... }
if (pipeIds[1] != -1) {
close(pipeIds[1]);
}if (pipeIds[1] != -1) { ... }
if (pidPtr != NULL) {
for (i = 0; i < numPids; i++) {
if (pidPtr[i] != JIM_BAD_PID) {
JimDetachPids(table, 1, &pidPtr[i]);
}if (pidPtr[i] != JIM_BAD_PID) { ... }
}for (i = 0; i < numPids; i++) { ... }
Jim_Free(pidPtr);
}if (pidPtr != NULL) { ... }
numPids = -1;
goto cleanup;
}{ ... }
/* ... */
static int JimCleanupChildren(Jim_Interp *interp, int numPids, pidtype *pidPtr, Jim_Obj *errStrObj)
{
struct WaitInfoTable *table = Jim_CmdPrivData(interp);
int result = JIM_OK;
int i;
for (i = 0; i < numPids; i++) {
int waitStatus = 0;
if (JimWaitForProcess(table, pidPtr[i], &waitStatus) != JIM_BAD_PID) {
if (JimCheckWaitStatus(interp, pidPtr[i], waitStatus, errStrObj) != JIM_OK) {
result = JIM_ERR;
}if (JimCheckWaitStatus(interp, pidPtr[i], waitStatus, errStrObj) != JIM_OK) { ... }
}if (JimWaitForProcess(table, pidPtr[i], &waitStatus) != JIM_BAD_PID) { ... }
}for (i = 0; i < numPids; i++) { ... }
Jim_Free(pidPtr);
return result;
}{ ... }
int Jim_execInit(Jim_Interp *interp)
{
struct WaitInfoTable *waitinfo;
Jim_PackageProvideCheck(interp, "exec");
waitinfo = JimAllocWaitInfoTable();
Jim_CreateCommand(interp, "exec", Jim_ExecCmd, waitinfo, JimFreeWaitInfoTable);
waitinfo->refcount++;
Jim_CreateCommand(interp, "wait", Jim_WaitCommand, waitinfo, JimFreeWaitInfoTable);
Jim_CreateCommand(interp, "pid", Jim_PidCommand, 0, 0);
return JIM_OK;
}{ ... }
#if defined(__MINGW32__)
static int
JimWinFindExecutable(const char *originalName, char fullPath[MAX_PATH])
{
int i;
static char extensions[][5] = {".exe", "", ".bat"};
for (i = 0; i < (int) (sizeof(extensions) / sizeof(extensions[0])); i++) {
snprintf(fullPath, MAX_PATH, "%s%s", originalName, extensions[i]);
if (SearchPath(NULL, fullPath, NULL, MAX_PATH, fullPath, NULL) == 0) {
continue;
}if (SearchPath(NULL, fullPath, NULL, MAX_PATH, fullPath, NULL) == 0) { ... }
if (GetFileAttributes(fullPath) & FILE_ATTRIBUTE_DIRECTORY) {
continue;
}if (GetFileAttributes(fullPath) & FILE_ATTRIBUTE_DIRECTORY) { ... }
return 0;
}for (i = 0; i < (int) (sizeof(extensions) / sizeof(extensions[0])); i++) { ... }
return -1;
}{ ... }
static char **JimSaveEnv(char **env)
{
return env;
}{ ... }
static void JimRestoreEnv(char **env)
{
JimFreeEnv(env, Jim_GetEnviron());
}{ ... }
static char **JimOriginalEnviron(void)
{
return NULL;
}{ ... }
static Jim_Obj *
JimWinBuildCommandLine(Jim_Interp *interp, char **argv)
{
char *start, *special;
int quote, i;
Jim_Obj *strObj = Jim_NewStringObj(interp, "", 0);
for (i = 0; argv[i]; i++) {
if (i > 0) {
Jim_AppendString(interp, strObj, " ", 1);
}if (i > 0) { ... }
if (argv[i][0] == '\0') {
quote = 1;
}if (argv[i][0] == '\0') { ... }
else {
quote = 0;
for (start = argv[i]; *start != '\0'; start++) {
if (isspace(UCHAR(*start))) {
quote = 1;
break;
}if (isspace(UCHAR(*start))) { ... }
}for (start = argv[i]; *start != '\0'; start++) { ... }
}else { ... }
if (quote) {
Jim_AppendString(interp, strObj, "\"" , 1);
}if (quote) { ... }
start = argv[i];
for (special = argv[i]; ; ) {
if ((*special == '\\') && (special[1] == '\\' ||
special[1] == '"' || (quote && special[1] == '\0'))) {
Jim_AppendString(interp, strObj, start, special - start);
start = special;
while (1) {
special++;
if (*special == '"' || (quote && *special == '\0')) {
/* ... */
Jim_AppendString(interp, strObj, start, special - start);
break;
}if (*special == '"' || (quote && *special == '\0')) { ... }
if (*special != '\\') {
break;
}if (*special != '\\') { ... }
}while (1) { ... }
Jim_AppendString(interp, strObj, start, special - start);
start = special;
}if ((*special == '\\') && (special[1] == '\\' || special[1] == '"' || (quote && special[1] == '\0'))) { ... }
if (*special == '"') {
if (special == start) {
Jim_AppendString(interp, strObj, "\"", 1);
}if (special == start) { ... }
else {
Jim_AppendString(interp, strObj, start, special - start);
}else { ... }
Jim_AppendString(interp, strObj, "\\\"", 2);
start = special + 1;
}if (*special == '"') { ... }
if (*special == '\0') {
break;
}if (*special == '\0') { ... }
special++;
}for (special = argv[i]; ;) { ... }
Jim_AppendString(interp, strObj, start, special - start);
if (quote) {
Jim_AppendString(interp, strObj, "\"", 1);
}if (quote) { ... }
}for (i = 0; argv[i]; i++) { ... }
return strObj;
}{ ... }
/* ... */
static pidtype
JimStartWinProcess(Jim_Interp *interp, char **argv, char **env, int inputId, int outputId, int errorId)
{
STARTUPINFO startInfo;
PROCESS_INFORMATION procInfo;
HANDLE hProcess;
char execPath[MAX_PATH];
pidtype pid = JIM_BAD_PID;
Jim_Obj *cmdLineObj;
char *winenv;
if (JimWinFindExecutable(argv[0], execPath) < 0) {
return JIM_BAD_PID;
}if (JimWinFindExecutable(argv[0], execPath) < 0) { ... }
argv[0] = execPath;
hProcess = GetCurrentProcess();
cmdLineObj = JimWinBuildCommandLine(interp, argv);
/* ... */
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags = STARTF_USESTDHANDLES;
startInfo.hStdInput = INVALID_HANDLE_VALUE;
startInfo.hStdOutput= INVALID_HANDLE_VALUE;
startInfo.hStdError = INVALID_HANDLE_VALUE;
/* ... */
/* ... */
if (inputId == -1) {
inputId = _fileno(stdin);
}if (inputId == -1) { ... }
DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(inputId), hProcess, &startInfo.hStdInput,
0, TRUE, DUPLICATE_SAME_ACCESS);
if (startInfo.hStdInput == INVALID_HANDLE_VALUE) {
goto end;
}if (startInfo.hStdInput == INVALID_HANDLE_VALUE) { ... }
/* ... */
if (outputId == -1) {
outputId = _fileno(stdout);
}if (outputId == -1) { ... }
DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(outputId), hProcess, &startInfo.hStdOutput,
0, TRUE, DUPLICATE_SAME_ACCESS);
if (startInfo.hStdOutput == INVALID_HANDLE_VALUE) {
goto end;
}if (startInfo.hStdOutput == INVALID_HANDLE_VALUE) { ... }
if (errorId == -1) {
errorId = _fileno(stderr);
}if (errorId == -1) { ... }
DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(errorId), hProcess, &startInfo.hStdError,
0, TRUE, DUPLICATE_SAME_ACCESS);
if (startInfo.hStdError == INVALID_HANDLE_VALUE) {
goto end;
}if (startInfo.hStdError == INVALID_HANDLE_VALUE) { ... }
/* ... */
if (env == NULL) {
winenv = NULL;
}if (env == NULL) { ... }
else if (env[0] == NULL) {
winenv = (char *)"\0";
}else if (env[0] == NULL) { ... }
else {
winenv = env[0];
}else { ... }
if (!CreateProcess(NULL, (char *)Jim_String(cmdLineObj), NULL, NULL, TRUE,
0, winenv, NULL, &startInfo, &procInfo)) {
goto end;
}if (!CreateProcess(NULL, (char *)Jim_String(cmdLineObj), NULL, NULL, TRUE, 0, winenv, NULL, &startInfo, &procInfo)) { ... }
/* ... */
WaitForInputIdle(procInfo.hProcess, 5000);
CloseHandle(procInfo.hThread);
pid = procInfo.hProcess;
end:
Jim_FreeNewObj(interp, cmdLineObj);
if (startInfo.hStdInput != INVALID_HANDLE_VALUE) {
CloseHandle(startInfo.hStdInput);
}if (startInfo.hStdInput != INVALID_HANDLE_VALUE) { ... }
if (startInfo.hStdOutput != INVALID_HANDLE_VALUE) {
CloseHandle(startInfo.hStdOutput);
}if (startInfo.hStdOutput != INVALID_HANDLE_VALUE) { ... }
if (startInfo.hStdError != INVALID_HANDLE_VALUE) {
CloseHandle(startInfo.hStdError);
}if (startInfo.hStdError != INVALID_HANDLE_VALUE) { ... }
return pid;
}{ ... }
/* ... */#else
static char **JimOriginalEnviron(void)
{
return Jim_GetEnviron();
}JimOriginalEnviron (void) { ... }
static char **JimSaveEnv(char **env)
{
char **saveenv = Jim_GetEnviron();
Jim_SetEnviron(env);
return saveenv;
}JimSaveEnv (char **env) { ... }
static void JimRestoreEnv(char **env)
{
JimFreeEnv(Jim_GetEnviron(), env);
Jim_SetEnviron(env);
}JimRestoreEnv (char **env) { ... }
/* ... */#endif/* ... */
#endif