2017-04-19 09:11:50 -07:00
|
|
|
// This is an open source non-commercial project. Dear PVS-Studio, please check
|
|
|
|
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
|
|
|
|
|
2015-04-13 20:53:16 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2016-02-27 20:54:05 -07:00
|
|
|
#include <stdint.h>
|
2018-03-06 19:38:10 -07:00
|
|
|
#ifdef _MSC_VER
|
2018-03-04 16:44:23 -07:00
|
|
|
#include <Windows.h>
|
|
|
|
#define usleep(usecs) Sleep(usecs/1000)
|
|
|
|
#else
|
2018-02-08 07:11:56 -07:00
|
|
|
#include <unistd.h>
|
2018-03-04 16:44:23 -07:00
|
|
|
#endif
|
2018-02-08 07:11:56 -07:00
|
|
|
|
2019-07-20 09:07:04 -07:00
|
|
|
static void flush_wait(void)
|
2018-02-08 07:11:56 -07:00
|
|
|
{
|
2019-09-01 16:52:21 -07:00
|
|
|
fflush(NULL);
|
2019-07-20 09:07:04 -07:00
|
|
|
usleep(10*1000); // Wait 10 ms.
|
2018-02-08 07:11:56 -07:00
|
|
|
}
|
2016-02-27 20:54:05 -07:00
|
|
|
|
|
|
|
static void help(void)
|
|
|
|
{
|
|
|
|
puts("A simple implementation of a shell for testing termopen().");
|
|
|
|
puts("");
|
|
|
|
puts("Usage:");
|
|
|
|
puts(" shell-test --help");
|
|
|
|
puts(" Prints this help to stdout.");
|
|
|
|
puts(" shell-test");
|
|
|
|
puts(" shell-test EXE");
|
|
|
|
puts(" Prints \"ready $ \" to stderr.");
|
2016-03-29 05:47:30 -07:00
|
|
|
puts(" shell-test -t {prompt text}");
|
|
|
|
puts(" Prints \"{prompt text} $ \" to stderr.");
|
2016-02-27 20:54:05 -07:00
|
|
|
puts(" shell-test EXE \"prog args...\"");
|
|
|
|
puts(" Prints \"ready $ prog args...\\n\" to stderr.");
|
2016-03-29 05:47:30 -07:00
|
|
|
puts(" shell-test -t {prompt text} EXE \"prog args...\"");
|
|
|
|
puts(" Prints \"{prompt text} $ progs args...\" to stderr.");
|
2019-07-20 09:07:04 -07:00
|
|
|
puts(" shell-test REP N {text}");
|
|
|
|
puts(" Prints \"{lnr}: {text}\\n\" to stdout N times, taking N milliseconds.");
|
|
|
|
puts(" Example:");
|
|
|
|
puts(" shell-test REP 97 \"foo bar\"");
|
|
|
|
puts(" 0: foo bar");
|
2016-02-27 20:54:05 -07:00
|
|
|
puts(" ...");
|
2019-07-20 09:07:04 -07:00
|
|
|
puts(" 96: foo bar");
|
2019-07-03 13:49:13 -07:00
|
|
|
puts(" shell-test INTERACT");
|
|
|
|
puts(" Prints \"interact $ \" to stderr, and waits for \"exit\" input.");
|
2016-02-27 20:54:05 -07:00
|
|
|
}
|
2015-04-13 20:53:16 -07:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2016-02-27 20:54:05 -07:00
|
|
|
if (argc == 2 && strcmp(argv[1], "--help") == 0) {
|
|
|
|
help();
|
|
|
|
}
|
2015-04-13 20:53:16 -07:00
|
|
|
|
2016-02-27 20:54:05 -07:00
|
|
|
if (argc >= 2) {
|
2016-03-29 05:47:30 -07:00
|
|
|
if (strcmp(argv[1], "-t") == 0) {
|
|
|
|
if (argc < 3) {
|
|
|
|
fprintf(stderr,"Missing prompt text for -t option\n");
|
|
|
|
return 5;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s $ ", argv[2]);
|
|
|
|
if (argc >= 5 && (strcmp(argv[3], "EXE") == 0)) {
|
|
|
|
fprintf(stderr, "%s\n", argv[4]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (strcmp(argv[1], "EXE") == 0) {
|
2016-02-27 20:54:05 -07:00
|
|
|
fprintf(stderr, "ready $ ");
|
|
|
|
if (argc >= 3) {
|
|
|
|
fprintf(stderr, "%s\n", argv[2]);
|
|
|
|
}
|
2019-08-27 04:04:28 -07:00
|
|
|
} else if (strcmp(argv[1], "REP") == 0) {
|
2019-07-20 09:07:04 -07:00
|
|
|
if (argc != 4) {
|
|
|
|
fprintf(stderr, "REP expects exactly 3 arguments\n");
|
2016-02-27 20:54:05 -07:00
|
|
|
return 4;
|
|
|
|
}
|
2019-07-16 12:35:53 -07:00
|
|
|
int count = 0;
|
|
|
|
if (sscanf(argv[2], "%d", &count) != 1) {
|
|
|
|
fprintf(stderr, "Invalid count: %s\n", argv[2]);
|
|
|
|
return 4;
|
|
|
|
}
|
2019-08-27 04:04:28 -07:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
printf("%d: %s\n", i, argv[3]);
|
|
|
|
if (i % 100 == 0) {
|
2019-08-08 07:02:28 -07:00
|
|
|
usleep(1000); // Wait 1 ms (simulate typical output).
|
|
|
|
}
|
2019-09-01 16:52:21 -07:00
|
|
|
fflush(NULL);
|
2016-02-27 20:54:05 -07:00
|
|
|
}
|
2018-02-08 07:11:56 -07:00
|
|
|
} else if (strcmp(argv[1], "UTF-8") == 0) {
|
|
|
|
// test split-up UTF-8 sequence
|
2019-07-20 09:07:04 -07:00
|
|
|
printf("\xc3"); flush_wait();
|
|
|
|
printf("\xa5\n"); flush_wait();
|
2018-02-08 07:11:56 -07:00
|
|
|
|
|
|
|
// split up a 2+2 grapheme clusters all possible ways
|
2019-07-20 09:07:04 -07:00
|
|
|
printf("ref: \xc3\xa5\xcc\xb2\n"); flush_wait();
|
2018-02-08 07:11:56 -07:00
|
|
|
|
2019-07-20 09:07:04 -07:00
|
|
|
printf("1: \xc3"); flush_wait();
|
|
|
|
printf("\xa5\xcc\xb2\n"); flush_wait();
|
2018-02-08 07:11:56 -07:00
|
|
|
|
2019-07-20 09:07:04 -07:00
|
|
|
printf("2: \xc3\xa5"); flush_wait();
|
|
|
|
printf("\xcc\xb2\n"); flush_wait();
|
2018-02-08 07:11:56 -07:00
|
|
|
|
2019-07-20 09:07:04 -07:00
|
|
|
printf("3: \xc3\xa5\xcc"); flush_wait();
|
|
|
|
printf("\xb2\n"); flush_wait();
|
2019-07-03 13:49:13 -07:00
|
|
|
} else if (strcmp(argv[1], "INTERACT") == 0) {
|
|
|
|
char input[256];
|
|
|
|
char cmd[100];
|
|
|
|
int arg;
|
|
|
|
int input_argc;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
fprintf(stderr, "interact $ ");
|
|
|
|
|
|
|
|
if (fgets(input, sizeof(input), stdin) == NULL) {
|
|
|
|
break; // EOF
|
|
|
|
}
|
|
|
|
|
2019-07-16 12:35:53 -07:00
|
|
|
input_argc = sscanf(input, "%99s %d", cmd, &arg);
|
2019-07-03 13:49:13 -07:00
|
|
|
if(1 == input_argc) {
|
|
|
|
arg = 0;
|
|
|
|
}
|
|
|
|
if (strcmp(cmd, "exit") == 0) {
|
|
|
|
return arg;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "command not found: %s\n", cmd);
|
|
|
|
}
|
|
|
|
}
|
2016-02-27 20:54:05 -07:00
|
|
|
} else {
|
2019-07-03 13:49:13 -07:00
|
|
|
fprintf(stderr, "Unknown first argument: %s\n", argv[1]);
|
2016-02-27 20:54:05 -07:00
|
|
|
return 3;
|
2015-04-13 20:53:16 -07:00
|
|
|
}
|
2019-09-01 16:52:21 -07:00
|
|
|
fflush(NULL);
|
2016-02-27 20:54:05 -07:00
|
|
|
return 0;
|
|
|
|
} else if (argc == 1) {
|
|
|
|
fprintf(stderr, "ready $ ");
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Missing first argument\n");
|
|
|
|
return 2;
|
2015-04-13 20:53:16 -07:00
|
|
|
}
|
|
|
|
}
|