msh.c 97 KB

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