#!/bin/bash
#  core/cleanup - Script Cleanup Function Support
#
# The `core/cleanup` library provides a mechanism for other libraries to
# register functions that get called when the script exits.
#
# Some libraries need to perform cleanup before exiting.  This can
# include releasing system resources (deleting files, closing sockets,
# etc.) or reversing temporary side-effects.  The ``cleanup_add()``
# function provided by this library allows registering functions to
# accomplish that task.

set -e


######
# Library Settings

# $cleanup_funcs[] - Lists functions to be called upon application exit.
lib_setting_arrays cleanup_funcs


######
# Public Interface

# cleanup_init() - Initializes the `core/cleanup` library.
#
# This function registers an internal function, ``cleanup_exec()``,
# that will called when the process receives the EXIT signal.
cleanup_init() {
	trap cleanup_exec EXIT
}

# cleanup_add() - Registers a function ($1) that will be called when the
# application exits.
cleanup_add() {
	has_args 1 "$@"
	list_append cleanup_funcs "$1"
}

######
# Internal Functions

# cleanup_exec() - Called when the process receives the EXIT signal
# at application ``exit()``.  Calls all cleanup functions that were
# registered with ``cleanup_add()``.
cleanup_exec() {
	for f in "${cleanup_funcs[@]}"; do
		$f
	done
}

View the Developer Guide Index

View the Reference Manual Index


Generated on Fri Jul 28 14:34:52 PDT 2017 by mcsh d14 v0.23.0.