svn commit: r316447 - head/usr.sbin/vidcontrol

Bruce Evans bde at FreeBSD.org
Mon Apr 3 09:21:01 UTC 2017


Author: bde
Date: Mon Apr  3 09:21:00 2017
New Revision: 316447
URL: https://svnweb.freebsd.org/changeset/base/316447

Log:
  Save errno across revert().
  
  Change 25 uses of errc() to err().  2 of these were correct.  23 used
  errno for the arg so were just a verbose spelling of err().  5 of these
  were just style bugs, and 18 depended on revert() saving errno.
  
  1 warn() also depended on revert() saving errno.
  
  Remove 2 warnx()'s that duplicate the message from a later errx().
  These used to be before returns, and should have reported errno in
  some cases.  errno is also not reported for for openguess() failures.
  
  Only "restore" the video mode in revert() if the mode was just set
  (necessarily partially).  Setting the mode to its current setting
  doesn't "restore" it, but resets it, with the least harmful change
  being to clear the screen.  revert() is called even for some harmless
  syntax errors and usually did nothing except reset the mode.  Now it
  usually does nothing.  The only things that it tries to restore apart
  from the mode are the active vty number, the screen map, 2 colors that
  only need to be restored (only after a mode change) to fix kernel bugs,
  and 3 colors that can't be restored due to kernel bugs.  (This is
  mostly for sc, since vt doesn't support mode changes.)
  
  revert() is not called for syntax and setting errors after [mode], so
  the mode reset is only done for failures to set raster mode after
  setting graphics mode.  normal colors can only be set after [mode],
  and that is why reverting them should be unnecessary.

Modified:
  head/usr.sbin/vidcontrol/vidcontrol.c

Modified: head/usr.sbin/vidcontrol/vidcontrol.c
==============================================================================
--- head/usr.sbin/vidcontrol/vidcontrol.c	Mon Apr  3 08:50:54 2017	(r316446)
+++ head/usr.sbin/vidcontrol/vidcontrol.c	Mon Apr  3 09:21:00 2017	(r316447)
@@ -95,6 +95,7 @@ static int	vesa_cols;
 static int	vesa_rows;
 static int	font_height;
 static int	vt4_mode = 0;
+static int	video_mode_changed;
 static struct	vid_info info;
 static struct	video_info new_mode_info;
 
@@ -117,25 +118,25 @@ static void
 init(void)
 {
 	if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
-		errc(1, errno, "getting active vty");
+		err(1, "getting active vty");
 
 	cur_info.console_info.size = sizeof(cur_info.console_info);
 
 	if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
-		errc(1, errno, "getting console information");
+		err(1, "getting console information");
 
 	/* vt(4) use unicode, so no screen mapping required. */
 	if (vt4_mode == 0 &&
 	    ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
-		errc(1, errno, "getting screen map");
+		err(1, "getting screen map");
 
 	if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
-		errc(1, errno, "getting video mode number");
+		err(1, "getting video mode number");
 
 	cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
 
 	if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
-		errc(1, errno, "getting video mode parameters");
+		err(1, "getting video mode parameters");
 }
 
 
@@ -149,7 +150,9 @@ init(void)
 static void
 revert(void)
 {
-	int size[3];
+	int save_errno, size[3];
+
+	save_errno = errno;
 
 	ioctl(0, VT_ACTIVATE, cur_info.active_vty);
 
@@ -160,24 +163,27 @@ revert(void)
 	if (vt4_mode == 0)
 		ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
 
-	if (cur_info.video_mode_number >= M_VESA_BASE)
-		ioctl(0, _IO('V', cur_info.video_mode_number - M_VESA_BASE),
-		      NULL);
-	else
-		ioctl(0, _IO('S', cur_info.video_mode_number), NULL);
-
-	if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
-		size[0] = cur_info.video_mode_info.vi_width / 8;
-		size[1] = cur_info.video_mode_info.vi_height /
-			  cur_info.console_info.font_size;
-		size[2] = cur_info.console_info.font_size;
-
-		ioctl(0, KDRASTER, size);
+	if (video_mode_changed) {
+		if (cur_info.video_mode_number >= M_VESA_BASE)
+			ioctl(0,
+			    _IO('V', cur_info.video_mode_number - M_VESA_BASE),
+			    NULL);
+		else
+			ioctl(0, _IO('S', cur_info.video_mode_number), NULL);
+		if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
+			size[0] = cur_info.video_mode_info.vi_width / 8;
+			size[1] = cur_info.video_mode_info.vi_height /
+			    cur_info.console_info.font_size;
+			size[2] = cur_info.console_info.font_size;
+			ioctl(0, KDRASTER, size);
+		}
 	}
 
 	/* Restore some colors last since mode setting forgets some. */
 	fprintf(stderr, "\033[=%dF", cur_info.console_info.mv_norm.fore);
 	fprintf(stderr, "\033[=%dG", cur_info.console_info.mv_norm.back);
+
+	errno = save_errno;
 }
 
 
