PERFORCE change 214578 for review

Brooks Davis brooks at FreeBSD.org
Wed Jul 18 21:51:30 UTC 2012


http://p4web.freebsd.org/@@214578?ac=10

Change 214578 by brooks at brooks_ecr_current on 2012/07/18 21:51:01

	Checkpoint a working if toy like text viewer application.
	
	Files wider that 49 colums have their lines truncated and there
	are arbitrary limits of 1024 lines and 32kb in size.  That being
	side, it renders the README file in the demo directory which was
	the main point.

Affected files ...

.. //depot/projects/ctsrd/beribsd/src/ctsrd/browser/browser.c#13 edit

Differences ...

==== //depot/projects/ctsrd/beribsd/src/ctsrd/browser/browser.c#13 (text+ko) ====

@@ -43,6 +43,7 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <fnmatch.h>
+#include <inttypes.h>
 #include <libutil.h>
 #include <magic.h>
 #include <poll.h>
@@ -115,7 +116,7 @@
 #define	SB_IMG_CHERI_ECOL	445
 #define	SB_MINCOL		SB_IMG_NONE_BCOL
 #define	SB_MAXCOL		SB_IMG_CHERI_ECOL
-#define	SB_MINROW		(fb_height - 39)
+#define	SB_MINROW		(fb_height - 38)
 #define	SB_MAXROW		(fb_height - 1)
 
 /* Start offsets for browser columns */
@@ -249,6 +250,7 @@
 	struct sigaction act;
 	struct pollfd pfd[1];
 	char buf[1024];
+	u_int32_t *image;
 
 restart:
 	if (openpty(&pmaster, &pslave, NULL, NULL, NULL) == -1)
