[go: up one dir, main page]

File: arglex.cc

package info (click to toggle)
srecord 1.58-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 5,144 kB
  • ctags: 2,732
  • sloc: cpp: 26,774; sh: 7,053; makefile: 2,889; awk: 187; vhdl: 15
file content (736 lines) | stat: -rw-r--r-- 16,320 bytes parent folder | download | duplicates (2)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//
// srecord - manipulate eprom load files
// Copyright (C) 1998, 1999, 2002, 2003, 2006-2011 Peter Miller
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 3 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 Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

#include <cctype>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <errno.h>
#include <iostream>
#include <unistd.h>

#include <srecord/arglex.h>
#include <srecord/progname.h>
#include <srecord/quit.h>
#include <srecord/versn_stamp.h>

// Cygwin's mingw has the execvp prototype in the wrong place.
#ifdef __MSVCRT__
#include <process.h>
#endif

static const srecord::arglex::table_ty default_table[] =
{
    { "-",            srecord::arglex::token_stdio,       },
    { "-Help",        srecord::arglex::token_help,        },
    { "-LICense",     srecord::arglex::token_license,     },
    { "-Page_Length", srecord::arglex::token_page_length, },
    { "-Page_Width",  srecord::arglex::token_page_width,  },
    { "-TRACIng",     srecord::arglex::token_tracing,     },
    { "-Verbose",     srecord::arglex::token_verbose,     },
    { "-VERSion",     srecord::arglex::token_version,     },
    SRECORD_ARGLEX_END_MARKER
};


srecord::arglex::arglex() :
    usage_tail_(0)
{
    table_set(default_table);
}


srecord::arglex::arglex(int ac, char **av) :
    usage_tail_(0)
{
    progname_set(av[0]);
    for (int j = 1; j < ac; ++j)
    {
        if (av[j][0] == '@')
            read_arguments_file(av[j] + 1);
        else
            arguments.push_back(av[j]);
    }
    table_set(default_table);
}


void
srecord::arglex::read_arguments_file(const char *filename)
{
    FILE *fp = fopen(filename, "r");
    if (!fp)
        quit_default.fatal_error_errno("open \"%s\"", filename);
    for (;;)
    {
        int sc = getc(fp);
        if (sc == EOF)
            break;
        unsigned char c = sc;

        //
        // Ignore white space between words.
        //
        if (isspace(c))
            continue;

        //
        // Ignore comments
        //
        if (c == '#')
        {
            for (;;)
            {
                sc = getc(fp);
                if (sc == EOF || sc == '\n')
                    break;
            }
            continue;
        }
        char buffer[1000];
        char *bp = buffer;
        for (;;)
        {
            if (bp < buffer + sizeof(buffer) - 1)
                *bp++ = c;
            sc = getc(fp);
            if (sc == EOF)
                break;
            c = sc;
            if (isspace(c))
                break;
            if (c == '#')
            {
                ungetc(c, fp);
                break;
            }
        }
        *bp = 0;
        if (buffer[0] == '@')
            read_arguments_file(buffer + 1);
        else
            arguments.push_back(std::string(buffer, bp - buffer));
    }
    fclose(fp);
}


srecord::arglex::~arglex()
{
}


void
srecord::arglex::table_set(const table_ty *tp)
{
    tables.push_back(tp);
}


static const char *partial;


bool
srecord::arglex::compare(const char *formal, const char *actual)
{
    for (;;)
    {
        unsigned char ac = *actual++;
        if (isupper(ac))
            ac = tolower(ac);
        unsigned char fc = *formal++;
        switch (fc)
        {
        case 0:
            return !ac;

        case '_':
            if (ac == '-')
                break;
            // fall through...

        case 'a': case 'b': case 'c': case 'd': case 'e':
        case 'f': case 'g': case 'h': case 'i': case 'j':
        case 'k': case 'l': case 'm': case 'n': case 'o':
        case 'p': case 'q': case 'r': case 's': case 't':
        case 'u': case 'v': case 'w': case 'x': case 'y':
        case 'z':
            //
            // optional characters
            //
            if (ac == fc && compare(formal, actual))
                return true;

            //
            // skip forward to next
            // mandatory character, or after '_'
            //
            while (islower(*formal))
                ++formal;
            if (*formal == '_')
            {
                ++formal;
                if (ac == '_' || ac == '-')
                    ++actual;
            }
            --actual;
            break;

        case '*':
            //
            // This is incomplete, it should really
            // check for a match match of the stuff after
            // the '*', too, a la glob.
            //
            if (!ac)
                return false;
            partial = actual - 1;
            return true;

        case '\\':
            if (actual[-1] != *formal++)
                return false;
            break;

        case 'A': case 'B': case 'C': case 'D': case 'E':
        case 'F': case 'G': case 'H': case 'I': case 'J':
        case 'K': case 'L': case 'M': case 'N': case 'O':
        case 'P': case 'Q': case 'R': case 'S': case 'T':
        case 'U': case 'V': case 'W': case 'X': case 'Y':
        case 'Z':
            fc = tolower(fc);
            // fall through...

        default:
            //
            // mandatory characters
            //
            if (fc != ac)
                return false;
            break;
        }
    }
}


