]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
release: 1.3.6
[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--1999 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
22 SCM
23 ly_str02scm (char const*c)
24 {
25   // this all really sucks, guile should take char const* arguments!
26   return gh_str02scm ((char*)c);
27 }
28
29 SCM
30 ly_eval_str (char const*c)
31 {
32   // this all really sucks, guile should take char const* arguments!
33   return gh_eval_str ((char*)c);
34 }
35
36   
37 /*
38   Pass string to scm parser, evaluate one expression.
39   Return result value and #chars read.
40
41   Thanks to Gary Houston <ghouston@freewire.co.uk>
42
43   Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
44 */
45 SCM
46 ly_parse_scm (char const* s, int* n)
47 {
48   SCM str = gh_str02scm ((char*)s);
49   SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
50                             "scm_eval_0str");
51   SCM from = scm_ftell (port);
52
53   SCM form;
54   SCM answer = SCM_UNSPECIFIED;
55
56   /* Read expression from port */
57   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
58     answer = scm_eval_x (form);
59
60   /*
61    After parsing
62
63        (begin (foo 1 2))
64
65    all seems fine, but after parsing
66
67        (foo 1 2)
68
69    read_buf has been advanced to read_pos - 1,
70    so that scm_ftell returns 1, instead of #parsed chars
71    */
72   
73   /*
74     urg: reset read_buf for scm_ftell
75     shouldn't scm_read () do this for us?
76   */
77   scm_fill_input (port);
78   SCM to = scm_ftell (port);
79   *n = gh_scm2int (to) - gh_scm2int (from);
80
81   /* Don't close the port here; if we re-enter this function via a
82      continuation, then the next time we enter it, we'll get an error.
83      It's a string port anyway, so there's no advantage to closing it
84      early.
85
86      scm_close_port (port);
87   */
88
89   return answer;
90 }
91
92 /*
93   scm_m_quote doesn't use any env, but needs one for a good signature in GUILE.
94 */
95 // apparently env arg is ignored.
96 SCM
97 ly_quote_scm (SCM s)
98 {
99   return scm_m_quote (scm_cons2 (SCM_EOL, s, SCM_EOL) ,SCM_EOL);
100 }
101
102
103 SCM
104 ly_symbol2scm(const char *s)
105 {
106   return gh_symbol2scm ((char *)s);
107 }
108
109 String
110 ly_symbol2string (SCM s)
111 {
112   return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
113 }
114
115 #if 0
116 SCM
117 ly_set_x (String name, SCM val)
118 {
119   return scm_sysintern ((char*)name.ch_C(), val);
120   
121 }
122 #endif
123
124 /**
125    Read a file, and shove it down GUILE.  GUILE also has file read
126    functions, but you can't fiddle with the path of those.
127  */
128 void
129 read_lily_scm_file (String fn)
130 {
131   String s = global_path.find (fn);
132   if (s == "")
133     {
134       String e = _f ("Can't find file: `%s'", fn);
135       e += " ";
136       e += _f ("(load path: `%s')", global_path.str ());
137       error (e);
138     }
139   else
140     *mlog << '[' << s;
141
142
143   Simple_file_storage f(s);
144   
145   ly_eval_str ((char *) f.ch_C());
146   *mlog << "]" << flush;  
147 }
148
149
150 SCM
151 ly_gulp_file (SCM name)
152 {
153   String fn (ly_scm2string (name));
154  String s = global_path.find (fn);
155   if (s == "")
156     {
157       String e = _f ("Can't find file: `%s'", fn);
158       e += " ";
159       e += _f ("(load path: `%s')", global_path.str ());
160       error (e);
161     }
162   else
163     *mlog << '[' << s;
164
165
166   Simple_file_storage f(s);
167   SCM result = ly_str02scm (f.ch_C());
168   *mlog << "]";
169   return result;
170 }
171
172 void
173 ly_display_scm (SCM s)
174 {
175   gh_display (s);
176   gh_newline ();
177 }
178
179 String
180 ly_scm2string (SCM s)
181 {
182   int len; 
183   char * p = gh_scm2newstr (s , &len);
184   
185   String r (p);
186
187   free (p);
188   return r;
189 }
190
191 SCM
192 index_cell (SCM s, Direction d)
193 {
194   assert (d);
195   return (d == LEFT) ? SCM_CAR (s) : SCM_CDR (s);
196 }
197
198   
199 SCM
200 array_to_list (SCM *a , int l)
201 {
202   SCM list = SCM_EOL;
203   for (int i= l; i--;  )
204     {
205       list =  gh_cons (a[i], list);
206     }
207   return list;
208 }
209
210 SCM
211 ly_warning (SCM str)
212 {
213   assert (gh_string_p (str));
214   warning ("lily-guile: " + ly_scm2string (str));
215   return SCM_BOOL_T;
216 }
217
218 SCM
219 ly_isdir_p (SCM s)
220 {
221   if (gh_number_p (s))
222     {
223       int i = gh_scm2int (s);
224       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
225     }
226   return SCM_BOOL_F;
227 }
228
229
230 void
231 init_functions ()
232 {
233   scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
234   scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
235   scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);  
236 }
237
238 ADD_SCM_INIT_FUNC(funcs, init_functions);
239
240
241 typedef void (*Void_fptr)();
242 Array<Void_fptr> *scm_init_funcs_;
243
244 void add_scm_init_func (void (*f)())
245 {
246   if (!scm_init_funcs_)
247     scm_init_funcs_ = new Array<Void_fptr>;
248
249   scm_init_funcs_->push (f);
250 }
251
252 void
253 init_lily_guile ()
254 {
255   for (int i=scm_init_funcs_->size() ; i--;)
256     (scm_init_funcs_->elem (i)) ();
257 }
258
259 unsigned int ly_scm_hash (SCM s)
260 {
261   return scm_ihashv (s, ~1u);
262 }