From 4e03c3b9da9ed33104b574ae045dc1382fc8f4fa Mon Sep 17 00:00:00 2001 From: codecrafters-bot Date: Mon, 30 Dec 2024 00:15:47 +0000 Subject: [PATCH 01/17] init [skip ci] --- .codecrafters/compile.sh | 11 +++++++++ .codecrafters/run.sh | 11 +++++++++ .gitattributes | 1 + .gitignore | 3 +++ README.md | 35 +++++++++++++++++++++++++++ codecrafters.yml | 11 +++++++++ pom.xml | 52 ++++++++++++++++++++++++++++++++++++++++ src/main/java/Main.java | 11 +++++++++ your_program.sh | 24 +++++++++++++++++++ 9 files changed, 159 insertions(+) create mode 100755 .codecrafters/compile.sh create mode 100755 .codecrafters/run.sh create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 codecrafters.yml create mode 100644 pom.xml create mode 100644 src/main/java/Main.java create mode 100755 your_program.sh diff --git a/.codecrafters/compile.sh b/.codecrafters/compile.sh new file mode 100755 index 0000000..968edea --- /dev/null +++ b/.codecrafters/compile.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to compile your program on CodeCrafters +# +# This runs before .codecrafters/run.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +mvn -B package -Ddir=/tmp/codecrafters-build-shell-java diff --git a/.codecrafters/run.sh b/.codecrafters/run.sh new file mode 100755 index 0000000..a0a1bd9 --- /dev/null +++ b/.codecrafters/run.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# +# This script is used to run your program on CodeCrafters +# +# This runs after .codecrafters/compile.sh +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit on failure + +exec java -jar /tmp/codecrafters-build-shell-java/codecrafters-shell.jar "$@" diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a48b18a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.jar +target/ +.idea/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b9591e --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +[![progress-banner](https://backend.codecrafters.io/progress/shell/f47d6fb6-94e9-44fa-a664-fd92a07e7ef8)](https://app.codecrafters.io/users/codecrafters-bot?r=2qF) + +This is a starting point for Java solutions to the +["Build Your Own Shell" Challenge](https://app.codecrafters.io/courses/shell/overview). + +In this challenge, you'll build your own POSIX compliant shell that's capable of +interpreting shell commands, running external programs and builtin commands like +cd, pwd, echo and more. Along the way, you'll learn about shell command parsing, +REPLs, builtin commands, and more. + +**Note**: If you're viewing this repo on GitHub, head over to +[codecrafters.io](https://codecrafters.io) to try the challenge. + +# Passing the first stage + +The entry point for your `shell` implementation is in `src/main/java/Main.java`. +Study and uncomment the relevant code, and push your changes to pass the first +stage: + +```sh +git commit -am "pass 1st stage" # any msg +git push origin master +``` + +Time to move on to the next stage! + +# Stage 2 & beyond + +Note: This section is for stages 2 and beyond. + +1. Ensure you have `mvn` installed locally +1. Run `./your_program.sh` to run your program, which is implemented in + `src/main/java/Main.java`. +1. Commit your changes and run `git push origin master` to submit your solution + to CodeCrafters. Test output will be streamed to your terminal. diff --git a/codecrafters.yml b/codecrafters.yml new file mode 100644 index 0000000..2406954 --- /dev/null +++ b/codecrafters.yml @@ -0,0 +1,11 @@ +# Set this to true if you want debug logs. +# +# These can be VERY verbose, so we suggest turning them off +# unless you really need them. +debug: false + +# Use this to change the Java version used to run your code +# on Codecrafters. +# +# Available versions: java-23 +language_pack: java-23 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..71b07b1 --- /dev/null +++ b/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + io.codecrafters + codecrafters-shell + 1.0 + + + 23 + 23 + UTF-8 + 23 + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + codecrafters-shell + + jar-with-dependencies + + false + + + true + + Main + + + ${dir} + + + + + make-assembly + package + + single + + + + + + + + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..46fcc4b --- /dev/null +++ b/src/main/java/Main.java @@ -0,0 +1,11 @@ +import java.util.Scanner; + +public class Main { + public static void main(String[] args) throws Exception { + // Uncomment this block to pass the first stage + // System.out.print("$ "); + + Scanner scanner = new Scanner(System.in); + String input = scanner.nextLine(); + } +} diff --git a/your_program.sh b/your_program.sh new file mode 100755 index 0000000..bec7f4a --- /dev/null +++ b/your_program.sh @@ -0,0 +1,24 @@ +#!/bin/sh +# +# Use this script to run your program LOCALLY. +# +# Note: Changing this script WILL NOT affect how CodeCrafters runs your program. +# +# Learn more: https://codecrafters.io/program-interface + +set -e # Exit early if any commands fail + +# Copied from .codecrafters/compile.sh +# +# - Edit this to change how your program compiles locally +# - Edit .codecrafters/compile.sh to change how your program compiles remotely +( + cd "$(dirname "$0")" # Ensure compile steps are run within the repository directory + mvn -B package -Ddir=/tmp/codecrafters-build-shell-java +) + +# Copied from .codecrafters/run.sh +# +# - Edit this to change how your program runs locally +# - Edit .codecrafters/run.sh to change how your program runs remotely +exec java -jar /tmp/codecrafters-build-shell-java/codecrafters-shell.jar "$@" From d3c7c6927c8e0bf6173b966de19139518503bf53 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Mon, 30 Dec 2024 09:32:39 +0530 Subject: [PATCH 02/17] test From a6a54568c7e0be4d35b099a60b9eebe993cda337 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Mon, 30 Dec 2024 09:38:05 +0530 Subject: [PATCH 03/17] uncommented prompt $ --- src/main/java/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 46fcc4b..ec22286 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -3,7 +3,7 @@ public class Main { public static void main(String[] args) throws Exception { // Uncomment this block to pass the first stage - // System.out.print("$ "); + System.out.print("$ "); Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); From 1b3ab6dcd4ce5905746634e28f90ee1e8412614b Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Mon, 30 Dec 2024 09:44:59 +0530 Subject: [PATCH 04/17] command not found line is added --- src/main/java/Main.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index ec22286..b955591 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,5 +7,7 @@ public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); + System.out.println(input + ": command not found"); + } } From 6791a4e599ee3f1a40c6dfe322a7adcc1cfd9f11 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Mon, 30 Dec 2024 09:56:47 +0530 Subject: [PATCH 05/17] codecrafters submit [skip ci] --- src/main/java/Main.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index b955591..7dcc5e7 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -3,11 +3,11 @@ public class Main { public static void main(String[] args) throws Exception { // Uncomment this block to pass the first stage - System.out.print("$ "); - Scanner scanner = new Scanner(System.in); - String input = scanner.nextLine(); - System.out.println(input + ": command not found"); - + while (true) { + System.out.print("$ "); + String input = scanner.nextLine(); + System.out.println(input + ": command not found"); + } } } From cd07e3ca3a03a43e76f73fb2095497f5bbef1f92 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Mon, 30 Dec 2024 10:02:39 +0530 Subject: [PATCH 06/17] codecrafters submit [skip ci] --- src/main/java/Main.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7dcc5e7..68bf157 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,7 +7,13 @@ public static void main(String[] args) throws Exception { while (true) { System.out.print("$ "); String input = scanner.nextLine(); - System.out.println(input + ": command not found"); + + if (!input.equalsIgnoreCase("exit 0")) { + System.out.println(input + ": command not found"); + } else { + return; + } + } } } From 6bcdf3177314382a9e8361e1e82837a9d1f145c4 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Mon, 30 Dec 2024 10:28:44 +0530 Subject: [PATCH 07/17] codecrafters submit [skip ci] --- src/main/java/Main.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 68bf157..14b69c0 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -9,7 +9,12 @@ public static void main(String[] args) throws Exception { String input = scanner.nextLine(); if (!input.equalsIgnoreCase("exit 0")) { - System.out.println(input + ": command not found"); + + if (input.startsWith("echo")) { + System.out.println(input.substring("echo".length() + 1, input.length())); + } else { + System.out.println(input + ": command not found"); + } } else { return; } From 679d7d8ea33b9f398060b38fa212b439fccea079 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Mon, 30 Dec 2024 10:39:53 +0530 Subject: [PATCH 08/17] codecrafters submit [skip ci] --- src/main/java/Main.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 14b69c0..515f169 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -12,6 +12,17 @@ public static void main(String[] args) throws Exception { if (input.startsWith("echo")) { System.out.println(input.substring("echo".length() + 1, input.length())); + } else if (input.startsWith("type")) { + String lit0 = input.substring("type".length() + 1, input.length()); + if (lit0.equalsIgnoreCase("echo")) { + System.out.println("echo is a shell builtin"); + } else if (lit0.equalsIgnoreCase("exit")) { + System.out.println("exit is a shell builtin"); + } else if (lit0.equalsIgnoreCase("type")) { + System.out.println("type is a shell builtin"); + } else { + System.out.println(lit0 + ": not found"); + } } else { System.out.println(input + ": command not found"); } From 01575449de0871db948cabd3ab8da7430ce7d885 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Mon, 30 Dec 2024 11:40:15 +0530 Subject: [PATCH 09/17] debug enabled From 890cf78b35f8aa32248ea84576b6e371a2cbaaa3 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Tue, 31 Dec 2024 09:52:01 +0530 Subject: [PATCH 10/17] codecrafters submit [skip ci] --- codecrafters.yml | 2 +- src/main/java/Main.java | 98 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 12 deletions(-) diff --git a/codecrafters.yml b/codecrafters.yml index 2406954..6ef99a9 100644 --- a/codecrafters.yml +++ b/codecrafters.yml @@ -2,7 +2,7 @@ # # These can be VERY verbose, so we suggest turning them off # unless you really need them. -debug: false +debug: true # Use this to change the Java version used to run your code # on Codecrafters. diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 515f169..ab4e072 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,9 +1,80 @@ import java.util.Scanner; +import java.util.Arrays; +import java.io.*; public class Main { + public static void notFound(String str) { + + System.out.println(str + ": not found"); + } + + public static void debugOn(String str) { + + System.out.printf("%s%n", str); + } + + public static String getPath(String typeString) { + Boolean pathFlag = Boolean.FALSE; + String path = System.getenv("PATH"); + String typeStringLoc = typeString + ": not found"; + // String typeStringLocdebug = ""; + + if (path != null || !(path.equalsIgnoreCase("")) || typeString != null) { + // System.out.println("INSIDE PATH LIST TYPE "); + // System.out.printf("HELLO %s", path); + String[] paths = path.split(":"); + + // System.out.printf(Arrays.toString(paths)); + // System.out.println("BEFORE PRINTING PATHS "); + for (String p : paths) { + pathFlag = Boolean.FALSE; + // System.out.printf("%s%n", p); + // System.out.println("FILES ->" + p); + + File file = new File(p); + File[] files = file.listFiles(); + + // System.out.printf(Arrays.toString(files)); + if (files != null) { + // System.out.println(Arrays.asList(files).contains(typeString)); + for (File f : files) { + // System.out.printf("%s%n", f); + String fstr = f.toString(); + String subFstr = fstr.substring(fstr.lastIndexOf(File.separator) + 1); + // System.out.println(fstr); + // System.out.println(subFstr); + if (subFstr.equalsIgnoreCase(typeString)) { + pathFlag = Boolean.TRUE; + // System.out.printf("CAT FOUND%s%n", f); + // File filefound = new File(f); + // System.out.println(typeString + " is " + filefound.getParent()); + // System.out.println(typeString + " is " + f); + typeStringLoc = typeString + " is " + f; + break; + } + } + } + } + // debugOn("COMMAND WHEN ??" + typeStringLoc); + // typeStringLocdebug += "\t" + typeStringLoc; + // if (pathFlag != Boolean.TRUE) { + // // System.out.println("HELLO BEFORE pathFLAG" + pathFlag); + // // System.out.println("INSIDE pathFLAG"); + // // notFound(typeString); + // typeStringLoc = typeString + " : not found"; + // // System.out.println("typeStringLoc " + typeStringLoc); + + // } + } + // debugOn("typeStringLocdebug " + typeStringLocdebug); + return typeStringLoc; + } + public static void main(String[] args) throws Exception { - // Uncomment this block to pass the first stage + + String[] built_in_commands = { "echo", "exit", "type" }; Scanner scanner = new Scanner(System.in); + while (true) { System.out.print("$ "); String input = scanner.nextLine(); @@ -11,23 +82,28 @@ public static void main(String[] args) throws Exception { if (!input.equalsIgnoreCase("exit 0")) { if (input.startsWith("echo")) { + System.out.println(input.substring("echo".length() + 1, input.length())); + } else if (input.startsWith("type")) { - String lit0 = input.substring("type".length() + 1, input.length()); - if (lit0.equalsIgnoreCase("echo")) { - System.out.println("echo is a shell builtin"); - } else if (lit0.equalsIgnoreCase("exit")) { - System.out.println("exit is a shell builtin"); - } else if (lit0.equalsIgnoreCase("type")) { - System.out.println("type is a shell builtin"); + + String typeString = input.substring("type".length() + 1, input.length()); + + if (Arrays.asList(built_in_commands).contains(typeString)) { + System.out.println(typeString + " is a shell builtin"); + // break; + } else { - System.out.println(lit0 + ": not found"); + // debugOn("BEFORE PATH"); + String typePath = getPath(typeString); + System.out.println(typePath); + // debugOn("AFTER PATH" + typePath); } } else { - System.out.println(input + ": command not found"); + notFound(input); } } else { - return; + break; } } From 9b47cd3b54aaea505423257dc5c178dfd6ab6e86 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Tue, 31 Dec 2024 12:27:04 +0530 Subject: [PATCH 11/17] codecrafters submit [skip ci] --- src/main/java/Main.java | 87 +++++++++++++++++++++++++++++++---------- 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index ab4e072..e62df30 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -14,7 +14,7 @@ public static void debugOn(String str) { } public static String getPath(String typeString) { - Boolean pathFlag = Boolean.FALSE; + // Boolean pathFlag = Boolean.FALSE; String path = System.getenv("PATH"); String typeStringLoc = typeString + ": not found"; // String typeStringLocdebug = ""; @@ -27,7 +27,7 @@ public static String getPath(String typeString) { // System.out.printf(Arrays.toString(paths)); // System.out.println("BEFORE PRINTING PATHS "); for (String p : paths) { - pathFlag = Boolean.FALSE; + // pathFlag = Boolean.FALSE; // System.out.printf("%s%n", p); // System.out.println("FILES ->" + p); @@ -41,35 +41,75 @@ public static String getPath(String typeString) { // System.out.printf("%s%n", f); String fstr = f.toString(); String subFstr = fstr.substring(fstr.lastIndexOf(File.separator) + 1); - // System.out.println(fstr); - // System.out.println(subFstr); if (subFstr.equalsIgnoreCase(typeString)) { - pathFlag = Boolean.TRUE; - // System.out.printf("CAT FOUND%s%n", f); - // File filefound = new File(f); - // System.out.println(typeString + " is " + filefound.getParent()); - // System.out.println(typeString + " is " + f); + // pathFlag = Boolean.TRUE; typeStringLoc = typeString + " is " + f; break; } } } } - // debugOn("COMMAND WHEN ??" + typeStringLoc); - // typeStringLocdebug += "\t" + typeStringLoc; - // if (pathFlag != Boolean.TRUE) { - // // System.out.println("HELLO BEFORE pathFLAG" + pathFlag); - // // System.out.println("INSIDE pathFLAG"); - // // notFound(typeString); - // typeStringLoc = typeString + " : not found"; - // // System.out.println("typeStringLoc " + typeStringLoc); - - // } } - // debugOn("typeStringLocdebug " + typeStringLocdebug); return typeStringLoc; } + public static String getPlainPath(String typeString) { + // Boolean pathFlag = Boolean.FALSE; + String path = System.getenv("PATH"); + String typeStringLoc = typeString + ": not found"; + // String typeStringLocdebug = ""; + + if (path != null || !(path.equalsIgnoreCase("")) || typeString != null) { + // System.out.println("INSIDE PATH LIST TYPE "); + // System.out.printf("HELLO %s", path); + String[] paths = path.split(":"); + + // System.out.printf(Arrays.toString(paths)); + // System.out.println("BEFORE PRINTING PATHS "); + for (String p : paths) { + // pathFlag = Boolean.FALSE; + // System.out.printf("%s%n", p); + // System.out.println("FILES ->" + p); + + File file = new File(p); + File[] files = file.listFiles(); + + // System.out.printf(Arrays.toString(files)); + if (files != null) { + // System.out.println(Arrays.asList(files).contains(typeString)); + for (File f : files) { + // System.out.printf("%s%n", f); + String fstr = f.toString(); + String subFstr = fstr.substring(fstr.lastIndexOf(File.separator) + 1); + if (subFstr.equalsIgnoreCase(typeString)) { + // pathFlag = Boolean.TRUE; + typeStringLoc = f.toString(); + break; + } + } + } + } + } + return typeStringLoc; + } + + public static void executeCommand(String input) throws Exception { + String command = input.split(" ")[0]; + // debugOn(command); + String path = getPlainPath(command); + File fPath = new File(path); + // debugOn(path); + if (!fPath.isFile()) { + System.out.println(command + ": command not found"); + } else { + String fullPath = path + input.substring(command.length()); + // debugOn(fullPath); + // System.out.println(Arrays.toString(fullPath.split(" "))); + Process p = Runtime.getRuntime().exec(fullPath.split(" ")); + p.getInputStream().transferTo(System.out); + } + } + public static void main(String[] args) throws Exception { String[] built_in_commands = { "echo", "exit", "type" }; @@ -100,12 +140,17 @@ public static void main(String[] args) throws Exception { // debugOn("AFTER PATH" + typePath); } } else { - notFound(input); + // debugOn("AM I HERE"); + // notFound(input); + // String typePath = getPath(input); + // System.out.println(typePath); + executeCommand(input); } } else { break; } + // debugOn(input); } } } From fad1dda0bd9237b483401e67d41c1f5842566eac Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Tue, 31 Dec 2024 19:34:57 +0530 Subject: [PATCH 12/17] codecrafters submit [skip ci] --- src/main/java/Main.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index e62df30..beb8443 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -99,7 +99,16 @@ public static void executeCommand(String input) throws Exception { String path = getPlainPath(command); File fPath = new File(path); // debugOn(path); - if (!fPath.isFile()) { + if (command.equalsIgnoreCase("pwd")) { + File pwd = new File(command); + // System.out.println(pwd.getAbsolutePath()); + // System.out.println(pwd.getCanonicalPath()); + // System.out.println(pwd.getName()); + // System.out.println(pwd.getParent()); + System.out.println(pwd.getCanonicalFile().getParent()); + // System.out.println(pwd.getPath()); + + } else if (!fPath.isFile()) { System.out.println(command + ": command not found"); } else { String fullPath = path + input.substring(command.length()); @@ -112,7 +121,7 @@ public static void executeCommand(String input) throws Exception { public static void main(String[] args) throws Exception { - String[] built_in_commands = { "echo", "exit", "type" }; + String[] built_in_commands = { "echo", "exit", "type", "pwd" }; Scanner scanner = new Scanner(System.in); while (true) { From 1adb7519602e281c4ccb83c8dcfeadc7f59a345c Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Tue, 31 Dec 2024 20:49:24 +0530 Subject: [PATCH 13/17] codecrafters submit [skip ci] From 99cdcd499571f678f598266eba72ab428e9006c1 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Tue, 31 Dec 2024 20:50:26 +0530 Subject: [PATCH 14/17] pwd implemented - imp1 From 5665a9aaad5fd2f68a61f1dbdc2e72201c312bf8 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Tue, 31 Dec 2024 20:51:12 +0530 Subject: [PATCH 15/17] codecrafters submit [skip ci] From 9b2b5191eeb0805c0cd9489856080fc4af3b40e0 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Wed, 8 Jan 2025 18:01:03 +0530 Subject: [PATCH 16/17] codecrafters submit [skip ci] --- codecrafters.yml | 1 + src/main/java/Main.java | 52 ++++++++++++++++++++++++++++++----------- your_program.sh | 1 + 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/codecrafters.yml b/codecrafters.yml index 6ef99a9..915ee64 100644 --- a/codecrafters.yml +++ b/codecrafters.yml @@ -1,4 +1,5 @@ # Set this to true if you want debug logs. +# Help me to master all my true abilites and make me understand myself and attain salvation # # These can be VERY verbose, so we suggest turning them off # unless you really need them. diff --git a/src/main/java/Main.java b/src/main/java/Main.java index beb8443..fbc40d9 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -3,16 +3,30 @@ import java.io.*; public class Main { + public static void println(String str) { + + System.out.println(str); + } + + public static void print(String str) { + + System.out.print(str); + } + public static void notFound(String str) { System.out.println(str + ": not found"); } public static void debugOn(String str) { - System.out.printf("%s%n", str); } + public static String getCwd(String command) throws Exception { + File pwd = new File(command); + return pwd.getCanonicalFile().getParent(); + } + public static String getPath(String typeString) { // Boolean pathFlag = Boolean.FALSE; String path = System.getenv("PATH"); @@ -94,24 +108,34 @@ public static String getPlainPath(String typeString) { } public static void executeCommand(String input) throws Exception { + // pass the entire command because the command might come with its own arguments String command = input.split(" ")[0]; + // String commandArgs = input.split(" ")[1]; // debugOn(command); + // debugOn(commandArgs); String path = getPlainPath(command); File fPath = new File(path); // debugOn(path); if (command.equalsIgnoreCase("pwd")) { - File pwd = new File(command); - // System.out.println(pwd.getAbsolutePath()); - // System.out.println(pwd.getCanonicalPath()); - // System.out.println(pwd.getName()); - // System.out.println(pwd.getParent()); - System.out.println(pwd.getCanonicalFile().getParent()); - // System.out.println(pwd.getPath()); + String cmd = "pwd"; + String printPWD = getCwd(cmd); + System.out.println(printPWD); } else if (!fPath.isFile()) { - System.out.println(command + ": command not found"); + println(command + ": command not found"); } else { - String fullPath = path + input.substring(command.length()); + // println("HELLO AM HERE"); + String commandArgs = input.split(" ")[1]; + // println("IS TERE AN ARG"); + // println(commandArgs); + // debugOn(command); + // debugOn(commandArgs); + String path0 = getPath(command); + File fPath0 = new File(path0); + + String fullPath = (!commandArgs.equalsIgnoreCase("")) ? command + " " + commandArgs + : path + input.substring(command.length()); + // debugOn(fullPath); // System.out.println(Arrays.toString(fullPath.split(" "))); Process p = Runtime.getRuntime().exec(fullPath.split(" ")); @@ -125,27 +149,27 @@ public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); while (true) { - System.out.print("$ "); + print("$ "); String input = scanner.nextLine(); if (!input.equalsIgnoreCase("exit 0")) { if (input.startsWith("echo")) { - System.out.println(input.substring("echo".length() + 1, input.length())); + println(input.substring("echo".length() + 1, input.length())); } else if (input.startsWith("type")) { String typeString = input.substring("type".length() + 1, input.length()); if (Arrays.asList(built_in_commands).contains(typeString)) { - System.out.println(typeString + " is a shell builtin"); + println(typeString + " is a shell builtin"); // break; } else { // debugOn("BEFORE PATH"); String typePath = getPath(typeString); - System.out.println(typePath); + println(typePath); // debugOn("AFTER PATH" + typePath); } } else { diff --git a/your_program.sh b/your_program.sh index bec7f4a..ac33f36 100755 --- a/your_program.sh +++ b/your_program.sh @@ -21,4 +21,5 @@ set -e # Exit early if any commands fail # # - Edit this to change how your program runs locally # - Edit .codecrafters/run.sh to change how your program runs remotely +# print java_home as debug mode env is enabled exec java -jar /tmp/codecrafters-build-shell-java/codecrafters-shell.jar "$@" From fbdb63438fd4d7fd2b0479cd98df3e1629589c41 Mon Sep 17 00:00:00 2001 From: krishnagkashyap Date: Sun, 12 Jan 2025 11:05:36 +0530 Subject: [PATCH 17/17] codecrafters submit [skip ci]