Skip to content

Commit

Permalink
Add --follow-name option.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed Nov 14, 2007
1 parent c3acfac commit 08b9008
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 21 deletions.
3 changes: 3 additions & 0 deletions NEWS.VER
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

Major changes between "less" versions 409 and @@VERSION@@

* New --follow-name option makes F command follow the name of a file
rather than the file descriptor if an open file is renamed.

* Make searching with -i/-I work correctly with non-ASCII text.

* Fix DJGPP build.
Expand Down
30 changes: 30 additions & 0 deletions ch.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
#include <windows.h>
#endif

#if HAVE_STAT_INO
#include <sys/stat.h>
extern dev_t curr_ino;
extern ino_t curr_dev;
#endif

typedef POSITION BLOCKNUM;

public int ignore_eoi;
Expand Down Expand Up @@ -98,6 +104,8 @@ static int maxbufs = -1;
extern int autobuf;
extern int sigs;
extern int secure;
extern int screen_trashed;
extern int follow_mode;
extern constant char helpdata[];
extern constant int size_helpdata;
extern IFILE curr_ifile;
Expand Down Expand Up @@ -276,6 +284,28 @@ fch_get()
#endif
#endif
slept = TRUE;

#if HAVE_STAT_INO
if (follow_mode == FOLLOW_NAME)
{
struct stat st;
int r = stat(get_filename(curr_ifile), &st);
long diff = 0;
if (r == 0)
{
diff = st.st_ino - curr_ino;
if (diff == 0)
diff = st.st_dev - curr_dev;
}
if (diff)
{
/* screen_trashed=2 makes make_display
* reopen the file. */
screen_trashed = 2;
return (EOI);
}
}
#endif
}
if (sigs)
return (EOI);
Expand Down
29 changes: 20 additions & 9 deletions command.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,21 @@ mca_char(c)
return (MCA_MORE);
}

/*
* Discard any buffered file data.
*/
static void
clear_buffers()
{
if (!(ch_getflags() & CH_CANSEEK))
return;
ch_flush();
clr_linenum();
#if HILITE_SEARCH
clr_hilite();
#endif
}

/*
* Make sure the screen is displayed.
*/
Expand All @@ -577,6 +592,8 @@ make_display()
int save_top_scroll;
save_top_scroll = top_scroll;
top_scroll = 1;
if (screen_trashed == 2)
reopen_curr_ifile();
repaint();
top_scroll = save_top_scroll;
}
Expand Down Expand Up @@ -1148,14 +1165,7 @@ commands()
* Flush buffers, then repaint screen.
* Don't flush the buffers on a pipe!
*/
if (ch_getflags() & CH_CANSEEK)
{
ch_flush();
clr_linenum();
#if HILITE_SEARCH
clr_hilite();
#endif
}
clear_buffers();
/* FALLTHRU */
case A_REPAINT:
/*
Expand Down Expand Up @@ -1257,7 +1267,8 @@ commands()
/*
* Define abbreviation for a commonly used sequence below.
*/
#define DO_SEARCH() if (number <= 0) number = 1; \
#define DO_SEARCH() \
if (number <= 0) number = 1; \
mca_search(); \
cmd_exec(); \
multi_search((char *)NULL, (int) number);
Expand Down
7 changes: 7 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ AH_TEMPLATE([HAVE_VOID],
[Define HAVE_VOID if your compiler supports the "void" type.])
AH_TEMPLATE([HAVE_CONST],
[Define HAVE_CONST if your compiler supports the "const" modifier.])
AH_TEMPLATE([HAVE_STAT_INO],
[Define HAVE_STAT_INO if your struct stat has st_ino and st_dev.])
AH_TEMPLATE([HAVE_TIME_T],
[Define HAVE_TIME_T if your system supports the "time_t" type.])
AH_TEMPLATE([HAVE_STRERROR],
Expand Down Expand Up @@ -241,6 +243,11 @@ AC_TRY_COMPILE(, [const int foo = 0;],
AC_MSG_CHECKING(for time_t)
AC_TRY_COMPILE([#include <time.h>], [time_t t = 0;],
[AC_MSG_RESULT(yes); AC_DEFINE(HAVE_TIME_T)], [AC_MSG_RESULT(no)])
AC_MSG_CHECKING(for st_ino in struct stat)
AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/stat.h>],
[struct stat s; dev_t dev = s.st_dev; ino_t ino = s.st_ino;],
[AC_MSG_RESULT(yes); AC_DEFINE(HAVE_STAT_INO)], [AC_MSG_RESULT(no)])

# Checks for library functions.
AC_TYPE_SIGNAL
Expand Down
33 changes: 32 additions & 1 deletion edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@


#include "less.h"
#if HAVE_STAT
#include <sys/stat.h>
#endif

public int fd0 = 0;

Expand All @@ -36,6 +39,11 @@ extern int force_logfile;
extern char *namelogfile;
#endif

#if HAVE_STAT_INO
public dev_t curr_ino;
public ino_t curr_dev;
#endif

char *curr_altfilename = NULL;
static void *curr_altpipe;

Expand Down Expand Up @@ -178,6 +186,9 @@ close_file()
curr_altfilename = NULL;
}
curr_ifile = NULL_IFILE;
#if HAVE_STAT_INO
curr_ino = curr_dev = 0;
#endif
}

