Compare commits
3 Commits
bdb77b272a
...
d502efaeb1
Author | SHA1 | Date |
---|---|---|
Sekulum Forka | d502efaeb1 | |
Sekulum Forka | 8a5e6c3cea | |
Sekulum Forka | c00678603b |
42
src/fsh.nim
42
src/fsh.nim
|
@ -4,11 +4,21 @@
|
|||
|
||||
# imports
|
||||
import streams
|
||||
import strutils
|
||||
import osproc
|
||||
import strtabs
|
||||
import os
|
||||
|
||||
# globals
|
||||
var shellVariables = newStringTable(modeCaseSensitive)
|
||||
# Environment variables get defined in a separate var
|
||||
var envVariables = newStringTable(modeCaseSensitive)
|
||||
# populate the table
|
||||
for k, v in envPairs():
|
||||
envVariables[k]=v
|
||||
|
||||
# forward declarations
|
||||
proc eval(cmd: string): string
|
||||
proc substitute(strm: Stream, delim = ";\p"): string
|
||||
|
||||
# skipSpaces reads the stream until a non-space character is found
|
||||
proc skipSpaces(strm: Stream): int =
|
||||
|
@ -104,6 +114,34 @@ proc readCommand(strm: Stream): string =
|
|||
of '\n', '\r', ';': break
|
||||
else: result.add(c)
|
||||
|
||||
# readVariable reads a variable
|
||||
proc readVariable(strm: Stream): string =
|
||||
let c = strm.readChar()
|
||||
case c:
|
||||
of '{':
|
||||
result = strm.readLiteral()
|
||||
of '[':
|
||||
result.add(strm.readInterpelation)
|
||||
of '"':
|
||||
result.add(strm.readSubstitution())
|
||||
of ']', '}':
|
||||
stderr.write("Unexpected " & c & ".\n")
|
||||
else:
|
||||
result.add(c)
|
||||
while not strm.atEnd():
|
||||
let d = strm.readChar()
|
||||
case d:
|
||||
of 'a'..'z', 'A'..'Z', '0'..'9', '_', ':': result.add(d)
|
||||
else: break
|
||||
|
||||
# evalVariable finds a value of a variable
|
||||
proc evalVariable(vari: string): string =
|
||||
if shellVariables.hasKey(vari):
|
||||
result=shellVariables[vari]
|
||||
elif envVariables.hasKey(vari):
|
||||
result = envVariables[vari]
|
||||
else: raise newException(Exception, "No such variable: " & vari & ".")
|
||||
|
||||
# substitute does a substitution on the stream passed in
|
||||
proc substitute(strm: Stream, delim=";\p"): string =
|
||||
while not strm.atEnd():
|
||||
|
@ -120,6 +158,7 @@ proc substitute(strm: Stream, delim=";\p"): string =
|
|||
stderr.write("Extra closing bracket")
|
||||
of '\\':
|
||||
result.add(strm.readChar())
|
||||
of '$': result.add(strm.readVariable.evalVariable)
|
||||
else:
|
||||
result.add(c)
|
||||
|
||||
|
@ -139,6 +178,7 @@ proc parseCommand(cmd: string): seq[string] =
|
|||
newitem.add(strm.readInterpelation.eval)
|
||||
of '"':
|
||||
newitem.add(strm.readSubstitution.newStringStream.substitute)
|
||||
of '$': result.add(strm.readVariable.evalVariable)
|
||||
of ' ':
|
||||
result.add(newitem)
|
||||
newitem = ""
|
||||
|
|
Loading…
Reference in New Issue