summaryrefslogtreecommitdiffstats
path: root/contrib/ncurses/test/view.c
diff options
context:
space:
mode:
authorpeter <peter@FreeBSD.org>2002-05-21 05:30:25 +0000
committerpeter <peter@FreeBSD.org>2002-05-21 05:30:25 +0000
commit9a7523d4b2c09cefef7372e8a011b8d54fb07e5e (patch)
treebd986d58f5a6e348466b5362637ba93e6cd5bf8c /contrib/ncurses/test/view.c
parent635048e64c28721e175b82444857d679f6740aa2 (diff)
downloadFreeBSD-src-9a7523d4b2c09cefef7372e8a011b8d54fb07e5e.zip
FreeBSD-src-9a7523d4b2c09cefef7372e8a011b8d54fb07e5e.tar.gz
Import ncurses-5.2-20020518 onto the vendor branch.
Obtained from: ftp://dickey.his.com/ncurses/
Diffstat (limited to 'contrib/ncurses/test/view.c')
-rw-r--r--contrib/ncurses/test/view.c264
1 files changed, 149 insertions, 115 deletions
diff --git a/contrib/ncurses/test/view.c b/contrib/ncurses/test/view.c
index ee60f27..83139d4 100644
--- a/contrib/ncurses/test/view.c
+++ b/contrib/ncurses/test/view.c
@@ -23,15 +23,14 @@
* scroll operation worked, and the refresh() code only had to do a
* partial repaint.
*
- * $Id: view.c,v 1.35 2001/01/14 01:39:24 tom Exp $
+ * $Id: view.c,v 1.52 2002/04/27 22:37:39 tom Exp $
*/
-#include <test.priv.h>
-
-#include <string.h>
#include <ctype.h>
-#include <signal.h>
#include <time.h>
+#include <locale.h>
+
+#include <test.priv.h>
#if HAVE_TERMIOS_H
# include <termios.h>
@@ -45,6 +44,8 @@
# endif
#endif
+#define my_pair 1
+
/* This is needed to compile 'struct winsize' */
#if NEED_PTEM_H
#include <sys/stream.h>
@@ -52,7 +53,7 @@
#endif
static RETSIGTYPE finish(int sig) GCC_NORETURN;
-static void show_all(void);
+static void show_all(const char *tag);
#if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZETERM
#define CAN_RESIZE 1
@@ -65,13 +66,13 @@ static RETSIGTYPE adjust(int sig);
static int interrupted;
#endif
-static int waiting;
-static int shift;
-static int utf8_mode = FALSE;
+static bool waiting = FALSE;
+static int shift = 0;
+static bool try_color = FALSE;
static char *fname;
-static chtype **lines;
-static chtype **lptr;
+static NCURSES_CH_T **my_lines;
+static NCURSES_CH_T **lptr;
static void
usage(void)
@@ -81,28 +82,37 @@ usage(void)
"Usage: view [options] file"
,""
,"Options:"
+ ," -c use color if terminal supports it"
+ ," -i ignore INT, QUIT, TERM signals"
," -n NUM specify maximum number of lines (default 1000)"
#if defined(KEY_RESIZE)
- ," -r use experimental KEY_RESIZE rather than our own handler"
+ ," -r use old-style sigwinch handler rather than KEY_RESIZE"
#endif
#ifdef TRACE
," -t trace screen updates"
," -T NUM specify trace mask"
#endif
- ," -u translate UTF-8 data"
};
size_t n;
for (n = 0; n < SIZEOF(msg); n++)
fprintf(stderr, "%s\n", msg[n]);
- exit(EXIT_FAILURE);
+ ExitProgram(EXIT_FAILURE);
}
static int
-ch_len(chtype * src)
+ch_len(NCURSES_CH_T * src)
{
int result = 0;
+#if USE_WIDEC_SUPPORT
+#endif
+
+#if USE_WIDEC_SUPPORT
+ while (getcchar(src++, NULL, NULL, NULL, NULL) > 0)
+ result++;
+#else
while (*src++)
result++;
+#endif
return result;
}
@@ -110,78 +120,56 @@ ch_len(chtype * src)
* Allocate a string into an array of chtype's. If UTF-8 mode is
* active, translate the string accordingly.
*/
-static chtype *
+static NCURSES_CH_T *
ch_dup(char *src)
{
unsigned len = strlen(src);
- chtype *dst = typeMalloc(chtype, len + 1);
+ NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
unsigned j, k;
- unsigned utf_count = 0;
- unsigned utf_char = 0;
-
-#define UCS_REPL 0xfffd
+#if USE_WIDEC_SUPPORT
+ wchar_t wstr[CCHARW_MAX + 1];
+ wchar_t wch;
+ int l = 0;
+ mbstate_t state;
+ size_t rc;
+ int width;
+#endif
+#if USE_WIDEC_SUPPORT
+ memset(&state, 0, sizeof(state));
+#endif
for (j = k = 0; j < len; j++) {
- if (utf8_mode) {
- unsigned c = CharOf(src[j]);
- /* Combine UTF-8 into Unicode */
- if (c < 0x80) {
- /* We received an ASCII character */
- if (utf_count > 0)
- dst[k++] = UCS_REPL; /* prev. sequence incomplete */
- dst[k++] = c;
- utf_count = 0;
- } else if (c < 0xc0) {
- /* We received a continuation byte */
- if (utf_count < 1) {
- dst[k++] = UCS_REPL; /* ... unexpectedly */
- } else {
- if (!utf_char && !((c & 0x7f) >> (7 - utf_count))) {
- utf_char = UCS_REPL;
- }
- /* characters outside UCS-2 become UCS_REPL */
- if (utf_char > 0x03ff) {
- /* value would be >0xffff */
- utf_char = UCS_REPL;
- } else {
- utf_char <<= 6;
- utf_char |= (c & 0x3f);
- }
- utf_count--;
- if (utf_count == 0)
- dst[k++] = utf_char;
- }
- } else {
- /* We received a sequence start byte */
- if (utf_count > 0)
- dst[k++] = UCS_REPL; /* prev. sequence incomplete */
- if (c < 0xe0) {
- utf_count = 1;
- utf_char = (c & 0x1f);
- if (!(c & 0x1e))
- utf_char = UCS_REPL; /* overlong sequence */
- } else if (c < 0xf0) {
- utf_count = 2;
- utf_char = (c & 0x0f);
- } else if (c < 0xf8) {
- utf_count = 3;
- utf_char = (c & 0x07);
- } else if (c < 0xfc) {
- utf_count = 4;
- utf_char = (c & 0x03);
- } else if (c < 0xfe) {
- utf_count = 5;
- utf_char = (c & 0x01);
- } else {
- dst[k++] = UCS_REPL;
- utf_count = 0;
- }
- }
- } else {
- dst[k++] = src[j];
+#if USE_WIDEC_SUPPORT
+ rc = mbrtowc(&wch, src + j, len - j, &state);
+ if (rc == (size_t) -1 || rc == (size_t) -2)
+ break;
+ j += rc - 1;
+ if ((width = wcwidth(wch)) < 0)
+ break;
+ if ((width > 0 && l > 0) || l == CCHARW_MAX) {
+ wstr[l] = L'\0';
+ l = 0;
+ if (setcchar(dst + k, wstr, 0, 0, NULL) != OK)
+ break;
+ ++k;
}
+ if (width == 0 && l == 0)
+ wstr[l++] = L' ';
+ wstr[l++] = wch;
+#else
+ dst[k++] = src[j];
+#endif
+ }
+#if USE_WIDEC_SUPPORT
+ if (l > 0) {
+ wstr[l] = L'\0';
+ if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
+ ++k;
}
+ setcchar(dst + k, L"", 0, 0, NULL);
+#else
dst[k] = 0;
+#endif
return dst;
}
@@ -193,24 +181,43 @@ main(int argc, char *argv[])
char buf[BUFSIZ];
int i;
int my_delay = 0;
- chtype **olptr;
+ NCURSES_CH_T **olptr;
int length = 0;
int value = 0;
bool done = FALSE;
bool got_number = FALSE;
#if CAN_RESIZE
- bool use_resize = TRUE;
+ bool nonposix_resize = FALSE;
+#endif
+ const char *my_label = "Input";
+
+ setlocale(LC_ALL, "");
+
+#ifndef NCURSES_VERSION
+ /*
+ * We know ncurses will catch SIGINT if we don't establish our own handler.
+ * Other versions of curses may/may not catch it.
+ */
+ (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
#endif
- while ((i = getopt(argc, argv, "n:rtT:u")) != EOF) {
+ while ((i = getopt(argc, argv, "cin:rtT:")) != EOF) {
switch (i) {
+ case 'c':
+ try_color = TRUE;
+ break;
+ case 'i':
+ signal(SIGINT, SIG_IGN);
+ signal(SIGQUIT, SIG_IGN);
+ signal(SIGTERM, SIG_IGN);
+ break;
case 'n':
if ((MAXLINES = atoi(optarg)) < 1)
usage();
break;
#if CAN_RESIZE
case 'r':
- use_resize = FALSE;
+ nonposix_resize = TRUE;
break;
#endif
#ifdef TRACE
@@ -221,9 +228,6 @@ main(int argc, char *argv[])
trace(TRACE_CALLS);
break;
#endif
- case 'u':
- utf8_mode = TRUE;
- break;
default:
usage();
}
@@ -231,23 +235,21 @@ main(int argc, char *argv[])
if (optind + 1 != argc)
usage();
- if ((lines = typeMalloc(chtype *, MAXLINES + 2)) == 0)
+ if ((my_lines = typeMalloc(NCURSES_CH_T *, MAXLINES + 2)) == 0)
usage();
fname = argv[optind];
if ((fp = fopen(fname, "r")) == 0) {
perror(fname);
- return EXIT_FAILURE;
+ ExitProgram(EXIT_FAILURE);
}
-
- (void) signal(SIGINT, finish); /* arrange interrupts to terminate */
#if CAN_RESIZE
- if (use_resize)
+ if (nonposix_resize)
(void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
#endif
/* slurp the file */
- for (lptr = &lines[0]; (lptr - lines) < MAXLINES; lptr++) {
+ for (lptr = &my_lines[0]; (lptr - my_lines) < MAXLINES; lptr++) {
char temp[BUFSIZ], *s, *d;
int col;
@@ -263,19 +265,24 @@ main(int argc, char *argv[])
col = (col | 7) + 1;
while ((d - temp) != col)
*d++ = ' ';
- } else if (isprint(CharOf(*d)) || utf8_mode) {
+ } else
+#if USE_WIDEC_SUPPORT
+ col++, d++;
+#else
+ if (isprint(UChar(*d))) {
col++;
d++;
} else {
- sprintf(d, "\\%03o", CharOf(*s));
+ sprintf(d, "\\%03o", UChar(*s));
d += strlen(d);
col = (d - temp);
}
+#endif
}
*lptr = ch_dup(temp);
}
(void) fclose(fp);
- length = lptr - lines;
+ length = lptr - my_lines;
(void) initscr(); /* initialize the curses library */
keypad(stdscr, TRUE); /* enable keyboard mapping */
@@ -285,18 +292,30 @@ main(int argc, char *argv[])
nodelay(stdscr, TRUE);
idlok(stdscr, TRUE); /* allow use of insert/delete line */
- lptr = lines;
+ if (try_color) {
+ if (has_colors()) {
+ start_color();
+ init_pair(my_pair, COLOR_WHITE, COLOR_BLUE);
+ bkgd(COLOR_PAIR(my_pair));
+ } else {
+ try_color = FALSE;
+ }
+ }
+
+ lptr = my_lines;
while (!done) {
int n, c;
if (!got_number)
- show_all();
+ show_all(my_label);
n = 0;
for (;;) {
#if CAN_RESIZE
- if (interrupted)
+ if (interrupted) {
adjust(0);
+ my_label = "interrupt";
+ }
#endif
waiting = TRUE;
c = getch();
@@ -318,12 +337,14 @@ main(int argc, char *argv[])
n = 1;
}
+ if (c != ERR)
+ my_label = keyname(c);
switch (c) {
case KEY_DOWN:
case 'n':
olptr = lptr;
for (i = 0; i < n; i++)
- if ((lptr - lines) < (length - LINES + 1))
+ if ((lptr - my_lines) < (length - LINES + 1))
lptr++;
else
break;
@@ -334,7 +355,7 @@ main(int argc, char *argv[])
case 'p':
olptr = lptr;
for (i = 0; i < n; i++)
- if (lptr > lines)
+ if (lptr > my_lines)
lptr--;
else
break;
@@ -343,28 +364,29 @@ main(int argc, char *argv[])
case 'h':
case KEY_HOME:
- lptr = lines;
+ lptr = my_lines;
break;
case 'e':
case KEY_END:
if (length > LINES)
- lptr = lines + length - LINES + 1;
+ lptr = my_lines + length - LINES + 1;
else
- lptr = lines;
+ lptr = my_lines;
break;
case 'r':
case KEY_RIGHT:
- shift++;
+ shift += n;
break;
case 'l':
case KEY_LEFT:
- if (shift)
- shift--;
- else
+ shift -= n;
+ if (shift < 0) {
+ shift = 0;
beep();
+ }
break;
case 'q':
@@ -408,14 +430,20 @@ static RETSIGTYPE
finish(int sig)
{
endwin();
- exit(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
+ ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
}
#if CAN_RESIZE
/*
* This uses functions that are "unsafe", but it seems to work on SunOS and
- * Linux. The 'wrefresh(curscr)' is needed to force the refresh to start from
- * the top of the screen -- some xterms mangle the bitmap while resizing.
+ * Linux. Usually: the "unsafe" refers to the functions that POSIX lists
+ * which may be called from a signal handler. Those do not include buffered
+ * I/O, which is used for instance in wrefresh(). To be really portable, you
+ * should use the KEY_RESIZE return (which relies on ncurses' sigwinch
+ * handler).
+ *
+ * The 'wrefresh(curscr)' is needed to force the refresh to start from the top
+ * of the screen -- some xterms mangle the bitmap while resizing.
*/
static RETSIGTYPE
adjust(int sig)
@@ -424,9 +452,9 @@ adjust(int sig)
struct winsize size;
if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
- resizeterm(size.ws_row, size.ws_col);
+ resize_term(size.ws_row, size.ws_col);
wrefresh(curscr); /* Linux needs this */
- show_all();
+ show_all(sig ? "SIGWINCH" : "interrupt");
}
interrupted = FALSE;
} else {
@@ -437,15 +465,15 @@ adjust(int sig)
#endif /* CAN_RESIZE */
static void
-show_all(void)
+show_all(const char *tag)
{
int i;
char temp[BUFSIZ];
- chtype *s;
+ NCURSES_CH_T *s;
time_t this_time;
#if CAN_RESIZE
- sprintf(temp, "(%3dx%3d) col %d ", LINES, COLS, shift);
+ sprintf(temp, "%s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
i = strlen(temp);
sprintf(temp + i, "view %.*s", (int) (sizeof(temp) - 7 - i), fname);
#else
@@ -465,12 +493,18 @@ show_all(void)
scrollok(stdscr, FALSE); /* prevent screen from moving */
for (i = 1; i < LINES; i++) {
move(i, 0);
- printw("%3ld:", (long) (lptr + i - lines));
+ printw("%3ld:", (long) (lptr + i - my_lines));
clrtoeol();
if ((s = lptr[i - 1]) != 0) {
int len = ch_len(s);
if (len > shift)
+#if USE_WIDEC_SUPPORT
+ add_wchstr(s + shift);
+#else
addchstr(s + shift);
+#endif
+ if (try_color)
+ wchgat(stdscr, -1, A_NORMAL, my_pair, NULL);
}
}
setscrreg(1, LINES - 1);
OpenPOWER on IntegriCloud