Basic SSL connection going
[hcoop/domtool2.git] / src / main.sml
CommitLineData
234b917a
AC
1(* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
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.
8 *
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.
13 *
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.
dac62e84 17 *)
234b917a
AC
18
19(* Main interface *)
20
21structure Main :> MAIN = struct
22
492c1cff 23open Ast Print
234b917a 24
6ae327f8
AC
25structure SM = StringMap
26
234b917a
AC
27val dmy = ErrorMsg.dummyLoc
28
6ae327f8 29val defaultT : record ref = ref SM.empty
8a7c40fa 30val defaultV : (unit -> exp) SM.map ref = ref SM.empty
6ae327f8
AC
31
32fun registerDefault (name, t, v) =
33 case SM.find (!defaultT, name) of
34 NONE => (defaultT := SM.insert (!defaultT, name, t);
35 defaultV := SM.insert (!defaultV, name, v))
36 | SOME _ => raise Fail "Duplicate default environment variable"
37
38fun tInit () = (TAction ((CRoot, dmy),
39 !defaultT,
40 StringMap.empty),
41 dmy)
d189ec0e
AC
42
43
234b917a 44
d189ec0e 45fun check' G fname =
a3698041 46 let
d189ec0e 47 (*val _ = print ("Check " ^ fname ^ "\n")*)
a3698041
AC
48 val prog = Parse.parse fname
49 in
50 if !ErrorMsg.anyErrors then
d189ec0e 51 G
a3698041 52 else
6ae327f8 53 Tycheck.checkFile G (tInit ()) prog
a3698041
AC
54 end
55
d189ec0e 56fun basis () =
234b917a 57 let
d189ec0e
AC
58 val dir = Posix.FileSys.opendir Config.libRoot
59
60 fun loop files =
61 case Posix.FileSys.readdir dir of
d612d62c
AC
62 NONE => (Posix.FileSys.closedir dir;
63 files)
d189ec0e
AC
64 | SOME fname =>
65 if String.isSuffix ".dtl" fname then
d612d62c
AC
66 loop (OS.Path.joinDirFile {dir = Config.libRoot,
67 file = fname}
d189ec0e
AC
68 :: files)
69 else
70 loop files
71
72 val files = loop []
73 val files = Order.order files
74 in
6ae327f8
AC
75 if !ErrorMsg.anyErrors then
76 Env.empty
77 else
78 foldl (fn (fname, G) => check' G fname) Env.empty files
d189ec0e
AC
79 end
80
81fun check fname =
82 let
83 val _ = ErrorMsg.reset ()
12adf55a 84 val _ = Env.preTycheck ()
d189ec0e
AC
85
86 val b = basis ()
234b917a
AC
87 in
88 if !ErrorMsg.anyErrors then
d189ec0e 89 (b, NONE)
234b917a
AC
90 else
91 let
7f012ffd 92 val _ = ErrorMsg.reset ()
d189ec0e 93 val prog = Parse.parse fname
234b917a 94 in
492c1cff 95 if !ErrorMsg.anyErrors then
d189ec0e 96 (Env.empty, NONE)
492c1cff 97 else
d189ec0e 98 let
6ae327f8 99 val G' = Tycheck.checkFile b (tInit ()) prog
d189ec0e
AC
100 in
101 (G', #3 prog)
102 end
234b917a
AC
103 end
104 end
105
d189ec0e 106fun reduce fname =
a3698041 107 let
d189ec0e 108 val (G, body) = check fname
a3698041
AC
109 in
110 if !ErrorMsg.anyErrors then
d189ec0e 111 NONE
a3698041 112 else
d189ec0e
AC
113 case body of
114 SOME body =>
115 let
116 val body' = Reduce.reduceExp G body
117 in
118 (*printd (PD.hovBox (PD.PPS.Rel 0,
119 [PD.string "Result:",
120 PD.space 1,
121 p_exp body']))*)
122 SOME body'
123 end
124 | _ => NONE
a3698041
AC
125 end
126
d189ec0e
AC
127fun eval fname =
128 case reduce fname of
129 (SOME body') =>
130 if !ErrorMsg.anyErrors then
131 ()
132 else
8a7c40fa 133 Eval.exec (SM.map (fn f => f ()) (!defaultV)) body'
d189ec0e
AC
134 | NONE => ()
135
3b267643
AC
136val dispatcher =
137 Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
559e89e9
AC
138
139fun ssl_err s =
140 let
141 val err = F_OpenSSL_SML_get_error.f ()
3b267643
AC
142
143 val lib = F_OpenSSL_SML_lib_error_string.f err
144 val func = F_OpenSSL_SML_func_error_string.f err
145 val reason = F_OpenSSL_SML_reason_error_string.f err
559e89e9
AC
146 in
147 print s;
148 print "\nReason: ";
3b267643
AC
149 if C.Ptr.isNull lib then
150 ()
151 else
152 (print (ZString.toML lib);
153 print ":");
154 if C.Ptr.isNull func then
155 ()
156 else
157 (print (ZString.toML func);
158 print ":");
159 if C.Ptr.isNull reason then
160 ()
161 else
162 print (ZString.toML reason);
559e89e9
AC
163 print "\n"
164 end
165
07cc384c 166
3b267643 167fun request fname =
07cc384c 168 let
3b267643
AC
169 val context = OpenSSL.context ("/home/adamc/fake/clientcert.pem",
170 "/home/adamc/fake/clientkey.pem",
171 Config.trustStore)
07cc384c 172
3b267643 173 val bio = OpenSSL.connect (context, dispatcher)
559e89e9 174
3b267643 175 val _ = print ("Subject: " ^ OpenSSL.peerCN bio ^ "\n")
559e89e9 176
3b267643
AC
177 val inf = TextIO.openIn fname
178
179 fun loop () =
180 case TextIO.inputLine inf of
181 NONE => ()
182 | SOME line => (OpenSSL.writeAll (bio, line);
183 loop ())
559e89e9 184 in
3b267643
AC
185 loop ();
186 TextIO.closeIn inf;
187 OpenSSL.close bio
559e89e9
AC
188 end
189
3b267643 190fun service () =
07cc384c 191 let
3b267643
AC
192 val context = OpenSSL.context (Config.serverCert,
193 Config.serverKey,
194 Config.trustStore)
195
196 val sock = OpenSSL.listen (Config.dispatcherPort, Config.queueSize)
197
198 fun loop () =
199 case OpenSSL.accept (context, sock) of
200 NONE => ()
201 | SOME bio =>
202 let
203 val _ = print ("Subject: " ^ OpenSSL.peerCN bio ^ "\n")
204
205 fun loop' () =
206 case OpenSSL.readOne bio of
207 NONE => ()
208 | SOME line => (print line;
209 loop' ())
210 in
211 loop' ();
212 OpenSSL.close bio;
213 loop ()
214 end
07cc384c 215 in
3b267643
AC
216 loop ();
217 OpenSSL.shutdown sock
07cc384c
AC
218 end
219
234b917a 220end