Bash install code is not printed dynamically based on TIOCSTI availability + OS (WSL/CYGWIN/...) #478

This commit is contained in:
Martin Dvorak 2023-03-19 19:52:53 +01:00
parent 6d5f50ffb6
commit 07298947a7
4 changed files with 76 additions and 40 deletions

View file

@ -62,6 +62,6 @@ hstrdebug {
QMAKE_LINK = gcc
# TIOCSTI debugging:
# DEFINES += __CYGWIN__
#DEFINES += DEBUG_NO_TIOCSTI
message(DEFINES of hstr.pro build: $$DEFINES)

View file

@ -157,7 +157,7 @@ static const char* HSTR_CASE_LABELS[]={
"sensitive"
};
static const char* INSTALL_BASH_STRING=
static const char* INSTALL_BASH_CODE_PREFIX=
"\n# HSTR configuration - add this to ~/.bashrc"
"\nalias hh=hstr # hh to be alias for hstr"
"\nexport HSTR_CONFIG=hicolor # get more colors"
@ -177,33 +177,7 @@ static const char* INSTALL_BASH_STRING=
// -c -r ... Forces entire .bash_history to be reloaded (handles history deletes, synchronizes different bash sessions)
"\n# ensure synchronization between bash memory and history file"
"\nexport PROMPT_COMMAND=\"history -a; history -n; ${PROMPT_COMMAND}\""
"\n# if this is interactive shell, then bind hstr to Ctrl-r (for Vi mode check doc)"
#if defined(__MS_WSL__)
// IMPROVE commands are NOT executed on return under win10 > consider hstr_utils changes
// Script hints:
// {...} is inline group ~ lambda function whose vars are visible to the other commands
// V=$(c) executes commands and stores it to var V
"\nfunction hstrwsl {"
"\n offset=${READLINE_POINT}"
"\n READLINE_POINT=0"
"\n { READLINE_LINE=$(</dev/tty hstr ${READLINE_LINE:0:offset} 2>&1 1>&$hstrout); } {hstrout}>&1"
"\n READLINE_POINT=${#READLINE_LINE}"
"\n}"
"\nif [[ $- =~ .*i.* ]]; then bind -x '\"\\C-r\": \"hstrwsl\"'; fi"
#elif defined(__CYGWIN__)
"\nfunction hstrcygwin {"
"\n offset=${READLINE_POINT}"
"\n READLINE_POINT=0"
"\n { READLINE_LINE=$(</dev/tty hstr ${READLINE_LINE:0:offset} 2>&1 1>&$hstrout); } {hstrout}>&1"
"\n READLINE_POINT=${#READLINE_LINE}"
"\n}"
"\nif [[ $- =~ .*i.* ]]; then bind -x '\"\\C-r\": \"hstrcygwin\"'; fi"
#else
"\nif [[ $- =~ .*i.* ]]; then bind '\"\\C-r\": \"\\C-a hstr -- \\C-j\"'; fi"
"\n# if this is interactive shell, then bind 'kill last command' to Ctrl-x k"
"\nif [[ $- =~ .*i.* ]]; then bind '\"\\C-xk\": \"\\C-a hstr -k \\C-j\"'; fi"
#endif
"\n\n";
"\n# if this is interactive shell, then bind hstr to Ctrl-r (for Vi mode check doc)";
// zsh doc: http://zsh.sourceforge.net/Guide/zshguide.html
static const char* INSTALL_ZSH_STRING=
@ -408,6 +382,64 @@ void signal_callback_handler_ctrl_c(int signum)
}
}
void print_bash_install_code(void)
{
printf("%s", INSTALL_BASH_CODE_PREFIX);
if(is_tiocsti) {
printf(
"\nif [[ $- =~ .*i.* ]]; then bind '\"\\C-r\": \"\\C-a hstr -- \\C-j\"'; fi"
"\n# if this is interactive shell, then bind 'kill last command' to Ctrl-x k"
"\nif [[ $- =~ .*i.* ]]; then bind '\"\\C-xk\": \"\\C-a hstr -k \\C-j\"'; fi"
"\nexport HSTR_TIOCSTI=y"
);
} else {
printf(
#if defined(__MS_WSL__)
// IMPROVE commands are NOT executed on return under win10 > consider hstr_utils changes
// Script hints:
// {...} is inline group ~ lambda function whose vars are visible to the other commands
// V=$(c) executes commands and stores it to var V
"\nfunction hstrwsl {"
"\n offset=${READLINE_POINT}"
"\n READLINE_POINT=0"
"\n { READLINE_LINE=$(</dev/tty hstr ${READLINE_LINE:0:offset} 2>&1 1>&$hstrout); } {hstrout}>&1"
"\n READLINE_POINT=${#READLINE_LINE}"
"\n}"
"\nif [[ $- =~ .*i.* ]]; then bind -x '\"\\C-r\": \"hstrwsl\"'; fi"
#elif defined(__CYGWIN__)
"\nfunction hstrcygwin {"
"\n offset=${READLINE_POINT}"
"\n READLINE_POINT=0"
"\n { READLINE_LINE=$(</dev/tty hstr ${READLINE_LINE:0:offset} 2>&1 1>&$hstrout); } {hstrout}>&1"
"\n READLINE_POINT=${#READLINE_LINE}"
"\n}"
"\nif [[ $- =~ .*i.* ]]; then bind -x '\"\\C-r\": \"hstrcygwin\"'; fi"
#else
"\nfunction hstrnotiocsti {"
"\n offset=${READLINE_POINT}"
"\n READLINE_POINT=0"
"\n { READLINE_LINE=$(</dev/tty hstr ${READLINE_LINE:0:offset} 2>&1 1>&$hstrout); } {hstrout}>&1"
"\n READLINE_POINT=${#READLINE_LINE}"
"\n}"
"\nif [[ $- =~ .*i.* ]]; then bind -x '\"\\C-r\": \"hstrnotiocsti\"'; fi"
#endif
);
printf("\nexport HSTR_TIOCSTI=n");
}
printf("\n\n");
}
void print_zsh_install_code(void)
{
if(is_tiocsti) {
} else {
}
}
unsigned recalculate_max_history_items(void)
{
int n = getmaxy(stdscr);
@ -1209,6 +1241,7 @@ void stdout_history_and_return(void)
}
// IMPROVE hstr doesn't have to be passed as parameter - it's global static
// TODO rename method _
char* getResultFromSelection(int selectionCursorPosition, Hstr* hstr, char* result) {
if (hstr->promptBottom) {
result=hstr->selection[hstr->promptItems-selectionCursorPosition-1];
@ -1723,7 +1756,7 @@ void hstr_getopt(int argc, char **argv)
if(is_zsh_parent_shell()) {
printf("%s", INSTALL_ZSH_STRING);
} else {
printf("%s", INSTALL_BASH_STRING);
print_bash_install_code();
}
hstr_exit(EXIT_SUCCESS);
break;
@ -1744,7 +1777,11 @@ int hstr_main(int argc, char* argv[])
setlocale(LC_ALL, "");
// initialize global TIOCSTI indicator
#ifdef DEBUG_NO_TIOCSTI
is_tiocsti = false;
#else
is_tiocsti = is_tiocsti_supported();
#endif
hstr=malloc(sizeof(Hstr));
hstr_init();

View file

@ -111,31 +111,30 @@ void hstr_chop(char *s)
}
}
bool is_tiocsti_supported()
bool is_tiocsti_supported(void)
{
#if defined(__MS_WSL__) || defined(__CYGWIN__)
return false;
#else
int fd;
struct termios t;
fd = open("/dev/tty", O_RDWR);
if (fd < 0) {
perror("open /dev/tty");
printf("Error: unable to detect whether TIOCSTI is supported by the kernel");
perror("open /dev/tty");
return false;
}
if (tcgetattr(fd, &t) < 0) {
perror("tcgetattr");
printf("Error: unable to detect whether TIOCSTI is supported by the kernel");
perror("tcgetattr");
return false;
}
bool is_supported = false;
// probe w/o sending characters which would appear @ prompt
if (!ioctl(fd, TIOCSTI, " ")) { // send character to probe
tcflush(fd, TCIOFLUSH); // flush probe character so that it doesn't appear @ prompt
// probe TIOCSTI by sending (and clearing) the character
if (!ioctl(fd, TIOCSTI, " ")) { // send probe character
tcflush(fd, TCIOFLUSH); // flush probe character (avoid insert to prompt)
return true;
}
close(fd);
@ -145,7 +144,7 @@ bool is_tiocsti_supported()
}
#if !defined(__MS_WSL__) && !defined(__CYGWIN__)
void tiocsti()
void tiocsti(void)
{
char buf[] = DEFAULT_COMMAND;
unsigned i;

View file

@ -82,9 +82,9 @@ char* hstr_strdup(const char* s);
int hstr_strlen(const char* s);
char* hstr_strelide(char* buffer, const char* s, unsigned maxlength);
void hstr_chop(char* s);
bool is_tiocsti_supported();
bool is_tiocsti_supported(void);
#ifndef __CYGWIN__
void tiocsti();
void tiocsti(void);
#endif
void fill_terminal_input(char* cmd, bool padding);
void reverse_char_pointer_array(char** array, unsigned length);