/*
Expand Down Expand Up @@ -360,7 +371,6 @@ edit_ifile(ifile)
}
}
}
free(qopen_filename);

/*
* Get the new ifile.
Expand All @@ -384,11 +394,24 @@ edit_ifile(ifile)
#if LOGFILE
if (namelogfile != NULL && is_tty)
use_logfile(namelogfile);
#endif
#if HAVE_STAT_INO
{
struct stat statbuf;
int r = stat(qopen_filename, &statbuf);
if (r == 0)
{
curr_ino = statbuf.st_ino;
curr_dev = statbuf.st_dev;
}
else abort();
}
#endif
if (every_first_cmd != NULL)
ungetsc(every_first_cmd);
}

free(qopen_filename);
no_display = !any_display;
flush();
any_display = TRUE;
Expand Down Expand Up @@ -657,6 +680,14 @@ reedit_ifile(save_ifile)
quit(QUIT_ERROR);
}

public void
reopen_curr_ifile()
{
IFILE save_ifile = save_curr_ifile();
close_file();
reedit_ifile(save_ifile);
}

/*
* Edit standard input.
*/
Expand Down
3 changes: 3 additions & 0 deletions less.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,9 @@ struct textlist
#define QUIT_ERROR 1
#define QUIT_SAVED_STATUS (-1)

#define FOLLOW_DESC 0
#define FOLLOW_NAME 1

/* filestate flags */
#define CH_CANSEEK 001
#define CH_KEEPOPEN 002
Expand Down
22 changes: 17 additions & 5 deletions less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -799,11 +799,6 @@ Disables sending the termcap initialization and deinitialization strings
to the terminal.
This is sometimes desirable if the deinitialization string does
something unnecessary, like clearing the screen.
.IP "\-\-no-keypad"
Disables sending the keypad initialization and deinitialization strings
to the terminal.
This is sometimes useful if the keypad strings make the numeric
keypad behave in an undesirable manner.
.IP "\-y\fIn\fP or \-\-max-forw-scroll=\fIn\fP"
Specifies a maximum number of lines to scroll forward.
If it is necessary to scroll forward more than \fIn\fP lines,
Expand Down Expand Up @@ -847,6 +842,23 @@ Specifies the default number of positions to scroll horizontally
in the RIGHTARROW and LEFTARROW commands.
If the number specified is zero, it sets the default number of
positions to one half of the screen width.
.IP "\-\-no-keypad"
Disables sending the keypad initialization and deinitialization strings
to the terminal.
This is sometimes useful if the keypad strings make the numeric
keypad behave in an undesirable manner.
.IP "\-\-follow-name"
Normally, if the input file is renamed while an F command is executing,
.I less
will continue to display the contents of the original file despite
its name change.
If \-\-follow-name is specified, during an F command
.I less
will periodically attempt to reopen the file by name.
If the reopen succeeds, which means that a new file has been created
with the same name as the original (now renamed) file,
.I less
will display the contents of that new file.
.IP \-\-
A command line argument of "\-\-" marks the end of option arguments.
Any arguments following this are interpreted as filenames.
Expand Down
10 changes: 6 additions & 4 deletions line.c
Original file line number Diff line number Diff line change
Expand Up @@ -1046,16 +1046,18 @@ pdone(endline)
linebuf[curr] = '\n';
attr[curr] = AT_NORMAL;
curr++;
} else if (ignaw && !auto_wrap && column >= sc_width)
}
else if (ignaw && !auto_wrap && column >= sc_width)
{
/*
* Big horrible kludge.
* No-wrap terminals are too hard to deal with when they get in
* the state where a full screen width of characters have been
* output but the cursor is sitting on the right edge instead
* of at the start of the next line. So after we output a
* full line, we output an extra space and backspace to force
* the cursor to the next line.
* of at the start of the next line.
* So after we output a full line, we output an extra
* space and backspace to force the cursor to the
* beginning of the next line, like a sane terminal.
*/
linebuf[curr] = ' ';
attr[curr++] = AT_NORMAL;
Expand Down
12 changes: 11 additions & 1 deletion opttbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public int shift_count; /* Number of positions to shift horizontally */
public int status_col; /* Display a status column */
public int use_lessopen; /* Use the LESSOPEN filter */
public int quit_on_intr; /* Quit on interrupt */
public int oldbot; /* Old bottom of screen behavior */
public int follow_mode; /* F cmd Follows file desc or file name? */
public int oldbot; /* Old bottom of screen behavior {{REMOVE}} */
#if HILITE_SEARCH
public int hilite_search; /* Highlight matched search patterns? */
#endif
Expand Down Expand Up @@ -113,6 +114,7 @@ static struct optname query_optname = { "help", NULL };
static struct optname pound_optname = { "shift", NULL };
static struct optname keypad_optname = { "no-keypad", NULL };
static struct optname oldbot_optname = { "old-bot", NULL };
static struct optname follow_optname = { "follow-name", NULL };


/*
Expand Down Expand Up @@ -440,6 +442,14 @@ static struct loption option[] =
NULL
}
},
{ '.', &follow_optname,
BOOL, FOLLOW_DESC, &follow_mode, NULL,
{
"F command Follows file descriptor",
"F command Follows file name",
NULL
}
},
{ '\0', NULL, NOVAR, 0, NULL, NULL, { NULL, NULL, NULL } }
};

Expand Down
3 changes: 2 additions & 1 deletion version.c
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ v411 11/6/07 Fix case-insensitive searching with non-ASCII text.
v412 11/6/07 Use symbolic SEEK constants.
v413 11/6/07 Fix search highlight bug with non-ASCII text.
v414 11/6/07 Fix display bug with no-wrap terminals.
v415 11/14/07 Add --follow-name option.
*/

char version[] = "414";
char version[] = "415";

0 comments on commit 08b9008

Please sign in to comment.