Skip to content

Commit 426a896

Browse files
committed
bash: Fix CR handling
Fixes #1839 `0005-bash-4.3-msys2-fix-lineendings.patch` adds CRLF support. However, `0001-bash-4.4-cygwin.patch` already added `igncr` option to Bash to support CRLF. I confirmed that the Cygwin version of Bash has also the same issue with #1839 when the `igncr` option is set. After debugging, I found that there is an issue in `rewind_input_string()` in `parser.y` that it doesn't take the CR into account. This PR adds the following changes: * Modify `rewind_input_string()` to take the CR into account. (It might be better to apply a similar change to the Cygwin version of Bash.) * Remove all the changes from `y.tab.c`. This file should be automatically generated from `parser.y`. * Set `LC_ALL=C.UTF-8` when running `make check` for running on non-English locales. * Add two tests: `run-ps1lf` and `run-crlf`. Note: This patch contains a line with CRLF. So, .gitattributes is also updated to keep the CRLF.
1 parent 7c7d154 commit 426a896

File tree

3 files changed

+79
-34
lines changed

3 files changed

+79
-34
lines changed

bash/.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*-bash-*-msys2-fix-lineendings.patch -text

bash/0005-bash-4.3-msys2-fix-lineendings.patch

Lines changed: 75 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ index c1135ec..b388af6 100644
8181
*niflp = invfl;
8282
if (vlp)
8383
diff --git a/parse.y b/parse.y
84-
index 0ae3458..c6e9520 100644
84+
index 8fd24a1c..232a33dc 100644
8585
--- a/parse.y
8686
+++ b/parse.y
8787
@@ -1459,7 +1459,13 @@ yy_input_name ()
@@ -99,6 +99,23 @@ index 0ae3458..c6e9520 100644
9999
}
100100

