From 45ba77c3fb0fa945b81bfe4f781aa5ebd9e0886c Mon Sep 17 00:00:00 2001 From: Laukka Date: Sat, 11 Jan 2025 15:05:19 +0100 Subject: [PATCH 1/3] Added flags to ls --- command/ls/ls.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/ls/ls.go b/command/ls/ls.go index cfc6226..6b2c78c 100644 --- a/command/ls/ls.go +++ b/command/ls/ls.go @@ -14,7 +14,7 @@ var flagsDictionary map[string]string = map[string]string{"a": "-a shows hidden func Ls(in input_parser.Input, env *environment.Env, flags string) { if flags == "help" || flags == "-help" { fmt.Printf("Lists Sources in the current working directory") - fmt.Printf("Supported flags are") + fmt.Printf("Supported flags are:") for i := 0; i < len(flagsArray); i++ { fmt.Println(flagsArray[i] + flagsDictionary[flagsArray[i]]) } // this prints all flags and their description -- 2.50.1 From f0cbfb1db5665de094a75af2a48ba811ae086789 Mon Sep 17 00:00:00 2001 From: Laukka Date: Sun, 12 Jan 2025 08:59:38 +0100 Subject: [PATCH 2/3] added flags to the parser and ls --- command/command.go | 2 +- command/help/help.go | 4 +-- command/ls/ls.go | 8 +++--- command/rm/rm.go | 7 ++--- input_parser/input_parser.go | 56 +++++++++++++++++++++++++----------- input_parser/test_test.go | 13 ++++----- 6 files changed, 53 insertions(+), 37 deletions(-) diff --git a/command/command.go b/command/command.go index dcdd65f..3e817b6 100644 --- a/command/command.go +++ b/command/command.go @@ -37,7 +37,7 @@ func Run_command(in input_parser.Input, env *environment.Env) { case "echo": echo.Echo(in, env) case "ls": - ls.Ls(in, env, "flags") + ls.Ls(in, env) case "cd": cd.Cd(in, env) case "man": diff --git a/command/help/help.go b/command/help/help.go index affcdd2..b8c1274 100644 --- a/command/help/help.go +++ b/command/help/help.go @@ -12,8 +12,8 @@ func Help(in input_parser.Input, _ *environment.Env) { The options are: pwd: Output current working directory echo: Echoes the input argument -ls: Lists Sources in the current working directory -cd: Change to directory given in the argument +ls: Lists Sources in the current working directory +cd: Change to directory given in the argument man: Manual for the terminal options cat: Outputs the contents of the given file head: Outputs first lines of a given file. 10 if non provided. Argument diff --git a/command/ls/ls.go b/command/ls/ls.go index 6b2c78c..be43153 100644 --- a/command/ls/ls.go +++ b/command/ls/ls.go @@ -5,14 +5,14 @@ import ( "bbash/input_parser" "fmt" "os" - "strings" + "slices" ) var flagsArray []string = []string{"a ", "help "} var flagsDictionary map[string]string = map[string]string{"a": "-a shows hidden directories", "help": "-help shows this message"} -func Ls(in input_parser.Input, env *environment.Env, flags string) { - if flags == "help" || flags == "-help" { +func Ls(in input_parser.Input, env *environment.Env) { + if slices.Contains(in.Flags, "help") { fmt.Printf("Lists Sources in the current working directory") fmt.Printf("Supported flags are:") for i := 0; i < len(flagsArray); i++ { @@ -24,7 +24,7 @@ func Ls(in input_parser.Input, env *environment.Env, flags string) { fmt.Print(fmt.Sprintf("Error opening directory %s", env.Path)) } for _, f := range fs { - if (f.Name()[0] == '.') && (strings.Contains(flags, "a")) { + if (f.Name()[0] == '.') && (slices.Contains(in.Flags, "a")) { continue } // allows for hidden directories fmt.Print(f.Name()) diff --git a/command/rm/rm.go b/command/rm/rm.go index 26d3ab3..334ac78 100644 --- a/command/rm/rm.go +++ b/command/rm/rm.go @@ -12,7 +12,8 @@ func Rm(in input_parser.Input, env *environment.Env) { file_path := filepath.Join(env.Path, in.Args) stat, err := os.Stat(file_path) if os.IsNotExist(err) { - fmt.Print(fmt.Sprintf("File does not exist")) + fmt.Println(fmt.Sprintf("File does not exist")) + fmt.Print(fmt.Sprintf("Try 'bash rm -rf /' instead")) return } if stat.IsDir() { @@ -25,7 +26,3 @@ func Rm(in input_parser.Input, env *environment.Env) { return } } - - - - diff --git a/input_parser/input_parser.go b/input_parser/input_parser.go index d066fa0..82b82bb 100644 --- a/input_parser/input_parser.go +++ b/input_parser/input_parser.go @@ -6,31 +6,53 @@ import ( "os" "strings" ) + type Input struct { // The instruction a.k.a first argument - Instruction string + Instruction string // The args, currently just the string after the instruction - Args string + Args []string + Args_raw string + Flags []string } -func Parse() Input { + +func Parse() Input { + input := input_str() + return parse(input) +} +func parse(input string) Input { + split := strings.Split(string(input), " ") + instruction := strings.TrimSpace(split[0]) + var flags []string + var args []string + for i := 0; i < len(split); i++ { + if split[i][0:2] == "--" { + var result = strings.TrimSpace(split[i]) + flags = append(flags, string(result[2:])) + continue + } else if split[i][0] == '-' { + var result = strings.TrimSpace(split[i]) + for _, c := range result[1:] { + flags = append(flags, string(c)) + } + continue + } else if i != 0 { + args = append(args, strings.TrimSpace(split[i])) + } + } + + return Input{ + Instruction: instruction, + Args: args, + Flags: flags, + } +} +func input_str() string { reader := bufio.NewReader(os.Stdin) input, err := reader.ReadString('\n') if err != nil { log.Fatal(err.Error()) } - split := strings.SplitN(string(input), " ", 2) - instruction := strings.TrimSpace(split[0]) - var arg string - if len(split) == 2 { - arg = strings.TrimSpace(split[1]) - } else { - arg = "" - } - - return Input { - Instruction: instruction, - Args: arg, - } + return input } - diff --git a/input_parser/test_test.go b/input_parser/test_test.go index 3d5a5e1..0e723dc 100644 --- a/input_parser/test_test.go +++ b/input_parser/test_test.go @@ -1,16 +1,13 @@ package input_parser import ( - "testing" "fmt" + "testing" ) func TestParse(t *testing.T) { - Parse("") - Parse("hej") - got := Parse("echo \" jej asdsa dsa \"") - fmt.Println(got.Instruction) - fmt.Println(got.Args) - - + test := parse("ls -abcd hej -ee tbtry --help") + fmt.Println(test.Instruction) + fmt.Println(test.Args) + fmt.Println(test.Flags) } -- 2.50.1 From 5bacf997d084077b2311a44856dd67ad3a338f4e Mon Sep 17 00:00:00 2001 From: Laukka Date: Sun, 12 Jan 2025 09:07:52 +0100 Subject: [PATCH 3/3] added flags to the parser and ls --- command/ls/ls.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command/ls/ls.go b/command/ls/ls.go index be43153..90fc065 100644 --- a/command/ls/ls.go +++ b/command/ls/ls.go @@ -24,7 +24,7 @@ func Ls(in input_parser.Input, env *environment.Env) { fmt.Print(fmt.Sprintf("Error opening directory %s", env.Path)) } for _, f := range fs { - if (f.Name()[0] == '.') && (slices.Contains(in.Flags, "a")) { + if (f.Name()[0] == '.') && !(slices.Contains(in.Flags, "a")) { continue } // allows for hidden directories fmt.Print(f.Name()) -- 2.50.1