Add LICENSE
[hcoop/domtool2.git] / src / domain.sml
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 (* Domain-related primitive actions *)
20
21 structure Domain :> DOMAIN = struct
22
23 open Ast
24
25 val befores = ref (fn (_ : string) => ())
26 val afters = ref (fn (_ : string) => ())
27
28 fun registerBefore f =
29 let
30 val old = !befores
31 in
32 befores := (fn x => (old x; f x))
33 end
34
35 fun registerAfter f =
36 let
37 val old = !afters
38 in
39 afters := (fn x => (old x; f x))
40 end
41
42 val current = ref ""
43 val currentPath = ref ""
44
45 fun currentDomain () = !current
46
47 fun domainFile name = TextIO.openOut (!currentPath ^ name)
48
49 fun getPath domain =
50 let
51 val toks = String.fields (fn ch => ch = #".") domain
52
53 val elems = foldr (fn (piece, elems) =>
54 let
55 val elems = piece :: elems
56 val path = String.concatWith "/" (Config.configRoot :: rev elems)
57 in
58 (if Posix.FileSys.ST.isDir
59 (Posix.FileSys.stat path) then
60 ()
61 else
62 (OS.FileSys.remove path;
63 OS.FileSys.mkDir path))
64 handle OS.SysErr _ => OS.FileSys.mkDir path;
65 elems
66 end) [] toks
67 in
68 String.concatWith "/" (Config.configRoot :: rev elems)
69 end
70
71 val _ = Env.registerContainer ("domain",
72 fn (_, [(EString dom, _)]) => (current := dom;
73 currentPath := getPath dom;
74 !befores dom;
75 StringMap.empty)
76 | _ => Env.badArgs "domain",
77 fn () => !afters (!current))
78
79 end