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