@@ -298,7 +304,6 @@ load_scrnmap(const char *filename)
 		rewind(fd);
 
 		if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
-			warnx("bad screenmap file");
 			fclose(fd);
 			revert();
 			errx(1, "bad screenmap file");
@@ -307,7 +312,7 @@ load_scrnmap(const char *filename)
 
 	if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
 		revert();
-		errc(1, errno, "loading screenmap");
+		err(1, "loading screenmap");
 	}
 
 	fclose(fd);
@@ -329,7 +334,7 @@ load_default_scrnmap(void)
 
 	if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
 		revert();
-		errc(1, errno, "loading default screenmap");
+		err(1, "loading default screenmap");
 	}
 }
 
@@ -346,7 +351,7 @@ print_scrnmap(void)
 
 	if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
 		revert();
-		errc(1, errno, "getting screenmap");
+		err(1, "getting screenmap");
 	}
 	for (i=0; i<sizeof(map); i++) {
 		if (i != 0 && i % 16 == 0)
@@ -415,7 +420,7 @@ load_default_vt4font(void)
 {
 	if (ioctl(0, PIO_VFONT_DEFAULT) == -1) {
 		revert();
-		errc(1, errno, "loading default vt font");
+		err(1, "loading default vt font");
 	}
 }
 
@@ -574,7 +579,6 @@ load_font(const char *type, const char *
 		rewind(fd);
 		if (fsize(fd) != size ||
 		    fread(fontmap, 1, size, fd) != (size_t)size) {
-			warnx("%s: bad font file", filename);
 			fclose(fd);
 			free(fontmap);
 			revert();
@@ -584,7 +588,7 @@ load_font(const char *type, const char *
 
 	if (ioctl(0, io, fontmap) == -1) {
 		revert();
-		errc(1, errno, "loading font");
+		err(1, "loading font");
 	}
 
 	fclose(fd);
@@ -614,7 +618,7 @@ set_screensaver_timeout(char *arg)
 
 	if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
 		revert();
-		errc(1, errno, "setting screensaver period");
+		err(1, "setting screensaver period");
 	}
 }
 
@@ -641,7 +645,7 @@ set_cursor_type(char *appearance)
 
 	if (ioctl(0, CONS_CURSORTYPE, &type) == -1) {
 		revert();
-		errc(1, errno, "setting cursor type");
+		err(1, "setting cursor type");
 	}
 }
 
@@ -694,7 +698,7 @@ video_mode(int argc, char **argv, int *m
 	int new_mode_num = 0;
 	unsigned long mode = 0;
 	int cur_mode; 
-	int ioerr;
+	int save_errno;
 	int size[3];
 	int i;
 
@@ -723,10 +727,10 @@ video_mode(int argc, char **argv, int *m
 			if (modes[i].name == NULL)
 				return;
 			if (ioctl(0, mode, NULL) < 0) {
-				ioerr = errno;
 				revert();
-				errc(1, ioerr, "cannot set videomode");
+				err(1, "cannot set videomode");
 			}
+			video_mode_changed = 1;
 		}
 
 		/*
@@ -737,7 +741,7 @@ video_mode(int argc, char **argv, int *m
 
 		if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
 			revert();
-			errc(1, errno, "obtaining new video mode parameters");
+			err(1, "obtaining new video mode parameters");
 		}
 
 		if (mode == 0) {
@@ -753,8 +757,9 @@ video_mode(int argc, char **argv, int *m
 
 		if (ioctl(0, mode, NULL) == -1) {
 			revert();
-			errc(1, errno, "setting video mode");
+			err(1, "setting video mode");
 		}
+		video_mode_changed = 1;
 
 		/*
 		 * For raster modes it's not enough to just set the mode.
@@ -791,7 +796,7 @@ video_mode(int argc, char **argv, int *m
 			/* set raster mode */
 
 			if (ioctl(0, KDRASTER, size)) {
-				ioerr = errno;
+				save_errno = errno;
 				if (cur_mode >= M_VESA_BASE)
 					ioctl(0,
 					    _IO('V', cur_mode - M_VESA_BASE),
@@ -799,8 +804,8 @@ video_mode(int argc, char **argv, int *m
 				else
 					ioctl(0, _IO('S', cur_mode), NULL);
 				revert();
-				errc(1, ioerr,
-				    "cannot activate raster display");
+				errno = save_errno;
+				err(1, "cannot activate raster display");
 			}
 		}
 
@@ -894,7 +899,7 @@ set_console(char *arg)
 		errx(1, "console number out of range");
 	} else if (ioctl(0, VT_ACTIVATE, n) == -1) {
 		revert();
-		errc(1, errno, "switching vty");
+		err(1, "switching vty");
 	}
 }
 
@@ -934,7 +939,7 @@ set_mouse_char(char *arg)
 
 	if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
 		revert();
-		errc(1, errno, "setting mouse character");
+		err(1, "setting mouse character");
 	}
 }
 
