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