From 0214e9b905808cf73870f66782dee65b517642e3 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Thu, 21 Sep 2023 02:35:35 +0330 Subject: [PATCH] Shell: Rewrite 'FOR NAME do ... done' according to Dr.POSIX Dr.POSIX says this form of the loop is supposed to iterate over exactly `"$@"`, this commit makes us support that. --- Userland/Shell/PosixParser.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Userland/Shell/PosixParser.cpp b/Userland/Shell/PosixParser.cpp index dcdc1cfe32..8db4d3ce48 100644 --- a/Userland/Shell/PosixParser.cpp +++ b/Userland/Shell/PosixParser.cpp @@ -1372,7 +1372,7 @@ ErrorOr> Parser::parse_term() ErrorOr> Parser::parse_for_clause() { - // FOR NAME newline+ do_group + // FOR NAME newline+ do_group //-> FOR NAME IN "$@" newline+ do_group // FOR NAME newline+ IN separator do_group // FOR NAME IN separator do_group // FOR NAME IN wordlist separator do_group @@ -1400,16 +1400,20 @@ ErrorOr> Parser::parse_for_clause() auto saw_in = false; Optional in_kw_position; + RefPtr iterated_expression; + if (peek().type == Token::Type::In) { saw_in = true; in_kw_position = peek().position; skip(); } else if (!saw_newline) { error(peek(), "Expected 'in' or a newline, not {}", peek().type_name()); + } else { + // FOR NAME newline+ do_group //-> FOR NAME IN "$@" newline+ do_group + iterated_expression = TRY(Parser { "\"$@\""_string }.parse_word()); } - RefPtr iterated_expression; - if (!saw_newline) + if (saw_in && !saw_newline) iterated_expression = parse_word_list(); if (saw_in) {