From d1f79411c618a7a3d078430450631dfca85c2a07 Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Sat, 05 Jan 2019 13:58:14 -0500 Subject: [WIP] ob_parse_control(): Run an error callback --- diff --git a/lib/control.sh b/lib/control.sh index 30c6b9c..cb4dfd6 100644 --- a/lib/control.sh +++ b/lib/control.sh @@ -19,35 +19,6 @@ _OB_SUBSTVARS_MAX_DEPTH=50 -_ob_parse_control_error() -{ - 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 [ ${line_nr} -eq 0 ]; then - file_info="$(printf '%20s:' "${file}")" - else - file_info="$(printf '%20s(l%d):' "${file}" "${line_nr}")" - fi - - orig_text_domain="$(ob_get_text_domain)" - ob_set_text_domain "${_OB_INTERNAL_TEXT_DOMAIN}" - - ob_warn "${file_info} $(ob_get_msg "${msg_id}")" "${@}" - - ob_set_text_domain "${orig_text_domain}" - - return 0 -} - ## @brief Parse a control file ## @details \fBob_parse_control\fP parses a control file of field names and ## values formatted like RFC 822 (or RFC 2822 or RFC 5322) headers. @@ -64,6 +35,10 @@ _ob_parse_control_error() ## @operand field_cb req Callback to run for each field. Must accept three ## arguments: the field name, the field value, and ## \fIuser_data\fP. +## @operand error_cb req Callback to run on errors. Must accept four or more +## arguments: \fIuser_data\fP, the file name, the line +## number, the error ID string, and any arguments for +## formatted output. ## @operand user_data req Data to pass to \fIfield_cb\fP. ## @operand req_fields opt Required fields that must appear in the control file. ## @operand opt_fields opt Optional fields that may appear in the control file. @@ -77,6 +52,7 @@ ob_parse_control() { local file= local field_cb= + local error_cb= local user_data= local req_fields= local opt_fields= @@ -88,17 +64,19 @@ ob_parse_control() local name= local value= - if [ ${#} -eq 3 ]; then + if [ ${#} -eq 4 ]; then file="${1}" field_cb="${2}" - user_data="${3}" + error_cb="${3}" + user_data="${4}" check_fields='false' - elif [ ${#} -eq 5 ]; then + elif [ ${#} -eq 6 ]; then file="${1}" field_cb="${2}" - user_data="${3}" - req_fields="${4}" - opt_fields="${5}" + error_cb="${3}" + user_data="${4}" + req_fields="${5}" + opt_fields="${6}" check_fields='true' all_fields=" ${req_fields} ${opt_fields} " else @@ -113,7 +91,7 @@ ob_parse_control() line_nr=$(($line_nr + 1)) case "${line}" in '') - _ob_parse_control_error "${file}" "${line_nr}" \ + "${error_cb}" "${user_data}" "${file}" "${line_nr}" \ 'control_empty_line' ;; '#'*) # Comment. @@ -129,7 +107,7 @@ ob_parse_control() EOF if [ -z "${name}" ]; then # Badly formatted control field. - _ob_parse_control_error "${file}" "${line_nr}" \ + "${error_cb}" "${user_data}" "${file}" "${line_nr}" \ 'control_bad_nv' continue fi @@ -137,7 +115,7 @@ ob_parse_control() if [ "${all_fields% ${name} *}" = \ "${all_fields}" ]; then # Unknown field. - _ob_parse_control_error \ + "${error_cb}" "${user_data}" \ "${file}" "${line_nr}" \ 'control_unknown_field' "${name}" else @@ -149,7 +127,7 @@ ob_parse_control() if [ "${got_fields% ${name} *}" != \ "${got_fields}" ]; then # Duplicate field. - _ob_parse_control_error "${file}" "${line_nr}" \ + "${error_cb}" "${user_data}" "${file}" "${line_nr}" \ 'control_duplicate_field' "${name}" else got_fields="${got_fields}${name} " @@ -158,7 +136,7 @@ ob_parse_control() ' '*) # Continuation line. if [ -z "${name}" ]; then # Expecting a "Name: Value" line. - _ob_parse_control_error "${file}" "${line_nr}" \ + "${error_cb}" "${user_data}" "${file}" "${line_nr}" \ 'control_found_continuation' continue fi @@ -182,7 +160,7 @@ ob_parse_control() # Missing required control fields. req_fields="$(echo "${req_fields}" | sed 's/ / /g' | \ sed "s/ /$(ob_get_msg 'list_item_separator')/g")" - _ob_parse_control_error "${file}" '0' \ + "${error_cb}" "${user_data}" "${file}" '0' \ 'control_missing_fields' "${req_fields}" fi fi -- cgit v0.9.1