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