From patchwork Thu Jan 23 11:42:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 11347431 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 68FB81580 for ; Thu, 23 Jan 2020 11:43:41 +0000 (UTC) Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4F94E2253D for ; Thu, 23 Jan 2020 11:43:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4F94E2253D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iuasg-00036m-Ik; Thu, 23 Jan 2020 11:42:38 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1iuasf-00036h-8a for xen-devel@lists.xenproject.org; Thu, 23 Jan 2020 11:42:37 +0000 X-Inumbo-ID: 6e83137e-3dd5-11ea-9fd7-bc764e2007e4 Received: from mx2.suse.de (unknown [195.135.220.15]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 6e83137e-3dd5-11ea-9fd7-bc764e2007e4; Thu, 23 Jan 2020 11:42:28 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 6D5D8B28B; Thu, 23 Jan 2020 11:42:27 +0000 (UTC) From: Jan Beulich To: "xen-devel@lists.xenproject.org" Message-ID: <63d1bdfc-9000-7471-f4f2-7c7f2e931bfe@suse.com> Date: Thu, 23 Jan 2020 12:42:36 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 Content-Language: en-US Subject: [Xen-devel] [PATCH v2] cmdline: treat hyphens and underscores the same X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Julien Grall , Wei Liu , Konrad Wilk , George Dunlap , Andrew Cooper , Ian Jackson Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" In order to avoid permanently having to ask that no new command line options using underscores be introduced (albeit I'm likely to still make remarks), and in order to also allow extending the use of hyphens to pre-existing ones, introduce custom comparison functions treating both characters as matching. Signed-off-by: Jan Beulich --- v2: Rename to opt_str{,n}cmp(). Don't use the new function for comapring against "no-" in parse_params(). Add comment to cdiff(). --- a/docs/misc/xen-command-line.pandoc +++ b/docs/misc/xen-command-line.pandoc @@ -72,6 +72,11 @@ Some options take a comma separated list Some parameters act as combinations of the above, most commonly a mix of Boolean and String. These are noted in the relevant sections. +### Spelling + +Parameter names may include hyphens or underscores. These are +generally being treated as matching one another by the parsing logic. + ## Parameter details ### acpi --- a/xen/common/kernel.c +++ b/xen/common/kernel.c @@ -23,6 +23,53 @@ enum system_state system_state = SYS_STA xen_commandline_t saved_cmdline; static const char __initconst opt_builtin_cmdline[] = CONFIG_CMDLINE; +/* + * Calculate the difference between two characters for command line parsing + * purposes, i.e. treating '-' and '_' the same. + */ +static int cdiff(unsigned char c1, unsigned char c2) +{ + int res = c1 - c2; + + if ( res && (c1 ^ c2) == ('-' ^ '_') && + (c1 == '-' || c1 == '_') ) + res = 0; + + return res; +} + +/* + * String comparison functions mostly matching strcmp() / strncmp(), + * except that they treat '-' and '_' as matching one another. + */ +static int opt_strcmp(const char *s1, const char *s2) +{ + int res; + + for ( ; ; ++s1, ++s2 ) + { + res = cdiff(*s1, *s2); + if ( res || !*s1 ) + break; + } + + return res; +} + +static int opt_strncmp(const char *s1, const char *s2, size_t n) +{ + int res = 0; + + for ( ; n--; ++s1, ++s2 ) + { + res = cdiff(*s1, *s2); + if ( res || !*s1 ) + break; + } + + return res; +} + static int assign_integer_param(const struct kernel_param *param, uint64_t val) { switch ( param->len ) @@ -94,7 +141,7 @@ static int parse_params(const char *cmdl /* Boolean parameters can be inverted with 'no-' prefix. */ key = optkey; - bool_assert = !!strncmp("no-", optkey, 3); + bool_assert = !!opt_strncmp("no-", optkey, 3); if ( !bool_assert ) optkey += 3; @@ -105,11 +152,11 @@ static int parse_params(const char *cmdl int rctmp; const char *s; - if ( strcmp(param->name, optkey) ) + if ( opt_strcmp(param->name, optkey) ) { if ( param->type == OPT_CUSTOM && q && strlen(param->name) == q + 1 - opt && - !strncmp(param->name, opt, q + 1 - opt) ) + !opt_strncmp(param->name, opt, q + 1 - opt) ) { found = true; optval[-1] = '='; @@ -284,7 +331,7 @@ int parse_boolean(const char *name, cons nlen = strlen(name); /* Does s now start with name? */ - if ( slen < nlen || strncmp(s, name, nlen) ) + if ( slen < nlen || opt_strncmp(s, name, nlen) ) return -1; /* Exact, unadorned name? Result depends on the 'no-' prefix. */ @@ -304,7 +351,7 @@ int cmdline_strcmp(const char *frag, con for ( ; ; frag++, name++ ) { unsigned char f = *frag, n = *name; - int res = f - n; + int res = cdiff(f, n); if ( res || n == '\0' ) {