1 /* Gacela, a GNU Guile extension for fast games development
2 Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <readline/readline.h>
23 #include <readline/history.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
29 #include "gacela_SDL.h"
30 #include "gacela_GL.h"
31 #include "gacela_FTGL.h"
37 find_matching_paren (int k)
41 int end_parens_found = 0;
43 // Choose the corresponding opening bracket
44 if (k == ')') c = '(';
45 else if (k == ']') c = '[';
46 else if (k == '}') c = '{';
48 for (i = rl_point-2; i >= 0; i--)
50 // Is the current character part of a character literal?
52 && rl_line_buffer[i - 1] == '\\'
53 && rl_line_buffer[i - 2] == '#')
55 else if (rl_line_buffer[i] == k)
57 else if (rl_line_buffer[i] == '"')
59 // Skip over a string literal
60 for (i--; i >= 0; i--)
61 if (rl_line_buffer[i] == '"'
63 && rl_line_buffer[i - 1] == '\\'))
66 else if (rl_line_buffer[i] == c)
68 if (end_parens_found==0) return i;
69 else --end_parens_found;
76 match_paren (int x, int k)
81 struct timeval timeout;
85 // Did we just insert a quoted paren? If so, then don't bounce
87 && rl_line_buffer[rl_point - 2] == '\\')
91 timeout.tv_sec = tmp / 1000000;
92 timeout.tv_usec = tmp % 1000000;
94 fno = fileno (rl_instream);
95 FD_SET (fno, &readset);
100 rl_point = find_matching_paren (k);
104 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
112 ctrl_c_handler (int signum)
114 printf ("ERROR: User interrupt\nABORT: (signal)\n");
119 init_gacela_client ()
121 struct sigaction new_action;
123 // init bouncing parens
124 rl_bind_key (')', match_paren);
125 rl_bind_key (']', match_paren);
126 rl_bind_key ('}', match_paren);
129 new_action.sa_handler = ctrl_c_handler;
130 sigemptyset (&new_action.sa_mask);
131 new_action.sa_flags = 0;
133 sigaction (SIGINT, &new_action, NULL);
137 gacela_client (SCM rec_channel, SCM send_channel)
145 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
147 init_gacela_client ();
148 read_history (history_path);
151 line = readline ("gacela> ");
157 scm_write (scm_from_locale_string (line), send_channel);
158 scm_force_output (send_channel);
160 while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
167 buffer = scm_read (rec_channel);
168 printf ("%s\n", scm_to_locale_string (buffer));
174 write_history (history_path);
179 init_gacela (void *data, int argc, char **argv)
181 // Guile configuration
182 scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
183 scm_c_eval_string ("(use-modules (ice-9 readline))");
184 scm_c_eval_string ("(activate-readline)");
185 scm_c_eval_string ("(use-modules (ice-9 optargs))");
186 scm_c_eval_string ("(use-modules (ice-9 receive))");
188 // Bindings for C functions and structs
189 SDL_register_functions (NULL);
190 GL_register_functions (NULL);
191 FTGL_register_functions (NULL);
197 load_scheme_files (char *path)
199 char load_path[strlen (path) + 1024];
201 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
202 scm_c_eval_string (load_path);
203 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
207 start_single (char *working_path)
209 char *argv = "guile";
211 scm_with_guile (&init_gacela, NULL);
212 load_scheme_files (working_path);
213 scm_shell (1, &argv);
217 start_server (char *working_path, int port)
219 char start_server[100];
221 scm_with_guile (&init_gacela, NULL);
222 load_scheme_files (working_path);
223 sprintf (start_server, "(start-server %d)", port);
224 scm_c_eval_string (start_server);
228 start_local_server (char *working_path, SCM pipes)
230 char start_server[100];
232 scm_with_guile (&init_gacela, NULL);
233 load_scheme_files (working_path);
234 scm_c_define ("pipes", pipes);
235 scm_c_eval_string ("(start-server pipes)");
240 start_remote_client (char *hostname, int port)
243 struct hostent *server;
244 struct sockaddr_in serv_addr;
246 // Connect to the server
247 sockfd = socket (AF_INET, SOCK_STREAM, 0);
248 server = gethostbyname (hostname);
249 bzero ((char *) &serv_addr, sizeof (serv_addr));
250 serv_addr.sin_family = AF_INET;
251 bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
252 serv_addr.sin_port = htons (port);
253 if (connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) == -1) {
254 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);
257 gacela_client (sockfd, sockfd);
264 start_remote_client (char *hostname, int port)
268 sockfd = scm_socket (AF_INET, SOCK_STREAM);
269 scm_connect (sockfd, AF_INET,
273 main (int argc, char *argv[])
276 int mode = 0; // shell: 1, server: 2, client: 3
283 // Checking arguments
284 for (i = 1; i < argc; i++) {
285 if (strcmp (argv[i], "--shell-mode") == 0)
287 else if (strncmp (argv[i], "--server", 8) == 0) {
289 arg = strtok (argv[i], "=");
290 arg = strtok (NULL, "=");
294 else if (strncmp (argv[i], "--client", 8) == 0) {
296 arg = strtok (argv[i], "=");
297 arg = strtok (NULL, "=");
299 host = strtok (arg, ":");
300 arg = strtok (NULL, ":");
310 start_single (dirname (argv[0]));
311 else if (mode == 2 && port != 0)
312 start_server (dirname (argv[0]), port);
313 else if (mode == 3 && port != 0)
314 start_remote_client (host, port);
321 scm_close (SCM_CAR (fd1));
322 scm_close (SCM_CDR (fd2));
323 start_local_server (dirname (argv[0]), scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
326 scm_close (SCM_CDR (fd1));
327 scm_close (SCM_CAR (fd2));
328 gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
335 printf ("Hijo: 0\n");
336 scm_write (scm_from_locale_string ("Hola mundo\n"), SCM_CDR (pipes));
337 scm_force_output (SCM_CDR (pipes));
338 printf ("Hijo: 1\n");
341 printf ("Padre: 0\n");
342 buffer = scm_read (SCM_CAR (pipes));
343 printf ("%s\n", scm_to_locale_string (buffer));
344 printf ("Padre: 1\n");