# Functions for parsing and validating package metadata # # Copyright (C) 2012, 2014, 2018, 2019 Patrick McDermott # # This file is part of opkbuild. # # opkbuild 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. # # opkbuild 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 opkbuild. If not, see . _ob_metadata_do() { local func="${1}" shift 1 || _ob_abort "_ob_${func}" "${@}" || return ${?} return 0 } ## @brief Validate a source package name ## @details \fBob_validate_source_name\fP() validates a source package name ## against the rules of the metadata plugin selected at libopkbuild's ## build time. ## @operand name req The source package name to validate. ## @return Returns 0 if valid or 1 if invalid. ## @pure yes This function has no side effects. ob_validate_source_name() { local name="${1}" shift 1 || _ob_abort _ob_metadata_do 'validate_source_name' "${name}" || return ${?} return 0 } ## @brief Validate a binary package name ## @details \fBob_validate_binary_name\fP() validates a binary package name ## against the rules of the metadata plugin selected at libopkbuild's ## build time. ## @operand name req The binary package name to validate. ## @return Returns 0 if valid or 1 if invalid. ## @pure yes This function has no side effects. ob_validate_binary_name() { local name="${1}" shift 1 || _ob_abort _ob_metadata_do 'validate_binary_name' "${name}" || return ${?} return 0 } ## @brief Parse a source package version ## @details \fBob_parse_version\fP() validates and parses a source package ## version identifier using the metadata plugin selected at ## libopkbuild's build time. Parsing is limited to extracting the ## entire upstream version and distribution revision. ## @option -u upstreamver_var The name of the variable in which to store the ## upstream version. ## @option -d distrev_var The name of the variable in which to store the ## distribution revision. ## @operand version req The version to validate and parse. ## @return Returns 0 on success or 1 on invalid \fIversion\fP. ## @pure maybe This function has side effects when used with either or both of ## the \fB-u\fP and \fB-d\fP options. Without these options, this ## function only validates a version identifier. The purpose of ## this function is mainly to extract version components using these ## options, so in practice this function is generally not ## subshell-safe. ob_parse_version() { local opt= local upstreamver_var= local distrev_var= local version= OPTIND=1 while getopts 'u:d:' opt; do case "${opt}" in u) upstreamver_var="${OPTARG}" if ! _ob_validate_var_name "${upstreamver_var}" then _ob_abort fi ;; d) distrev_var="${OPTARG}" if ! _ob_validate_var_name "${distrev_var}" then _ob_abort fi ;; ?) _ob_abort ;; esac done shift $((${OPTIND} - 1)) version="${1}" shift 1 || _ob_abort if ! _ob_metadata_do 'validate_version' "${version}"; then return 1 fi if [ -n "${upstreamver_var}" ]; then eval "${upstreamver_var}=\"\$(_ob_metadata_do 'get_upstreamver' \ "${version}")\"" fi if [ -n "${distrev_var}" ]; then eval "${distrev_var}=\"\$(_ob_metadata_do 'get_distrev' \ "${version}")\"" fi return 0 } ## @brief Get the running system's architecture ## @details \fBob_get_system_arch\fP() gets the architecture of the build ## (running) system using the metadata plugin selected at libopkbuild's ## build time. ## @return Returns 0 on success or 1 if the system's architecture cannot be ## determined. ## @stdout Prints the system's architecture. ## @pure yes This function has no side effects. ob_get_system_arch() { _ob_metadata_do 'get_system_arch' || return ${?} return 0 } ## @brief Get the running system's platform ## @details \fBob_get_system_plat\fP() gets the platform of the build (running) ## system using the metadata plugin selected at libopkbuild's build ## time. ## @return Returns 0 on success or 1 if the system's platform cannot be ## determined. ## @stdout Prints the system's platform. ## @pure yes This function has no side effects. ob_get_system_plat() { _ob_metadata_do 'get_system_plat' || return ${?} return 0 } _ob_match_arch() { local match_arch="${1}" local field_arch="${2}" shift 2 || _ob_abort local match_arch_rest= local field_arch_rest= local match_arch_part= local field_arch_part= # "foo-bar-baz" == "any" if [ x"${field_arch}" = x'any' ]; then return 0 fi # "foo-bar-baz" == "foo-any-any" match_arch_rest="${match_arch}" field_arch_rest="${field_arch}" while [ -n "${match_arch_rest}" ] && [ -n "${field_arch_rest}" ]; do IFS='-' read match_arch_part match_arch_rest <<-EOF ${match_arch_rest} EOF IFS='-' read field_arch_part field_arch_rest <<-EOF ${field_arch_rest} EOF case "${field_arch_part}" in "${match_arch_part}" | 'any') ;; *) return 1;; # Failed match esac done return 0 } ## @brief Match a host architecture against an architecture field ## @details \fBob_arch_is_concerned\fP() checks whether a host architecture ## matches an architecture field. The host architecture may be either ## a specific distribution architecture or "all". The architecture ## field may be either a space-separated list of either "any" or ## specific distribution architectures, any of which can be negated ## with a prefix of "!", or "all". ## @operand host_arch req The host architecture to match. ## @operand arches req The architecture field against which to match. ## @return Returns 0 if \fIhost_arch\fP matches \fIarches\fP, or 0 if not. ## @pure yes This function has no side effects. ob_arch_is_concerned() { local host_arch="${1}" local arches="${2}" shift 2 || _ob_abort local arch= local not_arch= local seen_arch= if [ x"${arches}" = x'' ]; then return 0 elif [ x"${host_arch}" = x'all' ]; then if [ x"${arches}" = x'all' ]; then return 0 else return 1 fi else seen_arch=1 for arch in ${arches}; do not_arch="${arch#!}" if [ x"${not_arch}" != x"${arch}" ]; then if _ob_match_arch "${host_arch}" "${not_arch}" then seen_arch=1 break else seen_arch=0 fi elif _ob_match_arch "${host_arch}" "${arch}"; then seen_arch=0 break fi done return ${seen_arch} fi } _ob_match_plat() { local match_plat="${1}" local field_plat="${2}" shift 2 || _ob_abort if [ x"${field_plat}" = x'any' ]; then return 0 fi if [ x"${field_plat}" = x"${match_plat}" ]; then return 0 fi return 1 } ## @brief Match a host platform against a platform field ## @details \fBob_plat_is_concerned\fP() checks whether a host platform matches ## a platform field. The host platform may be either a specific ## distribution platform or "all". The platform field may be either a ## space-separated list of either "any" or specific distribution ## platforms, any of which can be negated with a prefix of "!", or ## "all". ## @operand host_plat req The host platform to match. ## @operand plats req The platform field against which to match. ## @return Returns 0 if \fIhost_plat\fP matches \fIplats\fP, or 0 if not. ## @pure yes This function has no side effects. ob_plat_is_concerned() { local host_plat="${1}" local plats="${2}" shift 2 || _ob_abort local plat= local not_plat= local seen_plat= if [ x"${plats}" = x'' ]; then return 0 elif [ x"${host_plat}" = x'all' ]; then if [ x"${plats}" = x'all' ]; then return 0 else return 1 fi else seen_plat=1 for plat in ${plats}; do not_plat="${plat#!}" if [ x"${not_plat}" != x"${plat}" ]; then if _ob_match_plat "${host_plat}" "${not_plat}" then seen_plat=1 break else seen_plat=0 fi elif _ob_match_plat "${host_plat}" "${plat}"; then seen_plat=0 break fi done return ${seen_plat} fi } ## @brief Get a system path ## @details \fBob_get_system_path\fP() gets a system path consisting of ## \fIargs\fP formatted, by the metadata plugin selected at ## libopkbuild's build time, according to a path format identified by ## \fIpath_id\fP. The arguments for a \fIpath_id\fP of either ## \fBpackage-source\fP or \fBpackage-docs\fP are \fIsource\fP and ## \fIversion\fP. For \fBbuildflags\fP the argument is \fI arch\fP. ## For \fBplatconf\fP they can be either only \fIplat\fP or all of ## \fIsource\fP, \fIversion\fP, and \fIplat\fP; the latter form is ## deprecated. For \fBbuildflags\fP and \fBplatconf\fP, the system ## path will be under the value of the \fIOB_TEST_DATADIR\fP ## environment variable, if set and not null; this is intended for use ## in testing programs that call this function. ## @operand path_id req One of \fBpackage-source\fP, \fBpackage-docs\fP, ## \fBbuildflags\fP, or \fBplatconf\fP. ## @operand args req Additional arguments specific to each \fIpath_id\fP. ## @return Returns 0 on success. ## @stdout Prints the requested path with \fIargs\fP. ## @pure yes This function has no side effects. ob_get_system_path() { local path_id="${1}" shift 1 || _ob_abort _ob_metadata_do 'get_system_path' "${path_id}" "${@}" || return ${?} return 0 }