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 366 - (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 : blume 366 val newEra : unit -> unit
15 : blume 265 val revalidateCwd : unit -> unit
16 :    
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 366 (revalidateCwd ();
129 :     THEN_CWD { stamp = cwdStamp (),
130 :     name = cwdName (),
131 :     id = cwdId () })
132 : blume 270
133 : blume 353 fun sameDirContext p = DIR_OF p
134 : blume 270
135 :     fun mkElab (cache, name) = let
136 :     val e : elaboration =
137 :     { stamp = !elabStamp, name = name, id = ref NONE }
138 : blume 265 in
139 : blume 270 cache := SOME e; e
140 :     end
141 :    
142 :     fun validElab NONE = NONE
143 :     | validElab (SOME (e as { stamp, name, id })) =
144 :     if stamp = !elabStamp then SOME e else NONE
145 :    
146 : blume 352 val rootName = P.toString { isAbs = true, arcs = [], vol = "" }
147 : blume 353 val rootId = ref (NONE: id option)
148 : blume 352
149 : blume 270 fun elabContext c =
150 :     case c of
151 : blume 353 THEN_CWD { stamp, name, id } =>
152 : blume 265 { stamp = !elabStamp, id = ref (SOME id),
153 : blume 353 name = if stamp = cwdStamp () orelse name = cwdName ()
154 : blume 270 then P.currentArc else name }
155 : blume 304 | CONFIG_ANCHOR { fetch, cache, config_name } =>
156 : blume 270 (case validElab (!cache) of
157 :     SOME e => e
158 :     | NONE => mkElab (cache, fetch ()))
159 : blume 353 | DIR_OF p => let
160 :     val { name, stamp, ... } = elab p
161 :     in
162 :     { name = P.dir name, stamp = stamp, id = ref NONE }
163 :     end
164 :     | ROOT => { stamp = !elabStamp, name = rootName, id = rootId }
165 : blume 265
166 : blume 305 and elab (PATH { context, spec, cache }) =
167 : blume 353 case validElab (!cache) of
168 :     SOME e => e
169 : blume 357 | NONE => mkElab (cache,
170 :     P.mkCanonical (P.concat
171 :     (#name (elabContext context), spec)))
172 : blume 270
173 : blume 265 (* get the file id (calls elab, so don't cache externally!) *)
174 :     fun id p = let
175 :     val { id, name, ... } = elab p
176 :     in
177 :     case !id of
178 :     NONE => let
179 :     val i = getId name
180 :     in
181 :     id := SOME i; i
182 :     end
183 :     | SOME i => i
184 :     end
185 :    
186 :     (* get the name as a string (calls elab, so don't cache externally!) *)
187 : blume 354 fun osstring p = #name (elab p)
188 : blume 265
189 : blume 272 (* get the context back *)
190 : blume 353 fun contextOf (PATH { context = c, ... }) = c
191 : blume 272 fun contextName c = #name (elabContext c)
192 :    
193 :     (* get the spec back *)
194 : blume 353 fun specOf (PATH { spec = s, ... }) = s
195 : blume 272
196 : blume 265 (* compare pathnames efficiently *)
197 :     fun compare (p1, p2) = compareId (id p1, id p2)
198 :    
199 : blume 305 fun fresh (context, spec) =
200 :     PATH { context = context, spec = spec, cache = ref NONE }
201 : blume 265
202 : blume 268 (* make an abstract path from a native string *)
203 : blume 352 fun native { spec, context } = let
204 :     val { isAbs, vol, arcs } = P.fromString spec
205 :     val relSpec = P.toString { isAbs = false, vol = vol, arcs = arcs }
206 :     in
207 :     if isAbs then fresh (ROOT, relSpec)
208 :     else fresh (context, relSpec)
209 :     end
210 : blume 268
211 : blume 265 (* make an abstract path from a standard string *)
212 : blume 318 fun standard mode { spec, context } = let
213 : blume 265 fun delim #"/" = true
214 :     | delim #"\\" = true (* accept DOS-style, too *)
215 :     | delim _ = false
216 :    
217 : blume 353 fun transl ".." = P.parentArc
218 :     | transl "." = P.currentArc
219 : blume 265 | transl arc = arc
220 :    
221 : blume 352 fun mk (arcs, context) =
222 : blume 268 fresh (context,
223 : blume 352 P.toString { isAbs = false, vol = "",
224 : blume 305 arcs = map transl arcs })
225 : blume 265 in
226 :     case String.fields delim spec of
227 : blume 353 [""] => impossible "AbsPath.standard: zero-length name"
228 :     | "" :: arcs => mk (arcs, ROOT)
229 :     | [] => impossible "AbsPath.standard: no fields"
230 : blume 304 | arcs as (arc1 :: _) =>
231 : blume 318 (case PathConfig.configAnchor mode arc1 of
232 : blume 352 NONE => mk (arcs, context)
233 : blume 265 | SOME fetch => let
234 :     val anchorcontext =
235 :     CONFIG_ANCHOR { fetch = fetch,
236 : blume 304 cache = ref NONE,
237 :     config_name = arc1 }
238 : blume 265 in
239 : blume 352 mk (arcs, anchorcontext)
240 : blume 265 end)
241 :     end
242 : blume 268
243 : blume 305 (* make a pickle-string *)
244 : blume 353 fun pickle warn (path, gpath) = let
245 :     fun p_p (PATH { spec, context, ... }) =
246 :     spec :: p_c context
247 :     and p_c ROOT = (warn true; ["r"])
248 :     | p_c (THEN_CWD _) = impossible "AbsPath.pickle: THEN_CWD"
249 :     | p_c (CONFIG_ANCHOR { config_name = n, ... }) = [n, "a"]
250 :     | p_c (DIR_OF p) =
251 :     if compare (p, gpath) = EQUAL then (warn false; ["c"])
252 :     else p_p p
253 : blume 305 in
254 : blume 340 p_p path
255 : blume 305 end
256 :    
257 : blume 353 fun unpickle mode (l, gpath) = let
258 :     fun u_p (s :: l) =
259 :     Option.map
260 :     (fn c => PATH { spec = s, context = c, cache = ref NONE })
261 :     (u_c l)
262 :     | u_p [] = NONE
263 :     and u_c ["r"] = SOME ROOT
264 :     | u_c ["c"] = SOME (DIR_OF gpath)
265 :     | u_c [n, "a"] =
266 : blume 318 (case PathConfig.configAnchor mode n of
267 : blume 353 NONE => NONE
268 :     | SOME fetch =>
269 :     SOME (CONFIG_ANCHOR { config_name = n,
270 :     fetch = fetch,
271 :     cache = ref NONE }))
272 :     | u_c l = Option.map DIR_OF (u_p l)
273 : blume 305 in
274 : blume 353 u_p l
275 : blume 305 end
276 :    
277 : blume 354 fun tstamp p = TStamp.fmodTime (osstring p)
278 : blume 268
279 : blume 354 fun descr (PATH { spec, context, ... }) = let
280 :     fun dir (x, l) =
281 : blume 357 case P.dir x of
282 : blume 354 "" => l
283 :     | d => d :: "/" :: l
284 :     fun d_c (CONFIG_ANCHOR { config_name = n, ... }, l) =
285 :     "$" :: n :: "/" :: l
286 :     | d_c (DIR_OF (PATH { spec, context, ... }), l) =
287 :     d_c (context, dir (spec, l))
288 :     | d_c (THEN_CWD _, l) = "./" :: l
289 :     | d_c (ROOT, l) = "/" :: l
290 : blume 274 in
291 : blume 354 concat (d_c (context, [spec]))
292 : blume 274 end
293 : blume 275
294 : blume 354 fun reAnchoredName (p, dirstring) = let
295 :     fun path (PATH { context, spec, ... }) = let
296 :     fun mk c = P.concat (c, spec)
297 :     in
298 :     Option.map mk (ctxt context)
299 :     end
300 :     and ctxt (CONFIG_ANCHOR { config_name = n, ... }) =
301 :     SOME (P.concat (dirstring, n))
302 :     | ctxt (DIR_OF p) = Option.map P.dir (path p)
303 :     | ctxt (THEN_CWD _) = (Say.say ["."]; NONE)
304 :     | ctxt ROOT = (Say.say ["/"]; NONE)
305 : blume 275 in
306 : blume 354 Option.map P.mkCanonical (path p)
307 : blume 275 end
308 : blume 265 end
309 :     end

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