#include "less.h"
#if MSDOS_COMPILER==WIN32C
#include <windows.h>
#endif
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;
public int more_mode = 0;
#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
extern char * tags;
extern char * tagoption;
extern int jump_sline;
#endif
#ifdef WIN32
static char consoleTitle[256];
#endif
extern int missing_cap;
extern int know_dumb;
int
main(argc, argv)
int argc;
char *argv[];
{
IFILE ifile;
char *s;
int len;
#ifdef __EMX__
_response(&argc, &argv);
_wildcard(&argc, &argv);
#endif
progname = *argv++;
argc--;
secure = 0;
s = lgetenv("LESSSECURE");
if (s != NULL && *s != '\0')
secure = 1;
#ifdef WIN32
if (getenv("HOME") == NULL)
{
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);
}
}
GetConsoleTitle(consoleTitle, sizeof(consoleTitle)/sizeof(char));
#endif
len = strlen(progname);
if (len >= 4 && strcmp(&progname[len - 4], "more") == 0)
more_mode = 1;
is_tty = isatty(1);
get_term();
init_cmds();
init_prompt();
init_charset();
init_line();
init_option();
s = lgetenv("LESS");
if (more_mode) {
scan_option("-E");
scan_option("-m");
scan_option("-G");
scan_option("-f");
s = lgetenv("MORE");
} else {
s = lgetenv("LESS");
}
if (s != NULL)
scan_option(save(s));
#define isoptstring(s) (((s)[0] == '-' || (s)[0] == '+') && (s)[1] != '\0')
while (argc > 0 && (isoptstring(*argv) || isoptpending()))
{
s = *argv++;
argc--;
if (strcmp(s, "--") == 0)
break;
scan_option(s);
}
#undef isoptstring
if (isoptpending())
{
nopendopt();
quit(QUIT_OK);
}
#if EDITOR
editor = lgetenv("VISUAL");
if (editor == NULL || *editor == '\0')
{
editor = lgetenv("EDITOR");
if (editor == NULL || *editor == '\0')
editor = EDIT_PGM;
}
editproto = lgetenv("LESSEDIT");
if (editproto == NULL || *editproto == '\0')
editproto = "%E ?lm+%lm. %f";
#endif
ifile = NULL_IFILE;
if (dohelp)
ifile = get_ifile(FAKE_HELPFILE, ifile);
while (argc-- > 0)
{
char *filename;
#if (MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC)
struct textlist tlist;
char *gfilename;
gfilename = lglob(*argv++);
init_textlist(&tlist, gfilename);
filename = NULL;
while ((filename = forw_textlist(&tlist, filename)) != NULL)
{
(void) get_ifile(filename, ifile);
ifile = prev_ifile(NULL_IFILE);
}
free(gfilename);
#else
filename = shell_quote(*argv);
if (filename == NULL)
filename = *argv;
argv++;
(void) get_ifile(filename, ifile);
ifile = prev_ifile(NULL_IFILE);
#endif
}
if (!is_tty)
{
SET_BINARY(1);
if (nifile() == 0)
{
if (edit_stdin() == 0)
cat_file();
} else if (edit_first() == 0)
{
do {
cat_file();
} while (edit_next(1) == 0);
}
quit(QUIT_OK);
}
if (missing_cap && !know_dumb && !more_mode)
error("WARNING: terminal is not fully functional", NULL_PARG);
init_mark();
open_getchr();
raw_mode(1);
init_signals(1);
#if TAGS
if (tagoption != NULL || strcmp(tags, "-") == 0)
{
if (nifile() > 0)
{
error("No filenames allowed with -t option", NULL_PARG);
quit(QUIT_ERROR);
}
findtag(tagoption);
if (edit_tagfile())
quit(QUIT_ERROR);
initial_scrpos.pos = tagsearch();
if (initial_scrpos.pos == NULL_POSITION)
quit(QUIT_ERROR);
initial_scrpos.ln = jump_sline;
} else
#endif
if (nifile() == 0)
{
if (edit_stdin())
quit(QUIT_ERROR);
} else
{
if (edit_first())
quit(QUIT_ERROR);
}
init();
commands();
quit(QUIT_OK);
return (0);
}
public char *
save(s)
char *s;
{
register char *p;
p = (char *) ecalloc(strlen(s)+1, sizeof(char));
strcpy(p, s);
return (p);
}
public VOID_POINTER
ecalloc(count, size)
int count;
unsigned int size;
{
register VOID_POINTER p;
p = (VOID_POINTER) calloc(count, size);
if (p != NULL)
return (p);
error("Cannot allocate memory", NULL_PARG);
quit(QUIT_ERROR);
return (NULL);
}
public char *
skipsp(s)
register char *s;
{
while (*s == ' ' || *s == '\t')
s++;
return (s);
}
public int
sprefix(ps, s, uppercase)
char *ps;
char *s;
int uppercase;
{
register int c;
register int sc;
register int len = 0;
for ( ; *s != '\0'; s++, ps++)
{
c = *ps;
if (uppercase)
{
if (len == 0 && SIMPLE_IS_LOWER(c))
return (-1);
if (SIMPLE_IS_UPPER(c))
c = SIMPLE_TO_LOWER(c);
}
sc = *s;
if (len > 0 && SIMPLE_IS_UPPER(sc))
sc = SIMPLE_TO_LOWER(sc);
if (c != sc)
break;
len++;
}
return (len);
}
public void
quit(status)
int status;
{
static int save_status;
if (status < 0)
status = save_status;
else
save_status = status;
quitting = 1;
edit((char*)NULL);
if (any_display && is_tty)
clear_bot();
deinit();
flush();
raw_mode(0);
#if MSDOS_COMPILER && MSDOS_COMPILER != DJGPPC
close(2);
#endif
#if WIN32
SetConsoleTitle(consoleTitle);
#endif
close_getchr();
exit(status);
}