2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2012 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 */
29 #include "dimensions.hh"
30 #include "file-name.hh"
31 #include "file-path.hh"
32 #include "international.hh"
33 #include "libc-extension.hh"
34 #include "lily-guile.hh"
37 #include "program-option.hh"
38 #include "relocate.hh"
39 #include "string-convert.hh"
43 /* Declaration of log function(s) */
44 SCM ly_progress (SCM, SCM);
46 LY_DEFINE (ly_start_environment, "ly:start-environment",
48 "Return the environment (a list of strings) that was in"
49 " effect at program start.")
54 for (vsize i = 0; i < start_environment_global.size (); i++)
56 *tail = scm_cons (ly_string2scm (start_environment_global[i]),
58 tail = SCM_CDRLOC (*tail);
64 LY_DEFINE (ly_find_file, "ly:find-file",
66 "Return the absolute file name of @var{name},"
67 " or @code{#f} if not found.")
69 LY_ASSERT_TYPE (scm_is_string, name, 1);
71 string nm = ly_scm2string (name);
72 string file_name = global_path.find (nm);
73 if (file_name.empty ())
76 return ly_string2scm (file_name);
80 Ugh. Gulped file is copied twice. (maybe thrice if you count stdio
83 LY_DEFINE (ly_gulp_file, "ly:gulp-file",
84 1, 1, 0, (SCM name, SCM size),
85 "Read @var{size} characters from the file @var{name},"
86 " and return its contents in a string."
87 " If @var{size} is undefined, the entire file is read."
88 " The file is looked up using the search path.")
90 LY_ASSERT_TYPE (scm_is_string, name, 1);
92 if (size != SCM_UNDEFINED)
94 LY_ASSERT_TYPE (scm_is_number, size, 2);
95 sz = scm_to_int (size);
98 string contents = gulp_file_to_string (ly_scm2string (name), true, sz);
99 return scm_from_locale_stringn (contents.c_str (), contents.length ());
102 LY_DEFINE (ly_dir_p, "ly:dir?",
104 "Is @var{s} a direction? Valid directions are @w{@code{-1}},"
105 " @code{0}, or@tie{}@code{1}, where @w{@code{-1}} represents"
106 " left or down, @code{1}@tie{}represents right or up, and @code{0}"
107 " represents a neutral direction.")
109 if (scm_is_integer (s))
111 int i = scm_to_int (s);
112 return (i >= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
117 LY_DEFINE (ly_assoc_get, "ly:assoc-get",
119 (SCM key, SCM alist, SCM default_value, SCM strict_checking),
120 "Return value if @var{key} in @var{alist}, else @var{default-value}"
121 " (or @code{#f} if not specified). If @var{strict-checking} is set"
122 " to @code{#t} and @var{key} is not in @var{alist}, a programming_error"
125 LY_ASSERT_TYPE (ly_cheap_is_list, alist, 2);
127 SCM handle = scm_assoc (key, alist);
128 if (scm_is_pair (handle))
129 return scm_cdr (handle);
131 if (default_value == SCM_UNDEFINED)
132 default_value = SCM_BOOL_F;
134 if (strict_checking == SCM_BOOL_T)
136 string key_string = ly_scm2string
137 (scm_object_to_string (key, SCM_UNDEFINED));
138 string default_value_string = ly_scm2string
139 (scm_object_to_string (default_value,
141 programming_error ("Cannot find key `"
143 + "' in alist, setting to `"
144 + default_value_string + "'.");
147 return default_value;
150 LY_DEFINE (ly_string_substitute, "ly:string-substitute",
151 3, 0, 0, (SCM a, SCM b, SCM s),
152 "Replace string@tie{}@var{a} by string@tie{}@var{b} in"
153 " string@tie{}@var{s}.")
155 LY_ASSERT_TYPE (scm_is_string, s, 1);
156 LY_ASSERT_TYPE (scm_is_string, b, 2);
157 LY_ASSERT_TYPE (scm_is_string, s, 3);
159 string ss = ly_scm2string (s);
160 replace_all (&ss, ly_scm2string (a),
163 return ly_string2scm (ss);
167 is_not_escape_character (Byte c)
185 LY_DEFINE (ly_string_percent_encode, "ly:string-percent-encode",
187 "Encode all characters in string @var{str} with hexadecimal"
188 " percent escape sequences, with the following exceptions:"
189 " characters @w{@code{-},} @code{.}, @code{/}, and @code{_}; and"
190 " characters in ranges @code{0-9}, @code{A-Z}, and @code{a-z}.")
192 LY_ASSERT_TYPE (scm_is_string, str, 1);
194 string orig_str = ly_scm2string (str);
198 vsize n = orig_str.size ();
202 Byte cur = orig_str[i];
204 if (is_not_escape_character (cur))
209 new_str += String_convert::bin2hex (cur);
215 return ly_string2scm (new_str);
218 LY_DEFINE (ly_number_2_string, "ly:number->string",
220 "Convert @var{s} to a string without generating many decimals.")
222 LY_ASSERT_TYPE (scm_is_number, s, 1);
224 char str[400]; // ugh.
226 if (scm_exact_p (s) == SCM_BOOL_F)
228 Real r (scm_to_double (s));
229 if (isinf (r) || isnan (r))
231 programming_error ("infinity or NaN encountered while converting Real number, "
237 snprintf (str, sizeof (str), "%.4f", r);
240 snprintf (str, sizeof (str), "%d", int (scm_to_int (s)));
242 return scm_from_locale_string (str);
245 LY_DEFINE (ly_version, "ly:version", 0, 0, 0, (),
246 "Return the current lilypond version as a list, e.g.,"
247 " @code{(1 3 127 uu1)}.")
249 char const *vs = "\'(" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")";
251 return scm_c_eval_string ((char *)vs);
254 LY_DEFINE (ly_unit, "ly:unit", 0, 0, 0, (),
255 "Return the unit used for lengths as a string.")
257 return scm_from_locale_string (INTERNAL_UNIT);
260 LY_DEFINE (ly_dimension_p, "ly:dimension?", 1, 0, 0, (SCM d),
261 "Return @var{d} as a number. Used to distinguish length"
262 " variables from normal numbers.")
264 return scm_number_p (d);
270 LY_DEFINE (ly_protects, "ly:protects",
272 "Return hash of protected objects.")
277 LY_DEFINE (ly_gettext, "ly:gettext",
278 1, 0, 0, (SCM original),
279 "A Scheme wrapper function for @code{gettext}.")
281 LY_ASSERT_TYPE (scm_is_string, original, 1);
282 return ly_string2scm (_ (ly_scm2string (original).c_str ()));
285 LY_DEFINE (ly_output_formats, "ly:output-formats",
287 "Formats passed to @option{--format} as a list of strings,"
288 " used for the output.")
290 vector<string> output_formats = string_split (output_format_global, ',');
293 int output_formats_count = output_formats.size ();
294 for (int i = 0; i < output_formats_count; i++)
295 lst = scm_cons (ly_string2scm (output_formats[i]), lst);
300 LY_DEFINE (ly_wide_char_2_utf_8, "ly:wide-char->utf-8",
302 "Encode the Unicode codepoint @var{wc}, an integer, as UTF-8.")
306 LY_ASSERT_TYPE (scm_is_integer, wc, 1);
307 unsigned wide_char = (unsigned) scm_to_int (wc);
310 if (wide_char < 0x0080)
311 *p++ = (char)wide_char;
312 else if (wide_char < 0x0800)
314 *p++ = (char) (((wide_char >> 6)) | 0xC0);
315 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
317 else if (wide_char < 0x10000)
319 *p++ = (char) (((wide_char >> 12)) | 0xE0);
320 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
321 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
325 *p++ = (char) (((wide_char >> 18)) | 0xF0);
326 *p++ = (char) (((wide_char >> 12) & 0x3F) | 0x80);
327 *p++ = (char) (((wide_char >> 6) & 0x3F) | 0x80);
328 *p++ = (char) (((wide_char) & 0x3F) | 0x80);
332 return scm_from_locale_string (buf);
335 LY_DEFINE (ly_effective_prefix, "ly:effective-prefix",
337 "Return effective prefix.")
339 return ly_string2scm (lilypond_datadir);
342 LY_DEFINE (ly_chain_assoc_get, "ly:chain-assoc-get",
343 2, 2, 0, (SCM key, SCM achain, SCM default_value, SCM strict_checking),
344 "Return value for @var{key} from a list of alists @var{achain}."
345 " If no entry is found, return @var{default-value} or @code{#f} if"
346 " @var{default-value} is not specified. With @var{strict-checking}"
347 " set to @code{#t}, a programming_error is output in such cases.")
349 if (scm_is_pair (achain))
351 SCM handle = scm_assoc (key, scm_car (achain));
352 if (scm_is_pair (handle))
353 return scm_cdr (handle);
355 return ly_chain_assoc_get (key, scm_cdr (achain), default_value);
358 if (strict_checking == SCM_BOOL_T)
360 string key_string = ly_scm2string
361 (scm_object_to_string (key, SCM_UNDEFINED));
362 string default_value_string = ly_scm2string
363 (scm_object_to_string (default_value,
365 programming_error ("Cannot find key `"
367 + "' in achain, setting to `"
368 + default_value_string + "'.");
371 return default_value == SCM_UNDEFINED ? SCM_BOOL_F : default_value;
374 LY_DEFINE (ly_stderr_redirect, "ly:stderr-redirect",
375 1, 1, 0, (SCM file_name, SCM mode),
376 "Redirect stderr to @var{file-name}, opened with @var{mode}.")
378 LY_ASSERT_TYPE (scm_is_string, file_name, 1);
381 string f = ly_scm2string (file_name);
383 if (scm_is_string (mode))
384 m = ly_scm2string (mode);
385 /* dup2 and (fileno (current-error-port)) do not work with mingw'c
388 stderrfile = freopen (f.c_str (), m.c_str (), stderr);
390 error (_f ("failed redirecting stderr to `%s'", f.c_str ()));
391 return SCM_UNSPECIFIED;
395 accumulate_symbol (void * /* closure */,
400 return scm_cons (key, result);
403 LY_DEFINE (ly_hash_table_keys, "ly:hash-table-keys",
405 "Return a list of keys in @var{tab}.")
407 return scm_internal_hash_fold ((scm_t_hash_fold_fn) &accumulate_symbol,
411 LY_DEFINE (ly_camel_case_2_lisp_identifier, "ly:camel-case->lisp-identifier",
412 1, 0, 0, (SCM name_sym),
413 "Convert @code{FooBar_Bla} to @code{foo-bar-bla} style symbol.")
415 LY_ASSERT_TYPE (ly_is_symbol, name_sym, 1);
418 TODO: should use strings instead?
421 const string in = ly_symbol2string (name_sym);
422 string result = camel_case_to_lisp_identifier (in);
424 return ly_symbol2scm (result.c_str ());
427 LY_DEFINE (ly_expand_environment, "ly:expand-environment",
429 "Expand @code{$VAR} and @code{$@{VAR@}} in @var{str}.")
431 LY_ASSERT_TYPE (scm_is_string, str, 1);
433 return ly_string2scm (expand_environment_variables (ly_scm2string (str)));
436 LY_DEFINE (ly_truncate_list_x, "ly:truncate-list!",
437 2, 0, 0, (SCM lst, SCM i),
438 "Take at most the first @var{i} of list @var{lst}.")
440 LY_ASSERT_TYPE (scm_is_integer, i, 1);
442 int k = scm_to_int (i);
449 for (; scm_is_pair (s) && k--; s = scm_cdr (s))
453 scm_set_cdr_x (s, SCM_EOL);
459 format_single_argument (SCM arg, int precision, bool escape = false)
461 if (scm_is_integer (arg) && scm_exact_p (arg) == SCM_BOOL_T)
462 return (String_convert::int_string (scm_to_int (arg)));
463 else if (scm_is_number (arg))
465 Real val = scm_to_double (arg);
467 if (isnan (val) || isinf (val))
469 warning (_ ("Found infinity or nan in output. Substituting 0.0"));
471 if (strict_infinity_checking)
475 return (String_convert::form_string ("%.*lf", precision, val));
477 else if (scm_is_string (arg))
479 string s = ly_scm2string (arg);
482 // Escape backslashes and double quotes, wrap it in double quotes
483 replace_all (&s, "\\", "\\\\");
484 replace_all (&s, "\"", "\\\"");
485 // don't replace percents, since the png backend uses %d as escape sequence
486 // replace_all (&s, "%", "\\%");
487 replace_all (&s, "$", "\\$");
492 else if (scm_is_symbol (arg))
493 return (ly_symbol2string (arg));
496 ly_progress (scm_from_locale_string ("\nUnsupported SCM value for format: ~a"),
503 LY_DEFINE (ly_format, "ly:format",
504 1, 0, 1, (SCM str, SCM rest),
505 "LilyPond specific format, supporting @code{~a} and @code{~[0-9]f}."
506 " Basic support for @code{~s} is also provided.")
508 LY_ASSERT_TYPE (scm_is_string, str, 1);
510 string format = ly_scm2string (str);
511 vector<string> results;
514 while (i < format.size ())
516 vsize tilde = format.find ('~', i);
518 results.push_back (format.substr (i, (tilde - i)));
525 char spec = format.at (tilde++);
527 results.push_back ("~");
530 if (!scm_is_pair (rest))
532 programming_error (string (__FUNCTION__)
533 + ": not enough arguments for format.");
534 return ly_string2scm ("");
537 SCM arg = scm_car (rest);
538 rest = scm_cdr (rest);
544 else if (isdigit (spec))
546 precision = spec - '0';
547 spec = format.at (tilde++);
550 if (spec == 'a' || spec == 'A' || spec == 'f' || spec == '$')
551 results.push_back (format_single_argument (arg, precision));
552 else if (spec == 's' || spec == 'S')
553 results.push_back (format_single_argument (arg, precision, true));
554 else if (spec == 'l')
557 for (; scm_is_pair (s); s = scm_cdr (s))
559 results.push_back (format_single_argument (scm_car (s), precision));
560 if (scm_cdr (s) != SCM_EOL)
561 results.push_back (" ");
565 results.push_back (format_single_argument (s, precision));
573 if (scm_is_pair (rest))
574 programming_error (string (__FUNCTION__)
575 + ": too many arguments");
578 for (vsize i = 0; i < results.size (); i++)
579 len += results[i].size ();
581 char *result = (char *) scm_malloc (len + 1);
583 for (vsize i = 0; i < results.size (); i++)
585 strncpy (ptr, results[i].c_str (), results[i].size ());
586 ptr += results[i].size ();
590 return scm_take_locale_stringn (result, len);
594 ly_run_command (char *argv[], char **standard_output, char **standard_error)
598 int flags = G_SPAWN_SEARCH_PATH;
599 if (!standard_output)
600 flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
602 flags |= G_SPAWN_STDERR_TO_DEV_NULL;
603 if (!g_spawn_sync (0, argv, 0, GSpawnFlags (flags),
605 standard_output, standard_error,
606 &exit_status, &error))
608 fprintf (stderr, "failed (%d): %s: %s\n", exit_status, argv[0], error->message);
609 g_error_free (error);
618 ly_scm2utf8 (SCM str)
620 char *p = ly_scm2str0 (str);
621 char *g = g_locale_to_utf8 (p, -1, 0, 0, 0);
626 LY_DEFINE (ly_spawn, "ly:spawn",
627 1, 0, 1, (SCM command, SCM rest),
628 "Simple interface to g_spawn_sync"
630 " The error is formatted with @code{format} and @var{rest}.")
633 LY_ASSERT_TYPE (scm_is_string, command, 1);
635 int argc = scm_is_pair (rest) ? scm_ilength (rest) : 0;
636 char **argv = new char*[argc + 2];
639 argv[n++] = ly_scm2utf8 (command);
640 for (SCM s = rest; scm_is_pair (s); s = scm_cdr (s))
641 argv[n++] = ly_scm2utf8 (scm_car (s));
644 char *standard_output = 0;
645 char *standard_error = 0;
646 // Always get the pointer to the stdout/stderr messages
647 int exit_status = ly_run_command (argv, &standard_output, &standard_error);
649 // Print out stdout and stderr only in debug mode
650 debug_output (string ("\n") + standard_output + standard_error, true);
652 for (int i = 0; i < n; i++)
656 return scm_from_int (exit_status);