File: detrail.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 -march=native -mtune=native -flto -o ./detrail ./detrail.c
  29 */
  30 
  31 #include <stdbool.h>
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <string.h>
  35 #include <unistd.h>
  36 
  37 #ifdef _WIN32
  38 #include <fcntl.h>
  39 #include <windows.h>
  40 #endif
  41 
  42 #ifdef RED_ERRORS
  43 #define ERROR_STYLE "\x1b[38;2;204;0;0m"
  44 #ifdef __APPLE__
  45 #define ERROR_STYLE "\x1b[31m"
  46 #endif
  47 #define RESET_STYLE "\x1b[0m"
  48 #else
  49 #define ERROR_STYLE
  50 #define RESET_STYLE
  51 #endif
  52 
  53 #define ERROR_LINE(MSG) (ERROR_STYLE MSG RESET_STYLE "\n")
  54 
  55 #define BAD_ALLOC 2
  56 
  57 const char* info = ""
  58 "detrail [filenames...]\n"
  59 "\n"
  60 "Ignore trailing spaces and/or carriage-returns from text lines.\n"
  61 "";
  62 
  63 // slice is a growable region of bytes in memory
  64 typedef struct slice {
  65     // ptr is the starting place of the region
  66     unsigned char* ptr;
  67 
  68     // cap is how many bytes the memory region has available
  69     size_t cap;
  70 } slice;
  71 
  72 bool starts_with_bom(const unsigned char* b, const size_t n) {
  73     return (n >= 3 && b[0] == 0xef && b[1] == 0xbb && b[2] == 0xbf);
  74 }
  75 
  76 void handle_reader(FILE* w, FILE* r, slice* line, bool live_lines) {
  77     for (size_t i = 0; !feof(w); i++) {
  78         ssize_t len = getline((char**)&line->ptr, &line->cap, r);
  79         if (line->ptr == NULL) {
  80             fprintf(stderr, "\n");
  81             fprintf(stderr, ERROR_LINE("out of memory"));
  82             exit(BAD_ALLOC);
  83         }
  84 
  85         if (len < 0) {
  86             break;
  87         }
  88 
  89         unsigned char* ptr = line->ptr;
  90 
  91         // get rid of leading UTF-8 BOM (byte-order mark) if 1st line has it
  92         if (i == 0 && starts_with_bom(ptr, len)) {
  93             ptr += 3;
  94             len -= 3;
  95         }
  96 
  97         // replace trailing carriage-returns with line-feeds
  98         if (len >= 1 && ptr[len - 1] == '\r') {
  99             ptr[len - 1] = '\n';
 100         }
 101 
 102         // get rid of carriage-returns preceding line-feeds
 103         if (len >= 2 && ptr[len - 2] == '\r' && ptr[len - 1] == '\n') {
 104             ptr[len - 2] = '\n';
 105             len--;
 106         }
 107 
 108         // ignore trailing spaces
 109         while (len > 0 && ptr[len - 1] == ' ') {
 110             len--;
 111         }
 112 
 113         fwrite(ptr, 1, len, w);
 114         if (len < 1 || ptr[len - 1] != '\n') {
 115             fputc('\n', w);
 116         }
 117         if (live_lines) {
 118             fflush(w);
 119         }
 120     }
 121 
 122     if (!live_lines) {
 123         fflush(w);
 124     }
 125 }
 126 
 127 // handle_file handles data from the filename given; returns false only when
 128 // the file can't be opened
 129 bool handle_file(FILE* w, const char* path, slice* line, bool live_lines) {
 130     FILE* f = fopen(path, "rb");
 131     if (f == NULL) {
 132         fprintf(stderr, ERROR_LINE("can't open file named '%s'"), path);
 133         return false;
 134     }
 135 
 136     handle_reader(w, f, line, live_lines);
 137     fclose(f);
 138     return true;
 139 }
 140 
 141 // run returns the number of errors
 142 int run(int argc, char** argv, FILE* w, bool live_lines) {
 143     size_t dashes = 0;
 144     for (int i = 1; i < argc; i++) {
 145         if (strcmp(argv[i], "-") == 0) {
 146             dashes++;
 147         }
 148     }
 149 
 150     if (dashes > 1) {
 151         const char* m = "can't use the standard input (dash) more than once";
 152         fprintf(stderr, ERROR_LINE("%s"), m);
 153         return 1;
 154     }
 155 
 156     slice line;
 157     line.cap = 32 * 1024;
 158     line.ptr = malloc(line.cap);
 159 
 160     if (line.ptr == NULL) {
 161         fprintf(stderr, ERROR_LINE("out of memory"));
 162         exit(BAD_ALLOC);
 163     }
 164 
 165     size_t errors = 0;
 166 
 167     for (int i = 1; i < argc && !feof(w); i++) {
 168         if (strcmp(argv[i], "-") == 0) {
 169             handle_reader(w, stdin, &line, live_lines);
 170             continue;
 171         }
 172 
 173         if (!handle_file(w, argv[i], &line, live_lines)) {
 174             errors++;
 175         }
 176     }
 177 
 178     // use stdin when not given any filepaths
 179     if (argc <= 1) {
 180         handle_reader(w, stdin, &line, live_lines);
 181     }
 182 
 183     free(line.ptr);
 184     return errors;
 185 }
 186 
 187 int main(int argc, char** argv) {
 188 #ifdef _WIN32
 189     setmode(fileno(stdin), O_BINARY);
 190     // ensure output lines end in LF instead of CRLF on windows
 191     setmode(fileno(stdout), O_BINARY);
 192     setmode(fileno(stderr), O_BINARY);
 193 #endif
 194 
 195     if (argc > 1) {
 196         if (
 197             strcmp(argv[1], "-h") == 0 ||
 198             strcmp(argv[1], "-help") == 0 ||
 199             strcmp(argv[1], "--h") == 0 ||
 200             strcmp(argv[1], "--help") == 0
 201         ) {
 202             fprintf(stdout, "%s", info);
 203             return 0;
 204         }
 205     }
 206 
 207     const bool live_lines = lseek(fileno(stdout), 0, SEEK_CUR) != 0;
 208     if (!live_lines) {
 209         setvbuf(stdout, NULL, _IOFBF, 0);
 210     }
 211     return run(argc, argv, stdout, live_lines) == 0 ? 0 : 1;
 212 }