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