Commit: daf8b21c6e1a0c4f8a3f5d9cc11596033704da9b
Parent: bfbbaa9de151a42150add898b7900c837dc88837
Author: Randy Palamar
Date: Thu, 19 Aug 2021 15:22:16 -0600
workaround stdio buffering with oboerudata
this is not as pretty as piping data around like a chad but it works
until a better way is found.
Diffstat:
4 files changed, 87 insertions(+), 6 deletions(-)
diff --git a/Makefile b/Makefile
@@ -4,7 +4,7 @@ include config.mk
OBOERU_SRC = oboeru.c util.c
OBOERU_OBJ = $(OBOERU_SRC:.c=.o)
-default: oboeru oboeruhttp
+default: oboeru oboerudata oboeruhttp
config.h:
cp config.def.h $@
@@ -17,19 +17,24 @@ $(OBOERU_OBJ): config.h
oboeru: $(OBOERU_OBJ)
$(CC) -o $@ $(OBOERU_OBJ) $(LDFLAGS)
+oboerudata: oboerudata.go
+ go build -ldflags "$(GOLDFLAGS)" $@.go
oboeruhttp: oboeruhttp.go
go build -ldflags "$(GOLDFLAGS)" $@.go
-install: oboeru
+install: oboeru oboerudata oboeruhttp
mkdir -p $(PREFIX)/bin
cp oboeru $(PREFIX)/bin
- chmod 755 $(PREFIX)/bin/oboeru
+ cp oboerudata $(PREFIX)/bin
cp oboeruhttp $(PREFIX)/bin
+ chmod 755 $(PREFIX)/bin/oboeru
+ chmod 755 $(PREFIX)/bin/oboerudata
chmod 755 $(PREFIX)/bin/oboeruhttp
uninstall:
rm $(PREFIX)/bin/oboeru
+ rm $(PREFIX)/bin/oboerudata
rm $(PREFIX)/bin/oboeruhttp
clean:
- rm *.o oboeru oboeruhttp
+ rm *.o oboeru oboerudata oboeruhttp
diff --git a/oboeru.c b/oboeru.c
@@ -229,6 +229,8 @@ review_loop(Card *r[], const char *decks[], const char *fifo)
for (i = 0; i < n_reviews; i++) {
fprintf(stdout, "%s\t"CARDID"\n", decks[r[i]->deck], r[i]->id);
+ /* force a flush before blocking in open() */
+ fflush(stdout);
reply[0] = 0;
fd = open(fifo, O_RDONLY);
diff --git a/oboerudata.go b/oboerudata.go
@@ -0,0 +1,75 @@
+package main
+import (
+ "bufio"
+ "fmt"
+ "os"
+ "strings"
+ "strconv"
+ "time"
+)
+
+func usage() {
+ fmt.Fprintf(os.Stderr, "usage: %s fifo\n", os.Args[0])
+}
+
+func makemap(deck string) map[int]string {
+ f, err := os.OpenFile(deck + ".data", os.O_RDONLY, 0444)
+ defer f.Close()
+ if err != nil {
+ return nil
+ }
+
+ m := make(map[int]string)
+ r := bufio.NewScanner(f)
+ for r.Scan() {
+ str := strings.SplitN(r.Text(), "\t", 2)
+ key, _ := strconv.Atoi(str[0])
+ m[key] = str[1]
+ }
+ return m;
+}
+
+func wait_and_print(fifo string) {
+ m := make(map[string]map[int]string)
+ for {
+ f, err := os.OpenFile(fifo, os.O_RDONLY, os.ModeNamedPipe)
+ if err != nil {
+ break
+ }
+
+ r := bufio.NewScanner(f)
+ r.Scan()
+ if r.Text() == "" {
+ f.Close()
+ break
+ }
+
+ var key int
+ var deck string
+ fmt.Sscanf(r.Text(), "%s\t%d", &deck, &key)
+
+ _, ok := m[deck]
+ if !ok {
+ split := strings.Split(deck, ".")
+ m[deck] = makemap(split[0])
+ }
+
+ str, _ := m[deck][key]
+
+ fmt.Fprintln(os.Stdout, str)
+
+ f.Close()
+ time.Sleep(50 * time.Millisecond)
+ }
+}
+
+func main() {
+ if len(os.Args) != 2 {
+ usage()
+ os.Exit(1)
+ }
+
+ fifo := os.Args[1]
+
+ wait_and_print(fifo)
+}
diff --git a/oboeruhttp.go b/oboeruhttp.go
@@ -79,9 +79,8 @@ func response(w http.ResponseWriter, r *http.Request) {
}
func serve(w http.ResponseWriter, r *http.Request) {
- var card []string
if cardlock == false {
- card = get_next_card()
+ card := get_next_card()
if len(card) != 2 {
quit(w)
}