]> git.donarmstrong.com Git - lilypond.git/blob - flower/getopt-long.cc
Web-ja: update introduction
[lilypond.git] / flower / getopt-long.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2015 Han-Wen Nienhuys, <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "getopt-long.hh"
21
22 #include <cstring>
23 #include <cassert>
24 #include <cstdlib>
25
26 #include "config.hh"
27 #include "international.hh"
28
29 #if !HAVE_GETTEXT
30 inline char *
31 gettext (char const *s)
32 {
33   return (char *)s;
34 }
35 #else
36 #include <libintl.h>
37 #endif
38
39 long
40 Getopt_long::get_argument_index ()
41 {
42   long l;
43   if (!optional_argument_str0_
44       || sscanf (optional_argument_str0_, "%ld", &l) != 1)
45     report (E_ILLEGALARG);
46
47   return l;
48 }
49
50 const Long_option_init *
51 Getopt_long::parselong ()
52 {
53   char const *optnm = arg_value_char_a_a_[array_index_] + 2;
54   assert (*optnm);
55
56   char const *endopt = strchr (optnm, '=');
57   size_t searchlen = (endopt) ? (size_t) (endopt - optnm) : strlen (optnm);
58
59   found_option_ = 0;
60   for (int i = 0; i < table_len_; i++)
61     {
62       char const *ln = option_a_[i].longname_str0_;
63
64       if (ln && !strncmp (ln, optnm, searchlen))
65         {
66           found_option_ = option_a_ + i;
67           break;
68         }
69     }
70
71   if (!found_option_)
72     {
73       report (E_UNKNOWNOPTION);
74       return 0;
75     }
76   array_index_++;
77   argument_index_ = 0;
78
79   if (found_option_->take_arg_str0_)
80     {
81       if (endopt)
82         optional_argument_str0_ = endopt + 1; // a '='
83       else
84         {
85           optional_argument_str0_ = arg_value_char_a_a_[array_index_];
86           array_index_++;
87         }
88       if (!optional_argument_str0_)
89         report (E_ARGEXPECT);
90     }
91   else
92     {
93       optional_argument_str0_ = 0;
94       if (endopt)
95         report (E_NOARGEXPECT);
96     }
97
98   return found_option_;
99 }
100
101 string
102 Long_option_init::to_string () const
103 {
104   string str;
105   if (shortname_char_)
106     str += string ("-") + shortname_char_;
107   if (shortname_char_ && longname_str0_)
108     str += ", ";
109   if (longname_str0_)
110     str += string ("--") + longname_str0_;
111   return str;
112 }
113
114 string
115 Long_option_init::str_for_help () const
116 {
117   string s;
118   if (shortname_char_)
119     s = "-" + ::to_string (shortname_char_);
120   else
121     s = "  ";
122
123   s = s + ((shortname_char_ && longname_str0_) ? ", " : "  ");
124
125   if (longname_str0_)
126     s = s + "--" + longname_str0_;
127
128   if (take_arg_str0_)
129     {
130       if (longname_str0_)
131         s = s + "=";
132       else
133         s = s + " ";
134
135       s = s + gettext (take_arg_str0_);
136     }
137   return s;
138 }
139
140 // report an error, GNU style.
141 void
142 Getopt_long::report (Errorcod c)
143 {
144   error_ = c;
145   if (!error_out_)
146     return;
147
148   string str = arg_value_char_a_a_[0];
149   str += ": ";
150   switch (c)
151     {
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);
172     }
173   fprintf (error_out_, "%s\n", str.c_str ());
174   exit (2);
175 }
176
177 const Long_option_init *
178 Getopt_long::parseshort ()
179 {
180   char c = arg_value_char_a_a_[array_index_][argument_index_];
181   found_option_ = 0;
182   assert (c);
183
184   for (int i = 0; i < table_len_; i++)
185     if (option_a_[i].shortname_char_ == c)
186       {
187         found_option_ = option_a_ + i;
188         break;
189       }
190
191   if (!found_option_)
192     {
193       report (E_UNKNOWNOPTION);
194       return 0;
195     }
196
197   argument_index_++;
198   if (!found_option_->take_arg_str0_)
199     {
200       optional_argument_str0_ = 0;
201       return found_option_;
202     }
203   optional_argument_str0_ = arg_value_char_a_a_[array_index_] + argument_index_;
204
205   array_index_++;
206   argument_index_ = 0;
207
208   if (!optional_argument_str0_[0])
209     {
210       optional_argument_str0_ = arg_value_char_a_a_[array_index_];
211       array_index_++;
212     }
213   if (!optional_argument_str0_)
214     report (E_ARGEXPECT);
215
216   return found_option_;
217 }
218
219 const Long_option_init *
220 Getopt_long::operator () ()
221 {
222   if (!ok ())
223     return 0;
224
225   next ();
226   if (!ok ())
227     return 0;
228
229   if (argument_index_)
230     return parseshort ();
231
232   char const *argument = arg_value_char_a_a_[array_index_];
233
234   if (argument[0] != '-')
235     return 0;
236
237   if (argument[1] == '-')  // what to do with "command  --  bla"
238     {
239       if (argument[2])
240         return parselong ();
241       else
242         return 0;
243     }
244   else
245     {
246       if (argument[ 1 ])
247         {
248           argument_index_ = 1;
249           return parseshort ();
250         }
251       else
252         return 0;
253     }
254 }
255
256 Getopt_long::Getopt_long (int c, char **v, Long_option_init *lo)
257 {
258   option_a_ = lo;
259   error_out_ = stderr;
260   arg_value_char_a_a_ = v;
261   argument_count_ = c;
262   array_index_ = 1;
263   argument_index_ = 0;
264
265   //    reached end of option table?
266   table_len_ = 0;
267   for (int i = 0; option_a_[i].longname_str0_ || option_a_[i].shortname_char_; i++)
268     table_len_++;
269 }
270
271 bool
272 Getopt_long::ok () const
273 {
274   return array_index_ < argument_count_;
275 }
276
277 void
278 Getopt_long::next ()
279 {
280   error_ = E_NOERROR;
281   while (array_index_ < argument_count_
282          && !arg_value_char_a_a_[array_index_][argument_index_])
283     {
284       array_index_++;
285       argument_index_ = 0;
286     }
287 }
288
289 char const *
290 Getopt_long::current_arg ()
291 {
292   if (array_index_ >= argument_count_)
293     return 0;
294   char const *a = arg_value_char_a_a_[array_index_];
295   return a + argument_index_;
296 }
297
298 char const *
299 Getopt_long::get_next_arg ()
300 {
301   char const *a = current_arg ();
302   if (a)
303     {
304       array_index_++;
305       argument_index_ = 0;
306     }
307   return a;
308 }
309
310 const int EXTRA_SPACES = 5;
311
312 string
313 Long_option_init::table_string (Long_option_init *l)
314 {
315   string tabstr = "";
316
317   size_t wid = 0;
318   for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
319     wid = max (wid, l[i].str_for_help ().length ());
320
321   for (int i = 0; l[i].shortname_char_ || l[i].longname_str0_; i++)
322     {
323       string s = "  " + l[i].str_for_help ();
324       s += string (wid - s.length () + EXTRA_SPACES, ' ');
325
326       string help_text (gettext (l[i].help_str0_));
327       replace_all (&help_text, "\n",
328                    "\n" + string (wid + EXTRA_SPACES + 2, ' '));
329       tabstr += s + help_text + "\n";
330     }
331
332   return tabstr;
333 }
334
335 int
336 Long_option_init::compare (Long_option_init const &a, Long_option_init const &b)
337 {
338   if (a.shortname_char_ && b.shortname_char_ && a.shortname_char_ - b.shortname_char_)
339     return a.shortname_char_ - b.shortname_char_;
340
341   if (b.shortname_char_ && a.longname_str0_)
342     {
343       char s[2] = {b.shortname_char_, 0};
344       return strcmp (a.longname_str0_, s);
345     }
346   if (a.shortname_char_ && b.longname_str0_)
347     {
348       char s[2] = {a.shortname_char_, 0};
349       return strcmp (s, b.longname_str0_);
350     }
351
352   return strcmp (a.longname_str0_, b.longname_str0_);
353 }