Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified bash/README.md
100644 → 100755
Empty file.
Empty file modified bash/build.gradle
100644 → 100755
Empty file.
Empty file modified bash/gradle/wrapper/gradle-wrapper.jar
100644 → 100755
Empty file.
Empty file modified bash/gradle/wrapper/gradle-wrapper.properties
100644 → 100755
Empty file.
Empty file modified bash/gradlew
100644 → 100755
Empty file.
Empty file modified bash/gradlew.bat
100644 → 100755
Empty file.
Empty file modified bash/settings.gradle
100644 → 100755
Empty file.
Empty file modified bash/src/main/kotlin/hse/nedikov/bash/Controller.kt
100644 → 100755
Empty file.
40 changes: 40 additions & 0 deletions bash/src/main/kotlin/hse/nedikov/bash/Environment.kt
100644 → 100755
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
Expand All @@ -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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Работать с путями как со строками в 2k19 довольно странно - есть Path

}

/**
* 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
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А в Path эта логика уже реализована (Path.resolve)

}

/**
* Map of the local variables
Expand Down
Empty file modified bash/src/main/kotlin/hse/nedikov/bash/Lexer.kt
100644 → 100755
Empty file.
Empty file modified bash/src/main/kotlin/hse/nedikov/bash/Parser.kt
100644 → 100755
Empty file.
Empty file.
14 changes: 8 additions & 6 deletions bash/src/main/kotlin/hse/nedikov/bash/logic/Command.kt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.io.*
/**
* Base class for all interpreter commands
*/
abstract class Command {
abstract class Command(open val env: Environment) {
protected abstract fun execute(input: PipedReader, output: PipedWriter)
protected abstract fun execute(output: PipedWriter)

Expand Down Expand Up @@ -46,12 +46,14 @@ abstract class Command {
return Assign(name.take(name.length - 1), args, env)
}
return when (name) {
"echo" -> Echo(args)
"wc" -> WordCount(args)
"pwd" -> Pwd()
"echo" -> Echo(args, env)
"wc" -> WordCount(args, env)
"pwd" -> Pwd(env)
"exit" -> Exit(env)
"cat" -> Cat(args)
else -> OuterCommand(name, args)
"cat" -> Cat(args, env)
"cd" -> Cd(args, env)
"ls" -> Ls(args, env)
else -> OuterCommand(name, args, env)
}
}
}
Expand Down

This file was deleted.

5 changes: 3 additions & 2 deletions bash/src/main/kotlin/hse/nedikov/bash/logic/commands/Cat.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package hse.nedikov.bash.logic.commands

import hse.nedikov.bash.Environment
import hse.nedikov.bash.logic.Command
import java.io.*
import java.lang.Exception

/**
* cat command which prints files entries to the output stream
*/
class Cat(private val arguments: ArrayList<String>) : Command() {
class Cat(private val arguments: ArrayList<String>, override val env: Environment) : Command(env) {
/**
* Prints input to the output if has no arguments and prints entries of files from arguments otherwise
*/
Expand All @@ -22,7 +23,7 @@ class Cat(private val arguments: ArrayList<String>) : Command() {
override fun execute(output: PipedWriter) {
for (arg in arguments) {
try {
FileReader(arg).forEachLine { output.write(arg) }
FileReader(env.getPath(arg)).forEachLine { output.write(arg) }
} catch (e: Exception) {
output.write("cat: ${e.message}")
}
Expand Down
36 changes: 36 additions & 0 deletions bash/src/main/kotlin/hse/nedikov/bash/logic/commands/Cd.kt
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) { "./" }
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

В bash если cd не передать аргументов, то рабочая директория меняется на домашнюю


if (!env.updateDir(path)) {
output.write("File not found error")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не надо смешивать вывод команд и сообщения об ошибках. Бросайте исключения

}
}
}
3 changes: 2 additions & 1 deletion bash/src/main/kotlin/hse/nedikov/bash/logic/commands/Echo.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hse.nedikov.bash.logic.commands

import hse.nedikov.bash.Environment
import hse.nedikov.bash.logic.Command
import java.io.*
import java.util.*
Expand All @@ -8,7 +9,7 @@ import java.util.*
/**
* echo command which prints arguments to the output
*/
class Echo(private val arguments: ArrayList<String>) : Command() {
class Echo(private val arguments: ArrayList<String>, override val env: Environment = Environment()) : Command(env) {
/**
* Prints arguments which are joined with spaces to the output
*/
Expand Down
52 changes: 52 additions & 0 deletions bash/src/main/kotlin/hse/nedikov/bash/logic/commands/Ls.kt
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)
}
}
3 changes: 2 additions & 1 deletion bash/src/main/kotlin/hse/nedikov/bash/logic/commands/OuterCommand.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hse.nedikov.bash.logic.commands

import hse.nedikov.bash.Environment
import hse.nedikov.bash.logic.Command
import java.io.*
import java.util.concurrent.Executors
Expand All @@ -12,7 +13,7 @@ import java.util.concurrent.TimeUnit
* Class for calling commands in outer interpreter
* @param name name of the command
*/
class OuterCommand(private val name: String, private val arguments: ArrayList<String>) : Command() {
class OuterCommand(private val name: String, private val arguments: ArrayList<String>, override val env: Environment) : Command(env) {
/**
* Calls the command in outer interpreter and print theirs output or error to the output
* in case when the command is executed in less than 10 seconds
Expand Down
5 changes: 3 additions & 2 deletions bash/src/main/kotlin/hse/nedikov/bash/logic/commands/Pwd.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package hse.nedikov.bash.logic.commands

import hse.nedikov.bash.Environment
import hse.nedikov.bash.logic.Command
import java.io.*

/**
* pwd command which prints current working directory
*/
class Pwd : Command() {
class Pwd(override val env: Environment) : Command(env) {
/**
* Prints current working directory to the output
*/
Expand All @@ -19,7 +20,7 @@ class Pwd : Command() {
* Prints current working directory to the output
*/
override fun execute(output: PipedWriter) {
output.write(System.getProperty("user.dir") + "\n")
output.write(env.getPath("./") + "\n")
}

}
5 changes: 3 additions & 2 deletions bash/src/main/kotlin/hse/nedikov/bash/logic/commands/WordCount.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package hse.nedikov.bash.logic.commands

import hse.nedikov.bash.Environment
import hse.nedikov.bash.logic.Command
import java.io.*
import java.lang.Exception

/**
* wc command which calculates count of lines, words and bytes in files or input
*/
class WordCount(private val arguments: ArrayList<String>) : Command() {
class WordCount(private val arguments: ArrayList<String>, override val env: Environment) : Command(env) {
/**
* Calculates count of lines, words and bytes in input if arguments is empty and in files otherwise
*/
Expand All @@ -27,7 +28,7 @@ class WordCount(private val arguments: ArrayList<String>) : Command() {
val result = WCResult()
for (arg in arguments) {
try {
val r = calcInput(FileReader(arg))
val r = calcInput(FileReader(env.getPath(arg)))
output.write("${r.lines} ${r.words} ${r.bytes} $arg\n")
} catch (e: Exception) {
output.write("wc: ${e.message}\n")
Expand Down
4 changes: 2 additions & 2 deletions bash/src/main/kotlin/hse/nedikov/bash/logic/environment/Assign.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package hse.nedikov.bash.logic.environment

import hse.nedikov.bash.Environment
import hse.nedikov.bash.logic.EnvironmentCommand
import hse.nedikov.bash.logic.Command
import java.io.PipedReader
import java.io.PipedWriter
import java.lang.Exception
Expand All @@ -11,7 +11,7 @@ import java.lang.Exception
* @param name name of variable
*/
class Assign(private val name:String, private val arguments: ArrayList<String>, override val env: Environment)
: EnvironmentCommand(env) {
: Command(env) {

/**
* Do nothing in this case
Expand Down
4 changes: 2 additions & 2 deletions bash/src/main/kotlin/hse/nedikov/bash/logic/environment/Exit.kt
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package hse.nedikov.bash.logic.environment

import hse.nedikov.bash.Environment
import hse.nedikov.bash.logic.EnvironmentCommand
import hse.nedikov.bash.logic.Command
import java.io.*

/**
* Class for command which closes the interpreter
*/
class Exit(override val env: Environment) : EnvironmentCommand(env) {
class Exit(override val env: Environment) : Command(env) {
/**
* Stops the interpreter
*/
Expand Down
Empty file modified bash/src/test/kotlin/hse/nedikov/bash/LexerTest.kt
100644 → 100755
Empty file.
Empty file modified bash/src/test/kotlin/hse/nedikov/bash/ParserTest.kt
100644 → 100755
Empty file.
Empty file modified bash/src/test/kotlin/hse/nedikov/bash/TestUtil.kt
100644 → 100755
Empty file.
Loading