From patchwork Tue Mar 15 10:57:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12781315 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 F35E9C433FE for ; Tue, 15 Mar 2022 10:58:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347670AbiCOK7O (ORCPT ); Tue, 15 Mar 2022 06:59:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38984 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348038AbiCOK6z (ORCPT ); Tue, 15 Mar 2022 06:58:55 -0400 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 421B531DC1 for ; Tue, 15 Mar 2022 03:57:43 -0700 (PDT) Received: by mail-wm1-x333.google.com with SMTP id r7so432524wmq.2 for ; Tue, 15 Mar 2022 03:57:43 -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=asyH8zENqeZInonmw0zb6CmwYvafFYG+bvzVDl9fXr6NEvBybEAutSd7SwRrv2fojC 0+cgZFm3NQeFzutrtZpq0z7dRlZ9jnZHAF4q51HEvP3d6FHTVKbkMkKITAbsXtIfura3 Oe/gD/wqn9VehzLaJlb92XZKylVXDC9f6xaIhfyZfmQtNfxQk7ucIfPtmQ5uOWJE31XD 5pyBdNj4sxiM8aTd8zMbzzE3dYaz8xe9qNrhb8agZWnXl/bVH6W6H3PHNEwIdt+8ef+9 YUf1zJaNHEiEuuX6RIb4GNSSaBFsOdbnC5Dex9CUwNQxugKkcFQfEESYTvw9liiFZkBc X9sA== 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=Ry4MEoyhcaal5DoKdTr7IR0BDAss8HjlvIsEP+jYlXLbUDdQXImMhqhYGPG5ruvbtF vBE1FqBDHhcSZCpxh3hjAAINewy0EJ5SeeSyFcnpBUyyt+jVKXWwzW69D+VjteRg8G+c LbnmTEk2LDG/pA04X6eGrvaX7r+X3YVfzEL0wrye3N0HqSOATzclb3K/2ern6vZHncLL DXo6OEdFbYxiaxN+job0GENRm8ynwKnsUVozK4iuNPd3Asq7RgSTJ3pEWmE1gy20vU/4 KhpD87KLu+DOQSjG+Bo71nyJdFClg+V9hxNTILvCtrhVac4sYYUqix4LDNFtB0HJlF6W IZ4Q== X-Gm-Message-State: AOAM533y3F+iPZTsXJy01xS4hSpn0Ev8R0Ozz+XD6FbWjzVZiHhsfp6A Jxx9JZPpAB0gTj1SMb2XRubAA5NhJYfQ9Q== X-Google-Smtp-Source: ABdhPJz+D/kQKQ8qCc7LPbpYGOOd0sop5iv5XQtjEcH7ZOre9o96jFIO/nlasN0/EkryEpEHxc3GaQ== X-Received: by 2002:a05:600c:19d1:b0:389:7772:b21b with SMTP id u17-20020a05600c19d100b003897772b21bmr2860027wmq.118.1647341861635; Tue, 15 Mar 2022 03:57:41 -0700 (PDT) Received: from localhost.localdomain (217.2.7.51.dyn.plus.net. [51.7.2.217]) by smtp.gmail.com with ESMTPSA id p16-20020adff210000000b001f062b80091sm15038530wro.34.2022.03.15.03.57.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Mar 2022 03:57:41 -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 v3 1/4] terminal: use flags for save_term() Date: Tue, 15 Mar 2022 10:57:20 +0000 Message-Id: <20220315105723.19398-2-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220315105723.19398-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220315105723.19398-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 Tue Mar 15 10:57:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12781314 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 0B0C1C433F5 for ; Tue, 15 Mar 2022 10:58:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242648AbiCOK7N (ORCPT ); Tue, 15 Mar 2022 06:59:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38986 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348039AbiCOK6z (ORCPT ); Tue, 15 Mar 2022 06:58:55 -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 1A8E535269 for ; Tue, 15 Mar 2022 03:57:44 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id b19so5992839wrh.11 for ; Tue, 15 Mar 2022 03:57:44 -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=lnnWJ4rUdFcclnTXVMW9FMPghhyIQq8RedCrWKs8LIY=; b=H5mN95vq9qNLaKb4TPYSncZb/SZc6fjAszfZkxSqKpEuk4W8oVed+i+awvFkJ4QU1/ tFO9k0LwIGy/DZ5/w4Ge+T82auBKN4ckJ6WFhWrFE88eRAQVJnlz4X+6EEXDu6Qu/TIO RJG/1jU+PxKGWndqLBP+0ppUVWTxUBA68gWvDapXRfZxcjPmuRH7njCV1SkoIclbxjhN M2chxDkqW2os8pmyepY1Ww9SbcoNhN3z2cSYHDXgXJRuPY7O3KxpkUKLFh72I28Q8HOT veLUHig4J7topCHJM9opw1cjcTwzUgcw/3rOp5P8e7uAr1RrOnrTo4cl1o2PlmuZ/0fa l5xw== 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=lnnWJ4rUdFcclnTXVMW9FMPghhyIQq8RedCrWKs8LIY=; b=saH/Zo7kk6MBP8SJttgIQC+/Ts63IWU+zCKdOAC5/YbbzxkOBqQOyYp7tQhsCyVYG2 /veBAfV4275iCNd8ZWTX2WZNd0SxnT0ZXmYUNef80Rwer5j24RPPjGBPvxkF12ZB2hTd v67UTGKKeAyh4LB7lW6Q378lHa1bQHh4W5yfoHvV2S2wH+GqrvYhZbzIxF8aegUX3jAq 84v2VTUcVPFWiSdkWc4aPESwa+YZJIQKCeh8lq+Et+a3Y0B6qdEk0vF5RAmfkM201Ktt PvIS2JqCa/P9motdPUMASjFOVjrv2I1Dy3zsKzp7qZGDLzailtWlfBuwu7/dVqaU9yJ7 Mmyg== X-Gm-Message-State: AOAM530/j5/hbfgdMQ11x0P07RCo4UsnkxltGUDe0PPibQ6/Z04HKNYe qKWd6nBzW8rxi9xOdV9hsrMGjJvjo/p02A== X-Google-Smtp-Source: ABdhPJy9lufaIicKE7IwdPcTZbTQH7uqpWBeRk3bQxsnbWfR6JtarZpMbdVAJeBC2IXjfr+10G8NGQ== X-Received: by 2002:adf:ff92:0:b0:1f1:1754:b3c2 with SMTP id j18-20020adfff92000000b001f11754b3c2mr20414900wrr.655.1647341862559; Tue, 15 Mar 2022 03:57:42 -0700 (PDT) Received: from localhost.localdomain (217.2.7.51.dyn.plus.net. [51.7.2.217]) by smtp.gmail.com with ESMTPSA id p16-20020adff210000000b001f062b80091sm15038530wro.34.2022.03.15.03.57.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Mar 2022 03:57:42 -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 v3 2/4] terminal: don't assume stdin is /dev/tty Date: Tue, 15 Mar 2022 10:57:21 +0000 Message-Id: <20220315105723.19398-3-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220315105723.19398-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220315105723.19398-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 | 18 ++++++++++++------ compat/terminal.h | 2 ++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index da2f788137..bfbde3c1af 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -23,21 +23,28 @@ static void restore_term_on_signal(int sig) static int term_fd = -1; static struct termios old_term; +static void close_term_fd(void) +{ + if (term_fd) + close(term_fd); + 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) @@ -66,8 +73,7 @@ static int disable_bits(enum save_term_flags flags, tcflag_t bits) sigchain_pop_common(); error: - close(term_fd); - term_fd = -1; + close_term_fd(); return -1; } @@ -362,7 +368,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 Tue Mar 15 10:57:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12781318 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 18183C433F5 for ; Tue, 15 Mar 2022 10:58:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347706AbiCOK7V (ORCPT ); Tue, 15 Mar 2022 06:59:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348045AbiCOK64 (ORCPT ); Tue, 15 Mar 2022 06:58:56 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E371C3526F for ; Tue, 15 Mar 2022 03:57:44 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id j26so28369371wrb.1 for ; Tue, 15 Mar 2022 03:57:44 -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=rlSxrIK8uSgVb6L9FxQlZ/+0o5IDpChMZH+9IHC18nw=; b=dRJ2H4iS5ShDUbqLRq43svK5MMu6ndvU9jN/YqZbHhUcghQQhVoKRRcxiKm7iUslnV qEEmaCTvtCZ3b1C4bNYRBMX8uqXrluVkjtgmg+4M7+ifEy+L7pdB9PZCtly3Gu/Rfsam Bt2neBuwPUDIC2D7nPg+CuRZwoSFBPQInbZLS8nJAmeGDbnAV9HVvOOa6qRz6k0tiMSS miyQ5RuW9TTmsWaQQ7Es8wTH3JIXR7U1Y46PiKgeH5G6zr9jb8Qz6vTu7P5/z2sHX60v yNDH5d1TTMxVSXHXcY+z6VzuuKtDubtkVxMPMsknc1Cjc98ksUuLCRPtw336x33CZ33j acSQ== 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=rlSxrIK8uSgVb6L9FxQlZ/+0o5IDpChMZH+9IHC18nw=; b=BFkwvAndmZ6yNG5egELRFIjzyDH0CLEZH/0pAQru5pG5bal/a9ZlscBhJiL/qG2/Pb Hw1d+OuU7QaTjutmGhp8+zOsUOiTrYvnBsXrWPFqIqH3dpSaVx2jHODIMElzPHbztN1x Y75aGWb0ZKvZptDLf7takFiB9UnPedtfPXSvskRUobiBivZZVS9SFTzlD3z+TKBdwLyJ CoZqmm7okzXrbFQjQo7qnh+glQQrM3Y1dWDNHfsB/Yrz8HMqcfO0+QH5GjSUMZx4OS/E DKDUwfzazLtWo61/TG+p4xCy2kQhGPwS5ifHn+7Hjhm9UPt3axywit3gG6Ia8vzzRyl6 FAKg== X-Gm-Message-State: AOAM530du1IvF56ABgf2/nfh8/W5NxQryW5BlLojfobw/b7Z6qxBAOzM jENhhJV7pmyk5bmSKjqSR70rl6HAQvEpEQ== X-Google-Smtp-Source: ABdhPJzIEXyFtLh5q7KCsHTq74TGMtoVPNO4Ti4qwk5KJ0Bs8JaTkmBl7pSFosxffgkOVsNJDpP1ig== X-Received: by 2002:a5d:6d8a:0:b0:203:6efe:7961 with SMTP id l10-20020a5d6d8a000000b002036efe7961mr20297066wrs.367.1647341863543; Tue, 15 Mar 2022 03:57:43 -0700 (PDT) Received: from localhost.localdomain (217.2.7.51.dyn.plus.net. [51.7.2.217]) by smtp.gmail.com with ESMTPSA id p16-20020adff210000000b001f062b80091sm15038530wro.34.2022.03.15.03.57.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Mar 2022 03:57:43 -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 v3 3/4] terminal: work around macos poll() bug Date: Tue, 15 Mar 2022 10:57:22 +0000 Message-Id: <20220315105723.19398-4-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220315105723.19398-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220315105723.19398-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 bfbde3c1af..89f326adc1 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -87,6 +87,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$" @@ -252,6 +277,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 @@ -402,12 +437,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 Tue Mar 15 10:57:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12781317 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 54988C433F5 for ; Tue, 15 Mar 2022 10:58:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347253AbiCOK7T (ORCPT ); Tue, 15 Mar 2022 06:59:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348053AbiCOK66 (ORCPT ); Tue, 15 Mar 2022 06:58:58 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B813DE0C6 for ; Tue, 15 Mar 2022 03:57:45 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id b19so5992979wrh.11 for ; Tue, 15 Mar 2022 03:57:45 -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=2Dz0Cg11TBTd3hbbW9Jl0JfyYu6ZMfBwRlrVF6zr67E=; b=CbBCtHx4Qy80jshwzDkGfjS0mmIaT7169rHXXj7FBvGqhSAo7q7iYOaQ3pDvgzs1ed TBZ1OOk753XuOhG9O2Qvs2lDHwn1BeD0lBkK97IDh45wedxng4f8Kk3zaY7DpG3XXmWq iJodasGLJYcV2AZdgHbLATtBJr7yHE3ps9j4iKAt/tNC/95IA8ZsVmbtQU3vRNhaG8oh wYlfgMLcKWQVTgHbKf49RGj9zFssEo2JJKy7408GoUdexRi5U+6yF4qH4t9jtP06/zPO KKCL97e7tytW2x8aRjfteUjMmoKnVar/egz7tIdc8LlHPuZwWvkKD5MVosZhOVKJurIw 0+uQ== 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=2Dz0Cg11TBTd3hbbW9Jl0JfyYu6ZMfBwRlrVF6zr67E=; b=L4+Omn1QuT6sLG2mgacZH/iU2uIRFSPxAgUVmk86z/ZD4a/z9s4pe7Eb9NRZ7nYOkk PoAF9PAEnF9Tpqxz3bLqy8nekS64uhibd8WNyUTVAgXDmZ02v+imxZU7HwMxgwNQN/gG htz/Y9bsCcTd+G5ZiAlLVdg+4lOI4ujKzj1gMdgtFaCZt8ElJoL2VSt77XQH0qHpONtP H07G8ZO1tJhKruyVJHsNRKYbFU/IVqovqbOIe/32r/38Iz31Z4CpXXeLaP2xFDTFfvGY boBOtnCw0sta8kkaoQIfd5mbTpnbRudTFeFM1rWj8e17NM9jvjzNpC1Ihn3rZXLO9DKG cXwg== X-Gm-Message-State: AOAM532zXHenYqLj4+1nau5/TRt1xs9sogrkXznwwdmqMneOBxtzgllA NazNo9jkhCv7dS0+0QAOHZgZ5snHKk7y5Q== X-Google-Smtp-Source: ABdhPJyXJrb8lV3HHaGf8eGB/JitYqz1djkutFaTtS0qhKuKyILIiWfJZODHi7WAdgSg8G3lzf0fOA== X-Received: by 2002:a5d:6746:0:b0:203:d6c1:9c5b with SMTP id l6-20020a5d6746000000b00203d6c19c5bmr1615288wrw.446.1647341864321; Tue, 15 Mar 2022 03:57:44 -0700 (PDT) Received: from localhost.localdomain (217.2.7.51.dyn.plus.net. [51.7.2.217]) by smtp.gmail.com with ESMTPSA id p16-20020adff210000000b001f062b80091sm15038530wro.34.2022.03.15.03.57.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Mar 2022 03:57:44 -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 v3 4/4] terminal: restore settings on SIGTSTP Date: Tue, 15 Mar 2022 10:57:23 +0000 Message-Id: <20220315105723.19398-5-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220315105723.19398-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220315105723.19398-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 | 131 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 3 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index 89f326adc1..45a9084bb3 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" @@ -23,6 +23,101 @@ static void restore_term_on_signal(int sig) 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; + +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) @@ -38,10 +133,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 : open("/dev/tty", O_RDWR); @@ -50,6 +148,26 @@ int save_term(enum save_term_flags flags) if (tcgetattr(term_fd, &old_term) < 0) 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; } @@ -72,6 +190,7 @@ static int disable_bits(enum save_term_flags flags, tcflag_t bits) return 0; sigchain_pop_common(); + reset_job_signals(); error: close_term_fd(); return -1; @@ -97,6 +216,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; @@ -106,9 +226,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(); }