2000-05-23 05:51:17 +00:00
|
|
|
/* $FreeBSD$ */
|
2012-07-23 21:31:53 +00:00
|
|
|
/*
|
2019-06-29 04:39:01 +00:00
|
|
|
* Copyright (C) 1984-2019 Mark Nudelman
|
2012-07-23 21:31:53 +00:00
|
|
|
*
|
|
|
|
* You may distribute under the terms of either the GNU General Public
|
|
|
|
* License or the Less License, as specified in the README file.
|
|
|
|
*
|
|
|
|
* For more information, see the README file.
|
|
|
|
*/
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Entry point, initialization, miscellaneous routines.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "less.h"
|
2002-01-07 20:37:09 +00:00
|
|
|
#if MSDOS_COMPILER==WIN32C
|
2019-06-29 04:39:01 +00:00
|
|
|
#define WIN32_LEAN_AND_MEAN
|
2002-01-07 20:37:09 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
public char * every_first_cmd = NULL;
|
|
|
|
public int new_file;
|
|
|
|
public int is_tty;
|
|
|
|
public IFILE curr_ifile = NULL_IFILE;
|
|
|
|
public IFILE old_ifile = NULL_IFILE;
|
|
|
|
public struct scrpos initial_scrpos;
|
|
|
|
public int any_display = FALSE;
|
|
|
|
public POSITION start_attnpos = NULL_POSITION;
|
|
|
|
public POSITION end_attnpos = NULL_POSITION;
|
|
|
|
public int wscroll;
|
|
|
|
public char * progname;
|
|
|
|
public int quitting;
|
|
|
|
public int secure;
|
|
|
|
public int dohelp;
|
|
|
|
|
|
|
|
#if LOGFILE
|
|
|
|
public int logfile = -1;
|
|
|
|
public int force_logfile = FALSE;
|
|
|
|
public char * namelogfile = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if EDITOR
|
|
|
|
public char * editor;
|
|
|
|
public char * editproto;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TAGS
|
2002-01-07 20:37:09 +00:00
|
|
|
extern char * tags;
|
2000-05-22 09:53:22 +00:00
|
|
|
extern char * tagoption;
|
|
|
|
extern int jump_sline;
|
|
|
|
#endif
|
|
|
|
|
2002-01-07 20:37:09 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
static char consoleTitle[256];
|
|
|
|
#endif
|
|
|
|
|
2019-06-29 04:39:01 +00:00
|
|
|
public int one_screen;
|
2011-05-09 18:30:23 +00:00
|
|
|
extern int less_is_more;
|
2000-05-22 09:53:22 +00:00
|
|
|
extern int missing_cap;
|
|
|
|
extern int know_dumb;
|
2007-06-23 15:28:00 +00:00
|
|
|
extern int no_init;
|
2007-06-04 01:43:11 +00:00
|
|
|
extern int pr_type;
|
2017-04-29 06:30:21 +00:00
|
|
|
extern int quit_if_one_screen;
|
2019-06-29 04:39:01 +00:00
|
|
|
extern int no_init;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Entry point.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
IFILE ifile;
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
#ifdef __EMX__
|
|
|
|
_response(&argc, &argv);
|
|
|
|
_wildcard(&argc, &argv);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
progname = *argv++;
|
|
|
|
argc--;
|
|
|
|
|
|
|
|
secure = 0;
|
|
|
|
s = lgetenv("LESSSECURE");
|
2019-06-29 04:39:01 +00:00
|
|
|
if (!isnullenv(s))
|
2000-05-22 09:53:22 +00:00
|
|
|
secure = 1;
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
if (getenv("HOME") == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If there is no HOME environment variable,
|
|
|
|
* try the concatenation of HOMEDRIVE + HOMEPATH.
|
|
|
|
*/
|
|
|
|
char *drive = getenv("HOMEDRIVE");
|
|
|
|
char *path = getenv("HOMEPATH");
|
|
|
|
if (drive != NULL && path != NULL)
|
|
|
|
{
|
|
|
|
char *env = (char *) ecalloc(strlen(drive) +
|
|
|
|
strlen(path) + 6, sizeof(char));
|
|
|
|
strcpy(env, "HOME=");
|
|
|
|
strcat(env, drive);
|
|
|
|
strcat(env, path);
|
|
|
|
putenv(env);
|
|
|
|
}
|
|
|
|
}
|
2002-01-07 20:37:09 +00:00
|
|
|
GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif /* WIN32 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process command line arguments and LESS environment arguments.
|
|
|
|
* Command line arguments override environment arguments.
|
|
|
|
*/
|
|
|
|
is_tty = isatty(1);
|
2019-06-29 04:39:01 +00:00
|
|
|
init_mark();
|
2000-05-22 09:53:22 +00:00
|
|
|
init_cmds();
|
2017-11-20 06:53:49 +00:00
|
|
|
get_term();
|
|
|
|
expand_cmd_tables();
|
2000-05-22 09:53:22 +00:00
|
|
|
init_charset();
|
|
|
|
init_line();
|
2006-08-20 15:50:51 +00:00
|
|
|
init_cmdhist();
|
2000-05-22 09:53:22 +00:00
|
|
|
init_option();
|
2009-07-27 07:05:08 +00:00
|
|
|
init_search();
|
2007-06-04 01:43:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the name of the executable program is "more",
|
|
|
|
* act like LESS_IS_MORE is set.
|
|
|
|
*/
|
2019-06-29 04:39:01 +00:00
|
|
|
s = last_component(progname);
|
2007-06-04 01:43:11 +00:00
|
|
|
if (strcmp(s, "more") == 0)
|
|
|
|
less_is_more = 1;
|
|
|
|
|
|
|
|
init_prompt();
|
|
|
|
|
2007-06-16 02:43:44 +00:00
|
|
|
if (less_is_more)
|
|
|
|
scan_option("-fG");
|
|
|
|
|
2007-06-04 01:43:11 +00:00
|
|
|
s = lgetenv(less_is_more ? "MORE" : "LESS");
|
2000-05-22 09:53:22 +00:00
|
|
|
if (s != NULL)
|
|
|
|
scan_option(save(s));
|
|
|
|
|
2007-06-21 10:39:24 +00:00
|
|
|
#define isoptstring(s) less_is_more ? (((s)[0] == '-') && (s)[1] != '\0') : \
|
|
|
|
(((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
|
2000-05-22 09:53:22 +00:00
|
|
|
while (argc > 0 && (isoptstring(*argv) || isoptpending()))
|
|
|
|
{
|
|
|
|
s = *argv++;
|
|
|
|
argc--;
|
|
|
|
if (strcmp(s, "--") == 0)
|
|
|
|
break;
|
|
|
|
scan_option(s);
|
|
|
|
}
|
|
|
|
#undef isoptstring
|
|
|
|
|
|
|
|
if (isoptpending())
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Last command line option was a flag requiring a
|
|
|
|
* following string, but there was no following string.
|
|
|
|
*/
|
|
|
|
nopendopt();
|
|
|
|
quit(QUIT_OK);
|
|
|
|
}
|
|
|
|
|
2007-08-13 16:15:26 +00:00
|
|
|
if (less_is_more)
|
|
|
|
no_init = TRUE;
|
2007-06-04 01:43:11 +00:00
|
|
|
|
2000-05-22 09:53:22 +00:00
|
|
|
#if EDITOR
|
|
|
|
editor = lgetenv("VISUAL");
|
|
|
|
if (editor == NULL || *editor == '\0')
|
|
|
|
{
|
|
|
|
editor = lgetenv("EDITOR");
|
2019-06-29 04:39:01 +00:00
|
|
|
if (isnullenv(editor))
|
2000-05-22 09:53:22 +00:00
|
|
|
editor = EDIT_PGM;
|
|
|
|
}
|
|
|
|
editproto = lgetenv("LESSEDIT");
|
2019-06-29 04:39:01 +00:00
|
|
|
if (isnullenv(editproto))
|
|
|
|
editproto = "%E ?lm+%lm. %g";
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call get_ifile with all the command line filenames
|
|
|
|
* to "register" them with the ifile system.
|
|
|
|
*/
|
|
|
|
ifile = NULL_IFILE;
|
|
|
|
if (dohelp)
|
|
|
|
ifile = get_ifile(FAKE_HELPFILE, ifile);
|
|
|
|
while (argc-- > 0)
|
|
|
|
{
|
2002-01-07 20:37:09 +00:00
|
|
|
#if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Because the "shell" doesn't expand filename patterns,
|
|
|
|
* treat each argument as a filename pattern rather than
|
|
|
|
* a single filename.
|
|
|
|
* Expand the pattern and iterate over the expanded list.
|
|
|
|
*/
|
|
|
|
struct textlist tlist;
|
2017-11-20 06:53:49 +00:00
|
|
|
char *filename;
|
2000-05-22 09:53:22 +00:00
|
|
|
char *gfilename;
|
2017-11-20 06:53:49 +00:00
|
|
|
char *qfilename;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
gfilename = lglob(*argv++);
|
|
|
|
init_textlist(&tlist, gfilename);
|
|
|
|
filename = NULL;
|
|
|
|
while ((filename = forw_textlist(&tlist, filename)) != NULL)
|
2004-04-17 07:24:09 +00:00
|
|
|
{
|
2017-11-20 06:53:49 +00:00
|
|
|
qfilename = shell_unquote(filename);
|
|
|
|
(void) get_ifile(qfilename, ifile);
|
|
|
|
free(qfilename);
|
2004-04-17 07:24:09 +00:00
|
|
|
ifile = prev_ifile(NULL_IFILE);
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
free(gfilename);
|
|
|
|
#else
|
2017-11-20 06:53:49 +00:00
|
|
|
(void) get_ifile(*argv++, ifile);
|
2004-04-17 07:24:09 +00:00
|
|
|
ifile = prev_ifile(NULL_IFILE);
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Set up terminal, etc.
|
|
|
|
*/
|
|
|
|
if (!is_tty)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Output is not a tty.
|
|
|
|
* Just copy the input file(s) to output.
|
|
|
|
*/
|
|
|
|
SET_BINARY(1);
|
2019-06-29 04:39:01 +00:00
|
|
|
if (edit_first() == 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
cat_file();
|
|
|
|
} while (edit_next(1) == 0);
|
|
|
|
}
|
|
|
|
quit(QUIT_OK);
|
|
|
|
}
|
|
|
|
|
2007-09-04 13:54:09 +00:00
|
|
|
if (missing_cap && !know_dumb && !less_is_more)
|
2000-05-22 09:53:22 +00:00
|
|
|
error("WARNING: terminal is not fully functional", NULL_PARG);
|
|
|
|
open_getchr();
|
2004-04-17 07:24:09 +00:00
|
|
|
raw_mode(1);
|
2000-05-22 09:53:22 +00:00
|
|
|
init_signals(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Select the first file to examine.
|
|
|
|
*/
|
|
|
|
#if TAGS
|
2002-01-07 20:37:09 +00:00
|
|
|
if (tagoption != NULL || strcmp(tags, "-") == 0)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* A -t option was given.
|
|
|
|
* Verify that no filenames were also given.
|
|
|
|
* Edit the file selected by the "tags" search,
|
|
|
|
* and search for the proper line in the file.
|
|
|
|
*/
|
|
|
|
if (nifile() > 0)
|
|
|
|
{
|
|
|
|
error("No filenames allowed with -t option", NULL_PARG);
|
|
|
|
quit(QUIT_ERROR);
|
|
|
|
}
|
|
|
|
findtag(tagoption);
|
|
|
|
if (edit_tagfile()) /* Edit file which contains the tag */
|
|
|
|
quit(QUIT_ERROR);
|
|
|
|
/*
|
|
|
|
* Search for the line which contains the tag.
|
|
|
|
* Set up initial_scrpos so we display that line.
|
|
|
|
*/
|
|
|
|
initial_scrpos.pos = tagsearch();
|
|
|
|
if (initial_scrpos.pos == NULL_POSITION)
|
|
|
|
quit(QUIT_ERROR);
|
|
|
|
initial_scrpos.ln = jump_sline;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2019-06-29 04:39:01 +00:00
|
|
|
if (edit_first())
|
2000-05-22 09:53:22 +00:00
|
|
|
quit(QUIT_ERROR);
|
2019-06-29 04:39:01 +00:00
|
|
|
/*
|
|
|
|
* See if file fits on one screen to decide whether
|
|
|
|
* to send terminal init. But don't need this
|
|
|
|
* if -X (no_init) overrides this (see init()).
|
|
|
|
*/
|
2017-04-29 06:30:21 +00:00
|
|
|
if (quit_if_one_screen)
|
|
|
|
{
|
2019-06-29 04:39:01 +00:00
|
|
|
if (nifile() > 1) /* If more than one file, -F cannot be used */
|
2017-04-29 06:30:21 +00:00
|
|
|
quit_if_one_screen = FALSE;
|
2019-06-29 04:39:01 +00:00
|
|
|
else if (!no_init)
|
|
|
|
one_screen = get_one_screen();
|
2017-04-29 06:30:21 +00:00
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init();
|
|
|
|
commands();
|
|
|
|
quit(QUIT_OK);
|
|
|
|
/*NOTREACHED*/
|
2004-04-17 07:24:09 +00:00
|
|
|
return (0);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy a string to a "safe" place
|
|
|
|
* (that is, to a buffer allocated by calloc).
|
|
|
|
*/
|
|
|
|
public char *
|
2017-04-25 03:42:16 +00:00
|
|
|
save(s)
|
2017-04-29 06:30:21 +00:00
|
|
|
constant char *s;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-03-31 21:29:43 +00:00
|
|
|
char *p;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
p = (char *) ecalloc(strlen(s)+1, sizeof(char));
|
|
|
|
strcpy(p, s);
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate memory.
|
|
|
|
* Like calloc(), but never returns an error (NULL).
|
|
|
|
*/
|
|
|
|
public VOID_POINTER
|
2017-04-25 03:42:16 +00:00
|
|
|
ecalloc(count, size)
|
|
|
|
int count;
|
|
|
|
unsigned int size;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-03-31 21:29:43 +00:00
|
|
|
VOID_POINTER p;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
p = (VOID_POINTER) calloc(count, size);
|
|
|
|
if (p != NULL)
|
|
|
|
return (p);
|
|
|
|
error("Cannot allocate memory", NULL_PARG);
|
|
|
|
quit(QUIT_ERROR);
|
|
|
|
/*NOTREACHED*/
|
2004-04-17 07:24:09 +00:00
|
|
|
return (NULL);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skip leading spaces in a string.
|
|
|
|
*/
|
|
|
|
public char *
|
2017-04-25 03:42:16 +00:00
|
|
|
skipsp(s)
|
2017-04-29 06:30:21 +00:00
|
|
|
char *s;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
while (*s == ' ' || *s == '\t')
|
|
|
|
s++;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See how many characters of two strings are identical.
|
|
|
|
* If uppercase is true, the first string must begin with an uppercase
|
|
|
|
* character; the remainder of the first string may be either case.
|
|
|
|
*/
|
|
|
|
public int
|
2017-04-25 03:42:16 +00:00
|
|
|
sprefix(ps, s, uppercase)
|
|
|
|
char *ps;
|
|
|
|
char *s;
|
|
|
|
int uppercase;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2017-03-31 21:29:43 +00:00
|
|
|
int c;
|
|
|
|
int sc;
|
|
|
|
int len = 0;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
for ( ; *s != '\0'; s++, ps++)
|
|
|
|
{
|
|
|
|
c = *ps;
|
|
|
|
if (uppercase)
|
|
|
|
{
|
2006-08-20 15:50:51 +00:00
|
|
|
if (len == 0 && ASCII_IS_LOWER(c))
|
2000-05-22 09:53:22 +00:00
|
|
|
return (-1);
|
2006-08-20 15:50:51 +00:00
|
|
|
if (ASCII_IS_UPPER(c))
|
|
|
|
c = ASCII_TO_LOWER(c);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
sc = *s;
|
2006-08-20 15:50:51 +00:00
|
|
|
if (len > 0 && ASCII_IS_UPPER(sc))
|
|
|
|
sc = ASCII_TO_LOWER(sc);
|
2000-05-22 09:53:22 +00:00
|
|
|
if (c != sc)
|
|
|
|
break;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit the program.
|
|
|
|
*/
|
|
|
|
public void
|
2017-04-25 03:42:16 +00:00
|
|
|
quit(status)
|
|
|
|
int status;
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
static int save_status;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put cursor at bottom left corner, clear the line,
|
|
|
|
* reset the terminal modes, and exit.
|
|
|
|
*/
|
|
|
|
if (status < 0)
|
|
|
|
status = save_status;
|
|
|
|
else
|
|
|
|
save_status = status;
|
|
|
|
quitting = 1;
|
|
|
|
edit((char*)NULL);
|
2006-08-20 15:50:51 +00:00
|
|
|
save_cmdhist();
|
2000-05-22 09:53:22 +00:00
|
|
|
if (any_display && is_tty)
|
|
|
|
clear_bot();
|
|
|
|
deinit();
|
|
|
|
flush();
|
|
|
|
raw_mode(0);
|
|
|
|
#if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
|
|
|
|
/*
|
|
|
|
* If we don't close 2, we get some garbage from
|
|
|
|
* 2's buffer when it flushes automatically.
|
|
|
|
* I cannot track this one down RB
|
|
|
|
* The same bug shows up if we use ^C^C to abort.
|
|
|
|
*/
|
|
|
|
close(2);
|
2002-01-07 20:37:09 +00:00
|
|
|
#endif
|
2011-05-09 18:30:23 +00:00
|
|
|
#ifdef WIN32
|
2002-01-07 20:37:09 +00:00
|
|
|
SetConsoleTitle(consoleTitle);
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
|
|
|
close_getchr();
|
|
|
|
exit(status);
|
|
|
|
}
|