]> git.jsancho.org Git - gacela.git/blobdiff - src/gacela.c
(no commit message)
[gacela.git] / src / gacela.c
index 8c36b4eda218d2f91a890d01a0d6c54d74b95445..3357b0c1e05d6d76c0461c4ef4e330d9a2f4b434 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>
@@ -28,6 +30,8 @@
 #include "gacela_GL.h"
 #include "gacela_FTGL.h"
 
+// Generic variables
+int ctrl_c = 0;
 
 static int
 find_matching_paren (int k)
@@ -108,6 +112,7 @@ void
 ctrl_c_handler (int signum)
 {
   printf ("ERROR: User interrupt\nABORT: (signal)\n");
+  ctrl_c = 1;
 }
      
 static void
@@ -129,24 +134,13 @@ init_gacela_client ()
 }
 
 void
-gacela_client (char *hostname, int port)
+gacela_client (SCM rec_channel, SCM send_channel)
 {
-  int sockfd;
-  struct hostent *server;
-  struct sockaddr_in serv_addr;
-
+  int n;
+  SCM buffer;
   char *line;
   char *history_path;
 
-  // Connect to the server
-  sockfd = socket (AF_INET, SOCK_STREAM, 0);
-  server = gethostbyname (hostname);
-  bzero ((char *) &serv_addr, sizeof (serv_addr));
-  serv_addr.sin_family = AF_INET;
-  bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
-  serv_addr.sin_port = htons (port);
-  connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
-
   // Command line
   asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
 
@@ -155,11 +149,25 @@ 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);
+       scm_write (scm_from_locale_string ("("), send_channel);
+       scm_write (scm_from_locale_string (line), send_channel);
+       scm_write (scm_from_locale_string (")"), send_channel);
+
+       while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
+         if (ctrl_c) break;
+         sleep (1);
+       }
+       if (ctrl_c)
+         ctrl_c = 0;
+       else {
+         buffer = scm_read_line (rec_channel);
+         printf ("%s\n", scm_to_locale_string (buffer));
+       }
       }
     free (line);
   }
@@ -177,7 +185,6 @@ init_gacela (void *data, int argc, char **argv)
   scm_c_eval_string ("(activate-readline)");
   scm_c_eval_string ("(use-modules (ice-9 optargs))");
   scm_c_eval_string ("(use-modules (ice-9 receive))");
-  //  scm_c_eval_string ("(read-enable 'case-insensitive)");
 
   // Bindings for C functions and structs
   SDL_register_functions (NULL);
@@ -216,33 +223,92 @@ 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
-start_client (char *hostname, int port)
+start_local_server (char *working_path, SCM pipes)
 {
-  scm_init_guile ();
-  gacela_client (hostname, port);
+  char start_server[100];
+
+  scm_with_guile (&init_gacela, NULL);
+  load_scheme_files (working_path);
+  scm_c_define ("pipes", pipes);
+  scm_c_eval_string ("(start-server pipes)");
 }
+/*
+void
+start_remote_client (char *hostname, int port)
+{
+  int sockfd;
+  struct hostent *server;
+  struct sockaddr_in serv_addr;
 
+  // Connect to the server
+  sockfd = socket (AF_INET, SOCK_STREAM, 0);
+  server = gethostbyname (hostname);
+  bzero ((char *) &serv_addr, sizeof (serv_addr));
+  serv_addr.sin_family = AF_INET;
+  bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
+  serv_addr.sin_port = htons (port);
+  if (connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) == -1) {
+    printf ("%s [%d.%d.%d.%d] %d: Connection refused\n", hostname, server->h_addr[0], server->h_addr[1], server->h_addr[2], server->h_addr[3], port);
+  }
+  else {
+    gacela_client (sockfd, sockfd);
+    close (sockfd);
+  }
+}
+*/
 int
 main (int argc, char *argv[])
 {
-  int shell_mode = 0;
-  int port;
+  char *arg;
+  int mode = 0;   // shell: 1, server: 2, client: 3
+  char *host;
+  int port = 0;
   int i;
+  SCM pipes;
+  int pid;
 
-  for (i = 1; i < argc; i++)
+  // Checking arguments
+  for (i = 1; i < argc; i++) {
     if (strcmp (argv[i], "--shell-mode") == 0)
-      shell_mode = 1;
+      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 (shell_mode == 1)
+  scm_init_guile ();
+
+  if (mode == 1)
     start_single (dirname (argv[0]));
-  /*
-  if (fork () == 0)
-    start_server ();
-  else
-    start_client ("localhost", 1234);
-  */
+  else if (mode == 2 && port != 0)
+    start_server (dirname (argv[0]), port);
+  else if (mode == 3 && port != 0)
+    //start_remote_client (host, port);
+    return;
+  else {
+    pipes = scm_pipe ();
+    pid = fork ();
+    if (pid == 0)
+      start_local_server (dirname (argv[0]), pipes);
+    else
+      gacela_client (SCM_CAR (pipes), SCM_CDR (pipes));
+  }
 }