3
0

msh.c 102 KB

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