~sourcemage/sorcery

da2b9f84a38012485bb7c9189b13698a9345f62f — Thomas Orgis 4 years ago d490f40
libmisc: introduce get_safe_locale and use it in cast, libgpg

This makes sorcery not blindly assume that C.UTF-8 is a valid
locale, which it is not on ancient systems (meaning: stable
Source Mage). Now, a list of preferred UTF-8 locales is matched
against $(locale -a) to avoid the annoying stream of errors.
M usr/sbin/cast => usr/sbin/cast +1 -1
@@ 257,7 257,7 @@ cast_spell()  { (

  # HACK for bug 2910 and 10546
  local saved_lc_all=$LC_ALL
  export LC_ALL=C.UTF-8
  export LC_ALL=$(get_safe_locale)

  export DISTCC_DIR="$SOURCE_DIRECTORY/.distcc"


M var/lib/sorcery/modules/libapi => var/lib/sorcery/modules/libapi +19 -0
@@ 39,6 39,7 @@
#    download_src                   (libsummon)
#    force_depends                  (libdepends)
#    gather_docs                    (build_api/common)
#    get_safe_locale                (libmisc)
#    get_spell_provider             (libstate)
#    get_source_nums                (libmisc)
#    guess_compressor               (libunpack)


@@ 509,6 510,24 @@ get_source_nums() {

#---------------------------------------------------------------------
## @Type API
## @param optional list of preferred locales to test for instead
##        of internal default, in order (one string, space-separated)
##
## @See <@function var.lib.sorcery.modules.libmisc.html,real_get_safe_locale,> for more details.
##
## On a modern system, this should print C.UTF-8 or en_US.UTF-8, falling
## back to C, which always has to be there.
##
## @return 0 if the returned locale is a preferred one (not C fallback)
## @stdout the locale name
#---------------------------------------------------------------------
get_safe_locale() {
    debug "libapi" "get_safe_locale - $*"
    real_get_safe_locale "$@"
}

#---------------------------------------------------------------------
## @Type API
## @param Spell name
## @param Provider name
## @param If empty get the uncommited spell info, if anything else get

M var/lib/sorcery/modules/libgpg => var/lib/sorcery/modules/libgpg +1 -1
@@ 49,7 49,7 @@ gpg_verify_signature() { # $1 sig $2 file $3 pubring $4 algo var
    return 200
  else
    local output=$TMP_DIR/$(smgl_basename $file).gpgout
    LC_ALL=C.UTF-8 gpg --no-default-keyring  \
    LC_ALL="$(get_safe_locale)" gpg --no-default-keyring  \
               --always-trust        \
               --keyring $keyring    \
               --batch               \

M var/lib/sorcery/modules/libmisc => var/lib/sorcery/modules/libmisc +21 -0
@@ 1172,6 1172,27 @@ real_config_query_list() {
}

#---------------------------------------------------------------------
## Print out a safe locale to use, hopefully supporting UTF-8
## The default list of locales to look for can be overridden by
## an optional argument.
## This returns 0 if one of he preferred (UTF-8) locales is found.
## The fallback C is always there.
#---------------------------------------------------------------------
real_get_safe_locale() {
  local sys_locales=$(locale -a)
  local check_locales=${1:-C.UTF-8 C.utf8 en_US.UTF-8 en_US.utf8}
  for l in $check_locales
  do
    if list_find "$sys_locales" "$l"; then
      printf "%s\n" "$l"
      return 0
    fi
  done
  echo C
  return 1
}

#---------------------------------------------------------------------
## Output a list of source numbers associated with the current spell.
## This is the number portion of SOURCE[[:digit:]], eg '', "2", "3", etc.
## A prefix may be given and it will be prepended to each result value.