File: teletype.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 ./teletype ./teletype.c
  29 */
  30 
  31 #include <stdbool.h>
  32 #include <stdio.h>
  33 #include <string.h>
  34 #include <time.h>
  35 
  36 #ifdef _WIN32
  37 #include <windows.h>
  38 #endif
  39 
  40 #ifdef RED_ERRORS
  41 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  42 #ifdef __APPLE__
  43 #define ERROR_STYLE "\x1b[31m"
  44 #endif
  45 #define RESET_STYLE "\x1b[0m"
  46 #else
  47 #define ERROR_STYLE
  48 #define RESET_STYLE
  49 #endif
  50 
  51 #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n")
  52 
  53 const char* info = ""
  54 "teletype [options...] [filenames...]\n"
  55 "\n"
  56 "Simulate the cadence of old-fashioned teletype machines by delaying output.\n"
  57 "\n"
  58 "\n"
  59 "Options\n"
  60 "\n"
  61 "    -h, --h            show this help message\n"
  62 "    -help, --help      aliases for option -h\n"
  63 "";
  64 
  65 // handle_reader shows all bytes read from the source given as colored hex
  66 // values, showing offsets and ASCII symbols on the sides of each output line
  67 void handle_reader(FILE* w, FILE* src) {
  68     struct timespec delay;
  69     delay.tv_sec = 0;
  70     // delay.tv_nsec = 0;
  71 
  72     while (!feof(w)) {
  73         int c = getc(src);
  74         if (c < 0) {
  75             break;
  76         }
  77 
  78         delay.tv_nsec = (c == '\n') ? 750 * 1e6 : 15 * 1e6;
  79         nanosleep(&delay, NULL);
  80         fputc(c, w);
  81     }
  82 }
  83 
  84 // handle_file handles data from the filename given; returns false only when
  85 // the file can't be opened
  86 bool handle_file(FILE* w, const char* path) {
  87     FILE* f = fopen(path, "rb");
  88     if (f == NULL) {
  89         fputc('\n', w);
  90         fprintf(stderr, ERROR_LINE("can't open file named '%s'"), path);
  91         return false;
  92     }
  93 
  94     handle_reader(w, f);
  95     fclose(f);
  96     return true;
  97 }
  98 
  99 // is_help_option simplifies control-flow for func run
 100 bool is_help_option(const char* s) {
 101     return (s[0] == '-') && (
 102         strcmp(s, "-h") == 0 ||
 103         strcmp(s, "-help") == 0 ||
 104         strcmp(s, "--h") == 0 ||
 105         strcmp(s, "--help") == 0
 106     );
 107 }
 108 
 109 // run returns the number of errors
 110 int run(int argc, char** argv, FILE* w) {
 111     size_t files = 0;
 112     size_t errors = 0;
 113 
 114     // handle all filenames/options given
 115     for (size_t i = 1; i < argc && !feof(w); i++) {
 116         // a `-` filename stands for the standard input
 117         if (argv[i][0] == '-' && argv[i][1] == 0) {
 118             handle_reader(w, stdin);
 119             continue;
 120         }
 121 
 122         if (!handle_file(w, argv[i])) {
 123             errors++;
 124         }
 125         files++;
 126     }
 127 
 128     // no filenames means use stdin as the only input
 129     if (files == 0) {
 130         handle_reader(w, stdin);
 131     }
 132 
 133     return errors;
 134 }
 135 
 136 int main(int argc, char** argv) {
 137 #ifdef _WIN32
 138     setmode(fileno(stdin), O_BINARY);
 139     // ensure output lines end in LF instead of CRLF on windows
 140     setmode(fileno(stdout), O_BINARY);
 141     setmode(fileno(stderr), O_BINARY);
 142 #endif
 143 
 144     if (argc > 1 && is_help_option(argv[1])) {
 145         fprintf(stderr, "%s", info);
 146         return 0;
 147     }
 148 
 149     // disable buffering for stdout
 150     setvbuf(stdout, NULL, _IONBF, 0);
 151 
 152     return run(argc, argv, stdout) == 0 ? 0 : 1;
 153 }