Convert to use libxo.

Obtained from:  Phil Shafer <phil@juniper.net>
Sponsored by:   Juniper Networks, Inc.
This commit is contained in:
Marcel Moolenaar 2014-11-05 23:54:33 +00:00
parent 982247d2cf
commit 76c0abf129
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=274151
3 changed files with 82 additions and 37 deletions

View File

@ -4,8 +4,8 @@
PROG= w PROG= w
SRCS= fmt.c pr_time.c proc_compare.c w.c SRCS= fmt.c pr_time.c proc_compare.c w.c
MAN= w.1 uptime.1 MAN= w.1 uptime.1
DPADD= ${LIBKVM} ${LIBUTIL} DPADD= ${LIBKVM} ${LIBUTIL} ${LIBXO}
LDADD= -lkvm -lutil LDADD= -lkvm -lutil -lxo
#BINGRP= kmem #BINGRP= kmem
#BINMODE=2555 #BINMODE=2555
LINKS= ${BINDIR}/w ${BINDIR}/uptime LINKS= ${BINDIR}/w ${BINDIR}/uptime

View File

@ -41,6 +41,7 @@ static const char sccsid[] = "@(#)pr_time.c 8.2 (Berkeley) 4/4/94";
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <wchar.h> #include <wchar.h>
#include <libxo/xo.h>
#include "extern.h" #include "extern.h"
@ -82,12 +83,14 @@ pr_attime(time_t *started, time_t *now)
(void)wcsftime(buf, sizeof(buf), fmt, &tp); (void)wcsftime(buf, sizeof(buf), fmt, &tp);
len = wcslen(buf); len = wcslen(buf);
width = wcswidth(buf, len); width = wcswidth(buf, len);
xo_attr("since", "%lu", (unsigned long) *started);
xo_attr("delta", "%lu", (unsigned long) diff);
if (len == width) if (len == width)
(void)wprintf(L"%-7.7ls", buf); xo_emit("{:login-time/%-7.7ls/%ls}", buf);
else if (width < 7) else if (width < 7)
(void)wprintf(L"%ls%.*s", buf, 7 - width, " "); xo_emit("{:login-time/%ls}%.*s", buf, 7 - width, " ");
else { else {
(void)wprintf(L"%ls", buf); xo_emit("{:login-time/%ls}", buf);
offset = width - 7; offset = width - 7;
} }
return (offset); return (offset);
@ -104,7 +107,7 @@ pr_idle(time_t idle)
/* If idle more than 36 hours, print as a number of days. */ /* If idle more than 36 hours, print as a number of days. */
if (idle >= 36 * 3600) { if (idle >= 36 * 3600) {
int days = idle / 86400; int days = idle / 86400;
(void)printf(" %dday%s ", days, days > 1 ? "s" : " " ); xo_emit(" {:idle/%dday%s} ", days, days > 1 ? "s" : " " );
if (days >= 100) if (days >= 100)
return (2); return (2);
if (days >= 10) if (days >= 10)
@ -113,15 +116,15 @@ pr_idle(time_t idle)
/* If idle more than an hour, print as HH:MM. */ /* If idle more than an hour, print as HH:MM. */
else if (idle >= 3600) else if (idle >= 3600)
(void)printf(" %2d:%02d ", xo_emit(" {:idle/%2d:%02d/} ",
(int)(idle / 3600), (int)((idle % 3600) / 60)); (int)(idle / 3600), (int)((idle % 3600) / 60));
else if (idle / 60 == 0) else if (idle / 60 == 0)
(void)printf(" - "); xo_emit(" - ");
/* Else print the minutes idle. */ /* Else print the minutes idle. */
else else
(void)printf(" %2d ", (int)(idle / 60)); xo_emit(" {:idle/%2d} ", (int)(idle / 60));
return (0); /* not idle longer than 9 days */ return (0); /* not idle longer than 9 days */
} }

View File

