There was this problem when using fricas(sman) in a pipe,
the output may get truncated at beginning or end.

I used a workaround before, but now I have find the root cause
and a proper fix.

First, "expose-bug.patch" in the attachment reverts the workaround
and expose the bug during the book building.

The root cause is that, the first prompt is send from "FRICASsys"
to "session", skipping "sman", causing a race.

If "session" gets "(1) -> \0" from "FRICASsys", then gets
"a very long string" from "sman", then the socket may send them
together, so "spadclient" gets this:

    "(1) -> \0a very long string"

When printed by "fputs", those after '\0' is truncated.

Now, with my patch, the initial prompt is sent from "FRICASsys"
to terminal, then to "sman", then to "session", then to "spadclient".

- Qian

--
You received this message because you are subscribed to the Google Groups "FriCAS - 
computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fricas-devel+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/fricas-devel/d0dfca2b-fa68-42fc-97a2-bc917bdfde6a%40gmail.com.
diff --git a/src/interp/server.boot b/src/interp/server.boot
index 899730d2..13603341 100644
--- a/src/interp/server.boot
+++ b/src/interp/server.boot
@@ -94,8 +94,8 @@ serverReadLine(stream) ==
       sockSendInt($SessionManager, $CreateFrameAnswer)
       sockSendInt($SessionManager, $frameNumber)
       $frameNumber := $frameNumber + 1
---  MRX I'm not sure whether I should call ioHook("startPrompt")/ioHook("endOfPrompt") here
-      sockSendString($SessionManager, MKPROMPT())
+      princPrompt()
+      FORCE_-OUTPUT()
     action = $SwitchFrames =>
       $currentFrameNum := sockGetInt($SessionManager)
       currentFrame := LASSOC($currentFrameNum, $frameAlist)
diff --git a/src/sman/session.c b/src/sman/session.c
index bfba94f9..6a29ea37 100644
--- a/src/sman/session.c
+++ b/src/sman/session.c
@@ -344,11 +344,6 @@ accept_session_connection(Sock *server_sock)
       }
       plSock->Socket.frame = get_int(spad_server);
       active_session = (Sock *)plSock;
-      get_string_buf(spad_server, big_bad_buf, BufSize);
-      ret_code = swrite((Sock *)plSock, big_bad_buf, strlen(big_bad_buf)+1,
-                        "session: writing to InterpWindow");
-      if (ret_code == -1)
-        return -1;
       num_active_clients++;
 #ifdef DEBUG
 pr();
diff --git a/src/doc/Makefile.in b/src/doc/Makefile.in
index a223013d..31975493 100644
--- a/src/doc/Makefile.in
+++ b/src/doc/Makefile.in
@@ -638,7 +638,7 @@ ${HTEX_FILES_INPUT}: tmp/%.input: ${htexsrcdir}/%.htex
 
 HTEX_FILES_SPOOL = ${patsubst %, tmp/%.spool, ${HTEX_FILES}}
 ${HTEX_FILES_SPOOL}: tmp/%.spool: tmp/%.input
-	echo ")read $<" | FRICAS_INITFILE='' ${INTERPSYS} > $@
+	echo ")read $<" | FRICAS_INITFILE='' FRICAS=${FRICAS} ${FRICAS}/bin/fricas -noht -noclef > $@
 
 HTEX_FILES_TEX = ${patsubst %, tmp/%.tex, ${HTEX_FILES}}
 ${HTEX_FILES_TEX}: tmp/%.tex: tmp/%.spool
diff --git a/src/lib/sockio-c.c b/src/lib/sockio-c.c
index d8a62999..6941ba68 100644
--- a/src/lib/sockio-c.c
+++ b/src/lib/sockio-c.c
@@ -796,6 +796,7 @@ remote_stdio(Sock *sock)
   char buf[1024];
   fd_set rd;
   int len;
+  int bytes_print;
   while (1) {
     FD_ZERO(&rd);
     FD_SET(sock->socket,&rd);
@@ -823,8 +824,14 @@ remote_stdio(Sock *sock)
         return;
       else {
         *(buf + len) = '\0';
-        fputs(buf, stdout);
+        // if buf already ends with '\0', then the expected print count should -1
+        if(buf[len-1] == '\0') {len = len-1;}
+        //fputs(buf, stdout);
+        bytes_print = printf("%s", buf);
         fflush(stdout);
+        if (len != bytes_print) {
+        printf("-- output truncation at beginning detected. expected: %d, printed: %d\n", len, bytes_print);
+	}
       }
     }
   }
diff --git a/src/sman/spadclient.c b/src/sman/spadclient.c
index 4678160a..b0121181 100644
--- a/src/sman/spadclient.c
+++ b/src/sman/spadclient.c
@@ -56,8 +56,6 @@ main(void)
 {
   sock = connect_to_local_server(SessionServer, InterpWindow, Forever);
   bsdSignal(SIGINT, inter_handler,RestartSystemCalls);
-  /* wait for '$CreateFrame' in 'serverReadLine' to finish */
-  fricas_sleep(60);
   remote_stdio(sock);
   return(0);
 }

Reply via email to