![]() |
The Machine Perception Toolbox |
|
00001 /* 00002 * Copyright 1993, 1995 Christopher Seiwald. 00003 * 00004 * This file is part of Jam - see jam.c for Copyright information. 00005 */ 00006 00007 # include "jam.h" 00008 # include "option.h" 00009 00010 /* 00011 * option.c - command line option processing 00012 * 00013 * {o >o 00014 * <>) "Process command line options as defined in <option.h>. 00015 * Return the number of argv[] elements used up by options, 00016 * or -1 if an invalid option flag was given or an argument 00017 * was supplied for an option that does not require one." 00018 */ 00019 00020 int 00021 getoptions( 00022 int argc, 00023 char **argv, 00024 char *opts, 00025 option *optv ) 00026 { 00027 int i; 00028 int optc = N_OPTS; 00029 00030 memset( (char *)optv, '\0', sizeof( *optv ) * N_OPTS ); 00031 00032 for( i = 0; i < argc; i++ ) 00033 { 00034 char *arg; 00035 00036 if( argv[i][0] != '-' || ( argv[i][1] != '-' && !isalpha( argv[i][1] ) ) ) 00037 break; 00038 00039 if( !optc-- ) 00040 { 00041 printf( "too many options (%d max)\n", N_OPTS ); 00042 return -1; 00043 } 00044 00045 for( arg = &argv[i][1]; *arg; arg++ ) 00046 { 00047 char *f; 00048 00049 for( f = opts; *f; f++ ) 00050 if( *f == *arg ) 00051 break; 00052 00053 if( !*f ) 00054 { 00055 printf( "Invalid option: -%c\n", *arg ); 00056 return -1; 00057 } 00058 00059 optv->flag = *f; 00060 00061 if( f[1] != ':' ) 00062 { 00063 optv++->val = "true"; 00064 } 00065 else if( arg[1] ) 00066 { 00067 optv++->val = &arg[1]; 00068 break; 00069 } 00070 else if( ++i < argc ) 00071 { 00072 optv++->val = argv[i]; 00073 break; 00074 } 00075 else 00076 { 00077 printf( "option: -%c needs argument\n", *f ); 00078 return -1; 00079 } 00080 } 00081 } 00082 00083 return i; 00084 } 00085 00086 /* 00087 * Name: getoptval() - find an option given its character 00088 */ 00089 00090 char * 00091 getoptval( 00092 option *optv, 00093 char opt, 00094 int subopt ) 00095 { 00096 int i; 00097 00098 for( i = 0; i < N_OPTS; i++, optv++ ) 00099 if( optv->flag == opt && !subopt-- ) 00100 return optv->val; 00101 00102 return 0; 00103 }