Bug 104529: on OS/2, when writing to the command-line console, we must NSPRPUB_PRE_4_2_CLIENT_BRANCH
authorwtc%netscape.com
Fri, 18 Apr 2003 14:00:56 +0000
branchNSPRPUB_PRE_4_2_CLIENT_BRANCH
changeset 2809 d2338c1c3993a147676812bbace11abf32af5d6f
parent 2808 074c4313ccfd35dc686ba99e991a2cbe1b0451ec
child 2811 c324f1ef596e1a00de1e5c9fc40361d405708008
push idunknown
push userunknown
push dateunknown
bugs104529
Bug 104529: on OS/2, when writing to the command-line console, we must write \r\n to go to a new line. So we need to translate \n to \r\n. The patch is contributed by Julien Pierre. Tag: NSPRPUB_PRE_4_2_CLIENT_BRANCH
pr/src/io/prstdio.c
--- a/pr/src/io/prstdio.c
+++ b/pr/src/io/prstdio.c
@@ -51,12 +51,50 @@ PR_IMPLEMENT(PRUint32) PR_fprintf(PRFile
 }
 
 PR_IMPLEMENT(PRUint32) PR_vfprintf(PRFileDesc* fd, const char *fmt, va_list ap)
 {
     /* XXX this could be better */
     PRUint32 rv, len;
     char* msg = PR_vsmprintf(fmt, ap);
     len = strlen(msg);
+#ifdef XP_OS2
+    /*
+     * OS/2 really needs a \r for every \n.
+     * In the future we should try to use scatter-gather instead of a
+     * succession of PR_Write.
+     */
+    if (isatty(PR_FileDesc2NativeHandle(fd))) {
+        PRUint32 last = 0, idx;
+        PRInt32 tmp;
+        rv = 0;
+        for (idx = 0; idx < len+1; idx++) {
+            if ((idx - last > 0) && (('\n' == msg[idx]) || (idx == len))) {
+                tmp = PR_Write(fd, msg + last, idx - last);
+                if (tmp >= 0) {
+                    rv += tmp;
+                }
+                last = idx;
+            }
+            /*
+             * if current character is \n, and
+             * previous character isn't \r, and
+             * next character isn't \r
+             */
+            if (('\n' == msg[idx]) &&
+                ((0 == idx) || ('\r' != msg[idx-1])) &&
+                ('\r' != msg[idx+1])) {
+                /* add extra \r */
+                tmp = PR_Write(fd, "\r", 1);
+                if (tmp >= 0) {
+                    rv += tmp;
+                }
+            }
+        }
+    } else {
+        rv = PR_Write(fd, msg, len);
+    }
+#else
     rv = PR_Write(fd, msg, len);
+#endif
     PR_DELETE(msg);
     return rv;
 }