1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/tools/toolutil/uoptions.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* 1.5 +******************************************************************************* 1.6 +* 1.7 +* Copyright (C) 2000, International Business Machines 1.8 +* Corporation and others. All Rights Reserved. 1.9 +* 1.10 +******************************************************************************* 1.11 +* file name: uoptions.c 1.12 +* encoding: US-ASCII 1.13 +* tab size: 8 (not used) 1.14 +* indentation:4 1.15 +* 1.16 +* created on: 2000apr17 1.17 +* created by: Markus W. Scherer 1.18 +* 1.19 +* This file provides a command line argument parser. 1.20 +*/ 1.21 + 1.22 +#include "unicode/utypes.h" 1.23 +#include "cstring.h" 1.24 +#include "uoptions.h" 1.25 + 1.26 +U_CAPI int U_EXPORT2 1.27 +u_parseArgs(int argc, char* argv[], 1.28 + int optionCount, UOption options[]) { 1.29 + char *arg; 1.30 + int i=1, remaining=1; 1.31 + char c, stopOptions=0; 1.32 + 1.33 + while(i<argc) { 1.34 + arg=argv[i]; 1.35 + if(!stopOptions && *arg=='-' && (c=arg[1])!=0) { 1.36 + /* process an option */ 1.37 + UOption *option=NULL; 1.38 + arg+=2; 1.39 + if(c=='-') { 1.40 + /* process a long option */ 1.41 + if(*arg==0) { 1.42 + /* stop processing options after "--" */ 1.43 + stopOptions=1; 1.44 + } else { 1.45 + /* search for the option string */ 1.46 + int j; 1.47 + for(j=0; j<optionCount; ++j) { 1.48 + if(options[j].longName && uprv_strcmp(arg, options[j].longName)==0) { 1.49 + option=options+j; 1.50 + break; 1.51 + } 1.52 + } 1.53 + if(option==NULL) { 1.54 + /* no option matches */ 1.55 + return -i; 1.56 + } 1.57 + option->doesOccur=1; 1.58 + 1.59 + if(option->hasArg!=UOPT_NO_ARG) { 1.60 + /* parse the argument for the option, if any */ 1.61 + if(i+1<argc && !(argv[i+1][0]=='-' && argv[i+1][1]!=0)) { 1.62 + /* argument in the next argv[], and there is not an option in there */ 1.63 + option->value=argv[++i]; 1.64 + } else if(option->hasArg==UOPT_REQUIRES_ARG) { 1.65 + /* there is no argument, but one is required: return with error */ 1.66 + return -i; 1.67 + } 1.68 + } 1.69 + } 1.70 + } else { 1.71 + /* process one or more short options */ 1.72 + do { 1.73 + /* search for the option letter */ 1.74 + int j; 1.75 + for(j=0; j<optionCount; ++j) { 1.76 + if(c==options[j].shortName) { 1.77 + option=options+j; 1.78 + break; 1.79 + } 1.80 + } 1.81 + if(option==NULL) { 1.82 + /* no option matches */ 1.83 + return -i; 1.84 + } 1.85 + option->doesOccur=1; 1.86 + 1.87 + if(option->hasArg!=UOPT_NO_ARG) { 1.88 + /* parse the argument for the option, if any */ 1.89 + if(*arg!=0) { 1.90 + /* argument following in the same argv[] */ 1.91 + option->value=arg; 1.92 + /* do not process the rest of this arg as option letters */ 1.93 + break; 1.94 + } else if(i+1<argc && !(argv[i+1][0]=='-' && argv[i+1][1]!=0)) { 1.95 + /* argument in the next argv[], and there is not an option in there */ 1.96 + option->value=argv[++i]; 1.97 + /* this break is redundant because we know that *arg==0 */ 1.98 + break; 1.99 + } else if(option->hasArg==UOPT_REQUIRES_ARG) { 1.100 + /* there is no argument, but one is required: return with error */ 1.101 + return -i; 1.102 + } 1.103 + } 1.104 + 1.105 + /* get the next option letter */ 1.106 + option=NULL; 1.107 + c=*arg++; 1.108 + } while(c!=0); 1.109 + } 1.110 + 1.111 + if(option!=0 && option->optionFn!=0 && option->optionFn(option->context, option)<0) { 1.112 + /* the option function was called and returned an error */ 1.113 + return -i; 1.114 + } 1.115 + 1.116 + /* go to next argv[] */ 1.117 + ++i; 1.118 + } else { 1.119 + /* move a non-option up in argv[] */ 1.120 + argv[remaining++]=arg; 1.121 + ++i; 1.122 + } 1.123 + } 1.124 + return remaining; 1.125 +}