Start pictograms branch.
[lilypond/mpolesky.git] / flower / getopt-long.cc
blobbbc6c2e7305678f93454d7c5cd819588916da756
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1996--2010 Han-Wen Nienhuys, <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "getopt-long.hh"
22 #include <cstring>
23 #include <cassert>
24 #include <cstdlib>
26 #include "config.hh"
27 #include "international.hh"
29 #if !HAVE_GETTEXT
30 inline char *
31 gettext (char const *s)
33 return (char *)s;
35 #else
36 #include <libintl.h>
37 #endif
39 long
40 Getopt_long::get_argument_index ()
42 long l;
43 if (!optional_argument_str0_
44 || sscanf (optional_argument_str0_, "%ld", &l) != 1)
45 report (E_ILLEGALARG);
47 return l;
50 const Long_option_init *
51 Getopt_long::parselong ()
53 char const *optnm = arg_value_char_a_a_[array_index_] + 2;
54 assert (*optnm);
56 char const *endopt = strchr (optnm, '=');
57 int searchlen = (endopt) ? endopt - optnm : strlen (optnm);
59 found_option_ = 0;
60 for (int i = 0; i < table_len_; i++)
62 char const *ln = option_a_[i].longname_str0_;
64 if (ln && !strncmp (ln, optnm, searchlen))
66 found_option_ = option_a_ + i;
67 break;
71 if (!found_option_)
73 report (E_UNKNOWNOPTION);
74 return 0;
76 array_index_++;
77 argument_index_ = 0;
79 if (found_option_->take_arg_str0_)
81 if (endopt)
82 optional_argument_str0_ = endopt +1; // a '='
83 else
85 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
86 array_index_++;
88 if (!optional_argument_str0_)
89 report (E_ARGEXPECT);
91 else
93 optional_argument_str0_ = 0;
94 if (endopt)
95 report (E_NOARGEXPECT);
98 return found_option_;
101 string
102 Long_option_init::to_string () const
104 string str;
105 if (shortname_char_)
106 str += "-" + shortname_char_;
107 if (shortname_char_ && longname_str0_)
108 str += ", ";
109 if (longname_str0_)
110 str += string ("`--") + longname_str0_ + "'";
111 return str;
114 string
115 Long_option_init::str_for_help () const
117 string s;
118 if (shortname_char_)
119 s = "-" + ::to_string (shortname_char_);
120 else
121 s = " ";
123 s = s + ((shortname_char_ && longname_str0_) ? ", " : " ");
125 if (longname_str0_)
126 s = s + "--" + longname_str0_;
128 if (take_arg_str0_)
130 if (longname_str0_)
131 s = s + "=";
132 else
133 s = s + " ";
135 s = s + gettext (take_arg_str0_);
137 return s;
140 // report an error, GNU style.
141 void
142 Getopt_long::report (Errorcod c)
144 error_ = c;
145 if (!error_out_)
146 return;
148 string str = arg_value_char_a_a_[0];
149 str += ": ";
150 switch (c)
152 case E_ARGEXPECT:
153 str += _f ("option `%s' requires an argument",
154 found_option_->to_string ());
155 break;
156 case E_NOARGEXPECT:
157 str += _f ("option `%s' does not allow an argument",
158 found_option_->to_string ());
159 break;
160 case E_UNKNOWNOPTION:
161 str += _f ("unrecognized option: `%s'",
162 string (argument_index_
163 ? string ("-" + string (1, arg_value_char_a_a_[array_index_][argument_index_]))
164 : string (arg_value_char_a_a_[array_index_])));
165 break;
166 case E_ILLEGALARG:
167 str += _f ("invalid argument `%s' to option `%s'",
168 optional_argument_str0_, found_option_->to_string ());
169 break;
170 default:
171 assert (false);
173 fprintf (error_out_, "%s\n", str.c_str ());
174 exit (2);
177 const Long_option_init *
178 Getopt_long::parseshort ()
180 char c = arg_value_char_a_a_[array_index_][argument_index_];
181 found_option_ = 0;
182 assert (c);
184 for (int i = 0; i < table_len_; i++)
185 if (option_a_[i].shortname_char_ == c)
187 found_option_ = option_a_ + i;
188 break;
191 if (!found_option_)
193 report (E_UNKNOWNOPTION);
194 return 0;
197 argument_index_++;
198 if (!found_option_->take_arg_str0_)
200 optional_argument_str0_ = 0;
201 return found_option_;
203 optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
205 array_index_++;
206 argument_index_ = 0;
208 if (!optional_argument_str0_[0])
210 optional_argument_str0_ = arg_value_char_a_a_[array_index_];
211 array_index_++;
213 if (!optional_argument_str0_)
214 report (E_ARGEXPECT);
216 return found_option_;
219 const Long_option_init *
220 Getopt_long::operator () ()
222 if (!ok ())
223 return 0;
225 next ();
226 if (!ok ())
227 return 0;
229 if (argument_index_)
230 return parseshort ();
232 char const *argument = arg_value_char_a_a_[array_index_];
234 if (argument[0] != '-')
235 return 0;
237 if (argument[1] == '-') {// what to do with "command -- bla"
238 if (argument[2])
239 return parselong ();
240 else
241 return 0;
243 else
245 if (argument[ 1 ])
247 argument_index_ = 1;
248 return parseshort ();
250 else
251 return 0;
255 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
257 option_a_ = lo;
258 error_out_ = stderr;
259 arg_value_char_a_a_ = v;
260 argument_count_ = c;
261 array_index_ = 1;
262 argument_index_ = 0;
264 // reached end of option table?
265 table_len_ = 0;
266 for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
267 table_len_++;
270 bool
271 Getopt_long::ok () const
273 return array_index_ < argument_count_;
276 void
277 Getopt_long::next ()
279 error_ = E_NOERROR;
280 while (array_index_ < argument_count_
281 && !arg_value_char_a_a_[array_index_][argument_index_])
283 array_index_++;
284 argument_index_ = 0;
288 char const *
289 Getopt_long::current_arg ()
291 if (array_index_ >= argument_count_)
292 return 0;
293 char const *a = arg_value_char_a_a_[array_index_];
294 return a + argument_index_;
297 char const *
298 Getopt_long::get_next_arg ()
300 char const *a = current_arg ();
301 if (a)
303 array_index_++;
304 argument_index_ = 0;
306 return a;
309 const int EXTRA_SPACES = 5;
311 string
312 Long_option_init::table_string (Long_option_init *l)
314 string tabstr = "";
316 int wid = 0;
317 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
318 wid = max (int(wid), int(l[i].str_for_help ().length ()));
320 for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
322 string s = " " + l[i].str_for_help ();
323 s += string (wid - s.length () + EXTRA_SPACES, ' ');
325 string help_text (gettext (l[i].help_str0_));
326 replace_all (&help_text, "\n",
327 "\n" + string (wid + EXTRA_SPACES + 2, ' '));
328 tabstr += s + help_text + "\n";
331 return tabstr;
335 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
337 if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_- b.shortname_char_)
338 return a.shortname_char_ - b.shortname_char_;
340 if (b.shortname_char_ && a.longname_str0_)
342 char s[2] = {b.shortname_char_, 0};
343 return strcmp (a.longname_str0_, s);
345 if (a.shortname_char_ && b.longname_str0_)
347 char s[2] = {a.shortname_char_, 0};
348 return strcmp (s, b.longname_str0_);
351 return strcmp (a.longname_str0_, b.longname_str0_);