diff options
author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2019-04-23 16:58:06 (EDT) |
---|---|---|
committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2019-04-23 16:58:06 (EDT) |
commit | 1b5f9fea85ca6fe0ba5786367e1da11bc4df808e (patch) | |
tree | 2eb6e363ef05ac93a75edb564e796cb37329c412 | |
parent | a889301436b70b221e818db507204be52d74da48 (diff) |
ob_parse_control(): Replace if construct with case
-rw-r--r-- | src/control.sh | 132 |
1 files changed, 71 insertions, 61 deletions
diff --git a/src/control.sh b/src/control.sh index 2a2d719..45600a6 100644 --- a/src/control.sh +++ b/src/control.sh @@ -56,79 +56,89 @@ parse_control() while IFS='' read -r line; do line_nr=$((${line_nr} + 1)) - if [ "x$(echo ${line})" = 'x' ]; then - # Paragraph end. - if ${in_paragraph}; then - # The first line is blank to consolidate - # initialization code (see heredocument below). - in_paragraph='false' + case "${line}" in + '') + # Paragraph end. + if ${in_paragraph}; then + # The first line is blank to consolidate + # initialization code (see heredocument + # below). + in_paragraph='false' + if [ "x${name}" != 'x' ]; then + if ! "${field_cb}" "${name}" \ + "${value}"; then + return 0 + fi + fi + if ! "${paragraph_cb}"; then + return 0 + fi + if [ "x${para_req_fields}" != 'x' ]; then + para_req_fields="$(printf \ + "%s$(get_msg \ + 'list_item_separator')"\ + ${para_req_fields})" + _parse_control_error \ + "${file}" "${line_nr}" \ + 'control_missing_fields'\ + "${para_req_fields}" + fi + fi + para_req_fields="$(printf '%s\n' ${req_fields})" + got_fields="${LF}" + name='' + value='' + ;; + '#'*) # Comment. + in_paragraph='true' + ;; + [!\ ]*':'*) # "Name: Value" line. + in_paragraph='true' if [ "x${name}" != 'x' ]; then if ! "${field_cb}" "${name}" "${value}" then return 0 fi fi - if ! "${paragraph_cb}"; then - return 0 + name="${line%%:*}" + value="${line#*:}" + value="${value# }" + if [ "x${name}" = 'x' -o "x${name}" = \ + "x${line}" ]; then + _parse_control_error "${file}" \ + "${line_nr}" 'control_bad_nv' + continue fi if [ "x${para_req_fields}" != 'x' ]; then - para_req_fields="$(printf "%s$(get_msg \ - 'list_item_separator')" \ - ${para_req_fields})" + # Remove field from list of required + # fields. + para_req_fields="$(printf '%s' \ + "${para_req_fields}" | \ + grep -Fv "${name}")" + fi + if [ "x${got_fields%${LF}${name}${LF}*}" != \ + "x${got_fields}" ]; then + # Duplicate field. _parse_control_error \ "${file}" "${line_nr}" \ - 'control_missing_fields' \ - "${para_req_fields}" + 'control_duplicate_field' \ + "${name}" + else + got_fields="${got_fields}${name}${LF}" fi - fi - para_req_fields="$(printf '%s\n' ${req_fields})" - got_fields="${LF}" - name='' - value='' - elif [ "x${line#\#}" != "x${line}" ]; then - # Comment. - in_paragraph='true' - elif [ "x${line# }" = "x${line}" ]; then - # "Name: Value" line. - in_paragraph='true' - if [ "x${name}" != 'x' ]; then - if ! "${field_cb}" "${name}" "${value}"; then - return 0 + ;; + ' '*) # Continuation line. + in_paragraph='true' + if [ "x${name}" = 'x' ]; then + # Expecting a "Name: Value" line. + _parse_control_error "${file}" \ + "${line_nr}" \ + 'control_found_continuation' + continue fi - fi - name="${line%%:*}" - value="${line#*:}" - value="${value# }" - if [ "x${name}" = 'x' -o "x${name}" = "x${line}" ]; then - _parse_control_error "${file}" "${line_nr}" \ - 'control_bad_nv' - continue - fi - if [ "x${para_req_fields}" != 'x' ]; then - # Remove field from list of required fields. - para_req_fields="$(printf '%s' "${para_req_fields}" | \ - grep -Fv "${name}")" - fi - if [ "x${got_fields%${LF}${name}${LF}*}" != \ - "x${got_fields}" ]; then - # Duplicate field. - _parse_control_error "${file}" "${line_nr}" \ - 'control_duplicate_field' \ - "${name}" - else - got_fields="${got_fields}${name}${LF}" - fi - else - # Continuation line. - in_paragraph='true' - if [ "x${name}" = 'x' ]; then - # Expecting a "Name: Value" line. - _parse_control_error "${file}" "${line_nr}" \ - 'control_found_continuation' - continue - fi - value="${value}${LF}${line# }" - fi + value="${value}${LF}${line# }" + ;; + esac done <<-EOF $(cat "${file}") |