socsvn commit: r271186 - soc2014/zkorchev/freebsd_head/usr.bin/w

zkorchev at FreeBSD.org zkorchev at FreeBSD.org
Mon Jul 21 09:47:55 UTC 2014


Author: zkorchev
Date: Mon Jul 21 09:47:53 2014
New Revision: 271186
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=271186

Log:
  libsol support for w/uptime

Modified:
  soc2014/zkorchev/freebsd_head/usr.bin/w/Makefile
  soc2014/zkorchev/freebsd_head/usr.bin/w/extern.h
  soc2014/zkorchev/freebsd_head/usr.bin/w/pr_time.c
  soc2014/zkorchev/freebsd_head/usr.bin/w/w.c

Modified: soc2014/zkorchev/freebsd_head/usr.bin/w/Makefile
==============================================================================
--- soc2014/zkorchev/freebsd_head/usr.bin/w/Makefile	Mon Jul 21 08:47:54 2014	(r271185)
+++ soc2014/zkorchev/freebsd_head/usr.bin/w/Makefile	Mon Jul 21 09:47:53 2014	(r271186)
@@ -4,8 +4,9 @@
 PROG=	w
 SRCS=	fmt.c pr_time.c proc_compare.c w.c
 MAN=	w.1 uptime.1
+CFLAGS+=-DSOL_ON -I/usr/local/include
 DPADD=	${LIBKVM} ${LIBUTIL}
-LDADD=	-lkvm -lutil
+LDADD=	-lkvm -lutil -lsol
 #BINGRP= kmem
 #BINMODE=2555
 LINKS=	${BINDIR}/w ${BINDIR}/uptime

Modified: soc2014/zkorchev/freebsd_head/usr.bin/w/extern.h
==============================================================================
--- soc2014/zkorchev/freebsd_head/usr.bin/w/extern.h	Mon Jul 21 08:47:54 2014	(r271185)
+++ soc2014/zkorchev/freebsd_head/usr.bin/w/extern.h	Mon Jul 21 09:47:53 2014	(r271186)
@@ -32,6 +32,11 @@
 
 extern	int use_ampm;
 
+#if defined(SOL_ON)
+extern struct sol_stream sol_stream;
+#endif
+extern int sol_format;
+
 struct kinfo_proc;
 int	pr_attime(time_t *, time_t *);
 int	pr_idle(time_t);

Modified: soc2014/zkorchev/freebsd_head/usr.bin/w/pr_time.c
==============================================================================
--- soc2014/zkorchev/freebsd_head/usr.bin/w/pr_time.c	Mon Jul 21 08:47:54 2014	(r271185)
+++ soc2014/zkorchev/freebsd_head/usr.bin/w/pr_time.c	Mon Jul 21 09:47:53 2014	(r271186)
@@ -41,6 +41,9 @@
 #include <stdio.h>
 #include <string.h>
 #include <wchar.h>
+#if defined(SOL_ON)
+# include <sol.h>
+#endif
 
 #include "extern.h"
 
@@ -56,14 +59,18 @@
 	time_t diff;
 	const wchar_t *fmt;
 	int len, width, offset = 0;
+	static char sol_buf[256];
+	const char *sol_fmt;
 
 	tp = *localtime(started);
 	tm = *localtime(now);
 	diff = *now - *started;
 
 	/* If more than a week, use day-month-year. */
-	if (diff > 86400 * 7)
+	if (diff > 86400 * 7) {
 		fmt = L"%d%b%y";
+		sol_fmt = "%d%b%y";
+	}
 
 	/* If not today, use day-hour-am/pm. */
 	else if (tm.tm_mday != tp.tm_mday ||
@@ -72,23 +79,36 @@
 	/* The line below does not take DST into consideration */
 	/* else if (*now / 86400 != *started / 86400) { */
 		fmt = use_ampm ? L"%a%I%p" : L"%a%H";
+		sol_fmt = use_ampm ? "%a%I%p" : "%a%H";
 	}
 
 	/* Default is hh:mm{am,pm}. */
 	else {
 		fmt = use_ampm ? L"%l:%M%p" : L"%k:%M";
+		sol_fmt = use_ampm ? "%l:%M%p" : "%k:%M";
 	}
 
