xref: /curl/packages/OS400/initscript.sh (revision d14149e3)
1#!/bin/sh
2#***************************************************************************
3#                                  _   _ ____  _
4#  Project                     ___| | | |  _ \| |
5#                             / __| | | | |_) | |
6#                            | (__| |_| |  _ <| |___
7#                             \___|\___/|_| \_\_____|
8#
9# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
10#
11# This software is licensed as described in the file COPYING, which
12# you should have received as part of this distribution. The terms
13# are also available at https://curl.se/docs/copyright.html.
14#
15# You may opt to use, copy, modify, merge, publish, distribute and/or sell
16# copies of the Software, and permit persons to whom the Software is
17# furnished to do so, under the terms of the COPYING file.
18#
19# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20# KIND, either express or implied.
21#
22# SPDX-License-Identifier: curl
23#
24###########################################################################
25
26CLcommand()
27{
28        /usr/bin/system "${@}" || exit 1
29}
30
31setenv()
32
33{
34        #       Define and export.
35
36        eval "${1}=${2}"
37        export "${1?}"
38}
39
40
41case "${SCRIPTDIR}" in
42/*)     ;;
43*)      SCRIPTDIR="$(pwd)/${SCRIPTDIR}"
44esac
45
46while true
47do      case "${SCRIPTDIR}" in
48        */.)    SCRIPTDIR="${SCRIPTDIR%/.}";;
49        *)      break;;
50        esac
51done
52
53#  The script directory is supposed to be in $TOPDIR/packages/os400.
54
55TOPDIR=$(dirname "${SCRIPTDIR}")
56TOPDIR=$(dirname "${TOPDIR}")
57export SCRIPTDIR TOPDIR
58
59#  Extract the SONAME from the library makefile.
60
61SONAME="$(sed -e '/^VERSIONCHANGE=/!d;s/^.*=\([0-9]*\).*/\1/'           \
62                                        < "${TOPDIR}/lib/Makefile.soname")"
63export SONAME
64
65#       Get OS/400 configuration parameters.
66
67. "${SCRIPTDIR}/config400.default"
68if [ -f "${SCRIPTDIR}/config400.override" ]
69then    . "${SCRIPTDIR}/config400.override"
70fi
71
72#       Check if perl available.
73{ [ -n "${PASEPERL}" ] && [ -x "${PASEPERL}" ]; } || PASEPERL=
74
75#       Need to get the version definitions.
76
77LIBCURL_VERSION=$(grep '^#define  *LIBCURL_VERSION '                    \
78                        "${TOPDIR}/include/curl/curlver.h"              |
79                sed 's/.*"\(.*\)".*/\1/')
80LIBCURL_VERSION_MAJOR=$(grep '^#define  *LIBCURL_VERSION_MAJOR '        \
81                        "${TOPDIR}/include/curl/curlver.h"              |
82                sed 's/^#define  *LIBCURL_VERSION_MAJOR  *\([^ ]*\).*/\1/')
83LIBCURL_VERSION_MINOR=$(grep '^#define  *LIBCURL_VERSION_MINOR '        \
84                        "${TOPDIR}/include/curl/curlver.h"              |
85                sed 's/^#define  *LIBCURL_VERSION_MINOR  *\([^ ]*\).*/\1/')
86LIBCURL_VERSION_PATCH=$(grep '^#define  *LIBCURL_VERSION_PATCH '        \
87                        "${TOPDIR}/include/curl/curlver.h"              |
88                sed 's/^#define  *LIBCURL_VERSION_PATCH  *\([^ ]*\).*/\1/')
89LIBCURL_VERSION_NUM=$(grep '^#define  *LIBCURL_VERSION_NUM '            \
90                        "${TOPDIR}/include/curl/curlver.h"              |
91                sed 's/^#define  *LIBCURL_VERSION_NUM  *0x\([^ ]*\).*/\1/')
92LIBCURL_TIMESTAMP=$(grep '^#define  *LIBCURL_TIMESTAMP '                \
93                        "${TOPDIR}/include/curl/curlver.h"              |
94                sed 's/.*"\(.*\)".*/\1/')
95export LIBCURL_VERSION
96export LIBCURL_VERSION_MAJOR LIBCURL_VERSION_MINOR LIBCURL_VERSION_PATCH
97export LIBCURL_VERSION_NUM LIBCURL_TIMESTAMP
98
99################################################################################
100#
101#                       OS/400 specific definitions.
102#
103################################################################################
104
105LIBIFSNAME="/QSYS.LIB/${TARGETLIB}.LIB"
106
107
108################################################################################
109#
110#                               Procedures.
111#
112################################################################################
113
114#       action_needed dest [src]
115#
116#       dest is an object to build
117#       if specified, src is an object on which dest depends.
118#
119#       exit 0 (succeeds) if some action has to be taken, else 1.
120
121action_needed()
122
123{
124        [ ! -e "${1}" ] && return 0
125        [ -n "${2}" ] || return 1
126        # shellcheck disable=SC3013
127        [ "${1}" -ot "${2}" ] && return 0
128        return 1
129}
130
131
132#       canonicalize_path path
133#
134#       Return canonicalized path as:
135#       - Absolute
136#       - No . or .. component.
137
138canonicalize_path()
139
140{
141        if expr "${1}" : '^/' > /dev/null
142        then    P="${1}"
143        else    P="$(pwd)/${1}"
144        fi
145
146        R=
147        IFSSAVE="${IFS}"
148        IFS="/"
149
150        for C in ${P}
151        do      IFS="${IFSSAVE}"
152                case "${C}" in
153                .)      ;;
154                ..)     R="$(expr "${R}" : '^\(.*/\)..*')"
155                        ;;
156                ?*)     R="${R}${C}/"
157                        ;;
158                *)      ;;
159                esac
160        done
161
162        IFS="${IFSSAVE}"
163        echo "/$(expr "${R}" : '^\(.*\)/')"
164}
165
166
167#       make_module module_name source_name [additional_definitions]
168#
169#       Compile source name into ASCII module if needed.
170#       As side effect, append the module name to variable MODULES.
171#       Set LINK to "YES" if the module has been compiled.
172
173make_module()
174
175{
176        MODULES="${MODULES} ${1}"
177        MODIFSNAME="${LIBIFSNAME}/${1}.MODULE"
178        action_needed "${MODIFSNAME}" "${2}" || return 0;
179        SRCDIR="$(dirname "$(canonicalize_path "${2}")")"
180
181        #       #pragma convert has to be in the source file itself, i.e.
182        #               putting it in an include file makes it only active
183        #               for that include file.
184        #       Thus we build a temporary file with the pragma prepended to
185        #               the source file and we compile that temporary file.
186
187        {
188                echo "#line 1 \"${2}\""
189                echo "#pragma convert(819)"
190                echo "#line 1"
191                cat "${2}"
192        } > __tmpsrcf.c
193        CMD="CRTCMOD MODULE(${TARGETLIB}/${1}) SRCSTMF('__tmpsrcf.c')"
194        CMD="${CMD} SYSIFCOPT(*IFS64IO *ASYNCSIGNAL)"
195#       CMD="${CMD} OPTION(*INCDIRFIRST *SHOWINC *SHOWSYS)"
196        CMD="${CMD} OPTION(*INCDIRFIRST)"
197        CMD="${CMD} LOCALETYPE(*LOCALE) FLAG(10)"
198        CMD="${CMD} INCDIR('${QADRTDIR}/include'"
199        CMD="${CMD} '${TOPDIR}/include/curl' '${TOPDIR}/include' '${SRCDIR}'"
200        CMD="${CMD} '${TOPDIR}/packages/OS400'"
201
202        if [ "${WITH_ZLIB}" != "0" ]
203        then    CMD="${CMD} '${ZLIB_INCLUDE}'"
204        fi
205
206        if [ "${WITH_LIBSSH2}" != "0" ]
207        then    CMD="${CMD} '${LIBSSH2_INCLUDE}'"
208        fi
209
210        CMD="${CMD} ${INCLUDES})"
211        CMD="${CMD} TGTCCSID(${TGTCCSID}) TGTRLS(${TGTRLS})"
212        CMD="${CMD} OUTPUT(${OUTPUT})"
213        CMD="${CMD} OPTIMIZE(${OPTIMIZE})"
214        CMD="${CMD} DBGVIEW(${DEBUG})"
215
216        DEFINES="${3} 'qadrt_use_inline'"
217
218        if [ "${WITH_ZLIB}" != "0" ]
219        then    DEFINES="${DEFINES} HAVE_LIBZ"
220        fi
221
222        if [ "${WITH_LIBSSH2}" != "0" ]
223        then    DEFINES="${DEFINES} USE_LIBSSH2"
224        fi
225
226        if [ -n "${DEFINES}" ]
227        then    CMD="${CMD} DEFINE(${DEFINES})"
228        fi
229
230        CLcommand "${CMD}"
231        rm -f __tmpsrcf.c
232        # shellcheck disable=SC2034
233        LINK=YES
234}
235
236
237#       Determine DB2 object name from IFS name.
238
239db2_name()
240
241{
242        if [ "${2}" = 'nomangle' ]
243        then    basename "${1}"                                         |
244                tr 'a-z-' 'A-Z_'                                        |
245                sed -e 's/\..*//'                                       \
246                    -e 's/^\(.\).*\(.........\)$/\1\2/'
247        else    basename "${1}"                                         |
248                tr 'a-z-' 'A-Z_'                                        |
249                sed -e 's/\..*//'                                       \
250                    -e 's/^CURL_*/C/'                                   \
251                    -e 's/^TOOL_*/T/'                                   \
252                    -e 's/^\(.\).*\(.........\)$/\1\2/'
253        fi
254}
255
256
257#       Copy IFS file replacing version info.
258
259versioned_copy()
260
261{
262        sed -e "s/@LIBCURL_VERSION@/${LIBCURL_VERSION}/g"               \
263            -e "s/@LIBCURL_VERSION_MAJOR@/${LIBCURL_VERSION_MAJOR}/g"   \
264            -e "s/@LIBCURL_VERSION_MINOR@/${LIBCURL_VERSION_MINOR}/g"   \
265            -e "s/@LIBCURL_VERSION_PATCH@/${LIBCURL_VERSION_PATCH}/g"   \
266            -e "s/@LIBCURL_VERSION_NUM@/${LIBCURL_VERSION_NUM}/g"       \
267            -e "s/@LIBCURL_TIMESTAMP@/${LIBCURL_TIMESTAMP}/g"           \
268                < "${1}" > "${2}"
269}
270
271
272#       Get definitions from a make file.
273#       The `sed' statement works as follows:
274#       - Join \nl-separated lines.
275#       - Retain only lines that begins with "identifier =".
276#       - Replace @...@ substitutions by shell variable references.
277#       - Turn these lines into shell variable assignments.
278
279get_make_vars()
280
281{
282        eval "$(sed -e ': begin'                                        \
283                -e '/\\$/{'                                             \
284                -e 'N'                                                  \
285                -e 's/\\\n/ /'                                          \
286                -e 'b begin'                                            \
287                -e '}'                                                  \
288                -e 's/[[:space:]][[:space:]]*/ /g'                      \
289                -e '/^[A-Za-z_][A-Za-z0-9_]* *=/!d'                     \
290                -e 's/@\([A-Za-z0-9_]*\)@/${\1}/g'                      \
291                -e 's/ *= */=/'                                         \
292                -e 's/=\(.*[^ ]\) *$/="\1"/'                            \
293                -e 's/\$(\([^)]*\))/${\1}/g'                            \
294                < "${1}")"
295}
296