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 ###############################################################################
22 description_format_filename
() {
23 # Format the filename of a description file for a given zone or port
29 echo "$(zone_dir ${name})/description"
32 echo "$(port_dir ${name})/description"
37 description_touch_file
() {
38 # If the description file does not exist
39 # and we have no directory with the given name this function creates the file
43 local file=$
(description_format_filename
${type} ${name})
45 # We use the -e switch here because we want to touch
46 # when also no directory with this name exist
47 if ! [ -e ${file} ]; then
52 description_title_read
() {
53 # This function reads the title out of a given description file
57 [ -r "${file}" ] ||
return ${EXIT_ERROR}
60 read -r title
< ${file}
65 # This function provides a higher level interface special for description files of the editor function.
69 description_touch_file
${type} ${name}
71 local file=$
(description_format_filename
${type} ${name})
72 editor
${file} "description_check"
76 # This function prints a given description file.
80 local file=$
(description_format_filename
${type} ${name})
82 if [ ! -r "${file}" ] ||
[ ! -f "${file}" ]; then
83 warning
"${file} is not readable"
87 local title
=$
(description_title_read
${file})
89 cli_headline
1 "Description"
91 cli_print
2 "${title}"
94 # True if we are in the first line
97 # True if we get from the second line on, only whitespace or empty lines
98 local front_white
=true
100 # How many blank lines did we get
101 local white_counter
=0
104 if ${first_line}; then
105 # We are in the first line and pass they so first_line is now false
109 # Check if the line is blank or contain only whitespace
110 if ${front_white} && [[ "${line}" =~ ^
(|
[[:space
:]]+)$
]]; then
111 # The we do not print them
114 # we have found after the second line which is not blank or contain only white space so
115 # front_white is false. Now ew print empyt line but only if they are follewd by a non empty line.
117 if [[ "${line}" == "" ]] ||
[[ "${line}" =~ ^
[[:space
:]]$
]]; then
118 # If the line is blank or contain only white space we increase the counter.
119 (( white_counter
++ ))
121 # The line is not blank so we print all blank lines till now and print the current line after.
122 if [ ${white_counter} -gt 0 ]; then
123 for (( i
= 1; i
<= ${white_counter}; i
+= 1 )); do
127 # The counter is now zero, because the lines were printed.
130 cli_print
2 "${line}"
139 # Function for the command line interface
147 description_print
${type} ${name} ${@}
150 description_edit
${type} ${name} ${@}
153 error
"Invalid argument: ${action}"
159 description_check_title
() {
160 # Checks if the title is too long and if so prints a warning
164 local title_length
=40
166 # Have to be shorter then ${title_length}
167 if [ ${#title} -gt ${title_length} ]; then
168 warning
"Title '${title}' is to long. Only titles with ${title_length} or less chracters are allowed"
175 description_check
() {
176 # Check if a description file satisfy our needs
180 local title
=$
(description_title_read
${file})
182 description_check_title
"${title}"