-	(void)wcsftime(buf, sizeof(buf), fmt, &tp);
-	len = wcslen(buf);
-	width = wcswidth(buf, len);
-	if (len == width)
-		(void)wprintf(L"%-7.7ls", buf);
-	else if (width < 7)
-		(void)wprintf(L"%ls%.*s", buf, 7 - width, "      ");
-	else {
-		(void)wprintf(L"%ls", buf);
-		offset = width - 7;
+#if defined(SOL_ON)
+	if (sol_format)
+	{
+		SOL_MAP_KEYL(&sol_stream, "login");
+		strftime(sol_buf, sizeof(sol_buf), sol_fmt, &tp);
+		sol_string(&sol_stream, sol_buf, strlen(sol_buf));
+	}
+	else
+#endif
+	{
+		(void)wcsftime(buf, sizeof(buf), fmt, &tp);
+		len = wcslen(buf);
+		width = wcswidth(buf, len);
+		if (len == width)
+			(void)wprintf(L"%-7.7ls", buf);
+		else if (width < 7)
+			(void)wprintf(L"%ls%.*s", buf, 7 - width, "      ");
+		else {
+			(void)wprintf(L"%ls", buf);
+			offset = width - 7;
+		}
 	}
 	return (offset);
 }
@@ -104,7 +124,14 @@
 	/* If idle more than 36 hours, print as a number of days. */
 	if (idle >= 36 * 3600) {
 		int days = idle / 86400;
-		(void)printf(" %dday%s ", days, days > 1 ? "s" : " " );
+#if defined(SOL_ON)
+		if (sol_format) {
+			SOL_MAP_KEYL(&sol_stream, "days");
+			sol_integer(&sol_stream, days);
+		}
+		else
+#endif
+			(void)printf(" %dday%s ", days, days > 1 ? "s" : " " );
 		if (days >= 100)
 			return (2);
 		if (days >= 10)
@@ -112,16 +139,38 @@
 	}
 
 	/* If idle more than an hour, print as HH:MM. */
-	else if (idle >= 3600)
-		(void)printf(" %2d:%02d ",
-		    (int)(idle / 3600), (int)((idle % 3600) / 60));
+	else if (idle >= 3600) {
+#if defined(SOL_ON)
+		if (sol_format) {
+			SOL_MAP_KEYL(&sol_stream, "hours");
+			sol_integer(&sol_stream, idle / 3600);
+			SOL_MAP_KEYL(&sol_stream, "minutes");
+			sol_integer(&sol_stream, (idle % 3600) / 60);
+		}
+		else
+#endif
+			(void)printf(" %2d:%02d ",
+				(int)(idle / 3600), (int)((idle % 3600) / 60));
+	}
 
-	else if (idle / 60 == 0)
-		(void)printf("     - ");
+	else if (idle / 60 == 0) {
+#if defined(SOL_ON)
+		if (!sol_format)
+#endif
+			(void)printf("     - ");
+	}
 
 	/* Else print the minutes idle. */
-	else
-		(void)printf("    %2d ", (int)(idle / 60));
+	else {
+#if defined(SOL_ON)
+		if (sol_format) {
+			SOL_MAP_KEYL(&sol_stream, "minutes");
+			sol_integer(&sol_stream, idle / 60);
+		}
+		else
+#endif
+			(void)printf("    %2d ", (int)(idle / 60));
+	}
 
 	return (0); /* not idle longer than 9 days */
 }

Modified: soc2014/zkorchev/freebsd_head/usr.bin/w/w.c
==============================================================================
--- soc2014/zkorchev/freebsd_head/usr.bin/w/w.c	Mon Jul 21 08:47:54 2014	(r271185)
+++ soc2014/zkorchev/freebsd_head/usr.bin/w/w.c	Mon Jul 21 09:47:53 2014	(r271186)
@@ -82,6 +82,9 @@
 #include <unistd.h>
 #include <utmpx.h>
 #include <vis.h>
