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 opened_parens (char *line, int k)
143 // Choose the corresponding opening bracket
144 if (k == ')') c = '(';
145 else if (k == ']') c = '[';
146 else if (k == '}') c = '{';
148 for (i = 0; i < strlen (line); i++) {
149 // Is the current character part of a character literal?
150 if (i + 2 >= strlen (line)
152 && line[i + 1] == '\\')
154 else if (line[i] == '"') {
155 // Skip over a string literal
156 for (i++; i < strlen (line); i++)
158 && line[i - 1] != '\\')
161 else if (line[i] == c)
163 else if (line[i] == k)
171 gacela_client (SCM rec_channel, SCM send_channel)
175 char *line = NULL, *line_for_sending = NULL;
180 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
182 init_gacela_client ();
183 read_history (history_path);
187 line = readline ("... ");
189 line = readline ("gacela> ");
193 opened += opened_parens (line, ')');
199 if (line_for_sending == NULL) {
200 line_for_sending = strdup (line);
203 line_for_sending = realloc (line_for_sending, strlen (line_for_sending) + strlen (line) + 2);
204 line_for_sending = strcat (line_for_sending, " ");
205 line_for_sending = strcat (line_for_sending, line);
209 scm_write (scm_from_locale_string (line_for_sending), send_channel);
210 scm_force_output (send_channel);
211 free (line_for_sending);
212 line_for_sending = NULL;
214 while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
221 buffer = scm_read (rec_channel);
222 if (strlen (scm_to_locale_string (buffer)) > 0)
223 printf ("%s\n", scm_to_locale_string (buffer));
230 write_history (history_path);
235 init_scheme (void *data, int argc, char **argv)
237 // Guile configuration
238 scm_c_eval_string ("(set-repl-prompt! \"gacela> \")");
239 scm_c_eval_string ("(use-modules (ice-9 readline))");
240 scm_c_eval_string ("(activate-readline)");
241 scm_c_eval_string ("(use-modules (ice-9 optargs))");
242 scm_c_eval_string ("(use-modules (ice-9 receive))");
244 // Bindings for C functions and structs
245 SDL_register_functions (NULL);
246 GL_register_functions (NULL);
247 FTGL_register_functions (NULL);
253 load_scheme_files (char *path)
255 char load_path[strlen (path) + 1024];
257 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
258 scm_c_eval_string (load_path);
259 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
263 init_gacela (char *path)
265 scm_with_guile (&init_scheme, NULL);
266 load_scheme_files (path);
267 scm_c_eval_string ("(init-video-mode)");
271 start_server (int port)
275 asprintf (&start_server, "(start-server #:port %d)", port);
276 scm_c_eval_string (start_server);
280 start_local_server (SCM pipes)
282 char start_server[100];
284 scm_c_define ("pipes", pipes);
285 scm_c_eval_string ("(start-server #:pipes pipes)");
289 start_remote_client (char *hostname, int port)
292 char *connect_to_server;
294 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);
295 client_socket = scm_c_eval_string (connect_to_server);
296 gacela_client (client_socket, client_socket);
300 main (int argc, char *argv[])
303 int mode = 0; // dev: 1, server: 2, client: 3
310 // Checking arguments
311 for (i = 1; i < argc; i++) {
312 if (strcmp (argv[i], "--dev") == 0)
314 else if (strncmp (argv[i], "--server", 8) == 0) {
316 arg = strtok (argv[i], "=");
317 arg = strtok (NULL, "=");
321 else if (strncmp (argv[i], "--client", 8) == 0) {
323 arg = strtok (argv[i], "=");
324 arg = strtok (NULL, "=");
326 host = strtok (arg, ":");
327 arg = strtok (NULL, ":");
342 scm_close (SCM_CAR (fd1));
343 scm_close (SCM_CDR (fd2));
344 init_gacela (dirname (argv[0]));
345 start_local_server (scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
348 scm_close (SCM_CDR (fd1));
349 scm_close (SCM_CAR (fd2));
350 gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
354 else if (mode == 2 && port != 0) {
355 init_gacela (dirname (argv[0]));
358 else if (mode == 3 && port != 0)
359 start_remote_client (host, port);
361 init_gacela (dirname (argv[0]));
362 scm_shell (argc, argv);