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>
22 #include "gacela_SDL.h"
23 #include "gacela_GL.h"
24 #include "gacela_FTGL.h"
28 find_matching_paren (int k)
32 int end_parens_found = 0;
34 /* Choose the corresponding opening bracket. */
35 if (k == ')') c = '(';
36 else if (k == ']') c = '[';
37 else if (k == '}') c = '{';
39 for (i = rl_point-2; i >= 0; i--)
41 /* Is the current character part of a character literal? */
43 && rl_line_buffer[i - 1] == '\\'
44 && rl_line_buffer[i - 2] == '#')
46 else if (rl_line_buffer[i] == k)
48 else if (rl_line_buffer[i] == '"')
50 /* Skip over a string literal. */
51 for (i--; i >= 0; i--)
52 if (rl_line_buffer[i] == '"'
54 && rl_line_buffer[i - 1] == '\\'))
57 else if (rl_line_buffer[i] == c)
59 if (end_parens_found==0) return i;
60 else --end_parens_found;
67 match_paren (int x, int k)
72 struct timeval timeout;
76 /* Did we just insert a quoted paren? If so, then don't bounce. */
78 && rl_line_buffer[rl_point - 2] == '\\')
82 timeout.tv_sec = tmp / 1000000;
83 timeout.tv_usec = tmp % 1000000;
85 fno = fileno (rl_instream);
86 FD_SET (fno, &readset);
91 rl_point = find_matching_paren (k);
95 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
103 init_gacela_client ()
105 /* init bouncing parens */
106 rl_bind_key (')', match_paren);
107 rl_bind_key (']', match_paren);
108 rl_bind_key ('}', match_paren);
111 scm_c_eval_string ("(sigaction SIGINT (lambda (sig) (format #t \"ERROR: User interrupt~%ABORT: (signal)~%\")))");
119 init_gacela_client ();
122 line = readline ("gacela>");
123 printf ("%s\n", line);
126 if (strcmp(line, "fuera") == 0)
133 init_gacela (void *data, int argc, char **argv)
135 // Guile configuration
136 scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
137 scm_c_eval_string ("(use-modules (ice-9 readline))");
138 scm_c_eval_string ("(activate-readline)");
139 scm_c_eval_string ("(use-modules (ice-9 optargs))");
140 scm_c_eval_string ("(use-modules (ice-9 receive))");
141 scm_c_eval_string ("(read-enable 'case-insensitive)");
143 // Bindings for C functions and structs
144 SDL_register_functions (NULL);
145 GL_register_functions (NULL);
146 FTGL_register_functions (NULL);
152 load_scheme_files (char *path)
154 char load_path[strlen (path) + 1024];
156 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
157 scm_c_eval_string (load_path);
158 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
162 main (int argc, char *argv[])
165 scm_with_guile (&init_gacela, NULL);
166 load_scheme_files (dirname (argv[0]));
167 scm_shell (argc, argv);