4 Commits

4 changed files with 29 additions and 14 deletions

View File

@@ -5,9 +5,13 @@ fmt:
go fmt ./...
vet: fmt
go vet ./...
buildLib: vet
buildLibUnopt: vet
go build -gcflags="all=-N -l" ./...
buildCmd: buildLib
unopt: buildLibUnopt
go build -C kg/ -gcflags="all=-N -l" -o kg ./...
buildLib: vet
go build ./...
buildCmd: buildLib
go build -C kg/ -o kg ./...
test: buildCmd
go test -v ./...

View File

@@ -32,13 +32,21 @@ func genRange[T character](start, end T) []T {
}
// Returns whether or not the given file contains a NULL character
func fileContainsNullChar(file *os.File) bool {
func fileContainsNullChar(filename string) (bool, error) {
file, err := os.Open(filename)
if err != nil {
return true, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "\000") {
return true
return true, nil
}
}
return false
if err := scanner.Err(); err != nil {
return true, err
}
return false, nil
}

View File

@@ -91,6 +91,7 @@ func main() {
inputFilenames := flag.Args()[1:]
for _, inputFilename := range inputFilenames {
inputFile, err := os.Open(inputFilename)
defer inputFile.Close()
if err != nil {
fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename)
} else {
@@ -102,8 +103,14 @@ func main() {
if fileStat.Mode().IsDir() {
fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename)
} else {
if fileContainsNullChar(inputFile) {
fmt.Printf("%s: %s: Is a binary file\n", os.Args[0], inputFilename)
var nullCharPresent bool
if nullCharPresent, err = fileContainsNullChar(inputFilename); nullCharPresent {
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
} else {
fmt.Printf("%s: %s: Is a binary file\n", os.Args[0], inputFilename)
}
} else {
inputFiles = append(inputFiles, inputFile)
}

View File

@@ -131,13 +131,9 @@ func newEscapedNode(c rune, inCharClass bool) (postfixNode, error) {
case 'v': // Vertical tab
toReturn.nodetype = characterNode
toReturn.contents = append(toReturn.contents, rune(11))
case '-': // Literal hyphen - only in character class
if inCharClass {
toReturn.nodetype = characterNode
toReturn.contents = append(toReturn.contents, '-')
} else {
return postfixNode{}, fmt.Errorf("invalid escape character")
}
case '-': // Literal hyphen
toReturn.nodetype = characterNode
toReturn.contents = append(toReturn.contents, '-')
default: // None of the above - append it as a regular character
if isNormalChar(c) { // Normal characters cannot be escaped
return postfixNode{}, fmt.Errorf("invalid escape character")