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