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
143 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ user ^
".pem",
144 Config
.keyDir ^
"/" ^ user ^
".pem",
147 val bio
= OpenSSL
.connect (context
, dispatcher
)
149 val inf
= TextIO.openIn fname
152 case TextIO.inputLine inf
of
153 NONE
=> String.concat (List.rev lines
)
154 | SOME line
=> loop (line
:: lines
)
159 Msg
.send (bio
, MsgConfig code
);
161 NONE
=> print
"Server closed connection unexpectedly.\n"
164 MsgOk
=> print
"Configuration succeeded.\n"
165 | MsgError s
=> print ("Configuration failed: " ^ s ^
"\n")
166 | _
=> print
"Unexpected server reply.\n";
169 handle ErrorMsg
.Error
=> ()
173 val () = Acl
.read Config
.aclFile
175 val context
= OpenSSL
.context (Config
.serverCert
,
178 val _
= Domain
.set_context context
180 val sock
= OpenSSL
.listen (context
, Config
.dispatcherPort
)
183 case OpenSSL
.accept sock
of
187 val user
= OpenSSL
.peerCN bio
188 val () = print ("\nConnection from " ^ user ^
"\n")
189 val () = Domain
.setUser user
193 NONE
=> (OpenSSL
.close bio
194 handle OpenSSL
.OpenSSL _
=> ();
200 val _
= print
"Configuration:\n"
204 val outname
= OS
.FileSys
.tmpName ()
205 val outf
= TextIO.openOut outname
207 TextIO.output (outf
, code
);
208 TextIO.closeOut outf
;
210 Msg
.send (bio
, MsgOk
))
211 handle ErrorMsg
.Error
=>
212 (print
"Compilation error\n";
214 MsgError
"Error during configuration evaluation"))
215 | OpenSSL
.OpenSSL s
=>
216 (print
"OpenSSL error\n";
219 ("Error during configuration evaluation: "
221 OS
.FileSys
.remove outname
;
222 (ignore (OpenSSL
.readChar bio
);
224 handle OpenSSL
.OpenSSL _
=> ();
228 (Msg
.send (bio
, MsgError
"Unexpected command")
229 handle OpenSSL
.OpenSSL _
=> ();
231 handle OpenSSL
.OpenSSL _
=> ();
238 OpenSSL
.shutdown sock
243 val host
= hostname ()
245 val context
= OpenSSL
.context (Config
.certDir ^
"/" ^ host ^
".pem",
246 Config
.keyDir ^
"/" ^ host ^
".pem",
249 val sock
= OpenSSL
.listen (context
, Config
.slavePort
)
252 case OpenSSL
.accept sock
of
256 val peer
= OpenSSL
.peerCN bio
257 val () = print ("\nConnection from " ^ peer ^
"\n")
259 if peer
<> Config
.dispatcherName
then
260 (print
"Not authorized!\n";
266 NONE
=> print
"Dispatcher closed connection unexpectedly\n"
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"))
276 ignore (OpenSSL
.readChar bio
);
280 end handle OpenSSL
.OpenSSL s
=>
281 (print ("OpenSSL error: "^ s ^
"\n");
283 handle OpenSSL
.OpenSSL _
=> ();
287 OpenSSL
.shutdown sock
290 fun autodocBasis outdir
=
292 val dir
= Posix
.FileSys
.opendir Config
.libRoot
295 case Posix
.FileSys
.readdir dir
of
296 NONE
=> (Posix
.FileSys
.closedir dir
;
299 if String.isSuffix
".dtl" fname
then
300 loop (OS
.Path
.joinDirFile
{dir
= Config
.libRoot
,
308 Autodoc
.autodoc
{outdir
= outdir
, infiles
= files
}