Compare commits
3 Commits
472ecf7b25
...
eff52bf920
Author | SHA1 | Date |
---|---|---|
Sekulum Forka | eff52bf920 | |
Sekulum Forka | 0d8354ddd9 | |
Sekulum Forka | ef34936d29 |
27
src/fsh.nim
27
src/fsh.nim
|
@ -18,12 +18,13 @@ for k, v in envPairs():
|
|||
envVariables[k]=v
|
||||
|
||||
# list of builtin commands
|
||||
let builtins: array[5, string] = [
|
||||
let builtins: array[6, string] = [
|
||||
"set",
|
||||
"setenv",
|
||||
"echo",
|
||||
"exit",
|
||||
"if",
|
||||
"cd",
|
||||
]
|
||||
|
||||
# Exception types
|
||||
|
@ -178,6 +179,7 @@ proc readVariable(strm: Stream): string =
|
|||
result.add(strm.readSubstitution())
|
||||
of ']', '}':
|
||||
stderr.write("Unexpected " & c & ".\n")
|
||||
of '$': result = "$"
|
||||
else:
|
||||
result.add(c)
|
||||
while not strm.atEnd():
|
||||
|
@ -188,6 +190,7 @@ proc readVariable(strm: Stream): string =
|
|||
|
||||
# evalVariable finds a value of a variable
|
||||
proc evalVariable(vari: string): string =
|
||||
if vari == "$": return "$"
|
||||
if shellVariables.hasKey(vari):
|
||||
result=shellVariables[vari]
|
||||
elif envVariables.hasKey(vari):
|
||||
|
@ -304,7 +307,23 @@ proc runBuiltin(builtin: string, args: openArray[string]): (string, int) =
|
|||
i.inc
|
||||
i.inc
|
||||
continue
|
||||
|
||||
of "cd":
|
||||
try:
|
||||
if args.len == 0:
|
||||
shellVariables["LASTPWD"] = getCurrentDir()
|
||||
setCurrentDir(getHomeDir())
|
||||
return ("", 0)
|
||||
else:
|
||||
if args[0] == "-":
|
||||
let LASTPWD = getCurrentDir()
|
||||
setCurrentDir(shellVariables["LASTPWD"])
|
||||
shellVariables["LASTPWD"] = LASTPWD
|
||||
return ("", 0)
|
||||
shellVariables["LASTPWD"] = getCurrentDir()
|
||||
setCurrentDir(args[0])
|
||||
return ("", 0)
|
||||
except OsError as e:
|
||||
raise newShellCommandException("No such directory", "cd")
|
||||
else:
|
||||
raise newShellCommandException("No such builtin implemented", builtin)
|
||||
|
||||
|
@ -354,9 +373,13 @@ when isMainModule:
|
|||
# ctrlc handler
|
||||
setControlCHook(ctrlc)
|
||||
let stdinstrm = stdin.newFileStream
|
||||
# Set the default prompt
|
||||
shellVariables["PROMPT"] = "$PWD \\$"
|
||||
stdout.write(shellVariables["PROMPT"].newStringStream.substitute)
|
||||
while not stdinstrm.atEnd():
|
||||
try:
|
||||
discard stdinstrm.readCommand.execute
|
||||
stdout.write(shellVariables["PROMPT"].newStringStream.substitute)
|
||||
except ShellError, ShellCommandError, ShellVariableError:
|
||||
let e = getCurrentException()
|
||||
stderr.write(e.msg, "\n")
|
||||
|
|
Loading…
Reference in New Issue