Skip to content

Commit

Permalink
Change prompt expansion of %o to be the plain OSC8 URI,
Browse files Browse the repository at this point in the history
not the shell-escaped version. Shell escaping is unnecessary
since the LESS_OSC8_* script can just quote it manually.
  • Loading branch information
gwsw committed Jan 20, 2024
1 parent ca8f0f6 commit 5255418
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
8 changes: 4 additions & 4 deletions decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ static unsigned char edittable[] =
static unsigned char dflt_vartable[] =
{
'L','E','S','S','_','O','S','C','8','_','m','a','n', 0, EV_OK|A_EXTRA,
/* echo %o | sed "s,^man\:\\([^(]*\\)( *\\([^)]*\\)\.*,-man '\\2' '\\1'," */
'e','c','h','o',' ','%','o',' ','|',' ','s','e','d',' ','"','s',',','^','m','a','n','\\',':','\\','\\','(','[','^','(',']','*','\\','\\',')','(',' ','*','\\','\\','(','[','^',')',']','*','\\','\\',')','\\','.','*',',','-','m','a','n',' ','\'','\\','\\','2','\'',' ','\'','\\','\\','1','\'',',','"',
/* echo '%o' | sed "s,^man\:\\([^(]*\\)( *\\([^)]*\\)\.*,-man '\\2' '\\1'," */
'e','c','h','o',' ','\'','%','o','\'',' ','|',' ','s','e','d',' ','"','s',',','^','m','a','n','\\',':','\\','\\','(','[','^','(',']','*','\\','\\',')','(',' ','*','\\','\\','(','[','^',')',']','*','\\','\\',')','\\','.','*',',','-','m','a','n',' ','\'','\\','\\','2','\'',' ','\'','\\','\\','1','\'',',','"',
0,

'L','E','S','S','_','O','S','C','8','_','f','i','l','e', 0, EV_OK|A_EXTRA,
/* eval `echo %o | sed "s,^file\://\\([^/]*\\)\\(\.*\\),_H=\\1;_P=\\2,"`; if [ -z "$_H" -o "$_H" = localhost -o "$_H" = $HOSTNAME ]; then echo "\:e $_P"; else echo -echo Cannot open remote file on "$_H"; fi */
'e','v','a','l',' ','`','e','c','h','o',' ','%','o',' ','|',' ','s','e','d',' ','"','s',',','^','f','i','l','e','\\',':','/','/','\\','\\','(','[','^','/',']','*','\\','\\',')','\\','\\','(','\\','.','*','\\','\\',')',',','_','H','=','\\','\\','1',';','_','P','=','\\','\\','2',',','"','`',';',' ','i','f',' ','[',' ','-','z',' ','"','$','_','H','"',' ','-','o',' ','"','$','_','H','"',' ','=',' ','l','o','c','a','l','h','o','s','t',' ','-','o',' ','"','$','_','H','"',' ','=',' ','$','H','O','S','T','N','A','M','E',' ',']',';',' ','t','h','e','n',' ','e','c','h','o',' ','"','\\',':','e',' ','$','_','P','"',';',' ','e','l','s','e',' ','e','c','h','o',' ','-','e','c','h','o',' ','C','a','n','n','o','t',' ','o','p','e','n',' ','r','e','m','o','t','e',' ','f','i','l','e',' ','o','n',' ','"','$','_','H','"',';','f','i',
/* echo "$o" | sed -e "s,^man:\\([^(]*\\)( *\\([^)]*\\).*,-man '\\2' '\\1'," -e"t X" -e"s,.*,-echo Invalid man link," -e": X" */
'e','c','h','o',' ','"','$','o','"',' ','|',' ','s','e','d',' ','-','e',' ','"','s',',','^','m','a','n','\\',':','\\','\\','(','[','^','(',']','*','\\','\\',')','(',' ','*','\\','\\','(','[','^',')',']','*','\\','\\',')','\\','.','*',',','-','m','a','n',' ','\'','\\','\\','2','\'',' ','\'','\\','\\','1','\'',',','"',' ','-','e','"','t',' ','X','"',' ','-','e','"','s',',','\\','.','*',',','-','e','c','h','o',' ','I','n','v','a','l','i','d',' ','m','a','n',' ','l','i','n','k',',','"',' ','-','e','"','\\',':',' ','X','"',
0,
};

