]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela.c
(no commit message)
[gacela.git] / src / gacela.c
index fdb76db5156994278fccde594556d73e2d94d0c5..a3505049caf1ac1ff31b3b9802e73d3f6df7651d 100644 (file)
    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>
 #include <readline/history.h>
 #include <signal.h>
 #include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
 #include "gacela_SDL.h"
 #include "gacela_GL.h"
 #include "gacela_FTGL.h"
 
 
+// Generic variables
+int ctrl_c = 0;
+
 static int
 find_matching_paren (int k)
 {
@@ -105,6 +113,7 @@ void
 ctrl_c_handler (int signum)
 {
   printf ("ERROR: User interrupt\nABORT: (signal)\n");
+  ctrl_c = 1;
 }
      
 static void
@@ -128,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;
 
@@ -152,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);
 }
@@ -195,20 +224,24 @@ load_scheme_files (char *path)
 }
 
 void
-start_single (int argc, char *argv[])
+start_single (char *working_path)
 {
+  char *argv = "guile";
+
   scm_with_guile (&init_gacela, NULL);
-  load_scheme_files (dirname (argv[0]));
-  scm_shell (argc, argv);
+  load_scheme_files (working_path);
+  scm_shell (1, &argv);
 }
 
 void
-start_server (int argc, char *argv[])
+start_server (char *working_path, int port)
 {
+  char start_server[100];
+
   scm_with_guile (&init_gacela, NULL);
-  load_scheme_files (dirname (argv[0]));
-  scm_c_eval_string ("(start-server 1234)");
-  scm_c_eval_string ("(game-loop)");
+  load_scheme_files (working_path);
+  sprintf (start_server, "(start-server %d)", port);
+  scm_c_eval_string (start_server);
 }
 
 void
@@ -221,7 +254,42 @@ start_client (char *hostname, int port)
 int
 main (int argc, char *argv[])
 {
-  start_single (argc, argv);
+  char *arg;
+  int mode = 0;   // shell: 1, server: 2, client: 3
+  char *host;
+  int port = 0;
+  int 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) {
+      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 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 ();