-
Notifications
You must be signed in to change notification settings - Fork 1
add cdls #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: bash
Are you sure you want to change the base?
add cdls #4
Changes from 1 commit
94045d1
7819d51
9530499
5b5bef3
66ba27e
bec7ca4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package hse.nedikov.bash | ||
|
|
||
| import hse.nedikov.bash.Environment.State.* | ||
| import java.io.File | ||
|
|
||
| /** | ||
| * Environment of the interpreter | ||
|
|
@@ -12,6 +13,45 @@ class Environment { | |
|
|
||
| private val varMap = HashMap<String, String>() | ||
| private var state: State = Working | ||
| private var curDir = File("./") | ||
|
|
||
| /** | ||
| * Switches directory | ||
| * @param change new directory path | ||
| * @return true is change was successful false otherwise | ||
| */ | ||
| fun updateDir(change: String): Boolean { | ||
| val newDirectory = getFile(change) | ||
| val success = newDirectory.isDirectory | ||
|
|
||
| if (success) { | ||
| curDir = newDirectory | ||
| } | ||
|
|
||
| return success | ||
| } | ||
|
|
||
| /** | ||
| * Function for full path for current path | ||
| * @param path path to convert | ||
| * @return full path | ||
| */ | ||
| fun getPath(path: String): String { | ||
| return getFile(path).canonicalPath | ||
| } | ||
|
|
||
| /** | ||
| * Function for getting file for path | ||
| * @param path path to file | ||
| * @return file for path | ||
| */ | ||
| fun getFile(path: String): File { | ||
| return if (File(path).isAbsolute) { | ||
| File(path).canonicalFile | ||
| } else { | ||
| File(curDir, path).canonicalFile | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А в Path эта логика уже реализована (Path.resolve) |
||
| } | ||
|
|
||
| /** | ||
| * Map of the local variables | ||
|
|
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package hse.nedikov.bash.logic.commands | ||
|
|
||
| import hse.nedikov.bash.Environment | ||
| import hse.nedikov.bash.logic.Command | ||
| import java.io.PipedReader | ||
| import java.io.PipedWriter | ||
|
|
||
| /** | ||
| * Class that switches current directory | ||
| */ | ||
| class Cd(private val arguments: ArrayList<String>, override val env: Environment) : Command(env) { | ||
| /** | ||
| * Changes the working directory | ||
| */ | ||
| override fun execute(input: PipedReader, output: PipedWriter) { | ||
| return execute(output) | ||
| } | ||
|
|
||
| /** | ||
| * Changes the working directory | ||
| * switches to home of zero arguments | ||
| * switches to input dir if one argument and success | ||
| */ | ||
| override fun execute(output: PipedWriter) { | ||
| if (arguments.size > 1) { | ||
| output.write("Error: extra args in cd command") | ||
| return | ||
| } | ||
|
|
||
| val path = arguments.getOrElse(0) { "./" } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В bash если cd не передать аргументов, то рабочая директория меняется на домашнюю |
||
|
|
||
| if (!env.updateDir(path)) { | ||
| output.write("File not found error") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не надо смешивать вывод команд и сообщения об ошибках. Бросайте исключения |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package hse.nedikov.bash.logic.commands | ||
|
|
||
| import hse.nedikov.bash.Environment | ||
| import hse.nedikov.bash.logic.Command | ||
| import java.io.PipedReader | ||
| import java.io.PipedWriter | ||
|
|
||
| /** | ||
| * Class that prints all files and directories in specified path or current directory | ||
| */ | ||
| class Ls(private val arguments: ArrayList<String>, override val env: Environment) : Command(env) { | ||
| /** | ||
| * Prints all files and directories in specified path or current directory | ||
| * @param input input stream | ||
| * @param output output stream | ||
| */ | ||
| override fun execute(input: PipedReader, output: PipedWriter) { | ||
| return execute(output) | ||
| } | ||
|
|
||
| /** | ||
| * Prints all files and directories in specified path or current directory | ||
| * if arguments more than one error will occur | ||
| * @param output output stream | ||
| */ | ||
| override fun execute(output: PipedWriter) { | ||
| if (arguments.size > 1) { | ||
| output.write("Error: extra args in ls command") | ||
| return | ||
| } | ||
|
|
||
| val arg = env.getFile(arguments.getOrElse(0) { "./" }) | ||
| val sep = System.lineSeparator() | ||
|
|
||
| if (!(arg.isFile || arg.isDirectory)) { | ||
| output.write("Directory not found for ls") | ||
| return | ||
| } | ||
|
|
||
| if (arg.isDirectory) { | ||
| arg.listFiles().sorted().forEach { | ||
| if (!it.isHidden) { | ||
| output.write(it.name + sep) | ||
| } | ||
| } | ||
|
|
||
| return | ||
| } | ||
|
|
||
| output.write(arg.name + sep) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Работать с путями как со строками в 2k19 довольно странно - есть Path