From 25df408e3774d478fe1617648953f258a81dc855 Mon Sep 17 00:00:00 2001 From: fatalerrors Date: Tue, 10 Mar 2026 10:53:31 +0100 Subject: [PATCH] implemented ini style parsed configuration file --- profile.conf | 50 +++++++++++++++++ profile.sh | 155 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 147 insertions(+), 58 deletions(-) create mode 100755 profile.conf diff --git a/profile.conf b/profile.conf new file mode 100755 index 0000000..dda9908 --- /dev/null +++ b/profile.conf @@ -0,0 +1,50 @@ +[system] +# System section is used to set system-wide variables, such as PATH, environment variables, and other +# settings that affect the entire system. It is also a good place to set some global variables that can be used by the profile or its functions, such as the default editor and pager. +# Set bash history +HISTSIZE=50000 +HISTIGNORE="&:[bf]g:exit" + +# Set default pager +PAGER=less + +# More colors +TERM=xterm-256color + +[info] +# Default city for weather forcast +DEFAULT_CITY="Toulouse" + +[general] +# General section allow to set any variable that can be used by the profile or its functions. It is +# also a good place to set some global variables that can be used by the profile or its functions, such as +# the compilation flags and make options. +# Set some compiling values +CFLAGS="-O2 -pipe -march=native" +MAKEFLAGS='-j12' +PKGSOURCES='/share/src/archives' + +[aliases] +ll='ls -laFh --color=auto' +la='ls -Ah --color=auto' +l='ls -CF --color=auto' +ls='ls --color=auto' + +grep='grep --color=auto' +egrep='egrep --color=auto' +fgrep='fgrep --color=auto' +qfind="find . -name " + +mkck='make check' +mkin='make install' +mkdin='make DESTDIR=$PWD/dest-install install' +ssh='ssh -Y' + +wget='wget -c' # resume mode by default +myip='curl ip.appspot.com' + +# Human readable by default +df='df -H' +du='du -ch' +sdu='du -sk ./* | sort -n' +hdu='du -hs ./* | sort -H' diff --git a/profile.sh b/profile.sh index f697a51..ddf01a4 100644 --- a/profile.sh +++ b/profile.sh @@ -43,7 +43,6 @@ fi # ------------------------------------------------------------------------------ # path* : private functions for PATH variable management -# ------------------------------------------------------------------------------ pathremove() { local IFS=':' @@ -71,6 +70,96 @@ pathappend() local pathvar=${2:-PATH} export $pathvar="${!pathvar:+${!pathvar}:}$1" } +# ------------------------------------------------------------------------------ + + +# ------------------------------------------------------------------------------ +# Configuration file parser +parse_conf() +{ + local config_file="$1" + local current_section="" + + [[ ! -f "$config_file" ]] && return 1 + + while IFS='=' read -r key value || [[ -n "$key" ]]; do + # Clean key and value (strip CR and whitespace) + key=$(printf '%s' "$key" | tr -d '\r' | xargs 2>/dev/null) + value=$(printf '%s' "$value" | tr -d '\r' | xargs 2>/dev/null) + + # Skip comments and empty lines + [[ -z "$key" || "$key" =~ ^[#\;] ]] && continue + + # Section Detection: [section_name] + if [[ "$key" =~ ^\[(.*)\]$ ]]; then + current_section="${BASH_REMATCH[1]}" + # Dynamically declare the associative array for this section + declare -g -A "CONF_$current_section" + continue + fi + + # If we have a key/value pair and are inside a section + if [[ -n "$current_section" && -n "$value" ]]; then + # Strip quotes from value + value="${value%\"}"; value="${value#\"}" + value="${value%\'}"; value="${value#\'}" + + # Store in the dynamic array: CONF_sectionname[key]=value + eval "CONF_${current_section}['$key']='$value'" + fi + done < "$config_file" +} +# ------------------------------------------------------------------------------ + + +# ------------------------------------------------------------------------------ +# Load command aliases from configuration +load_alias() +{ + local section_name="CONF_$1" + + # Check if the associative array for this section exists + if [[ "$(declare -p "$section_name" 2>/dev/null)" != "declare -A"* ]]; then + return 1 + fi + + # Reference the array keys + eval "local keys=\"\${!$section_name[@]}\"" + + for key in $keys; do + # Fetch the value for this specific key + eval "local cmd=\"\${$section_name[$key]}\"" + + # Portability check: only alias if the command exists + local base_cmd=$(echo "$cmd" | awk '{print $1}') + if command -v "$base_cmd" >/dev/null 2>&1; then + alias "$key"="$cmd" + fi + done +} +# ------------------------------------------------------------------------------ + + +# ------------------------------------------------------------------------------ +# Load configuration values as environment variables +load_conf() +{ + local section_name="CONF_$1" + + if [[ "$(declare -p "$section_name" 2>/dev/null)" != "declare -A"* ]]; then + return 1 + fi + + eval "local keys=\"\${!$section_name[@]}\"" + + for key in $keys; do + eval "local val=\"\${$section_name[$key]}\"" + # Export as a standard shell variable + export "$key"="$val" + done +} +# ------------------------------------------------------------------------------ + # ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------ @@ -101,41 +190,14 @@ export PROFVERSION=$(cat "$MYPATH"/version) if [[ $EUID -eq 0 ]]; then pathappend /sbin:/usr/sbin fi -[[ -d /share/services/gestparc ]] && pathappend /share/services/gestparc [[ -d ~/bin ]] && pathappend ~/bin [[ -d ~/.local/bin ]] && pathappend ~/.local/bin -# ------------------------------------------------------------------------------ -# Default values are set here and will be overloaded with config file if any -# ------------------------------------------------------------------------------ - -# Set bash history -export HISTSIZE=50000 -export HISTIGNORE="&:[bf]g:exit" - -# Set default pager -export PAGER=less - -# More colors -export TERM=xterm-256color - -# Set some compiling values -export CFLAGS="-O2 -pipe -march=native" -export MAKEFLAGS='-j12' -export PKGSOURCES='/share/src/archives' - -# Default city for weather forcast -export DEFAULT_CITY="Toulouse" - -# ------------------------------------------------------------------------------ -# Default values could be altered after this line -# ------------------------------------------------------------------------------ - -# Load global configuration -[[ -f $MYPATH/etc/profile.conf ]] && . $MYPATH/etc/profile.conf - -# Load personal configuration -[[ -f ~/.profile.conf ]] && . ~/.profile.conf +# Parse and load general configuration +export PROFILE_CONF="$MYPATH/profile.conf" +parse_conf "$PROFILE_CONF" +load_conf system # Load Bash system behavior configuration (history, pager, etc.) +load_conf general # General purpose configuration (compilation flags, etc.) # Load module scripts for script in $MYPATH/profile.d/*.sh; do @@ -152,30 +214,7 @@ done if [[ $INTERACTIVE ]]; then # For compiling (as we often compile with LFS/0linux...) #Aliases - alias ll='ls -laFh --color=auto' - alias la='ls -Ah --color=auto' - alias l='ls -CF --color=auto' - alias ls='ls --color=auto' - - alias grep='grep --color=auto' - alias egrep='egrep --color=auto' - alias fgrep='fgrep --color=auto' - alias qfind="find . -name " - - alias mkck='make check' - alias mkin='make install' - alias mkdin='make DESTDIR=$PWD/dest-install install' - alias ssh='ssh -Y' - - alias wget='wget -c' # resume mode by default - alias myip='curl ip.appspot.com' - - # Human readable by default - alias df='df -H' - alias du='du -ch' - - alias sdu='du -sk ./* | sort -n' - alias hdu='du -hs ./* | sort -H' + load_alias aliases # Define PS1 trap 'timer_start' DEBUG @@ -183,7 +222,7 @@ if [[ $INTERACTIVE ]]; then # Set default language setfr - showinfo + showinfo && printf "\n" check_updates -q disp I "Profile version $PROFVERSION chargé..." fi