]> 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 <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 void
137 gacela_client (SCM rec_channel, SCM send_channel)
138 {
139   int n;
140   SCM buffer;
141   char *line;
142   char *history_path;
143
144   // Command line
145   asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
146
147   init_gacela_client ();
148   read_history (history_path);
149
150   while (1) {
151     line = readline ("gacela> ");
152     ctrl_c = 0;
153     if (!line) break;
154     if (line && *line)
155       {
156         add_history (line);
157         //scm_write (scm_from_locale_string ("("), send_channel);
158         scm_write (scm_from_locale_string (line), send_channel);
159         //scm_write (scm_from_locale_string (")"), send_channel);
160         scm_force_output (send_channel);
161
162         while (scm_char_ready_p (rec_channel) == SCM_BOOL_F) {
163           if (ctrl_c) break;
164           sleep (1);
165         }
166         if (ctrl_c)
167           ctrl_c = 0;
168         else {
169           buffer = scm_read (rec_channel);
170           printf ("%s\n", scm_to_locale_string (buffer));
171         }
172       }
173     free (line);
174   }
175
176   write_history (history_path);
177   free (history_path);
178 }
179
180 static void*
181 init_gacela (void *data, int argc, char **argv)
182 {
183   // Guile configuration
184   scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
185   scm_c_eval_string ("(use-modules (ice-9 readline))");
186   scm_c_eval_string ("(activate-readline)");
187   scm_c_eval_string ("(use-modules (ice-9 optargs))");
188   scm_c_eval_string ("(use-modules (ice-9 receive))");
189
190   // Bindings for C functions and structs
191   SDL_register_functions (NULL);
192   GL_register_functions (NULL);
193   FTGL_register_functions (NULL);
194
195   return NULL;
196 }
197
198 void
199 load_scheme_files (char *path)
200 {
201   char load_path[strlen (path) + 1024];
202
203   sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
204   scm_c_eval_string (load_path);
205   scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
206 }
207
208 void
209 start_single (char *working_path)
210 {
211   char *argv = "guile";
212
213   scm_with_guile (&init_gacela, NULL);
214   load_scheme_files (working_path);
215   scm_shell (1, &argv);
216 }
217
218 void
219 start_server (char *working_path, int port)
220 {
221   char start_server[100];
222
223   scm_with_guile (&init_gacela, NULL);
224   load_scheme_files (working_path);
225   sprintf (start_server, "(start-server %d)", port);
226   scm_c_eval_string (start_server);
227 }
228
229 void
230 start_local_server (char *working_path, SCM pipes)
231 {
232   char start_server[100];
233
234   scm_with_guile (&init_gacela, NULL);
235   load_scheme_files (working_path);
236   scm_c_define ("pipes", pipes);
237   scm_c_eval_string ("(start-server pipes)");
238 }
239 /*
240 void
241 start_remote_client (char *hostname, int port)
242 {
243   int sockfd;
244   struct hostent *server;
245   struct sockaddr_in serv_addr;
246
247   // Connect to the server
248   sockfd = socket (AF_INET, SOCK_STREAM, 0);
249   server = gethostbyname (hostname);
250   bzero ((char *) &serv_addr, sizeof (serv_addr));
251   serv_addr.sin_family = AF_INET;
252   bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
253   serv_addr.sin_port = htons (port);
254   if (connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) == -1) {
255     printf ("%s [%d.%d.%d.%d] %d: Connection refused\n", hostname, server->h_addr[0], server->h_addr[1], server->h_addr[2], server->h_addr[3], port);
256   }
257   else {
258     gacela_client (sockfd, sockfd);
259     close (sockfd);
260   }
261 }
262 */
263 int
264 main (int argc, char *argv[])
265 {
266   char *arg;
267   int mode = 0;   // shell: 1, server: 2, client: 3
268   char *host;
269   int port = 0;
270   int i;
271   SCM fd1, fd2;
272   int pid;
273
274   // Checking arguments
275   for (i = 1; i < argc; i++) {
276     if (strcmp (argv[i], "--shell-mode") == 0)
277       mode = 1;
278     else if (strncmp (argv[i], "--server", 8) == 0) {
279       mode = 2;
280       arg = strtok (argv[i], "=");
281       arg = strtok (NULL, "=");
282       if (arg != NULL)
283         port = atoi (arg);
284     }
285     else if (strncmp (argv[i], "--client", 8) == 0) {
286       mode = 3;
287       arg = strtok (argv[i], "=");
288       arg = strtok (NULL, "=");
289       if (arg != NULL) {
290         host = strtok (arg, ":");
291         arg = strtok (NULL, ":");
292         if (arg != NULL)
293           port = atoi (arg);
294       }
295     }
296   }
297
298   scm_init_guile ();
299
300   if (mode == 1)
301     start_single (dirname (argv[0]));
302   else if (mode == 2 && port != 0)
303     start_server (dirname (argv[0]), port);
304   else if (mode == 3 && port != 0)
305     //start_remote_client (host, port);
306     return;
307   else {
308     fd1 = scm_pipe ();
309     fd2 = scm_pipe ();
310     pid = fork ();
311
312     if (pid == 0) {
313       scm_close (SCM_CAR (fd1));
314       scm_close (SCM_CDR (fd2));
315       start_local_server (dirname (argv[0]), scm_cons (SCM_CAR (fd2), SCM_CDR (fd1)));
316     }
317     else {
318       scm_close (SCM_CDR (fd1));
319       scm_close (SCM_CAR (fd2));
320       gacela_client (SCM_CAR (fd1), SCM_CDR (fd2));
321       kill (pid, SIGKILL);
322     }
323
324     /*
325     SCM buffer;
326     if (pid == 0) {
327       printf ("Hijo: 0\n");
328       scm_write (scm_from_locale_string ("Hola mundo\n"), SCM_CDR (pipes));
329       scm_force_output (SCM_CDR (pipes));
330       printf ("Hijo: 1\n");
331     }
332     else {
333       printf ("Padre: 0\n");
334       buffer = scm_read (SCM_CAR (pipes));
335       printf ("%s\n", scm_to_locale_string (buffer));
336       printf ("Padre: 1\n");
337     }
338     */
339   }
340 }