Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp4042427ybg; Fri, 25 Oct 2019 12:29:27 -0700 (PDT) X-Google-Smtp-Source: APXvYqyrvGdmknnyk2sUH1XNmHJ/uUJvjFJxiuQukpmfxoszFDaFa8kNrlsUNo3qFhkNijMvw2kN X-Received: by 2002:a17:906:6d45:: with SMTP id a5mr5008047ejt.218.1572031767628; Fri, 25 Oct 2019 12:29:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572031767; cv=none; d=google.com; s=arc-20160816; b=pHI4nrZqyBLqsqM3DGj0fVms8AH6EOXU4Sv/8FtCUNnROSTxW5fPcYTF/cNKzbcucO wpNZU0ljlsbfHaauyUN0BNOMy4oPpsRaNPxAqF5ZGnZWzmU9Lx098ViKpXWQg3MuF8Z1 pfSU95LTqVOW6kcYd3Fmmruq7vVNaSZB5Y408yc17ltgT4dr9hhuE6MIFSfedTCwNYed 8Pv2xJ8DISOdPmbRsLcs5om3Ua9LRgvWxEdgbUU5UqOX7b62FbLcMREVrlfvdEaj/VCA 1bV9p9C1IKoE52SrxKxx4CrXdiWC6OYNqeyVN4N2sUdSVQ5wecKGaAuwUogblk8LmKgq 7WCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=K2G9g70k35Ma4xF7YfZ9Z31DwyEbpBEhJM1jJfvb0Ls=; b=w9e8IafPQdP0KViT9FjqB/j04csgA9i/cZGa9CfVcAMEThdL2eY17MevaA2W4dATnz JPGhiUJcXyoylK96HPbzzAmJhAvmnfB+BJAosl1Rj5X9jEeK62YmAh1JXoqTBx2qE8SM uNROQJWIr2Y7Dbpa3Oqvc0o6I2ggD2KPRyzYaSsEkUCh9w2MqKGlO45+66aDR2h0qq41 hR6MsXyH2UvzY7DqUU8vronj3UnZ0bTKiFnj+BlDOhc+LWUFR+dsYSv+HRjUZ271nkJQ W7Qyq8nKdVGn5cvqrzQSrZasCIyGdDaDawgGSmueiKtVRZqz9l9ZLDe4aFf27DVfPqTf 1Oug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KZPcyclU; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d15si1370356edn.49.2019.10.25.12.29.03; Fri, 25 Oct 2019 12:29:27 -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; dkim=pass header.i=@linaro.org header.s=google header.b=KZPcyclU; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2394320AbfJYHdr (ORCPT + 99 others); Fri, 25 Oct 2019 03:33:47 -0400 Received: from mail-wm1-f48.google.com ([209.85.128.48]:55687 "EHLO mail-wm1-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2394292AbfJYHdp (ORCPT ); Fri, 25 Oct 2019 03:33:45 -0400 Received: by mail-wm1-f48.google.com with SMTP id g24so910905wmh.5 for ; Fri, 25 Oct 2019 00:33:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=K2G9g70k35Ma4xF7YfZ9Z31DwyEbpBEhJM1jJfvb0Ls=; b=KZPcyclUjyY3yLuxdwofig+TMKb8hwaV/GkEcAnwSOzk46h9+ITKqyXeyxWpnIrWuX nDy7RdqY/pj3FifZQks815NKfP+KPn5CGujJofWfn2eHvm//5FawiG/ZIE0WF4Q6ZMjf sdgk8LCX96ssM3eihwYzq8pPN44a05KlVGxI2CSWb1d1qxakYWfvCZNsKuzYn4EVhTsQ mNisCKFMW1Nnn9Gw2WZpqru3/5txrIpYVrSwBF09yuJmL6nTE8ixDpPz7n2xANdjYbny 0ZRIWRinykxgRdKuuceIFRIngP4GJhTxKaVt0AU2PoIu1kKP16DK/0x/qNIJJiAooKh7 u/yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=K2G9g70k35Ma4xF7YfZ9Z31DwyEbpBEhJM1jJfvb0Ls=; b=E+K2G32bj5BHR+yovrdkx93XDZvLYwKKYetPS2LooMLvs8hcg2CnSs1HegypNTCqIM +gqqXqer51hIagOo/jFKvDTllNSorhxPB0MIMS9aoQOgg5cxUoB9P6ya+xdqOzWyWhCv nHolzgvwcoD5buIcxxplwh6GDXsrlZPQDKVJiGpNAgOk3RHK7vZ8TscdOFNtU6WbOKVS GXKhdutLI8LoyTW3AtSiXWzJKBRQQHjFAaiC4Yc2+6RiIY2/qvPmQZjcHK1s9coGbMO9 VNZdcOVmIX2lp5jGT/PfO7atJeFwHYZE165wzmsJjS0uJDAeWl9N2ayWGiqNs019Rtwm Orqw== X-Gm-Message-State: APjAAAUPy5fzrN67YCP34x5GGt0c8rPZOiDuB0kTVaVNTw0pdyEVhrFO M8njzXZ1cOF0kuu7oUC1ykZRzw== X-Received: by 2002:a1c:1d53:: with SMTP id d80mr1047271wmd.88.1571988823439; Fri, 25 Oct 2019 00:33:43 -0700 (PDT) Received: from wychelm.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id a11sm1586602wmh.40.2019.10.25.00.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Oct 2019 00:33:42 -0700 (PDT) From: Daniel Thompson To: Douglas Anderson , Jason Wessel Cc: Daniel Thompson , kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, patches@linaro.org Subject: [PATCH v4 1/5] kdb: Tidy up code to handle escape sequences Date: Fri, 25 Oct 2019 08:33:24 +0100 Message-Id: <20191025073328.643-2-daniel.thompson@linaro.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191025073328.643-1-daniel.thompson@linaro.org> References: <20191025073328.643-1-daniel.thompson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org kdb_read_get_key() has extremely complex break/continue control flow managed by state variables and is very hard to review or modify. In particular the way the escape sequence handling interacts with the general control flow is hard to follow. Separate out the escape key handling, without changing the control flow. This makes the main body of the code easier to review. Signed-off-by: Daniel Thompson Reviewed-by: Douglas Anderson --- kernel/debug/kdb/kdb_io.c | 128 ++++++++++++++++++++------------------ 1 file changed, 67 insertions(+), 61 deletions(-) diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 3a5184eb6977..cfc054fd8097 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -49,6 +49,65 @@ static int kgdb_transition_check(char *buffer) return 0; } +/** + * kdb_handle_escape() - validity check on an accumulated escape sequence. + * @buf: Accumulated escape characters to be examined. Note that buf + * is not a string, it is an array of characters and need not be + * nil terminated. + * @sz: Number of accumulated escape characters. + * + * Return: -1 if the escape sequence is unwanted, 0 if it is incomplete, + * otherwise it returns a mapped key value to pass to the upper layers. + */ +static int kdb_handle_escape(char *buf, size_t sz) +{ + char *lastkey = buf + sz - 1; + + switch (sz) { + case 1: + if (*lastkey == '\e') + return 0; + break; + + case 2: /* \e */ + if (*lastkey == '[') + return 0; + break; + + case 3: + switch (*lastkey) { + case 'A': /* \e[A, up arrow */ + return 16; + case 'B': /* \e[B, down arrow */ + return 14; + case 'C': /* \e[C, right arrow */ + return 6; + case 'D': /* \e[D, left arrow */ + return 2; + case '1': /* \e[<1,3,4>], may be home, del, end */ + case '3': + case '4': + return 0; + } + break; + + case 4: + if (*lastkey == '~') { + switch (buf[2]) { + case '1': /* \e[1~, home */ + return 1; + case '3': /* \e[3~, del */ + return 4; + case '4': /* \e[4~, end */ + return 5; + } + } + break; + } + + return -1; +} + static int kdb_read_get_key(char *buffer, size_t bufsize) { #define ESCAPE_UDELAY 1000 @@ -102,68 +161,15 @@ static int kdb_read_get_key(char *buffer, size_t bufsize) escape_delay = 2; continue; } - if (ped - escape_data == 1) { - /* \e */ - continue; - } else if (ped - escape_data == 2) { - /* \e */ - if (key != '[') - escape_delay = 2; - continue; - } else if (ped - escape_data == 3) { - /* \e[ */ - int mapkey = 0; - switch (key) { - case 'A': /* \e[A, up arrow */ - mapkey = 16; - break; - case 'B': /* \e[B, down arrow */ - mapkey = 14; - break; - case 'C': /* \e[C, right arrow */ - mapkey = 6; - break; - case 'D': /* \e[D, left arrow */ - mapkey = 2; - break; - case '1': /* dropthrough */ - case '3': /* dropthrough */ - /* \e[<1,3,4>], may be home, del, end */ - case '4': - mapkey = -1; - break; - } - if (mapkey != -1) { - if (mapkey > 0) { - escape_data[0] = mapkey; - escape_data[1] = '\0'; - } - escape_delay = 2; - } - continue; - } else if (ped - escape_data == 4) { - /* \e[<1,3,4> */ - int mapkey = 0; - if (key == '~') { - switch (escape_data[2]) { - case '1': /* \e[1~, home */ - mapkey = 1; - break; - case '3': /* \e[3~, del */ - mapkey = 4; - break; - case '4': /* \e[4~, end */ - mapkey = 5; - break; - } - } - if (mapkey > 0) { - escape_data[0] = mapkey; - escape_data[1] = '\0'; - } - escape_delay = 2; - continue; + + key = kdb_handle_escape(escape_data, ped - escape_data); + if (key > 0) { + escape_data[0] = key; + escape_data[1] = '\0'; } + if (key) + escape_delay = 2; + continue; } break; /* A key to process */ } -- 2.21.0