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