1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 1998--2021 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "lily-guile.hh"
22
23 #include "dimensions.hh"
24 #include "direction.hh"
25 #include "file-path.hh"
26 #include "international.hh"
27 #include "main.hh"
28 #include "memory.hh"
29 #include "misc.hh"
30 #include "offset.hh"
31 #include "pitch.hh"
32 #include "std-vector.hh"
33 #include "string-convert.hh"
34 #include "source-file.hh"
35 #include "warn.hh"
36 #include "lily-imports.hh"
37
38 #include <cctype>
39 #include <cstdio>
40 #include <cstdlib>
41 #include <cstring> /* strdup, strchr */
42
43 using std::string;
44 using std::vector;
45
46 /*
47 symbols/strings.
48 */
49 string
ly_scm_write_string(SCM s)50 ly_scm_write_string (SCM s)
51 {
52 SCM port = scm_mkstrport (SCM_INUM0,
53 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
54 SCM_OPN | SCM_WRTNG,
55 "ly_write2string");
56 scm_write (s, port);
57 return ly_scm2string (scm_strport_to_string (port));
58 }
59
60 string
ly_symbol2string(SCM s)61 ly_symbol2string (SCM s)
62 {
63 /*
64 Ugh. this is not very efficient.
65 */
66 return ly_scm2string (scm_symbol_to_string (s));
67 }
68
69 string
robust_symbol2string(SCM sym,const string & str)70 robust_symbol2string (SCM sym, const string &str)
71 {
72 return scm_is_symbol (sym) ? ly_symbol2string (sym) : str;
73 }
74
75 string
gulp_file_to_string(const string & fn,bool must_exist,int size)76 gulp_file_to_string (const string &fn, bool must_exist, int size)
77 {
78 string s = global_path.find (fn);
79 if (s == "")
80 {
81 if (must_exist)
82 {
83 string e = _f ("cannot find file: `%s'", fn);
84 e += " ";
85 char buf[PATH_MAX];
86 char *cwd = getcwd (buf, PATH_MAX);
87 e += _f ("(load path: `%s', cwd: `%s')", global_path.to_string (),
88 cwd);
89 error (e);
90 /* unreachable */
91 }
92 return s;
93 }
94
95 debug_output ("[" + s, true);
96
97 string result = gulp_file (s, size);
98
99 debug_output ("]\n", false);
100
101 return result;
102 }
103
104 extern "C" {
105 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
106 void
ly_display_scm(SCM s)107 ly_display_scm (SCM s)
108 {
109 scm_display (s, scm_current_output_port ());
110 scm_newline (scm_current_output_port ());
111 }
112 };
113
114 /*
115 STRINGS
116 */
117 string
ly_scm2string(SCM str)118 ly_scm2string (SCM str)
119 {
120 assert (scm_is_string (str));
121
122 // GUILE 1.8 with -lmcheck barfs because realloc with sz==0 returns
123 // NULL.
124 if (scm_c_string_length (str) == 0)
125 {
126 return string ();
127 }
128
129 string result;
130 size_t len;
131 unique_stdlib_ptr<char> c_string (scm_to_utf8_stringn (str, &len));
132 if (len)
133 {
134 result.assign (c_string.get (), len);
135 }
136 return result;
137 }
138
139 SCM
ly_string2scm(string const & str)140 ly_string2scm (string const &str)
141 {
142 return scm_from_utf8_stringn (str.c_str (), str.length ());
143 }
144
145 unique_stdlib_ptr<char>
ly_scm2str0(SCM str)146 ly_scm2str0 (SCM str)
147 {
148 return unique_stdlib_ptr<char> (scm_to_utf8_string (str));
149 }
150
151 /*
152 PAIRS
153 */
154 SCM
index_get_cell(SCM s,Direction d)155 index_get_cell (SCM s, Direction d)
156 {
157 assert (d);
158 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
159 }
160
161 SCM
index_set_cell(SCM s,Direction d,SCM v)162 index_set_cell (SCM s, Direction d, SCM v)
163 {
164 if (d == LEFT)
165 scm_set_car_x (s, v);
166 else if (d == RIGHT)
167 scm_set_cdr_x (s, v);
168 return s;
169 }
170
171 bool
is_number_pair(SCM p)172 is_number_pair (SCM p)
173 {
174 return scm_is_pair (p)
175 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
176 }
177
178 /*
179 OFFSET
180 */
from_scm(const SCM & s)181 template <> Offset from_scm<Offset> (const SCM &s)
182 {
183 return Offset (from_scm<Real> (scm_car (s)), from_scm<Real> (scm_cdr (s)));
184 }
185
to_scm(const Offset & i)186 template <> SCM to_scm<Offset> (const Offset &i)
187 {
188 return scm_cons (to_scm (i[X_AXIS]), to_scm (i[Y_AXIS]));
189 }
190
191 /*
192 ALIST
193 */
194 SCM
ly_alist_vals(SCM alist)195 ly_alist_vals (SCM alist)
196 {
197 SCM x = SCM_EOL;
198 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
199 x = scm_cons (scm_cdar (p), x);
200 return x;
201 }
202
203 /*
204 LISTS
205 */
206
207 /* Return I-th element, or last elt L. If I < 0, then we take the first
208 element.
209
210 PRE: length (L) > 0 */
211 SCM
robust_list_ref(int i,SCM l)212 robust_list_ref (int i, SCM l)
213 {
214 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
215 l = scm_cdr (l);
216 return scm_car (l);
217 }
218
219 SCM
ly_deep_copy(SCM src)220 ly_deep_copy (SCM src)
221 {
222 if (scm_is_pair (src))
223 {
224 SCM res = SCM_EOL;
225 do
226 {
227 res = scm_cons (ly_deep_copy (scm_car (src)), res);
228 src = scm_cdr (src);
229 }
230 while (scm_is_pair (src));
231 // Oh, come on, GUILE. Why do you require the second argument
232 // of scm_reverse_x to be a proper list? That makes no sense.
233 // return scm_reverse_x (res, ly_deep_copy (src));
234 SCM last_cons = res;
235 res = scm_reverse_x (res, SCM_EOL);
236 scm_set_cdr_x (last_cons, ly_deep_copy (src));
237 return res;
238 }
239 if (scm_is_vector (src))
240 {
241 vsize len = scm_c_vector_length (src);
242 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
243 for (vsize i = 0; i < len; i++)
244 {
245 scm_c_vector_set_x (nv, i, ly_deep_copy (scm_c_vector_ref (src, i)));
246 }
247 return nv;
248 }
249 return src;
250 }
251
252 string
print_scm_val(SCM val)253 print_scm_val (SCM val)
254 {
255 string realval = ly_scm_write_string (val);
256 if (realval.length () > 200)
257 realval = realval.substr (0, 100)
258 + "\n :\n :\n"
259 + realval.substr (realval.length () - 100);
260 return realval;
261 }
262
263 bool
type_check_assignment(SCM sym,SCM val,SCM type_symbol)264 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
265 {
266
267 // If undefined, some internal function caused it...should never happen.
268 assert (!SCM_UNBNDP (val));
269 if (!scm_is_symbol (sym))
270 return false;
271
272 SCM type = scm_object_property (sym, type_symbol);
273
274 if (!scm_is_null (type) && !ly_is_procedure (type))
275 {
276 warning (_f ("cannot find property type-check for `%s' (%s).",
277 ly_symbol2string (sym).c_str (),
278 ly_symbol2string (type_symbol).c_str ())
279 + " " + _ ("perhaps a typing error?"));
280
281 /* Be strict when being anal :) */
282 if (do_internal_type_checking_global)
283 scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
284 sym, val));
285
286 warning (_ ("skipping assignment"));
287 return false;
288 }
289
290 /*
291 Always succeeds.
292
293
294 TODO: should remove #f from allowed vals?
295 */
296 if (scm_is_null (val) || scm_is_false (val))
297 return true;
298
299 if (!scm_is_null (val)
300 && ly_is_procedure (type)
301 && scm_is_false (scm_call_1 (type, val)))
302 {
303 SCM type_name = Lily::type_name (type);
304
305 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
306 ly_symbol2string (sym).c_str (),
307 print_scm_val (val),
308 ly_scm2string (type_name).c_str ()));
309 return false;
310 }
311 return true;
312 }
313
314 /*
315 display stuff without using stack
316 */
317 SCM
display_list(SCM s)318 display_list (SCM s)
319 {
320 SCM p = scm_current_output_port ();
321
322 scm_puts ("(", p);
323 for (; scm_is_pair (s); s = scm_cdr (s))
324 {
325 scm_display (scm_car (s), p);
326 scm_puts (" ", p);
327 }
328 scm_puts (")", p);
329 return SCM_UNSPECIFIED;
330 }
331
332 // Needed as complement to int_list_to_slice since scm_c_memq refuses
333 // to work with dotted lists.
334
335 SCM
ly_memv(SCM v,SCM l)336 ly_memv (SCM v, SCM l)
337 {
338 for (; scm_is_pair (l); l = scm_cdr (l))
339 if (scm_is_true (scm_eqv_p (v, scm_car (l))))
340 return l;
341 return SCM_BOOL_F;
342 }
343
344 Slice
int_list_to_slice(SCM l)345 int_list_to_slice (SCM l)
346 {
347 Slice s;
348 s.set_empty ();
349 for (; scm_is_pair (l); l = scm_cdr (l))
350 if (scm_is_number (scm_car (l)))
351 s.add_point (scm_to_int (scm_car (l)));
352 return s;
353 }
354
355 string
robust_scm2string(SCM k,const string & s)356 robust_scm2string (SCM k, const string &s)
357 {
358 if (scm_is_string (k))
359 return ly_scm2string (k);
360 return s;
361 }
362
363 template <>
to_scm(const Rational & r)364 SCM to_scm<Rational> (const Rational &r)
365 {
366 if (isinf (r))
367 {
368 if (r > Rational (0))
369 return scm_inf ();
370
371 return scm_difference (scm_inf (), SCM_UNDEFINED);
372 }
373
374 return scm_divide (to_scm (r.numerator ()),
375 to_scm (r.denominator ()));
376 }
377
378 template <>
from_scm(const SCM & r)379 Rational from_scm<Rational> (const SCM &r)
380 {
381 if (scm_is_true (scm_inf_p (r)))
382 {
383 if (scm_is_true (scm_positive_p (r)))
384 {
385 return Rational::infinity ();
386 }
387 else
388 {
389 return -Rational::infinity ();
390 }
391 }
392
393 return Rational (scm_to_int64 (scm_numerator (r)),
394 scm_to_int64 (scm_denominator (r)));
395 }
396
397 template <>
is_scm(SCM n)398 bool is_scm<Rational> (SCM n)
399 {
400 return (scm_is_real (n)
401 && (scm_is_true (scm_exact_p (n))
402 || scm_is_true (scm_inf_p (n))));
403 }
404
405 SCM
alist_to_hashq(SCM alist)406 alist_to_hashq (SCM alist)
407 {
408 long i = scm_ilength (alist);
409 if (i < 0)
410 return scm_c_make_hash_table (0);
411
412 SCM tab = scm_c_make_hash_table (i);
413 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
414 {
415 SCM pt = scm_cdar (s);
416 scm_hashq_set_x (tab, scm_caar (s), pt);
417 }
418 return tab;
419 }
420
421 SCM
ly_hash2alist(SCM tab)422 ly_hash2alist (SCM tab)
423 {
424 return Lily::hash_table_to_alist (tab);
425 }
426
427 /*
428 C++ interfacing.
429 */
430
431 string
mangle_cxx_identifier(const char * cxx_id)432 mangle_cxx_identifier (const char *cxx_id)
433 {
434 std::string mangled_id (cxx_id);
435 if (mangled_id.substr (0, 3) == "ly_")
436 mangled_id.replace (0, 3, "ly:");
437 else
438 {
439 mangled_id = String_convert::to_lower (mangled_id);
440 mangled_id = "ly:" + mangled_id;
441 }
442 if (mangled_id.substr (mangled_id.length () - 2) == "_p")
443 mangled_id.replace (mangled_id.length () - 2, 2, "?");
444 else if (mangled_id.substr (mangled_id.length () - 2) == "_x")
445 mangled_id.replace (mangled_id.length () - 2, 2, "!");
446
447 replace_all (&mangled_id, "_less?", "<?");
448 replace_all (&mangled_id, "_2_", "->");
449 replace_all (&mangled_id, "__", "::");
450 replace_all (&mangled_id, '_', '-');
451
452 return mangled_id;
453 }
454
455 SCM
ly_string_array_to_scm(vector<string> a)456 ly_string_array_to_scm (vector<string> a)
457 {
458 SCM s = SCM_EOL;
459 for (vsize i = a.size (); i; i--)
460 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
461 return s;
462 }
463
464 /* SYMBOLS is a whitespace separated list. */
465 SCM
parse_symbol_list(char const * symbols)466 parse_symbol_list (char const *symbols)
467 {
468 while (isspace (*symbols))
469 symbols++;
470 string s = symbols;
471 replace_all (&s, '\n', ' ');
472 replace_all (&s, '\t', ' ');
473 replace_all (&s, " ", " ");
474 return ly_string_array_to_scm (string_split (s, ' '));
475 }
476
477 /* GDB debugging. */
478 struct ly_t_double_cell
479 {
480 SCM a;
481 SCM b;
482 SCM c;
483 SCM d;
484 };
485
486 /* inserts at front, removing duplicates */
ly_assoc_prepend_x(SCM alist,SCM key,SCM val)487 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
488 {
489 return scm_acons (key, val, scm_assoc_remove_x (alist, key));
490 }
491
492 SCM
ly_alist_copy(SCM alist)493 ly_alist_copy (SCM alist)
494 {
495 SCM l = SCM_EOL;
496 SCM *tail = &l;
497 for (; scm_is_pair (alist); alist = scm_cdr (alist))
498 {
499 *tail = scm_acons (scm_caar (alist), scm_cdar (alist), *tail);
500 tail = SCM_CDRLOC (*tail);
501 }
502 return l;
503 }
504