3
0

msh.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Minix shell port for busybox
  4. *
  5. * This version of the Minix shell was adapted for use in busybox
  6. * by Erik Andersen <andersen@codepoet.org>
  7. *
  8. * - backtick expansion did not work properly
  9. * Jonas Holmberg <jonas.holmberg@axis.com>
  10. * Robert Schwebel <r.schwebel@pengutronix.de>
  11. * Erik Andersen <andersen@codepoet.org>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * Original copyright notice is retained at the end of this file.
  28. */
  29. #include <ctype.h>
  30. #include <dirent.h>
  31. #include <errno.h>
  32. #include <fcntl.h>
  33. #include <limits.h>
  34. #include <setjmp.h>
  35. #include <signal.h>
  36. #include <stddef.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <time.h>
  41. #include <unistd.h>
  42. #include <sys/stat.h>
  43. #include <sys/times.h>
  44. #include <sys/types.h>
  45. #include <sys/wait.h>
  46. #include "cmdedit.h"
  47. #include "busybox.h"
  48. /* Conditional use of "register" keyword */
  49. #define REGISTER register
  50. /*#define MSHDEBUG 1*/
  51. #ifdef MSHDEBUG
  52. int mshdbg = 0;
  53. #define DBGPRINTF(x) if(mshdbg>0)printf x
  54. #define DBGPRINTF0(x) if(mshdbg>0)printf x
  55. #define DBGPRINTF1(x) if(mshdbg>1)printf x
  56. #define DBGPRINTF2(x) if(mshdbg>2)printf x
  57. #define DBGPRINTF3(x) if(mshdbg>3)printf x
  58. #define DBGPRINTF4(x) if(mshdbg>4)printf x
  59. #define DBGPRINTF5(x) if(mshdbg>5)printf x
  60. #define DBGPRINTF6(x) if(mshdbg>6)printf x
  61. #define DBGPRINTF7(x) if(mshdbg>7)printf x
  62. #define DBGPRINTF8(x) if(mshdbg>8)printf x
  63. #define DBGPRINTF9(x) if(mshdbg>9)printf x
  64. int mshdbg_rc = 0;
  65. #define RCPRINTF(x) if(mshdbg_rc)printf x
  66. #else
  67. #define DBGPRINTF(x)
  68. #define DBGPRINTF0(x)
  69. #define DBGPRINTF1(x)
  70. #define DBGPRINTF2(x)
  71. #define DBGPRINTF3(x)
  72. #define DBGPRINTF4(x)
  73. #define DBGPRINTF5(x)
  74. #define DBGPRINTF6(x)
  75. #define DBGPRINTF7(x)
  76. #define DBGPRINTF8(x)
  77. #define DBGPRINTF9(x)
  78. #define RCPRINTF(x)
  79. #endif /* MSHDEBUG */
  80. /* -------- sh.h -------- */
  81. /*
  82. * shell
  83. */
  84. #define LINELIM 2100
  85. #define NPUSH 8 /* limit to input nesting */
  86. #undef NOFILE
  87. #define NOFILE 20 /* Number of open files */
  88. #define NUFILE 10 /* Number of user-accessible files */
  89. #define FDBASE 10 /* First file usable by Shell */
  90. /*
  91. * values returned by wait
  92. */
  93. #define WAITSIG(s) ((s)&0177)
  94. #define WAITVAL(s) (((s)>>8)&0377)
  95. #define WAITCORE(s) (((s)&0200)!=0)
  96. /*
  97. * library and system definitions
  98. */
  99. typedef void xint; /* base type of jmp_buf, for not broken compilers */
  100. /*
  101. * shell components
  102. */
  103. #define QUOTE 0200
  104. #define NOBLOCK ((struct op *)NULL)
  105. #define NOWORD ((char *)NULL)
  106. #define NOWORDS ((char **)NULL)
  107. #define NOPIPE ((int *)NULL)
  108. /*
  109. * Description of a command or an operation on commands.
  110. * Might eventually use a union.
  111. */
  112. struct op {
  113. int type; /* operation type, see below */
  114. char **words; /* arguments to a command */
  115. struct ioword **ioact; /* IO actions (eg, < > >>) */
  116. struct op *left;
  117. struct op *right;
  118. char *str; /* identifier for case and for */
  119. };
  120. #define TCOM 1 /* command */
  121. #define TPAREN 2 /* (c-list) */
  122. #define TPIPE 3 /* a | b */
  123. #define TLIST 4 /* a [&;] b */
  124. #define TOR 5 /* || */
  125. #define TAND 6 /* && */
  126. #define TFOR 7
  127. #define TDO 8
  128. #define TCASE 9
  129. #define TIF 10
  130. #define TWHILE 11
  131. #define TUNTIL 12
  132. #define TELIF 13
  133. #define TPAT 14 /* pattern in case */
  134. #define TBRACE 15 /* {c-list} */
  135. #define TASYNC 16 /* c & */
  136. /* Added to support "." file expansion */
  137. #define TDOT 17
  138. /* Strings for names to make debug easier */
  139. char *T_CMD_NAMES[] = {
  140. "PLACEHOLDER",
  141. "TCOM",
  142. "TPAREN",
  143. "TPIPE",
  144. "TLIST",
  145. "TOR",
  146. "TAND",
  147. "TFOR",
  148. "TDO",
  149. "TCASE",
  150. "TIF",
  151. "TWHILE",
  152. "TUNTIL",
  153. "TELIF",
  154. "TPAT",
  155. "TBRACE",
  156. "TASYNC",
  157. "TDOT",
  158. };
  159. /*
  160. * actions determining the environment of a process
  161. */
  162. #define BIT(i) (1<<(i))
  163. #define FEXEC BIT(0) /* execute without forking */
  164. #if 0 /* Original value */
  165. #define AREASIZE (65000)
  166. #else
  167. #define AREASIZE (90000)
  168. #endif
  169. /*
  170. * flags to control evaluation of words
  171. */
  172. #define DOSUB 1 /* interpret $, `, and quotes */
  173. #define DOBLANK 2 /* perform blank interpretation */
  174. #define DOGLOB 4 /* interpret [?* */
  175. #define DOKEY 8 /* move words with `=' to 2nd arg. list */
  176. #define DOTRIM 16 /* trim resulting string */
  177. #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
  178. /* PROTOTYPES */
  179. static int newfile(char *s);
  180. static char *findeq(char *cp);
  181. static char *cclass(char *p, int sub);
  182. static void initarea(void);
  183. extern int msh_main(int argc, char **argv);
  184. struct brkcon {
  185. jmp_buf brkpt;
  186. struct brkcon *nextlev;
  187. };
  188. /*
  189. * redirection
  190. */
  191. struct ioword {
  192. short io_unit; /* unit affected */
  193. short io_flag; /* action (below) */
  194. char *io_name; /* file name */
  195. };
  196. #define IOREAD 1 /* < */
  197. #define IOHERE 2 /* << (here file) */
  198. #define IOWRITE 4 /* > */
  199. #define IOCAT 8 /* >> */
  200. #define IOXHERE 16 /* ${}, ` in << */
  201. #define IODUP 32 /* >&digit */
  202. #define IOCLOSE 64 /* >&- */
  203. #define IODEFAULT (-1) /* token for default IO unit */
  204. /*
  205. * parsing & execution environment
  206. */
  207. static struct env {
  208. char *linep;
  209. struct io *iobase;
  210. struct io *iop;
  211. xint *errpt; /* void * */
  212. int iofd;
  213. struct env *oenv;
  214. } e;
  215. /*
  216. * flags:
  217. * -e: quit on error
  218. * -k: look for name=value everywhere on command line
  219. * -n: no execution
  220. * -t: exit after reading and executing one command
  221. * -v: echo as read
  222. * -x: trace
  223. * -u: unset variables net diagnostic
  224. */
  225. static char *flag;
  226. static char *null; /* null value for variable */
  227. static int intr; /* interrupt pending */
  228. static char *trap[_NSIG + 1];
  229. static char ourtrap[_NSIG + 1];
  230. static int trapset; /* trap pending */
  231. static int heedint; /* heed interrupt signals */
  232. static int yynerrs; /* yacc */
  233. static char line[LINELIM];
  234. static char *elinep;
  235. /*
  236. * other functions
  237. */
  238. static int (*inbuilt(char *s)) (struct op *);
  239. static char *rexecve(char *c, char **v, char **envp);
  240. static char *space(int n);
  241. static char *strsave(char *s, int a);
  242. static char *evalstr(char *cp, int f);
  243. static char *putn(int n);
  244. static char *itoa(int n);
  245. static char *unquote(char *as);
  246. static struct var *lookup(char *n);
  247. static int rlookup(char *n);
  248. static struct wdblock *glob(char *cp, struct wdblock *wb);
  249. static int my_getc(int ec);
  250. static int subgetc(int ec, int quoted);
  251. static char **makenv(int all, struct wdblock *wb);
  252. static char **eval(char **ap, int f);
  253. static int setstatus(int s);
  254. static int waitfor(int lastpid, int canintr);
  255. static void onintr(int s); /* SIGINT handler */
  256. static int newenv(int f);
  257. static void quitenv(void);
  258. static void err(char *s);
  259. static int anys(char *s1, char *s2);
  260. static int any(int c, char *s);
  261. static void next(int f);
  262. static void setdash(void);
  263. static void onecommand(void);
  264. static void runtrap(int i);
  265. static int gmatch(char *s, char *p);
  266. /*
  267. * error handling
  268. */
  269. static void leave(void); /* abort shell (or fail in subshell) */
  270. static void fail(void); /* fail but return to process next command */
  271. static void warn(char *s);
  272. static void sig(int i); /* default signal handler */
  273. /* -------- area stuff -------- */
  274. #define REGSIZE sizeof(struct region)
  275. #define GROWBY (256)
  276. /* #define SHRINKBY (64) */
  277. #undef SHRINKBY
  278. #define FREE (32767)
  279. #define BUSY (0)
  280. #define ALIGN (sizeof(int)-1)
  281. struct region {
  282. struct region *next;
  283. int area;
  284. };
  285. /* -------- grammar stuff -------- */
  286. typedef union {
  287. char *cp;
  288. char **wp;
  289. int i;
  290. struct op *o;
  291. } YYSTYPE;
  292. #define WORD 256
  293. #define LOGAND 257
  294. #define LOGOR 258
  295. #define BREAK 259
  296. #define IF 260
  297. #define THEN 261
  298. #define ELSE 262
  299. #define ELIF 263
  300. #define FI 264
  301. #define CASE 265
  302. #define ESAC 266
  303. #define FOR 267
  304. #define WHILE 268
  305. #define UNTIL 269
  306. #define DO 270
  307. #define DONE 271
  308. #define IN 272
  309. /* Added for "." file expansion */
  310. #define DOT 273
  311. #define YYERRCODE 300
  312. /* flags to yylex */
  313. #define CONTIN 01 /* skip new lines to complete command */
  314. #define SYNTAXERR zzerr()
  315. static struct op *pipeline(int cf);
  316. static struct op *andor(void);
  317. static struct op *c_list(void);
  318. static int synio(int cf);
  319. static void musthave(int c, int cf);
  320. static struct op *simple(void);
  321. static struct op *nested(int type, int mark);
  322. static struct op *command(int cf);
  323. static struct op *dogroup(int onlydone);
  324. static struct op *thenpart(void);
  325. static struct op *elsepart(void);
  326. static struct op *caselist(void);
  327. static struct op *casepart(void);
  328. static char **pattern(void);
  329. static char **wordlist(void);
  330. static struct op *list(struct op *t1, struct op *t2);
  331. static struct op *block(int type, struct op *t1, struct op *t2, char **wp);
  332. static struct op *newtp(void);
  333. static struct op *namelist(struct op *t);
  334. static char **copyw(void);
  335. static void word(char *cp);
  336. static struct ioword **copyio(void);
  337. static struct ioword *io(int u, int f, char *cp);
  338. static void zzerr(void);
  339. static void yyerror(char *s);
  340. static int yylex(int cf);
  341. static int collect(int c, int c1);
  342. static int dual(int c);
  343. static void diag(int ec);
  344. static char *tree(unsigned size);
  345. /* -------- var.h -------- */
  346. struct var {
  347. char *value;
  348. char *name;
  349. struct var *next;
  350. char status;
  351. };
  352. #define COPYV 1 /* flag to setval, suggesting copy */
  353. #define RONLY 01 /* variable is read-only */
  354. #define EXPORT 02 /* variable is to be exported */
  355. #define GETCELL 04 /* name & value space was got with getcell */
  356. static int yyparse(void);
  357. static struct var *lookup(char *n);
  358. static void setval(struct var *vp, char *val);
  359. static void nameval(struct var *vp, char *val, char *name);
  360. static void export(struct var *vp);
  361. static void ronly(struct var *vp);
  362. static int isassign(char *s);
  363. static int checkname(char *cp);
  364. static int assign(char *s, int cf);
  365. static void putvlist(int f, int out);
  366. static int eqname(char *n1, char *n2);
  367. static int execute(struct op *t, int *pin, int *pout, int act);
  368. /* -------- io.h -------- */
  369. /* io buffer */
  370. struct iobuf {
  371. unsigned id; /* buffer id */
  372. char buf[512]; /* buffer */
  373. char *bufp; /* pointer into buffer */
  374. char *ebufp; /* pointer to end of buffer */
  375. };
  376. /* possible arguments to an IO function */
  377. struct ioarg {
  378. char *aword;
  379. char **awordlist;
  380. int afile; /* file descriptor */
  381. unsigned afid; /* buffer id */
  382. long afpos; /* file position */
  383. struct iobuf *afbuf; /* buffer for this file */
  384. };
  385. //static struct ioarg ioargstack[NPUSH];
  386. #define AFID_NOBUF (~0)
  387. #define AFID_ID 0
  388. /* an input generator's state */
  389. struct io {
  390. int (*iofn) (struct ioarg *, struct io *);
  391. struct ioarg *argp;
  392. int peekc;
  393. char prev; /* previous character read by readc() */
  394. char nlcount; /* for `'s */
  395. char xchar; /* for `'s */
  396. char task; /* reason for pushed IO */
  397. };
  398. //static struct io iostack[NPUSH];
  399. #define XOTHER 0 /* none of the below */
  400. #define XDOLL 1 /* expanding ${} */
  401. #define XGRAVE 2 /* expanding `'s */
  402. #define XIO 3 /* file IO */
  403. /* in substitution */
  404. #define INSUB() (e.iop->task == XGRAVE || e.iop->task == XDOLL)
  405. /*
  406. * input generators for IO structure
  407. */
  408. static int nlchar(struct ioarg *ap);
  409. static int strchar(struct ioarg *ap);
  410. static int qstrchar(struct ioarg *ap);
  411. static int filechar(struct ioarg *ap);
  412. static int herechar(struct ioarg *ap);
  413. static int linechar(struct ioarg *ap);
  414. static int gravechar(struct ioarg *ap, struct io *iop);
  415. static int qgravechar(struct ioarg *ap, struct io *iop);
  416. static int dolchar(struct ioarg *ap);
  417. static int wdchar(struct ioarg *ap);
  418. static void scraphere(void);
  419. static void freehere(int area);
  420. static void gethere(void);
  421. static void markhere(char *s, struct ioword *iop);
  422. static int herein(char *hname, int xdoll);
  423. static int run(struct ioarg *argp, int (*f) (struct ioarg *));
  424. /*
  425. * IO functions
  426. */
  427. static int eofc(void);
  428. static int readc(void);
  429. static void unget(int c);
  430. static void ioecho(int c);
  431. static void prs(char *s);
  432. static void prn(unsigned u);
  433. static void closef(int i);
  434. static void closeall(void);
  435. /*
  436. * IO control
  437. */
  438. static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
  439. static int remap(int fd);
  440. static int openpipe(int *pv);
  441. static void closepipe(int *pv);
  442. static struct io *setbase(struct io *ip);
  443. #define PUSHIO(what,arg,gen) ((temparg.what = (arg)),pushio(&temparg,(gen)))
  444. #define RUN(what,arg,gen) ((temparg.what = (arg)), run(&temparg,(gen)))
  445. /* -------- word.h -------- */
  446. #define NSTART 16 /* default number of words to allow for initially */
  447. struct wdblock {
  448. short w_bsize;
  449. short w_nword;
  450. /* bounds are arbitrary */
  451. char *w_words[1];
  452. };
  453. static struct wdblock *addword(char *wd, struct wdblock *wb);
  454. static struct wdblock *newword(int nw);
  455. static char **getwords(struct wdblock *wb);
  456. /* -------- area.h -------- */
  457. /*
  458. * storage allocation
  459. */
  460. static char *getcell(unsigned nbytes);
  461. static void garbage(void);
  462. static void setarea(char *cp, int a);
  463. static int getarea(char *cp);
  464. static void freearea(int a);
  465. static void freecell(char *cp);
  466. static int areanum; /* current allocation area */
  467. #define NEW(type) (type *)getcell(sizeof(type))
  468. #define DELETE(obj) freecell((char *)obj)
  469. /* -------- misc stuff -------- */
  470. static int forkexec(struct op *t, int *pin, int *pout, int act, char **wp);
  471. static int iosetup(struct ioword *iop, int pipein, int pipeout);
  472. static void echo(char **wp);
  473. static struct op **find1case(struct op *t, char *w);
  474. static struct op *findcase(struct op *t, char *w);
  475. static void brkset(struct brkcon *bc);
  476. static int dolabel(struct op *t);
  477. static int dohelp(struct op *t);
  478. static int dochdir(struct op *t);
  479. static int doshift(struct op *t);
  480. static int dologin(struct op *t);
  481. static int doumask(struct op *t);
  482. static int doexec(struct op *t);
  483. static int dodot(struct op *t);
  484. static int dowait(struct op *t);
  485. static int doread(struct op *t);
  486. static int doeval(struct op *t);
  487. static int dotrap(struct op *t);
  488. static int getsig(char *s);
  489. static void setsig(int n, sighandler_t f);
  490. static int getn(char *as);
  491. static int dobreak(struct op *t);
  492. static int docontinue(struct op *t);
  493. static int brkcontin(char *cp, int val);
  494. static int doexit(struct op *t);
  495. static int doexport(struct op *t);
  496. static int doreadonly(struct op *t);
  497. static void rdexp(char **wp, void (*f) (struct var *), int key);
  498. static void badid(char *s);
  499. static int doset(struct op *t);
  500. static void varput(char *s, int out);
  501. static int dotimes(struct op *t);
  502. static int expand(char *cp, struct wdblock **wbp, int f);
  503. static char *blank(int f);
  504. static int dollar(int quoted);
  505. static int grave(int quoted);
  506. static void globname(char *we, char *pp);
  507. static char *generate(char *start1, char *end1, char *middle, char *end);
  508. static int anyspcl(struct wdblock *wb);
  509. static int xstrcmp(char *p1, char *p2);
  510. static void glob0(char *a0, unsigned int a1, int a2,
  511. int (*a3) (char *, char *));
  512. static void glob1(char *base, char *lim);
  513. static void glob2(char *i, char *j);
  514. static void glob3(char *i, char *j, char *k);
  515. static void readhere(char **name, char *s, int ec);
  516. static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
  517. static int xxchar(struct ioarg *ap);
  518. struct here {
  519. char *h_tag;
  520. int h_dosub;
  521. struct ioword *h_iop;
  522. struct here *h_next;
  523. };
  524. static char *signame[] = {
  525. "Signal 0",
  526. "Hangup",
  527. (char *) NULL, /* interrupt */
  528. "Quit",
  529. "Illegal instruction",
  530. "Trace/BPT trap",
  531. "Abort",
  532. "Bus error",
  533. "Floating Point Exception",
  534. "Killed",
  535. "SIGUSR1",
  536. "SIGSEGV",
  537. "SIGUSR2",
  538. (char *) NULL, /* broken pipe */
  539. "Alarm clock",
  540. "Terminated",
  541. };
  542. #define NSIGNAL (sizeof(signame)/sizeof(signame[0]))
  543. struct res {
  544. char *r_name;
  545. int r_val;
  546. };
  547. static struct res restab[] = {
  548. {"for", FOR},
  549. {"case", CASE},
  550. {"esac", ESAC},
  551. {"while", WHILE},
  552. {"do", DO},
  553. {"done", DONE},
  554. {"if", IF},
  555. {"in", IN},
  556. {"then", THEN},
  557. {"else", ELSE},
  558. {"elif", ELIF},
  559. {"until", UNTIL},
  560. {"fi", FI},
  561. {";;", BREAK},
  562. {"||", LOGOR},
  563. {"&&", LOGAND},
  564. {"{", '{'},
  565. {"}", '}'},
  566. {".", DOT},
  567. {0, 0},
  568. };
  569. struct builtincmd {
  570. const char *name;
  571. int (*builtinfunc) (struct op * t);
  572. };
  573. static const struct builtincmd builtincmds[] = {
  574. {".", dodot},
  575. {":", dolabel},
  576. {"break", dobreak},
  577. {"cd", dochdir},
  578. {"continue", docontinue},
  579. {"eval", doeval},
  580. {"exec", doexec},
  581. {"exit", doexit},
  582. {"export", doexport},
  583. {"help", dohelp},
  584. {"login", dologin},
  585. {"newgrp", dologin},
  586. {"read", doread},
  587. {"readonly", doreadonly},
  588. {"set", doset},
  589. {"shift", doshift},
  590. {"times", dotimes},
  591. {"trap", dotrap},
  592. {"umask", doumask},
  593. {"wait", dowait},
  594. {0, 0}
  595. };
  596. struct op *scantree(struct op *);
  597. static struct op *dowholefile(int, int);
  598. /* Globals */
  599. extern char **environ; /* environment pointer */
  600. static char **dolv;
  601. static int dolc;
  602. static int exstat;
  603. static char gflg;
  604. static int interactive = 0; /* Is this an interactive shell */
  605. static int execflg;
  606. static int multiline; /* \n changed to ; */
  607. static struct op *outtree; /* result from parser */
  608. static xint *failpt;
  609. static xint *errpt;
  610. static struct brkcon *brklist;
  611. static int isbreak;
  612. static struct wdblock *wdlist;
  613. static struct wdblock *iolist;
  614. static char *trap[_NSIG + 1];
  615. static char ourtrap[_NSIG + 1];
  616. static int trapset; /* trap pending */
  617. static int yynerrs; /* yacc */
  618. static char line[LINELIM];
  619. #ifdef MSHDEBUG
  620. static struct var *mshdbg_var;
  621. #endif
  622. static struct var *vlist; /* dictionary */
  623. static struct var *homedir; /* home directory */
  624. static struct var *prompt; /* main prompt */
  625. static struct var *cprompt; /* continuation prompt */
  626. static struct var *path; /* search path for commands */
  627. static struct var *shell; /* shell to interpret command files */
  628. static struct var *ifs; /* field separators */
  629. static int areanum; /* current allocation area */
  630. static int intr;
  631. static int inparse;
  632. static char flags['z' - 'a' + 1];
  633. static char *flag = flags - 'a';
  634. static char *null = "";
  635. static int heedint = 1;
  636. static void (*qflag) (int) = SIG_IGN;
  637. static int startl;
  638. static int peeksym;
  639. static int nlseen;
  640. static int iounit = IODEFAULT;
  641. static YYSTYPE yylval;
  642. static char *elinep = line + sizeof(line) - 5;
  643. static struct ioarg temparg = { 0, 0, 0, AFID_NOBUF, 0 }; /* temporary for PUSHIO */
  644. static struct ioarg ioargstack[NPUSH];
  645. static struct io iostack[NPUSH];
  646. static struct iobuf sharedbuf = { AFID_NOBUF };
  647. static struct iobuf mainbuf = { AFID_NOBUF };
  648. static unsigned bufid = AFID_ID; /* buffer id counter */
  649. static struct here *inhere; /* list of hear docs while parsing */
  650. static struct here *acthere; /* list of active here documents */
  651. static struct region *areabot; /* bottom of area */
  652. static struct region *areatop; /* top of area */
  653. static struct region *areanxt; /* starting point of scan */
  654. static void *brktop;
  655. static void *brkaddr;
  656. static struct env e = {
  657. line, /* linep: char ptr */
  658. iostack, /* iobase: struct io ptr */
  659. iostack - 1, /* iop: struct io ptr */
  660. (xint *) NULL, /* errpt: void ptr for errors? */
  661. FDBASE, /* iofd: file desc */
  662. (struct env *) NULL /* oenv: struct env ptr */
  663. };
  664. #ifdef MSHDEBUG
  665. void print_t(struct op *t)
  666. {
  667. DBGPRINTF(("T: t=0x%x, type %s, words=0x%x, IOword=0x%x\n", t,
  668. T_CMD_NAMES[t->type], t->words, t->ioact));
  669. if (t->words) {
  670. DBGPRINTF(("T: W1: %s", t->words[0]));
  671. }
  672. return;
  673. }
  674. void print_tree(struct op *head)
  675. {
  676. if (head == NULL) {
  677. DBGPRINTF(("PRINT_TREE: no tree\n"));
  678. return;
  679. }
  680. DBGPRINTF(("NODE: 0x%x, left 0x%x, right 0x%x\n", head, head->left,
  681. head->right));
  682. if (head->left)
  683. print_tree(head->left);
  684. if (head->right)
  685. print_tree(head->right);
  686. return;
  687. }
  688. #endif /* MSHDEBUG */
  689. #ifdef CONFIG_FEATURE_COMMAND_EDITING
  690. static char *current_prompt;
  691. #endif
  692. /* -------- sh.c -------- */
  693. /*
  694. * shell
  695. */
  696. extern int msh_main(int argc, char **argv)
  697. {
  698. REGISTER int f;
  699. REGISTER char *s;
  700. int cflag;
  701. char *name, **ap;
  702. int (*iof) (struct ioarg *);
  703. DBGPRINTF(("MSH_MAIN: argc %d, environ 0x%x\n", argc, environ));
  704. initarea();
  705. if ((ap = environ) != NULL) {
  706. while (*ap)
  707. assign(*ap++, !COPYV);
  708. for (ap = environ; *ap;)
  709. export(lookup(*ap++));
  710. }
  711. closeall();
  712. areanum = 1;
  713. shell = lookup("SHELL");
  714. if (shell->value == null)
  715. setval(shell, (char *)DEFAULT_SHELL);
  716. export(shell);
  717. homedir = lookup("HOME");
  718. if (homedir->value == null)
  719. setval(homedir, "/");
  720. export(homedir);
  721. setval(lookup("$"), putn(getpid()));
  722. path = lookup("PATH");
  723. if (path->value == null) {
  724. if (geteuid() == 0)
  725. setval(path, "/sbin:/bin:/usr/sbin:/usr/bin");
  726. else
  727. setval(path, "/bin:/usr/bin");
  728. }
  729. export(path);
  730. ifs = lookup("IFS");
  731. if (ifs->value == null)
  732. setval(ifs, " \t\n");
  733. #ifdef MSHDEBUG
  734. mshdbg_var = lookup("MSHDEBUG");
  735. if (mshdbg_var->value == null)
  736. setval(mshdbg_var, "0");
  737. #endif
  738. prompt = lookup("PS1");
  739. #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
  740. if (prompt->value == null)
  741. #endif
  742. setval(prompt, "$ ");
  743. if (geteuid() == 0) {
  744. setval(prompt, "# ");
  745. prompt->status &= ~EXPORT;
  746. }
  747. cprompt = lookup("PS2");
  748. #ifdef CONFIG_FEATURE_SH_FANCY_PROMPT
  749. if (cprompt->value == null)
  750. #endif
  751. setval(cprompt, "> ");
  752. iof = filechar;
  753. cflag = 0;
  754. name = *argv++;
  755. if (--argc >= 1) {
  756. if (argv[0][0] == '-' && argv[0][1] != '\0') {
  757. for (s = argv[0] + 1; *s; s++)
  758. switch (*s) {
  759. case 'c':
  760. prompt->status &= ~EXPORT;
  761. cprompt->status &= ~EXPORT;
  762. setval(prompt, "");
  763. setval(cprompt, "");
  764. cflag = 1;
  765. if (--argc > 0)
  766. PUSHIO(aword, *++argv, iof = nlchar);
  767. break;
  768. case 'q':
  769. qflag = SIG_DFL;
  770. break;
  771. case 's':
  772. /* standard input */
  773. break;
  774. case 't':
  775. prompt->status &= ~EXPORT;
  776. setval(prompt, "");
  777. iof = linechar;
  778. break;
  779. case 'i':
  780. interactive++;
  781. default:
  782. if (*s >= 'a' && *s <= 'z')
  783. flag[(int) *s]++;
  784. }
  785. } else {
  786. argv--;
  787. argc++;
  788. }
  789. if (iof == filechar && --argc > 0) {
  790. setval(prompt, "");
  791. setval(cprompt, "");
  792. prompt->status &= ~EXPORT;
  793. cprompt->status &= ~EXPORT;
  794. /* Shell is non-interactive, activate printf-based debug */
  795. #ifdef MSHDEBUG
  796. mshdbg = (int) (((char) (mshdbg_var->value[0])) - '0');
  797. if (mshdbg < 0)
  798. mshdbg = 0;
  799. #endif
  800. DBGPRINTF(("MSH_MAIN: calling newfile()\n"));
  801. if (newfile(name = *++argv))
  802. exit(1); /* Exit on error */
  803. }
  804. }
  805. setdash();
  806. /* This won't be true if PUSHIO has been called, say from newfile() above */
  807. if (e.iop < iostack) {
  808. PUSHIO(afile, 0, iof);
  809. if (isatty(0) && isatty(1) && !cflag) {
  810. interactive++;
  811. #ifndef CONFIG_FEATURE_SH_EXTRA_QUIET
  812. #ifdef MSHDEBUG
  813. printf("\n\n" BB_BANNER " Built-in shell (msh with debug)\n");
  814. #else
  815. printf("\n\n" BB_BANNER " Built-in shell (msh)\n");
  816. #endif
  817. printf("Enter 'help' for a list of built-in commands.\n\n");
  818. #endif
  819. }
  820. }
  821. signal(SIGQUIT, qflag);
  822. if (name && name[0] == '-') {
  823. interactive++;
  824. if ((f = open(".profile", 0)) >= 0)
  825. next(remap(f));
  826. if ((f = open("/etc/profile", 0)) >= 0)
  827. next(remap(f));
  828. }
  829. if (interactive)
  830. signal(SIGTERM, sig);
  831. if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  832. signal(SIGINT, onintr);
  833. dolv = argv;
  834. dolc = argc;
  835. dolv[0] = name;
  836. if (dolc > 1) {
  837. for (ap = ++argv; --argc > 0;) {
  838. if (assign(*ap = *argv++, !COPYV)) {
  839. dolc--; /* keyword */
  840. } else {
  841. ap++;
  842. }
  843. }
  844. }
  845. setval(lookup("#"), putn((--dolc < 0) ? (dolc = 0) : dolc));
  846. DBGPRINTF(("MSH_MAIN: begin FOR loop, interactive %d, e.iop 0x%x, iostack 0x%x\n", interactive, e.iop, iostack));
  847. for (;;) {
  848. if (interactive && e.iop <= iostack) {
  849. #ifdef CONFIG_FEATURE_COMMAND_EDITING
  850. current_prompt = prompt->value;
  851. #else
  852. prs(prompt->value);
  853. #endif
  854. }
  855. onecommand();
  856. /* Ensure that getenv("PATH") stays current */
  857. setenv("PATH", path->value, 1);
  858. }
  859. DBGPRINTF(("MSH_MAIN: returning.\n"));
  860. }
  861. static void setdash()
  862. {
  863. REGISTER char *cp;
  864. REGISTER int c;
  865. char m['z' - 'a' + 1];
  866. cp = m;
  867. for (c = 'a'; c <= 'z'; c++)
  868. if (flag[(int) c])
  869. *cp++ = c;
  870. *cp = 0;
  871. setval(lookup("-"), m);
  872. }
  873. static int newfile(s)
  874. REGISTER char *s;
  875. {
  876. REGISTER int f;
  877. DBGPRINTF7(("NEWFILE: opening %s\n", s));
  878. if (strcmp(s, "-") != 0) {
  879. DBGPRINTF(("NEWFILE: s is %s\n", s));
  880. f = open(s, 0);
  881. if (f < 0) {
  882. prs(s);
  883. err(": cannot open");
  884. return (1);
  885. }
  886. } else
  887. f = 0;
  888. next(remap(f));
  889. return (0);
  890. }
  891. struct op *scantree(head)
  892. struct op *head;
  893. {
  894. struct op *dotnode;
  895. if (head == NULL)
  896. return (NULL);
  897. if (head->left != NULL) {
  898. dotnode = scantree(head->left);
  899. if (dotnode)
  900. return (dotnode);
  901. }
  902. if (head->right != NULL) {
  903. dotnode = scantree(head->right);
  904. if (dotnode)
  905. return (dotnode);
  906. }
  907. if (head->words == NULL)
  908. return (NULL);
  909. DBGPRINTF5(("SCANTREE: checking node 0x%x\n", head));
  910. if ((head->type != TDOT) && (strcmp(".", head->words[0]) == 0)) {
  911. DBGPRINTF5(("SCANTREE: dot found in node 0x%x\n", head));
  912. return (head);
  913. }
  914. return (NULL);
  915. }
  916. static void onecommand()
  917. {
  918. REGISTER int i;
  919. jmp_buf m1;
  920. DBGPRINTF(("ONECOMMAND: enter, outtree=0x%x\n", outtree));
  921. while (e.oenv)
  922. quitenv();
  923. areanum = 1;
  924. freehere(areanum);
  925. freearea(areanum);
  926. garbage();
  927. wdlist = 0;
  928. iolist = 0;
  929. e.errpt = 0;
  930. e.linep = line;
  931. yynerrs = 0;
  932. multiline = 0;
  933. inparse = 1;
  934. intr = 0;
  935. execflg = 0;
  936. setjmp(failpt = m1); /* Bruce Evans' fix */
  937. if (setjmp(failpt = m1) || yyparse() || intr) {
  938. DBGPRINTF(("ONECOMMAND: this is not good.\n"));
  939. while (e.oenv)
  940. quitenv();
  941. scraphere();
  942. if (!interactive && intr)
  943. leave();
  944. inparse = 0;
  945. intr = 0;
  946. return;
  947. }
  948. inparse = 0;
  949. brklist = 0;
  950. intr = 0;
  951. execflg = 0;
  952. if (!flag['n']) {
  953. DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=0x%x\n",
  954. outtree));
  955. execute(outtree, NOPIPE, NOPIPE, 0);
  956. }
  957. if (!interactive && intr) {
  958. execflg = 0;
  959. leave();
  960. }
  961. if ((i = trapset) != 0) {
  962. trapset = 0;
  963. runtrap(i);
  964. }
  965. }
  966. static void fail()
  967. {
  968. longjmp(failpt, 1);
  969. /* NOTREACHED */
  970. }
  971. static void leave()
  972. {
  973. DBGPRINTF(("LEAVE: leave called!\n"));
  974. if (execflg)
  975. fail();
  976. scraphere();
  977. freehere(1);
  978. runtrap(0);
  979. _exit(exstat);
  980. /* NOTREACHED */
  981. }
  982. static void warn(s)
  983. REGISTER char *s;
  984. {
  985. if (*s) {
  986. prs(s);
  987. exstat = -1;
  988. }
  989. prs("\n");
  990. if (flag['e'])
  991. leave();
  992. }
  993. static void err(s)
  994. char *s;
  995. {
  996. warn(s);
  997. if (flag['n'])
  998. return;
  999. if (!interactive)
  1000. leave();
  1001. if (e.errpt)
  1002. longjmp(e.errpt, 1);
  1003. closeall();
  1004. e.iop = e.iobase = iostack;
  1005. }
  1006. static int newenv(f)
  1007. int f;
  1008. {
  1009. REGISTER struct env *ep;
  1010. DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f));
  1011. if (f) {
  1012. quitenv();
  1013. return (1);
  1014. }
  1015. ep = (struct env *) space(sizeof(*ep));
  1016. if (ep == NULL) {
  1017. while (e.oenv)
  1018. quitenv();
  1019. fail();
  1020. }
  1021. *ep = e;
  1022. e.oenv = ep;
  1023. e.errpt = errpt;
  1024. return (0);
  1025. }
  1026. static void quitenv()
  1027. {
  1028. REGISTER struct env *ep;
  1029. REGISTER int fd;
  1030. DBGPRINTF(("QUITENV: e.oenv=0x%x\n", e.oenv));
  1031. if ((ep = e.oenv) != NULL) {
  1032. fd = e.iofd;
  1033. e = *ep;
  1034. /* should close `'d files */
  1035. DELETE(ep);
  1036. while (--fd >= e.iofd)
  1037. close(fd);
  1038. }
  1039. }
  1040. /*
  1041. * Is any character from s1 in s2?
  1042. */
  1043. static int anys(s1, s2)
  1044. REGISTER char *s1, *s2;
  1045. {
  1046. while (*s1)
  1047. if (any(*s1++, s2))
  1048. return (1);
  1049. return (0);
  1050. }
  1051. /*
  1052. * Is character c in s?
  1053. */
  1054. static int any(c, s)
  1055. REGISTER int c;
  1056. REGISTER char *s;
  1057. {
  1058. while (*s)
  1059. if (*s++ == c)
  1060. return (1);
  1061. return (0);
  1062. }
  1063. static char *putn(n)
  1064. REGISTER int n;
  1065. {
  1066. return (itoa(n));
  1067. }
  1068. static char *itoa(n)
  1069. REGISTER int n;
  1070. {
  1071. static char s[20];
  1072. snprintf(s, sizeof(s), "%u", n);
  1073. return (s);
  1074. }
  1075. static void next(int f)
  1076. {
  1077. PUSHIO(afile, f, filechar);
  1078. }
  1079. static void onintr(s)
  1080. int s; /* ANSI C requires a parameter */
  1081. {
  1082. signal(SIGINT, onintr);
  1083. intr = 1;
  1084. if (interactive) {
  1085. if (inparse) {
  1086. prs("\n");
  1087. fail();
  1088. }
  1089. } else if (heedint) {
  1090. execflg = 0;
  1091. leave();
  1092. }
  1093. }
  1094. static char *space(n)
  1095. int n;
  1096. {
  1097. REGISTER char *cp;
  1098. if ((cp = getcell(n)) == 0)
  1099. err("out of string space");
  1100. return (cp);
  1101. }
  1102. static char *strsave(s, a)
  1103. REGISTER char *s;
  1104. int a;
  1105. {
  1106. REGISTER char *cp, *xp;
  1107. if ((cp = space(strlen(s) + 1)) != NULL) {
  1108. setarea((char *) cp, a);
  1109. for (xp = cp; (*xp++ = *s++) != '\0';);
  1110. return (cp);
  1111. }
  1112. return ("");
  1113. }
  1114. /*
  1115. * trap handling
  1116. */
  1117. static void sig(i)
  1118. REGISTER int i;
  1119. {
  1120. trapset = i;
  1121. signal(i, sig);
  1122. }
  1123. static void runtrap(i)
  1124. int i;
  1125. {
  1126. char *trapstr;
  1127. if ((trapstr = trap[i]) == NULL)
  1128. return;
  1129. if (i == 0)
  1130. trap[i] = 0;
  1131. RUN(aword, trapstr, nlchar);
  1132. }
  1133. /* -------- var.c -------- */
  1134. /*
  1135. * Find the given name in the dictionary
  1136. * and return its value. If the name was
  1137. * not previously there, enter it now and
  1138. * return a null value.
  1139. */
  1140. static struct var *lookup(n)
  1141. REGISTER char *n;
  1142. {
  1143. REGISTER struct var *vp;
  1144. REGISTER char *cp;
  1145. REGISTER int c;
  1146. static struct var dummy;
  1147. if (isdigit(*n)) {
  1148. dummy.name = n;
  1149. for (c = 0; isdigit(*n) && c < 1000; n++)
  1150. c = c * 10 + *n - '0';
  1151. dummy.status = RONLY;
  1152. dummy.value = c <= dolc ? dolv[c] : null;
  1153. return (&dummy);
  1154. }
  1155. for (vp = vlist; vp; vp = vp->next)
  1156. if (eqname(vp->name, n))
  1157. return (vp);
  1158. cp = findeq(n);
  1159. vp = (struct var *) space(sizeof(*vp));
  1160. if (vp == 0 || (vp->name = space((int) (cp - n) + 2)) == 0) {
  1161. dummy.name = dummy.value = "";
  1162. return (&dummy);
  1163. }
  1164. for (cp = vp->name; (*cp = *n++) && *cp != '='; cp++);
  1165. if (*cp == 0)
  1166. *cp = '=';
  1167. *++cp = 0;
  1168. setarea((char *) vp, 0);
  1169. setarea((char *) vp->name, 0);
  1170. vp->value = null;
  1171. vp->next = vlist;
  1172. vp->status = GETCELL;
  1173. vlist = vp;
  1174. return (vp);
  1175. }
  1176. /*
  1177. * give variable at `vp' the value `val'.
  1178. */
  1179. static void setval(vp, val)
  1180. struct var *vp;
  1181. char *val;
  1182. {
  1183. nameval(vp, val, (char *) NULL);
  1184. }
  1185. /*
  1186. * if name is not NULL, it must be
  1187. * a prefix of the space `val',
  1188. * and end with `='.
  1189. * this is all so that exporting
  1190. * values is reasonably painless.
  1191. */
  1192. static void nameval(vp, val, name)
  1193. REGISTER struct var *vp;
  1194. char *val, *name;
  1195. {
  1196. REGISTER char *cp, *xp;
  1197. char *nv;
  1198. int fl;
  1199. if (vp->status & RONLY) {
  1200. for (xp = vp->name; *xp && *xp != '=';)
  1201. putc(*xp++, stderr);
  1202. err(" is read-only");
  1203. return;
  1204. }
  1205. fl = 0;
  1206. if (name == NULL) {
  1207. xp = space(strlen(vp->name) + strlen(val) + 2);
  1208. if (xp == 0)
  1209. return;
  1210. /* make string: name=value */
  1211. setarea((char *) xp, 0);
  1212. name = xp;
  1213. for (cp = vp->name; (*xp = *cp++) && *xp != '='; xp++);
  1214. if (*xp++ == 0)
  1215. xp[-1] = '=';
  1216. nv = xp;
  1217. for (cp = val; (*xp++ = *cp++) != '\0';);
  1218. val = nv;
  1219. fl = GETCELL;
  1220. }
  1221. if (vp->status & GETCELL)
  1222. freecell(vp->name); /* form new string `name=value' */
  1223. vp->name = name;
  1224. vp->value = val;
  1225. vp->status |= fl;
  1226. }
  1227. static void export(vp)
  1228. struct var *vp;
  1229. {
  1230. vp->status |= EXPORT;
  1231. }
  1232. static void ronly(vp)
  1233. struct var *vp;
  1234. {
  1235. if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
  1236. vp->status |= RONLY;
  1237. }
  1238. static int isassign(s)
  1239. REGISTER char *s;
  1240. {
  1241. DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s));
  1242. if (!isalpha((int) *s) && *s != '_')
  1243. return (0);
  1244. for (; *s != '='; s++)
  1245. if (*s == 0 || (!isalnum(*s) && *s != '_'))
  1246. return (0);
  1247. return (1);
  1248. }
  1249. static int assign(s, cf)
  1250. REGISTER char *s;
  1251. int cf;
  1252. {
  1253. REGISTER char *cp;
  1254. struct var *vp;
  1255. DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf));
  1256. if (!isalpha(*s) && *s != '_')
  1257. return (0);
  1258. for (cp = s; *cp != '='; cp++)
  1259. if (*cp == 0 || (!isalnum(*cp) && *cp != '_'))
  1260. return (0);
  1261. vp = lookup(s);
  1262. nameval(vp, ++cp, cf == COPYV ? (char *) NULL : s);
  1263. if (cf != COPYV)
  1264. vp->status &= ~GETCELL;
  1265. return (1);
  1266. }
  1267. static int checkname(cp)
  1268. REGISTER char *cp;
  1269. {
  1270. DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp));
  1271. if (!isalpha(*cp++) && *(cp - 1) != '_')
  1272. return (0);
  1273. while (*cp)
  1274. if (!isalnum(*cp++) && *(cp - 1) != '_')
  1275. return (0);
  1276. return (1);
  1277. }
  1278. static void putvlist(f, out)
  1279. REGISTER int f, out;
  1280. {
  1281. REGISTER struct var *vp;
  1282. for (vp = vlist; vp; vp = vp->next)
  1283. if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
  1284. if (vp->status & EXPORT)
  1285. write(out, "export ", 7);
  1286. if (vp->status & RONLY)
  1287. write(out, "readonly ", 9);
  1288. write(out, vp->name, (int) (findeq(vp->name) - vp->name));
  1289. write(out, "\n", 1);
  1290. }
  1291. }
  1292. static int eqname(n1, n2)
  1293. REGISTER char *n1, *n2;
  1294. {
  1295. for (; *n1 != '=' && *n1 != 0; n1++)
  1296. if (*n2++ != *n1)
  1297. return (0);
  1298. return (*n2 == 0 || *n2 == '=');
  1299. }
  1300. static char *findeq(cp)
  1301. REGISTER char *cp;
  1302. {
  1303. while (*cp != '\0' && *cp != '=')
  1304. cp++;
  1305. return (cp);
  1306. }
  1307. /* -------- gmatch.c -------- */
  1308. /*
  1309. * int gmatch(string, pattern)
  1310. * char *string, *pattern;
  1311. *
  1312. * Match a pattern as in sh(1).
  1313. */
  1314. #define CMASK 0377
  1315. #define QUOTE 0200
  1316. #define QMASK (CMASK&~QUOTE)
  1317. #define NOT '!' /* might use ^ */
  1318. static int gmatch(s, p)
  1319. REGISTER char *s, *p;
  1320. {
  1321. REGISTER int sc, pc;
  1322. if (s == NULL || p == NULL)
  1323. return (0);
  1324. while ((pc = *p++ & CMASK) != '\0') {
  1325. sc = *s++ & QMASK;
  1326. switch (pc) {
  1327. case '[':
  1328. if ((p = cclass(p, sc)) == NULL)
  1329. return (0);
  1330. break;
  1331. case '?':
  1332. if (sc == 0)
  1333. return (0);
  1334. break;
  1335. case '*':
  1336. s--;
  1337. do {
  1338. if (*p == '\0' || gmatch(s, p))
  1339. return (1);
  1340. } while (*s++ != '\0');
  1341. return (0);
  1342. default:
  1343. if (sc != (pc & ~QUOTE))
  1344. return (0);
  1345. }
  1346. }
  1347. return (*s == 0);
  1348. }
  1349. static char *cclass(p, sub)
  1350. REGISTER char *p;
  1351. REGISTER int sub;
  1352. {
  1353. REGISTER int c, d, not, found;
  1354. if ((not = *p == NOT) != 0)
  1355. p++;
  1356. found = not;
  1357. do {
  1358. if (*p == '\0')
  1359. return ((char *) NULL);
  1360. c = *p & CMASK;
  1361. if (p[1] == '-' && p[2] != ']') {
  1362. d = p[2] & CMASK;
  1363. p++;
  1364. } else
  1365. d = c;
  1366. if (c == sub || (c <= sub && sub <= d))
  1367. found = !not;
  1368. } while (*++p != ']');
  1369. return (found ? p + 1 : (char *) NULL);
  1370. }
  1371. /* -------- area.c -------- */
  1372. /*
  1373. * All memory between (char *)areabot and (char *)(areatop+1) is
  1374. * exclusively administered by the area management routines.
  1375. * It is assumed that sbrk() and brk() manipulate the high end.
  1376. */
  1377. #define sbrk(X) ({ void * __q = (void *)-1; if (brkaddr + (int)(X) < brktop) { __q = brkaddr; brkaddr+=(int)(X); } __q;})
  1378. static void initarea()
  1379. {
  1380. brkaddr = malloc(AREASIZE);
  1381. brktop = brkaddr + AREASIZE;
  1382. while ((int) sbrk(0) & ALIGN)
  1383. sbrk(1);
  1384. areabot = (struct region *) sbrk(REGSIZE);
  1385. areabot->next = areabot;
  1386. areabot->area = BUSY;
  1387. areatop = areabot;
  1388. areanxt = areabot;
  1389. }
  1390. char *getcell(nbytes)
  1391. unsigned nbytes;
  1392. {
  1393. REGISTER int nregio;
  1394. REGISTER struct region *p, *q;
  1395. REGISTER int i;
  1396. if (nbytes == 0) {
  1397. puts("getcell(0)");
  1398. abort();
  1399. }
  1400. /* silly and defeats the algorithm */
  1401. /*
  1402. * round upwards and add administration area
  1403. */
  1404. nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1;
  1405. for (p = areanxt;;) {
  1406. if (p->area > areanum) {
  1407. /*
  1408. * merge free cells
  1409. */
  1410. while ((q = p->next)->area > areanum && q != areanxt)
  1411. p->next = q->next;
  1412. /*
  1413. * exit loop if cell big enough
  1414. */
  1415. if (q >= p + nregio)
  1416. goto found;
  1417. }
  1418. p = p->next;
  1419. if (p == areanxt)
  1420. break;
  1421. }
  1422. i = nregio >= GROWBY ? nregio : GROWBY;
  1423. p = (struct region *) sbrk(i * REGSIZE);
  1424. if (p == (struct region *) -1)
  1425. return ((char *) NULL);
  1426. p--;
  1427. if (p != areatop) {
  1428. puts("not contig");
  1429. abort(); /* allocated areas are contiguous */
  1430. }
  1431. q = p + i;
  1432. p->next = q;
  1433. p->area = FREE;
  1434. q->next = areabot;
  1435. q->area = BUSY;
  1436. areatop = q;
  1437. found:
  1438. /*
  1439. * we found a FREE area big enough, pointed to by 'p', and up to 'q'
  1440. */
  1441. areanxt = p + nregio;
  1442. if (areanxt < q) {
  1443. /*
  1444. * split into requested area and rest
  1445. */
  1446. if (areanxt + 1 > q) {
  1447. puts("OOM");
  1448. abort(); /* insufficient space left for admin */
  1449. }
  1450. areanxt->next = q;
  1451. areanxt->area = FREE;
  1452. p->next = areanxt;
  1453. }
  1454. p->area = areanum;
  1455. return ((char *) (p + 1));
  1456. }
  1457. static void freecell(cp)
  1458. char *cp;
  1459. {
  1460. REGISTER struct region *p;
  1461. if ((p = (struct region *) cp) != NULL) {
  1462. p--;
  1463. if (p < areanxt)
  1464. areanxt = p;
  1465. p->area = FREE;
  1466. }
  1467. }
  1468. static void freearea(a)
  1469. REGISTER int a;
  1470. {
  1471. REGISTER struct region *p, *top;
  1472. top = areatop;
  1473. for (p = areabot; p != top; p = p->next)
  1474. if (p->area >= a)
  1475. p->area = FREE;
  1476. }
  1477. static void setarea(cp, a)
  1478. char *cp;
  1479. int a;
  1480. {
  1481. REGISTER struct region *p;
  1482. if ((p = (struct region *) cp) != NULL)
  1483. (p - 1)->area = a;
  1484. }
  1485. int getarea(cp)
  1486. char *cp;
  1487. {
  1488. return ((struct region *) cp - 1)->area;
  1489. }
  1490. static void garbage()
  1491. {
  1492. REGISTER struct region *p, *q, *top;
  1493. top = areatop;
  1494. for (p = areabot; p != top; p = p->next) {
  1495. if (p->area > areanum) {
  1496. while ((q = p->next)->area > areanum)
  1497. p->next = q->next;
  1498. areanxt = p;
  1499. }
  1500. }
  1501. #ifdef SHRINKBY
  1502. if (areatop >= q + SHRINKBY && q->area > areanum) {
  1503. brk((char *) (q + 1));
  1504. q->next = areabot;
  1505. q->area = BUSY;
  1506. areatop = q;
  1507. }
  1508. #endif
  1509. }
  1510. /* -------- csyn.c -------- */
  1511. /*
  1512. * shell: syntax (C version)
  1513. */
  1514. int yyparse()
  1515. {
  1516. DBGPRINTF7(("YYPARSE: enter...\n"));
  1517. startl = 1;
  1518. peeksym = 0;
  1519. yynerrs = 0;
  1520. outtree = c_list();
  1521. musthave('\n', 0);
  1522. return (yynerrs != 0);
  1523. }
  1524. static struct op *pipeline(cf)
  1525. int cf;
  1526. {
  1527. REGISTER struct op *t, *p;
  1528. REGISTER int c;
  1529. DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf));
  1530. t = command(cf);
  1531. DBGPRINTF9(("PIPELINE: t=0x%x\n", t));
  1532. if (t != NULL) {
  1533. while ((c = yylex(0)) == '|') {
  1534. if ((p = command(CONTIN)) == NULL) {
  1535. DBGPRINTF8(("PIPELINE: error!\n"));
  1536. SYNTAXERR;
  1537. }
  1538. if (t->type != TPAREN && t->type != TCOM) {
  1539. /* shell statement */
  1540. t = block(TPAREN, t, NOBLOCK, NOWORDS);
  1541. }
  1542. t = block(TPIPE, t, p, NOWORDS);
  1543. }
  1544. peeksym = c;
  1545. }
  1546. DBGPRINTF7(("PIPELINE: returning t=0x%x\n", t));
  1547. return (t);
  1548. }
  1549. static struct op *andor()
  1550. {
  1551. REGISTER struct op *t, *p;
  1552. REGISTER int c;
  1553. DBGPRINTF7(("ANDOR: enter...\n"));
  1554. t = pipeline(0);
  1555. DBGPRINTF9(("ANDOR: t=0x%x\n", t));
  1556. if (t != NULL) {
  1557. while ((c = yylex(0)) == LOGAND || c == LOGOR) {
  1558. if ((p = pipeline(CONTIN)) == NULL) {
  1559. DBGPRINTF8(("ANDOR: error!\n"));
  1560. SYNTAXERR;
  1561. }
  1562. t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS);
  1563. } /* WHILE */
  1564. peeksym = c;
  1565. }
  1566. DBGPRINTF7(("ANDOR: returning t=0x%x\n", t));
  1567. return (t);
  1568. }
  1569. static struct op *c_list()
  1570. {
  1571. REGISTER struct op *t, *p;
  1572. REGISTER int c;
  1573. DBGPRINTF7(("C_LIST: enter...\n"));
  1574. t = andor();
  1575. if (t != NULL) {
  1576. if ((peeksym = yylex(0)) == '&')
  1577. t = block(TASYNC, t, NOBLOCK, NOWORDS);
  1578. while ((c = yylex(0)) == ';' || c == '&'
  1579. || (multiline && c == '\n')) {
  1580. if ((p = andor()) == NULL)
  1581. return (t);
  1582. if ((peeksym = yylex(0)) == '&')
  1583. p = block(TASYNC, p, NOBLOCK, NOWORDS);
  1584. t = list(t, p);
  1585. } /* WHILE */
  1586. peeksym = c;
  1587. }
  1588. /* IF */
  1589. DBGPRINTF7(("C_LIST: returning t=0x%x\n", t));
  1590. return (t);
  1591. }
  1592. static int synio(cf)
  1593. int cf;
  1594. {
  1595. REGISTER struct ioword *iop;
  1596. REGISTER int i;
  1597. REGISTER int c;
  1598. DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf));
  1599. if ((c = yylex(cf)) != '<' && c != '>') {
  1600. peeksym = c;
  1601. return (0);
  1602. }
  1603. i = yylval.i;
  1604. musthave(WORD, 0);
  1605. iop = io(iounit, i, yylval.cp);
  1606. iounit = IODEFAULT;
  1607. if (i & IOHERE)
  1608. markhere(yylval.cp, iop);
  1609. DBGPRINTF7(("SYNIO: returning 1\n"));
  1610. return (1);
  1611. }
  1612. static void musthave(c, cf)
  1613. int c, cf;
  1614. {
  1615. if ((peeksym = yylex(cf)) != c) {
  1616. DBGPRINTF7(("MUSTHAVE: error!\n"));
  1617. SYNTAXERR;
  1618. }
  1619. peeksym = 0;
  1620. }
  1621. static struct op *simple()
  1622. {
  1623. REGISTER struct op *t;
  1624. t = NULL;
  1625. for (;;) {
  1626. switch (peeksym = yylex(0)) {
  1627. case '<':
  1628. case '>':
  1629. (void) synio(0);
  1630. break;
  1631. case WORD:
  1632. if (t == NULL) {
  1633. t = newtp();
  1634. t->type = TCOM;
  1635. }
  1636. peeksym = 0;
  1637. word(yylval.cp);
  1638. break;
  1639. default:
  1640. return (t);
  1641. }
  1642. }
  1643. }
  1644. static struct op *nested(type, mark)
  1645. int type, mark;
  1646. {
  1647. REGISTER struct op *t;
  1648. DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark));
  1649. multiline++;
  1650. t = c_list();
  1651. musthave(mark, 0);
  1652. multiline--;
  1653. return (block(type, t, NOBLOCK, NOWORDS));
  1654. }
  1655. static struct op *command(cf)
  1656. int cf;
  1657. {
  1658. REGISTER struct op *t;
  1659. struct wdblock *iosave;
  1660. REGISTER int c;
  1661. DBGPRINTF(("COMMAND: enter, cf=%d\n", cf));
  1662. iosave = iolist;
  1663. iolist = NULL;
  1664. if (multiline)
  1665. cf |= CONTIN;
  1666. while (synio(cf))
  1667. cf = 0;
  1668. c = yylex(cf);
  1669. switch (c) {
  1670. default:
  1671. peeksym = c;
  1672. if ((t = simple()) == NULL) {
  1673. if (iolist == NULL)
  1674. return ((struct op *) NULL);
  1675. t = newtp();
  1676. t->type = TCOM;
  1677. }
  1678. break;
  1679. case '(':
  1680. t = nested(TPAREN, ')');
  1681. break;
  1682. case '{':
  1683. t = nested(TBRACE, '}');
  1684. break;
  1685. case FOR:
  1686. t = newtp();
  1687. t->type = TFOR;
  1688. musthave(WORD, 0);
  1689. startl = 1;
  1690. t->str = yylval.cp;
  1691. multiline++;
  1692. t->words = wordlist();
  1693. if ((c = yylex(0)) != '\n' && c != ';')
  1694. peeksym = c;
  1695. t->left = dogroup(0);
  1696. multiline--;
  1697. break;
  1698. case WHILE:
  1699. case UNTIL:
  1700. multiline++;
  1701. t = newtp();
  1702. t->type = c == WHILE ? TWHILE : TUNTIL;
  1703. t->left = c_list();
  1704. t->right = dogroup(1);
  1705. t->words = NULL;
  1706. multiline--;
  1707. break;
  1708. case CASE:
  1709. t = newtp();
  1710. t->type = TCASE;
  1711. musthave(WORD, 0);
  1712. t->str = yylval.cp;
  1713. startl++;
  1714. multiline++;
  1715. musthave(IN, CONTIN);
  1716. startl++;
  1717. t->left = caselist();
  1718. musthave(ESAC, 0);
  1719. multiline--;
  1720. break;
  1721. case IF:
  1722. multiline++;
  1723. t = newtp();
  1724. t->type = TIF;
  1725. t->left = c_list();
  1726. t->right = thenpart();
  1727. musthave(FI, 0);
  1728. multiline--;
  1729. break;
  1730. case DOT:
  1731. t = newtp();
  1732. t->type = TDOT;
  1733. musthave(WORD, 0); /* gets name of file */
  1734. DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp));
  1735. word(yylval.cp); /* add word to wdlist */
  1736. word(NOWORD); /* terminate wdlist */
  1737. t->words = copyw(); /* dup wdlist */
  1738. break;
  1739. }
  1740. while (synio(0));
  1741. t = namelist(t);
  1742. iolist = iosave;
  1743. DBGPRINTF(("COMMAND: returning 0x%x\n", t));
  1744. return (t);
  1745. }
  1746. static struct op *dowholefile(type, mark)
  1747. int type;
  1748. int mark;
  1749. {
  1750. REGISTER struct op *t;
  1751. DBGPRINTF(("DOWHOLEFILE: enter, type=%d, mark=%d\n", type, mark));
  1752. multiline++;
  1753. t = c_list();
  1754. multiline--;
  1755. t = block(type, t, NOBLOCK, NOWORDS);
  1756. DBGPRINTF(("DOWHOLEFILE: return t=0x%x\n", t));
  1757. return (t);
  1758. }
  1759. static struct op *dogroup(onlydone)
  1760. int onlydone;
  1761. {
  1762. REGISTER int c;
  1763. REGISTER struct op *mylist;
  1764. c = yylex(CONTIN);
  1765. if (c == DONE && onlydone)
  1766. return ((struct op *) NULL);
  1767. if (c != DO)
  1768. SYNTAXERR;
  1769. mylist = c_list();
  1770. musthave(DONE, 0);
  1771. return (mylist);
  1772. }
  1773. static struct op *thenpart()
  1774. {
  1775. REGISTER int c;
  1776. REGISTER struct op *t;
  1777. if ((c = yylex(0)) != THEN) {
  1778. peeksym = c;
  1779. return ((struct op *) NULL);
  1780. }
  1781. t = newtp();
  1782. t->type = 0;
  1783. t->left = c_list();
  1784. if (t->left == NULL)
  1785. SYNTAXERR;
  1786. t->right = elsepart();
  1787. return (t);
  1788. }
  1789. static struct op *elsepart()
  1790. {
  1791. REGISTER int c;
  1792. REGISTER struct op *t;
  1793. switch (c = yylex(0)) {
  1794. case ELSE:
  1795. if ((t = c_list()) == NULL)
  1796. SYNTAXERR;
  1797. return (t);
  1798. case ELIF:
  1799. t = newtp();
  1800. t->type = TELIF;
  1801. t->left = c_list();
  1802. t->right = thenpart();
  1803. return (t);
  1804. default:
  1805. peeksym = c;
  1806. return ((struct op *) NULL);
  1807. }
  1808. }
  1809. static struct op *caselist()
  1810. {
  1811. REGISTER struct op *t;
  1812. t = NULL;
  1813. while ((peeksym = yylex(CONTIN)) != ESAC) {
  1814. DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym));
  1815. t = list(t, casepart());
  1816. }
  1817. DBGPRINTF(("CASELIST, returning t=0x%x\n", t));
  1818. return (t);
  1819. }
  1820. static struct op *casepart()
  1821. {
  1822. REGISTER struct op *t;
  1823. DBGPRINTF7(("CASEPART: enter...\n"));
  1824. t = newtp();
  1825. t->type = TPAT;
  1826. t->words = pattern();
  1827. musthave(')', 0);
  1828. t->left = c_list();
  1829. if ((peeksym = yylex(CONTIN)) != ESAC)
  1830. musthave(BREAK, CONTIN);
  1831. DBGPRINTF7(("CASEPART: made newtp(TPAT, t=0x%x)\n", t));
  1832. return (t);
  1833. }
  1834. static char **pattern()
  1835. {
  1836. REGISTER int c, cf;
  1837. cf = CONTIN;
  1838. do {
  1839. musthave(WORD, cf);
  1840. word(yylval.cp);
  1841. cf = 0;
  1842. } while ((c = yylex(0)) == '|');
  1843. peeksym = c;
  1844. word(NOWORD);
  1845. return (copyw());
  1846. }
  1847. static char **wordlist()
  1848. {
  1849. REGISTER int c;
  1850. if ((c = yylex(0)) != IN) {
  1851. peeksym = c;
  1852. return ((char **) NULL);
  1853. }
  1854. startl = 0;
  1855. while ((c = yylex(0)) == WORD)
  1856. word(yylval.cp);
  1857. word(NOWORD);
  1858. peeksym = c;
  1859. return (copyw());
  1860. }
  1861. /*
  1862. * supporting functions
  1863. */
  1864. static struct op *list(t1, t2)
  1865. REGISTER struct op *t1, *t2;
  1866. {
  1867. DBGPRINTF7(("LIST: enter, t1=0x%x, t2=0x%x\n", t1, t2));
  1868. if (t1 == NULL)
  1869. return (t2);
  1870. if (t2 == NULL)
  1871. return (t1);
  1872. return (block(TLIST, t1, t2, NOWORDS));
  1873. }
  1874. static struct op *block(type, t1, t2, wp)
  1875. int type;
  1876. struct op *t1, *t2;
  1877. char **wp;
  1878. {
  1879. REGISTER struct op *t;
  1880. DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type]));
  1881. t = newtp();
  1882. t->type = type;
  1883. t->left = t1;
  1884. t->right = t2;
  1885. t->words = wp;
  1886. DBGPRINTF7(("BLOCK: inserted 0x%x between 0x%x and 0x%x\n", t, t1,
  1887. t2));
  1888. return (t);
  1889. }
  1890. /* See if given string is a shell multiline (FOR, IF, etc) */
  1891. static int rlookup(n)
  1892. REGISTER char *n;
  1893. {
  1894. REGISTER struct res *rp;
  1895. DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n));
  1896. for (rp = restab; rp->r_name; rp++)
  1897. if (strcmp(rp->r_name, n) == 0) {
  1898. DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val));
  1899. return (rp->r_val); /* Return numeric code for shell multiline */
  1900. }
  1901. DBGPRINTF7(("RLOOKUP: NO match, returning 0\n"));
  1902. return (0); /* Not a shell multiline */
  1903. }
  1904. static struct op *newtp()
  1905. {
  1906. REGISTER struct op *t;
  1907. t = (struct op *) tree(sizeof(*t));
  1908. t->type = 0;
  1909. t->words = NULL;
  1910. t->ioact = NULL;
  1911. t->left = NULL;
  1912. t->right = NULL;
  1913. t->str = NULL;
  1914. DBGPRINTF3(("NEWTP: allocated 0x%x\n", t));
  1915. return (t);
  1916. }
  1917. static struct op *namelist(t)
  1918. REGISTER struct op *t;
  1919. {
  1920. DBGPRINTF7(("NAMELIST: enter, t=0x%x, type %s, iolist=0x%x\n", t,
  1921. T_CMD_NAMES[t->type], iolist));
  1922. if (iolist) {
  1923. iolist = addword((char *) NULL, iolist);
  1924. t->ioact = copyio();
  1925. } else
  1926. t->ioact = NULL;
  1927. if (t->type != TCOM) {
  1928. if (t->type != TPAREN && t->ioact != NULL) {
  1929. t = block(TPAREN, t, NOBLOCK, NOWORDS);
  1930. t->ioact = t->left->ioact;
  1931. t->left->ioact = NULL;
  1932. }
  1933. return (t);
  1934. }
  1935. word(NOWORD);
  1936. t->words = copyw();
  1937. return (t);
  1938. }
  1939. static char **copyw()
  1940. {
  1941. REGISTER char **wd;
  1942. wd = getwords(wdlist);
  1943. wdlist = 0;
  1944. return (wd);
  1945. }
  1946. static void word(cp)
  1947. char *cp;
  1948. {
  1949. wdlist = addword(cp, wdlist);
  1950. }
  1951. static struct ioword **copyio()
  1952. {
  1953. REGISTER struct ioword **iop;
  1954. iop = (struct ioword **) getwords(iolist);
  1955. iolist = 0;
  1956. return (iop);
  1957. }
  1958. static struct ioword *io(u, f, cp)
  1959. int u;
  1960. int f;
  1961. char *cp;
  1962. {
  1963. REGISTER struct ioword *iop;
  1964. iop = (struct ioword *) tree(sizeof(*iop));
  1965. iop->io_unit = u;
  1966. iop->io_flag = f;
  1967. iop->io_name = cp;
  1968. iolist = addword((char *) iop, iolist);
  1969. return (iop);
  1970. }
  1971. static void zzerr()
  1972. {
  1973. yyerror("syntax error");
  1974. }
  1975. static void yyerror(s)
  1976. char *s;
  1977. {
  1978. yynerrs++;
  1979. if (interactive && e.iop <= iostack) {
  1980. multiline = 0;
  1981. while (eofc() == 0 && yylex(0) != '\n');
  1982. }
  1983. err(s);
  1984. fail();
  1985. }
  1986. static int yylex(cf)
  1987. int cf;
  1988. {
  1989. REGISTER int c, c1;
  1990. int atstart;
  1991. if ((c = peeksym) > 0) {
  1992. peeksym = 0;
  1993. if (c == '\n')
  1994. startl = 1;
  1995. return (c);
  1996. }
  1997. nlseen = 0;
  1998. atstart = startl;
  1999. startl = 0;
  2000. yylval.i = 0;
  2001. e.linep = line;
  2002. /* MALAMO */
  2003. line[LINELIM - 1] = '\0';
  2004. loop:
  2005. while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */
  2006. ;
  2007. switch (c) {
  2008. default:
  2009. if (any(c, "0123456789")) {
  2010. unget(c1 = my_getc(0));
  2011. if (c1 == '<' || c1 == '>') {
  2012. iounit = c - '0';
  2013. goto loop;
  2014. }
  2015. *e.linep++ = c;
  2016. c = c1;
  2017. }
  2018. break;
  2019. case '#': /* Comment, skip to next newline or End-of-string */
  2020. while ((c = my_getc(0)) != 0 && c != '\n');
  2021. unget(c);
  2022. goto loop;
  2023. case 0:
  2024. DBGPRINTF5(("YYLEX: return 0, c=%d\n", c));
  2025. return (c);
  2026. case '$':
  2027. DBGPRINTF9(("YYLEX: found $\n"));
  2028. *e.linep++ = c;
  2029. if ((c = my_getc(0)) == '{') {
  2030. if ((c = collect(c, '}')) != '\0')
  2031. return (c);
  2032. goto pack;
  2033. }
  2034. break;
  2035. case '`':
  2036. case '\'':
  2037. case '"':
  2038. if ((c = collect(c, c)) != '\0')
  2039. return (c);
  2040. goto pack;
  2041. case '|':
  2042. case '&':
  2043. case ';':
  2044. startl = 1;
  2045. /* If more chars process them, else return NULL char */
  2046. if ((c1 = dual(c)) != '\0')
  2047. return (c1);
  2048. else
  2049. return (c);
  2050. case '^':
  2051. startl = 1;
  2052. return ('|');
  2053. case '>':
  2054. case '<':
  2055. diag(c);
  2056. return (c);
  2057. case '\n':
  2058. nlseen++;
  2059. gethere();
  2060. startl = 1;
  2061. if (multiline || cf & CONTIN) {
  2062. if (interactive && e.iop <= iostack) {
  2063. #ifdef CONFIG_FEATURE_COMMAND_EDITING
  2064. current_prompt = cprompt->value;
  2065. #else
  2066. prs(cprompt->value);
  2067. #endif
  2068. }
  2069. if (cf & CONTIN)
  2070. goto loop;
  2071. }
  2072. return (c);
  2073. case '(':
  2074. case ')':
  2075. startl = 1;
  2076. return (c);
  2077. }
  2078. unget(c);
  2079. pack:
  2080. while ((c = my_getc(0)) != 0 && !any(c, "`$ '\"\t;&<>()|^\n")) {
  2081. if (e.linep >= elinep)
  2082. err("word too long");
  2083. else
  2084. *e.linep++ = c;
  2085. };
  2086. unget(c);
  2087. if (any(c, "\"'`$"))
  2088. goto loop;
  2089. *e.linep++ = '\0';
  2090. if (atstart && (c = rlookup(line)) != 0) {
  2091. startl = 1;
  2092. return (c);
  2093. }
  2094. yylval.cp = strsave(line, areanum);
  2095. return (WORD);
  2096. }
  2097. static int collect(c, c1)
  2098. REGISTER int c, c1;
  2099. {
  2100. char s[2];
  2101. DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1));
  2102. *e.linep++ = c;
  2103. while ((c = my_getc(c1)) != c1) {
  2104. if (c == 0) {
  2105. unget(c);
  2106. s[0] = c1;
  2107. s[1] = 0;
  2108. prs("no closing ");
  2109. yyerror(s);
  2110. return (YYERRCODE);
  2111. }
  2112. if (interactive && c == '\n' && e.iop <= iostack) {
  2113. #ifdef CONFIG_FEATURE_COMMAND_EDITING
  2114. current_prompt = cprompt->value;
  2115. #else
  2116. prs(cprompt->value);
  2117. #endif
  2118. }
  2119. *e.linep++ = c;
  2120. }
  2121. *e.linep++ = c;
  2122. DBGPRINTF8(("COLLECT: return 0, line is %s\n", line));
  2123. return (0);
  2124. }
  2125. /* "multiline commands" helper func */
  2126. /* see if next 2 chars form a shell multiline */
  2127. static int dual(c)
  2128. REGISTER int c;
  2129. {
  2130. char s[3];
  2131. REGISTER char *cp = s;
  2132. DBGPRINTF8(("DUAL: enter, c=%d\n", c));
  2133. *cp++ = c; /* c is the given "peek" char */
  2134. *cp++ = my_getc(0); /* get next char of input */
  2135. *cp = 0; /* add EOS marker */
  2136. c = rlookup(s); /* see if 2 chars form a shell multiline */
  2137. if (c == 0)
  2138. unget(*--cp); /* String is not a shell multiline, put peek char back */
  2139. return (c); /* String is multiline, return numeric multiline (restab) code */
  2140. }
  2141. static void diag(ec)
  2142. REGISTER int ec;
  2143. {
  2144. REGISTER int c;
  2145. DBGPRINTF8(("DIAG: enter, ec=%d\n", ec));
  2146. c = my_getc(0);
  2147. if (c == '>' || c == '<') {
  2148. if (c != ec)
  2149. zzerr();
  2150. yylval.i = ec == '>' ? IOWRITE | IOCAT : IOHERE;
  2151. c = my_getc(0);
  2152. } else
  2153. yylval.i = ec == '>' ? IOWRITE : IOREAD;
  2154. if (c != '&' || yylval.i == IOHERE)
  2155. unget(c);
  2156. else
  2157. yylval.i |= IODUP;
  2158. }
  2159. static char *tree(size)
  2160. unsigned size;
  2161. {
  2162. REGISTER char *t;
  2163. if ((t = getcell(size)) == NULL) {
  2164. DBGPRINTF2(("TREE: getcell(%d) failed!\n", size));
  2165. prs("command line too complicated\n");
  2166. fail();
  2167. /* NOTREACHED */
  2168. }
  2169. return (t);
  2170. }
  2171. /* VARARGS1 */
  2172. /* ARGSUSED */
  2173. /* -------- exec.c -------- */
  2174. /*
  2175. * execute tree
  2176. */
  2177. static int execute(t, pin, pout, act)
  2178. REGISTER struct op *t;
  2179. int *pin, *pout;
  2180. int act;
  2181. {
  2182. REGISTER struct op *t1;
  2183. volatile int i, rv, a;
  2184. char *cp, **wp, **wp2;
  2185. struct var *vp;
  2186. struct op *outtree_save;
  2187. struct brkcon bc;
  2188. #if __GNUC__
  2189. /* Avoid longjmp clobbering */
  2190. (void) &wp;
  2191. #endif
  2192. if (t == NULL) {
  2193. DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n"));
  2194. return (0);
  2195. }
  2196. DBGPRINTF(("EXECUTE: t=0x%x, t->type=%d (%s), t->words is %s\n", t,
  2197. t->type, T_CMD_NAMES[t->type],
  2198. ((t->words == NULL) ? "NULL" : t->words[0])));
  2199. rv = 0;
  2200. a = areanum++;
  2201. wp = (wp2 = t->words) != NULL
  2202. ? eval(wp2, t->type == TCOM ? DOALL : DOALL & ~DOKEY)
  2203. : NULL;
  2204. /* Hard to know how many words there are, be careful of garbage pointer values */
  2205. /* They are likely to cause "PCI bus fault" errors */
  2206. #if 0
  2207. DBGPRINTF(("EXECUTE: t->left=0x%x, t->right=0x%x, t->words[1] is %s\n",
  2208. t->left, t->right,
  2209. ((t->words[1] == NULL) ? "NULL" : t->words[1])));
  2210. DBGPRINTF7(("EXECUTE: t->words[2] is %s, t->words[3] is %s\n",
  2211. ((t->words[2] == NULL) ? "NULL" : t->words[2]),
  2212. ((t->words[3] == NULL) ? "NULL" : t->words[3])));
  2213. #endif
  2214. switch (t->type) {
  2215. case TDOT:
  2216. DBGPRINTF3(("EXECUTE: TDOT\n"));
  2217. outtree_save = outtree;
  2218. newfile(evalstr(t->words[0], DOALL));
  2219. t->left = dowholefile(TLIST, 0);
  2220. t->right = NULL;
  2221. outtree = outtree_save;
  2222. if (t->left)
  2223. rv = execute(t->left, pin, pout, 0);
  2224. if (t->right)
  2225. rv = execute(t->right, pin, pout, 0);
  2226. break;
  2227. case TPAREN:
  2228. rv = execute(t->left, pin, pout, 0);
  2229. break;
  2230. case TCOM:
  2231. {
  2232. rv = forkexec(t, pin, pout, act, wp);
  2233. }
  2234. break;
  2235. case TPIPE:
  2236. {
  2237. int pv[2];
  2238. if ((rv = openpipe(pv)) < 0)
  2239. break;
  2240. pv[0] = remap(pv[0]);
  2241. pv[1] = remap(pv[1]);
  2242. (void) execute(t->left, pin, pv, 0);
  2243. rv = execute(t->right, pv, pout, 0);
  2244. }
  2245. break;
  2246. case TLIST:
  2247. (void) execute(t->left, pin, pout, 0);
  2248. rv = execute(t->right, pin, pout, 0);
  2249. break;
  2250. case TASYNC:
  2251. {
  2252. int hinteractive = interactive;
  2253. DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n"));
  2254. i = vfork();
  2255. if (i != 0) {
  2256. interactive = hinteractive;
  2257. if (i != -1) {
  2258. setval(lookup("!"), putn(i));
  2259. if (pin != NULL)
  2260. closepipe(pin);
  2261. if (interactive) {
  2262. prs(putn(i));
  2263. prs("\n");
  2264. }
  2265. } else
  2266. rv = -1;
  2267. setstatus(rv);
  2268. } else {
  2269. signal(SIGINT, SIG_IGN);
  2270. signal(SIGQUIT, SIG_IGN);
  2271. if (interactive)
  2272. signal(SIGTERM, SIG_DFL);
  2273. interactive = 0;
  2274. if (pin == NULL) {
  2275. close(0);
  2276. open("/dev/null", 0);
  2277. }
  2278. _exit(execute(t->left, pin, pout, FEXEC));
  2279. }
  2280. }
  2281. break;
  2282. case TOR:
  2283. case TAND:
  2284. rv = execute(t->left, pin, pout, 0);
  2285. if ((t1 = t->right) != NULL && (rv == 0) == (t->type == TAND))
  2286. rv = execute(t1, pin, pout, 0);
  2287. break;
  2288. case TFOR:
  2289. if (wp == NULL) {
  2290. wp = dolv + 1;
  2291. if ((i = dolc) < 0)
  2292. i = 0;
  2293. } else {
  2294. i = -1;
  2295. while (*wp++ != NULL);
  2296. }
  2297. vp = lookup(t->str);
  2298. while (setjmp(bc.brkpt))
  2299. if (isbreak)
  2300. goto broken;
  2301. brkset(&bc);
  2302. for (t1 = t->left; i-- && *wp != NULL;) {
  2303. setval(vp, *wp++);
  2304. rv = execute(t1, pin, pout, 0);
  2305. }
  2306. brklist = brklist->nextlev;
  2307. break;
  2308. case TWHILE:
  2309. case TUNTIL:
  2310. while (setjmp(bc.brkpt))
  2311. if (isbreak)
  2312. goto broken;
  2313. brkset(&bc);
  2314. t1 = t->left;
  2315. while ((execute(t1, pin, pout, 0) == 0) == (t->type == TWHILE))
  2316. rv = execute(t->right, pin, pout, 0);
  2317. brklist = brklist->nextlev;
  2318. break;
  2319. case TIF:
  2320. case TELIF:
  2321. if (t->right != NULL) {
  2322. rv = !execute(t->left, pin, pout, 0) ?
  2323. execute(t->right->left, pin, pout, 0) :
  2324. execute(t->right->right, pin, pout, 0);
  2325. }
  2326. break;
  2327. case TCASE:
  2328. if ((cp = evalstr(t->str, DOSUB | DOTRIM)) == 0)
  2329. cp = "";
  2330. DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n",
  2331. ((t->str == NULL) ? "NULL" : t->str),
  2332. ((cp == NULL) ? "NULL" : cp)));
  2333. if ((t1 = findcase(t->left, cp)) != NULL) {
  2334. DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=0x%x, t1=0x%x)...\n", t, t1));
  2335. rv = execute(t1, pin, pout, 0);
  2336. DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=0x%x, t1=0x%x)...\n", t, t1));
  2337. }
  2338. break;
  2339. case TBRACE:
  2340. /*
  2341. if (iopp = t->ioact)
  2342. while (*iopp)
  2343. if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
  2344. rv = -1;
  2345. break;
  2346. }
  2347. */
  2348. if (rv >= 0 && (t1 = t->left))
  2349. rv = execute(t1, pin, pout, 0);
  2350. break;
  2351. };
  2352. broken:
  2353. t->words = wp2;
  2354. isbreak = 0;
  2355. freehere(areanum);
  2356. freearea(areanum);
  2357. areanum = a;
  2358. if (interactive && intr) {
  2359. closeall();
  2360. fail();
  2361. }
  2362. if ((i = trapset) != 0) {
  2363. trapset = 0;
  2364. runtrap(i);
  2365. }
  2366. DBGPRINTF(("EXECUTE: returning from t=0x%x, rv=%d\n", t, rv));
  2367. return (rv);
  2368. }
  2369. static int
  2370. forkexec(REGISTER struct op *t, int *pin, int *pout, int act, char **wp)
  2371. {
  2372. pid_t newpid;
  2373. int i, rv;
  2374. int (*shcom) (struct op *) = NULL;
  2375. REGISTER int f;
  2376. char *cp = NULL;
  2377. struct ioword **iopp;
  2378. int resetsig;
  2379. char **owp;
  2380. int forked = 0;
  2381. int *hpin = pin;
  2382. int *hpout = pout;
  2383. char *hwp;
  2384. int hinteractive;
  2385. int hintr;
  2386. struct brkcon *hbrklist;
  2387. int hexecflg;
  2388. #if __GNUC__
  2389. /* Avoid longjmp clobbering */
  2390. (void) &pin;
  2391. (void) &pout;
  2392. (void) &wp;
  2393. (void) &shcom;
  2394. (void) &cp;
  2395. (void) &resetsig;
  2396. (void) &owp;
  2397. #endif
  2398. DBGPRINTF(("FORKEXEC: t=0x%x, pin 0x%x, pout 0x%x, act %d\n", t, pin,
  2399. pout, act));
  2400. DBGPRINTF7(("FORKEXEC: t->words is %s\n",
  2401. ((t->words == NULL) ? "NULL" : t->words[0])));
  2402. /* Hard to know how many words there are, be careful of garbage pointer values */
  2403. /* They are likely to cause "PCI bus fault" errors */
  2404. #if 0
  2405. DBGPRINTF7(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
  2406. ((t->words == NULL) ? "NULL" : t->words[0]),
  2407. ((t->words == NULL) ? "NULL" : t->words[1])));
  2408. DBGPRINTF7(("FORKEXEC: wp is %s, wp[1] is %s\n",
  2409. ((wp == NULL) ? "NULL" : wp[0]),
  2410. ((wp[1] == NULL) ? "NULL" : wp[1])));
  2411. DBGPRINTF7(("FORKEXEC: wp2 is %s, wp[3] is %s\n",
  2412. ((wp[2] == NULL) ? "NULL" : wp[2]),
  2413. ((wp[3] == NULL) ? "NULL" : wp[3])));
  2414. #endif
  2415. owp = wp;
  2416. resetsig = 0;
  2417. rv = -1; /* system-detected error */
  2418. if (t->type == TCOM) {
  2419. while ((cp = *wp++) != NULL);
  2420. cp = *wp;
  2421. /* strip all initial assignments */
  2422. /* not correct wrt PATH=yyy command etc */
  2423. if (flag['x']) {
  2424. DBGPRINTF9(("FORKEXEC: echo'ing, cp=0x%x, wp=0x%x, owp=0x%x\n",
  2425. cp, wp, owp));
  2426. echo(cp ? wp : owp);
  2427. }
  2428. #if 0
  2429. DBGPRINTF9(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
  2430. ((t->words == NULL) ? "NULL" : t->words[0]),
  2431. ((t->words == NULL) ? "NULL" : t->words[1])));
  2432. DBGPRINTF9(("FORKEXEC: wp is %s, wp[1] is %s\n",
  2433. ((wp == NULL) ? "NULL" : wp[0]),
  2434. ((wp == NULL) ? "NULL" : wp[1])));
  2435. #endif
  2436. if (cp == NULL && t->ioact == NULL) {
  2437. while ((cp = *owp++) != NULL && assign(cp, COPYV));
  2438. DBGPRINTF(("FORKEXEC: returning setstatus()\n"));
  2439. return (setstatus(0));
  2440. } else if (cp != NULL) {
  2441. shcom = inbuilt(cp);
  2442. }
  2443. }
  2444. t->words = wp;
  2445. f = act;
  2446. #if 0
  2447. DBGPRINTF3(("FORKEXEC: t->words is %s, t->words[1] is %s\n",
  2448. ((t->words == NULL) ? "NULL" : t->words[0]),
  2449. ((t->words == NULL) ? "NULL" : t->words[1])));
  2450. #endif
  2451. DBGPRINTF(("FORKEXEC: shcom 0x%x, f&FEXEC 0x%x, owp 0x%x\n", shcom,
  2452. f & FEXEC, owp));
  2453. if (shcom == NULL && (f & FEXEC) == 0) {
  2454. /* Save values in case the child process alters them */
  2455. hpin = pin;
  2456. hpout = pout;
  2457. hwp = *wp;
  2458. hinteractive = interactive;
  2459. hintr = intr;
  2460. hbrklist = brklist;
  2461. hexecflg = execflg;
  2462. DBGPRINTF3(("FORKEXEC: calling vfork()...\n"));
  2463. newpid = vfork();
  2464. if (newpid == -1) {
  2465. DBGPRINTF(("FORKEXEC: ERROR, unable to vfork()!\n"));
  2466. return (-1);
  2467. }
  2468. if (newpid > 0) { /* Parent */
  2469. /* Restore values */
  2470. pin = hpin;
  2471. pout = hpout;
  2472. *wp = hwp;
  2473. interactive = hinteractive;
  2474. intr = hintr;
  2475. brklist = hbrklist;
  2476. execflg = hexecflg;
  2477. /* moved up
  2478. if (i == -1)
  2479. return(rv);
  2480. */
  2481. if (pin != NULL)
  2482. closepipe(pin);
  2483. return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0);
  2484. }
  2485. /* Must be the child process, pid should be 0 */
  2486. DBGPRINTF(("FORKEXEC: child process, shcom=0x%x\n", shcom));
  2487. if (interactive) {
  2488. signal(SIGINT, SIG_IGN);
  2489. signal(SIGQUIT, SIG_IGN);
  2490. resetsig = 1;
  2491. }
  2492. interactive = 0;
  2493. intr = 0;
  2494. forked = 1;
  2495. brklist = 0;
  2496. execflg = 0;
  2497. }
  2498. if (owp != NULL)
  2499. while ((cp = *owp++) != NULL && assign(cp, COPYV))
  2500. if (shcom == NULL)
  2501. export(lookup(cp));
  2502. #ifdef COMPIPE
  2503. if ((pin != NULL || pout != NULL) && shcom != NULL && shcom != doexec) {
  2504. err("piping to/from shell builtins not yet done");
  2505. if (forked)
  2506. _exit(-1);
  2507. return (-1);
  2508. }
  2509. #endif
  2510. if (pin != NULL) {
  2511. dup2(pin[0], 0);
  2512. closepipe(pin);
  2513. }
  2514. if (pout != NULL) {
  2515. dup2(pout[1], 1);
  2516. closepipe(pout);
  2517. }
  2518. if ((iopp = t->ioact) != NULL) {
  2519. if (shcom != NULL && shcom != doexec) {
  2520. prs(cp);
  2521. err(": cannot redirect shell command");
  2522. if (forked)
  2523. _exit(-1);
  2524. return (-1);
  2525. }
  2526. while (*iopp)
  2527. if (iosetup(*iopp++, pin != NULL, pout != NULL)) {
  2528. if (forked)
  2529. _exit(rv);
  2530. return (rv);
  2531. }
  2532. }
  2533. if (shcom) {
  2534. i = setstatus((*shcom) (t));
  2535. if (forked)
  2536. _exit(i);
  2537. DBGPRINTF(("FORKEXEC: returning i=%d\n", i));
  2538. return (i);
  2539. }
  2540. /* should use FIOCEXCL */
  2541. for (i = FDBASE; i < NOFILE; i++)
  2542. close(i);
  2543. if (resetsig) {
  2544. signal(SIGINT, SIG_DFL);
  2545. signal(SIGQUIT, SIG_DFL);
  2546. }
  2547. if (t->type == TPAREN)
  2548. _exit(execute(t->left, NOPIPE, NOPIPE, FEXEC));
  2549. if (wp[0] == NULL)
  2550. _exit(0);
  2551. cp = rexecve(wp[0], wp, makenv(0, NULL));
  2552. prs(wp[0]);
  2553. prs(": ");
  2554. err(cp);
  2555. if (!execflg)
  2556. trap[0] = NULL;
  2557. DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", newpid));
  2558. leave();
  2559. /* NOTREACHED */
  2560. _exit(1);
  2561. }
  2562. /*
  2563. * 0< 1> are ignored as required
  2564. * within pipelines.
  2565. */
  2566. static int iosetup(iop, pipein, pipeout)
  2567. REGISTER struct ioword *iop;
  2568. int pipein, pipeout;
  2569. {
  2570. REGISTER int u = -1;
  2571. char *cp = NULL, *msg;
  2572. DBGPRINTF(("IOSETUP: iop 0x%x, pipein 0x%x, pipeout 0x%x\n", iop,
  2573. pipein, pipeout));
  2574. if (iop->io_unit == IODEFAULT) /* take default */
  2575. iop->io_unit = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
  2576. if (pipein && iop->io_unit == 0)
  2577. return (0);
  2578. if (pipeout && iop->io_unit == 1)
  2579. return (0);
  2580. msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create";
  2581. if ((iop->io_flag & IOHERE) == 0) {
  2582. cp = iop->io_name;
  2583. if ((cp = evalstr(cp, DOSUB | DOTRIM)) == NULL)
  2584. return (1);
  2585. }
  2586. if (iop->io_flag & IODUP) {
  2587. if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
  2588. prs(cp);
  2589. err(": illegal >& argument");
  2590. return (1);
  2591. }
  2592. if (*cp == '-')
  2593. iop->io_flag = IOCLOSE;
  2594. iop->io_flag &= ~(IOREAD | IOWRITE);
  2595. }
  2596. switch (iop->io_flag) {
  2597. case IOREAD:
  2598. u = open(cp, 0);
  2599. break;
  2600. case IOHERE:
  2601. case IOHERE | IOXHERE:
  2602. u = herein(iop->io_name, iop->io_flag & IOXHERE);
  2603. cp = "here file";
  2604. break;
  2605. case IOWRITE | IOCAT:
  2606. if ((u = open(cp, 1)) >= 0) {
  2607. lseek(u, (long) 0, 2);
  2608. break;
  2609. }
  2610. case IOWRITE:
  2611. u = creat(cp, 0666);
  2612. break;
  2613. case IODUP:
  2614. u = dup2(*cp - '0', iop->io_unit);
  2615. break;
  2616. case IOCLOSE:
  2617. close(iop->io_unit);
  2618. return (0);
  2619. }
  2620. if (u < 0) {
  2621. prs(cp);
  2622. prs(": cannot ");
  2623. warn(msg);
  2624. return (1);
  2625. } else {
  2626. if (u != iop->io_unit) {
  2627. dup2(u, iop->io_unit);
  2628. close(u);
  2629. }
  2630. }
  2631. return (0);
  2632. }
  2633. static void echo(wp)
  2634. REGISTER char **wp;
  2635. {
  2636. REGISTER int i;
  2637. prs("+");
  2638. for (i = 0; wp[i]; i++) {
  2639. if (i)
  2640. prs(" ");
  2641. prs(wp[i]);
  2642. }
  2643. prs("\n");
  2644. }
  2645. static struct op **find1case(t, w)
  2646. struct op *t;
  2647. char *w;
  2648. {
  2649. REGISTER struct op *t1;
  2650. struct op **tp;
  2651. REGISTER char **wp, *cp;
  2652. if (t == NULL) {
  2653. DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n"));
  2654. return ((struct op **) NULL);
  2655. }
  2656. DBGPRINTF3(("FIND1CASE: enter, t->type=%d (%s)\n", t->type,
  2657. T_CMD_NAMES[t->type]));
  2658. if (t->type == TLIST) {
  2659. if ((tp = find1case(t->left, w)) != NULL) {
  2660. DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=0x%x\n", tp));
  2661. return (tp);
  2662. }
  2663. t1 = t->right; /* TPAT */
  2664. } else
  2665. t1 = t;
  2666. for (wp = t1->words; *wp;)
  2667. if ((cp = evalstr(*wp++, DOSUB)) && gmatch(w, cp)) {
  2668. DBGPRINTF3(("FIND1CASE: returning &t1->left= 0x%x.\n",
  2669. &t1->left));
  2670. return (&t1->left);
  2671. }
  2672. DBGPRINTF(("FIND1CASE: returning NULL\n"));
  2673. return ((struct op **) NULL);
  2674. }
  2675. static struct op *findcase(t, w)
  2676. struct op *t;
  2677. char *w;
  2678. {
  2679. REGISTER struct op **tp;
  2680. return ((tp = find1case(t, w)) != NULL ? *tp : (struct op *) NULL);
  2681. }
  2682. /*
  2683. * Enter a new loop level (marked for break/continue).
  2684. */
  2685. static void brkset(bc)
  2686. struct brkcon *bc;
  2687. {
  2688. bc->nextlev = brklist;
  2689. brklist = bc;
  2690. }
  2691. /*
  2692. * Wait for the last process created.
  2693. * Print a message for each process found
  2694. * that was killed by a signal.
  2695. * Ignore interrupt signals while waiting
  2696. * unless `canintr' is true.
  2697. */
  2698. static int waitfor(lastpid, canintr)
  2699. REGISTER int lastpid;
  2700. int canintr;
  2701. {
  2702. REGISTER int pid, rv;
  2703. int s;
  2704. int oheedint = heedint;
  2705. heedint = 0;
  2706. rv = 0;
  2707. do {
  2708. pid = wait(&s);
  2709. if (pid == -1) {
  2710. if (errno != EINTR || canintr)
  2711. break;
  2712. } else {
  2713. if ((rv = WAITSIG(s)) != 0) {
  2714. if (rv < NSIGNAL) {
  2715. if (signame[rv] != NULL) {
  2716. if (pid != lastpid) {
  2717. prn(pid);
  2718. prs(": ");
  2719. }
  2720. prs(signame[rv]);
  2721. }
  2722. } else {
  2723. if (pid != lastpid) {
  2724. prn(pid);
  2725. prs(": ");
  2726. }
  2727. prs("Signal ");
  2728. prn(rv);
  2729. prs(" ");
  2730. }
  2731. if (WAITCORE(s))
  2732. prs(" - core dumped");
  2733. if (rv >= NSIGNAL || signame[rv])
  2734. prs("\n");
  2735. rv = -1;
  2736. } else
  2737. rv = WAITVAL(s);
  2738. }
  2739. } while (pid != lastpid);
  2740. heedint = oheedint;
  2741. if (intr) {
  2742. if (interactive) {
  2743. if (canintr)
  2744. intr = 0;
  2745. } else {
  2746. if (exstat == 0)
  2747. exstat = rv;
  2748. onintr(0);
  2749. }
  2750. }
  2751. return (rv);
  2752. }
  2753. static int setstatus(s)
  2754. REGISTER int s;
  2755. {
  2756. exstat = s;
  2757. setval(lookup("?"), putn(s));
  2758. return (s);
  2759. }
  2760. /*
  2761. * PATH-searching interface to execve.
  2762. * If getenv("PATH") were kept up-to-date,
  2763. * execvp might be used.
  2764. */
  2765. static char *rexecve(c, v, envp)
  2766. char *c, **v, **envp;
  2767. {
  2768. REGISTER int i;
  2769. REGISTER char *sp, *tp;
  2770. int eacces = 0, asis = 0;
  2771. #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
  2772. char *name = c;
  2773. optind = 1;
  2774. if (find_applet_by_name(name)) {
  2775. /* We have to exec here since we vforked. Running
  2776. * run_applet_by_name() won't work and bad things
  2777. * will happen. */
  2778. execve("/proc/self/exe", v, envp);
  2779. execve("busybox", v, envp);
  2780. }
  2781. #endif
  2782. DBGPRINTF(("REXECVE: c=0x%x, v=0x%x, envp=0x%x\n", c, v, envp));
  2783. sp = any('/', c) ? "" : path->value;
  2784. asis = *sp == '\0';
  2785. while (asis || *sp != '\0') {
  2786. asis = 0;
  2787. tp = e.linep;
  2788. for (; *sp != '\0'; tp++)
  2789. if ((*tp = *sp++) == ':') {
  2790. asis = *sp == '\0';
  2791. break;
  2792. }
  2793. if (tp != e.linep)
  2794. *tp++ = '/';
  2795. for (i = 0; (*tp++ = c[i++]) != '\0';);
  2796. DBGPRINTF3(("REXECVE: e.linep is %s\n", e.linep));
  2797. execve(e.linep, v, envp);
  2798. switch (errno) {
  2799. case ENOEXEC:
  2800. *v = e.linep;
  2801. tp = *--v;
  2802. *v = e.linep;
  2803. execve(DEFAULT_SHELL, v, envp);
  2804. *v = tp;
  2805. return ("no Shell");
  2806. case ENOMEM:
  2807. return ((char *) bb_msg_memory_exhausted);
  2808. case E2BIG:
  2809. return ("argument list too long");
  2810. case EACCES:
  2811. eacces++;
  2812. break;
  2813. }
  2814. }
  2815. return (errno == ENOENT ? "not found" : "cannot execute");
  2816. }
  2817. /*
  2818. * Run the command produced by generator `f'
  2819. * applied to stream `arg'.
  2820. */
  2821. static int run(struct ioarg *argp, int (*f) (struct ioarg *))
  2822. {
  2823. struct op *otree;
  2824. struct wdblock *swdlist;
  2825. struct wdblock *siolist;
  2826. jmp_buf ev, rt;
  2827. xint *ofail;
  2828. int rv;
  2829. #if __GNUC__
  2830. /* Avoid longjmp clobbering */
  2831. (void) &rv;
  2832. #endif
  2833. DBGPRINTF(("RUN: enter, areanum %d, outtree 0x%x, failpt 0x%x\n",
  2834. areanum, outtree, failpt));
  2835. areanum++;
  2836. swdlist = wdlist;
  2837. siolist = iolist;
  2838. otree = outtree;
  2839. ofail = failpt;
  2840. rv = -1;
  2841. if (newenv(setjmp(errpt = ev)) == 0) {
  2842. wdlist = 0;
  2843. iolist = 0;
  2844. pushio(argp, f);
  2845. e.iobase = e.iop;
  2846. yynerrs = 0;
  2847. if (setjmp(failpt = rt) == 0 && yyparse() == 0)
  2848. rv = execute(outtree, NOPIPE, NOPIPE, 0);
  2849. quitenv();
  2850. } else {
  2851. DBGPRINTF(("RUN: error from newenv()!\n"));
  2852. }
  2853. wdlist = swdlist;
  2854. iolist = siolist;
  2855. failpt = ofail;
  2856. outtree = otree;
  2857. freearea(areanum--);
  2858. return (rv);
  2859. }
  2860. /* -------- do.c -------- */
  2861. /*
  2862. * built-in commands: doX
  2863. */
  2864. static int dohelp(struct op *t)
  2865. {
  2866. int col;
  2867. const struct builtincmd *x;
  2868. printf("\nBuilt-in commands:\n");
  2869. printf("-------------------\n");
  2870. for (col = 0, x = builtincmds; x->builtinfunc != NULL; x++) {
  2871. if (!x->name)
  2872. continue;
  2873. col += printf("%s%s", ((col == 0) ? "\t" : " "), x->name);
  2874. if (col > 60) {
  2875. printf("\n");
  2876. col = 0;
  2877. }
  2878. }
  2879. #ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
  2880. {
  2881. int i;
  2882. const struct BB_applet *applet;
  2883. extern const struct BB_applet applets[];
  2884. extern const size_t NUM_APPLETS;
  2885. for (i = 0, applet = applets; i < NUM_APPLETS; applet++, i++) {
  2886. if (!applet->name)
  2887. continue;
  2888. col += printf("%s%s", ((col == 0) ? "\t" : " "), applet->name);
  2889. if (col > 60) {
  2890. printf("\n");
  2891. col = 0;
  2892. }
  2893. }
  2894. }
  2895. #endif
  2896. printf("\n\n");
  2897. return EXIT_SUCCESS;
  2898. }
  2899. static int dolabel(struct op *t)
  2900. {
  2901. return (0);
  2902. }
  2903. static int dochdir(t)
  2904. REGISTER struct op *t;
  2905. {
  2906. REGISTER char *cp, *er;
  2907. if ((cp = t->words[1]) == NULL && (cp = homedir->value) == NULL)
  2908. er = ": no home directory";
  2909. else if (chdir(cp) < 0)
  2910. er = ": bad directory";
  2911. else
  2912. return (0);
  2913. prs(cp != NULL ? cp : "cd");
  2914. err(er);
  2915. return (1);
  2916. }
  2917. static int doshift(t)
  2918. REGISTER struct op *t;
  2919. {
  2920. REGISTER int n;
  2921. n = t->words[1] ? getn(t->words[1]) : 1;
  2922. if (dolc < n) {
  2923. err("nothing to shift");
  2924. return (1);
  2925. }
  2926. dolv[n] = dolv[0];
  2927. dolv += n;
  2928. dolc -= n;
  2929. setval(lookup("#"), putn(dolc));
  2930. return (0);
  2931. }
  2932. /*
  2933. * execute login and newgrp directly
  2934. */
  2935. static int dologin(t)
  2936. struct op *t;
  2937. {
  2938. REGISTER char *cp;
  2939. if (interactive) {
  2940. signal(SIGINT, SIG_DFL);
  2941. signal(SIGQUIT, SIG_DFL);
  2942. }
  2943. cp = rexecve(t->words[0], t->words, makenv(0, NULL));
  2944. prs(t->words[0]);
  2945. prs(": ");
  2946. err(cp);
  2947. return (1);
  2948. }
  2949. static int doumask(t)
  2950. REGISTER struct op *t;
  2951. {
  2952. REGISTER int i, n;
  2953. REGISTER char *cp;
  2954. if ((cp = t->words[1]) == NULL) {
  2955. i = umask(0);
  2956. umask(i);
  2957. for (n = 3 * 4; (n -= 3) >= 0;)
  2958. putc('0' + ((i >> n) & 07), stderr);
  2959. putc('\n', stderr);
  2960. } else {
  2961. for (n = 0; *cp >= '0' && *cp <= '9'; cp++)
  2962. n = n * 8 + (*cp - '0');
  2963. umask(n);
  2964. }
  2965. return (0);
  2966. }
  2967. static int doexec(t)
  2968. REGISTER struct op *t;
  2969. {
  2970. REGISTER int i;
  2971. jmp_buf ex;
  2972. xint *ofail;
  2973. t->ioact = NULL;
  2974. for (i = 0; (t->words[i] = t->words[i + 1]) != NULL; i++);
  2975. if (i == 0)
  2976. return (1);
  2977. execflg = 1;
  2978. ofail = failpt;
  2979. if (setjmp(failpt = ex) == 0)
  2980. execute(t, NOPIPE, NOPIPE, FEXEC);
  2981. failpt = ofail;
  2982. execflg = 0;
  2983. return (1);
  2984. }
  2985. static int dodot(t)
  2986. struct op *t;
  2987. {
  2988. REGISTER int i;
  2989. REGISTER char *sp, *tp;
  2990. char *cp;
  2991. int maltmp;
  2992. DBGPRINTF(("DODOT: enter, t=0x%x, tleft 0x%x, tright 0x%x, e.linep is %s\n", t, t->left, t->right, ((e.linep == NULL) ? "NULL" : e.linep)));
  2993. if ((cp = t->words[1]) == NULL) {
  2994. DBGPRINTF(("DODOT: bad args, ret 0\n"));
  2995. return (0);
  2996. } else {
  2997. DBGPRINTF(("DODOT: cp is %s\n", cp));
  2998. }
  2999. sp = any('/', cp) ? ":" : path->value;
  3000. DBGPRINTF(("DODOT: sp is %s, e.linep is %s\n",
  3001. ((sp == NULL) ? "NULL" : sp),
  3002. ((e.linep == NULL) ? "NULL" : e.linep)));
  3003. while (*sp) {
  3004. tp = e.linep;
  3005. while (*sp && (*tp = *sp++) != ':')
  3006. tp++;
  3007. if (tp != e.linep)
  3008. *tp++ = '/';
  3009. for (i = 0; (*tp++ = cp[i++]) != '\0';);
  3010. /* Original code */
  3011. if ((i = open(e.linep, 0)) >= 0) {
  3012. exstat = 0;
  3013. maltmp = remap(i);
  3014. DBGPRINTF(("DODOT: remap=%d, exstat=%d, e.iofd %d, i %d, e.linep is %s\n", maltmp, exstat, e.iofd, i, e.linep));
  3015. next(maltmp); /* Basically a PUSHIO */
  3016. DBGPRINTF(("DODOT: returning exstat=%d\n", exstat));
  3017. return (exstat);
  3018. }
  3019. } /* While */
  3020. prs(cp);
  3021. err(": not found");
  3022. return (-1);
  3023. }
  3024. static int dowait(t)
  3025. struct op *t;
  3026. {
  3027. REGISTER int i;
  3028. REGISTER char *cp;
  3029. if ((cp = t->words[1]) != NULL) {
  3030. i = getn(cp);
  3031. if (i == 0)
  3032. return (0);
  3033. } else
  3034. i = -1;
  3035. setstatus(waitfor(i, 1));
  3036. return (0);
  3037. }
  3038. static int doread(t)
  3039. struct op *t;
  3040. {
  3041. REGISTER char *cp, **wp;
  3042. REGISTER int nb = 0;
  3043. REGISTER int nl = 0;
  3044. if (t->words[1] == NULL) {
  3045. err("Usage: read name ...");
  3046. return (1);
  3047. }
  3048. for (wp = t->words + 1; *wp; wp++) {
  3049. for (cp = e.linep; !nl && cp < elinep - 1; cp++)
  3050. if ((nb = read(0, cp, sizeof(*cp))) != sizeof(*cp) ||
  3051. (nl = (*cp == '\n')) || (wp[1] && any(*cp, ifs->value)))
  3052. break;
  3053. *cp = 0;
  3054. if (nb <= 0)
  3055. break;
  3056. setval(lookup(*wp), e.linep);
  3057. }
  3058. return (nb <= 0);
  3059. }
  3060. static int doeval(t)
  3061. REGISTER struct op *t;
  3062. {
  3063. return (RUN(awordlist, t->words + 1, wdchar));
  3064. }
  3065. static int dotrap(t)
  3066. REGISTER struct op *t;
  3067. {
  3068. REGISTER int n, i;
  3069. REGISTER int resetsig;
  3070. if (t->words[1] == NULL) {
  3071. for (i = 0; i <= _NSIG; i++)
  3072. if (trap[i]) {
  3073. prn(i);
  3074. prs(": ");
  3075. prs(trap[i]);
  3076. prs("\n");
  3077. }
  3078. return (0);
  3079. }
  3080. resetsig = isdigit(*t->words[1]);
  3081. for (i = resetsig ? 1 : 2; t->words[i] != NULL; ++i) {
  3082. n = getsig(t->words[i]);
  3083. freecell(trap[n]);
  3084. trap[n] = 0;
  3085. if (!resetsig) {
  3086. if (*t->words[1] != '\0') {
  3087. trap[n] = strsave(t->words[1], 0);
  3088. setsig(n, sig);
  3089. } else
  3090. setsig(n, SIG_IGN);
  3091. } else {
  3092. if (interactive)
  3093. if (n == SIGINT)
  3094. setsig(n, onintr);
  3095. else
  3096. setsig(n, n == SIGQUIT ? SIG_IGN : SIG_DFL);
  3097. else
  3098. setsig(n, SIG_DFL);
  3099. }
  3100. }
  3101. return (0);
  3102. }
  3103. static int getsig(s)
  3104. char *s;
  3105. {
  3106. REGISTER int n;
  3107. if ((n = getn(s)) < 0 || n > _NSIG) {
  3108. err("trap: bad signal number");
  3109. n = 0;
  3110. }
  3111. return (n);
  3112. }
  3113. static void setsig(REGISTER int n, sighandler_t f)
  3114. {
  3115. if (n == 0)
  3116. return;
  3117. if (signal(n, SIG_IGN) != SIG_IGN || ourtrap[n]) {
  3118. ourtrap[n] = 1;
  3119. signal(n, f);
  3120. }
  3121. }
  3122. static int getn(as)
  3123. char *as;
  3124. {
  3125. REGISTER char *s;
  3126. REGISTER int n, m;
  3127. s = as;
  3128. m = 1;
  3129. if (*s == '-') {
  3130. m = -1;
  3131. s++;
  3132. }
  3133. for (n = 0; isdigit(*s); s++)
  3134. n = (n * 10) + (*s - '0');
  3135. if (*s) {
  3136. prs(as);
  3137. err(": bad number");
  3138. }
  3139. return (n * m);
  3140. }
  3141. static int dobreak(t)
  3142. struct op *t;
  3143. {
  3144. return (brkcontin(t->words[1], 1));
  3145. }
  3146. static int docontinue(t)
  3147. struct op *t;
  3148. {
  3149. return (brkcontin(t->words[1], 0));
  3150. }
  3151. static int brkcontin(cp, val)
  3152. REGISTER char *cp;
  3153. int val;
  3154. {
  3155. REGISTER struct brkcon *bc;
  3156. REGISTER int nl;
  3157. nl = cp == NULL ? 1 : getn(cp);
  3158. if (nl <= 0)
  3159. nl = 999;
  3160. do {
  3161. if ((bc = brklist) == NULL)
  3162. break;
  3163. brklist = bc->nextlev;
  3164. } while (--nl);
  3165. if (nl) {
  3166. err("bad break/continue level");
  3167. return (1);
  3168. }
  3169. isbreak = val;
  3170. longjmp(bc->brkpt, 1);
  3171. /* NOTREACHED */
  3172. }
  3173. static int doexit(t)
  3174. struct op *t;
  3175. {
  3176. REGISTER char *cp;
  3177. execflg = 0;
  3178. if ((cp = t->words[1]) != NULL)
  3179. setstatus(getn(cp));
  3180. DBGPRINTF(("DOEXIT: calling leave(), t=0x%x\n", t));
  3181. leave();
  3182. /* NOTREACHED */
  3183. return (0);
  3184. }
  3185. static int doexport(t)
  3186. struct op *t;
  3187. {
  3188. rdexp(t->words + 1, export, EXPORT);
  3189. return (0);
  3190. }
  3191. static int doreadonly(t)
  3192. struct op *t;
  3193. {
  3194. rdexp(t->words + 1, ronly, RONLY);
  3195. return (0);
  3196. }
  3197. static void rdexp(char **wp, void (*f) (struct var *), int key)
  3198. {
  3199. DBGPRINTF6(("RDEXP: enter, wp=0x%x, func=0x%x, key=%d\n", wp, f, key));
  3200. DBGPRINTF6(("RDEXP: *wp=%s\n", *wp));
  3201. if (*wp != NULL) {
  3202. for (; *wp != NULL; wp++) {
  3203. if (isassign(*wp)) {
  3204. char *cp;
  3205. assign(*wp, COPYV);
  3206. for (cp = *wp; *cp != '='; cp++);
  3207. *cp = '\0';
  3208. }
  3209. if (checkname(*wp))
  3210. (*f) (lookup(*wp));
  3211. else
  3212. badid(*wp);
  3213. }
  3214. } else
  3215. putvlist(key, 1);
  3216. }
  3217. static void badid(s)
  3218. REGISTER char *s;
  3219. {
  3220. prs(s);
  3221. err(": bad identifier");
  3222. }
  3223. static int doset(t)
  3224. REGISTER struct op *t;
  3225. {
  3226. REGISTER struct var *vp;
  3227. REGISTER char *cp;
  3228. REGISTER int n;
  3229. if ((cp = t->words[1]) == NULL) {
  3230. for (vp = vlist; vp; vp = vp->next)
  3231. varput(vp->name, 1);
  3232. return (0);
  3233. }
  3234. if (*cp == '-') {
  3235. /* bad: t->words++; */
  3236. for (n = 0; (t->words[n] = t->words[n + 1]) != NULL; n++);
  3237. if (*++cp == 0)
  3238. flag['x'] = flag['v'] = 0;
  3239. else
  3240. for (; *cp; cp++)
  3241. switch (*cp) {
  3242. case 'e':
  3243. if (!interactive)
  3244. flag['e']++;
  3245. break;
  3246. default:
  3247. if (*cp >= 'a' && *cp <= 'z')
  3248. flag[(int) *cp]++;
  3249. break;
  3250. }
  3251. setdash();
  3252. }
  3253. if (t->words[1]) {
  3254. t->words[0] = dolv[0];
  3255. for (n = 1; t->words[n]; n++)
  3256. setarea((char *) t->words[n], 0);
  3257. dolc = n - 1;
  3258. dolv = t->words;
  3259. setval(lookup("#"), putn(dolc));
  3260. setarea((char *) (dolv - 1), 0);
  3261. }
  3262. return (0);
  3263. }
  3264. static void varput(s, out)
  3265. REGISTER char *s;
  3266. int out;
  3267. {
  3268. if (isalnum(*s) || *s == '_') {
  3269. write(out, s, strlen(s));
  3270. write(out, "\n", 1);
  3271. }
  3272. }
  3273. /*
  3274. * Copyright (c) 1999 Herbert Xu <herbert@debian.org>
  3275. * This file contains code for the times builtin.
  3276. */
  3277. static int dotimes(struct op *t)
  3278. {
  3279. struct tms buf;
  3280. long int clk_tck = sysconf(_SC_CLK_TCK);
  3281. times(&buf);
  3282. printf("%dm%fs %dm%fs\n%dm%fs %dm%fs\n",
  3283. (int) (buf.tms_utime / clk_tck / 60),
  3284. ((double) buf.tms_utime) / clk_tck,
  3285. (int) (buf.tms_stime / clk_tck / 60),
  3286. ((double) buf.tms_stime) / clk_tck,
  3287. (int) (buf.tms_cutime / clk_tck / 60),
  3288. ((double) buf.tms_cutime) / clk_tck,
  3289. (int) (buf.tms_cstime / clk_tck / 60),
  3290. ((double) buf.tms_cstime) / clk_tck);
  3291. return 0;
  3292. }
  3293. static int (*inbuilt(char *s)) (struct op *) {
  3294. const struct builtincmd *bp;
  3295. for (bp = builtincmds; bp->name != NULL; bp++)
  3296. if (strcmp(bp->name, s) == 0)
  3297. return (bp->builtinfunc);
  3298. return (NULL);
  3299. }
  3300. /* -------- eval.c -------- */
  3301. /*
  3302. * ${}
  3303. * `command`
  3304. * blank interpretation
  3305. * quoting
  3306. * glob
  3307. */
  3308. static char **eval(char **ap, int f)
  3309. {
  3310. struct wdblock *wb;
  3311. char **wp;
  3312. char **wf;
  3313. jmp_buf ev;
  3314. #if __GNUC__
  3315. /* Avoid longjmp clobbering */
  3316. (void) &wp;
  3317. (void) &ap;
  3318. #endif
  3319. DBGPRINTF4(("EVAL: enter, f=%d\n", f));
  3320. wp = NULL;
  3321. wb = NULL;
  3322. wf = NULL;
  3323. if (newenv(setjmp(errpt = ev)) == 0) {
  3324. while (*ap && isassign(*ap))
  3325. expand(*ap++, &wb, f & ~DOGLOB);
  3326. if (flag['k']) {
  3327. for (wf = ap; *wf; wf++) {
  3328. if (isassign(*wf))
  3329. expand(*wf, &wb, f & ~DOGLOB);
  3330. }
  3331. }
  3332. for (wb = addword((char *) 0, wb); *ap; ap++) {
  3333. if (!flag['k'] || !isassign(*ap))
  3334. expand(*ap, &wb, f & ~DOKEY);
  3335. }
  3336. wb = addword((char *) 0, wb);
  3337. wp = getwords(wb);
  3338. quitenv();
  3339. } else
  3340. gflg = 1;
  3341. return (gflg ? (char **) NULL : wp);
  3342. }
  3343. /*
  3344. * Make the exported environment from the exported
  3345. * names in the dictionary. Keyword assignments
  3346. * will already have been done.
  3347. */
  3348. static char **makenv(int all, struct wdblock *wb)
  3349. {
  3350. REGISTER struct var *vp;
  3351. DBGPRINTF5(("MAKENV: enter, all=%d\n", all));
  3352. for (vp = vlist; vp; vp = vp->next)
  3353. if (all || vp->status & EXPORT)
  3354. wb = addword(vp->name, wb);
  3355. wb = addword((char *) 0, wb);
  3356. return (getwords(wb));
  3357. }
  3358. static char *evalstr(cp, f)
  3359. REGISTER char *cp;
  3360. int f;
  3361. {
  3362. struct wdblock *wb;
  3363. DBGPRINTF6(("EVALSTR: enter, cp=0x%x, f=%d\n", cp, f));
  3364. wb = NULL;
  3365. if (expand(cp, &wb, f)) {
  3366. if (wb == NULL || wb->w_nword == 0
  3367. || (cp = wb->w_words[0]) == NULL)
  3368. cp = "";
  3369. DELETE(wb);
  3370. } else
  3371. cp = NULL;
  3372. return (cp);
  3373. }
  3374. static int expand(char *cp, REGISTER struct wdblock **wbp, int f)
  3375. {
  3376. jmp_buf ev;
  3377. #if __GNUC__
  3378. /* Avoid longjmp clobbering */
  3379. (void) &cp;
  3380. #endif
  3381. DBGPRINTF3(("EXPAND: enter, f=%d\n", f));
  3382. gflg = 0;
  3383. if (cp == NULL)
  3384. return (0);
  3385. if (!anys("$`'\"", cp) &&
  3386. !anys(ifs->value, cp) && ((f & DOGLOB) == 0 || !anys("[*?", cp))) {
  3387. cp = strsave(cp, areanum);
  3388. if (f & DOTRIM)
  3389. unquote(cp);
  3390. *wbp = addword(cp, *wbp);
  3391. return (1);
  3392. }
  3393. if (newenv(setjmp(errpt = ev)) == 0) {
  3394. PUSHIO(aword, cp, strchar);
  3395. e.iobase = e.iop;
  3396. while ((cp = blank(f)) && gflg == 0) {
  3397. e.linep = cp;
  3398. cp = strsave(cp, areanum);
  3399. if ((f & DOGLOB) == 0) {
  3400. if (f & DOTRIM)
  3401. unquote(cp);
  3402. *wbp = addword(cp, *wbp);
  3403. } else
  3404. *wbp = glob(cp, *wbp);
  3405. }
  3406. quitenv();
  3407. } else
  3408. gflg = 1;
  3409. return (gflg == 0);
  3410. }
  3411. /*
  3412. * Blank interpretation and quoting
  3413. */
  3414. static char *blank(f)
  3415. int f;
  3416. {
  3417. REGISTER int c, c1;
  3418. REGISTER char *sp;
  3419. int scanequals, foundequals;
  3420. DBGPRINTF3(("BLANK: enter, f=%d\n", f));
  3421. sp = e.linep;
  3422. scanequals = f & DOKEY;
  3423. foundequals = 0;
  3424. loop:
  3425. switch (c = subgetc('"', foundequals)) {
  3426. case 0:
  3427. if (sp == e.linep)
  3428. return (0);
  3429. *e.linep++ = 0;
  3430. return (sp);
  3431. default:
  3432. if (f & DOBLANK && any(c, ifs->value))
  3433. goto loop;
  3434. break;
  3435. case '"':
  3436. case '\'':
  3437. scanequals = 0;
  3438. if (INSUB())
  3439. break;
  3440. for (c1 = c; (c = subgetc(c1, 1)) != c1;) {
  3441. if (c == 0)
  3442. break;
  3443. if (c == '\'' || !any(c, "$`\""))
  3444. c |= QUOTE;
  3445. *e.linep++ = c;
  3446. }
  3447. c = 0;
  3448. }
  3449. unget(c);
  3450. if (!isalpha(c) && c != '_')
  3451. scanequals = 0;
  3452. for (;;) {
  3453. c = subgetc('"', foundequals);
  3454. if (c == 0 ||
  3455. f & (DOBLANK && any(c, ifs->value)) ||
  3456. (!INSUB() && any(c, "\"'"))) {
  3457. scanequals = 0;
  3458. unget(c);
  3459. if (any(c, "\"'"))
  3460. goto loop;
  3461. break;
  3462. }
  3463. if (scanequals) {
  3464. if (c == '=') {
  3465. foundequals = 1;
  3466. scanequals = 0;
  3467. } else if (!isalnum(c) && c != '_')
  3468. scanequals = 0;
  3469. }
  3470. *e.linep++ = c;
  3471. }
  3472. *e.linep++ = 0;
  3473. return (sp);
  3474. }
  3475. /*
  3476. * Get characters, substituting for ` and $
  3477. */
  3478. static int subgetc(ec, quoted)
  3479. REGISTER char ec;
  3480. int quoted;
  3481. {
  3482. REGISTER char c;
  3483. DBGPRINTF3(("SUBGETC: enter, quoted=%d\n", quoted));
  3484. again:
  3485. c = my_getc(ec);
  3486. if (!INSUB() && ec != '\'') {
  3487. if (c == '`') {
  3488. if (grave(quoted) == 0)
  3489. return (0);
  3490. e.iop->task = XGRAVE;
  3491. goto again;
  3492. }
  3493. if (c == '$' && (c = dollar(quoted)) == 0) {
  3494. e.iop->task = XDOLL;
  3495. goto again;
  3496. }
  3497. }
  3498. return (c);
  3499. }
  3500. /*
  3501. * Prepare to generate the string returned by ${} substitution.
  3502. */
  3503. static int dollar(quoted)
  3504. int quoted;
  3505. {
  3506. int otask;
  3507. struct io *oiop;
  3508. char *dolp;
  3509. REGISTER char *s, c, *cp = NULL;
  3510. struct var *vp;
  3511. DBGPRINTF3(("DOLLAR: enter, quoted=%d\n", quoted));
  3512. c = readc();
  3513. s = e.linep;
  3514. if (c != '{') {
  3515. *e.linep++ = c;
  3516. if (isalpha(c) || c == '_') {
  3517. while ((c = readc()) != 0 && (isalnum(c) || c == '_'))
  3518. if (e.linep < elinep)
  3519. *e.linep++ = c;
  3520. unget(c);
  3521. }
  3522. c = 0;
  3523. } else {
  3524. oiop = e.iop;
  3525. otask = e.iop->task;
  3526. e.iop->task = XOTHER;
  3527. while ((c = subgetc('"', 0)) != 0 && c != '}' && c != '\n')
  3528. if (e.linep < elinep)
  3529. *e.linep++ = c;
  3530. if (oiop == e.iop)
  3531. e.iop->task = otask;
  3532. if (c != '}') {
  3533. err("unclosed ${");
  3534. gflg++;
  3535. return (c);
  3536. }
  3537. }
  3538. if (e.linep >= elinep) {
  3539. err("string in ${} too long");
  3540. gflg++;
  3541. e.linep -= 10;
  3542. }
  3543. *e.linep = 0;
  3544. if (*s)
  3545. for (cp = s + 1; *cp; cp++)
  3546. if (any(*cp, "=-+?")) {
  3547. c = *cp;
  3548. *cp++ = 0;
  3549. break;
  3550. }
  3551. if (s[1] == 0 && (*s == '*' || *s == '@')) {
  3552. if (dolc > 1) {
  3553. /* currently this does not distinguish $* and $@ */
  3554. /* should check dollar */
  3555. e.linep = s;
  3556. PUSHIO(awordlist, dolv + 1, dolchar);
  3557. return (0);
  3558. } else { /* trap the nasty ${=} */
  3559. s[0] = '1';
  3560. s[1] = 0;
  3561. }
  3562. }
  3563. vp = lookup(s);
  3564. if ((dolp = vp->value) == null) {
  3565. switch (c) {
  3566. case '=':
  3567. if (isdigit(*s)) {
  3568. err("cannot use ${...=...} with $n");
  3569. gflg++;
  3570. break;
  3571. }
  3572. setval(vp, cp);
  3573. dolp = vp->value;
  3574. break;
  3575. case '-':
  3576. dolp = strsave(cp, areanum);
  3577. break;
  3578. case '?':
  3579. if (*cp == 0) {
  3580. prs("missing value for ");
  3581. err(s);
  3582. } else
  3583. err(cp);
  3584. gflg++;
  3585. break;
  3586. }
  3587. } else if (c == '+')
  3588. dolp = strsave(cp, areanum);
  3589. if (flag['u'] && dolp == null) {
  3590. prs("unset variable: ");
  3591. err(s);
  3592. gflg++;
  3593. }
  3594. e.linep = s;
  3595. PUSHIO(aword, dolp, quoted ? qstrchar : strchar);
  3596. return (0);
  3597. }
  3598. /*
  3599. * Run the command in `...` and read its output.
  3600. */
  3601. static int grave(quoted)
  3602. int quoted;
  3603. {
  3604. char *cp;
  3605. REGISTER int i;
  3606. int j;
  3607. int pf[2];
  3608. static char child_cmd[LINELIM];
  3609. char *src;
  3610. char *dest;
  3611. int count;
  3612. int ignore;
  3613. int ignore_once;
  3614. char *argument_list[4];
  3615. struct wdblock *wb = NULL;
  3616. #if __GNUC__
  3617. /* Avoid longjmp clobbering */
  3618. (void) &cp;
  3619. #endif
  3620. for (cp = e.iop->argp->aword; *cp != '`'; cp++)
  3621. if (*cp == 0) {
  3622. err("no closing `");
  3623. return (0);
  3624. }
  3625. /* string copy with dollar expansion */
  3626. src = e.iop->argp->aword;
  3627. dest = child_cmd;
  3628. count = 0;
  3629. ignore = 0;
  3630. ignore_once = 0;
  3631. while ((*src != '`') && (count < LINELIM)) {
  3632. if (*src == '\'')
  3633. ignore = !ignore;
  3634. if (*src == '\\')
  3635. ignore_once = 1;
  3636. if (*src == '$' && !ignore && !ignore_once) {
  3637. struct var *vp;
  3638. char var_name[LINELIM];
  3639. char alt_value[LINELIM];
  3640. int var_index = 0;
  3641. int alt_index = 0;
  3642. char operator = 0;
  3643. int braces = 0;
  3644. char *value;
  3645. src++;
  3646. if (*src == '{') {
  3647. braces = 1;
  3648. src++;
  3649. }
  3650. var_name[var_index++] = *src++;
  3651. while (isalnum(*src))
  3652. var_name[var_index++] = *src++;
  3653. var_name[var_index] = 0;
  3654. if (braces) {
  3655. switch (*src) {
  3656. case '}':
  3657. break;
  3658. case '-':
  3659. case '=':
  3660. case '+':
  3661. case '?':
  3662. operator = * src;
  3663. break;
  3664. default:
  3665. err("unclosed ${\n");
  3666. return (0);
  3667. }
  3668. if (operator) {
  3669. src++;
  3670. while (*src && (*src != '}')) {
  3671. alt_value[alt_index++] = *src++;
  3672. }
  3673. alt_value[alt_index] = 0;
  3674. if (*src != '}') {
  3675. err("unclosed ${\n");
  3676. return (0);
  3677. }
  3678. }
  3679. src++;
  3680. }
  3681. if (isalpha(*var_name)) {
  3682. /* let subshell handle it instead */
  3683. char *namep = var_name;
  3684. *dest++ = '$';
  3685. if (braces)
  3686. *dest++ = '{';
  3687. while (*namep)
  3688. *dest++ = *namep++;
  3689. if (operator) {
  3690. char *altp = alt_value;
  3691. *dest++ = operator;
  3692. while (*altp)
  3693. *dest++ = *altp++;
  3694. }
  3695. if (braces)
  3696. *dest++ = '}';
  3697. wb = addword(lookup(var_name)->name, wb);
  3698. } else {
  3699. /* expand */
  3700. vp = lookup(var_name);
  3701. if (vp->value != null)
  3702. value = (operator == '+') ?
  3703. alt_value : vp->value;
  3704. else if (operator == '?') {
  3705. err(alt_value);
  3706. return (0);
  3707. } else if (alt_index && (operator != '+')) {
  3708. value = alt_value;
  3709. if (operator == '=')
  3710. setval(vp, value);
  3711. } else
  3712. continue;
  3713. while (*value && (count < LINELIM)) {
  3714. *dest++ = *value++;
  3715. count++;
  3716. }
  3717. }
  3718. } else {
  3719. *dest++ = *src++;
  3720. count++;
  3721. ignore_once = 0;
  3722. }
  3723. }
  3724. *dest = '\0';
  3725. if (openpipe(pf) < 0)
  3726. return (0);
  3727. while ((i = vfork()) == -1 && errno == EAGAIN);
  3728. DBGPRINTF3(("GRAVE: i is %d\n", io));
  3729. if (i < 0) {
  3730. closepipe(pf);
  3731. err((char *) bb_msg_memory_exhausted);
  3732. return (0);
  3733. }
  3734. if (i != 0) {
  3735. waitpid(i, NULL, 0);
  3736. e.iop->argp->aword = ++cp;
  3737. close(pf[1]);
  3738. PUSHIO(afile, remap(pf[0]),
  3739. (int (*)(struct ioarg *)) ((quoted) ? qgravechar :
  3740. gravechar));
  3741. return (1);
  3742. }
  3743. /* allow trapped signals */
  3744. /* XXX - Maybe this signal stuff should go as well? */
  3745. for (j = 0; j <= _NSIG; j++)
  3746. if (ourtrap[j] && signal(j, SIG_IGN) != SIG_IGN)
  3747. signal(j, SIG_DFL);
  3748. dup2(pf[1], 1);
  3749. closepipe(pf);
  3750. argument_list[0] = (char *) DEFAULT_SHELL;
  3751. argument_list[1] = "-c";
  3752. argument_list[2] = child_cmd;
  3753. argument_list[3] = 0;
  3754. cp = rexecve(argument_list[0], argument_list, makenv(1, wb));
  3755. prs(argument_list[0]);
  3756. prs(": ");
  3757. err(cp);
  3758. _exit(1);
  3759. }
  3760. static char *unquote(as)
  3761. REGISTER char *as;
  3762. {
  3763. REGISTER char *s;
  3764. if ((s = as) != NULL)
  3765. while (*s)
  3766. *s++ &= ~QUOTE;
  3767. return (as);
  3768. }
  3769. /* -------- glob.c -------- */
  3770. /*
  3771. * glob
  3772. */
  3773. #define scopy(x) strsave((x), areanum)
  3774. #define BLKSIZ 512
  3775. #define NDENT ((BLKSIZ+sizeof(struct dirent)-1)/sizeof(struct dirent))
  3776. static struct wdblock *cl, *nl;
  3777. static char spcl[] = "[?*";
  3778. static struct wdblock *glob(cp, wb)
  3779. char *cp;
  3780. struct wdblock *wb;
  3781. {
  3782. REGISTER int i;
  3783. REGISTER char *pp;
  3784. if (cp == 0)
  3785. return (wb);
  3786. i = 0;
  3787. for (pp = cp; *pp; pp++)
  3788. if (any(*pp, spcl))
  3789. i++;
  3790. else if (!any(*pp & ~QUOTE, spcl))
  3791. *pp &= ~QUOTE;
  3792. if (i != 0) {
  3793. for (cl = addword(scopy(cp), (struct wdblock *) 0); anyspcl(cl);
  3794. cl = nl) {
  3795. nl = newword(cl->w_nword * 2);
  3796. for (i = 0; i < cl->w_nword; i++) { /* for each argument */
  3797. for (pp = cl->w_words[i]; *pp; pp++)
  3798. if (any(*pp, spcl)) {
  3799. globname(cl->w_words[i], pp);
  3800. break;
  3801. }
  3802. if (*pp == '\0')
  3803. nl = addword(scopy(cl->w_words[i]), nl);
  3804. }
  3805. for (i = 0; i < cl->w_nword; i++)
  3806. DELETE(cl->w_words[i]);
  3807. DELETE(cl);
  3808. }
  3809. for (i = 0; i < cl->w_nword; i++)
  3810. unquote(cl->w_words[i]);
  3811. glob0((char *) cl->w_words, cl->w_nword, sizeof(char *), xstrcmp);
  3812. if (cl->w_nword) {
  3813. for (i = 0; i < cl->w_nword; i++)
  3814. wb = addword(cl->w_words[i], wb);
  3815. DELETE(cl);
  3816. return (wb);
  3817. }
  3818. }
  3819. wb = addword(unquote(cp), wb);
  3820. return (wb);
  3821. }
  3822. static void globname(we, pp)
  3823. char *we;
  3824. REGISTER char *pp;
  3825. {
  3826. REGISTER char *np, *cp;
  3827. char *name, *gp, *dp;
  3828. int k;
  3829. DIR *dirp;
  3830. struct dirent *de;
  3831. char dname[NAME_MAX + 1];
  3832. struct stat dbuf;
  3833. for (np = we; np != pp; pp--)
  3834. if (pp[-1] == '/')
  3835. break;
  3836. for (dp = cp = space((int) (pp - np) + 3); np < pp;)
  3837. *cp++ = *np++;
  3838. *cp++ = '.';
  3839. *cp = '\0';
  3840. for (gp = cp = space(strlen(pp) + 1); *np && *np != '/';)
  3841. *cp++ = *np++;
  3842. *cp = '\0';
  3843. dirp = opendir(dp);
  3844. if (dirp == 0) {
  3845. DELETE(dp);
  3846. DELETE(gp);
  3847. return;
  3848. }
  3849. dname[NAME_MAX] = '\0';
  3850. while ((de = readdir(dirp)) != NULL) {
  3851. /* XXX Hmmm... What this could be? (abial) */
  3852. /*
  3853. if (ent[j].d_ino == 0)
  3854. continue;
  3855. */
  3856. strncpy(dname, de->d_name, NAME_MAX);
  3857. if (dname[0] == '.')
  3858. if (*gp != '.')
  3859. continue;
  3860. for (k = 0; k < NAME_MAX; k++)
  3861. if (any(dname[k], spcl))
  3862. dname[k] |= QUOTE;
  3863. if (gmatch(dname, gp)) {
  3864. name = generate(we, pp, dname, np);
  3865. if (*np && !anys(np, spcl)) {
  3866. if (stat(name, &dbuf)) {
  3867. DELETE(name);
  3868. continue;
  3869. }
  3870. }
  3871. nl = addword(name, nl);
  3872. }
  3873. }
  3874. closedir(dirp);
  3875. DELETE(dp);
  3876. DELETE(gp);
  3877. }
  3878. /*
  3879. * generate a pathname as below.
  3880. * start..end1 / middle end
  3881. * the slashes come for free
  3882. */
  3883. static char *generate(start1, end1, middle, end)
  3884. char *start1;
  3885. REGISTER char *end1;
  3886. char *middle, *end;
  3887. {
  3888. char *p;
  3889. REGISTER char *op, *xp;
  3890. p = op =
  3891. space((int) (end1 - start1) + strlen(middle) + strlen(end) + 2);
  3892. for (xp = start1; xp != end1;)
  3893. *op++ = *xp++;
  3894. for (xp = middle; (*op++ = *xp++) != '\0';);
  3895. op--;
  3896. for (xp = end; (*op++ = *xp++) != '\0';);
  3897. return (p);
  3898. }
  3899. static int anyspcl(wb)
  3900. REGISTER struct wdblock *wb;
  3901. {
  3902. REGISTER int i;
  3903. REGISTER char **wd;
  3904. wd = wb->w_words;
  3905. for (i = 0; i < wb->w_nword; i++)
  3906. if (anys(spcl, *wd++))
  3907. return (1);
  3908. return (0);
  3909. }
  3910. static int xstrcmp(p1, p2)
  3911. char *p1, *p2;
  3912. {
  3913. return (strcmp(*(char **) p1, *(char **) p2));
  3914. }
  3915. /* -------- word.c -------- */
  3916. static struct wdblock *newword(nw)
  3917. REGISTER int nw;
  3918. {
  3919. REGISTER struct wdblock *wb;
  3920. wb = (struct wdblock *) space(sizeof(*wb) + nw * sizeof(char *));
  3921. wb->w_bsize = nw;
  3922. wb->w_nword = 0;
  3923. return (wb);
  3924. }
  3925. static struct wdblock *addword(wd, wb)
  3926. char *wd;
  3927. REGISTER struct wdblock *wb;
  3928. {
  3929. REGISTER struct wdblock *wb2;
  3930. REGISTER int nw;
  3931. if (wb == NULL)
  3932. wb = newword(NSTART);
  3933. if ((nw = wb->w_nword) >= wb->w_bsize) {
  3934. wb2 = newword(nw * 2);
  3935. memcpy((char *) wb2->w_words, (char *) wb->w_words,
  3936. nw * sizeof(char *));
  3937. wb2->w_nword = nw;
  3938. DELETE(wb);
  3939. wb = wb2;
  3940. }
  3941. wb->w_words[wb->w_nword++] = wd;
  3942. return (wb);
  3943. }
  3944. static
  3945. char **getwords(wb)
  3946. REGISTER struct wdblock *wb;
  3947. {
  3948. REGISTER char **wd;
  3949. REGISTER int nb;
  3950. if (wb == NULL)
  3951. return ((char **) NULL);
  3952. if (wb->w_nword == 0) {
  3953. DELETE(wb);
  3954. return ((char **) NULL);
  3955. }
  3956. wd = (char **) space(nb = sizeof(*wd) * wb->w_nword);
  3957. memcpy((char *) wd, (char *) wb->w_words, nb);
  3958. DELETE(wb); /* perhaps should done by caller */
  3959. return (wd);
  3960. }
  3961. int (*func) (char *, char *);
  3962. int globv;
  3963. static void glob0(a0, a1, a2, a3)
  3964. char *a0;
  3965. unsigned a1;
  3966. int a2;
  3967. int (*a3) (char *, char *);
  3968. {
  3969. func = a3;
  3970. globv = a2;
  3971. glob1(a0, a0 + a1 * a2);
  3972. }
  3973. static void glob1(base, lim)
  3974. char *base, *lim;
  3975. {
  3976. REGISTER char *i, *j;
  3977. int v2;
  3978. char *lptr, *hptr;
  3979. int c;
  3980. unsigned n;
  3981. v2 = globv;
  3982. top:
  3983. if ((n = (int) (lim - base)) <= v2)
  3984. return;
  3985. n = v2 * (n / (2 * v2));
  3986. hptr = lptr = base + n;
  3987. i = base;
  3988. j = lim - v2;
  3989. for (;;) {
  3990. if (i < lptr) {
  3991. if ((c = (*func) (i, lptr)) == 0) {
  3992. glob2(i, lptr -= v2);
  3993. continue;
  3994. }
  3995. if (c < 0) {
  3996. i += v2;
  3997. continue;
  3998. }
  3999. }
  4000. begin:
  4001. if (j > hptr) {
  4002. if ((c = (*func) (hptr, j)) == 0) {
  4003. glob2(hptr += v2, j);
  4004. goto begin;
  4005. }
  4006. if (c > 0) {
  4007. if (i == lptr) {
  4008. glob3(i, hptr += v2, j);
  4009. i = lptr += v2;
  4010. goto begin;
  4011. }
  4012. glob2(i, j);
  4013. j -= v2;
  4014. i += v2;
  4015. continue;
  4016. }
  4017. j -= v2;
  4018. goto begin;
  4019. }
  4020. if (i == lptr) {
  4021. if (lptr - base >= lim - hptr) {
  4022. glob1(hptr + v2, lim);
  4023. lim = lptr;
  4024. } else {
  4025. glob1(base, lptr);
  4026. base = hptr + v2;
  4027. }
  4028. goto top;
  4029. }
  4030. glob3(j, lptr -= v2, i);
  4031. j = hptr -= v2;
  4032. }
  4033. }
  4034. static void glob2(i, j)
  4035. char *i, *j;
  4036. {
  4037. REGISTER char *index1, *index2, c;
  4038. int m;
  4039. m = globv;
  4040. index1 = i;
  4041. index2 = j;
  4042. do {
  4043. c = *index1;
  4044. *index1++ = *index2;
  4045. *index2++ = c;
  4046. } while (--m);
  4047. }
  4048. static void glob3(i, j, k)
  4049. char *i, *j, *k;
  4050. {
  4051. REGISTER char *index1, *index2, *index3;
  4052. int c;
  4053. int m;
  4054. m = globv;
  4055. index1 = i;
  4056. index2 = j;
  4057. index3 = k;
  4058. do {
  4059. c = *index1;
  4060. *index1++ = *index3;
  4061. *index3++ = *index2;
  4062. *index2++ = c;
  4063. } while (--m);
  4064. }
  4065. /* -------- io.c -------- */
  4066. /*
  4067. * shell IO
  4068. */
  4069. static int my_getc(int ec)
  4070. {
  4071. REGISTER int c;
  4072. if (e.linep > elinep) {
  4073. while ((c = readc()) != '\n' && c);
  4074. err("input line too long");
  4075. gflg++;
  4076. return (c);
  4077. }
  4078. c = readc();
  4079. if ((ec != '\'') && (ec != '`') && (e.iop->task != XGRAVE)) {
  4080. if (c == '\\') {
  4081. c = readc();
  4082. if (c == '\n' && ec != '\"')
  4083. return (my_getc(ec));
  4084. c |= QUOTE;
  4085. }
  4086. }
  4087. return (c);
  4088. }
  4089. static void unget(c)
  4090. int c;
  4091. {
  4092. if (e.iop >= e.iobase)
  4093. e.iop->peekc = c;
  4094. }
  4095. static int eofc()
  4096. {
  4097. return e.iop < e.iobase || (e.iop->peekc == 0 && e.iop->prev == 0);
  4098. }
  4099. static int readc()
  4100. {
  4101. REGISTER int c;
  4102. RCPRINTF(("READC: e.iop 0x%x, e.iobase 0x%x\n", e.iop, e.iobase));
  4103. for (; e.iop >= e.iobase; e.iop--) {
  4104. RCPRINTF(("READC: e.iop 0x%x, peekc 0x%x\n", e.iop, e.iop->peekc));
  4105. if ((c = e.iop->peekc) != '\0') {
  4106. e.iop->peekc = 0;
  4107. return (c);
  4108. } else {
  4109. if (e.iop->prev != 0) {
  4110. if ((c = (*e.iop->iofn) (e.iop->argp, e.iop)) != '\0') {
  4111. if (c == -1) {
  4112. e.iop++;
  4113. continue;
  4114. }
  4115. if (e.iop == iostack)
  4116. ioecho(c);
  4117. return (e.iop->prev = c);
  4118. } else if (e.iop->task == XIO && e.iop->prev != '\n') {
  4119. e.iop->prev = 0;
  4120. if (e.iop == iostack)
  4121. ioecho('\n');
  4122. return '\n';
  4123. }
  4124. }
  4125. if (e.iop->task == XIO) {
  4126. if (multiline) {
  4127. return e.iop->prev = 0;
  4128. }
  4129. if (interactive && e.iop == iostack + 1) {
  4130. #ifdef CONFIG_FEATURE_COMMAND_EDITING
  4131. current_prompt = prompt->value;
  4132. #else
  4133. prs(prompt->value);
  4134. #endif
  4135. }
  4136. }
  4137. }
  4138. } /* FOR */
  4139. if (e.iop >= iostack) {
  4140. RCPRINTF(("READC: return 0, e.iop 0x%x\n", e.iop));
  4141. return (0);
  4142. }
  4143. DBGPRINTF(("READC: leave()...\n"));
  4144. leave();
  4145. /* NOTREACHED */
  4146. return (0);
  4147. }
  4148. static void ioecho(c)
  4149. char c;
  4150. {
  4151. if (flag['v'])
  4152. write(2, &c, sizeof c);
  4153. }
  4154. static void pushio(struct ioarg *argp, int (*fn) (struct ioarg *))
  4155. {
  4156. DBGPRINTF(("PUSHIO: argp 0x%x, argp->afid 0x%x, e.iop 0x%x\n", argp,
  4157. argp->afid, e.iop));
  4158. /* Set env ptr for io source to next array spot and check for array overflow */
  4159. if (++e.iop >= &iostack[NPUSH]) {
  4160. e.iop--;
  4161. err("Shell input nested too deeply");
  4162. gflg++;
  4163. return;
  4164. }
  4165. /* We did not overflow the NPUSH array spots so setup data structs */
  4166. e.iop->iofn = (int (*)(struct ioarg *, struct io *)) fn; /* Store data source func ptr */
  4167. if (argp->afid != AFID_NOBUF)
  4168. e.iop->argp = argp;
  4169. else {
  4170. e.iop->argp = ioargstack + (e.iop - iostack); /* MAL - index into stack */
  4171. *e.iop->argp = *argp; /* copy data from temp area into stack spot */
  4172. /* MAL - mainbuf is for 1st data source (command line?) and all nested use a single shared buffer? */
  4173. if (e.iop == &iostack[0])
  4174. e.iop->argp->afbuf = &mainbuf;
  4175. else
  4176. e.iop->argp->afbuf = &sharedbuf;
  4177. /* MAL - if not a termimal AND (commandline OR readable file) then give it a buffer id? */
  4178. /* This line appears to be active when running scripts from command line */
  4179. if ((isatty(e.iop->argp->afile) == 0)
  4180. && (e.iop == &iostack[0]
  4181. || lseek(e.iop->argp->afile, 0L, 1) != -1)) {
  4182. if (++bufid == AFID_NOBUF) /* counter rollover check, AFID_NOBUF = 11111111 */
  4183. bufid = AFID_ID; /* AFID_ID = 0 */
  4184. e.iop->argp->afid = bufid; /* assign buffer id */
  4185. }
  4186. DBGPRINTF(("PUSHIO: iostack 0x%x, e.iop 0x%x, afbuf 0x%x\n",
  4187. iostack, e.iop, e.iop->argp->afbuf));
  4188. DBGPRINTF(("PUSHIO: mbuf 0x%x, sbuf 0x%x, bid %d, e.iop 0x%x\n",
  4189. &mainbuf, &sharedbuf, bufid, e.iop));
  4190. }
  4191. e.iop->prev = ~'\n';
  4192. e.iop->peekc = 0;
  4193. e.iop->xchar = 0;
  4194. e.iop->nlcount = 0;
  4195. if (fn == filechar || fn == linechar)
  4196. e.iop->task = XIO;
  4197. else if (fn == (int (*)(struct ioarg *)) gravechar
  4198. || fn == (int (*)(struct ioarg *)) qgravechar)
  4199. e.iop->task = XGRAVE;
  4200. else
  4201. e.iop->task = XOTHER;
  4202. return;
  4203. }
  4204. static struct io *setbase(ip)
  4205. struct io *ip;
  4206. {
  4207. REGISTER struct io *xp;
  4208. xp = e.iobase;
  4209. e.iobase = ip;
  4210. return (xp);
  4211. }
  4212. /*
  4213. * Input generating functions
  4214. */
  4215. /*
  4216. * Produce the characters of a string, then a newline, then EOF.
  4217. */
  4218. static int nlchar(ap)
  4219. REGISTER struct ioarg *ap;
  4220. {
  4221. REGISTER int c;
  4222. if (ap->aword == NULL)
  4223. return (0);
  4224. if ((c = *ap->aword++) == 0) {
  4225. ap->aword = NULL;
  4226. return ('\n');
  4227. }
  4228. return (c);
  4229. }
  4230. /*
  4231. * Given a list of words, produce the characters
  4232. * in them, with a space after each word.
  4233. */
  4234. static int wdchar(ap)
  4235. REGISTER struct ioarg *ap;
  4236. {
  4237. REGISTER char c;
  4238. REGISTER char **wl;
  4239. if ((wl = ap->awordlist) == NULL)
  4240. return (0);
  4241. if (*wl != NULL) {
  4242. if ((c = *(*wl)++) != 0)
  4243. return (c & 0177);
  4244. ap->awordlist++;
  4245. return (' ');
  4246. }
  4247. ap->awordlist = NULL;
  4248. return ('\n');
  4249. }
  4250. /*
  4251. * Return the characters of a list of words,
  4252. * producing a space between them.
  4253. */
  4254. static int dolchar(ap)
  4255. REGISTER struct ioarg *ap;
  4256. {
  4257. REGISTER char *wp;
  4258. if ((wp = *ap->awordlist++) != NULL) {
  4259. PUSHIO(aword, wp, *ap->awordlist == NULL ? strchar : xxchar);
  4260. return (-1);
  4261. }
  4262. return (0);
  4263. }
  4264. static int xxchar(ap)
  4265. REGISTER struct ioarg *ap;
  4266. {
  4267. REGISTER int c;
  4268. if (ap->aword == NULL)
  4269. return (0);
  4270. if ((c = *ap->aword++) == '\0') {
  4271. ap->aword = NULL;
  4272. return (' ');
  4273. }
  4274. return (c);
  4275. }
  4276. /*
  4277. * Produce the characters from a single word (string).
  4278. */
  4279. static int strchar(ap)
  4280. REGISTER struct ioarg *ap;
  4281. {
  4282. REGISTER int c;
  4283. if (ap->aword == NULL || (c = *ap->aword++) == 0)
  4284. return (0);
  4285. return (c);
  4286. }
  4287. /*
  4288. * Produce quoted characters from a single word (string).
  4289. */
  4290. static int qstrchar(ap)
  4291. REGISTER struct ioarg *ap;
  4292. {
  4293. REGISTER int c;
  4294. if (ap->aword == NULL || (c = *ap->aword++) == 0)
  4295. return (0);
  4296. return (c | QUOTE);
  4297. }
  4298. /*
  4299. * Return the characters from a file.
  4300. */
  4301. static int filechar(ap)
  4302. REGISTER struct ioarg *ap;
  4303. {
  4304. REGISTER int i;
  4305. char c;
  4306. struct iobuf *bp = ap->afbuf;
  4307. if (ap->afid != AFID_NOBUF) {
  4308. if ((i = ap->afid != bp->id) || bp->bufp == bp->ebufp) {
  4309. if (i)
  4310. lseek(ap->afile, ap->afpos, 0);
  4311. i = safe_read(ap->afile, bp->buf, sizeof(bp->buf));
  4312. if (i <= 0) {
  4313. closef(ap->afile);
  4314. return 0;
  4315. }
  4316. bp->id = ap->afid;
  4317. bp->ebufp = (bp->bufp = bp->buf) + i;
  4318. }
  4319. ap->afpos++;
  4320. return *bp->bufp++ & 0177;
  4321. }
  4322. #ifdef CONFIG_FEATURE_COMMAND_EDITING
  4323. if (interactive && isatty(ap->afile)) {
  4324. static char mycommand[BUFSIZ];
  4325. static int position = 0, size = 0;
  4326. while (size == 0 || position >= size) {
  4327. cmdedit_read_input(current_prompt, mycommand);
  4328. size = strlen(mycommand);
  4329. position = 0;
  4330. }
  4331. c = mycommand[position];
  4332. position++;
  4333. return (c);
  4334. } else
  4335. #endif
  4336. {
  4337. i = safe_read(ap->afile, &c, sizeof(c));
  4338. return (i == sizeof(c) ? (c & 0x7f) : (closef(ap->afile), 0));
  4339. }
  4340. }
  4341. /*
  4342. * Return the characters from a here temp file.
  4343. */
  4344. static int herechar(ap)
  4345. REGISTER struct ioarg *ap;
  4346. {
  4347. char c;
  4348. if (read(ap->afile, &c, sizeof(c)) != sizeof(c)) {
  4349. close(ap->afile);
  4350. c = 0;
  4351. }
  4352. return (c);
  4353. }
  4354. /*
  4355. * Return the characters produced by a process (`...`).
  4356. * Quote them if required, and remove any trailing newline characters.
  4357. */
  4358. static int gravechar(ap, iop)
  4359. struct ioarg *ap;
  4360. struct io *iop;
  4361. {
  4362. REGISTER int c;
  4363. if ((c = qgravechar(ap, iop) & ~QUOTE) == '\n')
  4364. c = ' ';
  4365. return (c);
  4366. }
  4367. static int qgravechar(ap, iop)
  4368. REGISTER struct ioarg *ap;
  4369. struct io *iop;
  4370. {
  4371. REGISTER int c;
  4372. DBGPRINTF3(("QGRAVECHAR: enter, ap=0x%x, iop=0x%x\n", ap, iop));
  4373. if (iop->xchar) {
  4374. if (iop->nlcount) {
  4375. iop->nlcount--;
  4376. return ('\n' | QUOTE);
  4377. }
  4378. c = iop->xchar;
  4379. iop->xchar = 0;
  4380. } else if ((c = filechar(ap)) == '\n') {
  4381. iop->nlcount = 1;
  4382. while ((c = filechar(ap)) == '\n')
  4383. iop->nlcount++;
  4384. iop->xchar = c;
  4385. if (c == 0)
  4386. return (c);
  4387. iop->nlcount--;
  4388. c = '\n';
  4389. }
  4390. return (c != 0 ? c | QUOTE : 0);
  4391. }
  4392. /*
  4393. * Return a single command (usually the first line) from a file.
  4394. */
  4395. static int linechar(ap)
  4396. REGISTER struct ioarg *ap;
  4397. {
  4398. REGISTER int c;
  4399. if ((c = filechar(ap)) == '\n') {
  4400. if (!multiline) {
  4401. closef(ap->afile);
  4402. ap->afile = -1; /* illegal value */
  4403. }
  4404. }
  4405. return (c);
  4406. }
  4407. static void prs(s)
  4408. REGISTER char *s;
  4409. {
  4410. if (*s)
  4411. write(2, s, strlen(s));
  4412. }
  4413. static void prn(u)
  4414. unsigned u;
  4415. {
  4416. prs(itoa(u));
  4417. }
  4418. static void closef(i)
  4419. REGISTER int i;
  4420. {
  4421. if (i > 2)
  4422. close(i);
  4423. }
  4424. static void closeall()
  4425. {
  4426. REGISTER int u;
  4427. for (u = NUFILE; u < NOFILE;)
  4428. close(u++);
  4429. }
  4430. /*
  4431. * remap fd into Shell's fd space
  4432. */
  4433. static int remap(fd)
  4434. REGISTER int fd;
  4435. {
  4436. REGISTER int i;
  4437. int map[NOFILE];
  4438. int newfd;
  4439. DBGPRINTF(("REMAP: fd=%d, e.iofd=%d\n", fd, e.iofd));
  4440. if (fd < e.iofd) {
  4441. for (i = 0; i < NOFILE; i++)
  4442. map[i] = 0;
  4443. do {
  4444. map[fd] = 1;
  4445. newfd = dup(fd);
  4446. fd = newfd;
  4447. } while (fd >= 0 && fd < e.iofd);
  4448. for (i = 0; i < NOFILE; i++)
  4449. if (map[i])
  4450. close(i);
  4451. if (fd < 0)
  4452. err("too many files open in shell");
  4453. }
  4454. return (fd);
  4455. }
  4456. static int openpipe(pv)
  4457. REGISTER int *pv;
  4458. {
  4459. REGISTER int i;
  4460. if ((i = pipe(pv)) < 0)
  4461. err("can't create pipe - try again");
  4462. return (i);
  4463. }
  4464. static void closepipe(pv)
  4465. REGISTER int *pv;
  4466. {
  4467. if (pv != NULL) {
  4468. close(*pv++);
  4469. close(*pv);
  4470. }
  4471. }
  4472. /* -------- here.c -------- */
  4473. /*
  4474. * here documents
  4475. */
  4476. static void markhere(s, iop)
  4477. REGISTER char *s;
  4478. struct ioword *iop;
  4479. {
  4480. REGISTER struct here *h, *lh;
  4481. DBGPRINTF7(("MARKHERE: enter, s=0x%x\n", s));
  4482. h = (struct here *) space(sizeof(struct here));
  4483. if (h == 0)
  4484. return;
  4485. h->h_tag = evalstr(s, DOSUB);
  4486. if (h->h_tag == 0)
  4487. return;
  4488. h->h_iop = iop;
  4489. iop->io_name = 0;
  4490. h->h_next = NULL;
  4491. if (inhere == 0)
  4492. inhere = h;
  4493. else
  4494. for (lh = inhere; lh != NULL; lh = lh->h_next)
  4495. if (lh->h_next == 0) {
  4496. lh->h_next = h;
  4497. break;
  4498. }
  4499. iop->io_flag |= IOHERE | IOXHERE;
  4500. for (s = h->h_tag; *s; s++)
  4501. if (*s & QUOTE) {
  4502. iop->io_flag &= ~IOXHERE;
  4503. *s &= ~QUOTE;
  4504. }
  4505. h->h_dosub = iop->io_flag & IOXHERE;
  4506. }
  4507. static void gethere()
  4508. {
  4509. REGISTER struct here *h, *hp;
  4510. DBGPRINTF7(("GETHERE: enter...\n"));
  4511. /* Scan here files first leaving inhere list in place */
  4512. for (hp = h = inhere; h != NULL; hp = h, h = h->h_next)
  4513. readhere(&h->h_iop->io_name, h->h_tag, h->h_dosub ? 0 : '\'');
  4514. /* Make inhere list active - keep list intact for scraphere */
  4515. if (hp != NULL) {
  4516. hp->h_next = acthere;
  4517. acthere = inhere;
  4518. inhere = NULL;
  4519. }
  4520. }
  4521. static void readhere(name, s, ec)
  4522. char **name;
  4523. REGISTER char *s;
  4524. int ec;
  4525. {
  4526. int tf;
  4527. char tname[30] = ".msh_XXXXXX";
  4528. REGISTER int c;
  4529. jmp_buf ev;
  4530. char myline[LINELIM + 1];
  4531. char *thenext;
  4532. DBGPRINTF7(("READHERE: enter, name=0x%x, s=0x%x\n", name, s));
  4533. tf = mkstemp(tname);
  4534. if (tf < 0)
  4535. return;
  4536. *name = strsave(tname, areanum);
  4537. if (newenv(setjmp(errpt = ev)) != 0)
  4538. unlink(tname);
  4539. else {
  4540. pushio(e.iop->argp, (int (*)(struct ioarg *)) e.iop->iofn);
  4541. e.iobase = e.iop;
  4542. for (;;) {
  4543. if (interactive && e.iop <= iostack) {
  4544. #ifdef CONFIG_FEATURE_COMMAND_EDITING
  4545. current_prompt = cprompt->value;
  4546. #else
  4547. prs(cprompt->value);
  4548. #endif
  4549. }
  4550. thenext = myline;
  4551. while ((c = my_getc(ec)) != '\n' && c) {
  4552. if (ec == '\'')
  4553. c &= ~QUOTE;
  4554. if (thenext >= &myline[LINELIM]) {
  4555. c = 0;
  4556. break;
  4557. }
  4558. *thenext++ = c;
  4559. }
  4560. *thenext = 0;
  4561. if (strcmp(s, myline) == 0 || c == 0)
  4562. break;
  4563. *thenext++ = '\n';
  4564. write(tf, myline, (int) (thenext - myline));
  4565. }
  4566. if (c == 0) {
  4567. prs("here document `");
  4568. prs(s);
  4569. err("' unclosed");
  4570. }
  4571. quitenv();
  4572. }
  4573. close(tf);
  4574. }
  4575. /*
  4576. * open here temp file.
  4577. * if unquoted here, expand here temp file into second temp file.
  4578. */
  4579. static int herein(hname, xdoll)
  4580. char *hname;
  4581. int xdoll;
  4582. {
  4583. REGISTER int hf;
  4584. int tf;
  4585. #if __GNUC__
  4586. /* Avoid longjmp clobbering */
  4587. (void) &tf;
  4588. #endif
  4589. if (hname == NULL)
  4590. return (-1);
  4591. DBGPRINTF7(("HEREIN: hname is %s, xdoll=%d\n", hname, xdoll));
  4592. hf = open(hname, 0);
  4593. if (hf < 0)
  4594. return (-1);
  4595. if (xdoll) {
  4596. char c;
  4597. char tname[30] = ".msh_XXXXXX";
  4598. jmp_buf ev;
  4599. tf = mkstemp(tname);
  4600. if (tf < 0)
  4601. return (-1);
  4602. if (newenv(setjmp(errpt = ev)) == 0) {
  4603. PUSHIO(afile, hf, herechar);
  4604. setbase(e.iop);
  4605. while ((c = subgetc(0, 0)) != 0) {
  4606. c &= ~QUOTE;
  4607. write(tf, &c, sizeof c);
  4608. }
  4609. quitenv();
  4610. } else
  4611. unlink(tname);
  4612. close(tf);
  4613. tf = open(tname, 0);
  4614. unlink(tname);
  4615. return (tf);
  4616. } else
  4617. return (hf);
  4618. }
  4619. static void scraphere()
  4620. {
  4621. REGISTER struct here *h;
  4622. DBGPRINTF7(("SCRAPHERE: enter...\n"));
  4623. for (h = inhere; h != NULL; h = h->h_next) {
  4624. if (h->h_iop && h->h_iop->io_name)
  4625. unlink(h->h_iop->io_name);
  4626. }
  4627. inhere = NULL;
  4628. }
  4629. /* unlink here temp files before a freearea(area) */
  4630. static void freehere(area)
  4631. int area;
  4632. {
  4633. REGISTER struct here *h, *hl;
  4634. DBGPRINTF6(("FREEHERE: enter, area=%d\n", area));
  4635. hl = NULL;
  4636. for (h = acthere; h != NULL; h = h->h_next)
  4637. if (getarea((char *) h) >= area) {
  4638. if (h->h_iop->io_name != NULL)
  4639. unlink(h->h_iop->io_name);
  4640. if (hl == NULL)
  4641. acthere = h->h_next;
  4642. else
  4643. hl->h_next = h->h_next;
  4644. } else
  4645. hl = h;
  4646. }
  4647. /*
  4648. * Copyright (c) 1987,1997, Prentice Hall
  4649. * All rights reserved.
  4650. *
  4651. * Redistribution and use of the MINIX operating system in source and
  4652. * binary forms, with or without modification, are permitted provided
  4653. * that the following conditions are met:
  4654. *
  4655. * Redistributions of source code must retain the above copyright
  4656. * notice, this list of conditions and the following disclaimer.
  4657. *
  4658. * Redistributions in binary form must reproduce the above
  4659. * copyright notice, this list of conditions and the following
  4660. * disclaimer in the documentation and/or other materials provided
  4661. * with the distribution.
  4662. *
  4663. * Neither the name of Prentice Hall nor the names of the software
  4664. * authors or contributors may be used to endorse or promote
  4665. * products derived from this software without specific prior
  4666. * written permission.
  4667. *
  4668. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
  4669. * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  4670. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  4671. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  4672. * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
  4673. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  4674. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  4675. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  4676. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  4677. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  4678. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  4679. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  4680. *
  4681. */