svn commit: r320270 - head/usr.sbin/sesutil

Baptiste Daroussin bapt at FreeBSD.org
Fri Jun 23 15:09:10 UTC 2017


Author: bapt
Date: Fri Jun 23 15:09:08 2017
New Revision: 320270
URL: https://svnweb.freebsd.org/changeset/base/320270

Log:
  Directly print the extra status instead of filling a buffer
  then printing it.
  
  This prepares the code to make it libxo friendly
  
  Reviewed by:	manu, Nikita Kozlov (nikita elyzion.net)
  MFC after:	2 weeks
  Sponsored by:	Gandi.net

Modified:
  head/usr.sbin/sesutil/Makefile
  head/usr.sbin/sesutil/eltsub.c
  head/usr.sbin/sesutil/eltsub.h
  head/usr.sbin/sesutil/sesutil.c

Modified: head/usr.sbin/sesutil/Makefile
==============================================================================
--- head/usr.sbin/sesutil/Makefile	Fri Jun 23 15:02:49 2017	(r320269)
+++ head/usr.sbin/sesutil/Makefile	Fri Jun 23 15:09:08 2017	(r320270)
@@ -4,6 +4,4 @@ PROG=	sesutil
 SRCS=	sesutil.c eltsub.c
 MAN=	sesutil.8
 
-LIBADD=	sbuf
-
 .include <bsd.prog.mk>

Modified: head/usr.sbin/sesutil/eltsub.c
==============================================================================
--- head/usr.sbin/sesutil/eltsub.c	Fri Jun 23 15:02:49 2017	(r320269)
+++ head/usr.sbin/sesutil/eltsub.c	Fri Jun 23 15:09:08 2017	(r320270)
@@ -32,9 +32,7 @@
  * mjacob at feral.com
  */
 
-#include <sys/endian.h>
 #include <sys/types.h>
-#include <sys/sbuf.h>
 
 #include <err.h>
 #include <stddef.h>
@@ -142,53 +140,4 @@ scode2ascii(u_char code)
 		snprintf(rbuf, sizeof(rbuf), "<Status 0x%x>", code & 0xf);
 		return (rbuf);
 	}
