summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjonsykkel <jonrevold@gmail.com>2021-09-21 23:00:17 +0200
committerjonsykkel <jonrevold@gmail.com>2021-09-21 23:00:17 +0200
commitc6fdf6195b7a214acebcabf6c0e5862f427c2894 (patch)
tree84ded1b978d165388ea3e17a5191ebde2cc7f1f2
parent2d144d1895da417dad085f00f03ba106b6fe00e9 (diff)
downloadokeffa-c6fdf6195b7a214acebcabf6c0e5862f427c2894.tar.gz
rngolade
-rw-r--r--calc/main.c26
-rw-r--r--calc/rng.c21
-rw-r--r--calc/rng.h16
-rw-r--r--makefile3
-rwxr-xr-xprog/ch8.sh3
5 files changed, 62 insertions, 7 deletions
diff --git a/calc/main.c b/calc/main.c
index ecb0146..a47b016 100644
--- a/calc/main.c
+++ b/calc/main.c
@@ -1,4 +1,5 @@
#include "os.h"
+#include "rng.h"
#include <okeffa/word.h>
#include <okeffa/fz_lim.h>
#include <okeffa/fz_basic.h>
@@ -12,6 +13,8 @@
#include <ctype.h>
#include <assert.h>
+//#define STACK_TRACE_READABLE
+
#define STACK(I) (stack+fl*(I))
#define STACK_INIT ((size_t)-1)
#define STACK_SIZE 65536
@@ -28,6 +31,7 @@ static size_t pos;
static size_t quote_lev;
static size_t comm_lev;
static size_t cond_lev;
+static rng_t rng;
static void zap(void){
for(size_t x = 0;x < sh;x++){
@@ -86,11 +90,12 @@ static word_t char_getnib(char c){
else return (word_t)(c-'a')+0xA;
}
-static void puts_stack(size_t p){
+#ifdef STACK_TRACE_READABLE
printf("%3zu ",p);
dump_fz(STACK(p),fl);
write_nl();
}
+#endif
static void op_normal(char c){
switch(c){
@@ -275,6 +280,11 @@ static void op_normal(char c){
//other
////////////////////////////////////////////////////////////////
+ case '?': //push rngolade to stack
+ push();
+ rng_read(&rng,STACK(sp),fl);
+ break;
+
case 'U': //mUx
want(3);
wbool_t sel = fz_nzero(STACK(sp),fl);
@@ -290,8 +300,7 @@ static void op_normal(char c){
case '#': //print top item and drop it
want(1);
- //dump_fz(STACK(sp),fl);
- puts_stack(sp);
+ dump_fz(STACK(sp),fl);
drop();
break;
@@ -301,8 +310,11 @@ static void op_normal(char c){
case 'Q': //quit with stack trace
for(size_t x = sp;x != STACK_INIT;x--){
- //dump_fz(STACK(x),fl);
+#ifdef STACK_TRACE_READABLE
puts_stack(x);
+#else
+ dump_fz(STACK(x),fl);
+#endif
}
exit(0);
@@ -354,13 +366,15 @@ static void op(char c){
int main(int argc,char **argv){
size_t bitness;
- if(argc != 3){
- eggog("usage: ffa_calc bitness stack_height");
+ if(argc < 3 || argc > 4){
+ eggog("usage: ffa_calc width height [rng device]");
}
bitness = strtol(argv[1],0,10);
sh = strtol(argv[2],0,10);
+ rng_init(&rng,argc >= 4 ? argv[3] : RNG_PATH_DEFAULT);
+
if(!fz_valid_bitness(bitness)){
eggog("invalid bitness: "FZ_VALIDITY_RULE_DOC);
}
diff --git a/calc/rng.c b/calc/rng.c
new file mode 100644
index 0000000..74db66f
--- /dev/null
+++ b/calc/rng.c
@@ -0,0 +1,21 @@
+#include "rng.h"
+#include "os.h"
+#include <fcntl.h>
+#include <unistd.h>
+
+void rng_init(rng_t *rng,char const *path){
+ int fd = open(path,O_RDONLY);
+ if(fd == -1){
+ eggog("could not open rng at \"%s\"!",path);
+ }
+ rng->fd = fd;
+}
+
+void rng_read(rng_t *rng,word_t *o,size_t len){
+ ssize_t b = len*WORD_BYTENESS;
+ ssize_t r = read(rng->fd,o,b);
+ if(r < b){
+ eggog("could not read from rng!");
+ }
+}
+
diff --git a/calc/rng.h b/calc/rng.h
new file mode 100644
index 0000000..fe9381c
--- /dev/null
+++ b/calc/rng.h
@@ -0,0 +1,16 @@
+#ifndef OKEFFACALC_RNG_H
+#define OKEFFACALC_RNG_H
+
+#include <okeffa/word.h>
+
+#define RNG_PATH_DEFAULT "/dev/random"
+
+typedef struct rng{
+ int fd;
+}rng_t;
+
+void rng_init (rng_t *rng,char const *path);
+void rng_read (rng_t *rng,word_t *o,size_t len);
+
+#endif
+
diff --git a/makefile b/makefile
index 303cd91..d38dd30 100644
--- a/makefile
+++ b/makefile
@@ -66,7 +66,8 @@ out := $(bin_dir)/$(out)
ifneq ($(run),)
run_cmd := @echo "run $(out)" 1>&2
#run_cmd += && ./$(out) 256 16
- run_cmd += && time $(out) 2048 16 < prog/ch7
+ #run_cmd += && time $(out) 2048 16 < prog/ch7
+ run_cmd += && prog/ch8.sh $(out)
endif
ifneq ($(debug),)
diff --git a/prog/ch8.sh b/prog/ch8.sh
new file mode 100755
index 0000000..5115942
--- /dev/null
+++ b/prog/ch8.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+echo "?Q" | $1 4096 16