+#if defined(SOL_ON)
+# include <sol.h>
+#endif
 
 #include "extern.h"
 
@@ -99,6 +102,11 @@
 static int	use_comma;      /* use comma as floats separator */
 static char   **sel_users;	/* login array of particular users selected */
 
+#if defined(SOL_ON)
+struct sol_stream sol_stream;
+#endif
+int sol_format;
+
 /*
  * One of these per active utmp entry.
  */
@@ -254,10 +262,21 @@
 	}
 	endutxent();
 
+#if defined(SOL_ON)
+	sol_format = sol_init(&sol_stream);
+#endif
+
+#if defined(SOL_ON)
+	if ((header && !sol_format) || wcmd == 0) {
+#else
 	if (header || wcmd == 0) {
+#endif
 		pr_header(&now, nusers);
 		if (wcmd == 0) {
 			(void)kvm_close(kd);
+#if defined(SOL_ON)
+			if (sol_format) sol_term(&sol_stream);
+#endif
 			exit(0);
 		}
 
@@ -342,6 +361,9 @@
 		}
 	}
 
+#if defined(SOL_ON)
+	if (sol_format) sol_array_start(&sol_stream);
+#endif
 	for (ep = ehead; ep != NULL; ep = ep->next) {
 		struct addrinfo hints, *res;
 		struct sockaddr_storage ss;
@@ -411,21 +433,56 @@
 				    dkp->ki_pid, ptr);
 			}
 		}
