summaryrefslogtreecommitdiff
path: root/admin/emacs-shell-lib
diff options
context:
space:
mode:
authorStefan Kangas <stefankangas@gmail.com>2022-10-19 13:26:54 +0200
committerStefan Kangas <stefankangas@gmail.com>2022-10-19 13:26:54 +0200
commita9111d8670b48f473e968a0e75d83782dbf74425 (patch)
tree4bc5653250eb2db2e95b9da8393556d5e776fbd8 /admin/emacs-shell-lib
parent5247a72aecb417e2f71e37af40ebacdfc26158b7 (diff)
downloademacs-a9111d8670b48f473e968a0e75d83782dbf74425.tar.gz
emacs-a9111d8670b48f473e968a0e75d83782dbf74425.tar.bz2
emacs-a9111d8670b48f473e968a0e75d83782dbf74425.zip
Add admin/emacs-shell-lib for shared bash code
* admin/emacs-shell-lib: New file for shared bash code. * admin/automerge: * admin/diff-tar-files: * admin/emacs-shell-lib: * admin/make-manuals: * admin/update_autogen: * admin/upload-manuals: Simplify and improve using above new library.
Diffstat (limited to 'admin/emacs-shell-lib')
-rw-r--r--admin/emacs-shell-lib87
1 files changed, 87 insertions, 0 deletions
diff --git a/admin/emacs-shell-lib b/admin/emacs-shell-lib
new file mode 100644
index 00000000000..750f81e0577
--- /dev/null
+++ b/admin/emacs-shell-lib
@@ -0,0 +1,87 @@
+#!/bin/bash
+### emacs-shell-lib - shared code for Emacs shell scripts
+
+## Copyright (C) 2022 Free Software Foundation, Inc.
+
+## Author: Stefan Kangas <stefankangas@gmail.com>
+
+## This file is part of GNU Emacs.
+
+## GNU Emacs 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.
+
+## GNU Emacs 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
+
+### Code:
+
+# Set an explicit umask.
+umask 077
+
+# Treat unset variables as an error.
+set -o nounset
+
+# Exit immediately on error.
+set -o errexit
+
+# Avoid non-standard command output from non-C locales.
+unset LANG LC_ALL LC_MESSAGES
+
+PN=${0##*/} # basename of script
+PD=${0%/*} # script directory
+
+[ "$PD" = "$0" ] && PD=. # if PATH includes PWD
+
+die () # write error to stderr and exit
+{
+ [ $# -gt 0 ] && echo "$PN: $@" >&2
+ exit 1
+}
+
+emacs_tempfiles=()
+
+emacs_tempfiles_cleanup ()
+{
+ for file in ${emacs_tempfiles[@]}; do
+ rm -f "${file}" 2> /dev/null
+ done
+}
+
+trap '
+ ret=$?
+ emacs_tempfiles_cleanup
+ exit $ret
+' EXIT
+
+emacs_mktemp ()
+{
+ local readonly file="${1-}"
+ local tempfile
+ local prefix
+
+ if [ -z "$file" ]; then
+ prefix="$PN"
+ else
+ prefix="$1"
+ fi
+
+ if [ -x "$(command -v mktemp)" ]; then
+ tempfile=$(mktemp "${TMPDIR-/tmp}/${prefix}.XXXXXXXXXX")
+ else
+ tempfile="${TMPDIR-/tmp}/${prefix}.$RANDOM$$"
+ (umask 077 && touch "$tempfile")
+ fi
+
+ [ -z "${tempfile}" ] && die "Creating temporary file failed"
+
+ emacs_tempfiles+=("${tempfile}")
+
+ echo "$tempfile"
+}