Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preexec doesn't work for function declarations #6

Open
evverx opened this issue Jun 17, 2015 · 34 comments
Open

preexec doesn't work for function declarations #6

evverx opened this issue Jun 17, 2015 · 34 comments

Comments

@evverx
Copy link
Contributor

evverx commented Jun 17, 2015

$ source .bash-preexec.sh
$ preexec() { echo "just typed $1"; }
$ date
just typed date
Wed Jun 17 16:20:15 UTC 2015
$ function foo() { echo foo; }
$ foo
just typed foo
foo
$ (date)
Wed Jun 24 02:42:48 UTC 2015
@rcaloras
Copy link
Owner

Hey thanks for the feedback! This is a known limitation along with subshell parsing e.g. doing something like

(ls)

I have a branch in progress for this fix, but it's not too easy. https://github.com/rcaloras/bash-preexec/tree/SubShellParsing.

It actually takes a decent amount of time for bash to handle this case and I believe it creates some lag. Would love your contribution if you can help figure it out. I'll give it another look tonight as well.

@evverx
Copy link
Contributor Author

evverx commented Jun 17, 2015

@rcaloras , thanks. I'll read https://github.com/rcaloras/bash-preexec/tree/SubShellParsing this evening:)

@evverx
Copy link
Contributor Author

evverx commented Jun 17, 2015

Reminder for me:

$ source .bash-preexec.sh
$ preexec() { echo "just typed $1"; }
$ date
just typed date
Wed Jun 17 17:07:28 UTC 2015
$ <SPACE>who
just typed date
vagrant  pts/1        2015-06-17 17:06 (10.0.2.2)

rcaloras added a commit that referenced this issue Jun 18, 2015
@rcaloras
Copy link
Owner

That should address the space issue :) Not sure about functions yet though.

@evverx
Copy link
Contributor Author

evverx commented Jun 18, 2015

@rcaloras , thanks:)
But man bash says:

A value of ‘ignoreboth’ is shorthand for ‘ignorespace’ and ‘ignoredups’

Overwriting HISTCONTROL is privacy issue:)

@rcaloras
Copy link
Owner

Yeah it should also replace ignoreboth with just ignoredups.

Why is overwriting histcontrol a privacy issue? This plugin overwrites both bash's DEBUG trap and PROMPT_COMMAND IMO that's more invasive :P

@evverx
Copy link
Contributor Author

evverx commented Jun 18, 2015

that's the joke:)

Reminder for me:
Some useful variables and options:

     set -o history enable command history

     HISTSIZE
              The number of commands to remember in the command  history.
              If  the value is 0, commands are not saved in
              the history list.  Numeric values less than zero result in every
              command  being  saved  on  the history list (there is no limit).
              The shell sets the  default  value  to  500  after  reading  any
              startup files.

     HISTIGNORE
              A colon-separated list of patterns used to decide which  command
              lines  should  be  saved  on  the history list.

rcaloras added a commit that referenced this issue Jun 21, 2015
-Replace ignoreboth with simpley ignoredups
@rcaloras
Copy link
Owner

Added a commit to handle ignoreboth.

@rcaloras
Copy link
Owner

Could probably add a few more checks to make sure history is properly enabled. Something like

history_check=$(set -o | grep history)
if [[ "$history_check" == "history off" ]]; then
   echo "Enable history to use bash preexec"
   return 1
fi;

@rcaloras
Copy link
Owner

I think it might be easier to get functions by checking changes in the bash history then investigating things like sub shell parsing.

@evverx
Copy link
Contributor Author

evverx commented Jun 22, 2015

@rcaloras , thanks.

Could probably add a few more checks to make sure history is properly enabled

Too many checks: history on, HISTSIZE, history-size in ~/.inputrc...

I vote for auto enabling:)

I've collected some useful settings:

# enable access to the command history
set -o history

# save each line of a multi-line command in the same history entry
shopt -s cmdhist

# save the command with embedded newlines instead of semicolons
shopt -s lithist

# save lines which begin with a space character in the history list
# preserve dups!
HISTCONTROL= # already done

# save all lines in the history list
HISTIGNORE=

# every command being saved on the history list (there is no limit)
HISTSIZE=-1
bind 'set history-size -1'

@evverx
Copy link
Contributor Author

evverx commented Jun 22, 2015

