]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
09eca147be7af061886b97d7e5fdeafd8ef6d8bd
[lilypond.git] / lily / lily-guile.cc
1 /*
2   lily-guile.cc -- implement assorted guile functions
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7
8   Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 */
10
11
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #include "libc-extension.hh"
16 #include "lily-guile.hh"
17 #include "main.hh"
18 #include "simple-file-storage.hh"
19 #include "file-path.hh"
20 #include "debug.hh"
21 #include "direction.hh"
22 #include "offset.hh"
23
24 SCM
25 ly_str02scm (char const*c)
26 {
27   // this all really sucks, guile should take char const* arguments!
28   return gh_str02scm ((char*)c);
29 }
30
31 SCM
32 ly_eval_str (String s)
33 {
34   // this all really sucks, guile should take char const* arguments!
35   return gh_eval_str ((char*)s.ch_C ());
36 }
37
38   
39 /*
40   Pass string to scm parser, evaluate one expression.
41   Return result value and #chars read.
42
43   Thanks to Gary Houston <ghouston@freewire.co.uk>
44
45   Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
46 */
47 SCM
48 ly_parse_scm (char const* s, int* n)
49 {
50   SCM str = gh_str02scm ((char*)s);
51   SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
52                             "scm_eval_0str");
53   SCM from = scm_ftell (port);
54
55   SCM form;
56   SCM answer = SCM_UNSPECIFIED;
57
58   /* Read expression from port */
59   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
60     answer = scm_eval_x (form);
61
62   /*
63    After parsing
64
65        (begin (foo 1 2))
66
67    all seems fine, but after parsing
68
69        (foo 1 2)
70
71    read_buf has been advanced to read_pos - 1,
72    so that scm_ftell returns 1, instead of #parsed chars
73    */
74   
75   /*
76     urg: reset read_buf for scm_ftell
77     shouldn't scm_read () do this for us?
78   */
79   scm_fill_input (port);
80   SCM to = scm_ftell (port);
81   *n = gh_scm2int (to) - gh_scm2int (from);
82
83   /* Don't close the port here; if we re-enter this function via a
84      continuation, then the next time we enter it, we'll get an error.
85      It's a string port anyway, so there's no advantage to closing it
86      early.
87
88      scm_close_port (port);
89   */
90
91   return answer;
92 }
93
94 SCM
95 ly_quote_scm (SCM s)
96 {
97   return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
98 }
99
100
101 SCM
102 ly_symbol2scm(const char *s)
103 {
104   return gh_symbol2scm ((char *)s);
105 }
106
107 String
108 ly_symbol2string (SCM s)
109 {
110   assert (gh_symbol_p (s));
111   return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
112 }
113
114
115 /**
116    Read a file, and shove it down GUILE.  GUILE also has file read
117    functions, but you can't fiddle with the path of those.
118  */
119 void
120 read_lily_scm_file (String fn)
121 {
122   String s = global_path.find (fn);
123   if (s == "")
124     {
125       String e = _f ("Can't find file: `%s'", fn);
126       e += " ";
127       e += _f ("(load path: `%s')", global_path.str ());
128       error (e);
129     }
130   else
131     progress_indication ("[" + s);
132
133
134   Simple_file_storage f(s);
135   
136   ly_eval_str ((char *) f.ch_C());
137   progress_indication ("]");
138 }
139
140
141 SCM
142 ly_gulp_file (SCM name)
143 {
144   String fn (ly_scm2string (name));
145  String s = global_path.find (fn);
146   if (s == "")
147     {
148       String e = _f ("Can't find file: `%s'", fn);
149       e += " ";
150       e += _f ("(load path: `%s')", global_path.str ());
151       error (e);
152     }
153   else
154     progress_indication ("[" + s );
155
156
157   Simple_file_storage f(s);
158   SCM result = ly_str02scm (f.ch_C());
159   progress_indication ("]");
160   return result;
161 }
162
163 void
164 ly_display_scm (SCM s)
165 {
166   gh_display (s);
167   gh_newline ();
168 }
169
170 String
171 ly_scm2string (SCM s)
172 {
173   assert (gh_string_p (s));
174   int len; 
175   char * p = gh_scm2newstr (s , &len);
176   
177   String r (p);
178
179   free (p);
180   return r;
181 }
182
183 SCM
184 index_cell (SCM s, Direction d)
185 {
186   assert (d);
187   return (d == LEFT) ? gh_car  (s) : gh_cdr (s);
188 }
189
190 SCM
191 index_set_cell (SCM s, Direction d, SCM v)
192 {
193   if (d == LEFT)
194     gh_set_car_x (s, v);
195   else if (d == RIGHT)
196     gh_set_cdr_x (s, v);
197   return s;
198 }
199   
200 SCM
201 ly_warning (SCM str)
202 {
203   assert (gh_string_p (str));
204   warning ("lily-guile: " + ly_scm2string (str));
205   return SCM_BOOL_T;
206 }
207
208 SCM
209 ly_isdir_p (SCM s)
210 {
211   if (gh_number_p (s))
212     {
213       int i = gh_scm2int (s);
214       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
215     }
216   return SCM_BOOL_F;
217 }
218
219
220 static void
221 init_functions ()
222 {
223   scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
224   scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
225   scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);  
226 }
227
228 ADD_SCM_INIT_FUNC(funcs, init_functions);
229
230
231 typedef void (*Void_fptr)();
232 Array<Void_fptr> *scm_init_funcs_;
233
234 void add_scm_init_func (void (*f)())
235 {
236   if (!scm_init_funcs_)
237     scm_init_funcs_ = new Array<Void_fptr>;
238
239   scm_init_funcs_->push (f);
240 }
241
242 void
243 init_lily_guile ()
244 {
245   for (int i=scm_init_funcs_->size() ; i--;)
246     (scm_init_funcs_->elem (i)) ();
247 }
248
249 unsigned int ly_scm_hash (SCM s)
250 {
251   return scm_ihashv (s, ~1u);
252 }
253
254
255
256 bool
257 isdir_b (SCM s)
258 {
259   if (gh_number_p (s))
260     {
261       int i = gh_scm2int (s);
262       return i>= -1 && i <= 1; 
263     }
264   return false;
265 }
266
267 Direction
268 to_dir (SCM s)
269 {
270   return (Direction) gh_scm2int (s);
271 }
272
273
274 SCM
275 to_scm (int i)
276 {
277   return gh_int2scm (i);
278 }
279
280 /*
281   UGR. junkme.
282  */
283 int
284 scm_to (SCM s, int* )
285 {
286   return gh_number_p (s) ? gh_scm2int (s) : 0;
287 }
288
289 SCM
290 to_scm (Real r)
291 {
292   return gh_double2scm (r);
293 }
294
295 Real
296 scm_to (SCM s, Real* )
297 {
298   return gh_number_p (s) ? gh_scm2double (s) : 0;
299 }
300
301 bool
302 to_boolean (SCM s)
303 {
304   return gh_boolean_p (s) && gh_scm2bool (s);
305 }
306
307 /*
308   Appendable list L: the cdr contains the list, the car the last cons
309   in the list.
310  */
311 SCM
312 appendable_list ()
313 {
314   SCM s = gh_cons (SCM_EOL, SCM_EOL);
315   gh_set_car_x (s, s);
316   
317   return s;
318 }
319
320 void
321 appendable_list_append (SCM l, SCM elt)
322 {
323   SCM newcons = gh_cons (elt, SCM_EOL);
324   
325   gh_set_cdr_x (gh_car (l), newcons);      
326   gh_set_car_x (l, newcons);
327 }
328
329
330 SCM
331 to_scm (Offset o)
332 {
333   return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]));
334 }
335
336 Offset
337 scm_to (SCM s, Offset*)
338 {
339   return Offset (gh_scm2double (gh_car (s)),
340                  gh_scm2double (gh_cdr (s)));
341 }
342
343 SCM
344 ly_type (SCM exp)
345 {
346   char const  * cp = "unknown";
347   if (gh_number_p (exp))
348     {
349       cp = "number";
350     }
351   else if (gh_string_p (exp))
352     {
353       cp = "string";
354     }
355   else if (gh_procedure_p (exp))
356     {
357       cp = "procedure";
358     }
359   else if (gh_boolean_p (exp))
360     {
361       cp = "boolean";
362     }
363   else if (gh_pair_p (exp))
364     {
365       cp = "list";
366     }
367
368   return ly_str02scm (cp);
369 }