@ -83,6 +83,7 @@ static const char sccsid[] = "@(#)w.c 8.4 (Berkeley) 4/16/94";
#include <unistd.h> #include <unistd.h>
#include <utmpx.h> #include <utmpx.h>
#include <vis.h> #include <vis.h>
#include <libxo/xo.h>
#include "extern.h" #include "extern.h"
@ -133,7 +134,7 @@ main(int argc, char *argv[])
struct stat *stp; struct stat *stp;
time_t touched; time_t touched;
int ch, i, nentries, nusers, wcmd, longidle, longattime, dropgid; int ch, i, nentries, nusers, wcmd, longidle, longattime, dropgid;
const char *memf, *nlistf, *p; const char *memf, *nlistf, *p, *save_p;
char *x_suffix; char *x_suffix;
char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX]; char buf[MAXHOSTNAMELEN], errbuf[_POSIX2_LINE_MAX];
char fn[MAXHOSTNAMELEN]; char fn[MAXHOSTNAMELEN];
@ -143,6 +144,10 @@ main(int argc, char *argv[])
use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0');
use_comma = (*nl_langinfo(RADIXCHAR) != ','); use_comma = (*nl_langinfo(RADIXCHAR) != ',');
argc = xo_parse_args(argc, argv);
if (argc < 0)
exit(1);
/* Are we w(1) or uptime(1)? */ /* Are we w(1) or uptime(1)? */
if (strcmp(basename(argv[0]), "uptime") == 0) { if (strcmp(basename(argv[0]), "uptime") == 0) {
wcmd = 0; wcmd = 0;
@ -254,9 +259,12 @@ main(int argc, char *argv[])
} }
endutxent(); endutxent();
xo_open_container("uptime-information");
if (header || wcmd == 0) { if (header || wcmd == 0) {
pr_header(&now, nusers); pr_header(&now, nusers);
if (wcmd == 0) { if (wcmd == 0) {
xo_close_container("uptime-information");
(void)kvm_close(kd); (void)kvm_close(kd);
exit(0); exit(0);
} }
@ -268,7 +276,7 @@ main(int argc, char *argv[])
#define HEADER_WHAT "WHAT\n" #define HEADER_WHAT "WHAT\n"
#define WUSED (W_DISPUSERSIZE + W_DISPLINESIZE + W_DISPHOSTSIZE + \ #define WUSED (W_DISPUSERSIZE + W_DISPLINESIZE + W_DISPHOSTSIZE + \
sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */ sizeof(HEADER_LOGIN_IDLE) + 3) /* header width incl. spaces */
(void)printf("%-*.*s %-*.*s %-*.*s %s", xo_emit("{T:/%-*.*s} {T:/%-*.*s} {T:/%-*.*s} {T:/%s}",
W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER, W_DISPUSERSIZE, W_DISPUSERSIZE, HEADER_USER,
W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY, W_DISPLINESIZE, W_DISPLINESIZE, HEADER_TTY,
W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM, W_DISPHOSTSIZE, W_DISPHOSTSIZE, HEADER_FROM,
@ -342,6 +350,9 @@ main(int argc, char *argv[])
} }
} }
xo_open_container("user-table");
xo_open_list("user-entry");
for (ep = ehead; ep != NULL; ep = ep->next) { for (ep = ehead; ep != NULL; ep = ep->next) {
struct addrinfo hints, *res; struct addrinfo hints, *res;
struct sockaddr_storage ss; struct sockaddr_storage ss;
@ -351,7 +362,9 @@ main(int argc, char *argv[])
time_t t; time_t t;
int isaddr; int isaddr;
p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-"; xo_open_instance("user-entry");
save_p = p = *ep->utmp.ut_host ? ep->utmp.ut_host : "-";
if ((x_suffix = strrchr(p, ':')) != NULL) { if ((x_suffix = strrchr(p, ':')) != NULL) {
if ((dot = strchr(x_suffix, '.')) != NULL && if ((dot = strchr(x_suffix, '.')) != NULL &&
strchr(dot+1, '.') == NULL) strchr(dot+1, '.') == NULL)
@ -400,6 +413,9 @@ main(int argc, char *argv[])
p = buf; p = buf;
} }
if (dflag) { if (dflag) {
xo_open_container("process-table");
xo_open_list("process-entry");
for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) { for (dkp = ep->dkp; dkp != NULL; dkp = debugproc(dkp)) {
const char *ptr; const char *ptr;
@ -407,24 +423,41 @@ main(int argc, char *argv[])
dkp->ki_comm, NULL, MAXCOMLEN); dkp->ki_comm, NULL, MAXCOMLEN);
if (ptr == NULL) if (ptr == NULL)
ptr = "-"; ptr = "-";
(void)printf("\t\t%-9d %s\n", xo_open_instance("process-entry");
xo_emit("\t\t{:process-id/%-9d/%d} {:command/%s}\n",
dkp->ki_pid, ptr); dkp->ki_pid, ptr);
xo_close_instance("process-entry");
} }
xo_close_list("process-entry");
xo_close_container("process-table");
} }
(void)printf("%-*.*s %-*.*s %-*.*s ", xo_emit("{:user/%-*.*s/%@**@s} {:tty/%-*.*s/%@**@s} ",
W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user, W_DISPUSERSIZE, W_DISPUSERSIZE, ep->utmp.ut_user,
W_DISPLINESIZE, W_DISPLINESIZE, W_DISPLINESIZE, W_DISPLINESIZE,
*ep->utmp.ut_line ? *ep->utmp.ut_line ?
(strncmp(ep->utmp.ut_line, "tty", 3) && (strncmp(ep->utmp.ut_line, "tty", 3) &&
strncmp(ep->utmp.ut_line, "cua", 3) ? strncmp(ep->utmp.ut_line, "cua", 3) ?
ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-", ep->utmp.ut_line : ep->utmp.ut_line + 3) : "-");
if (save_p && save_p != p)
xo_attr("address", "%s", save_p);
xo_emit("{:from/%-*.*s/%@**@s} ",
W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-"); W_DISPHOSTSIZE, W_DISPHOSTSIZE, *p ? p : "-");
t = ep->utmp.ut_tv.tv_sec; t = ep->utmp.ut_tv.tv_sec;
longattime = pr_attime(&t, &now); longattime = pr_attime(&t, &now);
longidle = pr_idle(ep->idle); longidle = pr_idle(ep->idle);
(void)printf("%.*s\n", argwidth - longidle - longattime, xo_emit("{:command/%.*s/%@*@s}\n",
argwidth - longidle - longattime,
ep->args); ep->args);
xo_close_instance("user-entry");
} }
xo_close_list("user-entry");
xo_close_container("user-table");
xo_close_container("uptime-information");
xo_finish();
(void)kvm_close(kd); (void)kvm_close(kd);
exit(0); exit(0);
} }
@ -443,7 +476,7 @@ pr_header(time_t *nowp, int nusers)
*/ */
if (strftime(buf, sizeof(buf), if (strftime(buf, sizeof(buf),
use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0) use_ampm ? "%l:%M%p" : "%k:%M", localtime(nowp)) != 0)
(void)printf("%s ", buf); xo_emit("{:time-of-day/%s} ", buf);
/* /*
* Print how long system has been up. * Print how long system has been up.
*/ */
@ -457,35 +490,45 @@ pr_header(time_t *nowp, int nusers)
uptime %= 3600; uptime %= 3600;
mins = uptime / 60; mins = uptime / 60;
secs = uptime % 60; secs = uptime % 60;
(void)printf(" up"); xo_emit(" up");
xo_attr("seconds", "%lu", (unsigned long) tp.tv_sec);
if (days > 0) if (days > 0)
(void)printf(" %d day%s,", days, days > 1 ? "s" : ""); xo_emit(" {:uptime/%d day%s},",
days, days > 1 ? "s" : "");
if (hrs > 0 && mins > 0) if (hrs > 0 && mins > 0)
(void)printf(" %2d:%02d,", hrs, mins); xo_emit(" {:uptime/%2d:%02d},", hrs, mins);
else if (hrs > 0) else if (hrs > 0)
(void)printf(" %d hr%s,", hrs, hrs > 1 ? "s" : ""); xo_emit(" {:uptime/%d hr%s},",
hrs, hrs > 1 ? "s" : "");
else if (mins > 0) else if (mins > 0)
(void)printf(" %d min%s,", mins, mins > 1 ? "s" : ""); xo_emit(" {:uptime/%d min%s},",
mins, mins > 1 ? "s" : "");
else else
(void)printf(" %d sec%s,", secs, secs > 1 ? "s" : ""); xo_emit(" {:uptime/%d sec%s},",
secs, secs > 1 ? "s" : "");
} }
/* Print number of users logged in to system */ /* Print number of users logged in to system */
(void)printf(" %d user%s", nusers, nusers == 1 ? "" : "s"); xo_emit(" {:users/%d} user%s", nusers, nusers == 1 ? "" : "s");
/* /*
* Print 1, 5, and 15 minute load averages. * Print 1, 5, and 15 minute load averages.
*/ */
if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1) if (getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])) == -1)
(void)printf(", no load average information available\n"); xo_emit(", no load average information available\n");
else { else {
(void)printf(", load averages:"); static const char *format[] = {
" {:load-average-1/%.2f}",
" {:load-average-5/%.2f}",
" {:load-average-15/%.2f}",
};
xo_emit(", load averages:");
for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) { for (i = 0; i < (int)(sizeof(avenrun) / sizeof(avenrun[0])); i++) {
if (use_comma && i > 0) if (use_comma && i > 0)
(void)printf(","); xo_emit(",");
(void)printf(" %.2f", avenrun[i]); xo_emit(format[i], avenrun[i]);
} }
(void)printf("\n"); xo_emit("\n");
} }
} }
@ -506,9 +549,8 @@ static void
usage(int wcmd) usage(int wcmd)
{ {
if (wcmd) if (wcmd)
(void)fprintf(stderr, xo_error("usage: w [-dhin] [-M core] [-N system] [user ...]\n");
"usage: w [-dhin] [-M core] [-N system] [user ...]\n");
else else
(void)fprintf(stderr, "usage: uptime\n"); xo_error("usage: uptime\n");
exit(1); exit(1);
} }