Fixed joiner detection, allowing it to work with timeseal
authorEric Mullins <emwine@earthlink.net>
Thu, 15 Oct 2009 13:18:42 +0000 (07:18 -0600)
committerEric Mullins <emwine@earthlink.net>
Thu, 15 Oct 2009 13:18:42 +0000 (07:18 -0600)
When timeseal's buffer fills in the middle of the continuation
sequence, detection fails, and the lines aren't joined.

As a result of this fix, measures intended to bypass joining are
no longer needed, so I removed them.  These utilize server vars
when available to prevent splitting.

backend.c [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 9e1bf62..0bbe876
--- a/backend.c
+++ b/backend.c
@@ -2022,7 +2022,8 @@ static int loggedOn = FALSE;
 int gs_gamenum;
 char gs_kind[MSG_SIZ];
 static char player1Name[128] = "";
 int gs_gamenum;
 char gs_kind[MSG_SIZ];
 static char player1Name[128] = "";
-static char player2Name[128] = "";
+static char player2Name[128] = "";\r
+static char cont_seq[] = "\n\\   ";
 static int player1Rating = -1;
 static int player2Rating = -1;
 /*----------------------------*/
 static int player1Rating = -1;
 static int player2Rating = -1;
 /*----------------------------*/
@@ -2054,7 +2055,9 @@ read_from_ics(isr, closure, data, count, error)
     static char buf[BUF_SIZE + 1];
     static int firstTime = TRUE, intfSet = FALSE;
     static ColorClass prevColor = ColorNormal;
     static char buf[BUF_SIZE + 1];
     static int firstTime = TRUE, intfSet = FALSE;
     static ColorClass prevColor = ColorNormal;
-    static int savingComment = FALSE;
+    static int savingComment = FALSE;\r
+       static int cmatch = 0; // continuation sequence match\r
+       char *bp;
     char str[500];
     int i, oldi;
     int buf_len;
     char str[500];
     int i, oldi;
     int buf_len;
@@ -2085,19 +2088,53 @@ read_from_ics(isr, closure, data, count, error)
              buf[i] = buf[leftover_start + i];
        }
 
              buf[i] = buf[leftover_start + i];
        }
 
-       /* Copy in new characters, removing nulls and \r's */
-       buf_len = leftover_len;
-       for (i = 0; i < count; i++) {
-           if (data[i] != NULLCHAR && data[i] != '\r')
-             buf[buf_len++] = data[i];
-           if(!appData.noJoin && buf_len >= 5 && buf[buf_len-5]=='\n' && buf[buf_len-4]=='\\' && 
-                               buf[buf_len-3]==' '  && buf[buf_len-2]==' '  && buf[buf_len-1]==' ') {
-               buf_len -= 5; // [HGM] ICS: join continuation line of Lasker 2.2.3 server with previous
-               if(buf_len == 0 || buf[buf_len-1] != ' ')
-                  buf[buf_len++] = ' '; // add space (assumes ICS does not break lines within word)
-           }
-       }
-
+    /* copy new characters into the buffer */\r
+    bp = buf + leftover_len;\r
+    buf_len=leftover_len;\r
+    for (i=0; i<count; i++)\r
+    {\r
+        // ignore these\r
+        if (data[i] == '\r')\r
+            continue;\r
+\r
+        // join lines split by ICS?\r
+        if (!appData.noJoin)\r
+        {\r
+            /*\r
+                Joining just consists of finding matches against the\r
+                continuation sequence, and discarding that sequence\r
+                if found instead of copying it.  So, until a match\r
+                fails, there's nothing to do since it might be the\r
+                complete sequence, and thus, something we don't want\r
+                copied.\r
+            */\r
+            if (data[i] == cont_seq[cmatch])\r
+            {\r
+                cmatch++;\r
+                if (cmatch == strlen(cont_seq))\r
+                    cmatch = 0; // complete match.  just reset the counter\r
+                continue;\r
+            }\r
+            else if (cmatch)\r
+            {\r
+                /*\r
+                    match failed, so we have to copy what matched before\r
+                    falling through and copying this character.  In reality,\r
+                    this will only ever be just the newline character, but\r
+                    it doesn't hurt to be precise.\r
+                */\r
+                strncpy(bp, cont_seq, cmatch);\r
+                bp += cmatch;\r
+                buf_len += cmatch;\r
+                cmatch = 0;\r
+            }\r
+        }\r
+\r
+        // copy this char\r
+        *bp++ = data[i];\r
+        buf_len++;\r
+    }\r
+\r
        buf[buf_len] = NULLCHAR;
        next_out = leftover_len;
        leftover_start = 0;
        buf[buf_len] = NULLCHAR;
        next_out = leftover_len;
        leftover_start = 0;
@@ -2224,8 +2261,6 @@ read_from_ics(isr, closure, data, count, error)
                  sprintf(str,
                          "/set-quietly interface %s\n/set-quietly style 12\n",
                          programVersion);
                  sprintf(str,
                          "/set-quietly interface %s\n/set-quietly style 12\n",
                          programVersion);
-          if (!appData.noJoin)
-              strcat(str, "/set-quietly wrap 0\n");
                } else if (ics_type == ICS_CHESSNET) {
                  sprintf(str, "/style 12\n");
                } else {
                } else if (ics_type == ICS_CHESSNET) {
                  sprintf(str, "/style 12\n");
                } else {
@@ -2235,8 +2270,6 @@ read_from_ics(isr, closure, data, count, error)
 #ifdef WIN32
                  strcat(str, "$iset nohighlight 1\n");
 #endif
 #ifdef WIN32
                  strcat(str, "$iset nohighlight 1\n");
 #endif
-          if (!appData.noJoin)
-              strcat(str, "$iset nowrap 1\n");
                  strcat(str, "$iset lock 1\n$style 12\n");
                }
                SendToICS(str);
                  strcat(str, "$iset lock 1\n$style 12\n");
                }
                SendToICS(str);