]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela.c
(no commit message)
[gacela.git] / src / gacela.c
index 3262aee68c5cd5a9e3f72bf6cb37e0cdab4b420c..a3505049caf1ac1ff31b3b9802e73d3f6df7651d 100644 (file)
@@ -15,6 +15,8 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <stdio.h>
+#include <string.h>
 #include <libguile.h>
 #include <libgen.h>
 #include <readline/readline.h>
@@ -29,6 +31,9 @@
 #include "gacela_FTGL.h"
 
 
+// Generic variables
+int ctrl_c = 0;
+
 static int
 find_matching_paren (int k)
 {
@@ -108,6 +113,7 @@ void
 ctrl_c_handler (int signum)
 {
   printf ("ERROR: User interrupt\nABORT: (signal)\n");
+  ctrl_c = 1;
 }
      
 static void
@@ -131,10 +137,10 @@ init_gacela_client ()
 void
 gacela_client (char *hostname, int port)
 {
-  int sockfd;
+  int sockfd, n;
   struct hostent *server;
   struct sockaddr_in serv_addr;
-
+  char buffer[256];
   char *line;
   char *history_path;
 
@@ -155,15 +161,35 @@ gacela_client (char *hostname, int port)
 
   while (1) {
     line = readline ("gacela> ");
+    ctrl_c = 0;
     if (!line) break;
     if (line && *line)
       {
-       printf ("%s\n", line);
        add_history (line);
+       write (sockfd, "(", 1);
+       n = write (sockfd, line, strlen (line));
+       write (sockfd, ")", 1);
+       if (n < 0)
+         error("ERROR writing to socket");
+
+       bzero (buffer, 256);
+       n = read (sockfd, buffer, sizeof (buffer));
+       while (n == 0) {
+         if (ctrl_c) break;
+         sleep (1);
+         n = read (sockfd, buffer, sizeof (buffer));
+       }
+       if (n < 0)
+         error("ERROR reading from socket");
+       if (ctrl_c)
+         ctrl_c = 0;
+       else
+         printf ("%s\n", buffer);
       }
     free (line);
   }
 
+  close (sockfd);
   write_history (history_path);
   free (history_path);
 }
@@ -216,7 +242,6 @@ start_server (char *working_path, int port)
   load_scheme_files (working_path);
   sprintf (start_server, "(start-server %d)", port);
   scm_c_eval_string (start_server);
-  scm_c_eval_string ("(game-loop)");
 }
 
 void
@@ -231,20 +256,40 @@ main (int argc, char *argv[])
 {
   char *arg;
   int mode = 0;   // shell: 1, server: 2, client: 3
-  int port = 2;
+  char *host;
+  int port = 0;
   int i;
 
-  for (i = 1; i < argc; i++)
+  // Checking arguments
+  for (i = 1; i < argc; i++) {
     if (strcmp (argv[i], "--shell-mode") == 0)
       mode = 1;
     else if (strncmp (argv[i], "--server", 8) == 0) {
-      arg = strdup (argv[i]);
-      port = 1234;
+      mode = 2;
+      arg = strtok (argv[i], "=");
+      arg = strtok (NULL, "=");
+      if (arg != NULL)
+       port = atoi (arg);
+    }
+    else if (strncmp (argv[i], "--client", 8) == 0) {
+      mode = 3;
+      arg = strtok (argv[i], "=");
+      arg = strtok (NULL, "=");
+      if (arg != NULL) {
+       host = strtok (arg, ":");
+       arg = strtok (NULL, ":");
+       if (arg != NULL)
+         port = atoi (arg);
+      }
+    }
+  }
 
   if (mode == 1)
     start_single (dirname (argv[0]));
-  else
-    printf ("Puerto: %d\n", port);
+  else if (mode == 2 && port != 0)
+    start_server (dirname (argv[0]), port);
+  else if (mode == 3 && port != 0)
+    start_client (host, port);
   /*
   if (fork () == 0)
     start_server ();