]> git.jsancho.org Git - gacela.git/blob - src/gacela.c
(no commit message)
[gacela.git] / src / gacela.c
1 /* Gacela, a GNU Guile extension for fast games development
2    Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3
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.
8
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.
13
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/>.
16 */
17
18 #include <libguile.h>
19 #include <libgen.h>
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"
25
26
27 static int
28 find_matching_paren (int k)
29 {
30   register int i;
31   register char c = 0;
32   int end_parens_found = 0;
33
34   /* Choose the corresponding opening bracket.  */
35   if (k == ')') c = '(';
36   else if (k == ']') c = '[';
37   else if (k == '}') c = '{';
38
39   for (i = rl_point-2; i >= 0; i--)
40     {
41       /* Is the current character part of a character literal?  */
42       if (i - 2 >= 0
43           && rl_line_buffer[i - 1] == '\\'
44           && rl_line_buffer[i - 2] == '#')
45         ;
46       else if (rl_line_buffer[i] == k)
47         end_parens_found++;
48       else if (rl_line_buffer[i] == '"')
49         {
50           /* Skip over a string literal.  */
51           for (i--; i >= 0; i--)
52             if (rl_line_buffer[i] == '"'
53                 && ! (i - 1 >= 0
54                       && rl_line_buffer[i - 1] == '\\'))
55               break;
56         }
57       else if (rl_line_buffer[i] == c)
58         {
59           if (end_parens_found==0) return i;
60           else --end_parens_found;
61         }
62     }
63   return -1;
64 }
65
66 static int
67 match_paren (int x, int k)
68 {
69   int tmp;
70   int fno;
71   SELECT_TYPE readset;
72   struct timeval timeout;
73
74   rl_insert (x, k);
75
76   /* Did we just insert a quoted paren?  If so, then don't bounce.  */
77   if (rl_point - 1 >= 1
78       && rl_line_buffer[rl_point - 2] == '\\')
79     return 0;
80
81   tmp = 500000;
82   timeout.tv_sec = tmp / 1000000;
83   timeout.tv_usec = tmp % 1000000;
84   FD_ZERO (&readset);
85   fno = fileno (rl_instream);
86   FD_SET (fno, &readset);
87
88   if (rl_point > 1)
89     {
90       tmp = rl_point;
91       rl_point = find_matching_paren (k);
92       if (rl_point > -1)
93         {
94           rl_redisplay ();
95           scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
96         }
97       rl_point = tmp;
98     }
99   return 0;
100 }
101
102 static void
103 init_gacela_client ()
104 {
105   /* init bouncing parens */
106   rl_bind_key (')', match_paren);
107   rl_bind_key (']', match_paren);
108   rl_bind_key ('}', match_paren);
109
110   /* SIGINT */
111   scm_c_eval_string ("(sigaction SIGINT (lambda (sig) (format #t \"ERROR: User interrupt~%ABORT: (signal)~%\")))");
112 }
113
114 void
115 gacela_client (void)
116 {
117   char *line;
118
119   init_gacela_client ();
120
121   while (1) {
122     line = readline ("gacela>");
123     printf ("%s\n", line);
124     if (line && *line)
125       add_history (line);
126     if (strcmp(line, "fuera") == 0)
127       break;
128     free (line);
129   }
130 }
131
132 static void*
133 init_gacela (void *data, int argc, char **argv)
134 {
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)");
142
143   // Bindings for C functions and structs
144   SDL_register_functions (NULL);
145   GL_register_functions (NULL);
146   FTGL_register_functions (NULL);
147
148   return NULL;
149 }
150
151 void
152 load_scheme_files (char *path)
153 {
154   char load_path[strlen (path) + 1024];
155
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"));
159 }
160
161 int
162 main (int argc, char *argv[])
163 {
164   /*
165   scm_with_guile (&init_gacela, NULL);
166   load_scheme_files (dirname (argv[0]));
167   scm_shell (argc, argv);
168   */
169   scm_init_guile ();
170   gacela_client ();
171 }