Skip to content

Commit

Permalink
Disable broken sourcing, provide self-exec as a way to load instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
tareksander committed Oct 9, 2022
1 parent 1dbe9c3 commit 02049c0
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ source **tgui-bash**
## Description
Sets up a connection to the Termux:GUI plugin through a small C helper, defines functions to interact with the plugin and finally sources the parameter into the script, so the functions are available.

Can be used in a shebang (`#!/bin/tgui-bash`) or sourced (`. tgui-bash --`).
Can be used in a shebang (`#!/bin/tgui-bash`) or a script can self-exec (`! [ -v tgc_activity_tid ] && exec tgui-bash "${BASH_SOURCE[0]}" "$@"`).

Using `set -eo pipefail` is advised to make your script exit when the connection to the plugin gets broken.
For development, you should use `set -u` to make sure you spelled the constant names correctly.
Expand Down
2 changes: 1 addition & 1 deletion TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The full source code can also be found in the tutorial folder.

To use the library, you have 2 options:
- Use the shebang `#!/bin/tgui-bash` instead of `#!/bin/bash`. The library will initialize itself and then load your script.
- Source the library with `. tgui-bash --`.
- Exec tgui-bash with your script path as argument when tgui-bash is not loaded: `! [ -v tgc_activity_tid ] && exec tgui-bash "${BASH_SOURCE[0]}" "$@"`.

The library exits when your script exits, and all remaining Activities are cleaned up by the Plugin after that.

Expand Down
Binary file modified doc_html/manual-dark.html.gz
Binary file not shown.
Binary file modified doc_html/manual-light.html.gz
Binary file not shown.
Binary file modified doc_html/tutorial-dark.html.gz
Binary file not shown.
Binary file modified doc_html/tutorial-light.html.gz
Binary file not shown.
21 changes: 10 additions & 11 deletions src/tgui-bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ if [ $# -ge 1 ] && [ "$1" = "--" ]; then
shift
fi

if [ $# -lt 1 ] && ! (return 0 2>/dev/null); then
echo 'Usage: tgui-bash path ...' >&2
if (return 0 2>/dev/null); then
echo 'tgui-bash does not support sourcing' >&2
exit 1
fi

if [ $# -ne 0 ] && (return 0 2>/dev/null); then
echo 'When sourced, tgui-bash needs no arguments' >&2
if [ $# -lt 1 ] && ! (return 0 2>/dev/null); then
echo 'Usage: tgui-bash path ...' >&2
exit 1
fi


if [ $# -ge 1 ] && [ "$1" = "-h" ]; then
if [ "$2" = 0 ]; then
docpath="@TERMUX_PREFIX@/share/tgui-bash/manual"
Expand Down Expand Up @@ -1204,7 +1205,7 @@ trap 'set +u; kill -9 $tg__main_PID >/dev/null 2>&1; kill -9 $tg__event_PID >/de



# start listening on teh sockets
# start listening on the sockets
coproc tg__main { $helper --main "$sock_main" ; }

# redirect stderr, save it and restore it, to suppress the warning for running 2 coprocs
Expand All @@ -1224,13 +1225,11 @@ fi

# clear up variables
unset helper
unset am_command
unset sock_main
unset sock_event


# Run user script if not sourced
if ! (return 0 2>/dev/null); then
# shellcheck disable=SC1090
. "$@"
fi
tg__user_script="$1"
shift
# shellcheck disable=SC1090
. "$tg__user_script" "$@"

0 comments on commit 02049c0

Please sign in to comment.