1 /*
2  * Copyright 1993, 1995 Christopher Seiwald.
3  *
4  * This file is part of Jam - see jam.c for Copyright information.
5  */
6 
7 # include "jam.h"
8 # include "option.h"
9 
10 /*
11  * option.c - command line option processing
12  *
13  * {o >o
14  *  \<>) "Process command line options as defined in <option.h>.
15  *        Return the number of argv[] elements used up by options,
16  *        or -1 if an invalid option flag was given or an argument
17  *        was supplied for an option that does not require one."
18  */
19 
getoptions(int argc,char ** argv,const char * opts,bjam_option * optv)20 int getoptions( int argc, char * * argv, const char * opts, bjam_option * optv )
21 {
22     int i;
23     int optc = N_OPTS;
24 
25     memset( (char *)optv, '\0', sizeof( *optv ) * N_OPTS );
26 
27     for ( i = 0; i < argc; ++i )
28     {
29         char *arg;
30 
31         if ( ( argv[ i ][ 0 ] != '-' ) ||
32             ( ( argv[ i ][ 1 ] != '-' ) && !isalpha( argv[ i ][ 1 ] ) ) )
33             continue;
34 
35         if ( !optc-- )
36         {
37             printf( "too many options (%d max)\n", N_OPTS );
38             return -1;
39         }
40 
41         for ( arg = &argv[ i ][ 1 ]; *arg; ++arg )
42         {
43             const char * f;
44 
45             for ( f = opts; *f; ++f )
46                 if ( *f == *arg )
47                     break;
48 
49             if ( !*f )
50             {
51                 printf( "Invalid option: -%c\n", *arg );
52                 return -1;
53             }
54 
55             optv->flag = *f;
56 
57             if ( f[ 1 ] != ':' )
58             {
59                 optv++->val = (char *)"true";
60             }
61             else if ( arg[ 1 ] )
62             {
63                 optv++->val = &arg[1];
64                 break;
65             }
66             else if ( ++i < argc )
67             {
68                 optv++->val = argv[ i ];
69                 break;
70             }
71             else
72             {
73                 printf( "option: -%c needs argument\n", *f );
74                 return -1;
75             }
76         }
77     }
78 
79     return i;
80 }
81 
82 
83 /*
84  * Name: getoptval() - find an option given its character.
85  */
86 
getoptval(bjam_option * optv,char opt,int subopt)87 char * getoptval( bjam_option * optv, char opt, int subopt )
88 {
89     int i;
90     for ( i = 0; i < N_OPTS; ++i, ++optv )
91         if ( ( optv->flag == opt ) && !subopt-- )
92             return optv->val;
93     return 0;
94 }
95