]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
release: 1.5.23
[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 #include <string.h> /* strdup, strchr */
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
119 String
120 ly_symbol2string (SCM s)
121 {
122   assert (gh_symbol_p (s));
123   return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
124 }
125
126
127 String
128 gulp_file_to_string (String fn)
129 {
130   String s = global_path.find (fn);
131   if (s == "")
132     {
133       String e = _f ("can't find file: `%s'", fn);
134       e += " ";
135       e += _f ("(load path: `%s')", global_path.str ());
136       error (e);
137     }
138   else if (verbose_global_b)
139     progress_indication ("[" + s);
140
141
142   Simple_file_storage f (s);
143   String result (f.ch_C ());
144   if (verbose_global_b)
145     progress_indication ("]");
146   return result;
147 }
148
149 SCM
150 ly_gulp_file (SCM fn)
151 {
152   return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C ());
153 }
154
155
156 /**
157    Read a file, and shove it down GUILE.  GUILE also has file read
158    functions, but you can't fiddle with the path of those.
159  */
160 void
161 read_lily_scm_file (String fn)
162 {
163   gh_eval_str ((char *) gulp_file_to_string (fn).ch_C ());
164 }
165
166 extern "C" {
167   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
168 void
169 ly_display_scm (SCM s)
170 {
171   gh_display (s);
172   gh_newline ();
173 }
174 };
175
176 String
177 ly_scm2string (SCM s)
178 {
179   assert (gh_string_p (s));
180
181   size_t len; 
182   char *p = gh_scm2newstr (s , &len);
183   
184   String r (p);
185
186   free (p);
187   return r;
188 }
189
190 SCM
191 index_cell (SCM s, Direction d)
192 {
193   assert (d);
194   return (d == LEFT) ? ly_car (s) : ly_cdr (s);
195 }
196
197 SCM
198 index_set_cell (SCM s, Direction d, SCM v)
199 {
200   if (d == LEFT)
201     gh_set_car_x (s, v);
202   else if (d == RIGHT)
203     gh_set_cdr_x (s, v);
204   return s;
205 }
206   
207 SCM
208 ly_warning (SCM str)
209 {
210   assert (gh_string_p (str));
211   warning ("lily-guile: " + ly_scm2string (str));
212   return SCM_BOOL_T;
213 }
214
215 SCM
216 ly_isdir_p (SCM s)
217 {
218   if (gh_number_p (s))
219     {
220       int i = gh_scm2int (s);
221       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
222     }
223   return SCM_BOOL_F;
224 }
225
226 bool
227 ly_number_pair_p (SCM p)
228 {
229   return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
230 }
231
232 bool
233 ly_axis_p (SCM a)
234 {
235   return gh_number_p (a) && (gh_scm2int (a) == 0 || gh_scm2int (a) == 1); 
236 }
237
238 typedef void (*Void_fptr) ();
239 Array<Void_fptr> *scm_init_funcs_;
240
241 void add_scm_init_func (void (*f) ())
242 {
243   if (!scm_init_funcs_)
244     scm_init_funcs_ = new Array<Void_fptr>;
245
246   scm_init_funcs_->push (f);
247 }
248
249 extern  void init_cxx_function_smobs ();
250
251 void
252 prepend_load_path (String p )
253 {
254   char s[1024];
255   sprintf (s, 
256            "(set! %%load-path (cons \"%s\" %%load-path))", p.ch_C());
257
258   scm_c_eval_string (s);
259 }
260
261 void
262 init_lily_guile (String p )
263 {
264   prepend_load_path (p);
265
266   // todo: junk this. We should make real modules iso. just loading files.
267   prepend_load_path (p + "/scm/");
268
269
270 #if GUILE_MINOR_VERSION >= 5
271   SCM last_mod = scm_current_module ();
272   scm_set_current_module (scm_c_resolve_module ("guile"));
273 #endif
274   
275   init_cxx_function_smobs ();
276   for (int i=scm_init_funcs_->size () ; i--;)
277     (scm_init_funcs_->elem (i)) ();
278
279   if (verbose_global_b)
280     progress_indication ("\n");
281   read_lily_scm_file ("lily.scm");
282
283 #if GUILE_MINOR_VERSION >= 5
284   scm_set_current_module (last_mod);
285 #endif
286 }
287
288 unsigned int ly_scm_hash (SCM s)
289 {
290   return scm_ihashv (s, ~1u);
291 }
292
293
294
295 bool
296 isdir_b (SCM s)
297 {
298   if (gh_number_p (s))
299     {
300       int i = gh_scm2int (s);
301       return i>= -1 && i <= 1; 
302     }
303   return false;
304 }
305
306
307 bool
308 isaxis_b (SCM s)
309 {
310   if (gh_number_p (s))
311     {
312       int i = gh_scm2int (s);
313       return i== 0 || i == 1;
314     }
315   return false;
316 }
317
318
319 Direction
320 to_dir (SCM s)
321 {
322   return (Direction) gh_scm2int (s);
323 }
324
325 Interval
326 ly_scm2interval (SCM p)
327 {
328   return  Interval (gh_scm2double (ly_car (p)),
329                     gh_scm2double (ly_cdr (p)));
330 }
331
332 SCM
333 ly_interval2scm (Interval i)
334 {
335   return gh_cons (gh_double2scm (i[LEFT]),
336                   gh_double2scm (i[RIGHT]));
337 }
338
339
340
341
342 bool
343 to_boolean (SCM s)
344 {
345   return gh_boolean_p (s) && gh_scm2bool (s);
346 }
347
348 /*
349   Appendable list L: the cdr contains the list, the car the last cons
350   in the list.
351  */
352 SCM
353 appendable_list ()
354 {
355   SCM s = gh_cons (SCM_EOL, SCM_EOL);
356   gh_set_car_x (s, s);
357   
358   return s;
359 }
360
361 void
362 appendable_list_append (SCM l, SCM elt)
363 {
364   SCM newcons = gh_cons (elt, SCM_EOL);
365   
366   gh_set_cdr_x (ly_car (l), newcons);      
367   gh_set_car_x (l, newcons);
368 }
369
370
371 SCM
372 ly_offset2scm (Offset o)
373 {
374   return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
375 }
376
377 Offset
378 ly_scm2offset (SCM s)
379 {
380   return Offset (gh_scm2double (ly_car (s)),
381                  gh_scm2double (ly_cdr (s)));
382 }
383
384 SCM
385 ly_type (SCM exp)
386 {
387   char const  * cp = "unknown";
388   if (gh_number_p (exp))
389     {
390       cp = "number";
391     }
392   else if (gh_string_p (exp))
393     {
394       cp = "string";
395     }
396   else if (gh_procedure_p (exp))
397     {
398       cp = "procedure";
399     }
400   else if (gh_boolean_p (exp))
401     {
402       cp = "boolean";
403     }
404   else if (gh_pair_p (exp))
405     {
406       cp = "list";
407     }
408
409   return ly_str02scm (cp);
410 }
411
412 /*
413   convert without too many decimals, and leave  a space at the end.
414  */
415    
416    
417 SCM
418 ly_number2string (SCM s)
419 {
420   assert (gh_number_p (s));
421
422   char str[400];                        // ugh.
423
424   if (scm_exact_p (s) == SCM_BOOL_F)
425     {
426       Real r (gh_scm2double (s));
427
428       if (isinf (r) || isnan (r))
429         {
430           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
431           r = 0.0;
432         }
433
434       sprintf (str, "%8.4f ", r);
435     }
436   else
437     {
438       sprintf (str, "%d ", gh_scm2int (s));
439     }
440
441   return ly_str02scm (str);
442 }
443
444 /*
445   Undef this to see if GUILE GC is causing too many swaps.
446  */
447
448 // #define TEST_GC
449
450 #ifdef TEST_GC
451 #include <libguile/gc.h>
452
453 static void *
454 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
455 {
456    fprintf (stderr, "entering sweep\n");
457 }
458
459 static void *
460 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
461 {
462    fprintf (stderr, "leaving sweep\n");
463 }
464 #endif
465
466
467 #include "version.hh"
468 SCM
469 ly_version ()
470 {
471   char const* vs =  "\' (" MAJOR_VERSION " " MINOR_VERSION " "  PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
472   
473   return gh_eval_str ((char*)vs);
474 }
475
476 static void
477 init_functions ()
478 {
479   scm_c_define_gsubr ("ly-warn", 1, 0, 0,
480                       (Scheme_function_unknown)ly_warning);
481   scm_c_define_gsubr ("ly-version", 0, 0, 0,
482                       (Scheme_function_unknown)ly_version);  
483   scm_c_define_gsubr ("ly-gulp-file", 1,0, 0,
484                       (Scheme_function_unknown)ly_gulp_file);
485   scm_c_define_gsubr ("dir?", 1,0, 0, (Scheme_function_unknown)ly_isdir_p);
486   scm_c_define_gsubr ("ly-number->string", 1, 0,0,
487                       (Scheme_function_unknown) ly_number2string);
488
489
490 #ifdef TEST_GC 
491   scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
492   scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
493 #endif
494 }
495
496 ADD_SCM_INIT_FUNC (funcs, init_functions);
497
498 SCM
499 ly_deep_copy (SCM l)
500 {
501   if (gh_pair_p (l))
502     {
503       return gh_cons (ly_deep_copy (ly_car (l)), ly_deep_copy (ly_cdr (l)));
504     }
505   else
506     return l;
507 }
508
509
510
511
512 SCM
513 ly_assoc_chain (SCM key, SCM achain)
514 {
515   if (gh_pair_p (achain))
516     {
517       SCM handle = scm_assoc (key, ly_car (achain));
518       if (gh_pair_p (handle))
519         return handle;
520       else
521         return ly_assoc_chain (key, ly_cdr (achain));
522     }
523   else
524     return SCM_BOOL_F;
525 }
526
527 /*
528   LIST has the form "sym1 sym2 sym3" 
529  */
530 SCM
531 parse_symbol_list (const char * list)
532 {
533   char * s = strdup (list);
534   char *orig = s;
535   SCM create_list = SCM_EOL;
536   if (!s[0] )
537     s = 0;
538
539   while (s)
540     {
541       char *next = strchr (s, ' ');
542       if (next)
543         *next++ = 0;
544
545       create_list = gh_cons (ly_symbol2scm (s), create_list);
546       s = next;
547     }
548
549   free (orig);
550   return create_list;
551 }
552
553
554 SCM
555 ly_truncate_list (int k, SCM l )
556 {
557   if (k == 0)
558     {
559       l = SCM_EOL;
560     }
561   else
562     {
563       SCM s = l;
564       k--;
565       for (; gh_pair_p (s) && k--; s = ly_cdr (s))
566         ;
567
568       if (gh_pair_p (s))
569         {
570           gh_set_cdr_x (s, SCM_EOL);
571         }
572     }
573   return l;
574 }
575
576 SCM my_gh_symbol2scm (const char* x)
577 {
578   return gh_symbol2scm ((char*)x);
579 }