]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
release: 0.1.43
[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 Han-Wen Nienhuys <hanwen@stack.nl>
7 */
8
9 #include <iostream.h>
10 #include <assert.h>
11 #include "proto.hh"
12 #include "plist.hh"
13 #include "lgetopt.hh"
14 #include "misc.hh"
15 #include "string.hh"
16 #include "main.hh"
17 #include "path.hh"
18 #include "config.hh"
19 #include "source.hh"
20 #include "debug.hh"
21 #include "my-lily-parser.hh"
22
23 static bool version_ignore_b = false;
24 Sources* source_global_l = 0;
25 bool no_paper_global_b = false;
26
27 bool experimental_features_global_b = false;
28 bool postscript_global_b = true;
29 int exit_status_i_;
30
31 void destill_inname (String &name_str_r);
32
33 Long_option_init theopts[] = {
34   {1, "output", 'o'},
35   {0, "warranty", 'w'},
36   {0, "help", 'h'},
37   {0, "test", 't'},
38   {0, "debug", 'd'},
39   {1, "init", 'i'},
40   {1, "include", 'I'},
41   {0, "no-paper", 'M'},
42   {0, "no-postscript", 'P'},
43   {0, "ignore-version", 'V'},
44   {0,0,0}
45 };
46
47 void
48 usage ()
49 {
50   cout <<
51     _("Usage: lilypond [options] [mudela-files]\n"
52     "Typeset and or produce midi output from mudela-file or stdin\n"
53     "\n"
54     "Options:\n"
55     "  -d, --debug            enable debugging output\n"
56     "  -I, --include=DIR      add DIR to search path\n"
57     "  -i, --init=FILE        use FILE as init file\n"
58     "  -h, --help             this help\n"
59     "  -w, --warranty         show warranty and copyright\n"
60     "  -o, --output=FILE      set FILE as default output\n"
61     "  -t, --test             switch on experimental features\n"
62     "  -M, --no-paper         produce midi output only\n"
63     "  -V, --ignore-version   ignore mudela version\n"
64     "  -P, --no-postscript    don't use PostScript\n"
65     "\n"
66     "GNU LilyPond was compiled with the following settings:\n")
67 #ifdef NDEBUG
68     "NDEBUG "
69 #endif
70 #ifdef NPRINT
71     "NPRINT "
72 #endif
73 #ifdef STRING_UTILS_INLINED
74     "STRING_UTILS_INLINED "
75 #endif
76     "datadir= " DIR_DATADIR
77
78     "\n";
79
80   ;
81
82
83 }
84
85 void
86 notice ()
87 {
88   cout <<
89     _("\n"
90     "GNU LilyPond -- The GNU Project music typesetter.\n"
91     "Copyright 1996, 97, 98 by\n"
92     "  Han-Wen Nienhuys <hanwen@stack.nl>\n"
93     "  Jan Nieuwenhuizen <jan@digicash.com>\n"
94     "\n"
95     "    This program is free software; you can redistribute it and/or\n"
96     "modify it under the terms of the GNU General Public License version 2\n"
97     "as published by the Free Software Foundation.\n"
98     "\n"
99     "    This program is distributed in the hope that it will be useful,\n"
100     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
101     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
102     "General Public License for more details.\n"
103     "\n"
104     "    You should have received a copy (refer to the file COPYING) of the\n"
105     "GNU General Public License along with this program; if not, write to\n"
106     "the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,\n"
107     "USA.\n");
108 }
109
110
111 static File_path path;
112
113 void
114 do_one_file (String init_str, String file_str)
115 {
116   if (init_str.length_i () && path.find (init_str).empty_b ())
117     {
118       error (_("Can not find `") + init_str +"\'");
119       return ;
120     }
121   if (file_str.length_i () && path.find (file_str).empty_b ())
122     {
123       error (_("Can not find `") + file_str + "'");
124       return ;
125     }
126
127   Sources sources;
128   source_global_l = &sources;
129   source_global_l->set_path (&path);
130   {
131     My_lily_parser parser (source_global_l);
132     parser.set_version_check (version_ignore_b);
133     parser.parse_file (init_str, file_str);
134     
135     if (file_str.length_i () && file_str[0] != '-')
136       {
137         String a,b,c,d;
138         split_path (file_str, a, b, c, d);
139         default_outname_base_global = c;
140       }
141     else
142       default_outname_base_global = "lelie";
143   
144     if (parser.error_level_i_)
145       {
146         exit_status_i_  = 1;
147       }
148     else
149       do_scores ();
150     clear_scores ();
151   }
152   source_global_l = 0;
153 }
154
155 void
156 identify ()
157 {
158   cout << get_version_str () << endl;
159 }
160
161 int
162 main (int argc, char **argv)
163 {
164   identify ();
165   call_constructors ();
166   debug_init ();                // should be first
167
168
169   // must override (come before) "/usr/local/share/lilypond"!
170   char const *env_sz = getenv ("LILYINCLUDE");
171   if (env_sz)
172     path.parse_path (env_sz);
173
174   path.add ("");
175   path.add (String (DIR_DATADIR) + "/init/");
176
177   path.push (DIR_DATADIR);
178
179   Getopt_long oparser (argc, argv,theopts);
180   String init_str ("lily-init.ly");
181
182   while (Long_option_init const * opt = oparser ())
183     {
184       switch (opt->shortname)
185         {
186         case 't':
187           experimental_features_global_b = true;
188           break;
189         case 'o':
190           default_outname_base_global = oparser.optional_argument_ch_C_;
191           break;
192         case 'w':
193           notice ();
194           exit (0);
195           break;
196         case 'I':
197           path.push (oparser.optional_argument_ch_C_);
198           break;
199         case 'i':
200           init_str = oparser.optional_argument_ch_C_;
201           break;
202         case 'h':
203           usage ();
204           exit (0);
205           break;
206         case 'V':
207           version_ignore_b = true;
208           break;
209         case 'P':
210           postscript_global_b = false;
211           break;
212         case 'd':
213           set_debug (true);
214           break;
215         case 'M':
216           no_paper_global_b = true;
217           break;
218         default:
219           assert (false);
220           break;
221         }
222     }
223
224   int p=0;
225   const char *arg ;
226   while ((arg= oparser.get_next_arg ()))
227     {
228       String f (arg);
229       destill_inname (f);
230       do_one_file (init_str,f);
231       p++;
232     }
233   if (!p)
234     {
235       do_one_file (init_str, "");
236     }
237
238   return exit_status_i_;
239 }
240
241 /// make input file name: add default extension. "" is stdin.
242 void
243 destill_inname (String &name_str_r)
244 {
245   if (name_str_r.length_i ())
246     {
247       if (name_str_r[ 0 ] != '-')
248         {
249           String a,b,c,d;
250           split_path (name_str_r,a,b,c,d);
251
252           // add extension if not present.
253           if (d.empty_b ())
254             d = ".ly";
255           name_str_r = a+b+c+d;
256         }
257     }
258   else name_str_r = "";
259 }