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
23 open Ast MsgTypes Print
25 structure SM
= StringMap
27 fun init () = Acl
.read Config
.aclFile
31 val prog
= Parse
.parse fname
33 if !ErrorMsg
.anyErrors
then
36 Tycheck
.checkFile
G (Defaults
.tInit ()) prog
41 val dir
= Posix
.FileSys
.opendir Config
.libRoot
44 case Posix
.FileSys
.readdir dir
of
45 NONE
=> (Posix
.FileSys
.closedir dir
;
48 if String.isSuffix
".dtl" fname
then
49 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
56 val (_
, files
) = Order
.order files
58 if !ErrorMsg
.anyErrors
then
61 foldl (fn (fname
, G
) => check
' G fname
) Env
.empty files
66 val _
= ErrorMsg
.reset ()
67 val _
= Env
.preTycheck ()
71 if !ErrorMsg
.anyErrors
then
75 val _
= ErrorMsg
.reset ()
76 val prog
= Parse
.parse fname
78 if !ErrorMsg
.anyErrors
then
82 val G
' = Tycheck
.checkFile
b (Defaults
.tInit ()) prog
84 if !ErrorMsg
.anyErrors
then
94 val (G
, body
) = check fname
96 if !ErrorMsg
.anyErrors
then
102 val body
' = Reduce
.reduceExp G body
104 (*printd (PD
.hovBox (PD
.PPS
.Rel
0,
105 [PD
.string "Result:",
116 if !ErrorMsg
.anyErrors
then
119 Eval
.exec (Defaults
.eInit ()) body
'
120 | NONE
=> raise ErrorMsg
.Error
123 Config
.dispatcher ^
":" ^
Int.toString Config
.dispatcherPort
127 val uid
= Posix
.ProcEnv
.getuid ()
128 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
130 val () = Acl
.read Config
.aclFile
131 val () = Domain
.setUser user
135 val uid
= Posix
.ProcEnv
.getuid ()
136 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
138 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ user ^
".pem",
139 Config
.keyDir ^
"/" ^ user ^
"/key.pem",
142 val bio
= OpenSSL
.connect (context
, dispatcher
)
144 val inf
= TextIO.openIn fname
147 case TextIO.inputLine inf
of
148 NONE
=> String.concat (List.rev lines
)
149 | SOME line
=> loop (line
:: lines
)
154 Msg
.send (bio
, MsgConfig code
);
156 NONE
=> print
"Server closed connection unexpectedly.\n"
159 MsgOk
=> print
"Configuration succeeded.\n"
160 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
161 | _
=> print
"Unexpected server reply.\n";
164 handle ErrorMsg
.Error
=> ()
168 val () = Acl
.read Config
.aclFile
170 val context
= OpenSSL
.context (Config
.serverCert
,
173 val _
= Domain
.set_context context
175 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
178 case OpenSSL
.accept sock
of
182 val user
= OpenSSL
.peerCN bio
183 val () = print ("\nConnection from " ^ user ^
"\n")
184 val () = Domain
.setUser user
188 NONE
=> (OpenSSL
.close bio
189 handle OpenSSL
.OpenSSL _
=> ();
195 val _
= print
"Configuration:\n"
199 val outname
= OS
.FileSys
.tmpName ()
200 val outf
= TextIO.openOut outname
202 TextIO.output (outf
, code
);
203 TextIO.closeOut outf
;
205 Msg
.send (bio
, MsgOk
))
206 handle ErrorMsg
.Error
=>
207 (print
"Compilation error\n";
209 MsgError
"Error during configuration evaluation"))
210 | OpenSSL
.OpenSSL s
=>
211 (print
"OpenSSL error\n";
214 ("Error during configuration evaluation: "
216 OS
.FileSys
.remove outname
;
217 (ignore (OpenSSL
.readChar bio
);
219 handle OpenSSL
.OpenSSL _
=> ();
223 (Msg
.send (bio
, MsgError
"Unexpected command")
224 handle OpenSSL
.OpenSSL _
=> ();
226 handle OpenSSL
.OpenSSL _
=> ();
231 handle OpenSSL
.OpenSSL s
=>
232 (print ("OpenSSL error: " ^ s ^
"\n");
234 handle OpenSSL
.OpenSSL _
=> ();
236 | OS
.SysErr (s
, _
) =>
237 (print ("System error: " ^ s ^
"\n");
239 handle OpenSSL
.OpenSSL _
=> ();
242 print
"Listening for connections....\n";
244 OpenSSL
.shutdown sock
249 val host
= Slave
.hostname ()
251 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
252 Config
.keyDir ^
"/" ^ host ^
"/key.pem",
255 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
258 case OpenSSL
.accept sock
of
262 val peer
= OpenSSL
.peerCN bio
263 val () = print ("\nConnection from " ^ peer ^
"\n")
265 if peer
<> Config
.dispatcherName
then
266 (print
"Not authorized!\n";
272 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
275 MsgFile file
=> loop
' (file
:: files
)
276 | MsgDoFiles
=> (Slave
.handleChanges files
;
277 Msg
.send (bio
, MsgOk
))
278 | _
=> (print
"Dispatcher sent unexpected command\n";
279 Msg
.send (bio
, MsgError
"Unexpected command"))
282 ignore (OpenSSL
.readChar bio
);
286 end handle OpenSSL
.OpenSSL s
=>
287 (print ("OpenSSL error: "^ s ^
"\n");
289 handle OpenSSL
.OpenSSL _
=> ();
291 | OS
.SysErr (s
, _
) =>
292 (print ("System error: "^ s ^
"\n");
294 handle OpenSSL
.OpenSSL _
=> ();
298 OpenSSL
.shutdown sock
301 fun autodocBasis outdir
=
303 val dir
= Posix
.FileSys
.opendir Config
.libRoot
306 case Posix
.FileSys
.readdir dir
of
307 NONE
=> (Posix
.FileSys
.closedir dir
;
310 if String.isSuffix
".dtl" fname
then
311 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
319 Autodoc
.autodoc
{outdir
= outdir
, infiles
= files
}