mirror of
https://github.com/arcan1s/ahriman.git
synced 2026-12-01 02:59:50 +00:00
fix: separate quoted and unquoted string parsing
there is anoter error caused by inner shell expression, which fails shlex parsing
This commit is contained in:
1 parent
1fcb4cdb84
commit
e1565398f4
3 files changed
+25
-4
No files matched your search
@@ -164,23 +164,37 @@ class PkgbuildParser(Iterable[PkgbuildPatch]):
|
||||
Returns:
|
||||
str: extracted string value
|
||||
"""
|
||||
def parts(source: str) -> Iterator[str]:
|
||||
for child in node.named_children:
|
||||
# retain gaps (such as newlines) between children
|
||||
prefix, _, source = source.partition(PkgbuildParser.text(child))
|
||||
yield prefix
|
||||
yield PkgbuildParser.value(child)
|
||||
yield source
|
||||
|
||||
def unescape(source: str) -> str:
|
||||
for original, replacement in PkgbuildParser._ESCAPED_CHARACTERS.items():
|
||||
source = source.replace(original, replacement)
|
||||
return source
|
||||
|
||||
# raw processing to avoid converting to text if not required
|
||||
if node.type == PkgbuildNodeType.CONCATENATION:
|
||||
return "".join(PkgbuildParser.value(child) for child in node.named_children)
|
||||
|
||||
value = PkgbuildParser.text(node)
|
||||
match node.type:
|
||||
case PkgbuildNodeType.STRING | PkgbuildNodeType.STRING_CONTENT:
|
||||
if node.type == PkgbuildNodeType.STRING:
|
||||
value = PkgbuildParser.unquote(value)
|
||||
# protect escaped backslashes from being interpreted a second time.
|
||||
case PkgbuildNodeType.STRING:
|
||||
# this node is always quoted, so that we manually remove first and last characters
|
||||
return "".join(parts(value[1:-1]))
|
||||
|
||||
case PkgbuildNodeType.STRING_CONTENT:
|
||||
# unescape literal text only, substitutions are returned as written
|
||||
value = PkgbuildParser.unquote(f'"{value}"')
|
||||
return "\\".join(unescape(part) for part in value.split("\\\\"))
|
||||
|
||||
case PkgbuildNodeType.NUMBER | PkgbuildNodeType.RAW_STRING | PkgbuildNodeType.WORD:
|
||||
return PkgbuildParser.unquote(value)
|
||||
|
||||
case _:
|
||||
return value
|
||||
|
||||
|
||||
@@ -290,4 +290,8 @@ def test_parse(resource_path_root: Path) -> None:
|
||||
PkgbuildPatch("var", "a\\qb"),
|
||||
PkgbuildPatch("var", "\\$name"),
|
||||
PkgbuildPatch("var", "first\nsecond"),
|
||||
PkgbuildPatch(
|
||||
"_source64",
|
||||
"""https:$(echo "$_dl_urls" | sed -En 's|^.*rel="(//.*/linuxx64-[^"]*)".*$|\\1|p')""",
|
||||
),
|
||||
]
|
||||
@@ -125,3 +125,6 @@ var="a\qb"
|
||||
var="\\\\$name"
|
||||
var="first
|
||||
second"
|
||||
|
||||
# nested quotes inside command substitutions
|
||||
_source64=https:"$(echo "$_dl_urls" | sed -En 's|^.*rel="(//.*/linuxx64-[^"]*)".*$|\1|p')"
|
||||
Reference in new issue
Block a user