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