├── .gitignore ├── LICENSE ├── README.rst ├── doc └── demo.gif ├── nimsh.nimble └── src └── nimsh.nim /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | bin/nimsh -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 jiro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ===== 2 | nimsh 3 | ===== 4 | 5 | nimsh is a simple implementation of a shell in pure Nim. 6 | nimsh is inspired by `lsh `_. 7 | 8 | |image-demo| 9 | 10 | .. contents:: Table of contents 11 | :depth: 3 12 | 13 | Installation 14 | ============ 15 | 16 | .. code-block:: shell 17 | 18 | nimble install -Y https://github.com/jiro4989/nimsh 19 | 20 | or 21 | 22 | Download binary from `Release `_. 23 | 24 | Usage 25 | ===== 26 | 27 | .. code-block:: shell 28 | 29 | nimble build 30 | ./nimsh 31 | 32 | LICENSE 33 | ======= 34 | 35 | MIT 36 | 37 | .. |image-demo| image:: ./doc/demo.gif 38 | -------------------------------------------------------------------------------- /doc/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiro4989/nimsh/d5976a66ffbced8b4b12c5dff1dca07a8485246c/doc/demo.gif -------------------------------------------------------------------------------- /nimsh.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.0" 4 | author = "jiro4989" 5 | description = "A tiny shell implementation in pure Nim." 6 | license = "MIT" 7 | srcDir = "src" 8 | bin = @["nimsh"] 9 | binDir = "bin" 10 | 11 | 12 | # Dependencies 13 | 14 | requires "nim >= 1.0.6" 15 | -------------------------------------------------------------------------------- /src/nimsh.nim: -------------------------------------------------------------------------------- 1 | from strutils import split 2 | import os, tables, posix 3 | 4 | const 5 | statusOk = 1 6 | statusNg = 0 7 | 8 | proc nimshCd(args: seq[string]): int = 9 | if args.len < 2: 10 | stderr.writeLine("nimsh: expected argument to 'cd'") 11 | else: 12 | setCurrentDir(args[1]) 13 | return statusOk 14 | 15 | proc nimshExit(args: seq[string]): int = 16 | return statusNg 17 | 18 | const 19 | builtinCommands = { 20 | "cd": nimshCd, 21 | "exit": nimshExit 22 | }.toTable 23 | 24 | proc nimshLaunch(args: seq[string]): int = 25 | let pid = fork() 26 | if pid == 0: 27 | if execvp(args[0].cstring, allocCStringArray(args)) == -1: 28 | stderr.writeLine "nimsh" 29 | elif pid < 0: 30 | stderr.writeLine "nimsh" 31 | else: 32 | var status: cint 33 | while (not WIFEXITED(status)) and (not WIFSIGNALED(status)): 34 | discard waitpid(pid, status, WUNTRACED) 35 | return statusOk 36 | 37 | proc nimshExec(args: seq[string]): int = 38 | if args.len < 1: return statusOk 39 | for cmdName, cmd in builtinCommands: 40 | if args[0] == cmdName: 41 | return cmd(args) 42 | return nimshLaunch(args) 43 | 44 | proc nimshLoop = 45 | const 46 | tokenDelimiter = {'\t', '\r', '\n', '\a', ' '} 47 | var 48 | line: string 49 | args: seq[string] 50 | status = statusOk 51 | while status != statusNg: 52 | stdout.write("nimsh > ") 53 | if not stdin.readLine(line): 54 | break 55 | args = line.split(tokenDelimiter) 56 | status = nimshExec(args) 57 | sleep 100 58 | 59 | proc main: int = 60 | nimshLoop() 61 | 62 | when isMainModule: 63 | quit main() 64 | --------------------------------------------------------------------------------