File: option_stream.c

package info (click to toggle)
swish%2B%2B 6.1.5-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 2,268 kB
  • ctags: 1,759
  • sloc: ansic: 11,931; lisp: 804; sh: 629; perl: 366; makefile: 80
file content (330 lines) | stat: -rw-r--r-- 9,959 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
**      PJL C++ Library
**      option_stream.c
**
**      Copyright (C) 1999  Paul J. Lucas
**
**      This program is free software; you can redistribute it and/or modify
**      it under the terms of the GNU General Public License as published by
**      the Free Software Foundation; either version 2 of the License, or
**      (at your option) any later version.
**
**      This program is distributed in the hope that it will be useful,
**      but WITHOUT ANY WARRANTY; without even the implied warranty of
**      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**      GNU General Public License for more details.
**
**      You should have received a copy of the GNU General Public License
**      along with this program; if not, write to the Free Software
**      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

// standard
#include <algorithm>                            /* for copy() */
#include <cstdlib>                              /* for abort(3) */
#include <cstring>
#include <iterator>
#include <iostream>

// local
#include "platform.h"
#include "option_stream.h"

using namespace std;

namespace PJL {

#define ERROR   os.err_ << os.argv_[0] << ": error: option "

//*****************************************************************************
//
// SYNOPSIS
//
        option_stream::option_stream(
            int argc, char *argv[], spec const specs[], ostream &err
        )
//
// DESCRIPTION
//
//      Construct (initialize) an option_stream.
//
// PARAMETERS
//
//      argc    The number of arguments.
//
//      argv    A vector of the arguments; argv[argc] is null.
//
//      specs   A vector of option specifications, i.e., the options that are
//              allowed and their argument parameters.
//
//      err     The ostream to emit error messages to.
//
//*****************************************************************************
    : argc_( argc ), argv_( argv ), specs_( specs ), err_( err ), argi_( 0 ),
      next_c_( 0 ), end_( false )
{
    // do nothing else
}

//*****************************************************************************
//
// SYNOPSIS
//
        option_stream& operator>>( option_stream &os, option_stream::option &o )
//
// DESCRIPTION
//
//      Parse and extract an option from an option stream (argv values).
//      Options begin with either a '-' for short options or a "--" for long
//      options.  Either a '-' or "--" by itself explicitly ends the options;
//      however, the difference is that '-' is returned as the first non-option
//      whereas "--" is skipped entirely.
//
//      When there are no more options, the option_stream converts to bool as
//      false.  The option_stream's shift() member is the number of options
//      parsed which the caller can use to adjust argc and argv.
//
//      Short options can take an argument either as the remaining characters
//      of the same argv or in the next argv unless the next argv looks like an
//      option by beginning with a '-').
//
//      Long option names can be abbreviated so long as the abbreviation is
//      unambiguous.  Long options can take an argument either directly after a
//      '=' in the same argv or in the next argv (but without an '=') unless
//      the next argv looks like an option by beginning with a '-').
//
// PARAMETERS
//
//      os  The option_stream to extract options from.
//
//      o   The option to deposit into.
//
// RETURN VALUE
//
//      Returns the passed-in option_stream.
//
//*****************************************************************************
{
    register char *arg;
    register option_stream::spec const *s, *found = 0;
    bool was_short_option = false;

    o.short_name_ = '\0';

    if ( os.next_c_ && *os.next_c_ ) {
        //
        // We left off within a grouped set of short options taking no
        // arguments, i.e., instead of -a -b -c, the user did -abc and next_c_
        // points to a character past 'a'.
        //
        arg = os.next_c_;
        goto short_option;
    }

    if ( ++os.argi_ >= os.argc_ )               // no more arguments
        goto the_end;
    arg = os.argv_[ os.argi_ ];
    if ( !arg || *arg != '-' || !*++arg )       // no more options
        goto the_end;

    if ( *arg == '-' ) {
        if ( !*++arg ) {                        // "--": end of options
            ++os.argi_;
            goto the_end;
        }
        was_short_option = false;

        //
        // Got the start of a long option: find the last character of its name.
        //
        char *end;
        for ( end = arg; *end && *end != '='; ++end ) ;

        //
        // Now look through the options table for a match.
        //
        for ( s = os.specs_; s->long_name; ++s ) {
            unsigned const len = end - arg;
            if ( ::strncmp( arg, s->long_name, len ) )
                continue;
            if ( ::strlen( s->long_name ) == len ) {
                found = s;                      // exact match
                break;
            }
            if ( !found ) {
                found = s;                      // partial match
                continue;
            }
            ERROR << '"';
            ::copy( arg, end, ostream_iterator<char>(os.err_, "") );
            os.err_ << "\" is ambiguous\n";
            goto option_error;
        }
        if ( !found ) {
            ERROR << '"';
            ::copy( arg, end, ostream_iterator<char>(os.err_, "") );
            os.err_ << "\" unrecognized\n";
            goto option_error;
        }

        //
        // Got a long option: now see about its argument.
        //
        arg = 0;
        switch ( found->arg_type ) {

            case option_stream::os_arg_none:
                if ( *end == '=' ) {
                    ERROR << '"' << found->long_name
                          << "\" takes no argument\n";
                    goto option_error;
                }
                break;

            case option_stream::os_arg_req:
            case option_stream::os_arg_opt:
                if ( *end == '=' ) {
                    arg = ++end;
                    break;
                }
                goto arg_next;

            default:
                goto bad_spec;
        }

        goto check_arg;
    }

short_option:
    was_short_option = true;
    //
    // Got a single '-' for a short option: look for it in the specs.
    //
    for ( s = os.specs_; s->short_name; ++s )
        if ( *arg == s->short_name ) {
            found = s;
            break;
        }
    if ( !found ) {
        ERROR << '"' << *arg << "\" unrecognized\n";
        goto option_error;
    }

    //
    // Found the short option: now see about its argument.
    //
    switch ( found->arg_type ) {

        case option_stream::os_arg_none:
            //
            // Set next_c_ in case the user gave a grouped set of short options
            // (see the comment near the beginning) so we can pick up where we
            // left off on the next call.
            //
            os.next_c_ = arg + 1;
            arg = 0;
            break;

        case option_stream::os_arg_req:
        case option_stream::os_arg_opt:
            //
            // Reset next_c_ since an option with either a required or optional
            // argument terminates a grouped set of options.
            //
            os.next_c_ = 0;

            if ( !*++arg ) {
                //
                // The argument, if any, is given in the next argv.
                //
arg_next:       if ( ++os.argi_ >= os.argc_ )
                    break;
                arg = os.argv_[ os.argi_ ];
                if ( *arg == '-' ) {
                    //
                    // The next argv looks like an option so assume it is one
                    // and that there is no argument for this option.
                    //
                    arg = 0;
                }
            }
            break;

        default:
            goto bad_spec;
    }

check_arg:
    if ( (arg && *arg) || found->arg_type != option_stream::os_arg_req ) {
        o.short_name_ = found->short_name;
        o.arg_ = arg;
        return os;
    }
    ERROR << '"';
    if ( was_short_option )
        os.err_ << found->short_name;
    else
        os.err_ << found->long_name;
    os.err_ << "\" requires an argument\n";
    // fall through
option_error:
    o.arg_ = '\0';
    return os;
the_end:
    os.end_ = true;
    return os;
bad_spec:
    ERROR << "invalid option argument spec: " << found->arg_type << '\n';
    ::abort();
}

} // namespace PJL

