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