3
0

msh.c 102 KB

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