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/>.
20 #include <readline/readline.h>
21 #include <readline/history.h>
23 #include "gacela_SDL.h"
24 #include "gacela_GL.h"
25 #include "gacela_FTGL.h"
29 find_matching_paren (int k)
33 int end_parens_found = 0;
35 /* Choose the corresponding opening bracket. */
36 if (k == ')') c = '(';
37 else if (k == ']') c = '[';
38 else if (k == '}') c = '{';
40 for (i = rl_point-2; i >= 0; i--)
42 /* Is the current character part of a character literal? */
44 && rl_line_buffer[i - 1] == '\\'
45 && rl_line_buffer[i - 2] == '#')
47 else if (rl_line_buffer[i] == k)
49 else if (rl_line_buffer[i] == '"')
51 /* Skip over a string literal. */
52 for (i--; i >= 0; i--)
53 if (rl_line_buffer[i] == '"'
55 && rl_line_buffer[i - 1] == '\\'))
58 else if (rl_line_buffer[i] == c)
60 if (end_parens_found==0) return i;
61 else --end_parens_found;
68 match_paren (int x, int k)
73 struct timeval timeout;
77 /* Did we just insert a quoted paren? If so, then don't bounce. */
79 && rl_line_buffer[rl_point - 2] == '\\')
83 timeout.tv_sec = tmp / 1000000;
84 timeout.tv_usec = tmp % 1000000;
86 fno = fileno (rl_instream);
87 FD_SET (fno, &readset);
92 rl_point = find_matching_paren (k);
96 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
104 ctrl_c_handler (int signum)
106 printf ("ERROR: User interrupt\nABORT: (signal)\n");
110 init_gacela_client ()
112 struct sigaction new_action;
114 /* init bouncing parens */
115 rl_bind_key (')', match_paren);
116 rl_bind_key (']', match_paren);
117 rl_bind_key ('}', match_paren);
120 new_action.sa_handler = ctrl_c_handler;
121 sigemptyset (&new_action.sa_mask);
122 new_action.sa_flags = 0;
124 sigaction (SIGINT, &new_action, NULL);
133 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
135 init_gacela_client ();
136 read_history (history_path);
139 line = readline ("gacela> ");
143 printf ("%s\n", line);
149 write_history (history_path);
154 init_gacela (void *data, int argc, char **argv)
156 // Guile configuration
157 scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
158 scm_c_eval_string ("(use-modules (ice-9 readline))");
159 scm_c_eval_string ("(activate-readline)");
160 scm_c_eval_string ("(use-modules (ice-9 optargs))");
161 scm_c_eval_string ("(use-modules (ice-9 receive))");
162 // scm_c_eval_string ("(read-enable 'case-insensitive)");
164 // Bindings for C functions and structs
165 SDL_register_functions (NULL);
166 GL_register_functions (NULL);
167 FTGL_register_functions (NULL);
173 load_scheme_files (char *path)
175 char load_path[strlen (path) + 1024];
177 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
178 scm_c_eval_string (load_path);
179 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
183 main (int argc, char *argv[])
186 scm_with_guile (&init_gacela, NULL);
187 load_scheme_files (dirname (argv[0]));
188 //scm_shell (argc, argv);
189 scm_c_eval_string ("(start-server 1234)");
190 scm_c_eval_string ("(run-game)");