<URL: http://bugs.freeciv.org/Ticket/Display.html?id=40225 >

 When server is built without readline, reading input from pipe does not work:

 (
    echo rulesetdir $RULESET
    echo set aifill 10
 ) | ./ser

 Problem is that server will receive all input at once and not line by
line. Attached patch fixes this for systems having getline()
available.


 - ML

diff -Nurd -X.diff_ignore freeciv/configure.ac freeciv/configure.ac
--- freeciv/configure.ac	2008-04-17 10:49:41.000000000 +0300
+++ freeciv/configure.ac	2008-05-02 15:56:12.000000000 +0300
@@ -611,7 +611,8 @@
 
 AC_CHECK_FUNCS([bind connect fileno flock ftime gethostbyname gethostname \
 		getpwuid inet_aton select snooze strcasecmp strcasestr \
-		strerror strlcat strlcpy strncasecmp strstr uname usleep])
+		strerror strlcat strlcpy strncasecmp strstr uname usleep \
+                getline])
 
 AC_MSG_CHECKING(for working gettimeofday)
   FC_CHECK_GETTIMEOFDAY_RUNTIME(,AC_DEFINE(HAVE_GETTIMEOFDAY, 1,
diff -Nurd -X.diff_ignore freeciv/server/sernet.c freeciv/server/sernet.c
--- freeciv/server/sernet.c	2008-04-22 12:19:37.000000000 +0300
+++ freeciv/server/sernet.c	2008-05-02 16:24:56.000000000 +0300
@@ -696,20 +696,39 @@
       continue;
 #else  /* !HAVE_LIBREADLINE */
       ssize_t didget;
-      char buf[BUF_SIZE + 1];
+      char *buffer = NULL; /* Must be NULL when calling getline() */
       char *buf_internal;
-      
-      didget = read(0, buf, BUF_SIZE);
+
+#ifdef HAVE_GETLINE
+      size_t len = 0;
+
+      didget = getline(&buffer, &len, stdin);
+      if (didget >= 1) {
+        buffer[didget-1] = '\0'; /* overwrite newline character */
+        didget--;
+        freelog(LOG_DEBUG, "Got line: \"%s\" (%ld, %ld)", buffer, didget, len);
+      }
+#else  /* HAVE_GETLINE */
+      buffer = malloc(BUF_SIZE + 1);
+
+      didget = read(0, buffer, BUF_SIZE);
+      if (didget < 0) {
+        didget = 0; /* Avoid buffer underrun below. */
+      }
+      *(buffer+didget)='\0';
+#endif /* HAVE_GETLINE */
       if (didget <= 0) {
         handle_stdin_close();
-	didget = 0; /* Avoid buffer underrun below. */
       }
 
-      *(buf + didget)='\0';
       con_prompt_enter();	/* will need a new prompt, regardless */
-      buf_internal = local_to_internal_string_malloc(buf);
-      handle_stdin_input(NULL, buf_internal, FALSE);
-      free(buf_internal);
+
+      if (didget >= 0) {
+        buf_internal = local_to_internal_string_malloc(buffer);
+        handle_stdin_input(NULL, buf_internal, FALSE);
+        free(buf_internal);
+      }
+      free(buffer);
 #endif /* !HAVE_LIBREADLINE */
     } else
 #endif /* !SOCKET_ZERO_ISNT_STDIN */
diff -Nurd -X.diff_ignore freeciv/configure.ac freeciv/configure.ac
--- freeciv/configure.ac	2008-04-06 13:43:16.000000000 +0300
+++ freeciv/configure.ac	2008-05-02 16:50:02.000000000 +0300
@@ -8,6 +8,7 @@
 AC_CONFIG_AUX_DIR(bootstrap) # This can't be quoted or automake will fail
 AM_CONFIG_HEADER(config.h)
 AC_CANONICAL_HOST
+AC_GNU_SOURCE
 
 PACKAGE=freeciv
 
@@ -569,7 +570,7 @@
 AC_CHECK_FUNCS([fileno ftime gethostname getpwuid inet_aton \
 		select snooze strerror strcasecmp strncasecmp \
 		strlcat strlcpy strstr usleep uname flock \
-		gethostbyname connect bind])
+		gethostbyname connect bind getline])
 
 AC_MSG_CHECKING(for working gettimeofday)
   FC_CHECK_GETTIMEOFDAY_RUNTIME(,AC_DEFINE(HAVE_GETTIMEOFDAY, 1,
diff -Nurd -X.diff_ignore freeciv/server/sernet.c freeciv/server/sernet.c
--- freeciv/server/sernet.c	2008-04-22 11:05:04.000000000 +0300
+++ freeciv/server/sernet.c	2008-05-02 16:49:43.000000000 +0300
@@ -701,20 +701,39 @@
       continue;
 #else  /* !HAVE_LIBREADLINE */
       ssize_t didget;
-      char buf[BUF_SIZE + 1];
+      char *buffer = NULL; /* Must be NULL when calling getline() */
       char *buf_internal;
-      
-      didget = read(0, buf, BUF_SIZE);
+
+#ifdef HAVE_GETLINE
+      size_t len = 0;
+
+      didget = getline(&buffer, &len, stdin);
+      if (didget >= 1) {
+        buffer[didget-1] = '\0'; /* overwrite newline character */
+        didget--;
+        freelog(LOG_DEBUG, "Got line: \"%s\" (%ld, %ld)", buffer, didget, len);
+      }
+#else  /* HAVE_GETLINE */
+      buffer = malloc(BUF_SIZE + 1);
+
+      didget = read(0, buffer, BUF_SIZE);
+      if (didget < 0) {
+        didget = 0; /* Avoid buffer underrun below. */
+      }
+      *(buffer+didget)='\0';
+#endif /* HAVE_GETLINE */
       if (didget <= 0) {
         handle_stdin_close();
-	didget = 0; /* Avoid buffer underrun below. */
       }
 
-      *(buf + didget)='\0';
       con_prompt_enter();	/* will need a new prompt, regardless */
-      buf_internal = local_to_internal_string_malloc(buf);
-      handle_stdin_input(NULL, buf_internal, FALSE);
-      free(buf_internal);
+
+      if (didget >= 0) {
+        buf_internal = local_to_internal_string_malloc(buffer);
+        handle_stdin_input(NULL, buf_internal, FALSE);
+        free(buf_internal);
+      }
+      free(buffer);
 #endif /* !HAVE_LIBREADLINE */
     } else
 #endif /* !SOCKET_ZERO_ISNT_STDIN */
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to