From patchwork Wed Mar 16 18:54:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12783046 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 10D69C433F5 for ; Wed, 16 Mar 2022 18:54:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357804AbiCPSzc (ORCPT ); Wed, 16 Mar 2022 14:55:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353551AbiCPSz3 (ORCPT ); Wed, 16 Mar 2022 14:55:29 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 82FA721806 for ; Wed, 16 Mar 2022 11:54:14 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id j26so4312357wrb.1 for ; Wed, 16 Mar 2022 11:54:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=a99DTiyebHzumtArYWAw5QFtDcxbm+7E/kL+LkfoINs=; b=Dmuin9tZT1h6wgQwPsmUUK4Zw9zizdFeWmDSnmNRRMOC9G96UBQDQJxFQh5BUm4fr8 IJSPAZVHZdEHw7XAYdnBaL3KdMJr6FuV+ZIT6citki3dD2qzin6k6D/49ZpBNbldgQiT BQvxWMLko37jOTihIPuflpJZ0LCLUQLSomJ+GI29tcJNwm4UsKZv/HabNC8pwbl+hn1J ZQVKLxPW1n5NPUxEm8o4YrjvA0hT3zi0CEbjEWvP5PNLBnRbSbFflHXvWtQHY55wMkoa pbJdwNlrA+Z0EQTGiEKf77PogGkYoSgDKWR1UT41ddSZUTuYjT+DGVA1qo2FTv+nV4Dc +eSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=a99DTiyebHzumtArYWAw5QFtDcxbm+7E/kL+LkfoINs=; b=c6ZnNSB8HsUxiHucH0KEFFbofy75InvbNszWvEnyOQaVFGD4S+It3nTgC+qhx0fjrr Uv5IHs88mIihmfW8dmHBD+CRn514Tl856QYvA6Z1ew+8nJpeit0Z3NgUnrjnUv0J94d7 Jac7BpCIRneeITRUBm5wjNfQlvCpO+aV8z/EZTUpx2Qw+cRudfa931sCIJBGAWepXuEZ mn8Q3sNTyaRVzXstrRUyUqq/XRNFVOo/Jal6+wWS74ebdnGBKgive1o1QLn4R0RmC09U hQ7MNXQmJw2aqvhNU1UJ+v7JrEZj9sVfhFuhKlHynUwRrzl8EvOnPWE45LOlFYoB32R4 nF7g== X-Gm-Message-State: AOAM533Jn1uHH2Zp6vXgSh6AxvMJTP+/+W93f8IizKjfTk5UveSp3VQ3 3jAwgmmHSb2SjbdnoWVEomaQYUetIVMhaw== X-Google-Smtp-Source: ABdhPJzy9++qvQ6fs1yAgnhKYgdST2k+AWvcREkSA2sCsccdYvqGDDgUxK/WptPC6+jAZMH3SSTHzw== X-Received: by 2002:adf:fcce:0:b0:203:dcdb:dd80 with SMTP id f14-20020adffcce000000b00203dcdbdd80mr992832wrs.455.1647456853067; Wed, 16 Mar 2022 11:54:13 -0700 (PDT) Received: from localhost.localdomain (230.2.7.51.dyn.plus.net. [51.7.2.230]) by smtp.gmail.com with ESMTPSA id f18-20020a5d6652000000b001e669ebd528sm2409144wrw.91.2022.03.16.11.54.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 11:54:12 -0700 (PDT) From: Phillip Wood To: Git Mailing List Cc: Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= , Carlo Arenas , Johannes Schindelin , Junio C Hamano , Ramsay Jones Subject: [PATCH v4 1/4] terminal: use flags for save_term() Date: Wed, 16 Mar 2022 18:54:02 +0000 Message-Id: <20220316185405.29551-2-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220316185405.29551-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220316185405.29551-1-phillip.wood123@gmail.com> Reply-To: Phillip Wood MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood The next commit will add another flag in addition to the existing full_duplex so change the function signature to take a flags argument. Also alter the functions that call save_term() so that they can pass flags down to it. The choice to use an enum for tho bitwise flags is because gdb will display the symbolic names of all the flags that are set rather than the integer value. Signed-off-by: Phillip Wood --- compat/terminal.c | 41 +++++++++++++++++++++-------------------- compat/terminal.h | 7 ++++++- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index 3620184e79..da2f788137 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -34,7 +34,7 @@ void restore_term(void) sigchain_pop_common(); } -int save_term(int full_duplex) +int save_term(enum save_term_flags flags) { if (term_fd < 0) term_fd = open("/dev/tty", O_RDWR); @@ -47,11 +47,11 @@ int save_term(int full_duplex) return 0; } -static int disable_bits(tcflag_t bits) +static int disable_bits(enum save_term_flags flags, tcflag_t bits) { struct termios t; - if (save_term(0) < 0) + if (save_term(flags) < 0) goto error; t = old_term; @@ -71,14 +71,14 @@ static int disable_bits(tcflag_t bits) return -1; } -static int disable_echo(void) +static int disable_echo(enum save_term_flags flags) { - return disable_bits(ECHO); + return disable_bits(flags, ECHO); } -static int enable_non_canonical(void) +static int enable_non_canonical(enum save_term_flags flags) { - return disable_bits(ICANON | ECHO); + return disable_bits(flags, ICANON | ECHO); } #elif defined(GIT_WINDOWS_NATIVE) @@ -126,15 +126,15 @@ void restore_term(void) hconin = hconout = INVALID_HANDLE_VALUE; } -int save_term(int full_duplex) +int save_term(enum save_term_flags flags) { hconin = CreateFileA("CONIN$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hconin == INVALID_HANDLE_VALUE) return -1; - if (full_duplex) { + if (flags & SAVE_TERM_DUPLEX) { hconout = CreateFileA("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); @@ -154,7 +154,7 @@ int save_term(int full_duplex) return -1; } -static int disable_bits(DWORD bits) +static int disable_bits(enum save_term_flags flags, DWORD bits) { if (use_stty) { struct child_process cp = CHILD_PROCESS_INIT; @@ -191,7 +191,7 @@ static int disable_bits(DWORD bits) use_stty = 0; } - if (save_term(0) < 0) + if (save_term(flags) < 0) return -1; if (!SetConsoleMode(hconin, cmode_in & ~bits)) { @@ -204,14 +204,15 @@ static int disable_bits(DWORD bits) return 0; } -static int disable_echo(void) +static int disable_echo(enum save_term_flags flags) { - return disable_bits(ENABLE_ECHO_INPUT); + return disable_bits(flags, ENABLE_ECHO_INPUT); } -static int enable_non_canonical(void) +static int enable_non_canonical(enum save_term_flags flags) { - return disable_bits(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); + return disable_bits(flags, + ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); } /* @@ -267,7 +268,7 @@ char *git_terminal_prompt(const char *prompt, int echo) return NULL; } - if (!echo && disable_echo()) { + if (!echo && disable_echo(0)) { fclose(input_fh); fclose(output_fh); return NULL; @@ -361,7 +362,7 @@ int read_key_without_echo(struct strbuf *buf) static int warning_displayed; int ch; - if (warning_displayed || enable_non_canonical() < 0) { + if (warning_displayed || enable_non_canonical(0) < 0) { if (!warning_displayed) { warning("reading single keystrokes not supported on " "this platform; reading line instead"); @@ -413,10 +414,10 @@ int read_key_without_echo(struct strbuf *buf) #else -int save_term(int full_duplex) +int save_term(enum save_term_flags flags) { - /* full_duplex == 1, but no support available */ - return -full_duplex; + /* no duplex support available */ + return -!!(flags & SAVE_TERM_DUPLEX); } void restore_term(void) diff --git a/compat/terminal.h b/compat/terminal.h index 0fb9fa147c..aeb24c9478 100644 --- a/compat/terminal.h +++ b/compat/terminal.h @@ -1,14 +1,19 @@ #ifndef COMPAT_TERMINAL_H #define COMPAT_TERMINAL_H +enum save_term_flags { + /* Save input and output settings */ + SAVE_TERM_DUPLEX = 1 << 0, +}; + /* * Save the terminal attributes so they can be restored later by a * call to restore_term(). Note that every successful call to * save_term() must be matched by a call to restore_term() even if the * attributes have not been changed. Returns 0 on success, -1 on * failure. */ -int save_term(int full_duplex); +int save_term(enum save_term_flags flags); /* Restore the terminal attributes that were saved with save_term() */ void restore_term(void); From patchwork Wed Mar 16 18:54:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12783047 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52132C433EF for ; Wed, 16 Mar 2022 18:54:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357806AbiCPSzd (ORCPT ); Wed, 16 Mar 2022 14:55:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357793AbiCPSza (ORCPT ); Wed, 16 Mar 2022 14:55:30 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C4AA21818 for ; Wed, 16 Mar 2022 11:54:15 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id a1so2897483wrh.10 for ; Wed, 16 Mar 2022 11:54:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=knoVMDKEHi1vepotLQ32wXGXLLrr3wy6O+9448po9Q8=; b=hzKQBtuHdiOu2fkFlZqjWBgbroN+1RRNDGzYfit1wppVivj5WpoxUbSzdKZi2F8KaD Ih9Y+S4w5QBVne2/ke1jtKFC4sfba5Kvr0goQCeXSy5UrbFbMV6xK5z6mR4JU9Ll/IDP thWYaKTtHrYYQuX4e5Tg2SeCUdB37lyTgpXCL1AhDpw9/Q44FtSINKqMaL7Dii8KiCLs tloxBqqLx46P1+TCX+sJ9La7KEY+cwgzo9juUmF6pDK6WgR3JKMDXWlr0HdpndP33LDS f+qmxW9rAHlwICKNGYZEn/NpHx2Xi8eLUnKdB1YB3gJ92b8bfSRQK7ee4t7oRKsIEQuj +71Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=knoVMDKEHi1vepotLQ32wXGXLLrr3wy6O+9448po9Q8=; b=nZTX/TxQIkp2EIbnebjUIZM+wsTH6iuah2y/UIPz1O8EfaZ5h16FZ8L9/cvkUc9NFp VuSsjCfh4BZhgBr2xNYo4vtwK9c1okDYrxb9udjxZcfksO4VulcX0H+cjCuMNwBBgzlh g12222Gdrj0R0O7FyOrmuM1GfU9LNcxUk3djq9u/SOXWqBGtbfg65x2re1ZRhzc1AD/v ubepcZRQO/Et/B/6MoMSj2cXrnHvbIpBb7yGmj3Cbqqxnl629YT1ZrhQBZsneceRyV7Q LKYJImvop7e/t/U5ISIJWkgHysDGcVaIwZoGUr7DlCSsVNB9YKVILqdvEVf05ssW2wP8 9V3w== X-Gm-Message-State: AOAM531YQO9lMeNMQvPXDMoSZsJ/tR294MlaG4qyIy1F9fd68P3lWTTA gmafhbwIUifE+JJtdMGeXCANU5n7SwkIDw== X-Google-Smtp-Source: ABdhPJwWBT51BLYo8HX6OmaWLLnXAnoLIBP2UPrgblBB2X4z/K9J6HiU3Heql1VLea2yEfFaR8gMEw== X-Received: by 2002:adf:ec02:0:b0:203:e0c5:ab14 with SMTP id x2-20020adfec02000000b00203e0c5ab14mr1015532wrn.95.1647456853994; Wed, 16 Mar 2022 11:54:13 -0700 (PDT) Received: from localhost.localdomain (230.2.7.51.dyn.plus.net. [51.7.2.230]) by smtp.gmail.com with ESMTPSA id f18-20020a5d6652000000b001e669ebd528sm2409144wrw.91.2022.03.16.11.54.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 11:54:13 -0700 (PDT) From: Phillip Wood To: Git Mailing List Cc: Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= , Carlo Arenas , Johannes Schindelin , Junio C Hamano , Ramsay Jones Subject: [PATCH v4 2/4] terminal: don't assume stdin is /dev/tty Date: Wed, 16 Mar 2022 18:54:03 +0000 Message-Id: <20220316185405.29551-3-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220316185405.29551-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220316185405.29551-1-phillip.wood123@gmail.com> Reply-To: Phillip Wood MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood read_key_without_echo() reads from stdin but uses /dev/tty when it disables echo. This is unfortunate as there no guarantee that stdin is the same device as /dev/tty. The perl version of "add -p" uses stdin when it sets the terminal mode, this commit does the same for the builtin version. There is still a difference between the perl and builtin versions though - the perl version will ignore any errors when setting the terminal mode[1] and will still read single bytes when stdin is not a terminal. The builtin version displays a warning if setting the terminal mode fails and switches to reading a line at a time. [1] https://github.com/jonathanstowe/TermReadKey/blob/b061c913bbf7ff9bad9b4eea6caae189eacd6063/ReadKey.xs#L1090 Signed-off-by: Phillip Wood --- compat/terminal.c | 29 ++++++++++++++++++++--------- compat/terminal.h | 2 ++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index da2f788137..4893294eb6 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -20,28 +20,41 @@ static void restore_term_on_signal(int sig) #define INPUT_PATH "/dev/tty" #define OUTPUT_PATH "/dev/tty" +static volatile sig_atomic_t term_fd_needs_closing; static int term_fd = -1; static struct termios old_term; +static void close_term_fd(void) +{ + if (term_fd_needs_closing) + close(term_fd); + term_fd_needs_closing = 0; + term_fd = -1; +} + void restore_term(void) { if (term_fd < 0) return; tcsetattr(term_fd, TCSAFLUSH, &old_term); - close(term_fd); - term_fd = -1; + close_term_fd(); sigchain_pop_common(); } int save_term(enum save_term_flags flags) { if (term_fd < 0) - term_fd = open("/dev/tty", O_RDWR); + term_fd = ((flags & SAVE_TERM_STDIN) + ? 0 + : open("/dev/tty", O_RDWR)); if (term_fd < 0) return -1; - if (tcgetattr(term_fd, &old_term) < 0) + term_fd_needs_closing = !(flags & SAVE_TERM_STDIN); + if (tcgetattr(term_fd, &old_term) < 0) { + close_term_fd(); return -1; + } sigchain_push_common(restore_term_on_signal); return 0; @@ -52,7 +65,7 @@ static int disable_bits(enum save_term_flags flags, tcflag_t bits) struct termios t; if (save_term(flags) < 0) - goto error; + return -1; t = old_term; @@ -65,9 +78,7 @@ static int disable_bits(enum save_term_flags flags, tcflag_t bits) return 0; sigchain_pop_common(); -error: - close(term_fd); - term_fd = -1; + close_term_fd(); return -1; } @@ -362,7 +373,7 @@ int read_key_without_echo(struct strbuf *buf) static int warning_displayed; int ch; - if (warning_displayed || enable_non_canonical(0) < 0) { + if (warning_displayed || enable_non_canonical(SAVE_TERM_STDIN) < 0) { if (!warning_displayed) { warning("reading single keystrokes not supported on " "this platform; reading line instead"); diff --git a/compat/terminal.h b/compat/terminal.h index aeb24c9478..79ed00cf61 100644 --- a/compat/terminal.h +++ b/compat/terminal.h @@ -4,6 +4,8 @@ enum save_term_flags { /* Save input and output settings */ SAVE_TERM_DUPLEX = 1 << 0, + /* Save stdin rather than /dev/tty (fails if stdin is not a terminal) */ + SAVE_TERM_STDIN = 1 << 1, }; /* From patchwork Wed Mar 16 18:54:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12783048 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3498FC433F5 for ; Wed, 16 Mar 2022 18:54:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234976AbiCPSzf (ORCPT ); Wed, 16 Mar 2022 14:55:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54376 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239949AbiCPSzb (ORCPT ); Wed, 16 Mar 2022 14:55:31 -0400 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 473E121829 for ; Wed, 16 Mar 2022 11:54:16 -0700 (PDT) Received: by mail-wr1-x42a.google.com with SMTP id x15so4241434wru.13 for ; Wed, 16 Mar 2022 11:54:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=ZJnaG7Qrvg1iVlT6oS76xZQrmLcaeNcLfgFeD+xm/BE=; b=dH8HzPmRbE+SjJcopIQ7V55Hm4a8m1s4MwO4d3aUiRWxJEctaiGYUu+6DGFoWbbmFc Di4fbY07O76jBdZ2etJPvIMrXJgWOTkJgSyWETCWo4VkU6vkJOG5qxOB7LBzTTWhVuZW AZyGSp8amCdYziZQPI15Qo4bz0IlXAi3FkTOO+PldzTIYA8f6BjLenmIJVVPk4rJXXLL gGmYSHVbwNzF0K+hp4uBubACTPjqopY9MAqDvrX4vSor+8h+zEyKmLVa6zNNzXYxZTWa a5as3rhQcD0f9dj8SDRpfBOlKNhtyzPspv8DoUEPxBEkkIHz2574TVcmn4T6ZUFYG6Il 01zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=ZJnaG7Qrvg1iVlT6oS76xZQrmLcaeNcLfgFeD+xm/BE=; b=w8ghcRItPJtwt30/KSj8CatB/wDzRH3arGzB+/l5uIctinnxUWReWsxKuskQC2wtSo AcdGm7R9msQmdUzfIIxJAEESpXvgJ+rBo0a59QaSL6goAoes/pbpn2UTTealsOtutz/8 3I0lkwCAaqtGHRwLyC+Mx8rZIkso3Z7pzxegPrxAobq9cKWr24Fy28mKs+IwXLEMlU+/ lrnrmR15WNGcXW76oYkfEEA+u3LZUZuqo7OmuzqBv3rm8yPywFxqSv58nStoKLY0WaEq Y20rDSPRNOXuA1lRk0WyOKSp7odvAjKeBYnuUZFUqiW3Zwqni/9sgVqRBdnhiHXtVFws /Lmg== X-Gm-Message-State: AOAM530c18tmoXkA8scVr7+ULvn31rlYLPUqn4SIvlF06diQs4jLvzJq LqJmviwvD3d4yf1aG6If9zDfXgi1GXHORQ== X-Google-Smtp-Source: ABdhPJwA5kXxkp8Peqn3EkvZDFGIVtl5p1iDsbE4EBfw2SQtkYcSHkG2mGLpF1PEiX356GfO71sFIw== X-Received: by 2002:a05:6000:178c:b0:203:8b24:42b7 with SMTP id e12-20020a056000178c00b002038b2442b7mr1021911wrg.352.1647456854838; Wed, 16 Mar 2022 11:54:14 -0700 (PDT) Received: from localhost.localdomain (230.2.7.51.dyn.plus.net. [51.7.2.230]) by smtp.gmail.com with ESMTPSA id f18-20020a5d6652000000b001e669ebd528sm2409144wrw.91.2022.03.16.11.54.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 11:54:14 -0700 (PDT) From: Phillip Wood To: Git Mailing List Cc: Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= , Carlo Arenas , Johannes Schindelin , Junio C Hamano , Ramsay Jones Subject: [PATCH v4 3/4] terminal: work around macos poll() bug Date: Wed, 16 Mar 2022 18:54:04 +0000 Message-Id: <20220316185405.29551-4-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220316185405.29551-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220316185405.29551-1-phillip.wood123@gmail.com> Reply-To: Phillip Wood MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood On macos the builtin "add -p" does not handle keys that generate escape sequences because poll() does not work with terminals there. Switch to using select() on non-windows platforms to work around this. Signed-off-by: Phillip Wood --- compat/terminal.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index 4893294eb6..2ae8a6f13e 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -92,6 +92,31 @@ static int enable_non_canonical(enum save_term_flags flags) return disable_bits(flags, ICANON | ECHO); } +/* + * On macos it is not possible to use poll() with a terminal so use select + * instead. + */ +static int getchar_with_timeout(int timeout) +{ + struct timeval tv, *tvp = NULL; + fd_set readfds; + int res; + + if (timeout >= 0) { + tv.tv_sec = timeout / 1000; + tv.tv_usec = (timeout % 1000) * 1000; + tvp = &tv; + } + + FD_ZERO(&readfds); + FD_SET(0, &readfds); + res = select(1, &readfds, NULL, NULL, tvp); + if (res <= 0) + return EOF; + + return getchar(); +} + #elif defined(GIT_WINDOWS_NATIVE) #define INPUT_PATH "CONIN$" @@ -257,6 +282,16 @@ static int mingw_getchar(void) } #define getchar mingw_getchar +static int getchar_with_timeout(int timeout) +{ + struct pollfd pfd = { .fd = 0, .events = POLLIN }; + + if (poll(&pfd, 1, timeout) < 1) + return EOF; + + return getchar(); +} + #endif #ifndef FORCE_TEXT @@ -407,12 +442,7 @@ int read_key_without_echo(struct strbuf *buf) * half a second when we know that the sequence is complete. */ while (!is_known_escape_sequence(buf->buf)) { - struct pollfd pfd = { .fd = 0, .events = POLLIN }; - - if (poll(&pfd, 1, 500) < 1) - break; - - ch = getchar(); + ch = getchar_with_timeout(500); if (ch == EOF) break; strbuf_addch(buf, ch); From patchwork Wed Mar 16 18:54:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12783049 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7E1A9C433F5 for ; Wed, 16 Mar 2022 18:54:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1357814AbiCPSzh (ORCPT ); Wed, 16 Mar 2022 14:55:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1357803AbiCPSzc (ORCPT ); Wed, 16 Mar 2022 14:55:32 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2723221E2C for ; Wed, 16 Mar 2022 11:54:17 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id d7so4277157wrb.7 for ; Wed, 16 Mar 2022 11:54:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references:reply-to :mime-version:content-transfer-encoding; bh=tw+9Nm2jbV7lxRhjbSy7DihHLfAAMIGRzwUVP1j4pyo=; b=baW1aeaLPnML0jJI/RuT8ky9c3soXQs25QCMHrlJFr8jclsQ2h8IY95rUIGJUyBg3E 0vPt9bKzgocSU8IvHLqyiary3Wg9TwX9OEuUgCBW6irXACUnFEIQQSOnKUr7lpWmeNa2 dSQusSCEu5gOYQvb4qWiXhVp6n6TysGc05UBToEe395FnRqTV2QR2A/pAnxJ4gM0jbD/ TthvdTtJ75FvLbB14+t9HfVuHfFVqNlEwAI2qWyQakrs4s4O2zvQ7TPv+xxai7c+B1TS IfOy63vOBkjdYZ1guK8cuJzRs5/yqg9eH7HyBJnf2ko2rL092m+Wfrv3DI9Xa6rlbl+2 Y4xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:reply-to:mime-version:content-transfer-encoding; bh=tw+9Nm2jbV7lxRhjbSy7DihHLfAAMIGRzwUVP1j4pyo=; b=cYZZz+enEGFqdcmopZsegtUv9BffinZVe0ISzxeH59zGhbNF1sdi2LUfzKQV90NPMF ozafkQvD/R3GjQYRtbW9d++pA0yGpH1tvR8PONRfW1/PtTSpe6DfEyx3HZbIYZjOTI00 0Q2JfQ6Ch1oLSZZz2t7B7G8cxNncq+RvrcbxgUyEfrOA5TdVo4wzgk33RsBpmLgUTlf8 LAJQllHA3ldDGZShd+JyDhA6FOZn6vMbRFiKYB5ZWIY8ql2icXLv7Aw8ST6lW/AgRBDG x1jo2uMV7lGxRmLHD6DCrBbMcYeEw6IrJclYq+bj5nrW9FPE0u9WyOsa1fDvfuh0iqA2 YxAQ== X-Gm-Message-State: AOAM532Pvpn7ijpVaMOLFF5MnVza42S0QRfhDmw9ZzgL+uy3wCU/VWj7 0jlEjPTs/Psufz1tPRIU38y2Tr1+dQBS2A== X-Google-Smtp-Source: ABdhPJyVanthhl7ljrXclKD7WQIK0qR9NGHAB2I4LVtAHHfjF/MeJWM1oCUR6uSZe8Hzj6zStVJNKg== X-Received: by 2002:adf:fe81:0:b0:1f0:246e:ae5 with SMTP id l1-20020adffe81000000b001f0246e0ae5mr1035563wrr.652.1647456855700; Wed, 16 Mar 2022 11:54:15 -0700 (PDT) Received: from localhost.localdomain (230.2.7.51.dyn.plus.net. [51.7.2.230]) by smtp.gmail.com with ESMTPSA id f18-20020a5d6652000000b001e669ebd528sm2409144wrw.91.2022.03.16.11.54.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Mar 2022 11:54:15 -0700 (PDT) From: Phillip Wood To: Git Mailing List Cc: Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= , Carlo Arenas , Johannes Schindelin , Junio C Hamano , Ramsay Jones Subject: [PATCH v4 4/4] terminal: restore settings on SIGTSTP Date: Wed, 16 Mar 2022 18:54:05 +0000 Message-Id: <20220316185405.29551-5-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220316185405.29551-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220316185405.29551-1-phillip.wood123@gmail.com> Reply-To: Phillip Wood MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Phillip Wood If the user suspends git while it is waiting for a keypress reset the terminal before stopping and restore the settings when git resumes. If the user tries to resume in the background print an error message (taking care to use async safe functions) before stopping again. Ideally we would reprint the prompt for the user when git resumes but this patch just restarts the read(). The signal handler is established with sigaction() rather than using sigchain_push() as this allows us to control the signal mask when the handler is invoked and ensure SA_RESTART is used to restart the read() when resuming. Signed-off-by: Phillip Wood --- compat/terminal.c | 132 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 129 insertions(+), 3 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index 2ae8a6f13e..7db330c52d 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -1,4 +1,4 @@ -#include "git-compat-util.h" +#include "cache.h" #include "compat/terminal.h" #include "sigchain.h" #include "strbuf.h" @@ -24,6 +24,102 @@ static volatile sig_atomic_t term_fd_needs_closing; static int term_fd = -1; static struct termios old_term; +static const char *background_resume_msg; +static const char *restore_error_msg; +static volatile sig_atomic_t ttou_received; + +/* async safe error function for use by signal handlers. */ +static void write_err(const char *msg) +{ + write_in_full(2, "error: ", strlen("error: ")); + write_in_full(2, msg, strlen(msg)); + write_in_full(2, "\n", 1); +} + +static void print_background_resume_msg(int signo) +{ + int saved_errno = errno; + sigset_t mask; + struct sigaction old_sa; + struct sigaction sa = { .sa_handler = SIG_DFL }; + + ttou_received = 1; + write_err(background_resume_msg); + sigaction(signo, &sa, &old_sa); + raise(signo); + sigemptyset(&mask); + sigaddset(&mask, signo); + sigprocmask(SIG_UNBLOCK, &mask, NULL); + /* Stopped here */ + sigprocmask(SIG_BLOCK, &mask, NULL); + sigaction(signo, &old_sa, NULL); + errno = saved_errno; +} + +static void restore_terminal_on_suspend(int signo) +{ + int saved_errno = errno; + int res; + struct termios t; + sigset_t mask; + struct sigaction old_sa; + struct sigaction sa = { .sa_handler = SIG_DFL }; + int can_restore = 1; + + if (tcgetattr(term_fd, &t) < 0) + can_restore = 0; + + if (tcsetattr(term_fd, TCSAFLUSH, &old_term) < 0) + write_err(restore_error_msg); + + sigaction(signo, &sa, &old_sa); + raise(signo); + sigemptyset(&mask); + sigaddset(&mask, signo); + sigprocmask(SIG_UNBLOCK, &mask, NULL); + /* Stopped here */ + sigprocmask(SIG_BLOCK, &mask, NULL); + sigaction(signo, &old_sa, NULL); + if (!can_restore) { + write_err(restore_error_msg); + goto out; + } + /* + * If we resume in the background then we receive SIGTTOU when calling + * tcsetattr() below. Set up a handler to print an error message in that + * case. + */ + sigemptyset(&mask); + sigaddset(&mask, SIGTTOU); + sa.sa_mask = old_sa.sa_mask; + sa.sa_handler = print_background_resume_msg; + sa.sa_flags = SA_RESTART; + sigaction(SIGTTOU, &sa, &old_sa); + again: + ttou_received = 0; + sigprocmask(SIG_UNBLOCK, &mask, NULL); + res = tcsetattr(term_fd, TCSAFLUSH, &t); + sigprocmask(SIG_BLOCK, &mask, NULL); + if (ttou_received) + goto again; + else if (res < 0) + write_err(restore_error_msg); + sigaction(SIGTTOU, &old_sa, NULL); + out: + errno = saved_errno; +} + +static void reset_job_signals(void) +{ + if (restore_error_msg) { + signal(SIGTTIN, SIG_DFL); + signal(SIGTTOU, SIG_DFL); + signal(SIGTSTP, SIG_DFL); + restore_error_msg = NULL; + background_resume_msg = NULL; + } +} + static void close_term_fd(void) { if (term_fd_needs_closing) @@ -40,10 +136,13 @@ void restore_term(void) tcsetattr(term_fd, TCSAFLUSH, &old_term); close_term_fd(); sigchain_pop_common(); + reset_job_signals(); } int save_term(enum save_term_flags flags) { + struct sigaction sa; + if (term_fd < 0) term_fd = ((flags & SAVE_TERM_STDIN) ? 0 @@ -56,6 +155,26 @@ int save_term(enum save_term_flags flags) return -1; } sigchain_push_common(restore_term_on_signal); + /* + * If job control is disabled then the shell will have set the + * disposition of SIGTSTP to SIG_IGN. + */ + sigaction(SIGTSTP, NULL, &sa); + if (sa.sa_handler == SIG_IGN) + return 0; + + /* avoid calling gettext() from signal handler */ + background_resume_msg = _("cannot resume in the background, please use 'fg' to resume"); + restore_error_msg = _("cannot restore terminal settings"); + sa.sa_handler = restore_terminal_on_suspend; + sa.sa_flags = SA_RESTART; + sigemptyset(&sa.sa_mask); + sigaddset(&sa.sa_mask, SIGTSTP); + sigaddset(&sa.sa_mask, SIGTTIN); + sigaddset(&sa.sa_mask, SIGTTOU); + sigaction(SIGTSTP, &sa, NULL); + sigaction(SIGTTIN, &sa, NULL); + sigaction(SIGTTOU, &sa, NULL); return 0; } @@ -78,6 +197,7 @@ static int disable_bits(enum save_term_flags flags, tcflag_t bits) return 0; sigchain_pop_common(); + reset_job_signals(); close_term_fd(); return -1; } @@ -102,6 +222,7 @@ static int getchar_with_timeout(int timeout) fd_set readfds; int res; + again: if (timeout >= 0) { tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout % 1000) * 1000; @@ -111,9 +232,14 @@ static int getchar_with_timeout(int timeout) FD_ZERO(&readfds); FD_SET(0, &readfds); res = select(1, &readfds, NULL, NULL, tvp); - if (res <= 0) + if (!res) return EOF; - + if (res < 0) { + if (errno == EINTR) + goto again; + else + return EOF; + } return getchar(); }