Fix indentation in GPL headers
[hcoop/domtool2.git] / src / domain.sml
CommitLineData
a3698041
AC
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.
dac62e84 17 *)
a3698041
AC
18
19(* Domain-related primitive actions *)
20
21structure Domain :> DOMAIN = struct
22
23open Ast
24
25val befores = ref (fn (_ : string) => ())
26val afters = ref (fn (_ : string) => ())
27
28fun registerBefore f =
29 let
30 val old = !befores
31 in
32 befores := (fn x => (old x; f x))
33 end
34
35fun registerAfter f =
36 let
37 val old = !afters
38 in
39 afters := (fn x => (old x; f x))
40 end
41
42val current = ref ""
dac62e84
AC
43val currentPath = ref ""
44
45fun currentDomain () = !current
46
47fun domainFile name = TextIO.openOut (!currentPath ^ name)
48
49fun 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
a3698041
AC
70
71val _ = Env.registerContainer ("domain",
72 fn (_, [(EString dom, _)]) => (current := dom;
dac62e84 73 currentPath := getPath dom;
a3698041
AC
74 !befores dom;
75 StringMap.empty)
76 | _ => Env.badArgs "domain",
77 fn () => !afters (!current))
78
79end