Deleted Added
full compact
2c2
< * Copyright (C) 1984-2000 Mark Nudelman
---
> * Copyright (C) 1984-2002 Mark Nudelman
52a53
> extern int use_lessopen;
64c65
< unquote_file(str)
---
> shell_unquote(str)
67d67
< #if SPACES_IN_FILENAMES
71,77c71,96
< if (*str != openquote)
< return (save(str));
< name = (char *) ecalloc(strlen(str), sizeof(char));
< strcpy(name, str+1);
< p = name + strlen(name) - 1;
< if (*p == closequote)
< *p = '\0';
---
> name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
> if (*str == openquote)
> {
> str++;
> while (*str != '\0')
> {
> if (*str == closequote)
> {
> if (str[1] != closequote)
> break;
> str++;
> }
> *p++ = *str++;
> }
> } else
> {
> char *esc = get_meta_escape();
> int esclen = strlen(esc);
> while (*str != '\0')
> {
> if (esclen > 0 && strncmp(str, esc, esclen) == 0)
> str += esclen;
> *p++ = *str++;
> }
> }
> *p = '\0';
79,81d97
< #else
< return (save(str));
< #endif
84a101,218
> * Get the shell's escape character.
> */
> public char *
> get_meta_escape()
> {
> char *s;
>
> s = lgetenv("LESSMETAESCAPE");
> if (s == NULL)
> s = DEF_METAESCAPE;
> return (s);
> }
>
> /*
> * Get the characters which the shell considers to be "metacharacters".
> */
> static char *
> metachars()
> {
> static char *mchars = NULL;
>
> if (mchars == NULL)
> {
> mchars = lgetenv("LESSMETACHARS");
> if (mchars == NULL)
> mchars = DEF_METACHARS;
> }
> return (mchars);
> }
>
> /*
> * Is this a shell metacharacter?
> */
> static int
> metachar(c)
> char c;
> {
> return (strchr(metachars(), c) != NULL);
> }
>
> /*
> * Insert a backslash before each metacharacter in a string.
> */
> public char *
> shell_quote(s)
> char *s;
> {
> char *p;
> char *newstr;
> int len;
> char *esc = get_meta_escape();
> int esclen = strlen(esc);
> int use_quotes = 0;
> int have_quotes = 0;
>
> /*
> * Determine how big a string we need to allocate.
> */
> len = 1; /* Trailing null byte */
> for (p = s; *p != '\0'; p++)
> {
> len++;
> if (*p == openquote || *p == closequote)
> have_quotes = 1;
> if (metachar(*p))
> {
> if (esclen == 0)
> {
> /*
> * We've got a metachar, but this shell
> * doesn't support escape chars. Use quotes.
> */
> use_quotes = 1;
> } else
> {
> /*
> * Allow space for the escape char.
> */
> len += esclen;
> }
> }
> }
> if (use_quotes)
> {
> if (have_quotes)
> /*
> * We can't quote a string that contains quotes.
> */
> return (NULL);
> len = strlen(s) + 3;
> }
> /*
> * Allocate and construct the new string.
> */
> newstr = p = (char *) ecalloc(len, sizeof(char));
> if (use_quotes)
> {
> sprintf(newstr, "%c%s%c", openquote, s, closequote);
> } else
> {
> while (*s != '\0')
> {
> if (metachar(*s))
> {
> /*
> * Add the escape char.
> */
> strcpy(p, esc);
> p += esclen;
> }
> *p++ = *s++;
> }
> *p = '\0';
> }
> return (newstr);
> }
>
> /*
110c244
< qpathname = unquote_file(pathname);
---
> qpathname = shell_unquote(pathname);
276a411
> char *qs;
306c441,442
< s = lglob(fpat);
---
> qs = lglob(fpat);
> s = shell_unquote(qs);
312,313c448,449
< free(s);
< s = NULL;
---
> free(qs);
> qs = NULL;
314a451
> free(s);
316c453
< return (s);
---
> return (qs);
401d537
< #if HAVE_SHELL
403,409d538
< /*
< * Get the shell's escape character.
< */
< static char *
< get_meta_escape()
< {
< char *s;
411,504d539
< s = lgetenv("LESSMETAESCAPE");
< if (s == NULL)
< s = DEF_METAESCAPE;
< return (s);
< }
<
< /*
< * Is this a shell metacharacter?
< */
< static int
< metachar(c)
< char c;
< {
< static char *metachars = NULL;
<
< if (metachars == NULL)
< {
< metachars = lgetenv("LESSMETACHARS");
< if (metachars == NULL)
< metachars = DEF_METACHARS;
< }
< return (strchr(metachars, c) != NULL);
< }
<
< /*
< * Insert a backslash before each metacharacter in a string.
< */
< public char *
< esc_metachars(s)
< char *s;
< {
< char *p;
< char *newstr;
< int len;
< char *esc;
< int esclen;
<
< /*
< * Determine how big a string we need to allocate.
< */
< esc = get_meta_escape();
< esclen = strlen(esc);
< len = 1; /* Trailing null byte */
< for (p = s; *p != '\0'; p++)
< {
< len++;
< if (metachar(*p))
< {
< if (*esc == '\0')
< {
< /*
< * We've got a metachar, but this shell
< * doesn't support escape chars. Give up.
< */
< return (NULL);
< }
< /*
< * Allow space for the escape char.
< */
< len += esclen;
< }
< }
< /*
< * Allocate and construct the new string.
< */
< newstr = p = (char *) ecalloc(len, sizeof(char));
< while (*s != '\0')
< {
< if (metachar(*s))
< {
< /*
< * Add the escape char.
< */
< strcpy(p, esc);
< p += esclen;
< }
< *p++ = *s++;
< }
< *p = '\0';
< return (newstr);
< }
<
< #else /* HAVE_SHELL */
<
< public char *
< esc_metachars(s)
< char *s;
< {
< return (save(s));
< }
<
< #endif /* HAVE_SHELL */
<
<
529,532c564,565
< * Try to escape any metacharacters in the command.
< * If we can't do that, just put the command in quotes.
< * (But that doesn't work well if the command itself
< * contains quotes.)
---
> * Read the output of <$SHELL -c cmd>.
> * Escape any metacharacters in the command.
534c567,568
< if ((esccmd = esc_metachars(cmd)) == NULL)
---
> esccmd = shell_quote(cmd);
> if (esccmd == NULL)
536,542c570
< /*
< * Cannot escape the metacharacters, so use quotes.
< * Read the output of <$SHELL -c "cmd">.
< */
< scmd = (char *) ecalloc(strlen(shell) + strlen(cmd) + 7,
< sizeof(char));
< sprintf(scmd, "%s -c \"%s\"", shell, cmd);
---
> fd = popen(cmd, "r");
545,548d572
< /*
< * Read the output of <$SHELL -c cmd>.
< * No quotes; use the escaped cmd.
< */
551c575
< sprintf(scmd, "%s -c %s", shell, esccmd);
---
> sprintf(scmd, "%s %s %s", shell, shell_coption(), esccmd);
552a577,578
> fd = popen(scmd, "r");
> free(scmd);
554,555d579
< fd = popen(scmd, "r");
< free(scmd);
560,564d583
< /*
< * Redirection in `popen' might have messed with the
< * standard devices. Restore binary input mode.
< */
< SET_BINARY(0);
565a585,589
> /*
> * Redirection in `popen' might have messed with the
> * standard devices. Restore binary input mode.
> */
> SET_BINARY(0);
585c609
< filename = unquote_file(ofilename);
---
> filename = shell_unquote(ofilename);
593a618
> char *qfilename;
606,610c631,636
< length += strlen(p) + 1;
< #if SPACES_IN_FILENAMES
< if (strchr(p, ' ') != NULL)
< length += 2; /* Allow for quotes */
< #endif
---
> qfilename = shell_quote(p);
> if (qfilename != NULL)
> {
> length += strlen(qfilename) + 1;
> free(qfilename);
> }
616,622c642,647
< #if SPACES_IN_FILENAMES
< if (strchr(p, ' ') != NULL)
< sprintf(gfilename + strlen(gfilename), "%c%s%c ",
< openquote, p, closequote);
< else
< #endif
< sprintf(gfilename + strlen(gfilename), "%s ", p);
---
> qfilename = shell_quote(p);
> if (qfilename != NULL)
> {
> sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
> free(qfilename);
> }
640,642c665,666
< #if SPACES_IN_FILENAMES
< register int spaces_in_file;
< #endif
---
> char *pathname;
> char *qpathname;
658,661c682,686
< #if SPACES_IN_FILENAMES
< spaces_in_file = 0;
< if (strchr(fnd.GLOB_NAME, ' ') != NULL ||
< strchr(filename, ' ') != NULL)
---
> pathname = (char *) ecalloc(n, sizeof(char));
> sprintf(pathname, "%s%s%s", drive, dir, fnd.GLOB_NAME);
> qpathname = shell_quote(pathname);
> free(pathname);
> if (qpathname != NULL)
663,664c688,706
< spaces_in_file = 1;
< n += 2;
---
> n = strlen(qpathname);
> while (p - gfilename + n + 2 >= len)
> {
> /*
> * No room in current buffer.
> * Allocate a bigger one.
> */
> len *= 2;
> *p = '\0';
> p = (char *) ecalloc(len, sizeof(char));
> strcpy(p, gfilename);
> free(gfilename);
> gfilename = p;
> p = gfilename + strlen(gfilename);
> }
> strcpy(p, qpathname);
> free(qpathname);
> p += n;
> *p++ = ' ';
666,687d707
< #endif
< while (p - gfilename + n+2 >= len)
< {
< /*
< * No room in current buffer. Allocate a bigger one.
< */
< len *= 2;
< *p = '\0';
< p = (char *) ecalloc(len, sizeof(char));
< strcpy(p, gfilename);
< free(gfilename);
< gfilename = p;
< p = gfilename + strlen(gfilename);
< }
< #if SPACES_IN_FILENAMES
< if (spaces_in_file)
< sprintf(p, "%c%s%s%s%c ", openquote,
< drive, dir, fnd.GLOB_NAME, closequote);
< else
< #endif
< sprintf(p, "%s%s%s ", drive, dir, fnd.GLOB_NAME);
< p += n;
706a727
> char *esc;
708,712c729,733
< lessecho = lgetenv("LESSECHO");
< if (lessecho == NULL || *lessecho == '\0')
< lessecho = "lessecho";
< s = esc_metachars(filename);
< if (s == NULL)
---
> esc = get_meta_escape();
> if (strlen(esc) == 0)
> esc = "-";
> esc = shell_quote(esc);
> if (esc == NULL)
714,719d734
< /*
< * There may be dangerous metachars in this name.
< * We can't risk passing it to the shell.
< * {{ For example, do "!;TAB" when the first file
< * in the dir is named "rm". }}
< */
722a738,740
> lessecho = lgetenv("LESSECHO");
> if (lessecho == NULL || *lessecho == '\0')
> lessecho = "lessecho";
726,728c744,749
< cmd = (char *) ecalloc(strlen(lessecho) + strlen(s) + 24, sizeof(char));
< sprintf(cmd, "%s -p0x%x -d0x%x -- %s",
< lessecho, openquote, closequote, s);
---
> cmd = (char *) ecalloc(strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24, sizeof(char));
> sprintf(cmd, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
> free(esc);
> for (s = metachars(); *s != '\0'; s++)
> sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
> sprintf(cmd + strlen(cmd), "-- %s", ofilename);
730d750
< free(s);
777d796
< char *gfilename;
784c803
< if (secure)
---
> if (!use_lessopen || secure)
806,814c825
< gfilename = esc_metachars(filename);
< if (gfilename == NULL)
< {
< /*
< * Cannot escape metacharacters.
< */
< return (NULL);
< }
< cmd = (char *) ecalloc(strlen(lessopen) + strlen(gfilename) + 2,
---
> cmd = (char *) ecalloc(strlen(lessopen) + strlen(filename) + 2,
816c827
< sprintf(cmd, lessopen, gfilename);
---
> sprintf(cmd, lessopen, filename);
818d828
< free(gfilename);
853c863
< gfilename = readfd(fd);
---
> cmd = readfd(fd);
855c865
< if (*gfilename == '\0')
---
> if (*cmd == '\0')
860c870
< return (gfilename);
---
> return (cmd);
875,876d884
< char *gfilename;
< char *galtfilename;
895,908c903,905
< gfilename = esc_metachars(filename);
< if (gfilename == NULL)
< {
< return;
< }
< galtfilename = esc_metachars(altfilename);
< if (galtfilename == NULL)
< {
< free(gfilename);
< return;
< }
< cmd = (char *) ecalloc(strlen(lessclose) + strlen(gfilename) +
< strlen(galtfilename) + 2, sizeof(char));
< sprintf(cmd, lessclose, gfilename, galtfilename);
---
> cmd = (char *) ecalloc(strlen(lessclose) + strlen(filename) +
> strlen(altfilename) + 2, sizeof(char));
> sprintf(cmd, lessclose, filename, altfilename);
910,911d906
< free(galtfilename);
< free(gfilename);
927c922
< filename = unquote_file(filename);
---
> filename = shell_unquote(filename);
963c958
< filename = unquote_file(filename);
---
> filename = shell_unquote(filename);
966c961
< static char is_dir[] = " is a directory";
---
> static char is_a_dir[] = " is a directory";
968c963
< m = (char *) ecalloc(strlen(filename) + sizeof(is_dir),
---
> m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
971c966
< strcat(m, is_dir);
---
> strcat(m, is_a_dir);
1022a1018,1025
> /*
> *
> */
> public char *
> shell_coption()
> {
> return ("-c");
> }