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, }; /*