diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | LICENSE | 2 | ||||
-rw-r--r-- | Makefile | 34 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | plaintext.c | 59 |
5 files changed, 99 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84f5558 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +plaintext @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Jakob Kaivo +Copyright (c) 2023 Jakob Kaivo Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b541a2b --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +.POSIX: + +# This Makefile was generated by maje +# See https://src.kaivo.net/dev/maje/ for more information +# Do not edit this Makefile by hand + +CC=c99 +LD=$(CC) +CFLAGS=-Wall -Wextra -Wpedantic -Werror -g +LDFLAGS= +LDLIBS= +SRCDIR=. +OBJDIR=. +BINDIR=$(OBJDIR) +LIBDIR=$(OBJDIR) +DESTDIR=/usr/local + +all: $(BINDIR)/plaintext + +clean: + rm -f $(BINDIR)/plaintext $(OBJDIR)/*.o + +install: $(BINDIR)/plaintext + mkdir -p $(DESTDIR)/bin + cp $(BINDIR)/plaintext $(DESTDIR)/bin + +$(BINDIR)/plaintext: $(OBJDIR)/plaintext.o +$(OBJDIR)/plaintext.o: $(SRCDIR)/plaintext.c + @mkdir -p $(@D) + $(CC) $(CFLAGS) -o $@ -c $(SRCDIR)/plaintext.c + +$(BINDIR)/plaintext: + @mkdir -p $(@D) + $(LD) $(LDFLAGS) -o $@ $(OBJDIR)/*.o $(LDLIBS) @@ -1 +1,3 @@ -Inspired by https://no-color.org/
\ No newline at end of file +Strips control characters and ECMA-48 escape sequences from text. + +Inspired by https://no-color.org/ diff --git a/plaintext.c b/plaintext.c new file mode 100644 index 0000000..a107317 --- /dev/null +++ b/plaintext.c @@ -0,0 +1,59 @@ +#define _POSIX_C_SOURCE 2 +#include <ctype.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +static int plaintext(const char *path) +{ + FILE *f = stdin; + if (path && strcmp(path, "-") != 0) { + f = fopen(path, "rb"); + if (f == NULL) { + perror(path); + return 1; + } + } + + int c = 0; + while ((c = fgetc(f)) != EOF) { + if (c == '\033') { + c = fgetc(f); /* read '[' */ + c = '\033'; + while (c != EOF && ! (0x40 <= c && c <= 0x7e)) { + c = fgetc(f); + } + continue; + } + if (iscntrl(c) && !isspace(c)) { + continue; + } + putchar(c); + } + + if (f != stdin) { + fclose(f); + } + return 0; +} + +int main(int argc, char *argv[]) +{ + int c; + while ((c = getopt(argc, argv, "")) != -1) { + switch (c) { + default: + return 1; + } + } + + if (optind >= argc) { + return plaintext(NULL); + } + + int r = 0; + for (int i = optind; i < argc; i++) { + r |= plaintext(argv[i]); + } + return r; +} |