]> git.jsancho.org Git - gacela.git/blob - src/gacela.c
af78da97433302a1a775cd16ffdbb95fe72972a0
[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] == ';') {
182       // Comment until endline
183       break;
184     }
185     else if (line[i] == c)
186       opened++;
187     else if (line[i] == k)
188       opened--;
189   }
190
191   return opened;
192 }
193
194 void
195 gacela_client (SCM rec_channel, SCM send_channel)
196 {
197   int n;
198   SCM buffer;
199   char *line = NULL, *line_for_sending = NULL;
200   int opened = 0;
201
202   // Command line
203   asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
204
205   init_gacela_client ();
206   read_history (history_path);
207
208   while (1) {
209     if (opened > 0)
210       line = readline ("... ");
211     else
212       line = readline ("gacela> ");
213
214     if (!line) break;
215
216     opened += opened_parens (line, ')');
217     ctrl_c = 0;
218
219     if (line && *line)
220       {
221         add_history (line);
222         if (line_for_sending == NULL) {
223           line_for_sending = strdup (line);
224         }
225         else {
226           line_for_sending = realloc (line_for_sending, strlen (line_for_sending) + strlen (line) + 2);
227           line_for_sending = strcat (line_for_sending, "\n");
228           line_for_sending = strcat (line_for_sending, line);
229         }
230
231         if (opened <= 0) {
232           scm_write (scm_from_locale_string (line_for_sending), send_channel);
233           scm_force_output (send_channel);
234           free (line_for_sending);
235           line_for_sending = NULL;
236
237           while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
238             if (ctrl_c) break;
239             sleep (0.5);
240           }
241           if (ctrl_c)
242             ctrl_c = 0;
243           else {
244             buffer = scm_read (rec_channel);
245             if (strlen (scm_to_locale_string (buffer)) > 0)
246               printf ("%s\n", scm_to_locale_string (buffer));
247           }
248         }
249       }
250     free (line);
251   }
252
253   write_history (history_path);
254   free (history_path);
255 }
256
257 static void*
258 init_scheme (void *data, int argc, char **argv)
259 {
260   // Guile configuration
261   scm_c_eval_string ("(set-repl-prompt! \"gacela> \")");
262   scm_c_eval_string ("(use-modules (ice-9 readline))");
263   scm_c_eval_string ("(activate-readline)");
264   scm_c_eval_string ("(use-modules (ice-9 optargs))");
265   scm_c_eval_string ("(use-modules (ice-9 receive))");
266
267   // Bindings for C functions and structs
268   SDL_register_functions (NULL);
269   GL_register_functions (NULL);
270   FTGL_register_functions (NULL);
271
272   return NULL;
273 }
274
275 void
276 load_scheme_files (char *path)
277 {
278   char load_path[strlen (path) + 1024];
279
280   sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
281   scm_c_eval_string (load_path);
282   scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
283 }
284
285 void
286 init_gacela (char *path)
287 {
288   scm_with_guile (&init_scheme, NULL);
289   load_scheme_files (path);
290   scm_c_eval_string ("(init-video-mode)");
291 }
292
293 void
294 start_server (int port)
295 {
296   char *start_server;
297
298   asprintf (&start_server, "(start-server #:port %d)", port);
299   scm_c_eval_string (start_server);
300 }
301
302 void
303 start_local_server (SCM pipes)
304 {
305   char start_server[100];
306
307   scm_c_define ("pipes", pipes);
308   scm_c_eval_string ("(start-server #:pipes pipes)");
309 }
310
311 void
312 start_remote_client (char *hostname, int port)
313 {
314   SCM client_socket;
315   char *connect_to_server;
316
317   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);
318   client_socket = scm_c_eval_string (connect_to_server);
319   gacela_client (client_socket, client_socket);
320 }
321
322 int
323 main (int argc, char *argv[])
324 {
325   char *arg;
326   int mode = 0;   // dev: 1, server: 2, client: 3
327   char *host;
328   int port = 0;
329   int i;
330   SCM fd1, fd2;
331
332   // Checking arguments
333   for (i = 1; i < argc; i++) {
334     if (strcmp (argv[i], "--dev") == 0)
335       mode = 1;
336     else if (strncmp (argv[i], "--server", 8) == 0) {
337       mode = 2;
338       arg = strtok (argv[i], "=");
339       arg = strtok (NULL, "=");
340       if (arg != NULL)
341         port = atoi (arg);
342     }
343     else if (strncmp (argv[i], "--client", 8) == 0) {
344       mode = 3;
345       arg = strtok (argv[i], "=");
346       arg = strtok (NULL, "=");
347       if (arg != NULL) {
348         host = strtok (arg, ":");
349         arg = strtok (NULL, ":");
350         if (arg != NULL)
351           port = atoi (arg);
352       }
353     }
354   }
355
356   scm_init_guile ();
357
358   if (mode == 1) {
359     fd1 = scm_pipe ();
360     fd2 = scm_pipe ();
361     pid = fork ();
362
363     if (pid == 0) {
364       scm_close (SCM_CAR (fd1));
365       scm_close (SCM_CDR (fd2));
366       init_gacela (dirname (argv[0]));
367       start_local_server (scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
368       kill (getppid (), SIGALRM);
369     }
370     else {
371       scm_close (SCM_CDR (fd1));
372       scm_close (SCM_CAR (fd2));
373       gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
374       kill (pid, SIGKILL);
375     }
376   }
377   else if (mode == 2 && port != 0) {
378     init_gacela (dirname (argv[0]));
379     start_server (port);
380   }
381   else if (mode == 3 && port != 0)
382     start_remote_client (host, port);
383   else {
384     init_gacela (dirname (argv[0]));
385     scm_shell (argc, argv);
386     SDL_Quit ();
387   }
388 }