|
|
@@ -61,6 +61,11 @@ func main() {
|
|
|
|
panic("Invalid match number to print.")
|
|
|
|
panic("Invalid match number to print.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Enable lineFlag if lineNumFlag is enabled
|
|
|
|
|
|
|
|
if *lineNumFlag {
|
|
|
|
|
|
|
|
*lineFlag = true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Process:
|
|
|
|
// Process:
|
|
|
|
// 1. Convert regex into postfix notation (Shunting-Yard algorithm)
|
|
|
|
// 1. Convert regex into postfix notation (Shunting-Yard algorithm)
|
|
|
|
// a. Add explicit concatenation operators to facilitate this
|
|
|
|
// a. Add explicit concatenation operators to facilitate this
|
|
|
@@ -68,11 +73,11 @@ func main() {
|
|
|
|
// 3. Run the string against the NFA
|
|
|
|
// 3. Run the string against the NFA
|
|
|
|
|
|
|
|
|
|
|
|
if len(flag.Args()) < 1 { // flag.Args() also strips out program name
|
|
|
|
if len(flag.Args()) < 1 { // flag.Args() also strips out program name
|
|
|
|
fmt.Println("ERROR: Missing cmdline args")
|
|
|
|
fmt.Printf("%s: ERROR: Missing cmdline args\n", os.Args[0])
|
|
|
|
os.Exit(22)
|
|
|
|
os.Exit(22)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if *recursiveFlag && len(flag.Args()) < 2 { // File/Directory must be provided with '-r'
|
|
|
|
if *recursiveFlag && len(flag.Args()) < 2 { // File/Directory must be provided with '-r'
|
|
|
|
fmt.Println("ERROR: Missing cmdline args")
|
|
|
|
fmt.Printf("%s: ERROR: Missing cmdline args\n", os.Args[0])
|
|
|
|
os.Exit(22)
|
|
|
|
os.Exit(22)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var re string
|
|
|
|
var re string
|
|
|
@@ -87,12 +92,22 @@ func main() {
|
|
|
|
for _, inputFilename := range inputFilenames {
|
|
|
|
for _, inputFilename := range inputFilenames {
|
|
|
|
inputFile, err := os.Open(inputFilename)
|
|
|
|
inputFile, err := os.Open(inputFilename)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%s: No such file or directory\n", flag.Args()[1])
|
|
|
|
fmt.Printf("%s: %s: No such file or directory\n", os.Args[0], inputFilename)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
fileStat, err := inputFile.Stat()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
fmt.Printf("%v\n", err)
|
|
|
|
os.Exit(2)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if fileStat.Mode().IsDir() {
|
|
|
|
|
|
|
|
fmt.Printf("%s: %s: Is a directory\n", os.Args[0], inputFilename)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
inputFiles = append(inputFiles, inputFile)
|
|
|
|
inputFiles = append(inputFiles, inputFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var test_str string
|
|
|
|
var test_str string
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
@@ -108,6 +123,7 @@ func main() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, inputFile := range inputFiles {
|
|
|
|
for _, inputFile := range inputFiles {
|
|
|
|
|
|
|
|
lineNum = 0
|
|
|
|
reader := bufio.NewReader(inputFile)
|
|
|
|
reader := bufio.NewReader(inputFile)
|
|
|
|
linesRead = false
|
|
|
|
linesRead = false
|
|
|
|
for true {
|
|
|
|
for true {
|
|
|
@@ -202,8 +218,13 @@ func main() {
|
|
|
|
if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 {
|
|
|
|
if !(*invertFlag) && len(matchIndices) == 0 || *invertFlag && len(matchIndices) > 0 {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if *recursiveFlag || len(flag.Args()) > 2 { // If we have 2 args, then we're only searching 1 file. We should only print the filename if there's more than 1 file.
|
|
|
|
color.New(color.FgMagenta).Fprintf(out, "%s:", inputFile.Name()) // Print filename
|
|
|
|
color.New(color.FgMagenta).Fprintf(out, "%s:", inputFile.Name()) // Print filename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if *lineNumFlag {
|
|
|
|
|
|
|
|
color.New(color.FgGreen).Fprintf(out, "%d:", lineNum) // Print filename
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we are substituting, we need a different behavior, as follows:
|
|
|
|
// If we are substituting, we need a different behavior, as follows:
|