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 inf
= TextIO.openIn
"/etc/hostname"
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))
136 val uid
= Posix
.ProcEnv
.getuid ()
137 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
139 val () = Acl
.read Config
.aclFile
140 val () = Domain
.setUser user
144 val uid
= Posix
.ProcEnv
.getuid ()
145 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
147 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ user ^
".pem",
148 Config
.keyDir ^
"/" ^ user ^
"/key.pem",
151 val bio
= OpenSSL
.connect (context
, dispatcher
)
153 val inf
= TextIO.openIn fname
156 case TextIO.inputLine inf
of
157 NONE
=> String.concat (List.rev lines
)
158 | SOME line
=> loop (line
:: lines
)
163 Msg
.send (bio
, MsgConfig code
);
165 NONE
=> print
"Server closed connection unexpectedly.\n"
168 MsgOk
=> print
"Configuration succeeded.\n"
169 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
170 | _
=> print
"Unexpected server reply.\n";
173 handle ErrorMsg
.Error
=> ()
177 val () = Acl
.read Config
.aclFile
179 val context
= OpenSSL
.context (Config
.serverCert
,
182 val _
= Domain
.set_context context
184 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
187 case OpenSSL
.accept sock
of
191 val user
= OpenSSL
.peerCN bio
192 val () = print ("\nConnection from " ^ user ^
"\n")
193 val () = Domain
.setUser user
197 NONE
=> (OpenSSL
.close bio
198 handle OpenSSL
.OpenSSL _
=> ();
204 val _
= print
"Configuration:\n"
208 val outname
= OS
.FileSys
.tmpName ()
209 val outf
= TextIO.openOut outname
211 TextIO.output (outf
, code
);
212 TextIO.closeOut outf
;
214 Msg
.send (bio
, MsgOk
))
215 handle ErrorMsg
.Error
=>
216 (print
"Compilation error\n";
218 MsgError
"Error during configuration evaluation"))
219 | OpenSSL
.OpenSSL s
=>
220 (print
"OpenSSL error\n";
223 ("Error during configuration evaluation: "
225 OS
.FileSys
.remove outname
;
226 (ignore (OpenSSL
.readChar bio
);
228 handle OpenSSL
.OpenSSL _
=> ();
232 (Msg
.send (bio
, MsgError
"Unexpected command")
233 handle OpenSSL
.OpenSSL _
=> ();
235 handle OpenSSL
.OpenSSL _
=> ();
240 handle OpenSSL
.OpenSSL s
=>
241 (print ("OpenSSL error: " ^ s ^
"\n");
243 handle OpenSSL
.OpenSSL _
=> ();
245 | OS
.SysErr (s
, _
) =>
246 (print ("System error: " ^ s ^
"\n");
248 handle OpenSSL
.OpenSSL _
=> ();
251 print
"Listening for connections....\n";
253 OpenSSL
.shutdown sock
258 val host
= hostname ()
260 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
261 Config
.keyDir ^
"/" ^ host ^
"/key.pem",
264 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
267 case OpenSSL
.accept sock
of
271 val peer
= OpenSSL
.peerCN bio
272 val () = print ("\nConnection from " ^ peer ^
"\n")
274 if peer
<> Config
.dispatcherName
then
275 (print
"Not authorized!\n";
281 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
284 MsgFile file
=> loop
' (file
:: files
)
285 | MsgDoFiles
=> (Slave
.handleChanges files
;
286 Msg
.send (bio
, MsgOk
))
287 | _
=> (print
"Dispatcher sent unexpected command\n";
288 Msg
.send (bio
, MsgError
"Unexpected command"))
291 ignore (OpenSSL
.readChar bio
);
295 end handle OpenSSL
.OpenSSL s
=>
296 (print ("OpenSSL error: "^ s ^
"\n");
298 handle OpenSSL
.OpenSSL _
=> ();
300 | OS
.SysErr (s
, _
) =>
301 (print ("System error: "^ s ^
"\n");
303 handle OpenSSL
.OpenSSL _
=> ();
307 OpenSSL
.shutdown sock
310 fun autodocBasis outdir
=
312 val dir
= Posix
.FileSys
.opendir Config
.libRoot
315 case Posix
.FileSys
.readdir dir
of
316 NONE
=> (Posix
.FileSys
.closedir dir
;
319 if String.isSuffix
".dtl" fname
then
320 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
328 Autodoc
.autodoc
{outdir
= outdir
, infiles
= files
}