1
0

fix runlength building

This commit is contained in:
dtb 2022-06-18 06:28:15 -04:00
parent 57ea5c20b8
commit d76402418c
3 changed files with 27 additions and 30 deletions

3
.gitignore vendored
View File

@ -4,9 +4,12 @@ bin/id
bin/lowercase bin/lowercase
bin/nonzero bin/nonzero
bin/nutshell bin/nutshell
bin/rldecode
bin/rlencode
bin/roll bin/roll
bin/simexec bin/simexec
bin/sleep bin/sleep
bin/str
bin/streq bin/streq
bin/sysexits bin/sysexits
build/* build/*

View File

@ -86,8 +86,15 @@ roll.o: lib/libio.h src/roll.c sysexits
roll: libio sysexits roll.o roll: libio sysexits roll.o
$(CC) $(CFLAGS) -o bin/roll build/libio.o build/roll.o $(CC) $(CFLAGS) -o bin/roll build/libio.o build/roll.o
runlength: sysexits src/runlength.c usefulmacros rldecode.o: sysexits src/runlength.c
$(CC) $(CFLAGS) -o bin/runlength src/runlength.c $(CC) $(CFLAGS) -Df=decode -c -o build/rldecode.o src/runlength.c
rlencode.o: sysexits src/runlength.c
$(CC) $(CFLAGS) -Df=encode -c -o build/rlencode.o src/runlength.c
runlength: rldecode.o rlencode.o
$(CC) $(CFLAGS) -o bin/rldecode build/rldecode.o
$(CC) $(CFLAGS) -o bin/rlencode build/rlencode.o
simexec.o: libio sysexits src/simexec.c simexec.o: libio sysexits src/simexec.c
$(CC) $(CFLAGS) -c -o build/simexec.o src/simexec.c $(CC) $(CFLAGS) -c -o build/simexec.o src/simexec.c

View File

@ -2,8 +2,6 @@
#include <stdio.h> /* fprintf(3), getc(3), putc(3) */ #include <stdio.h> /* fprintf(3), getc(3), putc(3) */
#include <stdlib.h> /* stdin, stderr, stdout */ #include <stdlib.h> /* stdin, stderr, stdout */
#include <sysexits.h> /* EX_DATAERR, EX_OK */ #include <sysexits.h> /* EX_DATAERR, EX_OK */
#include <unistd.h> /* for noargvzero.h */
#include "noargvzero.h"
/* Unicode compatibility coming someday */ /* Unicode compatibility coming someday */
#define GETC getc #define GETC getc
@ -15,7 +13,7 @@
/* if(STRICT) just print it out */ /* if(STRICT) just print it out */
#define STRICT 0 #define STRICT 0
int decode(FILE *input, FILE *output){ static int decode(FILE *input, FILE *output){
int c[2]; int c[2];
unsigned int num; unsigned int num;
enum{NUMBER_PARSING = 1} state; enum{NUMBER_PARSING = 1} state;
@ -43,10 +41,10 @@ int decode(FILE *input, FILE *output){
c[0] = c[1]; c[0] = c[1];
} }
if(state == NUMBER_PARSING && !STRICT){ if(state == NUMBER_PARSING && !STRICT)
/* it doesn't make sense to put this in a loop */ /* it doesn't make sense to put this in a loop */
PUTC(c[0], output); PUTC(c[0], output); { PUTC(c[0], output); PUTC(c[0], output); }
}else if(state == NUMBER_PARSING) else if(state == NUMBER_PARSING)
return EX_DATAERR; return EX_DATAERR;
else if(c[0] != EOF) else if(c[0] != EOF)
PUTC(c[0], output); PUTC(c[0], output);
@ -54,7 +52,7 @@ int decode(FILE *input, FILE *output){
return EX_OK; return EX_OK;
} }
int encode(FILE *input, FILE *output){ static int encode(FILE *input, FILE *output){
int c[2]; int c[2];
unsigned int num; unsigned int num;
enum{COUNTING = 1} state; enum{COUNTING = 1} state;
@ -89,32 +87,21 @@ next: if(c[1] == EOF)
} }
} }
int (*procedures[2])(FILE *input, FILE *output) = {decode, encode};
/* this is inelegant */
char procedure_names[2][7] = {"decode", "encode"};
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
int i; int r;
enum{DECODE = 0, ENCODE = 1, INVALID = 2} procedure;
procedure = 0;
int retval;
NOARGVZERO(argv); if(argc != 1){
fprintf(stderr,
if(argc != 2){ "Usage: %s\n",
usage: fprintf(stderr, "Usage: %s {decode,encode}\n", argv[0]); argv[0] == NULL ?
(f == decode ? "decode" : "encode")
: argv[0]
);
return EX_USAGE; return EX_USAGE;
} }
/* imperfect */ if((r = f(stdin, stdout)) == EX_DATAERR)
for(i = 0; argv[1][i] != '\0' || procedure_names[procedure][i] != '\0'; ++i)
if(argv[1][i] != procedure_names[procedure][i])
if(++procedure == INVALID)
goto usage;
if((retval = procedures[procedure](stdin, stdout)) == EX_DATAERR)
fprintf(stderr, "%s: syntax error.\n", argv[0]); fprintf(stderr, "%s: syntax error.\n", argv[0]);
return retval; return r;
} }