Path: csiph.com!xmission!news.snarked.org!news.linkpendium.com!news.linkpendium.com!panix!usenet.stanford.edu!not-for-mail From: Eric Pruitt Newsgroups: gnu.bash.bug Subject: Re: Option to disable VSUSP at prompt (feature request with proof of concept) Date: Wed, 3 Oct 2018 00:14:09 -0700 Lines: 110 Approved: bug-bash@gnu.org Message-ID: References: <20161124080520.GA27021@sinister.codevat.com> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="muwxfvyb3pjblu5t" X-Trace: usenet.stanford.edu 1538550859 25905 208.118.235.17 (3 Oct 2018 07:14:19 GMT) X-Complaints-To: action@cs.stanford.edu To: bug-bash@gnu.org Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:pgp-key:user-agent; bh=HmcRJ6ea74v4k4U7iMXfqzjfUH5nHnbunpWoz0ByGfM=; b=lyU0FltSxPBLEVxufrqTHcM20yfmZaHIn/DfyfDL+bmti9LfwWv75eNsx20yfpQRJQ 6TnIv1AEKYPV888Aysjx5EJtOgEiVW8y7OIaxKnaCqZvLaEACt/k7d4h07UsX3kQHVLY TKD9Wy/xqBN9V98/tOjzCZ0uHjxm9zXf3hLDiddDIaG4oQDnEe4HNoZrn+zZTrA4NZKf HenV+YUebpvem/hfqnK8KDTYU3z8KrMREu9JrcwSlNMmP8FnUqTN8qbWL40LHxEMEtPF cypyZMMFQQnry/0eKMKn64eLMg41zJFQmms9jzu1hwJMpZyfhltt1Wj7a037sNMMM9xU Dc6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:references :mime-version:content-disposition:in-reply-to:pgp-key:user-agent; bh=HmcRJ6ea74v4k4U7iMXfqzjfUH5nHnbunpWoz0ByGfM=; b=oPxBJywmLJIFEyMRdrmTH2Z5jDvPBmr/YfphVIJGgpApX8R4vAklo8kNGk9NDEvK/M xx2HNku+fb2qvXl3d8o6HIrVsQjmTfzFwdVHkDV5kL4mPvy56opH/r1ey1XBttrGQqIF EOinuAxGedUjJnFQMg8bzvAGcru9lgS3h44YRttPI4bSHvmQkyX1EVviE1uCI3KEAWaH 20SLS+0M+S1L88Ce5aj42XdEZCJT+GjIlZJXRrgNXzllYpRq2CAIjDmw7XKcM6Za3hWj FKPEnX12szXB9fJr4C2APFt5FdSIFDE4peSwXmaRxaLwa+7paomvW5ApWj3uF3yPcqG3 P6fA== X-Gm-Message-State: ABuFfog1G22JckI5BnbjeJd3dt3VKQtK/ActnmTIlDK7z3Q1IsCtsHgR xgv2vlWa7StxegAG5qmr+C/3tVnG X-Google-Smtp-Source: ACcGV60hSu2mZWTv5zKZsDlLAZdzcewJ6GJeM9mFNP3P+12Zomi4wZ7x4BbgxiUkyHvIARDu5XyI+A== X-Received: by 2002:a62:4b09:: with SMTP id y9-v6mr155303pfa.93.1538550851651; Wed, 03 Oct 2018 00:14:11 -0700 (PDT) Content-Disposition: inline In-Reply-To: <20161124080520.GA27021@sinister.codevat.com> PGP-Key: https://www.codevat.com/pgp.asc#F8601B5D2511B4C3535232488DDDE2E6053692AB User-Agent: NeoMutt/20170113 (1.7.2) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::442 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:14682 --muwxfvyb3pjblu5t Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Nov 24, 2016 at 12:05:20AM -0800, Eric Pruitt wrote: > In my Bash configuration, I have things setup so Ctrl+Z is no longer > translated into a signal at the Bash prompt so it can be remapped. Most > recently, I decided to modify the Bash source to implement this change > in the interpreter because the stty invocations introduced a perceptible > amount of lag on a virtualized OpenBSD host I use. I think this feature > would be a useful default since it usually does not make sense to send > SIGTSTP to a prompt. Here's an accompanying snippet from my inputrc: > > # Allows Ctrl+Z to be used to bring programs back into the > # foreground. The cursor is moved to the beginning of the line > # before typing so a specific job can be resumed by typing its > # identifier (e.g. a number) then hitting Ctrl+Z. This depends on > # Ctrl+Z being a literal sequence i.e. "stty susp undef". > "\C-z": "\C-afg \C-m" This patch had a bug that I recently fixed, and I've attached an updated version. Under certain conditions, ^Z for SIGTSTP would not be re-enabled after executing a command. For example, if "cat /dev/zero > /dev/null" was interrupted by pressing Ctrl+C, pressing Ctrl+Z would not generate a literal ^Z control character at the next prompt. Some debugging revealed that I did not fully understand the control flow of the prompt handling when writing the original patch; control may be resumed at the top of the "reader_loop" function by way of a long lump which bypassed the vsusp toggling code. The bug has been fixed by moving the vsusp toggle logic after the aforementioned long-jump destination. I'm not requesting that this patch be accepted upstream, just posting an updated version for anyone that might interested in this feature. Eric --muwxfvyb3pjblu5t Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="bash-4.4-automatic-susp-toggle.patch" When running commands interactively, modify the terminal attributes so the suspend character is interpreted as literal sequence at the prompt but produces a SIGTSTP signal during command execution. This makes it possible to bind ^Z in readline. For example, adding `"\C-z": "\C-afg \C-m"` to "~/.inputrc" would make it possible to suspend a program using ^Z then pressing ^Z again to bring the program back to the foreground. --- eval.c 2016-06-02 15:49:27.000000000 -0700 +++ eval.c 2016-11-20 15:02:23.681680378 -0800 @@ -31,6 +31,7 @@ #include #include +#include #include "bashintl.h" @@ -69,6 +70,8 @@ int reader_loop () { + struct termios ttyattr; + cc_t vsusp = 0; int our_indirection_level; COMMAND * volatile current_command; @@ -84,6 +87,13 @@ code = setjmp_nosigs (top_level); + if (!vsusp && indirection_level == 1 && interactive && + !tcgetattr(STDERR_FILENO, &ttyattr)) { + vsusp = ttyattr.c_cc[VSUSP]; + ttyattr.c_cc[VSUSP] = 0; + tcsetattr(STDERR_FILENO, TCSADRAIN, &ttyattr); + } + #if defined (PROCESS_SUBSTITUTION) unlink_fifo_list (); #endif /* PROCESS_SUBSTITUTION */ @@ -177,6 +187,13 @@ free (ps0_string); } + if (vsusp) { + ttyattr.c_cc[VSUSP] = vsusp; + if (!tcsetattr(STDERR_FILENO, TCSADRAIN, &ttyattr)) { + vsusp = 0; + } + } + execute_command (current_command); exec_done: @@ -199,6 +216,11 @@ EOF_Reached = EOF; } indirection_level--; + + if (vsusp) { + ttyattr.c_cc[VSUSP] = vsusp; + tcsetattr(STDERR_FILENO, TCSADRAIN, &ttyattr); + } return (last_command_exit_value); } --muwxfvyb3pjblu5t--