Author: dj
Date: 2008-04-12 22:12:00 -0600 (Sat, 12 Apr 2008)
New Revision: 8522

Added:
   trunk/bootscripts/contrib/lsb-v3/lsb/manage_functions
Modified:
   trunk/bootscripts/contrib/lsb-v3/Changelog
   trunk/bootscripts/contrib/lsb-v3/Makefile
Log:
Added manage_functions.

Modified: trunk/bootscripts/contrib/lsb-v3/Changelog
===================================================================
--- trunk/bootscripts/contrib/lsb-v3/Changelog  2008-04-11 22:19:24 UTC (rev 
8521)
+++ trunk/bootscripts/contrib/lsb-v3/Changelog  2008-04-13 04:12:00 UTC (rev 
8522)
@@ -1,5 +1,9 @@
-Change Log
+Changelog
 
+20080413 - [dj] * Added manage_functions script for use by scripts such as
+                  {install,remove}_initd or chkconfig, that need to parse the
+                  LSB headers.
+
 20080315 - [dj] * Corrected test for $pidfile in pidofproc()
                 * Replaced exit with return in statusproc() - (lfs-functions)
 

Modified: trunk/bootscripts/contrib/lsb-v3/Makefile
===================================================================
--- trunk/bootscripts/contrib/lsb-v3/Makefile   2008-04-11 22:19:24 UTC (rev 
8521)
+++ trunk/bootscripts/contrib/lsb-v3/Makefile   2008-04-13 04:12:00 UTC (rev 
8522)
@@ -47,6 +47,7 @@
        install -m ${MODE} init.d/udev              ${EXTDIR}/init.d/
        install -m ${MODE} init.d/udev_retry        ${EXTDIR}/init.d/
        install -m ${CONFMODE} lsb/init-functions   ${DESTDIR}/lib/lsb/
+       install -m ${CONFMODE} lsb/manage_functions ${DESTDIR}/lib/lsb/
        ln -sf ../init.d/network     ${EXTDIR}/rc0.d/K80network
        ln -sf ../init.d/sysklogd    ${EXTDIR}/rc0.d/K90sysklogd
        ln -sf ../init.d/sendsignals ${EXTDIR}/rc0.d/S60sendsignals
@@ -107,6 +108,7 @@
        install -m ${MODE} init.d/swap              ${EXTDIR}/init.d/
        install -m ${MODE} init.d/udev              ${EXTDIR}/init.d/
        install -m ${CONFMODE} lsb/init-functions   ${DESTDIR}/lib/lsb/
+       install -m ${CONFMODE} lsb/manage_functions ${DESTDIR}/lib/lsb/
        ln -sf ../init.d/sendsignals ${EXTDIR}/rc0.d/S60sendsignals
        ln -sf ../init.d/mountfs     ${EXTDIR}/rc0.d/S70mountfs
        ln -sf ../init.d/swap        ${EXTDIR}/rc0.d/S80swap

Added: trunk/bootscripts/contrib/lsb-v3/lsb/manage_functions
===================================================================
--- trunk/bootscripts/contrib/lsb-v3/lsb/manage_functions                       
        (rev 0)
