on master help sign in
Tinker with popen/poll
0fa9239Edwin Brady
libs/neweffects/Effect/StdIO.idr+6
@@ -15,6 +15,12 @@ instance Handler StdIO IO where
15 handle () (PutCh c) k = do putChar c; k () ()
15 handle () (PutCh c) k = do putChar c; k () ()
16 handle () GetCh k = do x <- getChar; k x ()
16 handle () GetCh k = do x <- getChar; k x ()
17
17
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+
18--- The Effect and associated functions
24--- The Effect and associated functions
19
25
20STDIO : EFFECT
26STDIO : EFFECT
libs/prelude/Prelude.idr+11−1
@@ -503,11 +503,17 @@ do_fread h = prim_fread h
503fgetc : File -> IO Char
503fgetc : File -> IO Char
504fgetc (FHandle h) = return (cast !(mkForeign (FFun "fgetc" [FPtr] FInt) h))
504fgetc (FHandle h) = return (cast !(mkForeign (FFun "fgetc" [FPtr] FInt) h))
505
505
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+
506fflush : File -> IO ()
512fflush : File -> IO ()
507fflush (FHandle h) = mkForeign (FFun "fflush" [FPtr] FUnit) h
513fflush (FHandle h) = mkForeign (FFun "fflush" [FPtr] FUnit) h
508
514
509do_popen : String -> String -> IO Ptr
515do_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
511
517
512popen : String -> Mode -> IO File
518popen : String -> Mode -> IO File
513popen f m = do ptr <- do_popen f (modeStr m)
519popen f m = do ptr <- do_popen f (modeStr m)
@@ -552,6 +558,10 @@ ferror : File -> IO Bool
552ferror (FHandle h) = do err <- do_ferror h
558ferror (FHandle h) = do err <- do_ferror h
553 return (not (err == 0))
559 return (not (err == 0))
554
560
561+fpoll : File -> IO Bool
562+fpoll (FHandle h) = do p <- mkForeign (FFun "fpoll" [FPtr] FInt) h
563+ return (p > 0)
564+
555partial
565partial
556nullPtr : Ptr -> IO Bool
566nullPtr : Ptr -> IO Bool
557nullPtr p = do ok <- mkForeign (FFun "isNull" [FPtr] FInt) p
567nullPtr p = do ok <- mkForeign (FFun "isNull" [FPtr] FInt) p
rts/idris_stdfgn.c+26
@@ -1,5 +1,8 @@
1#include "idris_stdfgn.h"
1#include "idris_stdfgn.h"
2#include "idris_rts.h"
2#include "idris_rts.h"
3+#include <sys/select.h>
4+#include <fcntl.h>
5+#include <stdio.h>
3
6
4extern char** environ;
7extern char** environ;
5
8
@@ -32,6 +35,29 @@ void fputStr(void* h, char* str) {
32 fputs(str, f);
35 fputs(str, f);
33}
36}
34
37
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+
35int isNull(void* ptr) {
61int isNull(void* ptr) {
36 return ptr==NULL;
62 return ptr==NULL;
37}
63}
rts/idris_stdfgn.h+3
@@ -12,6 +12,9 @@ int fileEOF(void* h);
12int fileError(void* h);
12int fileError(void* h);
13void fputStr(void*h, char* str);
13void fputStr(void*h, char* str);
14
14
15+void* do_popen(const char* cmd, const char* mode);
16+int fpoll(void* h);
17+
15int idris_eqPtr(void* x, void* y);
18int idris_eqPtr(void* x, void* y);
16int isNull(void* ptr);
19int isNull(void* ptr);
17void* idris_stdin();
20void* idris_stdin();