2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2010 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
25 #include <cstring> /* memset */
28 #include "dimensions.hh"
29 #include "file-name.hh"
30 #include "file-path.hh"
31 #include "international.hh"
32 #include "libc-extension.hh"
33 #include "lily-guile.hh"
36 #include "program-option.hh"
37 #include "relocate.hh"
38 #include "string-convert.hh"
42 LY_DEFINE (ly_start_environment, "ly:start-environment",
44 "Return the environment (a list of strings) that was in"
45 " effect at program start.")
50 for (vsize i = 0; i < start_environment_global.size (); i++)
52 *tail = scm_cons (ly_string2scm (start_environment_global[i]),
54 tail = SCM_CDRLOC(*tail);
61 LY_DEFINE (ly_find_file, "ly:find-file",
63 "Return the absolute file name of @var{name},"
64 " or @code{#f} if not found.")
66 LY_ASSERT_TYPE (scm_is_string, name, 1);
68 string nm = ly_scm2string (name);
69 string file_name = global_path.find (nm);
70 if (file_name.empty ())
73 return ly_string2scm (file_name);
77 Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
80 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
81 1, 1, 0, (SCM name, SCM size),
82 "Read @var{size} characters from the file @var{name},"
83 " and return its contents in a string."
84 " If @var{size} is undefined, the entire file is read."
85 " The file is looked up using the search path.")
87 LY_ASSERT_TYPE (scm_is_string, name, 1);
89 if (size != SCM_UNDEFINED)
91 LY_ASSERT_TYPE (scm_is_number, size, 2);
92 sz = scm_to_int (size);
95 string contents = gulp_file_to_string (ly_scm2string (name), true, sz);
96 return scm_from_locale_stringn (contents.c_str (), contents.length ());
99 LY_DEFINE (ly_error, "ly:error",
100 1, 0, 1, (SCM str, SCM rest),
101 "A Scheme callable function to issue the error @var{str}."
102 " The error is formatted with @code{format} and @var{rest}.")
104 LY_ASSERT_TYPE (scm_is_string, str, 1);
105 str = scm_simple_format (SCM_BOOL_F, str, rest);
106 error (ly_scm2string (str));
107 return SCM_UNSPECIFIED;
110 LY_DEFINE (ly_message, "ly:message",
111 1, 0, 1, (SCM str, SCM rest),
112 "A Scheme callable function to issue the message @var{str}."
113 " The message is formatted with @code{format} and @var{rest}.")
115 LY_ASSERT_TYPE (scm_is_string, str, 1);
116 str = scm_simple_format (SCM_BOOL_F, str, rest);
117 message (ly_scm2string (str));
118 return SCM_UNSPECIFIED;
121 LY_DEFINE (ly_progress, "ly:progress",
122 1, 0, 1, (SCM str, SCM rest),
123 "A Scheme callable function to print progress @var{str}."
124 " The message is formatted with @code{format} and @var{rest}.")
126 LY_ASSERT_TYPE (scm_is_string, str, 1);
127 str = scm_simple_format (SCM_BOOL_F, str, rest);
128 progress_indication (ly_scm2string (str));
129 return SCM_UNSPECIFIED;
132 LY_DEFINE (ly_programming_error, "ly:programming-error",
133 1, 0, 1, (SCM str, SCM rest),
134 "A Scheme callable function to issue the internal warning"
135 " @var{str}. The message is formatted with @code{format}"
138 LY_ASSERT_TYPE (scm_is_string, str, 1);
139 str = scm_simple_format (SCM_BOOL_F, str, rest);
141 if (get_program_option ("warning-as-error"))
142 error (ly_scm2string (str));
144 programming_error (ly_scm2string (str));
146 return SCM_UNSPECIFIED;
149 LY_DEFINE (ly_success, "ly:success",
150 1, 0, 1, (SCM str, SCM rest),
151 "A Scheme callable function to issue a success message @var{str}."
152 " The message is formatted with @code{format} and @var{rest}.")
154 LY_ASSERT_TYPE (scm_is_string, str, 1);
155 str = scm_simple_format (SCM_BOOL_F, str, rest);
156 successful (ly_scm2string (str));
157 return SCM_UNSPECIFIED;
160 LY_DEFINE (ly_warning, "ly:warning",
161 1, 0, 1, (SCM str, SCM rest),
162 "A Scheme callable function to issue the warning @var{str}."
163 " The message is formatted with @code{format} and @var{rest}.")
165 LY_ASSERT_TYPE (scm_is_string, str, 1);
166 str = scm_simple_format (SCM_BOOL_F, str, rest);
168 if (get_program_option ("warning-as-error"))
169 error (ly_scm2string (str));
171 warning (ly_scm2string (str));
173 return SCM_UNSPECIFIED;
176 LY_DEFINE (ly_dir_p, "ly:dir?",
178 "Is @var{s} a direction? Valid directions are @code{-1},"
179 " @code{0}, or@tie{}@code{1}, where @code{-1} represents"
180 " left or down, @code{1}@tie{}represents right or up, and @code{0}"
181 " represents a neutral direction.")
183 if (scm_is_number (s))
185 int i = scm_to_int (s);
186 return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
191 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
193 (SCM key, SCM alist, SCM default_value, SCM strict_checking),
194 "Return value if @var{key} in @var{alist}, else @var{default-value}"
195 " (or @code{#f} if not specified). If @var{strict-checking} is set"
196 " to @code{#t} and @var{key} is not in @var{alist}, a programming_error"
199 LY_ASSERT_TYPE(ly_cheap_is_list, alist, 2);
201 SCM handle = scm_assoc (key, alist);
202 if (scm_is_pair (handle))
203 return scm_cdr (handle);
205 if (default_value == SCM_UNDEFINED)
206 default_value = SCM_BOOL_F;
208 if (strict_checking == SCM_BOOL_T)
210 string key_string = ly_scm2string
211 (scm_object_to_string (key, SCM_UNDEFINED));
212 string default_value_string = ly_scm2string
213 (scm_object_to_string (default_value,
215 programming_error ("Cannot find key `" +
217 "' in alist, setting to `" +
218 default_value_string + "'.");
221 return default_value;
224 LY_DEFINE (ly_string_substitute, "ly:string-substitute",
225 3, 0, 0, (SCM a, SCM b, SCM s),
226 "Replace string@tie{}@var{a} by string@tie{}@var{b} in"
227 " string@tie{}@var{s}.")
229 LY_ASSERT_TYPE (scm_is_string, s, 1);
230 LY_ASSERT_TYPE (scm_is_string, b, 2);
231 LY_ASSERT_TYPE (scm_is_string, s, 3);
233 string ss = ly_scm2string (s);
234 replace_all (&ss, ly_scm2string (a),
237 return ly_string2scm (ss);
241 is_not_escape_character (Byte c)
259 LY_DEFINE (ly_string_percent_encode, "ly:string-percent-encode",
261 "Encode all characters in string @var{str} with hexadecimal"
262 " percent escape sequences, with the following exceptions:"
263 " characters @code{-}, @code{.}, @code{/}, and @code{_}; and"
264 " characters in ranges @code{0-9}, @code{A-Z}, and @code{a-z}.")
266 LY_ASSERT_TYPE (scm_is_string, str, 1);
268 string orig_str = ly_scm2string (str);
272 vsize n = orig_str.size ();
276 Byte cur = orig_str[i];
278 if (is_not_escape_character (cur))
283 new_str += String_convert::bin2hex (cur);
289 return ly_string2scm (new_str);
292 LY_DEFINE (ly_number_2_string, "ly:number->string",
294 "Convert @var{s} to a string without generating many decimals.")
296 LY_ASSERT_TYPE (scm_is_number, s, 1);
298 char str[400]; // ugh.
300 if (scm_exact_p (s) == SCM_BOOL_F)
302 Real r (scm_to_double (s));
303 if (isinf (r) || isnan (r))
305 programming_error (_ ("infinity or NaN encountered while converting Real number"));
306 programming_error (_ ("setting to zero"));
311 snprintf (str, sizeof (str), "%.4f", r);
314 snprintf (str, sizeof (str), "%d", int (scm_to_int (s)));
316 return scm_from_locale_string (str);
319 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
320 "Return the current lilypond version as a list, e.g.,"
321 " @code{(1 3 127 uu1)}.")
323 char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
325 return scm_c_eval_string ((char *)vs);
328 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
329 "Return the unit used for lengths as a string.")
331 return scm_from_locale_string (INTERNAL_UNIT);
334 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
335 "Return @var{d} as a number. Used to distinguish length"
336 " variables from normal numbers.")
338 return scm_number_p (d);
344 LY_DEFINE (ly_protects, "ly:protects",
346 "Return hash of protected objects.")
351 LY_DEFINE (ly_gettext, "ly:gettext",
352 1, 0, 0, (SCM original),
353 "A Scheme wrapper function for @code{gettext}.")
355 LY_ASSERT_TYPE (scm_is_string, original, 1);
356 return ly_string2scm (_ (ly_scm2string (original).c_str ()));
359 LY_DEFINE (ly_output_formats, "ly:output-formats",
361 "Formats passed to @option{--format} as a list of strings,"
362 " used for the output.")
364 vector<string> output_formats = string_split (output_format_global, ',');
367 int output_formats_count = output_formats.size ();
368 for (int i = 0; i < output_formats_count; i++)
369 lst = scm_cons (ly_string2scm (output_formats[i]), lst);
374 LY_DEFINE (ly_wide_char_2_utf_8, "ly:wide-char->utf-8",
376 "Encode the Unicode codepoint @var{wc}, an integer, as UTF-8.")
380 LY_ASSERT_TYPE (scm_is_integer, wc, 1);
381 unsigned wide_char = (unsigned) scm_to_int (wc);
384 if (wide_char < 0x0080)
385 *p++ = (char)wide_char;
386 else if (wide_char < 0x0800)
388 *p++ = (char) (((wide_char >> 6)) | 0xC0);
389 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
391 else if (wide_char < 0x10000)
393 *p++ = (char) (((wide_char >> 12)) | 0xE0);
394 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
395 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
399 *p++ = (char) (((wide_char >> 18)) | 0xF0);
400 *p++ = (char) (((wide_char >> 12) & 0x3F) | 0x80);
401 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
402 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
406 return scm_from_locale_string (buf);
409 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
411 "Return effective prefix.")
413 return ly_string2scm (lilypond_datadir);
416 LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
417 2, 2, 0, (SCM key, SCM achain, SCM default_value, SCM strict_checking),
418 "Return value for @var{key} from a list of alists @var{achain}."
419 " If no entry is found, return @var{default-value} or @code{#f} if"
420 " @var{default-value} is not specified. With @var{strict-checking}"
421 " set to @code{#t}, a programming_error is output in such cases.")
423 if (scm_is_pair (achain))
425 SCM handle = scm_assoc (key, scm_car (achain));
426 if (scm_is_pair (handle))
427 return scm_cdr (handle);
429 return ly_chain_assoc_get (key, scm_cdr (achain), default_value);
432 if (strict_checking == SCM_BOOL_T)
434 string key_string = ly_scm2string
435 (scm_object_to_string (key, SCM_UNDEFINED));
436 string default_value_string = ly_scm2string
437 (scm_object_to_string (default_value,
439 programming_error ("Cannot find key `" +
441 "' in achain, setting to `" +
442 default_value_string + "'.");
445 return default_value == SCM_UNDEFINED ? SCM_BOOL_F : default_value;
449 LY_DEFINE (ly_stderr_redirect, "ly:stderr-redirect",
450 1, 1, 0, (SCM file_name, SCM mode),
451 "Redirect stderr to @var{file-name}, opened with @var{mode}.")
453 LY_ASSERT_TYPE (scm_is_string, file_name, 1);
457 if (mode != SCM_UNDEFINED && scm_string_p (mode))
458 m = ly_scm2string (mode);
459 /* dup2 and (fileno (current-error-port)) do not work with mingw'c
462 stderrfile = freopen (ly_scm2string (file_name).c_str (), m.c_str (), stderr);
463 return SCM_UNSPECIFIED;
467 accumulate_symbol (void * /* closure */,
472 return scm_cons (key, result);
475 LY_DEFINE (ly_hash_table_keys, "ly:hash-table-keys",
477 "Return a list of keys in @var{tab}.")
479 return scm_internal_hash_fold ((scm_t_hash_fold_fn) &accumulate_symbol,
483 LY_DEFINE (ly_camel_case_2_lisp_identifier, "ly:camel-case->lisp-identifier",
484 1, 0, 0, (SCM name_sym),
485 "Convert @code{FooBar_Bla} to @code{foo-bar-bla} style symbol.")
487 LY_ASSERT_TYPE (ly_is_symbol, name_sym, 1);
490 TODO: should use strings instead?
493 const string in = ly_symbol2string (name_sym);
494 string result = camel_case_to_lisp_identifier (in);
496 return ly_symbol2scm (result.c_str ());
499 LY_DEFINE (ly_expand_environment, "ly:expand-environment",
501 "Expand @code{$VAR} and @code{$@{VAR@}} in @var{str}.")
503 LY_ASSERT_TYPE (scm_is_string, str, 1);
505 return ly_string2scm (expand_environment_variables (ly_scm2string (str)));
509 LY_DEFINE (ly_truncate_list_x, "ly:truncate-list!",
510 2, 0, 0, (SCM lst, SCM i),
511 "Take at most the first @var{i} of list @var{lst}.")
513 LY_ASSERT_TYPE (scm_is_integer, i, 1);
515 int k = scm_to_int (i);
522 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
526 scm_set_cdr_x (s, SCM_EOL);
532 format_single_argument (SCM arg, int precision, bool escape = false)
534 if (scm_is_integer (arg) && scm_exact_p (arg) == SCM_BOOL_T)
535 return (String_convert::int_string (scm_to_int (arg)));
536 else if (scm_is_number (arg))
538 Real val = scm_to_double (arg);
540 if (isnan (val) || isinf (val))
542 warning (_ ("Found infinity or nan in output. Substituting 0.0"));
544 if (strict_infinity_checking)
548 return (String_convert::form_string ("%.*lf", precision, val));
550 else if (scm_is_string (arg))
552 string s = ly_scm2string (arg);
555 // Escape backslashes and double quotes, wrap it in double quotes
556 replace_all (&s, "\\", "\\\\");
557 replace_all (&s, "\"", "\\\"");
558 // don't replace percents, since the png backend uses %d as escape sequence
559 // replace_all (&s, "%", "\\%");
560 replace_all (&s, "$", "\\$");
565 else if (scm_is_symbol (arg))
566 return (ly_symbol2string (arg));
569 ly_progress (scm_from_locale_string ("\nUnsupported SCM value for format: ~a"),
577 LY_DEFINE (ly_format, "ly:format",
578 1, 0, 1, (SCM str, SCM rest),
579 "LilyPond specific format, supporting @code{~a} and @code{~[0-9]f}."
580 " Basic support for @code{~s} is also provided.")
582 LY_ASSERT_TYPE (scm_is_string, str, 1);
584 string format = ly_scm2string (str);
585 vector<string> results;
588 while (i < format.size ())
590 vsize tilde = format.find ('~', i);
592 results.push_back (format.substr (i, (tilde-i)));
599 char spec = format.at (tilde ++);
601 results.push_back ("~");
604 if (!scm_is_pair (rest))
606 programming_error (string (__FUNCTION__)
607 + ": not enough arguments for format.");
608 return ly_string2scm ("");
611 SCM arg = scm_car (rest);
612 rest = scm_cdr (rest);
618 else if (isdigit (spec))
620 precision = spec - '0';
621 spec = format.at (tilde ++);
624 if (spec == 'a' || spec == 'A' || spec == 'f' || spec == '$')
625 results.push_back (format_single_argument (arg, precision));
626 else if (spec == 's' || spec == 'S')
627 results.push_back (format_single_argument (arg, precision, true));
628 else if (spec == 'l')
631 for (; scm_is_pair (s); s = scm_cdr (s))
633 results.push_back (format_single_argument (scm_car (s), precision));
634 if (scm_cdr (s) != SCM_EOL)
635 results.push_back (" ");
639 results.push_back (format_single_argument (s, precision));
647 if (scm_is_pair (rest))
648 programming_error (string (__FUNCTION__)
649 + ": too many arguments");
652 for (vsize i = 0; i < results.size (); i++)
653 len += results[i].size ();
655 char *result = (char*) scm_malloc (len + 1);
657 for (vsize i = 0; i < results.size (); i++)
659 strncpy (ptr, results[i].c_str (), results[i].size ());
660 ptr += results[i].size ();
664 return scm_take_locale_stringn (result, len);