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"
36 char *history_path = NULL;
40 find_matching_paren (int k)
44 int end_parens_found = 0;
46 // Choose the corresponding opening bracket
47 if (k == ')') c = '(';
48 else if (k == ']') c = '[';
49 else if (k == '}') c = '{';
51 for (i = rl_point-2; i >= 0; i--)
53 // Is the current character part of a character literal?
55 && rl_line_buffer[i - 1] == '\\'
56 && rl_line_buffer[i - 2] == '#')
58 else if (rl_line_buffer[i] == k)
60 else if (rl_line_buffer[i] == '"')
62 // Skip over a string literal
63 for (i--; i >= 0; i--)
64 if (rl_line_buffer[i] == '"'
66 && rl_line_buffer[i - 1] == '\\'))
69 else if (rl_line_buffer[i] == c)
71 if (end_parens_found==0) return i;
72 else --end_parens_found;
79 match_paren (int x, int k)
84 struct timeval timeout;
88 // Did we just insert a quoted paren? If so, then don't bounce
90 && rl_line_buffer[rl_point - 2] == '\\')
94 timeout.tv_sec = tmp / 1000000;
95 timeout.tv_usec = tmp % 1000000;
97 fno = fileno (rl_instream);
98 FD_SET (fno, &readset);
103 rl_point = find_matching_paren (k);
107 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
115 ctrl_c_handler (int signum)
117 printf ("ERROR: User interrupt\nABORT: (signal)\n");
122 child_dies_handler (int signum)
124 write_history (history_path);
129 init_gacela_client ()
131 struct sigaction ctrl_c_action, child_dies_action;
133 // init bouncing parens
134 rl_bind_key (')', match_paren);
135 rl_bind_key (']', match_paren);
136 rl_bind_key ('}', match_paren);
139 ctrl_c_action.sa_handler = ctrl_c_handler;
140 sigemptyset (&ctrl_c_action.sa_mask);
141 ctrl_c_action.sa_flags = 0;
143 sigaction (SIGINT, &ctrl_c_action, NULL);
147 child_dies_action.sa_handler = child_dies_handler;
148 sigemptyset (&child_dies_action.sa_mask);
149 child_dies_action.sa_flags = 0;
151 sigaction (SIGALRM, &child_dies_action, NULL);
157 opened_parens (char *line, int k)
163 // Choose the corresponding opening bracket
164 if (k == ')') c = '(';
165 else if (k == ']') c = '[';
166 else if (k == '}') c = '{';
168 for (i = 0; i < strlen (line); i++) {
169 // Is the current character part of a character literal?
170 if (i + 2 >= strlen (line)
172 && line[i + 1] == '\\')
174 else if (line[i] == '"') {
175 // Skip over a string literal
176 for (i++; i < strlen (line); i++)
178 && line[i - 1] != '\\')
181 else if (line[i] == c)
183 else if (line[i] == k)
191 gacela_client (SCM rec_channel, SCM send_channel)
195 char *line = NULL, *line_for_sending = NULL;
199 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
201 init_gacela_client ();
202 read_history (history_path);
206 line = readline ("... ");
208 line = readline ("gacela> ");
212 opened += opened_parens (line, ')');
218 if (line_for_sending == NULL) {
219 line_for_sending = strdup (line);
222 line_for_sending = realloc (line_for_sending, strlen (line_for_sending) + strlen (line) + 2);
223 line_for_sending = strcat (line_for_sending, " ");
224 line_for_sending = strcat (line_for_sending, line);
228 scm_write (scm_from_locale_string (line_for_sending), send_channel);
229 scm_force_output (send_channel);
230 free (line_for_sending);
231 line_for_sending = NULL;
233 while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
240 buffer = scm_read (rec_channel);
241 if (strlen (scm_to_locale_string (buffer)) > 0)
242 printf ("%s\n", scm_to_locale_string (buffer));
249 write_history (history_path);
254 init_scheme (void *data, int argc, char **argv)
256 // Guile configuration
257 scm_c_eval_string ("(set-repl-prompt! \"gacela> \")");
258 scm_c_eval_string ("(use-modules (ice-9 readline))");
259 scm_c_eval_string ("(activate-readline)");
260 scm_c_eval_string ("(use-modules (ice-9 optargs))");
261 scm_c_eval_string ("(use-modules (ice-9 receive))");
263 // Bindings for C functions and structs
264 SDL_register_functions (NULL);
265 GL_register_functions (NULL);
266 FTGL_register_functions (NULL);
272 load_scheme_files (char *path)
274 char load_path[strlen (path) + 1024];
276 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
277 scm_c_eval_string (load_path);
278 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
282 init_gacela (char *path)
284 scm_with_guile (&init_scheme, NULL);
285 load_scheme_files (path);
286 scm_c_eval_string ("(init-video-mode)");
290 start_server (int port)
294 asprintf (&start_server, "(start-server #:port %d)", port);
295 scm_c_eval_string (start_server);
299 start_local_server (SCM pipes)
301 char start_server[100];
303 scm_c_define ("pipes", pipes);
304 scm_c_eval_string ("(start-server #:pipes pipes)");
308 start_remote_client (char *hostname, int port)
311 char *connect_to_server;
313 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);
314 client_socket = scm_c_eval_string (connect_to_server);
315 gacela_client (client_socket, client_socket);
319 main (int argc, char *argv[])
322 int mode = 0; // dev: 1, server: 2, client: 3
328 // Checking arguments
329 for (i = 1; i < argc; i++) {
330 if (strcmp (argv[i], "--dev") == 0)
332 else if (strncmp (argv[i], "--server", 8) == 0) {
334 arg = strtok (argv[i], "=");
335 arg = strtok (NULL, "=");
339 else if (strncmp (argv[i], "--client", 8) == 0) {
341 arg = strtok (argv[i], "=");
342 arg = strtok (NULL, "=");
344 host = strtok (arg, ":");
345 arg = strtok (NULL, ":");
360 scm_close (SCM_CAR (fd1));
361 scm_close (SCM_CDR (fd2));
362 init_gacela (dirname (argv[0]));
363 start_local_server (scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
364 kill (getppid (), SIGALRM);
367 scm_close (SCM_CDR (fd1));
368 scm_close (SCM_CAR (fd2));
369 gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
373 else if (mode == 2 && port != 0) {
374 init_gacela (dirname (argv[0]));
377 else if (mode == 3 && port != 0)
378 start_remote_client (host, port);
380 init_gacela (dirname (argv[0]));
381 scm_shell (argc, argv);