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