]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
Add cautionary-style to interface.
[lilypond.git] / lily / main.cc
1 /*
2   main.cc -- implement main: entrypoints
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <iostream.h>
12 #include <assert.h>
13 #include <locale.h>
14 #include <stdio.h>
15
16 #include "config.h"
17
18 #if HAVE_GETTEXT
19 #include <libintl.h>
20 #endif
21
22 #include "lily-guile.hh"
23 #include "lily-version.hh"
24 #include "all-font-metrics.hh"
25 #include "getopt-long.hh"
26 #include "misc.hh"
27 #include "string.hh"
28 #include "main.hh"
29 #include "file-path.hh"
30 #include "file-results.hh"
31 #include "debug.hh"
32 #include "lily-guile.hh"
33 #include "paper-def.hh"
34 #include "midi-def.hh"
35 #include "global-ctor.hh"
36 #include "kpath.hh"
37
38 static int sane_putenv (char const* key, char const* value, bool overwrite = false);
39
40 /*
41   Global options that can be overridden through command line.
42 */
43
44 /* Write dependencies file? */
45 bool dependency_global_b = false;
46
47 /* Prepend to dependencies */
48 String dependency_prefix_global;
49
50 /* Names of header fields to be dumped to a separate file. */
51 Array<String> dump_header_fieldnames_global;
52
53 /* Name of initialisation file. */
54 String init_name_global;
55
56 /* Do not calculate and write paper output? */
57 bool no_paper_global_b = false;
58
59 /* Selected output format.
60    One of tex, ps, scm, as. */
61 String output_format_global = "tex";
62
63 /* Current output name. */
64 String output_name_global;
65
66 /* Run in safe mode? -- FIXME: should be re-analised */
67 bool safe_global_b = false;
68
69 /* Verbose progress indication? */
70 bool verbose_global_b = false;
71
72 /* Scheme code to execute before parsing, after .scm init */
73 String init_scheme_code_string = "(begin #t ";
74
75
76 /*
77   Misc. global stuff.
78  */
79
80
81 All_font_metrics *all_fonts_global_p;
82 int exit_status_global;
83 File_path global_path;
84
85 /* Number of current score output block.  If there's more than one
86    score block, this counter will be added to the output filename. */
87 int score_count_global;
88
89
90
91 /*
92   File globals.
93  */
94
95 /*  The option parser */
96 static Getopt_long *oparser_p_static = 0;
97
98 /*
99  Internationalisation kludge in two steps:
100    * use _i () to get entry in POT file
101    * call gettext () explicitely for actual "translation"
102
103  Note: these messages all start with lower case (ie, don't
104        follow regular localisation guidelines).
105  */
106 static Long_option_init options_static[] = {
107   /* print example usage:  lilypond -e "" ? */
108   {_i ("EXPR"), "evaluate", 'e',_i ("Scheme options: try -e \"(set-lily-option 'help 0)\" for more help.")},
109   /* another bug in option parser: --output=foe is taken as an abbreviation
110      for --output-format */
111   {_i ("EXT"), "format", 'f',  _i ("use output format EXT (tex [default], pdftex, ps, scm or as)")},
112   {0, "help", 'h',  _i ("this help")},
113   {_i ("FIELD"), "header", 'H',  _i ("write header field to BASENAME.FIELD")},
114   {_i ("DIR"), "include", 'I',  _i ("add DIR to search path")},
115   {_i ("FILE"), "init", 'i',  _i ("use FILE as init file")},
116   {0, "dependencies", 'M',  _i ("write Makefile dependencies for every input file")},
117   {0, "no-paper", 'm',  _i ("produce MIDI output only")},
118   {_i ("FILE"), "output", 'o',  _i ("write output to FILE")},
119   {_i ("DIR"), "dep-prefix", 'P',  _i ("prepend DIR to dependencies")},
120   {0, "safe", 's',  _i ("inhibit file output naming and exporting")},
121   {0, "version", 'v',  _i ("print version number")},
122   {0, "verbose", 'V', _i ("verbose")},
123   {0, "warranty", 'w',  _i ("show warranty and copyright")},
124   {0,0,0,0}
125 };
126
127 void
128 identify (FILE* os)
129 {
130   fputs(gnu_lilypond_version_str ().ch_C(), os);
131 }
132
133 void
134 usage ()
135 {
136   
137   /*
138     No version number or newline here. It confuses help2man
139    */
140   cout << _f ("Usage: %s [OPTION]... FILE...", "lilypond").ch_C();
141   cout << "\n\n";
142   cout << _ ("Typeset music and or play MIDI from FILE").ch_C();
143   cout << "\n\n";
144   cout << 
145 _ (
146 "LilyPond is a music typesetter.  It produces beautiful sheet music\n"
147 "using a high level description file as input.  LilyPond is part of \n"
148 "the GNU Project.\n"
149 ).ch_C();
150
151   cout << '\n';
152   cout << _ ("Options:").ch_C();
153   cout << '\n';
154   cout << Long_option_init::table_str (options_static).ch_C();
155   cout << '\n';
156   cout << _ ("This binary was compiled with the following options:") .ch_C()
157     << " " <<
158 #ifdef NDEBUG
159     "NDEBUG "
160 #endif
161     "\n"
162     "datadir: `" DIR_DATADIR "'\n"
163     "localedir: `" DIR_LOCALEDIR "'\n"
164     "\n";
165
166
167   cout << endl;
168
169   cout << _f ("Report bugs to %s", "bug-lilypond@gnu.org").ch_C() << endl;
170 }
171
172 void
173 version ()
174 {
175   identify (stdout);
176   cout << '\n';
177   cout << _f (""
178   "This is free software.  It is covered by the GNU General Public License,\n"
179   "and you are welcome to change it and/or distribute copies of it under\n"
180   "certain conditions.  Invoke as `%s --warranty' for more information.\n",
181     "lilypond").ch_C();
182   cout << endl;
183
184   cout << _f ("Copyright (c) %s by", "1996--2002").ch_C();
185   cout << '\n';
186   cout << "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
187   cout << "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
188 }
189
190 void
191 notice ()
192 {
193   cout << '\n';
194   cout << _ ("GNU LilyPond -- The music typesetter").ch_C();
195   cout << '\n';
196   cout << _f ("Copyright (c) %s by", "1996--2002").ch_C();
197   cout << '\n';
198   cout << "  Han-Wen Nienhuys <hanwen@cs.uu.nl>\n";
199   cout << "  Jan Nieuwenhuizen <janneke@gnu.org>\n";
200   cout << '\n';
201   cout << _ (
202              "    This program is free software; you can redistribute it and/or\n"
203              "modify it under the terms of the GNU General Public License version 2\n"
204              "as published by the Free Software Foundation.\n"
205              "\n"
206              "    This program is distributed in the hope that it will be useful,\n"
207              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
208              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
209              "General Public License for more details.\n"
210              "\n"
211              "    You should have received a copy (refer to the file COPYING) of the\n"
212              "GNU General Public License along with this program; if not, write to\n"
213              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
214              "USA.\n").ch_C();
215 }
216
217 String prefix_directory;
218
219 void
220 setup_paths ()
221 {
222   // facilitate binary distributions
223   char const *env_lily = getenv ("LILYPONDPREFIX");
224
225   if (env_lily)
226     prefix_directory = env_lily;
227
228 #if HAVE_GETTEXT
229   setlocale (LC_ALL, ""); /* enable locales */
230   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
231   String lily_locale_dir;
232   String name (PACKAGE);
233   name.to_lower ();
234
235   /*
236     urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
237     handy for multiple source-dir runs, though...
238    */
239   if (!prefix_directory.empty_b ())
240     {
241       lily_locale_dir = prefix_directory + "/share/locale";
242       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C ());
243     }
244   else
245     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
246   textdomain (name.ch_C ());
247 #endif
248
249   global_path.add ("");
250
251
252   /* Adding mf/out make lilypond unchanged source directory, when setting
253      LILYPONDPREFIX to lilypond-x.y.z */
254   char *suffixes[] = {"ly", "afm", "mf/out", "scm", "tfm", "ps", 0};
255
256   if (prefix_directory.empty_b ())
257     prefix_directory =  DIR_DATADIR;
258   for (char **s = suffixes; *s; s++)
259     {
260       String p =  prefix_directory + to_str ('/') + String (*s);
261       global_path.add (p);
262
263 #if !KPATHSEA
264       /* Urg: GNU make's $ (word) index starts at 1 */
265       int i  = 1;
266       while (global_path.try_add (p + to_str (".") + to_str (i)))
267         i++;
268 #endif
269     }
270 }
271
272 /**
273   Make input file name from command argument.
274
275   Path describes file name with added default extension,
276   ".ly" if none.  "-" is stdin.
277  */
278 Path
279 distill_inname (String str)
280 {
281   Path p = split_path (str);
282   if (str.empty_b () || str == "-")
283     p.base = "-";
284   else
285     {
286       String orig_ext = p.ext;
287       char const *extensions[] = {"ly", "fly", "sly", "", 0};
288       for (int i = 0; extensions[i]; i++)
289         {
290           p.ext = orig_ext;
291           if (*extensions[i] && !p.ext.empty_b ())
292             p.ext += ".";
293           p.ext += extensions[i];
294           if (!global_path.find (p.str ()).empty_b ())
295               break;
296         }
297       /* Reshuffle extension */
298       p = split_path (p.str ());
299     }
300   return p;
301 }
302
303 String
304 format_to_ext (String format)
305 {
306   if (format == "tex")
307     /* .lytex change put off */
308     return "tex"; // "lytex";
309   return format;
310 }
311
312 void
313 main_prog (void * , int, char**)
314 {
315   /*
316     need to do this first. Engravers use lily.scm contents.
317    */
318   
319   /*
320     prepend onto GUILE  loadpath.
321
322     Very ugh.
323    */
324
325   init_lily_guile (prefix_directory);
326   cout << endl;
327
328   call_constructors ();
329   all_fonts_global_p = new All_font_metrics (global_path.str ());
330
331   init_scheme_code_string += ")";
332   gh_eval_str ((char *)init_scheme_code_string.ch_C());
333   
334   int p=0;
335   const char *arg  = oparser_p_static->get_next_arg ();
336
337   if (!arg)
338     {
339       usage ();
340       /* No FILE arguments is now a usage error */
341       exit (2);
342     }
343   else
344     do 
345     {
346       String infile (arg);
347         
348       /* What/when was this supposed to do?
349        It looks like it reset the outname_str_global for every new
350        file, but only if user didn't specify a outname?  Huh?
351
352        // if (outname_str_global == "")
353
354       */
355       {
356         Midi_def::reset_score_count ();
357         Paper_def::reset_score_count ();
358       }
359
360       Path inpath = distill_inname (infile);
361
362       /* By default, use base name of input file for output file name */
363       Path outpath = inpath;
364       if (inpath.str () != "-")
365         outpath.ext = format_to_ext (output_format_global);
366
367       /* By default, write output to cwd; do not copy directory part
368          of input file name */
369       outpath.root = "";
370       outpath.dir = "";
371       
372       if (!output_name_global.empty_b ())
373         outpath = split_path (output_name_global);
374       
375       String init;
376       if (!init_name_global.empty_b ())
377         init = init_name_global;
378       else if (!inpath.ext.empty_b ())
379         init = "init." + inpath.ext;
380       else
381         init = "init.ly";
382         
383       /* Burp: output name communication goes through _global */
384       String save_output_name_global = output_name_global;
385       output_name_global = outpath.str ();
386       do_one_file (init, inpath.str ());
387       output_name_global = save_output_name_global;
388       
389       p++;
390     } while ((arg  = oparser_p_static->get_next_arg ()));
391   delete oparser_p_static;
392   oparser_p_static = 0;
393   exit (exit_status_global);
394 }
395
396
397 static int
398 sane_putenv (char const* key, char const* value, bool overwrite)
399 {
400   if (overwrite || !getenv (key))
401     return putenv ((char*)((String (key) + "=" + value).ch_C ()));
402   return -1;
403 }
404
405 int
406 main (int argc, char **argv)
407 {
408   setup_paths ();
409
410   /* Prepare GUILE for heavy memory usage.  If you have plenty memory,
411      this may speed up GUILE a bit.  If you're short on memory, these
412      settings
413     
414          export GUILE_INIT_SEGMENT_SIZE_1=36000
415          export GUILE_MAX_SEGMENT_SIZE=576000
416
417      may considerably decrease memory footprint (~*0.85), with a small
418      execution time penalty (~*1.10).  However, if this 15% gain in memory
419      usage prevents swapping, the execution time falls drastically. */
420   
421   sane_putenv ("GUILE_INIT_SEGMENT_SIZE_1", "4194304", false);
422   sane_putenv ("GUILE_MAX_SEGMENT_SIZE", "8388608", false);
423
424   ly_init_kpath (argv[0]);
425   
426   oparser_p_static = new Getopt_long (argc, argv, options_static);
427   while (Long_option_init const * opt = (*oparser_p_static) ())
428     {
429       switch (opt->shortname_ch_)
430         {
431         case 'v':
432           version ();
433           exit (0);             // we print a version anyway.
434           break;
435         case 'o':
436           {
437             String s = oparser_p_static->optional_argument_ch_C_;
438             Path p = split_path (s);
439             if (s != "-" && p.ext.empty_b ())
440               p.ext = format_to_ext (output_format_global);
441             output_name_global = p.str ();
442           }
443           break;
444         case 'e':
445           init_scheme_code_string +=
446             oparser_p_static->optional_argument_ch_C_;
447           break;
448         case 'w':
449           notice ();
450           exit (0);
451           break;
452         case 'f':
453             output_format_global = oparser_p_static->optional_argument_ch_C_;
454           break;
455         case 'P':
456             dependency_prefix_global = oparser_p_static->optional_argument_ch_C_;
457           break;
458         case 'H':
459           dump_header_fieldnames_global.push (oparser_p_static->optional_argument_ch_C_);
460           break;
461         case 'I':
462           global_path.push (oparser_p_static->optional_argument_ch_C_);
463           break;
464         case 'i':
465           init_name_global = oparser_p_static->optional_argument_ch_C_;
466           break;
467         case 'h':
468           usage ();
469           exit (0);
470           break;
471         case 'V':
472           verbose_global_b = true;
473           break;
474         case 's':
475           safe_global_b = true;
476           break;
477         case 'M':
478           dependency_global_b = true;
479           break; 
480         case 'm':
481           no_paper_global_b = true;
482           break;
483         default:
484           assert (false);
485           break;
486         }
487     }
488   identify (stderr);
489
490 #ifdef WINNT
491   scm_boot_guile (argc, argv, main_prog, 0);
492 #else
493   scm_boot_guile (argc, argv, (void (*) (void*, int, char**))main_prog, 0);
494 #endif
495
496   return 0;                     // unreachable
497 }
498
499