Skip to content

Commit

Permalink
Add double-pipe syntax in LESSOPEN to use exit status.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwsw committed May 20, 2012
1 parent 2b0c815 commit 37a6c97
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
2 changes: 2 additions & 0 deletions ch.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ ch_length()
return (NULL_POSITION);
if (ch_flags & CH_HELPFILE)
return (size_helpdata);
if (ch_flags & CH_NODATA)
return (0);
return (ch_fsize);
}

Expand Down
4 changes: 4 additions & 0 deletions edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ edit_ifile(ifile)
*/
__djgpp_set_ctrl_c(1);
#endif
} else if (strcmp(open_filename, FAKE_EMPTYFILE) == 0)
{
f = -1;
chflags |= CH_NODATA;
} else if (strcmp(open_filename, FAKE_HELPFILE) == 0)
{
f = -1;
Expand Down
17 changes: 13 additions & 4 deletions filename.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ open_altfile(filename, pf, pfd)
ch_ungetchar(-1);
if ((lessopen = lgetenv("LESSOPEN")) == NULL)
return (NULL);
if (*lessopen == '|')
while (*lessopen == '|')
{
/*
* If LESSOPEN starts with a |, it indicates
Expand All @@ -843,7 +843,7 @@ open_altfile(filename, pf, pfd)
return (NULL);
#else
lessopen++;
returnfd = 1;
returnfd++;
#endif
}
if (*lessopen == '-') {
Expand Down Expand Up @@ -883,9 +883,18 @@ open_altfile(filename, pf, pfd)
if (read(f, &c, 1) != 1)
{
/*
* Pipe is empty. This means there is no alt file.
* Pipe is empty.
* If more than 1 pipe char was specified,
* the exit status tells whether the file itself
* is empty, or if there is no alt file.
* If only one pipe char, just assume no alt file.
*/
pclose(fd);
int status = pclose(fd);
if (returnfd > 1 && status == 0) {
*pfd = NULL;
*pf = -1;
return (save(FAKE_EMPTYFILE));
}
return (NULL);
}
ch_ungetchar(c);
Expand Down
3 changes: 3 additions & 0 deletions less.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,10 +485,13 @@ struct textlist
#define CH_KEEPOPEN 002
#define CH_POPENED 004
#define CH_HELPFILE 010
#define CH_NODATA 020 /* Special case for zero length files */


#define ch_zero() ((POSITION)0)

#define FAKE_HELPFILE "@/\\less/\\help/\\file/\\@"
#define FAKE_EMPTYFILE "@/\\less/\\empty/\\file/\\@"

/* Flags for cvt_text */
#define CVT_TO_LC 01 /* Convert upper-case to lower-case */
Expand Down
22 changes: 21 additions & 1 deletion less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -1161,14 +1161,33 @@ lesspipe.sh:
case "$1" in
.br
*.Z) uncompress \-c $1 2>/dev/null
.br
*) exit 1
.br
;;
.br
esac
.br
exit $?
.br
.PP
To use this script, put it where it can be executed and set
LESSOPEN="|lesspipe.sh %s".
.PP
Note that a preprocessor cannot output an empty file, since that
is interpreted as meaning there is no replacement, and
the original file is used.
To avoid this, if LESSOPEN starts with two vertical bars,
the exit status of the script becomes meaningful.
If the exit status is zero, the output is considered to be
replacement text, even if it empty.
If the exit status is nonzero, any output is ignored and the
original file is used.
For compatibility with previous versions of
.I less,
if LESSOPEN starts with only one vertical bar, the exit status
of the preprocessor is ignored.
.PP
When an input pipe is used, a LESSCLOSE postprocessor can be used,
but it is usually not necessary since there is no replacement file
to clean up.
Expand All @@ -1187,7 +1206,8 @@ the preprocessor command.
If standard input is being viewed, the input preprocessor is passed
a file name consisting of a single dash.
Similarly, if the first two characters of LESSOPEN are vertical bar and dash
(|\-), the input pipe is used on standard input as well as other files.
(|\-) or two vertical bars and a dash (||\-),
the input pipe is used on standard input as well as other files.
Again, in this case the dash is not considered to be part of
the input pipe command.

Expand Down

0 comments on commit 37a6c97

Please sign in to comment.