101101
/* Call this to unget C. That is, to make C the next character
102+
@@ -1684,7 +1690,15 @@ rewind_input_string ()
103+
into account, e.g., $(...\n) */
104+
xchars = shell_input_line_len - shell_input_line_index;
105+
if (bash_input.location.string[-1] == '\n')
106+
- xchars++;
107+
+ {
108+
+ xchars++;
109+
+#ifdef __MSYS__
110+
+ {
111+
+ if (bash_input.location.string[-2] == '\r')
112+
+ xchars++;
113+
+ }
114+
+#endif
115+
+ }
116+
117+
/* XXX - how to reflect bash_input.location.string back to string passed to
118+
parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how
102119
diff --git a/shell.c b/shell.c
103120
index ee9d445..8f25726 100644
104121
--- a/shell.c
@@ -156,6 +173,63 @@ index 2001b4e..3ba2029 100644
156173
}
157174
else
158175
break;
176+
diff --git a/tests/crlf.right b/tests/crlf.right
177+
new file mode 100644
178+
index 0000000..d7fd195
179+
--- /dev/null
180+
+++ b/tests/crlf.right
181+
@@ -0,0 +1,8 @@
182+
+Line with LF
183+
+Line with CR
184+
+Line with CRLF
185+
+Line with
186+
+ LF
187+
+Line with CR
188+
+Line with
189+
+ CRLF
190+
diff --git a/tests/crlf.tests b/tests/crlf.tests
191+
new file mode 100644
192+
index 0000000..b2c4da7
193+
--- /dev/null
194+
+++ b/tests/crlf.tests
195+
@@ -0,0 +1,6 @@
196+
+echo $(echo -e "Line with\n LF")
197+
+echo $(echo -e "Line with\r CR")
198+
+echo $(echo -e "Line with\r\n CRLF")
199+
+echo "$(echo -e "Line with\n LF")"
200+
+echo "$(echo -e "Line with\r CR")"
201+
+echo "$(echo -e "Line with\r\n CRLF")"
202+
diff --git a/tests/ps1lf.right b/tests/ps1lf.right
203+
new file mode 100644
204+
index 0000000..ee83131
205+
--- /dev/null
206+
+++ b/tests/ps1lf.right
207+
@@ -0,0 +1,2 @@
208+
+foo
209+
+$ exit
210+
diff --git a/tests/ps1lf.tests b/tests/ps1lf.tests
211+
new file mode 100644
212+
index 0000000..01a2265
213+
--- /dev/null
214+
+++ b/tests/ps1lf.tests
215+
@@ -0,0 +1 @@
216+
+PS1='$(echo foo)\n\$ '
217+
diff --git a/tests/run-crlf b/tests/run-crlf
218+
new file mode 100644
219+
index 0000000..3f6037c
220+
--- /dev/null
221+
+++ b/tests/run-crlf
222+
@@ -0,0 +1,2 @@
223+
+${THIS_SH} ./crlf.tests > ${BASH_TSTOUT}
224+
+diff ${BASH_TSTOUT} crlf.right && rm -f ${BASH_TSTOUT}
225+
diff --git a/tests/run-ps1lf b/tests/run-ps1lf
226+
new file mode 100644
227+
index 0000000..1617108
228+
--- /dev/null
229+
+++ b/tests/run-ps1lf
230+
@@ -0,0 +1,2 @@
231+
+${THIS_SH} --rcfile ./ps1lf.tests -i < /dev/null 2>&1 | tr -d '\r' > ${BASH_TSTOUT}
232+
+diff ${BASH_TSTOUT} ps1lf.right && rm -f ${BASH_TSTOUT}
159233
diff --git a/variables.c b/variables.c
160234
index 028667c..a10594d 100644
161235
--- a/variables.c
@@ -181,33 +255,3 @@ index 028667c..a10594d 100644
181255
if (shell_variables == 0)
182256
create_variable_tables ();
183257

184-
diff --git a/y.tab.c b/y.tab.c
185-
index 32b4c7c..ac70820 100644
186-
--- a/y.tab.c
187-
+++ b/y.tab.c
188-
@@ -3770,7 +3770,13 @@ yy_input_name ()
189-
static int
190-
yy_getc ()
191-
{
192-
- return (*(bash_input.getter)) ();
193-
+#ifdef __MSYS__
194-
+ int c;
195-
+ while ((c = (*(bash_input.getter)) ()) == '\r');
196-
+ return c;
197-
+#else
198-
+ return (*(bash_input.getter)) ();
199-
+#endif
200-
}
201-
202-
/* Call this to unget C. That is, to make C the next character
203-
@@ -4746,6 +4752,10 @@ shell_getc (remove_quoted_newline)
204-
else
205-
RESIZE_MALLOCED_BUFFER (shell_input_line, i, 2, shell_input_line_size, 256);
206-
207-
+#ifdef __MSYS__
208-
+ if (c == '\r')
209-
+ continue;
210-
+#endif
211-
if (c == EOF)
212-
{
213-
if (bash_input.type == st_stream)

bash/PKGBUILD

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pkgname=('bash' 'bash-devel')
66
_basever=5.2
77
_patchlevel=037 #prepare for some patches
88
pkgver=${_basever}.${_patchlevel}
9-
pkgrel=2
9+
pkgrel=3
1010
pkgdesc="The GNU Bourne Again shell"
1111
arch=('i686' 'x86_64')
1212
license=('GPL')
@@ -82,7 +82,7 @@ build() {
8282

8383
check() {
8484
cd ${srcdir}/${pkgname}-$_basever
85-
make check
85+
LC_ALL=C.UTF-8 make check
8686
}
8787

8888
package_bash() {
@@ -129,7 +129,7 @@ sha256sums=('a139c166df7ff4471c5e0733051642ee5556c1cc8a4a78f145583c5c81ab32fb'
129129
'SKIP'
130130
'948b8b5401dcb4e5eb577cfa6543e740e2e3bd0690939d8e77d078d75d110097'
131131
'16584e119db9418030912171f89aecae319858ecd357d3e56c95eba83667dae7'
132-
'c55c24110fbe90a2000411239e6399c1baed2843a61220b4e8a7a036f4a7436a'
132+
'b598a3dcfab16eb2bd0cee4228a7f0041fc6f10892e5ffc9da1ae1c2e1d7570e'
133133
'500c75c64593a70276585345a55c807226c0cc220d08b7cccece2ab005b3bcea'
134134
'cbae1aa81d56eba4e916bdaf2b2983731d6e2537dd8d606a3b378e49bcb81e79'
135135
'f42f2fee923bc2209f406a1892772121c467f44533bedfe00a176139da5d310a'

0 commit comments

Comments
 (0)