Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/cm/paths/abspath.sml
ViewVC logotype

Annotation of /sml/trunk/src/cm/paths/abspath.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 272 - (view) (download)

1 : blume 265 (*
2 : blume 267 * Operations over abstract path names.
3 : blume 265 *
4 : blume 267 * Copyright (c) 1999 by Lucent Technologies, Bell Laboratories
5 : blume 265 *
6 : blume 267 * Author: Matthias Blume (blume@cs.princeton.edu)
7 : blume 265 *)
8 :     signature ABSPATH = sig
9 :    
10 : blume 270 type context
11 : blume 265 type t
12 :    
13 :     val revalidateCwd : unit -> unit
14 :    
15 : blume 270 val cwdContext: unit -> context
16 :     val configContext: (unit -> string) -> context
17 :     val relativeContext: t -> context
18 :    
19 : blume 265 val name : t -> string
20 :     val compare : t * t -> order
21 : blume 272 val context : t -> context
22 :     val spec : t -> string
23 :     val contextName : context -> string
24 : blume 265
25 : blume 270 val native : { context: context, spec: string } -> t
26 :     val standard : { context: context, spec: string } -> t
27 : blume 268
28 :     val joinDirFile : { dir: t, file: string } -> t
29 :     val splitDirFile : t -> { dir: t, file: string }
30 :     val dir : t -> t
31 :     val file : t -> string
32 : blume 265 end
33 :    
34 :     structure AbsPath :> ABSPATH = struct
35 :    
36 :     structure P = OS.Path
37 :     structure F = OS.FileSys
38 :    
39 :     (* unique file id that can handle absent files *)
40 :     datatype id =
41 :     PRESENT of F.file_id
42 :     | ABSENT of string
43 :    
44 :     (* comparison of unique file ids *)
45 :     fun compareId (PRESENT fid, PRESENT fid') = F.compare (fid, fid')
46 :     | compareId (ABSENT _, PRESENT _) = LESS
47 :     | compareId (PRESENT _, ABSENT _) = GREATER
48 :     | compareId (ABSENT s, ABSENT s') = String.compare (s, s')
49 :    
50 : blume 270 fun getId f = (PRESENT (F.fileId f) handle _ => ABSENT f)
51 : blume 265
52 :     type elaboration = { stamp : unit ref,
53 :     name : string,
54 :     id : id option ref }
55 : blume 270
56 : blume 265 (* When a relative name is to be looked up wrt. CUR:
57 :     * - if the cwd hasn't changed since, then use relative path
58 :     * - if the cwd has changed, then make absolute path using name
59 :     * If we come back to the original dir, then ideally we should
60 :     * re-validate the stamp, but that would require having a cwd
61 :     * history -- and, thus, is probably not worth the effort.
62 :     *)
63 :    
64 :     type cwdinfo = { stamp: unit ref, name: string, id: id }
65 : blume 270
66 :     datatype context =
67 : blume 265 CUR of cwdinfo
68 :     | CONFIG_ANCHOR of { fetch: unit -> string,
69 :     cache: elaboration option ref }
70 : blume 270 | RELATIVE of t
71 :    
72 :     and t =
73 :     PATH of { context: context,
74 : blume 265 spec: string,
75 :     cache: elaboration option ref }
76 :    
77 :     local
78 :     val elabStamp = ref (ref ())
79 :     val cwdInfoCache : cwdinfo option ref = ref NONE
80 :     fun cwdInfo () =
81 :     case !cwdInfoCache of
82 :     SOME i => i
83 :     | NONE => let
84 :     val stamp = ref ()
85 :     val name = F.getDir ()
86 :     val id = PRESENT (F.fileId name)
87 :     val i = { stamp = stamp, name = name, id = id }
88 :     in
89 :     cwdInfoCache := SOME i;
90 :     i
91 :     end
92 :     val cwdStamp = #stamp o cwdInfo
93 :     val cwdName = #name o cwdInfo
94 :     val cwdId = #id o cwdInfo
95 :     fun invalidateCwdInfo () = cwdInfoCache := NONE
96 :     in
97 :     (* start a new era (i.e., invalidate all previous elaborations) *)
98 :     fun newEra () = elabStamp := ref ()
99 :    
100 :     (* make sure the cwd is consistent *)
101 :     fun revalidateCwd () =
102 :     case !cwdInfoCache of
103 :     NONE => ignore (cwdInfo ())
104 :     | SOME { name, id, ... } => let
105 :     val name' = F.getDir ()
106 :     val id' = PRESENT (F.fileId name')
107 :     in
108 :     if compareId (id, id') <> EQUAL then
109 :     (newEra ();
110 :     cwdInfoCache := SOME { stamp = ref (),
111 :     name = name', id = id' })
112 :     else ()
113 :     end
114 :    
115 : blume 270 fun cwdContext () =
116 :     CUR { stamp = cwdStamp (), name = cwdName (), id = cwdId () }
117 :    
118 :     fun configContext fetch =
119 :     CONFIG_ANCHOR { fetch = fetch, cache = ref NONE }
120 :    
121 :     fun relativeContext p = RELATIVE p
122 :    
123 :     fun mkElab (cache, name) = let
124 :     val e : elaboration =
125 :     { stamp = !elabStamp, name = name, id = ref NONE }
126 : blume 265 in
127 : blume 270 cache := SOME e; e
128 :     end
129 :    
130 :     fun validElab NONE = NONE
131 :     | validElab (SOME (e as { stamp, name, id })) =
132 :     if stamp = !elabStamp then SOME e else NONE
133 :    
134 :     fun elabContext c =
135 :     case c of
136 : blume 265 CUR { stamp, name, id } =>
137 :     { stamp = !elabStamp, id = ref (SOME id),
138 : blume 270 name = if stamp = cwdStamp () orelse
139 :     name = cwdName ()
140 :     then P.currentArc else name }
141 :     | CONFIG_ANCHOR { fetch, cache } =>
142 :     (case validElab (!cache) of
143 :     SOME e => e
144 :     | NONE => mkElab (cache, fetch ()))
145 :     | RELATIVE p => elab p
146 : blume 265
147 : blume 270 and elab (PATH { context, spec, cache }) =
148 :     (case validElab (!cache) of
149 :     SOME e => e
150 :     | NONE => let
151 :     val name =
152 :     if P.isAbsolute spec then spec
153 :     else P.mkCanonical
154 :     (P.concat (#name (elabContext context),
155 :     spec))
156 :     in
157 :     mkElab (cache, name)
158 :     end)
159 :    
160 : blume 265 (* get the file id (calls elab, so don't cache externally!) *)
161 :     fun id p = let
162 :     val { id, name, ... } = elab p
163 :     in
164 :     case !id of
165 :     NONE => let
166 :     val i = getId name
167 :     in
168 :     id := SOME i; i
169 :     end
170 :     | SOME i => i
171 :     end
172 :    
173 :     (* get the name as a string (calls elab, so don't cache externally!) *)
174 :     fun name p = #name (elab p)
175 :    
176 : blume 272 (* get the context back *)
177 :     fun context (PATH { context = c, ... }) = c
178 :     fun contextName c = #name (elabContext c)
179 :    
180 :     (* get the spec back *)
181 :     fun spec (PATH { spec = s, ... }) = s
182 :    
183 : blume 265 (* compare pathnames efficiently *)
184 :     fun compare (p1, p2) = compareId (id p1, id p2)
185 :    
186 : blume 268 fun fresh (context, spec) =
187 : blume 270 PATH { context = context, spec = spec, cache = ref NONE }
188 : blume 265
189 : blume 268 (* make an abstract path from a native string *)
190 :     fun native { spec, context } = fresh (context, spec)
191 :    
192 : blume 265 (* make an abstract path from a standard string *)
193 :     fun standard { spec, context } = let
194 :     fun delim #"/" = true
195 :     | delim #"\\" = true (* accept DOS-style, too *)
196 :     | delim _ = false
197 :    
198 :     fun transl ".." = OS.Path.parentArc
199 :     | transl "." = OS.Path.currentArc
200 :     | transl arc = arc
201 :    
202 :     fun mk (isAbs, arcs, context) =
203 : blume 268 fresh (context,
204 :     P.toString { isAbs = isAbs, vol = "",
205 :     arcs = map transl arcs })
206 : blume 265 in
207 :     case String.fields delim spec of
208 :     "" :: arcs => mk (true, arcs, context)
209 :     | [] => mk (false, [], context)
210 :     | arcs as (arc1 :: arcn) =>
211 :     (case PathConfig.configAnchor arc1 of
212 :     NONE => mk (false, arcs, context)
213 :     | SOME fetch => let
214 :     val anchorcontext =
215 :     CONFIG_ANCHOR { fetch = fetch,
216 :     cache = ref NONE }
217 :     in
218 :     mk (false, arcn, anchorcontext)
219 :     end)
220 :     end
221 : blume 268
222 : blume 269 (* . and .. are not permitted as file parameter *)
223 : blume 270 fun joinDirFile { dir = PATH { context, spec, ... }, file } =
224 : blume 269 if file = P.currentArc orelse file = P.parentArc then
225 :     raise Fail "AbsPath.joinDirFile: . or .."
226 : blume 270 else fresh (context, P.joinDirFile { dir = spec, file = file })
227 : blume 268
228 : blume 269 (* splitDirFile never walks past a context.
229 :     * Moreover, it is an error to split something that ends in "..". *)
230 : blume 270 fun splitDirFile (PATH { context, spec, ... }) = let
231 :     fun loop "" =
232 :     raise Fail "AbsPath.splitDirFile: tried to split a context"
233 :     | loop spec = let
234 :     val { dir, file } = P.splitDirFile spec
235 :     in
236 :     if file = P.currentArc then loop dir
237 :     else if file = P.parentArc then
238 :     raise Fail "AbsPath.splitDirFile: <path>/.."
239 :     else (dir, file)
240 :     end
241 :     val (dir, file) = loop spec
242 :     val dir = if dir = "" then P.currentArc else dir
243 :     in
244 :     { dir = fresh (context, dir), file = file }
245 :     end
246 : blume 268
247 :     val dir = #dir o splitDirFile
248 :     val file = #file o splitDirFile
249 : blume 265 end
250 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0