Line | |
---|
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 | |
---|
20 | int |
---|
21 | getoptions( |
---|
22 | int argc, |
---|
23 | char **argv, |
---|
24 | char *opts, |
---|
25 | option *optv ) |
---|
26 | { |
---|
27 | int i; |
---|
28 | int optc = N_OPTS; |
---|
29 | |
---|
30 | memset( (char *)optv, '\0', sizeof( *optv ) * N_OPTS ); |
---|
31 | |
---|
32 | for( i = 0; i < argc; i++ ) |
---|
33 | { |
---|
34 | char *arg; |
---|
35 | |
---|
36 | if( argv[i][0] != '-' || ( argv[i][1] != '-' && !isalpha( argv[i][1] ) ) ) |
---|
37 | continue; |
---|
38 | |
---|
39 | if( !optc-- ) |
---|
40 | { |
---|
41 | printf( "too many options (%d max)\n", N_OPTS ); |
---|
42 | return -1; |
---|
43 | } |
---|
44 | |
---|
45 | for( arg = &argv[i][1]; *arg; arg++ ) |
---|
46 | { |
---|
47 | char *f; |
---|
48 | |
---|
49 | for( f = opts; *f; f++ ) |
---|
50 | if( *f == *arg ) |
---|
51 | break; |
---|
52 | |
---|
53 | if( !*f ) |
---|
54 | { |
---|
55 | printf( "Invalid option: -%c\n", *arg ); |
---|
56 | return -1; |
---|
57 | } |
---|
58 | |
---|
59 | optv->flag = *f; |
---|
60 | |
---|
61 | if( f[1] != ':' ) |
---|
62 | { |
---|
63 | optv++->val = "true"; |
---|
64 | } |
---|
65 | else if( arg[1] ) |
---|
66 | { |
---|
67 | optv++->val = &arg[1]; |
---|
68 | break; |
---|
69 | } |
---|
70 | else if( ++i < argc ) |
---|
71 | { |
---|
72 | optv++->val = argv[i]; |
---|
73 | break; |
---|
74 | } |
---|
75 | else |
---|
76 | { |
---|
77 | printf( "option: -%c needs argument\n", *f ); |
---|
78 | return -1; |
---|
79 | } |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | return i; |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * Name: getoptval() - find an option given its character |
---|
88 | */ |
---|
89 | |
---|
90 | char * |
---|
91 | getoptval( |
---|
92 | option *optv, |
---|
93 | char opt, |
---|
94 | int subopt ) |
---|
95 | { |
---|
96 | int i; |
---|
97 | |
---|
98 | for( i = 0; i < N_OPTS; i++, optv++ ) |
---|
99 | if( optv->flag == opt && !subopt-- ) |
---|
100 | return optv->val; |
---|
101 | |
---|
102 | return 0; |
---|
103 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.