From 129aee4288fe239b8c46bd283977ac1667feac37 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 26 Jul 2012 01:18:31 -0400 Subject: Add oh-parsechangelog. --- diff --git a/lib/Makefile.in b/lib/Makefile.in index 4e9ecdf..ecf1e3a 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -28,7 +28,7 @@ LIBDIR = @libdir@ .SUFFIXES: .SUFFIXES: .sh -SRCS = controlfields.sh architecture.sh archive.sh messages.sh +SRCS = controlfields.sh architecture.sh archive.sh messages.sh changelog.sh OBJS = $(SRCS:.sh=) PACKAGE = @package@ diff --git a/lib/changelog.sh b/lib/changelog.sh new file mode 100644 index 0000000..81e3332 --- /dev/null +++ b/lib/changelog.sh @@ -0,0 +1,145 @@ +#!@@SHELL@@ +# +# opkhelper +# lib/changelog +# Functions for parsing changelogs. +# +# Copyright (C) 2012 Patrick "P. J." McDermott +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. @@LIBDIR@@/opkhelper/messages + +oh_changelog_parse_error() +{ + _file="${1}" + _line_nr="${2}" + _error="${3}" + _expect_id="${4}" + + case "${_expect_id}" in + FIRST_HEADING) _expect_str='first heading';; + NEXT_OR_EOF) _expect_str='next heading or eof';; + START_CHANGES) _expect_str='start of change data';; + CHANGES_OR_TRAILER) _expect_str='more change data or trailer';; + *) _expect_str='unknown';; + esac + + _file_info=$(printf '%20s(l%d):' "${_file}" "${_line_nr}") + oh_warn "${_file_info} ${_error}" "${_expect_str}" +} + +oh_changelog_parse() +{ + # Parsing logic based on that of dpkg. + + _file="${1}" + _cb="${2}" + + _expect=FIRST_HEADING + _line_nr=0 + _entries=0 + + while IFS= read _line; do + _line_nr=$(($_line_nr + 1)) + if [ -z "${_line}" ]; then + if [ "${_expect}" = START_CHANGES ]; then + OH_CHANGELOG_CHANGES="${OH_CHANGELOG_CHANGES} +${_line}" + elif [ "${_expect}" = NEXT_OR_EOF ]; then + : + elif [ "${_expect}" != CHANGES_OR_TRAILER ]; then + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'found blank line where expected %s' "${_expect}" + else + _blank_lines="${_blank_lines} +${_line}" + fi + elif [ "${_line# }" = "${_line}" ]; then + if [ "${_expect}" != FIRST_HEADING -a \ + "${_expect}" != NEXT_OR_EOF ]; then + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'found start of entry where expected %s' "${_expect}" + fi + _source="${_line%% (*}" + _line_="${_line#* (}" + _distribution="${_line_##*) }" + _line_="${_line_%) *}" + _version="${_line_}" + if [ -z "${_source}" -o -z "${_distribution}" -o \ + -z "${_version}" -o \ + "${_version% *}" != "${_version}" ]; then + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'badly formatted heading line' + OH_CHANGELOG_SOURCE= + OH_CHANGELOG_VERSION= + OH_CHANGELOG_DISTRIBUTION= + OH_CHANGELOG_CHANGES= + else + OH_CHANGELOG_SOURCE="${_source}" + OH_CHANGELOG_VERSION="${_version}" + OH_CHANGELOG_DISTRIBUTION="${_distribution}" + OH_CHANGELOG_CHANGES="${_line}" + fi + _expect=START_CHANGES + _blank_lines= + elif [ "${_line# -- }" != "${_line}" ]; then + if [ "${_expect}" != CHANGES_OR_TRAILER ]; then + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'found trailer where expected %s' "${_expect}" + fi + _line="${_line# -- }" + _maintainer="${_line%% *}" + _date="${_line#* }" + if [ -z "${_maintainer}" -o -z "${_date}" -o \ + "${_maintainer}" = "${_date}" ]; then + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'badly formatted trailer line' + OH_CHANGELOG_MAINTAINER= + OH_CHANGELOG_DATE= + elif [ -n "${OH_CHANGELOG_SOURCE}" ]; then + OH_CHANGELOG_MAINTAINER="${_maintainer}" + OH_CHANGELOG_DATE="${_date}" + "${_cb}" + _entries=$(($_entries + 1)) + fi + _expect=NEXT_OR_EOF + _blank_lines= + elif [ "${_line# --}" != "${_line}" ]; then + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'badly formatted trailer line' + elif [ "${_line## }" != "${_line}" ]; then + if [ "${_expect}" != START_CHANGES -a \ + "${_expect}" != CHANGES_OR_TRAILER ]; then + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'found change data where expected %s' "${_expect}" + fi + OH_CHANGELOG_CHANGES="${OH_CHANGELOG_CHANGES} +${_blank_lines}${_line}" + _expect=CHANGES_OR_TRAILER + _blank_lines= + else + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'unrecognized line' + _blank_lines= + fi + done <"${_file}" + + if [ "${_expect}" != NEXT_OR_EOF ]; then + oh_changelog_parse_error "${_file}" "${_line_nr}" \ + 'found eof where expected %s' "${_expect}" + fi + + return ${_entries} +} diff --git a/src/Makefile.in b/src/Makefile.in index 1037f98..bc4a362 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -31,7 +31,7 @@ BINDIR = @bindir@ SRCS = opkbuild.sh \ oh-checkbuilddeps.sh oh-applypatches.sh oh-copyconfig.sh \ oh-strip.sh oh-installfiles.sh oh-installdocs.sh \ - oh-gencontrol.sh oh-buildopk.sh + oh-gencontrol.sh oh-buildopk.sh oh-parsechangelog.sh OBJS = $(SRCS:.sh=) PACKAGE = @package@ diff --git a/src/oh-parsechangelog.sh b/src/oh-parsechangelog.sh new file mode 100644 index 0000000..86d8a8a --- /dev/null +++ b/src/oh-parsechangelog.sh @@ -0,0 +1,56 @@ +#!@@SHELL@@ +# +# opkhelper +# src/oh-parsechangelog +# Parse a changelog. +# +# Copyright (C) 2012 Patrick "P. J." McDermott +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +. @@LIBDIR@@/opkhelper/changelog + +print_usage() +{ + printf 'Usage: %s\n' "${1}" +} + +main() +{ + oh_changelog_parse changelog output + + printf 'Parsed %d entries.\n' ${?} +} + +output() +{ + printf 'Source: %s\nVersion: %s\nDistribution: %s\n' \ + "${OH_CHANGELOG_SOURCE}" "${OH_CHANGELOG_VERSION}" \ + "${OH_CHANGELOG_DISTRIBUTION}" + printf 'Maintainer: %s\nDate: %s\n' \ + "${OH_CHANGELOG_MAINTAINER}" "${OH_CHANGELOG_DATE}" + printf 'Changes:\n' + while IFS= read -r line; do + if [ -z "${line}" ]; then + printf ' .\n' + else + printf ' %s\n' "${line}" + fi + done <