//*****************************************************************************

/*#define TEST_OPTION_STREAM /**/
#ifdef  TEST_OPTION_STREAM

using namespace PJL;

int main( int argc, char *argv[] ) {
    static option_stream::spec const spec[] = {
        "n-no-arg",     0, 'n',
        "m-no-arg",     0, 'm',
        "r-req-arg",    1, 'r',
        "o-opt-arg",    2, 'o',
        0,
    };
    option_stream opt_in( argc, argv, spec );
    option_stream::option opt;
    while ( opt_in >> opt ) {
        switch ( opt ) {
            case 'm':
                cout << "got --m-no-arg\n";
                break;
            case 'n':
                cout << "got --n-no-arg\n";
                break;
            case 'r':
                cout << "got --r-req-arg=" << opt.arg() << '\n';
                break;
            case 'o':
                cout << "got --o-opt-arg=";
                cout << (opt.arg() ? opt.arg() : "(null)") << '\n';
                break;
            default:
                cout << "got weird=" << (int)(char)opt << '\n';
        }
    }
    cout << "shift=" << opt_in.shift() << '\n';

    argc -= opt_in.shift();
    argv += opt_in.shift();

    cout << "argc=" << argc << endl;
    cout << "first non-option=" << (argv[0] ? argv[0] : "(null)") << endl;
}

#endif  /* TEST_OPTION_STREAM */
/* vim:set et sw=4 ts=4: */