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