OSDN Git Service

[add] : Use locale.sh for parse locale files
authorhayao <shun819.mail@gmail.com>
Fri, 20 Nov 2020 09:56:56 +0000 (18:56 +0900)
committerhayao <shun819.mail@gmail.com>
Fri, 20 Nov 2020 09:56:56 +0000 (18:56 +0900)
lfbs
tools/locale.sh [new file with mode: 0755]
tools/msg.sh [new file with mode: 0755]

diff --git a/lfbs b/lfbs
index d55b46a..7ef91af 100755 (executable)
--- a/lfbs
+++ b/lfbs
@@ -270,35 +270,7 @@ make_basefs() {
 
 # Parse files
 parse_files() {
-    #-- ロケールを解析、設定 --#
-    local _get_locale_line_number _locale_config_file _locale_name_list _locale_line_number _locale_config_line
-
-    # 選択されたロケールの設定が描かれた行番号を取得
-    _locale_config_file="${script_path}/system/locale-${arch}"
-    _locale_name_list=($(cat "${_locale_config_file}" | grep -h -v ^'#' | awk '{print $1}'))
-    _get_locale_line_number() {
-        local _lang _count=0
-        for _lang in ${_locale_name_list[@]}; do
-            _count=$(( _count + 1 ))
-            if [[ "${_lang}" == "${locale_name}" ]]; then echo "${_count}"; return 0; fi
-        done
-        echo -n "failed"
-    }
-    _locale_line_number="$(_get_locale_line_number)"
-
-    # 不正なロケール名なら終了する
-    [[ "${_locale_line_number}" == "failed" ]] && msg_error "${locale_name} is not a valid language." "1"
-
-    # ロケール設定ファイルから該当の行を抽出
-    _locale_config_line=($(cat "${_locale_config_file}" | grep -h -v ^'#' | grep -v ^$ | head -n "${_locale_line_number}" | tail -n 1))
-
-    # 抽出された行に書かれた設定をそれぞれの変数に代入
-    # ここで定義された変数のみがグローバル変数
-    locale_name="${_locale_config_line[0]}"
-    locale_gen_name="${_locale_config_line[1]}"
-    locale_version="${_locale_config_line[2]}"
-    locale_time="${_locale_config_line[3]}"
-    locale_fullname="${_locale_config_line[4]}"
+    eval $(bash "${script_path}/tools/locale.sh" -s -a "${arch}" get "${locale_name}")
 }
 
 prepare_build() {
@@ -516,8 +488,8 @@ make_checksum() {
 # 引数解析()
 # 参考記事:https://0e0.pw/ci83 https://0e0.pw/VJlg
 
-_opt_short="w:l:o:ha:-:m:c:d"
-_opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only"
+_opt_short="w:l:o:ha:-:m:c:dx"
+_opt_long="help,arch:,codename:,debug,help,lang,mirror:,out:,work,cache-only,bash-debug"
 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- "${@}")
 
 if [[ ${?} != 0 ]]; then
diff --git a/tools/locale.sh b/tools/locale.sh
new file mode 100755 (executable)
index 0000000..b87d693
--- /dev/null
@@ -0,0 +1,169 @@
+#!/usr/bin/env bash
+
+set -e
+
+script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
+mode=""
+arch=""
+channel=""
+locale=""
+script=false
+
+_help() {
+    echo "usage ${0} [options] [command]"
+    echo
+    echo "Scripts that perform locale-related processing " 
+    echo
+    echo " General command:"
+    echo "    check [name]       Determine if the locale is available"
+    echo "    show               Shows a list of available locales"
+    echo "    get [name]         Prints the specified locale settings"
+    echo "    help               This help message"
+    echo
+    echo " General options:"
+    echo "    -a | --arch [arch]        Specify the architecture"
+    echo "    -c | --channel            Specify the channel"
+    echo "    -h | --help               This help message"
+}
+
+# Usage: getclm <number>
+# 標準入力から値を受けとり、引数で指定された列を抽出します。
+getclm() {
+    echo "$(cat -)" | cut -d " " -f "${1}"
+}
+
+# Message functions
+msg_error() {
+    "${script_path}/tools/msg.sh" -s 6 -a "locale.sh" error "${1}"
+}
+
+gen_locale_list() {
+    if [[ -z "${arch}" ]]; then
+        msg_error "No architecture specified." 
+        exit 1
+    fi
+    local _locale
+    for _locale in $(grep -h -v ^'#' "${script_path}/system/locale-${arch}" | getclm 1); do 
+        localelist+=("${_locale}")
+    done
+}
+
+check() {
+    gen_locale_list
+    if [[ ! "${#}" = "1" ]]; then
+        _help
+        exit 1
+    fi
+    if [[ $(printf '%s\n' "${localelist[@]}" | grep -qx "${1}"; echo -n ${?} ) -eq 0 ]]; then
+        echo "correct"
+        exit 0
+    else
+        echo "incorrect"
+        exit 1
+    fi
+}
+
+show() {
+    gen_locale_list
+    if (( "${#localelist[*]}" > 0)); then
+        echo "${localelist[*]}"
+    fi
+}
+
+get() {
+    gen_locale_list
+    if [[ ! "${#}" = "1" ]]; then
+        _help
+        exit 1
+    fi
+
+    #-- ロケールを解析、設定 --#
+    local _get_locale_line_number _locale_config_file _locale_name_list _locale_line_number _locale_config_line
+
+    # 選択されたロケールの設定が描かれた行番号を取得
+    _locale_config_file="${script_path}/system/locale-${arch}"
+    _locale_name_list=($(cat "${_locale_config_file}" | grep -h -v ^'#' | awk '{print $1}'))
+    _get_locale_line_number() {
+        local _lang _count=0
+        for _lang in ${_locale_name_list[@]}; do
+            _count=$(( _count + 1 ))
+            if [[ "${_lang}" = "${1}" ]]; then echo "${_count}"; return 0; fi
+        done
+        echo -n "failed"
+    }
+    _locale_line_number="$(_get_locale_line_number ${@})"
+
+    # 不正なロケール名なら終了する
+    if [[ "${_locale_line_number}" = "failed" ]]; then
+        msg_error "${1} is not a valid language."
+        if [[ "${script}" = true ]]; then
+            echo "exit 1"
+        fi
+        exit 1
+    fi
+
+    # ロケール設定ファイルから該当の行を抽出
+    _locale_config_line=($(cat "${_locale_config_file}" | grep -h -v ^'#' | grep -v ^$ | head -n "${_locale_line_number}" | tail -n 1))
+
+    # 抽出された行に書かれた設定をそれぞれの変数に代入
+    # ここで定義された変数のみがグローバル変数
+    cat << EOF
+locale_name="${_locale_config_line[0]}"
+locale_gen_name="${_locale_config_line[1]}"
+locale_version="${_locale_config_line[2]}"
+locale_time="${_locale_config_line[3]}"
+locale_fullname="${_locale_config_line[4]}"
+EOF
+}
+
+# Parse options
+ARGUMENT="${@}"
+_opt_short="a:c:hs"
+_opt_long="arch:,channel:,help,script"
+OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- ${ARGUMENT})
+[[ ${?} != 0 ]] && exit 1
+
+eval set -- "${OPT}"
+unset OPT _opt_short _opt_long
+
+while true; do
+    case ${1} in
+        -a | --arch)
+            arch="${2}"
+            shift 2
+            ;;
+        -c | --channel)
+            channel="${2}"
+            shift 2
+            ;;
+        -s | --script)
+            script=true
+            shift 1
+            ;;
+        -h | --help)
+            _help
+            exit 0
+            ;;
+        --)
+            shift 1
+            break
+            ;;
+
+    esac
+done
+
+if [[ -z "${1}" ]]; then
+    _help
+    exit 1
+else
+    mode="${1}"
+    shift 1
+fi
+
+case "${mode}" in
+    "check" ) check ${@}    ;;
+    "show"  ) show          ;;
+    "get"   ) get ${@}      ;;
+    "help"  ) _help; exit 0 ;;
+    *       ) _help; exit 1 ;;
+esac
diff --git a/tools/msg.sh b/tools/msg.sh
new file mode 100755 (executable)
index 0000000..65a8825
--- /dev/null
@@ -0,0 +1,246 @@
+#!/usr/bin/env bash
+
+set -e
+
+script_path="$( cd -P "$( dirname "$(readlink -f "$0")" )" && cd .. && pwd )"
+
+appname="msg.sh"
+bash_debug=false
+nocolor=false
+echo_opts=""
+message=""
+msg_type="info"
+msg_label=""
+label_space="7"
+adjust_chr=" "
+customized_label=false
+customized_label_color=false
+nolabel=false
+noappname=false
+noadjust=false
+output="stdout"
+
+_help() {
+    echo "usage ${0} [option] [type] [message]"
+    echo
+    echo "Display a message with a colored app name and message type label"
+    echo
+    echo " General type:"
+    echo "    info                      General message"
+    echo "    warn                      Warning message"
+    echo "    error                     Error message"
+    echo "    debug                     Debug message"
+    echo
+    echo " General options:"
+    echo "    -a [name]                 Specify the app name"
+    echo "    -c [character]            Specify the character to adjust the label"
+    echo "    -l [label]                Specify the label."
+    echo "    -n | --nocolor            No output colored output"
+    echo "    -o [option]               Specify echo options"
+    echo "    -r [color]                Specify the color of label"
+    echo "    -s [number]               Specifies the label space."
+    echo "    -x | --bash-debug         Enables output bash debugging"
+    echo "    -h | --help               This help message"
+    echo
+    echo "         --nolabel            Do not output label"
+    echo "         --noappname          Do not output app name"
+    echo "         --noadjust           Do not adjust the width of the label"
+}
+
+# Message functions
+msg_error() {
+    "${script_path}/tools/msg.sh" -a "msg.sh" error "${1}"
+}
+
+
+while getopts "a:c:l:no:r:s:xh-:" arg; do
+  case ${arg} in
+        a) appname="${OPTARG}" ;;
+        c) adjust_chr="${OPTARG}" ;;
+        l) 
+            customized_label=true
+            msg_label="${OPTARG}"
+            ;;
+        n) nocolor=true ;;
+        o) echo_opts="${OPTARG}" ;;
+        r)
+            customized_label_color=true
+            case ${OPTARG} in
+                "black")
+                    labelcolor="30"
+                    ;;
+                "red")
+                    labelcolor="31"
+                    ;;
+                "green")
+                    labelcolor="32"
+                    ;;
+                "yellow")
+                    labelcolor="33"
+                    ;;
+                "blue")
+                    labelcolor="34"
+                    ;;
+                "magenta")
+                    labelcolor="35"
+                    ;;
+                "cyan")
+                    labelcolor="36"
+                    ;;
+                "white")
+                    labelcolor="37"
+                    ;;
+                *)
+                    msg_error "The wrong color."
+                    exit 1
+                    ;;
+            esac
+            ;;
+        s) label_space="${OPTARG}" ;;
+        x)
+            bash_debug=true
+            set -xv
+            ;;
+        h)
+            _help
+            shift 1
+            exit 0
+            ;;
+        -)
+            case "${OPTARG}" in
+                "nocolor") nocolor=true ;;
+                "bash-debug")
+                    bash_debug=true
+                    set -xv
+                    ;;
+                "help") 
+                    _help
+                    exit 0
+                    ;;
+                "nolabel") nolabel=true ;;
+                "noappname") noappname=true ;;
+                "noadjust") noadjust=true ;;
+                *)
+                    _help
+                    exit 1
+                    ;;
+            esac
+  esac
+done
+
+shift $((OPTIND - 1))
+
+# Color echo
+#
+# Text Color
+# 30 => Black
+# 31 => Red
+# 32 => Green
+# 33 => Yellow
+# 34 => Blue
+# 35 => Magenta
+# 36 => Cyan
+# 37 => White
+#
+# Background color
+# 40 => Black
+# 41 => Red
+# 42 => Green
+# 43 => Yellow
+# 44 => Blue
+# 45 => Magenta
+# 46 => Cyan
+# 47 => White
+#
+# Text decoration
+# You can specify multiple decorations with ;.
+# 0 => All attributs off (ノーマル)
+# 1 => Bold on (太字)
+# 4 => Underscore (下線)
+# 5 => Blink on (点滅)
+# 7 => Reverse video on (色反転)
+# 8 => Concealed on
+
+case ${1} in
+    "info")
+        msg_type="type"
+        output="stdout"
+        [[ "${customized_label_color}" = false ]] && labelcolor="32"
+        [[ "${customized_label}"       = false ]] && msg_label="Info"
+        shift 1
+        ;;
+    "warn")
+        msg_type="warn"
+        output="stdout"
+        [[ "${customized_label_color}" = false ]] && labelcolor="33"
+        [[ "${customized_label}"       = false ]] && msg_label="Warning"
+        shift 1
+        ;;
+    "debug")
+        msg_type="debug"
+        output="stdout"
+        [[ "${customized_label_color}" = false ]] && labelcolor="35"
+        [[ "${customized_label}"       = false ]] && msg_label="Debug"
+        shift 1
+        ;;
+    "error")
+        msg_type="error"
+        output="stderr"
+        [[ "${customized_label_color}" = false ]] && labelcolor="31"
+        [[ "${customized_label}"       = false ]] && msg_label="Error"
+        shift 1
+        ;;
+    "")
+        msg_error "Please specify the message type"
+        exit 1
+        ;;
+    *)
+        msg_error "Unknown message type"
+        exit 1
+        ;;
+esac
+
+word_count="${#msg_label}"
+message="${@}"
+
+echo_type() {
+    if [[ "${nolabel}" = false ]]; then
+        if [[ "${noadjust}" = false ]]; then
+            for i in $( seq 1 $(( ${label_space} - ${word_count})) ); do
+                echo -ne "${adjust_chr}"
+            done
+        fi
+        if [[ "${nocolor}" = false ]]; then
+            echo -ne "\e[$([[ -v backcolor ]] && echo -n "${backcolor}"; [[ -v labelcolor ]] && echo -n ";${labelcolor}"; [[ -v decotypes ]] && echo -n ";${decotypes}")m${msg_label}\e[m "
+        else
+            echo -ne "${msg_label} "
+        fi
+    fi
+}
+
+echo_appname() {
+    if [[ "${noappname}" = false ]]; then
+        if [[ "${nocolor}" = false ]]; then
+            echo -ne "\e[36m[${appname}]\e[m "
+        else
+            echo -ne "[${appname}] "
+        fi
+    fi
+}
+
+
+for count in $(seq "1" "$(echo -ne "${message}\n" | wc -l)"); do
+    echo_message=$(echo -ne "${message}\n" |head -n "${count}" | tail -n 1 )
+    full_message="$(echo_appname)$(echo_type)${echo_message}"
+    case "${output}" in
+        "stdout")
+            echo ${echo_opts} "${full_message}" >&1
+            ;;
+        "stderr")
+            echo ${echo_opts} "${full_message}" >&2
+            ;;
+        *)
+            echo ${echo_opts} "${full_message}" > ${output}
+            ;;
+    esac
+done