Marcel van Lohuizen | bc4d65d | 2018-12-10 15:40:02 +0100 | [diff] [blame] | 1 | // Copyright 2018 The CUE Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package load |
| 16 | |
| 17 | import ( |
| 18 | "unicode/utf8" |
| 19 | |
| 20 | "cuelang.org/go/cue/build" |
| 21 | "cuelang.org/go/internal/str" |
| 22 | ) |
| 23 | |
| 24 | // Package rules: |
| 25 | // |
| 26 | // - the package clause defines a namespace. |
| 27 | // - a cue file without a package clause is a standalone file. |
| 28 | // - all files with the same package name within a directory and its |
| 29 | // ancestor directories up to the package root belong to the same package. |
| 30 | // - The package root is either the top of the file hierarchy or the first |
| 31 | // directory in which a cue.mod file is defined. |
| 32 | // |
| 33 | // The contents of a namespace depends on the directory that is selected as the |
| 34 | // starting point to load a package. An instance defines a package-directory |
| 35 | // pair. |
| 36 | |
| 37 | // allFiles returns the names of all the files considered for the package. |
| 38 | // This is used for sanity and security checks, so we include all files, |
| 39 | // even IgnoredGoFiles, because some subcommands consider them. |
| 40 | func allFiles(p *build.Instance) []string { |
| 41 | return str.StringList( |
| 42 | p.CUEFiles, |
| 43 | p.ToolCUEFiles, |
| 44 | p.TestCUEFiles, |
| 45 | p.IgnoredCUEFiles, |
| 46 | p.InvalidCUEFiles, |
| 47 | p.DataFiles, |
| 48 | ) |
| 49 | } |
| 50 | |
| 51 | var foldPath = make(map[string]string) |
| 52 | |
| 53 | // safeArg reports whether arg is a "safe" command-line argument, |
| 54 | // meaning that when it appears in a command-line, it probably |
| 55 | // doesn't have some special meaning other than its own name. |
| 56 | // Obviously args beginning with - are not safe (they look like flags). |
| 57 | // Less obviously, args beginning with @ are not safe (they look like |
| 58 | // GNU binutils flagfile specifiers, sometimes called "response files"). |
| 59 | // To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII. |
| 60 | // We accept leading . _ and / as likely in file system paths. |
| 61 | // There is a copy of this function in cmd/compile/internal/gc/noder.go. |
| 62 | func safeArg(name string) bool { |
| 63 | if name == "" { |
| 64 | return false |
| 65 | } |
| 66 | c := name[0] |
| 67 | return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf |
| 68 | } |