Generate autodoc index with SML/NJ HTML lib
[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
36e42cb8 23open Ast MsgTypes Print
234b917a 24
6ae327f8
AC
25structure SM = StringMap
26
aa56e112 27fun init () = Acl.read Config.aclFile
234b917a 28
d189ec0e 29fun check' G fname =
a3698041
AC
30 let
31 val prog = Parse.parse fname
32 in
33 if !ErrorMsg.anyErrors then
d189ec0e 34 G
a3698041 35 else
aa56e112 36 Tycheck.checkFile G (Defaults.tInit ()) prog
a3698041
AC
37 end
38
d189ec0e 39fun basis () =
234b917a 40 let
d189ec0e
AC
41 val dir = Posix.FileSys.opendir Config.libRoot
42
43 fun loop files =
44 case Posix.FileSys.readdir dir of
d612d62c
AC
45 NONE => (Posix.FileSys.closedir dir;
46 files)
d189ec0e
AC
47 | SOME fname =>
48 if String.isSuffix ".dtl" fname then
d612d62c
AC
49 loop (OS.Path.joinDirFile {dir = Config.libRoot,
50 file = fname}
d189ec0e
AC
51 :: files)
52 else
53 loop files
54
55 val files = loop []
3196000d 56 val (_, files) = Order.order files
d189ec0e 57 in
6ae327f8
AC
58 if !ErrorMsg.anyErrors then
59 Env.empty
60 else
61 foldl (fn (fname, G) => check' G fname) Env.empty files
d189ec0e
AC
62 end
63
64fun check fname =
65 let
66 val _ = ErrorMsg.reset ()
12adf55a 67 val _ = Env.preTycheck ()
d189ec0e
AC
68
69 val b = basis ()
234b917a
AC
70 in
71 if !ErrorMsg.anyErrors then
36e42cb8 72 raise ErrorMsg.Error
234b917a
AC
73 else
74 let
7f012ffd 75 val _ = ErrorMsg.reset ()
d189ec0e 76 val prog = Parse.parse fname
234b917a 77 in
492c1cff 78 if !ErrorMsg.anyErrors then
36e42cb8 79 raise ErrorMsg.Error
492c1cff 80 else
d189ec0e 81 let
aa56e112 82 val G' = Tycheck.checkFile b (Defaults.tInit ()) prog
d189ec0e 83 in
36e42cb8
AC
84 if !ErrorMsg.anyErrors then
85 raise ErrorMsg.Error
86 else
87 (G', #3 prog)
d189ec0e 88 end
234b917a
AC
89 end
90 end
91
d189ec0e 92fun reduce fname =
a3698041 93 let
d189ec0e 94 val (G, body) = check fname
a3698041
AC
95 in
96 if !ErrorMsg.anyErrors then
d189ec0e 97 NONE
a3698041 98 else
d189ec0e
AC
99 case body of
100 SOME body =>
101 let
102 val body' = Reduce.reduceExp G body
103 in
104 (*printd (PD.hovBox (PD.PPS.Rel 0,
105 [PD.string "Result:",
106 PD.space 1,
107 p_exp body']))*)
108 SOME body'
109 end
110 | _ => NONE
a3698041
AC
111 end
112
d189ec0e
AC
113fun eval fname =
114 case reduce fname of
115 (SOME body') =>
116 if !ErrorMsg.anyErrors then
36e42cb8 117 raise ErrorMsg.Error
d189ec0e 118 else
aa56e112 119 Eval.exec (Defaults.eInit ()) body'
36e42cb8 120 | NONE => raise ErrorMsg.Error
d189ec0e 121
3b267643
AC
122val dispatcher =
123 Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
559e89e9 124
36e42cb8
AC
125fun hostname () =
126 let
127 val inf = TextIO.openIn "/etc/hostname"
128 in
129 case TextIO.inputLine inf of
130 NONE => (TextIO.closeIn inf; raise Fail "No line in /etc/hostname")
131 | SOME line => (TextIO.closeIn inf; String.substring (line, 0, size line - 1))
132 end
133
3b267643 134fun request fname =
07cc384c 135 let
aa56e112
AC
136 val uid = Posix.ProcEnv.getuid ()
137 val user = Posix.SysDB.Passwd.name (Posix.SysDB.getpwuid uid)
138
139 val () = Acl.read Config.aclFile
140 val () = Domain.setUser user
141 val _ = check fname
142
143 val context = OpenSSL.context (Config.certDir ^ "/" ^ user ^ ".pem",
144 Config.keyDir ^ "/" ^ user ^ ".pem",
3b267643 145 Config.trustStore)
07cc384c 146
3b267643 147 val bio = OpenSSL.connect (context, dispatcher)
559e89e9 148
3b267643
AC
149 val inf = TextIO.openIn fname
150
36e42cb8 151 fun loop lines =
3b267643 152 case TextIO.inputLine inf of
36e42cb8
AC
153 NONE => String.concat (List.rev lines)
154 | SOME line => loop (line :: lines)
155
156 val code = loop []
559e89e9 157 in
3b267643 158 TextIO.closeIn inf;
36e42cb8
AC
159 Msg.send (bio, MsgConfig code);
160 case Msg.recv bio of
161 NONE => print "Server closed connection unexpectedly.\n"
162 | SOME m =>
163 case m of
164 MsgOk => print "Configuration succeeded.\n"
165 | MsgError s => print ("Configuration failed: " ^ s ^ "\n")
166 | _ => print "Unexpected server reply.\n";
3b267643 167 OpenSSL.close bio
559e89e9 168 end
aa56e112 169 handle ErrorMsg.Error => ()
559e89e9 170
3b267643 171fun service () =
07cc384c 172 let
aa56e112
AC
173 val () = Acl.read Config.aclFile
174
3b267643
AC
175 val context = OpenSSL.context (Config.serverCert,
176 Config.serverKey,
177 Config.trustStore)
36e42cb8 178 val _ = Domain.set_context context
3b267643 179
60534712 180 val sock = OpenSSL.listen (context, Config.dispatcherPort)
3b267643
AC
181
182 fun loop () =
60534712 183 case OpenSSL.accept sock of
3b267643
AC
184 NONE => ()
185 | SOME bio =>
186 let
aa56e112
AC
187 val user = OpenSSL.peerCN bio
188 val () = print ("\nConnection from " ^ user ^ "\n")
189 val () = Domain.setUser user
190
36e42cb8
AC
191 fun cmdLoop () =
192 case Msg.recv bio of
193 NONE => (OpenSSL.close bio
194 handle OpenSSL.OpenSSL _ => ();
195 loop ())
196 | SOME m =>
197 case m of
198 MsgConfig code =>
199 let
200 val _ = print "Configuration:\n"
201 val _ = print code
202 val _ = print "\n"
3b267643 203
36e42cb8
AC
204 val outname = OS.FileSys.tmpName ()
205 val outf = TextIO.openOut outname
206 in
207 TextIO.output (outf, code);
208 TextIO.closeOut outf;
209 (eval outname;
210 Msg.send (bio, MsgOk))
211 handle ErrorMsg.Error =>
212 (print "Compilation error\n";
213 Msg.send (bio,
214 MsgError "Error during configuration evaluation"))
215 | OpenSSL.OpenSSL s =>
216 (print "OpenSSL error\n";
217 Msg.send (bio,
218 MsgError
219 ("Error during configuration evaluation: "
220 ^ s)));
221 OS.FileSys.remove outname;
222 (ignore (OpenSSL.readChar bio);
223 OpenSSL.close bio)
224 handle OpenSSL.OpenSSL _ => ();
225 loop ()
226 end
227 | _ =>
228 (Msg.send (bio, MsgError "Unexpected command")
229 handle OpenSSL.OpenSSL _ => ();
230 OpenSSL.close bio
231 handle OpenSSL.OpenSSL _ => ();
232 loop ())
233 in
234 cmdLoop ()
235 end
236 in
237 loop ();
238 OpenSSL.shutdown sock
239 end
240
241fun slave () =
242 let
243 val host = hostname ()
244
245 val context = OpenSSL.context (Config.certDir ^ "/" ^ host ^ ".pem",
246 Config.keyDir ^ "/" ^ host ^ ".pem",
247 Config.trustStore)
248
249 val sock = OpenSSL.listen (context, Config.slavePort)
250
251 fun loop () =
252 case OpenSSL.accept sock of
253 NONE => ()
254 | SOME bio =>
255 let
256 val peer = OpenSSL.peerCN bio
257 val () = print ("\nConnection from " ^ peer ^ "\n")
3b267643 258 in
36e42cb8
AC
259 if peer <> Config.dispatcherName then
260 (print "Not authorized!\n";
261 OpenSSL.close bio;
262 loop ())
263 else let
264 fun loop' files =
265 case Msg.recv bio of
266 NONE => print "Dispatcher closed connection unexpectedly\n"
267 | SOME m =>
268 case m of
269 MsgFile file => loop' (file :: files)
270 | MsgDoFiles => (Slave.handleChanges files;
271 Msg.send (bio, MsgOk))
272 | _ => (print "Dispatcher sent unexpected command\n";
273 Msg.send (bio, MsgError "Unexpected command"))
274 in
275 loop' [];
276 ignore (OpenSSL.readChar bio);
277 OpenSSL.close bio;
278 loop ()
279 end
3196000d
AC
280 end handle OpenSSL.OpenSSL s =>
281 (print ("OpenSSL error: "^ s ^ "\n");
282 OpenSSL.close bio
283 handle OpenSSL.OpenSSL _ => ();
284 loop ())
07cc384c 285 in
3b267643
AC
286 loop ();
287 OpenSSL.shutdown sock
07cc384c
AC
288 end
289
3196000d
AC
290fun autodocBasis outdir =
291 let
292 val dir = Posix.FileSys.opendir Config.libRoot
293
294 fun loop files =
295 case Posix.FileSys.readdir dir of
296 NONE => (Posix.FileSys.closedir dir;
297 files)
298 | SOME fname =>
299 if String.isSuffix ".dtl" fname then
300 loop (OS.Path.joinDirFile {dir = Config.libRoot,
301 file = fname}
302 :: files)
303 else
304 loop files
305
306 val files = loop []
307 in
308 Autodoc.autodoc {outdir = outdir, infiles = files}
309 end
310
234b917a 311end