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 
  34 #ifdef RED_ERRORS
  35 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  36 #ifdef __APPLE__
  37 #define ERROR_STYLE "\x1b[31m"
  38 #endif
  39 #define RESET_STYLE "\x1b[0m"
  40 #else
  41 #define ERROR_STYLE
  42 #define RESET_STYLE
  43 #endif
  44 
  45 #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n")
  46 
  47 /*
  48 const char* info = ""
  49 "alterecho [words...]\n"
  50 "\n"
  51 "Join all words given as arguments using single spaces, showing the result\n"
  52 "in an alternate screen. Press any key to quit.\n"
  53 "";
  54 */
  55 
  56 int main(int argc, char** argv) {
  57     FILE* in = fopen("/dev/tty", "rb");
  58     if (in == NULL) {
  59         fprintf(stderr, ERROR_LINE("can't open tty for input"));
  60         return 1;
  61     }
  62 
  63     FILE* out = fopen("/dev/tty", "wb");
  64     if (out == NULL) {
  65         fclose(in);
  66         fprintf(stderr, ERROR_LINE("can't open tty for output"));
  67         return 1;
  68     }
  69 
  70     // use UTF-8
  71     setlocale(LC_ALL, "");
  72 
  73     // initscr takes over stdin and stdout, which interferes with being
  74     // used/run via xargs
  75     newterm("xterm", out, in);
  76 
  77     noecho(); // don't show keys pressed
  78     keypad(stdscr, TRUE); // handle all keys correctly
  79     set_escdelay(10); // don't wait long after the escape key is pressed
  80     curs_set(0); // hide the cursor
  81     refresh(); // switch to alternate screen immediately
  82 
  83     for (size_t i = 1; i < argc; i++) {
  84         printw((i > 1) ? " %s" : "%s", argv[i]);
  85     }
  86     if (argc > 1) {
  87         printw("\n");
  88     }
  89 
  90     refresh(); // ensure contents show up
  91     getch(); // wait for any keypress, even the escape key
  92     endwin(); // restore normal screen and settings
  93 
  94     fclose(out);
  95     fclose(in);
  96     return 0;
  97 }