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