Basic HTML documentation generation
[hcoop/domtool2.git] / src / main.sml
... / ...
CommitLineData
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.
17 *)
18
19(* Main interface *)
20
21structure Main :> MAIN = struct
22
23open Ast MsgTypes Print
24
25structure SM = StringMap
26
27fun init () = Acl.read Config.aclFile
28
29fun check' G fname =
30 let
31 val prog = Parse.parse fname
32 in
33 if !ErrorMsg.anyErrors then
34 G
35 else
36 Tycheck.checkFile G (Defaults.tInit ()) prog
37 end
38
39fun basis () =
40 let
41 val dir = Posix.FileSys.opendir Config.libRoot
42
43 fun loop files =
44 case Posix.FileSys.readdir dir of
45 NONE => (Posix.FileSys.closedir dir;
46 files)
47 | SOME fname =>
48 if String.isSuffix ".dtl" fname then
49 loop (OS.Path.joinDirFile {dir = Config.libRoot,
50 file = fname}
51 :: files)
52 else
53 loop files
54
55 val files = loop []
56 val (_, files) = Order.order files
57 in
58 if !ErrorMsg.anyErrors then
59 Env.empty
60 else
61 foldl (fn (fname, G) => check' G fname) Env.empty files
62 end
63
64fun check fname =
65 let
66 val _ = ErrorMsg.reset ()
67 val _ = Env.preTycheck ()
68
69 val b = basis ()
70 in
71 if !ErrorMsg.anyErrors then
72 raise ErrorMsg.Error
73 else
74 let
75 val _ = ErrorMsg.reset ()
76 val prog = Parse.parse fname
77 in
78 if !ErrorMsg.anyErrors then
79 raise ErrorMsg.Error
80 else
81 let
82 val G' = Tycheck.checkFile b (Defaults.tInit ()) prog
83 in
84 if !ErrorMsg.anyErrors then
85 raise ErrorMsg.Error
86 else
87 (G', #3 prog)
88 end
89 end
90 end
91
92fun reduce fname =
93 let
94 val (G, body) = check fname
95 in
96 if !ErrorMsg.anyErrors then
97 NONE
98 else
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
111 end
112
113fun eval fname =
114 case reduce fname of
115 (SOME body') =>
116 if !ErrorMsg.anyErrors then
117 raise ErrorMsg.Error
118 else
119 Eval.exec (Defaults.eInit ()) body'
120 | NONE => raise ErrorMsg.Error
121
122val dispatcher =
123 Config.dispatcher ^ ":" ^ Int.toString Config.dispatcherPort
124
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
134fun request fname =
135 let
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",
145 Config.trustStore)
146
147 val bio = OpenSSL.connect (context, dispatcher)
148
149 val inf = TextIO.openIn fname
150
151 fun loop lines =
152 case TextIO.inputLine inf of
153 NONE => String.concat (List.rev lines)
154 | SOME line => loop (line :: lines)
155
156 val code = loop []
157 in
158 TextIO.closeIn inf;
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";
167 OpenSSL.close bio
168 end
169 handle ErrorMsg.Error => ()
170
171fun service () =
172 let
173 val () = Acl.read Config.aclFile
174
175 val context = OpenSSL.context (Config.serverCert,
176 Config.serverKey,
177 Config.trustStore)
178 val _ = Domain.set_context context
179
180 val sock = OpenSSL.listen (context, Config.dispatcherPort)
181
182 fun loop () =
183 case OpenSSL.accept sock of
184 NONE => ()
185 | SOME bio =>
186 let
187 val user = OpenSSL.peerCN bio
188 val () = print ("\nConnection from " ^ user ^ "\n")
189 val () = Domain.setUser user
190
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"
203
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")
258 in
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
280 end handle OpenSSL.OpenSSL s =>
281 (print ("OpenSSL error: "^ s ^ "\n");
282 OpenSSL.close bio
283 handle OpenSSL.OpenSSL _ => ();
284 loop ())
285 in
286 loop ();
287 OpenSSL.shutdown sock
288 end
289
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
311end