diff --git a/README.md b/README.md index 390be4c..085ae06 100755 --- a/README.md +++ b/README.md @@ -170,48 +170,14 @@ if you want to use the capabilities of the _ai-cli_ library, configure your syst to use the Homebrew commands in preference to the ones supplied with macOS. ## Shell startup configuration -You can configure the _ai-cli_ library to be always available in your _bash_ shell by -adding the following lines in your `.bashrc` file. -Adjust the `AI_CLI_LIB` setting to match the _ai-cli_ library installation path; +You can configure the _ai-cli_ library to be always available in your _bash_ +shell by adding the following lines in your `.bashrc` file +(ideally near its beginning for performance reasons). +Adjust the provided path match the _ai-cli_ library installation path; it is currently set for a local installation in your home directory. ```bash -# >>> initialize the ai-cli library >>> - -# Location of the ai-cli shared library; adjust as needed. -AI_CLI_LIB=("$HOME/lib/ai_cli."*) - -# Execute only if configured, installed, and not initialized. -if [[ -r ~/.aicliconfig && -r $AI_CLI_LIB && "$LD_PRELOAD" != *$AI_CLI_LIB* ]] -then - # Set Linux and Cygwin environment variable. - if [ -z "$LD_PRELOAD" ] ; then - export LD_PRELOAD="$AI_CLI_LIB" - else - LD_PRELOAD="$LD_PRELOAD:$AI_CLI_LIB" - fi - - # Set macOS environment variables. - if [ -z "$DYLD_LIBRARY_PATH" ] ; then - export DYLD_LIBRARY_PATH=/opt/homebrew/lib - else - DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH" - fi - if [ -z "$DYLD_INSERT_LIBRARIES" ] ; then - export DYLD_INSERT_LIBRARIES="$AI_CLI_LIB" - else - DYLD_INSERT_LIBRARIES="$LD_PRELOAD:$AI_CLI_LIB" - fi - - # Overlay current bash with a new instance, which will include the required - # environment variables. - if shopt -q login_shell ; then - exec -l bash - else - exec bash - fi - -fi -# <<< initialize the ai-cli library <<< +# Initialize the ai-cli library +source $HOME/share/ai-cli/ai-cli-activate-bash.sh ``` ## Reference documentation diff --git a/src/Makefile b/src/Makefile index a9279eb..1e93d06 100644 --- a/src/Makefile +++ b/src/Makefile @@ -26,6 +26,7 @@ MANPREFIX ?= "$(PREFIX)/share/man/" SHAREPREFIX ?= "$(PREFIX)/share/ai-cli" PROGS=rl_driver $(SHARED_LIB) +ACTIVATION_SCRIPTS=$(wildcard ai-cli-activate-*) RL_SRC=ai_cli.c config.c ini.c fetch_anthropic.c fetch_hal.c fetch_openai.c \ fetch_llamacpp.c support.c TEST_SRC=$(wildcard *_test.c) @@ -113,6 +114,9 @@ install: ai_cli.$(DLL_EXTENSION) # Help: Install library and manual pages install -m 644 ai_cli.5 $(DESTDIR)$(MANPREFIX)/man5 install -m 644 ai_cli.7 $(DESTDIR)$(MANPREFIX)/man7 install -m 644 ai-cli-config $(DESTDIR)$(SHAREPREFIX)/config + for s in $(ACTIVATION_SCRIPTS) ; do \ + sed -e "s|__LIBPREFIX__|$(LIBPREFIX)|" $$s >$(DESTDIR)$(SHAREPREFIX)/$$s ; \ + done help: # Help: Show this help message @echo 'The following make targets are available.' diff --git a/src/ai-cli-activate-bash.sh b/src/ai-cli-activate-bash.sh new file mode 100644 index 0000000..3ffd696 --- /dev/null +++ b/src/ai-cli-activate-bash.sh @@ -0,0 +1,56 @@ +# +# ai_cli - readline wrapper to obtain a generative AI suggestion +# +# Bash commands to activate ai-cli +# +# Copyright 2023-2024 Diomidis Spinellis +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +if [ "$0" != bash -a "$0" != -bash ] ; then + echo 'The ai-cli-lib activation script must be sourced, not executed.' 1>&2 + exit 1 +fi + +AI_CLI_LIB=("__LIBPREFIX__/ai_cli."*) +if [[ $- == *i* # Shell is interactive + && -r ~/.aicliconfig # User has configured script + && -r $AI_CLI_LIB # Library installed + && "$LD_PRELOAD" != *$AI_CLI_LIB* ]] # Variable not set +then + # Linux and Cygwin (is also used for checking) + if [ -z "$LD_PRELOAD" ] ; then + export LD_PRELOAD="$AI_CLI_LIB" + else + LD_PRELOAD="$LD_PRELOAD:$AI_CLI_LIB" + fi + + # macOS + if [ -z "$DYLD_LIBRARY_PATH" ] ; then + export DYLD_LIBRARY_PATH=/opt/homebrew/lib + else + DYLD_LIBRARY_PATH="/opt/homebrew/lib:$DYLD_LIBRARY_PATH" + fi + if [ -z "$DYLD_INSERT_LIBRARIES" ] ; then + export DYLD_INSERT_LIBRARIES="$AI_CLI_LIB" + else + DYLD_INSERT_LIBRARIES="$LD_PRELOAD:$AI_CLI_LIB" + fi + + if shopt -q login_shell ; then + exec -l bash + else + exec bash + fi +fi