blob: 54c34b3e83e515e354c916b9abaf877dd603c6b8 [file] [log] [blame]
// Copyright 2018 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package load
import (
"unicode/utf8"
"cuelang.org/go/cue/build"
"cuelang.org/go/internal/str"
)
// Package rules:
//
// - the package clause defines a namespace.
// - a cue file without a package clause is a standalone file.
// - all files with the same package name within a directory and its
// ancestor directories up to the package root belong to the same package.
// - The package root is either the top of the file hierarchy or the first
// directory in which a cue.mod file is defined.
//
// The contents of a namespace depends on the directory that is selected as the
// starting point to load a package. An instance defines a package-directory
// pair.
// allFiles returns the names of all the files considered for the package.
// This is used for sanity and security checks, so we include all files,
// even IgnoredGoFiles, because some subcommands consider them.
func allFiles(p *build.Instance) []string {
return str.StringList(
p.CUEFiles,
p.ToolCUEFiles,
p.TestCUEFiles,
p.IgnoredCUEFiles,
p.InvalidCUEFiles,
p.DataFiles,
)
}
// safeArg reports whether arg is a "safe" command-line argument,
// meaning that when it appears in a command-line, it probably
// doesn't have some special meaning other than its own name.
// Obviously args beginning with - are not safe (they look like flags).
// Less obviously, args beginning with @ are not safe (they look like
// GNU binutils flagfile specifiers, sometimes called "response files").
// To be conservative, we reject almost any arg beginning with non-alphanumeric ASCII.
// We accept leading . _ and / as likely in file system paths.
// There is a copy of this function in cmd/compile/internal/gc/noder.go.
func safeArg(name string) bool {
if name == "" {
return false
}
c := name[0]
return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || c == '.' || c == '_' || c == '/' || c >= utf8.RuneSelf
}