-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix backslash and ~(...) processing of patterns from split fields
*** Problem 1: The following bug shows up in pathname expansion, in every ksh93 version: $ mkdir testdir $ cd testdir $ touch a b c ./- # GNU touch won't accept - as file name $ p='[a\-c]' $ echo $p a b c As the - in the bracket expression is backslash-escaped, it should not be treated as a bracket expression range operator, so the expected output is: - a c Analysis: For compatibility with the Bourne/POSIX/ksh88, extended patterns (with a syntax involving parentheses) are not expanded in fields resulting from field splitting, e.g., from unquoted variable expansions. The shell adds an extra level of backslash-escaping to fields resulting from field splitting, so that characters that indicate extended pattern syntax can be disabled by escaping them. Of course, the backslash itself also acquires an extra backslash, and that is the cause of the bug above. The fix is to remove one layer of backslash-escaping from patterns resulting from field splitting before expanding them. But we cannot do this unconditionally; we also need a new flag to check if the extra escaping is disallowing an extended pattern. See below. [v1.1] Additionally, this commit adds a --globex shell option that allows globbing of fields resulting from field splitting to ex-pand ex-tended patterns, achieving consistent behaviour at the expense of full backward compatibility. It is off by default. *** Problem 2: The fix for unwanted brace expansion of ~(E)... patterns and the like was incomplete; it only checked for ~( at the beginning of the pattern, but it may occur anywhere in the pattern. src/cmd/ksh93/data/options.c, src/cmd/ksh93/include/shell.h: - [v1.1] Add new --globex (SH_GLOBEX) shell option. src/cmd/ksh93/sh/expand.c: - path_expand(): - Add a new 'musttrim' paraneter. - If that parameter is nonzero, trim the pattern before expanding by copying it into sh.strbuf and calling sh_trim() on it. - Replace the unexpanded result by the original untrimmed pattern if it wasn't expanded; this avoids regressions with unquoted variables and comsubs. - must_disallow_bracepat(): - Add new 'withbackslash' parameter to handle escaped patterns. - Don't bother checking for initial ~ as this will now be called by path_generate() on encountering a ~ anywhere in the pattern. - Return -1 if there is no ( or if the option's don't indicate a change in the brace expansion compatibility state. - path_generate(): - Add 'musttrim' parameter to pass on to path_expand() and must_disallow_bracepat(). - While scanning the pattern, when encountering '~' and we're not already in a brace expansion, call must_disallow_bracepat() there instead of at the start, so that the 'nobracepat' state is kept up to date throughout the scan. This fixes problem 2. src/cmd/ksh93/sh/macro.c: - Add two new fields to the Mac_t struct: 1. noextpat: set to disallow extended patterns; this is set in addition to backslash-escaping & | ( ). 2. wasexpan: set when varsub() is called (except in the 'nosub:' case) or when comsubst() is called; this should cover all the expansions that may result in field splitting. - mac_copy(): When escaping to disallow extended patterns, also set noextpat unless the --globex shell option is on. - end_field(): Introduce a 'musttrim' flag that is set if wasexpan is set (it was a variable or comsub expansion), there is no disallowed extended pattern, and the string contains a backslash. Pass this flag on to path_expand() or path_generate(). Along with the changes in expand.c, this fixes Problem 1. Resolves: #549
- Loading branch information
Showing
9 changed files
with
170 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.