; TeX output 1995.09.02:1414HH!/ߍ!1( b> 3 cmmi10=)!", 3 cmsy10*': 3 cmti10GetoptforGNU. Copyright(C)1987,1989F)rpeeSoftwareF)oundation,Inc. Thisprpogramisfreesoftware;youcanredistributeitand/ormodify itunderthetermsoftheGNUGenerpalPublicLicenseaspublishedby theF)rpeeSoftwareF)oundation;eitherversion1,or(atyouroption) anylaterversion. ThisprpogramisdistributedinthehopethatitwilFlbeuseful, butWITHOUTANYW)ARRANTY;withouteventheimpliepdwarrantyof MER\CHANT)ABILITYorFITNESSFORAP)ARTICULARPURPOSE.Sepethe GNUGenerpalPublicLicenseformoredetails. Y)oushouldhaverpeceivedacpopyoftheGNUGeneralPublicLicense alongwiththisprpogram;ifnot,writetotheF)rpeeSoftware F)oundation,Inc.,675MassA\ve,Cambridge,MA02139,USA.='K`y 3 cmr101*HH!/ߍ!1=Thisversionof`getopt'apppearstothecpalFlerlikestandardUnix`getopt' butitbpehavesdi erentlyfortheuser,sinceitalFlowstheuser tointersppersetheoptionswiththeotherarguments. As`getopt'works,itppermutestheelementsof`argv'sothat, whenitisdone,alFltheoptionsprpecedeeverythingelse. vThus alFlapplicpationprogramsareextendedtohandFle exibleargumentorder. SettingtheenvirponmentvariableՉff[{POSIXff[OPTIONff[ORDERdisablespermutation. Thenthebpehavioriscompletelystandard. GNUapplicpationprogramscanuseathirdalternativemodeinwhich theycpandistinguishtherelativeorderofoptionsandotherarguments. v=1-1=IfcpompiledwithGNUC,usethebuilt-inalFlopca=1#ifdefNffH?ffH GNUC/ffHVffH1#definefalloMcaNffH?ffH builtin/ffH'alloca1#else,M=notՉff[fff[ GNUCff[|ff[=1#ifdeffsparc1#includef1#else1,"V 3 cmbx10ctharfalloMca();1#endif1#endif&Z=notՉff[fff[ GNUCff[|ff[=1#ifdeffUSG1#definefbMcop!y(s,d,l)memcpy((d),(s),(l))1#definefindexstrc!hr1#endif1ctharfgeten!v();1ctharfindex();1ctharfmalloMc();1=F)orcpommunicationfrom`getopt'tothecalFler. When`getopt' ndsanoptionthattakesanarpgument, thearpgumentvalueisreturnedhere. A\lso,when`orpdering'isRETURNff[INff[ORDER, epachnon-optionAR\GV-elementisreturnedhere. v=1ctharfoptarg=0;1=IndexinAR\GVofthenextelementtobpescanned. ThisisusepdforcommunicationtoandfromthecalFler andforcpommunicationbetweensuccessivecalFlsto`getopt'.2HH!/ߍ! Onentryto`getopt',zerpomeansthisisthe rstcalFl;initialize. When`getopt'rpeturnsEOF,thisistheindexofthe rstofthe non-optionelementsthatthecpalFlershoulditselfscan. Otherwise,`optind'cpommunicatesfromonecalFltothenext howmuchofAR\GVhasbpeenscannedsofar. v=1inttfoptind=0;1=Thenextchartobpescannedintheoption-element inwhichthelastoptioncharpacterwereturnedwasfound. ThisalFlowsustopickupthescpanwherewelefto . Ifthisiszerpo,oranulFlstring,itmeansresumethescan byadvancingtothenextAR\GV-element. v=1staticfctharnextc!har;1=CalFlersstorpezeroheretoinhibittheerrormessage forunrpecognizedoptions. v=1inttfopterr=1;1=DescribpehowtodealwithoptionsthatfolFlownon-optionAR\GV-elements. IfthecpalFlerdidnotspecifyanything, thedefaultisREQUIREff[ORDERiftheenvirponmentvariable0ff[POSIXff[OPTIONff[ORDERisde nepd,PERMUTEotherwise. REQUIREff[ORDERmepansdons'trecognizethemasoptions. Stopoptionprpocessingwhenthe rstnon-optionissepen. ThisiswhatUnixdopes. PERMUTEisthedefault. vWeppermutethecontentsofAR\GVaswescan, sothateventualFlyalltheoptionsarpeattheend. vThisallowsoptions tobpegiveninanyorder,evenwithprogramsthatwerenotwrittento exppectthis. RETURNff[INff[ORDERisanoptionavailabletoprpogramsthatwerpewritten toexppectoptionsandotherAR\GV-elementsinanyorderandthatcareabout theorpderingofthetwo. vWedescribeeachnon-optionAR\GV-element asifitwerpetheargumentofanoptionwithcharactercodeone. Using`-'asthe rstcharpacterofthelistofoptioncharacters rpequeststhismopdeofoperation.3HH!/ߍ! Thesppecialargument`{'forcesanendofoption-scanningregardFless ofthevalueof`orpdering'. vInthecaseofRETURNff[INff[ORDER,only `{'cpancause`getopt'toreturnEOFwith`optind'!=AR\GC.=1staticfentumfREQUIRE/ffH'ORDER,PERMUTE,RETURN/ffHIN/ffHORDERgordering;1=Describpethelong-namedoptionsrequestedbytheapplication.0ff[GETOPTff[LONGff[OPTIONSisavepctorof`structoptions'terminatedbyan elementcpontaininganamewhichiszero. The eld`hasff[arpg'is1iftheoptiontakesanargument, 2ifittakesanoptionalarpgument. v=1structfoption1f ectharfname; einttfhas/ffH'arg; einttf ag; einttfvdDal;1g;1structfoption/ffH'getopt/ffHlong/ffHoptions;1inttNffHgetopt/ffH'long/ffHonlyf=0;1#iff01=Thisisanuglykludge. vPrpogramsshouldusetheoptff[indexarpgument togetoptff[longinstepad. v=1=Nameoflong-namepdoptionactualFlyfound. v=1ctharf/ffH'getopt/ffHoption/ffHname;1#endif1=IndexinՉff[{GETOPTff[LONGff[OPTIONSofthelong-namepdoptionactualFlyfound. Onlyvalidwhenalong-namepdoptionwasfound. v=1inttfoption/ffH'index;47HH!/ߍ!1=HandFleppermutationofarguments. v= 1=DescribpethepartofAR\GVthatcontainsnon-optionsthathave bpeenskipped. v` rstff[nonopt'istheindexinAR\GVofthe rstofthem; `lastff[nonopt'istheindexafterthelastofthem. v=1staticfintt rst/ffH'nonopt;1staticfinttlast/ffH'nonopt;1=ExchangetwoadjacpentsubsequencesofAR\GV. Onesubsepquenceiselements[ rstff[nonopt,lastff[nonopt)whichcpontainsalFlthenon-optionsthathavebeenskippedsofar. Theotheriselements[lastff[nonopt,optind),whichcpontainsalFltheoptionsprpocessedsincethosenon-optionswereskipped. ` rstff[nonopt'and`lastff[nonopt'arperelocatedsothattheydescribethenewindicpesofthenon-optionsinAR\GVaftertheyaremoved. v=1staticfvtoid1exc!hangef(argv)x3ctharfargv;1f einttfnonopts/ffH'size=f(last/ffH'nonopt- rst/ffHnonopt)sizeof(cthar); ectharftemp=(char)alloMca(nonopts/ffH'size); e=Interpchangethetwoblocksofdatainargv. v= ebMcop!yf(&argv[ rst/ffH'nonopt],temp,nonopts/ffHsize); ebMcop!yf(&argv[last/ffH'nonopt],&argv[ rst/ffHnonopt],.^(optindf-last/ffH'nonopt)sizeof(cthar)); ebMcop!yf(temp,&argv[ rst/ffH'nonopt+optind-last/ffHnonopt],.^nonopts/ffH'size); e=Uppdaterecordsfortheslotsthenon-optionsnowoccupy. v= e rst/ffH'nonoptf+=(optind-last/ffHnonopt); elast/ffH'nonoptf=optind;1g5ɠHH!/ߍ!1=ScpanelementsofAR\GV(whoselengthisARGC)foroptioncharpacters giveninOPTSTRING. IfanelementofAR\GVstartswith'-',andisnotexactly"-"or"{", thenitisanoptionelement. vThecharpactersofthiselement (asidefrpomtheinitial'-')areoptioncharacters. vIf`getopt' iscpalFledrepeatedFly,itreturnssuccessivelyeachoftheoptioncharacters frpomeachoftheoptionelements. If`getopt' ndsanotheroptioncharpacter,itreturnsthatcharacter, uppdating`optind'and`nextchar'sothatthenextcalFlto`getopt'can rpesumethescanwiththefolFlowingoptioncharacterorAR\GV-element. Iftherpearenomoreoptioncharacters,`getopt'returns`EOF'. Then`optind'istheindexinAR\GVofthe rstARGV-element thatisnotanoption. v(TheAR\GV-elementshavebpeenpermuted sothatthosethatarpenotoptionsnowcomelast.) OPTSTRINGisastringcpontainingthelegitimateoptioncharacters. IfanoptioncharpacterisseenthatisnotlistedinOPTSTRING, rpeturn'?' vafterprintinganerrormessage. vIfyouset`opterr'to zerpo,theerrormessageissuppressedbutwestilFlreturn'?'. IfacharinOPTSTRINGisfolFlowepdbyacolon,thatmeansitwantsanarg, sothefolFlowingtextinthesameAR\GV-element,orthetextofthefollowing AR\GV-element,isrpeturnedin`optarpg'. vTwocolonsmeananoptionthat wantsanoptionalarpg;ifthereistextinthecurrentAR\GV-element, itisrpeturnedin`optarpg',otherwise`optarg'issettozero. IfOPTSTRINGstartswith`-',itrpequestsadi erpentmethodofhandFlingthe non-optionAR\GV-elements. vSepethecommentsaboutRETURNff[INff[ORDER,above. Lpong-namedoptionsbpeginwith`+'instepadof`-'. Theirnamesmaybpeabbreviatedaslongastheabbreviationisunique orisanexactmatchforsomede nepdoption. vIftheyhavean arpgument,itfolFlowstheoptionnameinthesameAR\GV-element,separated frpomtheoptionnamebya`=',orelsetheinnextAR\GV-element. `getopt'rpeturns0whenit ndsalong-namedoption. v=1intt1getoptf(argc,argv,optstring)x3inttfargc;x3ctharfargv;x3ctharfoptstring;1f eoptargf=0;6(HH!/ߍ! e=Initializetheinternaldatawhenthe rstcpalFlismade. x3StartprpocessingoptionswithAR\GV-element1(sinceAR\GV-element0x3istheprpogramname);thesepquenceofprpeviouslyskippedx3non-optionAR\GV-elementsisempty. v= eiff(optind==0)f rst/ffH'nonoptf=last/ffHnonopt=optind=1;nextc!harf=0;=DeterminehowtohandFletheorpderingofoptionsandnonoptions. v=iff(optstring[0]=='-')(orderingf=RETURN/ffH'IN/ffHORDER;elsefif(geten!v("ffs#POSIXffsOPTIONffsORDER")f6=0)(orderingf=REQUIRE/ffH'ORDER;else(orderingf=PERMUTE;g eiff(nextc!har==0jjnextchar==0)fiff(ordering==PERMUTE)(f35=IfwehavejustprpocessedsomeoptionsfolFlowingsomenon-options,DEexchangethemsothattheoptionscpome rst. v=35iff( rst/ffH'nonopt6=last/ffHnonopt&&last/ffHnonopt6=optind)>iexc!hangef(argv);35elsefif(last/ffH'nonopt6=optind)>i rst/ffH'nonoptf=optind;35=Nowskipanyadditionalnon-optionsDEandextendtherpangeofnon-optionspreviouslyskipped. v=35whilef(optindioptind++;35last/ffH'nonoptf=optind;(g=SppecialAR\GV-element`{'meansprematureendofoptions.73͠HH!/ߍ!.^SkipitlikeanulFloption, .^thenexchangewithprpeviousnon-optionsasifitwereanoption,.^thenskipeverythingelselikeanon-option. v=iff(optind6=argc&&!strcmp(argv[optind],"--"))(f35optind++;35iff( rst/ffH'nonopt6=last/ffHnonopt&&last/ffHnonopt6=optind)>iexc!hangef(argv);35elsefif( rst/ffH'nonopt==last/ffHnonopt)>i rst/ffH'nonoptf=optind;35last/ffH'nonoptf=argc;35optindf=argc;(g=IfwehavedonealFltheAR\GV-elements,stopthescpan.^andbpackoveranynon-optionsthatweskippedandpermuted. v=iff(optind==argc)(f35=Setthenext-arpg-indextopointatthenon-optionsDEthatweprpeviouslyskipped,sothecalFlerwilldigestthem. v=35iff( rst/ffH'nonopt6=last/ffHnonopt)>ioptindf= rst/ffH'nonopt;35returnfEOF;(g=Ifwehavecpometoanon-optionanddidnotpermuteit,.^eitherstopthescpanordescribeittothecalFlerandpassitby. v=iff((argv[optind][0]6='-'jjargv[optind][1]==0)35&&f(/ffH'getopt/ffHlong/ffHoptions==0Ijjfargv[optind][0]6='+'jjargv[optind][1]==0))(f35iff(ordering==REQUIRE/ffH'ORDER)>ireturnfEOF;35optargf=argv[optind++];1#iff04dffH7\getopt/ffH'option/ffHnamef=0;1#endif35returnf1;(g=Wehavefoundanotheroption-AR\GV-element..^Startdepcodingitscharacters. v=8 ;ܠHH!/ߍ! nextc!harf=argv[optind]+1;g eiff(/ffH'getopt/ffHlong/ffHoptions6=0&&f(argv[optind][0]=='+'35jjf(/ffH'getopt/ffHlong/ffHonly&&argv[optind][0]=='-')))fstructfoptionp;ctharfs=nextc!har;inttfexact=0;inttfam!big=0;structfoptionpfound=0;inttfindfound;whilef(s&&s6='=')s++;=T)estalFloptionsforeitherexactmatchorabbrpeviatedmatches. v=forf(p=NffHgetopt/ffH'long/ffHoptions,foption/ffHindex=0;p!name;9Qp++,foption/ffH'index++)(iff(!strncmp(p!name,nextc!har,s-nextchar))35f>iiff(s-nextc!har==strlen(p!name))IfT=Exactmatchfound. v=Tpfoundf=p;Tindfoundf=option/ffH'index;Texactf=1;Tbreak;Ig>ielsefif(pfound==0)IfT=Firstnonexactmatchfound. v=Tpfoundf=p;Tindfoundf=option/ffH'index;Ig>ielseI=Sepcondnonexactmatchfound. v=Iam!bigf=1;35giff(am!big&&!exact)(f35fprin!tff(stderr,"%s: Toption`%s'isambiguousnn",eargv[0],fargv[optind]);35nextc!harf+=strlen(nextchar);9 CǠHH!/ߍ!35returnf'7w?'; (giff(pfound6=0)(f35option/ffH'indexf=indfound;35optind++;35iff(s)>ifIiff(pfound!has/ffH'arg>0)Toptargf=s+1;IelseTf_fprin!tff(stderr,o"%s: Toption`%c%s'doesn'tallowanargumentnn",oargv[0],fargv[optind-1][0],pfound!name);_nextc!harf+=strlen(nextchar);_returnf'7w?';Tg>ig35elsefif(pfound!has/ffH'arg)>ifIiff(optindig1#iff04dffH7\getopt/ffH'option/ffHnamef=pfound!name;1#endif35nextc!harf+=strlen(nextchar);35iff(pfound! ag)>i(pfound! ag)f=pfound!vdDal;35returnf0;(g=Cans't nditasalongoption. vIfthisisgetoptff[longff[only,.^andtheoptionstartswith'-'andisavalidshort.^option,theninterprpetitasashortoption. vOtherwiseit's.^anerrpor. v=iff(/ffH'getopt/ffHlong/ffHonly==0jjargv[optind][0]=='+'jj35indexf(optstring,nextc!har)==0)(f10 JeHH!/ߍ!35iff(opterr6=0) >ifprin!tff(stderr,"%s: Tunrecognizedoption`%c%s'nn",pargv[0],fargv[optind][0],nextc!har);35nextc!harf+=strlen(nextchar);35returnf'7w?';(gg e=LpookatandhandFlethenextoption-character. v= efctharfc=nextc!har++;ctharftemp=index(optstring,c);=Incrpement`optind'whenwestarttoprocessitslastcharacter. v=iff(nextc!har==0)optind++;iff(temp==0jjc==':')f(iff(opterr6=0)35f>iiff(c<040jjc0177)Ifprin!tff(stderr,"%s: Tunrecognizedoption,charactercode10%onn",{argv[0],fc);>ielseIfprin!tff(stderr,"%s: Tunrecognizedoption`-%c'nn",{argv[0],fc);35g(returnf'7w?';giff(temp[1]==':')f(iff(temp[2]==':')35f>i=ThisisanoptionthatacpceptsanarpgumentoptionalFly. v=>iiff(nextc!har6=0)IfToptargf=nextc!har;Toptind++;Ig>ielseIoptargf=0;>inextc!harf=0;35g(else11 Q;HH!/ߍ!35f >i=Thisisanoptionthatrpequiresanarpgument. v=>iiff(nextc!har6=0)IfToptargf=nextc!har;T=IfweendthisAR\GV-elementbytakingtherpestasanarg,ewemustadvancpetothenextelementnow. v=Toptind++;Ig>ielsefif(optind==argc)IfTiff(opterr6=0)_fprin!tff(stderr,"%s: Toption`-%c'requiresanargumentnn",oargv[0],fc);Tcf='7w?';Ig>ielseI=Wealrpeadyincremented`optind'once;Z+kincrpementitagainwhentakingnextAR\GV-eltasargument. v=Ioptargf=argv[optind++];>inextc!harf=0;35ggreturnfc; eg1g12 VĠHH!/ߍ!1#ifdeffTEST 1=Compilewith-DTESTtomakeanexepcutableforuseintesting theabpovede nitionof`getopt'. v=1intt1mainf(argc,argv)x3inttfargc;x3ctharfargv;1f ectharfc; einttfdigit/ffH'optind=0; ewhilef(1)finttfthis/ffH'option/ffHoptind=optind;iff((c=getopt(argc,argv,"abc:d:0123456789"))==EOF)(break;switcthf(c)(f(casef'0':(casef'1':(casef'2':(casef'3':(casef'4':(casef'5':(casef'6':(casef'7':(casef'8':(casef'9':35iff(digit/ffH'optind6=0&&digit/ffHoptind6=this/ffHoption/ffHoptind)>iprin!tff("digitsoccurintwodifferentargv-elements.nn");35digit/ffH'optindf=this/ffHoption/ffHoptind;35prin!tff("option%cnn",c);35break;(casef'a':35prin!tff("optionann");35break;(casef'b':35prin!tff("optionbnn");35break;(casef'c':35prin!tff("optioncwithvalue`%s'nn",optarg);13ZRHH!/ߍ!35break; (casef'7w?':35break;(default:35prin!tff("?? Tgetoptreturnedcharactercode0%o??nn",c);(gg eiff(optind 3 cmmi10'K`y 3 cmr10bC