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