mkjambase strips comments at the end of line and spaces before such comments
[k8jam.git] / option.c
blob4360531d64d502b6eab3b4660129c2243a072203
1 /*
2 * Copyright 1993, 1995 Christopher Seiwald.
4 * This file is part of Jam - see jam.c for Copyright information.
5 */
7 /*
8 * option.c - command line option processing
10 * {o >o
11 * \<>) "Process command line options as defined in <option.h>.
12 * Return the number of argv[] elements used up by options,
13 * or -1 if an invalid option flag was given or an argument
14 * was supplied for an option that does not require one."
16 * 11/04/02 (seiwald) - const-ing for string literals
19 # include "jam.h"
20 # include "option.h"
22 int
23 getoptions(
24 int argc,
25 char **argv,
26 const char *opts,
27 option *optv,
28 char** targets )
30 int i, n;
31 int optc = N_OPTS;
33 memset( (char *)optv, '\0', sizeof( *optv ) * N_OPTS );
35 n = 0;
36 for( i = 0; i < argc; i++ )
38 char *arg;
40 if ( argv[i][0] == '-' )
42 if( !optc-- )
44 printf( "too many options (%d max)\n", N_OPTS );
45 return -1;
48 for( arg = &argv[i][1]; *arg; arg++ )
50 const char *f;
52 for( f = opts; *f; f++ )
53 if( *f == *arg )
54 break;
56 if( !*f )
58 printf( "Invalid option: -%c\n", *arg );
59 return -1;
62 optv->flag = *f;
64 if( f[1] != ':' )
66 optv++->val = "true";
68 else if( arg[1] )
70 optv++->val = &arg[1];
71 break;
73 else if( ++i < argc )
75 optv++->val = argv[i];
76 break;
78 else
80 printf( "option: -%c needs argument\n", *f );
81 return -1;
85 else
87 /* something like VARNAME=.... is treated as an implicit '-s' flag */
88 if ( argv[i][0] != '=' && strchr( argv[i],'=' ) )
90 if ( !optc-- )
92 printf( "too many options (%d max)\n", N_OPTS );
93 return -1;
96 optv->flag = 's';
97 optv++->val = argv[i];
99 else
101 if ( n >= N_TARGETS )
103 printf( "too many targets (%d max)\n", N_TARGETS );
104 return -1;
106 targets[n++] = argv[i];
111 return n;
115 * Name: getoptval() - find an option given its character
118 const char *
119 getoptval(
120 option *optv,
121 char opt,
122 int subopt )
124 int i;
126 for( i = 0; i < N_OPTS; i++, optv++ )
127 if( optv->flag == opt && !subopt-- )
128 return optv->val;
130 return 0;