Expand Down
14 changes: 5 additions & 9 deletions less.nro.VER
Original file line number Diff line number Diff line change
Expand Up @@ -521,25 +521,21 @@ In a simple case where the opener accepts the complete URI
as a command line parameter, the handler may be as simple as
.nf
.sp
echo mybrowser %o
echo mybrowser '%o'
.sp
.fi
In other cases, the URI may need to be modified, so the handler
may have to do some manipulation of the %o value.
.PP
If the LESS_OSC8_xxx variable is not set, links using that scheme
cannot be opened.
However, there are default handlers for the schemes "man" and "file",
However, there are default handlers for the
schemes "man" (used when LESS_OSC_man is not set)
and "file" (used when LESS_OSC_file is not set),
which should work on systems which provide the
.BR sed (1)
command and a shell with syntax compatible with the Bourne shell
.BR sh (1).
These default handlers are used if LESS_OSC8_man or LESS_OSC8_file are not set.
As an example handler, this is the default value for "LESS_OSC8_man":
.nf
.sp
echo\ %o\ |\ sed\ "s,\(haman\e:\e\e([\(ha(]*\e\e)(\ *\e\e([\(ha)]*\e\e)\e.*,-man\ '\e\e2'\ '\e\e1',"
.fi
.PP
The URI passed to an OSC8 handler via %o is guaranteed not to contain any single quote
or double quote characters, but it may contain any other shell metacharacters
Expand Down Expand Up @@ -2053,7 +2049,7 @@ Replaced by the line number of the last line in the input file.
.IP "%m"
Replaced by the total number of input files.
.IP "%o"
Replaced by the shell-escaped URI of the currently selected OSC8 hyperlink,
Replaced by the URI of the currently selected OSC8 hyperlink,
or a question mark if no hyperlink is selected.
This is used by OSC8 handlers as explained in the \(haO\(haO command description.
.IP "%p\fIX\fP"
Expand Down
14 changes: 9 additions & 5 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,19 @@ int main(int argc, constant char *argv[])
* Copy a string to a "safe" place
* (that is, to a buffer allocated by calloc).
*/
public char * save(constant char *s)
public char * saven(constant char *s, size_t n)
{
char *p;

p = (char *) ecalloc(strlen(s)+1, sizeof(char));
strcpy(p, s);
char *p = (char *) ecalloc(n+1, sizeof(char));
strncpy(p, s, n);
p[n] = '\0';
return (p);
}

public char * save(constant char *s)
{
return saven(s, strlen(s));
}

public void out_of_memory(void)
{
error("Cannot allocate memory", NULL_PARG);
Expand Down
8 changes: 2 additions & 6 deletions search.c
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,6 @@ static lbool osc8_search_line1(int search_type, POSITION linepos, POSITION spos,
struct osc8_parse_info op1;
struct osc8_parse_info op2;
constant char *linep;
size_t uri_len;
constant size_t min_osc8_size = 6; /* "\e]8;;\7" */

if (search_type & SRCH_FORW)
Expand Down Expand Up @@ -1415,10 +1414,7 @@ static lbool osc8_search_line1(int search_type, POSITION linepos, POSITION spos,
osc8_text_end = spos + ptr_diff(op2.osc8_start, line);

/* Save URI for message in prompt(). */
uri_len = ptr_diff(op1.uri_end, op1.uri_start);
osc8_uri = ecalloc(uri_len+1, sizeof(char));
strncpy(osc8_uri, op1.uri_start, uri_len);
osc8_uri[uri_len] = '\0';
osc8_uri = saven(op1.uri_start, ptr_diff(op1.uri_end, op1.uri_start));
return TRUE;
}

Expand Down Expand Up @@ -1889,7 +1885,7 @@ public void osc8_open(void)
return;
}
/* {{ ugly global osc8_path }} */
osc8_path = shell_quoten(op.uri_start, uri_len);
osc8_path = saven(op.uri_start, uri_len);
hf = popen(pr_expand(handler), "r");
free(osc8_path);
osc8_path = NULL;
Expand Down

0 comments on commit 5255418

Please sign in to comment.