on master help sign in
Tinker with popen/poll
commit0fa92398519365ae05ae6a633606838f08c882a1 authorEdwin Brady <ecb10@st-andrews.ac.uk> dateThu 20 Feb 2014, 23:30 (13 years ago) parentb651aaf
4 files changed+46−1
libs/neweffects/Effect/StdIO.idr+6 libs/prelude/Prelude.idr+11−1 rts/idris_stdfgn.c+26 rts/idris_stdfgn.h+3
libs/neweffects/Effect/StdIO.idr+6
@@ -15,6 +15,12 @@ instance Handler StdIO IO where
1515 handle () (PutCh c) k = do putChar c; k () ()
1616 handle () GetCh k = do x <- getChar; k x ()
1717
18+instance Handler StdIO (IOExcept a) where
19+ handle () (PutStr s) k = do ioe_lift $ putStr s; k () ()
20+ handle () GetStr k = do x <- ioe_lift $ getLine; k x ()
21+ handle () (PutCh c) k = do ioe_lift $ putChar c; k () ()
22+ handle () GetCh k = do x <- ioe_lift $ getChar; k x ()
23+
1824--- The Effect and associated functions
1925
2026STDIO : EFFECT
libs/prelude/Prelude.idr+11−1
@@ -503,11 +503,17 @@ do_fread h = prim_fread h
503503fgetc : File -> IO Char
504504fgetc (FHandle h) = return (cast !(mkForeign (FFun "fgetc" [FPtr] FInt) h))
505505
506+fgetc' : File -> IO (Maybe Char)
507+fgetc' (FHandle h)
508+ = do x <- mkForeign (FFun "fgetc" [FPtr] FInt) h
509+ if (x < 0) then return Nothing
510+ else return (Just (cast x))
511+
506512fflush : File -> IO ()
507513fflush (FHandle h) = mkForeign (FFun "fflush" [FPtr] FUnit) h
508514
509515do_popen : String -> String -> IO Ptr
510−do_popen f m = mkForeign (FFun "popen" [FString, FString] FPtr) f m
516+do_popen f m = mkForeign (FFun "do_popen" [FString, FString] FPtr) f m
511517
512518popen : String -> Mode -> IO File
513519popen f m = do ptr <- do_popen f (modeStr m)
@@ -552,6 +558,10 @@ ferror : File -> IO Bool
552558ferror (FHandle h) = do err <- do_ferror h
553559 return (not (err == 0))
554560
561+fpoll : File -> IO Bool
562+fpoll (FHandle h) = do p <- mkForeign (FFun "fpoll" [FPtr] FInt) h
563+ return (p > 0)
564+
555565partial
556566nullPtr : Ptr -> IO Bool
557567nullPtr p = do ok <- mkForeign (FFun "isNull" [FPtr] FInt) p
rts/idris_stdfgn.c+26
@@ -1,5 +1,8 @@
11#include "idris_stdfgn.h"
22#include "idris_rts.h"
3+#include <sys/select.h>
4+#include <fcntl.h>
5+#include <stdio.h>
36
47extern char** environ;
58
@@ -32,6 +35,29 @@ void fputStr(void* h, char* str) {
3235 fputs(str, f);
3336}
3437
38+int fpoll(void* h)
39+{
40+ FILE* f = (FILE*)h;
41+ fd_set x;
42+ struct timeval timeout;
43+ timeout.tv_sec = 1;
44+ timeout.tv_usec = 0;
45+ int fd = fileno(f);
46+
47+ FD_ZERO(&x);
48+ FD_SET(fd, &x);
49+
50+ int r = select(fd+1, &x, 0, 0, &timeout);
51+ return r;
52+}
53+
54+void* do_popen(const char* cmd, const char* mode) {
55+ FILE* f = popen(cmd, mode);
56+// int d = fileno(f);
57+// fcntl(d, F_SETFL, O_NONBLOCK);
58+ return f;
59+}
60+
3561int isNull(void* ptr) {
3662 return ptr==NULL;
3763}
rts/idris_stdfgn.h+3
@@ -12,6 +12,9 @@ int fileEOF(void* h);
1212int fileError(void* h);
1313void fputStr(void*h, char* str);
1414
15+void* do_popen(const char* cmd, const char* mode);
16+int fpoll(void* h);
17+
1518int idris_eqPtr(void* x, void* y);
1619int isNull(void* ptr);
1720void* idris_stdin();