From 261f2d46d79788e8cc9c4c93d3e7e66abc5507fc Mon Sep 17 00:00:00 2001 From: dtb Date: Mon, 12 Sep 2022 23:28:28 -0400 Subject: [PATCH] separate out rot13(1) --- .gitignore | 1 + Makefile | 8 ++-- rot13/Makefile | 26 ++++++++++++ rot13/rot13.c | 68 +++++++++++++++++++++++++++++ src/rot13.c | 41 ------------------ src/xml.c | 113 ------------------------------------------------- 6 files changed, 99 insertions(+), 158 deletions(-) create mode 100644 rot13/Makefile create mode 100644 rot13/rot13.c delete mode 100644 src/rot13.c delete mode 100644 src/xml.c diff --git a/.gitignore b/.gitignore index 2023dfa..503d9e0 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,5 @@ bin/sysexits build/* include/sysexits.h lib/*.o +rot13/rot13 src/*.o diff --git a/Makefile b/Makefile index ef93e1d..91fac8e 100644 --- a/Makefile +++ b/Makefile @@ -118,11 +118,11 @@ roll_stdio.o: lib/libio.h src/roll.c sysexits roll_stdio: libio roll_stdio.o $(CC) $(CFLAGS) -o bin/roll_stdio build/libio.o build/roll_stdio.o -rot13.o: libio sysexits src/rot13.c - $(CC) $(CFLAGS) -c -o build/rot13.o src/rot13.c +rot13: rot13/rot13 + mv rot13/rot13 bin/rot13 -rot13: libio rot13.o - $(CC) $(CFLAGS) -o bin/rot13 build/libio.o build/rot13.o +rot13/rot13: sysexits rot13/rot13.c rot13/Makefile + $(MAKE) -C rot13 sane rldecode.o: sysexits src/runlength.c $(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c diff --git a/rot13/Makefile b/rot13/Makefile new file mode 100644 index 0000000..6a0bab6 --- /dev/null +++ b/rot13/Makefile @@ -0,0 +1,26 @@ +all: rot13 + +clean: + rm -rf ../dist/rot13 ../dist/rot13.tar ../dist/rot13.tar.gz rot13 + +dist: ../dist/rot13.tar.gz + +sane: rot13.c ../include/sysexits.h + $(CC) -DDONT_USE_SYSTEM_SYSEXITS -o rot13 rot13.c +rot13: rot13.c + $(CC) -o rot13 rot13.c + +../dist/rot13: rot13 + mkdir -p ../dist/rot13.tmp/bin/ + cp rot13 ../dist/rot13.tmp/bin/rot13 + mv ../dist/rot13.tmp ../dist/rot13 + +../dist/rot13.tar: ../dist/rot13 + cd ../dist/rot13 && pax -w -x ustar . >../rot13.tar.tmp + mv ../dist/rot13.tar.tmp ../dist/rot13.tar + +../dist/rot13.tar.gz: ../dist/rot13.tar + gzip -c <../dist/rot13.tar >../dist/rot13.tar.gz.tmp + mv ../dist/rot13.tar.gz.tmp ../dist/rot13.tar.gz + +.PHONY: all clean sane diff --git a/rot13/rot13.c b/rot13/rot13.c new file mode 100644 index 0000000..08915be --- /dev/null +++ b/rot13/rot13.c @@ -0,0 +1,68 @@ +#include /* stdin, stdout */ + +/* When rot13(1) is a part of ~trinity/src, which doesn't assume the system has + * and generates it out of a table. */ +#ifdef DONT_USE_SYSTEM_SYSEXITS +# include "../include/sysexits.h" /* EX_OK, EX_USAGE */ +#else +# include /* EX_OK, EX_USAGE */ +#endif + +#ifdef U_UNICODE_VERSION /* libicu Unicode support */ +# define CHARACTER UChar32 + +# include /* u_isupper(3) */ +# define ISUPPER u_isupper + +# include /* U_EOF */ +# define ENDOFFILE U_EOF +#else /* ifdef U_UNICODE_VERSION */ +# define CHARACTER int + +# include /* isalpha(3), isupper(3) */ +# define ISALPHA isalpha +# define ISUPPER isupper + +# include /* getc(3), putc(3) */ +# define ENDOFFILE EOF +# define GETC getc +# define PUTC putc +#endif /* ifdef U_UNICODE_VERSION */ + +static char *program_name = "rot13"; + +// /* Assumes this is a character in the provided glyph system. +// * Probably not Unicode-capable but doesn't assume Alphabet. +// * Returns `c` rotated around `a` by `r` characters. */ +// #include /* strchr(3), strlen(3) */ +// static int +// caesar(char c, int r, const char *a){ +// return a[(strchr(a, c) - a + r) % strlen(a)]; +// } + +/* Or... make some static arrays of the alphabet * 1.5, so c + '\0' + 13 is + * always a valid index and always reflects the correct rotation (13 places). + */ +static char alpha_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM"; +static char alpha_lower[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"; + +int main(int argc, char *argv[]){ + char *a; /* easier than doing freaky math */ + CHARACTER c; + + if(argc > 1){ + fprintf(stderr, "Usage: %s\n", + argv[0] == NULL ? program_name : argv[0] + ); + return EX_USAGE; + } + + while((c = getc(stdin)) != EOF) + if(isalpha(c)){ + a = isupper(c) ? alpha_upper : alpha_lower; + putc(a[c - a[0] + 13], stdout); + }else + putc(c, stdout); + + return EX_OK; +} diff --git a/src/rot13.c b/src/rot13.c deleted file mode 100644 index cc99dc1..0000000 --- a/src/rot13.c +++ /dev/null @@ -1,41 +0,0 @@ -#include /* isalpha(3), isupper(3) */ -#include /* getc(3), putc(3) */ -#include /* stdin, stdout, EOF */ -#include /* strchr(3), strlen(3) */ -#include /* EX_OK, EX_USAGE */ -#include /* write(2) */ -#include "libio.h" /* fdputs(3) */ -#include "usefulmacros.h" /* ARRAYLEN */ - -static char *program_name = "rot13"; - -/* Assumes this is a character in the provided glyph system. - * Probably not Unicode-capable but doesn't assume Alphabet. - * Returns `c` rotated around `a` by `r` characters. */ -static int -caesar(char c, int r, const char *a){ - return a[(strchr(a, c) - a + r) % strlen(a)]; -} - -/* It's faster to assume Alphabet. */ -static char alpha_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM"; -static char alpha_lower[] = "abcdefghijklmnopqrstuvwxyzabcdefghijklm"; - -int main(int argc, char *argv[]){ - char *a; - int c; - - if(argc > 1){ - write(2, "Usage: ", 7); - fdputs(2, argv[0] == NULL ? program_name : argv[0]); - return EX_USAGE; - } - - while((c = getc(stdin)) != EOF){ - if(isalpha(c)){ - a = isupper(c) ? alpha_upper : alpha_lower; - putc(a[c - a[0] + 13], stdout); - }else - putc(c, stdout); - } -} diff --git a/src/xml.c b/src/xml.c deleted file mode 100644 index 18ed7ea..0000000 --- a/src/xml.c +++ /dev/null @@ -1,113 +0,0 @@ -#include -#include -#include -#include -#include "arraylen.h" -#include "bool.h" - -#define TAGSTART '<' -#define TAGEND '>' -#define ETAG '/' - -#define LF '\n' - -/* - start-tag end-tag - | | - V V - CONTENT - ^ ^ ^ - | | | - | TAGEND ETAG - | - TAGSTART - - empty-element tag - | - | optional whitespace - | | - | | TAGEND - | | | - V V V - - ^ ^ - | | - | ETAG - | - TAGSTART - -*/ - -/* a lot of this program's design is weird and kind of overengineered. - this is to drastically limit the memory used. */ - - -enum state{ - error = -1, /* for future use */ - init = 0, /* waiting for a root tag */ - starttagreading, - tagreading, - eetagreading, /* throw away everything until state change */ - endtagreading, - contentreading /* printing */ -}; - -void -usage(char *name){ - fprintf(stdout, "Usage: %s [tag...]\n", name); - return; -} - -int main(int argc, char *argv[]){ - if(argc < 2){ - usage(argv[0]); - return 0; - } - - bool enable = 0; - char c; - int linenumber = 0; - enum state s = init; - FILE *input = stdin; - FILE *output = stdout; - int level = 1; /* 1-indexed because args start at 1 */ - size_t p = 0; - - while(c = getc(input) != EOF){ - if(c == LF){ - ++linenumber; - continue; - } - switch(s){ - case init: - if(c == TAGSTART) - s = starttagreading; - break; - case starttagreading: - if(c == ETAG) - s = endtagreading; - else if(!isblank(c)) - s = tagreading; - break; - case tagreading: - if(c == ETAG) - s = eetagreading; - break; - /* case eetagreading: */ - case endtagreading: - if(c == ETAG) - fprintf(stderr, "%s:%d: Stray \'%c\' in an end tag?\n", argv[0], linenumber, ETAG); - break; - case contentreading: - if(enable) - putc(c, output); - else if(c == TAGSTART) - s = starttagreading; - else if(c == TAGEND) - fprintf(stderr, "%s:%d: Possible state error or XML syntax error; \'%c\' in mode contentreading\n", argv[0], linenumber, TAGEND); - break; - } - } - - return 0; -}