argtable3.c 151 KB

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