3
0

msh.c 99 KB

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