Another history problem is HISTTIMEFORMAT

$ source .bash-preexec.sh
$ preexec() { echo "just typed $1"; }
$ HISTTIMEFORMAT='%F %T '
just typed HISTTIMEFORMAT='%F %T '
$ echo WAT
just typed 2015-06-22 04:56:43 echo WAT

Possible solution:

local this_command="$(HISTTIMEFORMAT= history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g")";

@rcaloras
Copy link
Owner

Want to submit a pull request for that one? Would love to get you some contributor credit :) Feel free to open it as a separate issue if you'd like so we can close it specifically.

@evverx
Copy link
Contributor Author

evverx commented Jun 23, 2015

Fixed:)

@evverx evverx changed the title preexec doesn't work for functions preexec doesn't work for function declarations and (list) compound commands Jun 24, 2015
rcaloras added a commit that referenced this issue Jun 28, 2015
- Watches for history changes to properly invoke preexec
  function. Handles duplicates and shell startup edge cases.

- Still a bit of a hack since it's not truly prexection (i.e. will come after
  the function or subshell is executed) but the only way I
  found in bash to get prexec to invoke for both functions and subshells.
@rcaloras
Copy link
Owner

@evverx I created a viable work around that's based on history changes to detect sub shell and function executed commands.

Give it a look. I'm testing it out now. I'll merge to master and cut a release if I find it's working fine.

@rcaloras
Copy link
Owner

Also moved your history related suggestions to #10. Thanks!

@evverx
Copy link
Contributor Author

evverx commented Jun 29, 2015

Wow!
@rcaloras , thanks.
I'll test this change tomorrow.

@rcaloras
Copy link
Owner

rcaloras commented Jul 8, 2015

Bump! @evverx I'm going to merge and cut a version if you're cool with this guy. I've been using it locally for a few days now on my installation of bashhub and all seems good.

@evverx
Copy link
Contributor Author

evverx commented Jul 8, 2015

@rcaloras , sorry.
There is a problem:

git clone https://github.com/rcaloras/bash-preexec
cd bash-preexec/
git checkout --track origin/FunctionAndSubshells
source bash-preexec.sh
preexec() { echo "just typed $1"; }

function wat { echo wat; } # outputs just typed 'function wat { echo wat; }'. It's OK!
function wat { echo wat; } # nothing here! It's not OK

(date) # outputs just typed '(date)'. It's OK.
(date) # nothing here. It's not OK.

date # outputs just typed 'date'
date # outputs just typed 'date'

@rcaloras
Copy link
Owner

So here's the problem. Function declarations and subshell commands don't invoke the DEBUG trap. So I instead monitor changes to the history file and invoke preexec when a changes occurs. As such we'd also need to remove ignoring duplicates to detect that someone has declared the same function twice or entered the same subshell command twice.

Although I removed the ignoring for white spaces by default, I'm not sure I'm comfortable with removing ignore duplicates :/

@isp5
Copy link

isp5 commented Aug 14, 2015

I have a question that seems to be related to some of this discussion. While using this script, the history does not store duplicate commands. Is there any particular reason for that? Forgive me if this is self-explanatory, I possibly haven't looked at it enough to figure it out on my own.

@evverx
Copy link
Contributor Author

evverx commented Aug 15, 2015

Hi, @isp5 .
You have ignoreboth or ignoredups in the HISTCONTROL variable.
Try HISTCONTROL=.

@isp5
Copy link

isp5 commented Aug 17, 2015

Thanks! Any particular reason the history was set like that?

@rcaloras
Copy link
Owner

bash-preexec shouldn't change that aspect of your HISTCONTROL. It will alter it to not ignore whitespace. Ignoredups or ignoreboth were probably set by something else, but if I'm mistaken please let me know :)

@rcaloras
Copy link
Owner

@gnachman Thanks for the input!

I removed those lines during the original rewrite as I was never able to get them to work correctly. I added them back on the SubShellParsing branch and got it sort of working, but ran into issues with the PS1 variable.

@rcaloras
Copy link
Owner

Yeah looking back through my comments. It failed on things like prompts that include environment information (e.g. git branch, virtual env)

@gnachman
Copy link

I'm using a weird series of hacks on top of the old twistedmatrix code but it behaves itself with a PS1 like this (i.e., preexec and precmd run at the right time):

