Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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.
1 change: 1 addition & 0 deletions bash/build.gradle
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileKotlin {
Expand Down
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.
49 changes: 49 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,13 @@
package hse.nedikov.bash

import hse.nedikov.bash.Environment.State.*
import hse.nedikov.bash.exceptions.DirectoryUpdateException
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files



/**
* Environment of the interpreter
Expand All @@ -12,6 +19,48 @@ class Environment {

private val varMap = HashMap<String, String>()
private var state: State = Working
private var curPath = Paths.get(".").toAbsolutePath().normalize()

/**
* Switches directory
* @param change new directory path
* @return true is change was successful false otherwise
*/
fun updateDir(change: String) {
val newDirectory = curPath.resolve(change)
if (!Files.exists(newDirectory) || !Files.isDirectory(newDirectory)) {
throw DirectoryUpdateException("can't update current directory: specified directory doesn't exist or not a directory")
}

curPath = newDirectory.toAbsolutePath().normalize()
}

/**
* Function for full path for current path
* @param path path to convert
* @return full path
*/
fun getPathString(path: String): String {
return getPath(path).toAbsolutePath().normalize().toString()
}

/**
* Function for getting file for path
* @param path path to file
* @return file for path
*/
private fun getPath(path: String): Path {
return curPath.resolve(path)
}

/**
* Function for getting Path for path string
* @param path path string
* @return Path for path string
*/
fun getFile(path: String): File {
return getPath(path).toFile()
}

/**
* 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package hse.nedikov.bash.exceptions

import java.lang.Exception

/**
* Exception for directory updates errors
*/
internal class DirectoryUpdateException(message: String) : Exception("Error occurred while updating directory: $message")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package hse.nedikov.bash.exceptions

import java.lang.Exception

/**
* Exception for incorrect arguments
*/
internal class IncorrectArgumentsException(message: String) : Exception("Incorrect arguments exception: $message")
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.getPathString(arg)).forEachLine { output.write(arg) }
} catch (e: Exception) {
output.write("cat: ${e.message}")
}
Expand Down
35 changes: 35 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,35 @@
package hse.nedikov.bash.logic.commands

import hse.nedikov.bash.Environment
import hse.nedikov.bash.exceptions.DirectoryUpdateException
import hse.nedikov.bash.exceptions.IncorrectArgumentsException
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) {
throw IncorrectArgumentsException("extra arguments in cd command")
}

val path = arguments.getOrElse(0) { System.getProperty("user.home") }

env.updateDir(path)
}
}
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.exceptions.IncorrectArgumentsException
import hse.nedikov.bash.logic.Command
import java.io.FileNotFoundException
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) {
throw IncorrectArgumentsException("extra arguments in ls command")
}

val arg = env.getFile(arguments.getOrElse(0) { "./" })
val sep = System.lineSeparator()

if (!(arg.isFile || arg.isDirectory)) {
throw FileNotFoundException("Not a file or directory")
}

if (arg.isDirectory) {
arg.listFiles().sorted().forEach {
if (!it.isHidden) {
output.write(it.name + sep)
}
}

return
}

output.write(arg.name + sep)
}
}
5 changes: 3 additions & 2 deletions 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 Expand Up @@ -48,7 +49,7 @@ class OuterCommand(private val name: String, private val arguments: ArrayList<St
private fun createProcess(): Process {
val environmentStart = if (isWindows) "cmd.exe /c" else "sh -c"
val command = StringJoiner(" ", "$name ", "").also { joiner -> arguments.forEach { joiner.add(it) } }.toString()
return Runtime.getRuntime().exec("$environmentStart $command")
return Runtime.getRuntime().exec("$environmentStart $command", null, env.getFile("./"))
}

private class StreamGobbler(private val inputStream: InputStream, private val consumer: (String) -> Unit) : Runnable {
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.getPathString("./") + "\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.getPathString(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