summaryrefslogtreecommitdiff
path: root/ghc/lib/glaExts/PreludeDialogueIO.lhs
blob: 08de2179b487ad6a7fb361efbc6a8264312b98d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
%
% (c) The GRASP/AQUA Project, Glasgow University, 1993-1994
%
\section{The @Dialogue@ interface}

\begin{code}
module PreludeDialogueIO (
	requestToPrimIO,    -- RTS uses this!

	processIORequest,   -- used in PreludeGlaIO
	appendChan#,	    -- used elsewhere in prelude
	unpackArgv,	    -- ditto
	unpackProgName	    -- ditto
    ) where

import PreludeGlaST	-- for _ST stuff
import PreludeGlaMisc	-- for stable pointers
import Cls
import Core
import IChar
import IInt
import IList
import IO		( stdout, stdin )
import List		( (++), reverse, foldr, foldl )
import PS		-- packed strings
import Prel		( chr, flip )
import Stdio		( fopen, fclose, fflush, _FILE )
import Text
import TyArray		( Array(..) )
import TyIO
\end{code}

%************************************************************************
%*									*
\subsection[requestToIO]{Dialogue-to-IO}
%*									*
%************************************************************************

We would like to take existing Haskell programs, written with @main@
of type @Dialogue@, and run them on our system.	 To do this, our
system actually evaluates @mainPrimIO@ (rather than @main@ directly).
@main@ has type @Dialogue@ then @mainPrimIO@ [separate module] is defined
like this:
\begin{verbatim}
mainPrimIO :: PrimIO ()
mainPrimIO s = case (requestToPrimIO main s) of
	     ( (), s2) -> ( (), s2 )
\end{verbatim}

So, here's @requestToPrimIO@:
\begin{code}
requestToPrimIO :: Dialogue -> PrimIO ()

requestToPrimIO dialogue 
 = newVar (error "HELP! (Forgot to link with -fhaskell-1.3?)\n")
					`thenPrimIO` \ rsV ->
   unsafeInterleavePrimIO (readVar rsV)	`thenPrimIO` \ rs ->
   run (dialogue rs) rsV

run :: [Request] -> MutableVar _RealWorld [Response] -> PrimIO ()

run []	       v = returnPrimIO ()
run (req:reqs) v 
 = processIORequest req			`thenPrimIO` \ r ->
   newVar (error "GlasgowIO:run:synch")	`thenPrimIO` \ rsV ->
   unsafeInterleavePrimIO (readVar rsV)	`thenPrimIO` \ rs ->
   writeVar v (r:rs)			`seqPrimIO`
   run reqs rsV
\end{code}

%************************************************************************
%*									*
\subsection[processIORequest]{@processIORequest@}
%*									*
%************************************************************************

The guy that really does the business is @processIORequest@.  We make
this available to the intrepid user.

\begin{code}
processIORequest :: Request -> PrimIO Response

processIORequest (ReadFile name)
  = fopen name "r"	`thenPrimIO` \ file_star ->
    if (file_star == ``NULL'')
    then returnPrimIO (Failure (ReadError ("ReadFile: can't read: "++name)))
	-- ToDo: return SearchErrors when appropriate

    else readFile# file_star `thenPrimIO` \ str ->
	 returnPrimIO (Str str)

processIORequest (WriteFile name string)
  = fopen name "w"	`thenPrimIO` \ file_star ->
    if (file_star == ``NULL'')
    then returnPrimIO (Failure (WriteError ("WriteFile: open failed: "++name)))

    else writeFile# file_star string  `seqPrimIO`
	 fclose	     file_star	      `thenPrimIO` \ status ->
	 returnPrimIO (
	     if status == 0
	     then Success
	     else Failure (WriteError ("WriteFile: closed failed: "++name))
	 )

