File: option.c

package info (click to toggle)
ftjam 2.5.2-1.1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, lenny, sid, squeeze, stretch, wheezy
  • size: 1,216 kB
  • ctags: 1,237
  • sloc: ansic: 7,226; sh: 2,751; yacc: 443; makefile: 121
file content (131 lines) | stat: -rwxr-xr-x 3,199 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
/*
 * Copyright 1993, 1995 Christopher Seiwald.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

/*
 * option.c - command line option processing
 *
 * {o >o
 *  \<>) "Process command line options as defined in <option.h>.
 *                  Return the number of argv[] elements used up by options,
 *                  or -1 if an invalid option flag was given or an argument
 *                  was supplied for an option that does not require one."
 *
 * 11/04/02 (seiwald) - const-ing for string literals
 */

# include "jam.h"
# include "option.h"

int
getoptions(
    int argc,
    char **argv,
    const char *opts,
    option *optv,
    char** targets )
{
    int i, n;
    int optc = N_OPTS;

    memset( (char *)optv, '\0', sizeof( *optv ) * N_OPTS );

    n = 0;
    for( i = 0; i < argc; i++ )
    {
        char *arg;

        if ( argv[i][0] == '-' )
        {
            if( !optc-- )
            {
                printf( "too many options (%d max)\n", N_OPTS );
                return -1;
            }

            for( arg = &argv[i][1]; *arg; arg++ )
            {
                const char *f;

                for( f = opts; *f; f++ )
                    if( *f == *arg )
                        break;

                if( !*f )
                {
                    printf( "Invalid option: -%c\n", *arg );
                    return -1;
                }

                optv->flag = *f;

                if( f[1] != ':' )
                {
                    optv++->val = "true";
                }
                else if( arg[1] )
                {
                    optv++->val = &arg[1];
                    break;
                }
                else if( ++i < argc )
                {
                    optv++->val = argv[i];
                    break;
                }
                else
                {
                    printf( "option: -%c needs argument\n", *f );
                    return -1;
                }
            }
        }
        else
        {
            /* something like VARNAME=.... is treated as an implicit '-s' flag */
            if ( argv[i][0] != '=' && strchr( argv[i],'=' ) )
            {
                if ( !optc-- )
                {
                    printf( "too many options (%d max)\n", N_OPTS );
                    return -1;
                }

                optv->flag  = 's';
                optv++->val = argv[i];
            }
            else
            {
                if ( n >= N_TARGETS )
                {
                    printf( "too many targets (%d max)\n", N_TARGETS );
                    return -1;
                }
                targets[n++] = argv[i];
            }
        }
    }

    return n;
}

/*
 * Name: getoptval() - find an option given its character
 */

const char *
getoptval(
        option *optv,
        char opt,
        int subopt )
{
        int i;

        for( i = 0; i < N_OPTS; i++, optv++ )
            if( optv->flag == opt && !subopt-- )
                return optv->val;

        return 0;
}