Colorful Status Messages in FreeBSD

NOTICE: (new)As of src/etc/rc.subr rev. 1.72 this patch may no longer work. A significant amount of the rc status messages and control have been organized in methods now. The rc.subr patch needs to be restructured to handle this new format. Hopefully, this will mean a much cleaner patch.

Yes, we've all seen them when using Linux. Colorful messages and a more elegant status message system for the rc startup system. This is an effort to add this support to FreeBSD's rc system.

I have a patch that may be applied to FreeBSD 7.0-CURRENT:

rc_fancy-cokane5.patch

--- rc.subr.orig	Sun Apr 30 18:37:42 2006
+++ rc.subr	Sun Apr 30 18:59:20 2006
@@ -319,12 +319,16 @@
 			break
 		fi
 		_list=$_nlist
-		echo -n ${_prefix:-"Waiting for PIDS: "}$_list
+		if ! checkyesno rc_fancy; then
+			echo -n ${_prefix:-"Waiting for PIDS: "}$_list
+		fi
 		_prefix=", "
 		sleep 2
 	done
 	if [ -n "$_prefix" ]; then
-		echo "."
+		if ! checkyesno rc_fancy; then
+			echo "."
+		fi
 	fi
 }
 
@@ -576,7 +580,7 @@
 				eval $_precmd $rc_extra_args
 				_return=$?
 				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
-				    return 1
+				    (echo_fancy $rc_fancy_msg_skip 1) && return 1
 			fi
 
 			if [ -n "$_cmd" ]; then
@@ -584,7 +588,7 @@
 				eval $_cmd $rc_extra_args
 				_return=$?
 				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
-				    return 1
+				    (echo_fancy $rc_fancy_msg_failed 0) && return 1
 			fi
 
 			if [ -n "$_postcmd" ]; then
@@ -592,6 +596,7 @@
 				 eval $_postcmd $rc_extra_args
 				_return=$?
 			fi
+			echo_fancy $rc_fancy_msg_ok 0
 			return $_return
 		fi
 
@@ -607,7 +612,9 @@
 			;;
 
 		start)
+ 			echo -n "Starting ${name}"
 			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
+ 				echo_fancy $rc_fancy_msg_skip 1
 				echo 1>&2 "${name} already running? (pid=$rc_pid)."
 				return 1
 			fi
@@ -624,6 +631,7 @@
 				if ! checkyesno $_f; then
 					warn "\$${_f} is not enabled."
 					if [ -z "$rc_force" ]; then
+						echo_fancy $rc_fancy_msg_skip 1
 						return 1
 					fi
 				fi
@@ -632,6 +640,7 @@
 				if [ ! -d "${_f}/." ]; then
 					warn "${_f} is not a directory."
 					if [ -z "$rc_force" ]; then
+						echo_fancy $rc_fancy_msg_error 1
 						return 1
 					fi
 				fi
@@ -640,6 +649,7 @@
 				if [ ! -r "${_f}" ]; then
 					warn "${_f} is not readable."
 					if [ -z "$rc_force" ]; then
+						echo_fancy $rc_fancy_msg_error 1
 						return 1
 					fi
 				fi
@@ -653,12 +663,11 @@
 				eval $_precmd
 				_return=$?
 				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
-				    return 1
+				    (echo_fancy $rc_fancy_msg_error 1) && return 1
 			fi
 
 					# setup the command to run, and run it
 					#
-			echo "Starting ${name}."
 			if [ -n "$_chroot" ]; then
 				_doit="\
 ${_nice:+nice -n $_nice }\
@@ -680,7 +689,7 @@
 			debug "run_rc_command: _doit: $_doit"
 			eval $_doit
 			_return=$?
-			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
+			[ $_return -ne 0 ] && [ -z "$rc_force" ] && (echo_fancy $rc_fancy_msg_failed 1) && return 1
 
 					# finally, run postcmd
 					#
@@ -688,15 +697,19 @@
 				debug "run_rc_command: evaluating ${_postcmd}()."
 				eval $_postcmd
 			fi
+ 			echo_fancy $rc_fancy_msg_ok 1
 			;;
 
 		stop)
+ 			echo -n "Stopping ${name}"
 			if [ -z "$rc_pid" ]; then
 				[ -n "$rc_fast" ] && return 0
 				if [ -n "$pidfile" ]; then
+ 					echo_fancy $rc_fancy_msg_skip 1
 					echo 1>&2 \
 				    "${name} not running? (check $pidfile)."
 				else
+ 					echo_fancy $rc_fancy_msg_skip 1
 					echo 1>&2 "${name} not running?"
 				fi
 				return 1
@@ -709,12 +722,11 @@
 				eval $_precmd
 				_return=$?
 				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
-				    return 1
+					(echo_fancy $rc_fancy_msg_error 1) && return 1
 			fi
 
 					# send the signal to stop
 					#
-			echo "Stopping ${name}."
 			_doit="kill -${sig_stop:-TERM} $rc_pid"
 			if [ -n "$_user" ]; then
 				_doit="su -m $_user -c 'sh -c \"$_doit\"'"
@@ -725,7 +737,7 @@
 					#
 			eval $_doit
 			_return=$?
-			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
+			[ $_return -ne 0 ] && [ -z "$rc_force" ] && (echo_fancy $rc_fancy_msg_failed 1) && return 1
 
 					# wait for the command to exit,
 					# and run postcmd.
@@ -734,14 +746,17 @@
 				eval $_postcmd
 				_return=$?
 			fi
