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