]> git.donarmstrong.com Git - lilypond.git/blob - lily/main.cc
6f3b7b07e40717072b92d15df5a62458cdeea883
[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   
170   path.add ("");
171   // must override (come before) "/usr/local/share/lilypond"!
172   char const *env_sz = getenv ("LILYINCLUDE");
173   if (env_sz)
174     path.parse_path (env_sz);
175
176   path.add (String (DIR_DATADIR) + "/init/");
177
178   path.push (DIR_DATADIR);
179
180   Getopt_long oparser (argc, argv,theopts);
181   String init_str ("lily-init.ly");
182
183   while (Long_option_init const * opt = oparser ())
184     {
185       switch (opt->shortname)
186         {
187         case 't':
188           experimental_features_global_b = true;
189           break;
190         case 'o':
191           default_outname_base_global = oparser.optional_argument_ch_C_;
192           break;
193         case 'w':
194           notice ();
195           exit (0);
196           break;
197         case 'I':
198           path.push (oparser.optional_argument_ch_C_);
199           break;
200         case 'i':
201           init_str = oparser.optional_argument_ch_C_;
202           break;
203         case 'h':
204           usage ();
205           exit (0);
206           break;
207         case 'V':
208           version_ignore_b = true;
209           break;
210         case 'P':
211           postscript_global_b = false;
212           break;
213         case 'd':
214           set_debug (true);
215           break;
216         case 'M':
217           no_paper_global_b = true;
218           break;
219         default:
220           assert (false);
221           break;
222         }
223     }
224
225   int p=0;
226   const char *arg ;
227   while ((arg= oparser.get_next_arg ()))
228     {
229       String f (arg);
230       destill_inname (f);
231       do_one_file (init_str,f);
232       p++;
233     }
234   if (!p)
235     {
236       do_one_file (init_str, "");
237     }
238
239   return exit_status_i_;
240 }
241
242 /// make input file name: add default extension. "" is stdin.
243 void
244 destill_inname (String &name_str_r)
245 {
246   if (name_str_r.length_i ())
247     {
248       if (name_str_r[ 0 ] != '-')
249         {
250           String a,b,c,d;
251           split_path (name_str_r,a,b,c,d);
252
253           // add extension if not present.
254           if (d.empty_b ())
255             d = ".ly";
256           name_str_r = a+b+c+d;
257         }
258     }
259   else name_str_r = "";
260 }