#include "less.h"
public int fd0 = 0;
extern int new_file;
extern int errmsgs;
extern int cbufs;
extern char *every_first_cmd;
extern int any_display;
extern int force_open;
extern int is_tty;
extern int sigs;
extern IFILE curr_ifile;
extern IFILE old_ifile;
extern struct scrpos initial_scrpos;
extern void constant *ml_examine;
#if SPACES_IN_FILENAMES
extern char openquote;
extern char closequote;
#endif
#if LOGFILE
extern int logfile;
extern int force_logfile;
extern char *namelogfile;
#endif
char *curr_altfilename = NULL;
static void *curr_altpipe;
public void
init_textlist(tlist, str)
struct textlist *tlist;
char *str;
{
char *s;
#if SPACES_IN_FILENAMES
int quoted = 0;
#endif
tlist->string = skipsp(str);
tlist->endstring = tlist->string + strlen(tlist->string);
for (s = str; s < tlist->endstring; s++)
{
#if SPACES_IN_FILENAMES
if (*s == ' ' && !quoted)
*s = '\0';
if (!quoted && *s == openquote)
quoted = 1;
else if (quoted && *s == closequote)
quoted = 0;
#else
if (*s == ' ')
*s = '\0';
#endif
}
}
public char *
forw_textlist(tlist, prev)
struct textlist *tlist;
char *prev;
{
char *s;
if (prev == NULL)
s = tlist->string;
else
s = prev + strlen(prev);
if (s >= tlist->endstring)
return (NULL);
while (*s == '\0')
s++;
if (s >= tlist->endstring)
return (NULL);
return (s);
}
public char *
back_textlist(tlist, prev)
struct textlist *tlist;
char *prev;
{
char *s;
if (prev == NULL)
s = tlist->endstring;
else if (prev <= tlist->string)
return (NULL);
else
s = prev - 1;
while (*s == '\0')
s--;
if (s <= tlist->string)
return (NULL);
while (s[-1] != '\0' && s > tlist->string)
s--;
return (s);
}
static void
close_file()
{
struct scrpos scrpos;
char *filename;
if (curr_ifile == NULL_IFILE)
return;
get_scrpos(&scrpos);
if (scrpos.pos != NULL_POSITION)
{
store_pos(curr_ifile, &scrpos);
lastmark();
}
ch_close();
if (curr_altfilename != NULL)
{
filename = unquote_file(get_filename(curr_ifile));
close_altfile(curr_altfilename, filename, curr_altpipe);
free(filename);
free(curr_altfilename);
curr_altfilename = NULL;
}
curr_ifile = NULL_IFILE;
}
public int
edit(filename)
char *filename;
{
if (filename == NULL)
return (edit_ifile(NULL_IFILE));
return (edit_ifile(get_ifile(filename, curr_ifile)));
}
public int
edit_ifile(ifile)
IFILE ifile;
{
int f;
int answer;
int no_display;
int chflags;
char *filename;
char *open_filename;
char *alt_filename;
void *alt_pipe;
IFILE was_curr_ifile;
PARG parg;
if (ifile == curr_ifile)
{
return (0);
}
#if LOGFILE
end_logfile();
#endif
was_curr_ifile = save_curr_ifile();
if (curr_ifile != NULL_IFILE)
{
chflags = ch_getflags();
close_file();
if ((chflags & CH_HELPFILE) && held_ifile(was_curr_ifile) <= 1)
{
del_ifile(was_curr_ifile);
was_curr_ifile = old_ifile;
}
}
if (ifile == NULL_IFILE)
{
unsave_ifile(was_curr_ifile);
return (0);
}
filename = unquote_file(get_filename(ifile));
alt_pipe = NULL;
alt_filename = open_altfile(filename, &f, &alt_pipe);
open_filename = (alt_filename != NULL) ? alt_filename : filename;
chflags = 0;
if (alt_pipe != NULL)
{
chflags |= CH_POPENED;
} else if (strcmp(open_filename, "-") == 0)
{
f = fd0;
chflags |= CH_KEEPOPEN;
SET_BINARY(f);
#if MSDOS_COMPILER==DJGPPC
__djgpp_set_ctrl_c(1);
#endif
} else if (strcmp(open_filename, FAKE_HELPFILE) == 0)
{
f = -1;
chflags |= CH_HELPFILE;
} else if ((parg.p_string = bad_file(open_filename)) != NULL)
{
error("%s", &parg);
free(parg.p_string);
err1:
if (alt_filename != NULL)
{
close_altfile(alt_filename, filename, alt_pipe);
free(alt_filename);
}
del_ifile(ifile);
free(filename);
reedit_ifile(was_curr_ifile);
return (1);
} else if ((f = open(open_filename, OPEN_READ)) < 0)
{
parg.p_string = errno_message(filename);
error("%s", &parg);
free(parg.p_string);
goto err1;
} else
{
chflags |= CH_CANSEEK;
if (!force_open && !opened(ifile) && bin_file(f))
{
parg.p_string = filename;
answer = query("\"%s\" may be a binary file. See it anyway? ",
&parg);
if (answer != 'y' && answer != 'Y')
{
close(f);
goto err1;
}
}
}
if (was_curr_ifile != NULL_IFILE)
{
old_ifile = was_curr_ifile;
unsave_ifile(was_curr_ifile);
}
curr_ifile = ifile;
curr_altfilename = alt_filename;
curr_altpipe = alt_pipe;
set_open(curr_ifile);
get_pos(curr_ifile, &initial_scrpos);
new_file = TRUE;
ch_init(f, chflags);
if (!(chflags & CH_HELPFILE))
{
#if LOGFILE
if (namelogfile != NULL && is_tty)
use_logfile(namelogfile);
#endif
if (every_first_cmd != NULL)
ungetsc(every_first_cmd);
}
no_display = !any_display;
flush();
any_display = TRUE;
if (is_tty)
{
pos_clear();
clr_linenum();
#if HILITE_SEARCH
clr_hilite();
#endif
cmd_addhist(ml_examine, filename);
if (no_display && errmsgs > 0)
{
parg.p_string = filename;
error("%s", &parg);
}
}
free(filename);
return (0);
}
public int
edit_list(filelist)
char *filelist;
{
IFILE save_ifile;
char *good_filename;
char *filename;
char *gfilelist;
char *gfilename;
struct textlist tl_files;
struct textlist tl_gfiles;
save_ifile = save_curr_ifile();
good_filename = NULL;
init_textlist(&tl_files, filelist);
filename = NULL;
while ((filename = forw_textlist(&tl_files, filename)) != NULL)
{
gfilelist = lglob(filename);
init_textlist(&tl_gfiles, gfilelist);
gfilename = NULL;
while ((gfilename = forw_textlist(&tl_gfiles, gfilename)) != NULL)
{
if (edit(gfilename) == 0 && good_filename == NULL)
good_filename = get_filename(curr_ifile);
}
free(gfilelist);
}
if (good_filename == NULL)
{
unsave_ifile(save_ifile);
return (1);
}
if (get_ifile(good_filename, curr_ifile) == curr_ifile)
{
unsave_ifile(save_ifile);
return (0);
}
reedit_ifile(save_ifile);
return (edit(good_filename));
}
public int
edit_first()
{
curr_ifile = NULL_IFILE;
return (edit_next(1));
}
public int
edit_last()
{
curr_ifile = NULL_IFILE;
return (edit_prev(1));
}
static int
edit_istep(h, n, dir)
IFILE h;
int n;
int dir;
{
IFILE next;
for (;;)
{
next = (dir > 0) ? next_ifile(h) : prev_ifile(h);
if (--n < 0)
{
if (edit_ifile(h) == 0)
break;
}
if (next == NULL_IFILE)
{
return (1);
}
if (ABORT_SIGS())
{
return (1);
}
h = next;
}
return (0);
}
static int
edit_inext(h, n)
IFILE h;
int n;
{
return (edit_istep(h, n, 1));
}
public int
edit_next(n)
int n;
{
return edit_istep(curr_ifile, n, 1);
}
static int
edit_iprev(h, n)
IFILE h;
int n;
{
return (edit_istep(h, n, -1));
}
public int
edit_prev(n)
int n;
{
return edit_istep(curr_ifile, n, -1);
}
public int
edit_index(n)
int n;
{
IFILE h;
h = NULL_IFILE;
do
{
if ((h = next_ifile(h)) == NULL_IFILE)
{
return (1);
}
} while (get_index(h) != n);
return (edit_ifile(h));
}
public IFILE
save_curr_ifile()
{
if (curr_ifile != NULL_IFILE)
hold_ifile(curr_ifile, 1);
return (curr_ifile);
}
public void
unsave_ifile(save_ifile)
IFILE save_ifile;
{
if (save_ifile != NULL_IFILE)
hold_ifile(save_ifile, -1);
}
public void
reedit_ifile(save_ifile)
IFILE save_ifile;
{
IFILE next;
IFILE prev;
unsave_ifile(save_ifile);
next = next_ifile(save_ifile);
prev = prev_ifile(save_ifile);
if (edit_ifile(save_ifile) == 0)
return;
if (next != NULL_IFILE && edit_inext(next, 0) == 0)
return;
if (prev != NULL_IFILE && edit_iprev(prev, 0) == 0)
return;
quit(QUIT_ERROR);
}
public int
edit_stdin()
{
if (isatty(fd0))
{
error("Missing filename (\"less --help\" for help)", NULL_PARG);
quit(QUIT_OK);
}
return (edit("-"));
}
public void
cat_file()
{
register int c;
while ((c = ch_forw_get()) != EOI)
putchr(c);
flush();
}
#if LOGFILE
public void
use_logfile(filename)
char *filename;
{
register int exists;
register int answer;
PARG parg;
if (ch_getflags() & CH_CANSEEK)
return;
filename = unquote_file(filename);
exists = open(filename, OPEN_READ);
close(exists);
exists = (exists >= 0);
if (!exists || force_logfile)
{
answer = 'O';
} else
{
parg.p_string = filename;
answer = query("Warning: \"%s\" exists; Overwrite, Append or Don't log? ", &parg);
}
loop:
switch (answer)
{
case 'O': case 'o':
logfile = creat(filename, 0644);
break;
case 'A': case 'a':
logfile = open(filename, OPEN_APPEND);
if (lseek(logfile, (off_t)0, 2) == BAD_LSEEK)
{
close(logfile);
logfile = -1;
}
break;
case 'D': case 'd':
free(filename);
return;
case 'q':
quit(QUIT_OK);
default:
answer = query("Overwrite, Append, or Don't log? (Type \"O\", \"A\", \"D\" or \"q\") ", NULL_PARG);
goto loop;
}
if (logfile < 0)
{
parg.p_string = filename;
error("Cannot write to \"%s\"", &parg);
free(filename);
return;
}
free(filename);
SET_BINARY(logfile);
}
#endif