Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp1146639imm; Tue, 5 Jun 2018 09:45:56 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKY1MeCKvqv3sFUeAZie5gzpGRpZbW415HulsiBap3rLHjZ8yWrfBvoGCjYZ54AFkosP4z+ X-Received: by 2002:a63:b008:: with SMTP id h8-v6mr20237390pgf.137.1528217156614; Tue, 05 Jun 2018 09:45:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528217156; cv=none; d=google.com; s=arc-20160816; b=j7j5V4fiixOIrdjxL1W+PDq1wkvJOlZiQiFcW+ql7x30qR4SMt0shUQgNvcLde3HVQ IUz/DktOobTbJ+RFYqv3zglaIhr3AOaZw4HUjTgCVLJA0X6pg2RTP1i4Cg0HSXBb3PTa sq7uK5LxwQ9l0rsvBKvGed1OSqDMlhKN+V2Jb7gawjc3gvg7cDoLcESLyDKdxYv+qfgV x7d/MOjRwxS+sWrkg+EIPrxpCZwrHceWtKXbxTivOK4ezh82OUrVSeaAwArOnkgR43By oZlei07VwZowLJiwFI4ywnXMqh0roaEYSLbEw3PGU/NNufkNc7/ZU0h3FCNtb4yxL20S wWTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:to:from :arc-authentication-results; bh=oNBaYinkE+tlQhMiDJRBM3MqXJJrtgPaCnVCvZ+MHrQ=; b=wMyaJm3O1+l4UfwTus70/P+oQx9cSAkmFV8UW7JKF1jD+h77sljuCMtjzNWyT+OxwB JLKCDbrP6i7Klo1wS/DUmUltjFuhOlG6mqmqyWoxGAzOhkj7C0JFeCiAzkX7H6f0aN3C rYfOJZEesjgYIOZbc6IdSkc+COxNKMYWwEhrULHUsI5ks7jgHOIa++FGEmmIBBav6CnL vlJWE4MwM5xWknLbCxVDXk9JbGNWVp4rATTR+44waHyDoC5KrlFFCflN+ddTnrAcaJz5 pxefMrXhgeT3VuFQidFu9ARrP/AD5+RO5W6RddI7zUGB4E16zRL+zQZ929Pe5wN+3q+V dZwQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d1-v6si17231908pln.471.2018.06.05.09.45.42; Tue, 05 Jun 2018 09:45:56 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752302AbeFEQor (ORCPT + 99 others); Tue, 5 Jun 2018 12:44:47 -0400 Received: from mx2.suse.de ([195.135.220.15]:46880 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752025AbeFEQnt (ORCPT ); Tue, 5 Jun 2018 12:43:49 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" Received: from relay2.suse.de (charybdis-ext-too.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 5BC70ADA1; Tue, 5 Jun 2018 16:43:47 +0000 (UTC) From: Michal Suchanek To: Jonathan Corbet , Michal Suchanek , Arnd Bergmann , Frederic Weisbecker , Ingo Molnar , Aaron Wu , Tony Luck , Andrew Morton , Thomas Gleixner , "Steven Rostedt," , Laura Abbott , Dominik Brodowski , Alexey Dobriyan , Tom Lendacky , Jeffrey Hugo , Baoquan He , Ilya Matveychikov , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v10 4/5] lib/cmdline.c: Implement single quotes in commandline argument parsing. Date: Tue, 5 Jun 2018 18:43:11 +0200 Message-Id: X-Mailer: git-send-email 2.13.6 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This brings the kernel parser about on par with bourne shell, grub, and other tools that chew the arguments before kernel does. This should make it easier to deal with multiple levels of nesting/quoting. With same quoting grammar on each level there is less room for confusion. Signed-off-by: Michal Suchanek --- - optimize end quote removal --- lib/cmdline.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/cmdline.c b/lib/cmdline.c index aa0086dbb082..e699ed5aac8a 100644 --- a/lib/cmdline.c +++ b/lib/cmdline.c @@ -192,19 +192,26 @@ bool parse_option_str(const char *str, const char *option) } #define break_arg_end(i) { \ - if (isspace(args[i]) && !in_quote && !backslash) \ + if (isspace(args[i]) && !in_quote && !backslash && !in_single) \ break; \ } +#define squash_char { \ + break_arg_end(i + 1); \ + memmove(args + 1, args, i); \ + args++; \ + i--; \ +} + /* * Parse a string to get a param value pair. - * You can use " around spaces, and you can escape with \ + * You can use " or ' around spaces, and you can escape with \ * Hyphens and underscores equivalent in parameter names. */ char *next_arg(char *args, char **param, char **val, int *state_flags) { unsigned int i, equals = 0; - int in_quote = 0, backslash = 0; + int in_quote = 0, backslash = 0, in_single = 0; char *next; for (i = 0; args[i]; i++) { @@ -213,17 +220,23 @@ char *next_arg(char *args, char **param, char **val, int *state_flags) if ((equals == 0) && (args[i] == '=')) equals = i; - if (!backslash) { - if ((args[i] == '"') || (args[i] == '\\')) { + if (in_single) { + if (args[i] == '\'') { + in_single = 0; + + squash_char; + } + } else if (!backslash) { + if ((args[i] == '"') || (args[i] == '\\') || + (args[i] == '\'')) { if (args[i] == '"') in_quote = !in_quote; if (args[i] == '\\') backslash = 1; + if (args[i] == '\'') + in_single = 1; - break_arg_end(i + 1); - memmove(args + 1, args, i); - args++; - i--; + squash_char; } } else { backslash = 0; -- 2.13.6