Skip to content

Commit

Permalink
refactor render
Browse files Browse the repository at this point in the history
  • Loading branch information
Selyss committed Nov 19, 2023
1 parent b81aed2 commit facd094
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 61 deletions.
28 changes: 6 additions & 22 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ func parseArgs() *CLIOptions {
prettyPrint := parser.Flag("p", "pretty-print", &argparse.Options{Help: "Pretty print JSON result"})

err := parser.Parse(os.Args)
if err != nil {
if err != nil || (*query == "" && *arch == "") {
fmt.Print(parser.Usage(err))
os.Exit(1)
}

opts.Syscall = *query
Expand All @@ -46,26 +47,9 @@ func parseArgs() *CLIOptions {

func main() {
opts := parseArgs()
if opts.Syscall == "" && opts.Arch == "" {
table, err := assembuddy.GetArchData(opts.Arch, opts.PrettyPrint)
if err != nil {
log.Fatal(err)
}
assembuddy.RenderArchTable(opts.Arch, table)
}

if opts.Syscall != "" {
table, err := assembuddy.GetNameData(opts.Syscall, opts.PrettyPrint)
if err != nil {
log.Fatalf("error: %v", err)
}
assembuddy.RenderNameTable(table)
}
if opts.Arch != "" {
table, err := assembuddy.GetArchData(opts.Arch, opts.PrettyPrint)
if err != nil {
log.Fatal(err)
}
assembuddy.RenderArchTable(opts.Arch, table)
table, err := assembuddy.GetSyscallData(opts.Arch, opts.Syscall, opts.PrettyPrint)
if err != nil {
log.Fatal(err)
}
assembuddy.RenderTable(opts.Arch, table)
}
45 changes: 24 additions & 21 deletions pkg/assembuddy/renderTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,51 @@ import (
"github.com/olekukonko/tablewriter"
)

func RenderNameTable(tableData []Syscall) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ARCH", "NR", "NAME", "RETURN", "ARG0", "ARG1", "ARG2", "ARG3", "ARG4", "ARG5"})
col := tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiGreenColor}
table.SetHeaderColor(col, col, col, col, col, col, col, col, col, col)
for _, syscall := range tableData {
table.Append([]string{syscall.Arch, fmt.Sprint(syscall.Nr), syscall.Name, syscall.ReturnValue, syscall.Arg0, syscall.Arg1, syscall.Arg2, syscall.Arg3, syscall.Arg4, syscall.Arg5})
}
table.Render()
}

func RenderArchTable(arch string, tableData []Syscall) {
table := tablewriter.NewWriter(os.Stdout)
getArchTable(arch, *table)
for _, syscall := range tableData {
table.Append([]string{syscall.Name, syscall.ReturnValue, syscall.Arg0, syscall.Arg1, syscall.Arg2, syscall.Arg3, syscall.Arg4, syscall.Arg5})
}
table.Render()
}

func getArchTable(arch string, table tablewriter.Table) {
func RenderTable(arch string, tableData []Syscall) {
// TODO: add the ARG0 (x0) stuff to headers
//
// HACK: hard coding the header len as 9 for now
//
// INFO: I actually need the col stuff because the library needs it
table := tablewriter.NewWriter(os.Stdout)
switch arch {
case "x64":
table.SetHeader([]string{"NR", "SYSCALL", "RAX", "rdi", "rsi", "rdx", "r10", "r8", "r9"})
col := tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiYellowColor}
table.SetHeaderColor(col, col, col, col, col, col, col, col, col)
for _, syscall := range tableData {
table.Append([]string{syscall.Name, syscall.ReturnValue, syscall.Arg0, syscall.Arg1, syscall.Arg2, syscall.Arg3, syscall.Arg4, syscall.Arg5})
}
case "x86":
table.SetHeader([]string{"NR", "SYSCALL", "eax", "ebx", "ecx", "edx", "esi", "edi", "ebp"})
col := tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiCyanColor}
table.SetHeaderColor(col, col, col, col, col, col, col, col, col)
for _, syscall := range tableData {
table.Append([]string{syscall.Name, syscall.ReturnValue, syscall.Arg0, syscall.Arg1, syscall.Arg2, syscall.Arg3, syscall.Arg4, syscall.Arg5})
}
case "arm64":
table.SetHeader([]string{"NR", "SYSCALL", "x8", "x0", "x1", "x2", "x3", "x4", "x5"})
col := tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor}
table.SetHeaderColor(col, col, col, col, col, col, col, col, col)
for _, syscall := range tableData {
table.Append([]string{syscall.Name, syscall.ReturnValue, syscall.Arg0, syscall.Arg1, syscall.Arg2, syscall.Arg3, syscall.Arg4, syscall.Arg5})
}
case "arm":
table.SetHeader([]string{"NR", "SYSCALL", "r7", "r0", "r1", "r2", "r3", "r4", "r5"})
col := tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiMagentaColor}
table.SetHeaderColor(col, col, col, col, col, col, col, col, col)
for _, syscall := range tableData {
table.Append([]string{syscall.Name, syscall.ReturnValue, syscall.Arg0, syscall.Arg1, syscall.Arg2, syscall.Arg3, syscall.Arg4, syscall.Arg5})
}

case "":
table.SetHeader([]string{"ARCH", "NR", "NAME", "RETURN", "ARG0", "ARG1", "ARG2", "ARG3", "ARG4", "ARG5"})
col := tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiGreenColor}
table.SetHeaderColor(col, col, col, col, col, col, col, col, col, col)

for _, syscall := range tableData {
table.Append([]string{syscall.Arch, fmt.Sprint(syscall.Nr), syscall.Name, syscall.ReturnValue, syscall.Arg0, syscall.Arg1, syscall.Arg2, syscall.Arg3, syscall.Arg4, syscall.Arg5})
}
}
table.Render()
}
22 changes: 4 additions & 18 deletions pkg/assembuddy/requestTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,15 @@ func fetchData(endpointURL string, prettyp bool) ([]Syscall, error) {
return systemCalls, nil
}

func GetSpecificData(arch string, name string, prettyp bool) ([]Syscall, error) {
func GetSyscallData(arch string, name string, prettyp bool) ([]Syscall, error) {
url := "https://api.syscall.sh/v1/syscalls/"
// if arch is x64, x86, arm, or arm64, concat to endpointURL
if arch == "x64" || arch == "x86" || arch == "arm" || arch == "arm64" {
url += arch
} else {
// if arch is not empty, return error
} else if arch != "" {
return nil, errors.New("invalid architecture")
}
return fetchData(url, prettyp)
}

func GetArchData(arch string, prettyp bool) ([]Syscall, error) {
url := "https://api.syscall.sh/v1/syscalls/"
// if arch is x64, x86, arm, or arm64, concat to endpointURL
if arch == "x64" || arch == "x86" || arch == "arm" || arch == "arm64" {
url += arch
} else {
return nil, errors.New("invalid architecture")
}
return fetchData(url, prettyp)
}

func GetNameData(name string, prettyp bool) ([]Syscall, error) {
url := "https://api.syscall.sh/v1/syscalls/" + name // we validate this if request fails
url += name
return fetchData(url, prettyp)
}

0 comments on commit facd094

Please sign in to comment.