Refine the Custom type of generated '*-modes' options
[emacs.git] / admin / emacs-shell-lib
blob1c4d895fdb4c365d62556c289348e7aeb0ddd2af
1 #!/bin/bash
2 ### emacs-shell-lib - shared code for Emacs shell scripts
4 ## Copyright (C) 2022-2024 Free Software Foundation, Inc.
6 ## Author: Stefan Kangas <stefankangas@gmail.com>
8 ## This file is part of GNU Emacs.
10 ## GNU Emacs is free software: you can redistribute it and/or modify
11 ## it under the terms of the GNU General Public License as published by
12 ## the Free Software Foundation, either version 3 of the License, or
13 ## (at your option) any later version.
15 ## GNU Emacs is distributed in the hope that it will be useful,
16 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ## GNU General Public License for more details.
20 ## You should have received a copy of the GNU General Public License
21 ## along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
23 ### Code:
25 # Set an explicit umask.
26 umask 077
28 # Treat unset variables as an error.
29 set -o nounset
31 # Exit immediately on error.
32 set -o errexit
34 # Avoid non-standard command output from non-C locales.
35 unset LANG LC_ALL LC_MESSAGES
37 PN=${0##*/} # basename of script
38 PD=${0%/*} # script directory
40 [ "$PD" = "$0" ] && PD=. # if PATH includes PWD
42 die () # write error to stderr and exit
44 [ $# -gt 0 ] && echo "$PN: $@" >&2
45 exit 1
48 emacs_tempfiles=()
50 emacs_tempfiles_cleanup ()
52 for file in ${emacs_tempfiles[@]}; do
53 rm -f "${file}" 2> /dev/null
54 done
57 trap '
58 ret=$?
59 emacs_tempfiles_cleanup
60 exit $ret
61 ' EXIT
63 emacs_mktemp ()
65 local readonly file="${1-}"
66 local tempfile
67 local prefix
69 if [ -z "$file" ]; then
70 prefix="$PN"
71 else
72 prefix="$1"
75 if [ -x "$(command -v mktemp)" ]; then
76 tempfile=$(mktemp "${TMPDIR-/tmp}/${prefix}.XXXXXXXXXX")
77 else
78 tempfile="${TMPDIR-/tmp}/${prefix}.$RANDOM$$"
79 (umask 077 && touch "$tempfile")
82 [ -z "${tempfile}" ] && die "Creating temporary file failed"
84 emacs_tempfiles+=("${tempfile}")
86 echo "$tempfile"