@@ -259,7 +261,8 @@
 	else if (pid == 0) {
 		close(pmaster);
 		if (login_tty(pslave) < 0) {
-			syslog(LOG_ALERT, "login_tty failed in child: %s", strerror(errno));
+			syslog(LOG_ALERT, "login_tty failed in child: %s",
+			    strerror(errno));
 			err(1, "tty_login");
 		}
 		/* return to begin normal processing */
@@ -294,6 +297,11 @@
 				    WEXITSTATUS(status));
 				if (WEXITSTATUS(status) == 99) {
 					warnx("child was exploited");
+					image = malloc(sizeof(u_int32_t) *
+					    fb_width * fb_height);
+					if (image == NULL)
+						err(1, "malloc");
+					fb_save(image);
 					fb_dialog(FBDT_PINCH2CLOSE, black,
 					    white, black,
 					    "Browser Exited", 
@@ -301,6 +309,8 @@
 "\n"
 "Pinch to close dialog and restart"
 					    );
+					fb_post(image);
+					free(image);
 				}
 			} else if(WIFSIGNALED(status)) {
 				warn("child killed by signal %d",
@@ -602,7 +612,7 @@
 static int
 show_png(int dfd, const char *name)
 {
-	int fd;
+	int fd, ret = 0;
 	u_int32_t *image, *previmage;
 
 	image = malloc(sizeof(u_int32_t) * fb_width * fb_height);
@@ -611,16 +621,21 @@
 		err(1, "malloc");
 	fb_save(previmage);
 	busy_indicator();
-	if ((fd = openat(dfd, name, O_RDONLY)) == -1)
-		return (-1);
+	if ((fd = openat(dfd, name, O_RDONLY)) == -1) {
+		ret = -1;
+		goto end;
+	}
 	busy_indicator();
-	if (read_png_fd(fd, image, fb_width, fb_height) != 0)
-		return (-1);
+	if (read_png_fd(fd, image, fb_width, fb_height) != 0) {
+		ret = -1;
+		goto end;
+	}
 	/* read_png_fd() closes the descriptor */
 	fb_post(image);
 	for (;;)
 		if(ts_poll()->ts_gesture == TSG2_ZOOM_OUT)
 			break;
+end:
 	fb_post(previmage);
 	free(previmage);
 	free(image);
@@ -628,59 +643,96 @@
 	return (0);
 }
 
-#ifdef NOTYET
 static int
 show_text_file(int dfd, const char *name)
 {
-	FILE *fp;
 	int fd, i, nlines, topline;
-	size_t linelen;
+	size_t fbuflen;
+	ssize_t len;
 	fb_dialog_action da;
-	char *lines[1024], buf[14 * 50];
+	char dbuf[14 * 50], *dbufp, *fbuf, *lines[1024];
+	const size_t maxbuflen = 32 * 1024 - 1;
+	u_int32_t *image;
+
+	printf("entering show_text_file\n");
 
-	if ((fd = openat(dfd, name, O_RDONLY)) == -1)
+	if ((fd = openat(dfd, name, O_RDONLY)) < 0)
 		return (-1);
-	if ((fp = fdopen(fd, "rb")) == NULL)
-		return (-1);
-	for (nlines = 0; nlines++; nlines++) {
-		lines[nlines] = NULL;
-		linelen = 0;
-		if (getdelim(&lines[nlines], &linelen, '\n', fp) == -1)
+	printf("fd = %d\n", fd);
+
+	fbuf = malloc(maxbuflen + 1);
+	if (fbuf == NULL)
+		err(1, "malloc");
+	for (fbuflen = 0; fbuflen < maxbuflen; fbuflen += len) {
+		len = read(fd, fbuf + fbuflen,
+		    (fbuflen - maxbuflen < 4096) ? fbuflen - maxbuflen : 4096);
+		if (len < 0) {
+			warn("read");
+			if (fbuflen > 0)
+				break; /* Use what we have if anything */
+			free(fbuf);
+			return (-1);
+		}
+		if (len == 0)
 			break;
-		if (linelen >= 50)
+	}
+	/* NUL terminate the buffer, replacing the last \n if there */
+	if (fbuf[fbuflen - 1] == '\n')
+		fbuf[fbuflen - 1] = '\0';
+	else
+		fbuf[fbuflen] = '\0';
+
+	printf("fbuf = '%s'\n", fbuf);
+	nlines = 0;
+	while (nlines <= 1024 &&
+	    (lines[nlines] = strsep(&fbuf, "\n")) != NULL) {
+		if (strlen(lines[nlines]) > 50)
 			lines[nlines][50] = '\0';
+		nlines++;
 	}
-	if (nlines == 0)
+	if (nlines == 0) {
+		free(fbuf);
 		return (-1);
+	}
 
+	image = malloc(sizeof(u_int32_t) * fb_width * fb_height);
+	if (image == NULL)
+		err(1, "malloc");
+	fb_save(image);
+
 	topline = 0;
 	for (;;) {
-		buf[0] = '\0';
-		/* XXX: inefficient, assumes re-termination above */
-		for (i = topline; i < nlines && i < topline + 14; i++)
-			strcat(buf, lines[i]);
+		dbufp = dbuf;
+		for (i = topline; i < topline + 13; i++) {
+			len = sprintf(dbufp, "%-49s%s",
+			    (i < nlines) ? lines[i] : "",
+			    (i < topline + 13 - 1) ? "\n" : "");
+			dbufp += len;
+		}
 
 		da = fb_dialog(FBDT_PINCH_OR_VSCROLL, blue, black, blue,
-		    name, buf);
+		    name, dbuf);
 		switch (da) {
 		case FBDA_OK:
 			for (i = 0; i < nlines; i++)
 				free(lines[nlines]);
+			free(fbuf);
+			fb_post(image);
+			free(image);
 			return (0);
 		case FBDA_UP:
 			if (topline > 0)
-				topline -= 14;
+				topline -= 13;
 			break;
 		case FBDA_DOWN:
 			if (topline + 14 < nlines)
-				topline += 14;
+				topline += 13;
 			break;
 		default:
 			err(1, "unhandled action");
 		}
 	}
 }
-#endif
 
 static int
 browsedir(int dfd)
@@ -782,13 +834,11 @@
 				show_png(dfd,
 				    dents[topslot + action]->entry->d_name);
 				goto render;
-#ifdef NOTYET
 			} else if (strcmp("text/plain",
 			    dents[topslot + action]->desc) == 0) {
 				show_text_file(dfd,
 				    dents[topslot + action]->entry->d_name);
 				goto render;
-#endif
 			} else {
 				if (verbose)
 					printf("opening non-directory not "


More information about the p4-projects mailing list