From patchwork Tue Mar 5 07:37:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hanyuan Zhao X-Patchwork-Id: 1908055 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=rjqjXdjn; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TpnWX1fHkz23hX for ; Tue, 5 Mar 2024 18:39:20 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id D72AE87FC9; Tue, 5 Mar 2024 08:39:16 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.b="rjqjXdjn"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id AC44987FC9; Tue, 5 Mar 2024 08:39:13 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,RDNS_DYNAMIC, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from out203-205-221-236.mail.qq.com (out203-205-221-236.mail.qq.com [203.205.221.236]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id B7C3287BA8 for ; Tue, 5 Mar 2024 08:39:07 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=hanyuan-z@qq.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1709624341; bh=/Kp9DA9Myi+jMRjV6kvBcXcHXnt0U234+7s5FQJqWd0=; h=From:To:Cc:Subject:Date; b=rjqjXdjnkco36Y9BUs/XVApH6h/zVKtRA47bHsmLhgg2XJmsijO572H9ZpjB8IpvT iy6Es9LYZ5O8BojT08WtyvaJ7U2wjYgkxG8mReoK7Jez2NcYiSRhmId04lsQVFm78l /QgRoUxMWbfotOYFHGLMEyFShwYmZ3xh6iE3WIcw= Received: from localhost.localdomain ([183.172.219.254]) by newxmesmtplogicsvrsza7-0.qq.com (NewEsmtp) with SMTP id 9BB85255; Tue, 05 Mar 2024 15:38:59 +0800 X-QQ-mid: xmsmtpt1709624339ta13qwcnm Message-ID: X-QQ-XMAILINFO: NDgMZBR9sMma8CaA269/WIGV3/ltVSGR8621dZANmvn/0WAihbUhi3N64Rtz3O 4qDjUP8lwwkrrQVwp9PwFxTL4JX9HaDiwc27kgyW9obXotGQXS7v0t9uWJQWPYuLXbhO3H0vkOo+ YhmFT8U4aKjf8sXQasG0bd7upc0PHDxFOYMthHCeGiTC/RwSdwuBtDV0wPQpiXC7tFUtMpaCsESJ wImO7pljx8McIIcUFsyVwxtGtsJX1VHgJtpI2R7wDOgH+th3uid0MjqiKs8Yzciyga3fsRzPAgUB sORFCvSyDUjgOc49JuqSqRkL3a7o/sSmNczXdvTNgyvPiBxUyuq/xGf7BxaLQ9r0rwcn3LASFNgX 52MhZoRn3RC7JE33red/MXD/qPrHJo4LRoCl8VX9EMj3M+YfiG9Zv2lSQWV7xjKMj2tjZ8DXkZuv nFamkuli80xn5Kz2euvMhKd4rwYx0Us2JzCinBgorCKieyojmEoEa7Eu7JV52tNAVWNQB9eYWYbf NTm4LM103c6fx11zATxKnTFmaTvGkNM0YqJgh9pxdKkdMTjly79eCUwf2DtadFGORRHy+1LqZ/R+ SkJuFYQmjy38egNyTfggDeYPV6qCsrvTvEEy1PAAS6GM9QUAa44F85bQHD/A8Y30kIy05PEDSAHJ hR/jQgK+oR4BA+XNX86yjaakNWha7qvo8gqzrWS3o4UEsuEDsqaT9JBS+OLsjxs2Eh4j+tm8kHaU mD8zt7rxucGAISp0TjTToC43vCkTxleJ1byxOESSMf3lkEanJlsrJQzExUAy0x9xUOfVxsvehVpx 5/YlgDzF0LBMJPdSaMFy3cMDvf8o34Bh5LM82OjlKsjdTd0bvSCvMT/+s5gHm6i+UlubdASYnai6 bxtvjZ08EF265dvQrfqdxkdpCeYdWnyvIN5YHR6DuTx2eqd9TtxH2AttHEpQnCohGSq/PYTT5eYm FZmbeQ/z7hPFxwEuJVM0ZtOMFlhE2XOo05Om4e2yP3OBDAnP2E9sMNbYWwfUAOQwiof5a4vu8= X-QQ-XMRINFO: OD9hHCdaPRBwq3WW+NvGbIU= From: Hanyuan Zhao To: u-boot@lists.denx.de Cc: Tom Rini , Simon Glass , Hanyuan Zhao Subject: [PATCH v3 1/3] cli: panic when failed to allocate memory for the history buffer Date: Tue, 5 Mar 2024 15:37:33 +0800 X-OQ-MSGID: <20240305073734.14483-1-hanyuan-z@qq.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This commit simply modifies the history initialize function, replacing the return value by panic with reasons. The calling chains of hist_init don't have steps explicitly throwing or dealing with the ENOMEM error, and once the init fails, the whole system is died. Using panic here to provide error information instead. Signed-off-by: Hanyuan Zhao --- This is v3 of patch series cli: allow users to disable history if unused at all. Please ignore the v2 version. --- Changes v1 -> v3: - Separate the first patch and let this patch be the panic one. --- common/cli_readline.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/cli_readline.c b/common/cli_readline.c index 2507be2295..99e7efdfe5 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -110,7 +110,7 @@ static int hist_init(void) hist = calloc(HIST_MAX, HIST_SIZE + 1); if (!hist) - return -ENOMEM; + panic("%s: calloc: out of memory!\n", __func__); for (i = 0; i < HIST_MAX; i++) hist_list[i] = hist + (i * (HIST_SIZE + 1)); From patchwork Tue Mar 5 07:37:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hanyuan Zhao X-Patchwork-Id: 1908056 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=QSJY/1kS; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TpnWw6JyDz23hX for ; Tue, 5 Mar 2024 18:39:40 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id AC0FC87FC8; Tue, 5 Mar 2024 08:39:37 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.b="QSJY/1kS"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 37D5787FCC; Tue, 5 Mar 2024 08:39:36 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RDNS_DYNAMIC,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from out203-205-221-221.mail.qq.com (out203-205-221-221.mail.qq.com [203.205.221.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id DE6C487FE7 for ; Tue, 5 Mar 2024 08:39:30 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=hanyuan-z@qq.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1709624367; bh=ll/J+AfiLLxIqLSUz5411VkCwK7e3fgH8Mg5rfhxkvk=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=QSJY/1kSnEfZNfnoBG2NSfSPRxk5Y8r5UOVAp0UFdxKXQPxvOTduHmpKetLTuDh8G Ivv/ye7qvnPUz9w5DawfOycbi3yPlx89IYpCV5j1Cl6/GE/B7yoBm78yMcM/bM4xjH PR/hSgHrppx9u4DzW+otW6fYrJmpvKksm6ZnMgzo= Received: from localhost.localdomain ([183.172.219.254]) by newxmesmtplogicsvrsza7-0.qq.com (NewEsmtp) with SMTP id 9BB85255; Tue, 05 Mar 2024 15:38:59 +0800 X-QQ-mid: xmsmtpt1709624341t3ebe9jp8 Message-ID: X-QQ-XMAILINFO: NhUkPfKlCtQwNzPIPQ+tPLjr91YjZdZys0M8guvtukQh6xWmJzCLZJ7DCIqnRY FSgegguoejiwMQmCdXWA5MXu1Tm3TOi6LluqvzGf7v2oOJvH7KBY0bLbG62i9CBnKNtvmcp2EkPN AdPyOXUjSxB9ZjFEQGjPl4SUclSdF25kudBhnH0ZN+rkhPHcczBlRUOzpqkiXC/f21Zv3D3vOajE veI4Ol+HHUVdYRpCuoYdEnaessDS4bjTL2Fmtz4s582URcMIBmuVSs9K8s7QHFANdG1Xy+/csoE1 EH3AN1UNZ3HQtVmIn53NRjA98H0kzY0urA0YIB8yn52FNNEnLJP2txFhOGtD+00FExFJgXx2Oo7u Zew6lqGayGqHn5JFGaWXmsnCAn+HFfJkBbRg21fb7xD9GfEUx2ZLfrV0d0DrTQGLYM54gtKVP6z/ q6f7/kwFzqVEQmU5tDRR9WCMA2Kw/5Ew1/cIqnKSjJzbSeOq0ZpGjLxhBC3Vs+aU1WhjD+Sf0jji KbPwXYfqS8mbymCrcgNcgyUYmIYpz3wTa6qkzPkny4fmal03S3WTc2+pCNKHkE5I9g1kRYUJtnWm 037t9V2278WH7ndxMeGsoFst9pH5DSXL4hpXifaStiFVn6M63YUGQDvXUD7ts3Opy3hspAA/XBIu Ge9qDcxqvmv72CYL1RfREkav1ofcemxlFY7H6kW1U57ZNH7K7FqL1DMaIzAixkKjJgKYlqKg5cUf uMW+foE989Yl/bGZgR1C3nudXgNq8dqve30C1pDCPoN///oSFDlYq/5uyR1IxDXHhHM0JnHbQH8z ZEVpSS8jjvIlDHnfruiLFaxWUvunNxlCBwmF2aDNvzhJh8xMnCmSIfbIncim5xVkwQ2UzoA1TVMk p9mJokmah0TZ9IdiKXsKc8V0JCebnvH8takbIENFj4aOtCyWLAOVLBg4jOiI48mQgvEqpXdYZ+eA Sm0wy59lXm5x7RlO3UfRcoODvicSkEPh1BPXKcwTZBN5SchMFMn2WfcI1BM2V1zj7uxJd6T+Y= X-QQ-XMRINFO: MSVp+SPm3vtS1Vd6Y4Mggwc= From: Hanyuan Zhao To: u-boot@lists.denx.de Cc: Tom Rini , Simon Glass , Hanyuan Zhao Subject: [PATCH v3 2/3] cli: allow users to determine history buffer allocation method Date: Tue, 5 Mar 2024 15:37:35 +0800 X-OQ-MSGID: <20240305073734.14483-2-hanyuan-z@qq.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240305073734.14483-1-hanyuan-z@qq.com> References: <20240305073734.14483-1-hanyuan-z@qq.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This commit allows users to choose the appropriate memory allocation method between static allocated and dynamically calloc. The previous static-array way will not obviously contribute to the final binary size since it is uninitialized, and might have better performance than the dynamical one. Now we provide the users with both the two options. Signed-off-by: Hanyuan Zhao --- This is v3 of patch series cli: allow users to disable history if unused at all. Please ignore the v2 version. --- Changes v1 -> v3: - Add more detailed information about the CMD_HISTORY_USE_CALLOC option both in the Kconfig and the code. - Update the comments on global history array and flash running problems. --- cmd/Kconfig | 11 +++++++++++ common/cli_readline.c | 36 +++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/cmd/Kconfig b/cmd/Kconfig index a86b570517..7d2c050e08 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -189,6 +189,17 @@ config CMD_HISTORY Show the command-line history, i.e. a list of commands that are in the history buffer. +config CMD_HISTORY_USE_CALLOC + bool "dynamically allocate memory" + default y + depends on CMD_HISTORY + help + Saying Y to this will use calloc to get the space for history + storing. Otherwise the history buffer will be an uninitialized + static array directly, without the memory allocation, and it is + writable after relocation to RAM. If u-boot is running from ROM + all the time or unsure, say Y to this. + config CMD_LICENSE bool "license" select BUILD_BIN2C diff --git a/common/cli_readline.c b/common/cli_readline.c index 99e7efdfe5..cf4339d0e5 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -86,6 +86,9 @@ static int hist_add_idx; static int hist_cur = -1; static unsigned hist_num; +#ifndef CONFIG_CMD_HISTORY_USE_CALLOC +static char hist_data[HIST_MAX][HIST_SIZE + 1]; +#endif static char *hist_list[HIST_MAX]; #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) @@ -100,20 +103,26 @@ static void getcmd_putchars(int count, int ch) static int hist_init(void) { - unsigned char *hist; int i; - hist_max = 0; - hist_add_idx = 0; - hist_cur = -1; - hist_num = 0; - - hist = calloc(HIST_MAX, HIST_SIZE + 1); +#ifndef CONFIG_CMD_HISTORY_USE_CALLOC + for (i = 0; i < HIST_MAX; i++) { + hist_list[i] = hist_data[i]; + hist_list[i][0] = '\0'; + } +#else + unsigned char *hist = calloc(HIST_MAX, HIST_SIZE + 1); if (!hist) panic("%s: calloc: out of memory!\n", __func__); for (i = 0; i < HIST_MAX; i++) hist_list[i] = hist + (i * (HIST_SIZE + 1)); +#endif + + hist_max = 0; + hist_add_idx = 0; + hist_cur = -1; + hist_num = 0; return 0; } @@ -643,10 +652,15 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer, static int initted; /* - * History uses a global array which is not - * writable until after relocation to RAM. - * Revert to non-history version if still - * running from flash. + * Say N to CMD_HISTORY_USE_CALLOC will skip runtime + * allocation for the history buffer and directly + * use an uninitialized static array as the buffer. + * Doing this might have better performance and not + * increase the binary file's size, as it only marks + * the size. However, the array is only writable after + * relocation to RAM. If u-boot is running from ROM + * all the time, consider say Y to CMD_HISTORY_USE_CALLOC + * or disable CMD_HISTORY. */ if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) { if (!initted) { From patchwork Tue Mar 5 07:37:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hanyuan Zhao X-Patchwork-Id: 1908057 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=TVx8yA6z; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TpnXW4nYMz23cm for ; Tue, 5 Mar 2024 18:40:11 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1BAAA87FE0; Tue, 5 Mar 2024 08:40:07 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=qq.com header.i=@qq.com header.b="TVx8yA6z"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C8BE087FE7; Tue, 5 Mar 2024 08:40:04 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, HELO_DYNAMIC_IPADDR,RDNS_DYNAMIC,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 Received: from out203-205-221-210.mail.qq.com (out203-205-221-210.mail.qq.com [203.205.221.210]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id B0CCC87BA8 for ; Tue, 5 Mar 2024 08:40:00 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=hanyuan-z@qq.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1709624396; bh=11hXqpPKi3UDPdJN86+GdPo8tIqYoNVqbp8sYfnIkvQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=TVx8yA6zytr7E5Wt9hgkweezga2sUGycxC0SWJhhYPqnZr9O3bhS0gHv0DoaXg6B2 B/PVsrA69GQFuiKNzndY6qkYm7YBPsLwQDGTXC7NfyQR2OLj7BUUmslCBFTB087JyL wlCV50METCYQsyj/fwSV41rLwLGYrkpX2MaxgPH8= Received: from localhost.localdomain ([183.172.219.254]) by newxmesmtplogicsvrsza7-0.qq.com (NewEsmtp) with SMTP id 9BB85255; Tue, 05 Mar 2024 15:38:59 +0800 X-QQ-mid: xmsmtpt1709624366t0fb8aigo Message-ID: X-QQ-XMAILINFO: OUMxvQDaATieRaNn7cxJUYLafRk/ZLe0nfgyJlAYfX/8AQjXz9NrWPlTrH71Sx QpabiIT2stCO+TM8cLEsX88swF308KoKGkTFbADa3oMC4QxYLoa37hh+3PGRUryoXwr4HjInhydu J+5SXDI+Ohjzs2QeTK3vRx88RghG8XlObFaOi5VkZL+zykcJRGoBCE8kQYayEjrbNttQZ8SNm78h bAIk+UJXubQW99CIavesjZHlWpY2sMgZrmyh4xU3fYaYVcPnC9ienA+hnRhDQNsdHoHS4OkMxsjp 9HOAup0GoT+20i4C6Y595H6A8SanmQXXe8t16G5m32u5c/Zb/iJNslN7fg79DoI+YNEg2TflLLf6 OfcoWAvpILcRo7cR5lK7K09Qbvdgy+TBZlNqX1irIrEkgC8m8wdyRGFj8z6Pan9unQ/8GDqE02Vr V7+fdtXBSeCDc1htHE3jYFhZ6VhZ/wpFfkjWFuV379M9k/rlcBv2bKtOd95Bn//R7yxPiQhqem8g aascohRpuYSS9OFqDTNHEo4DKPq+qieu+MgNxQlX7Ut25WIAL31Zq4KnjUoA+HAGfl1sT99JABgQ Pw3dwcjwRCRumA4pLsLVEUK83n2zWE1aHPbNDgZOUIz90dYCb8VAbK59pj+NBVHuDBQJ5LmOUIIy gLZbjVoXln8KlJNqZM+Brt8R6PswbPCx0jnil06HbJptvmwlpp6WT3NNmdrgbywi3k7YnVFjjNhJ alZsokLPbS/dMMka5ehOlJDPj1Elk1wW9t5r/A1RRGbzEBUtolpdNjZdUy3ATbfAYaEF+W/JE3Rz z0m5FdpQ2w7GOIvABpushiZ1ebHUOUK0W+xPZgg0aCHw2QFbn/jTGvpFjV5QhiaAsWgrPTAfKJ3K QDwu5udt3Ssa4zWXEziDpaIeiIMIRJ1SLDNxrLE/KqxOLgEtIkSnAqUxLpE3suN1JPwCv+Qd4xi/ 65qfMWPDuzj1TxWxJwh350EMTMrwEW+uj1jgFtM9g4odVa6TdBgg878tecPSJMp9JqAlspl70= X-QQ-XMRINFO: Mp0Kj//9VHAxr69bL5MkOOs= From: Hanyuan Zhao To: u-boot@lists.denx.de Cc: Tom Rini , Simon Glass , Hanyuan Zhao Subject: [PATCH v3 3/3] cli: compile history code if and only if history config is selected Date: Tue, 5 Mar 2024 15:37:37 +0800 X-OQ-MSGID: <20240305073734.14483-3-hanyuan-z@qq.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240305073734.14483-1-hanyuan-z@qq.com> References: <20240305073734.14483-1-hanyuan-z@qq.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This commit allows user to determine whether to have history recording in command-line. Previously to this commit, the CMD_HISTORY only sets the compiling of cmd/history.c, and the history code in cli_readline.c is always compiled and will take a lot of space to store history even if we say N to CMD_HISTORY. Signed-off-by: Hanyuan Zhao --- This is v3 of patch series cli: allow users to disable history if unused at all. Please ignore the v2 version. --- Changes v1 -> v3: - Move the #ifdef CONFIG_CMD_HISTORY directives to this patch. These directives are still necessary when users are not using the history. --- common/cli_readline.c | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/common/cli_readline.c b/common/cli_readline.c index cf4339d0e5..9f71b33a01 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -67,12 +67,14 @@ static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen) #define CTL_BACKSPACE ('\b') #define DEL ((char)255) #define DEL7 ((char)127) -#define CREAD_HIST_CHAR ('!') #define getcmd_putch(ch) putc(ch) #define getcmd_getch() getchar() #define getcmd_cbeep() getcmd_putch('\a') +#ifdef CONFIG_CMD_HISTORY + +#define CREAD_HIST_CHAR ('!') #ifdef CONFIG_SPL_BUILD #define HIST_MAX 3 #define HIST_SIZE 32 @@ -93,14 +95,6 @@ static char *hist_list[HIST_MAX]; #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) -static void getcmd_putchars(int count, int ch) -{ - int i; - - for (i = 0; i < count; i++) - getcmd_putch(ch); -} - static int hist_init(void) { int i; @@ -201,6 +195,15 @@ void cread_print_hist_list(void) i++; } } +#endif /* CONFIG_CMD_HISTORY */ + +static void getcmd_putchars(int count, int ch) +{ + int i; + + for (i = 0; i < count; i++) + getcmd_putch(ch); +} #define BEGINNING_OF_LINE() { \ while (cls->num) { \ @@ -374,6 +377,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar) cls->eol_num--; } break; +#ifdef CONFIG_CMD_HISTORY case CTL_CH('p'): case CTL_CH('n'): if (cls->history) { @@ -403,6 +407,7 @@ int cread_line_process_ch(struct cli_line_state *cls, char ichar) break; } break; +#endif case '\t': if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) { int num2, col; @@ -499,19 +504,23 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len, } *len = cls->eol_num; +#ifdef CONFIG_CMD_HISTORY if (buf[0] && buf[0] != CREAD_HIST_CHAR) cread_add_to_hist(buf); hist_cur = hist_add_idx; +#endif return 0; } #else /* !CONFIG_CMDLINE_EDITING */ +#ifdef CONFIG_CMD_HISTORY static inline int hist_init(void) { return 0; } +#endif static int cread_line(const char *const prompt, char *buf, unsigned int *len, int timeout) @@ -649,7 +658,9 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer, char *p = buffer; uint len = CONFIG_SYS_CBSIZE; int rc; - static int initted; +#ifdef CONFIG_CMD_HISTORY + static int hist_initted; +#endif /* * Say N to CMD_HISTORY_USE_CALLOC will skip runtime @@ -663,11 +674,13 @@ int cli_readline_into_buffer(const char *const prompt, char *buffer, * or disable CMD_HISTORY. */ if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) { - if (!initted) { +#ifdef CONFIG_CMD_HISTORY + if (!hist_initted) { rc = hist_init(); if (rc == 0) - initted = 1; + hist_initted = 1; } +#endif if (prompt) puts(prompt);