//
// NAME
//      is_a_number
//
// SYNOPSIS
//      int is_a_number(char *s);
//
// DESCRIPTION
//      The is_a_number function is used to determine if the
//      argument is a number.
//
//      The value is placed in arglex_value.alv_number as
//      a side effect.
//
//      Negative and positive signs are accepted.
//      The C conventions for decimal, octal and hexadecimal are understood.
//
//      There may be no white space anywhere in the string,
//      and the string must end after the last digit.
//      Trailing garbage will be interpreted to mean it is not a string.
//
// ARGUMENTS
//      s       - string to be tested and evaluated
//
// RETURNS
//      int;    zero if not a number,
//              non-zero if is a number.
//

static int
is_a_number(const char *s, long &n)
{
    int         sign;

    n = 0;
    switch (*s)
    {
    case '-':
        ++s;
        sign = -1;
        break;

    case '+':
        ++s;
        sign = 1;
        break;

    default:
        sign = 1;
        break;
    }
    switch (*s)
    {
    case '0':
        if ((s[1] == 'x' || s[1] == 'X') && s[2])
        {
            s += 2;
            for (;;)
            {
                switch (*s)
                {
                case '0': case '1': case '2': case '3':
                case '4': case '5': case '6': case '7':
                case '8': case '9':
                    n = n * 16 + *s++ - '0';
                    continue;

                case 'A': case 'B': case 'C':
                case 'D': case 'E': case 'F':
                    n = n * 16 + *s++ - 'A' + 10;
                    continue;

                case 'a': case 'b': case 'c':
                case 'd': case 'e': case 'f':
                    n = n * 16 + *s++ - 'a' + 10;
                    continue;
                }
                break;
            }
        }
        else
        {
            for (;;)
            {
                switch (*s)
                {
                case '0': case '1': case '2': case '3':
                case '4': case '5': case '6': case '7':
                    n = n * 8 + *s++ - '0';
                    continue;
                }
                break;
            }
        }
        break;

    case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9':
        for (;;)
        {
            switch (*s)
            {
            case '0': case '1': case '2': case '3':
            case '4': case '5': case '6': case '7':
            case '8': case '9':
                n = n * 10 + *s++ - '0';
                continue;
            }
            break;
        }
        break;

default:
        return 0;
    }
    if (*s)
        return 0;
    n *= sign;
    return 1;
}


//
// NAME
//      arglex
//
// SYNOPSIS
//      int arglex::token_next(void);
//
// DESCRIPTION
//      The arglex function is used to perfom lexical analysis
//      on the command line arguments.
//
//      Unrecognised options are returned as arglex_token_option
//      for anything starting with a '-', or
//      arglex_token_string otherwise.
//
// RETURNS
//      The next token in the token stream.
//      When the end is reached, arglex_token_eoln is returned forever.
//
// CAVEAT
//      Must call arglex_init befor this function is called.
//

int
srecord::arglex::token_next()
{
    const table_ty  *tp;
    const table_ty  *hit[20];
    int             nhit;

    std::string arg;
    if (!pushback.empty())
    {
        //
        // the second half of a "-foo=bar" style argument.
        //
        arg = pushback.back();
        pushback.pop_back();
    }
    else
    {
        if (arguments.empty())
        {
            value_string_ = "";
            token = token_eoln;
            return token;
        }
        arg = arguments.front();
        arguments.pop_front();

        //
        // See if it looks like a GNU "-foo=bar" option.
        // Split it at the '=' to make it something the
        // rest of the code understands.
        //
        if (arg[0] == '-' && arg[1] != '=')
        {
            const char *eqp = strchr(arg.c_str(), '=');
            if (eqp)
            {
                pushback.push_back(eqp + 1);
                arg = std::string(arg.c_str(), eqp - arg.c_str());
            }
        }

        //
        // Turn the GNU-style leading "--"
        // into "-" if necessary.
        //
        if
        (
            arg.size() > 2
        &&
            arg[0] == '-'
        &&
            arg[1] == '-'
        &&
            !is_a_number(arg.c_str() + 1, value_number_)
        )
            arg = std::string(arg.c_str() + 1);
    }
    value_string_ = arg;

    //
    // see if it is a number
    //
    if (is_a_number(arg.c_str(), value_number_))
    {
        token = arglex::token_number;
        return token;
    }

    //
    // scan the tables to see what it matches
    //
    nhit = 0;
    partial = 0;
    for
    (
        table_ptr_vec_t::iterator it = tables.begin();
        it != tables.end();
        ++it
    )
    {
        for (tp = *it; tp->name; tp++)
        {
            if (compare(tp->name, arg.c_str()))
                hit[nhit++] = tp;
        }
    }

    //
    // deal with unknown or ambiguous options
    //
    switch (nhit)
    {
    case 0:
        //
        // not found in the tables
        //
        if (value_string_[0] == '-')
            token = arglex::token_option;
        else
            token = arglex::token_string;
        break;

    default:
        //
        // if all the hits are the same, it isn't ambiguous
        //
        {
            bool all_same = true;
            std::string possibilities = hit[0]->name;
            for (int k = 1; k < nhit; ++k)
            {
                if (hit[0]->token != hit[k]->token)
                    all_same = false;
                possibilities += ", ";
                possibilities += hit[k]->name;
            }
            if (!all_same)
            {
                fatal_error
                (
                    "option \"%s\" is ambiguous, did you mean one of: %s?",
                    value_string_.c_str(),
                    possibilities.c_str()
                );
                // NOTREACHED
            }
        }
        // fall through...

    case 1:
        if (partial)
        {
            pushback.push_back(partial);
            partial = 0;
        }
        value_string_ = hit[0]->name;
        token = hit[0]->token;
        check_deprecated(arg);
        break;
    }
    return token;
}


