2006-12-07 00:25:32 +01:00
|
|
|
/*
|
2006-12-07 00:25:37 +01:00
|
|
|
* Copyright (C) 1980 The Regents of the University of California.
|
2006-12-07 00:25:32 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that the above copyright notice and this paragraph are
|
|
|
|
* duplicated in all such forms and that any documentation,
|
|
|
|
* advertising materials, and other materials related to such
|
|
|
|
* distribution and use acknowledge that the software was developed
|
|
|
|
* by the University of California, Berkeley. The name of the
|
|
|
|
* University may not be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
/* more.c - General purpose tty output filter and file perusal program
|
|
|
|
*
|
|
|
|
* by Eric Shienbrood, UC Berkeley
|
|
|
|
*
|
|
|
|
* modified by Geoff Peck
|
|
|
|
* UCB to add underlining, single spacing
|
|
|
|
* modified by John Foderaro
|
|
|
|
* UCB to add -c and MORE environment variable
|
|
|
|
* modified by Erik Troan <ewt@redhat.com>
|
|
|
|
* to be more posix and so compile on linux/axp.
|
|
|
|
* modified by Kars de Jong <jongk@cs.utwente.nl>
|
|
|
|
* to use terminfo instead of termcap.
|
2013-01-26 17:07:51 +01:00
|
|
|
* 1999-02-22 Arkadiusz Miśkiewicz <misiek@pld.ORG.PL>
|
2012-10-08 08:08:17 +01:00
|
|
|
* added Native Language Support
|
|
|
|
* 1999-03-19 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
|
|
|
|
* more nls translatable strings
|
|
|
|
* 1999-05-09 aeb
|
|
|
|
* applied a RedHat patch (setjmp->sigsetjmp); without it a second
|
|
|
|
* ^Z would fail.
|
|
|
|
* 1999-05-09 aeb
|
|
|
|
* undone Kars' work, so that more works without libcurses (and
|
|
|
|
* hence can be in /bin with libcurses being in
|
|
|
|
* /usr/lib which may not be mounted). However, when termcap is not
|
|
|
|
* present curses can still be used.
|
|
|
|
* 2010-10-21 Davidlohr Bueso <dave@gnu.org>
|
|
|
|
* modified mem allocation handling for util-linux
|
|
|
|
*/
|
2006-12-07 00:25:32 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2006-12-07 00:25:34 +01:00
|
|
|
#include <string.h>
|
2006-12-07 00:25:32 +01:00
|
|
|
#include <unistd.h>
|
2018-05-26 20:08:34 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
2006-12-07 00:25:32 +01:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
2007-01-03 00:28:02 +01:00
|
|
|
#include <fcntl.h>
|
2006-12-07 00:25:32 +01:00
|
|
|
#include <termios.h>
|
|
|
|
#include <setjmp.h>
|
2006-12-07 00:25:34 +01:00
|
|
|
#include <sys/ioctl.h>
|
2006-12-07 00:25:32 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
2006-12-07 00:25:34 +01:00
|
|
|
#include <sys/wait.h>
|
2018-04-19 22:46:27 +01:00
|
|
|
#include <regex.h>
|
|
|
|
|
|
|
|
#if defined(HAVE_NCURSESW_TERM_H)
|
|
|
|
# include <ncursesw/term.h>
|
|
|
|
#elif defined(HAVE_NCURSES_TERM_H)
|
|
|
|
# include <ncurses/term.h>
|
|
|
|
#elif defined(HAVE_TERM_H)
|
|
|
|
# include <term.h>
|
|
|
|
#endif
|
2010-10-22 12:22:56 -03:00
|
|
|
|
2012-04-04 19:39:15 +02:00
|
|
|
#include "strutils.h"
|
2006-12-07 00:25:39 +01:00
|
|
|
#include "nls.h"
|
2010-10-22 12:22:56 -03:00
|
|
|
#include "xalloc.h"
|
2006-12-07 00:25:54 +01:00
|
|
|
#include "widechar.h"
|
2012-04-04 19:39:15 +02:00
|
|
|
#include "closestream.h"
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2013-01-24 19:27:12 +01:00
|
|
|
#ifdef TEST_PROGRAM
|
|
|
|
# define NON_INTERACTIVE_MORE 1
|
|
|
|
#endif
|
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
#define VI "vi" /* found on the user's path */
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
#define BS "\b"
|
|
|
|
#define BSB "\b \b"
|
|
|
|
#define CARAT "^"
|
2018-05-26 09:37:47 +01:00
|
|
|
#define RINGBELL '\007'
|
2018-04-19 22:46:27 +01:00
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
#define LINSIZ 256 /* minimal Line buffer size */
|
2006-12-07 00:25:32 +01:00
|
|
|
#define ctrl(letter) (letter & 077)
|
2012-10-08 08:08:17 +01:00
|
|
|
#define RUBOUT '\177'
|
|
|
|
#define ESC '\033'
|
|
|
|
#define QUIT '\034'
|
2011-02-12 15:02:51 +01:00
|
|
|
#define SCROLL_LEN 11
|
|
|
|
#define LINES_PER_PAGE 24
|
|
|
|
#define NUM_COLUMNS 80
|
|
|
|
#define TERMINAL_BUF 4096
|
|
|
|
#define INIT_BUF 80
|
|
|
|
#define SHELL_LINE 1000
|
|
|
|
#define COMMAND_BUF 200
|
2012-06-05 14:19:13 +02:00
|
|
|
#define REGERR_BUF NUM_COLUMNS
|
2018-04-19 22:46:27 +01:00
|
|
|
|
|
|
|
#define TERM_AUTO_RIGHT_MARGIN "am"
|
|
|
|
#define TERM_CEOL "xhp"
|
|
|
|
#define TERM_CLEAR "clear"
|
|
|
|
#define TERM_CLEAR_TO_LINE_END "el"
|
|
|
|
#define TERM_CLEAR_TO_SCREEN_END "ed"
|
|
|
|
#define TERM_COLS "cols"
|
|
|
|
#define TERM_CURSOR_ADDRESS "cup"
|
|
|
|
#define TERM_EAT_NEW_LINE "xenl"
|
|
|
|
#define TERM_ENTER_UNDERLINE "smul"
|
|
|
|
#define TERM_EXIT_STANDARD_MODE "rmso"
|
|
|
|
#define TERM_EXIT_UNDERLINE "rmul"
|
|
|
|
#define TERM_HARD_COPY "hc"
|
|
|
|
#define TERM_HOME "home"
|
|
|
|
#define TERM_LINE_DOWN "cud1"
|
|
|
|
#define TERM_LINES "lines"
|
|
|
|
#define TERM_OVER_STRIKE "os"
|
|
|
|
#define TERM_PAD_CHAR "pad"
|
|
|
|
#define TERM_STANDARD_MODE "smso"
|
|
|
|
#define TERM_STD_MODE_GLITCH "xmc"
|
|
|
|
#define TERM_UNDERLINE_CHAR "uc"
|
|
|
|
#define TERM_UNDERLINE "ul"
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
struct more_control {
|
|
|
|
struct termios otty; /* output terminal */
|
|
|
|
struct termios savetty0; /* original terminal settings */
|
|
|
|
long file_pos; /* file position */
|
|
|
|
long file_size; /* file size */
|
|
|
|
int fnum; /* argv[] position */
|
|
|
|
int dlines; /* screen size in lines */
|
|
|
|
int nscroll; /* number of lines scrolled by 'd' */
|
|
|
|
int promptlen; /* message prompt length */
|
|
|
|
int Currline; /* line we are currently at */
|
|
|
|
char **fnames; /* The list of file names */
|
|
|
|
int nfiles; /* Number of files left to process */
|
|
|
|
char *shell; /* name of the shell to use */
|
|
|
|
int shellp; /* does previous shell command exist */
|
|
|
|
sigjmp_buf restore; /* siglongjmp() destination */
|
|
|
|
char *Line; /* line buffer */
|
|
|
|
size_t LineLen; /* size of Line buffer */
|
|
|
|
int Lpp; /* lines per page */
|
|
|
|
char *Clear; /* clear screen */
|
|
|
|
char *eraseln; /* erase line */
|
|
|
|
char *Senter; /* enter standout mode */
|
|
|
|
char *Sexit; /* exit standout mode */
|
|
|
|
char *ULenter; /* enter underline mode */
|
|
|
|
char *ULexit; /* exit underline mode */
|
|
|
|
char *chUL; /* underline character */
|
|
|
|
char *chBS; /* backspace character */
|
|
|
|
char *Home; /* go to home */
|
|
|
|
char *cursorm; /* cursor movement */
|
|
|
|
char cursorhome[40]; /* contains cursor movement to home */
|
|
|
|
char *EodClr; /* clear rest of screen */
|
|
|
|
int Mcol; /* number of columns */
|
|
|
|
char *previousre; /* previous search() buf[] item */
|
|
|
|
struct {
|
|
|
|
long chrctr; /* row number */
|
|
|
|
long line; /* line number */
|
|
|
|
} context,
|
|
|
|
screen_start;
|
|
|
|
int lastcmd; /* previous more key command */
|
|
|
|
int lastarg; /* previous key command argument */
|
|
|
|
int lastcolon; /* is a colon-prefixed key command */
|
|
|
|
char shell_line[SHELL_LINE];
|
|
|
|
unsigned int
|
|
|
|
bad_so:1, /* true if overwriting does not turn off standout */
|
|
|
|
catch_susp:1, /* we should catch the SIGTSTP signal */
|
|
|
|
clreol:1, /* do not scroll, paint each screen from the top */
|
|
|
|
docrterase:1, /* is erase previous supported */
|
|
|
|
docrtkill:1, /* is erase input supported */
|
|
|
|
dumb:1, /* is terminal type known */
|
|
|
|
dum_opt:1, /* suppress bell */
|
|
|
|
eatnl:1, /* is newline ignored after 80 cols */
|
|
|
|
errors:1, /* is an error reported */
|
|
|
|
firstf:1, /* is the input file the first in list */
|
|
|
|
fold_opt:1, /* fold long lines */
|
|
|
|
hard:1, /* is this hard copy terminal (a printer or such) */
|
|
|
|
hardtabs:1, /* print spaces instead of '\t' */
|
|
|
|
inwait:1, /* is waiting user input */
|
|
|
|
lastp:1, /* run previous key command */
|
|
|
|
no_intty:1, /* is input in interactive mode */
|
|
|
|
noscroll:1, /* do not scroll, clear the screen and then display text */
|
|
|
|
notell:1, /* suppress quit dialog */
|
|
|
|
no_tty:1, /* is output in interactive mode */
|
|
|
|
Pause:1, /* is output paused */
|
|
|
|
pstate:1, /* current UL state */
|
|
|
|
soglitch:1, /* terminal has standout mode glitch */
|
|
|
|
ssp_opt:1, /* suppress white space */
|
|
|
|
startup:1, /* is startup completed */
|
|
|
|
stop_opt:1, /* stop after form feeds */
|
|
|
|
ulglitch:1, /* terminal has underline mode glitch */
|
|
|
|
ul_opt:1, /* underline as best we can */
|
|
|
|
within:1, /* true if we are within a file, false if we are between files */
|
|
|
|
Wrap:1; /* set if automargins */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* FIXME: global_ctl is used in signal handlers. */
|
|
|
|
struct more_control *global_ctl;
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2016-01-26 11:50:58 +01:00
|
|
|
static void putstring(char *s)
|
2012-10-08 08:08:17 +01:00
|
|
|
{
|
|
|
|
tputs(s, fileno(stdout), putchar); /* putp(s); */
|
2006-12-07 00:25:39 +01:00
|
|
|
}
|
|
|
|
|
2017-06-21 22:37:37 +02:00
|
|
|
static void __attribute__((__noreturn__)) usage(void)
|
2011-02-12 15:02:51 +01:00
|
|
|
{
|
2017-06-21 22:37:37 +02:00
|
|
|
FILE *out = stdout;
|
2014-02-09 11:26:49 +01:00
|
|
|
fputs(USAGE_HEADER, out);
|
|
|
|
fprintf(out, _(" %s [options] <file>...\n"), program_invocation_short_name);
|
2014-12-22 22:57:17 +01:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
|
|
|
fputs(_("A file perusal filter for CRT viewing.\n"), out);
|
|
|
|
|
2014-02-09 11:26:49 +01:00
|
|
|
fputs(USAGE_OPTIONS, out);
|
|
|
|
fputs(_(" -d display help instead of ringing bell\n"), out);
|
|
|
|
fputs(_(" -f count logical rather than screen lines\n"), out);
|
|
|
|
fputs(_(" -l suppress pause after form feed\n"), out);
|
|
|
|
fputs(_(" -c do not scroll, display text and clean line ends\n"), out);
|
|
|
|
fputs(_(" -p do not scroll, clean screen and display text\n"), out);
|
|
|
|
fputs(_(" -s squeeze multiple blank lines into one\n"), out);
|
|
|
|
fputs(_(" -u suppress underlining\n"), out);
|
|
|
|
fputs(_(" -<number> the number of lines per screenful\n"), out);
|
|
|
|
fputs(_(" +<number> display file beginning from line number\n"), out);
|
|
|
|
fputs(_(" +/<string> display file beginning from search string match\n"), out);
|
2017-06-21 22:37:37 +02:00
|
|
|
|
|
|
|
fputs(USAGE_SEPARATOR, out);
|
2017-06-25 14:49:47 +02:00
|
|
|
printf( " --help %s\n", USAGE_OPTSTR_HELP);
|
|
|
|
printf( " -V, --version %s\n", USAGE_OPTSTR_VERSION);
|
2017-06-29 15:52:16 +02:00
|
|
|
printf(USAGE_MAN_TAIL("more(1)"));
|
2017-06-21 22:37:37 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
2006-12-07 00:25:44 +01:00
|
|
|
}
|
|
|
|
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void argscan(struct more_control *ctl, char *s)
|
2012-10-08 08:08:17 +01:00
|
|
|
{
|
2006-12-07 00:25:32 +01:00
|
|
|
int seen_num = 0;
|
|
|
|
|
|
|
|
while (*s != '\0') {
|
|
|
|
switch (*s) {
|
2012-10-08 08:08:17 +01:00
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
2006-12-07 00:25:32 +01:00
|
|
|
if (!seen_num) {
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->dlines = 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
seen_num = 1;
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->dlines = ctl->dlines * 10 + *s - '0';
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
case 'd':
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->dum_opt = 1;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
case 'l':
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->stop_opt = 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
case 'f':
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->fold_opt = 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
case 'p':
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->noscroll = 1;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
case 'c':
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->clreol = 1;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
case 's':
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->ssp_opt = 1;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
case 'u':
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->ul_opt = 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
case '-':
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
break;
|
|
|
|
case 'V':
|
2013-01-23 13:48:50 +01:00
|
|
|
printf(UTIL_LINUX_VERSION);
|
2012-10-08 08:08:17 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2006-12-07 00:25:44 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
default:
|
|
|
|
warnx(_("unknown option -%s"), s);
|
2017-06-21 22:37:37 +02:00
|
|
|
errtryhelp(EXIT_FAILURE);
|
2006-12-07 00:25:44 +01:00
|
|
|
break;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-26 09:37:47 +01:00
|
|
|
static void more_fseek(struct more_control *ctl, FILE *stream, long pos)
|
|
|
|
{
|
|
|
|
ctl->file_pos = pos;
|
|
|
|
fseek(stream, pos, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int more_getc(struct more_control *ctl, FILE *stream)
|
|
|
|
{
|
|
|
|
ctl->file_pos++;
|
|
|
|
return getc(stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int more_ungetc(struct more_control *ctl, int c, FILE *stream)
|
|
|
|
{
|
|
|
|
ctl->file_pos--;
|
|
|
|
return ungetc(c, stream);
|
|
|
|
}
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
/* force clear to end of line */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void cleareol(struct more_control *ctl)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
putstring(ctl->eraseln);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 21:54:42 +01:00
|
|
|
/* magic --
|
|
|
|
* check for file magic numbers. This code would best be shared
|
|
|
|
* with the file(1) program or, perhaps, more should not try to be
|
|
|
|
* so smart. */
|
|
|
|
static int magic(FILE *f, char *fs)
|
|
|
|
{
|
|
|
|
signed char twobytes[2];
|
|
|
|
|
|
|
|
/* don't try to look ahead if the input is unseekable */
|
|
|
|
if (fseek(f, 0L, SEEK_SET))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (fread(twobytes, 2, 1, f) == 1) {
|
|
|
|
switch (twobytes[0] + (twobytes[1] << 8)) {
|
|
|
|
case 0407: /* a.out obj */
|
|
|
|
case 0410: /* a.out exec */
|
|
|
|
case 0413: /* a.out demand exec */
|
|
|
|
case 0405:
|
|
|
|
case 0411:
|
|
|
|
case 0177545:
|
|
|
|
case 0x457f: /* simple ELF detection */
|
|
|
|
printf(_("\n******** %s: Not a text file ********\n\n"),
|
|
|
|
fs);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fseek(f, 0L, SEEK_SET); /* rewind() not necessary */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
/* Check whether the file named by fs is an ASCII file which the user may
|
|
|
|
* access. If it is, return the opened file. Otherwise return NULL. */
|
2018-05-23 22:58:34 +01:00
|
|
|
static FILE *checkf(struct more_control *ctl, register char *fs, int *clearfirst)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
|
|
|
struct stat stbuf;
|
|
|
|
register FILE *f;
|
2006-12-07 00:25:49 +01:00
|
|
|
int c;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
if (stat(fs, &stbuf) == -1) {
|
2012-10-08 08:08:18 +01:00
|
|
|
fflush(stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2015-01-29 21:21:48 +01:00
|
|
|
warn(_("stat of %s failed"), fs);
|
2012-10-08 08:08:17 +01:00
|
|
|
return ((FILE *)NULL);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
|
2006-12-07 00:26:24 +01:00
|
|
|
printf(_("\n*** %s: directory ***\n\n"), fs);
|
2012-10-08 08:08:17 +01:00
|
|
|
return ((FILE *)NULL);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2018-05-26 09:37:47 +01:00
|
|
|
ctl->Currline = 0;
|
|
|
|
ctl->file_pos = 0;
|
|
|
|
if ((f = fopen(fs, "r")) == NULL) {
|
2012-10-08 08:08:18 +01:00
|
|
|
fflush(stdout);
|
2014-12-07 10:13:03 +00:00
|
|
|
warn(_("cannot open %s"), fs);
|
2012-10-08 08:08:17 +01:00
|
|
|
return ((FILE *)NULL);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2014-01-14 17:49:34 +01:00
|
|
|
if (magic(f, fs)) {
|
|
|
|
fclose(f);
|
2012-10-08 08:08:17 +01:00
|
|
|
return ((FILE *)NULL);
|
2014-01-14 17:49:34 +01:00
|
|
|
}
|
2012-10-08 08:08:17 +01:00
|
|
|
fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
|
2018-05-26 09:37:47 +01:00
|
|
|
c = more_getc(ctl, f);
|
2006-12-07 00:25:48 +01:00
|
|
|
*clearfirst = (c == '\f');
|
2018-05-26 09:37:47 +01:00
|
|
|
more_ungetc(ctl, c, f);
|
2018-05-23 22:58:34 +01:00
|
|
|
if ((ctl->file_size = stbuf.st_size) == 0)
|
|
|
|
ctl->file_size = LONG_MAX;
|
2012-10-08 08:08:17 +01:00
|
|
|
return (f);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void prepare_line_buffer(struct more_control *ctl)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
size_t nsz = ctl->Mcol * 4;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->LineLen >= nsz)
|
2018-04-19 22:46:26 +01:00
|
|
|
return;
|
2009-11-16 15:52:45 +01:00
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
if (nsz < LINSIZ)
|
|
|
|
nsz = LINSIZ;
|
2009-11-16 15:52:45 +01:00
|
|
|
|
2013-08-01 12:58:22 +02:00
|
|
|
/* alloc nsz and extra space for \n\0 */
|
2018-05-26 20:08:34 +01:00
|
|
|
ctl->Line = xrealloc(ctl->Line, nsz + 2);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->LineLen = nsz;
|
2009-11-16 15:52:45 +01:00
|
|
|
}
|
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
/* Get a logical line */
|
2018-05-23 22:58:34 +01:00
|
|
|
static int get_line(struct more_control *ctl, register FILE *f, int *length)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2012-10-08 08:08:17 +01:00
|
|
|
int c;
|
|
|
|
char *p;
|
|
|
|
int column;
|
|
|
|
static int colflg;
|
2006-12-07 00:25:54 +01:00
|
|
|
|
2007-01-30 13:49:50 +01:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2012-10-08 08:08:17 +01:00
|
|
|
size_t i;
|
|
|
|
wchar_t wc;
|
|
|
|
int wc_width;
|
|
|
|
mbstate_t state, state_bak; /* Current status of the stream. */
|
|
|
|
char mbc[MB_LEN_MAX]; /* Buffer for one multibyte char. */
|
|
|
|
size_t mblength; /* Byte length of multibyte char. */
|
|
|
|
size_t mbc_pos = 0; /* Position of the MBC. */
|
|
|
|
int use_mbc_buffer_flag = 0; /* If 1, mbc has data. */
|
|
|
|
int break_flag = 0; /* If 1, exit while(). */
|
2018-05-26 09:37:47 +01:00
|
|
|
long file_pos_bak = ctl->file_pos;
|
2012-10-08 08:08:17 +01:00
|
|
|
|
|
|
|
memset(&state, '\0', sizeof(mbstate_t));
|
|
|
|
#endif
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
prepare_line_buffer(ctl);
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
p = ctl->Line;
|
2012-10-08 08:08:17 +01:00
|
|
|
column = 0;
|
2018-05-26 09:37:47 +01:00
|
|
|
c = more_getc(ctl, f);
|
2012-10-08 08:08:17 +01:00
|
|
|
if (colflg && c == '\n') {
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline++;
|
2018-05-26 09:37:47 +01:00
|
|
|
c = more_getc(ctl, f);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
while (p < &ctl->Line[ctl->LineLen - 1]) {
|
2007-01-30 13:49:50 +01:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->fold_opt && use_mbc_buffer_flag && MB_CUR_MAX > 1) {
|
2012-10-08 08:08:17 +01:00
|
|
|
use_mbc_buffer_flag = 0;
|
|
|
|
state_bak = state;
|
|
|
|
mbc[mbc_pos++] = c;
|
|
|
|
process_mbc:
|
|
|
|
mblength = mbrtowc(&wc, mbc, mbc_pos, &state);
|
|
|
|
|
|
|
|
switch (mblength) {
|
|
|
|
case (size_t)-2: /* Incomplete multibyte character. */
|
|
|
|
use_mbc_buffer_flag = 1;
|
|
|
|
state = state_bak;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (size_t)-1: /* Invalid as a multibyte character. */
|
|
|
|
*p++ = mbc[0];
|
|
|
|
state = state_bak;
|
|
|
|
column++;
|
|
|
|
file_pos_bak++;
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (column >= ctl->Mcol) {
|
2018-05-26 09:37:47 +01:00
|
|
|
more_fseek(ctl, f, file_pos_bak);
|
2012-10-08 08:08:17 +01:00
|
|
|
} else {
|
|
|
|
memmove(mbc, mbc + 1, --mbc_pos);
|
|
|
|
if (mbc_pos > 0) {
|
|
|
|
mbc[mbc_pos] = '\0';
|
|
|
|
goto process_mbc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wc_width = wcwidth(wc);
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (column + wc_width > ctl->Mcol) {
|
2018-05-26 09:37:47 +01:00
|
|
|
more_fseek(ctl, f, file_pos_bak);
|
2012-10-08 08:08:17 +01:00
|
|
|
break_flag = 1;
|
|
|
|
} else {
|
2018-05-23 22:58:34 +01:00
|
|
|
for (i = 0; p < &ctl->Line[ctl->LineLen - 1] &&
|
2013-08-01 16:00:21 +02:00
|
|
|
i < mbc_pos; i++)
|
2012-10-08 08:08:17 +01:00
|
|
|
*p++ = mbc[i];
|
|
|
|
if (wc_width > 0)
|
|
|
|
column += wc_width;
|
|
|
|
}
|
|
|
|
}
|
2006-12-07 00:25:54 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (break_flag || column >= ctl->Mcol)
|
2012-10-08 08:08:17 +01:00
|
|
|
break;
|
2006-12-07 00:25:54 +01:00
|
|
|
|
2018-05-26 09:37:47 +01:00
|
|
|
c = more_getc(ctl, f);
|
2012-10-08 08:08:17 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_WIDECHAR */
|
|
|
|
if (c == EOF) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (p > ctl->Line) {
|
2012-10-08 08:08:17 +01:00
|
|
|
*p = '\0';
|
2018-05-23 22:58:34 +01:00
|
|
|
*length = p - ctl->Line;
|
2012-10-08 08:08:17 +01:00
|
|
|
return (column);
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
*length = p - ctl->Line;
|
2012-10-08 08:08:17 +01:00
|
|
|
return (EOF);
|
|
|
|
}
|
|
|
|
if (c == '\n') {
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline++;
|
2012-10-08 08:08:17 +01:00
|
|
|
break;
|
2006-12-07 00:25:54 +01:00
|
|
|
}
|
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
*p++ = c;
|
|
|
|
if (c == '\t') {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->hardtabs || (column < ctl->promptlen && !ctl->hard)) {
|
|
|
|
if (ctl->hardtabs && ctl->eraseln && !ctl->dumb) {
|
2012-10-08 08:08:17 +01:00
|
|
|
column = 1 + (column | 7);
|
2018-05-23 22:58:34 +01:00
|
|
|
putp(ctl->eraseln);
|
|
|
|
ctl->promptlen = 0;
|
2012-10-08 08:08:17 +01:00
|
|
|
} else {
|
2018-05-23 22:58:34 +01:00
|
|
|
for (--p; p < &ctl->Line[ctl->LineLen - 1];) {
|
2012-10-08 08:08:17 +01:00
|
|
|
*p++ = ' ';
|
|
|
|
if ((++column & 7) == 0)
|
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (column >= ctl->promptlen)
|
|
|
|
ctl->promptlen = 0;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
column = 1 + (column | 7);
|
|
|
|
} else if (c == '\b' && column > 0) {
|
|
|
|
column--;
|
|
|
|
} else if (c == '\r') {
|
2018-05-26 09:37:47 +01:00
|
|
|
int next = more_getc(ctl, f);
|
2012-10-08 08:08:17 +01:00
|
|
|
if (next == '\n') {
|
|
|
|
p--;
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline++;
|
2012-10-08 08:08:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-05-26 09:37:47 +01:00
|
|
|
more_ungetc(ctl, c, f);
|
2012-10-08 08:08:17 +01:00
|
|
|
column = 0;
|
2018-05-23 22:58:34 +01:00
|
|
|
} else if (c == '\f' && ctl->stop_opt) {
|
2012-10-08 08:08:17 +01:00
|
|
|
p[-1] = '^';
|
|
|
|
*p++ = 'L';
|
|
|
|
column += 2;
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Pause = 1;
|
2012-10-08 08:08:17 +01:00
|
|
|
} else if (c == EOF) {
|
2018-05-23 22:58:34 +01:00
|
|
|
*length = p - ctl->Line;
|
2012-10-08 08:08:17 +01:00
|
|
|
return (column);
|
|
|
|
} else {
|
|
|
|
#ifdef HAVE_WIDECHAR
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->fold_opt && MB_CUR_MAX > 1) {
|
2012-10-08 08:08:17 +01:00
|
|
|
memset(mbc, '\0', MB_LEN_MAX);
|
|
|
|
mbc_pos = 0;
|
|
|
|
mbc[mbc_pos++] = c;
|
|
|
|
state_bak = state;
|
|
|
|
|
|
|
|
mblength = mbrtowc(&wc, mbc, mbc_pos, &state);
|
|
|
|
/* The value of mblength is always less than 2 here. */
|
|
|
|
switch (mblength) {
|
|
|
|
case (size_t)-2:
|
|
|
|
p--;
|
2018-05-26 09:37:47 +01:00
|
|
|
file_pos_bak = ctl->file_pos - 1;
|
2012-10-08 08:08:17 +01:00
|
|
|
state = state_bak;
|
|
|
|
use_mbc_buffer_flag = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (size_t)-1:
|
|
|
|
state = state_bak;
|
|
|
|
column++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
wc_width = wcwidth(wc);
|
|
|
|
if (wc_width > 0)
|
|
|
|
column += wc_width;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif /* HAVE_WIDECHAR */
|
|
|
|
{
|
|
|
|
if (isprint(c))
|
|
|
|
column++;
|
|
|
|
}
|
2006-12-07 00:25:44 +01:00
|
|
|
}
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (column >= ctl->Mcol && ctl->fold_opt)
|
2012-10-08 08:08:17 +01:00
|
|
|
break;
|
2013-08-01 16:41:50 +02:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2018-05-23 22:58:34 +01:00
|
|
|
if (use_mbc_buffer_flag == 0 && p >= &ctl->Line[ctl->LineLen - 1 - 4])
|
2013-08-01 16:41:50 +02:00
|
|
|
/* don't read another char if there is no space for
|
|
|
|
* whole multibyte sequence */
|
|
|
|
break;
|
|
|
|
#endif
|
2018-05-26 09:37:47 +01:00
|
|
|
c = more_getc(ctl, f);
|
2006-12-07 00:25:44 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (column >= ctl->Mcol && ctl->Mcol > 0) {
|
|
|
|
if (!ctl->Wrap) {
|
2012-10-08 08:08:17 +01:00
|
|
|
*p++ = '\n';
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2006-12-07 00:25:54 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
colflg = column == ctl->Mcol && ctl->fold_opt;
|
|
|
|
if (colflg && ctl->eatnl && ctl->Wrap) {
|
2012-10-08 08:08:17 +01:00
|
|
|
*p++ = '\n'; /* simulate normal wrap */
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
*length = p - ctl->Line;
|
2012-10-08 08:08:17 +01:00
|
|
|
*p = 0;
|
|
|
|
return (column);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void clreos(struct more_control *ctl)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
putstring(ctl->EodClr);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
/* Erase the rest of the prompt, assuming we are starting at column col. */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void erasep(struct more_control *ctl, register int col)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->promptlen == 0)
|
2012-10-08 08:08:17 +01:00
|
|
|
return;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->hard) {
|
2012-10-08 08:08:17 +01:00
|
|
|
putchar('\n');
|
|
|
|
} else {
|
|
|
|
if (col == 0)
|
|
|
|
putchar('\r');
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->dumb && ctl->eraseln)
|
|
|
|
putstring(ctl->eraseln);
|
2012-10-08 08:08:17 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
printf("%*s", ctl->promptlen - col, "");
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen = 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2016-06-03 14:51:01 +02:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2014-12-19 14:34:03 +01:00
|
|
|
static UL_ASAN_BLACKLIST size_t xmbrtowc(wchar_t *wc, const char *s, size_t n,
|
2014-12-17 22:57:02 +00:00
|
|
|
mbstate_t *mbstate)
|
|
|
|
{
|
2014-12-19 14:34:03 +01:00
|
|
|
const size_t mblength = mbrtowc(wc, s, n, mbstate);
|
2014-12-17 22:57:02 +00:00
|
|
|
if (mblength == (size_t)-2 || mblength == (size_t)-1)
|
|
|
|
return 1;
|
|
|
|
return mblength;
|
|
|
|
}
|
2016-06-03 14:51:01 +02:00
|
|
|
#endif
|
2014-12-17 22:57:02 +00:00
|
|
|
|
2018-05-26 09:37:47 +01:00
|
|
|
static int wouldul(char *s, int n)
|
|
|
|
{
|
|
|
|
if (n < 2)
|
|
|
|
return 0;
|
|
|
|
if ((s[0] == '_' && s[1] == '\b') || (s[1] == '\b' && s[2] == '_'))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-07 00:25:32 +01:00
|
|
|
/* Print a buffer of n characters */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void prbuf(struct more_control *ctl, register char *s, register int n)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2012-10-08 08:08:17 +01:00
|
|
|
register char c; /* next output character */
|
|
|
|
register int state; /* next output char's UL state */
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
while (--n >= 0)
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->ul_opt)
|
2012-10-08 08:08:17 +01:00
|
|
|
putchar(*s++);
|
|
|
|
else {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (*s == ' ' && ctl->pstate == 0 && ctl->ulglitch
|
2012-10-08 08:08:17 +01:00
|
|
|
&& wouldul(s + 1, n - 1)) {
|
|
|
|
s++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((state = wouldul(s, n)) != 0) {
|
|
|
|
c = (*s == '_') ? s[2] : *s;
|
|
|
|
n -= 2;
|
|
|
|
s += 3;
|
|
|
|
} else
|
|
|
|
c = *s++;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (state != ctl->pstate) {
|
|
|
|
if (c == ' ' && state == 0 && ctl->ulglitch
|
2012-10-08 08:08:17 +01:00
|
|
|
&& wouldul(s, n - 1))
|
|
|
|
state = 1;
|
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
putp(state ? ctl->ULenter : ctl->ULexit);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (c != ' ' || ctl->pstate == 0 || state != 0
|
|
|
|
|| ctl->ulglitch == 0)
|
2008-04-16 22:47:37 +02:00
|
|
|
#ifdef HAVE_WIDECHAR
|
2012-10-08 08:08:17 +01:00
|
|
|
{
|
|
|
|
wchar_t wc;
|
|
|
|
size_t mblength;
|
|
|
|
mbstate_t mbstate;
|
|
|
|
memset(&mbstate, '\0', sizeof(mbstate_t));
|
|
|
|
s--;
|
|
|
|
n++;
|
2014-12-17 22:57:02 +00:00
|
|
|
mblength = xmbrtowc(&wc, s, n, &mbstate);
|
2012-10-08 08:08:17 +01:00
|
|
|
while (mblength--)
|
|
|
|
putchar(*s++);
|
|
|
|
n += mblength;
|
|
|
|
}
|
2007-08-14 11:51:19 +02:00
|
|
|
#else
|
2012-10-08 08:08:17 +01:00
|
|
|
putchar(c);
|
|
|
|
#endif /* HAVE_WIDECHAR */
|
2018-05-23 22:58:34 +01:00
|
|
|
if (state && *ctl->chUL) {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(ctl->chBS, stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
putstring(ctl->chUL);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->pstate = state;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Erase the current line entirely */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void kill_line(struct more_control *ctl)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
erasep(ctl, 0);
|
|
|
|
if (!ctl->eraseln || ctl->dumb)
|
2012-10-08 08:08:17 +01:00
|
|
|
putchar('\r');
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void prompt(struct more_control *ctl, char *filename)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
|
|
|
else if (ctl->promptlen > 0)
|
|
|
|
kill_line(ctl);
|
|
|
|
if (!ctl->hard) {
|
|
|
|
ctl->promptlen = 0;
|
|
|
|
if (ctl->Senter && ctl->Sexit) {
|
|
|
|
putstring(ctl->Senter);
|
|
|
|
ctl->promptlen += (2 * ctl->soglitch);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
|
|
|
ctl->promptlen += printf(_("--More--"));
|
2018-04-19 22:46:26 +01:00
|
|
|
if (filename != NULL) {
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen += printf(_("(Next file: %s)"), filename);
|
|
|
|
} else if (!ctl->no_intty) {
|
|
|
|
ctl->promptlen +=
|
2018-04-19 22:46:26 +01:00
|
|
|
printf("(%d%%)",
|
2018-05-23 22:58:34 +01:00
|
|
|
(int)((ctl->file_pos * 100) / ctl->file_size));
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->dum_opt) {
|
|
|
|
ctl->promptlen +=
|
2018-04-19 22:46:26 +01:00
|
|
|
printf(_("[Press space to continue, 'q' to quit.]"));
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->Senter && ctl->Sexit)
|
|
|
|
putstring(ctl->Sexit);
|
|
|
|
if (ctl->clreol)
|
|
|
|
clreos(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
fflush(stdout);
|
|
|
|
} else
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(RINGBELL, stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->inwait++;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 21:54:42 +01:00
|
|
|
static int ourputch(int c)
|
|
|
|
{
|
|
|
|
return putc(c, stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_tty(void)
|
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
if (global_ctl->no_tty)
|
2018-05-23 21:54:42 +01:00
|
|
|
return;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (global_ctl->pstate) {
|
|
|
|
tputs(global_ctl->ULexit, fileno(stdout), ourputch);
|
2018-05-23 21:54:42 +01:00
|
|
|
fflush(stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
global_ctl->pstate = 0;
|
2018-05-23 21:54:42 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
global_ctl->otty.c_lflag |= ICANON | ECHO;
|
|
|
|
global_ctl->otty.c_cc[VMIN] = global_ctl->savetty0.c_cc[VMIN];
|
|
|
|
global_ctl->otty.c_cc[VTIME] = global_ctl->savetty0.c_cc[VTIME];
|
2018-05-26 09:37:47 +01:00
|
|
|
tcsetattr(STDERR_FILENO, TCSANOW, &global_ctl->savetty0);
|
2018-05-23 21:54:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clean up terminal state and exit. Also come here if interrupt signal received */
|
|
|
|
static void __attribute__((__noreturn__)) end_it(int dummy __attribute__((__unused__)))
|
|
|
|
{
|
|
|
|
/* May be executed as a signal handler as well as by main process.
|
|
|
|
*
|
|
|
|
* The _exit() may wait for pending I/O for really long time, be sure
|
|
|
|
* that signal handler is not executed in this time to avoid double
|
|
|
|
* de-initialization (free() calls, etc.).
|
|
|
|
*/
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
|
|
|
|
reset_tty();
|
2018-05-23 22:58:34 +01:00
|
|
|
if (global_ctl->clreol) {
|
2018-05-23 21:54:42 +01:00
|
|
|
putchar('\r');
|
2018-05-23 22:58:34 +01:00
|
|
|
clreos(global_ctl);
|
2018-05-23 21:54:42 +01:00
|
|
|
fflush(stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
} else if (!global_ctl->clreol && (global_ctl->promptlen > 0)) {
|
|
|
|
kill_line(global_ctl);
|
2018-05-23 21:54:42 +01:00
|
|
|
fflush(stdout);
|
|
|
|
} else
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc('\n', stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
free(global_ctl->previousre);
|
|
|
|
free(global_ctl->Line);
|
2018-05-23 21:54:42 +01:00
|
|
|
_exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static int readch(struct more_control *ctl)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2018-04-19 22:46:26 +01:00
|
|
|
unsigned char c;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
if (read(fileno(stderr), &c, 1) <= 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
end_it(0);
|
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
c = ctl->otty.c_cc[VKILL];
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
return (c);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Read a decimal number from the terminal. Set cmd to the non-digit
|
|
|
|
* which terminates the number. */
|
2018-05-23 22:58:34 +01:00
|
|
|
static int number(struct more_control *ctl, char *cmd)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2018-04-19 22:46:26 +01:00
|
|
|
register int i;
|
2018-05-26 20:08:34 +01:00
|
|
|
char ch;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
i = 0;
|
2018-05-26 20:08:34 +01:00
|
|
|
ch = ctl->otty.c_cc[VKILL];
|
2012-10-08 08:08:17 +01:00
|
|
|
for (;;) {
|
2018-05-26 20:08:34 +01:00
|
|
|
ch = readch(ctl);
|
|
|
|
if (isdigit(ch))
|
|
|
|
i = i * 10 + ch - '0';
|
|
|
|
else if ((cc_t) ch == ctl->otty.c_cc[VKILL])
|
2018-04-19 22:46:26 +01:00
|
|
|
i = 0;
|
|
|
|
else {
|
2018-05-26 20:08:34 +01:00
|
|
|
*cmd = ch;
|
2018-04-19 22:46:26 +01:00
|
|
|
break;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
return (i);
|
|
|
|
}
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Skip nskip files in the file list (from the command line). Nskip may
|
|
|
|
* be negative. */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void skipf(struct more_control *ctl, register int nskip)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
|
|
|
if (nskip == 0)
|
|
|
|
return;
|
|
|
|
if (nskip > 0) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->fnum + nskip > ctl->nfiles - 1)
|
|
|
|
nskip = ctl->nfiles - ctl->fnum - 1;
|
|
|
|
} else if (ctl->within)
|
|
|
|
ctl->fnum++;
|
|
|
|
ctl->fnum += nskip;
|
|
|
|
if (ctl->fnum < 0)
|
|
|
|
ctl->fnum = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
puts(_("\n...Skipping "));
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
if (nskip > 0)
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_("...Skipping to file "), stdout);
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_("...Skipping back to file "), stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
puts(ctl->fnames[ctl->fnum]);
|
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\n');
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->fnum--;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void show(struct more_control *ctl, char c)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
|
|
|
if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
|
|
|
|
c += (c == RUBOUT) ? -0100 : 0100;
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(CARAT, stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen++;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(c, stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen++;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void more_error(struct more_control *ctl, char *mess)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
kill_line(ctl);
|
|
|
|
ctl->promptlen += strlen(mess);
|
|
|
|
if (ctl->Senter && ctl->Sexit) {
|
|
|
|
putp(ctl->Senter);
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(mess, stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
putp(ctl->Sexit);
|
2018-04-19 22:46:26 +01:00
|
|
|
} else
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(mess, stdout);
|
2018-04-19 22:46:26 +01:00
|
|
|
fflush(stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->errors++;
|
|
|
|
siglongjmp(ctl->restore, 1);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-26 09:37:47 +01:00
|
|
|
static void erase_one_column(struct more_control *ctl)
|
|
|
|
{
|
|
|
|
if (ctl->docrterase)
|
|
|
|
fputs(BSB, stderr);
|
|
|
|
else
|
|
|
|
fputs(BS, stderr);
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void ttyin(struct more_control *ctl, char buf[], register int nmax, char pchar)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
|
|
|
char *sp;
|
|
|
|
int c;
|
|
|
|
int slash = 0;
|
|
|
|
int maxlen;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
sp = buf;
|
|
|
|
maxlen = 0;
|
|
|
|
while (sp - buf < nmax) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->promptlen > maxlen)
|
|
|
|
maxlen = ctl->promptlen;
|
|
|
|
c = readch(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
if (c == '\\') {
|
|
|
|
slash++;
|
2018-05-23 22:58:34 +01:00
|
|
|
} else if (((cc_t) c == ctl->otty.c_cc[VERASE]) && !slash) {
|
2018-04-19 22:46:26 +01:00
|
|
|
if (sp > buf) {
|
|
|
|
#ifdef HAVE_WIDECHAR
|
|
|
|
if (MB_CUR_MAX > 1) {
|
|
|
|
wchar_t wc;
|
|
|
|
size_t pos = 0, mblength;
|
|
|
|
mbstate_t state, state_bak;
|
|
|
|
|
|
|
|
memset(&state, '\0', sizeof(mbstate_t));
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
state_bak = state;
|
|
|
|
mblength =
|
|
|
|
mbrtowc(&wc, buf + pos,
|
|
|
|
sp - buf, &state);
|
|
|
|
|
|
|
|
state = (mblength == (size_t)-2
|
|
|
|
|| mblength ==
|
|
|
|
(size_t)-1) ? state_bak
|
|
|
|
: state;
|
|
|
|
mblength =
|
|
|
|
(mblength == (size_t)-2
|
|
|
|
|| mblength == (size_t)-1
|
|
|
|
|| mblength ==
|
|
|
|
0) ? 1 : mblength;
|
|
|
|
|
|
|
|
if (buf + pos + mblength >= sp)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pos += mblength;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
|
|
|
|
if (mblength == 1) {
|
2018-05-26 09:37:47 +01:00
|
|
|
erase_one_column(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
} else {
|
|
|
|
int wc_width;
|
|
|
|
wc_width = wcwidth(wc);
|
|
|
|
wc_width =
|
|
|
|
(wc_width <
|
|
|
|
1) ? 1 : wc_width;
|
|
|
|
while (wc_width--) {
|
2018-05-26 09:37:47 +01:00
|
|
|
erase_one_column(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (mblength--) {
|
2018-05-23 22:58:34 +01:00
|
|
|
--ctl->promptlen;
|
2018-04-19 22:46:26 +01:00
|
|
|
--sp;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif /* HAVE_WIDECHAR */
|
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
--ctl->promptlen;
|
2018-05-26 09:37:47 +01:00
|
|
|
erase_one_column(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
--sp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*sp < ' ' && *sp != '\n') || *sp == RUBOUT) {
|
2018-05-23 22:58:34 +01:00
|
|
|
--ctl->promptlen;
|
2018-05-26 09:37:47 +01:00
|
|
|
erase_one_column(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
continue;
|
2012-10-08 08:08:17 +01:00
|
|
|
} else {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->eraseln)
|
|
|
|
ctl->promptlen = maxlen;
|
|
|
|
siglongjmp(ctl->restore, 1);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
} else if (((cc_t) c == ctl->otty.c_cc[VKILL]) && !slash) {
|
|
|
|
if (ctl->hard) {
|
|
|
|
show(ctl, c);
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\n');
|
|
|
|
putchar(pchar);
|
2012-10-08 08:08:17 +01:00
|
|
|
} else {
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\r');
|
|
|
|
putchar(pchar);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->eraseln)
|
|
|
|
erasep(ctl, 1);
|
|
|
|
else if (ctl->docrtkill)
|
|
|
|
while (ctl->promptlen-- > 1)
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(BSB, stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen = 1;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
sp = buf;
|
2012-10-08 08:08:17 +01:00
|
|
|
fflush(stdout);
|
2018-04-19 22:46:26 +01:00
|
|
|
continue;
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (slash && ((cc_t) c == ctl->otty.c_cc[VKILL]
|
|
|
|
|| (cc_t) c == ctl->otty.c_cc[VERASE])) {
|
2018-05-26 09:37:47 +01:00
|
|
|
erase_one_column(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
--sp;
|
|
|
|
}
|
|
|
|
if (c != '\\')
|
|
|
|
slash = 0;
|
|
|
|
*sp++ = c;
|
|
|
|
if ((c < ' ' && c != '\n' && c != ESC) || c == RUBOUT) {
|
|
|
|
c += (c == RUBOUT) ? -0100 : 0100;
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(CARAT, stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen++;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
if (c != '\n' && c != ESC) {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(c, stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen++;
|
2018-04-19 22:46:26 +01:00
|
|
|
} else
|
2012-10-08 08:08:17 +01:00
|
|
|
break;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
*--sp = '\0';
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->eraseln)
|
|
|
|
ctl->promptlen = maxlen;
|
2018-04-19 22:46:26 +01:00
|
|
|
if (sp - buf >= nmax - 1)
|
2018-05-23 22:58:34 +01:00
|
|
|
more_error(ctl, _("Line too long"));
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static int expand(struct more_control *ctl, char **outbuf, char *inbuf)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
|
|
|
char *inpstr;
|
|
|
|
char *outstr;
|
|
|
|
char c;
|
|
|
|
char *temp;
|
|
|
|
int changed = 0;
|
|
|
|
int tempsz, xtra, offset;
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
xtra = strlen(ctl->fnames[ctl->fnum]) + strlen(ctl->shell_line) + 1;
|
2018-04-19 22:46:26 +01:00
|
|
|
tempsz = 200 + xtra;
|
|
|
|
temp = xmalloc(tempsz);
|
|
|
|
inpstr = inbuf;
|
|
|
|
outstr = temp;
|
|
|
|
while ((c = *inpstr++) != '\0') {
|
|
|
|
offset = outstr - temp;
|
|
|
|
if (tempsz - offset - 1 < xtra) {
|
|
|
|
tempsz += 200 + xtra;
|
|
|
|
temp = xrealloc(temp, tempsz);
|
|
|
|
outstr = temp + offset;
|
|
|
|
}
|
|
|
|
switch (c) {
|
|
|
|
case '%':
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->no_intty) {
|
|
|
|
strcpy(outstr, ctl->fnames[ctl->fnum]);
|
|
|
|
outstr += strlen(ctl->fnames[ctl->fnum]);
|
2018-04-19 22:46:26 +01:00
|
|
|
changed++;
|
|
|
|
} else
|
|
|
|
*outstr++ = c;
|
|
|
|
break;
|
|
|
|
case '!':
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->shellp)
|
|
|
|
more_error(ctl, _
|
2018-04-19 22:46:26 +01:00
|
|
|
("No previous command to substitute for"));
|
2018-05-23 22:58:34 +01:00
|
|
|
strcpy(outstr, ctl->shell_line);
|
|
|
|
outstr += strlen(ctl->shell_line);
|
2018-04-19 22:46:26 +01:00
|
|
|
changed++;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if (*inpstr == '%' || *inpstr == '!') {
|
|
|
|
*outstr++ = *inpstr++;
|
2012-10-08 08:08:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2017-05-16 09:18:24 +01:00
|
|
|
/* fallthrough */
|
2012-10-08 08:08:17 +01:00
|
|
|
default:
|
2018-04-19 22:46:26 +01:00
|
|
|
*outstr++ = c;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
*outstr++ = '\0';
|
|
|
|
*outbuf = temp;
|
|
|
|
return (changed);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void set_tty(struct more_control *ctl)
|
2012-10-08 08:08:17 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->otty.c_lflag &= ~(ICANON | ECHO);
|
|
|
|
ctl->otty.c_cc[VMIN] = 1; /* read at least 1 char */
|
|
|
|
ctl->otty.c_cc[VTIME] = 0; /* no timeout */
|
2018-05-26 09:37:47 +01:00
|
|
|
tcsetattr(STDERR_FILENO, TCSANOW, &ctl->otty);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 21:54:42 +01:00
|
|
|
/* Come here if a quit signal is received */
|
|
|
|
static void onquit(int dummy __attribute__((__unused__)))
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
2018-05-23 21:54:42 +01:00
|
|
|
signal(SIGQUIT, SIG_IGN);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!global_ctl->inwait) {
|
2018-05-23 21:54:42 +01:00
|
|
|
putchar('\n');
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!global_ctl->startup) {
|
2018-05-23 21:54:42 +01:00
|
|
|
signal(SIGQUIT, onquit);
|
2018-05-23 22:58:34 +01:00
|
|
|
siglongjmp(global_ctl->restore, 1);
|
2018-05-23 21:54:42 +01:00
|
|
|
} else
|
2018-05-23 22:58:34 +01:00
|
|
|
global_ctl->Pause = 1;
|
|
|
|
} else if (!global_ctl->dum_opt && global_ctl->notell) {
|
|
|
|
global_ctl->promptlen += fprintf(stderr, _("[Use q or Q to quit]"));
|
|
|
|
global_ctl->notell = 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2018-05-23 21:54:42 +01:00
|
|
|
signal(SIGQUIT, onquit);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 21:54:42 +01:00
|
|
|
/* Come here when we get a suspend signal from the terminal */
|
|
|
|
static void onsusp(int dummy __attribute__((__unused__)))
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2018-05-23 21:54:42 +01:00
|
|
|
sigset_t signals, oldmask;
|
|
|
|
|
|
|
|
/* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
|
|
|
|
signal(SIGTTOU, SIG_IGN);
|
|
|
|
reset_tty();
|
|
|
|
fflush(stdout);
|
|
|
|
signal(SIGTTOU, SIG_DFL);
|
|
|
|
/* Send the TSTP signal to suspend our process group */
|
|
|
|
signal(SIGTSTP, SIG_DFL);
|
|
|
|
|
|
|
|
/* unblock SIGTSTP or we won't be able to suspend ourself */
|
|
|
|
sigemptyset(&signals);
|
|
|
|
sigaddset(&signals, SIGTSTP);
|
|
|
|
sigprocmask(SIG_UNBLOCK, &signals, &oldmask);
|
|
|
|
|
|
|
|
kill(0, SIGTSTP);
|
|
|
|
/* Pause for station break */
|
|
|
|
|
|
|
|
sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
|
|
|
|
|
|
|
/* We're back */
|
|
|
|
signal(SIGTSTP, onsusp);
|
2018-05-23 22:58:34 +01:00
|
|
|
set_tty(global_ctl);
|
|
|
|
if (global_ctl->inwait)
|
|
|
|
siglongjmp(global_ctl->restore, 1);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void execute(struct more_control *ctl, char *filename, char *cmd, ...)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
|
|
|
int id;
|
|
|
|
int n;
|
|
|
|
va_list argp;
|
|
|
|
char *arg;
|
|
|
|
char **args;
|
|
|
|
int argcount;
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
reset_tty();
|
|
|
|
for (n = 10; (id = fork()) < 0 && n > 0; n--)
|
|
|
|
sleep(5);
|
|
|
|
if (id == 0) {
|
|
|
|
int errsv;
|
2018-05-26 20:08:34 +01:00
|
|
|
if (!isatty(STDIN_FILENO)) {
|
|
|
|
close(STDIN_FILENO);
|
2018-04-19 22:46:26 +01:00
|
|
|
open("/dev/tty", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(argp, cmd);
|
|
|
|
arg = va_arg(argp, char *);
|
|
|
|
argcount = 0;
|
|
|
|
while (arg) {
|
|
|
|
argcount++;
|
|
|
|
arg = va_arg(argp, char *);
|
|
|
|
}
|
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
args = alloca(sizeof(char *) * (argcount + 1));
|
|
|
|
args[argcount] = NULL;
|
|
|
|
|
|
|
|
va_start(argp, cmd);
|
|
|
|
arg = va_arg(argp, char *);
|
|
|
|
argcount = 0;
|
|
|
|
while (arg) {
|
|
|
|
args[argcount] = arg;
|
|
|
|
argcount++;
|
|
|
|
arg = va_arg(argp, char *);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
execvp(cmd, args);
|
|
|
|
errsv = errno;
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_("exec failed\n"), stderr);
|
2018-04-19 22:46:26 +01:00
|
|
|
exit(errsv == ENOENT ? EX_EXEC_ENOENT : EX_EXEC_FAILED);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
if (id > 0) {
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->catch_susp)
|
2018-04-19 22:46:26 +01:00
|
|
|
signal(SIGTSTP, SIG_DFL);
|
2018-05-26 20:08:34 +01:00
|
|
|
while (wait(NULL) > 0)
|
|
|
|
/* nothing */ ;
|
2018-04-19 22:46:26 +01:00
|
|
|
signal(SIGINT, end_it);
|
|
|
|
signal(SIGQUIT, onquit);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->catch_susp)
|
2018-04-19 22:46:26 +01:00
|
|
|
signal(SIGTSTP, onsusp);
|
|
|
|
} else
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_("can't fork\n"), stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
set_tty(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
puts("------------------------");
|
2018-05-23 22:58:34 +01:00
|
|
|
prompt(ctl, filename);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void do_shell(struct more_control *ctl, char *filename)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2011-02-12 15:02:51 +01:00
|
|
|
char cmdbuf[COMMAND_BUF];
|
2006-12-07 00:25:39 +01:00
|
|
|
int rc;
|
|
|
|
char *expanded;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
kill_line(ctl);
|
2006-12-07 00:26:24 +01:00
|
|
|
putchar('!');
|
2012-10-08 08:08:17 +01:00
|
|
|
fflush(stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen = 1;
|
|
|
|
if (ctl->lastp)
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(ctl->shell_line, stdout);
|
2006-12-07 00:25:32 +01:00
|
|
|
else {
|
2018-05-23 22:58:34 +01:00
|
|
|
ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '!');
|
2017-02-11 20:23:26 +00:00
|
|
|
expanded = NULL;
|
2018-05-23 22:58:34 +01:00
|
|
|
rc = expand(ctl, &expanded, cmdbuf);
|
2006-12-07 00:25:39 +01:00
|
|
|
if (expanded) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (strlen(expanded) < sizeof(ctl->shell_line))
|
|
|
|
strcpy(ctl->shell_line, expanded);
|
2006-12-07 00:25:39 +01:00
|
|
|
else
|
|
|
|
rc = -1;
|
|
|
|
free(expanded);
|
|
|
|
}
|
|
|
|
if (rc < 0) {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_(" Overflow\n"), stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
prompt(ctl, filename);
|
2006-12-07 00:25:39 +01:00
|
|
|
return;
|
|
|
|
} else if (rc > 0) {
|
2018-05-23 22:58:34 +01:00
|
|
|
kill_line(ctl);
|
|
|
|
ctl->promptlen = printf("!%s", ctl->shell_line);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-08 08:08:17 +01:00
|
|
|
fflush(stdout);
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc('\n', stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen = 0;
|
|
|
|
ctl->shellp = 1;
|
|
|
|
execute(ctl, filename, ctl->shell, ctl->shell, "-c", ctl->shell_line, 0);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Execute a colon-prefixed command. Returns <0 if not a command that
|
|
|
|
* should cause more of the file to be printed. */
|
2018-05-23 22:58:34 +01:00
|
|
|
static int colon(struct more_control *ctl, char *filename, int cmd, int nlines)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
2018-05-26 20:08:34 +01:00
|
|
|
char ch;
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
if (cmd == 0)
|
2018-05-26 20:08:34 +01:00
|
|
|
ch = readch(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-26 20:08:34 +01:00
|
|
|
ch = cmd;
|
|
|
|
ctl->lastcolon = ch;
|
|
|
|
switch (ch) {
|
2018-04-19 22:46:26 +01:00
|
|
|
case 'f':
|
2018-05-23 22:58:34 +01:00
|
|
|
kill_line(ctl);
|
|
|
|
if (!ctl->no_intty)
|
|
|
|
ctl->promptlen =
|
|
|
|
printf(_("\"%s\" line %d"), ctl->fnames[ctl->fnum], ctl->Currline);
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen = printf(_("[Not a file] line %d"), ctl->Currline);
|
2018-04-19 22:46:26 +01:00
|
|
|
fflush(stdout);
|
|
|
|
return (-1);
|
|
|
|
case 'n':
|
|
|
|
if (nlines == 0) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->fnum >= ctl->nfiles - 1)
|
2018-04-19 22:46:26 +01:00
|
|
|
end_it(0);
|
|
|
|
nlines++;
|
|
|
|
}
|
|
|
|
putchar('\r');
|
2018-05-23 22:58:34 +01:00
|
|
|
erasep(ctl, 0);
|
|
|
|
skipf(ctl, nlines);
|
2018-04-19 22:46:26 +01:00
|
|
|
return (0);
|
|
|
|
case 'p':
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->no_intty) {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(RINGBELL, stderr);
|
2018-04-19 22:46:26 +01:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
putchar('\r');
|
2018-05-23 22:58:34 +01:00
|
|
|
erasep(ctl, 0);
|
2018-04-19 22:46:26 +01:00
|
|
|
if (nlines == 0)
|
|
|
|
nlines++;
|
2018-05-23 22:58:34 +01:00
|
|
|
skipf(ctl, -nlines);
|
2018-04-19 22:46:26 +01:00
|
|
|
return (0);
|
|
|
|
case '!':
|
2018-05-23 22:58:34 +01:00
|
|
|
do_shell(ctl, filename);
|
2018-04-19 22:46:26 +01:00
|
|
|
return (-1);
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
end_it(0);
|
|
|
|
default:
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(RINGBELL, stderr);
|
2018-04-19 22:46:26 +01:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip n lines in the file f */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void skiplns(struct more_control *ctl, register int n, register FILE *f)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
|
|
|
register int c;
|
|
|
|
|
|
|
|
while (n > 0) {
|
2018-05-26 09:37:47 +01:00
|
|
|
while ((c = more_getc(ctl, f)) != '\n')
|
2018-04-19 22:46:26 +01:00
|
|
|
if (c == EOF)
|
|
|
|
return;
|
|
|
|
n--;
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline++;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the screen */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void doclear(struct more_control *ctl)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->Clear && !ctl->hard) {
|
|
|
|
putp(ctl->Clear);
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Put out carriage return so that system doesn't get
|
|
|
|
* confused by escape sequences when expanding tabs */
|
|
|
|
putchar('\r');
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void rdline(struct more_control *ctl, register FILE *f)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
|
|
|
register int c;
|
|
|
|
register char *p;
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
prepare_line_buffer(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
p = ctl->Line;
|
2018-05-26 09:37:47 +01:00
|
|
|
while ((c = more_getc(ctl, f)) != '\n' && c != EOF
|
2018-05-23 22:58:34 +01:00
|
|
|
&& (size_t)(p - ctl->Line) < ctl->LineLen - 1)
|
2018-04-19 22:46:26 +01:00
|
|
|
*p++ = c;
|
|
|
|
if (c == '\n')
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline++;
|
2018-04-19 22:46:26 +01:00
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Go to home position */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void home(struct more_control *ctl)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
2018-05-23 22:58:34 +01:00
|
|
|
putp(ctl->Home);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2012-10-08 08:08:17 +01:00
|
|
|
/* Search for nth occurrence of regular expression contained in buf in
|
|
|
|
* the file */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void search(struct more_control *ctl, char buf[], FILE *file, register int n)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2018-05-26 09:37:47 +01:00
|
|
|
long startline = ctl->file_pos;
|
2012-10-08 08:08:17 +01:00
|
|
|
register long line1 = startline;
|
|
|
|
register long line2 = startline;
|
2013-07-07 20:54:31 +01:00
|
|
|
register long line3;
|
2012-10-08 08:08:17 +01:00
|
|
|
register int lncount;
|
2012-12-16 10:43:55 +00:00
|
|
|
int saveln, rc;
|
2012-10-08 08:08:17 +01:00
|
|
|
regex_t re;
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->context.line = saveln = ctl->Currline;
|
|
|
|
ctl->context.chrctr = startline;
|
2012-10-08 08:08:17 +01:00
|
|
|
lncount = 0;
|
2015-01-27 22:28:57 +00:00
|
|
|
if (!buf)
|
|
|
|
goto notfound;
|
2012-10-08 08:08:17 +01:00
|
|
|
if ((rc = regcomp(&re, buf, REG_NOSUB)) != 0) {
|
|
|
|
char s[REGERR_BUF];
|
|
|
|
regerror(rc, &re, s, sizeof s);
|
2018-05-23 22:58:34 +01:00
|
|
|
more_error(ctl, s);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
|
|
|
while (!feof(file)) {
|
|
|
|
line3 = line2;
|
|
|
|
line2 = line1;
|
2018-05-26 09:37:47 +01:00
|
|
|
line1 = ctl->file_pos;
|
2018-05-23 22:58:34 +01:00
|
|
|
rdline(ctl, file);
|
2012-10-08 08:08:17 +01:00
|
|
|
lncount++;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (regexec(&re, ctl->Line, 0, NULL, 0) == 0) {
|
2012-10-08 08:08:17 +01:00
|
|
|
if (--n == 0) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (lncount > 3 || (lncount > 1 && ctl->no_intty)) {
|
2012-10-08 08:08:17 +01:00
|
|
|
putchar('\n');
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_("...skipping\n"), stdout);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->no_intty) {
|
|
|
|
ctl->Currline -=
|
2012-10-08 08:08:17 +01:00
|
|
|
(lncount >= 3 ? 3 : lncount);
|
2018-05-26 09:37:47 +01:00
|
|
|
more_fseek(ctl, file, line3);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->noscroll) {
|
|
|
|
if (ctl->clreol) {
|
|
|
|
home(ctl);
|
|
|
|
cleareol(ctl);
|
2012-10-08 08:08:17 +01:00
|
|
|
} else
|
2018-05-23 22:58:34 +01:00
|
|
|
doclear(ctl);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-23 22:58:34 +01:00
|
|
|
kill_line(ctl);
|
|
|
|
if (ctl->noscroll) {
|
|
|
|
if (ctl->clreol) {
|
|
|
|
home(ctl);
|
|
|
|
cleareol(ctl);
|
2012-10-08 08:08:17 +01:00
|
|
|
} else
|
2018-05-23 22:58:34 +01:00
|
|
|
doclear(ctl);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
puts(ctl->Line);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
|
|
|
break;
|
2006-12-07 00:25:37 +01:00
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2012-05-23 17:51:15 -04:00
|
|
|
}
|
2012-10-08 08:08:19 +01:00
|
|
|
regfree(&re);
|
2012-10-08 08:08:17 +01:00
|
|
|
if (feof(file)) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->no_intty) {
|
|
|
|
ctl->Currline = saveln;
|
2018-05-26 09:37:47 +01:00
|
|
|
more_fseek(ctl, file, startline);
|
2012-10-08 08:08:17 +01:00
|
|
|
} else {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_("\nPattern not found\n"), stdout);
|
2012-10-08 08:08:17 +01:00
|
|
|
end_it(0);
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
free(ctl->previousre);
|
|
|
|
ctl->previousre = NULL;
|
2015-01-27 22:28:57 +00:00
|
|
|
notfound:
|
2018-05-23 22:58:34 +01:00
|
|
|
more_error(ctl, _("Pattern not found"));
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Read a command and do it. A command consists of an optional integer
|
|
|
|
* argument followed by the command character. Return the number of
|
|
|
|
* lines to display in the next screenful. If there is nothing more to
|
|
|
|
* display in the current file, zero is returned. */
|
2018-05-23 22:58:34 +01:00
|
|
|
static int command(struct more_control *ctl, char *filename, register FILE *f)
|
2006-12-07 00:25:32 +01:00
|
|
|
{
|
2018-04-19 22:46:26 +01:00
|
|
|
register int nlines;
|
|
|
|
register int retval = 0;
|
|
|
|
register int c;
|
|
|
|
char colonch;
|
|
|
|
int done;
|
|
|
|
char comchar, cmdbuf[INIT_BUF];
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
done = 0;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->errors)
|
|
|
|
prompt(ctl, filename);
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->errors = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
for (;;) {
|
2018-05-23 22:58:34 +01:00
|
|
|
nlines = number(ctl, &comchar);
|
|
|
|
ctl->lastp = colonch = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
if (comchar == '.') { /* Repeat last command */
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->lastp++;
|
|
|
|
comchar = ctl->lastcmd;
|
|
|
|
nlines = ctl->lastarg;
|
|
|
|
if (ctl->lastcmd == ':')
|
|
|
|
colonch = ctl->lastcolon;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->lastcmd = comchar;
|
|
|
|
ctl->lastarg = nlines;
|
|
|
|
if ((cc_t) comchar == ctl->otty.c_cc[VERASE]) {
|
|
|
|
kill_line(ctl);
|
|
|
|
prompt(ctl, filename);
|
2018-04-19 22:46:26 +01:00
|
|
|
continue;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
switch (comchar) {
|
|
|
|
case ':':
|
2018-05-23 22:58:34 +01:00
|
|
|
retval = colon(ctl, filename, colonch, nlines);
|
2018-04-19 22:46:26 +01:00
|
|
|
if (retval >= 0)
|
|
|
|
done++;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
case ctrl('B'):
|
|
|
|
{
|
|
|
|
register int initline;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->no_intty) {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(RINGBELL, stderr);
|
2018-04-19 22:46:26 +01:00
|
|
|
return (-1);
|
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
if (nlines == 0)
|
|
|
|
nlines++;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\r');
|
2018-05-23 22:58:34 +01:00
|
|
|
erasep(ctl, 0);
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\n');
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
printf(P_("...back %d page",
|
|
|
|
"...back %d pages", nlines),
|
|
|
|
nlines);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\n');
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
initline = ctl->Currline - ctl->dlines * (nlines + 1);
|
|
|
|
if (!ctl->noscroll)
|
2018-04-19 22:46:26 +01:00
|
|
|
--initline;
|
|
|
|
if (initline < 0)
|
|
|
|
initline = 0;
|
2018-05-26 09:37:47 +01:00
|
|
|
more_fseek(ctl, f, 0L);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline = 0; /* skiplns() will make Currline correct */
|
|
|
|
skiplns(ctl, initline, f);
|
|
|
|
if (!ctl->noscroll) {
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = ctl->dlines + 1;
|
2018-04-19 22:46:26 +01:00
|
|
|
} else {
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = ctl->dlines;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-26 09:37:47 +01:00
|
|
|
done = 1;
|
|
|
|
break;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
case ' ':
|
|
|
|
case 'z':
|
|
|
|
if (nlines == 0)
|
2018-05-23 22:58:34 +01:00
|
|
|
nlines = ctl->dlines;
|
2018-04-19 22:46:26 +01:00
|
|
|
else if (comchar == 'z')
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->dlines = nlines;
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = nlines;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2018-04-19 22:46:26 +01:00
|
|
|
case 'd':
|
|
|
|
case ctrl('D'):
|
|
|
|
if (nlines != 0)
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->nscroll = nlines;
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = ctl->nscroll;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2018-04-19 22:46:26 +01:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
end_it(0);
|
|
|
|
case 's':
|
|
|
|
case 'f':
|
|
|
|
case ctrl('F'):
|
|
|
|
if (nlines == 0)
|
|
|
|
nlines++;
|
|
|
|
if (comchar == 'f')
|
2018-05-23 22:58:34 +01:00
|
|
|
nlines *= ctl->dlines;
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\r');
|
2018-05-23 22:58:34 +01:00
|
|
|
erasep(ctl, 0);
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\n');
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
printf(P_("...skipping %d line",
|
|
|
|
"...skipping %d lines", nlines),
|
|
|
|
nlines);
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\n');
|
2013-01-24 19:27:12 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
while (nlines > 0) {
|
2018-05-26 09:37:47 +01:00
|
|
|
while ((c = more_getc(ctl, f)) != '\n')
|
2018-04-19 22:46:26 +01:00
|
|
|
if (c == EOF) {
|
|
|
|
retval = 0;
|
|
|
|
done++;
|
|
|
|
goto endsw;
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline++;
|
2018-04-19 22:46:26 +01:00
|
|
|
nlines--;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = ctl->dlines;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2018-04-19 22:46:26 +01:00
|
|
|
case '\n':
|
|
|
|
if (nlines != 0)
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->dlines = nlines;
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
|
|
|
nlines = 1;
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = nlines;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2018-04-19 22:46:26 +01:00
|
|
|
case '\f':
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->no_intty) {
|
|
|
|
doclear(ctl);
|
2018-05-26 09:37:47 +01:00
|
|
|
more_fseek(ctl, f, ctl->screen_start.chrctr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline = ctl->screen_start.line;
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = ctl->dlines;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
} else {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(RINGBELL, stderr);
|
2018-04-19 22:46:26 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
case '\'':
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->no_intty) {
|
|
|
|
kill_line(ctl);
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_("\n***Back***\n\n"), stdout);
|
|
|
|
more_fseek(ctl, f, ctl->context.chrctr);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Currline = ctl->context.line;
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = ctl->dlines;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2018-04-19 22:46:26 +01:00
|
|
|
} else {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(RINGBELL, stderr);
|
2018-04-19 22:46:26 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
case '=':
|
2018-05-23 22:58:34 +01:00
|
|
|
kill_line(ctl);
|
|
|
|
ctl->promptlen = printf("%d", ctl->Currline);
|
2018-04-19 22:46:26 +01:00
|
|
|
fflush(stdout);
|
|
|
|
break;
|
|
|
|
case 'n':
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->previousre) {
|
|
|
|
more_error(ctl, _("No previous regular expression"));
|
2018-04-19 22:46:26 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->lastp++;
|
2018-04-19 22:46:26 +01:00
|
|
|
/* fallthrough */
|
|
|
|
case '/':
|
|
|
|
if (nlines == 0)
|
|
|
|
nlines++;
|
2018-05-23 22:58:34 +01:00
|
|
|
kill_line(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('/');
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen = 1;
|
2018-04-19 22:46:26 +01:00
|
|
|
fflush(stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->lastp) {
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc('\r', stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
search(ctl, ctl->previousre, f, nlines);
|
2012-10-08 08:08:17 +01:00
|
|
|
} else {
|
2018-05-23 22:58:34 +01:00
|
|
|
ttyin(ctl, cmdbuf, sizeof(cmdbuf) - 2, '/');
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc('\r', stderr);
|
2018-05-23 22:58:34 +01:00
|
|
|
free(ctl->previousre);
|
|
|
|
ctl->previousre = xstrdup(cmdbuf);
|
|
|
|
search(ctl, cmdbuf, f, nlines);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-26 09:37:47 +01:00
|
|
|
retval = ctl->dlines - 1;
|
|
|
|
done = 1;
|
|
|
|
break;
|
2018-04-19 22:46:26 +01:00
|
|
|
case '!':
|
2018-05-23 22:58:34 +01:00
|
|
|
do_shell(ctl, filename);
|
2018-04-19 22:46:26 +01:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
case 'h':
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->noscroll)
|
|
|
|
doclear(ctl);
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_("\n"
|
2018-04-19 22:46:26 +01:00
|
|
|
"Most commands optionally preceded by integer argument k. "
|
|
|
|
"Defaults in brackets.\n"
|
2018-05-26 09:37:47 +01:00
|
|
|
"Star (*) indicates argument becomes new default.\n"), stdout);
|
2018-04-19 22:46:26 +01:00
|
|
|
puts("---------------------------------------"
|
|
|
|
"----------------------------------------");
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs(_
|
2018-04-19 22:46:26 +01:00
|
|
|
("<space> Display next k lines of text [current screen size]\n"
|
|
|
|
"z Display next k lines of text [current screen size]*\n"
|
|
|
|
"<return> Display next k lines of text [1]*\n"
|
|
|
|
"d or ctrl-D Scroll k lines [current scroll size, initially 11]*\n"
|
|
|
|
"q or Q or <interrupt> Exit from more\n"
|
|
|
|
"s Skip forward k lines of text [1]\n"
|
|
|
|
"f Skip forward k screenfuls of text [1]\n"
|
|
|
|
"b or ctrl-B Skip backwards k screenfuls of text [1]\n"
|
|
|
|
"' Go to place where previous search started\n"
|
|
|
|
"= Display current line number\n"
|
|
|
|
"/<regular expression> Search for kth occurrence of regular expression [1]\n"
|
|
|
|
"n Search for kth occurrence of last r.e [1]\n"
|
|
|
|
"!<cmd> or :!<cmd> Execute <cmd> in a subshell\n"
|
|
|
|
"v Start up /usr/bin/vi at current line\n"
|
|
|
|
"ctrl-L Redraw screen\n"
|
|
|
|
":n Go to kth next file [1]\n"
|
|
|
|
":p Go to kth previous file [1]\n"
|
|
|
|
":f Display current file name and line number\n"
|
2018-05-26 09:37:47 +01:00
|
|
|
". Repeat previous command\n"), stdout);
|
2018-04-19 22:46:26 +01:00
|
|
|
puts("---------------------------------------"
|
|
|
|
"----------------------------------------");
|
2018-05-23 22:58:34 +01:00
|
|
|
prompt(ctl, filename);
|
2018-04-19 22:46:26 +01:00
|
|
|
break;
|
|
|
|
case 'v': /* This case should go right before default */
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->no_intty) {
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Earlier: call vi +n file. This also
|
|
|
|
* works for emacs. POSIX: call vi -c n
|
|
|
|
* file (when editor is vi or ex). */
|
|
|
|
char *editor, *p;
|
2018-05-23 22:58:34 +01:00
|
|
|
int n = (ctl->Currline - ctl->dlines <= 0 ? 1 :
|
|
|
|
ctl->Currline - (ctl->dlines + 1) / 2);
|
2018-04-19 22:46:26 +01:00
|
|
|
int split = 0;
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
editor = getenv("VISUAL");
|
|
|
|
if (editor == NULL || *editor == '\0')
|
|
|
|
editor = getenv("EDITOR");
|
|
|
|
if (editor == NULL || *editor == '\0')
|
|
|
|
editor = VI;
|
|
|
|
|
|
|
|
p = strrchr(editor, '/');
|
|
|
|
if (p)
|
|
|
|
p++;
|
|
|
|
else
|
|
|
|
p = editor;
|
|
|
|
if (!strcmp(p, "vi") || !strcmp(p, "ex")) {
|
|
|
|
sprintf(cmdbuf, "-c %d", n);
|
|
|
|
split = 1;
|
|
|
|
} else {
|
|
|
|
sprintf(cmdbuf, "+%d", n);
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
kill_line(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
printf("%s %s %s", editor, cmdbuf,
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->fnames[ctl->fnum]);
|
2018-04-19 22:46:26 +01:00
|
|
|
if (split) {
|
|
|
|
cmdbuf[2] = 0;
|
2018-05-23 22:58:34 +01:00
|
|
|
execute(ctl, filename, editor, editor,
|
2018-04-19 22:46:26 +01:00
|
|
|
cmdbuf, cmdbuf + 3,
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->fnames[ctl->fnum], (char *)0);
|
2018-04-19 22:46:26 +01:00
|
|
|
} else
|
2018-05-23 22:58:34 +01:00
|
|
|
execute(ctl, filename, editor, editor,
|
|
|
|
cmdbuf, ctl->fnames[ctl->fnum],
|
2018-04-19 22:46:26 +01:00
|
|
|
(char *)0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* fallthrough */
|
|
|
|
default:
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->dum_opt) {
|
|
|
|
kill_line(ctl);
|
|
|
|
if (ctl->Senter && ctl->Sexit) {
|
|
|
|
putstring(ctl->Senter);
|
|
|
|
ctl->promptlen =
|
2018-04-19 22:46:26 +01:00
|
|
|
printf(_
|
|
|
|
("[Press 'h' for instructions.]"))
|
2018-05-23 22:58:34 +01:00
|
|
|
+ 2 * ctl->soglitch;
|
|
|
|
putstring(ctl->Sexit);
|
2018-04-19 22:46:26 +01:00
|
|
|
} else
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen =
|
2018-04-19 22:46:26 +01:00
|
|
|
printf(_
|
|
|
|
("[Press 'h' for instructions.]"));
|
|
|
|
fflush(stdout);
|
|
|
|
} else
|
2018-05-26 09:37:47 +01:00
|
|
|
fputc(RINGBELL, stderr);
|
2018-04-19 22:46:26 +01:00
|
|
|
break;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
if (done)
|
|
|
|
break;
|
2006-12-07 00:25:37 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\r');
|
|
|
|
endsw:
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->inwait = 0;
|
|
|
|
ctl->notell = 1;
|
2018-04-19 22:46:26 +01:00
|
|
|
return (retval);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Print out the contents of the file f, one screenful at a time. */
|
2018-05-23 22:58:34 +01:00
|
|
|
static void screen(struct more_control *ctl, register FILE *f, register int num_lines)
|
2012-10-08 08:08:17 +01:00
|
|
|
{
|
2018-04-19 22:46:26 +01:00
|
|
|
register int c;
|
|
|
|
register int nchars;
|
|
|
|
int length; /* length of current line */
|
|
|
|
static int prev_len = 1; /* length of previous line */
|
2012-10-08 08:08:17 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
for (;;) {
|
2018-05-23 22:58:34 +01:00
|
|
|
while (num_lines > 0 && !ctl->Pause) {
|
|
|
|
if ((nchars = get_line(ctl, f, &length)) == EOF) {
|
|
|
|
if (ctl->clreol)
|
|
|
|
clreos(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
return;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->ssp_opt && length == 0 && prev_len == 0)
|
2018-04-19 22:46:26 +01:00
|
|
|
continue;
|
|
|
|
prev_len = length;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->bad_so
|
|
|
|
|| ((ctl->Senter && *ctl->Senter == ' ') && (ctl->promptlen > 0)))
|
|
|
|
erasep(ctl, 0);
|
2018-04-19 22:46:26 +01:00
|
|
|
/* must clear before drawing line since tabs on
|
|
|
|
* some terminals do not erase what they tab
|
|
|
|
* over. */
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
cleareol(ctl);
|
|
|
|
prbuf(ctl, ctl->Line, length);
|
|
|
|
if (nchars < ctl->promptlen)
|
|
|
|
erasep(ctl, nchars); /* erasep () sets promptlen to 0 */
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->promptlen = 0;
|
|
|
|
if (nchars < ctl->Mcol || !ctl->fold_opt)
|
|
|
|
prbuf(ctl, "\n", 1); /* will turn off UL if necessary */
|
2018-04-19 22:46:26 +01:00
|
|
|
num_lines--;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->pstate) {
|
|
|
|
putp(ctl->ULexit);
|
|
|
|
ctl->pstate = 0;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
fflush(stdout);
|
2018-05-26 09:37:47 +01:00
|
|
|
if ((c = more_getc(ctl, f)) == EOF) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->clreol)
|
|
|
|
clreos(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
return;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->Pause && ctl->clreol)
|
|
|
|
clreos(ctl);
|
2018-05-26 09:37:47 +01:00
|
|
|
more_ungetc(ctl, c, f);
|
2018-05-23 22:58:34 +01:00
|
|
|
sigsetjmp(ctl->restore, 1);
|
|
|
|
ctl->Pause = 0;
|
|
|
|
ctl->startup = 0;
|
|
|
|
if ((num_lines = command(ctl, NULL, f)) == 0)
|
2018-04-19 22:46:26 +01:00
|
|
|
return;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl->hard && ctl->promptlen > 0)
|
|
|
|
erasep(ctl, 0);
|
|
|
|
if (ctl->noscroll && num_lines >= ctl->dlines) {
|
|
|
|
if (ctl->clreol)
|
|
|
|
home(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
doclear(ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->screen_start.line = ctl->Currline;
|
2018-05-26 09:37:47 +01:00
|
|
|
ctl->screen_start.chrctr = ctl->file_pos;
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
/* Come here if a signal for a window size change is received */
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
static void chgwinsz(int dummy __attribute__((__unused__)))
|
2012-10-08 08:08:17 +01:00
|
|
|
{
|
2018-04-19 22:46:26 +01:00
|
|
|
struct winsize win;
|
|
|
|
|
|
|
|
signal(SIGWINCH, SIG_IGN);
|
|
|
|
if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
|
|
|
|
if (win.ws_row != 0) {
|
2018-05-23 22:58:34 +01:00
|
|
|
global_ctl->Lpp = win.ws_row;
|
|
|
|
global_ctl->nscroll = global_ctl->Lpp / 2 - 1;
|
|
|
|
if (global_ctl->nscroll <= 0)
|
|
|
|
global_ctl->nscroll = 1;
|
2018-05-26 20:08:34 +01:00
|
|
|
global_ctl->dlines = global_ctl->Lpp - 1;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
if (win.ws_col != 0)
|
2018-05-23 22:58:34 +01:00
|
|
|
global_ctl->Mcol = win.ws_col;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
signal(SIGWINCH, chgwinsz);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
2018-04-19 22:46:26 +01:00
|
|
|
#endif /* SIGWINCH */
|
2006-12-07 00:25:32 +01:00
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
static void copy_file(register FILE *f)
|
2012-10-08 08:08:17 +01:00
|
|
|
{
|
2018-04-19 22:46:26 +01:00
|
|
|
char buf[BUFSIZ];
|
|
|
|
size_t sz;
|
|
|
|
|
|
|
|
while ((sz = fread(&buf, sizeof(char), sizeof(buf), f)) > 0)
|
|
|
|
fwrite(&buf, sizeof(char), sz, stdout);
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2006-12-07 00:25:34 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
static void initterm(struct more_control *ctl)
|
2018-04-19 22:46:26 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char *term;
|
|
|
|
struct winsize win;
|
|
|
|
|
|
|
|
#ifndef NON_INTERACTIVE_MORE
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->no_tty = tcgetattr(fileno(stdout), &ctl->otty);
|
2018-04-19 22:46:26 +01:00
|
|
|
#endif
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->no_tty) {
|
|
|
|
ctl->docrterase = (ctl->otty.c_cc[VERASE] != 255);
|
|
|
|
ctl->docrtkill = (ctl->otty.c_cc[VKILL] != 255);
|
2018-04-19 22:46:26 +01:00
|
|
|
if ((term = getenv("TERM")) == NULL) {
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->dumb = 1;
|
|
|
|
ctl->ul_opt = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
setupterm(term, 1, &ret);
|
|
|
|
if (ret <= 0) {
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->dumb = 1;
|
|
|
|
ctl->ul_opt = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
} else {
|
|
|
|
#ifdef TIOCGWINSZ
|
|
|
|
if (ioctl(fileno(stdout), TIOCGWINSZ, &win) < 0) {
|
|
|
|
#endif
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Lpp = tigetnum(TERM_LINES);
|
|
|
|
ctl->Mcol = tigetnum(TERM_COLS);
|
2018-04-19 22:46:26 +01:00
|
|
|
#ifdef TIOCGWINSZ
|
|
|
|
} else {
|
2018-05-23 22:58:34 +01:00
|
|
|
if ((ctl->Lpp = win.ws_row) == 0)
|
|
|
|
ctl->Lpp = tigetnum(TERM_LINES);
|
|
|
|
if ((ctl->Mcol = win.ws_col) == 0)
|
|
|
|
ctl->Mcol = tigetnum(TERM_COLS);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
#endif
|
2018-05-23 22:58:34 +01:00
|
|
|
if ((ctl->Lpp <= 0) || tigetflag(TERM_HARD_COPY)) {
|
2018-05-26 20:08:34 +01:00
|
|
|
ctl->hard = 1;
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Lpp = LINES_PER_PAGE;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tigetflag(TERM_EAT_NEW_LINE))
|
|
|
|
/* Eat newline at last column + 1; dec, concept */
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->eatnl++;
|
|
|
|
if (ctl->Mcol <= 0)
|
|
|
|
ctl->Mcol = NUM_COLUMNS;
|
|
|
|
|
|
|
|
ctl->Wrap = tigetflag(TERM_AUTO_RIGHT_MARGIN);
|
|
|
|
ctl->bad_so = tigetflag(TERM_CEOL);
|
|
|
|
ctl->eraseln = tigetstr(TERM_CLEAR_TO_LINE_END);
|
|
|
|
ctl->Clear = tigetstr(TERM_CLEAR);
|
|
|
|
ctl->Senter = tigetstr(TERM_STANDARD_MODE);
|
|
|
|
ctl->Sexit = tigetstr(TERM_EXIT_STANDARD_MODE);
|
|
|
|
if (0 < tigetnum(TERM_STD_MODE_GLITCH))
|
|
|
|
ctl->soglitch = 1;
|
2018-04-19 22:46:26 +01:00
|
|
|
|
|
|
|
/* Set up for underlining: some terminals don't
|
|
|
|
* need it; others have start/stop sequences,
|
|
|
|
* still others have an underline char sequence
|
|
|
|
* which is assumed to move the cursor forward
|
|
|
|
* one character. If underline sequence isn't
|
|
|
|
* available, settle for standout sequence. */
|
|
|
|
if (tigetflag(TERM_UNDERLINE)
|
|
|
|
|| tigetflag(TERM_OVER_STRIKE))
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->ul_opt = 0;
|
|
|
|
if ((ctl->chUL = tigetstr(TERM_UNDERLINE_CHAR)) == NULL)
|
|
|
|
ctl->chUL = "";
|
|
|
|
if (((ctl->ULenter =
|
2018-04-19 22:46:26 +01:00
|
|
|
tigetstr(TERM_ENTER_UNDERLINE)) == NULL
|
2018-05-23 22:58:34 +01:00
|
|
|
|| (ctl->ULexit =
|
2018-04-19 22:46:26 +01:00
|
|
|
tigetstr(TERM_EXIT_UNDERLINE)) == NULL)
|
2018-05-23 22:58:34 +01:00
|
|
|
&& !*ctl->chUL) {
|
|
|
|
if ((ctl->ULenter = ctl->Senter) == NULL
|
|
|
|
|| (ctl->ULexit = ctl->Sexit) == NULL) {
|
|
|
|
ctl->ULenter = "";
|
|
|
|
ctl->ULexit = "";
|
2018-04-19 22:46:26 +01:00
|
|
|
} else
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->ulglitch = ctl->soglitch;
|
2018-04-19 22:46:26 +01:00
|
|
|
} else {
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->ulglitch = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->Home = tigetstr(TERM_HOME);
|
|
|
|
if (ctl->Home == NULL || *ctl->Home == '\0') {
|
|
|
|
if ((ctl->cursorm =
|
2018-04-19 22:46:26 +01:00
|
|
|
tigetstr(TERM_CURSOR_ADDRESS)) != NULL) {
|
|
|
|
const char *t =
|
2018-05-23 22:58:34 +01:00
|
|
|
(const char *)tparm(ctl->cursorm, 0,
|
2018-04-19 22:46:26 +01:00
|
|
|
0);
|
2018-05-23 22:58:34 +01:00
|
|
|
xstrncpy(ctl->cursorhome, t,
|
|
|
|
sizeof(ctl->cursorhome));
|
|
|
|
ctl->Home = ctl->cursorhome;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->EodClr = tigetstr(TERM_CLEAR_TO_SCREEN_END);
|
|
|
|
if ((ctl->chBS = tigetstr(TERM_LINE_DOWN)) == NULL)
|
|
|
|
ctl->chBS = BS;
|
2018-04-19 22:46:26 +01:00
|
|
|
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if ((ctl->shell = getenv("SHELL")) == NULL)
|
|
|
|
ctl->shell = "/bin/sh";
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl->no_intty = tcgetattr(fileno(stdin), &ctl->otty);
|
|
|
|
tcgetattr(fileno(stderr), &ctl->otty);
|
|
|
|
ctl->savetty0 = ctl->otty;
|
2018-05-26 20:08:34 +01:00
|
|
|
ctl->hardtabs = (ctl->otty.c_oflag & TABDLY) != TAB3;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl->no_tty) {
|
|
|
|
ctl->otty.c_lflag &= ~(ICANON | ECHO);
|
|
|
|
ctl->otty.c_cc[VMIN] = 1;
|
|
|
|
ctl->otty.c_cc[VTIME] = 0;
|
2012-10-08 08:08:17 +01:00
|
|
|
}
|
2006-12-07 00:25:32 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 22:46:26 +01:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
char *s;
|
|
|
|
int chr;
|
|
|
|
int left;
|
|
|
|
int prnames = 0;
|
|
|
|
int initopt = 0;
|
|
|
|
int srchopt = 0;
|
|
|
|
int clearit = 0;
|
|
|
|
int initline = 0;
|
|
|
|
char *initbuf = NULL;
|
2018-05-23 22:58:34 +01:00
|
|
|
struct more_control ctl = {
|
|
|
|
.firstf = 1,
|
|
|
|
.fold_opt = 1,
|
|
|
|
.notell = 1,
|
|
|
|
.startup = 1,
|
|
|
|
.stop_opt = 1,
|
|
|
|
.ul_opt = 1,
|
|
|
|
.Wrap = 1,
|
|
|
|
.Lpp = LINES_PER_PAGE,
|
|
|
|
.Mcol = NUM_COLUMNS,
|
|
|
|
.nscroll = SCROLL_LEN,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
global_ctl = &ctl;
|
2018-04-19 22:46:26 +01:00
|
|
|
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
bindtextdomain(PACKAGE, LOCALEDIR);
|
|
|
|
textdomain(PACKAGE);
|
|
|
|
atexit(close_stdout);
|
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
/* first arg may be one of our standard longopts */
|
|
|
|
if (!strcmp(argv[1], "--help"))
|
|
|
|
usage();
|
|
|
|
if (!strcmp(argv[1], "--version")) {
|
|
|
|
printf(UTIL_LINUX_VERSION);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.nfiles = argc;
|
|
|
|
ctl.fnames = argv;
|
2018-04-19 22:46:26 +01:00
|
|
|
setlocale(LC_ALL, "");
|
2018-05-23 22:58:34 +01:00
|
|
|
initterm(&ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
|
|
|
|
/* Auto set no scroll on when binary is called page */
|
|
|
|
if (!(strcmp(program_invocation_short_name, "page")))
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.noscroll++;
|
2018-04-19 22:46:26 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
prepare_line_buffer(&ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.nscroll = ctl.Lpp / 2 - 1;
|
|
|
|
if (ctl.nscroll <= 0)
|
|
|
|
ctl.nscroll = 1;
|
2018-04-19 22:46:26 +01:00
|
|
|
|
|
|
|
if ((s = getenv("MORE")) != NULL)
|
2018-05-23 22:58:34 +01:00
|
|
|
argscan(&ctl, s);
|
2018-04-19 22:46:26 +01:00
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
while (--ctl.nfiles > 0) {
|
|
|
|
if ((chr = (*++ctl.fnames)[0]) == '-') {
|
|
|
|
argscan(&ctl, *ctl.fnames + 1);
|
2018-04-19 22:46:26 +01:00
|
|
|
} else if (chr == '+') {
|
2018-05-23 22:58:34 +01:00
|
|
|
s = *ctl.fnames;
|
2018-04-19 22:46:26 +01:00
|
|
|
if (*++s == '/') {
|
|
|
|
srchopt++;
|
|
|
|
initbuf = xstrdup(s + 1);
|
|
|
|
} else {
|
|
|
|
initopt++;
|
|
|
|
for (initline = 0; *s != '\0'; s++)
|
|
|
|
if (isdigit(*s))
|
|
|
|
initline =
|
|
|
|
initline * 10 + *s - '0';
|
|
|
|
--initline;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* allow clreol only if Home and eraseln and EodClr strings are
|
|
|
|
* defined, and in that case, make sure we are in noscroll mode */
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl.clreol) {
|
|
|
|
if ((ctl.Home == NULL) || (*ctl.Home == '\0') ||
|
|
|
|
(ctl.eraseln == NULL) || (*ctl.eraseln == '\0') ||
|
|
|
|
(ctl.EodClr == NULL) || (*ctl.EodClr == '\0'))
|
|
|
|
ctl.clreol = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.noscroll = 1;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl.dlines == 0)
|
2018-05-26 20:08:34 +01:00
|
|
|
ctl.dlines = ctl.Lpp - 1;
|
2018-05-23 22:58:34 +01:00
|
|
|
left = ctl.dlines;
|
|
|
|
if (ctl.nfiles > 1)
|
2018-04-19 22:46:26 +01:00
|
|
|
prnames++;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl.no_intty && ctl.nfiles == 0) {
|
2018-04-19 22:46:26 +01:00
|
|
|
warnx(_("bad usage"));
|
|
|
|
errtryhelp(EXIT_FAILURE);
|
|
|
|
} else
|
|
|
|
f = stdin;
|
2018-05-23 22:58:34 +01:00
|
|
|
if (!ctl.no_tty) {
|
2018-04-19 22:46:26 +01:00
|
|
|
signal(SIGQUIT, onquit);
|
|
|
|
signal(SIGINT, end_it);
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
signal(SIGWINCH, chgwinsz);
|
|
|
|
#endif
|
|
|
|
if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
|
|
|
|
signal(SIGTSTP, onsusp);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.catch_susp++;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-26 09:37:47 +01:00
|
|
|
tcsetattr(STDERR_FILENO, TCSANOW, &ctl.otty);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl.no_intty) {
|
|
|
|
if (ctl.no_tty)
|
2018-04-19 22:46:26 +01:00
|
|
|
copy_file(stdin);
|
|
|
|
else {
|
2018-05-23 22:58:34 +01:00
|
|
|
if ((chr = getc(f)) == '\f')
|
|
|
|
doclear(&ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
else {
|
2018-05-23 22:58:34 +01:00
|
|
|
ungetc(chr, f);
|
|
|
|
if (ctl.noscroll && (chr != EOF)) {
|
|
|
|
if (ctl.clreol)
|
|
|
|
home(&ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
doclear(&ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (srchopt) {
|
2018-05-23 22:58:34 +01:00
|
|
|
free(ctl.previousre);
|
|
|
|
ctl.previousre = xstrdup(initbuf);
|
|
|
|
search(&ctl, initbuf, stdin, 1);
|
|
|
|
if (ctl.noscroll)
|
2018-04-19 22:46:26 +01:00
|
|
|
left--;
|
|
|
|
} else if (initopt)
|
2018-05-23 22:58:34 +01:00
|
|
|
skiplns(&ctl, initline, stdin);
|
|
|
|
screen(&ctl, stdin, left);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.no_intty = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
prnames++;
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.firstf = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:58:34 +01:00
|
|
|
while (ctl.fnum < ctl.nfiles) {
|
|
|
|
if ((f = checkf(&ctl, ctl.fnames[ctl.fnum], &clearit)) != NULL) {
|
|
|
|
ctl.context.line = ctl.context.chrctr = 0;
|
|
|
|
ctl.Currline = 0;
|
|
|
|
if (ctl.firstf)
|
|
|
|
sigsetjmp(ctl.restore, 1);
|
|
|
|
if (ctl.firstf) {
|
|
|
|
ctl.firstf = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
if (srchopt) {
|
2018-05-23 22:58:34 +01:00
|
|
|
free(ctl.previousre);
|
|
|
|
ctl.previousre = xstrdup(initbuf);
|
|
|
|
search(&ctl, initbuf, f, 1);
|
|
|
|
if (ctl.noscroll)
|
2018-04-19 22:46:26 +01:00
|
|
|
left--;
|
|
|
|
} else if (initopt)
|
2018-05-23 22:58:34 +01:00
|
|
|
skiplns(&ctl, initline, f);
|
|
|
|
} else if (ctl.fnum < ctl.nfiles && !ctl.no_tty) {
|
|
|
|
sigsetjmp(ctl.restore, 1);
|
|
|
|
left = command(&ctl, ctl.fnames[ctl.fnum], f);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
if (left != 0) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if ((ctl.noscroll || clearit)
|
|
|
|
&& (ctl.file_size != LONG_MAX)) {
|
|
|
|
if (ctl.clreol)
|
|
|
|
home(&ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
else
|
2018-05-23 22:58:34 +01:00
|
|
|
doclear(&ctl);
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
if (prnames) {
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl.bad_so)
|
|
|
|
erasep(&ctl, 0);
|
|
|
|
if (ctl.clreol)
|
|
|
|
cleareol(&ctl);
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs("::::::::::::::", stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl.promptlen > 14)
|
|
|
|
erasep(&ctl, 14);
|
2018-04-19 22:46:26 +01:00
|
|
|
putchar('\n');
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl.clreol)
|
|
|
|
cleareol(&ctl);
|
|
|
|
puts(ctl.fnames[ctl.fnum]);
|
|
|
|
if (ctl.clreol)
|
|
|
|
cleareol(&ctl);
|
2018-05-26 09:37:47 +01:00
|
|
|
fputs("::::::::::::::\n", stdout);
|
2018-05-23 22:58:34 +01:00
|
|
|
if (left > ctl.Lpp - 4)
|
|
|
|
left = ctl.Lpp - 4;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
if (ctl.no_tty)
|
2018-04-19 22:46:26 +01:00
|
|
|
copy_file(f);
|
|
|
|
else {
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.within = 1;
|
|
|
|
screen(&ctl, f, left);
|
|
|
|
ctl.within = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
sigsetjmp(ctl.restore, 1);
|
2018-04-19 22:46:26 +01:00
|
|
|
fflush(stdout);
|
|
|
|
fclose(f);
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.screen_start.line = ctl.screen_start.chrctr = 0L;
|
|
|
|
ctl.context.line = ctl.context.chrctr = 0L;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
ctl.fnum++;
|
|
|
|
ctl.firstf = 0;
|
2018-04-19 22:46:26 +01:00
|
|
|
}
|
2018-05-23 22:58:34 +01:00
|
|
|
free(ctl.previousre);
|
2018-04-19 22:46:26 +01:00
|
|
|
free(initbuf);
|
2018-05-23 22:58:34 +01:00
|
|
|
free(ctl.Line);
|
2018-04-19 22:46:26 +01:00
|
|
|
reset_tty();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|