llcppsigfetch:config the output

This commit is contained in:
luoliwoshang
2024-09-23 15:24:03 +08:00
parent dbaf12b043
commit 074090a0aa

View File

@@ -31,30 +31,37 @@ import (
) )
func main() { func main() {
if len(os.Args) == 1 { cfgFile := ""
// run with default config file outputToFile := false
runFromConfig() for i := 1; i < len(os.Args); i++ {
return arg := os.Args[i]
} if arg == "--extract" {
runExtract()
if os.Args[1] == "--extract" { return
runExtract() } else if arg == "--help" || arg == "-h" {
} else if os.Args[1] == "--help" || os.Args[1] == "-h" { printUsage()
printUsage() return
} else { } else if strings.HasPrefix(arg, "-out=") {
runFromConfig() outputToFile = parseBoolArg(arg, "out", false)
} else if cfgFile == "" && !strings.HasPrefix(arg, "-") {
cfgFile = arg
}
} }
runFromConfig(cfgFile, outputToFile)
} }
func printUsage() { func printUsage() {
fmt.Println("Usage:") fmt.Println("Usage:")
fmt.Println(" llcppsigfetch [<config_file>]") fmt.Println(" llcppsigfetch [<config_file>] [-out=<bool>]")
fmt.Println(" OR") fmt.Println(" OR")
fmt.Println(" llcppsigfetch --extract <file> <temp> [args...]") fmt.Println(" llcppsigfetch --extract <file> [-out=<bool>] [-temp=<bool>] [-cpp=<bool>] [args...]")
fmt.Println("") fmt.Println("")
fmt.Println("Options:") fmt.Println("Options:")
fmt.Println(" [<config_file>]: Path to the configuration file (use '-' for stdin)") fmt.Println(" [<config_file>]: Path to the configuration file (use '-' for stdin)")
fmt.Println(" If not provided, uses default 'llcppg.cfg'") fmt.Println(" If not provided, uses default 'llcppg.cfg'")
fmt.Println(" -out=<bool>: Optional. Set to 'true' to output results to a file,")
fmt.Println(" 'false' (default) to output to stdout")
fmt.Println(" This option can be used with both modes")
fmt.Println("") fmt.Println("")
fmt.Println(" --extract: Extract information from a single file") fmt.Println(" --extract: Extract information from a single file")
fmt.Println(" <file>: Path to the file to process, or file content if -temp=true") fmt.Println(" <file>: Path to the file to process, or file content if -temp=true")
@@ -71,10 +78,9 @@ func printUsage() {
fmt.Println("Note: The two usage modes are mutually exclusive. Use either [<config_file>] OR --extract, not both.") fmt.Println("Note: The two usage modes are mutually exclusive. Use either [<config_file>] OR --extract, not both.")
} }
func runFromConfig() { func runFromConfig(cfgFile string, outputToFile bool) {
cfgFile := "llcppg.cfg" if cfgFile == "" {
if len(os.Args) > 1 { cfgFile = "llcppg.cfg"
cfgFile = os.Args[1]
} }
var data []byte var data []byte
@@ -100,7 +106,7 @@ func runFromConfig() {
err = context.ProcessFiles(files) err = context.ProcessFiles(files)
check(err) check(err)
outputInfo(context) outputInfo(context, outputToFile)
} }
func runExtract() { func runExtract() {
@@ -117,6 +123,7 @@ func runExtract() {
Temp: false, Temp: false,
} }
outputToFile := false
for i := 3; i < len(os.Args); i++ { for i := 3; i < len(os.Args); i++ {
arg := os.Args[i] arg := os.Args[i]
switch { switch {
@@ -128,6 +135,10 @@ func runExtract() {
cfg.IsCpp = parseBoolArg(arg, "cpp", true) cfg.IsCpp = parseBoolArg(arg, "cpp", true)
os.Args = append(os.Args[:i], os.Args[i+1:]...) os.Args = append(os.Args[:i], os.Args[i+1:]...)
i-- i--
case strings.HasPrefix(arg, "-out="):
outputToFile = parseBoolArg(arg, "out", false)
os.Args = append(os.Args[:i], os.Args[i+1:]...)
i--
default: default:
cfg.Args = append(cfg.Args, arg) cfg.Args = append(cfg.Args, arg)
} }
@@ -139,7 +150,7 @@ func runExtract() {
check(err) check(err)
result := converter.MarshalOutputASTFiles() result := converter.MarshalOutputASTFiles()
cstr := result.Print() cstr := result.Print()
c.Printf(cstr) outputResult(cstr, outputToFile)
cjson.FreeCStr(cstr) cjson.FreeCStr(cstr)
result.Delete() result.Delete()
converter.Dispose() converter.Dispose()
@@ -151,6 +162,20 @@ func check(err error) {
} }
} }
func outputResult(result *c.Char, outputToFile bool) {
if outputToFile {
outputFile := "llcppg.sigfetch.json"
err := os.WriteFile(outputFile, []byte(c.GoString(result)), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing to output file: %v\n", err)
os.Exit(1)
}
fmt.Printf("Results saved to %s\n", outputFile)
} else {
c.Printf(result)
}
}
func getHeaderFiles(cflags string, files []string) []string { func getHeaderFiles(cflags string, files []string) []string {
prefix := cflags prefix := cflags
prefix = strings.TrimPrefix(prefix, "-I") prefix = strings.TrimPrefix(prefix, "-I")
@@ -161,12 +186,12 @@ func getHeaderFiles(cflags string, files []string) []string {
return paths return paths
} }
func outputInfo(context *parse.Context) { func outputInfo(context *parse.Context, outputToFile bool) {
info := context.Output() info := context.Output()
str := info.Print() str := info.Print()
defer cjson.FreeCStr(str) defer cjson.FreeCStr(str)
defer info.Delete() defer info.Delete()
c.Printf(str) outputResult(str, outputToFile)
} }
func parseBoolArg(arg, name string, defaultValue bool) bool { func parseBoolArg(arg, name string, defaultValue bool) bool {