@@ -959,7 +964,7 @@ set_mouse(char *arg)
 
 	if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
 		revert();
-		errc(1, errno, "%sing the mouse",
+		err(1, "%sing the mouse",
 		     mouse.operation == MOUSE_SHOW ? "show" : "hid");
 	}
 }
@@ -981,7 +986,7 @@ set_lockswitch(char *arg)
 
 	if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
 		revert();
-		errc(1, errno, "turning %s vty switching",
+		err(1, "turning %s vty switching",
 		     data == 0x01 ? "off" : "on");
 	}
 }
@@ -1041,7 +1046,7 @@ show_adapter_info(void)
 
 	if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
 		revert();
-		errc(1, errno, "obtaining adapter information");
+		err(1, "obtaining adapter information");
 	}
 
 	printf("fb%d:\n", ad.va_index);
@@ -1200,7 +1205,7 @@ dump_screen(int mode, int opt)
 
 	if (ioctl(0, CONS_GETINFO, &_info) == -1) {
 		revert();
-		errc(1, errno, "obtaining current video mode parameters");
+		err(1, "obtaining current video mode parameters");
 		return;
 	}
 
@@ -1218,7 +1223,7 @@ dump_screen(int mode, int opt)
 
 	if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
 		revert();
-		errc(1, errno, "dumping screen");
+		err(1, "dumping screen");
 	}
 
 	if (mode == DUMP_FMT_RAW) {
@@ -1284,7 +1289,7 @@ set_history(char *opt)
 
 	if (ioctl(0, CONS_HISTORY, &size) == -1) {
 		revert();
-		errc(1, errno, "setting history buffer size");
+		err(1, "setting history buffer size");
 	}
 }
 
@@ -1298,7 +1303,7 @@ clear_history(void)
 {
 	if (ioctl(0, CONS_CLRHIST) == -1) {
 		revert();
-		errc(1, errno, "clearing history buffer");
+		err(1, "clearing history buffer");
 	}
 }
 


More information about the svn-src-head mailing list