2006-12-18 12:32:42 +08:00
|
|
|
/****************************************************************************
|
2008-01-20 08:51:10 +08:00
|
|
|
* Copyright (c) 1998-2007,2008 Free Software Foundation, Inc. *
|
2006-12-18 12:32:42 +08:00
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a *
|
|
|
|
* copy of this software and associated documentation files (the *
|
|
|
|
* "Software"), to deal in the Software without restriction, including *
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, *
|
|
|
|
* distribute, distribute with modifications, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included *
|
|
|
|
* in all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
|
|
|
|
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
|
|
|
|
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
|
|
|
* *
|
|
|
|
* Except as contained in this notice, the name(s) of the above copyright *
|
|
|
|
* holders shall not be used in advertising or otherwise to promote the *
|
|
|
|
* sale, use or other dealings in this Software without prior written *
|
|
|
|
* authorization. *
|
|
|
|
****************************************************************************/
|
1997-05-15 12:00:00 +08:00
|
|
|
/*
|
|
|
|
* view.c -- a silly little viewer program
|
|
|
|
*
|
|
|
|
* written by Eric S. Raymond <esr@snark.thyrsus.com> December 1994
|
|
|
|
* to test the scrolling code in ncurses.
|
|
|
|
*
|
|
|
|
* modified by Thomas Dickey <dickey@clark.net> July 1995 to demonstrate
|
2000-07-09 10:46:08 +08:00
|
|
|
* the use of 'resizeterm()', and May 2000 to illustrate wide-character
|
|
|
|
* handling.
|
1997-05-15 12:00:00 +08:00
|
|
|
*
|
1998-03-01 12:21:12 +08:00
|
|
|
* Takes a filename argument. It's a simple file-viewer with various
|
1997-05-15 12:00:00 +08:00
|
|
|
* scroll-up and scroll-down commands.
|
|
|
|
*
|
|
|
|
* n -- scroll one line forward
|
|
|
|
* p -- scroll one line back
|
|
|
|
*
|
|
|
|
* Either command accepts a numeric prefix interpreted as a repeat count.
|
|
|
|
* Thus, typing `5n' should scroll forward 5 lines in the file.
|
|
|
|
*
|
|
|
|
* The way you can tell this is working OK is that, in the trace file,
|
|
|
|
* there should be one scroll operation plus a small number of line
|
|
|
|
* updates, as opposed to a whole-page update. This means the physical
|
|
|
|
* scroll operation worked, and the refresh() code only had to do a
|
|
|
|
* partial repaint.
|
|
|
|
*
|
2008-09-07 09:02:23 +08:00
|
|
|
* $Id: view.c,v 1.69 2008/09/06 22:10:50 tom Exp $
|
1997-05-15 12:00:00 +08:00
|
|
|
*/
|
|
|
|
|
2005-10-10 02:41:57 +08:00
|
|
|
#include <test.priv.h>
|
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
#include <time.h>
|
|
|
|
|
2005-10-10 02:41:57 +08:00
|
|
|
#undef CTRL /* conflict on AIX 5.2 with <sys/ioctl.h> */
|
1997-05-15 12:00:00 +08:00
|
|
|
|
|
|
|
#if HAVE_TERMIOS_H
|
|
|
|
# include <termios.h>
|
|
|
|
#else
|
|
|
|
# include <sgtty.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(sun) || !HAVE_TERMIOS_H
|
|
|
|
# if HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
#define my_pair 1
|
|
|
|
|
1997-05-15 12:00:00 +08:00
|
|
|
/* This is needed to compile 'struct winsize' */
|
1998-03-01 12:21:12 +08:00
|
|
|
#if NEED_PTEM_H
|
1997-05-15 12:00:00 +08:00
|
|
|
#include <sys/stream.h>
|
|
|
|
#include <sys/ptem.h>
|
|
|
|
#endif
|
|
|
|
|
2008-09-07 09:02:23 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
|
|
|
#if HAVE_MBTOWC && HAVE_MBLEN
|
|
|
|
#define reset_mbytes(state) mblen(NULL, 0), mbtowc(NULL, NULL, 0)
|
|
|
|
#define count_mbytes(buffer,length,state) mblen(buffer,length)
|
|
|
|
#define check_mbytes(wch,buffer,length,state) \
|
|
|
|
(int) mbtowc(&wch, buffer, length)
|
|
|
|
#define state_unused
|
|
|
|
#elif HAVE_MBRTOWC && HAVE_MBRLEN
|
|
|
|
#define reset_mbytes(state) init_mb(state)
|
|
|
|
#define count_mbytes(buffer,length,state) mbrlen(buffer,length,&state)
|
|
|
|
#define check_mbytes(wch,buffer,length,state) \
|
|
|
|
(int) mbrtowc(&wch, buffer, length, &state)
|
|
|
|
#else
|
|
|
|
make an error
|
|
|
|
#endif
|
|
|
|
#endif /* USE_WIDEC_SUPPORT */
|
|
|
|
|
1998-03-01 12:21:12 +08:00
|
|
|
static RETSIGTYPE finish(int sig) GCC_NORETURN;
|
2002-10-13 11:35:53 +08:00
|
|
|
static void show_all(const char *tag);
|
1998-03-01 12:21:12 +08:00
|
|
|
|
2004-02-09 10:15:26 +08:00
|
|
|
#if defined(SIGWINCH) && defined(TIOCGWINSZ) && HAVE_RESIZE_TERM
|
1997-05-15 12:00:00 +08:00
|
|
|
#define CAN_RESIZE 1
|
|
|
|
#else
|
|
|
|
#define CAN_RESIZE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CAN_RESIZE
|
|
|
|
static RETSIGTYPE adjust(int sig);
|
2000-07-09 10:46:08 +08:00
|
|
|
static int interrupted;
|
1997-05-15 12:00:00 +08:00
|
|
|
#endif
|
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
static bool waiting = FALSE;
|
|
|
|
static int shift = 0;
|
|
|
|
static bool try_color = FALSE;
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
static char *fname;
|
2005-10-10 02:41:57 +08:00
|
|
|
static NCURSES_CH_T **vec_lines;
|
2002-10-13 11:35:53 +08:00
|
|
|
static NCURSES_CH_T **lptr;
|
2005-10-10 02:41:57 +08:00
|
|
|
static int num_lines;
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
static void
|
|
|
|
usage(void)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2000-07-09 10:46:08 +08:00
|
|
|
static const char *msg[] =
|
|
|
|
{
|
|
|
|
"Usage: view [options] file"
|
1998-03-01 12:21:12 +08:00
|
|
|
,""
|
|
|
|
,"Options:"
|
2002-10-13 11:35:53 +08:00
|
|
|
," -c use color if terminal supports it"
|
|
|
|
," -i ignore INT, QUIT, TERM signals"
|
1998-03-01 12:21:12 +08:00
|
|
|
," -n NUM specify maximum number of lines (default 1000)"
|
|
|
|
#if defined(KEY_RESIZE)
|
2002-10-13 11:35:53 +08:00
|
|
|
," -r use old-style sigwinch handler rather than KEY_RESIZE"
|
1998-03-01 12:21:12 +08:00
|
|
|
#endif
|
|
|
|
#ifdef TRACE
|
|
|
|
," -t trace screen updates"
|
|
|
|
," -T NUM specify trace mask"
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
size_t n;
|
|
|
|
for (n = 0; n < SIZEOF(msg); n++)
|
|
|
|
fprintf(stderr, "%s\n", msg[n]);
|
2002-10-13 11:35:53 +08:00
|
|
|
ExitProgram(EXIT_FAILURE);
|
2000-07-09 10:46:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2002-10-13 11:35:53 +08:00
|
|
|
ch_len(NCURSES_CH_T * src)
|
2000-07-09 10:46:08 +08:00
|
|
|
{
|
|
|
|
int result = 0;
|
2002-10-13 11:35:53 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if USE_WIDEC_SUPPORT
|
|
|
|
while (getcchar(src++, NULL, NULL, NULL, NULL) > 0)
|
|
|
|
result++;
|
|
|
|
#else
|
2000-07-09 10:46:08 +08:00
|
|
|
while (*src++)
|
|
|
|
result++;
|
2002-10-13 11:35:53 +08:00
|
|
|
#endif
|
2000-07-09 10:46:08 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a string into an array of chtype's. If UTF-8 mode is
|
|
|
|
* active, translate the string accordingly.
|
|
|
|
*/
|
2002-10-13 11:35:53 +08:00
|
|
|
static NCURSES_CH_T *
|
2000-07-09 10:46:08 +08:00
|
|
|
ch_dup(char *src)
|
|
|
|
{
|
|
|
|
unsigned len = strlen(src);
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
|
2000-07-09 10:46:08 +08:00
|
|
|
unsigned j, k;
|
2002-10-13 11:35:53 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
|
|
|
wchar_t wstr[CCHARW_MAX + 1];
|
|
|
|
wchar_t wch;
|
|
|
|
int l = 0;
|
|
|
|
size_t rc;
|
|
|
|
int width;
|
2008-09-07 09:02:23 +08:00
|
|
|
#ifndef state_unused
|
|
|
|
mbstate_t state;
|
2002-10-13 11:35:53 +08:00
|
|
|
#endif
|
2008-09-07 09:02:23 +08:00
|
|
|
#endif /* USE_WIDEC_SUPPORT */
|
2000-07-09 10:46:08 +08:00
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
2008-09-07 09:02:23 +08:00
|
|
|
reset_mbytes(state);
|
2002-10-13 11:35:53 +08:00
|
|
|
#endif
|
2000-07-09 10:46:08 +08:00
|
|
|
for (j = k = 0; j < len; j++) {
|
2002-10-13 11:35:53 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
2008-09-07 09:02:23 +08:00
|
|
|
rc = check_mbytes(wch, src + j, len - j, state);
|
2002-10-13 11:35:53 +08:00
|
|
|
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;
|
2000-07-09 10:46:08 +08:00
|
|
|
}
|
2002-10-13 11:35:53 +08:00
|
|
|
if (width == 0 && l == 0)
|
|
|
|
wstr[l++] = L' ';
|
|
|
|
wstr[l++] = wch;
|
|
|
|
#else
|
|
|
|
dst[k++] = src[j];
|
|
|
|
#endif
|
2000-07-09 10:46:08 +08:00
|
|
|
}
|
2002-10-13 11:35:53 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
|
|
|
if (l > 0) {
|
|
|
|
wstr[l] = L'\0';
|
|
|
|
if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
|
|
|
|
++k;
|
|
|
|
}
|
2005-10-10 02:41:57 +08:00
|
|
|
wstr[0] = L'\0';
|
|
|
|
setcchar(dst + k, wstr, 0, 0, NULL);
|
2002-10-13 11:35:53 +08:00
|
|
|
#else
|
2000-07-09 10:46:08 +08:00
|
|
|
dst[k] = 0;
|
2002-10-13 11:35:53 +08:00
|
|
|
#endif
|
2000-07-09 10:46:08 +08:00
|
|
|
return dst;
|
1998-03-01 12:21:12 +08:00
|
|
|
}
|
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2000-07-09 10:46:08 +08:00
|
|
|
int MAXLINES = 1000;
|
|
|
|
FILE *fp;
|
|
|
|
char buf[BUFSIZ];
|
|
|
|
int i;
|
2002-10-13 11:35:53 +08:00
|
|
|
int my_delay = 0;
|
|
|
|
NCURSES_CH_T **olptr;
|
|
|
|
int value = 0;
|
|
|
|
bool done = FALSE;
|
|
|
|
bool got_number = FALSE;
|
1998-03-01 12:21:12 +08:00
|
|
|
#if CAN_RESIZE
|
2002-10-13 11:35:53 +08:00
|
|
|
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 */
|
1998-03-01 12:21:12 +08:00
|
|
|
#endif
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2007-07-22 07:58:49 +08:00
|
|
|
while ((i = getopt(argc, argv, "cin:rtT:")) != -1) {
|
1998-03-01 12:21:12 +08:00
|
|
|
switch (i) {
|
2002-10-13 11:35:53 +08:00
|
|
|
case 'c':
|
|
|
|
try_color = TRUE;
|
|
|
|
break;
|
|
|
|
case 'i':
|
2006-12-18 12:32:42 +08:00
|
|
|
CATCHALL(SIG_IGN);
|
2002-10-13 11:35:53 +08:00
|
|
|
break;
|
1998-03-01 12:21:12 +08:00
|
|
|
case 'n':
|
2008-08-05 09:06:16 +08:00
|
|
|
if ((MAXLINES = atoi(optarg)) < 1 ||
|
|
|
|
(MAXLINES + 2) <= 1)
|
1998-03-01 12:21:12 +08:00
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
#if CAN_RESIZE
|
|
|
|
case 'r':
|
2002-10-13 11:35:53 +08:00
|
|
|
nonposix_resize = TRUE;
|
1998-03-01 12:21:12 +08:00
|
|
|
break;
|
|
|
|
#endif
|
1997-05-15 12:00:00 +08:00
|
|
|
#ifdef TRACE
|
1998-03-01 12:21:12 +08:00
|
|
|
case 'T':
|
2005-10-10 02:41:57 +08:00
|
|
|
trace((unsigned) atoi(optarg));
|
1998-03-01 12:21:12 +08:00
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
trace(TRACE_CALLS);
|
|
|
|
break;
|
1997-05-15 12:00:00 +08:00
|
|
|
#endif
|
1998-03-01 12:21:12 +08:00
|
|
|
default:
|
|
|
|
usage();
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
1998-03-01 12:21:12 +08:00
|
|
|
if (optind + 1 != argc)
|
|
|
|
usage();
|
|
|
|
|
2005-10-10 02:41:57 +08:00
|
|
|
if ((vec_lines = typeMalloc(NCURSES_CH_T *, MAXLINES + 2)) == 0)
|
1998-03-01 12:21:12 +08:00
|
|
|
usage();
|
|
|
|
|
|
|
|
fname = argv[optind];
|
|
|
|
if ((fp = fopen(fname, "r")) == 0) {
|
|
|
|
perror(fname);
|
2002-10-13 11:35:53 +08:00
|
|
|
ExitProgram(EXIT_FAILURE);
|
1998-03-01 12:21:12 +08:00
|
|
|
}
|
1997-05-15 12:00:00 +08:00
|
|
|
#if CAN_RESIZE
|
2002-10-13 11:35:53 +08:00
|
|
|
if (nonposix_resize)
|
2000-07-09 10:46:08 +08:00
|
|
|
(void) signal(SIGWINCH, adjust); /* arrange interrupts to resize */
|
1997-05-15 12:00:00 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* slurp the file */
|
2005-10-10 02:41:57 +08:00
|
|
|
for (lptr = &vec_lines[0]; (lptr - vec_lines) < MAXLINES; lptr++) {
|
1997-05-15 12:00:00 +08:00
|
|
|
char temp[BUFSIZ], *s, *d;
|
2000-07-09 10:46:08 +08:00
|
|
|
int col;
|
1997-05-15 12:00:00 +08:00
|
|
|
|
1998-03-01 12:21:12 +08:00
|
|
|
if (fgets(buf, sizeof(buf), fp) == 0)
|
|
|
|
break;
|
1997-05-15 12:00:00 +08:00
|
|
|
|
|
|
|
/* convert tabs so that shift will work properly */
|
|
|
|
for (s = buf, d = temp, col = 0; (*d = *s) != '\0'; s++) {
|
|
|
|
if (*d == '\n') {
|
|
|
|
*d = '\0';
|
|
|
|
break;
|
|
|
|
} else if (*d == '\t') {
|
|
|
|
col = (col | 7) + 1;
|
2000-07-09 10:46:08 +08:00
|
|
|
while ((d - temp) != col)
|
1997-05-15 12:00:00 +08:00
|
|
|
*d++ = ' ';
|
2002-10-13 11:35:53 +08:00
|
|
|
} else
|
|
|
|
#if USE_WIDEC_SUPPORT
|
|
|
|
col++, d++;
|
|
|
|
#else
|
|
|
|
if (isprint(UChar(*d))) {
|
1997-05-15 12:00:00 +08:00
|
|
|
col++;
|
|
|
|
d++;
|
|
|
|
} else {
|
2002-10-13 11:35:53 +08:00
|
|
|
sprintf(d, "\\%03o", UChar(*s));
|
1997-05-15 12:00:00 +08:00
|
|
|
d += strlen(d);
|
|
|
|
col = (d - temp);
|
|
|
|
}
|
2002-10-13 11:35:53 +08:00
|
|
|
#endif
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
*lptr = ch_dup(temp);
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
(void) fclose(fp);
|
2005-10-10 02:41:57 +08:00
|
|
|
num_lines = lptr - vec_lines;
|
1998-03-01 12:21:12 +08:00
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
(void) initscr(); /* initialize the curses library */
|
|
|
|
keypad(stdscr, TRUE); /* enable keyboard mapping */
|
|
|
|
(void) nonl(); /* tell curses not to do NL->CR/NL on output */
|
|
|
|
(void) cbreak(); /* take input chars one at a time, no wait for \n */
|
|
|
|
(void) noecho(); /* don't echo input */
|
2002-10-13 11:35:53 +08:00
|
|
|
nodelay(stdscr, TRUE);
|
2000-07-09 10:46:08 +08:00
|
|
|
idlok(stdscr, TRUE); /* allow use of insert/delete line */
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-10 02:41:57 +08:00
|
|
|
lptr = vec_lines;
|
1997-05-15 12:00:00 +08:00
|
|
|
while (!done) {
|
1998-03-01 12:21:12 +08:00
|
|
|
int n, c;
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
if (!got_number)
|
|
|
|
show_all(my_label);
|
1997-05-15 12:00:00 +08:00
|
|
|
|
|
|
|
n = 0;
|
2000-07-09 10:46:08 +08:00
|
|
|
for (;;) {
|
1997-05-15 12:00:00 +08:00
|
|
|
#if CAN_RESIZE
|
2002-10-13 11:35:53 +08:00
|
|
|
if (interrupted) {
|
1997-05-15 12:00:00 +08:00
|
|
|
adjust(0);
|
2002-10-13 11:35:53 +08:00
|
|
|
my_label = "interrupt";
|
|
|
|
}
|
1997-05-15 12:00:00 +08:00
|
|
|
#endif
|
|
|
|
waiting = TRUE;
|
|
|
|
c = getch();
|
|
|
|
waiting = FALSE;
|
1998-03-01 12:21:12 +08:00
|
|
|
if ((c < 127) && isdigit(c)) {
|
|
|
|
if (!got_number) {
|
2000-07-09 10:46:08 +08:00
|
|
|
mvprintw(0, 0, "Count: ");
|
1998-03-01 12:21:12 +08:00
|
|
|
clrtoeol();
|
|
|
|
}
|
2005-10-10 02:41:57 +08:00
|
|
|
addch(UChar(c));
|
2002-10-13 11:35:53 +08:00
|
|
|
value = 10 * value + (c - '0');
|
1998-03-01 12:21:12 +08:00
|
|
|
got_number = TRUE;
|
2000-07-09 10:46:08 +08:00
|
|
|
} else
|
1997-05-15 12:00:00 +08:00
|
|
|
break;
|
|
|
|
}
|
2002-10-13 11:35:53 +08:00
|
|
|
if (got_number && value) {
|
|
|
|
n = value;
|
|
|
|
} else {
|
1997-05-15 12:00:00 +08:00
|
|
|
n = 1;
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2002-10-13 11:35:53 +08:00
|
|
|
if (c != ERR)
|
|
|
|
my_label = keyname(c);
|
2000-07-09 10:46:08 +08:00
|
|
|
switch (c) {
|
1998-03-01 12:21:12 +08:00
|
|
|
case KEY_DOWN:
|
1997-05-15 12:00:00 +08:00
|
|
|
case 'n':
|
|
|
|
olptr = lptr;
|
1998-03-01 12:21:12 +08:00
|
|
|
for (i = 0; i < n; i++)
|
2005-10-10 02:41:57 +08:00
|
|
|
if ((lptr - vec_lines) < (num_lines - LINES + 1))
|
1997-05-15 12:00:00 +08:00
|
|
|
lptr++;
|
1998-03-01 12:21:12 +08:00
|
|
|
else
|
1997-05-15 12:00:00 +08:00
|
|
|
break;
|
2007-07-01 08:42:27 +08:00
|
|
|
scrl(lptr - olptr);
|
1998-03-01 12:21:12 +08:00
|
|
|
break;
|
1997-05-15 12:00:00 +08:00
|
|
|
|
1998-03-01 12:21:12 +08:00
|
|
|
case KEY_UP:
|
1997-05-15 12:00:00 +08:00
|
|
|
case 'p':
|
|
|
|
olptr = lptr;
|
1998-03-01 12:21:12 +08:00
|
|
|
for (i = 0; i < n; i++)
|
2005-10-10 02:41:57 +08:00
|
|
|
if (lptr > vec_lines)
|
1997-05-15 12:00:00 +08:00
|
|
|
lptr--;
|
1998-03-01 12:21:12 +08:00
|
|
|
else
|
1997-05-15 12:00:00 +08:00
|
|
|
break;
|
2007-07-01 08:42:27 +08:00
|
|
|
scrl(lptr - olptr);
|
1998-03-01 12:21:12 +08:00
|
|
|
break;
|
1997-05-15 12:00:00 +08:00
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case KEY_HOME:
|
2005-10-10 02:41:57 +08:00
|
|
|
lptr = vec_lines;
|
1997-05-15 12:00:00 +08:00
|
|
|
break;
|
|
|
|
|
1998-03-01 12:21:12 +08:00
|
|
|
case 'e':
|
|
|
|
case KEY_END:
|
2005-10-10 02:41:57 +08:00
|
|
|
if (num_lines > LINES)
|
|
|
|
lptr = vec_lines + num_lines - LINES + 1;
|
1998-03-01 12:21:12 +08:00
|
|
|
else
|
2005-10-10 02:41:57 +08:00
|
|
|
lptr = vec_lines;
|
1998-03-01 12:21:12 +08:00
|
|
|
break;
|
|
|
|
|
1997-05-15 12:00:00 +08:00
|
|
|
case 'r':
|
|
|
|
case KEY_RIGHT:
|
2002-10-13 11:35:53 +08:00
|
|
|
shift += n;
|
1997-05-15 12:00:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
case KEY_LEFT:
|
2002-10-13 11:35:53 +08:00
|
|
|
shift -= n;
|
|
|
|
if (shift < 0) {
|
|
|
|
shift = 0;
|
1997-05-15 12:00:00 +08:00
|
|
|
beep();
|
2002-10-13 11:35:53 +08:00
|
|
|
}
|
1997-05-15 12:00:00 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
|
1998-03-01 12:21:12 +08:00
|
|
|
#ifdef KEY_RESIZE
|
2000-07-09 10:46:08 +08:00
|
|
|
case KEY_RESIZE: /* ignore this; ncurses will repaint */
|
1998-03-01 12:21:12 +08:00
|
|
|
break;
|
|
|
|
#endif
|
2002-10-13 11:35:53 +08:00
|
|
|
case 's':
|
|
|
|
if (got_number) {
|
|
|
|
halfdelay(my_delay = n);
|
|
|
|
} else {
|
|
|
|
nodelay(stdscr, FALSE);
|
|
|
|
my_delay = -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
nodelay(stdscr, TRUE);
|
|
|
|
my_delay = 0;
|
|
|
|
break;
|
1998-03-01 12:21:12 +08:00
|
|
|
case ERR:
|
2002-10-13 11:35:53 +08:00
|
|
|
if (!my_delay)
|
|
|
|
napms(50);
|
1998-03-01 12:21:12 +08:00
|
|
|
break;
|
1997-05-15 12:00:00 +08:00
|
|
|
default:
|
|
|
|
beep();
|
2002-10-13 11:35:53 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (c >= KEY_MIN || (c > 0 && !isdigit(c))) {
|
|
|
|
got_number = FALSE;
|
|
|
|
value = 0;
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-03-01 12:21:12 +08:00
|
|
|
finish(0); /* we're done */
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
static RETSIGTYPE
|
|
|
|
finish(int sig)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
|
|
|
endwin();
|
2005-10-10 02:41:57 +08:00
|
|
|
#if NO_LEAKS
|
|
|
|
if (vec_lines != 0) {
|
|
|
|
int n;
|
|
|
|
for (n = 0; n < num_lines; ++n) {
|
|
|
|
free(vec_lines[n]);
|
|
|
|
}
|
|
|
|
free(vec_lines);
|
|
|
|
}
|
|
|
|
#endif
|
2002-10-13 11:35:53 +08:00
|
|
|
ExitProgram(sig != 0 ? EXIT_FAILURE : EXIT_SUCCESS);
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if CAN_RESIZE
|
|
|
|
/*
|
|
|
|
* This uses functions that are "unsafe", but it seems to work on SunOS and
|
2002-10-13 11:35:53 +08:00
|
|
|
* 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.
|
1997-05-15 12:00:00 +08:00
|
|
|
*/
|
2000-07-09 10:46:08 +08:00
|
|
|
static RETSIGTYPE
|
|
|
|
adjust(int sig)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2000-07-09 10:46:08 +08:00
|
|
|
if (waiting || sig == 0) {
|
1997-05-15 12:00:00 +08:00
|
|
|
struct winsize size;
|
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
if (ioctl(fileno(stdout), TIOCGWINSZ, &size) == 0) {
|
2002-10-13 11:35:53 +08:00
|
|
|
resize_term(size.ws_row, size.ws_col);
|
2000-07-09 10:46:08 +08:00
|
|
|
wrefresh(curscr); /* Linux needs this */
|
2002-10-13 11:35:53 +08:00
|
|
|
show_all(sig ? "SIGWINCH" : "interrupt");
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
interrupted = FALSE;
|
|
|
|
} else {
|
|
|
|
interrupted = TRUE;
|
|
|
|
}
|
|
|
|
(void) signal(SIGWINCH, adjust); /* some systems need this */
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
#endif /* CAN_RESIZE */
|
1997-05-15 12:00:00 +08:00
|
|
|
|
2000-07-09 10:46:08 +08:00
|
|
|
static void
|
2002-10-13 11:35:53 +08:00
|
|
|
show_all(const char *tag)
|
1997-05-15 12:00:00 +08:00
|
|
|
{
|
2000-07-09 10:46:08 +08:00
|
|
|
int i;
|
|
|
|
char temp[BUFSIZ];
|
2002-10-13 11:35:53 +08:00
|
|
|
NCURSES_CH_T *s;
|
|
|
|
time_t this_time;
|
1997-05-15 12:00:00 +08:00
|
|
|
|
|
|
|
#if CAN_RESIZE
|
2008-08-05 09:06:16 +08:00
|
|
|
sprintf(temp, "%.20s (%3dx%3d) col %d ", tag, LINES, COLS, shift);
|
2000-07-09 10:46:08 +08:00
|
|
|
i = strlen(temp);
|
2008-08-05 09:06:16 +08:00
|
|
|
if ((i + 7) < (int) sizeof(temp))
|
|
|
|
sprintf(temp + i, "view %.*s", (int) (sizeof(temp) - 7 - i), fname);
|
1997-05-15 12:00:00 +08:00
|
|
|
#else
|
2008-01-20 08:51:10 +08:00
|
|
|
(void) tag;
|
2000-07-09 10:46:08 +08:00
|
|
|
sprintf(temp, "view %.*s", (int) sizeof(temp) - 7, fname);
|
1997-05-15 12:00:00 +08:00
|
|
|
#endif
|
2000-07-09 10:46:08 +08:00
|
|
|
move(0, 0);
|
|
|
|
printw("%.*s", COLS, temp);
|
|
|
|
clrtoeol();
|
2002-10-13 11:35:53 +08:00
|
|
|
this_time = time((time_t *) 0);
|
|
|
|
strcpy(temp, ctime(&this_time));
|
|
|
|
if ((i = strlen(temp)) != 0) {
|
|
|
|
temp[--i] = 0;
|
|
|
|
if (move(0, COLS - i - 2) != ERR)
|
|
|
|
printw(" %s", temp);
|
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
|
|
|
|
scrollok(stdscr, FALSE); /* prevent screen from moving */
|
|
|
|
for (i = 1; i < LINES; i++) {
|
|
|
|
move(i, 0);
|
2005-10-10 02:41:57 +08:00
|
|
|
printw("%3ld:", (long) (lptr + i - vec_lines));
|
1997-05-15 12:00:00 +08:00
|
|
|
clrtoeol();
|
2000-07-09 10:46:08 +08:00
|
|
|
if ((s = lptr[i - 1]) != 0) {
|
|
|
|
int len = ch_len(s);
|
2004-02-09 10:15:26 +08:00
|
|
|
if (len > shift) {
|
2002-10-13 11:35:53 +08:00
|
|
|
#if USE_WIDEC_SUPPORT
|
|
|
|
add_wchstr(s + shift);
|
|
|
|
#else
|
2000-07-09 10:46:08 +08:00
|
|
|
addchstr(s + shift);
|
2002-10-13 11:35:53 +08:00
|
|
|
#endif
|
2004-02-09 10:15:26 +08:00
|
|
|
}
|
|
|
|
#if defined(NCURSES_VERSION) || defined(HAVE_WCHGAT)
|
2002-10-13 11:35:53 +08:00
|
|
|
if (try_color)
|
|
|
|
wchgat(stdscr, -1, A_NORMAL, my_pair, NULL);
|
2004-02-09 10:15:26 +08:00
|
|
|
#endif
|
1998-03-01 12:21:12 +08:00
|
|
|
}
|
2000-07-09 10:46:08 +08:00
|
|
|
}
|
|
|
|
setscrreg(1, LINES - 1);
|
|
|
|
scrollok(stdscr, TRUE);
|
|
|
|
refresh();
|
1997-05-15 12:00:00 +08:00
|
|
|
}
|