argtable3.c 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955
  1. /*******************************************************************************
  2. * This file is part of the argtable3 library.
  3. *
  4. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  5. * <sheitmann@users.sourceforge.net>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. ******************************************************************************/
  30. #include "argtable3.h"
  31. #pragma GCC diagnostic ignored "-Wclobbered"
  32. /*******************************************************************************
  33. * This file is part of the argtable3 library.
  34. *
  35. * Copyright (C) 2013 Tom G. Huang
  36. * <tomghuang@gmail.com>
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions are met:
  41. * * Redistributions of source code must retain the above copyright
  42. * notice, this list of conditions and the following disclaimer.
  43. * * Redistributions in binary form must reproduce the above copyright
  44. * notice, this list of conditions and the following disclaimer in the
  45. * documentation and/or other materials provided with the distribution.
  46. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  47. * may be used to endorse or promote products derived from this software
  48. * without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  51. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  53. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  54. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  55. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  56. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  57. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  59. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. ******************************************************************************/
  61. #ifndef ARG_UTILS_H
  62. #define ARG_UTILS_H
  63. #define ARG_ENABLE_TRACE 0
  64. #define ARG_ENABLE_LOG 1
  65. #ifdef __cplusplus
  66. extern "C" {
  67. #endif
  68. enum
  69. {
  70. EMINCOUNT = 1,
  71. EMAXCOUNT,
  72. EBADINT,
  73. EOVERFLOW,
  74. EBADDOUBLE,
  75. EBADDATE,
  76. EREGNOMATCH
  77. };
  78. #if defined(_MSC_VER)
  79. #define ARG_TRACE(x) \
  80. __pragma(warning(push)) \
  81. __pragma(warning(disable:4127)) \
  82. do { if (ARG_ENABLE_TRACE) dbg_printf x; } while (0) \
  83. __pragma(warning(pop))
  84. #define ARG_LOG(x) \
  85. __pragma(warning(push)) \
  86. __pragma(warning(disable:4127)) \
  87. do { if (ARG_ENABLE_LOG) dbg_printf x; } while (0) \
  88. __pragma(warning(pop))
  89. #else
  90. #define ARG_TRACE(x) \
  91. do { if (ARG_ENABLE_TRACE) dbg_printf x; } while (0)
  92. #define ARG_LOG(x) \
  93. do { if (ARG_ENABLE_LOG) dbg_printf x; } while (0)
  94. #endif
  95. extern void dbg_printf(const char *fmt, ...);
  96. #ifdef __cplusplus
  97. }
  98. #endif
  99. #endif
  100. /*******************************************************************************
  101. * This file is part of the argtable3 library.
  102. *
  103. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  104. * <sheitmann@users.sourceforge.net>
  105. * All rights reserved.
  106. *
  107. * Redistribution and use in source and binary forms, with or without
  108. * modification, are permitted provided that the following conditions are met:
  109. * * Redistributions of source code must retain the above copyright
  110. * notice, this list of conditions and the following disclaimer.
  111. * * Redistributions in binary form must reproduce the above copyright
  112. * notice, this list of conditions and the following disclaimer in the
  113. * documentation and/or other materials provided with the distribution.
  114. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  115. * may be used to endorse or promote products derived from this software
  116. * without specific prior written permission.
  117. *
  118. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  119. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  120. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  121. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  122. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  123. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  124. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  125. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  126. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  127. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  128. ******************************************************************************/
  129. #include <stdarg.h>
  130. #include <stdio.h>
  131. void dbg_printf(const char *fmt, ...)
  132. {
  133. va_list args;
  134. va_start(args, fmt);
  135. vfprintf(stderr, fmt, args);
  136. va_end(args);
  137. }
  138. /* $Id: getopt.h,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $ */
  139. /* $OpenBSD: getopt.h,v 1.1 2002/12/03 20:24:29 millert Exp $ */
  140. /* $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $ */
  141. /*-
  142. * Copyright (c) 2000 The NetBSD Foundation, Inc.
  143. * All rights reserved.
  144. *
  145. * This code is derived from software contributed to The NetBSD Foundation
  146. * by Dieter Baron and Thomas Klausner.
  147. *
  148. * Redistribution and use in source and binary forms, with or without
  149. * modification, are permitted provided that the following conditions
  150. * are met:
  151. * 1. Redistributions of source code must retain the above copyright
  152. * notice, this list of conditions and the following disclaimer.
  153. * 2. Redistributions in binary form must reproduce the above copyright
  154. * notice, this list of conditions and the following disclaimer in the
  155. * documentation and/or other materials provided with the distribution.
  156. * 3. All advertising materials mentioning features or use of this software
  157. * must display the following acknowledgement:
  158. * This product includes software developed by the NetBSD
  159. * Foundation, Inc. and its contributors.
  160. * 4. Neither the name of The NetBSD Foundation nor the names of its
  161. * contributors may be used to endorse or promote products derived
  162. * from this software without specific prior written permission.
  163. *
  164. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  165. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  166. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  167. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  168. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  169. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  170. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  171. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  172. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  173. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  174. * POSSIBILITY OF SUCH DAMAGE.
  175. */
  176. #ifndef _GETOPT_H_
  177. #define _GETOPT_H_
  178. #if 0
  179. #include <sys/cdefs.h>
  180. #endif
  181. /*
  182. * GNU-like getopt_long() and 4.4BSD getsubopt()/optreset extensions
  183. */
  184. #define no_argument 0
  185. #define required_argument 1
  186. #define optional_argument 2
  187. struct option {
  188. /* name of long option */
  189. const char *name;
  190. /*
  191. * one of no_argument, required_argument, and optional_argument:
  192. * whether option takes an argument
  193. */
  194. int has_arg;
  195. /* if not NULL, set *flag to val when option found */
  196. int *flag;
  197. /* if flag not NULL, value to set *flag to; else return value */
  198. int val;
  199. };
  200. #ifdef __cplusplus
  201. extern "C" {
  202. #endif
  203. int getopt_long(int, char * const *, const char *,
  204. const struct option *, int *);
  205. int getopt_long_only(int, char * const *, const char *,
  206. const struct option *, int *);
  207. #ifndef _GETOPT_DEFINED
  208. #define _GETOPT_DEFINED
  209. int getopt(int, char * const *, const char *);
  210. int getsubopt(char **, char * const *, char **);
  211. extern char *optarg; /* getopt(3) external variables */
  212. extern int opterr;
  213. extern int optind;
  214. extern int optopt;
  215. extern int optreset;
  216. extern char *suboptarg; /* getsubopt(3) external variable */
  217. #endif /* _GETOPT_DEFINED */
  218. #ifdef __cplusplus
  219. }
  220. #endif
  221. #endif /* !_GETOPT_H_ */
  222. /* $Id: getopt_long.c,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $ */
  223. /* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $ */
  224. /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
  225. /*
  226. * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
  227. *
  228. * Permission to use, copy, modify, and distribute this software for any
  229. * purpose with or without fee is hereby granted, provided that the above
  230. * copyright notice and this permission notice appear in all copies.
  231. *
  232. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  233. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  234. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  235. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  236. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  237. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  238. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  239. *
  240. * Sponsored in part by the Defense Advanced Research Projects
  241. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  242. * Materiel Command, USAF, under agreement number F39502-99-1-0512.
  243. */
  244. #ifndef lint
  245. //static const char rcsid[]="$Id: getopt_long.c,v 1.1 2009/10/16 19:50:28 rodney Exp rodney $";
  246. #endif /* lint */
  247. /*-
  248. * Copyright (c) 2000 The NetBSD Foundation, Inc.
  249. * All rights reserved.
  250. *
  251. * This code is derived from software contributed to The NetBSD Foundation
  252. * by Dieter Baron and Thomas Klausner.
  253. *
  254. * Redistribution and use in source and binary forms, with or without
  255. * modification, are permitted provided that the following conditions
  256. * are met:
  257. * 1. Redistributions of source code must retain the above copyright
  258. * notice, this list of conditions and the following disclaimer.
  259. * 2. Redistributions in binary form must reproduce the above copyright
  260. * notice, this list of conditions and the following disclaimer in the
  261. * documentation and/or other materials provided with the distribution.
  262. *
  263. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  264. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  265. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  266. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  267. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  268. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  269. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  270. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  271. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  272. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  273. * POSSIBILITY OF SUCH DAMAGE.
  274. */
  275. #include <errno.h>
  276. #include <stdlib.h>
  277. #include <string.h>
  278. // Define this to replace system getopt
  279. //
  280. //#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
  281. #ifdef REPLACE_GETOPT
  282. int opterr = 1; /* if error message should be printed */
  283. int optind = 1; /* index into parent argv vector */
  284. int optopt = '?'; /* character checked for validity */
  285. int optreset; /* reset getopt */
  286. char *optarg; /* argument associated with option */
  287. #define PRINT_ERROR ((opterr) && (*options != ':'))
  288. #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
  289. #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
  290. #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
  291. /* return values */
  292. #define BADCH (int)'?'
  293. #define BADARG ((*options == ':') ? (int)':' : (int)'?')
  294. #define INORDER (int)1
  295. #define EMSG ""
  296. static int getopt_internal(int, char * const *, const char *,
  297. const struct option *, int *, int);
  298. static int parse_long_options(char * const *, const char *,
  299. const struct option *, int *, int);
  300. static int gcd(int, int);
  301. static void permute_args(int, int, int, char * const *);
  302. static char *place = EMSG; /* option letter processing */
  303. /* XXX: set optreset to 1 rather than these two */
  304. static int nonopt_start = -1; /* first non option argument (for permute) */
  305. static int nonopt_end = -1; /* first option after non options (for permute) */
  306. /* Error messages */
  307. static const char recargchar[] = "option requires an argument -- %c";
  308. static const char recargstring[] = "option requires an argument -- %s";
  309. static const char ambig[] = "ambiguous option -- %.*s";
  310. static const char noarg[] = "option doesn't take an argument -- %.*s";
  311. static const char illoptchar[] = "unknown option -- %c";
  312. static const char illoptstring[] = "unknown option -- %s";
  313. #if defined(_WIN32) || defined(ESP_PLATFORM)
  314. /* Windows needs warnx(). We change the definition though:
  315. * 1. (another) global is defined, opterrmsg, which holds the error message
  316. * 2. errors are always printed out on stderr w/o the program name
  317. * Note that opterrmsg always gets set no matter what opterr is set to. The
  318. * error message will not be printed if opterr is 0 as usual.
  319. */
  320. #include <stdio.h>
  321. #include <stdarg.h>
  322. extern char opterrmsg[128];
  323. char opterrmsg[128]; /* buffer for the last error message */
  324. static void warnx(const char *fmt, ...)
  325. {
  326. va_list ap;
  327. va_start(ap, fmt);
  328. /*
  329. Make sure opterrmsg is always zero-terminated despite the _vsnprintf()
  330. implementation specifics and manually suppress the warning.
  331. */
  332. memset(opterrmsg, 0, sizeof opterrmsg);
  333. if (fmt != NULL)
  334. vsnprintf(opterrmsg, sizeof(opterrmsg) - 1, fmt, ap);
  335. va_end(ap);
  336. #if defined(_WIN32)
  337. #pragma warning(suppress: 6053)
  338. #endif
  339. fprintf(stderr, "%s\n", opterrmsg);
  340. }
  341. #else
  342. #include <err.h>
  343. #endif /*_WIN32*/
  344. /*
  345. * Compute the greatest common divisor of a and b.
  346. */
  347. static int
  348. gcd(int a, int b)
  349. {
  350. int c;
  351. c = a % b;
  352. while (c != 0) {
  353. a = b;
  354. b = c;
  355. c = a % b;
  356. }
  357. return (b);
  358. }
  359. /*
  360. * Exchange the block from nonopt_start to nonopt_end with the block
  361. * from nonopt_end to opt_end (keeping the same order of arguments
  362. * in each block).
  363. */
  364. static void
  365. permute_args(int panonopt_start, int panonopt_end, int opt_end,
  366. char * const *nargv)
  367. {
  368. int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
  369. char *swap;
  370. /*
  371. * compute lengths of blocks and number and size of cycles
  372. */
  373. nnonopts = panonopt_end - panonopt_start;
  374. nopts = opt_end - panonopt_end;
  375. ncycle = gcd(nnonopts, nopts);
  376. cyclelen = (opt_end - panonopt_start) / ncycle;
  377. for (i = 0; i < ncycle; i++) {
  378. cstart = panonopt_end+i;
  379. pos = cstart;
  380. for (j = 0; j < cyclelen; j++) {
  381. if (pos >= panonopt_end)
  382. pos -= nnonopts;
  383. else
  384. pos += nopts;
  385. swap = nargv[pos];
  386. /* LINTED const cast */
  387. ((char **) nargv)[pos] = nargv[cstart];
  388. /* LINTED const cast */
  389. ((char **)nargv)[cstart] = swap;
  390. }
  391. }
  392. }
  393. /*
  394. * parse_long_options --
  395. * Parse long options in argc/argv argument vector.
  396. * Returns -1 if short_too is set and the option does not match long_options.
  397. */
  398. static int
  399. parse_long_options(char * const *nargv, const char *options,
  400. const struct option *long_options, int *idx, int short_too)
  401. {
  402. char *current_argv, *has_equal;
  403. size_t current_argv_len;
  404. int i, match;
  405. current_argv = place;
  406. match = -1;
  407. optind++;
  408. if ((has_equal = strchr(current_argv, '=')) != NULL) {
  409. /* argument found (--option=arg) */
  410. current_argv_len = has_equal - current_argv;
  411. has_equal++;
  412. } else
  413. current_argv_len = strlen(current_argv);
  414. for (i = 0; long_options[i].name; i++) {
  415. /* find matching long option */
  416. if (strncmp(current_argv, long_options[i].name,
  417. current_argv_len))
  418. continue;
  419. if (strlen(long_options[i].name) == current_argv_len) {
  420. /* exact match */
  421. match = i;
  422. break;
  423. }
  424. /*
  425. * If this is a known short option, don't allow
  426. * a partial match of a single character.
  427. */
  428. if (short_too && current_argv_len == 1)
  429. continue;
  430. if (match == -1) /* partial match */
  431. match = i;
  432. else {
  433. /* ambiguous abbreviation */
  434. if (PRINT_ERROR)
  435. warnx(ambig, (int)current_argv_len,
  436. current_argv);
  437. optopt = 0;
  438. return (BADCH);
  439. }
  440. }
  441. if (match != -1) { /* option found */
  442. if (long_options[match].has_arg == no_argument
  443. && has_equal) {
  444. if (PRINT_ERROR)
  445. warnx(noarg, (int)current_argv_len,
  446. current_argv);
  447. /*
  448. * XXX: GNU sets optopt to val regardless of flag
  449. */
  450. if (long_options[match].flag == NULL)
  451. optopt = long_options[match].val;
  452. else
  453. optopt = 0;
  454. return (BADARG);
  455. }
  456. if (long_options[match].has_arg == required_argument ||
  457. long_options[match].has_arg == optional_argument) {
  458. if (has_equal)
  459. optarg = has_equal;
  460. else if (long_options[match].has_arg ==
  461. required_argument) {
  462. /*
  463. * optional argument doesn't use next nargv
  464. */
  465. optarg = nargv[optind++];
  466. }
  467. }
  468. if ((long_options[match].has_arg == required_argument)
  469. && (optarg == NULL)) {
  470. /*
  471. * Missing argument; leading ':' indicates no error
  472. * should be generated.
  473. */
  474. if (PRINT_ERROR)
  475. warnx(recargstring,
  476. current_argv);
  477. /*
  478. * XXX: GNU sets optopt to val regardless of flag
  479. */
  480. if (long_options[match].flag == NULL)
  481. optopt = long_options[match].val;
  482. else
  483. optopt = 0;
  484. --optind;
  485. return (BADARG);
  486. }
  487. } else { /* unknown option */
  488. if (short_too) {
  489. --optind;
  490. return (-1);
  491. }
  492. if (PRINT_ERROR)
  493. warnx(illoptstring, current_argv);
  494. optopt = 0;
  495. return (BADCH);
  496. }
  497. if (idx)
  498. *idx = match;
  499. if (long_options[match].flag) {
  500. *long_options[match].flag = long_options[match].val;
  501. return (0);
  502. } else
  503. return (long_options[match].val);
  504. }
  505. /*
  506. * getopt_internal --
  507. * Parse argc/argv argument vector. Called by user level routines.
  508. */
  509. static int
  510. getopt_internal(int nargc, char * const *nargv, const char *options,
  511. const struct option *long_options, int *idx, int flags)
  512. {
  513. char *oli; /* option letter list index */
  514. int optchar, short_too;
  515. static int posixly_correct = -1;
  516. if (options == NULL)
  517. return (-1);
  518. /*
  519. * Disable GNU extensions if POSIXLY_CORRECT is set or options
  520. * string begins with a '+'.
  521. */
  522. if (posixly_correct == -1)
  523. posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
  524. if (posixly_correct || *options == '+')
  525. flags &= ~FLAG_PERMUTE;
  526. else if (*options == '-')
  527. flags |= FLAG_ALLARGS;
  528. if (*options == '+' || *options == '-')
  529. options++;
  530. /*
  531. * XXX Some GNU programs (like cvs) set optind to 0 instead of
  532. * XXX using optreset. Work around this braindamage.
  533. */
  534. if (optind == 0)
  535. optind = optreset = 1;
  536. optarg = NULL;
  537. if (optreset)
  538. nonopt_start = nonopt_end = -1;
  539. start:
  540. if (optreset || !*place) { /* update scanning pointer */
  541. optreset = 0;
  542. if (optind >= nargc) { /* end of argument vector */
  543. place = EMSG;
  544. if (nonopt_end != -1) {
  545. /* do permutation, if we have to */
  546. permute_args(nonopt_start, nonopt_end,
  547. optind, nargv);
  548. optind -= nonopt_end - nonopt_start;
  549. }
  550. else if (nonopt_start != -1) {
  551. /*
  552. * If we skipped non-options, set optind
  553. * to the first of them.
  554. */
  555. optind = nonopt_start;
  556. }
  557. nonopt_start = nonopt_end = -1;
  558. return (-1);
  559. }
  560. if (*(place = nargv[optind]) != '-' ||
  561. (place[1] == '\0' && strchr(options, '-') == NULL)) {
  562. place = EMSG; /* found non-option */
  563. if (flags & FLAG_ALLARGS) {
  564. /*
  565. * GNU extension:
  566. * return non-option as argument to option 1
  567. */
  568. optarg = nargv[optind++];
  569. return (INORDER);
  570. }
  571. if (!(flags & FLAG_PERMUTE)) {
  572. /*
  573. * If no permutation wanted, stop parsing
  574. * at first non-option.
  575. */
  576. return (-1);
  577. }
  578. /* do permutation */
  579. if (nonopt_start == -1)
  580. nonopt_start = optind;
  581. else if (nonopt_end != -1) {
  582. permute_args(nonopt_start, nonopt_end,
  583. optind, nargv);
  584. nonopt_start = optind -
  585. (nonopt_end - nonopt_start);
  586. nonopt_end = -1;
  587. }
  588. optind++;
  589. /* process next argument */
  590. goto start;
  591. }
  592. if (nonopt_start != -1 && nonopt_end == -1)
  593. nonopt_end = optind;
  594. /*
  595. * If we have "-" do nothing, if "--" we are done.
  596. */
  597. if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
  598. optind++;
  599. place = EMSG;
  600. /*
  601. * We found an option (--), so if we skipped
  602. * non-options, we have to permute.
  603. */
  604. if (nonopt_end != -1) {
  605. permute_args(nonopt_start, nonopt_end,
  606. optind, nargv);
  607. optind -= nonopt_end - nonopt_start;
  608. }
  609. nonopt_start = nonopt_end = -1;
  610. return (-1);
  611. }
  612. }
  613. /*
  614. * Check long options if:
  615. * 1) we were passed some
  616. * 2) the arg is not just "-"
  617. * 3) either the arg starts with -- we are getopt_long_only()
  618. */
  619. if (long_options != NULL && place != nargv[optind] &&
  620. (*place == '-' || (flags & FLAG_LONGONLY))) {
  621. short_too = 0;
  622. if (*place == '-')
  623. place++; /* --foo long option */
  624. else if (*place != ':' && strchr(options, *place) != NULL)
  625. short_too = 1; /* could be short option too */
  626. optchar = parse_long_options(nargv, options, long_options,
  627. idx, short_too);
  628. if (optchar != -1) {
  629. place = EMSG;
  630. return (optchar);
  631. }
  632. }
  633. if ((optchar = (int)*place++) == (int)':' ||
  634. (optchar == (int)'-' && *place != '\0') ||
  635. (oli = strchr(options, optchar)) == NULL) {
  636. /*
  637. * If the user specified "-" and '-' isn't listed in
  638. * options, return -1 (non-option) as per POSIX.
  639. * Otherwise, it is an unknown option character (or ':').
  640. */
  641. if (optchar == (int)'-' && *place == '\0')
  642. return (-1);
  643. if (!*place)
  644. ++optind;
  645. if (PRINT_ERROR)
  646. warnx(illoptchar, optchar);
  647. optopt = optchar;
  648. return (BADCH);
  649. }
  650. if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
  651. /* -W long-option */
  652. if (*place) /* no space */
  653. /* NOTHING */;
  654. else if (++optind >= nargc) { /* no arg */
  655. place = EMSG;
  656. if (PRINT_ERROR)
  657. warnx(recargchar, optchar);
  658. optopt = optchar;
  659. return (BADARG);
  660. } else /* white space */
  661. place = nargv[optind];
  662. optchar = parse_long_options(nargv, options, long_options,
  663. idx, 0);
  664. place = EMSG;
  665. return (optchar);
  666. }
  667. if (*++oli != ':') { /* doesn't take argument */
  668. if (!*place)
  669. ++optind;
  670. } else { /* takes (optional) argument */
  671. optarg = NULL;
  672. if (*place) /* no white space */
  673. optarg = place;
  674. else if (oli[1] != ':') { /* arg not optional */
  675. if (++optind >= nargc) { /* no arg */
  676. place = EMSG;
  677. if (PRINT_ERROR)
  678. warnx(recargchar, optchar);
  679. optopt = optchar;
  680. return (BADARG);
  681. } else
  682. optarg = nargv[optind];
  683. }
  684. place = EMSG;
  685. ++optind;
  686. }
  687. /* dump back option letter */
  688. return (optchar);
  689. }
  690. /*
  691. * getopt --
  692. * Parse argc/argv argument vector.
  693. *
  694. * [eventually this will replace the BSD getopt]
  695. */
  696. int
  697. getopt(int nargc, char * const *nargv, const char *options)
  698. {
  699. /*
  700. * We don't pass FLAG_PERMUTE to getopt_internal() since
  701. * the BSD getopt(3) (unlike GNU) has never done this.
  702. *
  703. * Furthermore, since many privileged programs call getopt()
  704. * before dropping privileges it makes sense to keep things
  705. * as simple (and bug-free) as possible.
  706. */
  707. return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
  708. }
  709. /*
  710. * getopt_long --
  711. * Parse argc/argv argument vector.
  712. */
  713. int
  714. getopt_long(int nargc, char * const *nargv, const char *options,
  715. const struct option *long_options, int *idx)
  716. {
  717. return (getopt_internal(nargc, nargv, options, long_options, idx,
  718. FLAG_PERMUTE));
  719. }
  720. /*
  721. * getopt_long_only --
  722. * Parse argc/argv argument vector.
  723. */
  724. int
  725. getopt_long_only(int nargc, char * const *nargv, const char *options,
  726. const struct option *long_options, int *idx)
  727. {
  728. return (getopt_internal(nargc, nargv, options, long_options, idx,
  729. FLAG_PERMUTE|FLAG_LONGONLY));
  730. }
  731. #endif /* REPLACE_GETOPT */
  732. /*******************************************************************************
  733. * This file is part of the argtable3 library.
  734. *
  735. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  736. * <sheitmann@users.sourceforge.net>
  737. * All rights reserved.
  738. *
  739. * Redistribution and use in source and binary forms, with or without
  740. * modification, are permitted provided that the following conditions are met:
  741. * * Redistributions of source code must retain the above copyright
  742. * notice, this list of conditions and the following disclaimer.
  743. * * Redistributions in binary form must reproduce the above copyright
  744. * notice, this list of conditions and the following disclaimer in the
  745. * documentation and/or other materials provided with the distribution.
  746. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  747. * may be used to endorse or promote products derived from this software
  748. * without specific prior written permission.
  749. *
  750. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  751. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  752. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  753. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  754. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  755. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  756. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  757. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  758. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  759. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  760. ******************************************************************************/
  761. #include <stdlib.h>
  762. #include <string.h>
  763. #include "argtable3.h"
  764. char * arg_strptime(const char *buf, const char *fmt, struct tm *tm);
  765. static void arg_date_resetfn(struct arg_date *parent)
  766. {
  767. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  768. parent->count = 0;
  769. }
  770. static int arg_date_scanfn(struct arg_date *parent, const char *argval)
  771. {
  772. int errorcode = 0;
  773. if (parent->count == parent->hdr.maxcount)
  774. {
  775. errorcode = EMAXCOUNT;
  776. }
  777. else if (!argval)
  778. {
  779. /* no argument value was given, leave parent->tmval[] unaltered but still count it */
  780. parent->count++;
  781. }
  782. else
  783. {
  784. const char *pend;
  785. struct tm tm = parent->tmval[parent->count];
  786. /* parse the given argument value, store result in parent->tmval[] */
  787. pend = arg_strptime(argval, parent->format, &tm);
  788. if (pend && pend[0] == '\0')
  789. parent->tmval[parent->count++] = tm;
  790. else
  791. errorcode = EBADDATE;
  792. }
  793. ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode));
  794. return errorcode;
  795. }
  796. static int arg_date_checkfn(struct arg_date *parent)
  797. {
  798. int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
  799. ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode));
  800. return errorcode;
  801. }
  802. static void arg_date_errorfn(
  803. struct arg_date *parent,
  804. FILE *fp,
  805. int errorcode,
  806. const char *argval,
  807. const char *progname)
  808. {
  809. const char *shortopts = parent->hdr.shortopts;
  810. const char *longopts = parent->hdr.longopts;
  811. const char *datatype = parent->hdr.datatype;
  812. /* make argval NULL safe */
  813. argval = argval ? argval : "";
  814. fprintf(fp, "%s: ", progname);
  815. switch(errorcode)
  816. {
  817. case EMINCOUNT:
  818. fputs("missing option ", fp);
  819. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  820. break;
  821. case EMAXCOUNT:
  822. fputs("excess option ", fp);
  823. arg_print_option(fp, shortopts, longopts, argval, "\n");
  824. break;
  825. case EBADDATE:
  826. {
  827. struct tm tm;
  828. char buff[200];
  829. fprintf(fp, "illegal timestamp format \"%s\"\n", argval);
  830. memset(&tm, 0, sizeof(tm));
  831. arg_strptime("1999-12-31 23:59:59", "%F %H:%M:%S", &tm);
  832. strftime(buff, sizeof(buff), parent->format, &tm);
  833. printf("correct format is \"%s\"\n", buff);
  834. break;
  835. }
  836. }
  837. }
  838. struct arg_date * arg_date0(
  839. const char * shortopts,
  840. const char * longopts,
  841. const char * format,
  842. const char *datatype,
  843. const char *glossary)
  844. {
  845. return arg_daten(shortopts, longopts, format, datatype, 0, 1, glossary);
  846. }
  847. struct arg_date * arg_date1(
  848. const char * shortopts,
  849. const char * longopts,
  850. const char * format,
  851. const char *datatype,
  852. const char *glossary)
  853. {
  854. return arg_daten(shortopts, longopts, format, datatype, 1, 1, glossary);
  855. }
  856. struct arg_date * arg_daten(
  857. const char * shortopts,
  858. const char * longopts,
  859. const char * format,
  860. const char *datatype,
  861. int mincount,
  862. int maxcount,
  863. const char *glossary)
  864. {
  865. size_t nbytes;
  866. struct arg_date *result;
  867. /* foolproof things by ensuring maxcount is not less than mincount */
  868. maxcount = (maxcount < mincount) ? mincount : maxcount;
  869. /* default time format is the national date format for the locale */
  870. if (!format)
  871. format = "%x";
  872. nbytes = sizeof(struct arg_date) /* storage for struct arg_date */
  873. + maxcount * sizeof(struct tm); /* storage for tmval[maxcount] array */
  874. /* allocate storage for the arg_date struct + tmval[] array. */
  875. /* we use calloc because we want the tmval[] array zero filled. */
  876. result = (struct arg_date *)calloc(1, nbytes);
  877. if (result)
  878. {
  879. /* init the arg_hdr struct */
  880. result->hdr.flag = ARG_HASVALUE;
  881. result->hdr.shortopts = shortopts;
  882. result->hdr.longopts = longopts;
  883. result->hdr.datatype = datatype ? datatype : format;
  884. result->hdr.glossary = glossary;
  885. result->hdr.mincount = mincount;
  886. result->hdr.maxcount = maxcount;
  887. result->hdr.parent = result;
  888. result->hdr.resetfn = (arg_resetfn *)arg_date_resetfn;
  889. result->hdr.scanfn = (arg_scanfn *)arg_date_scanfn;
  890. result->hdr.checkfn = (arg_checkfn *)arg_date_checkfn;
  891. result->hdr.errorfn = (arg_errorfn *)arg_date_errorfn;
  892. /* store the tmval[maxcount] array immediately after the arg_date struct */
  893. result->tmval = (struct tm *)(result + 1);
  894. /* init the remaining arg_date member variables */
  895. result->count = 0;
  896. result->format = format;
  897. }
  898. ARG_TRACE(("arg_daten() returns %p\n", result));
  899. return result;
  900. }
  901. /*-
  902. * Copyright (c) 1997, 1998, 2005, 2008 The NetBSD Foundation, Inc.
  903. * All rights reserved.
  904. *
  905. * This code was contributed to The NetBSD Foundation by Klaus Klein.
  906. * Heavily optimised by David Laight
  907. *
  908. * Redistribution and use in source and binary forms, with or without
  909. * modification, are permitted provided that the following conditions
  910. * are met:
  911. * 1. Redistributions of source code must retain the above copyright
  912. * notice, this list of conditions and the following disclaimer.
  913. * 2. Redistributions in binary form must reproduce the above copyright
  914. * notice, this list of conditions and the following disclaimer in the
  915. * documentation and/or other materials provided with the distribution.
  916. *
  917. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  918. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  919. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  920. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  921. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  922. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  923. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  924. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  925. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  926. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  927. * POSSIBILITY OF SUCH DAMAGE.
  928. */
  929. #include <ctype.h>
  930. #include <string.h>
  931. #include <time.h>
  932. /*
  933. * We do not implement alternate representations. However, we always
  934. * check whether a given modifier is allowed for a certain conversion.
  935. */
  936. #define ALT_E 0x01
  937. #define ALT_O 0x02
  938. #define LEGAL_ALT(x) { if (alt_format & ~(x)) return (0); }
  939. #define TM_YEAR_BASE (1900)
  940. static int conv_num(const char * *, int *, int, int);
  941. static const char *day[7] = {
  942. "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
  943. "Friday", "Saturday"
  944. };
  945. static const char *abday[7] = {
  946. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  947. };
  948. static const char *mon[12] = {
  949. "January", "February", "March", "April", "May", "June", "July",
  950. "August", "September", "October", "November", "December"
  951. };
  952. static const char *abmon[12] = {
  953. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  954. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  955. };
  956. static const char *am_pm[2] = {
  957. "AM", "PM"
  958. };
  959. static int arg_strcasecmp(const char *s1, const char *s2)
  960. {
  961. const unsigned char *us1 = (const unsigned char *)s1;
  962. const unsigned char *us2 = (const unsigned char *)s2;
  963. while (tolower(*us1) == tolower(*us2++))
  964. if (*us1++ == '\0')
  965. return 0;
  966. return tolower(*us1) - tolower(*--us2);
  967. }
  968. static int arg_strncasecmp(const char *s1, const char *s2, size_t n)
  969. {
  970. if (n != 0)
  971. {
  972. const unsigned char *us1 = (const unsigned char *)s1;
  973. const unsigned char *us2 = (const unsigned char *)s2;
  974. do
  975. {
  976. if (tolower(*us1) != tolower(*us2++))
  977. return tolower(*us1) - tolower(*--us2);
  978. if (*us1++ == '\0')
  979. break;
  980. } while (--n != 0);
  981. }
  982. return 0;
  983. }
  984. char * arg_strptime(const char *buf, const char *fmt, struct tm *tm)
  985. {
  986. char c;
  987. const char *bp;
  988. size_t len = 0;
  989. int alt_format, i, split_year = 0;
  990. bp = buf;
  991. while ((c = *fmt) != '\0') {
  992. /* Clear `alternate' modifier prior to new conversion. */
  993. alt_format = 0;
  994. /* Eat up white-space. */
  995. if (isspace((int) c)) {
  996. while (isspace((int) *bp))
  997. bp++;
  998. fmt++;
  999. continue;
  1000. }
  1001. if ((c = *fmt++) != '%')
  1002. goto literal;
  1003. again:
  1004. switch (c = *fmt++)
  1005. {
  1006. case '%': /* "%%" is converted to "%". */
  1007. literal:
  1008. if (c != *bp++)
  1009. return (0);
  1010. break;
  1011. /*
  1012. * "Alternative" modifiers. Just set the appropriate flag
  1013. * and start over again.
  1014. */
  1015. case 'E': /* "%E?" alternative conversion modifier. */
  1016. LEGAL_ALT(0);
  1017. alt_format |= ALT_E;
  1018. goto again;
  1019. case 'O': /* "%O?" alternative conversion modifier. */
  1020. LEGAL_ALT(0);
  1021. alt_format |= ALT_O;
  1022. goto again;
  1023. /*
  1024. * "Complex" conversion rules, implemented through recursion.
  1025. */
  1026. case 'c': /* Date and time, using the locale's format. */
  1027. LEGAL_ALT(ALT_E);
  1028. bp = arg_strptime(bp, "%x %X", tm);
  1029. if (!bp)
  1030. return (0);
  1031. break;
  1032. case 'D': /* The date as "%m/%d/%y". */
  1033. LEGAL_ALT(0);
  1034. bp = arg_strptime(bp, "%m/%d/%y", tm);
  1035. if (!bp)
  1036. return (0);
  1037. break;
  1038. case 'R': /* The time as "%H:%M". */
  1039. LEGAL_ALT(0);
  1040. bp = arg_strptime(bp, "%H:%M", tm);
  1041. if (!bp)
  1042. return (0);
  1043. break;
  1044. case 'r': /* The time in 12-hour clock representation. */
  1045. LEGAL_ALT(0);
  1046. bp = arg_strptime(bp, "%I:%M:%S %p", tm);
  1047. if (!bp)
  1048. return (0);
  1049. break;
  1050. case 'T': /* The time as "%H:%M:%S". */
  1051. LEGAL_ALT(0);
  1052. bp = arg_strptime(bp, "%H:%M:%S", tm);
  1053. if (!bp)
  1054. return (0);
  1055. break;
  1056. case 'X': /* The time, using the locale's format. */
  1057. LEGAL_ALT(ALT_E);
  1058. bp = arg_strptime(bp, "%H:%M:%S", tm);
  1059. if (!bp)
  1060. return (0);
  1061. break;
  1062. case 'x': /* The date, using the locale's format. */
  1063. LEGAL_ALT(ALT_E);
  1064. bp = arg_strptime(bp, "%m/%d/%y", tm);
  1065. if (!bp)
  1066. return (0);
  1067. break;
  1068. /*
  1069. * "Elementary" conversion rules.
  1070. */
  1071. case 'A': /* The day of week, using the locale's form. */
  1072. case 'a':
  1073. LEGAL_ALT(0);
  1074. for (i = 0; i < 7; i++) {
  1075. /* Full name. */
  1076. len = strlen(day[i]);
  1077. if (arg_strncasecmp(day[i], bp, len) == 0)
  1078. break;
  1079. /* Abbreviated name. */
  1080. len = strlen(abday[i]);
  1081. if (arg_strncasecmp(abday[i], bp, len) == 0)
  1082. break;
  1083. }
  1084. /* Nothing matched. */
  1085. if (i == 7)
  1086. return (0);
  1087. tm->tm_wday = i;
  1088. bp += len;
  1089. break;
  1090. case 'B': /* The month, using the locale's form. */
  1091. case 'b':
  1092. case 'h':
  1093. LEGAL_ALT(0);
  1094. for (i = 0; i < 12; i++) {
  1095. /* Full name. */
  1096. len = strlen(mon[i]);
  1097. if (arg_strncasecmp(mon[i], bp, len) == 0)
  1098. break;
  1099. /* Abbreviated name. */
  1100. len = strlen(abmon[i]);
  1101. if (arg_strncasecmp(abmon[i], bp, len) == 0)
  1102. break;
  1103. }
  1104. /* Nothing matched. */
  1105. if (i == 12)
  1106. return (0);
  1107. tm->tm_mon = i;
  1108. bp += len;
  1109. break;
  1110. case 'C': /* The century number. */
  1111. LEGAL_ALT(ALT_E);
  1112. if (!(conv_num(&bp, &i, 0, 99)))
  1113. return (0);
  1114. if (split_year) {
  1115. tm->tm_year = (tm->tm_year % 100) + (i * 100);
  1116. } else {
  1117. tm->tm_year = i * 100;
  1118. split_year = 1;
  1119. }
  1120. break;
  1121. case 'd': /* The day of month. */
  1122. case 'e':
  1123. LEGAL_ALT(ALT_O);
  1124. if (!(conv_num(&bp, &tm->tm_mday, 1, 31)))
  1125. return (0);
  1126. break;
  1127. case 'k': /* The hour (24-hour clock representation). */
  1128. LEGAL_ALT(0);
  1129. /* FALLTHROUGH */
  1130. case 'H':
  1131. LEGAL_ALT(ALT_O);
  1132. if (!(conv_num(&bp, &tm->tm_hour, 0, 23)))
  1133. return (0);
  1134. break;
  1135. case 'l': /* The hour (12-hour clock representation). */
  1136. LEGAL_ALT(0);
  1137. /* FALLTHROUGH */
  1138. case 'I':
  1139. LEGAL_ALT(ALT_O);
  1140. if (!(conv_num(&bp, &tm->tm_hour, 1, 12)))
  1141. return (0);
  1142. if (tm->tm_hour == 12)
  1143. tm->tm_hour = 0;
  1144. break;
  1145. case 'j': /* The day of year. */
  1146. LEGAL_ALT(0);
  1147. if (!(conv_num(&bp, &i, 1, 366)))
  1148. return (0);
  1149. tm->tm_yday = i - 1;
  1150. break;
  1151. case 'M': /* The minute. */
  1152. LEGAL_ALT(ALT_O);
  1153. if (!(conv_num(&bp, &tm->tm_min, 0, 59)))
  1154. return (0);
  1155. break;
  1156. case 'm': /* The month. */
  1157. LEGAL_ALT(ALT_O);
  1158. if (!(conv_num(&bp, &i, 1, 12)))
  1159. return (0);
  1160. tm->tm_mon = i - 1;
  1161. break;
  1162. case 'p': /* The locale's equivalent of AM/PM. */
  1163. LEGAL_ALT(0);
  1164. /* AM? */
  1165. if (arg_strcasecmp(am_pm[0], bp) == 0) {
  1166. if (tm->tm_hour > 11)
  1167. return (0);
  1168. bp += strlen(am_pm[0]);
  1169. break;
  1170. }
  1171. /* PM? */
  1172. else if (arg_strcasecmp(am_pm[1], bp) == 0) {
  1173. if (tm->tm_hour > 11)
  1174. return (0);
  1175. tm->tm_hour += 12;
  1176. bp += strlen(am_pm[1]);
  1177. break;
  1178. }
  1179. /* Nothing matched. */
  1180. return (0);
  1181. case 'S': /* The seconds. */
  1182. LEGAL_ALT(ALT_O);
  1183. if (!(conv_num(&bp, &tm->tm_sec, 0, 61)))
  1184. return (0);
  1185. break;
  1186. case 'U': /* The week of year, beginning on sunday. */
  1187. case 'W': /* The week of year, beginning on monday. */
  1188. LEGAL_ALT(ALT_O);
  1189. /*
  1190. * XXX This is bogus, as we can not assume any valid
  1191. * information present in the tm structure at this
  1192. * point to calculate a real value, so just check the
  1193. * range for now.
  1194. */
  1195. if (!(conv_num(&bp, &i, 0, 53)))
  1196. return (0);
  1197. break;
  1198. case 'w': /* The day of week, beginning on sunday. */
  1199. LEGAL_ALT(ALT_O);
  1200. if (!(conv_num(&bp, &tm->tm_wday, 0, 6)))
  1201. return (0);
  1202. break;
  1203. case 'Y': /* The year. */
  1204. LEGAL_ALT(ALT_E);
  1205. if (!(conv_num(&bp, &i, 0, 9999)))
  1206. return (0);
  1207. tm->tm_year = i - TM_YEAR_BASE;
  1208. break;
  1209. case 'y': /* The year within 100 years of the epoch. */
  1210. LEGAL_ALT(ALT_E | ALT_O);
  1211. if (!(conv_num(&bp, &i, 0, 99)))
  1212. return (0);
  1213. if (split_year) {
  1214. tm->tm_year = ((tm->tm_year / 100) * 100) + i;
  1215. break;
  1216. }
  1217. split_year = 1;
  1218. if (i <= 68)
  1219. tm->tm_year = i + 2000 - TM_YEAR_BASE;
  1220. else
  1221. tm->tm_year = i + 1900 - TM_YEAR_BASE;
  1222. break;
  1223. /*
  1224. * Miscellaneous conversions.
  1225. */
  1226. case 'n': /* Any kind of white-space. */
  1227. case 't':
  1228. LEGAL_ALT(0);
  1229. while (isspace((int) *bp))
  1230. bp++;
  1231. break;
  1232. default: /* Unknown/unsupported conversion. */
  1233. return (0);
  1234. }
  1235. }
  1236. /* LINTED functional specification */
  1237. return ((char *)bp);
  1238. }
  1239. static int conv_num(const char * *buf, int *dest, int llim, int ulim)
  1240. {
  1241. int result = 0;
  1242. /* The limit also determines the number of valid digits. */
  1243. int rulim = ulim;
  1244. if (**buf < '0' || **buf > '9')
  1245. return (0);
  1246. do {
  1247. result *= 10;
  1248. result += *(*buf)++ - '0';
  1249. rulim /= 10;
  1250. } while ((result * 10 <= ulim) && rulim && **buf >= '0' && **buf <= '9');
  1251. if (result < llim || result > ulim)
  1252. return (0);
  1253. *dest = result;
  1254. return (1);
  1255. }
  1256. /*******************************************************************************
  1257. * This file is part of the argtable3 library.
  1258. *
  1259. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  1260. * <sheitmann@users.sourceforge.net>
  1261. * All rights reserved.
  1262. *
  1263. * Redistribution and use in source and binary forms, with or without
  1264. * modification, are permitted provided that the following conditions are met:
  1265. * * Redistributions of source code must retain the above copyright
  1266. * notice, this list of conditions and the following disclaimer.
  1267. * * Redistributions in binary form must reproduce the above copyright
  1268. * notice, this list of conditions and the following disclaimer in the
  1269. * documentation and/or other materials provided with the distribution.
  1270. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  1271. * may be used to endorse or promote products derived from this software
  1272. * without specific prior written permission.
  1273. *
  1274. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  1275. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1276. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1277. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  1278. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  1279. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1280. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  1281. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1282. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1283. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1284. ******************************************************************************/
  1285. #include <stdlib.h>
  1286. #include "argtable3.h"
  1287. static void arg_dbl_resetfn(struct arg_dbl *parent)
  1288. {
  1289. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  1290. parent->count = 0;
  1291. }
  1292. static int arg_dbl_scanfn(struct arg_dbl *parent, const char *argval)
  1293. {
  1294. int errorcode = 0;
  1295. if (parent->count == parent->hdr.maxcount)
  1296. {
  1297. /* maximum number of arguments exceeded */
  1298. errorcode = EMAXCOUNT;
  1299. }
  1300. else if (!argval)
  1301. {
  1302. /* a valid argument with no argument value was given. */
  1303. /* This happens when an optional argument value was invoked. */
  1304. /* leave parent argument value unaltered but still count the argument. */
  1305. parent->count++;
  1306. }
  1307. else
  1308. {
  1309. double val;
  1310. char *end;
  1311. /* extract double from argval into val */
  1312. val = strtod(argval, &end);
  1313. /* if success then store result in parent->dval[] array otherwise return error*/
  1314. if (*end == 0)
  1315. parent->dval[parent->count++] = val;
  1316. else
  1317. errorcode = EBADDOUBLE;
  1318. }
  1319. ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode));
  1320. return errorcode;
  1321. }
  1322. static int arg_dbl_checkfn(struct arg_dbl *parent)
  1323. {
  1324. int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
  1325. ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode));
  1326. return errorcode;
  1327. }
  1328. static void arg_dbl_errorfn(
  1329. struct arg_dbl *parent,
  1330. FILE *fp,
  1331. int errorcode,
  1332. const char *argval,
  1333. const char *progname)
  1334. {
  1335. const char *shortopts = parent->hdr.shortopts;
  1336. const char *longopts = parent->hdr.longopts;
  1337. const char *datatype = parent->hdr.datatype;
  1338. /* make argval NULL safe */
  1339. argval = argval ? argval : "";
  1340. fprintf(fp, "%s: ", progname);
  1341. switch(errorcode)
  1342. {
  1343. case EMINCOUNT:
  1344. fputs("missing option ", fp);
  1345. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  1346. break;
  1347. case EMAXCOUNT:
  1348. fputs("excess option ", fp);
  1349. arg_print_option(fp, shortopts, longopts, argval, "\n");
  1350. break;
  1351. case EBADDOUBLE:
  1352. fprintf(fp, "invalid argument \"%s\" to option ", argval);
  1353. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  1354. break;
  1355. }
  1356. }
  1357. struct arg_dbl * arg_dbl0(
  1358. const char * shortopts,
  1359. const char * longopts,
  1360. const char *datatype,
  1361. const char *glossary)
  1362. {
  1363. return arg_dbln(shortopts, longopts, datatype, 0, 1, glossary);
  1364. }
  1365. struct arg_dbl * arg_dbl1(
  1366. const char * shortopts,
  1367. const char * longopts,
  1368. const char *datatype,
  1369. const char *glossary)
  1370. {
  1371. return arg_dbln(shortopts, longopts, datatype, 1, 1, glossary);
  1372. }
  1373. struct arg_dbl * arg_dbln(
  1374. const char * shortopts,
  1375. const char * longopts,
  1376. const char *datatype,
  1377. int mincount,
  1378. int maxcount,
  1379. const char *glossary)
  1380. {
  1381. size_t nbytes;
  1382. struct arg_dbl *result;
  1383. /* foolproof things by ensuring maxcount is not less than mincount */
  1384. maxcount = (maxcount < mincount) ? mincount : maxcount;
  1385. nbytes = sizeof(struct arg_dbl) /* storage for struct arg_dbl */
  1386. + (maxcount + 1) * sizeof(double); /* storage for dval[maxcount] array plus one extra for padding to memory boundary */
  1387. result = (struct arg_dbl *)malloc(nbytes);
  1388. if (result)
  1389. {
  1390. size_t addr;
  1391. size_t rem;
  1392. /* init the arg_hdr struct */
  1393. result->hdr.flag = ARG_HASVALUE;
  1394. result->hdr.shortopts = shortopts;
  1395. result->hdr.longopts = longopts;
  1396. result->hdr.datatype = datatype ? datatype : "<double>";
  1397. result->hdr.glossary = glossary;
  1398. result->hdr.mincount = mincount;
  1399. result->hdr.maxcount = maxcount;
  1400. result->hdr.parent = result;
  1401. result->hdr.resetfn = (arg_resetfn *)arg_dbl_resetfn;
  1402. result->hdr.scanfn = (arg_scanfn *)arg_dbl_scanfn;
  1403. result->hdr.checkfn = (arg_checkfn *)arg_dbl_checkfn;
  1404. result->hdr.errorfn = (arg_errorfn *)arg_dbl_errorfn;
  1405. /* Store the dval[maxcount] array on the first double boundary that
  1406. * immediately follows the arg_dbl struct. We do the memory alignment
  1407. * purely for SPARC and Motorola systems. They require floats and
  1408. * doubles to be aligned on natural boundaries.
  1409. */
  1410. addr = (size_t)(result + 1);
  1411. rem = addr % sizeof(double);
  1412. result->dval = (double *)(addr + sizeof(double) - rem);
  1413. ARG_TRACE(("addr=%p, dval=%p, sizeof(double)=%d rem=%d\n", addr, result->dval, (int)sizeof(double), (int)rem));
  1414. result->count = 0;
  1415. }
  1416. ARG_TRACE(("arg_dbln() returns %p\n", result));
  1417. return result;
  1418. }
  1419. /*******************************************************************************
  1420. * This file is part of the argtable3 library.
  1421. *
  1422. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  1423. * <sheitmann@users.sourceforge.net>
  1424. * All rights reserved.
  1425. *
  1426. * Redistribution and use in source and binary forms, with or without
  1427. * modification, are permitted provided that the following conditions are met:
  1428. * * Redistributions of source code must retain the above copyright
  1429. * notice, this list of conditions and the following disclaimer.
  1430. * * Redistributions in binary form must reproduce the above copyright
  1431. * notice, this list of conditions and the following disclaimer in the
  1432. * documentation and/or other materials provided with the distribution.
  1433. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  1434. * may be used to endorse or promote products derived from this software
  1435. * without specific prior written permission.
  1436. *
  1437. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  1438. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1439. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1440. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  1441. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  1442. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1443. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  1444. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1445. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1446. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1447. ******************************************************************************/
  1448. #include <stdlib.h>
  1449. #include "argtable3.h"
  1450. static void arg_end_resetfn(struct arg_end *parent)
  1451. {
  1452. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  1453. parent->count = 0;
  1454. }
  1455. static void arg_end_errorfn(
  1456. void *parent,
  1457. FILE *fp,
  1458. int error,
  1459. const char *argval,
  1460. const char *progname)
  1461. {
  1462. /* suppress unreferenced formal parameter warning */
  1463. (void)parent;
  1464. progname = progname ? progname : "";
  1465. argval = argval ? argval : "";
  1466. fprintf(fp, "%s: ", progname);
  1467. switch(error)
  1468. {
  1469. case ARG_ELIMIT:
  1470. fputs("too many errors to display", fp);
  1471. break;
  1472. case ARG_EMALLOC:
  1473. fputs("insufficent memory", fp);
  1474. break;
  1475. case ARG_ENOMATCH:
  1476. fprintf(fp, "unexpected argument \"%s\"", argval);
  1477. break;
  1478. case ARG_EMISSARG:
  1479. fprintf(fp, "option \"%s\" requires an argument", argval);
  1480. break;
  1481. case ARG_ELONGOPT:
  1482. fprintf(fp, "invalid option \"%s\"", argval);
  1483. break;
  1484. default:
  1485. fprintf(fp, "invalid option \"-%c\"", error);
  1486. break;
  1487. }
  1488. fputc('\n', fp);
  1489. }
  1490. struct arg_end * arg_end(int maxcount)
  1491. {
  1492. size_t nbytes;
  1493. struct arg_end *result;
  1494. nbytes = sizeof(struct arg_end)
  1495. + maxcount * sizeof(int) /* storage for int error[maxcount] array*/
  1496. + maxcount * sizeof(void *) /* storage for void* parent[maxcount] array */
  1497. + maxcount * sizeof(char *); /* storage for char* argval[maxcount] array */
  1498. result = (struct arg_end *)malloc(nbytes);
  1499. if (result)
  1500. {
  1501. /* init the arg_hdr struct */
  1502. result->hdr.flag = ARG_TERMINATOR;
  1503. result->hdr.shortopts = NULL;
  1504. result->hdr.longopts = NULL;
  1505. result->hdr.datatype = NULL;
  1506. result->hdr.glossary = NULL;
  1507. result->hdr.mincount = 1;
  1508. result->hdr.maxcount = maxcount;
  1509. result->hdr.parent = result;
  1510. result->hdr.resetfn = (arg_resetfn *)arg_end_resetfn;
  1511. result->hdr.scanfn = NULL;
  1512. result->hdr.checkfn = NULL;
  1513. result->hdr.errorfn = (arg_errorfn *)arg_end_errorfn;
  1514. /* store error[maxcount] array immediately after struct arg_end */
  1515. result->error = (int *)(result + 1);
  1516. /* store parent[maxcount] array immediately after error[] array */
  1517. result->parent = (void * *)(result->error + maxcount );
  1518. /* store argval[maxcount] array immediately after parent[] array */
  1519. result->argval = (const char * *)(result->parent + maxcount );
  1520. }
  1521. ARG_TRACE(("arg_end(%d) returns %p\n", maxcount, result));
  1522. return result;
  1523. }
  1524. void arg_print_errors(FILE * fp, struct arg_end * end, const char * progname)
  1525. {
  1526. int i;
  1527. ARG_TRACE(("arg_errors()\n"));
  1528. for (i = 0; i < end->count; i++)
  1529. {
  1530. struct arg_hdr *errorparent = (struct arg_hdr *)(end->parent[i]);
  1531. if (errorparent->errorfn)
  1532. errorparent->errorfn(end->parent[i],
  1533. fp,
  1534. end->error[i],
  1535. end->argval[i],
  1536. progname);
  1537. }
  1538. }
  1539. /*******************************************************************************
  1540. * This file is part of the argtable3 library.
  1541. *
  1542. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  1543. * <sheitmann@users.sourceforge.net>
  1544. * All rights reserved.
  1545. *
  1546. * Redistribution and use in source and binary forms, with or without
  1547. * modification, are permitted provided that the following conditions are met:
  1548. * * Redistributions of source code must retain the above copyright
  1549. * notice, this list of conditions and the following disclaimer.
  1550. * * Redistributions in binary form must reproduce the above copyright
  1551. * notice, this list of conditions and the following disclaimer in the
  1552. * documentation and/or other materials provided with the distribution.
  1553. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  1554. * may be used to endorse or promote products derived from this software
  1555. * without specific prior written permission.
  1556. *
  1557. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  1558. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1559. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1560. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  1561. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  1562. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1563. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  1564. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1565. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1566. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1567. ******************************************************************************/
  1568. #include <string.h>
  1569. #include <stdlib.h>
  1570. #include "argtable3.h"
  1571. #ifdef WIN32
  1572. # define FILESEPARATOR1 '\\'
  1573. # define FILESEPARATOR2 '/'
  1574. #else
  1575. # define FILESEPARATOR1 '/'
  1576. # define FILESEPARATOR2 '/'
  1577. #endif
  1578. static void arg_file_resetfn(struct arg_file *parent)
  1579. {
  1580. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  1581. parent->count = 0;
  1582. }
  1583. /* Returns ptr to the base filename within *filename */
  1584. static const char * arg_basename(const char *filename)
  1585. {
  1586. const char *result = NULL, *result1, *result2;
  1587. /* Find the last occurrence of eother file separator character. */
  1588. /* Two alternative file separator chars are supported as legal */
  1589. /* file separators but not both together in the same filename. */
  1590. result1 = (filename ? strrchr(filename, FILESEPARATOR1) : NULL);
  1591. result2 = (filename ? strrchr(filename, FILESEPARATOR2) : NULL);
  1592. if (result2)
  1593. result = result2 + 1; /* using FILESEPARATOR2 (the alternative file separator) */
  1594. if (result1)
  1595. result = result1 + 1; /* using FILESEPARATOR1 (the preferred file separator) */
  1596. if (!result)
  1597. result = filename; /* neither file separator was found so basename is the whole filename */
  1598. /* special cases of "." and ".." are not considered basenames */
  1599. if (result && ( strcmp(".", result) == 0 || strcmp("..", result) == 0 ))
  1600. result = filename + strlen(filename);
  1601. return result;
  1602. }
  1603. /* Returns ptr to the file extension within *basename */
  1604. static const char * arg_extension(const char *basename)
  1605. {
  1606. /* find the last occurrence of '.' in basename */
  1607. const char *result = (basename ? strrchr(basename, '.') : NULL);
  1608. /* if no '.' was found then return pointer to end of basename */
  1609. if (basename && !result)
  1610. result = basename + strlen(basename);
  1611. /* special case: basenames with a single leading dot (eg ".foo") are not considered as true extensions */
  1612. if (basename && result == basename)
  1613. result = basename + strlen(basename);
  1614. /* special case: empty extensions (eg "foo.","foo..") are not considered as true extensions */
  1615. if (basename && result && result[1] == '\0')
  1616. result = basename + strlen(basename);
  1617. return result;
  1618. }
  1619. static int arg_file_scanfn(struct arg_file *parent, const char *argval)
  1620. {
  1621. int errorcode = 0;
  1622. if (parent->count == parent->hdr.maxcount)
  1623. {
  1624. /* maximum number of arguments exceeded */
  1625. errorcode = EMAXCOUNT;
  1626. }
  1627. else if (!argval)
  1628. {
  1629. /* a valid argument with no argument value was given. */
  1630. /* This happens when an optional argument value was invoked. */
  1631. /* leave parent arguiment value unaltered but still count the argument. */
  1632. parent->count++;
  1633. }
  1634. else
  1635. {
  1636. parent->filename[parent->count] = argval;
  1637. parent->basename[parent->count] = arg_basename(argval);
  1638. parent->extension[parent->count] =
  1639. arg_extension(parent->basename[parent->count]); /* only seek extensions within the basename (not the file path)*/
  1640. parent->count++;
  1641. }
  1642. ARG_TRACE(("%s4:scanfn(%p) returns %d\n", __FILE__, parent, errorcode));
  1643. return errorcode;
  1644. }
  1645. static int arg_file_checkfn(struct arg_file *parent)
  1646. {
  1647. int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
  1648. ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode));
  1649. return errorcode;
  1650. }
  1651. static void arg_file_errorfn(
  1652. struct arg_file *parent,
  1653. FILE *fp,
  1654. int errorcode,
  1655. const char *argval,
  1656. const char *progname)
  1657. {
  1658. const char *shortopts = parent->hdr.shortopts;
  1659. const char *longopts = parent->hdr.longopts;
  1660. const char *datatype = parent->hdr.datatype;
  1661. /* make argval NULL safe */
  1662. argval = argval ? argval : "";
  1663. fprintf(fp, "%s: ", progname);
  1664. switch(errorcode)
  1665. {
  1666. case EMINCOUNT:
  1667. fputs("missing option ", fp);
  1668. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  1669. break;
  1670. case EMAXCOUNT:
  1671. fputs("excess option ", fp);
  1672. arg_print_option(fp, shortopts, longopts, argval, "\n");
  1673. break;
  1674. default:
  1675. fprintf(fp, "unknown error at \"%s\"\n", argval);
  1676. }
  1677. }
  1678. struct arg_file * arg_file0(
  1679. const char * shortopts,
  1680. const char * longopts,
  1681. const char *datatype,
  1682. const char *glossary)
  1683. {
  1684. return arg_filen(shortopts, longopts, datatype, 0, 1, glossary);
  1685. }
  1686. struct arg_file * arg_file1(
  1687. const char * shortopts,
  1688. const char * longopts,
  1689. const char *datatype,
  1690. const char *glossary)
  1691. {
  1692. return arg_filen(shortopts, longopts, datatype, 1, 1, glossary);
  1693. }
  1694. struct arg_file * arg_filen(
  1695. const char * shortopts,
  1696. const char * longopts,
  1697. const char *datatype,
  1698. int mincount,
  1699. int maxcount,
  1700. const char *glossary)
  1701. {
  1702. size_t nbytes;
  1703. struct arg_file *result;
  1704. /* foolproof things by ensuring maxcount is not less than mincount */
  1705. maxcount = (maxcount < mincount) ? mincount : maxcount;
  1706. nbytes = sizeof(struct arg_file) /* storage for struct arg_file */
  1707. + sizeof(char *) * maxcount /* storage for filename[maxcount] array */
  1708. + sizeof(char *) * maxcount /* storage for basename[maxcount] array */
  1709. + sizeof(char *) * maxcount; /* storage for extension[maxcount] array */
  1710. result = (struct arg_file *)malloc(nbytes);
  1711. if (result)
  1712. {
  1713. int i;
  1714. /* init the arg_hdr struct */
  1715. result->hdr.flag = ARG_HASVALUE;
  1716. result->hdr.shortopts = shortopts;
  1717. result->hdr.longopts = longopts;
  1718. result->hdr.glossary = glossary;
  1719. result->hdr.datatype = datatype ? datatype : "<file>";
  1720. result->hdr.mincount = mincount;
  1721. result->hdr.maxcount = maxcount;
  1722. result->hdr.parent = result;
  1723. result->hdr.resetfn = (arg_resetfn *)arg_file_resetfn;
  1724. result->hdr.scanfn = (arg_scanfn *)arg_file_scanfn;
  1725. result->hdr.checkfn = (arg_checkfn *)arg_file_checkfn;
  1726. result->hdr.errorfn = (arg_errorfn *)arg_file_errorfn;
  1727. /* store the filename,basename,extension arrays immediately after the arg_file struct */
  1728. result->filename = (const char * *)(result + 1);
  1729. result->basename = result->filename + maxcount;
  1730. result->extension = result->basename + maxcount;
  1731. result->count = 0;
  1732. /* foolproof the string pointers by initialising them with empty strings */
  1733. for (i = 0; i < maxcount; i++)
  1734. {
  1735. result->filename[i] = "";
  1736. result->basename[i] = "";
  1737. result->extension[i] = "";
  1738. }
  1739. }
  1740. ARG_TRACE(("arg_filen() returns %p\n", result));
  1741. return result;
  1742. }
  1743. /*******************************************************************************
  1744. * This file is part of the argtable3 library.
  1745. *
  1746. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  1747. * <sheitmann@users.sourceforge.net>
  1748. * All rights reserved.
  1749. *
  1750. * Redistribution and use in source and binary forms, with or without
  1751. * modification, are permitted provided that the following conditions are met:
  1752. * * Redistributions of source code must retain the above copyright
  1753. * notice, this list of conditions and the following disclaimer.
  1754. * * Redistributions in binary form must reproduce the above copyright
  1755. * notice, this list of conditions and the following disclaimer in the
  1756. * documentation and/or other materials provided with the distribution.
  1757. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  1758. * may be used to endorse or promote products derived from this software
  1759. * without specific prior written permission.
  1760. *
  1761. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  1762. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1763. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1764. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  1765. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  1766. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  1767. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  1768. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  1769. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  1770. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1771. ******************************************************************************/
  1772. #include <stdlib.h>
  1773. #include <limits.h>
  1774. #include <ctype.h>
  1775. #include "argtable3.h"
  1776. static void arg_int_resetfn(struct arg_int *parent)
  1777. {
  1778. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  1779. parent->count = 0;
  1780. }
  1781. /* strtol0x() is like strtol() except that the numeric string is */
  1782. /* expected to be prefixed by "0X" where X is a user supplied char. */
  1783. /* The string may optionally be prefixed by white space and + or - */
  1784. /* as in +0X123 or -0X123. */
  1785. /* Once the prefix has been scanned, the remainder of the numeric */
  1786. /* string is converted using strtol() with the given base. */
  1787. /* eg: to parse hex str="-0X12324", specify X='X' and base=16. */
  1788. /* eg: to parse oct str="+0o12324", specify X='O' and base=8. */
  1789. /* eg: to parse bin str="-0B01010", specify X='B' and base=2. */
  1790. /* Failure of conversion is indicated by result where *endptr==str. */
  1791. static long int strtol0X(const char * str,
  1792. const char * *endptr,
  1793. char X,
  1794. int base)
  1795. {
  1796. long int val; /* stores result */
  1797. int s = 1; /* sign is +1 or -1 */
  1798. const char *ptr = str; /* ptr to current position in str */
  1799. /* skip leading whitespace */
  1800. while (isspace((int) *ptr))
  1801. ptr++;
  1802. /* printf("1) %s\n",ptr); */
  1803. /* scan optional sign character */
  1804. switch (*ptr)
  1805. {
  1806. case '+':
  1807. ptr++;
  1808. s = 1;
  1809. break;
  1810. case '-':
  1811. ptr++;
  1812. s = -1;
  1813. break;
  1814. default:
  1815. s = 1;
  1816. break;
  1817. }
  1818. /* printf("2) %s\n",ptr); */
  1819. /* '0X' prefix */
  1820. if ((*ptr++) != '0')
  1821. {
  1822. /* printf("failed to detect '0'\n"); */
  1823. *endptr = str;
  1824. return 0;
  1825. }
  1826. /* printf("3) %s\n",ptr); */
  1827. if (toupper((int) *ptr++) != toupper((int) X))
  1828. {
  1829. /* printf("failed to detect '%c'\n",X); */
  1830. *endptr = str;
  1831. return 0;
  1832. }
  1833. /* printf("4) %s\n",ptr); */
  1834. /* attempt conversion on remainder of string using strtol() */
  1835. val = strtol(ptr, (char * *)endptr, base);
  1836. if (*endptr == ptr)
  1837. {
  1838. /* conversion failed */
  1839. *endptr = str;
  1840. return 0;
  1841. }
  1842. /* success */
  1843. return s * val;
  1844. }
  1845. /* Returns 1 if str matches suffix (case insensitive). */
  1846. /* Str may contain trailing whitespace, but nothing else. */
  1847. static int detectsuffix(const char *str, const char *suffix)
  1848. {
  1849. /* scan pairwise through strings until mismatch detected */
  1850. while( toupper((int) *str) == toupper((int) *suffix) )
  1851. {
  1852. /* printf("'%c' '%c'\n", *str, *suffix); */
  1853. /* return 1 (success) if match persists until the string terminator */
  1854. if (*str == '\0')
  1855. return 1;
  1856. /* next chars */
  1857. str++;
  1858. suffix++;
  1859. }
  1860. /* printf("'%c' '%c' mismatch\n", *str, *suffix); */
  1861. /* return 0 (fail) if the matching did not consume the entire suffix */
  1862. if (*suffix != 0)
  1863. return 0; /* failed to consume entire suffix */
  1864. /* skip any remaining whitespace in str */
  1865. while (isspace((int) *str))
  1866. str++;
  1867. /* return 1 (success) if we have reached end of str else return 0 (fail) */
  1868. return (*str == '\0') ? 1 : 0;
  1869. }
  1870. static int arg_int_scanfn(struct arg_int *parent, const char *argval)
  1871. {
  1872. int errorcode = 0;
  1873. if (parent->count == parent->hdr.maxcount)
  1874. {
  1875. /* maximum number of arguments exceeded */
  1876. errorcode = EMAXCOUNT;
  1877. }
  1878. else if (!argval)
  1879. {
  1880. /* a valid argument with no argument value was given. */
  1881. /* This happens when an optional argument value was invoked. */
  1882. /* leave parent arguiment value unaltered but still count the argument. */
  1883. parent->count++;
  1884. }
  1885. else
  1886. {
  1887. long int val;
  1888. const char *end;
  1889. /* attempt to extract hex integer (eg: +0x123) from argval into val conversion */
  1890. val = strtol0X(argval, &end, 'X', 16);
  1891. if (end == argval)
  1892. {
  1893. /* hex failed, attempt octal conversion (eg +0o123) */
  1894. val = strtol0X(argval, &end, 'O', 8);
  1895. if (end == argval)
  1896. {
  1897. /* octal failed, attempt binary conversion (eg +0B101) */
  1898. val = strtol0X(argval, &end, 'B', 2);
  1899. if (end == argval)
  1900. {
  1901. /* binary failed, attempt decimal conversion with no prefix (eg 1234) */
  1902. val = strtol(argval, (char * *)&end, 10);
  1903. if (end == argval)
  1904. {
  1905. /* all supported number formats failed */
  1906. return EBADINT;
  1907. }
  1908. }
  1909. }
  1910. }
  1911. /* Safety check for integer overflow. WARNING: this check */
  1912. /* achieves nothing on machines where size(int)==size(long). */
  1913. if ( val > INT_MAX || val < INT_MIN )
  1914. errorcode = EOVERFLOW;
  1915. /* Detect any suffixes (KB,MB,GB) and multiply argument value appropriately. */
  1916. /* We need to be mindful of integer overflows when using such big numbers. */
  1917. if (detectsuffix(end, "KB")) /* kilobytes */
  1918. {
  1919. if ( val > (INT_MAX / 1024) || val < (INT_MIN / 1024) )
  1920. errorcode = EOVERFLOW; /* Overflow would occur if we proceed */
  1921. else
  1922. val *= 1024; /* 1KB = 1024 */
  1923. }
  1924. else if (detectsuffix(end, "MB")) /* megabytes */
  1925. {
  1926. if ( val > (INT_MAX / 1048576) || val < (INT_MIN / 1048576) )
  1927. errorcode = EOVERFLOW; /* Overflow would occur if we proceed */
  1928. else
  1929. val *= 1048576; /* 1MB = 1024*1024 */
  1930. }
  1931. else if (detectsuffix(end, "GB")) /* gigabytes */
  1932. {
  1933. if ( val > (INT_MAX / 1073741824) || val < (INT_MIN / 1073741824) )
  1934. errorcode = EOVERFLOW; /* Overflow would occur if we proceed */
  1935. else
  1936. val *= 1073741824; /* 1GB = 1024*1024*1024 */
  1937. }
  1938. else if (!detectsuffix(end, ""))
  1939. errorcode = EBADINT; /* invalid suffix detected */
  1940. /* if success then store result in parent->ival[] array */
  1941. if (errorcode == 0)
  1942. parent->ival[parent->count++] = val;
  1943. }
  1944. /* printf("%s:scanfn(%p,%p) returns %d\n",__FILE__,parent,argval,errorcode); */
  1945. return errorcode;
  1946. }
  1947. static int arg_int_checkfn(struct arg_int *parent)
  1948. {
  1949. int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
  1950. /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
  1951. return errorcode;
  1952. }
  1953. static void arg_int_errorfn(
  1954. struct arg_int *parent,
  1955. FILE *fp,
  1956. int errorcode,
  1957. const char *argval,
  1958. const char *progname)
  1959. {
  1960. const char *shortopts = parent->hdr.shortopts;
  1961. const char *longopts = parent->hdr.longopts;
  1962. const char *datatype = parent->hdr.datatype;
  1963. /* make argval NULL safe */
  1964. argval = argval ? argval : "";
  1965. fprintf(fp, "%s: ", progname);
  1966. switch(errorcode)
  1967. {
  1968. case EMINCOUNT:
  1969. fputs("missing option ", fp);
  1970. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  1971. break;
  1972. case EMAXCOUNT:
  1973. fputs("excess option ", fp);
  1974. arg_print_option(fp, shortopts, longopts, argval, "\n");
  1975. break;
  1976. case EBADINT:
  1977. fprintf(fp, "invalid argument \"%s\" to option ", argval);
  1978. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  1979. break;
  1980. case EOVERFLOW:
  1981. fputs("integer overflow at option ", fp);
  1982. arg_print_option(fp, shortopts, longopts, datatype, " ");
  1983. fprintf(fp, "(%s is too large)\n", argval);
  1984. break;
  1985. }
  1986. }
  1987. struct arg_int * arg_int0(
  1988. const char *shortopts,
  1989. const char *longopts,
  1990. const char *datatype,
  1991. const char *glossary)
  1992. {
  1993. return arg_intn(shortopts, longopts, datatype, 0, 1, glossary);
  1994. }
  1995. struct arg_int * arg_int1(
  1996. const char *shortopts,
  1997. const char *longopts,
  1998. const char *datatype,
  1999. const char *glossary)
  2000. {
  2001. return arg_intn(shortopts, longopts, datatype, 1, 1, glossary);
  2002. }
  2003. struct arg_int * arg_intn(
  2004. const char *shortopts,
  2005. const char *longopts,
  2006. const char *datatype,
  2007. int mincount,
  2008. int maxcount,
  2009. const char *glossary)
  2010. {
  2011. size_t nbytes;
  2012. struct arg_int *result;
  2013. /* foolproof things by ensuring maxcount is not less than mincount */
  2014. maxcount = (maxcount < mincount) ? mincount : maxcount;
  2015. nbytes = sizeof(struct arg_int) /* storage for struct arg_int */
  2016. + maxcount * sizeof(int); /* storage for ival[maxcount] array */
  2017. result = (struct arg_int *)malloc(nbytes);
  2018. if (result)
  2019. {
  2020. /* init the arg_hdr struct */
  2021. result->hdr.flag = ARG_HASVALUE;
  2022. result->hdr.shortopts = shortopts;
  2023. result->hdr.longopts = longopts;
  2024. result->hdr.datatype = datatype ? datatype : "<int>";
  2025. result->hdr.glossary = glossary;
  2026. result->hdr.mincount = mincount;
  2027. result->hdr.maxcount = maxcount;
  2028. result->hdr.parent = result;
  2029. result->hdr.resetfn = (arg_resetfn *)arg_int_resetfn;
  2030. result->hdr.scanfn = (arg_scanfn *)arg_int_scanfn;
  2031. result->hdr.checkfn = (arg_checkfn *)arg_int_checkfn;
  2032. result->hdr.errorfn = (arg_errorfn *)arg_int_errorfn;
  2033. /* store the ival[maxcount] array immediately after the arg_int struct */
  2034. result->ival = (int *)(result + 1);
  2035. result->count = 0;
  2036. }
  2037. ARG_TRACE(("arg_intn() returns %p\n", result));
  2038. return result;
  2039. }
  2040. /*******************************************************************************
  2041. * This file is part of the argtable3 library.
  2042. *
  2043. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  2044. * <sheitmann@users.sourceforge.net>
  2045. * All rights reserved.
  2046. *
  2047. * Redistribution and use in source and binary forms, with or without
  2048. * modification, are permitted provided that the following conditions are met:
  2049. * * Redistributions of source code must retain the above copyright
  2050. * notice, this list of conditions and the following disclaimer.
  2051. * * Redistributions in binary form must reproduce the above copyright
  2052. * notice, this list of conditions and the following disclaimer in the
  2053. * documentation and/or other materials provided with the distribution.
  2054. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  2055. * may be used to endorse or promote products derived from this software
  2056. * without specific prior written permission.
  2057. *
  2058. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  2059. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2060. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2061. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  2062. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2063. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2064. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2065. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2066. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2067. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2068. ******************************************************************************/
  2069. #include <stdlib.h>
  2070. #include "argtable3.h"
  2071. static void arg_lit_resetfn(struct arg_lit *parent)
  2072. {
  2073. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  2074. parent->count = 0;
  2075. }
  2076. static int arg_lit_scanfn(struct arg_lit *parent, const char *argval)
  2077. {
  2078. int errorcode = 0;
  2079. if (parent->count < parent->hdr.maxcount )
  2080. parent->count++;
  2081. else
  2082. errorcode = EMAXCOUNT;
  2083. ARG_TRACE(("%s:scanfn(%p,%s) returns %d\n", __FILE__, parent, argval,
  2084. errorcode));
  2085. return errorcode;
  2086. }
  2087. static int arg_lit_checkfn(struct arg_lit *parent)
  2088. {
  2089. int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
  2090. ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode));
  2091. return errorcode;
  2092. }
  2093. static void arg_lit_errorfn(
  2094. struct arg_lit *parent,
  2095. FILE *fp,
  2096. int errorcode,
  2097. const char *argval,
  2098. const char *progname)
  2099. {
  2100. const char *shortopts = parent->hdr.shortopts;
  2101. const char *longopts = parent->hdr.longopts;
  2102. const char *datatype = parent->hdr.datatype;
  2103. switch(errorcode)
  2104. {
  2105. case EMINCOUNT:
  2106. fprintf(fp, "%s: missing option ", progname);
  2107. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  2108. fprintf(fp, "\n");
  2109. break;
  2110. case EMAXCOUNT:
  2111. fprintf(fp, "%s: extraneous option ", progname);
  2112. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  2113. break;
  2114. }
  2115. ARG_TRACE(("%s:errorfn(%p, %p, %d, %s, %s)\n", __FILE__, parent, fp,
  2116. errorcode, argval, progname));
  2117. }
  2118. struct arg_lit * arg_lit0(
  2119. const char * shortopts,
  2120. const char * longopts,
  2121. const char * glossary)
  2122. {
  2123. return arg_litn(shortopts, longopts, 0, 1, glossary);
  2124. }
  2125. struct arg_lit * arg_lit1(
  2126. const char *shortopts,
  2127. const char *longopts,
  2128. const char *glossary)
  2129. {
  2130. return arg_litn(shortopts, longopts, 1, 1, glossary);
  2131. }
  2132. struct arg_lit * arg_litn(
  2133. const char *shortopts,
  2134. const char *longopts,
  2135. int mincount,
  2136. int maxcount,
  2137. const char *glossary)
  2138. {
  2139. struct arg_lit *result;
  2140. /* foolproof things by ensuring maxcount is not less than mincount */
  2141. maxcount = (maxcount < mincount) ? mincount : maxcount;
  2142. result = (struct arg_lit *)malloc(sizeof(struct arg_lit));
  2143. if (result)
  2144. {
  2145. /* init the arg_hdr struct */
  2146. result->hdr.flag = 0;
  2147. result->hdr.shortopts = shortopts;
  2148. result->hdr.longopts = longopts;
  2149. result->hdr.datatype = NULL;
  2150. result->hdr.glossary = glossary;
  2151. result->hdr.mincount = mincount;
  2152. result->hdr.maxcount = maxcount;
  2153. result->hdr.parent = result;
  2154. result->hdr.resetfn = (arg_resetfn *)arg_lit_resetfn;
  2155. result->hdr.scanfn = (arg_scanfn *)arg_lit_scanfn;
  2156. result->hdr.checkfn = (arg_checkfn *)arg_lit_checkfn;
  2157. result->hdr.errorfn = (arg_errorfn *)arg_lit_errorfn;
  2158. /* init local variables */
  2159. result->count = 0;
  2160. }
  2161. ARG_TRACE(("arg_litn() returns %p\n", result));
  2162. return result;
  2163. }
  2164. /*******************************************************************************
  2165. * This file is part of the argtable3 library.
  2166. *
  2167. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  2168. * <sheitmann@users.sourceforge.net>
  2169. * All rights reserved.
  2170. *
  2171. * Redistribution and use in source and binary forms, with or without
  2172. * modification, are permitted provided that the following conditions are met:
  2173. * * Redistributions of source code must retain the above copyright
  2174. * notice, this list of conditions and the following disclaimer.
  2175. * * Redistributions in binary form must reproduce the above copyright
  2176. * notice, this list of conditions and the following disclaimer in the
  2177. * documentation and/or other materials provided with the distribution.
  2178. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  2179. * may be used to endorse or promote products derived from this software
  2180. * without specific prior written permission.
  2181. *
  2182. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  2183. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2184. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2185. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  2186. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2187. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2188. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2189. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2190. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2191. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2192. ******************************************************************************/
  2193. #include <stdlib.h>
  2194. #include "argtable3.h"
  2195. struct arg_rem *arg_rem(const char *datatype, const char *glossary)
  2196. {
  2197. struct arg_rem *result = (struct arg_rem *)malloc(sizeof(struct arg_rem));
  2198. if (result)
  2199. {
  2200. result->hdr.flag = 0;
  2201. result->hdr.shortopts = NULL;
  2202. result->hdr.longopts = NULL;
  2203. result->hdr.datatype = datatype;
  2204. result->hdr.glossary = glossary;
  2205. result->hdr.mincount = 1;
  2206. result->hdr.maxcount = 1;
  2207. result->hdr.parent = result;
  2208. result->hdr.resetfn = NULL;
  2209. result->hdr.scanfn = NULL;
  2210. result->hdr.checkfn = NULL;
  2211. result->hdr.errorfn = NULL;
  2212. }
  2213. ARG_TRACE(("arg_rem() returns %p\n", result));
  2214. return result;
  2215. }
  2216. /*******************************************************************************
  2217. * This file is part of the argtable3 library.
  2218. *
  2219. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  2220. * <sheitmann@users.sourceforge.net>
  2221. * All rights reserved.
  2222. *
  2223. * Redistribution and use in source and binary forms, with or without
  2224. * modification, are permitted provided that the following conditions are met:
  2225. * * Redistributions of source code must retain the above copyright
  2226. * notice, this list of conditions and the following disclaimer.
  2227. * * Redistributions in binary form must reproduce the above copyright
  2228. * notice, this list of conditions and the following disclaimer in the
  2229. * documentation and/or other materials provided with the distribution.
  2230. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  2231. * may be used to endorse or promote products derived from this software
  2232. * without specific prior written permission.
  2233. *
  2234. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  2235. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2236. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2237. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  2238. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  2239. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  2240. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  2241. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  2242. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  2243. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  2244. ******************************************************************************/
  2245. #include <stdlib.h>
  2246. #include <string.h>
  2247. #include "argtable3.h"
  2248. #ifndef _TREX_H_
  2249. #define _TREX_H_
  2250. /***************************************************************
  2251. T-Rex a tiny regular expression library
  2252. Copyright (C) 2003-2006 Alberto Demichelis
  2253. This software is provided 'as-is', without any express
  2254. or implied warranty. In no event will the authors be held
  2255. liable for any damages arising from the use of this software.
  2256. Permission is granted to anyone to use this software for
  2257. any purpose, including commercial applications, and to alter
  2258. it and redistribute it freely, subject to the following restrictions:
  2259. 1. The origin of this software must not be misrepresented;
  2260. you must not claim that you wrote the original software.
  2261. If you use this software in a product, an acknowledgment
  2262. in the product documentation would be appreciated but
  2263. is not required.
  2264. 2. Altered source versions must be plainly marked as such,
  2265. and must not be misrepresented as being the original software.
  2266. 3. This notice may not be removed or altered from any
  2267. source distribution.
  2268. ****************************************************************/
  2269. #ifdef __cplusplus
  2270. extern "C" {
  2271. #endif
  2272. #ifdef _UNICODE
  2273. #define TRexChar unsigned short
  2274. #define MAX_CHAR 0xFFFF
  2275. #define _TREXC(c) L##c
  2276. #define trex_strlen wcslen
  2277. #define trex_printf wprintf
  2278. #else
  2279. #define TRexChar char
  2280. #define MAX_CHAR 0xFF
  2281. #define _TREXC(c) (c)
  2282. #define trex_strlen strlen
  2283. #define trex_printf printf
  2284. #endif
  2285. #ifndef TREX_API
  2286. #define TREX_API extern
  2287. #endif
  2288. #define TRex_True 1
  2289. #define TRex_False 0
  2290. #define TREX_ICASE ARG_REX_ICASE
  2291. typedef unsigned int TRexBool;
  2292. typedef struct TRex TRex;
  2293. typedef struct {
  2294. const TRexChar *begin;
  2295. int len;
  2296. } TRexMatch;
  2297. TREX_API TRex *trex_compile(const TRexChar *pattern, const TRexChar **error, int flags);
  2298. TREX_API void trex_free(TRex *exp);
  2299. TREX_API TRexBool trex_match(TRex* exp, const TRexChar* text);
  2300. TREX_API TRexBool trex_search(TRex* exp, const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end);
  2301. TREX_API TRexBool trex_searchrange(TRex* exp, const TRexChar* text_begin, const TRexChar* text_end, const TRexChar** out_begin, const TRexChar** out_end);
  2302. TREX_API int trex_getsubexpcount(TRex* exp);
  2303. TREX_API TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch *subexp);
  2304. #ifdef __cplusplus
  2305. }
  2306. #endif
  2307. #endif
  2308. struct privhdr
  2309. {
  2310. const char *pattern;
  2311. int flags;
  2312. };
  2313. static void arg_rex_resetfn(struct arg_rex *parent)
  2314. {
  2315. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  2316. parent->count = 0;
  2317. }
  2318. static int arg_rex_scanfn(struct arg_rex *parent, const char *argval)
  2319. {
  2320. int errorcode = 0;
  2321. const TRexChar *error = NULL;
  2322. TRex *rex = NULL;
  2323. TRexBool is_match = TRex_False;
  2324. if (parent->count == parent->hdr.maxcount )
  2325. {
  2326. /* maximum number of arguments exceeded */
  2327. errorcode = EMAXCOUNT;
  2328. }
  2329. else if (!argval)
  2330. {
  2331. /* a valid argument with no argument value was given. */
  2332. /* This happens when an optional argument value was invoked. */
  2333. /* leave parent argument value unaltered but still count the argument. */
  2334. parent->count++;
  2335. }
  2336. else
  2337. {
  2338. struct privhdr *priv = (struct privhdr *)parent->hdr.priv;
  2339. /* test the current argument value for a match with the regular expression */
  2340. /* if a match is detected, record the argument value in the arg_rex struct */
  2341. rex = trex_compile(priv->pattern, &error, priv->flags);
  2342. is_match = trex_match(rex, argval);
  2343. if (!is_match)
  2344. errorcode = EREGNOMATCH;
  2345. else
  2346. parent->sval[parent->count++] = argval;
  2347. trex_free(rex);
  2348. }
  2349. ARG_TRACE(("%s:scanfn(%p) returns %d\n",__FILE__,parent,errorcode));
  2350. return errorcode;
  2351. }
  2352. static int arg_rex_checkfn(struct arg_rex *parent)
  2353. {
  2354. int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
  2355. //struct privhdr *priv = (struct privhdr*)parent->hdr.priv;
  2356. /* free the regex "program" we constructed in resetfn */
  2357. //regfree(&(priv->regex));
  2358. /*printf("%s:checkfn(%p) returns %d\n",__FILE__,parent,errorcode);*/
  2359. return errorcode;
  2360. }
  2361. static void arg_rex_errorfn(struct arg_rex *parent,
  2362. FILE *fp,
  2363. int errorcode,
  2364. const char *argval,
  2365. const char *progname)
  2366. {
  2367. const char *shortopts = parent->hdr.shortopts;
  2368. const char *longopts = parent->hdr.longopts;
  2369. const char *datatype = parent->hdr.datatype;
  2370. /* make argval NULL safe */
  2371. argval = argval ? argval : "";
  2372. fprintf(fp, "%s: ", progname);
  2373. switch(errorcode)
  2374. {
  2375. case EMINCOUNT:
  2376. fputs("missing option ", fp);
  2377. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  2378. break;
  2379. case EMAXCOUNT:
  2380. fputs("excess option ", fp);
  2381. arg_print_option(fp, shortopts, longopts, argval, "\n");
  2382. break;
  2383. case EREGNOMATCH:
  2384. fputs("illegal value ", fp);
  2385. arg_print_option(fp, shortopts, longopts, argval, "\n");
  2386. break;
  2387. default:
  2388. {
  2389. //char errbuff[256];
  2390. //regerror(errorcode, NULL, errbuff, sizeof(errbuff));
  2391. //printf("%s\n", errbuff);
  2392. }
  2393. break;
  2394. }
  2395. }
  2396. struct arg_rex * arg_rex0(const char * shortopts,
  2397. const char * longopts,
  2398. const char * pattern,
  2399. const char *datatype,
  2400. int flags,
  2401. const char *glossary)
  2402. {
  2403. return arg_rexn(shortopts,
  2404. longopts,
  2405. pattern,
  2406. datatype,
  2407. 0,
  2408. 1,
  2409. flags,
  2410. glossary);
  2411. }
  2412. struct arg_rex * arg_rex1(const char * shortopts,
  2413. const char * longopts,
  2414. const char * pattern,
  2415. const char *datatype,
  2416. int flags,
  2417. const char *glossary)
  2418. {
  2419. return arg_rexn(shortopts,
  2420. longopts,
  2421. pattern,
  2422. datatype,
  2423. 1,
  2424. 1,
  2425. flags,
  2426. glossary);
  2427. }
  2428. struct arg_rex * arg_rexn(const char * shortopts,
  2429. const char * longopts,
  2430. const char * pattern,
  2431. const char *datatype,
  2432. int mincount,
  2433. int maxcount,
  2434. int flags,
  2435. const char *glossary)
  2436. {
  2437. size_t nbytes;
  2438. struct arg_rex *result;
  2439. struct privhdr *priv;
  2440. int i;
  2441. const TRexChar *error = NULL;
  2442. TRex *rex = NULL;
  2443. if (!pattern)
  2444. {
  2445. printf(
  2446. "argtable: ERROR - illegal regular expression pattern \"(NULL)\"\n");
  2447. printf("argtable: Bad argument table.\n");
  2448. return NULL;
  2449. }
  2450. /* foolproof things by ensuring maxcount is not less than mincount */
  2451. maxcount = (maxcount < mincount) ? mincount : maxcount;
  2452. nbytes = sizeof(struct arg_rex) /* storage for struct arg_rex */
  2453. + sizeof(struct privhdr) /* storage for private arg_rex data */
  2454. + maxcount * sizeof(char *); /* storage for sval[maxcount] array */
  2455. result = (struct arg_rex *)malloc(nbytes);
  2456. if (result == NULL)
  2457. return result;
  2458. /* init the arg_hdr struct */
  2459. result->hdr.flag = ARG_HASVALUE;
  2460. result->hdr.shortopts = shortopts;
  2461. result->hdr.longopts = longopts;
  2462. result->hdr.datatype = datatype ? datatype : pattern;
  2463. result->hdr.glossary = glossary;
  2464. result->hdr.mincount = mincount;
  2465. result->hdr.maxcount = maxcount;
  2466. result->hdr.parent = result;
  2467. result->hdr.resetfn = (arg_resetfn *)arg_rex_resetfn;
  2468. result->hdr.scanfn = (arg_scanfn *)arg_rex_scanfn;
  2469. result->hdr.checkfn = (arg_checkfn *)arg_rex_checkfn;
  2470. result->hdr.errorfn = (arg_errorfn *)arg_rex_errorfn;
  2471. /* store the arg_rex_priv struct immediately after the arg_rex struct */
  2472. result->hdr.priv = result + 1;
  2473. priv = (struct privhdr *)(result->hdr.priv);
  2474. priv->pattern = pattern;
  2475. priv->flags = flags;
  2476. /* store the sval[maxcount] array immediately after the arg_rex_priv struct */
  2477. result->sval = (const char * *)(priv + 1);
  2478. result->count = 0;
  2479. /* foolproof the string pointers by initializing them to reference empty strings */
  2480. for (i = 0; i < maxcount; i++)
  2481. result->sval[i] = "";
  2482. /* here we construct and destroy a regex representation of the regular
  2483. * expression for no other reason than to force any regex errors to be
  2484. * trapped now rather than later. If we don't, then errors may go undetected
  2485. * until an argument is actually parsed.
  2486. */
  2487. rex = trex_compile(priv->pattern, &error, priv->flags);
  2488. if (rex == NULL)
  2489. {
  2490. ARG_LOG(("argtable: %s \"%s\"\n", error ? error : _TREXC("undefined"), priv->pattern));
  2491. ARG_LOG(("argtable: Bad argument table.\n"));
  2492. }
  2493. trex_free(rex);
  2494. ARG_TRACE(("arg_rexn() returns %p\n", result));
  2495. return result;
  2496. }
  2497. /* see copyright notice in trex.h */
  2498. #include <string.h>
  2499. #include <stdlib.h>
  2500. #include <ctype.h>
  2501. #include <setjmp.h>
  2502. #ifdef _UINCODE
  2503. #define scisprint iswprint
  2504. #define scstrlen wcslen
  2505. #define scprintf wprintf
  2506. #define _SC(x) L(x)
  2507. #else
  2508. #define scisprint isprint
  2509. #define scstrlen strlen
  2510. #define scprintf printf
  2511. #define _SC(x) (x)
  2512. #endif
  2513. #ifdef _DEBUG
  2514. #include <stdio.h>
  2515. static const TRexChar *g_nnames[] =
  2516. {
  2517. _SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"),
  2518. _SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"),
  2519. _SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"),
  2520. _SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB")
  2521. };
  2522. #endif
  2523. #define OP_GREEDY (MAX_CHAR+1) // * + ? {n}
  2524. #define OP_OR (MAX_CHAR+2)
  2525. #define OP_EXPR (MAX_CHAR+3) //parentesis ()
  2526. #define OP_NOCAPEXPR (MAX_CHAR+4) //parentesis (?:)
  2527. #define OP_DOT (MAX_CHAR+5)
  2528. #define OP_CLASS (MAX_CHAR+6)
  2529. #define OP_CCLASS (MAX_CHAR+7)
  2530. #define OP_NCLASS (MAX_CHAR+8) //negates class the [^
  2531. #define OP_RANGE (MAX_CHAR+9)
  2532. #define OP_CHAR (MAX_CHAR+10)
  2533. #define OP_EOL (MAX_CHAR+11)
  2534. #define OP_BOL (MAX_CHAR+12)
  2535. #define OP_WB (MAX_CHAR+13)
  2536. #define TREX_SYMBOL_ANY_CHAR ('.')
  2537. #define TREX_SYMBOL_GREEDY_ONE_OR_MORE ('+')
  2538. #define TREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*')
  2539. #define TREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?')
  2540. #define TREX_SYMBOL_BRANCH ('|')
  2541. #define TREX_SYMBOL_END_OF_STRING ('$')
  2542. #define TREX_SYMBOL_BEGINNING_OF_STRING ('^')
  2543. #define TREX_SYMBOL_ESCAPE_CHAR ('\\')
  2544. typedef int TRexNodeType;
  2545. typedef struct tagTRexNode{
  2546. TRexNodeType type;
  2547. int left;
  2548. int right;
  2549. int next;
  2550. }TRexNode;
  2551. struct TRex{
  2552. const TRexChar *_eol;
  2553. const TRexChar *_bol;
  2554. const TRexChar *_p;
  2555. int _first;
  2556. int _op;
  2557. TRexNode *_nodes;
  2558. int _nallocated;
  2559. int _nsize;
  2560. int _nsubexpr;
  2561. TRexMatch *_matches;
  2562. int _currsubexp;
  2563. void *_jmpbuf;
  2564. const TRexChar **_error;
  2565. int _flags;
  2566. };
  2567. static int trex_list(TRex *exp);
  2568. static int trex_newnode(TRex *exp, TRexNodeType type)
  2569. {
  2570. TRexNode n;
  2571. int newid;
  2572. n.type = type;
  2573. n.next = n.right = n.left = -1;
  2574. if(type == OP_EXPR)
  2575. n.right = exp->_nsubexpr++;
  2576. if(exp->_nallocated < (exp->_nsize + 1)) {
  2577. exp->_nallocated *= 2;
  2578. exp->_nodes = (TRexNode *)realloc(exp->_nodes, exp->_nallocated * sizeof(TRexNode));
  2579. }
  2580. exp->_nodes[exp->_nsize++] = n; // NOLINT(clang-analyzer-unix.Malloc)
  2581. newid = exp->_nsize - 1;
  2582. return (int)newid;
  2583. }
  2584. static void trex_error(TRex *exp,const TRexChar *error)
  2585. {
  2586. if(exp->_error) *exp->_error = error;
  2587. longjmp(*((jmp_buf*)exp->_jmpbuf),-1);
  2588. }
  2589. static void trex_expect(TRex *exp, int n){
  2590. if((*exp->_p) != n)
  2591. trex_error(exp, _SC("expected paren"));
  2592. exp->_p++;
  2593. }
  2594. static TRexChar trex_escapechar(TRex *exp)
  2595. {
  2596. if(*exp->_p == TREX_SYMBOL_ESCAPE_CHAR){
  2597. exp->_p++;
  2598. switch(*exp->_p) {
  2599. case 'v': exp->_p++; return '\v';
  2600. case 'n': exp->_p++; return '\n';
  2601. case 't': exp->_p++; return '\t';
  2602. case 'r': exp->_p++; return '\r';
  2603. case 'f': exp->_p++; return '\f';
  2604. default: return (*exp->_p++);
  2605. }
  2606. } else if(!scisprint((int) *exp->_p)) trex_error(exp,_SC("letter expected"));
  2607. return (*exp->_p++);
  2608. }
  2609. static int trex_charclass(TRex *exp,int classid)
  2610. {
  2611. int n = trex_newnode(exp,OP_CCLASS);
  2612. exp->_nodes[n].left = classid;
  2613. return n;
  2614. }
  2615. static int trex_charnode(TRex *exp,TRexBool isclass)
  2616. {
  2617. TRexChar t;
  2618. if(*exp->_p == TREX_SYMBOL_ESCAPE_CHAR) {
  2619. exp->_p++;
  2620. switch(*exp->_p) {
  2621. case 'n': exp->_p++; return trex_newnode(exp,'\n');
  2622. case 't': exp->_p++; return trex_newnode(exp,'\t');
  2623. case 'r': exp->_p++; return trex_newnode(exp,'\r');
  2624. case 'f': exp->_p++; return trex_newnode(exp,'\f');
  2625. case 'v': exp->_p++; return trex_newnode(exp,'\v');
  2626. case 'a': case 'A': case 'w': case 'W': case 's': case 'S':
  2627. case 'd': case 'D': case 'x': case 'X': case 'c': case 'C':
  2628. case 'p': case 'P': case 'l': case 'u':
  2629. {
  2630. t = *exp->_p; exp->_p++;
  2631. return trex_charclass(exp,t);
  2632. }
  2633. case 'b':
  2634. case 'B':
  2635. if(!isclass) {
  2636. int node = trex_newnode(exp,OP_WB);
  2637. exp->_nodes[node].left = *exp->_p;
  2638. exp->_p++;
  2639. return node;
  2640. } //else default
  2641. /* falls through */
  2642. default:
  2643. t = *exp->_p; exp->_p++;
  2644. return trex_newnode(exp,t);
  2645. }
  2646. }
  2647. else if(!scisprint((int) *exp->_p)) {
  2648. trex_error(exp,_SC("letter expected"));
  2649. }
  2650. t = *exp->_p; exp->_p++;
  2651. return trex_newnode(exp,t);
  2652. }
  2653. static int trex_class(TRex *exp)
  2654. {
  2655. int ret = -1;
  2656. int first = -1,chain;
  2657. if(*exp->_p == TREX_SYMBOL_BEGINNING_OF_STRING){
  2658. ret = trex_newnode(exp,OP_NCLASS);
  2659. exp->_p++;
  2660. }else ret = trex_newnode(exp,OP_CLASS);
  2661. if(*exp->_p == ']') trex_error(exp,_SC("empty class"));
  2662. chain = ret;
  2663. while(*exp->_p != ']' && exp->_p != exp->_eol) {
  2664. if(*exp->_p == '-' && first != -1){
  2665. int r,t;
  2666. if(*exp->_p++ == ']') trex_error(exp,_SC("unfinished range"));
  2667. r = trex_newnode(exp,OP_RANGE);
  2668. if(first>*exp->_p) trex_error(exp,_SC("invalid range"));
  2669. if(exp->_nodes[first].type == OP_CCLASS) trex_error(exp,_SC("cannot use character classes in ranges"));
  2670. exp->_nodes[r].left = exp->_nodes[first].type;
  2671. t = trex_escapechar(exp);
  2672. exp->_nodes[r].right = t;
  2673. exp->_nodes[chain].next = r;
  2674. chain = r;
  2675. first = -1;
  2676. }
  2677. else{
  2678. if(first!=-1){
  2679. int c = first;
  2680. exp->_nodes[chain].next = c;
  2681. chain = c;
  2682. first = trex_charnode(exp,TRex_True);
  2683. }
  2684. else{
  2685. first = trex_charnode(exp,TRex_True);
  2686. }
  2687. }
  2688. }
  2689. if(first!=-1){
  2690. int c = first;
  2691. exp->_nodes[chain].next = c;
  2692. chain = c;
  2693. first = -1;
  2694. }
  2695. /* hack? */
  2696. exp->_nodes[ret].left = exp->_nodes[ret].next;
  2697. exp->_nodes[ret].next = -1;
  2698. return ret;
  2699. }
  2700. static int trex_parsenumber(TRex *exp)
  2701. {
  2702. int ret = *exp->_p-'0';
  2703. int positions = 10;
  2704. exp->_p++;
  2705. while(isdigit((int) *exp->_p)) {
  2706. ret = ret*10+(*exp->_p++-'0');
  2707. if(positions==1000000000) trex_error(exp,_SC("overflow in numeric constant"));
  2708. positions *= 10;
  2709. };
  2710. return ret;
  2711. }
  2712. static int trex_element(TRex *exp)
  2713. {
  2714. int ret = -1;
  2715. switch(*exp->_p)
  2716. {
  2717. case '(': {
  2718. int expr,newn;
  2719. exp->_p++;
  2720. if(*exp->_p =='?') {
  2721. exp->_p++;
  2722. trex_expect(exp,':');
  2723. expr = trex_newnode(exp,OP_NOCAPEXPR);
  2724. }
  2725. else
  2726. expr = trex_newnode(exp,OP_EXPR);
  2727. newn = trex_list(exp);
  2728. exp->_nodes[expr].left = newn;
  2729. ret = expr;
  2730. trex_expect(exp,')');
  2731. }
  2732. break;
  2733. case '[':
  2734. exp->_p++;
  2735. ret = trex_class(exp);
  2736. trex_expect(exp,']');
  2737. break;
  2738. case TREX_SYMBOL_END_OF_STRING: exp->_p++; ret = trex_newnode(exp,OP_EOL);break;
  2739. case TREX_SYMBOL_ANY_CHAR: exp->_p++; ret = trex_newnode(exp,OP_DOT);break;
  2740. default:
  2741. ret = trex_charnode(exp,TRex_False);
  2742. break;
  2743. }
  2744. {
  2745. TRexBool isgreedy = TRex_False;
  2746. unsigned short p0 = 0, p1 = 0;
  2747. switch(*exp->_p){
  2748. case TREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; isgreedy = TRex_True; break;
  2749. case TREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; isgreedy = TRex_True; break;
  2750. case TREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; isgreedy = TRex_True; break;
  2751. case '{':
  2752. exp->_p++;
  2753. if(!isdigit((int) *exp->_p)) trex_error(exp,_SC("number expected"));
  2754. p0 = (unsigned short)trex_parsenumber(exp);
  2755. /*******************************/
  2756. switch(*exp->_p) {
  2757. case '}':
  2758. p1 = p0; exp->_p++;
  2759. break;
  2760. case ',':
  2761. exp->_p++;
  2762. p1 = 0xFFFF;
  2763. if(isdigit((int) *exp->_p)){
  2764. p1 = (unsigned short)trex_parsenumber(exp);
  2765. }
  2766. trex_expect(exp,'}');
  2767. break;
  2768. default:
  2769. trex_error(exp,_SC(", or } expected"));
  2770. }
  2771. /*******************************/
  2772. isgreedy = TRex_True;
  2773. break;
  2774. }
  2775. if(isgreedy) {
  2776. int nnode = trex_newnode(exp,OP_GREEDY);
  2777. exp->_nodes[nnode].left = ret;
  2778. exp->_nodes[nnode].right = ((p0)<<16)|p1;
  2779. ret = nnode;
  2780. }
  2781. }
  2782. if((*exp->_p != TREX_SYMBOL_BRANCH) && (*exp->_p != ')') && (*exp->_p != TREX_SYMBOL_GREEDY_ZERO_OR_MORE) && (*exp->_p != TREX_SYMBOL_GREEDY_ONE_OR_MORE) && (*exp->_p != '\0')) {
  2783. int nnode = trex_element(exp);
  2784. exp->_nodes[ret].next = nnode;
  2785. }
  2786. return ret;
  2787. }
  2788. static int trex_list(TRex *exp)
  2789. {
  2790. int ret=-1,e;
  2791. if(*exp->_p == TREX_SYMBOL_BEGINNING_OF_STRING) {
  2792. exp->_p++;
  2793. ret = trex_newnode(exp,OP_BOL);
  2794. }
  2795. e = trex_element(exp);
  2796. if(ret != -1) {
  2797. exp->_nodes[ret].next = e;
  2798. }
  2799. else ret = e;
  2800. if(*exp->_p == TREX_SYMBOL_BRANCH) {
  2801. int temp,tright;
  2802. exp->_p++;
  2803. temp = trex_newnode(exp,OP_OR);
  2804. exp->_nodes[temp].left = ret;
  2805. tright = trex_list(exp);
  2806. exp->_nodes[temp].right = tright;
  2807. ret = temp;
  2808. }
  2809. return ret;
  2810. }
  2811. static TRexBool trex_matchcclass(int cclass,TRexChar ch)
  2812. {
  2813. int c = ch;
  2814. switch(cclass) {
  2815. case 'a': return isalpha(c)?TRex_True:TRex_False;
  2816. case 'A': return !isalpha(c)?TRex_True:TRex_False;
  2817. case 'w': return (isalnum(c) || c == '_')?TRex_True:TRex_False;
  2818. case 'W': return (!isalnum(c) && c != '_')?TRex_True:TRex_False;
  2819. case 's': return isspace(c)?TRex_True:TRex_False;
  2820. case 'S': return !isspace(c)?TRex_True:TRex_False;
  2821. case 'd': return isdigit(c)?TRex_True:TRex_False;
  2822. case 'D': return !isdigit(c)?TRex_True:TRex_False;
  2823. case 'x': return isxdigit(c)?TRex_True:TRex_False;
  2824. case 'X': return !isxdigit(c)?TRex_True:TRex_False;
  2825. case 'c': return iscntrl(c)?TRex_True:TRex_False;
  2826. case 'C': return !iscntrl(c)?TRex_True:TRex_False;
  2827. case 'p': return ispunct(c)?TRex_True:TRex_False;
  2828. case 'P': return !ispunct(c)?TRex_True:TRex_False;
  2829. case 'l': return islower(c)?TRex_True:TRex_False;
  2830. case 'u': return isupper(c)?TRex_True:TRex_False;
  2831. }
  2832. return TRex_False; /*cannot happen*/
  2833. }
  2834. static TRexBool trex_matchclass(TRex* exp,TRexNode *node,TRexChar c)
  2835. {
  2836. do {
  2837. switch(node->type) {
  2838. case OP_RANGE:
  2839. if (exp->_flags & TREX_ICASE)
  2840. {
  2841. if(c >= toupper(node->left) && c <= toupper(node->right)) return TRex_True;
  2842. if(c >= tolower(node->left) && c <= tolower(node->right)) return TRex_True;
  2843. }
  2844. else
  2845. {
  2846. if(c >= node->left && c <= node->right) return TRex_True;
  2847. }
  2848. break;
  2849. case OP_CCLASS:
  2850. if(trex_matchcclass(node->left,c)) return TRex_True;
  2851. break;
  2852. default:
  2853. if (exp->_flags & TREX_ICASE)
  2854. {
  2855. if (c == tolower(node->type) || c == toupper(node->type)) return TRex_True;
  2856. }
  2857. else
  2858. {
  2859. if(c == node->type)return TRex_True;
  2860. }
  2861. }
  2862. } while((node->next != -1) && (node = &exp->_nodes[node->next]));
  2863. return TRex_False;
  2864. }
  2865. static const TRexChar *trex_matchnode(TRex* exp,TRexNode *node,const TRexChar *str,TRexNode *next)
  2866. {
  2867. TRexNodeType type = node->type;
  2868. switch(type) {
  2869. case OP_GREEDY: {
  2870. //TRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL;
  2871. TRexNode *greedystop = NULL;
  2872. int p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0;
  2873. const TRexChar *s=str, *good = str;
  2874. if(node->next != -1) {
  2875. greedystop = &exp->_nodes[node->next];
  2876. }
  2877. else {
  2878. greedystop = next;
  2879. }
  2880. while((nmaches == 0xFFFF || nmaches < p1)) {
  2881. const TRexChar *stop;
  2882. if(!(s = trex_matchnode(exp,&exp->_nodes[node->left],s,greedystop)))
  2883. break;
  2884. nmaches++;
  2885. good=s;
  2886. if(greedystop) {
  2887. //checks that 0 matches satisfy the expression(if so skips)
  2888. //if not would always stop(for instance if is a '?')
  2889. if(greedystop->type != OP_GREEDY ||
  2890. (greedystop->type == OP_GREEDY && ((greedystop->right >> 16)&0x0000FFFF) != 0))
  2891. {
  2892. TRexNode *gnext = NULL;
  2893. if(greedystop->next != -1) {
  2894. gnext = &exp->_nodes[greedystop->next];
  2895. }else if(next && next->next != -1){
  2896. gnext = &exp->_nodes[next->next];
  2897. }
  2898. stop = trex_matchnode(exp,greedystop,s,gnext);
  2899. if(stop) {
  2900. //if satisfied stop it
  2901. if(p0 == p1 && p0 == nmaches) break;
  2902. else if(nmaches >= p0 && p1 == 0xFFFF) break;
  2903. else if(nmaches >= p0 && nmaches <= p1) break;
  2904. }
  2905. }
  2906. }
  2907. if(s >= exp->_eol)
  2908. break;
  2909. }
  2910. if(p0 == p1 && p0 == nmaches) return good;
  2911. else if(nmaches >= p0 && p1 == 0xFFFF) return good;
  2912. else if(nmaches >= p0 && nmaches <= p1) return good;
  2913. return NULL;
  2914. }
  2915. case OP_OR: {
  2916. const TRexChar *asd = str;
  2917. TRexNode *temp=&exp->_nodes[node->left];
  2918. while( (asd = trex_matchnode(exp,temp,asd,NULL)) ) {
  2919. if(temp->next != -1)
  2920. temp = &exp->_nodes[temp->next];
  2921. else
  2922. return asd;
  2923. }
  2924. asd = str;
  2925. temp = &exp->_nodes[node->right];
  2926. while( (asd = trex_matchnode(exp,temp,asd,NULL)) ) {
  2927. if(temp->next != -1)
  2928. temp = &exp->_nodes[temp->next];
  2929. else
  2930. return asd;
  2931. }
  2932. return NULL;
  2933. break;
  2934. }
  2935. case OP_EXPR:
  2936. case OP_NOCAPEXPR:{
  2937. TRexNode *n = &exp->_nodes[node->left];
  2938. const TRexChar *cur = str;
  2939. int capture = -1;
  2940. if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) {
  2941. capture = exp->_currsubexp;
  2942. exp->_matches[capture].begin = cur;
  2943. exp->_currsubexp++;
  2944. }
  2945. do {
  2946. TRexNode *subnext = NULL;
  2947. if(n->next != -1) {
  2948. subnext = &exp->_nodes[n->next];
  2949. }else {
  2950. subnext = next;
  2951. }
  2952. if(!(cur = trex_matchnode(exp,n,cur,subnext))) {
  2953. if(capture != -1){
  2954. exp->_matches[capture].begin = 0;
  2955. exp->_matches[capture].len = 0;
  2956. }
  2957. return NULL;
  2958. }
  2959. } while((n->next != -1) && (n = &exp->_nodes[n->next]));
  2960. if(capture != -1)
  2961. exp->_matches[capture].len = cur - exp->_matches[capture].begin;
  2962. return cur;
  2963. }
  2964. case OP_WB:
  2965. if((str == exp->_bol && !isspace((int) *str))
  2966. || ((str == exp->_eol && !isspace((int) *(str-1))))
  2967. || ((!isspace((int) *str) && isspace((int) *(str+1))))
  2968. || ((isspace((int) *str) && !isspace((int) *(str+1)))) ) {
  2969. return (node->left == 'b')?str:NULL;
  2970. }
  2971. return (node->left == 'b')?NULL:str;
  2972. case OP_BOL:
  2973. if(str == exp->_bol) return str;
  2974. return NULL;
  2975. case OP_EOL:
  2976. if(str == exp->_eol) return str;
  2977. return NULL;
  2978. case OP_DOT:
  2979. str++;
  2980. return str;
  2981. case OP_NCLASS:
  2982. case OP_CLASS:
  2983. if(trex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?TRex_True:TRex_False):(type == OP_NCLASS?TRex_True:TRex_False)) {
  2984. str++;
  2985. return str;
  2986. }
  2987. return NULL;
  2988. case OP_CCLASS:
  2989. if(trex_matchcclass(node->left,*str)) {
  2990. str++;
  2991. return str;
  2992. }
  2993. return NULL;
  2994. default: /* char */
  2995. if (exp->_flags & TREX_ICASE)
  2996. {
  2997. if(*str != tolower(node->type) && *str != toupper(node->type)) return NULL;
  2998. }
  2999. else
  3000. {
  3001. if (*str != node->type) return NULL;
  3002. }
  3003. str++;
  3004. return str;
  3005. }
  3006. return NULL;
  3007. }
  3008. /* public api */
  3009. TRex *trex_compile(const TRexChar *pattern,const TRexChar **error,int flags)
  3010. {
  3011. TRex *exp = (TRex *)malloc(sizeof(TRex));
  3012. exp->_eol = exp->_bol = NULL;
  3013. exp->_p = pattern;
  3014. exp->_nallocated = (int)scstrlen(pattern) * sizeof(TRexChar);
  3015. exp->_nodes = (TRexNode *)malloc(exp->_nallocated * sizeof(TRexNode));
  3016. exp->_nsize = 0;
  3017. exp->_matches = 0;
  3018. exp->_nsubexpr = 0;
  3019. exp->_first = trex_newnode(exp,OP_EXPR);
  3020. exp->_error = error;
  3021. exp->_jmpbuf = malloc(sizeof(jmp_buf));
  3022. exp->_flags = flags;
  3023. if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) {
  3024. int res = trex_list(exp);
  3025. exp->_nodes[exp->_first].left = res;
  3026. if(*exp->_p!='\0')
  3027. trex_error(exp,_SC("unexpected character"));
  3028. #ifdef _DEBUG
  3029. {
  3030. int nsize,i;
  3031. TRexNode *t;
  3032. nsize = exp->_nsize;
  3033. t = &exp->_nodes[0];
  3034. scprintf(_SC("\n"));
  3035. for(i = 0;i < nsize; i++) {
  3036. if(exp->_nodes[i].type>MAX_CHAR)
  3037. scprintf(_SC("[%02d] %10s "),i,g_nnames[exp->_nodes[i].type-MAX_CHAR]);
  3038. else
  3039. scprintf(_SC("[%02d] %10c "),i,exp->_nodes[i].type);
  3040. scprintf(_SC("left %02d right %02d next %02d\n"),exp->_nodes[i].left,exp->_nodes[i].right,exp->_nodes[i].next);
  3041. }
  3042. scprintf(_SC("\n"));
  3043. }
  3044. #endif
  3045. exp->_matches = (TRexMatch *) malloc(exp->_nsubexpr * sizeof(TRexMatch));
  3046. memset(exp->_matches,0,exp->_nsubexpr * sizeof(TRexMatch));
  3047. }
  3048. else{
  3049. trex_free(exp);
  3050. return NULL;
  3051. }
  3052. return exp;
  3053. }
  3054. void trex_free(TRex *exp)
  3055. {
  3056. if(exp) {
  3057. if(exp->_nodes) free(exp->_nodes);
  3058. if(exp->_jmpbuf) free(exp->_jmpbuf);
  3059. if(exp->_matches) free(exp->_matches);
  3060. free(exp);
  3061. }
  3062. }
  3063. TRexBool trex_match(TRex* exp,const TRexChar* text)
  3064. {
  3065. const TRexChar* res = NULL;
  3066. exp->_bol = text;
  3067. exp->_eol = text + scstrlen(text);
  3068. exp->_currsubexp = 0;
  3069. res = trex_matchnode(exp,exp->_nodes,text,NULL);
  3070. if(res == NULL || res != exp->_eol)
  3071. return TRex_False;
  3072. return TRex_True;
  3073. }
  3074. TRexBool trex_searchrange(TRex* exp,const TRexChar* text_begin,const TRexChar* text_end,const TRexChar** out_begin, const TRexChar** out_end)
  3075. {
  3076. const TRexChar *cur = NULL;
  3077. int node = exp->_first;
  3078. if(text_begin >= text_end) return TRex_False;
  3079. exp->_bol = text_begin;
  3080. exp->_eol = text_end;
  3081. do {
  3082. cur = text_begin;
  3083. while(node != -1) {
  3084. exp->_currsubexp = 0;
  3085. cur = trex_matchnode(exp,&exp->_nodes[node],cur,NULL);
  3086. if(!cur)
  3087. break;
  3088. node = exp->_nodes[node].next;
  3089. }
  3090. text_begin++;
  3091. } while(cur == NULL && text_begin != text_end);
  3092. if(cur == NULL)
  3093. return TRex_False;
  3094. --text_begin;
  3095. if(out_begin) *out_begin = text_begin;
  3096. if(out_end) *out_end = cur;
  3097. return TRex_True;
  3098. }
  3099. TRexBool trex_search(TRex* exp,const TRexChar* text, const TRexChar** out_begin, const TRexChar** out_end)
  3100. {
  3101. return trex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end);
  3102. }
  3103. int trex_getsubexpcount(TRex* exp)
  3104. {
  3105. return exp->_nsubexpr;
  3106. }
  3107. TRexBool trex_getsubexp(TRex* exp, int n, TRexMatch *subexp)
  3108. {
  3109. if( n<0 || n >= exp->_nsubexpr) return TRex_False;
  3110. *subexp = exp->_matches[n];
  3111. return TRex_True;
  3112. }
  3113. /*******************************************************************************
  3114. * This file is part of the argtable3 library.
  3115. *
  3116. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  3117. * <sheitmann@users.sourceforge.net>
  3118. * All rights reserved.
  3119. *
  3120. * Redistribution and use in source and binary forms, with or without
  3121. * modification, are permitted provided that the following conditions are met:
  3122. * * Redistributions of source code must retain the above copyright
  3123. * notice, this list of conditions and the following disclaimer.
  3124. * * Redistributions in binary form must reproduce the above copyright
  3125. * notice, this list of conditions and the following disclaimer in the
  3126. * documentation and/or other materials provided with the distribution.
  3127. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  3128. * may be used to endorse or promote products derived from this software
  3129. * without specific prior written permission.
  3130. *
  3131. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  3132. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  3133. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  3134. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  3135. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3136. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3137. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  3138. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3139. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3140. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3141. ******************************************************************************/
  3142. #include <stdlib.h>
  3143. #include "argtable3.h"
  3144. static void arg_str_resetfn(struct arg_str *parent)
  3145. {
  3146. ARG_TRACE(("%s:resetfn(%p)\n", __FILE__, parent));
  3147. parent->count = 0;
  3148. }
  3149. static int arg_str_scanfn(struct arg_str *parent, const char *argval)
  3150. {
  3151. int errorcode = 0;
  3152. if (parent->count == parent->hdr.maxcount)
  3153. {
  3154. /* maximum number of arguments exceeded */
  3155. errorcode = EMAXCOUNT;
  3156. }
  3157. else if (!argval)
  3158. {
  3159. /* a valid argument with no argument value was given. */
  3160. /* This happens when an optional argument value was invoked. */
  3161. /* leave parent arguiment value unaltered but still count the argument. */
  3162. parent->count++;
  3163. }
  3164. else
  3165. {
  3166. parent->sval[parent->count++] = argval;
  3167. }
  3168. ARG_TRACE(("%s:scanfn(%p) returns %d\n", __FILE__, parent, errorcode));
  3169. return errorcode;
  3170. }
  3171. static int arg_str_checkfn(struct arg_str *parent)
  3172. {
  3173. int errorcode = (parent->count < parent->hdr.mincount) ? EMINCOUNT : 0;
  3174. ARG_TRACE(("%s:checkfn(%p) returns %d\n", __FILE__, parent, errorcode));
  3175. return errorcode;
  3176. }
  3177. static void arg_str_errorfn(
  3178. struct arg_str *parent,
  3179. FILE *fp,
  3180. int errorcode,
  3181. const char *argval,
  3182. const char *progname)
  3183. {
  3184. const char *shortopts = parent->hdr.shortopts;
  3185. const char *longopts = parent->hdr.longopts;
  3186. const char *datatype = parent->hdr.datatype;
  3187. /* make argval NULL safe */
  3188. argval = argval ? argval : "";
  3189. fprintf(fp, "%s: ", progname);
  3190. switch(errorcode)
  3191. {
  3192. case EMINCOUNT:
  3193. fputs("missing option ", fp);
  3194. arg_print_option(fp, shortopts, longopts, datatype, "\n");
  3195. break;
  3196. case EMAXCOUNT:
  3197. fputs("excess option ", fp);
  3198. arg_print_option(fp, shortopts, longopts, argval, "\n");
  3199. break;
  3200. }
  3201. }
  3202. struct arg_str * arg_str0(
  3203. const char *shortopts,
  3204. const char *longopts,
  3205. const char *datatype,
  3206. const char *glossary)
  3207. {
  3208. return arg_strn(shortopts, longopts, datatype, 0, 1, glossary);
  3209. }
  3210. struct arg_str * arg_str1(
  3211. const char *shortopts,
  3212. const char *longopts,
  3213. const char *datatype,
  3214. const char *glossary)
  3215. {
  3216. return arg_strn(shortopts, longopts, datatype, 1, 1, glossary);
  3217. }
  3218. struct arg_str * arg_strn(
  3219. const char *shortopts,
  3220. const char *longopts,
  3221. const char *datatype,
  3222. int mincount,
  3223. int maxcount,
  3224. const char *glossary)
  3225. {
  3226. size_t nbytes;
  3227. struct arg_str *result;
  3228. /* should not allow this stupid error */
  3229. /* we should return an error code warning this logic error */
  3230. /* foolproof things by ensuring maxcount is not less than mincount */
  3231. maxcount = (maxcount < mincount) ? mincount : maxcount;
  3232. nbytes = sizeof(struct arg_str) /* storage for struct arg_str */
  3233. + maxcount * sizeof(char *); /* storage for sval[maxcount] array */
  3234. result = (struct arg_str *)malloc(nbytes);
  3235. if (result)
  3236. {
  3237. int i;
  3238. /* init the arg_hdr struct */
  3239. result->hdr.flag = ARG_HASVALUE;
  3240. result->hdr.shortopts = shortopts;
  3241. result->hdr.longopts = longopts;
  3242. result->hdr.datatype = datatype ? datatype : "<string>";
  3243. result->hdr.glossary = glossary;
  3244. result->hdr.mincount = mincount;
  3245. result->hdr.maxcount = maxcount;
  3246. result->hdr.parent = result;
  3247. result->hdr.resetfn = (arg_resetfn *)arg_str_resetfn;
  3248. result->hdr.scanfn = (arg_scanfn *)arg_str_scanfn;
  3249. result->hdr.checkfn = (arg_checkfn *)arg_str_checkfn;
  3250. result->hdr.errorfn = (arg_errorfn *)arg_str_errorfn;
  3251. /* store the sval[maxcount] array immediately after the arg_str struct */
  3252. result->sval = (const char * *)(result + 1);
  3253. result->count = 0;
  3254. /* foolproof the string pointers by initialising them to reference empty strings */
  3255. for (i = 0; i < maxcount; i++)
  3256. result->sval[i] = "";
  3257. }
  3258. ARG_TRACE(("arg_strn() returns %p\n", result));
  3259. return result;
  3260. }
  3261. /*******************************************************************************
  3262. * This file is part of the argtable3 library.
  3263. *
  3264. * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
  3265. * <sheitmann@users.sourceforge.net>
  3266. * All rights reserved.
  3267. *
  3268. * Redistribution and use in source and binary forms, with or without
  3269. * modification, are permitted provided that the following conditions are met:
  3270. * * Redistributions of source code must retain the above copyright
  3271. * notice, this list of conditions and the following disclaimer.
  3272. * * Redistributions in binary form must reproduce the above copyright
  3273. * notice, this list of conditions and the following disclaimer in the
  3274. * documentation and/or other materials provided with the distribution.
  3275. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  3276. * may be used to endorse or promote products derived from this software
  3277. * without specific prior written permission.
  3278. *
  3279. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  3280. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  3281. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  3282. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  3283. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  3284. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  3285. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  3286. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  3287. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  3288. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  3289. ******************************************************************************/
  3290. #include <stdlib.h>
  3291. #include <string.h>
  3292. #include <stdlib.h>
  3293. #include <ctype.h>
  3294. #include "argtable3.h"
  3295. static
  3296. void arg_register_error(struct arg_end *end,
  3297. void *parent,
  3298. int error,
  3299. const char *argval)
  3300. {
  3301. /* printf("arg_register_error(%p,%p,%d,%s)\n",end,parent,error,argval); */
  3302. if (end->count < end->hdr.maxcount)
  3303. {
  3304. end->error[end->count] = error;
  3305. end->parent[end->count] = parent;
  3306. end->argval[end->count] = argval;
  3307. end->count++;
  3308. }
  3309. else
  3310. {
  3311. end->error[end->hdr.maxcount - 1] = ARG_ELIMIT;
  3312. end->parent[end->hdr.maxcount - 1] = end;
  3313. end->argval[end->hdr.maxcount - 1] = NULL;
  3314. }
  3315. }
  3316. /*
  3317. * Return index of first table entry with a matching short option
  3318. * or -1 if no match was found.
  3319. */
  3320. static
  3321. int find_shortoption(struct arg_hdr * *table, char shortopt)
  3322. {
  3323. int tabindex;
  3324. for(tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++)
  3325. {
  3326. if (table[tabindex]->shortopts &&
  3327. strchr(table[tabindex]->shortopts, shortopt))
  3328. return tabindex;
  3329. }
  3330. return -1;
  3331. }
  3332. struct longoptions
  3333. {
  3334. int getoptval;
  3335. int noptions;
  3336. struct option *options;
  3337. };
  3338. #if 0
  3339. static
  3340. void dump_longoptions(struct longoptions * longoptions)
  3341. {
  3342. int i;
  3343. printf("getoptval = %d\n", longoptions->getoptval);
  3344. printf("noptions = %d\n", longoptions->noptions);
  3345. for (i = 0; i < longoptions->noptions; i++)
  3346. {
  3347. printf("options[%d].name = \"%s\"\n",
  3348. i,
  3349. longoptions->options[i].name);
  3350. printf("options[%d].has_arg = %d\n", i, longoptions->options[i].has_arg);
  3351. printf("options[%d].flag = %p\n", i, longoptions->options[i].flag);
  3352. printf("options[%d].val = %d\n", i, longoptions->options[i].val);
  3353. }
  3354. }
  3355. #endif
  3356. static
  3357. struct longoptions * alloc_longoptions(struct arg_hdr * *table)
  3358. {
  3359. struct longoptions *result;
  3360. size_t nbytes;
  3361. int noptions = 1;
  3362. size_t longoptlen = 0;
  3363. int tabindex;
  3364. /*
  3365. * Determine the total number of option structs required
  3366. * by counting the number of comma separated long options
  3367. * in all table entries and return the count in noptions.
  3368. * note: noptions starts at 1 not 0 because we getoptlong
  3369. * requires a NULL option entry to terminate the option array.
  3370. * While we are at it, count the number of chars required
  3371. * to store private copies of all the longoption strings
  3372. * and return that count in logoptlen.
  3373. */
  3374. tabindex = 0;
  3375. do
  3376. {
  3377. const char *longopts = table[tabindex]->longopts;
  3378. longoptlen += (longopts ? strlen(longopts) : 0) + 1;
  3379. while (longopts)
  3380. {
  3381. noptions++;
  3382. longopts = strchr(longopts + 1, ',');
  3383. }
  3384. } while(!(table[tabindex++]->flag & ARG_TERMINATOR));
  3385. /*printf("%d long options consuming %d chars in total\n",noptions,longoptlen);*/
  3386. /* allocate storage for return data structure as: */
  3387. /* (struct longoptions) + (struct options)[noptions] + char[longoptlen] */
  3388. nbytes = sizeof(struct longoptions)
  3389. + sizeof(struct option) * noptions
  3390. + longoptlen;
  3391. result = (struct longoptions *)malloc(nbytes);
  3392. if (result)
  3393. {
  3394. int option_index = 0;
  3395. char *store;
  3396. result->getoptval = 0;
  3397. result->noptions = noptions;
  3398. result->options = (struct option *)(result + 1);
  3399. store = (char *)(result->options + noptions);
  3400. for(tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++)
  3401. {
  3402. const char *longopts = table[tabindex]->longopts;
  3403. while(longopts && *longopts)
  3404. {
  3405. char *storestart = store;
  3406. /* copy progressive longopt strings into the store */
  3407. while (*longopts != 0 && *longopts != ',')
  3408. *store++ = *longopts++;
  3409. *store++ = 0;
  3410. if (*longopts == ',')
  3411. longopts++;
  3412. /*fprintf(stderr,"storestart=\"%s\"\n",storestart);*/
  3413. result->options[option_index].name = storestart;
  3414. result->options[option_index].flag = &(result->getoptval);
  3415. result->options[option_index].val = tabindex;
  3416. if (table[tabindex]->flag & ARG_HASOPTVALUE)
  3417. result->options[option_index].has_arg = 2;
  3418. else if (table[tabindex]->flag & ARG_HASVALUE)
  3419. result->options[option_index].has_arg = 1;
  3420. else
  3421. result->options[option_index].has_arg = 0;
  3422. option_index++;
  3423. }
  3424. }
  3425. /* terminate the options array with a zero-filled entry */
  3426. result->options[option_index].name = 0;
  3427. result->options[option_index].has_arg = 0;
  3428. result->options[option_index].flag = 0;
  3429. result->options[option_index].val = 0;
  3430. }
  3431. /*dump_longoptions(result);*/
  3432. return result;
  3433. }
  3434. static
  3435. char * alloc_shortoptions(struct arg_hdr * *table)
  3436. {
  3437. char *result;
  3438. size_t len = 2;
  3439. int tabindex;
  3440. /* determine the total number of option chars required */
  3441. for(tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++)
  3442. {
  3443. struct arg_hdr *hdr = table[tabindex];
  3444. len += 3 * (hdr->shortopts ? strlen(hdr->shortopts) : 0);
  3445. }
  3446. result = malloc(len);
  3447. if (result)
  3448. {
  3449. char *res = result;
  3450. /* add a leading ':' so getopt return codes distinguish */
  3451. /* unrecognised option and options missing argument values */
  3452. *res++ = ':';
  3453. for(tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++)
  3454. {
  3455. struct arg_hdr *hdr = table[tabindex];
  3456. const char *shortopts = hdr->shortopts;
  3457. while(shortopts && *shortopts)
  3458. {
  3459. *res++ = *shortopts++;
  3460. if (hdr->flag & ARG_HASVALUE)
  3461. *res++ = ':';
  3462. if (hdr->flag & ARG_HASOPTVALUE)
  3463. *res++ = ':';
  3464. }
  3465. }
  3466. /* null terminate the string */
  3467. *res = 0;
  3468. }
  3469. /*printf("alloc_shortoptions() returns \"%s\"\n",(result?result:"NULL"));*/
  3470. return result;
  3471. }
  3472. /* return index of the table terminator entry */
  3473. static
  3474. int arg_endindex(struct arg_hdr * *table)
  3475. {
  3476. int tabindex = 0;
  3477. while (!(table[tabindex]->flag & ARG_TERMINATOR))
  3478. tabindex++;
  3479. return tabindex;
  3480. }
  3481. static
  3482. void arg_parse_tagged(int argc,
  3483. char * *argv,
  3484. struct arg_hdr * *table,
  3485. struct arg_end *endtable)
  3486. {
  3487. struct longoptions *longoptions;
  3488. char *shortoptions;
  3489. int copt;
  3490. /*printf("arg_parse_tagged(%d,%p,%p,%p)\n",argc,argv,table,endtable);*/
  3491. /* allocate short and long option arrays for the given opttable[]. */
  3492. /* if the allocs fail then put an error msg in the last table entry. */
  3493. longoptions = alloc_longoptions(table);
  3494. shortoptions = alloc_shortoptions(table);
  3495. if (!longoptions || !shortoptions)
  3496. {
  3497. /* one or both memory allocs failed */
  3498. arg_register_error(endtable, endtable, ARG_EMALLOC, NULL);
  3499. /* free anything that was allocated (this is null safe) */
  3500. free(shortoptions);
  3501. free(longoptions);
  3502. return;
  3503. }
  3504. /*dump_longoptions(longoptions);*/
  3505. /* reset getopts internal option-index to zero, and disable error reporting */
  3506. optind = 0;
  3507. opterr = 0;
  3508. /* fetch and process args using getopt_long */
  3509. while( (copt =
  3510. getopt_long(argc, argv, shortoptions, longoptions->options,
  3511. NULL)) != -1)
  3512. {
  3513. /*
  3514. printf("optarg='%s'\n",optarg);
  3515. printf("optind=%d\n",optind);
  3516. printf("copt=%c\n",(char)copt);
  3517. printf("optopt=%c (%d)\n",optopt, (int)(optopt));
  3518. */
  3519. switch(copt)
  3520. {
  3521. case 0:
  3522. {
  3523. int tabindex = longoptions->getoptval;
  3524. void *parent = table[tabindex]->parent;
  3525. /*printf("long option detected from argtable[%d]\n", tabindex);*/
  3526. if (optarg && optarg[0] == 0 &&
  3527. (table[tabindex]->flag & ARG_HASVALUE))
  3528. {
  3529. /* printf(": long option %s requires an argument\n",argv[optind-1]); */
  3530. arg_register_error(endtable, endtable, ARG_EMISSARG,
  3531. argv[optind - 1]);
  3532. /* continue to scan the (empty) argument value to enforce argument count checking */
  3533. }
  3534. if (table[tabindex]->scanfn)
  3535. {
  3536. int errorcode = table[tabindex]->scanfn(parent, optarg);
  3537. if (errorcode != 0)
  3538. arg_register_error(endtable, parent, errorcode, optarg);
  3539. }
  3540. }
  3541. break;
  3542. case '?':
  3543. /*
  3544. * getopt_long() found an unrecognised short option.
  3545. * if it was a short option its value is in optopt
  3546. * if it was a long option then optopt=0
  3547. */
  3548. switch (optopt)
  3549. {
  3550. case 0:
  3551. /*printf("?0 unrecognised long option %s\n",argv[optind-1]);*/
  3552. arg_register_error(endtable, endtable, ARG_ELONGOPT,
  3553. argv[optind - 1]);
  3554. break;
  3555. default:
  3556. /*printf("?* unrecognised short option '%c'\n",optopt);*/
  3557. arg_register_error(endtable, endtable, optopt, NULL);
  3558. break;
  3559. }
  3560. break;
  3561. case ':':
  3562. /*
  3563. * getopt_long() found an option with its argument missing.
  3564. */
  3565. /*printf(": option %s requires an argument\n",argv[optind-1]); */
  3566. arg_register_error(endtable, endtable, ARG_EMISSARG,
  3567. argv[optind - 1]);
  3568. break;
  3569. default:
  3570. {
  3571. /* getopt_long() found a valid short option */
  3572. int tabindex = find_shortoption(table, (char)copt);
  3573. /*printf("short option detected from argtable[%d]\n", tabindex);*/
  3574. if (tabindex == -1)
  3575. {
  3576. /* should never get here - but handle it just in case */
  3577. /*printf("unrecognised short option %d\n",copt);*/
  3578. arg_register_error(endtable, endtable, copt, NULL);
  3579. }
  3580. else
  3581. {
  3582. if (table[tabindex]->scanfn)
  3583. {
  3584. void *parent = table[tabindex]->parent;
  3585. int errorcode = table[tabindex]->scanfn(parent, optarg);
  3586. if (errorcode != 0)
  3587. arg_register_error(endtable, parent, errorcode, optarg);
  3588. }
  3589. }
  3590. break;
  3591. }
  3592. }
  3593. }
  3594. free(shortoptions);
  3595. free(longoptions);
  3596. }
  3597. static
  3598. void arg_parse_untagged(int argc,
  3599. char * *argv,
  3600. struct arg_hdr * *table,
  3601. struct arg_end *endtable)
  3602. {
  3603. int tabindex = 0;
  3604. int errorlast = 0;
  3605. const char *optarglast = NULL;
  3606. void *parentlast = NULL;
  3607. /*printf("arg_parse_untagged(%d,%p,%p,%p)\n",argc,argv,table,endtable);*/
  3608. while (!(table[tabindex]->flag & ARG_TERMINATOR))
  3609. {
  3610. void *parent;
  3611. int errorcode;
  3612. /* if we have exhausted our argv[optind] entries then we have finished */
  3613. if (optind >= argc)
  3614. {
  3615. /*printf("arg_parse_untagged(): argv[] exhausted\n");*/
  3616. return;
  3617. }
  3618. /* skip table entries with non-null long or short options (they are not untagged entries) */
  3619. if (table[tabindex]->longopts || table[tabindex]->shortopts)
  3620. {
  3621. /*printf("arg_parse_untagged(): skipping argtable[%d] (tagged argument)\n",tabindex);*/
  3622. tabindex++;
  3623. continue;
  3624. }
  3625. /* skip table entries with NULL scanfn */
  3626. if (!(table[tabindex]->scanfn))
  3627. {
  3628. /*printf("arg_parse_untagged(): skipping argtable[%d] (NULL scanfn)\n",tabindex);*/
  3629. tabindex++;
  3630. continue;
  3631. }
  3632. /* attempt to scan the current argv[optind] with the current */
  3633. /* table[tabindex] entry. If it succeeds then keep it, otherwise */
  3634. /* try again with the next table[] entry. */
  3635. parent = table[tabindex]->parent;
  3636. errorcode = table[tabindex]->scanfn(parent, argv[optind]);
  3637. if (errorcode == 0)
  3638. {
  3639. /* success, move onto next argv[optind] but stay with same table[tabindex] */
  3640. /*printf("arg_parse_untagged(): argtable[%d] successfully matched\n",tabindex);*/
  3641. optind++;
  3642. /* clear the last tentative error */
  3643. errorlast = 0;
  3644. }
  3645. else
  3646. {
  3647. /* failure, try same argv[optind] with next table[tabindex] entry */
  3648. /*printf("arg_parse_untagged(): argtable[%d] failed match\n",tabindex);*/
  3649. tabindex++;
  3650. /* remember this as a tentative error we may wish to reinstate later */
  3651. errorlast = errorcode;
  3652. optarglast = argv[optind];
  3653. parentlast = parent;
  3654. }
  3655. }
  3656. /* if a tenative error still remains at this point then register it as a proper error */
  3657. if (errorlast)
  3658. {
  3659. arg_register_error(endtable, parentlast, errorlast, optarglast);
  3660. optind++;
  3661. }
  3662. /* only get here when not all argv[] entries were consumed */
  3663. /* register an error for each unused argv[] entry */
  3664. while (optind < argc)
  3665. {
  3666. /*printf("arg_parse_untagged(): argv[%d]=\"%s\" not consumed\n",optind,argv[optind]);*/
  3667. arg_register_error(endtable, endtable, ARG_ENOMATCH, argv[optind++]);
  3668. }
  3669. return;
  3670. }
  3671. static
  3672. void arg_parse_check(struct arg_hdr * *table, struct arg_end *endtable)
  3673. {
  3674. int tabindex = 0;
  3675. /* printf("arg_parse_check()\n"); */
  3676. do
  3677. {
  3678. if (table[tabindex]->checkfn)
  3679. {
  3680. void *parent = table[tabindex]->parent;
  3681. int errorcode = table[tabindex]->checkfn(parent);
  3682. if (errorcode != 0)
  3683. arg_register_error(endtable, parent, errorcode, NULL);
  3684. }
  3685. } while(!(table[tabindex++]->flag & ARG_TERMINATOR));
  3686. }
  3687. static
  3688. void arg_reset(void * *argtable)
  3689. {
  3690. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  3691. int tabindex = 0;
  3692. /*printf("arg_reset(%p)\n",argtable);*/
  3693. do
  3694. {
  3695. if (table[tabindex]->resetfn)
  3696. table[tabindex]->resetfn(table[tabindex]->parent);
  3697. } while(!(table[tabindex++]->flag & ARG_TERMINATOR));
  3698. }
  3699. int arg_parse(int argc, char * *argv, void * *argtable)
  3700. {
  3701. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  3702. struct arg_end *endtable;
  3703. int endindex;
  3704. char * *argvcopy = NULL;
  3705. /*printf("arg_parse(%d,%p,%p)\n",argc,argv,argtable);*/
  3706. /* reset any argtable data from previous invocations */
  3707. arg_reset(argtable);
  3708. /* locate the first end-of-table marker within the array */
  3709. endindex = arg_endindex(table);
  3710. endtable = (struct arg_end *)table[endindex];
  3711. /* Special case of argc==0. This can occur on Texas Instruments DSP. */
  3712. /* Failure to trap this case results in an unwanted NULL result from */
  3713. /* the malloc for argvcopy (next code block). */
  3714. if (argc == 0)
  3715. {
  3716. /* We must still perform post-parse checks despite the absence of command line arguments */
  3717. arg_parse_check(table, endtable);
  3718. /* Now we are finished */
  3719. return endtable->count;
  3720. }
  3721. argvcopy = (char **)malloc(sizeof(char *) * (argc + 1));
  3722. if (argvcopy)
  3723. {
  3724. int i;
  3725. /*
  3726. Fill in the local copy of argv[]. We need a local copy
  3727. because getopt rearranges argv[] which adversely affects
  3728. susbsequent parsing attempts.
  3729. */
  3730. for (i = 0; i < argc; i++)
  3731. argvcopy[i] = argv[i];
  3732. argvcopy[argc] = NULL;
  3733. /* parse the command line (local copy) for tagged options */
  3734. arg_parse_tagged(argc, argvcopy, table, endtable);
  3735. /* parse the command line (local copy) for untagged options */
  3736. arg_parse_untagged(argc, argvcopy, table, endtable);
  3737. /* if no errors so far then perform post-parse checks otherwise dont bother */
  3738. if (endtable->count == 0)
  3739. arg_parse_check(table, endtable);
  3740. /* release the local copt of argv[] */
  3741. free(argvcopy);
  3742. }
  3743. else
  3744. {
  3745. /* memory alloc failed */
  3746. arg_register_error(endtable, endtable, ARG_EMALLOC, NULL);
  3747. }
  3748. return endtable->count;
  3749. }
  3750. /*
  3751. * Concatenate contents of src[] string onto *pdest[] string.
  3752. * The *pdest pointer is altered to point to the end of the
  3753. * target string and *pndest is decremented by the same number
  3754. * of chars.
  3755. * Does not append more than *pndest chars into *pdest[]
  3756. * so as to prevent buffer overruns.
  3757. * Its something like strncat() but more efficient for repeated
  3758. * calls on the same destination string.
  3759. * Example of use:
  3760. * char dest[30] = "good"
  3761. * size_t ndest = sizeof(dest);
  3762. * char *pdest = dest;
  3763. * arg_char(&pdest,"bye ",&ndest);
  3764. * arg_char(&pdest,"cruel ",&ndest);
  3765. * arg_char(&pdest,"world!",&ndest);
  3766. * Results in:
  3767. * dest[] == "goodbye cruel world!"
  3768. * ndest == 10
  3769. */
  3770. static
  3771. void arg_cat(char * *pdest, const char *src, size_t *pndest)
  3772. {
  3773. char *dest = *pdest;
  3774. char *end = dest + *pndest;
  3775. /*locate null terminator of dest string */
  3776. while(dest < end && *dest != 0)
  3777. dest++;
  3778. /* concat src string to dest string */
  3779. while(dest < end && *src != 0)
  3780. *dest++ = *src++;
  3781. /* null terminate dest string */
  3782. *dest = 0;
  3783. /* update *pdest and *pndest */
  3784. *pndest = end - dest;
  3785. *pdest = dest;
  3786. }
  3787. static
  3788. void arg_cat_option(char *dest,
  3789. size_t ndest,
  3790. const char *shortopts,
  3791. const char *longopts,
  3792. const char *datatype,
  3793. int optvalue)
  3794. {
  3795. if (shortopts)
  3796. {
  3797. char option[3];
  3798. /* note: option array[] is initialiazed dynamically here to satisfy */
  3799. /* a deficiency in the watcom compiler wrt static array initializers. */
  3800. option[0] = '-';
  3801. option[1] = shortopts[0];
  3802. option[2] = 0;
  3803. arg_cat(&dest, option, &ndest);
  3804. if (datatype)
  3805. {
  3806. arg_cat(&dest, " ", &ndest);
  3807. if (optvalue)
  3808. {
  3809. arg_cat(&dest, "[", &ndest);
  3810. arg_cat(&dest, datatype, &ndest);
  3811. arg_cat(&dest, "]", &ndest);
  3812. }
  3813. else
  3814. arg_cat(&dest, datatype, &ndest);
  3815. }
  3816. }
  3817. else if (longopts)
  3818. {
  3819. size_t ncspn;
  3820. /* add "--" tag prefix */
  3821. arg_cat(&dest, "--", &ndest);
  3822. /* add comma separated option tag */
  3823. ncspn = strcspn(longopts, ",");
  3824. strncat(dest, longopts, (ncspn < ndest) ? ncspn : ndest);
  3825. if (datatype)
  3826. {
  3827. arg_cat(&dest, "=", &ndest);
  3828. if (optvalue)
  3829. {
  3830. arg_cat(&dest, "[", &ndest);
  3831. arg_cat(&dest, datatype, &ndest);
  3832. arg_cat(&dest, "]", &ndest);
  3833. }
  3834. else
  3835. arg_cat(&dest, datatype, &ndest);
  3836. }
  3837. }
  3838. else if (datatype)
  3839. {
  3840. if (optvalue)
  3841. {
  3842. arg_cat(&dest, "[", &ndest);
  3843. arg_cat(&dest, datatype, &ndest);
  3844. arg_cat(&dest, "]", &ndest);
  3845. }
  3846. else
  3847. arg_cat(&dest, datatype, &ndest);
  3848. }
  3849. }
  3850. static
  3851. void arg_cat_optionv(char *dest,
  3852. size_t ndest,
  3853. const char *shortopts,
  3854. const char *longopts,
  3855. const char *datatype,
  3856. int optvalue,
  3857. const char *separator)
  3858. {
  3859. separator = separator ? separator : "";
  3860. if (shortopts)
  3861. {
  3862. const char *c = shortopts;
  3863. while(*c)
  3864. {
  3865. /* "-a|-b|-c" */
  3866. char shortopt[3];
  3867. /* note: shortopt array[] is initialiazed dynamically here to satisfy */
  3868. /* a deficiency in the watcom compiler wrt static array initializers. */
  3869. shortopt[0] = '-';
  3870. shortopt[1] = *c;
  3871. shortopt[2] = 0;
  3872. arg_cat(&dest, shortopt, &ndest);
  3873. if (*++c)
  3874. arg_cat(&dest, separator, &ndest);
  3875. }
  3876. }
  3877. /* put separator between long opts and short opts */
  3878. if (shortopts && longopts)
  3879. arg_cat(&dest, separator, &ndest);
  3880. if (longopts)
  3881. {
  3882. const char *c = longopts;
  3883. while(*c)
  3884. {
  3885. size_t ncspn;
  3886. /* add "--" tag prefix */
  3887. arg_cat(&dest, "--", &ndest);
  3888. /* add comma separated option tag */
  3889. ncspn = strcspn(c, ",");
  3890. strncat(dest, c, (ncspn < ndest) ? ncspn : ndest);
  3891. c += ncspn;
  3892. /* add given separator in place of comma */
  3893. if (*c == ',')
  3894. {
  3895. arg_cat(&dest, separator, &ndest);
  3896. c++;
  3897. }
  3898. }
  3899. }
  3900. if (datatype)
  3901. {
  3902. if (longopts)
  3903. arg_cat(&dest, "=", &ndest);
  3904. else if (shortopts)
  3905. arg_cat(&dest, " ", &ndest);
  3906. if (optvalue)
  3907. {
  3908. arg_cat(&dest, "[", &ndest);
  3909. arg_cat(&dest, datatype, &ndest);
  3910. arg_cat(&dest, "]", &ndest);
  3911. }
  3912. else
  3913. arg_cat(&dest, datatype, &ndest);
  3914. }
  3915. }
  3916. /* this function should be deprecated because it doesnt consider optional argument values (ARG_HASOPTVALUE) */
  3917. void arg_print_option(FILE *fp,
  3918. const char *shortopts,
  3919. const char *longopts,
  3920. const char *datatype,
  3921. const char *suffix)
  3922. {
  3923. char syntax[200] = "";
  3924. suffix = suffix ? suffix : "";
  3925. /* there is no way of passing the proper optvalue for optional argument values here, so we must ignore it */
  3926. arg_cat_optionv(syntax,
  3927. sizeof(syntax),
  3928. shortopts,
  3929. longopts,
  3930. datatype,
  3931. 0,
  3932. "|");
  3933. fputs(syntax, fp);
  3934. fputs(suffix, fp);
  3935. }
  3936. /*
  3937. * Print a GNU style [OPTION] string in which all short options that
  3938. * do not take argument values are presented in abbreviated form, as
  3939. * in: -xvfsd, or -xvf[sd], or [-xvsfd]
  3940. */
  3941. static
  3942. void arg_print_gnuswitch(FILE *fp, struct arg_hdr * *table)
  3943. {
  3944. int tabindex;
  3945. const char *format1 = " -%c";
  3946. const char *format2 = " [-%c";
  3947. const char *suffix = "";
  3948. /* print all mandatory switches that are without argument values */
  3949. for(tabindex = 0;
  3950. table[tabindex] && !(table[tabindex]->flag & ARG_TERMINATOR);
  3951. tabindex++)
  3952. {
  3953. /* skip optional options */
  3954. if (table[tabindex]->mincount < 1)
  3955. continue;
  3956. /* skip non-short options */
  3957. if (table[tabindex]->shortopts == NULL)
  3958. continue;
  3959. /* skip options that take argument values */
  3960. if (table[tabindex]->flag & ARG_HASVALUE)
  3961. continue;
  3962. /* print the short option (only the first short option char, ignore multiple choices)*/
  3963. fprintf(fp, format1, table[tabindex]->shortopts[0]);
  3964. format1 = "%c";
  3965. format2 = "[%c";
  3966. }
  3967. /* print all optional switches that are without argument values */
  3968. for(tabindex = 0;
  3969. table[tabindex] && !(table[tabindex]->flag & ARG_TERMINATOR);
  3970. tabindex++)
  3971. {
  3972. /* skip mandatory args */
  3973. if (table[tabindex]->mincount > 0)
  3974. continue;
  3975. /* skip args without short options */
  3976. if (table[tabindex]->shortopts == NULL)
  3977. continue;
  3978. /* skip args with values */
  3979. if (table[tabindex]->flag & ARG_HASVALUE)
  3980. continue;
  3981. /* print first short option */
  3982. fprintf(fp, format2, table[tabindex]->shortopts[0]);
  3983. format2 = "%c";
  3984. suffix = "]";
  3985. }
  3986. fprintf(fp, "%s", suffix);
  3987. }
  3988. void arg_print_syntax(FILE *fp, void * *argtable, const char *suffix)
  3989. {
  3990. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  3991. int i, tabindex;
  3992. /* print GNU style [OPTION] string */
  3993. arg_print_gnuswitch(fp, table);
  3994. /* print remaining options in abbreviated style */
  3995. for(tabindex = 0;
  3996. table[tabindex] && !(table[tabindex]->flag & ARG_TERMINATOR);
  3997. tabindex++)
  3998. {
  3999. char syntax[200] = "";
  4000. const char *shortopts, *longopts, *datatype;
  4001. /* skip short options without arg values (they were printed by arg_print_gnu_switch) */
  4002. if (table[tabindex]->shortopts &&
  4003. !(table[tabindex]->flag & ARG_HASVALUE))
  4004. continue;
  4005. shortopts = table[tabindex]->shortopts;
  4006. longopts = table[tabindex]->longopts;
  4007. datatype = table[tabindex]->datatype;
  4008. arg_cat_option(syntax,
  4009. sizeof(syntax),
  4010. shortopts,
  4011. longopts,
  4012. datatype,
  4013. table[tabindex]->flag & ARG_HASOPTVALUE);
  4014. if (strlen(syntax) > 0)
  4015. {
  4016. /* print mandatory instances of this option */
  4017. for (i = 0; i < table[tabindex]->mincount; i++)
  4018. fprintf(fp, " %s", syntax);
  4019. /* print optional instances enclosed in "[..]" */
  4020. switch ( table[tabindex]->maxcount - table[tabindex]->mincount )
  4021. {
  4022. case 0:
  4023. break;
  4024. case 1:
  4025. fprintf(fp, " [%s]", syntax);
  4026. break;
  4027. case 2:
  4028. fprintf(fp, " [%s] [%s]", syntax, syntax);
  4029. break;
  4030. default:
  4031. fprintf(fp, " [%s]...", syntax);
  4032. break;
  4033. }
  4034. }
  4035. }
  4036. if (suffix)
  4037. fprintf(fp, "%s", suffix);
  4038. }
  4039. void arg_print_syntaxv(FILE *fp, void * *argtable, const char *suffix)
  4040. {
  4041. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  4042. int i, tabindex;
  4043. /* print remaining options in abbreviated style */
  4044. for(tabindex = 0;
  4045. table[tabindex] && !(table[tabindex]->flag & ARG_TERMINATOR);
  4046. tabindex++)
  4047. {
  4048. char syntax[200] = "";
  4049. const char *shortopts, *longopts, *datatype;
  4050. shortopts = table[tabindex]->shortopts;
  4051. longopts = table[tabindex]->longopts;
  4052. datatype = table[tabindex]->datatype;
  4053. arg_cat_optionv(syntax,
  4054. sizeof(syntax),
  4055. shortopts,
  4056. longopts,
  4057. datatype,
  4058. table[tabindex]->flag & ARG_HASOPTVALUE,
  4059. "|");
  4060. /* print mandatory options */
  4061. for (i = 0; i < table[tabindex]->mincount; i++)
  4062. fprintf(fp, " %s", syntax);
  4063. /* print optional args enclosed in "[..]" */
  4064. switch ( table[tabindex]->maxcount - table[tabindex]->mincount )
  4065. {
  4066. case 0:
  4067. break;
  4068. case 1:
  4069. fprintf(fp, " [%s]", syntax);
  4070. break;
  4071. case 2:
  4072. fprintf(fp, " [%s] [%s]", syntax, syntax);
  4073. break;
  4074. default:
  4075. fprintf(fp, " [%s]...", syntax);
  4076. break;
  4077. }
  4078. }
  4079. if (suffix)
  4080. fprintf(fp, "%s", suffix);
  4081. }
  4082. void arg_print_glossary(FILE *fp, void * *argtable, const char *format)
  4083. {
  4084. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  4085. int tabindex;
  4086. format = format ? format : " %-20s %s\n";
  4087. for (tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++)
  4088. {
  4089. if (table[tabindex]->glossary)
  4090. {
  4091. char syntax[200] = "";
  4092. const char *shortopts = table[tabindex]->shortopts;
  4093. const char *longopts = table[tabindex]->longopts;
  4094. const char *datatype = table[tabindex]->datatype;
  4095. const char *glossary = table[tabindex]->glossary;
  4096. arg_cat_optionv(syntax,
  4097. sizeof(syntax),
  4098. shortopts,
  4099. longopts,
  4100. datatype,
  4101. table[tabindex]->flag & ARG_HASOPTVALUE,
  4102. ", ");
  4103. fprintf(fp, format, syntax, glossary);
  4104. }
  4105. }
  4106. }
  4107. /**
  4108. * Print a piece of text formatted, which means in a column with a
  4109. * left and a right margin. The lines are wrapped at whitspaces next
  4110. * to right margin. The function does not indent the first line, but
  4111. * only the following ones.
  4112. *
  4113. * Example:
  4114. * arg_print_formatted( fp, 0, 5, "Some text that doesn't fit." )
  4115. * will result in the following output:
  4116. *
  4117. * Some
  4118. * text
  4119. * that
  4120. * doesn'
  4121. * t fit.
  4122. *
  4123. * Too long lines will be wrapped in the middle of a word.
  4124. *
  4125. * arg_print_formatted( fp, 2, 7, "Some text that doesn't fit." )
  4126. * will result in the following output:
  4127. *
  4128. * Some
  4129. * text
  4130. * that
  4131. * doesn'
  4132. * t fit.
  4133. *
  4134. * As you see, the first line is not indented. This enables output of
  4135. * lines, which start in a line where output already happened.
  4136. *
  4137. * Author: Uli Fouquet
  4138. */
  4139. void arg_print_formatted( FILE *fp,
  4140. const unsigned lmargin,
  4141. const unsigned rmargin,
  4142. const char *text )
  4143. {
  4144. const unsigned textlen = strlen( text );
  4145. unsigned line_start = 0;
  4146. unsigned line_end = textlen + 1;
  4147. const unsigned colwidth = (rmargin - lmargin) + 1;
  4148. /* Someone doesn't like us... */
  4149. if ( line_end < line_start )
  4150. { fprintf( fp, "%s\n", text ); }
  4151. while (line_end - 1 > line_start )
  4152. {
  4153. /* Eat leading whitespaces. This is essential because while
  4154. wrapping lines, there will often be a whitespace at beginning
  4155. of line */
  4156. while ( isspace((int) *(text + line_start)) )
  4157. { line_start++; }
  4158. if ((line_end - line_start) > colwidth )
  4159. { line_end = line_start + colwidth; }
  4160. /* Find last whitespace, that fits into line */
  4161. while ( ( line_end > line_start )
  4162. && ( line_end - line_start > colwidth )
  4163. && !isspace((int) *(text + line_end)))
  4164. { line_end--; }
  4165. /* Do not print trailing whitespace. If this text
  4166. has got only one line, line_end now points to the
  4167. last char due to initialization. */
  4168. line_end--;
  4169. /* Output line of text */
  4170. while ( line_start < line_end )
  4171. {
  4172. fputc(*(text + line_start), fp );
  4173. line_start++;
  4174. }
  4175. fputc( '\n', fp );
  4176. /* Initialize another line */
  4177. if ( line_end + 1 < textlen )
  4178. {
  4179. unsigned i;
  4180. for (i = 0; i < lmargin; i++ )
  4181. { fputc( ' ', fp ); }
  4182. line_end = textlen;
  4183. }
  4184. /* If we have to print another line, get also the last char. */
  4185. line_end++;
  4186. } /* lines of text */
  4187. }
  4188. /**
  4189. * Prints the glossary in strict GNU format.
  4190. * Differences to arg_print_glossary() are:
  4191. * - wraps lines after 80 chars
  4192. * - indents lines without shortops
  4193. * - does not accept formatstrings
  4194. *
  4195. * Contributed by Uli Fouquet
  4196. */
  4197. void arg_print_glossary_gnu(FILE *fp, void * *argtable )
  4198. {
  4199. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  4200. int tabindex;
  4201. for(tabindex = 0; !(table[tabindex]->flag & ARG_TERMINATOR); tabindex++)
  4202. {
  4203. if (table[tabindex]->glossary)
  4204. {
  4205. char syntax[200] = "";
  4206. const char *shortopts = table[tabindex]->shortopts;
  4207. const char *longopts = table[tabindex]->longopts;
  4208. const char *datatype = table[tabindex]->datatype;
  4209. const char *glossary = table[tabindex]->glossary;
  4210. if ( !shortopts && longopts )
  4211. {
  4212. /* Indent trailing line by 4 spaces... */
  4213. memset( syntax, ' ', 4 );
  4214. *(syntax + 4) = '\0';
  4215. }
  4216. arg_cat_optionv(syntax,
  4217. sizeof(syntax),
  4218. shortopts,
  4219. longopts,
  4220. datatype,
  4221. table[tabindex]->flag & ARG_HASOPTVALUE,
  4222. ", ");
  4223. /* If syntax fits not into column, print glossary in new line... */
  4224. if ( strlen(syntax) > 25 )
  4225. {
  4226. fprintf( fp, " %-25s %s\n", syntax, "" );
  4227. *syntax = '\0';
  4228. }
  4229. fprintf( fp, " %-25s ", syntax );
  4230. arg_print_formatted( fp, 28, 79, glossary );
  4231. }
  4232. } /* for each table entry */
  4233. fputc( '\n', fp );
  4234. }
  4235. /**
  4236. * Checks the argtable[] array for NULL entries and returns 1
  4237. * if any are found, zero otherwise.
  4238. */
  4239. int arg_nullcheck(void * *argtable)
  4240. {
  4241. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  4242. int tabindex;
  4243. /*printf("arg_nullcheck(%p)\n",argtable);*/
  4244. if (!table)
  4245. return 1;
  4246. tabindex = 0;
  4247. do
  4248. {
  4249. /*printf("argtable[%d]=%p\n",tabindex,argtable[tabindex]);*/
  4250. if (!table[tabindex])
  4251. return 1;
  4252. } while(!(table[tabindex++]->flag & ARG_TERMINATOR));
  4253. return 0;
  4254. }
  4255. /*
  4256. * arg_free() is deprecated in favour of arg_freetable() due to a flaw in its design.
  4257. * The flaw results in memory leak in the (very rare) case that an intermediate
  4258. * entry in the argtable array failed its memory allocation while others following
  4259. * that entry were still allocated ok. Those subsequent allocations will not be
  4260. * deallocated by arg_free().
  4261. * Despite the unlikeliness of the problem occurring, and the even unlikelier event
  4262. * that it has any deliterious effect, it is fixed regardless by replacing arg_free()
  4263. * with the newer arg_freetable() function.
  4264. * We still keep arg_free() for backwards compatibility.
  4265. */
  4266. void arg_free(void * *argtable)
  4267. {
  4268. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  4269. int tabindex = 0;
  4270. int flag;
  4271. /*printf("arg_free(%p)\n",argtable);*/
  4272. do
  4273. {
  4274. /*
  4275. if we encounter a NULL entry then somewhat incorrectly we presume
  4276. we have come to the end of the array. It isnt strictly true because
  4277. an intermediate entry could be NULL with other non-NULL entries to follow.
  4278. The subsequent argtable entries would then not be freed as they should.
  4279. */
  4280. if (table[tabindex] == NULL)
  4281. break;
  4282. flag = table[tabindex]->flag;
  4283. free(table[tabindex]);
  4284. table[tabindex++] = NULL;
  4285. } while(!(flag & ARG_TERMINATOR));
  4286. }
  4287. /* frees each non-NULL element of argtable[], where n is the size of the number of entries in the array */
  4288. void arg_freetable(void * *argtable, size_t n)
  4289. {
  4290. struct arg_hdr * *table = (struct arg_hdr * *)argtable;
  4291. size_t tabindex = 0;
  4292. /*printf("arg_freetable(%p)\n",argtable);*/
  4293. for (tabindex = 0; tabindex < n; tabindex++)
  4294. {
  4295. if (table[tabindex] == NULL)
  4296. continue;
  4297. free(table[tabindex]);
  4298. table[tabindex] = NULL;
  4299. };
  4300. }