void
srecord::arglex::deprecated_option(const std::string &old_fashioned)
{
    deprecated_options.push_back(old_fashioned);
}


void
srecord::arglex::check_deprecated(const std::string &actual)
    const
{
    for
    (
        deprecated_options_t::const_iterator it = deprecated_options.begin();
        it != deprecated_options.end();
        ++it
    )
    {
        std::string formal = *it;
        if (compare(formal.c_str(), actual.c_str()))
        {
            quit_default.warning
            (
                "option \"%s\" is deprecated, use \"%s\" instead",
                formal.c_str(),
                token_name(token)
            );
        }
    }
}


const char *
srecord::arglex::token_name(int n)
    const
{
    switch (n)
    {
    case token_eoln:
        return "end of command line";

    case token_number:
        return "number";

    case token_option:
        return "option";

    case token_stdio:
        return "standard input or output";

    case token_string:
        return "string";

    default:
        break;
    }
    for
    (
        table_ptr_vec_t::const_iterator it = tables.begin();
        it != tables.end();
        ++it
    )
    {
        for (const table_ty *tp = *it; tp->name; ++tp)
        {
            if (tp->token == n)
                return tp->name;
        }
    }
    return "unknown command line token";
}


void
srecord::arglex::help(const char *name)
    const
{
    if (!name)
        name = progname_get();
    const char *cmd[3] = { "man", name, 0 };
    execvp(cmd[0], (char *const *)cmd);
    std::cerr << cmd[0] << ": " << strerror(errno) << std::endl;
    exit(1);
}


void
srecord::arglex::version()
    const
{
    print_version();
    exit(0);
}


void
srecord::arglex::license()
    const
{
    help("srecord::license");
}


void
srecord::arglex::bad_argument()
    const
{
    switch (token_cur())
    {
    case token_string:
        std::cerr << "misplaced file name (\"" << value_string()
            << "\") on command line" << std::endl;
        break;

    case token_number:
        std::cerr << "misplaced number (" << value_string()
            << ") on command line" << std::endl;
        break;

    case token_option:
        std::cerr << "unknown \"" << value_string() << "\" option" << std::endl;
        break;

    case token_eoln:
        std::cerr << "command line too short" << std::endl;
        break;

    default:
        std::cerr << "misplaced \"" << value_string() << "\" option"
            << std::endl;
        break;
    }
    usage();
    // NOTREACHED
}


int
srecord::arglex::token_first()
{
    switch (token_next())
    {
    default:
        return token_cur();

    case token_help:
        if (token_next() != token_eoln)
            bad_argument();
        help();
        break;

    case token_version:
        if (token_next() != token_eoln)
            bad_argument();
        version();
        break;

    case token_license:
        if (token_next() != token_eoln)
            bad_argument();
        license();
        break;
    }
    exit(0);
}


void
srecord::arglex::usage_tail_set(const char *s)
{
    usage_tail_ = s;
}


const char *
srecord::arglex::usage_tail_get()
    const
{
    if (!usage_tail_)
        usage_tail_ = "<filename>...";
    return usage_tail_;
}


void
srecord::arglex::usage()
    const
{
    std::cerr << "Usage: " << progname_get() << " [ <option>... ] "
        << usage_tail_get() << std::endl;
    std::cerr << "       " << progname_get() << " -Help" << std::endl;
    std::cerr << "       " << progname_get() << " -VERSion" << std::endl;
    std::cerr << "       " << progname_get() << " -LICense" << std::endl;
    exit(1);
    // NOTREACHED
}


void
srecord::arglex::default_command_line_processing()
{
    bad_argument();
}


void
srecord::arglex::fatal_error(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    quit_default.fatal_error_v(fmt, ap);
    // NOTREACHED
    va_end(ap);
}

// vim: set ts=8 sw=4 et :