1 /* Gacela, a GNU Guile extension for fast games development
2 Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
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.
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.
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/>.
22 #include <readline/readline.h>
23 #include <readline/history.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
29 #include "gacela_SDL.h"
30 #include "gacela_GL.h"
31 #include "gacela_FTGL.h"
38 find_matching_paren (int k)
42 int end_parens_found = 0;
44 // Choose the corresponding opening bracket
45 if (k == ')') c = '(';
46 else if (k == ']') c = '[';
47 else if (k == '}') c = '{';
49 for (i = rl_point-2; i >= 0; i--)
51 // Is the current character part of a character literal?
53 && rl_line_buffer[i - 1] == '\\'
54 && rl_line_buffer[i - 2] == '#')
56 else if (rl_line_buffer[i] == k)
58 else if (rl_line_buffer[i] == '"')
60 // Skip over a string literal
61 for (i--; i >= 0; i--)
62 if (rl_line_buffer[i] == '"'
64 && rl_line_buffer[i - 1] == '\\'))
67 else if (rl_line_buffer[i] == c)
69 if (end_parens_found==0) return i;
70 else --end_parens_found;
77 match_paren (int x, int k)
82 struct timeval timeout;
86 // Did we just insert a quoted paren? If so, then don't bounce
88 && rl_line_buffer[rl_point - 2] == '\\')
92 timeout.tv_sec = tmp / 1000000;
93 timeout.tv_usec = tmp % 1000000;
95 fno = fileno (rl_instream);
96 FD_SET (fno, &readset);
101 rl_point = find_matching_paren (k);
105 scm_std_select (fno + 1, &readset, NULL, NULL, &timeout);
113 ctrl_c_handler (int signum)
115 printf ("ERROR: User interrupt\nABORT: (signal)\n");
120 init_gacela_client ()
122 struct sigaction new_action;
124 // init bouncing parens
125 rl_bind_key (')', match_paren);
126 rl_bind_key (']', match_paren);
127 rl_bind_key ('}', match_paren);
130 new_action.sa_handler = ctrl_c_handler;
131 sigemptyset (&new_action.sa_mask);
132 new_action.sa_flags = 0;
134 sigaction (SIGINT, &new_action, NULL);
138 gacela_client (char *hostname, int port)
141 struct hostent *server;
142 struct sockaddr_in serv_addr;
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 if (connect (sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr)) == -1) {
155 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);
160 asprintf (&history_path, "%s/.gacela_history", getenv("HOME"));
162 init_gacela_client ();
163 read_history (history_path);
166 line = readline ("gacela> ");
172 write (sockfd, "(", 1);
173 n = write (sockfd, line, strlen (line));
174 write (sockfd, ")", 1);
176 error("ERROR writing to socket");
179 n = read (sockfd, buffer, sizeof (buffer));
183 n = read (sockfd, buffer, sizeof (buffer));
186 error("ERROR reading from socket");
190 printf ("%s\n", buffer);
196 write_history (history_path);
201 init_gacela (void *data, int argc, char **argv)
203 // Guile configuration
204 scm_c_eval_string ("(set-repl-prompt! \"gacela>\")");
205 scm_c_eval_string ("(use-modules (ice-9 readline))");
206 scm_c_eval_string ("(activate-readline)");
207 scm_c_eval_string ("(use-modules (ice-9 optargs))");
208 scm_c_eval_string ("(use-modules (ice-9 receive))");
209 // scm_c_eval_string ("(read-enable 'case-insensitive)");
211 // Bindings for C functions and structs
212 SDL_register_functions (NULL);
213 GL_register_functions (NULL);
214 FTGL_register_functions (NULL);
220 load_scheme_files (char *path)
222 char load_path[strlen (path) + 1024];
224 sprintf (load_path, "(set! %%load-path (cons \"%s\" %%load-path))", path);
225 scm_c_eval_string (load_path);
226 scm_primitive_load_path (scm_from_locale_string ("gacela_loader.scm"));
230 start_single (char *working_path)
232 char *argv = "guile";
234 scm_with_guile (&init_gacela, NULL);
235 load_scheme_files (working_path);
236 scm_shell (1, &argv);
240 start_server (char *working_path, int port)
242 char start_server[100];
244 scm_with_guile (&init_gacela, NULL);
245 load_scheme_files (working_path);
246 sprintf (start_server, "(start-server %d)", port);
247 scm_c_eval_string (start_server);
251 start_client (char *hostname, int port)
254 gacela_client (hostname, port);
258 main (int argc, char *argv[])
261 int mode = 0; // shell: 1, server: 2, client: 3
266 // Checking arguments
267 for (i = 1; i < argc; i++) {
268 if (strcmp (argv[i], "--shell-mode") == 0)
270 else if (strncmp (argv[i], "--server", 8) == 0) {
272 arg = strtok (argv[i], "=");
273 arg = strtok (NULL, "=");
277 else if (strncmp (argv[i], "--client", 8) == 0) {
279 arg = strtok (argv[i], "=");
280 arg = strtok (NULL, "=");
282 host = strtok (arg, ":");
283 arg = strtok (NULL, ":");
291 start_single (dirname (argv[0]));
292 else if (mode == 2 && port != 0)
293 start_server (dirname (argv[0]), port);
294 else if (mode == 3 && port != 0)
295 start_client (host, port);
300 start_client ("localhost", 1234);