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 (Tycheck
.allowExterns ();
62 foldl (fn (fname
, G
) => check
' G fname
) Env
.empty files
63 before Tycheck
.disallowExterns ())
68 val _
= ErrorMsg
.reset ()
69 val _
= Env
.preTycheck ()
73 if !ErrorMsg
.anyErrors
then
77 val _
= Tycheck
.disallowExterns ()
78 val _
= ErrorMsg
.reset ()
79 val prog
= Parse
.parse fname
81 if !ErrorMsg
.anyErrors
then
85 val G
' = Tycheck
.checkFile
b (Defaults
.tInit ()) prog
87 if !ErrorMsg
.anyErrors
then
97 val (G
, body
) = check fname
99 if !ErrorMsg
.anyErrors
then
105 val body
' = Reduce
.reduceExp G body
107 (*printd (PD
.hovBox (PD
.PPS
.Rel
0,
108 [PD
.string "Result:",
119 if !ErrorMsg
.anyErrors
then
122 Eval
.exec (Defaults
.eInit ()) body
'
123 | NONE
=> raise ErrorMsg
.Error
126 Config
.dispatcher ^
":" ^
Int.toString Config
.dispatcherPort
130 val uid
= Posix
.ProcEnv
.getuid ()
131 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
133 val () = Acl
.read Config
.aclFile
134 val () = Domain
.setUser user
138 val uid
= Posix
.ProcEnv
.getuid ()
139 val user
= Posix
.SysDB
.Passwd
.name (Posix
.SysDB
.getpwuid uid
)
141 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ user ^
".pem",
142 Config
.keyDir ^
"/" ^ user ^
"/key.pem",
145 val bio
= OpenSSL
.connect (context
, dispatcher
)
147 val inf
= TextIO.openIn fname
150 case TextIO.inputLine inf
of
151 NONE
=> String.concat (List.rev lines
)
152 | SOME line
=> loop (line
:: lines
)
157 Msg
.send (bio
, MsgConfig code
);
159 NONE
=> print
"Server closed connection unexpectedly.\n"
162 MsgOk
=> print
"Configuration succeeded.\n"
163 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
164 | _
=> print
"Unexpected server reply.\n";
167 handle ErrorMsg
.Error
=> ()
171 val () = Acl
.read Config
.aclFile
173 val context
= OpenSSL
.context (Config
.serverCert
,
176 val _
= Domain
.set_context context
178 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
181 case OpenSSL
.accept sock
of
185 val user
= OpenSSL
.peerCN bio
186 val () = print ("\nConnection from " ^ user ^
"\n")
187 val () = Domain
.setUser user
191 NONE
=> (OpenSSL
.close bio
192 handle OpenSSL
.OpenSSL _
=> ();
198 val _
= print
"Configuration:\n"
202 val outname
= OS
.FileSys
.tmpName ()
203 val outf
= TextIO.openOut outname
205 TextIO.output (outf
, code
);
206 TextIO.closeOut outf
;
208 Msg
.send (bio
, MsgOk
))
209 handle ErrorMsg
.Error
=>
210 (print
"Compilation error\n";
212 MsgError
"Error during configuration evaluation"))
213 | OpenSSL
.OpenSSL s
=>
214 (print
"OpenSSL error\n";
217 ("Error during configuration evaluation: "
219 OS
.FileSys
.remove outname
;
220 (ignore (OpenSSL
.readChar bio
);
222 handle OpenSSL
.OpenSSL _
=> ();
226 (Msg
.send (bio
, MsgError
"Unexpected command")
227 handle OpenSSL
.OpenSSL _
=> ();
229 handle OpenSSL
.OpenSSL _
=> ();
234 handle OpenSSL
.OpenSSL s
=>
235 (print ("OpenSSL error: " ^ s ^
"\n");
237 handle OpenSSL
.OpenSSL _
=> ();
239 | OS
.SysErr (s
, _
) =>
240 (print ("System error: " ^ s ^
"\n");
242 handle OpenSSL
.OpenSSL _
=> ();
245 print
"Listening for connections....\n";
247 OpenSSL
.shutdown sock
252 val host
= Slave
.hostname ()
254 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
255 Config
.keyDir ^
"/" ^ host ^
"/key.pem",
258 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
261 case OpenSSL
.accept sock
of
265 val peer
= OpenSSL
.peerCN bio
266 val () = print ("\nConnection from " ^ peer ^
"\n")
268 if peer
<> Config
.dispatcherName
then
269 (print
"Not authorized!\n";
275 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
278 MsgFile file
=> loop
' (file
:: files
)
279 | MsgDoFiles
=> (Slave
.handleChanges files
;
280 Msg
.send (bio
, MsgOk
))
281 | _
=> (print
"Dispatcher sent unexpected command\n";
282 Msg
.send (bio
, MsgError
"Unexpected command"))
285 ignore (OpenSSL
.readChar bio
);
289 end handle OpenSSL
.OpenSSL s
=>
290 (print ("OpenSSL error: "^ s ^
"\n");
292 handle OpenSSL
.OpenSSL _
=> ();
294 | OS
.SysErr (s
, _
) =>
295 (print ("System error: "^ s ^
"\n");
297 handle OpenSSL
.OpenSSL _
=> ();
301 OpenSSL
.shutdown sock
304 fun autodocBasis outdir
=
306 val dir
= Posix
.FileSys
.opendir Config
.libRoot
309 case Posix
.FileSys
.readdir dir
of
310 NONE
=> (Posix
.FileSys
.closedir dir
;
313 if String.isSuffix
".dtl" fname
then
314 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
322 Autodoc
.autodoc
{outdir
= outdir
, infiles
= files
}