Wed, 05 Sep 2012 16:29:42 +0800, Thorsten Glaser <[email protected]>  wrote:

Roy dixit:

MSYS environment, program will return string in CRLF line ending
usually (even MinGW GCC returns so), without this patch, 0x0D will be

Right. Your patch however breaks binary-cleanliness, and mksh is
primarily a shell for operating environments that are BSD-ish enough
to support it, so this is nothing that will be changed in mksh. If
you script on/for MSYS, you either must set its stdin/stdout/stderr
to binary mode (in the callees - mksh uses Unix I/O instead of stdio
and such is not affected) or deal with this in your shell scripts,
e.g. x=$(foo | sed 's/\r$//') using a binary-safe sed.

But MSYS bash did it in same way. The following patch works only with CRLF, not any CR. I leave the patch here so that someone who want to use mksh as MSYS shell can be helped.

Index: eval.c
===================================================================
RCS file: /cvs/src/bin/mksh/eval.c,v
retrieving revision 1.128
diff -u -r1.128 eval.c
--- eval.c      24 Aug 2012 21:16:06 -0000      1.128
+++ eval.c      5 Sep 2012 11:16:26 -0000
@@ -202,6 +202,7 @@
     int f)             /* DO* flags */
 {
        int c = 0;
+       int d = 0;
        int type;               /* expansion type */
        int quote = 0;          /* quoted */
        XString ds;             /* destination string */
@@ -844,10 +845,17 @@
                                c = '\n';
                                --newlines;
                        } else {
-                               while ((c = shf_getc(x.u.shf)) == 0 || c == 
'\n')
+                               while ((c = shf_getc(x.u.shf)) == 0 || c == 
'\n' || c == '\r') {
                                        if (c == '\n')
                                                /* Save newlines */
                                                newlines++;
+#ifdef __MSYS__
+                                       if (c == '\n' && d == '\r')
+                                               /* Save newlines */
+                                               newlines++;
+                                       d = c;
+#endif
+                               }
                                if (newlines && c != EOF) {
                                        shf_ungetc(c, x.u.shf);
                                        c = '\n';

Reply via email to