Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 6 Jul 2018 12:07:06 +0000 (UTC)
From:      Daichi GOTO <daichi@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r336028 - head/usr.bin/top
Message-ID:  <201807061207.w66C76CR043533@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: daichi
Date: Fri Jul  6 12:07:06 2018
New Revision: 336028
URL: https://svnweb.freebsd.org/changeset/base/336028

Log:
  Changed to eliminate the upper limit of command length displayed
  by "-a" and expand to match terminal width
  
  Reviewed by:	eadler
  Approved by:	gnn (mentor)
  Differential Revision:	https://reviews.freebsd.org/D16083

Modified:
  head/usr.bin/top/display.c
  head/usr.bin/top/machine.c
  head/usr.bin/top/screen.c
  head/usr.bin/top/top.h

Modified: head/usr.bin/top/display.c
==============================================================================
--- head/usr.bin/top/display.c	Fri Jul  6 11:50:59 2018	(r336027)
+++ head/usr.bin/top/display.c	Fri Jul  6 12:07:06 2018	(r336028)
@@ -57,9 +57,8 @@ FILE *debug;
 static int lmpid = 0;
 static int last_hi = 0;		/* used in u_process and u_endscreen */
 static int lastline = 0;
-static int display_width = MAX_COLS;
 
-#define lineindex(l) ((l)*display_width)
+#define lineindex(l) ((l)*screen_width)
 
 
 /* things initialized by display_init and used thruout */
@@ -94,6 +93,9 @@ static enum { OFF, ON, ERASE } header_status = ON;
 static void summary_format(char *, int *, const char * const *);
 static void line_update(char *, char *, int, int);
 
+static int setup_buffer_bufsiz = 0;
+static char * setup_buffer(char *, int);
+
 int  x_lastpid =	10;
 int  y_lastpid =	0;
 int  x_loadave =	33;
@@ -138,17 +140,9 @@ display_resize(void)
 
     if (lines < 0)
 	lines = 0;
-    /* we don't want more than MAX_COLS columns, since the machine-dependent
-       modules make static allocations based on MAX_COLS and we don't want
-       to run off the end of their buffers */
-    display_width = screen_width;
-    if (display_width >= MAX_COLS)
-    {
-	display_width = MAX_COLS - 1;
-    }
 
     /* now, allocate space for the screen buffer */
-    screenbuf = calloc(lines, display_width);
+    screenbuf = calloc(lines, screen_width);
     if (screenbuf == NULL)
     {
 	/* oops! */
@@ -336,7 +330,7 @@ i_timeofday(time_t *tod)
 }
 
 static int ltotal = 0;
-static char procstates_buffer[MAX_COLS];
+static char *procstates_buffer = NULL;
 
 /*
  *  *_procstates(total, brkdn, names) - print the process summary line
@@ -350,6 +344,8 @@ i_procstates(int total, int *brkdn)
 {
     int i;
 
+    procstates_buffer = setup_buffer(procstates_buffer, 0);
+
     /* write current number of processes and remember the value */
     printf("%d %s:", total, (ps.thread) ? "threads" :"processes");
     ltotal = total;
