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"
38 find_matching_paren (int k)
42 int end_parens_found = 0;
44 // Choose the corresponding opening bracket
45 if (k == ')') c = '(';
46 else if (k == ']') c = '[';
47 else if (k == '}') c = '{';
49 for (i = rl_point-2; i >= 0; i--)
51 // Is the current character part of a character literal?
53 && rl_line_buffer[i - 1] == '\\'
54 && rl_line_buffer[i - 2] == '#')
56 else if (rl_line_buffer[i] == k)
58 else if (rl_line_buffer[i] == '"')
60 // Skip over a string literal
61 for (i--; i >= 0; i--)
62 if (rl_line_buffer[i] == '"'
64 && rl_line_buffer[i - 1] == '\\'))
67 else if (rl_line_buffer[i] == c)
69 if (end_parens_found==0) return i;
70 else --end_parens_found;
77 match_paren (int x, int k)
82 struct timeval timeout;
86 // Did we just insert a quoted paren? If so, then don't bounce
88 && rl_line_buffer[rl_point - 2] == '\\')
92 timeout.tv_sec = tmp / 1000000;
93 timeout.tv_usec = tmp % 1000000;
95 fno = fileno (rl_instream);
96 FD_SET (fno, &readset);
101 rl_point = find_matching_paren (k);
105 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
113 ctrl_c_handler (int signum)
115 printf ("ERROR: User interrupt\nABORT: (signal)\n");
120 look_child_handler (int signum)
126 init_gacela_client ()
128 struct sigaction ctrl_c_action, look_child_action;
130 // init bouncing parens
131 rl_bind_key (')', match_paren);
132 rl_bind_key (']', match_paren);
133 rl_bind_key ('}', match_paren);
136 ctrl_c_action.sa_handler = ctrl_c_handler;
137 sigemptyset (&ctrl_c_action.sa_mask);
138 ctrl_c_action.sa_flags = 0;
140 sigaction (SIGINT, &ctrl_c_action, NULL);
144 look_child_action.sa_handler = look_child_handler;
145 sigemptyset (&look_child_action.sa_mask);
146 look_child_action.sa_flags = 0;
148 sigaction (SIGALRM, &look_child_action, NULL);
154 opened_parens (char *line, int k)
160 // Choose the corresponding opening bracket
161 if (k == ')') c = '(';
162 else if (k == ']') c = '[';
163 else if (k == '}') c = '{';
165 for (i = 0; i < strlen (line); i++) {
166 // Is the current character part of a character literal?
167 if (i + 2 >= strlen (line)
169 && line[i + 1] == '\\')
171 else if (line[i] == '"') {
172 // Skip over a string literal
173 for (i++; i < strlen (line); i++)
175 && line[i - 1] != '\\')
178 else if (line[i] == c)
180 else if (line[i] == k)
188 gacela_client (SCM rec_channel, SCM send_channel)
192 char *line = NULL, *line_for_sending = NULL;
197 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
199 init_gacela_client ();
200 read_history (history_path);
204 line = readline ("... ");
206 line = readline ("gacela> ");
210 opened += opened_parens (line, ')');
216 if (line_for_sending == NULL) {
217 line_for_sending = strdup (line);
220 line_for_sending = realloc (line_for_sending, strlen (line_for_sending) + strlen (line) + 2);
221 line_for_sending = strcat (line_for_sending, " ");
222 line_for_sending = strcat (line_for_sending, line);
226 scm_write (scm_from_locale_string (line_for_sending), send_channel);
227 scm_force_output (send_channel);
228 free (line_for_sending);
229 line_for_sending = NULL;
231 while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
238 buffer = scm_read (rec_channel);
239 if (strlen (scm_to_locale_string (buffer)) > 0)
240 printf ("%s\n", scm_to_locale_string (buffer));
247 write_history (history_path);
252 init_scheme (void *data, int argc, char **argv)
254 // Guile configuration
255 scm_c_eval_string ("(set-repl-prompt! \"gacela> \")");
256 scm_c_eval_string ("(use-modules (ice-9 readline))");
257 scm_c_eval_string ("(activate-readline)");
258 scm_c_eval_string ("(use-modules (ice-9 optargs))");
259 scm_c_eval_string ("(use-modules (ice-9 receive))");
261 // Bindings for C functions and structs
262 SDL_register_functions (NULL);
263 GL_register_functions (NULL);
264 FTGL_register_functions (NULL);
270 load_scheme_files (char *path)
272 char load_path[strlen (path) + 1024];
274 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
275 scm_c_eval_string (load_path);
276 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
280 init_gacela (char *path)
282 scm_with_guile (&init_scheme, NULL);
283 load_scheme_files (path);
284 scm_c_eval_string ("(init-video-mode)");
288 start_server (int port)
292 asprintf (&start_server, "(start-server #:port %d)", port);
293 scm_c_eval_string (start_server);
297 start_local_server (SCM pipes)
299 char start_server[100];
301 scm_c_define ("pipes", pipes);
302 scm_c_eval_string ("(start-server #:pipes pipes)");
306 start_remote_client (char *hostname, int port)
309 char *connect_to_server;
311 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);
312 client_socket = scm_c_eval_string (connect_to_server);
313 gacela_client (client_socket, client_socket);
317 main (int argc, char *argv[])
320 int mode = 0; // dev: 1, server: 2, client: 3
326 // Checking arguments
327 for (i = 1; i < argc; i++) {
328 if (strcmp (argv[i], "--dev") == 0)
330 else if (strncmp (argv[i], "--server", 8) == 0) {
332 arg = strtok (argv[i], "=");
333 arg = strtok (NULL, "=");
337 else if (strncmp (argv[i], "--client", 8) == 0) {
339 arg = strtok (argv[i], "=");
340 arg = strtok (NULL, "=");
342 host = strtok (arg, ":");
343 arg = strtok (NULL, ":");
358 scm_close (SCM_CAR (fd1));
359 scm_close (SCM_CDR (fd2));
360 init_gacela (dirname (argv[0]));
361 start_local_server (scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
364 scm_close (SCM_CDR (fd1));
365 scm_close (SCM_CAR (fd2));
366 gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
370 else if (mode == 2 && port != 0) {
371 init_gacela (dirname (argv[0]));
374 else if (mode == 3 && port != 0)
375 start_remote_client (host, port);
377 init_gacela (dirname (argv[0]));
378 scm_shell (argc, argv);