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] == ';') {
182 // Comment until endline
185 else if (line[i] == c)
187 else if (line[i] == k)
195 gacela_client (SCM rec_channel, SCM send_channel)
199 char *line = NULL, *line_for_sending = NULL;
203 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
205 init_gacela_client ();
206 read_history (history_path);
210 line = readline ("... ");
212 line = readline ("gacela> ");
216 opened += opened_parens (line, ')');
222 if (line_for_sending == NULL) {
223 line_for_sending = strdup (line);
226 line_for_sending = realloc (line_for_sending, strlen (line_for_sending) + strlen (line) + 2);
227 line_for_sending = strcat (line_for_sending, "\n");
228 line_for_sending = strcat (line_for_sending, line);
232 scm_write (scm_from_locale_string (line_for_sending), send_channel);
233 scm_force_output (send_channel);
234 free (line_for_sending);
235 line_for_sending = NULL;
237 while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
244 buffer = scm_read (rec_channel);
245 if (strlen (scm_to_locale_string (buffer)) > 0)
246 printf ("%s\n", scm_to_locale_string (buffer));
253 write_history (history_path);
258 init_scheme (void *data, int argc, char **argv)
260 // Guile configuration
261 scm_c_eval_string ("(set-repl-prompt! \"gacela> \")");
262 scm_c_eval_string ("(use-modules (ice-9 readline))");
263 scm_c_eval_string ("(activate-readline)");
264 scm_c_eval_string ("(use-modules (ice-9 optargs))");
265 scm_c_eval_string ("(use-modules (ice-9 receive))");
267 // Bindings for C functions and structs
268 SDL_register_functions (NULL);
269 GL_register_functions (NULL);
270 FTGL_register_functions (NULL);
276 load_scheme_files (char *path)
278 char load_path[strlen (path) + 1024];
280 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
281 scm_c_eval_string (load_path);
282 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
286 init_gacela (char *path)
288 scm_with_guile (&init_scheme, NULL);
289 load_scheme_files (path);
290 scm_c_eval_string ("(init-video-mode)");
294 start_server (int port)
298 asprintf (&start_server, "(start-server #:port %d)", port);
299 scm_c_eval_string (start_server);
303 start_local_server (SCM pipes)
305 char start_server[100];
307 scm_c_define ("pipes", pipes);
308 scm_c_eval_string ("(start-server #:pipes pipes)");
312 start_remote_client (char *hostname, int port)
315 char *connect_to_server;
317 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);
318 client_socket = scm_c_eval_string (connect_to_server);
319 gacela_client (client_socket, client_socket);
323 main (int argc, char *argv[])
326 int mode = 0; // dev: 1, server: 2, client: 3
332 // Checking arguments
333 for (i = 1; i < argc; i++) {
334 if (strcmp (argv[i], "--dev") == 0)
336 else if (strncmp (argv[i], "--server", 8) == 0) {
338 arg = strtok (argv[i], "=");
339 arg = strtok (NULL, "=");
343 else if (strncmp (argv[i], "--client", 8) == 0) {
345 arg = strtok (argv[i], "=");
346 arg = strtok (NULL, "=");
348 host = strtok (arg, ":");
349 arg = strtok (NULL, ":");
364 scm_close (SCM_CAR (fd1));
365 scm_close (SCM_CDR (fd2));
366 init_gacela (dirname (argv[0]));
367 start_local_server (scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
368 kill (getppid (), SIGALRM);
371 scm_close (SCM_CDR (fd1));
372 scm_close (SCM_CAR (fd2));
373 gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
377 else if (mode == 2 && port != 0) {
378 init_gacela (dirname (argv[0]));
381 else if (mode == 3 && port != 0)
382 start_remote_client (host, port);
384 init_gacela (dirname (argv[0]));
385 scm_shell (argc, argv);