@@ -372,9 +368,11 @@ i_procstates(int total, int *brkdn)
 void
 u_procstates(int total, int *brkdn)
 {
-    static char new[MAX_COLS];
+    static char *new = NULL;
     int i;
 
+    new = setup_buffer(new, 0);
+
     /* update number of processes only if it has changed */
     if (ltotal != total)
     {
@@ -551,11 +549,13 @@ z_cpustates(void)
  *                for i_memory ONLY: cursor is on the previous line
  */
 
-static char memory_buffer[MAX_COLS];
+static char *memory_buffer = NULL;
 
 void
 i_memory(int *stats)
 {
+    memory_buffer = setup_buffer(memory_buffer, 0);
+
     fputs("\nMem: ", stdout);
     lastline++;
 
@@ -567,8 +567,10 @@ i_memory(int *stats)
 void
 u_memory(int *stats)
 {
-    static char new[MAX_COLS];
+    static char *new = NULL;
 
+    new = setup_buffer(new, 0);
+
     /* format the new line */
     summary_format(new, stats, memory_names);
     line_update(memory_buffer, new, x_mem, y_mem);
@@ -580,11 +582,13 @@ u_memory(int *stats)
  *  Assumptions:  cursor is on "lastline"
  *                for i_arc ONLY: cursor is on the previous line
  */
-static char arc_buffer[MAX_COLS];
+static char *arc_buffer = NULL;
 
 void
 i_arc(int *stats)
 {
+    arc_buffer = setup_buffer(arc_buffer, 0);
+
     if (arc_names == NULL)
 	return;
 
@@ -599,8 +603,10 @@ i_arc(int *stats)
 void
 u_arc(int *stats)
 {
-    static char new[MAX_COLS];
+    static char *new = NULL;
 
+    new = setup_buffer(new, 0);
+
     if (arc_names == NULL)
 	return;
 
@@ -616,11 +622,13 @@ u_arc(int *stats)
  *  Assumptions:  cursor is on "lastline"
  *                for i_carc ONLY: cursor is on the previous line
  */
-static char carc_buffer[MAX_COLS];
+static char *carc_buffer = NULL;
 
 void
 i_carc(int *stats)
 {
+    carc_buffer = setup_buffer(carc_buffer, 0);
+
     if (carc_names == NULL)
 	return;
 
@@ -635,8 +643,10 @@ i_carc(int *stats)
 void
 u_carc(int *stats)
 {
-    static char new[MAX_COLS];
+    static char *new = NULL;
 
+    new = setup_buffer(new, 0);
+
     if (carc_names == NULL)
 	return;
 
@@ -652,11 +662,13 @@ u_carc(int *stats)
  *                for i_swap ONLY: cursor is on the previous line
  */
 
-static char swap_buffer[MAX_COLS];
+static char *swap_buffer = NULL;
 
 void
 i_swap(int *stats)
 {
+    swap_buffer = setup_buffer(swap_buffer, 0);
+
     fputs("\nSwap: ", stdout);
     lastline++;
 
@@ -668,8 +680,10 @@ i_swap(int *stats)
 void
 u_swap(int *stats)
 {
-    static char new[MAX_COLS];
+    static char *new = NULL;
 
+    new = setup_buffer(new, 0);
+
     /* format the new line */
     summary_format(new, stats, swap_names);
     line_update(swap_buffer, new, x_swap, y_swap);
@@ -689,7 +703,7 @@ u_swap(int *stats)
  *	respect to screen updates).
  */
 
-static char next_msg[MAX_COLS + 5];
+static char *next_msg = NULL;
 static int msglen = 0;
 /* Invariant: msglen is always the length of the message currently displayed
    on the screen (even when next_msg doesn't contain that message). */
@@ -697,6 +711,7 @@ static int msglen = 0;
 void
 i_message(void)
 {
+    next_msg = setup_buffer(next_msg, 5);
 
     while (lastline < y_message)
     {
@@ -736,7 +751,7 @@ trim_header(const char *text)
 	int width;
 
 	s = NULL;
-	width = display_width;
+	width = screen_width;
 	header_length = strlen(text);
 	if (header_length >= width) {
 		s = strndup(text, width);
@@ -807,7 +822,7 @@ i_process(int line, char *thisline)
     }
 
     /* truncate the line to conform to our current screen width */
-    thisline[display_width] = '\0';
+    thisline[screen_width] = '\0';
 
     /* write the line out */
     fputs(thisline, stdout);
@@ -817,7 +832,7 @@ i_process(int line, char *thisline)
     p = stpcpy(base, thisline);
 
     /* zero fill the rest of it */
-    memset(p, 0, display_width - (p - base));
+    memset(p, 0, screen_width - (p - base));
 }
 
 void
@@ -831,7 +846,7 @@ u_process(int line, char *newline)
     bufferline = &screenbuf[lineindex(line)];
 
     /* truncate the line to conform to our current screen width */
-    newline[display_width] = '\0';
+    newline[screen_width] = '\0';
 
     /* is line higher than we went on the last display? */
     if (line >= last_hi)
@@ -856,7 +871,7 @@ u_process(int line, char *newline)
 	optr = stpcpy(bufferline, newline);
 
 	/* zero fill the rest of it */
-	memset(optr, 0, display_width - (optr - bufferline));
+	memset(optr, 0, screen_width - (optr - bufferline));
     }
     else
     {
@@ -1235,7 +1250,7 @@ line_update(char *old, char *new, int start, int line)
     } while (ch != '\0');
 
     /* zero out the rest of the line buffer -- MUST BE DONE! */
-    diff = display_width - newcol;
+    diff = screen_width - newcol;
     if (diff > 0)
     {
 	memset(old, 0, diff);
@@ -1326,4 +1341,32 @@ i_uptime(struct timeval *bt, time_t *tod)
 	}
 	printf(" up %d+%02d:%02d:%02d", days, hrs, mins, secs);
     }
+}
+
+static char *
+setup_buffer(char *buffer, int addlen)
+{
+	char *b = NULL;
+
+	if (NULL == buffer) {
+		setup_buffer_bufsiz = screen_width;
+		b = calloc(setup_buffer_bufsiz + addlen, sizeof(char));
+	} else {
+		if (screen_width > setup_buffer_bufsiz) {
+			setup_buffer_bufsiz = screen_width;
+			free(buffer);
+			b = calloc(setup_buffer_bufsiz + addlen,
+					sizeof(char));
+		} else {
+			b = buffer;
+		}	
+	}
+
+	if (NULL == b) {
+		fprintf(stderr, "%s: can't allocate sufficient memory\n",
+				myname);
+		exit(4);
+	}
+
+	return b;
 }

Modified: head/usr.bin/top/machine.c
==============================================================================
--- head/usr.bin/top/machine.c	Fri Jul  6 11:50:59 2018	(r336027)
+++ head/usr.bin/top/machine.c	Fri Jul  6 12:07:06 2018	(r336028)
@@ -870,7 +870,6 @@ format_next_process(struct handle * xhandle, char *(*g
 	long p_tot, s_tot;
 	char *cmdbuf = NULL;
 	char **args;
-	const int cmdlen = 256;
 	static struct sbuf* procbuf = NULL;
 
 	/* clean up from last time. */
@@ -935,31 +934,31 @@ format_next_process(struct handle * xhandle, char *(*g
 		break;
 	}
 
-	cmdbuf = calloc(cmdlen + 1, 1);
+	cmdbuf = calloc(screen_width + 1, 1);
 	if (cmdbuf == NULL) {
-		warn("calloc(%d)", cmdlen + 1);
+		warn("calloc(%d)", screen_width + 1);
 		return NULL;
 	}
 
 	if (!(flags & FMT_SHOWARGS)) {
 		if (ps.thread && pp->ki_flag & P_HADTHREADS &&
 		    pp->ki_tdname[0]) {
-			snprintf(cmdbuf, cmdlen, "%s{%s%s}", pp->ki_comm,
+			snprintf(cmdbuf, screen_width, "%s{%s%s}", pp->ki_comm,
 			    pp->ki_tdname, pp->ki_moretdname);
 		} else {
-			snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm);
+			snprintf(cmdbuf, screen_width, "%s", pp->ki_comm);
 		}
 	} else {
 		if (pp->ki_flag & P_SYSTEM ||
-		    (args = kvm_getargv(kd, pp, cmdlen)) == NULL ||
+		    (args = kvm_getargv(kd, pp, screen_width)) == NULL ||
 		    !(*args)) {
 			if (ps.thread && pp->ki_flag & P_HADTHREADS &&
 		    	    pp->ki_tdname[0]) {
-				snprintf(cmdbuf, cmdlen,
+				snprintf(cmdbuf, screen_width,
 				    "[%s{%s%s}]", pp->ki_comm, pp->ki_tdname,
 				    pp->ki_moretdname);
 			} else {
-				snprintf(cmdbuf, cmdlen,
+				snprintf(cmdbuf, screen_width,
 				    "[%s]", pp->ki_comm);
 			}
 		} else {
@@ -969,7 +968,7 @@ format_next_process(struct handle * xhandle, char *(*g
 			size_t argbuflen;
 			size_t len;
 
-			argbuflen = cmdlen * 4;
+			argbuflen = screen_width * 4;
 			argbuf = calloc(argbuflen + 1, 1);
 			if (argbuf == NULL) {
 				warn("calloc(%zu)", argbuflen + 1);
@@ -1005,21 +1004,21 @@ format_next_process(struct handle * xhandle, char *(*g
 			if (strcmp(cmd, pp->ki_comm) != 0) {
 				if (ps.thread && pp->ki_flag & P_HADTHREADS &&
 				    pp->ki_tdname[0])
-					snprintf(cmdbuf, cmdlen,
+					snprintf(cmdbuf, screen_width,
 					    "%s (%s){%s%s}", argbuf,
 					    pp->ki_comm, pp->ki_tdname,
 					    pp->ki_moretdname);
 				else
-					snprintf(cmdbuf, cmdlen,
+					snprintf(cmdbuf, screen_width,
 					    "%s (%s)", argbuf, pp->ki_comm);
 			} else {
 				if (ps.thread && pp->ki_flag & P_HADTHREADS &&
 				    pp->ki_tdname[0])
-					snprintf(cmdbuf, cmdlen,
+					snprintf(cmdbuf, screen_width,
 					    "%s{%s%s}", argbuf, pp->ki_tdname,
 					    pp->ki_moretdname);
 				else
-					strlcpy(cmdbuf, argbuf, cmdlen);
+					strlcpy(cmdbuf, argbuf, screen_width);
 			}
 			free(argbuf);
 		}

Modified: head/usr.bin/top/screen.c
==============================================================================
--- head/usr.bin/top/screen.c	Fri Jul  6 11:50:59 2018	(r336027)
+++ head/usr.bin/top/screen.c	Fri Jul  6 12:07:06 2018	(r336028)
@@ -62,8 +62,7 @@ init_termcap(bool interactive)
     char *term_name;
     int status;
 
-    /* set defaults in case we aren't smart */
-    screen_width = MAX_COLS;
+    screen_width = 0;
     screen_length = 0;
 
     if (!interactive)

Modified: head/usr.bin/top/top.h
==============================================================================
--- head/usr.bin/top/top.h	Fri Jul  6 11:50:59 2018	(r336027)
+++ head/usr.bin/top/top.h	Fri Jul  6 12:07:06 2018	(r336028)
@@ -13,9 +13,6 @@
 /* Number of lines of header information on the standard screen */
 extern int Header_lines;
 
-/* Maximum number of columns allowed for display */
-#define MAX_COLS	512
-
 /* Special atoi routine returns either a non-negative number or one of: */
 #define Infinity	-1
 #define Invalid		-2



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201807061207.w66C76CR043533>