-		(void)printf("%-*.*s %-*.*s %-*.*s ",
-		    W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
-		    W_DISPLINESIZE, W_DISPLINESIZE,
-		    *ep->utmp.ut_line ?
-		    (strncmp(ep->utmp.ut_line, "tty", 3) &&
-		    strncmp(ep->utmp.ut_line, "cua", 3) ?
-		    ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-",
-		    W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
+#if defined(SOL_ON)
+		if (sol_format)
+		{
+			SOL_MAP_KEYL(&sol_stream, "user");
+			sol_string(&sol_stream, ep->utmp.ut_user, strlen(ep->utmp.ut_user));
+			if (*ep->utmp.ut_line) {
+				const char *tty = ep->utmp.ut_line;
+
+				if (!strncmp(ep->utmp.ut_line, "tty", 3) || !strncmp(ep->utmp.ut_line, "cua", 3))
+					tty += 3;
+				SOL_MAP_KEYL(&sol_stream, "tty");
+				sol_string(&sol_stream, tty, strlen(tty));
+			}
+			if (*p) {
+				SOL_MAP_KEYL(&sol_stream, "from");
+				sol_string(&sol_stream, p, strlen(p));
+			}
+		}
+		else
+#endif
+		{
+			(void)printf("%-*.*s %-*.*s %-*.*s ",
+				W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
+				W_DISPLINESIZE, W_DISPLINESIZE,
+				*ep->utmp.ut_line ?
+				(strncmp(ep->utmp.ut_line, "tty", 3) &&
+				strncmp(ep->utmp.ut_line, "cua", 3) ?
+				ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-",
+				W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
+		}
 		t = ep->utmp.ut_tv.tv_sec;
 		longattime = pr_attime(&t, &now);
 		longidle = pr_idle(ep->idle);
-		(void)printf("%.*s\n", argwidth - longidle - longattime,
-		    ep->args);
+#if defined(SOL_ON)
+		if (sol_format) {
+			SOL_MAP_KEYL(&sol_stream, "what");
+			sol_string(&sol_stream, ep->args, strlen(ep->args));
+		}
+		else
+#endif
+			(void)printf("%.*s\n", argwidth - longidle - longattime,
+				ep->args);
 	}
 	(void)kvm_close(kd);
+#if defined(SOL_ON)
+	if (sol_format) {
+		sol_array_end(&sol_stream);
+		sol_term(&sol_stream);
+	}
+#endif
 	exit(0);
 }
 
@@ -438,12 +495,24 @@
 	int days, hrs, i, mins, secs;
 	char buf[256];
 
+#if defined(SOL_ON)
+	if (sol_format) sol_map_start(&sol_stream);
+#endif
+
 	/*
 	 * Print time of day.
 	 */
 	if (strftime(buf, sizeof(buf),
-	    use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
-		(void)printf("%s ", buf);
+	    use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0) {
+#if defined(SOL_ON)
+		if (sol_format) {
+			SOL_MAP_KEYL(&sol_stream, "time");
+			sol_string(&sol_stream, buf, strlen(buf));
+		}
+		else
+#endif
+			(void)printf("%s ", buf);
+	}
 	/*
 	 * Print how long system has been up.
 	 */
@@ -457,36 +526,92 @@
 		uptime %= 3600;
 		mins = uptime / 60;
 		secs = uptime % 60;
-		(void)printf(" up");
-		if (days > 0)
-			(void)printf(" %d day%s,", days, days > 1 ? "s" : "");
-		if (hrs > 0 && mins > 0)
-			(void)printf(" %2d:%02d,", hrs, mins);
-		else if (hrs > 0)
-			(void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
-		else if (mins > 0)
-			(void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
+
+#if defined(SOL_ON)
+		if (sol_format)
+		{
+			SOL_MAP_KEYL(&sol_stream, "uptime");
+			sol_map_start(&sol_stream);
+			if (days > 0) {
+				SOL_MAP_KEYL(&sol_stream, "days");
+				sol_integer(&sol_stream, days);
+			}
+			if (hrs > 0) {
+				SOL_MAP_KEYL(&sol_stream, "hours");
+				sol_integer(&sol_stream, hrs);
+			}
+			else if (mins > 0) {
+				SOL_MAP_KEYL(&sol_stream, "minutes");
+				sol_integer(&sol_stream, mins);
+			}
+			else {
+				SOL_MAP_KEYL(&sol_stream, "seconds");
+				sol_integer(&sol_stream, secs);
+			}
+			sol_map_end(&sol_stream);
+		}
 		else
-			(void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
+#endif
+		{
+			(void)printf(" up");
+			if (days > 0)
+				(void)printf(" %d day%s,", days, days > 1 ? "s" : "");
+			if (hrs > 0 && mins > 0)
+				(void)printf(" %2d:%02d,", hrs, mins);
+			else if (hrs > 0)
+				(void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : "");
+			else if (mins > 0)
+				(void)printf(" %d min%s,", mins, mins > 1 ? "s" : "");
+			else
+				(void)printf(" %d sec%s,", secs, secs > 1 ? "s" : "");
+		}
 	}
 
 	/* Print number of users logged in to system */
-	(void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
+#if defined(SOL_ON)
+	if (sol_format) {
+		SOL_MAP_KEYL(&sol_stream, "users");
+		sol_integer(&sol_stream, nusers);
+	}
+	else
+#endif
+		(void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s");
 
 	/*
 	 * Print 1, 5, and 15 minute load averages.
 	 */
-	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
-		(void)printf(", no load average information available\n");
+	if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) {
+#if defined(SOL_ON)
+		if (!sol_format)
+#endif
+			(void)printf(", no load average information available\n");
+	}
 	else {
-		(void)printf(", load averages:");
-		for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
-			if (use_comma && i > 0)
-				(void)printf(",");
-			(void)printf(" %.2f", avenrun[i]);
+#if defined(SOL_ON)
+		if (sol_format)
+		{
+			SOL_MAP_KEYL(&sol_stream, "load");
+			sol_array_start(&sol_stream);
+			for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++)
+				sol_float(&sol_stream, avenrun[i]);
+			sol_array_end(&sol_stream);
+		}
+		else
+#endif
+		{
+			(void)printf(", load averages:");
+			for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
+				if (use_comma && i > 0)
+					(void)printf(",");
+				(void)printf(" %.2f", avenrun[i]);
+			}
+			(void)printf("\n");
 		}
-		(void)printf("\n");
 	}
+
+#if defined(SOL_ON)
+	if (sol_format) sol_map_end(&sol_stream);
+#endif
 }
 
 static struct stat *


More information about the svn-soc-all mailing list