DIRECTORY FileNames, FileNameExtras, FS USING [EnumerateForNames, Error, FileInfo, GetDefaultWDir, NameProc], List USING [AList, Assoc, PutAssoc], ProcessProps USING [GetPropList], RefText USING [AppendRope, New], Rope USING [Cat, Concat, Fetch, Find, FromRefText, Index, IsEmpty, Length, Replace, ROPE, SkipTo, Substr, Translate, TranslatorType], UserCredentials USING [Get]; FileNamesImpl: CEDAR PROGRAM IMPORTS FS, List, ProcessProps, RefText, Rope, UserCredentials EXPORTS FileNames, FileNameExtras = BEGIN FileWithSearchRules: PUBLIC PROC [root: Rope.ROPE, defaultExtension: Rope.ROPE, requireExtension: BOOL _ TRUE, requireExact: BOOL _ TRUE, searchRules: REF ANY] RETURNS [fullPath: Rope.ROPE _ NIL, ambiguous: BOOL _ FALSE] = { fullPathName: BOOL _ FALSE; withExt: Rope.ROPE _ NIL; withStarExt: Rope.ROPE _ NIL; rules: LIST OF REF ANY; ropeRules: LIST OF Rope.ROPE; list: LIST OF REF ANY; ropeList: LIST OF Rope.ROPE; hasExtension: BOOL; hasVersion: BOOL; doRoot, doWithExt, doWithStarExt: BOOL; Try: PROC [name: Rope.ROPE, wDir: Rope.ROPE _ NIL] RETURNS [found: BOOL] = { IF name.IsEmpty[] THEN RETURN[FALSE]; fullPath _ FS.FileInfo[name: name, wDir: wDir ! FS.Error => IF error.group = user THEN CONTINUE].fullFName; IF NOT fullPath.IsEmpty[] THEN { fullPath _ ConvertToSlashFormat[fullPath]; RETURN[TRUE]; }; RETURN[FALSE]; }; UniqueMatch: PROC [name: Rope.ROPE, wDir: Rope.ROPE _ NIL] RETURNS [found: BOOL] = { lst: LIST OF Rope.ROPE _ NIL; p: FS.NameProc = { continue _ lst = NIL; -- give up after this name if it is the second one IF lst # NIL THEN ambiguous _ TRUE; lst _ CONS[fullFName, lst]; }; IF name.IsEmpty[] THEN { fullPath _ NIL; RETURN[TRUE]; }; FS.EnumerateForNames[pattern: name, proc: p, wDir: wDir ! FS.Error => IF error.group = $user THEN CONTINUE]; IF lst = NIL THEN RETURN[FALSE]; IF ambiguous THEN { fullPath _ NIL; RETURN[TRUE]; }; ambiguous _ TRUE; fullPath _ ConvertToSlashFormat[lst.first]; RETURN[TRUE]; }; IF root.IsEmpty[] THEN RETURN[NIL]; root _ ResolveRelativePath[root]; fullPathName _ root.Fetch[0] = '/ OR root.Fetch[0] = '[; hasExtension _ root.Find["."] # -1; hasVersion _ root.Find["!"] # -1; IF requireExtension AND NOT hasExtension AND hasVersion THEN RETURN[NIL]; IF requireExtension AND hasExtension THEN { IF Rope.Find[s1: root, s2: defaultExtension, pos1: 0, case: FALSE] # (Rope.Index[s1: root, s2: "!", pos1: 0, case: TRUE] - defaultExtension.Length[]) THEN RETURN[NIL]; }; IF NOT hasExtension THEN { withExt _ Rope.Concat[root, defaultExtension]; withStarExt _ Rope.Cat[root, "*", defaultExtension]; IF root.Find["!"] = -1 THEN { withExt _ Rope.Concat[withExt, "!H"]; withStarExt _ Rope.Concat[withStarExt, "!H"]; }; }; doRoot _ NOT requireExtension OR hasExtension; doWithExt _ NOT hasExtension; doWithStarExt _ (NOT requireExact) AND (NOT hasExtension); IF fullPathName THEN { IF doRoot AND Try[name: root] THEN RETURN; IF doWithExt AND Try[name: withExt] THEN RETURN; IF doWithStarExt AND UniqueMatch[name: withStarExt] THEN RETURN; } ELSE { IF doRoot AND Try[name: root] THEN RETURN; WITH searchRules SELECT FROM lra: LIST OF REF ANY => rules _ lra; lr: LIST OF Rope.ROPE => ropeRules _ lr; ENDCASE; list _ rules; ropeList _ ropeRules; IF doRoot THEN { WHILE list # NIL DO IF Try[name: root, wDir: NARROW[list.first, Rope.ROPE]] THEN RETURN; list _ list.rest; ENDLOOP; WHILE ropeList # NIL DO IF Try[name: root, wDir: ropeList.first] THEN RETURN; ropeList _ ropeList.rest; ENDLOOP; }; IF doWithExt AND Try[name: withExt] THEN RETURN; IF doWithExt THEN { list _ rules; ropeList _ ropeRules; WHILE list # NIL DO IF Try[name: withExt, wDir: NARROW[list.first, Rope.ROPE]] THEN RETURN; list _ list.rest; ENDLOOP; WHILE ropeList # NIL DO IF Try[name: withExt, wDir: ropeList.first] THEN RETURN; ropeList _ ropeList.rest; ENDLOOP; }; IF doWithStarExt THEN { IF UniqueMatch[name: withStarExt] THEN RETURN; list _ rules; ropeList _ ropeRules; WHILE list # NIL DO IF UniqueMatch[name: withStarExt, wDir: NARROW[list.first, Rope.ROPE]] THEN RETURN; list _ list.rest; ENDLOOP; WHILE ropeList # NIL DO IF UniqueMatch[name: withStarExt, wDir: ropeList.first] THEN RETURN; ropeList _ ropeList.rest; ENDLOOP; }; }; }; Parent: PUBLIC PROC [path: Rope.ROPE] RETURNS [Rope.ROPE] = { pathLength: INT; pos: INT; path _ ConvertToSlashFormat[path]; pathLength _ path.Length[]; IF pathLength <= 1 THEN RETURN[NIL]; pos _ IF path.Fetch[pathLength - 1] = '/ THEN pathLength - 2 ELSE pathLength - 1; RETURN [DirectoryContaining[path: path, pos: pos]]; }; ParseState: TYPE = {idle, slash, oneDot, twoDots}; ResolveRelativePath: PUBLIC PROC [path: Rope.ROPE] RETURNS [Rope.ROPE] = { i: INT _ 0; state: ParseState _ slash; pathLength: INT; out: REF TEXT _ NIL; char: CHAR; usedCWD: BOOL _ FALSE; HandleDot: PROC [pos: INT] = { IF out = NIL THEN out _ RefText.New[pathLength]; IF pos - 1 > 0 THEN out _ RefText.AppendRope[to: out, from: path.Substr[0, pos - 1]]; IF pos >= pathLength THEN path _ NIL ELSE path _ path.Substr[pos + 1]; pathLength _ path.Length[]; -- recompute pathLength }; HandleDotDot: PROC [pos: INT] = { first: BOOL _ TRUE; IF out = NIL THEN out _ RefText.New[pathLength]; IF pos - 2 > 0 THEN out _ RefText.AppendRope[to: out, from: path.Substr[0, pos - 2]]; IF pos >= pathLength THEN path _ NIL ELSE path _ path.Substr[pos + 1]; pathLength _ path.Length[]; -- recompute pathLength pos _ out.length - 1; -- index of last character in out DO IF pos < 0 THEN { out.length _ 0; IF usedCWD THEN EXIT; usedCWD _ TRUE; out _ RefText.AppendRope[to: out, from: CurrentWorkingDirectory[]]; pos _ out.length - 1; -- continue scanning at end of CWD LOOP; }; IF out[pos] = '/ AND NOT first THEN { out.length _ pos + 1; EXIT; }; pos _ pos - 1; first _ FALSE; ENDLOOP; }; path _ ConvertToSlashFormat[path]; pathLength _ path.Length[]; IF pathLength = 0 THEN RETURN[path]; DO IF i >= pathLength THEN EXIT; char _ path.Fetch[i]; SELECT state FROM idle => IF char = '/ THEN state _ slash; slash => { SELECT char FROM '. => state _ oneDot; '/ => state _ slash; ENDCASE => state _ idle; }; oneDot => { SELECT char FROM '. => state _ twoDots; '/ => { -- delete the "./" construct HandleDot[i]; i _ 0; -- continue scanning from beginning (since HandleDot has re-written path) state _ slash; LOOP; }; ENDCASE => state _ idle; }; twoDots => { IF char = '/ THEN { -- handle the "../" construct HandleDotDot[i]; i _ 0; -- continue scanning from beginning (since HandleDot has re-written path) state _ slash; LOOP; } ELSE state _ idle; }; ENDCASE => ERROR; i _ i + 1; ENDLOOP; IF state = oneDot OR state = twoDots THEN { IF state = oneDot THEN HandleDot[pathLength] ELSE HandleDotDot[pathLength]; path _ Rope.FromRefText[out]; } ELSE { IF out # NIL THEN path _ Rope.Concat[Rope.FromRefText[out], path]; }; IF path.IsEmpty[] AND NOT usedCWD THEN path _ CurrentWorkingDirectory[]; RETURN[path]; }; ConvertToSlashFormat: PUBLIC PROC [path: Rope.ROPE] RETURNS [Rope.ROPE] = { pos: INT; ToSlash: Rope.TranslatorType = { new _ SELECT old FROM '[, '], '<, '> => '/ ENDCASE => old; }; pos _ path.Find["]<"]; IF pos # -1 THEN path _ Rope.Replace[base: path, start: pos, len: 2, with: "/"]; IF Rope.SkipTo[s: path, pos: 0, skip: "[]<>"] = path.Length[] THEN RETURN[path]; path _ Rope.Translate[base: path, translator: ToSlash]; RETURN[path]; }; CurrentWorkingDirectory: PUBLIC PROC RETURNS [Rope.ROPE] = { ra: REF ANY _ List.Assoc[key: $WorkingDirectory, aList: ProcessProps.GetPropList[]]; wDir: Rope.ROPE; length: INT; slashWDir: Rope.ROPE; IF ISTYPE[ra, Rope.ROPE] AND ra # NIL THEN { wDir _ NARROW[ra]; slashWDir _ ConvertToSlashFormat[wDir]; length _ slashWDir.Length[]; IF slashWDir = wDir AND length > 0 AND slashWDir.Fetch[length - 1] = '/ THEN RETURN[wDir]; -- EQ } ELSE slashWDir _ ConvertToSlashFormat[FS.GetDefaultWDir[]]; length _ slashWDir.Length[]; IF length > 0 AND slashWDir.Fetch[length - 1] # '/ THEN slashWDir _ Rope.Concat[slashWDir, "/"]; [] _ List.PutAssoc[key: $WorkingDirectory, val: slashWDir, aList: ProcessProps.GetPropList[]]; RETURN[slashWDir]; }; HomeDirectory: PUBLIC PROC RETURNS [Rope.ROPE] = { RETURN[Rope.Cat["///Users/", UserCredentials.Get[].name, "/"]]; }; IsADirectory: PUBLIC PROC [path: Rope.ROPE] RETURNS [BOOL] = { length: INT; path _ ConvertToSlashFormat[path]; length _ path.Length[]; IF length = 0 THEN RETURN[FALSE]; RETURN[path.Fetch[length-1] = '/]; }; IsAPattern: PUBLIC PROC [path: Rope.ROPE] RETURNS [BOOL] = { RETURN[path.Find["*"] # -1]; }; GetShortName: PUBLIC PROC [path: Rope.ROPE, stripOffVersionNumber: BOOL _ TRUE] RETURNS [Rope.ROPE] = { path _ ConvertToSlashFormat[path]; IF stripOffVersionNumber THEN { pos: INT _ path.Find["!"]; IF pos # -1 THEN path _ path.Substr[0, pos]; }; RETURN[Tail[s: path, char: '/]]; }; Tail: PUBLIC PROC [s: Rope.ROPE, char: CHAR] RETURNS [Rope.ROPE] = { pos: INT _ s.Length[] - 1; IF pos < 0 THEN RETURN[NIL]; DO IF s.Fetch[pos] = char THEN RETURN[s.Substr[pos + 1]]; IF pos = 0 THEN RETURN[s]; pos _ pos - 1; ENDLOOP; }; StripVersionNumber: PUBLIC PROC [path: Rope.ROPE] RETURNS [Rope.ROPE] = { pos: INT; path _ ConvertToSlashFormat[path]; pos _ path.Find["!"]; IF pos = -1 THEN RETURN[path]; RETURN[path.Substr[0, pos]]; }; IsRemote: PUBLIC PROC [path: Rope.ROPE] RETURNS [BOOL] = { first, second: CHAR; path _ ConvertToSlashFormat[path]; IF path.Length[] < 2 THEN ERROR; first _ path.Fetch[0]; second _ path.Fetch[1]; RETURN[NOT ((first = '/ AND second = '/) OR (first = '[ AND second = ']))]; }; InASubdirectory: PUBLIC PROC [parent: Rope.ROPE, path: Rope.ROPE] RETURNS [BOOL] = { pos: INT; path _ ConvertToSlashFormat[path]; pos _ Rope.Find[s1: path, s2: parent, case: FALSE]; IF pos = -1 THEN RETURN [FALSE]; RETURN [Rope.Find[s1: path, s2: "/", pos1: parent.Length[]] # -1]; }; FirstSubdirectory: PUBLIC PROC [parent: Rope.ROPE, path: Rope.ROPE] RETURNS [Rope.ROPE] = { pos: INT; end: INT; path _ ConvertToSlashFormat[path]; pos _ Rope.Find[s1: path, s2: parent, case: FALSE]; IF pos # 0 THEN RETURN [NIL]; pos _ parent.Length[]; IF pos = 0 THEN RETURN [NIL]; end _ Rope.Find[s1: path, s2: "/", pos1: pos]; IF end = -1 THEN RETURN [NIL]; RETURN [Rope.Substr[base: path, start: 0, len: end + 1]]; }; DirectoryContaining: PUBLIC PROC [path: Rope.ROPE, pos: INT] RETURNS [Rope.ROPE] = { length: INT; path _ ConvertToSlashFormat[path]; length _ path.Length[]; IF length = 0 THEN RETURN[NIL]; pos _ MIN[pos, length-1]; DO IF path.Fetch[pos] = '/ THEN RETURN[Rope.Substr[base: path, start: 0, len: pos + 1]]; IF pos = 0 THEN RETURN[NIL]; pos _ pos - 1; ENDLOOP; }; Directory: PUBLIC PROC [path: Rope.ROPE] RETURNS [Rope.ROPE] = { c: CHAR; pos: INT; path _ ConvertToSlashFormat[path]; pos _ path.Length[]; DO pos _ pos - 1; IF pos < 0 THEN RETURN[NIL]; c _ path.Fetch[pos]; IF c = '/ OR c = '> OR c = '] THEN RETURN[Rope.Substr[base: path, start: 0, len: pos + 1]]; ENDLOOP; }; END. January 15, 1984 6:01 pm, Stewart, bulletproofing, fix ResolveRelativePath FileNamesImpl.mesa L. Stewart, January 16, 1984 1:03 pm FileWithSearchRules uses the working directory and the search rules to try to translate the short name of a file into a full path name. Calls ResolveRelativePath first. NIL is returned if the file cannot be found. searchRules should either be a LIST OF REF ANY or a LIST OF Rope.ROPE. If requireExtension, then if root has an extension, it must be the same as defaultExtension. FileWithSearchRules adds !H to all pattern matches IF requireExact = TRUE, then FileWithSearchRules will not try for a unique pattern match, but will only check exact names. FileWithSearchRules returns NIL and ambiguous = TRUE if it finds more than one matching file. FileNames returns not-NIL and ambiguous = TRUE if the result was found as a consequence of an exact pattern match. FileWithSearchRules uses the following rules: IF root is a full path name then { tries root tries Concat[root, defaultExtension] tries for a unique match for Cat[root, *, defaultExtension] } ELSE { tries root (automatically used $WorkingDirectory) FOR wdir _ each element of search rules { tries Concat[wdir, root] } tries Concat[root, defaultExtension] (automatically used $WorkingDirectory) FOR wdir _ each element of search rules { tries Cat[wdir, root, defaultExtension] } IF NOT requireExact THEN FOR wdir _ each element of search rules { tries for a unique match for Cat[wdir, root, *, defaultExtension] } } Returns NIL if can't find it. Exactly one match try the exact matches try for a unique match If path is a directory, then returns the parent directory. If path is a file, then returns the current directory. In either case, this means that Parent returns the directory which contains the current object. If path starts with or contains ./ or ../, ResolveRelativePath converts it into the equivalent full path name using the $WorkingDirectory property on the process properties list. If path is exactly . or .., ResolveRelativePath converts it to the current or parent directory. HandleDot is called with pos pointing to the location of the trailing '/ is the "/./" construct, it works if either the leading or trailing slash aren't there, as in "./xxx" or "xxx/." After execution of HandleDot, out holds the path tp to and including the leading / while path holds the path after the trailing slash. If the leading slash doesn't exist, then out will be allocated but empty. If the trailing slash doesn't exist (or was the last character in path), then path will be empty out now includes up to and including the leading slash. path now starts at the character after the trailing slash HandleDotDot is called with pos pointing to the location of the trailing '/ is the "/../" construct. It worksif either the leading or trailing slash aren't there, as in "../xxx" or "xxx/.." After execution of HandleDotDot, out holds the path tp to and including the / terminating the parent directory while path holds the path after the trailing slash. If the leading slash doesn't exist, then out will contain the parent of CWD. If the trailing slash doesn't exist (or was the last character in path), then path will be empty pos is the location of the trailing '/ out now contains the path up to and including the leading slash path now starts at the character after the trailing slash Now strip back out to the last slash, provided it is not the first character examined The next character we get should be the one after the slash handle state at end of path The next lines call HandleDot (or DotDot) with a pointer to the '/ which is off the end of the path. Convert to slashes Store back the altered version. Strip off the version number Tail returns the part of a rope after the last instance of char. Return the prefix of the part of name before pos which is the name of a directory. Return the directory part of a name. Κn˜šœ™J™$—J™šΟk ˜ Jšœ ˜ Jšœ˜Jšœœ@˜HJšœœ˜$Jšœ œ˜!J˜ JšœœJœ-˜…Jšœœ˜—J˜šœœ˜Jšœœ4˜>Jšœ˜!Jšœ˜—J˜Icode2šœΧ™ΧKšœF™FKšœ\™\K™2Kšœz™zKšœ]™]Kšœr™ršœ-™-šœ"™"Jšœ ™ Jšœ$™$Jšœ;™;J™—šœ™Jšœ1™1šœ)™)Jšœ™J™—JšœK™Kšœ)™)Jšœ'™'J™—šœB™BJšœA™AJ™—J™—Jšœ™—š Οnœœœ œœœœœœœœœœœ œœ˜ΰJšœœœ˜Jšœœœ˜Jšœœœ˜Jš œœœœœ˜Jšœ œœœ˜Jš œœœœœ˜Jšœ œœœ˜Jšœœ˜Jšœ œ˜Jšœ"œ˜'šžœœ œ œœœ œ˜LJšœœœœ˜%Jš œ œ#œ œœœ ˜kšœœœ˜ Jšœ*˜*Jšœœ˜ J˜—Jšœœ˜J˜—šž œœ œ œœœ œ˜TJš œœœœœ˜šœœ ˜JšœœΟc2˜IJšœœœ œ˜#Jšœœ˜Jšœ˜—J˜šœœ˜Jšœ œ˜Jšœœ˜ J˜—Jš œ8œ œœœ˜lJš œœœœœ˜ šœ œ˜Jšœ œ˜Jšœœ˜ J˜—J™Jšœ œ˜Jšœ+˜+Jšœœ˜ J˜—J˜Jšœœœœ˜#Jšœ!˜!Jšœ"œ˜8Jšœ#˜#Jšœ!˜!Jšœœœœ œœœ˜Išœœœ˜+Jš œ:œ2œœœœ˜§J˜—šœœœ˜Jšœ.˜.Jšœ4˜4šœœ˜Jšœ%˜%Jšœ-˜-J˜—J˜—Jšœ œœ˜.Jšœ œ˜Jšœœœœ˜:šœœ˜J™Jšœœœœ˜*Jšœ œœœ˜0J™Jšœœ œœ˜@J˜—šœ˜Jšœœœœ˜*šœ œ˜Jš œœœœœ˜$Jšœœœœ˜(Jšœ˜—J˜ J˜šœœ˜šœœ˜Jš œœœœœ˜DJšœ˜Jšœ˜—šœ œ˜Jšœ'œœ˜5Jšœ˜Jšœ˜—J˜—Jšœ œœœ˜0šœ œ˜J˜ J˜šœœ˜Jš œœœœœ˜GJšœ˜Jšœ˜—šœ œ˜Jšœ*œœ˜8Jšœ˜Jšœ˜—J˜—šœœ˜Jšœ œœ˜.J˜ J˜šœœ˜Jš œ&œœœœ˜SJšœ˜Jšœ˜—šœ œ˜Jšœ6œœ˜DJšœ˜Jšœ˜—J˜—J˜—Jšœ˜—Icode˜J™Σš žœ œ œœœ˜=Lšœ œ˜Lšœœ˜ Lšœ"˜"Lšœ˜Lšœœœœ˜$Lšœœ!œœ˜QLšœ-˜3L˜—L˜Jšœ²™²Jšœ_™_J˜J˜2J˜š žœœœ œœœ˜JJšœœ˜ J˜Jšœ œ˜Jšœœœœ˜Jšœœ˜ Jšœ œœ˜J™ΈJ™†J™IJ™`šž œœœ˜Jšœœœ˜0Jšœ œB˜UJ™7Jšœœ˜$Jšœ˜!J™9JšœŸ˜4J˜—J™½J™’J™LJ™`šž œœœ˜!Jšœœœ˜J™&Jšœœœ˜0Jšœ œB˜UJ™?Jšœœ˜$Jšœ˜!J™9JšœŸ˜4J™UJšœŸ!˜8š˜šœ œ˜J˜Jšœ œœ˜Jšœ œ˜JšœC˜CJšœŸ"˜9Jšœ˜J˜—šœœœœ˜%J˜Jšœ˜J˜—J˜Jšœœ˜Jšœ˜—J˜—J˜"Jšœ˜Jšœœœ˜$J˜š˜Jšœœœ˜Jšœ˜šœ˜Jšœœ œ˜(˜ šœ˜J˜J˜Jšœ˜—J˜—˜ šœ˜J˜šœ Ÿ˜%J˜ JšœŸI˜QJ˜Jšœ˜J˜—Jšœ˜—J˜—˜ šœ œŸ˜2J˜JšœŸI˜QJ˜J™;Jšœ˜J˜—Jšœ˜J˜—Jšœœ˜—J˜ Jšœ˜—J™šœœœ˜+Jšœd™dJšœœ˜,Jšœ˜Jšœ˜J˜—šœ˜Jšœœœ1˜BJ˜—Jšœœœ œ"˜HJšœ˜ J˜—J˜š žœœœ œœœ˜KJšœœ˜ šœ ˜ šœœ˜J˜Jšœ˜—J˜—J™Jšœ˜Jšœ œ@˜PJšœ<œœ˜PJšœ7˜7Jšœ˜ J˜—L˜š žœœœœœ˜Lšœœ˜ Jšœ"˜"Lšœ˜Lšœ œœœ˜!Lšœ˜"L˜—L˜š ž œœœ œœœ˜