]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.1.62
[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--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <stdlib.h>
10 #include <iostream.h>
11 #include <assert.h>
12 #include <locale.h>
13 #include "lily-guile.hh"
14 #include "lily-version.hh"
15
16 #include "all-fonts.hh"
17 #include "proto.hh"
18 #include "dimensions.hh"
19 #include "getopt-long.hh"
20 #include "misc.hh"
21 #include "string.hh"
22 #include "main.hh"
23 #include "file-path.hh"
24 #include "config.hh"
25 #include "file-results.hh"
26 #include "debug.hh"
27 #include "lily-guile.hh"
28 #include "paper-def.hh"
29 #include "midi-def.hh"
30 #include "global-ctor.hh"
31
32 #if HAVE_GETTEXT
33 #include <libintl.h>
34 #endif
35
36
37 bool version_ignore_global_b = false;
38 bool no_paper_global_b = false;
39 bool no_timestamps_global_b = false;
40 bool find_old_relative_b = false;
41
42 char const* output_global_ch = "tex";
43 All_font_metrics *all_fonts_global_p;
44
45 String default_outname_base_global =  "lelie";
46 String outname_str_global;
47 String init_str_global;
48
49 int default_count_global;
50 File_path global_path;
51
52 bool safe_global_b = false;
53 bool experimental_features_global_b = false;
54 bool dependency_global_b = false;
55
56 int exit_status_i_;
57
58 Getopt_long * oparser_global_p = 0;
59
60 String distill_inname_str (String name_str, String& ext_r);
61
62 Long_option_init theopts[] = {
63   {0, "about", 'a'},
64   {1, "output", 'o'},
65   {0, "warranty", 'w'},
66   {0, "help", 'h'},
67   {0, "test", 't'},
68   {0, "debug", 'd'},
69   {1, "init", 'i'},
70   {1, "include", 'I'},
71   {0, "no-paper", 'm'},
72   {0, "dependencies", 'M'},     // like GCC
73   {0, "no-timestamps", 'T'},    // why do we have this option?
74   {0, "find-old-relative", 'Q'},
75   {0, "ignore-version", 'V'},
76   {0, "version", 'v'},
77   {1, "output-format", 'f'},
78   {0, "safe", 's'},
79   {0,0,0}
80 };
81
82 void
83 usage ()
84 {
85   cout << _f ("Usage: %s [OPTION]... [FILE]...", "lilypond") << '\n';
86   cout << _ ("Typeset music and or play MIDI from FILE or <stdin>");
87   cout << '\n';
88   cout << '\n';
89   cout << _ ("Options:");
90   cout << '\n';
91   cout  << _ (
92     "  -a, --about            about LilyPond\n"
93     );
94   cout  << _ (
95     "  -d, --debug            enable debugging output\n"
96     );
97   cout  << _ (
98     "  -M, --dependencies     write Makefile dependencies for every input file\n"
99     );
100   cout  << _ (
101     "  -h, --help             this help\n"
102     );
103   cout  << _ (
104     "  -f, --output-format=X  use output format X\n"
105     );
106   cout  << _ (
107     "  -I, --include=DIR      add DIR to search path\n"
108     );
109   cout  << _ (
110     "  -i, --init=FILE        use FILE as init file\n"
111     );
112   cout  << _ (
113     "  -m, --no-paper         produce midi output only\n"
114     );
115   cout  << _ (
116     "  -o, --output=FILE      set FILE as default output base\n"
117     );
118   cout  << _ (
119     "  -Q, --find-old-relative show all changes in relative syntax\n"
120     );
121   cout << _ (
122     "  -s, --safe             inhibit file output naming and exporting TeX  macros\n");
123   cout  << _ (
124     "  -t, --test             switch on experimental features\n"
125     );
126   cout  << _ (
127     "  -T, --no-timestamps    don't timestamp the output\n"
128     );
129   cout  << _ (
130     "  -V, --ignore-version   ignore mudela version\n"
131     );
132   cout  << _ (
133               "  -w, --warranty         show warranty and copyright\n"
134               );
135   cout << '\n';
136   cout << _ ("GNU LilyPond was compiled with the following settings:");
137   cout << '\n';
138   cout <<
139 #ifdef NDEBUG
140     "NDEBUG "
141 #endif
142 #ifdef NPRINT
143     "NPRINT "
144 #endif
145 #ifdef STRING_UTILS_INLINED
146     "STRING_UTILS_INLINED "
147 #endif
148     "datadir=" DIR_DATADIR
149     "\n"
150     "localedir=" DIR_LOCALEDIR
151
152     "\n";
153
154   ;
155
156   print_mudela_versions (cout);
157 }
158
159 void
160 about ()
161 {
162   cout << '\n';
163   cout << 
164 #include "BLURB.hh"
165     cout << '\n';
166   cout << _ ("GNU LilyPond is Free software, see --warranty");
167   cout << '\n';
168   cout << '\n';
169   cout << _f ("Copyright (c) %s by", "1996--1999");
170   cout << '\n';
171   cout << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
172   cout << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
173   cout << '\n';
174 }
175
176 void
177 notice ()
178 {
179   cout << '\n';
180   cout << _ ("GNU LilyPond -- The GNU Project music typesetter");
181   cout << '\n';
182   cout << _f ("Copyright (c) %s by", "1996--1999");
183   cout << '\n';
184   cout << "  " + _ ("Han-Wen Nienhuys <hanwen@cs.uu.nl>") + "\n";
185   cout << "  " + _ ("Jan Nieuwenhuizen <janneke@gnu.org>") + "\n";
186   cout << '\n';
187   cout << _ (
188              "    This program is free software; you can redistribute it and/or\n"
189              "modify it under the terms of the GNU General Public License version 2\n"
190              "as published by the Free Software Foundation.\n"
191              "\n"
192              "    This program is distributed in the hope that it will be useful,\n"
193              "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
194              "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
195              "General Public License for more details.\n"
196              "\n"
197              "    You should have received a copy (refer to the file COPYING) of the\n"
198              "GNU General Public License along with this program; if not, write to\n"
199              "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
200              "USA.\n");
201 }
202
203 void
204 identify ()
205 {
206   *mlog << gnu_lilypond_version_str () << endl;
207 }
208
209 void
210 setup_paths ()
211 {
212   // facilitate binary distributions
213   char const *env_lily = getenv ("LILYPONDPREFIX");
214   String prefix_directory;
215   if (env_lily)
216     prefix_directory = env_lily;
217
218 #if HAVE_GETTEXT
219   setlocale (LC_ALL, ""); /* enable locales */
220   setlocale (LC_NUMERIC, "C"); /* musn't have comma's in TeX output... */
221   String lily_locale_dir;
222   String name (PACKAGE);
223   name.to_lower ();
224
225   /*
226     urg; what *do* we want with $LILYPONDPREFIX, DIR_DATADIR and $prefix/share
227     handy for multiple source-dir runs, though...
228    */
229   if (!prefix_directory.empty_b())
230     {
231       lily_locale_dir = prefix_directory + "/share/locale";
232       bindtextdomain (name.ch_C (), lily_locale_dir.ch_C());
233     }
234   else
235     bindtextdomain (name.ch_C (), DIR_LOCALEDIR);
236   textdomain (name.ch_C ());
237 #endif
238
239   global_path.add ("");
240   // must override (come before) "/usr/local/share/lilypond"!
241   char const *env_sz = getenv ("LILYINCLUDE");
242   if (env_sz)
243     global_path.parse_path (env_sz);
244
245
246   char *suffixes[] = {"ly", "afm", "scm", "tfm", "cmtfm", "ps", 0};
247   for (char **s = suffixes; *s; s++){
248     if (!prefix_directory.empty_b())
249       global_path.add (prefix_directory + to_str ('/') + String (*s));
250     else
251       global_path.add (String (DIR_DATADIR) + to_str ('/') + String(*s));
252   }
253 }
254
255
256 void
257 main_prog (int argc, char **argv)
258 {
259   /*
260     need to do this first. Engravers use lily.scm contents.
261    */
262   extern void ly_init_protection();
263   ly_init_protection();  
264   init_lily_guile ();
265   read_lily_scm_file ( "lily.scm");
266   cout << endl;
267
268   call_constructors ();
269   default_outname_base_global = "lelie";
270   all_fonts_global_p = new All_font_metrics (global_path.str ());
271   
272   int p=0;
273   const char *arg ;
274   while ((arg= oparser_global_p->get_next_arg ()))
275     {
276       
277       if (outname_str_global == "")
278         {
279           Midi_def::reset_default_count ();
280           Paper_def::reset_default_count ();
281         }
282       String f (arg);
283       String i;
284       f = distill_inname_str (f, i);
285       if (f == "-")
286         default_outname_base_global = "-";
287       else
288         {
289           String a,b,c,d;
290           split_path (f, a, b, c, d);
291           default_outname_base_global = c;
292         }
293       if (outname_str_global.length_i ())
294         default_outname_base_global = outname_str_global;
295       if (init_str_global.length_i ())
296         i = init_str_global;
297       else
298         i = "init" + i;
299       do_one_file (i, f);
300       p++;
301     }
302   if (!p)
303     {
304       String i;
305       if (init_str_global.length_i ())
306         i = init_str_global;
307       else
308         i = "init.ly";
309       default_outname_base_global = "-";
310       if (outname_str_global.length_i ())
311         default_outname_base_global = outname_str_global;
312       do_one_file (i, default_outname_base_global);
313     }
314   delete oparser_global_p;
315   exit( exit_status_i_);
316 }
317
318
319 int
320 main (int argc, char **argv)
321 {
322   identify ();
323   debug_init ();                // should be first
324   setup_paths ();
325
326   oparser_global_p = new Getopt_long(argc, argv,theopts);
327   while (Long_option_init const * opt = (*oparser_global_p)())
328     {
329       switch (opt->shortname)
330         {
331         case 'v':
332           exit (0);             // we print a version anyway.
333           break;
334         case 't':
335           experimental_features_global_b = true;
336           *mlog << "*** enabling experimental features, you're on your own now ***\n";
337           break;
338         case 'o':
339           outname_str_global = oparser_global_p->optional_argument_ch_C_;
340           break;
341         case 'w':
342           notice ();
343           exit (0);
344           break;
345         case 'f':
346           output_global_ch = oparser_global_p->optional_argument_ch_C_;
347           break;
348         case 'Q':
349           find_old_relative_b= true;
350           break;
351         case 'I':
352           global_path.push (oparser_global_p->optional_argument_ch_C_);
353           break;
354         case 'i':
355           init_str_global = oparser_global_p->optional_argument_ch_C_;
356           break;
357         case 'a':
358           about ();
359           exit (0);
360         case 'h':
361           usage ();
362           exit (0);
363           break;
364         case 'V':
365           version_ignore_global_b = true;
366           break;
367         case 's':
368           safe_global_b = true;
369           break;
370         case 'M':
371           dependency_global_b = true;
372           break; 
373         case 'd':
374           set_debug (true);
375           break;
376         case 'm':
377           no_paper_global_b = true;
378           break;
379         case 'T':
380           no_timestamps_global_b = true;
381           break;
382         default:
383           assert (false);
384           break;
385         }
386     }
387
388 #ifdef WINNT
389   gh_enter (argc, argv, main_prog);
390 #else
391   gh_enter (argc, argv, (void(*)())main_prog);
392 #endif
393
394   return 0;                     // unreachable
395 }
396
397 /**
398   make input file name from command arg.
399
400   @input file name
401
402   @output file name with added default extension. "" is stdin.
403           in reference argument: the extension. ".ly" if none
404  */
405 String
406 distill_inname_str (String name_str, String& ext_r)
407 {
408   String str = name_str;
409   if (str.length_i ())
410     {
411       if (str != "-")
412         {
413           String a,b,c;
414           split_path (str,a,b,c,ext_r);
415
416           // add extension if not present.
417           char const* extensions[] = {"", ".ly", ".fly", ".sly", "", 0};
418           extensions[0] = ext_r.ch_C ();
419           for (int i = 0; extensions[i]; i++)
420             {
421               if (!global_path.find (a+b+c+extensions[i]).empty_b ())
422                 {
423                   ext_r = extensions[i];
424                   break;
425                 }
426             }
427           str = a+b+c+ext_r;
428           // in any case, assume (init).ly
429           if (!ext_r.length_i ())
430             ext_r = ".ly";
431         }
432     }
433   else 
434     {
435       str = "-";
436       ext_r = ".ly";
437     }
438   return str;
439 }
440