Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return result
class SubstCommandVisitor(bashlex.ast.nodevisitor):
"""Uses bashlex to parse and process sh/bash substitution commands.
May result in a parsing exception for invalid commands."""
def __init__(self):
self.substs = []
def visitcommandsubstitution(self, n, cmd):
self.substs.append(n)
return False
class CommandProcessor(bashlex.ast.nodevisitor):
"""Uses bashlex to parse and traverse the resulting bash AST
looking for and extracting compilation commands."""
@staticmethod
def process(line, wd):
trees = bashlex.parser.parse(line)
if not trees:
return []
for tree in trees:
svisitor = SubstCommandVisitor()
svisitor.visit(tree)
substs = svisitor.substs
substs.reverse()
preprocessed = list(line)
for s in substs:
start, end = s.command.pos
s_cmd = line[start:end]
def _paramexpand(parserobj, string, sindex):
node = None
zindex = sindex + 1
c = string[zindex] if zindex < len(string) else None
if c and c in '0123456789$#?-!*@':
# XXX 7685
node = ast.node(kind='parameter', value=c,
pos=(sindex, zindex+1))
elif c == '{':
# XXX 7863
# TODO not start enough, doesn't consider escaping
zindex = string.find('}', zindex + 1)
node = ast.node(kind='parameter', value=string[sindex+2:zindex],
pos=(sindex, zindex+1))
# TODO
# return _parameterbraceexpand(string, zindex)
elif c == '(':
return _extractcommandsubst(parserobj, string, zindex + 1)
elif c == '[':
raise NotImplementedError('arithmetic substitution')
#return _extractarithmeticsubst(string, zindex + 1)
else:
tindex = zindex
for zindex in range(tindex, len(string) + 1):
if zindex == len(string):
break
if not string[zindex].isalnum() and not string[zindex] == '_':
break
temp1 = string[sindex:zindex]
| FOR WORD newline_list IN word_list list_terminator newline_list DO compound_list DONE
| FOR WORD newline_list IN word_list list_terminator newline_list LEFT_CURLY compound_list RIGHT_CURLY
| FOR WORD newline_list IN list_terminator newline_list DO compound_list DONE
| FOR WORD newline_list IN list_terminator newline_list LEFT_CURLY compound_list RIGHT_CURLY'''
parts = _makeparts(p)
# find the operatornode that we might have there due to
# list_terminator/newline_list and convert it to a reservedword so its
# considered as part of the for loop
for i, part in enumerate(parts):
if part.kind == 'operator' and part.op == ';':
parts[i] = ast.node(kind='reservedword', word=';', pos=part.pos)
break # there could be only one in there...
p[0] = ast.node(kind='compound',
redirects=[],
list=[ast.node(kind='for', parts=parts, pos=_partsspan(parts))],
pos=_partsspan(parts))
def _extractcommandsubst(parserobj, string, sindex, sxcommand=False):
if string[sindex] == '(':
raise NotImplementedError('arithmetic expansion')
#return _extractdelimitedstring(parserobj, string, sindex, '$(', '(', '(', sxcommand=True)
else:
node, si = _parsedolparen(parserobj, string, sindex)
si += 1
return ast.node(kind='commandsubstitution', command=node, pos=(sindex-2, si)), si