]> git.jsancho.org Git - gacela.git/blob - src/gacela.c
489d8c622897b8ad365c35b21898d42579f1afc5
[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 <stdio.h>
19 #include <string.h>
20 #include <libguile.h>
21 #include <libgen.h>
22 #include <readline/readline.h>
23 #include <readline/history.h>
24 #include <signal.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netdb.h>
28
29 #include "gacela_SDL.h"
30 #include "gacela_GL.h"
31 #include "gacela_FTGL.h"
32
33 // Generic variables
34 int ctrl_c = 0;
35
36 static int
37 find_matching_paren (int k)
38 {
39   register int i;
40   register char c = 0;
41   int end_parens_found = 0;
42
43   // Choose the corresponding opening bracket
44   if (k == ')') c = '(';
45   else if (k == ']') c = '[';
46   else if (k == '}') c = '{';
47
48   for (i = rl_point-2; i >= 0; i--)
49     {
50       // Is the current character part of a character literal?
51       if (i - 2 >= 0
52           && rl_line_buffer[i - 1] == '\\'
53           && rl_line_buffer[i - 2] == '#')
54         ;
55       else if (rl_line_buffer[i] == k)
56         end_parens_found++;
57       else if (rl_line_buffer[i] == '"')
58         {
59           // Skip over a string literal
60           for (i--; i >= 0; i--)
61             if (rl_line_buffer[i] == '"'
62                 && ! (i - 1 >= 0
63                       && rl_line_buffer[i - 1] == '\\'))
64               break;
65         }
66       else if (rl_line_buffer[i] == c)
67         {
68           if (end_parens_found==0) return i;
69           else --end_parens_found;
70         }
71     }
72   return -1;
73 }
74
75 static int
76 match_paren (int x, int k)
77 {
78   int tmp;
79   int fno;
80   SELECT_TYPE readset;
81   struct timeval timeout;
82
83   rl_insert (x, k);
84
85   // Did we just insert a quoted paren?  If so, then don't bounce
86   if (rl_point - 1 >= 1
87       && rl_line_buffer[rl_point - 2] == '\\')
88     return 0;
89
90   tmp = 500000;
91   timeout.tv_sec = tmp / 1000000;
92   timeout.tv_usec = tmp % 1000000;
93   FD_ZERO (&readset);
94   fno = fileno (rl_instream);
95   FD_SET (fno, &readset);
96
97   if (rl_point > 1)
98     {
99       tmp = rl_point;
100       rl_point = find_matching_paren (k);
101       if (rl_point > -1)
102         {
103           rl_redisplay ();
104           scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
105         }
106       rl_point = tmp;
107     }
108   return 0;
109 }
110
111 void
112 ctrl_c_handler (int signum)
113 {
114   printf ("ERROR: User interrupt\nABORT: (signal)\n");
115   ctrl_c = 1;
116 }
117      
118 static void
119 init_gacela_client ()
120 {
121   struct sigaction new_action;
122
123   // init bouncing parens
124   rl_bind_key (')', match_paren);
125   rl_bind_key (']', match_paren);
126   rl_bind_key ('}', match_paren);
127
128   // SIGINT
129   new_action.sa_handler = ctrl_c_handler;
130   sigemptyset (&new_action.sa_mask);
131   new_action.sa_flags = 0;
132
133   sigaction (SIGINT, &new_action, NULL);
134 }
135
136 int
137 opened_parens (char *line, int k)
138 {
139   int i;
140   int opened = 0;
141   char c = 0;
142
143   // Choose the corresponding opening bracket
144   if (k == ')') c = '(';
145   else if (k == ']') c = '[';
146   else if (k == '}') c = '{';
147
148   for (i = 0; i < strlen (line); i++) {
149     if (line[i] == c)
150       opened++;
151     else if (line[i] == k)
152       opened--;
153   }
154
155   return opened;
156 }
157
158 void
159 gacela_client (SCM rec_channel, SCM send_channel)
160 {
161   int n;
162   SCM buffer;
163   char *line, *line_for_sending;
164   char *history_path;
165   int opened = 0;
166
167   // Command line
168   asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
169
170   init_gacela_client ();
171   read_history (history_path);
172
173   while (1) {
174     if (opened > 0)
175       line = readline ("... ");
176     else
177       line = readline ("gacela> ");
178
179     opened += opened_parens (line, ')');
180     ctrl_c = 0;
181     if (!line) break;
182     if (line && *line)
183       {
184         add_history (line);
185         if (opened == 0) {
186           scm_write (scm_from_locale_string (line), send_channel);
187           scm_force_output (send_channel);
188
189           while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
190             if (ctrl_c) break;
191             sleep (0.5);
192           }
193           if (ctrl_c)
194             ctrl_c = 0;
195           else {
196             buffer = scm_read (rec_channel);
197             if (strlen (scm_to_locale_string (buffer)) > 0)
198               printf ("%s\n", scm_to_locale_string (buffer));
199           }
200         }
201       }
202     free (line);
203   }
204
205   write_history (history_path);
206   free (history_path);
207 }
208
209 static void*
210 init_scheme (void *data, int argc, char **argv)
211 {
212   // Guile configuration
213   scm_c_eval_string ("(set-repl-prompt! \"gacela> \")");
214   scm_c_eval_string ("(use-modules (ice-9 readline))");
215   scm_c_eval_string ("(activate-readline)");
216   scm_c_eval_string ("(use-modules (ice-9 optargs))");
217   scm_c_eval_string ("(use-modules (ice-9 receive))");
218
219   // Bindings for C functions and structs
220   SDL_register_functions (NULL);
221   GL_register_functions (NULL);
222   FTGL_register_functions (NULL);
223
224   return NULL;
225 }
226
227 void
228 load_scheme_files (char *path)
229 {
230   char load_path[strlen (path) + 1024];
231
232   sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
233   scm_c_eval_string (load_path);
234   scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
235 }
236
237 void
238 init_gacela (char *path)
239 {
240   scm_with_guile (&init_scheme, NULL);
241   load_scheme_files (path);
242   scm_c_eval_string ("(init-video-mode)");
243 }
244
245 void
246 start_server (int port)
247 {
248   char *start_server;
249
250   asprintf (&start_server, "(start-server #:port %d)", port);
251   scm_c_eval_string (start_server);
252 }
253
254 void
255 start_local_server (SCM pipes)
256 {
257   char start_server[100];
258
259   scm_c_define ("pipes", pipes);
260   scm_c_eval_string ("(start-server #:pipes pipes)");
261 }
262
263 void
264 start_remote_client (char *hostname, int port)
265 {
266   SCM client_socket;
267   char *connect_to_server;
268
269   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);
270   client_socket = scm_c_eval_string (connect_to_server);
271   gacela_client (client_socket, client_socket);
272 }
273
274 int
275 main (int argc, char *argv[])
276 {
277   char *arg;
278   int mode = 0;   // dev: 1, server: 2, client: 3
279   char *host;
280   int port = 0;
281   int i;
282   SCM fd1, fd2;
283   int pid;
284
285   // Checking arguments
286   for (i = 1; i < argc; i++) {
287     if (strcmp (argv[i], "--dev") == 0)
288       mode = 1;
289     else if (strncmp (argv[i], "--server", 8) == 0) {
290       mode = 2;
291       arg = strtok (argv[i], "=");
292       arg = strtok (NULL, "=");
293       if (arg != NULL)
294         port = atoi (arg);
295     }
296     else if (strncmp (argv[i], "--client", 8) == 0) {
297       mode = 3;
298       arg = strtok (argv[i], "=");
299       arg = strtok (NULL, "=");
300       if (arg != NULL) {
301         host = strtok (arg, ":");
302         arg = strtok (NULL, ":");
303         if (arg != NULL)
304           port = atoi (arg);
305       }
306     }
307   }
308
309   scm_init_guile ();
310
311   if (mode == 1) {
312     fd1 = scm_pipe ();
313     fd2 = scm_pipe ();
314     pid = fork ();
315
316     if (pid == 0) {
317       scm_close (SCM_CAR (fd1));
318       scm_close (SCM_CDR (fd2));
319       init_gacela (dirname (argv[0]));
320       start_local_server (scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
321     }
322     else {
323       scm_close (SCM_CDR (fd1));
324       scm_close (SCM_CAR (fd2));
325       gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
326       kill (pid, SIGKILL);
327     }
328   }
329   else if (mode == 2 && port != 0) {
330     init_gacela (dirname (argv[0]));
331     start_server (port);
332   }
333   else if (mode == 3 && port != 0)
334     start_remote_client (host, port);
335   else {
336     init_gacela (dirname (argv[0]));
337     scm_shell (argc, argv);
338     SDL_Quit ();
339   }
340 }