processIORequest (AppendFile name string)
  = fopen name "a+"{-don't create-} `thenPrimIO` \ file_star ->
    if (file_star == ``NULL'')
    then returnPrimIO (Failure (WriteError ("AppendFile: open failed: "++name)))

    else writeFile# file_star string `seqPrimIO`
	 fclose	    file_star	     `thenPrimIO` \ status ->
	 returnPrimIO (
	     if status == 0
	     then Success
	     else Failure (WriteError ("AppendFile: closed failed: "++name))
	 )

processIORequest (DeleteFile name)
  = _casm_ ``%r = (I_) unlink((char *) %0);'' name   `thenPrimIO` \ status ->
    returnPrimIO (
    if (status == (0::Int)) then
	Success
    else if ( (``errno''::Int) == (``ENOENT''::Int) ) then
	Failure (SearchError ("DeleteFile: no such file: "++name))
    else
	Failure (WriteError ("DeleteFile: could not delete: "++name))
    )

processIORequest (AppendChan chan str)
  = case chan of 
      "stdout" ->
    	appendChan# ``stdout'' str   	`seqPrimIO` 
    	fflush ``stdout''    	    	`thenPrimIO` \ status ->
	returnPrimIO (
	    if status == 0
	    then Success
	    else Failure (WriteError ("AppendChan: flush failed: " ++ chan))
	)
      "stderr" ->
    	appendChan# ``stderr'' str   	`seqPrimIO` 
    	fflush ``stderr''    	    	`thenPrimIO` \ status ->
	returnPrimIO (
	    if status == 0
	    then Success
	    else Failure (WriteError ("AppendChan: flush failed: " ++ chan))
	)
      _ -> error "AppendChan: not implemented except for \"stdout\" and \"stderr\"\n"

processIORequest (ReadChan chan)
  = case chan of
      "stdin" -> readChan# ``stdin'' `thenPrimIO` \ str ->
		 returnPrimIO (Str str)

      _ -> error "ReadChan: not implemented except for \"stdin\"\n"

processIORequest (Echo False) = returnPrimIO Success
processIORequest (Echo True)
  = {- REMOVED: Can't be bothered. WDP: 95/04
    appendChan# ``stderr'' "Glasgow Haskell doesn't support \"Echo\" requests properly (yet)\n"
    `seqPrimIO` -} returnPrimIO Success

processIORequest GetArgs
  = returnPrimIO (StrList (unpackArgv ``prog_argv'' (``prog_argc''::Int) ))

processIORequest GetProgName
  = returnPrimIO (Str (unpackProgName ``prog_argv''))

processIORequest (GetEnv name)
  = _casm_ ``%r = getenv((char *) %0);'' name `thenPrimIO` \ litstring ->
    returnPrimIO (
	if (eqAddr litstring ``NULL'') then
	    Failure (SearchError ("GetEnv:"++name))
	else
	    Str (_unpackPS (_packCString litstring)) -- cheaper than it looks
    )
  where
    eqAddr (A# a1) (A# a2) = eqAddr# a1 a2

#ifndef __PARALLEL_HASKELL__

processIORequest (SigAction n act)
  = (case act of
    SAIgnore -> _ccall_ stg_sig_ignore n (``NULL''::_Addr)
    SADefault -> _ccall_ stg_sig_default n (``NULL''::_Addr)
    SACatch dialogue -> 
                 let handler :: PrimIO ()
                     handler s = case (requestToPrimIO dialogue s) of
    	    	    	    	( (), s2@(S# _) ) -> ( (), s2 )
    	    	 in
    	    	    makeStablePtr handler    `thenPrimIO` \ sptr ->
    	    	    _ccall_ stg_sig_catch n sptr (``NULL''::_Addr))
    	    	    	    	    	     `thenPrimIO` \ osptr ->
    returnPrimIO (
    	if osptr >= 0 then Success
        else Failure (OtherError ("SigAction:" ++ show n)))

#endif {-!parallel-}

processIORequest _
  = error "DialogueToIO.processIORequest: unimplemented I/O request (please report)\n"
\end{code}

%************************************************************************
%*									*
\subsection[DialogueIO]{Access to all @Dialogues@ in the IO world}
%*									*
%************************************************************************

This is Andy Gill's stuff to make all of @Dialogue@-style IO readily
available in the monadic IO world.

%************************************************************************
%*									*
\subsection{Support bits for all of this}
%*									*
%************************************************************************

\begin{code}
-- like unpackCString ...

type CHAR_STAR_STAR	= _Addr	-- this is all a  HACK
type CHAR_STAR		= _Addr

unpackArgv	:: CHAR_STAR_STAR -> Int -> [String] -- argv[1 .. argc-1]
unpackProgName	:: CHAR_STAR_STAR	 -> String   -- argv[0]

unpackArgv argv argc = unpack 1
  where
    unpack :: Int -> [String]
    unpack n
      = if (n >= argc)
	then ([] :: [String])
	else case (indexAddrOffAddr argv n) of { item ->
	     _unpackPS (_packCString item) : unpack (n + 1)
	     }

unpackProgName argv
  = case (indexAddrOffAddr argv 0) of { prog ->
    de_slash [] (_unpackPS (_packCString prog)) }
  where
    -- re-start accumulating at every '/'
    de_slash :: String -> String -> String
    de_slash acc []	  = reverse acc
    de_slash acc ('/':xs) = de_slash []	     xs
    de_slash acc (x:xs)	  = de_slash (x:acc) xs
\end{code}

Read and append a string from/on a given @FILE *@ stream.  @appendChan#@
and @readChan#@ are well-behaved lazy functions; @writeFile#@ and
@readFile#@ (which ``know'' they are writing/reading disk files) are
much stricter.

\begin{code}
appendChan#, writeFile# :: _FILE -> String -> PrimIO Bool

appendChan# stream [] = returnPrimIO True

appendChan# stream (c : cs)
  = _ccall_ stg_putc c stream	`seqPrimIO` -- stg_putc expands to putc
    appendChan# stream cs		    -- (just does some casting stream)

-----------
writeFile# stream [] = returnPrimIO True

writeFile# stream (c1@(C# _) : c2@(C# _) : c3@(C# _) : c4@(C# _)
		 : c5@(C# _) : c6@(C# _) : c7@(C# _) : c8@(C# _)
		 : c9@(C# _) : c10@(C# _): c11@(C# _): c12@(C# _)
		 : c13@(C# _): c14@(C# _): c15@(C# _): c16@(C# _): cs)
 = _ccall_ stg_putc  c1 stream	`seqPrimIO`
   _ccall_ stg_putc  c2 stream	`seqPrimIO`
   _ccall_ stg_putc  c3 stream	`seqPrimIO`
   _ccall_ stg_putc  c4 stream	`seqPrimIO`
   _ccall_ stg_putc  c5 stream	`seqPrimIO`
   _ccall_ stg_putc  c6 stream	`seqPrimIO`
   _ccall_ stg_putc  c7 stream	`seqPrimIO`
   _ccall_ stg_putc  c8 stream	`seqPrimIO`
   _ccall_ stg_putc  c9 stream	`seqPrimIO`
   _ccall_ stg_putc c10 stream	`seqPrimIO`
   _ccall_ stg_putc c11 stream	`seqPrimIO`
   _ccall_ stg_putc c12 stream	`seqPrimIO`
   _ccall_ stg_putc c13 stream	`seqPrimIO`
   _ccall_ stg_putc c14 stream	`seqPrimIO`
   _ccall_ stg_putc c15 stream	`seqPrimIO`
   _ccall_ stg_putc c16 stream	`seqPrimIO`
   writeFile# stream cs

writeFile# stream (c : cs)
  = _ccall_ stg_putc c stream	`seqPrimIO`
    writeFile# stream cs
\end{code}

@readChan#@ lazily reads the rest of some stream.  Dodgy because two
uses of.

ToDo: return fclose status.

\begin{code}
readChan#, readFile# :: _FILE -> PrimIO String

readChan# stream
  = let
	read_rest
	  =  _ccall_ stg_getc{-macro-} stream `thenPrimIO` \ ch ->

	     if ch < 0 then -- SIGH: ch ==# ``EOF'' then
		returnPrimIO []
	     else
		unsafeInterleavePrimIO read_rest `thenPrimIO` \ rest ->
		returnPrimIO (chr ch : rest)
    in
    unsafeInterleavePrimIO read_rest `thenPrimIO` \ contents ->
    returnPrimIO contents

------------------
readFile# stream
  = let
	read_rest
	  =  newCharArray (0::Int, 1023){-malloc!?-} `thenStrictlyST` \ arr# ->
		-- ToDo: lift newCharArray out of the loop!

	     _ccall_ fread arr# (1::Int) (1024::Int) stream `thenPrimIO` \ num_read ->

	     cvt arr# 0 (num_read - 1)	`thenPrimIO` \ chars ->

	     if num_read < 1024 then
		fclose stream `seqPrimIO`
		returnPrimIO chars
	     else
		unsafeInterleavePrimIO read_rest `thenPrimIO` \ rest ->
		returnPrimIO (chars ++ rest)
    in
    unsafeInterleavePrimIO read_rest `thenPrimIO` \ contents ->
    returnPrimIO contents
  where
    cvt :: _MutableByteArray _RealWorld Int
	-> Int -> Int
	-> PrimIO [Char]

    cvt arr# idx last
      = if idx > last then
	   returnPrimIO []
        else
	   readCharArray arr# idx   `thenPrimIO` \ ch ->
	   cvt arr# (idx + 1) last  `thenPrimIO` \ rest ->
	   returnPrimIO (ch : rest)
\end{code}