[Cluster-devel] gfs2_edit: Fix signal handling and window resize

Steven Whitehouse swhiteho at redhat.com
Tue Dec 6 18:00:54 UTC 2011


This started out as a patch to remove the signal() call from this
bit of code, but when I started looking in more detail, I
spotted several other things which were wrong, so I fixed
those instead.

Firstly ncurses has its own SIGWINCH handler these days, so
we should not be overriding it, but instead, getting the info
about resize at getch() time.

Secondly, the function for erasing the screen was using the
supposed number of columns to index an array which was only 256
in length, so although unlikely to happen in real life, this
could cause an out of bounds access.

The simple solution seems to be to use ncurses own functions.
I noted some comments about them not working correctly, but
with the additional setting of the background attributes,
that seemed to work ok to me, unless I'm missing something.

I've played around resizing the window and the like and it
all seems to still be working correctly.

Signed-off-by: Steven Whitehouse <swhiteho at redhat.com>

diff --git a/gfs2/edit/gfs2hex.c b/gfs2/edit/gfs2hex.c
index 5616122..41630f9 100644
--- a/gfs2/edit/gfs2hex.c
+++ b/gfs2/edit/gfs2hex.c
@@ -56,7 +56,6 @@ int start_row[DMODES], end_row[DMODES], lines_per_row[DMODES];
 struct gfs_sb *sbd1;
 int gfs2_struct_type;
 unsigned int offset;
-int termcols = 80;
 struct indirect_info masterdir;
 struct gfs2_inum gfs1_quota_di;
 int print_entry_ndx;
diff --git a/gfs2/edit/hexedit.c b/gfs2/edit/hexedit.c
index 267e8ca..052be4c 100644
--- a/gfs2/edit/hexedit.c
+++ b/gfs2/edit/hexedit.c
@@ -42,6 +42,7 @@ struct gfs2_log_descriptor *lld;
 int pgnum;
 int details = 0;
 long int gziplevel = 9;
+static int termcols;
 
 int display(int identify_only);
 
@@ -742,50 +743,14 @@ static int gfs2_qc_assignval(struct gfs2_quota_change *qc,
 	return -1;
 }
 
-/* ------------------------------------------------------------------------ */
-/* UpdateSize - screen size changed, so update it                           */
-/* ------------------------------------------------------------------------ */
-static void UpdateSize(int sig)
-{
-	static char term_buffer[2048];
-	int rc;
-
-	termlines = 30;
-	termtype = getenv("TERM");
-	if (termtype == NULL)
-		return;
-	rc=tgetent(term_buffer,termtype);
-	if (rc>=0) {
-		termlines = tgetnum((char *)"li");
-		if (termlines < 10)
-			termlines = 30;
-		termcols = tgetnum((char *)"co");
-		if (termcols < 80)
-			termcols = 80;
-	}
-	else
-		perror("Error: tgetent failed.");
-	termlines--; /* last line is number of lines -1 */
-	display(FALSE);
-	signal(SIGWINCH, UpdateSize);
-}
-
 /* ------------------------------------------------------------------------- */
 /* erase - clear the screen */
 /* ------------------------------------------------------------------------- */
 static void Erase(void)
 {
-	int i;
-	char spaces[256];
-
-	memset(spaces, ' ', sizeof(spaces));
-	spaces[termcols] = '\0';
-	for (i = 0; i < termlines; i++) {
-		move(i, 0);
-		printw(spaces);
-	}
-	/*clear(); doesn't set background correctly */
-	/*erase();*/
+	bkgd(A_NORMAL|COLOR_PAIR(COLOR_NORMAL));
+	/* clear();*/ /* doesn't set background correctly */
+	erase();
 	/*bkgd(bg);*/
 }
 
@@ -2708,10 +2673,9 @@ static void interactive_mode(void)
 		eol(0);
 		exit(-1);
 	}
-
+	getmaxyx(stdscr, termlines, termcols);
+	termlines--;
 	/* Do our initial screen stuff: */
-	signal(SIGWINCH, UpdateSize); /* handle the terminal resize signal */
-	UpdateSize(0); /* update screen size based on terminal settings */
 	clear(); /* don't use Erase */
 	start_color();
 	noecho();
@@ -2934,11 +2898,15 @@ static void interactive_mode(void)
 		/* --------------------------------------------------------- */
 		/* enter key - change a value */
 		/* --------------------------------------------------------- */
-		case(KEY_ENTER):
+		case KEY_ENTER:
 		case('\n'):
 		case('\r'):
 			editing = !editing;
 			break;
+		case KEY_RESIZE:
+			getmaxyx(stdscr, termlines, termcols);
+			termlines--;
+			break;
 		default:
 			move(termlines - 1, 0);
 			printw("Keystroke not understood: 0x%03x",ch);
diff --git a/gfs2/edit/hexedit.h b/gfs2/edit/hexedit.h
index 02281cf..27950b0 100644
--- a/gfs2/edit/hexedit.h
+++ b/gfs2/edit/hexedit.h
@@ -38,7 +38,6 @@ extern uint64_t dev_offset;
 extern uint64_t max_block;
 extern struct gfs2_buffer_head *bh;
 extern int termlines;
-extern int termcols;
 extern int insert;
 extern const char *termtype;
 extern int line;





More information about the Cluster-devel mailing list