]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
*** empty log message ***
[lilypond.git] / lily / main.cc
1 /*
2   main.cc -- implement main () entrypoint.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <assert.h>
12 #include <locale.h>
13 #include <string.h>
14
15 #include "config.h"
16
17 #if HAVE_GETTEXT
18 #include <libintl.h>
19 #endif
20
21 #include "lily-guile.hh"
22 #include "lily-version.hh"
23 #include "all-font-metrics.hh"
24 #include "getopt-long.hh"
25 #include "misc.hh"
26 #include "string.hh"
27 #include "main.hh"
28 #include "file-path.hh"
29 #include "warn.hh"
30 #include "lily-guile.hh"
31 #include "paper-def.hh"
32 #include "midi-def.hh"
33 #include "global-ctor.hh"
34 #include "kpath.hh"
35
36 /*
37  * Global options that can be overridden through command line.
38  */
39
40 /* Names of header fields to be dumped to a separate file. */
41 Array<String> dump_header_fieldnames_global;
42
43 /* Name of initialisation file. */
44 String init_name_global;
45
46 /* Do not calculate and write paper output? */
47 bool no_paper_global_b = false;
48
49 /* Selected output format.
50    One of tex, ps, scm, as. */
51 String output_format_global = "tex";
52
53 /* Current output name. */
54 String output_name_global;
55
56 /* Run in safe mode? */
57 bool safe_global_b = false;
58
59 /* Verbose progress indication? */
60 bool verbose_global_b = false;
61
62 /* Scheme code to execute before parsing, after .scm init
63    This is where -e arguments are appended to.
64 */
65 String init_scheme_code_string = "(begin #t ";
66
67
68
69 /*
70  * Miscellaneous global stuff.
71  */
72 File_path global_path;
73
74
75 /*
76  * File globals.
77  */
78
79 static char const *AUTHORS =
80 "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n"
81 "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
82
83 static char const *PROGRAM_NAME = "lilypond-bin";
84 static char const *PROGRAM_URL = "http://lilypond.org";
85
86 static char const *NOTICE =
87 _i ("This program is free software.  It is covered by the GNU General Public\n"
88     "License and you are welcome to change it and/or distribute copies of it\n"
89     "under certain conditions.  Invoke as `lilypond-bin --warranty' for more\n"
90     "information.\n");
91   
92 static char const *WARRANTY =
93 _i ("    This program is free software; you can redistribute it and/or\n"
94     "modify it under the terms of the GNU General Public License version 2\n"
95     "as published by the Free Software Foundation.\n"
96     "\n"
97     "    This program is distributed in the hope that it will be useful,\n"
98     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
99     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
100     "General Public License for more details.\n"
101     "\n"
102     "    You should have received a copy (refer to the file COPYING) of the\n"
103     "GNU General Public License along with this program; if not, write to\n"
104     "the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\n"
105     "Boston, MA 02111-1307, USA.\n");
106
107
108 /* Where the init files live.  Typically:
109    LILYPOND_DATADIR = /usr/share/lilypond
110    LOCAL_LILYPOND_DATADIR = /usr/share/lilypond/<VERSION> */
111 char const *prefix_directory[] = {LILYPOND_DATADIR, LOCAL_LILYPOND_DATADIR, 0};
112
113 /*  The option parser */
114 static Getopt_long *option_parser = 0;
115
116 /* Internationalisation kludge in two steps:
117    * use _i () to get entry in POT file
118    * call gettext () explicitely for actual "translation"  */
119
120 static Long_option_init options_static[] =
121   {
122     {_i ("EXPR"), "evaluate", 'e',
123      _i ("set options, use -e '(ly-option-usage)' for help")},
124     /* Bug in option parser: --output=foe is taken as an abbreviation
125        for --output-format.  */
126     {_i ("EXT"), "format", 'f', _i ("use output format EXT")},
127     {0, "help", 'h',  _i ("print this help")},
128     {_i ("FIELD"), "header", 'H',  _i ("write header field to BASENAME.FIELD")},
129     {_i ("DIR"), "include", 'I',  _i ("add DIR to search path")},
130     {_i ("FILE"), "init", 'i',  _i ("use FILE as init file")},
131     {0, "no-paper", 'm',  _i ("produce MIDI output only")},
132     {_i ("FILE"), "output", 'o',  _i ("write output to FILE")},
133     {0, "safe-mode", 's',  _i ("run in safe mode")},
134     {0, "version", 'v',  _i ("print version number")},
135     {0, "verbose", 'V', _i ("be verbose")},
136     {0, "warranty", 'w',  _i ("show warranty and copyright")},
137     {0,0,0,0}
138   };
139
140 static void
141 dir_info (FILE *out)
142 {
143   fputs ("\n", out);
144   fprintf (out, "lilypond_datadir: `%s'\n", LILYPOND_DATADIR);
145   fprintf (out, "local_lilypond_datadir: `%s'\n", LOCAL_LILYPOND_DATADIR);
146   fprintf (out, "localedir: `%s'\n", LOCALEDIR);
147
148   char *lilypond_prefix = getenv ("LILYPONDPREFIX");
149   fprintf (out, "LILYPONDPREFIX: `%s'\n",
150            (lilypond_prefix ? lilypond_prefix : ""));
151 }
152
153 static void
154 copyright ()
155 {
156   printf (_f ("Copyright (c) %s by\n%s  and others.",
157               "1996--2004",
158               AUTHORS).to_str0 ());
159   printf ("\n");
160 }
161
162 static void
163 identify (FILE *out)
164 {
165   fputs (gnu_lilypond_version_string ().to_str0 (), out);
166 }
167  
168 static void
169 notice ()
170 {
171   identify (stdout);
172   printf ("\n");
173   printf (_f (NOTICE, PROGRAM_NAME).to_str0 ());
174   printf ("\n");
175   copyright ();
176 }
177
178 static void
179 usage ()
180 {
181   /* No version number or newline here.  It confuses help2man.  */
182   printf (_f ("Usage: %s [OPTIONS]... FILE...", PROGRAM_NAME).to_str0 ());
183   printf ("\n\n");
184   printf (_ ("Typeset music and/or produce MIDI from FILE.").to_str0 ());
185   printf ("\n\n");
186   printf (_ ("LilyPond produces beautiful music notation.").to_str0 ());
187   printf ("\n");
188   printf (_f ("For more information, see %s", PROGRAM_URL).to_str0 ());
189   printf ("\n\n");
190   printf (_ ("Options:").to_str0 ());
191   printf ("\n");
192   printf (Long_option_init::table_string (options_static).to_str0 ());
193   printf ("\n");
194   printf (_f ("Report bugs to %s.", "bug-lilypond@gnu.org").to_str0 ());
195   printf ("\n");
196   printf ("\n");
197 }
198
199 static void
200 warranty ()
201 {
202   identify (stdout);
203   printf ("\n");
204   copyright ();
205   printf ("\n");
206   printf (_ (WARRANTY).to_str0 ());
207 }
208
209 static void
210 setup_paths ()
211 {
212   if (char const *lilypond_prefix = getenv ("LILYPONDPREFIX"))
213     prefix_directory[1] = lilypond_prefix;
214
215   global_path.add ("");
216
217   /* Adding mf/out make lilypond unchanged source directory, when setting
218      LILYPONDPREFIX to lilypond-x.y.z */
219   char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
220
221   for (unsigned i = 0; prefix_directory[i]; i++)
222     for (char **s = suffixes; *s; s++)
223       {
224         String p = prefix_directory[i] + to_string ('/') + String (*s);
225         global_path.prepend (p);
226         
227 #if !KPATHSEA
228         /* Urg: GNU make's $ (word) index starts at 1 */
229         int i  = 1;
230         while (global_path.try_add (p + to_string (".") + to_string (i)))
231           i++;
232 #endif
233       }
234 }
235   
236 static void
237 prepend_load_path (String dir)
238 {
239   String s = "(set! %load-path (cons \"" + dir + "\" %load-path))";
240   scm_c_eval_string (s.to_str0 ());
241 }
242
243 static void
244 main_with_guile (void *, int, char **)
245 {
246   /* Engravers use lily.scm contents, need to make Guile find it.
247      Prepend onto GUILE %load-path, very ugh. */
248   for (unsigned i = 0; prefix_directory[i]; i++)
249     {
250       prepend_load_path (prefix_directory[i]);
251       /* Junk this.  We should make real modules iso. just loading files. */
252       prepend_load_path (String (prefix_directory[i]) + "/scm");
253     }
254
255   if (verbose_global_b)
256     dir_info (stderr);
257
258   ly_c_init_guile ();
259   call_constructors ();
260   progress_indication ("\n");
261
262   all_fonts_global = new All_font_metrics (global_path.to_string ());
263
264   init_scheme_code_string += ")";
265   scm_c_eval_string ((char*) init_scheme_code_string.to_str0 ());
266
267   /* We accept multiple independent music files on the command line to
268      reduce compile time when processing lots of small files.
269      Starting the GUILE engine is very time consuming.  */
270
271   SCM files = SCM_EOL;
272   SCM *tail = &files;
273   while (char const *arg = option_parser->get_next_arg ())
274     {
275       *tail = scm_cons (scm_makfrom0str (arg), SCM_EOL);
276       tail = SCM_CDRLOC (*tail);
277     }
278   delete option_parser;
279   option_parser = 0;
280
281   if (files == SCM_EOL)
282     {
283       /* No FILE arguments is now a usage error to help newbies.  If you
284          want a filter, you're not a newbie and should know to use file
285          argument `-'.  */
286       usage ();
287       exit (2);
288     }
289
290   SCM result = scm_call_1 (ly_scheme_function ("lilypond-main"), files);
291   (void) result;
292
293
294   /* Unreachable.  */
295   exit (0);
296 }
297
298 static void
299 setup_localisation ()
300 {
301 #if HAVE_GETTEXT
302   /* Enable locales */
303   setlocale (LC_ALL, "");
304   
305   /* FIXME: check if this is still true.
306     Disable localisation of float values.  This breaks TeX output.  */
307   setlocale (LC_NUMERIC, "C");
308   
309   String name (PACKAGE);
310   name.to_lower ();
311   bindtextdomain (name.to_str0 (), LOCALEDIR);
312   textdomain (name.to_str0 ());
313 #endif
314 }
315
316 static void
317 parse_argv (int argc, char **argv)
318 {
319   bool help_b = false;
320   option_parser = new Getopt_long (argc, argv, options_static);
321   while (Long_option_init const * opt = (*option_parser) ())
322     {
323       switch (opt->shortname_char_)
324         {
325         case 'v':
326           notice ();
327           exit (0);
328           break;
329         case 'o':
330           {
331             String s = option_parser->optional_argument_str0_;
332             Path p = split_path (s);
333             if (s != "-" && p.ext.is_empty ())
334               p.ext = output_format_global;
335             output_name_global = p.to_string ();
336           }
337           break;
338         case 'e':
339           init_scheme_code_string += option_parser->optional_argument_str0_;
340           break;
341         case 'w':
342           warranty ();
343           exit (0);
344           break;
345         case 'f':
346           if (option_parser->optional_argument_str0_ == "help")
347             {
348               printf (_ ("This option is for developers only.").to_str0 ());
349               printf (_ ("Read the sources for more information.").to_str0 ());
350               exit (0);
351             }
352           output_format_global = option_parser->optional_argument_str0_;
353           break;
354         case 'H':
355           dump_header_fieldnames_global
356             .push (option_parser->optional_argument_str0_);
357           break;
358         case 'I':
359           global_path.push (option_parser->optional_argument_str0_);
360           break;
361         case 'i':
362           init_name_global = option_parser->optional_argument_str0_;
363           break;
364         case 'h':
365           help_b = true;
366           break;
367         case 'V':
368           verbose_global_b = true;
369           break;
370         case 's':
371           safe_global_b = true;
372           break;
373         case 'm':
374           no_paper_global_b = true;
375           break;
376         default:
377           assert (false);
378           break;
379         }
380     }
381
382   if (help_b)
383     {
384       usage ();
385       if (verbose_global_b)
386         dir_info (stdout);
387       exit (0);
388     }
389 }
390
391 int
392 main (int argc, char **argv)
393 {
394   setup_localisation ();  
395   setup_paths ();
396   parse_argv (argc, argv);
397   initialize_kpathsea (argv[0]);
398
399   scm_boot_guile (argc, argv, main_with_guile, 0);
400
401   /* Unreachable */
402   return 0;
403 }