#include "gacela_GL.h"
#include "gacela_FTGL.h"
-
// Generic variables
int ctrl_c = 0;
}
void
-gacela_client (char *hostname, int port)
+gacela_client (SCM rec_channel, SCM send_channel)
{
- int sockfd, n;
- struct hostent *server;
- struct sockaddr_in serv_addr;
- char buffer[256];
+ 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);
- 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);
- return;
- }
-
// Command line
asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
if (line && *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) {
+ scm_write (scm_from_locale_string (line), send_channel);
+ scm_force_output (send_channel);
+
+ while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
if (ctrl_c) break;
- sleep (1);
- n = read (sockfd, buffer, sizeof (buffer));
+ sleep (0.5);
}
- if (n < 0)
- error("ERROR reading from socket");
if (ctrl_c)
ctrl_c = 0;
- else
- printf ("%s\n", buffer);
+ else {
+ buffer = scm_read (rec_channel);
+ printf ("%s\n", scm_to_locale_string (buffer));
+ }
}
free (line);
}
- close (sockfd);
write_history (history_path);
free (history_path);
}
init_gacela (void *data, int argc, char **argv)
{
// Guile configuration
- scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
+ scm_c_eval_string ("(set-repl-prompt! \"gacela> \")");
scm_c_eval_string ("(use-modules (ice-9 readline))");
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);
}
void
-start_single (char *working_path)
+start_single ()
{
char *argv = "guile";
- scm_with_guile (&init_gacela, NULL);
- load_scheme_files (working_path);
scm_shell (1, &argv);
}
void
-start_server (char *working_path, int port)
+start_server (int port)
{
- char start_server[100];
+ char *start_server;
- scm_with_guile (&init_gacela, NULL);
- load_scheme_files (working_path);
- sprintf (start_server, "(start-server %d)", port);
+ asprintf (&start_server, "(start-server #:port %d)", port);
scm_c_eval_string (start_server);
}
void
-start_client (char *hostname, int port)
+start_local_server (SCM pipes)
{
- scm_init_guile ();
- gacela_client (hostname, port);
+ char start_server[100];
+
+ scm_c_define ("pipes", pipes);
+ scm_c_eval_string ("(start-server #:pipes pipes)");
+}
+
+void
+start_remote_client (char *hostname, int port)
+{
+ SCM client_socket;
+ char *connect_to_server;
+
+ asprintf (&connect_to_server, "(let ((s (socket PF_INET SOCK_STREAM 0))) (connect s AF_INET (car (hostent:addr-list (gethost \"%s\"))) %d) s)", hostname, port);
+ client_socket = scm_c_eval_string (connect_to_server);
+ gacela_client (client_socket, client_socket);
}
int
char *host;
int port = 0;
int i;
+ SCM fd1, fd2;
+ int pid;
// Checking arguments
for (i = 1; i < argc; i++) {
}
}
- if (mode == 1)
- start_single (dirname (argv[0]));
- else if (mode == 2 && port != 0)
- start_server (dirname (argv[0]), port);
+ scm_init_guile ();
+
+ if (mode == 1) {
+ scm_with_guile (&init_gacela, NULL);
+ load_scheme_files (dirname (argv[0]));
+ start_single ();
+ }
+ else if (mode == 2 && port != 0) {
+ scm_with_guile (&init_gacela, NULL);
+ load_scheme_files (dirname (argv[0]));
+ start_server (port);
+ }
else if (mode == 3 && port != 0)
- start_client (host, port);
- /*
- if (fork () == 0)
- start_server ();
- else
- start_client ("localhost", 1234);
- */
+ start_remote_client (host, port);
+ else {
+ fd1 = scm_pipe ();
+ fd2 = scm_pipe ();
+ pid = fork ();
+
+ if (pid == 0) {
+ scm_close (SCM_CAR (fd1));
+ scm_close (SCM_CDR (fd2));
+ scm_with_guile (&init_gacela, NULL);
+ load_scheme_files (dirname (argv[0]));
+ start_local_server (scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
+ }
+ else {
+ scm_close (SCM_CDR (fd1));
+ scm_close (SCM_CAR (fd2));
+ gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
+ kill (pid, SIGKILL);
+ }
+ }
}