-}
-
-struct sbuf *
-stat2sbuf(int eletype, u_char *cstat)
-{
-	struct sbuf *buf;
-
-	buf = sbuf_new_auto();
-	if (buf == NULL)
-		err(EXIT_FAILURE, "sbuf_new_auto()");
-
-	if (cstat[0] & 0x40)
-		sbuf_printf(buf, "\t\t- Predicted Failure\n");
-	if (cstat[0] & 0x20)
-		sbuf_printf(buf, "\t\t- Disabled\n");
-	if (cstat[0] & 0x10)
-		sbuf_printf(buf, "\t\t- Swapped\n");
-	switch (eletype) {
-	case ELMTYP_DEVICE:
-		if (cstat[2] & 0x02)
-			sbuf_printf(buf, "\t\t- LED=locate\n");
-		if (cstat[2] & 0x20)
-			sbuf_printf(buf, "\t\t- LED=fault\n");
-		break;
-	case ELMTYP_ARRAY_DEV:
-		if (cstat[2] & 0x02)
-			sbuf_printf(buf, "\t\t- LED=locate\n");
-		if (cstat[2] & 0x20)
-			sbuf_printf(buf, "\t\t- LED=fault\n");
-		break;
-	case ELMTYP_FAN:
-		sbuf_printf(buf, "\t\t- Speed: %d rpm\n",
-		    (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
-		break;
-	case ELMTYP_THERM:
-		if (cstat[2]) {
-			sbuf_printf(buf, "\t\t- Temperature: %d C\n",
-			    cstat[2] - TEMPERATURE_OFFSET);
-		} else {
-			sbuf_printf(buf, "\t\t- Temperature: -reserved-\n");
-		}
-		break;
-	case ELMTYP_VOM:
-		sbuf_printf(buf, "\t\t- Voltage: %.2f V\n",
-		    be16dec(cstat + 2) / 100.0);
-		break;
-	}
-	sbuf_finish(buf);
-	return (buf);
 }

Modified: head/usr.sbin/sesutil/eltsub.h
==============================================================================
--- head/usr.sbin/sesutil/eltsub.h	Fri Jun 23 15:02:49 2017	(r320269)
+++ head/usr.sbin/sesutil/eltsub.h	Fri Jun 23 15:09:08 2017	(r320270)
@@ -34,4 +34,3 @@
 
 const char *geteltnm(int);
 const char *scode2ascii(u_char);
-struct sbuf *stat2sbuf(int, u_char *);

Modified: head/usr.sbin/sesutil/sesutil.c
==============================================================================
--- head/usr.sbin/sesutil/sesutil.c	Fri Jun 23 15:02:49 2017	(r320269)
+++ head/usr.sbin/sesutil/sesutil.c	Fri Jun 23 15:09:08 2017	(r320270)
@@ -29,10 +29,10 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
+#include <sys/endian.h>
 #include <sys/param.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
-#include <sys/sbuf.h>
 
 #include <err.h>
 #include <errno.h>
@@ -56,6 +56,7 @@ static int fault(int argc, char **argv);
 static int locate(int argc, char **argv);
 static int objmap(int argc, char **argv);
 static int sesled(int argc, char **argv, bool fault);
+static void sesutil_print(bool *title, const char *fmt, ...) __printflike(2,3);
 
 static struct command {
 	const char *name;
@@ -303,10 +304,74 @@ fault(int argc, char **argv)
 	return (sesled(argc, argv, true));
 }
 
+#define TEMPERATURE_OFFSET 20
+static void
+sesutil_print(bool *title, const char *fmt, ...)
+{
+	va_list args;
+
+	if (!*title) {
+		printf("\t\tExtra status:\n");
+		*title = true;
+	}
+	va_start(args, fmt);
+	vprintf(fmt, args);
+	va_end(args);
+}
+
+static void
+print_extra_status(int eletype, u_char *cstat)
+{
+	bool title = false;
+
+	if (cstat[0] & 0x40) {
+		sesutil_print(&title, "\t\t- Predicted Failure\n");
+	}
+	if (cstat[0] & 0x20) {
+		sesutil_print(&title, "\t\t- Disabled\n");
+	}
+	if (cstat[0] & 0x10) {
+		sesutil_print(&title, "\t\t- Swapped\n");
+	}
+	switch (eletype) {
+	case ELMTYP_DEVICE:
+		if (cstat[2] & 0x02) {
+			sesutil_print(&title, "\t\t- LED=locate\n");
+		}
+		if (cstat[2] & 0x20) {
+			sesutil_print(&title, "\t\t- LED=fault\n");
+		}
+		break;
+	case ELMTYP_ARRAY_DEV:
+		if (cstat[2] & 0x02) {
+			sesutil_print(&title, "\t\t- LED=locate\n");
+		}
+		if (cstat[2] & 0x20) {
+			sesutil_print(&title, "\t\t- LED=fault\n");
+		}
+		break;
+	case ELMTYP_FAN:
+		sesutil_print(&title, "\t\t- Speed: %d rpm\n",
+		    (((0x7 & cstat[1]) << 8) + cstat[2]) * 10);
+		break;
+	case ELMTYP_THERM:
+		if (cstat[2]) {
+			sesutil_print(&title, "\t\t- Temperature: %d C\n",
+			    cstat[2] - TEMPERATURE_OFFSET);
+		} else {
+			sesutil_print(&title, "\t\t- Temperature: -reserved-\n");
+		}
+		break;
+	case ELMTYP_VOM:
+		sesutil_print(&title, "\t\t- Voltage: %.2f V\n",
+		    be16dec(cstat + 2) / 100.0);
+		break;
+	}
+}
+
 static int
 objmap(int argc, char **argv __unused)
 {
-	struct sbuf *extra;
 	encioc_string_t stri;
 	encioc_elm_devnames_t e_devname;
 	encioc_elm_status_t e_status;
@@ -422,12 +487,7 @@ objmap(int argc, char **argv __unused)
 				printf("\t\tDevice Names: %s\n",
 				    e_devname.elm_devnames);
 			}
-			extra = stat2sbuf(e_ptr[j].elm_type, e_status.cstat);
-			if (sbuf_len(extra) > 0) {
-				printf("\t\tExtra status:\n%s",
-				   sbuf_data(extra));
-			}
-			sbuf_delete(extra);
+			print_extra_status(e_ptr[j].elm_type, e_status.cstat);
 			free(e_devname.elm_devnames);
 		}
 		free(e_ptr);


More information about the svn-src-head mailing list