Compare commits

...

7 Commits

1 changed files with 53 additions and 11 deletions

View File

@ -25,6 +25,41 @@ let builtins: array[4, string] = [
"exit",
]
# Exception types
type
ShellError= object of CatchableError
line: int
col: int
whileExec: string
ShellVariableError= object of ShellError
variable: string
ShellCommandError = object of ShellError
command: string
# procs for making procedures
proc newShellException(msg: string, line=0, col=0, whileExec=""): ref ShellError =
var e = newException(ShellError, msg)
e.line=line
e.col=col
e.whileExec = whileExec
return e
proc newShellVariableException(msg: string, vari: string, line=0, col=0, whileExec=""): ref ShellVariableError =
var e= newException(ShellVariableError, vari & ": " & msg)
e.variable =vari
e.line=line
e.col=col
e.whileExec=whileExec
return e
proc newShellCommandException(msg: string, command: string, line=0, col=0, whileExec=""): ref ShellCommandError =
var e=newException(ShellCommandError, command & ": " & msg)
e.command=command
e.line=line
e.col=col
e.whileExec=whileExec
return e
# forward declarations
proc eval(cmd: string): string
proc substitute(strm: Stream, delim = ";\p"): string
@ -150,7 +185,8 @@ proc evalVariable(vari: string): string =
result=shellVariables[vari]
elif envVariables.hasKey(vari):
result = envVariables[vari]
else: raise newException(Exception, "No such variable: " & vari & ".")
else:
raise newShellVariableException("No such variable", vari)
# substitute does a substitution on the stream passed in
proc substitute(strm: Stream, delim=";\p"): string =
@ -205,17 +241,17 @@ proc runBuiltin(builtin: string, args: openArray[string]): string =
case builtin:
of "set":
if args.len < 2:
raise newException(Exception, "Set: not enough arguments provided")
raise newShellCommandException("Not enough arguments provided", "set")
shellVariables[args[0]] = args[1..^1].join(" ")
return ""
of "setenv":
if args.len < 2:
raise newException(Exception, "setenv: Not enough arguments provided")
raise newShellCommandException("Not enough arguments provided", "setenv")
try:
envVariables[args[0]] = args[1..^1].join(":")
putEnv(args[0], args[1..^1].join(":"))
except OSError as e:
raise newException(Exception, "setenv: " & e.msg)
raise newShellCommandException(e.msg, "setenv")
return ""
of "echo":
if args[0] == "-n":
@ -241,7 +277,7 @@ proc runBuiltin(builtin: string, args: openArray[string]): string =
stdout.write(args.join(" "), "\p")
quit(0)
else:
raise newException(Exception, "Fsh: No such builtin implemented")
raise newShellCommandException("No such builtin implemented", builtin)
# executes executes the command. For now it involves catting the command and printing it
proc execute(cmd: string): int =
@ -256,9 +292,12 @@ proc execute(cmd: string): int =
stdout.write(runBuiltin(progname, args))
result = 0
continue
let ps = startProcess(progname, args=args, options={poUsePath, poParentStreams})
result = ps.waitForExit
ps.close
try:
let ps = startProcess(progname, args=args, options={poUsePath, poParentStreams})
result = ps.waitForExit
ps.close
except OSError as e:
raise newShellCommandException("No such command", progname)
strm.close
# eval evaluates the given string
@ -273,9 +312,12 @@ proc eval(cmd: string): string =
if progname in builtins:
result = runBuiltin(progname, args)
continue
let ps = startProcess(progname, args=args, options={poUsePath})
result=ps.outputStream.readAll
ps.close
try:
let ps = startProcess(progname, args=args, options={poUsePath})
result=ps.outputStream.readAll
ps.close
except OSError as e:
raise newShellCommandException("No such command", progname)
strm.close
when isMainModule: