Changeset 0e4ddfa for jhalfs


Ignore:
Timestamp:
04/13/2019 04:31:24 PM (5 years ago)
Author:
Pierre Labastie <pierre@…>
Branches:
ablfs-more, legacy, trunk
Children:
bc2f591
Parents:
2758d94
Message:

Improve top-level jhalfs based on shellcheck

  • Various improvements from shellcheck around quoting, the use of read, vs $() and other logical comparisons
  • Provide a function for sourcing/loading files so we can remove a lot of duplication. Move loading of some functions from common/common-functions to ./jhalfs for consistency
  • Provide some functions for standardized messaging
  • Remove other duplication in the code, especially checking VERBOSITY
  • Standardize indentation. Two spaces seemed the most prevalent so I went with that.

Patch by Jeremy Huntwork, with slight modifications.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • jhalfs

    r2758d94 r0e4ddfa  
    66
    77# VT100 colors
    8 declare -r  BLACK=$'\e[1;30m'
    9 declare -r  DK_GRAY=$'\e[0;30m'
    10 
    118declare -r  RED=$'\e[31m'
    129declare -r  GREEN=$'\e[32m'
    1310declare -r  YELLOW=$'\e[33m'
     11
     12# shellcheck disable=SC2034
    1413declare -r  BLUE=$'\e[34m'
    15 declare -r  MAGENTA=$'\e[35m'
    16 declare -r  CYAN=$'\e[36m'
    17 declare -r  WHITE=$'\e[37m'
    18 
    1914declare -r  OFF=$'\e[0m'
    2015declare -r  BOLD=$'\e[1m'
    21 declare -r  REVERSE=$'\e[7m'
    22 declare -r  HIDDEN=$'\e[8m'
    23 
    2416declare -r  tab_=$'\t'
    2517declare -r  nl_=$'\n'
    2618
     19# shellcheck disable=SC2034
    2720declare -r   DD_BORDER="${BOLD}==============================================================================${OFF}"
     21# shellcheck disable=SC2034
    2822declare -r   SD_BORDER="${BOLD}------------------------------------------------------------------------------${OFF}"
     23# shellcheck disable=SC2034
    2924declare -r STAR_BORDER="${BOLD}******************************************************************************${OFF}"
    3025
     
    3833simple_error() {        # Basic error trap.... JUST DIE
    3934#-----------------------#
    40   # If +e then disable text output
    41   if [[ "$-" =~ e ]]; then
    42     LASTLINE="$1"
    43     LASTERR="$2"
    44     LASTFUNC="$3"
    45     LASTSOURCE="$4"
    46 #    echo -e "\n${RED}ERROR:${GREEN} basic error trapped!${OFF}\n" >&2
    47     echo -e "\n${RED}ERROR:${GREEN} Error $LASTERR at $LASTSOURCE line ${LASTLINE}!${OFF}\n" >&2
    48   fi
    49   exit $LASTERR
     35  LASTLINE="$1"
     36  LASTERR="$2"
     37  LASTSOURCE="$4"
     38  error_message "${GREEN} Error $LASTERR at $LASTSOURCE line ${LASTLINE}!"
    5039}
    5140
    5241see_ya() {
    53     echo -e "\n${L_arrow}${BOLD}jhalfs${R_arrow} exit${OFF}\n"
     42  printf '\n%b%bjhalfs%b exit%b\n' "$L_arrow" "$BOLD" "$R_arrow" "$OFF"
    5443}
    5544##### Simple error TRAPS
     
    7261#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    7362
     63simple_message() {
     64  # Prevents having to check $VERBOSITY everywhere
     65  if [ "$VERBOSITY" -ne 0 ] ; then
     66    # shellcheck disable=SC2059
     67    printf "$*"
     68  fi
     69}
     70
     71warning_message() {
     72  simple_message "${YELLOW}\\nWARNING:${OFF} $*\\n\\n"
     73}
     74
     75error_message() {
     76  # Prints an error message and exits with LASTERR or 1
     77  if [ -n "$LASTERR" ] ; then
     78    LASTERR=$(printf '%d' "$LASTERR")
     79  else
     80    LASTERR=1
     81  fi
     82  # If +e then disable text output
     83  if [[ "$-" =~ e ]]; then
     84    printf '\n\n%bERROR:%b %s\n' "$RED" "$OFF" "$*" >&2
     85  fi
     86  exit "$LASTERR"
     87}
     88
     89load_file() {
     90  # source files in a consistent way with an optional message
     91  file="$1"
     92  shift
     93  msg="Loading file ${file}..."
     94  [ -z "$*" ] || msg="$*..."
     95  simple_message "$msg"
     96
     97  # shellcheck disable=SC1090
     98  source "$file" 2>/dev/null || error_message "$file did not load"
     99  simple_message "OK\\n"
     100}
     101
    74102version="
    75103${BOLD}  \"jhalfs\"${OFF} builder tool (development) \$Rev$
     
    97125# If the user has not saved his configuration file, let's ask
    98126# if he or she really wants to run this stuff
    99 if [ $(ls -l --time-style='+%Y%m%d%H%M%S' configuration.old | cut -d' ' -f 6) \
    100  -ge $(ls -l --time-style='+%Y%m%d%H%M%S' configuration | cut -d' ' -f 6) ]
     127time_old=$(stat -c '%Y' configuration.old 2>/dev/null)
     128time_current=$(stat -c '%Y' configuration 2>/dev/null)
     129if [ "$(printf '%d' "$time_old")" -ge "$(printf '%d' "$time_current")" ]
    101130  then echo -n "Do you want to run jhalfs? yes/no (yes): "
    102   read ANSWER
    103   if [ x${ANSWER:0:1} = "xn" -o x${ANSWER:0:1} = "xN" ] ; then
     131  read -r ANSWER
     132  if [ "x${ANSWER:0:1}" = "xn" ] || [ "x${ANSWER:0:1}" = "xN" ] ; then
    104133    echo "${nl_}Exiting gracefully.${nl_}"
    105134    exit
     
    110139VERBOSITY=1
    111140
    112 [[ $VERBOSITY > 0 ]] && echo -n "Loading config params from <configuration>..."
    113 source configuration
    114 [[ $? > 0 ]] && echo "file: configuration did not load.." && exit 1
    115 [[ $VERBOSITY > 0 ]] && echo "OK"
     141load_file configuration "Loading config params from <configuration>"
    116142
    117143# These are boolean vars generated from Config.in.
     
    151177
    152178if [[ "${NO_PROGRESS_BAR}" = "y" ]] ; then
    153         NO_PROGRESS="#"
     179# shellcheck disable=SC2034
     180  NO_PROGRESS="#"
    154181fi
    155182
    156183
    157184# Sanity check on the location of $BUILDDIR / $JHALFSDIR
    158 CWD=$(cd `dirname $0` && pwd)
    159 if [[ $JHALFSDIR == $CWD ]]; then
    160     echo " The jhalfs source directory conflicts with the jhalfs build directory."
    161     echo " Please move the source directory or change the build directory."
    162     exit 2
     185CWD="$(cd "$(dirname "$0")" && pwd)"
     186if [[ $JHALFSDIR == "$CWD" ]]; then
     187  echo " The jhalfs source directory conflicts with the jhalfs build directory."
     188  echo " Please move the source directory or change the build directory."
     189  exit 2
    163190fi
    164191
     
    219246
    220247#--- Envars not sourced from configuration
     248# shellcheck disable=SC2034
    221249case $PROGNAME in
    222       clfs ) declare -r GIT="git://git.clfs.org/cross-lfs" ;;
    223       clfs2 ) declare -r GIT="git://git.clfs.org/clfs-sysroot" ;;
    224       clfs3 ) declare -r GIT="git://git.clfs.org/clfs-embedded" ;;
    225       *) declare -r SVN="svn://svn.linuxfromscratch.org" ;;
     250  clfs ) declare -r GIT="git://git.clfs.org/cross-lfs" ;;
     251  clfs2 ) declare -r GIT="git://git.clfs.org/clfs-sysroot" ;;
     252  clfs3 ) declare -r GIT="git://git.clfs.org/clfs-embedded" ;;
     253  *) declare -r SVN="svn://svn.linuxfromscratch.org" ;;
    226254esac
     255
    227256declare -r LOG=000-masterscript.log
    228   # Needed for fetching BLFS book sources when building CLFS
     257# Needed for fetching BLFS book sources when building CLFS
     258# shellcheck disable=SC2034
    229259declare -r SVN_2="svn://svn.linuxfromscratch.org"
    230260
    231261# Set true internal variables
    232262COMMON_DIR="common"
    233 PACKAGE_DIR=$(echo $PROGNAME | tr '[a-z]' '[A-Z]')
     263PACKAGE_DIR=$(echo "$PROGNAME" | tr '[:lower:]' '[:upper:]')
    234264MODULE=$PACKAGE_DIR/master.sh
    235265PKGMNGTDIR="pkgmngt"
    236266# The name packageManager.xml is hardcoded in *.xsl, so no variable.
    237267
    238 [[ $VERBOSITY > 0 ]] && echo -n "Loading common-functions module..."
    239 source $COMMON_DIR/common-functions
    240 [[ $? > 0 ]] && echo " $COMMON_DIR/common-functions did not load.." && exit
    241 [[ $VERBOSITY > 0 ]] && echo "OK"
    242 [[ $VERBOSITY > 0 ]] && echo -n "Loading code module <$MODULE>..."
    243 source $MODULE
    244 [[ $? > 0 ]] && echo "$MODULE did not load.." && exit 2
    245 [[ $VERBOSITY > 0 ]] && echo "OK"
    246 #
    247 [[ $VERBOSITY > 0 ]] && echo "${SD_BORDER}${nl_}"
     268for file in \
     269    "$COMMON_DIR/common-functions" \
     270    "$COMMON_DIR/libs/func_book_parser" \
     271    "$COMMON_DIR/libs/func_download_pkgs" \
     272    "$COMMON_DIR/libs/func_wrt_Makefile" \
     273    "$MODULE" ; do
     274  load_file "$file"
     275done
     276
     277simple_message "${SD_BORDER}${nl_}"
    248278
    249279
    250280#*******************************************************************#
    251 [[ $VERBOSITY > 0 ]] && echo -n "Loading function <func_check_version.sh>..."
    252 source $COMMON_DIR/libs/func_check_version.sh
    253 [[ $? > 0 ]] && echo " function module did not load.." && exit 2
    254 [[ $VERBOSITY > 0 ]] && echo "OK"
    255 
    256 [[ $VERBOSITY > 0 ]] && echo -n "Loading function <func_validate_configs.sh>..."
    257 source $COMMON_DIR/libs/func_validate_configs.sh
    258 [[ $? > 0 ]] && echo " function module did not load.." && exit 2
    259 [[ $VERBOSITY > 0 ]] && echo "OK"
    260 
    261 [[ $VERBOSITY > 0 ]] && echo -n "Loading function <func_custom_pkgs>..."
    262 source $COMMON_DIR/libs/func_custom_pkgs
    263 [[ $? > 0 ]] && echo " function module did not load.." && exit 2
    264 [[ $VERBOSITY > 0 ]] && echo "OK"
    265 
    266 
    267 [[ $VERBOSITY > 0 ]] && echo "${SD_BORDER}${nl_}"
    268 
    269 [[ $VERBOSITY > 0 ]] && echo Checking tools required for jhalfs
     281LASTERR=2
     282for file in \
     283    "$COMMON_DIR/libs/func_check_version.sh" \
     284    "$COMMON_DIR/libs/func_validate_configs.sh" \
     285    "$COMMON_DIR/libs/func_custom_pkgs" ; do
     286  load_file "$file"
     287done
     288unset LASTERR
     289
     290simple_message "${SD_BORDER}${nl_}"
     291simple_message 'Checking tools required for jhalfs'
    270292check_alfs_tools
    271 
    272 [[ $VERBOSITY > 0 ]] && echo "${SD_BORDER}${nl_}"
     293simple_message "${SD_BORDER}${nl_}"
    273294
    274295# blfs-tool envars
    275296BLFS_TOOL=${BLFS_TOOL:-n}
    276297if [[ "${BLFS_TOOL}" = "y" ]] ; then
    277   [[ $VERBOSITY > 0 ]] && echo Checking supplementary tools for installing BLFS
     298  simple_message 'Checking supplementary tools for installing BLFS'
    278299  check_blfs_tools
    279   [[ $VERBOSITY > 0 ]] && echo "${SD_BORDER}${nl_}"
     300  simple_message "${SD_BORDER}${nl_}"
    280301  BLFS_SVN=${BLFS_SVN:-n}
    281302  BLFS_WORKING_COPY=${BLFS_WORKING_COPY:-n}
     
    285306    BLFS_TREE=trunk/BOOK
    286307  elif [[ "${BLFS_WORKING_COPY}" = "y" ]]; then
    287     [[ -d "$BLFS_WC_LOCATION" ]] &&
    288     [[ -d "$BLFS_WC_LOCATION/postlfs" ]] || {
     308    if [[ ! -d "$BLFS_WC_LOCATION/postlfs" ]] ; then
    289309      echo " BLFS tools: This is not a working copy: $BLFS_WC_LOCATION."
    290310      echo " Please rerun make and fix the configuration."
    291311      exit 2
    292       }
    293     BLFS_TREE=$(cd $BLFS_WC_LOCATION; svn info | grep '^URL' | sed 's@.*BLFS/@@')
    294     BLFS_BRANCH_ID=$(echo $BLFS_TREE | sed -e 's@trunk/BOOK@development@' \
    295                                            -e 's@branches/@branch-@' \
    296                                            -e 's@tags/@@' \
    297                                            -e 's@/BOOK@@')
     312    fi
     313    BLFS_TREE=$(cd "$BLFS_WC_LOCATION"; svn info | grep '^URL' | sed 's@.*BLFS/@@')
     314    BLFS_BRANCH_ID=$(echo "$BLFS_TREE" | sed -e 's@trunk/BOOK@development@' \
     315                                             -e 's@branches/@branch-@' \
     316                                             -e 's@tags/@@' \
     317                                             -e 's@/BOOK@@')
    298318  elif [[ "${BLFS_BRANCH}" = "y" ]] ; then
    299319    case $BLFS_BRANCH_ID in
     
    307327    esac
    308328  fi
    309   [[ $VERBOSITY > 0 ]] && echo -n "Loading blfs tools installation function..."
    310   source $COMMON_DIR/libs/func_install_blfs
    311   [[ $? > 0 ]] && echo "function module did not load.." && exit 1
    312   [[ $VERBOSITY > 0 ]] && echo "OK"
     329  load_file "${COMMON_DIR}/libs/func_install_blfs"
    313330fi
    314331
     
    321338echo "${SD_BORDER}${nl_}"
    322339echo -n "Are you happy with these settings? yes/no (no): "
    323 read ANSWER
    324 if [ x$ANSWER != "xyes" ] ; then
     340read -r ANSWER
     341if [ "x$ANSWER" != "xyes" ] ; then
    325342  echo "${nl_}Rerun make to fix the configuration options.${nl_}"
    326343  exit
     
    331348# compare module
    332349if [[ "$COMPARE" = "y" ]]; then
    333   [[ $VERBOSITY > 0 ]] && echo -n "Loading compare module..."
    334   source $COMMON_DIR/libs/func_compare.sh
    335   [[ $? > 0 ]] && echo "$COMMON_DIR/libs/func_compare.sh did not load.." && exit
    336   [[ $VERBOSITY > 0 ]] && echo "OK"
     350  load_file "${COMMON_DIR}/libs/func_compare.sh" 'Loading compare module'
    337351fi
    338352#
    339353# optimize module
    340354if [[ "$OPTIMIZE" != "0" ]]; then
    341   [[ $VERBOSITY > 0 ]] && echo -n "Loading optimization module..."
    342   source optimize/optimize_functions
    343   [[ $? > 0 ]] && echo " optimize/optimize_functions did not load.." && exit
    344   [[ $VERBOSITY > 0 ]] && echo "OK"
     355  load_file optimize/optimize_functions 'Loading optimization module'
    345356  #
    346357  # optimize configurations
    347   [[ $VERBOSITY > 0 ]] && echo -n "Loading optimization config..."
    348   source optimize/opt_config
    349   [[ $? > 0 ]] && echo " optimize/opt_config did not load.." && exit
    350   [[ $VERBOSITY > 0 ]] && echo "OK"
     358  load_file optimize/opt_config 'Loading optimization config'
    351359  # The number of parallel jobs is taken from configuration now
     360  # shellcheck disable=SC2034
    352361  MAKEFLAGS="-j${N_PARALLEL}"
    353362  # Validate optimize settings, if required
     
    362371
    363372  if [[ ! -d $JHALFSDIR ]]; then
    364     sudo mkdir -p $JHALFSDIR
    365     sudo chown $USER:$USER $JHALFSDIR
     373    sudo mkdir -p "$JHALFSDIR"
     374    sudo chown "$USER":"$USER" "$JHALFSDIR"
    366375  fi
    367376
    368377# Create $BUILDDIR/sources even though it could be created by get_sources()
    369378  if [[ ! -d $BUILDDIR/sources ]]; then
    370     sudo mkdir -p $BUILDDIR/sources
    371     sudo chmod a+wt $BUILDDIR/sources
     379    sudo mkdir -p "$BUILDDIR/sources"
     380    sudo chmod a+wt "$BUILDDIR/sources"
    372381  fi
    373382
    374383# Create the log directory
    375384  if [[ ! -d $LOGDIR ]]; then
    376     mkdir $LOGDIR
    377   fi
    378   >$LOGDIR/$LOG
     385    mkdir "$LOGDIR"
     386  fi
     387  true >"$LOGDIR/$LOG"
    379388
    380389# Copy common helper files
    381   cp $COMMON_DIR/{makefile-functions,progress_bar.sh} $JHALFSDIR/
     390  cp "$COMMON_DIR"/{makefile-functions,progress_bar.sh} "$JHALFSDIR/"
    382391
    383392# Copy needed stylesheets
    384   cp $COMMON_DIR/{packages.xsl,chroot.xsl,kernfs.xsl} $JHALFSDIR/
     393  cp "$COMMON_DIR"/{packages.xsl,chroot.xsl,kernfs.xsl} "$JHALFSDIR/"
    385394
    386395# Fix the XSL book parser
    387396  case $PROGNAME in
    388     clfs* ) sed 's,FAKEDIR,'${BOOK}/BOOK',' ${PACKAGE_DIR}/${XSL} > $JHALFSDIR/${XSL} ;;
    389     lfs | hlfs ) sed 's,FAKEDIR,'$BOOK',' $PACKAGE_DIR/$XSL > $JHALFSDIR/${XSL} ;;
     397    clfs* ) sed 's,FAKEDIR,'"${BOOK}/BOOK"',' "${PACKAGE_DIR}/${XSL}" > "${JHALFSDIR}/${XSL}" ;;
     398    lfs | hlfs ) sed 's,FAKEDIR,'"$BOOK"',' "${PACKAGE_DIR}/${XSL}" > "${JHALFSDIR}/${XSL}" ;;
    390399    * ) ;;
    391400  esac
     
    394403# Copy packageManager.xml, if needed
    395404  [[ "$PKGMNGT" = "y" ]] && [[ "$PROGNAME" = "lfs" ]] && {
    396     cp $PKGMNGTDIR/packageManager.xml  $JHALFSDIR/
    397     cp $PKGMNGTDIR/packInstall.sh  $JHALFSDIR/
     405    cp "$PKGMNGTDIR/packageManager.xml" "$JHALFSDIR/"
     406    cp "$PKGMNGTDIR/packInstall.sh" "$JHALFSDIR/"
    398407    }
    399408
    400409# Copy urls.xsl, if needed
    401   [[ "$GETPKG" = "y" ]] && cp $COMMON_DIR/urls.xsl  $JHALFSDIR/
     410  [[ "$GETPKG" = "y" ]] && cp "$COMMON_DIR/urls.xsl" "$JHALFSDIR/"
    402411
    403412# Always create the test-log directory to allow user to "uncomment" test
    404413# instructions
    405   install -d -m 1777 $TESTLOGDIR
     414  install -d -m 1777 "$TESTLOGDIR"
    406415
    407416# Create the installed-files directory, if needed
    408   [[ "$INSTALL_LOG" = "y" ]] && [[ ! -d $FILELOGDIR ]] && install -d -m 1777 $FILELOGDIR
     417  [[ "$INSTALL_LOG" = "y" ]] && [[ ! -d $FILELOGDIR ]] && install -d -m 1777 "$FILELOGDIR"
    409418
    410419# Prepare report creation, if needed
    411420  if [[ "$REPORT" = "y" ]]; then
    412     cp $COMMON_DIR/create-sbu_du-report.sh  $JHALFSDIR/
     421    cp $COMMON_DIR/create-sbu_du-report.sh  "$JHALFSDIR/"
    413422    # After making sure that all looks sane, dump the settings to a file
    414423    # This file will be used to create the REPORT header
    415     validate_config > $JHALFSDIR/jhalfs.config
     424    validate_config >"$JHALFSDIR/jhalfs.config"
    416425  fi
    417426
    418427# Copy optimize files, if needed
    419   [[ "$OPTIMIZE" != "0" ]] && cp optimize/opt_override $JHALFSDIR/
     428  [[ "$OPTIMIZE" != "0" ]] && cp optimize/opt_override "$JHALFSDIR/"
    420429
    421430# Copy compare files, if needed
    422431  if [[ "$COMPARE" = "y" ]]; then
    423     mkdir -p $JHALFSDIR/extras
    424     cp extras/* $JHALFSDIR/extras
     432    mkdir -p "$JHALFSDIR/extras"
     433    cp extras/* "$JHALFSDIR/extras"
    425434  fi
    426435
     
    428437  if [[ "${CUSTOM_TOOLS}" = "y" ]]; then
    429438    echo "Copying custom tool scripts to $JHALFSDIR"
    430     mkdir -p $JHALFSDIR/custom-commands
    431     cp -f custom/config/* $JHALFSDIR/custom-commands
     439    mkdir -p "$JHALFSDIR/custom-commands"
     440    cp -f custom/config/* "$JHALFSDIR/custom-commands"
    432441  fi
    433442
     
    438447  extract_commands
    439448  echo "${SD_BORDER}${nl_}"
    440   cd $CWD # the functions above change directory
     449  cd "$CWD" # the functions above change directory
    441450
    442451# Install blfs-tool, if requested.
    443452  if [[ "${BLFS_TOOL}" = "y" ]] ; then
    444453    echo Installing BLFS book and tools
    445     install_blfs_tools 2>&1 | tee -a $LOGDIR/$LOG
     454    install_blfs_tools 2>&1 | tee -a "$LOGDIR/$LOG"
    446455    [[ ${PIPESTATUS[0]} != 0 ]] && exit 1
    447456  fi
     
    451460# When regenerating the Makefile, we need to know also the
    452461# canonical book version
     462# shellcheck disable=SC2034
    453463if [[ "$REBUILD_MAKEFILE" = "y" ]] ; then
    454464  case $PROGNAME in
    455465    clfs* )
    456       VERSION=$(xmllint --noent $BOOK/prologue/$ARCH/bookinfo.xml 2>/dev/null | grep subtitle | sed -e 's/^.*ion //'  -e 's/<\/.*//') ;;
     466      VERSION=$(xmllint --noent "$BOOK/prologue/$ARCH/bookinfo.xml" 2>/dev/null | grep subtitle | sed -e 's/^.*ion //'  -e 's/<\/.*//') ;;
    457467    lfs)
    458468      if [[ "$INITSYS" = "sysv" ]] ; then
    459         VERSION=$(grep 'ENTITY version ' $BOOK/general.ent| cut -d\" -f2)
     469        VERSION=$(grep 'ENTITY version ' "$BOOK/general.ent" | cut -d\" -f2)
    460470      else
    461         VERSION=$(grep 'ENTITY versiond' $BOOK/general.ent| cut -d\" -f2)
     471        VERSION=$(grep 'ENTITY versiond' "$BOOK/general.ent" | cut -d\" -f2)
    462472      fi
    463473      ;;
    464474    *)
    465       VERSION=$(xmllint --noent $BOOK/prologue/bookinfo.xml 2>/dev/null | grep subtitle | sed -e 's/^.*ion //'  -e 's/<\/.*//') ;;
     475      VERSION=$(xmllint --noent "$BOOK/prologue/bookinfo.xml" 2>/dev/null | grep subtitle | sed -e 's/^.*ion //'  -e 's/<\/.*//') ;;
    466476  esac
    467477fi
     
    473483# Check for build prerequisites.
    474484  echo
    475   cd $CWD
     485  cd "$CWD"
    476486  check_prerequisites
    477487  echo "${SD_BORDER}${nl_}"
Note: See TracChangeset for help on using the changeset viewer.