1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
4 * This program is free software
; you can redistribute it
and/or
5 * modify it under the terms
of the GNU General Public License
6 * as published by the Free Software Foundation
; either version
2
7 * of the License
, or (at your option
) any later version
.
9 * This program is distributed
in the hope that it will be useful
,
10 * but WITHOUT ANY WARRANTY
; without even the implied warranty
of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
. See the
12 * GNU General Public License for more details
.
14 * You should have received a copy
of the GNU General Public License
15 * along
with this program
; if not
, write to the Free Software
16 * Foundation
, Inc
., 51 Franklin Street
, Fifth Floor
, Boston
, MA
02110-1301, USA
.
21 structure Main
:> MAIN
= struct
25 structure SM
= StringMap
27 val dmy
= ErrorMsg
.dummyLoc
29 fun init () = (F_OpenSSL_SML_add_all_algorithms
.f
' ();
30 F_OpenSSL_SML_load_error_strings
.f
' ();
31 F_OpenSSL_SML_load_BIO_strings
.f
' ())
35 val defaultT
: record ref
= ref SM
.empty
36 val defaultV
: (unit
-> exp
) SM
.map ref
= ref SM
.empty
38 fun registerDefault (name
, t
, v
) =
39 case SM
.find (!defaultT
, name
) of
40 NONE
=> (defaultT
:= SM
.insert (!defaultT
, name
, t
);
41 defaultV
:= SM
.insert (!defaultV
, name
, v
))
42 | SOME _
=> raise Fail
"Duplicate default environment variable"
44 fun tInit () = (TAction ((CRoot
, dmy
),
53 (*val _
= print ("Check " ^ fname ^
"\n")*)
54 val prog
= Parse
.parse fname
56 if !ErrorMsg
.anyErrors
then
59 Tycheck
.checkFile
G (tInit ()) prog
64 val dir
= Posix
.FileSys
.opendir Config
.libRoot
67 case Posix
.FileSys
.readdir dir
of
68 NONE
=> (Posix
.FileSys
.closedir dir
;
71 if String.isSuffix
".dtl" fname
then
72 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
79 val files
= Order
.order files
81 if !ErrorMsg
.anyErrors
then
84 foldl (fn (fname
, G
) => check
' G fname
) Env
.empty files
89 val _
= ErrorMsg
.reset ()
90 val _
= Env
.preTycheck ()
94 if !ErrorMsg
.anyErrors
then
98 val _
= ErrorMsg
.reset ()
99 val prog
= Parse
.parse fname
101 if !ErrorMsg
.anyErrors
then
105 val G
' = Tycheck
.checkFile
b (tInit ()) prog
114 val (G
, body
) = check fname
116 if !ErrorMsg
.anyErrors
then
122 val body
' = Reduce
.reduceExp G body
124 (*printd (PD
.hovBox (PD
.PPS
.Rel
0,
125 [PD
.string "Result:",
136 if !ErrorMsg
.anyErrors
then
139 Eval
.exec (SM
.map (fn f
=> f ()) (!defaultV
)) body
'
142 val dispatcher
: C
.rw ZString
.zstring
' = ZString
.dupML
' Config
.dispatcher
146 val err
= F_OpenSSL_SML_get_error
.f ()
150 print (ZString
.toML (F_OpenSSL_SML_lib_error_string
.f err
));
152 print (ZString
.toML (F_OpenSSL_SML_func_error_string
.f err
));
154 print (ZString
.toML (F_OpenSSL_SML_reason_error_string
.f err
));
158 exception OpenSSL
of string
160 fun writeAll (bio
, s
) =
162 val buf
= ZString
.dupML
' s
164 fun loop (buf
, len
) =
166 val r
= F_OpenSSL_SML_write
.f
' (bio
, C
.Ptr
.inject
' buf
, len
)
172 raise OpenSSL
"BIO_write failed")
174 loop (C
.Ptr
.|
+! C
.S
.uchar (buf
, Int32
.toInt r
), Int32
.- (len
, r
))
177 loop (buf
, Int32
.fromInt (size s
));
183 val bio
= F_OpenSSL_SML_new_connect
.f
' dispatcher
185 if C
.Ptr
.isNull
' bio
then
186 (ssl_err ("Error initializating connection to dispatcher at " ^ Config
.dispatcher
);
187 F_OpenSSL_SML_free_all
.f
' bio
)
188 else if F_OpenSSL_SML_do_connect
.f
' bio
<= 0 then
189 (ssl_err ("Error connecting to dispatcher at " ^ Config
.dispatcher
);
190 F_OpenSSL_SML_free_all
.f
' bio
)
192 val inf
= TextIO.openIn fname
195 case TextIO.inputLine inf
of
197 | SOME line
=> (writeAll (bio
, line
);
202 F_OpenSSL_SML_free_all
.f
' bio