]> 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
34 // Generic variables
35 int ctrl_c = 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 static void
120 init_gacela_client ()
121 {
122   struct sigaction new_action;
123
124   // init bouncing parens
125   rl_bind_key (')', match_paren);
126   rl_bind_key (']', match_paren);
127   rl_bind_key ('}', match_paren);
128
129   // SIGINT
130   new_action.sa_handler = ctrl_c_handler;
131   sigemptyset (&new_action.sa_mask);
132   new_action.sa_flags = 0;
133
134   sigaction (SIGINT, &new_action, NULL);
135 }
136
137 void
138 gacela_client (char *hostname, int port)
139 {
140   int sockfd, n;
141   struct hostent *server;
142   struct sockaddr_in serv_addr;
143   char buffer[256];
144   char *line;
145   char *history_path;
146
147   // Connect to the server
148   sockfd = socket (AF_INET, SOCK_STREAM, 0);
149   server = gethostbyname (hostname);
150   bzero ((char *) &serv_addr, sizeof (serv_addr));
151   serv_addr.sin_family = AF_INET;
152   bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
153   serv_addr.sin_port = htons (port);
154   connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
155
156   // Command line
157   asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
158
159   init_gacela_client ();
160   read_history (history_path);
161
162   while (1) {
163     line = readline ("gacela> ");
164     ctrl_c = 0;
165     if (!line) break;
166     if (line && *line)
167       {
168         add_history (line);
169         write (sockfd, "(", 1);
170         n = write (sockfd, line, strlen (line));
171         write (sockfd, ")", 1);
172         if (n < 0)
173           error("ERROR writing to socket");
174
175         bzero (buffer, 256);
176         n = read (sockfd, buffer, sizeof (buffer));
177         while (n == 0) {
178           if (ctrl_c) break;
179           sleep (1);
180           n = read (sockfd, buffer, sizeof (buffer));
181         }
182         if (n < 0)
183           error("ERROR reading from socket");
184         if (ctrl_c)
185           ctrl_c = 0;
186         else
187           printf ("%s\n", buffer);
188       }
189     free (line);
190   }
191
192   close (sockfd);
193   write_history (history_path);
194   free (history_path);
195 }
196
197 static void*
198 init_gacela (void *data, int argc, char **argv)
199 {
200   // Guile configuration
201   scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
202   scm_c_eval_string ("(use-modules (ice-9 readline))");
203   scm_c_eval_string ("(activate-readline)");
204   scm_c_eval_string ("(use-modules (ice-9 optargs))");
205   scm_c_eval_string ("(use-modules (ice-9 receive))");
206   //  scm_c_eval_string ("(read-enable 'case-insensitive)");
207
208   // Bindings for C functions and structs
209   SDL_register_functions (NULL);
210   GL_register_functions (NULL);
211   FTGL_register_functions (NULL);
212
213   return NULL;
214 }
215
216 void
217 load_scheme_files (char *path)
218 {
219   char load_path[strlen (path) + 1024];
220
221   sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
222   scm_c_eval_string (load_path);
223   scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
224 }
225
226 void
227 start_single (char *working_path)
228 {
229   char *argv = "guile";
230
231   scm_with_guile (&init_gacela, NULL);
232   load_scheme_files (working_path);
233   scm_shell (1, &argv);
234 }
235
236 void
237 start_server (char *working_path, int port)
238 {
239   char start_server[100];
240
241   scm_with_guile (&init_gacela, NULL);
242   load_scheme_files (working_path);
243   sprintf (start_server, "(start-server %d)", port);
244   scm_c_eval_string (start_server);
245 }
246
247 void
248 start_client (char *hostname, int port)
249 {
250   scm_init_guile ();
251   gacela_client (hostname, port);
252 }
253
254 int
255 main (int argc, char *argv[])
256 {
257   char *arg;
258   int mode = 0;   // shell: 1, server: 2, client: 3
259   char *host;
260   int port = 0;
261   int i;
262
263   // Checking arguments
264   for (i = 1; i < argc; i++) {
265     if (strcmp (argv[i], "--shell-mode") == 0)
266       mode = 1;
267     else if (strncmp (argv[i], "--server", 8) == 0) {
268       mode = 2;
269       arg = strtok (argv[i], "=");
270       arg = strtok (NULL, "=");
271       if (arg != NULL)
272         port = atoi (arg);
273     }
274     else if (strncmp (argv[i], "--client", 8) == 0) {
275       mode = 3;
276       arg = strtok (argv[i], "=");
277       arg = strtok (NULL, "=");
278       if (arg != NULL) {
279         host = strtok (arg, ":");
280         arg = strtok (NULL, ":");
281         if (arg != NULL)
282           port = atoi (arg);
283       }
284     }
285   }
286
287   if (mode == 1)
288     start_single (dirname (argv[0]));
289   else if (mode == 2 && port != 0)
290     start_server (dirname (argv[0]), port);
291   else if (mode == 3 && port != 0)
292     start_client (host, port);
293   /*
294   if (fork () == 0)
295     start_server ();
296   else
297     start_client ("localhost", 1234);
298   */
299 }