diff options
-rw-r--r-- | lib/control.sh | 244 |
1 files changed, 125 insertions, 119 deletions
diff --git a/lib/control.sh b/lib/control.sh index 4c38583..5ba6f0a 100644 --- a/lib/control.sh +++ b/lib/control.sh @@ -30,146 +30,149 @@ OB_CONTROL_VALUE= ob_parse_control() { - _ob_local _obpco_file _obpco_field_cb _obpco_req_fields _obpco_opt_fields \ - _obpco_check_fields _obpco_all_fields _obpco_got_fields \ - _obpco_line_nr _obpco_line _obpco_name _obpco_value + local file= + local field_cb= + local req_fields= + local opt_fields= + local check_fields= + local all_fields= + local got_fields= + local line_nr= + local line= + local name= + local value= if [ ${#} -eq 2 ]; then - _obpco_file="${1}" - _obpco_field_cb="${2}" - _obpco_check_fields='false' + file="${1}" + field_cb="${2}" + check_fields='false' elif [ ${#} -eq 4 ]; then - _obpco_file="${1}" - _obpco_field_cb="${2}" - _obpco_req_fields="${3}" - _obpco_opt_fields="${4}" - _obpco_check_fields='true' - _obpco_all_fields=" ${_obpco_req_fields} ${_obpco_opt_fields} " + file="${1}" + field_cb="${2}" + req_fields="${3}" + opt_fields="${4}" + check_fields='true' + all_fields=" ${req_fields} ${opt_fields} " else - _ob_return 125 - return ${?} + return 125 fi - _obpco_got_fields=' ' + got_fields=' ' - _obpco_line_nr=0 + line_nr=0 - while IFS= read -r _obpco_line; do - _obpco_line_nr=$(($_obpco_line_nr + 1)) - if [ -z "$(echo ${_obpco_line})" ]; then - _ob_parse_control_error "${_obpco_file}" "${_obpco_line_nr}" \ + while IFS= read -r line; do + line_nr=$(($line_nr + 1)) + if [ -z "$(echo ${line})" ]; then + _ob_parse_control_error "${file}" "${line_nr}" \ 'control_empty_line' - elif [ "${_obpco_line#\#}" != "${_obpco_line}" ]; then + elif [ "${line#\#}" != "${line}" ]; then # Comment. : - elif [ "${_obpco_line# }" = "${_obpco_line}" ]; then + elif [ "${line# }" = "${line}" ]; then # "Name: Value" line. - if [ -n "${_obpco_name}" ]; then - OB_CONTROL_NAME="${_obpco_name}" - OB_CONTROL_VALUE="${_obpco_value}" - "${_obpco_field_cb}" + if [ -n "${name}" ]; then + OB_CONTROL_NAME="${name}" + OB_CONTROL_VALUE="${value}" + "${field_cb}" if [ ${?} -ne 0 ]; then - _ob_return 0 - return ${?} + return 0 fi fi - _obpco_name="${_obpco_line%%:*}" - _obpco_value="${_obpco_line#*:}" - _obpco_value="${_obpco_value# }" - if [ -z "${_obpco_name}" \ - -o "${_obpco_name}" = "${_obpco_line}" ]; then + name="${line%%:*}" + value="${line#*:}" + value="${value# }" + if [ -z "${name}" \ + -o "${name}" = "${line}" ]; then # Badly formatted control field. - _ob_parse_control_error "${_obpco_file}" "${_obpco_line_nr}" \ + _ob_parse_control_error "${file}" "${line_nr}" \ 'control_bad_nv' continue fi - if ${_obpco_check_fields}; then - if [ "${_obpco_all_fields% ${_obpco_name} *}" = \ - "${_obpco_all_fields}" ]; then + if ${check_fields}; then + if [ "${all_fields% ${name} *}" = \ + "${all_fields}" ]; then # Unknown field. _ob_parse_control_error \ - "${_obpco_file}" "${_obpco_line_nr}" \ - 'control_unknown_field' "${_obpco_name}" + "${file}" "${line_nr}" \ + 'control_unknown_field' "${name}" else # Remove field from list of required fields. - _obpco_req_fields="$(echo "${_obpco_req_fields}" | \ - sed "s/${_obpco_name}//")" + req_fields="$(echo "${req_fields}" | \ + sed "s/${name}//")" fi fi - if [ "${_obpco_got_fields% ${_obpco_name} *}" != \ - "${_obpco_got_fields}" ]; then + if [ "${got_fields% ${name} *}" != \ + "${got_fields}" ]; then # Duplicate field. - _ob_parse_control_error "${_obpco_file}" "${_obpco_line_nr}" \ - 'control_duplicate_field' "${_obpco_name}" + _ob_parse_control_error "${file}" "${line_nr}" \ + 'control_duplicate_field' "${name}" else - _obpco_got_fields="${_obpco_got_fields}${_obpco_name} " + got_fields="${got_fields}${name} " fi else # Continuation line. - if [ -z "${_obpco_name}" ]; then + if [ -z "${name}" ]; then # Expecting a "Name: Value" line. - _ob_parse_control_error "${_obpco_file}" "${_obpco_line_nr}" \ + _ob_parse_control_error "${file}" "${line_nr}" \ 'control_found_continuation' continue fi - _obpco_value="${_obpco_value} -${_obpco_line# }" + value="${value} +${line# }" fi done <<-EOF - $(cat "${_obpco_file}") + $(cat "${file}") EOF - if [ -n "${_obpco_name}" ]; then - OB_CONTROL_NAME="${_obpco_name}" - OB_CONTROL_VALUE="${_obpco_value}" - "${_obpco_field_cb}" + if [ -n "${name}" ]; then + OB_CONTROL_NAME="${name}" + OB_CONTROL_VALUE="${value}" + "${field_cb}" if [ ${?} -ne 0 ]; then - _ob_return 0 - return ${?} + return 0 fi fi - if ${_obpco_check_fields}; then - _obpco_req_fields="${_obpco_req_fields## }" - _obpco_req_fields="${_obpco_req_fields%% }" - if [ -n "${_obpco_req_fields}" ]; then + if ${check_fields}; then + req_fields="${req_fields## }" + req_fields="${req_fields%% }" + if [ -n "${req_fields}" ]; then # Missing required control fields. - _obpco_req_fields="$(echo "${_obpco_req_fields}" | \ + req_fields="$(echo "${req_fields}" | \ sed 's/ / /g' | \ sed "s/ /$(ob_get_msg 'list_item_separator')/g")" - _ob_parse_control_error "${_obpco_file}" '0' \ + _ob_parse_control_error "${file}" '0' \ 'control_missing_fields' \ - "${_obpco_req_fields}" + "${req_fields}" fi fi - _ob_return 0 - return ${?} + return 0 } ob_set_substvar() { - _ob_local _obssv_name _obssv_value + local name= + local value= if [ ${#} -eq 2 ]; then - _obssv_name="${1}" - _obssv_value="${2}" + name="${1}" + value="${2}" else - _ob_return 125 - return ${?} + return 125 fi # Convert variable name to uppercase and validate. - _obssv_name="$(echo "${_obssv_name}" | tr 'a-z-' 'A-Z_')" - case "${_obssv_name:- }" in + name="$(echo "${name}" | tr 'a-z-' 'A-Z_')" + case "${name:- }" in *[!A-Z0-9_]*) - _ob_return 125 - return ${?} + return 125 ;; esac # Trim leading and trailing whitespace from value. - _obssv_value="$(echo "${_obssv_value}" | sed -n ' + value="$(echo "${value}" | sed -n ' H; # Store each input line in the hold space. ${ # At the last line of input, … g; # … restore the hold space into the pattern space, … @@ -180,90 +183,93 @@ ob_set_substvar() ')" # Escape single quotes in value. - _obssv_value="$(printf '%s\n' "${_obssv_value}" | sed "s/'/'\\\\''/g")" + value="$(printf '%s\n' "${value}" | sed "s/'/'\\\\''/g")" - eval "_OB_SUBSTVAR_${_obssv_name}='${_obssv_value}'" + eval "_OB_SUBSTVAR_${name}='${value}'" - _ob_return 125 - return ${?} + return 125 } ob_substvars() { - _ob_local _obsv_string _obsv_depth \ - _obsv_lhs _obsv_name _obsv_rhs _obsv_old_rhs _obsv_value + local string= + local depth= + local lhs= + local name= + local rhs= + local old_rhs= + local value= if [ ${#} -eq 1 ]; then - _obsv_string="${1}" + string="${1}" else - _ob_return 125 - return ${?} + return 125 fi # Logic inspired by that of dpkg's Dpkg::Substvars::substvars() subroutine. - _obsv_depth=0 + depth=0 while true; do - _obsv_lhs="${_obsv_string%%\$\{*}" - if [ ${#_obsv_lhs} -eq ${#_obsv_string} ]; then + lhs="${string%%\$\{*}" + if [ ${#lhs} -eq ${#string} ]; then # No "${" was found. break fi - _obsv_string="${_obsv_string#*\$\{}" - _obsv_name="${_obsv_string%%\}*}" - _obsv_rhs="${_obsv_string#*\}}" + string="${string#*\$\{}" + name="${string%%\}*}" + rhs="${string#*\}}" - if [ ${#_obsv_rhs} -lt ${#_obsv_old_rhs} ]; then + if [ ${#rhs} -lt ${#old_rhs} ]; then # Reset the nesting counter if we've advanced the right side of the # matched space. - _obsv_depth=0 + depth=0 fi - if [ ${_obsv_depth} -ge ${_OB_SUBSTVARS_MAX_DEPTH} ]; then + if [ ${depth} -ge ${_OB_SUBSTVARS_MAX_DEPTH} ]; then # Warn of possible recursion. ob_warn "$(ob_get_msg 'substvar_deep_nesting')" - _ob_return 1 - return ${?} + return 1 fi - _obsv_old_rhs="${_obsv_rhs}" + old_rhs="${rhs}" # Perform the substitution. - _obsv_name="$(echo "${_obsv_name}" | tr 'a-z-' 'A-Z_')" - _obsv_value="$(eval echo \"\$\{"_OB_SUBSTVAR_${_obsv_name}"\}\")" - _obsv_string="${_obsv_lhs}${_obsv_value}${_obsv_rhs}" - _obsv_depth=$(($_obsv_depth + 1)) + name="$(echo "${name}" | tr 'a-z-' 'A-Z_')" + value="$(eval echo \"\$\{"_OB_SUBSTVAR_${name}"\}\")" + string="${lhs}${value}${rhs}" + depth=$(($depth + 1)) done - printf '%s\n' "${_obsv_string}" + printf '%s\n' "${string}" - _ob_return 0 - return ${?} + return 0 } _ob_parse_control_error() { - _ob_local _obpcoe_file _obpcoe_line_nr _obpcoe_msg_id \ - _obpcoe_file_info _obpcoe_orig_text_domain - - _obpcoe_file="${1}" - _obpcoe_line_nr="${2}" - _obpcoe_msg_id="${3}" + local file= + local line_nr= + local msg_id= + local file_info= + local orig_text_domain= + + file="${1}" + line_nr="${2}" + msg_id="${3}" shift 3 - if [ "${_obpcoe_line_nr}" -eq 0 ]; then - _obpcoe_file_info="$(printf '%20s:' "${_obpcoe_file}")" + if [ "${line_nr}" -eq 0 ]; then + file_info="$(printf '%20s:' "${file}")" else - _obpcoe_file_info="$(printf '%20s(l%d):' "${_obpcoe_file}" \ - "${_obpcoe_line_nr}")" + file_info="$(printf '%20s(l%d):' "${file}" \ + "${line_nr}")" fi - _obpcoe_orig_text_domain="$(ob_get_text_domain)" + orig_text_domain="$(ob_get_text_domain)" ob_set_text_domain "${_OB_INTERNAL_TEXT_DOMAIN}" - ob_warn "${_obpcoe_file_info} $(ob_get_msg "${_obpcoe_msg_id}")" "${@}" + ob_warn "${file_info} $(ob_get_msg "${msg_id}")" "${@}" - ob_set_text_domain "${_obpcoe_orig_text_domain}" + ob_set_text_domain "${orig_text_domain}" - _ob_return 0 - return ${?} + return 0 } |