public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Corey Minyard <cminyard@mvista.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	openipmi-developer@lists.sourceforge.net,
	linux-kernel@vger.kernel.org
Cc: Corey Minyard <minyard@acm.org>
Subject: [PATCH v1 05/10] ipmi_si: Introduce panic_event_str array
Date: Tue, 30 Mar 2021 21:16:44 +0300	[thread overview]
Message-ID: <20210330181649.66496-5-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20210330181649.66496-1-andriy.shevchenko@linux.intel.com>

Instead of twice repeat the constant literals, introduce
panic_event_str array. It allows to simplify the code with
help of match_string() API.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/char/ipmi/ipmi_msghandler.c | 49 ++++++++++-------------------
 1 file changed, 17 insertions(+), 32 deletions(-)

diff --git a/drivers/char/ipmi/ipmi_msghandler.c b/drivers/char/ipmi/ipmi_msghandler.c
index f19f0f967e28..c7d37366d7bb 100644
--- a/drivers/char/ipmi/ipmi_msghandler.c
+++ b/drivers/char/ipmi/ipmi_msghandler.c
@@ -52,8 +52,12 @@ static bool drvregistered;
 enum ipmi_panic_event_op {
 	IPMI_SEND_PANIC_EVENT_NONE,
 	IPMI_SEND_PANIC_EVENT,
-	IPMI_SEND_PANIC_EVENT_STRING
+	IPMI_SEND_PANIC_EVENT_STRING,
+	IPMI_SEND_PANIC_EVENT_MAX
 };
+
+static const char *const panic_event_str[] = { "none", "event", "string", NULL };
+
 #ifdef CONFIG_IPMI_PANIC_STRING
 #define IPMI_PANIC_DEFAULT IPMI_SEND_PANIC_EVENT_STRING
 #elif defined(CONFIG_IPMI_PANIC_EVENT)
@@ -68,46 +72,27 @@ static int panic_op_write_handler(const char *val,
 				  const struct kernel_param *kp)
 {
 	char valcp[16];
-	char *s;
-
-	strncpy(valcp, val, 15);
-	valcp[15] = '\0';
+	int e;
 
-	s = strstrip(valcp);
-
-	if (strcmp(s, "none") == 0)
-		ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_NONE;
-	else if (strcmp(s, "event") == 0)
-		ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT;
-	else if (strcmp(s, "string") == 0)
-		ipmi_send_panic_event = IPMI_SEND_PANIC_EVENT_STRING;
-	else
-		return -EINVAL;
+	strscpy(valcp, val, sizeof(valcp));
+	e = match_string(panic_event_str, -1, strstrip(valcp));
+	if (e < 0)
+		return e;
 
+	ipmi_send_panic_event = e;
 	return 0;
 }
 
 static int panic_op_read_handler(char *buffer, const struct kernel_param *kp)
 {
-	switch (ipmi_send_panic_event) {
-	case IPMI_SEND_PANIC_EVENT_NONE:
-		strcpy(buffer, "none\n");
-		break;
-
-	case IPMI_SEND_PANIC_EVENT:
-		strcpy(buffer, "event\n");
-		break;
-
-	case IPMI_SEND_PANIC_EVENT_STRING:
-		strcpy(buffer, "string\n");
-		break;
+	const char *event_str;
 
-	default:
-		strcpy(buffer, "???\n");
-		break;
-	}
+	if (ipmi_send_panic_event >= IPMI_SEND_PANIC_EVENT_MAX)
+		event_str = "???";
+	else
+		event_str = panic_event_str[ipmi_send_panic_event];
 
-	return strlen(buffer);
+	return sprintf(buffer, "%s\n", event_str);
 }
 
 static const struct kernel_param_ops panic_op_ops = {
-- 
2.30.2


  parent reply	other threads:[~2021-03-30 18:17 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-30 18:16 [PATCH v1 01/10] ipmi_si: Switch to use platform_get_mem_or_io() Andy Shevchenko
2021-03-30 18:16 ` [PATCH v1 02/10] ipmi_si: Remove bogus err_free label Andy Shevchenko
2021-03-30 18:16 ` [PATCH v1 03/10] ipmi_si: Utilize temporary variable to hold device pointer Andy Shevchenko
2021-04-02 13:45   ` Corey Minyard
2021-03-30 18:16 ` [PATCH v1 04/10] ipmi_si: Use proper ACPI macros to check error code for failures Andy Shevchenko
2021-03-30 18:16 ` Andy Shevchenko [this message]
2021-04-02 13:48   ` [PATCH v1 05/10] ipmi_si: Introduce panic_event_str array Corey Minyard
2021-03-30 18:16 ` [PATCH v1 06/10] ipmi_si: Reuse si_to_str array in ipmi_hardcode_init_one() Andy Shevchenko
2021-04-02 13:53   ` Corey Minyard
2021-03-30 18:16 ` [PATCH v1 07/10] ipmi_si: Get rid of ->addr_source_cleanup() Andy Shevchenko
2021-03-30 18:16 ` [PATCH v1 08/10] ipmi_si: Use strstrip() to remove surrounding spaces Andy Shevchenko
2021-03-30 18:16 ` [PATCH v1 09/10] ipmi_si: Drop redundant check before calling put_device() Andy Shevchenko
2021-03-30 18:16 ` [PATCH v1 10/10] ipmi_si: Join string literals back Andy Shevchenko
2021-04-02 13:57 ` [PATCH v1 01/10] ipmi_si: Switch to use platform_get_mem_or_io() Corey Minyard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210330181649.66496-5-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=cminyard@mvista.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=minyard@acm.org \
    --cc=openipmi-developer@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox