From acf9238af56b7876048ba1178aa1b0f3949e3ffc Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sat, 4 Mar 2023 22:58:17 -0500 Subject: quick and dirty implementation --- .gitignore | 2 ++ LICENSE | 2 +- Makefile | 34 ++++++++++++++++++++++++++++++++++ README.md | 4 +++- plaintext.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 plaintext.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84f5558 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.o +plaintext diff --git a/LICENSE b/LICENSE index 46722ef..6dd526f 100644 --- a/LICENSE +++ b/LICENSE @@ -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) diff --git a/README.md b/README.md index 9b40b88..64a7208 100644 --- a/README.md +++ b/README.md @@ -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 +#include +#include +#include + +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; +} -- cgit v1.2.1