use a more common/generic routine for TIOCGWINSZ;

ensure x_cols and x_lins always have a sane value
after them (even if not tty_fd)
This commit is contained in:
tg 2008-12-29 21:05:15 +00:00
parent 5e3fcc7ab9
commit de9fe12a4c
3 changed files with 41 additions and 41 deletions

41
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.148 2008/12/13 17:02:12 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.149 2008/12/29 21:05:13 tg Exp $");
/* tty driver characters we are interested in */
typedef struct {
@ -49,10 +49,6 @@ static int x_vi(char *, size_t);
#define x_flush() shf_flush(shl_out)
#define x_putc(c) shf_putc((c), shl_out)
#ifdef TIOCGWINSZ
static void chkwinsz(void);
#endif
static int path_order_cmp(const void *aa, const void *bb);
static char *add_glob(const char *, int);
static void glob_table(const char *, XPtrV *, struct table *);
@ -74,39 +70,10 @@ x_init(void)
edchars.eof = -2;
/* default value for deficient systems */
edchars.werase = 027; /* ^W */
#ifdef TIOCGWINSZ
chkwinsz();
#endif
change_winsz();
x_init_emacs();
}
#ifdef TIOCGWINSZ
static void
chkwinsz(void)
{
struct winsize ws;
if (procpid == kshpid && ioctl(tty_fd, TIOCGWINSZ, &ws) >= 0) {
struct tbl *vp;
/* Do NOT export COLUMNS/LINES. Many applications
* check COLUMNS/LINES before checking ws.ws_col/row,
* so if the app is started with C/L in the environ
* and the window is then resized, the app won't
* see the change cause the environ doesn't change.
*/
if (ws.ws_col) {
x_cols = ws.ws_col < MIN_COLS ? MIN_COLS : ws.ws_col;
if ((vp = typeset("COLUMNS", 0, 0, 0, 0)))
setint(vp, (long)ws.ws_col);
}
if (ws.ws_row && (vp = typeset("LINES", 0, 0, 0, 0)))
setint(vp, (long)ws.ws_row);
}
}
#endif
/*
* read an edited command line
*/
@ -126,9 +93,7 @@ x_read(char *buf, size_t len)
else
i = -1; /* internal error */
x_mode(false);
#ifdef TIOCGWINSZ
chkwinsz();
#endif
change_winsz();
return i;
}

5
sh.h
View File

@ -103,7 +103,7 @@
#define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.270 2008/12/29 20:53:48 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.271 2008/12/29 21:05:15 tg Exp $");
#endif
#define MKSH_VERSION "R36 2008/12/17"
@ -685,7 +685,7 @@ EXTERN size_t current_wd_size;
#define MIN_COLS (2 + MIN_EDIT_SPACE + 3)
#define MIN_LINS 3
EXTERN int x_cols I__(80); /* tty columns */
EXTERN int x_lins I__(24); /* tty lines */
EXTERN int x_lins I__(-1); /* tty lines */
/* These to avoid bracket matching problems */
#define OPAREN '('
@ -1543,6 +1543,7 @@ char **makenv(void);
#if !HAVE_ARC4RANDOM || !defined(MKSH_SMALL)
void change_random(unsigned long);
#endif
void change_winsz(void);
int array_ref_len(const char *);
char *arrayname(const char *);
void set_array(const char *, int, const char **);

36
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.66 2008/12/29 20:52:10 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.67 2008/12/29 21:05:15 tg Exp $");
/*
* Variables
@ -1291,3 +1291,37 @@ set_array(const char *var, int reset, const char **vals)
setstr(vq, vals[i], KSH_RETURN_ERROR);
}
}
void
change_winsz(void)
{
if (x_lins < 0) {
/* first time initialisation */
#ifdef TIOCGWINSZ
if (tty_fd < 0)
/* non-FTALKING, try to get an fd anyway */
tty_init(false, false);
#endif
x_cols = -1;
}
#ifdef TIOCGWINSZ
/* check if window size has changed since first time */
if (tty_fd >= 0) {
struct winsize ws;
if (ioctl(tty_fd, TIOCGWINSZ, &ws) >= 0) {
if (ws.ws_col)
x_cols = ws.ws_col;
if (ws.ws_row)
x_lins = ws.ws_row;
}
}
#endif
/* bounds check for sane values, use defaults otherwise */
if (x_cols < MIN_COLS)
x_cols = 80;
if (x_lins < MIN_LINS)
x_lins = 24;
}