]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 1.1.1
[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/ly/");
228       global_path.add (prefix_directory + "/share/lilypond/afm/");
229     }
230   else
231     {
232       global_path.add (String (DIR_DATADIR) + "/ly/");
233       global_path.add (String (DIR_DATADIR) + "/afm/");  
234     }
235 }
236
237
238
239 void
240 main_prog (int argc, char **argv)
241 {
242   identify ();
243   call_constructors ();
244   debug_init ();                // should be first
245
246   setup_paths ();
247
248   String init_str;
249   String outname_str;
250
251   Getopt_long oparser (argc, argv,theopts);
252   while (Long_option_init const * opt = oparser ())
253     {
254       switch (opt->shortname)
255         {
256         case 't':
257           experimental_features_global_b = true;
258           *mlog << "*** enabling experimental features, you're on your own now ***\n";
259           break;
260         case 'o':
261           outname_str = oparser.optional_argument_ch_C_;
262           break;
263         case 'w':
264           notice ();
265           exit (0);
266           break;
267         case 'f':
268           output_global_ch = oparser.optional_argument_ch_C_;
269           break;
270         case 'Q':
271           find_quarts_global_b = true;
272           break;
273         case 'I':
274           global_path.push (oparser.optional_argument_ch_C_);
275           break;
276         case 'i':
277           init_str = oparser.optional_argument_ch_C_;
278           break;
279         case 'a':
280           about ();
281           exit (0);
282         case 'h':
283           usage ();
284           exit (0);
285           break;
286         case 'V':
287           version_ignore_global_b = true;
288           break;
289         case 'd':
290           dependency_global_b = true;
291           break; 
292         case 'D':
293           set_debug (true);
294           break;
295         case 'M':
296           no_paper_global_b = true;
297           break;
298         case 'T':
299           no_timestamps_global_b = true;
300           break;
301         default:
302           assert (false);
303           break;
304         }
305     }
306
307   default_outname_base_global = "lelie";
308
309   
310
311   int p=0;
312   const char *arg ;
313   while ((arg= oparser.get_next_arg ()))
314     {
315       String f (arg);
316       String i;
317       f = distill_inname_str (f, i);
318       if (f == "-")
319         default_outname_base_global = "-";
320       else
321         {
322           String a,b,c,d;
323           split_path (f, a, b, c, d);
324           default_outname_base_global = c;
325         }
326       if (outname_str.length_i ())
327         default_outname_base_global = outname_str;
328       if (init_str.length_i ())
329         i = init_str;
330       else
331         i = "init" + i;
332       do_one_file (i, f);
333       p++;
334     }
335   if (!p)
336     {
337       String i;
338       if (init_str.length_i ())
339         i = init_str;
340       else
341         i = "init.ly";
342       default_outname_base_global = "-";
343       if (outname_str.length_i ())
344         default_outname_base_global = outname_str;
345       do_one_file (i, default_outname_base_global);
346     }
347   exit( exit_status_i_);
348 }
349
350 int
351 main (int argc, char **argv)
352 {
353 #ifdef HAVE_LIBGUILE
354   gh_enter (argc, argv, (void(*)())main_prog);
355   return exit_status_i_;
356 #else
357   main_prog (argc, argv);
358   return exit_status_i_;
359 #endif
360 }
361
362 /**
363   make input file name from command arg.
364
365   @input file name
366
367   @output file name with added default extension. "" is stdin.
368           in reference argument: the extension. ".ly" if none
369  */
370 String
371 distill_inname_str (String name_str, String& ext_r)
372 {
373   String str = name_str;
374   if (str.length_i ())
375     {
376       if (str != "-")
377         {
378           String a,b,c;
379           split_path (str,a,b,c,ext_r);
380
381           // add extension if not present.
382           // UGH. Should parametrise in list of default extensions.
383           if (ext_r.empty_b ())
384             {
385               ext_r = ".fly";
386               if (global_path.find (a+b+c+ext_r).empty_b ())
387                 ext_r = ".ly";
388             }
389           str = a+b+c+ext_r;
390         }
391     }
392   else 
393     {
394       str = "-";
395       ext_r = ".ly";
396     }
397   return str;
398 }
399