export PS1='$(git branch) > '

Is that what you meant about prompts that include environment information?

@rcaloras
Copy link
Owner

rcaloras commented Mar 29, 2016

@gnachman Yeah exactly. I'm looking through your code now. I'll definitely try to recreate it. Looks like you're doing some interesting stuff with PS1 to work around it e.g. https://github.com/gnachman/iterm2-website/blob/master/source/misc/bash_startup.in#L62-L83.

I actually just had some users of Bashhub try to use Iterm2 + Shell Integration and it failed because we're both trying to install our own versions of preexec :(

I'd love to fix up this project to make it compatible with the awesome iTerm2 :) I wrote bash-preexec to include function arrays and to avoid multiple inclusion which allows multiple shell integrations to add preexec functions triggered through one debug trap hook. Any chance you'd be willing to switch to this library if I make it compatible?

@gnachman
Copy link

@rcaloras I would love to not own the preexec code. It hurts my head. The main difficulty is ensuring it works on the ancient version of bash that ships with Mac OS, and the weird hack for CentOS 7.2.

@rcaloras
Copy link
Owner

@gnachman Awesome, would love to take it off your hands. I'm traveling over the next few weeks, but will take a look at aligning mine with some of your changes when I get back. Thanks! 👍

rcaloras added a commit that referenced this issue Apr 18, 2016
- Addresses part of #6
- Still doesn't support functions.
- Also expose the correct exit code to preexec.
@rcaloras
Copy link
Owner

@evverx Addressed the issue for subshells by looking at some of the code from @gnachman. [[ ! -t 1 && -z ]] was the main thing I was was missing 👍

Not positive there's a solution for functions :/ I'm going to release this and another change. Give it a try if you get a chance. If everything looks good I'll rename this issue to just address function declarations.

@evverx
Copy link
Contributor Author

evverx commented Apr 24, 2016

@rcaloras , works fine! Thanks!

$ git clone https://github.com/rcaloras/bash-preexec
$ source bash-preexec/bash-preexec.sh
$ preexec() { echo "just typed $1"; }
$ (date)
just typed (date)
Sun 24 Apr 08:56:31 UTC 2016

But I don't really understand how:)

@rcaloras rcaloras changed the title preexec doesn't work for function declarations and (list) compound commands preexec doesn't work for function declarations May 1, 2016
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Jul 16, 2019
In 3458480

    Remove ignorespace from $HISTCONTROL

and in 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Jul 16, 2019
In 3458480

    Remove ignorespace from $HISTCONTROL

and in 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Jul 16, 2019
In 3458480

    Remove ignorespace from $HISTCONTROL

and in 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Jul 16, 2019
In 3458480

    Remove ignorespace from $HISTCONTROL

and in 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Jul 16, 2019
After 3458480

    Remove ignorespace from $HISTCONTROL

and after 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Jul 16, 2019
After 3458480

    Remove ignorespace from $HISTCONTROL

and after 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Mar 11, 2020
After 3458480

    Remove ignorespace from $HISTCONTROL

and after 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
gaelicWizard added a commit to gaelicWizard/bash-preexec that referenced this issue Jul 28, 2021
After 3458480

    Remove ignorespace from $HISTCONTROL

and after 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Oct 23, 2021
After 3458480

    Remove ignorespace from $HISTCONTROL

and after 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Oct 23, 2021
After 3458480

    Remove ignorespace from $HISTCONTROL

and after 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
Repository owner deleted a comment from rafly2103 Feb 15, 2024
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Feb 19, 2024
After 3458480

    Remove ignorespace from $HISTCONTROL

and after 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
ilya-bobyr added a commit to ilya-bobyr/bash-preexec that referenced this issue Feb 19, 2024
After 3458480

    Remove ignorespace from $HISTCONTROL

and after 7e55ac1

    Follow up commit for issue rcaloras#6

    -Replace ignoreboth with simpley ignoredups

this script would remove 'ignorespace' and would replace 'ignoreboth'
with 'ignoredups'.  This effectively disables the functionality of not
adding space prefixed commands into history.  It used to happen
siliently and could be quite confusing to users who use this feature.

This script relies on the command to be in the history, but we can
mostly fix the issue by "manual" removing a whitespace prefixed command
from the history after reading it from there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants