Compare commits

...

3 Commits

Author SHA1 Message Date
Sekulum Forka eff52bf920 Variables: 2 dollarsignsin a row are equal to a single dollarsign now 2021-05-16 16:50:19 +02:00
Sekulum Forka 0d8354ddd9 Prompt: Added a prompt (Still buggy a little if it contains dollarsigns) 2021-05-16 16:47:09 +02:00
Sekulum Forka ef34936d29 builtins: Added cd 2021-05-16 16:18:39 +02:00
1 changed files with 25 additions and 2 deletions

View File

@ -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")