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

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