intl/icu/source/tools/toolutil/uoptions.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 2000, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 *******************************************************************************
michael@0 8 * file name: uoptions.c
michael@0 9 * encoding: US-ASCII
michael@0 10 * tab size: 8 (not used)
michael@0 11 * indentation:4
michael@0 12 *
michael@0 13 * created on: 2000apr17
michael@0 14 * created by: Markus W. Scherer
michael@0 15 *
michael@0 16 * This file provides a command line argument parser.
michael@0 17 */
michael@0 18
michael@0 19 #include "unicode/utypes.h"
michael@0 20 #include "cstring.h"
michael@0 21 #include "uoptions.h"
michael@0 22
michael@0 23 U_CAPI int U_EXPORT2
michael@0 24 u_parseArgs(int argc, char* argv[],
michael@0 25 int optionCount, UOption options[]) {
michael@0 26 char *arg;
michael@0 27 int i=1, remaining=1;
michael@0 28 char c, stopOptions=0;
michael@0 29
michael@0 30 while(i<argc) {
michael@0 31 arg=argv[i];
michael@0 32 if(!stopOptions && *arg=='-' && (c=arg[1])!=0) {
michael@0 33 /* process an option */
michael@0 34 UOption *option=NULL;
michael@0 35 arg+=2;
michael@0 36 if(c=='-') {
michael@0 37 /* process a long option */
michael@0 38 if(*arg==0) {
michael@0 39 /* stop processing options after "--" */
michael@0 40 stopOptions=1;
michael@0 41 } else {
michael@0 42 /* search for the option string */
michael@0 43 int j;
michael@0 44 for(j=0; j<optionCount; ++j) {
michael@0 45 if(options[j].longName && uprv_strcmp(arg, options[j].longName)==0) {
michael@0 46 option=options+j;
michael@0 47 break;
michael@0 48 }
michael@0 49 }
michael@0 50 if(option==NULL) {
michael@0 51 /* no option matches */
michael@0 52 return -i;
michael@0 53 }
michael@0 54 option->doesOccur=1;
michael@0 55
michael@0 56 if(option->hasArg!=UOPT_NO_ARG) {
michael@0 57 /* parse the argument for the option, if any */
michael@0 58 if(i+1<argc && !(argv[i+1][0]=='-' && argv[i+1][1]!=0)) {
michael@0 59 /* argument in the next argv[], and there is not an option in there */
michael@0 60 option->value=argv[++i];
michael@0 61 } else if(option->hasArg==UOPT_REQUIRES_ARG) {
michael@0 62 /* there is no argument, but one is required: return with error */
michael@0 63 return -i;
michael@0 64 }
michael@0 65 }
michael@0 66 }
michael@0 67 } else {
michael@0 68 /* process one or more short options */
michael@0 69 do {
michael@0 70 /* search for the option letter */
michael@0 71 int j;
michael@0 72 for(j=0; j<optionCount; ++j) {
michael@0 73 if(c==options[j].shortName) {
michael@0 74 option=options+j;
michael@0 75 break;
michael@0 76 }
michael@0 77 }
michael@0 78 if(option==NULL) {
michael@0 79 /* no option matches */
michael@0 80 return -i;
michael@0 81 }
michael@0 82 option->doesOccur=1;
michael@0 83
michael@0 84 if(option->hasArg!=UOPT_NO_ARG) {
michael@0 85 /* parse the argument for the option, if any */
michael@0 86 if(*arg!=0) {
michael@0 87 /* argument following in the same argv[] */
michael@0 88 option->value=arg;
michael@0 89 /* do not process the rest of this arg as option letters */
michael@0 90 break;
michael@0 91 } else if(i+1<argc && !(argv[i+1][0]=='-' && argv[i+1][1]!=0)) {
michael@0 92 /* argument in the next argv[], and there is not an option in there */
michael@0 93 option->value=argv[++i];
michael@0 94 /* this break is redundant because we know that *arg==0 */
michael@0 95 break;
michael@0 96 } else if(option->hasArg==UOPT_REQUIRES_ARG) {
michael@0 97 /* there is no argument, but one is required: return with error */
michael@0 98 return -i;
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 /* get the next option letter */
michael@0 103 option=NULL;
michael@0 104 c=*arg++;
michael@0 105 } while(c!=0);
michael@0 106 }
michael@0 107
michael@0 108 if(option!=0 && option->optionFn!=0 && option->optionFn(option->context, option)<0) {
michael@0 109 /* the option function was called and returned an error */
michael@0 110 return -i;
michael@0 111 }
michael@0 112
michael@0 113 /* go to next argv[] */
michael@0 114 ++i;
michael@0 115 } else {
michael@0 116 /* move a non-option up in argv[] */
michael@0 117 argv[remaining++]=arg;
michael@0 118 ++i;
michael@0 119 }
michael@0 120 }
michael@0 121 return remaining;
michael@0 122 }

mercurial