mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Robert Abel <rabel@robertabel.eu>
To: linux-kernel@vger.kernel.org
Cc: Robert Abel <rabel@robertabel.eu>,
	miguel.ojeda.sandonis@gmail.com, w@1wt.eu, geert@linux-m68k.org,
	andy.shevchenko@gmail.com
Subject: [PATCH 1/2] auxdisplay: charlcd: fix x/y address commands
Date: Wed, 28 Feb 2018 01:05:35 +0100	[thread overview]
Message-ID: <20180228000536.9489-2-rabel@robertabel.eu> (raw)
In-Reply-To: <20180228000536.9489-1-rabel@robertabel.eu>

The current version does not parse x/y commands at all.
Simplify the x/y command syntax to the one indicated in
the comment all along and introduce a parsing function
that handles parsing a sequence of one or two subcommands
where each subcommand must appear at most once.

Signed-off-by: Robert Abel <rabel@robertabel.eu>
---
 drivers/auxdisplay/charlcd.c | 109 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 96 insertions(+), 13 deletions(-)

diff --git a/drivers/auxdisplay/charlcd.c b/drivers/auxdisplay/charlcd.c
index e3b2fd15c5a3..ae078f414539 100644
--- a/drivers/auxdisplay/charlcd.c
+++ b/drivers/auxdisplay/charlcd.c
@@ -292,6 +292,96 @@ static int charlcd_init_display(struct charlcd *lcd)
 	return 0;
 }
 
+/**
+ * parse_xy() - Parse coordinates of a movement command.
+ * @s: Pointer to null-terminated movement command string.
+ * @x: Pointer to x position.
+ * @y: Pointer to y position.
+ *
+ * Parses a movement command of the form "([xy][0-9]+){1,2};",
+ * where each group must begin with a different subcommand.
+ *
+ * The positions will only be updated when their respective
+ * subcommand is encountered and when the whole movement
+ * command is valid.
+ *
+ * The movement command string must contain a ';' at the end.
+ *
+ * For instance:
+ *   - ";"          fails.
+ *   - "x1;"        returns (1, <original y>).
+ *   - "y2x1;"      returns (1, 2).
+ *   - "x12y34x56;" fails.
+ *   - ""           fails.
+ *   - "x"          illegal input.
+ *   - "x;"         fails.
+ *   - "x1"         illegal input.
+ *   - "xy12;"      fails.
+ *   - "x12yy12;"   fails.
+ *   - "xx"         illegal input.
+ *
+ * Return: Returns whether the command is valid. The position arguments are
+ * only written if the parsing was successful.
+ */
+static bool parse_xy(const char *s, unsigned long *x, unsigned long *y)
+{
+
+	unsigned long new_x = *x;
+	unsigned long new_y = *y;
+
+	char xcoord[LCD_ESCAPE_LEN];
+	char ycoord[LCD_ESCAPE_LEN];
+	const char *split = strpbrk(s + 1, "xy");
+	const char *end = strchr(s, ';');
+	char *coord0 = xcoord;
+	char *coord1 = ycoord;
+	unsigned long *new0 = &new_x;
+	unsigned long *new1 = &new_y;
+
+	memset(xcoord, 0, sizeof(xcoord));
+	memset(ycoord, 0, sizeof(ycoord));
+
+	/* validate input */
+	switch (*s) {
+	case 'x':
+		if (split != NULL && *split != 'y')
+			return false;
+		break;
+	case 'y':
+		/* swap coordinates */
+		coord0 = ycoord;
+		coord1 = xcoord;
+		new0 = &new_y;
+		new1 = &new_x;
+
+		if (split != NULL && *split != 'x')
+			return false;
+		break;
+	default:
+		return false;
+	}
+
+	/* parse coordinate 0 and 1 */
+	if (split == NULL) {
+		memcpy(coord0, s + 1, end - s - 1);
+		if (kstrtoul(coord0, 10, new0) < 0)
+			return false;
+	} else {
+		memcpy(coord0, s + 1, split - s - 1);
+		memcpy(coord1, split + 1, end - split - 1);
+		if (kstrtoul(coord0, 10, new0) < 0)
+			return false;
+		if (kstrtoul(coord1, 10, new1) < 0)
+			return false;
+	}
+
+	/* update coordinates on success */
+	*x = new_x;
+	*y = new_y;
+	return true;
+
+}
+
 /*
  * These are the file operation function for user access to /dev/lcd
  * This function can also be called from inside the kernel, by
@@ -473,21 +563,14 @@ static inline int handle_lcd_special_code(struct charlcd *lcd)
 		if (!strchr(esc, ';'))
 			break;
 
-		while (*esc) {
-			if (*esc == 'x') {
-				esc++;
-				if (kstrtoul(esc, 10, &priv->addr.x) < 0)
-					break;
-			} else if (*esc == 'y') {
-				esc++;
-				if (kstrtoul(esc, 10, &priv->addr.y) < 0)
-					break;
-			} else {
-				break;
-			}
+		/* If the command is valid, move to the new address */
+		if (parse_xy(esc, &priv->addr.x, &priv->addr.y)) {
+			priv->addr.x = min_t(unsigned long, priv->addr.x, lcd->bwidth - 1);
+			priv->addr.y %= lcd->height;
+			charlcd_gotoxy(lcd);
 		}
 
