robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #39626 docsdf5d559878

master
5.2 KiB188 linesraw
1#include <stdbool.h>
2#include <stdint.h>
3#include <stdio.h>
4#include <string.h>
5#ifdef MSWIN
6# include <windows.h>
7# define usleep(usecs) Sleep((usecs) / 1000)
8#else
9# include <stdlib.h>
10# include <termios.h>
11# include <unistd.h>
12#endif
13
14static void flush_wait(void)
15{
16  fflush(NULL);
17  usleep(10 * 1000);  // Wait 10 ms.
18}
19
20static void help(void)
21{
22  puts("Fake shell");
23  puts("");
24  puts("Usage:");
25  puts("  shell-test --help");
26  puts("    Prints this help to stdout.");
27  puts("  shell-test");
28  puts("  shell-test EXE");
29  puts("    Prints \"ready $ \" to stderr.");
30  puts("  shell-test -t {prompt text}");
31  puts("    Prints \"{prompt text} $ \" to stderr.");
32  puts("  shell-test EXE \"prog args...\"");
33  puts("    Prints \"ready $ prog args...\\n\" to stderr.");
34  puts("  shell-test -t {prompt text} EXE \"prog args...\"");
35  puts("    Prints \"{prompt text} $ progs args...\" to stderr.");
36  puts("  shell-test EXECVP \"prog\" \"arg0\" args...");
37  puts("    Executes prog arg0 args... using execvp().");
38  puts("  shell-test REP N {text}");
39  puts("    Prints \"{lnr}: {text}\\n\" to stdout N times, pausing every 100 lines.");
40  puts("    Example:");
41  puts("      shell-test REP 97 \"foo bar\"");
42  puts("      0: foo bar");
43  puts("      ...");
44  puts("      96: foo bar");
45  puts("  shell-test REPFAST N {text}");
46  puts("    Like REP, but print as fast as possible and then exit immediately.");
47  puts("  shell-test INTERACT");
48  puts("    Prints \"interact $ \" to stderr, and waits for \"exit\" input.");
49  puts("  shell-test HOLD");
50  puts("    Prints \"holding $ \" to stderr, and waits indefinitely (without reading stdin, unlike INTERACT).");
51  puts("  shell-test EXIT {code}");
52  puts("    Exits immediately with exit code \"{code}\".");
53}
54
55#ifndef MSWIN
56static void drain_tty(void) {
57  // Sometimes the final output to TTY can be lost (at least on FreeBSD).
58  // Call tcdrain() to ensure all output has been transmitted to host terminal.
59  tcdrain(STDOUT_FILENO);
60  tcdrain(STDERR_FILENO);
61}
62#endif
63
64int main(int argc, char **argv)
65{
66#ifdef MSWIN
67  SetConsoleOutputCP(CP_UTF8);
68#else
69  atexit(drain_tty);
70#endif
71
72  if (argc == 2 && strcmp(argv[1], "--help") == 0) {
73    help();
74  }
75
76  if (argc >= 2) {
77    if (strcmp(argv[1], "-t") == 0) {
78      if (argc < 3) {
79        fprintf(stderr, "Missing prompt text for -t option\n");
80        return 5;
81      } else {
82        fprintf(stderr, "%s $ ", argv[2]);
83        if (argc >= 5 && (strcmp(argv[3], "EXE") == 0)) {
84          fprintf(stderr, "%s\n", argv[4]);
85        }
86      }
87    } else if (strcmp(argv[1], "EXE") == 0) {
88      if (argc >= 3) {
89        fprintf(stderr, "ready $ %s\n", argv[2]);
90      } else {
91        fprintf(stderr, "ready $ ");
92      }
93#ifndef MSWIN
94    } else if (strcmp(argv[1], "EXECVP") == 0) {
95      if (argc < 4) {
96        fprintf(stderr, "Not enough arguments for EXECVP\n");
97        return 6;
98      }
99      execvp(argv[2], argv + 3);
100#endif
101    } else if (strcmp(argv[1], "REP") == 0 || strcmp(argv[1], "REPFAST") == 0) {
102      bool fast = strcmp(argv[1], "REPFAST") == 0;
103      if (argc != 4) {
104        fprintf(stderr, "REP/REPFAST expects exactly 3 arguments\n");
105        return 4;
106      }
107      int count = 0;
108      if (sscanf(argv[2], "%d", &count) != 1) {
109        fprintf(stderr, "Invalid count: %s\n", argv[2]);
110        return 4;
111      }
112      for (int i = 0; i < count; i++) {
113        printf("%d: %s\n", i, argv[3]);
114        if (!fast && i % 100 == 0) {
115          usleep(1000);  // Wait 1 ms (simulate typical output).
116        }
117        fflush(NULL);
118      }
119    } else if (strcmp(argv[1], "UTF-8") == 0) {
120      // test split-up UTF-8 sequence
121      printf("\xc3"); flush_wait();
122      printf("\xa5\n"); flush_wait();
123
124      // split up a 2+2 grapheme clusters all possible ways
125      printf("ref: \xc3\xa5\xcc\xb2\n"); flush_wait();
126
127      printf("1: \xc3"); flush_wait();
128      printf("\xa5\xcc\xb2\n"); flush_wait();
129
130      printf("2: \xc3\xa5"); flush_wait();
131      printf("\xcc\xb2\n"); flush_wait();
132
133      printf("3: \xc3\xa5\xcc"); flush_wait();
134      printf("\xb2\n"); flush_wait();
135    } else if (strcmp(argv[1], "INTERACT") == 0) {
136      char input[256];
137      char cmd[100];
138      int arg;
139
140      while (true) {
141        fprintf(stderr, "interact $ ");
142
143        if (fgets(input, sizeof(input), stdin) == NULL) {
144          break;  // EOF
145        }
146
147        if (1 == sscanf(input, "%99s %d", cmd, &arg)) {
148          arg = 0;
149        }
150        if (strcmp(cmd, "exit") == 0) {
151          return arg;
152        } else {
153          fprintf(stderr, "command not found: %s\n", cmd);
154        }
155      }
156    } else if (strcmp(argv[1], "HOLD") == 0) {
157      fprintf(stderr, "holding $ ");
158      fflush(NULL);
159      while (true) {
160#ifdef MSWIN
161        Sleep(1000);
162#else
163        pause();
164#endif
165      }
166    } else if (strcmp(argv[1], "EXIT") == 0) {
167      int code = 1;
168      if (argc >= 3) {
169        if (sscanf(argv[2], "%d", &code) != 1) {
170          fprintf(stderr, "Invalid exit code: %s\n", argv[2]);
171          return 2;
172        }
173      }
174      return code;
175    } else {
176      fprintf(stderr, "Unknown first argument: %s\n", argv[1]);
177      return 3;
178    }
179    fflush(NULL);
180    return 0;
181  } else if (argc == 1) {
182    fprintf(stderr, "ready $ ");
183    return 0;
184  } else {
185    fprintf(stderr, "Missing first argument\n");
186    return 2;
187  }
188}