mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-20 17:53:02 +00:00
diagnostics: get the screen width from the terminal
* bootstrap.conf: We need winsz-ioctl and winsz-termios. * src/location.c (columns): Use winsize to get the number of columns. Code taken from the GNU Coreutils. * src/location.h, src/location.c (caret_init): New. * src/complain.c (complain_init): Call it. * tests/bison.in: Export COLUMNS so that users of tests/bison can enjoy proper line truncation.
This commit is contained in:
@@ -45,6 +45,8 @@ gnulib_modules='
|
|||||||
unistd unistd-safer unlink unlocked-io
|
unistd unistd-safer unlink unlocked-io
|
||||||
update-copyright unsetenv verify
|
update-copyright unsetenv verify
|
||||||
warnings
|
warnings
|
||||||
|
winsz-ioctl
|
||||||
|
winsz-termios
|
||||||
xalloc
|
xalloc
|
||||||
xalloc-die
|
xalloc-die
|
||||||
xconcat-filename
|
xconcat-filename
|
||||||
|
|||||||
2
m4/.gitignore
vendored
2
m4/.gitignore
vendored
@@ -202,3 +202,5 @@
|
|||||||
/xalloc.m4
|
/xalloc.m4
|
||||||
/xsize.m4
|
/xsize.m4
|
||||||
/xstrndup.m4
|
/xstrndup.m4
|
||||||
|
/jm-winsz1.m4
|
||||||
|
/jm-winsz2.m4
|
||||||
|
|||||||
@@ -308,6 +308,8 @@ complain_init_color (void)
|
|||||||
void
|
void
|
||||||
complain_init (void)
|
complain_init (void)
|
||||||
{
|
{
|
||||||
|
caret_init ();
|
||||||
|
|
||||||
warnings warnings_default =
|
warnings warnings_default =
|
||||||
Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother;
|
Wconflicts_sr | Wconflicts_rr | Wdeprecated | Wother;
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,14 @@
|
|||||||
#include <mbswidth.h>
|
#include <mbswidth.h>
|
||||||
#include <quotearg.h>
|
#include <quotearg.h>
|
||||||
#include <stdio.h> /* fileno */
|
#include <stdio.h> /* fileno */
|
||||||
|
#include <sys/ioctl.h>
|
||||||
#include <sys/stat.h> /* fstat */
|
#include <sys/stat.h> /* fstat */
|
||||||
|
#include <termios.h>
|
||||||
|
|
||||||
|
#ifdef WINSIZE_IN_PTEM
|
||||||
|
# include <sys/stream.h>
|
||||||
|
# include <sys/ptem.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "complain.h"
|
#include "complain.h"
|
||||||
#include "getargs.h"
|
#include "getargs.h"
|
||||||
@@ -39,7 +46,13 @@ min_int (int a, int b)
|
|||||||
return a < b ? a : b;
|
return a < b ? a : b;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The terminal width. */
|
static int
|
||||||
|
max_int (int a, int b)
|
||||||
|
{
|
||||||
|
return a >= b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The terminal width. Not less than 40. */
|
||||||
static int
|
static int
|
||||||
columns (void)
|
columns (void)
|
||||||
{
|
{
|
||||||
@@ -50,9 +63,21 @@ columns (void)
|
|||||||
unsigned long ul = strtoul (cp, NULL, 10);
|
unsigned long ul = strtoul (cp, NULL, 10);
|
||||||
res = ul < INT_MAX ? ul : INT_MAX;
|
res = ul < INT_MAX ? ul : INT_MAX;
|
||||||
}
|
}
|
||||||
return res;
|
else
|
||||||
|
{
|
||||||
|
#ifdef TIOCGWINSZ
|
||||||
|
struct winsize ws;
|
||||||
|
if (ioctl (STDERR_FILENO, TIOCGWINSZ, &ws) != -1
|
||||||
|
&& 0 < ws.ws_col && ws.ws_col == (size_t) ws.ws_col)
|
||||||
|
res = ws.ws_col;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return max_int (res, 40);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Available screen width. */
|
||||||
|
static int screen_width = 80;
|
||||||
|
|
||||||
/* If BUF is null, add BUFSIZE (which in this case must be less than
|
/* If BUF is null, add BUFSIZE (which in this case must be less than
|
||||||
INT_MAX) to COLUMN; otherwise, add mbsnwidth (BUF, BUFSIZE, 0) to
|
INT_MAX) to COLUMN; otherwise, add mbsnwidth (BUF, BUFSIZE, 0) to
|
||||||
COLUMN. If an overflow occurs, return INT_MAX. */
|
COLUMN. If an overflow occurs, return INT_MAX. */
|
||||||
@@ -235,6 +260,11 @@ caret_set_file (const char *file)
|
|||||||
return caret_info.file;
|
return caret_info.file;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void caret_init (void)
|
||||||
|
{
|
||||||
|
screen_width = columns ();
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
caret_free (void)
|
caret_free (void)
|
||||||
{
|
{
|
||||||
@@ -308,10 +338,17 @@ location_caret (location loc, const char *style, FILE *out)
|
|||||||
boundary_compute (&caret_info.pos, mb_ptr (c), mb_len (c));
|
boundary_compute (&caret_info.pos, mb_ptr (c), mb_len (c));
|
||||||
}
|
}
|
||||||
int line_len = caret_info.pos.column;
|
int line_len = caret_info.pos.column;
|
||||||
|
/* Go back to the beginning of line. */
|
||||||
|
fseek (caret_info.file, caret_info.offset, SEEK_SET);
|
||||||
|
/* Reset mbf's internal state.
|
||||||
|
FIXME: should be done in mbfile. */
|
||||||
|
caret_info.mbfile.eof_seen = 0;
|
||||||
|
caret_info.pos.column = 1;
|
||||||
|
|
||||||
|
|
||||||
/* Available width. Eight chars are consumed by the left-margin of
|
/* Available width. Eight chars are consumed by the left-margin of
|
||||||
the quoting lines. */
|
the quoting lines. */
|
||||||
int width = columns () - 8;
|
int width = screen_width - 8;
|
||||||
int skip = 0;
|
int skip = 0;
|
||||||
if (width < line_len)
|
if (width < line_len)
|
||||||
{
|
{
|
||||||
@@ -327,13 +364,6 @@ location_caret (location loc, const char *style, FILE *out)
|
|||||||
if (width < line_len - skip)
|
if (width < line_len - skip)
|
||||||
width -= 3;
|
width -= 3;
|
||||||
|
|
||||||
/* Go back to the beginning of line. */
|
|
||||||
fseek (caret_info.file, caret_info.offset, SEEK_SET);
|
|
||||||
/* Reset mbf's internal state.
|
|
||||||
FIXME: should be done in mbfile. */
|
|
||||||
caret_info.mbfile.eof_seen = 0;
|
|
||||||
caret_info.pos.column = 1;
|
|
||||||
|
|
||||||
/* Read the actual line. Don't update the offset, so that we keep a pointer
|
/* Read the actual line. Don't update the offset, so that we keep a pointer
|
||||||
to the start of the line. */
|
to the start of the line. */
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -114,6 +114,9 @@ void location_compute (location *loc,
|
|||||||
Warning: uses quotearg's slot 3. */
|
Warning: uses quotearg's slot 3. */
|
||||||
unsigned location_print (location loc, FILE *out);
|
unsigned location_print (location loc, FILE *out);
|
||||||
|
|
||||||
|
/* Prepare the use of location_caret. */
|
||||||
|
void caret_init (void);
|
||||||
|
|
||||||
/* Free any allocated resources and close any open file handles that are
|
/* Free any allocated resources and close any open file handles that are
|
||||||
left-over by the usage of location_caret. */
|
left-over by the usage of location_caret. */
|
||||||
void caret_free (void);
|
void caret_free (void);
|
||||||
|
|||||||
@@ -34,6 +34,11 @@ if test -t 2; then
|
|||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# We redirect stderr, which breaks the computation of the terminal
|
||||||
|
# screen width. So export COLUMNS to Bison, hoping for the shell to
|
||||||
|
# have defined it.
|
||||||
|
: ${COLUMNS=132}
|
||||||
|
export COLUMNS
|
||||||
$PREBISON "$abs_top_builddir/src/bison" ${1+"$@"} 2>"$stderr"
|
$PREBISON "$abs_top_builddir/src/bison" ${1+"$@"} 2>"$stderr"
|
||||||
status=$?
|
status=$?
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user