]>
git.ipfire.org Git - network.git/blob - src/functions/functions.editor
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2017 IPFire Network Development Team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
23 # Open a file with the best available editor
29 for editor
in ${EDITOR} vim vi
; do
38 ${EXIT_COMMAND_NOT_FOUND})
48 error
"Unable to find a working editor"
50 return ${EXIT_COMMAND_NOT_FOUND}
57 if [ ! -f ${file} ] ||
[ ! -w ${file} ]; then
58 error
"${file} is not valid file or is not writeable"
62 lock
"${file}.lock" __editor
"$@"
66 # This function open a file for editing and take care of all preperation and postprocessing
70 local check_func
="${2}"
72 # create a temporary file
73 local temp_file
="$(mktemp)"
75 if ! [ -f "${temp_file}" ]; then
76 error
"Cannot create temporary file"
79 # copy the content into this temporary file
80 cp -f "${file}" "${temp_file}"
83 if ! editor_find_best
"${temp_file}"; then
84 error
"Could not edit ${file}"
86 # Delete temporary file
87 file_delete
"${temp_file}"
92 # run the check if we have one
93 if isset check_func
&& ! editor_check
"${check_func}" "${temp_file}"; then
94 # Delete temporary file
95 file_delete
"${temp_file}"
100 # copy the changes back
101 cp -f "${temp_file}" "${file}"
103 # Delete temporary file
104 file_delete
"${temp_file}"
108 # Execute the check function to make sure that the changes does not break anything
109 local check_func
="${1}"
112 # Execute the check function
118 ${EXIT_OK}|
${EXIT_TRUE})
119 log DEBUG
"Check succeeded"
124 ${EXIT_ERROR}|
${EXIT_FALSE})
125 log CRITICAL
"Check failed"
130 ${EXIT_COMMAND_NOT_FOUND})
131 log CRITICAL
"Check function '${check_func}' was not found"
136 log CRITICAL
"Unhandled exit code for '${check_func}': ${ret}"