File: echoff.c 1 /* 2 The MIT License (MIT) 3 4 Copyright © 2020-2025 pacman64 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy of 7 this software and associated documentation files (the “Software”), to deal 8 in the Software without restriction, including without limitation the rights to 9 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 10 of the Software, and to permit persons to whom the Software is furnished to do 11 so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 SOFTWARE. 23 */ 24 25 /* 26 You can build this command-line app by running 27 28 cc -Wall -s -O3 -flto -o ./echoff ./echoff.c -lncursesw 29 */ 30 31 #include <locale.h> 32 #include <ncurses.h> 33 #include <sys/stat.h> 34 #include <unistd.h> 35 36 #ifdef RED_ERRORS 37 #define ERROR_STYLE "\x1b[38;2;204;0;0m" 38 #ifdef __APPLE__ 39 #define ERROR_STYLE "\x1b[31m" 40 #endif 41 #define RESET_STYLE "\x1b[0m" 42 #else 43 #define ERROR_STYLE 44 #define RESET_STYLE 45 #endif 46 47 #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n") 48 49 /* 50 const char* info = "" 51 "alterecho [words...]\n" 52 "\n" 53 "Join all words given as arguments using single spaces, showing the result\n" 54 "in an alternate screen. Press any key to quit.\n" 55 ""; 56 */ 57 58 int main(int argc, char** argv) { 59 // if stdout is being piped, just act like the `echo` command 60 struct stat out_info; 61 if (fstat(STDOUT_FILENO, &out_info) == 0 && S_ISFIFO(out_info.st_mode)) { 62 for (size_t i = 1; i < argc; i++) { 63 if (i > 1) { 64 fputc(' ', stdout); 65 } 66 fprintf(stdout, "%s", argv[i]); 67 } 68 if (argc > 1) { 69 fputc('\n', stdout); 70 } 71 return 0; 72 } 73 74 FILE* in = fopen("/dev/tty", "rb"); 75 if (in == NULL) { 76 fprintf(stderr, ERROR_LINE("can't open tty for input")); 77 return 1; 78 } 79 80 FILE* out = fopen("/dev/tty", "wb"); 81 if (out == NULL) { 82 fclose(in); 83 fprintf(stderr, ERROR_LINE("can't open tty for output")); 84 return 1; 85 } 86 87 // use UTF-8 88 setlocale(LC_ALL, ""); 89 90 // initscr takes over stdin and stdout, which interferes with being 91 // used/run via xargs 92 newterm("xterm", out, in); 93 94 noecho(); // don't show keys pressed 95 keypad(stdscr, TRUE); // handle all keys correctly 96 set_escdelay(10); // don't wait long after the escape key is pressed 97 curs_set(0); // hide the cursor 98 refresh(); // switch to alternate screen immediately 99 100 for (size_t i = 1; i < argc; i++) { 101 if (i > 1) { 102 printw(" "); 103 } 104 printw("%s", argv[i]); 105 } 106 if (argc > 1) { 107 printw("\n"); 108 } 109 110 refresh(); // ensure contents show up 111 getch(); // wait for any keypress, even the escape key 112 endwin(); // restore normal screen and settings 113 114 fclose(out); 115 fclose(in); 116 return 0; 117 }