separate out rot13(1)
This commit is contained in:
parent
7441fa08ea
commit
261f2d46d7
1
.gitignore
vendored
1
.gitignore
vendored
@ -19,4 +19,5 @@ bin/sysexits
|
|||||||
build/*
|
build/*
|
||||||
include/sysexits.h
|
include/sysexits.h
|
||||||
lib/*.o
|
lib/*.o
|
||||||
|
rot13/rot13
|
||||||
src/*.o
|
src/*.o
|
||||||
|
8
Makefile
8
Makefile
@ -118,11 +118,11 @@ roll_stdio.o: lib/libio.h src/roll.c sysexits
|
|||||||
roll_stdio: libio roll_stdio.o
|
roll_stdio: libio roll_stdio.o
|
||||||
$(CC) $(CFLAGS) -o bin/roll_stdio build/libio.o build/roll_stdio.o
|
$(CC) $(CFLAGS) -o bin/roll_stdio build/libio.o build/roll_stdio.o
|
||||||
|
|
||||||
rot13.o: libio sysexits src/rot13.c
|
rot13: rot13/rot13
|
||||||
$(CC) $(CFLAGS) -c -o build/rot13.o src/rot13.c
|
mv rot13/rot13 bin/rot13
|
||||||
|
|
||||||
rot13: libio rot13.o
|
rot13/rot13: sysexits rot13/rot13.c rot13/Makefile
|
||||||
$(CC) $(CFLAGS) -o bin/rot13 build/libio.o build/rot13.o
|
$(MAKE) -C rot13 sane
|
||||||
|
|
||||||
rldecode.o: sysexits src/runlength.c
|
rldecode.o: sysexits src/runlength.c
|
||||||
$(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c
|
$(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c
|
||||||
|
26
rot13/Makefile
Normal file
26
rot13/Makefile
Normal file
@ -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
|
68
rot13/rot13.c
Normal file
68
rot13/rot13.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include <stddef.h> /* stdin, stdout */
|
||||||
|
|
||||||
|
/* When rot13(1) is a part of ~trinity/src, which doesn't assume the system has
|
||||||
|
* <sysexits.h> and generates it out of a table. */
|
||||||
|
#ifdef DONT_USE_SYSTEM_SYSEXITS
|
||||||
|
# include "../include/sysexits.h" /* EX_OK, EX_USAGE */
|
||||||
|
#else
|
||||||
|
# include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef U_UNICODE_VERSION /* libicu Unicode support */
|
||||||
|
# define CHARACTER UChar32
|
||||||
|
|
||||||
|
# include <uchar.h> /* u_isupper(3) */
|
||||||
|
# define ISUPPER u_isupper
|
||||||
|
|
||||||
|
# include <ustdio.h> /* U_EOF */
|
||||||
|
# define ENDOFFILE U_EOF
|
||||||
|
#else /* ifdef U_UNICODE_VERSION */
|
||||||
|
# define CHARACTER int
|
||||||
|
|
||||||
|
# include <ctype.h> /* isalpha(3), isupper(3) */
|
||||||
|
# define ISALPHA isalpha
|
||||||
|
# define ISUPPER isupper
|
||||||
|
|
||||||
|
# include <stdio.h> /* 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 <string.h> /* 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;
|
||||||
|
}
|
41
src/rot13.c
41
src/rot13.c
@ -1,41 +0,0 @@
|
|||||||
#include <ctype.h> /* isalpha(3), isupper(3) */
|
|
||||||
#include <stdio.h> /* getc(3), putc(3) */
|
|
||||||
#include <stddef.h> /* stdin, stdout, EOF */
|
|
||||||
#include <string.h> /* strchr(3), strlen(3) */
|
|
||||||
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
|
||||||
#include <unistd.h> /* 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);
|
|
||||||
}
|
|
||||||
}
|
|
113
src/xml.c
113
src/xml.c
@ -1,113 +0,0 @@
|
|||||||
#include <ctype.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "arraylen.h"
|
|
||||||
#include "bool.h"
|
|
||||||
|
|
||||||
#define TAGSTART '<'
|
|
||||||
#define TAGEND '>'
|
|
||||||
#define ETAG '/'
|
|
||||||
|
|
||||||
#define LF '\n'
|
|
||||||
|
|
||||||
/*
|
|
||||||
start-tag end-tag
|
|
||||||
| |
|
|
||||||
V V
|
|
||||||
<TAG>CONTENT</TAG>
|
|
||||||
^ ^ ^
|
|
||||||
| | |
|
|
||||||
| TAGEND ETAG
|
|
||||||
|
|
|
||||||
TAGSTART
|
|
||||||
|
|
||||||
empty-element tag
|
|
||||||
|
|
|
||||||
| optional whitespace
|
|
||||||
| |
|
|
||||||
| | TAGEND
|
|
||||||
| | |
|
|
||||||
V V V
|
|
||||||
<TAG />
|
|
||||||
^ ^
|
|
||||||
| |
|
|
||||||
| 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;
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user