]> 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 static int
35 find_matching_paren (int k)
36 {
37   register int i;
38   register char c = 0;
39   int end_parens_found = 0;
40
41   // Choose the corresponding opening bracket
42   if (k == ')') c = '(';
43   else if (k == ']') c = '[';
44   else if (k == '}') c = '{';
45
46   for (i = rl_point-2; i >= 0; i--)
47     {
48       // Is the current character part of a character literal?
49       if (i - 2 >= 0
50           && rl_line_buffer[i - 1] == '\\'
51           && rl_line_buffer[i - 2] == '#')
52         ;
53       else if (rl_line_buffer[i] == k)
54         end_parens_found++;
55       else if (rl_line_buffer[i] == '"')
56         {
57           // Skip over a string literal
58           for (i--; i >= 0; i--)
59             if (rl_line_buffer[i] == '"'
60                 && ! (i - 1 >= 0
61                       && rl_line_buffer[i - 1] == '\\'))
62               break;
63         }
64       else if (rl_line_buffer[i] == c)
65         {
66           if (end_parens_found==0) return i;
67           else --end_parens_found;
68         }
69     }
70   return -1;
71 }
72
73 static int
74 match_paren (int x, int k)
75 {
76   int tmp;
77   int fno;
78   SELECT_TYPE readset;
79   struct timeval timeout;
80
81   rl_insert (x, k);
82
83   // Did we just insert a quoted paren?  If so, then don't bounce
84   if (rl_point - 1 >= 1
85       && rl_line_buffer[rl_point - 2] == '\\')
86     return 0;
87
88   tmp = 500000;
89   timeout.tv_sec = tmp / 1000000;
90   timeout.tv_usec = tmp % 1000000;
91   FD_ZERO (&readset);
92   fno = fileno (rl_instream);
93   FD_SET (fno, &readset);
94
95   if (rl_point > 1)
96     {
97       tmp = rl_point;
98       rl_point = find_matching_paren (k);
99       if (rl_point > -1)
100         {
101           rl_redisplay ();
102           scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
103         }
104       rl_point = tmp;
105     }
106   return 0;
107 }
108
109 void
110 ctrl_c_handler (int signum)
111 {
112   printf ("ERROR: User interrupt\nABORT: (signal)\n");
113 }
114      
115 static void
116 init_gacela_client ()
117 {
118   struct sigaction new_action;
119
120   // init bouncing parens
121   rl_bind_key (')', match_paren);
122   rl_bind_key (']', match_paren);
123   rl_bind_key ('}', match_paren);
124
125   // SIGINT
126   new_action.sa_handler = ctrl_c_handler;
127   sigemptyset (&new_action.sa_mask);
128   new_action.sa_flags = 0;
129
130   sigaction (SIGINT, &new_action, NULL);
131 }
132
133 void
134 gacela_client (char *hostname, int port)
135 {
136   int sockfd;
137   struct hostent *server;
138   struct sockaddr_in serv_addr;
139
140   char *line;
141   char *history_path;
142
143   // Connect to the server
144   sockfd = socket (AF_INET, SOCK_STREAM, 0);
145   server = gethostbyname (hostname);
146   bzero ((char *) &serv_addr, sizeof (serv_addr));
147   serv_addr.sin_family = AF_INET;
148   bcopy ((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
149   serv_addr.sin_port = htons (port);
150   connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
151
152   // Command line
153   asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
154
155   init_gacela_client ();
156   read_history (history_path);
157
158   while (1) {
159     line = readline ("gacela> ");
160     if (!line) break;
161     if (line && *line)
162       {
163         printf ("%s\n", line);
164         add_history (line);
165       }
166     free (line);
167   }
168
169   write_history (history_path);
170   free (history_path);
171 }
172
173 static void*
174 init_gacela (void *data, int argc, char **argv)
175 {
176   // Guile configuration
177   scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
178   scm_c_eval_string ("(use-modules (ice-9 readline))");
179   scm_c_eval_string ("(activate-readline)");
180   scm_c_eval_string ("(use-modules (ice-9 optargs))");
181   scm_c_eval_string ("(use-modules (ice-9 receive))");
182   //  scm_c_eval_string ("(read-enable 'case-insensitive)");
183
184   // Bindings for C functions and structs
185   SDL_register_functions (NULL);
186   GL_register_functions (NULL);
187   FTGL_register_functions (NULL);
188
189   return NULL;
190 }
191
192 void
193 load_scheme_files (char *path)
194 {
195   char load_path[strlen (path) + 1024];
196
197   sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
198   scm_c_eval_string (load_path);
199   scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
200 }
201
202 void
203 start_single (char *working_path)
204 {
205   char *argv = "guile";
206
207   scm_with_guile (&init_gacela, NULL);
208   load_scheme_files (working_path);
209   scm_shell (1, &argv);
210 }
211
212 void
213 start_server (char *working_path, int port)
214 {
215   char start_server[100];
216
217   scm_with_guile (&init_gacela, NULL);
218   load_scheme_files (working_path);
219   sprintf (start_server, "(start-server %d)", port);
220   scm_c_eval_string (start_server);
221   scm_c_eval_string ("(game-loop)");
222 }
223
224 void
225 start_client (char *hostname, int port)
226 {
227   scm_init_guile ();
228   gacela_client (hostname, port);
229 }
230
231 int
232 main (int argc, char *argv[])
233 {
234   char *arg;
235   int mode = 0;   // shell: 1, server: 2, client: 3
236   char *host;
237   int port = 0;
238   int i;
239
240   // Checking arguments
241   for (i = 1; i < argc; i++) {
242     if (strcmp (argv[i], "--shell-mode") == 0)
243       mode = 1;
244     else if (strncmp (argv[i], "--server", 8) == 0) {
245       mode = 2;
246       arg = strtok (argv[i], "=");
247       arg = strtok (NULL, "=");
248       if (arg != NULL)
249         port = atoi (arg);
250     }
251     else if (strncmp (argv[i], "--client", 8) == 0) {
252       mode = 3;
253       arg = strtok (argv[i], "=");
254       arg = strtok (NULL, "=");
255       if (arg != NULL) {
256         host = strtok (arg, ":");
257         arg = strtok (NULL, ":");
258         if (arg != NULL)
259           port = atoi (arg);
260       }
261     }
262   }
263
264   if (mode == 1)
265     start_single (dirname (argv[0]));
266   else if (mode == 2 && port != 0)
267     start_server (dirname (argv[0]), port);
268   else if (mode == 3 && port != 0)
269     start_client (host, port);
270   /*
271   if (fork () == 0)
272     start_server ();
273   else
274     start_client ("localhost", 1234);
275   */
276 }