2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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 CLR_BLACK_R
='\e[0;30m'
25 CLR_GREEN_R
='\e[0;32m'
26 CLR_YELLOW_R
='\e[0;33m'
28 CLR_PURPLE_R
='\e[0;35m'
30 CLR_WHITE_R
='\e[0;37m'
33 CLR_BLACK_B
='\e[1;30m'
35 CLR_GREEN_B
='\e[1;32m'
36 CLR_YELLOW_B
='\e[1;33m'
38 CLR_PURPLE_B
='\e[1;35m'
40 CLR_WHITE_B
='\e[1;37m'
46 CLR_YELLOW_BG
='\e[43m'
48 CLR_PURPLE_BG
='\e[45m'
55 FONT_UNDERLINED
="\e[4m"
60 CLR_RESET
="${FONT_RESET}"
63 MSG_HOOK_UP
="${CLR_GREEN_BG}${CLR_WHITE_B} UP ${CLR_RESET}"
64 MSG_HOOK_DOWN
="${CLR_RED_BG}${CLR_WHITE_B} DOWN ${CLR_RESET}"
66 MSG_DEVICE_STATUS_UNKNOWN
="${CLR_GREY_BG}${CLR_BLACK_B} UNKNOWN ${CLR_RESET}"
67 MSG_DEVICE_STATUS_UP
="${CLR_GREEN_BG}${CLR_WHITE_B} UP ${CLR_RESET}"
68 MSG_DEVICE_STATUS_DOWN
="${CLR_RED_BG}${CLR_WHITE_B} DOWN ${CLR_RESET}"
69 MSG_DEVICE_STATUS_NOCARRIER
="${CLR_YELLOW_BG}${CLR_WHITE_B} NO-CARRIER ${CLR_RESET}"
71 MSG_STP_FORWARDING
="${CLR_GREEN_BG}${CLR_WHITE_B} FORWARDING ${CLR_RESET}"
72 MSG_STP_DISCARDING
="${CLR_RED_BG}${CLR_WHITE_B} DISCARDING ${CLR_RESET}"
73 MSG_STP_LEARNING
="${CLR_YELLOW_BG}${CLR_WHITE_B} LEARNING ${CLR_RESET}"
74 MSG_STP_LISTENING
="${CLR_YELLOW_BG}${CLR_WHITE_B} LISTENING ${CLR_RESET}"
75 MSG_STP_BLOCKING
="${CLR_RED_BG}${CLR_WHITE_B} BLOCKING ${CLR_RESET}"
78 # Is the cli function to parse the options submitted by a user.
86 # Check if we get to many arguments
88 # We want to print only the 5th and greater argument
90 error
"Too many arguments: $@"
93 color_set
${type} ${name} ${color}
96 # We set the color to white.
97 # Check if we get to many arguments
100 # We want to print only the 4th and greater argument
102 error
"Too many arguments: $@"
105 color_set
${type} ${name} "ffffff"
108 error
"Invalid argument: ${action}"
115 # Write a given color into the color config file of a zone or port.
121 # Check if we get to many arguments
122 # Check if the color code is valid
123 if ! color_hex_is_valid
${COLOR}; then
124 error
"Hexadecimal color code '${COLOR}' is not valid"
128 local file=$
(color_format_filename
${type} ${name})
129 settings_write
${file} COLOR
133 # Read a color out of color config file of a zone or port.
134 # If this is unsuccessful we use white.
138 local file=$
(color_format_filename
${type} ${name})
142 if ! settings_read
${file} COLOR
; then
149 color_format_filename
() {
150 # Formats the color config file name.
155 echo "$(zone_dir ${name})/color"
158 echo "$(port_dir ${name})/color"
163 color_hex_is_valid
() {
164 # Check if a color hex is valid.
165 [[ ${1} =~ ^
[0-9a-fA-F]{6}$
]]
169 # Converts a color hex into rgb values.
172 assert
[ ${#hex} -eq 6 ]
174 for (( i
= 0; i
< 6; i
+= 2 )); do
175 hex2dec
${hex:${i}:2}
181 _find_nearest_rgb_value
() {
182 # For the calculation of the xterm value the rgb values must be:
183 # 0; 95; 135; 175; 215; 255;
184 # this function find the closest value of these 6 numbers for a give rgb number
188 local best_value_index
190 local values
=( 0 95 135 175 215 255 )
195 for value
in ${values[@]}; do
196 result
=$
(( ${value} - ${rgb} ))
197 result
=$
(abs
${result})
199 if [ -z ${best_value} ]; then
201 best_value_index
=${i}
203 # In the first iteration best_value is empty and so set to ${result}
204 # two lines above. So if statement must use -le because in the first iteration
205 # is the best_value eqal to result
206 elif [ ${result} -le ${best_value} ]; then
208 best_value_index
=${i}
214 echo "${best_value_index}"
218 # Converts a rgb value triple into an xterm color code.
226 for color
in red green blue
; do
227 printf -v "${color}" $
(_find_nearest_rgb_value
${!color})
230 print $
(( 16 + 36 * ${red} + 6 * ${green} + ${blue} ))
234 # Set the shell color which unfourtunately does not work for putty.
248 # Convert color from hex to RGB
250 read red green blue
<<< $
(color_hex2rgb
${color})
252 # Set standard shell color
253 local shell_color
=$
(color_rgb2shell
${red} ${green} ${blue})
254 printf "${prefix};5;${shell_color}m"
256 # For shells that support it, we will try to set the RGB color code
259 # PuTTY is a piece of garbage and does not know
260 # how to handle colors at all although it has nice
261 # checkboxes to enable them, but they actually make
262 # things even worse. So no colors for you Windows
266 printf "${prefix};2;${red};${green};${blue}m"