+ 			echo_fancy $rc_fancy_msg_ok 1
 			;;
 
 		reload)
 			if [ -z "$rc_pid" ]; then
 				if [ -n "$pidfile" ]; then
+ 					echo_fancy $rc_fancy_msg_skipped 1
 					echo 1>&2 \
 				    "${name} not running? (check $pidfile)."
 				else
+ 					echo_fancy $rc_fancy_msg_skipped 1
 					echo 1>&2 "${name} not running?"
 				fi
 				return 1
@@ -751,7 +766,7 @@
 				eval $_precmd
 				_return=$?
 				[ $_return -ne 0 ] && [ -z "$rc_force" ] &&
-				    return 1
+				    (echo_fancy $rc_fancy_msg_failed 1) && return 1
 			fi
 			_doit="kill -${sig_reload:-HUP} $rc_pid"
 			if [ -n "$_user" ]; then
@@ -759,11 +774,12 @@
 			fi
 			eval $_doit
 			_return=$?
-			[ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1
+			[ $_return -ne 0 ] && [ -z "$rc_force" ] && (echo_fancy $rc_fancy_msg_failed 1) && return 1
 			if [ -n "$_postcmd" ]; then
 				eval $_postcmd
 				_return=$?
 			fi
+			echo_fancy $rc_fancy_msg_ok 1
 			;;
 
 		restart)
@@ -1461,5 +1477,53 @@
 }
 
 fi
+
+echo_fancy () {
+	if checkyesno rc_fancy; then
+	    local padding;
+		local do_msg_width;
+		columns=$rc_fancy_cols
+		label=$1
+		labelprefix=""
+		labelsuffix=""
+		labelprepad=""
+		labelpostpad=""
+		do_msg_width=${#label}
+		if [ $rc_fancy_msg_width -gt ${#label} ]; then
+			spaceavail=$(($rc_fancy_msg_width - ${#label}))
+			labelprepad=$(($spaceavail / 2))
+			labelpostpad=$(($spaceavail - $labelprepad))
+			labelpostpad="\e[${labelpostpad}C"
+			labelprepad="\e[${labelprepad}C"
+			do_msg_width=$rc_fancy_msg_width
+		fi
+		padding="\e[$(($columns - $do_msg_width - 3))\`"
+		if checkyesno rc_fancy_color || checkyesno rc_fancy_colour; then
+			labelsuffix="\e[0m"
+			case $label in
+			$rc_fancy_msg_ok)
+				labelprefix="\e[1;${rc_fancy_good_color};49m"
+				;;
+			$rc_fancy_msg_error|$rc_fancy_msg_failed)
+				labelprefix="\e[1;${rc_fancy_error_color};49m"
+				;;
+			$rc_fancy_msg_skip|$rc_fancy_msg_skipped)
+				labelprefix="\e[1;${rc_fancy_warn_color};49m"
+				;;
+			esac
+		fi
+		if [ $2 = 0 ]; then
+			if checkyesno rc_fancy_verbose; then
+				echo -e "\rRunning ${rc_arg} $name ${padding} [${labelprepad}${labelprefix}${label}${labelsuffix}${labelpostpad}]"
+			fi
+		else
+			echo -e " ... ${padding} [${labelprepad}${labelprefix}${label}${labelsuffix}${labelpostpad}]"
+		fi
+	else
+		if [ $2 = 0 ]; then
+			echo "."
+		fi
+	fi
+}
 
 _rc_subr_loaded=:
--- defaults/rc.conf.orig	Sun Apr 30 18:37:50 2006
+++ defaults/rc.conf	Sun Apr 30 18:56:29 2006
@@ -570,6 +570,30 @@
 #jail_example_fstab=""				# fstab(5) for mount/umount
 #jail_example_flags="-l -U root"		# flags for jail(8)
 
+
+##############################################################
+### Fancy RC Configuration ###################################
+##############################################################
+rc_fancy="NO"                   # Turn on "fancy" rc reporting
+rc_fancy_color="NO"             # Colorize the fancy rc messages
+rc_fancy_colour="NO"            #    ditto for europeans
+rc_fancy_msg_ok="OK"            # The message printed for OK status
+rc_fancy_msg_failed="FAILED"    # The message printed with FAILED status
+rc_fancy_msg_error="ERROR"      # The message printed with ERROR status
+rc_fancy_msg_skipped="SKIPPED"  # The message when an rc is skipped
+rc_fancy_msg_skip="SKIP"        # The message when an rc is set as SKIP
+rc_fancy_msg_width="6"          # Width of the message field
+rc_fancy_cols="80"              # Width of the screen
+rc_fancy_verbose="NO"           # Print -all- rc messages
+# The VT102 codes are used for the colors here:
+# 30 = Black, 31 = Red, 32 = Green, 33 = Yellow, 34 = Blue,
+# 35 = Magenta, 36 = Cyan, 37 = White, 39 = Normal
+# the background is left as "Normal"
+# the attribute set has "Bold" (1) turned on
+rc_fancy_warn_color="33"        # Warning color (skip/skipped)
+rc_fancy_error_color="31"       # Error color (error/failed)
+rc_fancy_good_color="32"        # Good color (ok)
+
 ##############################################################
 ### Define source_rc_confs, the mechanism used by /etc/rc.* ##
 ### scripts to source rc_conf_files overrides safely.	    ##
 
freebsd/colored_status_messages.txt · Last modified: 2007-03-12 by cokane
 
Recent changes RSS feed