]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
patch::: 1.5.11.jcn3
[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--2001 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 #include <math.h>               // isinf
15
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
18 #include "main.hh"
19 #include "simple-file-storage.hh"
20 #include "file-path.hh"
21 #include "debug.hh"
22 #include "direction.hh"
23 #include "offset.hh"
24 #include "interval.hh"
25
26 SCM
27 ly_last (SCM list)
28 {
29   return ly_car (scm_last_pair (list));
30 }
31
32 SCM
33 ly_str02scm (char const*c)
34 {
35   // this all really sucks, guile should take char const* arguments!
36   return gh_str02scm ((char*)c);
37 }
38
39
40 SCM
41 ly_write2scm (SCM s)
42 {
43   SCM port = scm_mkstrport (SCM_INUM0, 
44                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
45                             SCM_OPN | SCM_WRTNG,
46                             "ly_write2string");
47   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
48   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
49   
50   // scm_apply (write, port, SCM_EOL);
51   gh_call2 (write, s, port);
52   return scm_strport_to_string (port);
53 }
54
55
56 /*
57   Pass string to scm parser, evaluate one expression.
58   Return result value and #chars read.
59
60   Thanks to Gary Houston <ghouston@freewire.co.uk>
61
62   Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
63 */
64 SCM
65 ly_parse_scm (char const* s, int* n)
66 {
67   SCM str = ly_str02scm (s);
68   SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
69                             "ly_eval_scm_0str");
70   SCM from = scm_ftell (port);
71
72   SCM form;
73   SCM answer = SCM_UNSPECIFIED;
74
75   /* Read expression from port */
76   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
77     answer = scm_primitive_eval (form);
78  
79   /*
80    After parsing
81
82  (begin (foo 1 2))
83
84    all seems fine, but after parsing
85
86  (foo 1 2)
87
88    read_buf has been advanced to read_pos - 1,
89    so that scm_ftell returns 1, instead of #parsed chars
90    */
91   
92   /*
93     urg: reset read_buf for scm_ftell
94     shouldn't scm_read () do this for us?
95   */
96   scm_fill_input (port);
97   SCM to = scm_ftell (port);
98   *n = gh_scm2int (to) - gh_scm2int (from);
99
100   /* Don't close the port here; if we re-enter this function via a
101      continuation, then the next time we enter it, we'll get an error.
102      It's a string port anyway, so there's no advantage to closing it
103      early.
104
105      scm_close_port (port);
106   */
107
108   return answer;
109 }
110
111 SCM
112 ly_quote_scm (SCM s)
113 {
114   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
115 }
116
117
118 SCM
119 ly_symbol2scm (const char *s)
120 {
121   return gh_symbol2scm ((char *)s);
122 }
123
124
125 String
126 ly_symbol2string (SCM s)
127 {
128   assert (gh_symbol_p (s));
129   return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
130 }
131
132
133 String
134 gulp_file_to_string (String fn)
135 {
136   String s = global_path.find (fn);
137   if (s == "")
138     {
139       String e = _f ("can't find file: `%s'", fn);
140       e += " ";
141       e += _f ("(load path: `%s')", global_path.str ());
142       error (e);
143     }
144   else if (verbose_global_b)
145     progress_indication ("[" + s);
146
147
148   Simple_file_storage f (s);
149   String result (f.ch_C ());
150   if (verbose_global_b)
151     progress_indication ("]");
152   return result;
153 }
154
155 SCM
156 ly_gulp_file (SCM fn)
157 {
158   return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C ());
159 }
160
161
162 /**
163    Read a file, and shove it down GUILE.  GUILE also has file read
164    functions, but you can't fiddle with the path of those.
165  */
166 void
167 read_lily_scm_file (String fn)
168 {
169   scm_c_eval_string ((char *) gulp_file_to_string (fn).ch_C ());
170 }
171
172 extern "C" {
173   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
174 void
175 ly_display_scm (SCM s)
176 {
177   gh_display (s);
178   gh_newline ();
179 }
180 };
181
182 String
183 ly_scm2string (SCM s)
184 {
185   assert (gh_string_p (s));
186
187   size_t len; 
188   char *p = gh_scm2newstr (s , &len);
189   
190   String r (p);
191
192   free (p);
193   return r;
194 }
195
196 SCM
197 index_cell (SCM s, Direction d)
198 {
199   assert (d);
200   return (d == LEFT) ? ly_car (s) : ly_cdr (s);
201 }
202
203 SCM
204 index_set_cell (SCM s, Direction d, SCM v)
205 {
206   if (d == LEFT)
207     gh_set_car_x (s, v);
208   else if (d == RIGHT)
209     gh_set_cdr_x (s, v);
210   return s;
211 }
212   
213 SCM
214 ly_warning (SCM str)
215 {
216   assert (gh_string_p (str));
217   warning ("lily-guile: " + ly_scm2string (str));
218   return SCM_BOOL_T;
219 }
220
221 SCM
222 ly_isdir_p (SCM s)
223 {
224   if (gh_number_p (s))
225     {
226       int i = gh_scm2int (s);
227       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
228     }
229   return SCM_BOOL_F;
230 }
231
232 bool
233 ly_number_pair_p (SCM p)
234 {
235   return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
236 }
237
238 bool
239 ly_axis_p (SCM a)
240 {
241   return gh_number_p (a) && (gh_scm2int (a) == 0 || gh_scm2int (a) == 1); 
242 }
243
244 typedef void (*Void_fptr) ();
245 Array<Void_fptr> *scm_init_funcs_;
246
247 void add_scm_init_func (void (*f) ())
248 {
249   if (!scm_init_funcs_)
250     scm_init_funcs_ = new Array<Void_fptr>;
251
252   scm_init_funcs_->push (f);
253 }
254 extern  void init_cxx_function_smobs ();
255
256 void
257 init_lily_guile ()
258 {
259   init_cxx_function_smobs ();
260   for (int i=scm_init_funcs_->size () ; i--;)
261  (scm_init_funcs_->elem (i)) ();
262 }
263
264 unsigned int ly_scm_hash (SCM s)
265 {
266   return scm_ihashv (s, ~1u);
267 }
268
269
270
271 bool
272 isdir_b (SCM s)
273 {
274   if (gh_number_p (s))
275     {
276       int i = gh_scm2int (s);
277       return i>= -1 && i <= 1; 
278     }
279   return false;
280 }
281
282
283 bool
284 isaxis_b (SCM s)
285 {
286   if (gh_number_p (s))
287     {
288       int i = gh_scm2int (s);
289       return i== 0 || i == 1;
290     }
291   return false;
292 }
293
294
295 Direction
296 to_dir (SCM s)
297 {
298   return (Direction) gh_scm2int (s);
299 }
300
301 Interval
302 ly_scm2interval (SCM p)
303 {
304   return  Interval (gh_scm2double (ly_car (p)),
305                     gh_scm2double (ly_cdr (p)));
306 }
307
308 SCM
309 ly_interval2scm (Interval i)
310 {
311   return gh_cons (gh_double2scm (i[LEFT]),
312                   gh_double2scm (i[RIGHT]));
313 }
314
315
316
317
318 bool
319 to_boolean (SCM s)
320 {
321   return gh_boolean_p (s) && gh_scm2bool (s);
322 }
323
324 /*
325   Appendable list L: the cdr contains the list, the car the last cons
326   in the list.
327  */
328 SCM
329 appendable_list ()
330 {
331   SCM s = gh_cons (SCM_EOL, SCM_EOL);
332   gh_set_car_x (s, s);
333   
334   return s;
335 }
336
337 void
338 appendable_list_append (SCM l, SCM elt)
339 {
340   SCM newcons = gh_cons (elt, SCM_EOL);
341   
342   gh_set_cdr_x (ly_car (l), newcons);      
343   gh_set_car_x (l, newcons);
344 }
345
346
347 SCM
348 ly_offset2scm (Offset o)
349 {
350   return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
351 }
352
353 Offset
354 ly_scm2offset (SCM s)
355 {
356   return Offset (gh_scm2double (ly_car (s)),
357                  gh_scm2double (ly_cdr (s)));
358 }
359
360 SCM
361 ly_type (SCM exp)
362 {
363   char const  * cp = "unknown";
364   if (gh_number_p (exp))
365     {
366       cp = "number";
367     }
368   else if (gh_string_p (exp))
369     {
370       cp = "string";
371     }
372   else if (gh_procedure_p (exp))
373     {
374       cp = "procedure";
375     }
376   else if (gh_boolean_p (exp))
377     {
378       cp = "boolean";
379     }
380   else if (gh_pair_p (exp))
381     {
382       cp = "list";
383     }
384
385   return ly_str02scm (cp);
386 }
387
388 /*
389   convert without too many decimals, and leave  a space at the end.
390  */
391    
392    
393 SCM
394 ly_number2string (SCM s)
395 {
396   assert (gh_number_p (s));
397
398   char str[400];                        // ugh.
399
400   if (scm_integer_p (s) == SCM_BOOL_F)
401     {
402       Real r (gh_scm2double (s));
403
404       if (isinf (r) || isnan (r))
405         {
406           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
407           r = 0.0;
408         }
409
410       sprintf (str, "%8.4f ", r);
411     }
412   else
413     {
414       sprintf (str, "%d ", gh_scm2int (s));
415     }
416
417   return ly_str02scm (str);
418 }
419
420 /*
421   Undef this to see if GUILE GC is causing too many swaps.
422  */
423
424 // #define TEST_GC
425
426 #ifdef TEST_GC
427 #include <libguile/gc.h>
428
429 static void *
430 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
431 {
432    fprintf (stderr, "entering sweep\n");
433 }
434
435 static void *
436 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
437 {
438    fprintf (stderr, "leaving sweep\n");
439 }
440 #endif
441
442
443 #include "version.hh"
444 SCM
445 ly_version ()
446 {
447   char const* vs =  "\' (" MAJOR_VERSION " " MINOR_VERSION " "  PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
448   
449   return scm_c_eval_string ((char*)vs);
450 }
451
452 static void
453 init_functions ()
454 {
455   scm_c_define_gsubr ("ly-warn", 1, 0, 0,
456                       (Scheme_function_unknown)ly_warning);
457   scm_c_define_gsubr ("ly-version", 0, 0, 0,
458                       (Scheme_function_unknown)ly_version);  
459   scm_c_define_gsubr ("ly-gulp-file", 1,0, 0,
460                       (Scheme_function_unknown)ly_gulp_file);
461   scm_c_define_gsubr ("dir?", 1,0, 0, (Scheme_function_unknown)ly_isdir_p);
462   scm_c_define_gsubr ("ly-number->string", 1, 0,0,
463                       (Scheme_function_unknown) ly_number2string);
464
465
466 #ifdef TEST_GC 
467   scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
468   scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
469 #endif
470 }
471
472 ADD_SCM_INIT_FUNC (funcs, init_functions);
473
474 SCM
475 ly_deep_copy (SCM l)
476 {
477   if (gh_pair_p (l))
478     {
479       return gh_cons (ly_deep_copy (ly_car (l)), ly_deep_copy (ly_cdr (l)));
480     }
481   else
482     return l;
483 }
484
485
486
487
488 SCM
489 ly_assoc_chain (SCM key, SCM achain)
490 {
491   if (gh_pair_p (achain))
492     {
493       SCM handle = scm_assoc (key, ly_car (achain));
494       if (gh_pair_p (handle))
495         return handle;
496       else
497         return ly_assoc_chain (key, ly_cdr (achain));
498     }
499   else
500     return SCM_BOOL_F;
501 }