lilypond-0.0.1
[lilypond.git] / flower / lgetopt.cc
blobac9a12d0359da9250419cec8bef039da945c293b
1 /*
2 process command line, GNU style.
5 this is (Copyleft) 1996, Han-Wen Nienhuys, <hanwen@stack.urc.tue.nl>
6 */
7 #include <stdio.h>
8 #include <iostream.h>
9 #include <assert.h>
10 #include "lgetopt.hh"
12 long
13 Getopt_long::intarg() {
14 long l;
15 if (sscanf(optarg, "%ld", &l) != 1)
16 report(E_ILLEGALARG);
18 return l;
20 long_option_init *
21 Getopt_long::parselong() {
22 const char *optnm = argv[optind] + 2 ;
23 assert(*optnm);
25 char *endopt = strchr(optnm, '=');
26 int searchlen = (endopt) ? endopt - optnm : strlen(optnm);
28 beet=0;
29 for (int i=0; i< table_len; i++) {
30 const char *ln = the_opts[i].longname;
32 if (ln && !strncmp(ln, optnm, searchlen)) {
33 beet = the_opts+i;
34 break;
38 if (!beet) {
39 report(E_UNKNOWNOPTION);
40 return 0;
42 optind++;
43 optindind = 0;
46 if (beet->take_arg) {
47 if (endopt)
48 optarg = endopt +1; // a '='
49 else {
50 optarg = argv[optind];
51 optind++;
53 if (!optarg)
54 report(E_ARGEXPECT);
56 } else {
57 optarg = 0;
58 if (endopt)
59 report(E_NOARGEXPECT);
62 return beet;
66 ostream &
67 long_option_init::printon(ostream &errorout)
69 if (shortname)
70 errorout <<"-" << shortname;
71 if (shortname && longname)
72 errorout << ", ";
73 if (longname)
74 errorout << "`--" << longname << "'";
75 return errorout;
78 // report an error, GNU style.
79 void
80 Getopt_long::report(Errorcod c)
82 error = c;
83 if (!errorout)
84 return;
86 *errorout << argv[0] << ": ";
87 switch (c) {
88 case E_ARGEXPECT:
89 *errorout<< "option ";
90 beet->printon(*errorout);
91 *errorout << "requires an argument"<<endl;
92 break;
93 case E_NOARGEXPECT:
94 *errorout << "option `--" << beet->longname << "' does not allow an argument"<<endl;
95 break;
97 case E_UNKNOWNOPTION:
98 *errorout << "unrecognized option ";
99 if (optindind)
100 *errorout << "-" << argv[optind][optindind] << endl;
101 else
102 *errorout << argv[optind] << endl;
104 break;
105 case E_ILLEGALARG:
106 *errorout << "illegal argument `" << optarg << "\'to option ";
107 beet->printon(*errorout);
108 *errorout << '\n';
109 default:
110 assert(false);
112 exit(2);
115 long_option_init *
116 Getopt_long::parseshort() {
117 char c=argv[optind][optindind];
118 beet=0;
119 assert(c);
121 for (int i=0; i < table_len; i++)
122 if (the_opts[i].shortname == c) {
123 beet = the_opts+i;
124 break;
127 if (!beet){
128 report(E_UNKNOWNOPTION);
129 return 0;
132 optindind++;
133 if (!beet->take_arg){
134 optarg = 0;
135 return beet;
137 optarg = argv[optind] + optindind;
139 optind ++;
140 optindind = 0;
142 if (!optarg[0]) {
143 optarg = argv[optind];
144 optind ++;
146 if (!optarg) {
147 report(E_ARGEXPECT);
150 return beet;
153 long_option_init *
154 Getopt_long::operator()() {
155 if (!next())
156 return 0;
158 if (optindind)
159 return parseshort();
161 if (argv[optind][0] != '-')
162 return 0;
164 if (argv[optind][1] == '-') {// what to do with "command -- bla"
165 return parselong();
166 } else {
167 optindind = 1;
168 return parseshort();
172 Getopt_long::Getopt_long(int c, char **v, long_option_init *lo) {
173 the_opts = lo;
174 errorout = &cerr;
175 argv = v;
176 argc = c;
177 optind = 1;
178 optindind = 0;
180 // reached end of option table?
181 int i;
182 for (i = 0; the_opts[i].longname ||the_opts[i].shortname; i++)
184 table_len = i;
187 bool Getopt_long::next() {
188 error = E_NOERROR;
189 while (optind < argc && !argv[optind][optindind]) {
190 optind++;
191 optindind = 0;
193 return (optind < argc);
196 char *
197 Getopt_long::current_arg()
199 if (optind >= argc)
200 return 0;
201 char * a = argv[optind];
202 return a + optindind;
205 char *
206 Getopt_long::get_next_arg()
208 char * a = current_arg();
209 if ( a) {
210 optind ++;
211 optindind = 0;
213 return a;