+++ trunk/bootscripts/contrib/lsb-v3/lsb/manage_functions       2008-04-13 
04:12:00 UTC (rev 8522)
@@ -0,0 +1,214 @@
+#!/bin/bash
+# Begin /lib/lsb/manage_functions
+
+# /lib/lsb/manage_functions contains the functions used by 
+# /lib/lsb/install_initd and /lib/lsb/remove_initd as well as additional helper
+# functions for use in programs that would provide functionality similar to
+# the RedHat chkconfig utility, for instance.
+
+## Define rcbase for now
+## (this will go away when a config file is defined)
+rcbase=/etc/init.d
+
+# Define all arrays at script start to avoid scope issues
+# scriptlist is a list of valid scripts used as an index
+declare -a scriptlist
+# fullheaders is a complete set of valid LSB headers, stored in memory for 
+# each indexed script, to avoid multiple disk reads
+declare -a fullheaders
+
+###############################################################################
+# get_headers() - Obtains a valid list of scripts contained in ${rcbase} and  #
+#                 inserts the name of the script into the scriptlist[] array  #
+#                 for use by all other functions.  Additionally, it inserts   #
+#                 the entire LSB header information from each script into a   #
+#                 second array, fullheaders[], so that diskreads need only be #
+#                 done once                                                   #
+#                 Returns no value, but populates the variable ${scriptcount} #
+#                 and the arrays ${scriptlist} and ${fullheaders} for use     #
+#                 with other functions in this script.                        #
+###############################################################################
+get_headers()
+{
+    echo -n "Retrieving script information from disk..."
+    count=1
+    for file in $(find -P /etc/init.d -xdev -perm -u=x | sed -n 2~1p \
+                      | sed "s@/etc/init.d/rc@@")
+    do
+        # determine if script is an LSB compliant script
+        grep "### BEGIN INIT INFO" $file > /dev/null
+        if test $? -gt "0"
+        then
+            # this is not a valid script and is ignored
+            # skip the rest of the loop
+            continue
+        fi
+        # determine basename using only bash (is basename a builtin?)
+        filename=$(echo "${file}" | sed "[EMAIL PROTECTED]/@@")
+        # assign it to an array possition
+        scriptlist["${count}"]="${filename}"
+        # find the begining of the init info for the script
+        begin=$(grep -n "### BEGIN INIT INFO" "${file}" | cut -d: -f1)
+        # find the end of the init info for the script
+        end=$(grep -n "### END INIT INFO" "${file}" | cut -d: -f1)
+        # we'll use the difference between the values in the tail command
+        diff=$(( ${end} - ${begin} ))
+        # assign the entire LSB header information as a single string to the
+        # fullheaders[] array
+        fullheaders["${count}"]=$(head -n "${end}" "${file}" \
+                                    | tail -n "${diff}")
+        count=$(( ${count} + 1 ))
+        unset begin
+        unset end
+        unset diff
+        unset filename
+    done
+    # a number or array elements would be a nice regular variable assignment
+    scriptcount="[EMAIL PROTECTED]"
+    unset count
+    echo -e "Completed!"
+}
+
+###############################################################################
+# print_headers() - Presents a formatted list of all LSB compliant script     #
+#                   headers to stdout preceeded by script name for use in     #
+#                   other scripts                                             #
+###############################################################################
+print_headers()
+{
+    get_headers
+    count=1
+    while test "${count}" -lt "${scriptcount}"
+    do
+        echo "${scriptlist[$count]}"
+        echo "============================================================="
+        echo "${fullheaders[$count]}"
+        echo ""
+        echo ""
+        count="$(( ${count} + 1 ))"
+    done
+}
+
+###############################################################################
+# get_index() - Determines the array index of the specified script            #
+###############################################################################
+
+get_index()
+{
+    filename=$(echo "${1}" | sed "[EMAIL PROTECTED]/@@")
+    count=1
+    while test "${count}" -lt "${scriptcount}"
+    do
+        echo "${scriptlist[${count}]}" | grep "${filename}" > /dev/null
+        if test "${?}" -ne "0"
+        then
+            count=$(( ${count} + 1 ))
+            continue
+        else
+            break
+        fi
+    done
+    if test "${filename}" == "${scriptlist[${count}]}"
+    then
+        echo "${count}"
+    else
+        echo "${1} is not a valid LSB init script."
+        exit 1
+    fi
+    unset filename
+    unset count
+}
+
+###############################################################################
+# get_lsb_value() - Obtains the LSB Value of $1 for index of script ($2).     #
+###############################################################################
+get_lsb_value()
+{
+    # Probably need some error checking in here
+    echo "${fullheaders[${2}]}" | \
+        grep "^# ${1}" | \
+        sed -e "[EMAIL PROTECTED] ${1}:@@" \
+            -e "s/^[ \t]*//"
+}
+
+###############################################################################
+# convert_lsb_required() - Converts LSB defined facilities (facility names    #
+#                          begining with a '$' character) into script names   #
+#                          for required start/stop                            #
+###############################################################################
+convert_lsb_required()
+{
+    local count=0
+    local provides=""
+    local reqfacility=""
+    local reqprovideslist=""
+
+    for reqfacility in $@
+    do
+        # find the requires and it's index and then find the script name 
+        # from the index.  Since this is required, exit if it is not found
+        ## If reqfacility is already in script name format, nothing needs to
+        ## be done, just echo it back out.  I can't think of an easy way to
+        ## do this right now, the scriptname will be the same as the provides
+        ## anyway, so just let it fly for now...it'll be correct, it just 
+        ## takes an extra couple of commands to get the same result.
+        ## Besides, this will do some extra sanity checking in case somebody
+        ## writes a script that isn't named the same as provides, though this
+        ## isn't LSB compliant.  Additionally, these same comments apply to
+        ## the convert_lsb_should() fucntion below.
+        count=0
+        while test ${count} -lt ${scriptcount}
+        do
+            count=$(( $count + 1 ))
+            provides="$( get_lsb_value Provides ${count} )"
+            if test "${provides}" = "${reqfacility}"
+            then
+                 reqprovideslist="${reqprovideslist} ${scriptlist[$count]}"
+                 break
+            fi
+            if test ${count} -eq ${scriptcount}; then
+                # If we've never broken out of the while loop, then this is an
+                # unrecoverable error since it is a required item.  Exit now!
+                echo "Error: unable to locate required facility 
${reqfacility}!"
+                exit 5
+            fi
+        done
+    done
+    echo "${reqprovideslist}" | sed -e "s/^[ \t]*//" -e sed "s/[ \t]*$//"
+}
+
+###############################################################################
+# convert_lsb_should() - Converts LSB defined facilities (facility names      #
+#                        begining with a '$' character) into script names for #
+#                        should start/stop                                    #
+###############################################################################
+
+convert_lsb_should()
+{
+    local count=0
+    local provides=""
+    local optfacility=""
+    local optprovideslist=""
+
+    for optfacility in $@
+    do
+        # find the should and it's index and then find the script name 
+        # from the index.  Since this is not an error, simply warn if it
+        # is not found.
+        count=0
+        while test ${count} -lt ${scriptcount}
+        do
+            count=$(( $count + 1 ))
+            provides="$( get_lsb_value Provides ${count} )"
+            if test "${provides}" = "${optfacility}"
+            then
+                 optprovideslist="${optprovideslist} ${scriptlist[$count]}"
+                 break
+            fi
+            # No need to error or warn on should items, and it's messy if so!
+        done
+    done
+    echo "${optprovideslist}" | sed -e "s/^[ \t]*//" -e sed "s/[ \t]*$//"
+}
+
+

-- 
http://linuxfromscratch.org/mailman/listinfo/lfs-book
FAQ: http://www.linuxfromscratch.org/lfs/faq.html
Unsubscribe: See the above information page

Reply via email to