From patchwork Wed Mar 9 11:03: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: 12774928 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 47CD5C433FE for ; Wed, 9 Mar 2022 11:04:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231818AbiCILFK (ORCPT ); Wed, 9 Mar 2022 06:05:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229893AbiCILFJ (ORCPT ); Wed, 9 Mar 2022 06:05:09 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CDFA1C6821 for ; Wed, 9 Mar 2022 03:04:10 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id p9so2378800wra.12 for ; Wed, 09 Mar 2022 03:04:10 -0800 (PST) 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=2mqmskBG2Ee9f/l9FxQefhYnj2kroD5St/4wlTzv3PY=; b=U35ivG7bj1Of70E/p49O7fzN//TWo8w6gyKeZWJjZJaTYcxx16TnThUHuFJUlVfnBv 05gENrzFn3EQEP8wA5q6GvPXztbG0qp9nmDeF1EsXdss/PdtA/5AcO35xBfCLleOFnUj 69vY2rRweJBWeP2pHvVjBWOX55qVdlVV+7vUyTNOqsjAg6hi1FIlvfkqGqJMTrhxS4pY 24OEdtc+NUZy55GEQwucpoSINs0OODlNERADOaM1KfYR9oQKbI7YyiAbxKneMH+nkL7d inxvNwQo1+vywZ2Cm1Xf3c9nvKsEQUr7C0zwAniQDHBgnH8UVsXrFmXh34Eu28oQRw1b 8Wtg== 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=2mqmskBG2Ee9f/l9FxQefhYnj2kroD5St/4wlTzv3PY=; b=TLta6T1LHorPdsY0mUEu8DPciACz0JBz9hZg5BGOJhe7Nss5nmm3DE3bfuVM+YrUfV m+X9XO/Gc9a/BHtp6TRzRN4f85dJbbIjQkXknT6D7+IhAl8JmqA6uR2gGyUQyb5e64E6 PEHrqYIcNdt5kU1P3QUIrib6IGB/9HQ9KQOi+/sAWlJ6MVGNBYaUN665TOo2+7zMbmYI rgEAD2jPDN0YokoZqyLM3UhJg0azKPStY+YN7xD6Y58LezJHdj9WXg7TJfbopPxOpl0w zo0I/ZqsIyEbYj84sMVTcezRElSbBcfhdWDI40OQC8Wn0+cvBnTnfC5Aa4nQiUoiWpQ+ H0fg== X-Gm-Message-State: AOAM530ib+OqZ3ATNMdfg83eeWBwDoD/P77d4zlWURrELZiT0g4b4QXp e+L6Gj03QUF+FQMNVE2xHDOSpy5PrhQ= X-Google-Smtp-Source: ABdhPJzlpjFXATZ+BvV3jJ0cfcqq/3VMbgfrGeq7BU+TyfO1zCeg7wNYHOwN/OcSNn5nymcC3WV4BQ== X-Received: by 2002:a5d:6da5:0:b0:1f1:f958:46fa with SMTP id u5-20020a5d6da5000000b001f1f95846famr10032188wrs.622.1646823849436; Wed, 09 Mar 2022 03:04:09 -0800 (PST) Received: from localhost.localdomain (217.2.7.51.dyn.plus.net. [51.7.2.217]) by smtp.gmail.com with ESMTPSA id a17-20020a5d5091000000b001edb61b2687sm2072203wrt.63.2022.03.09.03.04.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Mar 2022 03:04:09 -0800 (PST) From: Phillip Wood To: Git Mailing List Cc: Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= , Carlo Arenas , Johannes Schindelin , Ramsay Jones Subject: [PATCH v2 1/4] terminal: use flags for save_term() Date: Wed, 9 Mar 2022 11:03:22 +0000 Message-Id: <20220309110325.36917-2-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220309110325.36917-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220309110325.36917-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 d882dfa06e..9392b068b1 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..24c4df4c0e 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(unsigned flags); /* Restore the terminal attributes that were saved with save_term() */ void restore_term(void); From patchwork Wed Mar 9 11:03: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: 12774929 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 0EF64C433EF for ; Wed, 9 Mar 2022 11:04:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231824AbiCILFL (ORCPT ); Wed, 9 Mar 2022 06:05:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231801AbiCILFK (ORCPT ); Wed, 9 Mar 2022 06:05:10 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2237C9920 for ; Wed, 9 Mar 2022 03:04:11 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id u10so2392989wra.9 for ; Wed, 09 Mar 2022 03:04:11 -0800 (PST) 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=QuHF8Flg9SuapVM1SspQoc8UUTKP1kNuqZM8jPHoqxI=; b=BwldxxhUYY+q6cFT3uDi3fIHvML4w96899w3uxW1rZKogEydeugMFu3EeHSUR2z5jq LSCj8RxG/3NI2Tix/v8DyaCBGBiEB7Qmx3kzvHDQ4gnAPSLvKJcWjGa2474bbq0AS3Wo FPthswZT6R8oRvJ73eo17VN2eLeF7z33tiNXymF27PmlLugnIPejH9SNDA004htjr7Mb Lp898mpPDgCCp79e+RRthqO1tMeTiB3mz22ikthiUzWiGw8hMp/Liy/hpcLRCtlpsljS Dj0Jh9L4yl+FUSWDYC+s31tYImygVPayo1a5aFdKG4ndtyKDbnx/CG5sp7lHqIDyuxwX ad1Q== 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=QuHF8Flg9SuapVM1SspQoc8UUTKP1kNuqZM8jPHoqxI=; b=bFZ+sGiXxNfT91hiUiKcnhWfwJs3eM/eGu3YURUqASOwrhmFVJBud2k0Tl3AGtmSEY /JsrACjAmLW4sIfTrpbKiRoguUUe8Yl1MpCDBsO0ru3/a8vNuL7iuSwSjCp2Djq/1THb v6FVGKFYFibqGpfeTEL6bovUkFvEPF5QsANlHaudMjrt3Mvh1n0LTce7M0XFY3/QeLrU qGDEel/YEj6qtr0psqtcun9mkK6/+vLezCBP3le3/Y2+8RY1JPeyBMZO/zIfKE1xZADD jRAWwjo9y9O9Umdrf6UdbPWNeepGUNDrxFijvx8Wd4rpX5TQdQOM4FHviDrwInxKVUIN EguQ== X-Gm-Message-State: AOAM5327o8u62oxMpXsNV1yNJpEsY0Sh8TOaFN9/znjH7laQ3uS1a+5X ikl9axqmq+5ie9iFBU8/Up4uaqau/+c= X-Google-Smtp-Source: ABdhPJzbKkRuHZphKN77xuKSgJLuPpdoYWcSxSKXRc3Pincq4teQ5U/s+4He+eo3hqGpIdg29ibRbA== X-Received: by 2002:adf:fcce:0:b0:1f0:2250:65aa with SMTP id f14-20020adffcce000000b001f0225065aamr14747372wrs.628.1646823850181; Wed, 09 Mar 2022 03:04:10 -0800 (PST) Received: from localhost.localdomain (217.2.7.51.dyn.plus.net. [51.7.2.217]) by smtp.gmail.com with ESMTPSA id a17-20020a5d5091000000b001edb61b2687sm2072203wrt.63.2022.03.09.03.04.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Mar 2022 03:04:09 -0800 (PST) From: Phillip Wood To: Git Mailing List Cc: Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= , Carlo Arenas , Johannes Schindelin , Ramsay Jones Subject: [PATCH v2 2/4] terminal: don't assume stdin is /dev/tty Date: Wed, 9 Mar 2022 11:03:23 +0000 Message-Id: <20220309110325.36917-3-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220309110325.36917-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220309110325.36917-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 | 5 +++-- compat/terminal.h | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index 9392b068b1..cb653419c3 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -37,7 +37,8 @@ void restore_term(void) 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) @@ -362,7 +363,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 24c4df4c0e..3217611b08 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 9 11:03:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12774930 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 D8B71C433F5 for ; Wed, 9 Mar 2022 11:04:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231847AbiCILFV (ORCPT ); Wed, 9 Mar 2022 06:05:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42754 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231815AbiCILFK (ORCPT ); Wed, 9 Mar 2022 06:05:10 -0500 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 8DA33CC50D for ; Wed, 9 Mar 2022 03:04:12 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id r9-20020a1c4409000000b00389d0a5c511so74681wma.5 for ; Wed, 09 Mar 2022 03:04:12 -0800 (PST) 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=Y0NjIS+CY1bRy5mGkZTuI5JlDYUm77x/O9lkeLYyxKs=; b=fZ/SLpob6wWgoZ6IgTWWFlQS4OwCP979tHaSvBYGnYAe7StnzFhGbeL/ZBDRbgJmWo l9321imsHKSnM8cFj+OrK+cSdyrYFJkjIpJsFRW/VgamaeV/m2vaGxQ/5mdUsVcTkk/H Tsevm1k4z3R8oJKaPHRNjMqhrcSNVI+b7u5BHIEVrYijKtolrOl0Ri3e2BGFVM2wK1Br nGKzVTZH8N1+6mgoalyOCUd/ku4100uHhopZEFY8/x7CexlRXWwHzc2w8XnFoUsaUpvf cPCPZKtyCKcqEhr8Z7jU5K6ogNZKJGI0nP5/i062kiOFRGeviAaxsPZURPRWlYFTGRzo nELw== 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=Y0NjIS+CY1bRy5mGkZTuI5JlDYUm77x/O9lkeLYyxKs=; b=veo7gw9SGMQbfKbcC+esZCuAUGdBXqZTpvYdptISV4C+LS1+E1A6drE49t3YCQM5XK KUoA8m3LKUz+2TBBya+1leOnic7mtV8gU8E7zM3YmgmBKEwezHTK5VH42sz4Gs1P9f2G ngkuPZagB9ZweEGFRfQOrCl1GE1AlNUeR/jEuTAk8gBW+bl7k36KnWNT3ecVQkl4KrG2 vS0jdyImElHCnQNiKyZKGmnVsDepSgjGsHWfl20GczFVbNYHvFa8dz5P4cCu8Bnh/yWj X3whgpbCMhBUV7jSSUCLkZdR/G2oCpFp3OyajX4udMZV6uYf8YiSSkIyyb/XZCNCX+AZ kskg== X-Gm-Message-State: AOAM5325Cas2NUSY2TUeSEemGNXmuyiAXtvFAZzCFuEcEWI5wpLp/3EB 1g2xKnWwv3gnTO0PaI06ezpuDWp+ipg= X-Google-Smtp-Source: ABdhPJwcpg9JZ+mmK4oQP+2LnPIIK8yohqLOZDJBO1HJF0xgkk0xeWwVmBg/p4zG/uSLNOVPsn2yIA== X-Received: by 2002:a05:600c:4202:b0:382:a5d7:9ce5 with SMTP id x2-20020a05600c420200b00382a5d79ce5mr2845225wmh.45.1646823850957; Wed, 09 Mar 2022 03:04:10 -0800 (PST) Received: from localhost.localdomain (217.2.7.51.dyn.plus.net. [51.7.2.217]) by smtp.gmail.com with ESMTPSA id a17-20020a5d5091000000b001edb61b2687sm2072203wrt.63.2022.03.09.03.04.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Mar 2022 03:04:10 -0800 (PST) From: Phillip Wood To: Git Mailing List Cc: Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= , Carlo Arenas , Johannes Schindelin , Ramsay Jones Subject: [PATCH v2 3/4] terminal: work around macos poll() bug Date: Wed, 9 Mar 2022 11:03:24 +0000 Message-Id: <20220309110325.36917-4-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220309110325.36917-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220309110325.36917-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 | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index cb653419c3..4189cbb12c 100644 --- a/compat/terminal.c +++ b/compat/terminal.c @@ -82,6 +82,32 @@ 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. + */ +#include +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$" @@ -247,6 +273,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 @@ -397,12 +433,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) return 0; strbuf_addch(buf, ch); From patchwork Wed Mar 9 11:03:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phillip Wood X-Patchwork-Id: 12774931 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 6C1CBC433F5 for ; Wed, 9 Mar 2022 11:04:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231901AbiCILFY (ORCPT ); Wed, 9 Mar 2022 06:05:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42910 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231801AbiCILFM (ORCPT ); Wed, 9 Mar 2022 06:05:12 -0500 Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D087CE902 for ; Wed, 9 Mar 2022 03:04:13 -0800 (PST) Received: by mail-wm1-x32b.google.com with SMTP id m42-20020a05600c3b2a00b00382ab337e14so3110208wms.3 for ; Wed, 09 Mar 2022 03:04:13 -0800 (PST) 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=gBBWGGf+ajwLZb9/2dlAU59Gd3RM6T/+YGhGYuEGcFI=; b=SGH3xrGXjcUfETq9gM9OSxiEmCVw4/uGlBPlemg/nIj9n27dIJuOkEZcESHrN816HT lHOL5WQ5dyFTM2fyiD/CRRYApmPnHqAsFlUUq/q3KvoHERFzhQb9LHp1gpvyrsb49+E/ mxe9xH+3vmxLJ6B2h738uRKgxzjpncI+KfRiYY3sujyMryFe43UnfoVx506p5OV75Fp4 /RCJ/Ouv9IzK/ISM4Boj399dF6orZ5pMJIH/+3kb/mzF0cGbwZXxyFj9t8A4dbixvXa8 tHBwzr+wiRz5ezoKl5rdUJFjUsSlmlnvIRZs00LiS4SCzsghRwVLzY83cDr/i6jau76m OO4Q== 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=gBBWGGf+ajwLZb9/2dlAU59Gd3RM6T/+YGhGYuEGcFI=; b=MMMGPRXAv+KP8+vajJWeSi3qoiB2n5QDL/WUNynH2oBqgB1t5mPW/laH6OAoJWuRz1 tX5B+KZANwPhaG1y0qt4vJ/JnLMAnWWYrtfQBwWqAdNGv1vp0lzc+hy1RRUYgFG3p/ts brtFvf2kqNRXxggppPIFe+DgmCKuY/j5nmLQJDNu4T03OOFiRULB44z2c6ff4Sf1WsBB h0qumnLxg0/TuqfXb9dK0FCLMxurNQtlf2wlJKvpdrK+0taNpQuIZrVTcuryccD09r1M BrJtTAtWGAtmBr1ZeJhN59hZ3E/VFByW0ni7ytQeG6OI2l9Sxc1Un00/EmjwxdBDOZq6 1TmQ== X-Gm-Message-State: AOAM531+XjEDeYU1mKiNXKquRgeejw+KaYQdCbKTSCzE6RjqVeAhuUPI 8+IBlDdH6kpP9dx3OPmeI1EsYCgQSZk= X-Google-Smtp-Source: ABdhPJyj9Dd1Mh8Av/kLjTfxgcMujsHa+r3viBBW3OhKlZwEHUBraXkA2fRKzqMvL99B5avMx8kOug== X-Received: by 2002:a05:600c:2056:b0:389:736a:5631 with SMTP id p22-20020a05600c205600b00389736a5631mr7225937wmg.120.1646823851707; Wed, 09 Mar 2022 03:04:11 -0800 (PST) Received: from localhost.localdomain (217.2.7.51.dyn.plus.net. [51.7.2.217]) by smtp.gmail.com with ESMTPSA id a17-20020a5d5091000000b001edb61b2687sm2072203wrt.63.2022.03.09.03.04.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Mar 2022 03:04:11 -0800 (PST) From: Phillip Wood To: Git Mailing List Cc: Phillip Wood , =?utf-8?b?w4Z2YXIgQXJuZmo=?= =?utf-8?b?w7Zyw7AgQmphcm1hc29u?= , Carlo Arenas , Johannes Schindelin , Ramsay Jones Subject: [PATCH v2 4/4] terminal: restore settings on SIGTSTP Date: Wed, 9 Mar 2022 11:03:25 +0000 Message-Id: <20220309110325.36917-5-phillip.wood123@gmail.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220309110325.36917-1-phillip.wood123@gmail.com> References: <20220304131126.8293-1-phillip.wood123@gmail.com> <20220309110325.36917-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 | 125 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 121 insertions(+), 4 deletions(-) diff --git a/compat/terminal.c b/compat/terminal.c index 4189cbb12c..f425f7b339 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,90 @@ 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; +} + void restore_term(void) { if (term_fd < 0) @@ -32,10 +116,19 @@ void restore_term(void) close(term_fd); term_fd = -1; sigchain_pop_common(); + if (restore_error_msg) { + signal(SIGTTIN, SIG_DFL); + signal(SIGTTOU, SIG_DFL); + signal(SIGTSTP, SIG_DFL); + restore_error_msg = NULL; + background_resume_msg = NULL; + } } 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); @@ -44,6 +137,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; } @@ -93,6 +206,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; @@ -102,9 +216,12 @@ static int getchar_with_timeout(int timeout) FD_ZERO(&readfds); FD_SET(0, &readfds); res = select(1, &readfds, NULL, NULL, tvp); - if (res < 0) - return EOF; - + if (res < 0) { + if (errno == EINTR) + goto again; + else + return EOF; + } return getchar(); }