-		charlcd_gotoxy(lcd);
+		/* Regardless of its validity, mark as processed */
 		processed = 1;
 		break;
 	}
-- 
2.11.0

  reply	other threads:[~2018-02-28  0:06 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-09 23:50 [PATCH 0/3] auxdisplay: charlcd: miscellaneous patches Robert Abel
2018-02-09 23:50 ` [PATCH 1/3] auxdisplay: charlcd: fix hex literal ranges for graphics command Robert Abel
2018-02-09 23:50   ` [PATCH 2/3] auxdisplay: charlcd: use null character instead of zero literal to terminate strings Robert Abel
2018-02-09 23:50     ` [PATCH 3/3] auxdisplay: charlcd: replace octal literal with form-feed escape sequence Robert Abel
2018-02-10  9:43       ` Miguel Ojeda
2018-02-10  9:29     ` [PATCH 2/3] auxdisplay: charlcd: use null character instead of zero literal to terminate strings Miguel Ojeda
2018-02-10  8:58   ` [PATCH 1/3] auxdisplay: charlcd: fix hex literal ranges for graphics command Miguel Ojeda
2018-02-10  9:20     ` Willy Tarreau
2018-02-10  9:41       ` Miguel Ojeda
2018-02-12 13:56         ` Miguel Ojeda
2018-02-13 13:36         ` Andy Shevchenko
2018-02-13 19:15           ` Willy Tarreau
2018-02-14 23:17           ` Robert Abel
2018-02-15 10:57             ` Andy Shevchenko
2018-02-25 23:34               ` Robert Abel
2018-02-25 23:52                 ` Robert Abel
2018-02-25 23:54                 ` Robert Abel
2018-02-25 23:54                   ` [PATCH 1/4] auxdisplay: charlcd: fix two-line command ^[[LN not marked as processed Robert Abel
2018-02-25 23:54                     ` [PATCH 2/4] auxdisplay: charlcd: name x/y address struct Robert Abel
2018-02-25 23:54                       ` [PATCH 3/4] auxdisplay: charlcd: fix x/y address commands Robert Abel
2018-02-25 23:54                         ` [PATCH 4/4] auxdisplay: charlcd: make home command unshift display Robert Abel
2018-02-26 17:16                           ` Miguel Ojeda
2018-02-26  8:46                         ` [PATCH 3/4] auxdisplay: charlcd: fix x/y address commands Geert Uytterhoeven
2018-02-26 22:29                           ` Robert Abel
2018-02-26 11:44                         ` Andy Shevchenko
2018-02-26 16:54                           ` Miguel Ojeda
2018-02-26 17:09                             ` Andy Shevchenko
2018-02-26 17:26                               ` Miguel Ojeda
2018-02-26 17:56                                 ` Andy Shevchenko
2018-02-26 23:00                                 ` Robert Abel
2018-02-26 22:38                           ` Robert Abel
2018-02-26 23:06                             ` Miguel Ojeda
2018-02-26 16:49                         ` Miguel Ojeda
2018-02-26 16:57                           ` Miguel Ojeda
2018-02-26 22:43                           ` Robert Abel
2018-02-27  5:19                             ` Willy Tarreau
2018-02-27 19:31                               ` Miguel Ojeda
2018-02-27 23:29                               ` Robert Abel
2018-02-28  0:05                                 ` [PATCH RFC 0/2] auxdisplay: charlcd: fix movement and home commands Robert Abel
2018-02-28  0:05                                   ` Robert Abel [this message]
2018-02-28  0:05                                     ` [PATCH 2/2] auxdisplay: charlcd: make home command unshift display Robert Abel
2018-02-28  4:21                                 ` [PATCH 3/4] auxdisplay: charlcd: fix x/y address commands Willy Tarreau
2018-02-26 23:05                           ` Robert Abel
2018-02-27  5:20                             ` Willy Tarreau
2018-02-26  8:35                       ` [PATCH 2/4] auxdisplay: charlcd: name x/y address struct Geert Uytterhoeven
2018-02-26 15:59                       ` Miguel Ojeda
2018-02-26  8:34                     ` [PATCH 1/4] auxdisplay: charlcd: fix two-line command ^[[LN not marked as processed Geert Uytterhoeven
2018-02-26 15:53                       ` Miguel Ojeda
2018-02-26 23:08                   ` [PATCH 1/3] auxdisplay: charlcd: fix hex literal ranges for graphics command Robert Abel
2018-02-10 18:31     ` Geert Uytterhoeven
2018-02-10 18:58       ` Willy Tarreau

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=20180228000536.9489-2-rabel@robertabel.eu \
    --to=rabel@robertabel.eu \
    --cc=andy.shevchenko@gmail.com \
    --cc=geert@linux-m68k.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=miguel.ojeda.sandonis@gmail.com \
    --cc=w@1wt.eu \
    /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

all inboxes | Powered by JetHome®