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 364 - (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 : blume 305 type ord_key = t
13 : blume 265
14 :     val revalidateCwd : unit -> unit
15 : blume 277 val newEra : unit -> unit
16 : blume 265
17 : blume 270 val cwdContext: unit -> context
18 : blume 353 val sameDirContext: t -> context
19 : blume 270
20 : blume 354 val osstring : t -> string
21 :     val descr : t -> string
22 : blume 265 val compare : t * t -> order
23 : blume 353 val contextOf : t -> context
24 :     val specOf : t -> string
25 : blume 272 val contextName : context -> string
26 : blume 265
27 : blume 352 (* Replace the anchor context in the path argument with the
28 :     * given context. Returns NONE if there was no anchor context. *)
29 : blume 354 val reAnchoredName : t * string -> string option
30 : blume 352
31 : blume 270 val native : { context: context, spec: string } -> t
32 : blume 318 val standard : PathConfig.mode -> { context: context, spec: string } -> t
33 : blume 268
34 : blume 353 (* the second path argument is the path of the group spec that
35 :     * pickling is based upon. *)
36 : blume 340 val pickle : (bool -> unit) -> t * t -> string list
37 :     val unpickle : PathConfig.mode -> string list * t -> t option
38 : blume 305
39 : blume 274 val tstamp : t -> TStamp.t
40 : blume 265 end
41 :    
42 :     structure AbsPath :> ABSPATH = struct
43 :    
44 :     structure P = OS.Path
45 :     structure F = OS.FileSys
46 : blume 340 val impossible = GenericVC.ErrorMsg.impossible
47 : blume 265
48 :     (* unique file id that can handle absent files *)
49 :     datatype id =
50 :     PRESENT of F.file_id
51 :     | ABSENT of string
52 :    
53 :     (* comparison of unique file ids *)
54 :     fun compareId (PRESENT fid, PRESENT fid') = F.compare (fid, fid')
55 :     | compareId (ABSENT _, PRESENT _) = LESS
56 :     | compareId (PRESENT _, ABSENT _) = GREATER
57 :     | compareId (ABSENT s, ABSENT s') = String.compare (s, s')
58 :    
59 : blume 270 fun getId f = (PRESENT (F.fileId f) handle _ => ABSENT f)
60 : blume 265
61 :     type elaboration = { stamp : unit ref,
62 :     name : string,
63 :     id : id option ref }
64 : blume 270
65 : blume 265 (* When a relative name is to be looked up wrt. CUR:
66 :     * - if the cwd hasn't changed since, then use relative path
67 :     * - if the cwd has changed, then make absolute path using name
68 :     * If we come back to the original dir, then ideally we should
69 :     * re-validate the stamp, but that would require having a cwd
70 :     * history -- and, thus, is probably not worth the effort.
71 :     *)
72 :    
73 :     type cwdinfo = { stamp: unit ref, name: string, id: id }
74 : blume 270
75 :     datatype context =
76 : blume 353 THEN_CWD of cwdinfo
77 : blume 265 | CONFIG_ANCHOR of { fetch: unit -> string,
78 : blume 304 cache: elaboration option ref,
79 :     config_name: string }
80 : blume 353 | DIR_OF of t
81 : blume 352 | ROOT
82 : blume 270
83 : blume 324 and t =
84 : blume 270 PATH of { context: context,
85 : blume 265 spec: string,
86 :     cache: elaboration option ref }
87 :    
88 : blume 305 type ord_key = t
89 :    
90 : blume 265 local
91 :     val elabStamp = ref (ref ())
92 :     val cwdInfoCache : cwdinfo option ref = ref NONE
93 :     fun cwdInfo () =
94 :     case !cwdInfoCache of
95 :     SOME i => i
96 :     | NONE => let
97 :     val stamp = ref ()
98 :     val name = F.getDir ()
99 :     val id = PRESENT (F.fileId name)
100 :     val i = { stamp = stamp, name = name, id = id }
101 :     in
102 :     cwdInfoCache := SOME i;
103 :     i
104 :     end
105 :     val cwdStamp = #stamp o cwdInfo
106 :     val cwdName = #name o cwdInfo
107 :     val cwdId = #id o cwdInfo
108 :     in
109 :     (* start a new era (i.e., invalidate all previous elaborations) *)
110 :     fun newEra () = elabStamp := ref ()
111 :    
112 :     (* make sure the cwd is consistent *)
113 :     fun revalidateCwd () =
114 :     case !cwdInfoCache of
115 :     NONE => ignore (cwdInfo ())
116 :     | SOME { name, id, ... } => let
117 :     val name' = F.getDir ()
118 :     val id' = PRESENT (F.fileId name')
119 :     in
120 :     if compareId (id, id') <> EQUAL then
121 :     (newEra ();
122 :     cwdInfoCache := SOME { stamp = ref (),
123 :     name = name', id = id' })
124 :     else ()
125 :     end
126 :    
127 : blume 270 fun cwdContext () =
128 : blume 353 THEN_CWD { stamp = cwdStamp (), name = cwdName (), id = cwdId () }
129 : blume 270
130 : blume 353 fun sameDirContext p = DIR_OF p
131 : blume 270
132 :     fun mkElab (cache, name) = let
133 :     val e : elaboration =
134 :     { stamp = !elabStamp, name = name, id = ref NONE }
135 : blume 265 in
136 : blume 270 cache := SOME e; e
137 :     end
138 :    
139 :     fun validElab NONE = NONE
140 :     | validElab (SOME (e as { stamp, name, id })) =
141 :     if stamp = !elabStamp then SOME e else NONE
142 :    
143 : blume 352 val rootName = P.toString { isAbs = true, arcs = [], vol = "" }
144 : blume 353 val rootId = ref (NONE: id option)
145 : blume 352
146 : blume 270 fun elabContext c =
147 :     case c of
148 : blume 353 THEN_CWD { stamp, name, id } =>
149 : blume 265 { stamp = !elabStamp, id = ref (SOME id),
150 : blume 353 name = if stamp = cwdStamp () orelse name = cwdName ()
151 : blume 270 then P.currentArc else name }
152 : blume 304 | CONFIG_ANCHOR { fetch, cache, config_name } =>
153 : blume 270 (case validElab (!cache) of
154 :     SOME e => e
155 :     | NONE => mkElab (cache, fetch ()))
156 : blume 353 | DIR_OF p => let
157 :     val { name, stamp, ... } = elab p
158 :     in
159 :     { name = P.dir name, stamp = stamp, id = ref NONE }
160 :     end
161 :     | ROOT => { stamp = !elabStamp, name = rootName, id = rootId }
162 : blume 265
163 : blume 305 and elab (PATH { context, spec, cache }) =
164 : blume 353 case validElab (!cache) of
165 :     SOME e => e
166 : blume 357 | NONE => mkElab (cache,
167 :     P.mkCanonical (P.concat
168 :     (#name (elabContext context), spec)))
169 : blume 270
170 : blume 265 (* get the file id (calls elab, so don't cache externally!) *)
171 :     fun id p = let
172 :     val { id, name, ... } = elab p
173 :     in
174 :     case !id of
175 :     NONE => let
176 :     val i = getId name
177 :     in
178 :     id := SOME i; i
179 :     end
180 :     | SOME i => i
181 :     end
182 :    
183 :     (* get the name as a string (calls elab, so don't cache externally!) *)
184 : blume 354 fun osstring p = #name (elab p)
185 : blume 265
186 : blume 272 (* get the context back *)
187 : blume 353 fun contextOf (PATH { context = c, ... }) = c
188 : blume 272 fun contextName c = #name (elabContext c)
189 :    
190 :     (* get the spec back *)
191 : blume 353 fun specOf (PATH { spec = s, ... }) = s
192 : blume 272
193 : blume 265 (* compare pathnames efficiently *)
194 :     fun compare (p1, p2) = compareId (id p1, id p2)
195 :    
196 : blume 305 fun fresh (context, spec) =
197 :     PATH { context = context, spec = spec, cache = ref NONE }
198 : blume 265
199 : blume 268 (* make an abstract path from a native string *)
200 : blume 352 fun native { spec, context } = let
201 :     val { isAbs, vol, arcs } = P.fromString spec
202 :     val relSpec = P.toString { isAbs = false, vol = vol, arcs = arcs }
203 :     in
204 :     if isAbs then fresh (ROOT, relSpec)
205 :     else fresh (context, relSpec)
206 :     end
207 : blume 268
208 : blume 265 (* make an abstract path from a standard string *)
209 : blume 318 fun standard mode { spec, context } = let
210 : blume 265 fun delim #"/" = true
211 :     | delim #"\\" = true (* accept DOS-style, too *)
212 :     | delim _ = false
213 :    
214 : blume 353 fun transl ".." = P.parentArc
215 :     | transl "." = P.currentArc
216 : blume 265 | transl arc = arc
217 :    
218 : blume 352 fun mk (arcs, context) =
219 : blume 268 fresh (context,
220 : blume 352 P.toString { isAbs = false, vol = "",
221 : blume 305 arcs = map transl arcs })
222 : blume 265 in
223 :     case String.fields delim spec of
224 : blume 353 [""] => impossible "AbsPath.standard: zero-length name"
225 :     | "" :: arcs => mk (arcs, ROOT)
226 :     | [] => impossible "AbsPath.standard: no fields"
227 : blume 304 | arcs as (arc1 :: _) =>
228 : blume 318 (case PathConfig.configAnchor mode arc1 of
229 : blume 352 NONE => mk (arcs, context)
230 : blume 265 | SOME fetch => let
231 :     val anchorcontext =
232 :     CONFIG_ANCHOR { fetch = fetch,
233 : blume 304 cache = ref NONE,
234 :     config_name = arc1 }
235 : blume 265 in
236 : blume 352 mk (arcs, anchorcontext)
237 : blume 265 end)
238 :     end
239 : blume 268
240 : blume 305 (* make a pickle-string *)
241 : blume 353 fun pickle warn (path, gpath) = let
242 :     fun p_p (PATH { spec, context, ... }) =
243 :     spec :: p_c context
244 :     and p_c ROOT = (warn true; ["r"])
245 :     | p_c (THEN_CWD _) = impossible "AbsPath.pickle: THEN_CWD"
246 :     | p_c (CONFIG_ANCHOR { config_name = n, ... }) = [n, "a"]
247 :     | p_c (DIR_OF p) =
248 :     if compare (p, gpath) = EQUAL then (warn false; ["c"])
249 :     else p_p p
250 : blume 305 in
251 : blume 340 p_p path
252 : blume 305 end
253 :    
254 : blume 353 fun unpickle mode (l, gpath) = let
255 :     fun u_p (s :: l) =
256 :     Option.map
257 :     (fn c => PATH { spec = s, context = c, cache = ref NONE })
258 :     (u_c l)
259 :     | u_p [] = NONE
260 :     and u_c ["r"] = SOME ROOT
261 :     | u_c ["c"] = SOME (DIR_OF gpath)
262 :     | u_c [n, "a"] =
263 : blume 318 (case PathConfig.configAnchor mode n of
264 : blume 353 NONE => NONE
265 :     | SOME fetch =>
266 :     SOME (CONFIG_ANCHOR { config_name = n,
267 :     fetch = fetch,
268 :     cache = ref NONE }))
269 :     | u_c l = Option.map DIR_OF (u_p l)
270 : blume 305 in
271 : blume 353 u_p l
272 : blume 305 end
273 :    
274 : blume 354 fun tstamp p = TStamp.fmodTime (osstring p)
275 : blume 268
276 : blume 354 fun descr (PATH { spec, context, ... }) = let
277 :     fun dir (x, l) =
278 : blume 357 case P.dir x of
279 : blume 354 "" => l
280 :     | d => d :: "/" :: l
281 :     fun d_c (CONFIG_ANCHOR { config_name = n, ... }, l) =
282 :     "$" :: n :: "/" :: l
283 :     | d_c (DIR_OF (PATH { spec, context, ... }), l) =
284 :     d_c (context, dir (spec, l))
285 :     | d_c (THEN_CWD _, l) = "./" :: l
286 :     | d_c (ROOT, l) = "/" :: l
287 : blume 274 in
288 : blume 354 concat (d_c (context, [spec]))
289 : blume 274 end
290 : blume 275
291 : blume 354 fun reAnchoredName (p, dirstring) = let
292 :     fun path (PATH { context, spec, ... }) = let
293 :     fun mk c = P.concat (c, spec)
294 :     in
295 :     Option.map mk (ctxt context)
296 :     end
297 :     and ctxt (CONFIG_ANCHOR { config_name = n, ... }) =
298 :     SOME (P.concat (dirstring, n))
299 :     | ctxt (DIR_OF p) = Option.map P.dir (path p)
300 :     | ctxt (THEN_CWD _) = (Say.say ["."]; NONE)
301 :     | ctxt ROOT = (Say.say ["/"]; NONE)
302 : blume 275 in
303 : blume 354 Option.map P.mkCanonical (path p)
304 : blume 275 end
305 : blume 265 end
306 :     end

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