msh.c 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335
  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 bb_banner "busybox standalone"
  37. # define ENABLE_FEATURE_SH_STANDALONE 0
  38. # define bb_msg_memory_exhausted "memory exhausted"
  39. # define xmalloc(size) malloc(size)
  40. # define msh_main(argc,argv) main(argc,argv)
  41. # define safe_read(fd,buf,count) read(fd,buf,count)
  42. # define nonblock_safe_read(fd,buf,count) read(fd,buf,count)
  43. # define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1])
  44. # define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
  45. # define NORETURN __attribute__ ((__noreturn__))
  46. static int find_applet_by_name(const char *applet)
  47. {
  48. return -1;
  49. }
  50. static char *utoa_to_buf(unsigned n, char *buf, unsigned buflen)
  51. {
  52. unsigned i, out, res;
  53. assert(sizeof(unsigned) == 4);
  54. if (buflen) {
  55. out = 0;
  56. for (i = 1000000000; i; i /= 10) {
  57. res = n / i;
  58. if (res || out || i == 1) {
  59. if (!--buflen) break;
  60. out++;
  61. n -= res*i;
  62. *buf++ = '0' + res;
  63. }
  64. }
  65. }
  66. return buf;
  67. }
  68. static char *itoa_to_buf(int n, char *buf, unsigned buflen)
  69. {
  70. if (buflen && n < 0) {
  71. n = -n;
  72. *buf++ = '-';
  73. buflen--;
  74. }
  75. return utoa_to_buf((unsigned)n, buf, buflen);
  76. }
  77. static char local_buf[12];
  78. static char *itoa(int n)
  79. {
  80. *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
  81. return local_buf;
  82. }
  83. #else
  84. # include "busybox.h" /* for applet_names */
  85. #endif
  86. //#define MSHDEBUG 4
  87. #ifdef MSHDEBUG
  88. static int mshdbg = MSHDEBUG;
  89. #define DBGPRINTF(x) if (mshdbg > 0) printf x
  90. #define DBGPRINTF0(x) if (mshdbg > 0) printf x
  91. #define DBGPRINTF1(x) if (mshdbg > 1) printf x
  92. #define DBGPRINTF2(x) if (mshdbg > 2) printf x
  93. #define DBGPRINTF3(x) if (mshdbg > 3) printf x
  94. #define DBGPRINTF4(x) if (mshdbg > 4) printf x
  95. #define DBGPRINTF5(x) if (mshdbg > 5) printf x
  96. #define DBGPRINTF6(x) if (mshdbg > 6) printf x
  97. #define DBGPRINTF7(x) if (mshdbg > 7) printf x
  98. #define DBGPRINTF8(x) if (mshdbg > 8) printf x
  99. #define DBGPRINTF9(x) if (mshdbg > 9) printf x
  100. static int mshdbg_rc = 0;
  101. #define RCPRINTF(x) if (mshdbg_rc) printf x
  102. #else
  103. #define DBGPRINTF(x)
  104. #define DBGPRINTF0(x) ((void)0)
  105. #define DBGPRINTF1(x) ((void)0)
  106. #define DBGPRINTF2(x) ((void)0)
  107. #define DBGPRINTF3(x) ((void)0)
  108. #define DBGPRINTF4(x) ((void)0)
  109. #define DBGPRINTF5(x) ((void)0)
  110. #define DBGPRINTF6(x) ((void)0)
  111. #define DBGPRINTF7(x) ((void)0)
  112. #define DBGPRINTF8(x) ((void)0)
  113. #define DBGPRINTF9(x) ((void)0)
  114. #define RCPRINTF(x) ((void)0)
  115. #endif /* MSHDEBUG */
  116. #if ENABLE_FEATURE_EDITING_FANCY_PROMPT
  117. # define DEFAULT_ROOT_PROMPT "\\u:\\w> "
  118. # define DEFAULT_USER_PROMPT "\\u:\\w$ "
  119. #else
  120. # define DEFAULT_ROOT_PROMPT "# "
  121. # define DEFAULT_USER_PROMPT "$ "
  122. #endif
  123. /* -------- sh.h -------- */
  124. /*
  125. * shell
  126. */
  127. #define LINELIM 2100
  128. #define NPUSH 8 /* limit to input nesting */
  129. #undef NOFILE
  130. #define NOFILE 20 /* Number of open files */
  131. #define NUFILE 10 /* Number of user-accessible files */
  132. #define FDBASE 10 /* First file usable by Shell */
  133. /*
  134. * values returned by wait
  135. */
  136. #define WAITSIG(s) ((s) & 0177)
  137. #define WAITVAL(s) (((s) >> 8) & 0377)
  138. #define WAITCORE(s) (((s) & 0200) != 0)
  139. /*
  140. * library and system definitions
  141. */
  142. typedef void xint; /* base type of jmp_buf, for not broken compilers */
  143. /*
  144. * shell components
  145. */
  146. #define NOBLOCK ((struct op *)NULL)
  147. #define NOWORD ((char *)NULL)
  148. #define NOWORDS ((char **)NULL)
  149. #define NOPIPE ((int *)NULL)
  150. /*
  151. * redirection
  152. */
  153. struct ioword {
  154. smallint io_flag; /* action (below) */
  155. int io_fd; /* fd affected */
  156. char *io_name; /* file name */
  157. };
  158. #define IOREAD 1 /* < */
  159. #define IOHERE 2 /* << (here file) */
  160. #define IOWRITE 4 /* > */
  161. #define IOCAT 8 /* >> */
  162. #define IOXHERE 16 /* ${}, ` in << */
  163. #define IODUP 32 /* >&digit */
  164. #define IOCLOSE 64 /* >&- */
  165. #define IODEFAULT (-1) /* "default" IO fd */
  166. /*
  167. * Description of a command or an operation on commands.
  168. * Might eventually use a union.
  169. */
  170. struct op {
  171. smallint op_type; /* operation type, see Txxxx below */
  172. char **op_words; /* arguments to a command */
  173. struct ioword **ioact; /* IO actions (eg, < > >>) */
  174. struct op *left;
  175. struct op *right;
  176. char *str; /* identifier for case and for */
  177. };
  178. #define TCOM 1 /* command */
  179. #define TPAREN 2 /* (c-list) */
  180. #define TPIPE 3 /* a | b */
  181. #define TLIST 4 /* a [&;] b */
  182. #define TOR 5 /* || */
  183. #define TAND 6 /* && */
  184. #define TFOR 7
  185. #define TDO 8
  186. #define TCASE 9
  187. #define TIF 10
  188. #define TWHILE 11
  189. #define TUNTIL 12
  190. #define TELIF 13
  191. #define TPAT 14 /* pattern in case */
  192. #define TBRACE 15 /* {c-list} */
  193. #define TASYNC 16 /* c & */
  194. /* Added to support "." file expansion */
  195. #define TDOT 17
  196. /* Strings for names to make debug easier */
  197. #ifdef MSHDEBUG
  198. static const char *const T_CMD_NAMES[] = {
  199. "PLACEHOLDER",
  200. "TCOM",
  201. "TPAREN",
  202. "TPIPE",
  203. "TLIST",
  204. "TOR",
  205. "TAND",
  206. "TFOR",
  207. "TDO",
  208. "TCASE",
  209. "TIF",
  210. "TWHILE",
  211. "TUNTIL",
  212. "TELIF",
  213. "TPAT",
  214. "TBRACE",
  215. "TASYNC",
  216. "TDOT",
  217. };
  218. #endif
  219. #define AREASIZE (90000)
  220. /*
  221. * flags to control evaluation of words
  222. */
  223. #define DOSUB 1 /* interpret $, `, and quotes */
  224. #define DOBLANK 2 /* perform blank interpretation */
  225. #define DOGLOB 4 /* interpret [?* */
  226. #define DOKEY 8 /* move words with `=' to 2nd arg. list */
  227. #define DOTRIM 16 /* trim resulting string */
  228. #define DOALL (DOSUB|DOBLANK|DOGLOB|DOKEY|DOTRIM)
  229. struct brkcon {
  230. jmp_buf brkpt;
  231. struct brkcon *nextlev;
  232. };
  233. static smallint trapset; /* trap pending (signal number) */
  234. static smallint yynerrs; /* yacc (flag) */
  235. /* moved to G: static char line[LINELIM]; */
  236. #if ENABLE_FEATURE_EDITING
  237. static char *current_prompt;
  238. static line_input_t *line_input_state;
  239. #endif
  240. /*
  241. * other functions
  242. */
  243. static const char *rexecve(char *c, char **v, char **envp);
  244. static char *evalstr(char *cp, int f);
  245. static char *putn(int n);
  246. static char *unquote(char *as);
  247. static int rlookup(char *n);
  248. static struct wdblock *glob(char *cp, struct wdblock *wb);
  249. static int my_getc(int ec);
  250. static int subgetc(char ec, int quoted);
  251. static char **makenv(int all, struct wdblock *wb);
  252. static char **eval(char **ap, int f);
  253. static int setstatus(int s);
  254. static int waitfor(int lastpid, int canintr);
  255. static void onintr(int s); /* SIGINT handler */
  256. static int newenv(int f);
  257. static void quitenv(void);
  258. static void next(int f);
  259. static void setdash(void);
  260. static void onecommand(void);
  261. static void runtrap(int i);
  262. /* -------- area stuff -------- */
  263. #define REGSIZE sizeof(struct region)
  264. #define GROWBY (256)
  265. /* #define SHRINKBY (64) */
  266. #undef SHRINKBY
  267. #define FREE (32767)
  268. #define BUSY (0)
  269. #define ALIGN (sizeof(int)-1)
  270. struct region {
  271. struct region *next;
  272. int area;
  273. };
  274. /* -------- grammar stuff -------- */
  275. typedef union {
  276. char *cp;
  277. char **wp;
  278. int i;
  279. struct op *o;
  280. } YYSTYPE;
  281. #define WORD 256
  282. #define LOGAND 257
  283. #define LOGOR 258
  284. #define BREAK 259
  285. #define IF 260
  286. #define THEN 261
  287. #define ELSE 262
  288. #define ELIF 263
  289. #define FI 264
  290. #define CASE 265
  291. #define ESAC 266
  292. #define FOR 267
  293. #define WHILE 268
  294. #define UNTIL 269
  295. #define DO 270
  296. #define DONE 271
  297. #define IN 272
  298. /* Added for "." file expansion */
  299. #define DOT 273
  300. #define YYERRCODE 300
  301. /* flags to yylex */
  302. #define CONTIN 01 /* skip new lines to complete command */
  303. static struct op *pipeline(int cf);
  304. static struct op *andor(void);
  305. static struct op *c_list(void);
  306. static int synio(int cf);
  307. static void musthave(int c, int cf);
  308. static struct op *simple(void);
  309. static struct op *nested(int type, int mark);
  310. static struct op *command(int cf);
  311. static struct op *dogroup(int onlydone);
  312. static struct op *thenpart(void);
  313. static struct op *elsepart(void);
  314. static struct op *caselist(void);
  315. static struct op *casepart(void);
  316. static char **pattern(void);
  317. static char **wordlist(void);
  318. static struct op *list(struct op *t1, struct op *t2);
  319. static struct op *block(int type, struct op *t1, struct op *t2, char **wp);
  320. static struct op *newtp(void);
  321. static struct op *namelist(struct op *t);
  322. static char **copyw(void);
  323. static void word(char *cp);
  324. static struct ioword **copyio(void);
  325. static struct ioword *io(int u, int f, char *cp);
  326. static int yylex(int cf);
  327. static int collect(int c, int c1);
  328. static int dual(int c);
  329. static void diag(int ec);
  330. static char *tree(unsigned size);
  331. /* -------- var.h -------- */
  332. struct var {
  333. char *value;
  334. char *name;
  335. struct var *next;
  336. char status;
  337. };
  338. #define COPYV 1 /* flag to setval, suggesting copy */
  339. #define RONLY 01 /* variable is read-only */
  340. #define EXPORT 02 /* variable is to be exported */
  341. #define GETCELL 04 /* name & value space was got with getcell */
  342. static int yyparse(void);
  343. /* -------- io.h -------- */
  344. /* io buffer */
  345. struct iobuf {
  346. unsigned id; /* buffer id */
  347. char buf[512]; /* buffer */
  348. char *bufp; /* pointer into buffer */
  349. char *ebufp; /* pointer to end of buffer */
  350. };
  351. /* possible arguments to an IO function */
  352. struct ioarg {
  353. const char *aword;
  354. char **awordlist;
  355. int afile; /* file descriptor */
  356. unsigned afid; /* buffer id */
  357. off_t afpos; /* file position */
  358. struct iobuf *afbuf; /* buffer for this file */
  359. };
  360. /* an input generator's state */
  361. struct io {
  362. int (*iofn) (struct ioarg *, struct io *);
  363. struct ioarg *argp;
  364. int peekc;
  365. char prev; /* previous character read by readc() */
  366. char nlcount; /* for `'s */
  367. char xchar; /* for `'s */
  368. char task; /* reason for pushed IO */
  369. };
  370. /* ->task: */
  371. #define XOTHER 0 /* none of the below */
  372. #define XDOLL 1 /* expanding ${} */
  373. #define XGRAVE 2 /* expanding `'s */
  374. #define XIO 3 /* file IO */
  375. /*
  376. * input generators for IO structure
  377. */
  378. static int nlchar(struct ioarg *ap);
  379. static int strchar(struct ioarg *ap);
  380. static int qstrchar(struct ioarg *ap);
  381. static int filechar(struct ioarg *ap);
  382. static int herechar(struct ioarg *ap);
  383. static int linechar(struct ioarg *ap);
  384. static int gravechar(struct ioarg *ap, struct io *iop);
  385. static int qgravechar(struct ioarg *ap, struct io *iop);
  386. static int dolchar(struct ioarg *ap);
  387. static int wdchar(struct ioarg *ap);
  388. static void scraphere(void);
  389. static void freehere(int area);
  390. static void gethere(void);
  391. static void markhere(char *s, struct ioword *iop);
  392. static int herein(char *hname, int xdoll);
  393. static int run(struct ioarg *argp, int (*f) (struct ioarg *));
  394. static int eofc(void);
  395. static int readc(void);
  396. static void unget(int c);
  397. static void ioecho(char c);
  398. /*
  399. * IO control
  400. */
  401. static void pushio(struct ioarg *argp, int (*f) (struct ioarg *));
  402. #define PUSHIO(what,arg,gen) ((temparg.what = (arg)), pushio(&temparg,(gen)))
  403. static int remap(int fd);
  404. static int openpipe(int *pv);
  405. static void closepipe(int *pv);
  406. static struct io *setbase(struct io *ip);
  407. /* -------- word.h -------- */
  408. #define NSTART 16 /* default number of words to allow for initially */
  409. struct wdblock {
  410. short w_bsize;
  411. short w_nword;
  412. /* bounds are arbitrary */
  413. char *w_words[1];
  414. };
  415. static struct wdblock *addword(char *wd, struct wdblock *wb);
  416. static struct wdblock *newword(int nw);
  417. static char **getwords(struct wdblock *wb);
  418. /* -------- misc stuff -------- */
  419. static int dolabel(struct op *t, char **args);
  420. static int dohelp(struct op *t, char **args);
  421. static int dochdir(struct op *t, char **args);
  422. static int doshift(struct op *t, char **args);
  423. static int dologin(struct op *t, char **args);
  424. static int doumask(struct op *t, char **args);
  425. static int doexec(struct op *t, char **args);
  426. static int dodot(struct op *t, char **args);
  427. static int dowait(struct op *t, char **args);
  428. static int doread(struct op *t, char **args);
  429. static int doeval(struct op *t, char **args);
  430. static int dotrap(struct op *t, char **args);
  431. static int dobreak(struct op *t, char **args);
  432. static int doexit(struct op *t, char **args);
  433. static int doexport(struct op *t, char **args);
  434. static int doreadonly(struct op *t, char **args);
  435. static int doset(struct op *t, char **args);
  436. static int dotimes(struct op *t, char **args);
  437. static int docontinue(struct op *t, char **args);
  438. static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp);
  439. static int execute(struct op *t, int *pin, int *pout, int no_fork);
  440. static int iosetup(struct ioword *iop, int pipein, int pipeout);
  441. static void brkset(struct brkcon *bc);
  442. static int getsig(char *s);
  443. static void setsig(int n, sighandler_t f);
  444. static int getn(char *as);
  445. static int brkcontin(char *cp, int val);
  446. static void rdexp(char **wp, void (*f) (struct var *), int key);
  447. static void badid(char *s);
  448. static void varput(char *s, int out);
  449. static int expand(const char *cp, struct wdblock **wbp, int f);
  450. static char *blank(int f);
  451. static int dollar(int quoted);
  452. static int grave(int quoted);
  453. static void globname(char *we, char *pp);
  454. static char *generate(char *start1, char *end1, char *middle, char *end);
  455. static int anyspcl(struct wdblock *wb);
  456. static void readhere(char **name, char *s, int ec);
  457. static int xxchar(struct ioarg *ap);
  458. struct here {
  459. char *h_tag;
  460. char h_dosub;
  461. struct ioword *h_iop;
  462. struct here *h_next;
  463. };
  464. static const char *const signame[] = {
  465. "Signal 0",
  466. "Hangup",
  467. NULL, /* interrupt */
  468. "Quit",
  469. "Illegal instruction",
  470. "Trace/BPT trap",
  471. "Abort",
  472. "Bus error",
  473. "Floating Point Exception",
  474. "Killed",
  475. "SIGUSR1",
  476. "SIGSEGV",
  477. "SIGUSR2",
  478. NULL, /* broken pipe */
  479. "Alarm clock",
  480. "Terminated"
  481. };
  482. typedef int (*builtin_func_ptr)(struct op *, char **);
  483. struct builtincmd {
  484. const char *name;
  485. builtin_func_ptr builtinfunc;
  486. };
  487. static const struct builtincmd builtincmds[] = {
  488. { "." , dodot },
  489. { ":" , dolabel },
  490. { "break" , dobreak },
  491. { "cd" , dochdir },
  492. { "continue", docontinue },
  493. { "eval" , doeval },
  494. { "exec" , doexec },
  495. { "exit" , doexit },
  496. { "export" , doexport },
  497. { "help" , dohelp },
  498. { "login" , dologin },
  499. { "newgrp" , dologin },
  500. { "read" , doread },
  501. { "readonly", doreadonly },
  502. { "set" , doset },
  503. { "shift" , doshift },
  504. { "times" , dotimes },
  505. { "trap" , dotrap },
  506. { "umask" , doumask },
  507. { "wait" , dowait },
  508. { NULL , NULL },
  509. };
  510. static struct op *dowholefile(int /*, int*/);
  511. /* Globals */
  512. static char **dolv;
  513. static int dolc;
  514. static uint8_t exstat;
  515. static smallint gflg; /* (seems to be a parse error indicator) */
  516. static smallint interactive; /* Is this an interactive shell */
  517. static smallint execflg;
  518. static smallint isbreak; /* "break" statement was seen */
  519. static int multiline; /* '\n' changed to ';' (counter) */
  520. static struct op *outtree; /* result from parser */
  521. static xint *failpt;
  522. static xint *errpt;
  523. static struct brkcon *brklist;
  524. static struct wdblock *wdlist;
  525. static struct wdblock *iolist;
  526. #ifdef MSHDEBUG
  527. static struct var *mshdbg_var;
  528. #endif
  529. static struct var *vlist; /* dictionary */
  530. static struct var *homedir; /* home directory */
  531. static struct var *prompt; /* main prompt */
  532. static struct var *cprompt; /* continuation prompt */
  533. static struct var *path; /* search path for commands */
  534. static struct var *shell; /* shell to interpret command files */
  535. static struct var *ifs; /* field separators */
  536. static int areanum; /* current allocation area */
  537. static smallint intr; /* interrupt pending (bool) */
  538. static smallint heedint = 1; /* heed interrupt signals (bool) */
  539. static int inparse;
  540. static char *null = (char*)""; /* null value for variable */
  541. static void (*qflag)(int) = SIG_IGN;
  542. static int startl;
  543. static int peeksym;
  544. static int nlseen;
  545. static int iounit = IODEFAULT;
  546. static YYSTYPE yylval;
  547. static char *elinep; /* done in main(): = line + sizeof(line) - 5 */
  548. static struct here *inhere; /* list of hear docs while parsing */
  549. static struct here *acthere; /* list of active here documents */
  550. static struct region *areabot; /* bottom of area */
  551. static struct region *areatop; /* top of area */
  552. static struct region *areanxt; /* starting point of scan */
  553. static void *brktop;
  554. static void *brkaddr;
  555. #define AFID_NOBUF (~0)
  556. #define AFID_ID 0
  557. /*
  558. * parsing & execution environment
  559. */
  560. struct env {
  561. char *linep;
  562. struct io *iobase;
  563. struct io *iop;
  564. xint *errpt; /* void * */
  565. int iofd;
  566. struct env *oenv;
  567. };
  568. struct globals {
  569. struct env global_env;
  570. struct ioarg temparg; // = { .afid = AFID_NOBUF }; /* temporary for PUSHIO */
  571. unsigned bufid; // = AFID_ID; /* buffer id counter */
  572. char ourtrap[_NSIG + 1];
  573. char *trap[_NSIG + 1];
  574. struct iobuf sharedbuf; /* in main(): set to { AFID_NOBUF } */
  575. struct iobuf mainbuf; /* in main(): set to { AFID_NOBUF } */
  576. struct ioarg ioargstack[NPUSH];
  577. /*
  578. * flags:
  579. * -e: quit on error
  580. * -k: look for name=value everywhere on command line
  581. * -n: no execution
  582. * -t: exit after reading and executing one command
  583. * -v: echo as read
  584. * -x: trace
  585. * -u: unset variables net diagnostic
  586. */
  587. char flags['z' - 'a' + 1];
  588. char filechar_cmdbuf[BUFSIZ];
  589. char line[LINELIM];
  590. char child_cmd[LINELIM];
  591. struct io iostack[NPUSH];
  592. char grave__var_name[LINELIM];
  593. char grave__alt_value[LINELIM];
  594. };
  595. #define G (*ptr_to_globals)
  596. #define global_env (G.global_env )
  597. #define temparg (G.temparg )
  598. #define bufid (G.bufid )
  599. #define ourtrap (G.ourtrap )
  600. #define trap (G.trap )
  601. #define sharedbuf (G.sharedbuf )
  602. #define mainbuf (G.mainbuf )
  603. #define ioargstack (G.ioargstack )
  604. /* this looks weird, but is OK ... we index FLAG with 'a'...'z' */
  605. #define FLAG (G.flags - 'a' )
  606. #define filechar_cmdbuf (G.filechar_cmdbuf)
  607. #define line (G.line )
  608. #define child_cmd (G.child_cmd )
  609. #define iostack (G.iostack )
  610. #define INIT_G() do { \
  611. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  612. global_env.linep = line; \
  613. global_env.iobase = iostack; \
  614. global_env.iop = iostack - 1; \
  615. global_env.iofd = FDBASE; \
  616. temparg.afid = AFID_NOBUF; \
  617. bufid = AFID_ID; \
  618. } while (0)
  619. /* in substitution */
  620. #define INSUB() (global_env.iop->task == XGRAVE || global_env.iop->task == XDOLL)
  621. #define RUN(what, arg, gen) ((temparg.what = (arg)), run(&temparg, (gen)))
  622. #ifdef MSHDEBUG
  623. static void print_tree(struct op *head)
  624. {
  625. if (head == NULL) {
  626. DBGPRINTF(("PRINT_TREE: no tree\n"));
  627. return;
  628. }
  629. DBGPRINTF(("NODE: %p, left %p, right %p\n", head, head->left,
  630. head->right));
  631. if (head->left)
  632. print_tree(head->left);
  633. if (head->right)
  634. print_tree(head->right);
  635. }
  636. #endif /* MSHDEBUG */
  637. /*
  638. * IO functions
  639. */
  640. static void prs(const char *s)
  641. {
  642. if (*s)
  643. xwrite_str(STDERR_FILENO, s);
  644. }
  645. static void prn(unsigned u)
  646. {
  647. prs(itoa(u));
  648. }
  649. static void echo(char **wp)
  650. {
  651. int i;
  652. prs("+");
  653. for (i = 0; wp[i]; i++) {
  654. if (i)
  655. prs(" ");
  656. prs(wp[i]);
  657. }
  658. prs("\n");
  659. }
  660. static void closef(int i)
  661. {
  662. if (i > 2)
  663. close(i);
  664. }
  665. static void closeall(void)
  666. {
  667. int u;
  668. for (u = NUFILE; u < NOFILE;)
  669. close(u++);
  670. }
  671. /* fail but return to process next command */
  672. static void fail(void) NORETURN;
  673. static void fail(void)
  674. {
  675. longjmp(failpt, 1);
  676. /* NOTREACHED */
  677. }
  678. /* abort shell (or fail in subshell) */
  679. static void leave(void) NORETURN;
  680. static void leave(void)
  681. {
  682. DBGPRINTF(("LEAVE: leave called!\n"));
  683. if (execflg)
  684. fail();
  685. scraphere();
  686. freehere(1);
  687. runtrap(0);
  688. _exit(exstat);
  689. /* NOTREACHED */
  690. }
  691. static void warn(const char *s)
  692. {
  693. if (*s) {
  694. prs(s);
  695. if (!exstat)
  696. exstat = 255;
  697. }
  698. prs("\n");
  699. if (FLAG['e'])
  700. leave();
  701. }
  702. static void err(const char *s)
  703. {
  704. warn(s);
  705. if (FLAG['n'])
  706. return;
  707. if (!interactive)
  708. leave();
  709. if (global_env.errpt)
  710. longjmp(global_env.errpt, 1);
  711. closeall();
  712. global_env.iop = global_env.iobase = iostack;
  713. }
  714. /* -------- area.c -------- */
  715. /*
  716. * All memory between (char *)areabot and (char *)(areatop+1) is
  717. * exclusively administered by the area management routines.
  718. * It is assumed that sbrk() and brk() manipulate the high end.
  719. */
  720. #define sbrk(X) ({ \
  721. void * __q = (void *)-1; \
  722. if (brkaddr + (int)(X) < brktop) { \
  723. __q = brkaddr; \
  724. brkaddr += (int)(X); \
  725. } \
  726. __q; \
  727. })
  728. static void initarea(void)
  729. {
  730. brkaddr = xmalloc(AREASIZE);
  731. brktop = brkaddr + AREASIZE;
  732. while ((long) sbrk(0) & ALIGN)
  733. sbrk(1);
  734. areabot = (struct region *) sbrk(REGSIZE);
  735. areabot->next = areabot;
  736. areabot->area = BUSY;
  737. areatop = areabot;
  738. areanxt = areabot;
  739. }
  740. static char *getcell(unsigned nbytes)
  741. {
  742. int nregio;
  743. struct region *p, *q;
  744. int i;
  745. if (nbytes == 0) {
  746. puts("getcell(0)");
  747. abort();
  748. }
  749. /* silly and defeats the algorithm */
  750. /*
  751. * round upwards and add administration area
  752. */
  753. nregio = (nbytes + (REGSIZE - 1)) / REGSIZE + 1;
  754. p = areanxt;
  755. for (;;) {
  756. if (p->area > areanum) {
  757. /*
  758. * merge free cells
  759. */
  760. while ((q = p->next)->area > areanum && q != areanxt)
  761. p->next = q->next;
  762. /*
  763. * exit loop if cell big enough
  764. */
  765. if (q >= p + nregio)
  766. goto found;
  767. }
  768. p = p->next;
  769. if (p == areanxt)
  770. break;
  771. }
  772. i = nregio >= GROWBY ? nregio : GROWBY;
  773. p = (struct region *) sbrk(i * REGSIZE);
  774. if (p == (struct region *) -1)
  775. return NULL;
  776. p--;
  777. if (p != areatop) {
  778. puts("not contig");
  779. abort(); /* allocated areas are contiguous */
  780. }
  781. q = p + i;
  782. p->next = q;
  783. p->area = FREE;
  784. q->next = areabot;
  785. q->area = BUSY;
  786. areatop = q;
  787. found:
  788. /*
  789. * we found a FREE area big enough, pointed to by 'p', and up to 'q'
  790. */
  791. areanxt = p + nregio;
  792. if (areanxt < q) {
  793. /*
  794. * split into requested area and rest
  795. */
  796. if (areanxt + 1 > q) {
  797. puts("OOM");
  798. abort(); /* insufficient space left for admin */
  799. }
  800. areanxt->next = q;
  801. areanxt->area = FREE;
  802. p->next = areanxt;
  803. }
  804. p->area = areanum;
  805. return (char *) (p + 1);
  806. }
  807. static void freecell(char *cp)
  808. {
  809. struct region *p;
  810. p = (struct region *) cp;
  811. if (p != NULL) {
  812. p--;
  813. if (p < areanxt)
  814. areanxt = p;
  815. p->area = FREE;
  816. }
  817. }
  818. #define DELETE(obj) freecell((char *)obj)
  819. static void freearea(int a)
  820. {
  821. struct region *p, *top;
  822. top = areatop;
  823. for (p = areabot; p != top; p = p->next)
  824. if (p->area >= a)
  825. p->area = FREE;
  826. }
  827. static void setarea(char *cp, int a)
  828. {
  829. struct region *p;
  830. p = (struct region *) cp;
  831. if (p != NULL)
  832. (p - 1)->area = a;
  833. }
  834. static int getarea(char *cp)
  835. {
  836. return ((struct region *) cp - 1)->area;
  837. }
  838. static void garbage(void)
  839. {
  840. struct region *p, *q, *top;
  841. top = areatop;
  842. for (p = areabot; p != top; p = p->next) {
  843. if (p->area > areanum) {
  844. while ((q = p->next)->area > areanum)
  845. p->next = q->next;
  846. areanxt = p;
  847. }
  848. }
  849. #ifdef SHRINKBY
  850. if (areatop >= q + SHRINKBY && q->area > areanum) {
  851. brk((char *) (q + 1));
  852. q->next = areabot;
  853. q->area = BUSY;
  854. areatop = q;
  855. }
  856. #endif
  857. }
  858. static void *get_space(int n)
  859. {
  860. char *cp;
  861. cp = getcell(n);
  862. if (cp == NULL)
  863. err("out of string space");
  864. return cp;
  865. }
  866. static char *strsave(const char *s, int a)
  867. {
  868. char *cp;
  869. cp = get_space(strlen(s) + 1);
  870. if (cp == NULL) {
  871. // FIXME: I highly doubt this is good.
  872. return (char*)"";
  873. }
  874. setarea(cp, a);
  875. strcpy(cp, s);
  876. return cp;
  877. }
  878. /* -------- var.c -------- */
  879. static int eqname(const char *n1, const char *n2)
  880. {
  881. for (; *n1 != '=' && *n1 != '\0'; n1++)
  882. if (*n2++ != *n1)
  883. return 0;
  884. return *n2 == '\0' || *n2 == '=';
  885. }
  886. static const char *findeq(const char *cp)
  887. {
  888. while (*cp != '\0' && *cp != '=')
  889. cp++;
  890. return cp;
  891. }
  892. /*
  893. * Find the given name in the dictionary
  894. * and return its value. If the name was
  895. * not previously there, enter it now and
  896. * return a null value.
  897. */
  898. static struct var *lookup(const char *n)
  899. {
  900. // FIXME: dirty hack
  901. static struct var dummy;
  902. struct var *vp;
  903. const char *cp;
  904. char *xp;
  905. int c;
  906. if (isdigit(*n)) {
  907. dummy.name = (char*)n;
  908. for (c = 0; isdigit(*n) && c < 1000; n++)
  909. c = c * 10 + *n - '0';
  910. dummy.status = RONLY;
  911. dummy.value = (c <= dolc ? dolv[c] : null);
  912. return &dummy;
  913. }
  914. for (vp = vlist; vp; vp = vp->next)
  915. if (eqname(vp->name, n))
  916. return vp;
  917. cp = findeq(n);
  918. vp = get_space(sizeof(*vp));
  919. if (vp == 0 || (vp->name = get_space((int) (cp - n) + 2)) == NULL) {
  920. dummy.name = dummy.value = (char*)"";
  921. return &dummy;
  922. }
  923. xp = vp->name;
  924. while ((*xp = *n++) != '\0' && *xp != '=')
  925. xp++;
  926. *xp++ = '=';
  927. *xp = '\0';
  928. setarea((char *) vp, 0);
  929. setarea((char *) vp->name, 0);
  930. vp->value = null;
  931. vp->next = vlist;
  932. vp->status = GETCELL;
  933. vlist = vp;
  934. return vp;
  935. }
  936. /*
  937. * if name is not NULL, it must be
  938. * a prefix of the space `val',
  939. * and end with `='.
  940. * this is all so that exporting
  941. * values is reasonably painless.
  942. */
  943. static void nameval(struct var *vp, const char *val, const char *name)
  944. {
  945. const char *cp;
  946. char *xp;
  947. int fl;
  948. if (vp->status & RONLY) {
  949. xp = vp->name;
  950. while (*xp && *xp != '=')
  951. fputc(*xp++, stderr);
  952. err(" is read-only");
  953. return;
  954. }
  955. fl = 0;
  956. if (name == NULL) {
  957. xp = get_space(strlen(vp->name) + strlen(val) + 2);
  958. if (xp == NULL)
  959. return;
  960. /* make string: name=value */
  961. setarea(xp, 0);
  962. name = xp;
  963. cp = vp->name;
  964. while ((*xp = *cp++) != '\0' && *xp != '=')
  965. xp++;
  966. *xp++ = '=';
  967. strcpy(xp, val);
  968. val = xp;
  969. fl = GETCELL;
  970. }
  971. if (vp->status & GETCELL)
  972. freecell(vp->name); /* form new string `name=value' */
  973. vp->name = (char*)name;
  974. vp->value = (char*)val;
  975. vp->status |= fl;
  976. }
  977. /*
  978. * give variable at `vp' the value `val'.
  979. */
  980. static void setval(struct var *vp, const char *val)
  981. {
  982. nameval(vp, val, NULL);
  983. }
  984. static void export(struct var *vp)
  985. {
  986. vp->status |= EXPORT;
  987. }
  988. static void ronly(struct var *vp)
  989. {
  990. if (isalpha(vp->name[0]) || vp->name[0] == '_') /* not an internal symbol */
  991. vp->status |= RONLY;
  992. }
  993. static int isassign(const char *s)
  994. {
  995. unsigned char c;
  996. DBGPRINTF7(("ISASSIGN: enter, s=%s\n", s));
  997. c = *s;
  998. /* no isalpha() - we shouldn't use locale */
  999. /* c | 0x20 - lowercase (Latin) letters */
  1000. if (c != '_' && (unsigned)((c|0x20) - 'a') > 25)
  1001. /* not letter */
  1002. return 0;
  1003. while (1) {
  1004. c = *++s;
  1005. if (c == '=')
  1006. return 1;
  1007. if (c == '\0')
  1008. return 0;
  1009. if (c != '_'
  1010. && (unsigned)(c - '0') > 9 /* not number */
  1011. && (unsigned)((c|0x20) - 'a') > 25 /* not letter */
  1012. ) {
  1013. return 0;
  1014. }
  1015. }
  1016. }
  1017. static int assign(const char *s, int cf)
  1018. {
  1019. const char *cp;
  1020. struct var *vp;
  1021. DBGPRINTF7(("ASSIGN: enter, s=%s, cf=%d\n", s, cf));
  1022. if (!isalpha(*s) && *s != '_')
  1023. return 0;
  1024. for (cp = s; *cp != '='; cp++)
  1025. if (*cp == '\0' || (!isalnum(*cp) && *cp != '_'))
  1026. return 0;
  1027. vp = lookup(s);
  1028. nameval(vp, ++cp, cf == COPYV ? NULL : s);
  1029. if (cf != COPYV)
  1030. vp->status &= ~GETCELL;
  1031. return 1;
  1032. }
  1033. static int checkname(char *cp)
  1034. {
  1035. DBGPRINTF7(("CHECKNAME: enter, cp=%s\n", cp));
  1036. if (!isalpha(*cp++) && *(cp - 1) != '_')
  1037. return 0;
  1038. while (*cp)
  1039. if (!isalnum(*cp++) && *(cp - 1) != '_')
  1040. return 0;
  1041. return 1;
  1042. }
  1043. static void putvlist(int f, int out)
  1044. {
  1045. struct var *vp;
  1046. for (vp = vlist; vp; vp = vp->next) {
  1047. if (vp->status & f && (isalpha(*vp->name) || *vp->name == '_')) {
  1048. if (vp->status & EXPORT)
  1049. write(out, "export ", 7);
  1050. if (vp->status & RONLY)
  1051. write(out, "readonly ", 9);
  1052. write(out, vp->name, (int) (findeq(vp->name) - vp->name));
  1053. write(out, "\n", 1);
  1054. }
  1055. }
  1056. }
  1057. /*
  1058. * trap handling
  1059. */
  1060. static void sig(int i)
  1061. {
  1062. trapset = i;
  1063. signal(i, sig);
  1064. }
  1065. static void runtrap(int i)
  1066. {
  1067. char *trapstr;
  1068. trapstr = trap[i];
  1069. if (trapstr == NULL)
  1070. return;
  1071. if (i == 0)
  1072. trap[i] = NULL;
  1073. RUN(aword, trapstr, nlchar);
  1074. }
  1075. static void setdash(void)
  1076. {
  1077. char *cp;
  1078. int c;
  1079. char m['z' - 'a' + 1];
  1080. cp = m;
  1081. for (c = 'a'; c <= 'z'; c++)
  1082. if (FLAG[c])
  1083. *cp++ = c;
  1084. *cp = '\0';
  1085. setval(lookup("-"), m);
  1086. }
  1087. static int newfile(char *s)
  1088. {
  1089. int f;
  1090. DBGPRINTF7(("NEWFILE: opening %s\n", s));
  1091. f = 0;
  1092. if (NOT_LONE_DASH(s)) {
  1093. DBGPRINTF(("NEWFILE: s is %s\n", s));
  1094. f = open(s, O_RDONLY);
  1095. if (f < 0) {
  1096. prs(s);
  1097. err(": can't open");
  1098. return 1;
  1099. }
  1100. }
  1101. next(remap(f));
  1102. return 0;
  1103. }
  1104. #ifdef UNUSED
  1105. struct op *scantree(struct op *head)
  1106. {
  1107. struct op *dotnode;
  1108. if (head == NULL)
  1109. return NULL;
  1110. if (head->left != NULL) {
  1111. dotnode = scantree(head->left);
  1112. if (dotnode)
  1113. return dotnode;
  1114. }
  1115. if (head->right != NULL) {
  1116. dotnode = scantree(head->right);
  1117. if (dotnode)
  1118. return dotnode;
  1119. }
  1120. if (head->op_words == NULL)
  1121. return NULL;
  1122. DBGPRINTF5(("SCANTREE: checking node %p\n", head));
  1123. if ((head->op_type != TDOT) && LONE_CHAR(head->op_words[0], '.')) {
  1124. DBGPRINTF5(("SCANTREE: dot found in node %p\n", head));
  1125. return head;
  1126. }
  1127. return NULL;
  1128. }
  1129. #endif
  1130. static void onecommand(void)
  1131. {
  1132. int i;
  1133. jmp_buf m1;
  1134. DBGPRINTF(("ONECOMMAND: enter, outtree=%p\n", outtree));
  1135. while (global_env.oenv)
  1136. quitenv();
  1137. areanum = 1;
  1138. freehere(areanum);
  1139. freearea(areanum);
  1140. garbage();
  1141. wdlist = NULL;
  1142. iolist = NULL;
  1143. global_env.errpt = NULL;
  1144. global_env.linep = line;
  1145. yynerrs = 0;
  1146. multiline = 0;
  1147. inparse = 1;
  1148. intr = 0;
  1149. execflg = 0;
  1150. failpt = m1;
  1151. setjmp(failpt); /* Bruce Evans' fix */
  1152. failpt = m1;
  1153. if (setjmp(failpt) || yyparse() || intr) {
  1154. DBGPRINTF(("ONECOMMAND: this is not good.\n"));
  1155. while (global_env.oenv)
  1156. quitenv();
  1157. scraphere();
  1158. if (!interactive && intr)
  1159. leave();
  1160. inparse = 0;
  1161. intr = 0;
  1162. return;
  1163. }
  1164. inparse = 0;
  1165. brklist = 0;
  1166. intr = 0;
  1167. execflg = 0;
  1168. if (!FLAG['n']) {
  1169. DBGPRINTF(("ONECOMMAND: calling execute, t=outtree=%p\n",
  1170. outtree));
  1171. execute(outtree, NOPIPE, NOPIPE, /* no_fork: */ 0);
  1172. }
  1173. if (!interactive && intr) {
  1174. execflg = 0;
  1175. leave();
  1176. }
  1177. i = trapset;
  1178. if (i != 0) {
  1179. trapset = 0;
  1180. runtrap(i);
  1181. }
  1182. }
  1183. static int newenv(int f)
  1184. {
  1185. struct env *ep;
  1186. DBGPRINTF(("NEWENV: f=%d (indicates quitenv and return)\n", f));
  1187. if (f) {
  1188. quitenv();
  1189. return 1;
  1190. }
  1191. ep = get_space(sizeof(*ep));
  1192. if (ep == NULL) {
  1193. while (global_env.oenv)
  1194. quitenv();
  1195. fail();
  1196. }
  1197. *ep = global_env;
  1198. global_env.oenv = ep;
  1199. global_env.errpt = errpt;
  1200. return 0;
  1201. }
  1202. static void quitenv(void)
  1203. {
  1204. struct env *ep;
  1205. int fd;
  1206. DBGPRINTF(("QUITENV: global_env.oenv=%p\n", global_env.oenv));
  1207. ep = global_env.oenv;
  1208. if (ep != NULL) {
  1209. fd = global_env.iofd;
  1210. global_env = *ep;
  1211. /* should close `'d files */
  1212. DELETE(ep);
  1213. while (--fd >= global_env.iofd)
  1214. close(fd);
  1215. }
  1216. }
  1217. /*
  1218. * Is character c in s?
  1219. */
  1220. static int any(int c, const char *s)
  1221. {
  1222. while (*s)
  1223. if (*s++ == c)
  1224. return 1;
  1225. return 0;
  1226. }
  1227. /*
  1228. * Is any character from s1 in s2?
  1229. */
  1230. static int anys(const char *s1, const char *s2)
  1231. {
  1232. while (*s1)
  1233. if (any(*s1++, s2))
  1234. return 1;
  1235. return 0;
  1236. }
  1237. static char *putn(int n)
  1238. {
  1239. return itoa(n);
  1240. }
  1241. static void next(int f)
  1242. {
  1243. PUSHIO(afile, f, filechar);
  1244. }
  1245. static void onintr(int s UNUSED_PARAM) /* ANSI C requires a parameter */
  1246. {
  1247. signal(SIGINT, onintr);
  1248. intr = 1;
  1249. if (interactive) {
  1250. if (inparse) {
  1251. prs("\n");
  1252. fail();
  1253. }
  1254. } else if (heedint) {
  1255. execflg = 0;
  1256. leave();
  1257. }
  1258. }
  1259. /* -------- gmatch.c -------- */
  1260. /*
  1261. * int gmatch(string, pattern)
  1262. * char *string, *pattern;
  1263. *
  1264. * Match a pattern as in sh(1).
  1265. */
  1266. #define CMASK 0377
  1267. #define QUOTE 0200
  1268. #define QMASK (CMASK & ~QUOTE)
  1269. #define NOT '!' /* might use ^ */
  1270. static const char *cclass(const char *p, int sub)
  1271. {
  1272. int c, d, not, found;
  1273. not = (*p == NOT);
  1274. if (not != 0)
  1275. p++;
  1276. found = not;
  1277. do {
  1278. if (*p == '\0')
  1279. return NULL;
  1280. c = *p & CMASK;
  1281. if (p[1] == '-' && p[2] != ']') {
  1282. d = p[2] & CMASK;
  1283. p++;
  1284. } else
  1285. d = c;
  1286. if (c == sub || (c <= sub && sub <= d))
  1287. found = !not;
  1288. } while (*++p != ']');
  1289. return found ? p + 1 : NULL;
  1290. }
  1291. static int gmatch(const char *s, const char *p)
  1292. {
  1293. int sc, pc;
  1294. if (s == NULL || p == NULL)
  1295. return 0;
  1296. while ((pc = *p++ & CMASK) != '\0') {
  1297. sc = *s++ & QMASK;
  1298. switch (pc) {
  1299. case '[':
  1300. p = cclass(p, sc);
  1301. if (p == NULL)
  1302. return 0;
  1303. break;
  1304. case '?':
  1305. if (sc == 0)
  1306. return 0;
  1307. break;
  1308. case '*':
  1309. s--;
  1310. do {
  1311. if (*p == '\0' || gmatch(s, p))
  1312. return 1;
  1313. } while (*s++ != '\0');
  1314. return 0;
  1315. default:
  1316. if (sc != (pc & ~QUOTE))
  1317. return 0;
  1318. }
  1319. }
  1320. return *s == '\0';
  1321. }
  1322. /* -------- csyn.c -------- */
  1323. /*
  1324. * shell: syntax (C version)
  1325. */
  1326. static void yyerror(const char *s) NORETURN;
  1327. static void yyerror(const char *s)
  1328. {
  1329. yynerrs = 1;
  1330. if (interactive && global_env.iop <= iostack) {
  1331. multiline = 0;
  1332. while (eofc() == 0 && yylex(0) != '\n')
  1333. continue;
  1334. }
  1335. err(s);
  1336. fail();
  1337. }
  1338. static void zzerr(void) NORETURN;
  1339. static void zzerr(void)
  1340. {
  1341. yyerror("syntax error");
  1342. }
  1343. int yyparse(void)
  1344. {
  1345. DBGPRINTF7(("YYPARSE: enter...\n"));
  1346. startl = 1;
  1347. peeksym = 0;
  1348. yynerrs = 0;
  1349. outtree = c_list();
  1350. musthave('\n', 0);
  1351. return yynerrs; /* 0/1 */
  1352. }
  1353. static struct op *pipeline(int cf)
  1354. {
  1355. struct op *t, *p;
  1356. int c;
  1357. DBGPRINTF7(("PIPELINE: enter, cf=%d\n", cf));
  1358. t = command(cf);
  1359. DBGPRINTF9(("PIPELINE: t=%p\n", t));
  1360. if (t != NULL) {
  1361. while ((c = yylex(0)) == '|') {
  1362. p = command(CONTIN);
  1363. if (p == NULL) {
  1364. DBGPRINTF8(("PIPELINE: error!\n"));
  1365. zzerr();
  1366. }
  1367. if (t->op_type != TPAREN && t->op_type != TCOM) {
  1368. /* shell statement */
  1369. t = block(TPAREN, t, NOBLOCK, NOWORDS);
  1370. }
  1371. t = block(TPIPE, t, p, NOWORDS);
  1372. }
  1373. peeksym = c;
  1374. }
  1375. DBGPRINTF7(("PIPELINE: returning t=%p\n", t));
  1376. return t;
  1377. }
  1378. static struct op *andor(void)
  1379. {
  1380. struct op *t, *p;
  1381. int c;
  1382. DBGPRINTF7(("ANDOR: enter...\n"));
  1383. t = pipeline(0);
  1384. DBGPRINTF9(("ANDOR: t=%p\n", t));
  1385. if (t != NULL) {
  1386. while ((c = yylex(0)) == LOGAND || c == LOGOR) {
  1387. p = pipeline(CONTIN);
  1388. if (p == NULL) {
  1389. DBGPRINTF8(("ANDOR: error!\n"));
  1390. zzerr();
  1391. }
  1392. t = block(c == LOGAND ? TAND : TOR, t, p, NOWORDS);
  1393. }
  1394. peeksym = c;
  1395. }
  1396. DBGPRINTF7(("ANDOR: returning t=%p\n", t));
  1397. return t;
  1398. }
  1399. static struct op *c_list(void)
  1400. {
  1401. struct op *t, *p;
  1402. int c;
  1403. DBGPRINTF7(("C_LIST: enter...\n"));
  1404. t = andor();
  1405. if (t != NULL) {
  1406. peeksym = yylex(0);
  1407. if (peeksym == '&')
  1408. t = block(TASYNC, t, NOBLOCK, NOWORDS);
  1409. while ((c = yylex(0)) == ';' || c == '&'
  1410. || (multiline && c == '\n')
  1411. ) {
  1412. p = andor();
  1413. if (p== NULL)
  1414. return t;
  1415. peeksym = yylex(0);
  1416. if (peeksym == '&')
  1417. p = block(TASYNC, p, NOBLOCK, NOWORDS);
  1418. t = list(t, p);
  1419. } /* WHILE */
  1420. peeksym = c;
  1421. }
  1422. /* IF */
  1423. DBGPRINTF7(("C_LIST: returning t=%p\n", t));
  1424. return t;
  1425. }
  1426. static int synio(int cf)
  1427. {
  1428. struct ioword *iop;
  1429. int i;
  1430. int c;
  1431. DBGPRINTF7(("SYNIO: enter, cf=%d\n", cf));
  1432. c = yylex(cf);
  1433. if (c != '<' && c != '>') {
  1434. peeksym = c;
  1435. return 0;
  1436. }
  1437. i = yylval.i;
  1438. musthave(WORD, 0);
  1439. iop = io(iounit, i, yylval.cp);
  1440. iounit = IODEFAULT;
  1441. if (i & IOHERE)
  1442. markhere(yylval.cp, iop);
  1443. DBGPRINTF7(("SYNIO: returning 1\n"));
  1444. return 1;
  1445. }
  1446. static void musthave(int c, int cf)
  1447. {
  1448. peeksym = yylex(cf);
  1449. if (peeksym != c) {
  1450. DBGPRINTF7(("MUSTHAVE: error!\n"));
  1451. zzerr();
  1452. }
  1453. peeksym = 0;
  1454. }
  1455. static struct op *simple(void)
  1456. {
  1457. struct op *t;
  1458. t = NULL;
  1459. for (;;) {
  1460. switch (peeksym = yylex(0)) {
  1461. case '<':
  1462. case '>':
  1463. (void) synio(0);
  1464. break;
  1465. case WORD:
  1466. if (t == NULL) {
  1467. t = newtp();
  1468. t->op_type = TCOM;
  1469. }
  1470. peeksym = 0;
  1471. word(yylval.cp);
  1472. break;
  1473. default:
  1474. return t;
  1475. }
  1476. }
  1477. }
  1478. static struct op *nested(int type, int mark)
  1479. {
  1480. struct op *t;
  1481. DBGPRINTF3(("NESTED: enter, type=%d, mark=%d\n", type, mark));
  1482. multiline++;
  1483. t = c_list();
  1484. musthave(mark, 0);
  1485. multiline--;
  1486. return block(type, t, NOBLOCK, NOWORDS);
  1487. }
  1488. static struct op *command(int cf)
  1489. {
  1490. struct op *t;
  1491. struct wdblock *iosave;
  1492. int c;
  1493. DBGPRINTF(("COMMAND: enter, cf=%d\n", cf));
  1494. iosave = iolist;
  1495. iolist = NULL;
  1496. if (multiline)
  1497. cf |= CONTIN;
  1498. while (synio(cf))
  1499. cf = 0;
  1500. c = yylex(cf);
  1501. switch (c) {
  1502. default:
  1503. peeksym = c;
  1504. t = simple();
  1505. if (t == NULL) {
  1506. if (iolist == NULL)
  1507. return NULL;
  1508. t = newtp();
  1509. t->op_type = TCOM;
  1510. }
  1511. break;
  1512. case '(':
  1513. t = nested(TPAREN, ')');
  1514. break;
  1515. case '{':
  1516. t = nested(TBRACE, '}');
  1517. break;
  1518. case FOR:
  1519. t = newtp();
  1520. t->op_type = TFOR;
  1521. musthave(WORD, 0);
  1522. startl = 1;
  1523. t->str = yylval.cp;
  1524. multiline++;
  1525. t->op_words = wordlist();
  1526. c = yylex(0);
  1527. if (c != '\n' && c != ';')
  1528. peeksym = c;
  1529. t->left = dogroup(0);
  1530. multiline--;
  1531. break;
  1532. case WHILE:
  1533. case UNTIL:
  1534. multiline++;
  1535. t = newtp();
  1536. t->op_type = (c == WHILE ? TWHILE : TUNTIL);
  1537. t->left = c_list();
  1538. t->right = dogroup(1);
  1539. /* t->op_words = NULL; - newtp() did this */
  1540. multiline--;
  1541. break;
  1542. case CASE:
  1543. t = newtp();
  1544. t->op_type = TCASE;
  1545. musthave(WORD, 0);
  1546. t->str = yylval.cp;
  1547. startl++;
  1548. multiline++;
  1549. musthave(IN, CONTIN);
  1550. startl++;
  1551. t->left = caselist();
  1552. musthave(ESAC, 0);
  1553. multiline--;
  1554. break;
  1555. case IF:
  1556. multiline++;
  1557. t = newtp();
  1558. t->op_type = TIF;
  1559. t->left = c_list();
  1560. t->right = thenpart();
  1561. musthave(FI, 0);
  1562. multiline--;
  1563. break;
  1564. case DOT:
  1565. t = newtp();
  1566. t->op_type = TDOT;
  1567. musthave(WORD, 0); /* gets name of file */
  1568. DBGPRINTF7(("COMMAND: DOT clause, yylval.cp is %s\n", yylval.cp));
  1569. word(yylval.cp); /* add word to wdlist */
  1570. word(NOWORD); /* terminate wdlist */
  1571. t->op_words = copyw(); /* dup wdlist */
  1572. break;
  1573. }
  1574. while (synio(0))
  1575. continue;
  1576. t = namelist(t);
  1577. iolist = iosave;
  1578. DBGPRINTF(("COMMAND: returning %p\n", t));
  1579. return t;
  1580. }
  1581. static struct op *dowholefile(int type /*, int mark*/)
  1582. {
  1583. struct op *t;
  1584. DBGPRINTF(("DOWHOLEFILE: enter, type=%d\n", type /*, mark*/));
  1585. multiline++;
  1586. t = c_list();
  1587. multiline--;
  1588. t = block(type, t, NOBLOCK, NOWORDS);
  1589. DBGPRINTF(("DOWHOLEFILE: return t=%p\n", t));
  1590. return t;
  1591. }
  1592. static struct op *dogroup(int onlydone)
  1593. {
  1594. int c;
  1595. struct op *mylist;
  1596. c = yylex(CONTIN);
  1597. if (c == DONE && onlydone)
  1598. return NULL;
  1599. if (c != DO)
  1600. zzerr();
  1601. mylist = c_list();
  1602. musthave(DONE, 0);
  1603. return mylist;
  1604. }
  1605. static struct op *thenpart(void)
  1606. {
  1607. int c;
  1608. struct op *t;
  1609. c = yylex(0);
  1610. if (c != THEN) {
  1611. peeksym = c;
  1612. return NULL;
  1613. }
  1614. t = newtp();
  1615. /*t->op_type = 0; - newtp() did this */
  1616. t->left = c_list();
  1617. if (t->left == NULL)
  1618. zzerr();
  1619. t->right = elsepart();
  1620. return t;
  1621. }
  1622. static struct op *elsepart(void)
  1623. {
  1624. int c;
  1625. struct op *t;
  1626. switch (c = yylex(0)) {
  1627. case ELSE:
  1628. t = c_list();
  1629. if (t == NULL)
  1630. zzerr();
  1631. return t;
  1632. case ELIF:
  1633. t = newtp();
  1634. t->op_type = TELIF;
  1635. t->left = c_list();
  1636. t->right = thenpart();
  1637. return t;
  1638. default:
  1639. peeksym = c;
  1640. return NULL;
  1641. }
  1642. }
  1643. static struct op *caselist(void)
  1644. {
  1645. struct op *t;
  1646. t = NULL;
  1647. while ((peeksym = yylex(CONTIN)) != ESAC) {
  1648. DBGPRINTF(("CASELIST, doing yylex, peeksym=%d\n", peeksym));
  1649. t = list(t, casepart());
  1650. }
  1651. DBGPRINTF(("CASELIST, returning t=%p\n", t));
  1652. return t;
  1653. }
  1654. static struct op *casepart(void)
  1655. {
  1656. struct op *t;
  1657. DBGPRINTF7(("CASEPART: enter...\n"));
  1658. t = newtp();
  1659. t->op_type = TPAT;
  1660. t->op_words = pattern();
  1661. musthave(')', 0);
  1662. t->left = c_list();
  1663. peeksym = yylex(CONTIN);
  1664. if (peeksym != ESAC)
  1665. musthave(BREAK, CONTIN);
  1666. DBGPRINTF7(("CASEPART: made newtp(TPAT, t=%p)\n", t));
  1667. return t;
  1668. }
  1669. static char **pattern(void)
  1670. {
  1671. int c, cf;
  1672. cf = CONTIN;
  1673. do {
  1674. musthave(WORD, cf);
  1675. word(yylval.cp);
  1676. cf = 0;
  1677. c = yylex(0);
  1678. } while (c == '|');
  1679. peeksym = c;
  1680. word(NOWORD);
  1681. return copyw();
  1682. }
  1683. static char **wordlist(void)
  1684. {
  1685. int c;
  1686. c = yylex(0);
  1687. if (c != IN) {
  1688. peeksym = c;
  1689. return NULL;
  1690. }
  1691. startl = 0;
  1692. while ((c = yylex(0)) == WORD)
  1693. word(yylval.cp);
  1694. word(NOWORD);
  1695. peeksym = c;
  1696. return copyw();
  1697. }
  1698. /*
  1699. * supporting functions
  1700. */
  1701. static struct op *list(struct op *t1, struct op *t2)
  1702. {
  1703. DBGPRINTF7(("LIST: enter, t1=%p, t2=%p\n", t1, t2));
  1704. if (t1 == NULL)
  1705. return t2;
  1706. if (t2 == NULL)
  1707. return t1;
  1708. return block(TLIST, t1, t2, NOWORDS);
  1709. }
  1710. static struct op *block(int type, struct op *t1, struct op *t2, char **wp)
  1711. {
  1712. struct op *t;
  1713. DBGPRINTF7(("BLOCK: enter, type=%d (%s)\n", type, T_CMD_NAMES[type]));
  1714. t = newtp();
  1715. t->op_type = type;
  1716. t->left = t1;
  1717. t->right = t2;
  1718. t->op_words = wp;
  1719. DBGPRINTF7(("BLOCK: inserted %p between %p and %p\n", t, t1, t2));
  1720. return t;
  1721. }
  1722. /* See if given string is a shell multiline (FOR, IF, etc) */
  1723. static int rlookup(char *n)
  1724. {
  1725. struct res {
  1726. char r_name[6];
  1727. int16_t r_val;
  1728. };
  1729. static const struct res restab[] = {
  1730. { "for" , FOR },
  1731. { "case" , CASE },
  1732. { "esac" , ESAC },
  1733. { "while", WHILE },
  1734. { "do" , DO },
  1735. { "done" , DONE },
  1736. { "if" , IF },
  1737. { "in" , IN },
  1738. { "then" , THEN },
  1739. { "else" , ELSE },
  1740. { "elif" , ELIF },
  1741. { "until", UNTIL },
  1742. { "fi" , FI },
  1743. { ";;" , BREAK },
  1744. { "||" , LOGOR },
  1745. { "&&" , LOGAND },
  1746. { "{" , '{' },
  1747. { "}" , '}' },
  1748. { "." , DOT },
  1749. { },
  1750. };
  1751. const struct res *rp;
  1752. DBGPRINTF7(("RLOOKUP: enter, n is %s\n", n));
  1753. for (rp = restab; rp->r_name[0]; rp++)
  1754. if (strcmp(rp->r_name, n) == 0) {
  1755. DBGPRINTF7(("RLOOKUP: match, returning %d\n", rp->r_val));
  1756. return rp->r_val; /* Return numeric code for shell multiline */
  1757. }
  1758. DBGPRINTF7(("RLOOKUP: NO match, returning 0\n"));
  1759. return 0; /* Not a shell multiline */
  1760. }
  1761. static struct op *newtp(void)
  1762. {
  1763. struct op *t;
  1764. t = (struct op *) tree(sizeof(*t));
  1765. memset(t, 0, sizeof(*t));
  1766. DBGPRINTF3(("NEWTP: allocated %p\n", t));
  1767. return t;
  1768. }
  1769. static struct op *namelist(struct op *t)
  1770. {
  1771. DBGPRINTF7(("NAMELIST: enter, t=%p, type %s, iolist=%p\n", t,
  1772. T_CMD_NAMES[t->op_type], iolist));
  1773. if (iolist) {
  1774. iolist = addword((char *) NULL, iolist);
  1775. t->ioact = copyio();
  1776. } else
  1777. t->ioact = NULL;
  1778. if (t->op_type != TCOM) {
  1779. if (t->op_type != TPAREN && t->ioact != NULL) {
  1780. t = block(TPAREN, t, NOBLOCK, NOWORDS);
  1781. t->ioact = t->left->ioact;
  1782. t->left->ioact = NULL;
  1783. }
  1784. return t;
  1785. }
  1786. word(NOWORD);
  1787. t->op_words = copyw();
  1788. return t;
  1789. }
  1790. static char **copyw(void)
  1791. {
  1792. char **wd;
  1793. wd = getwords(wdlist);
  1794. wdlist = NULL;
  1795. return wd;
  1796. }
  1797. static void word(char *cp)
  1798. {
  1799. wdlist = addword(cp, wdlist);
  1800. }
  1801. static struct ioword **copyio(void)
  1802. {
  1803. struct ioword **iop;
  1804. iop = (struct ioword **) getwords(iolist);
  1805. iolist = NULL;
  1806. return iop;
  1807. }
  1808. static struct ioword *io(int u, int f, char *cp)
  1809. {
  1810. struct ioword *iop;
  1811. iop = (struct ioword *) tree(sizeof(*iop));
  1812. iop->io_fd = u;
  1813. iop->io_flag = f;
  1814. iop->io_name = cp;
  1815. iolist = addword((char *) iop, iolist);
  1816. return iop;
  1817. }
  1818. static int yylex(int cf)
  1819. {
  1820. int c, c1;
  1821. int atstart;
  1822. c = peeksym;
  1823. if (c > 0) {
  1824. peeksym = 0;
  1825. if (c == '\n')
  1826. startl = 1;
  1827. return c;
  1828. }
  1829. nlseen = 0;
  1830. atstart = startl;
  1831. startl = 0;
  1832. yylval.i = 0;
  1833. global_env.linep = line;
  1834. /* MALAMO */
  1835. line[LINELIM - 1] = '\0';
  1836. loop:
  1837. while ((c = my_getc(0)) == ' ' || c == '\t') /* Skip whitespace */
  1838. continue;
  1839. switch (c) {
  1840. default:
  1841. if (any(c, "0123456789")) {
  1842. c1 = my_getc(0);
  1843. unget(c1);
  1844. if (c1 == '<' || c1 == '>') {
  1845. iounit = c - '0';
  1846. goto loop;
  1847. }
  1848. *global_env.linep++ = c;
  1849. c = c1;
  1850. }
  1851. break;
  1852. case '#': /* Comment, skip to next newline or End-of-string */
  1853. while ((c = my_getc(0)) != '\0' && c != '\n')
  1854. continue;
  1855. unget(c);
  1856. goto loop;
  1857. case 0:
  1858. DBGPRINTF5(("YYLEX: return 0, c=%d\n", c));
  1859. return c;
  1860. case '$':
  1861. DBGPRINTF9(("YYLEX: found $\n"));
  1862. *global_env.linep++ = c;
  1863. c = my_getc(0);
  1864. if (c == '{') {
  1865. c = collect(c, '}');
  1866. if (c != '\0')
  1867. return c;
  1868. goto pack;
  1869. }
  1870. break;
  1871. case '`':
  1872. case '\'':
  1873. case '"':
  1874. c = collect(c, c);
  1875. if (c != '\0')
  1876. return c;
  1877. goto pack;
  1878. case '|':
  1879. case '&':
  1880. case ';':
  1881. startl = 1;
  1882. /* If more chars process them, else return NULL char */
  1883. c1 = dual(c);
  1884. if (c1 != '\0')
  1885. return c1;
  1886. return c;
  1887. case '^':
  1888. startl = 1;
  1889. return '|';
  1890. case '>':
  1891. case '<':
  1892. diag(c);
  1893. return c;
  1894. case '\n':
  1895. nlseen++;
  1896. gethere();
  1897. startl = 1;
  1898. if (multiline || cf & CONTIN) {
  1899. if (interactive && global_env.iop <= iostack) {
  1900. #if ENABLE_FEATURE_EDITING
  1901. current_prompt = cprompt->value;
  1902. #else
  1903. prs(cprompt->value);
  1904. #endif
  1905. }
  1906. if (cf & CONTIN)
  1907. goto loop;
  1908. }
  1909. return c;
  1910. case '(':
  1911. case ')':
  1912. startl = 1;
  1913. return c;
  1914. }
  1915. unget(c);
  1916. pack:
  1917. while ((c = my_getc(0)) != '\0' && !any(c, "`$ '\"\t;&<>()|^\n")) {
  1918. if (global_env.linep >= elinep)
  1919. err("word too long");
  1920. else
  1921. *global_env.linep++ = c;
  1922. };
  1923. unget(c);
  1924. if (any(c, "\"'`$"))
  1925. goto loop;
  1926. *global_env.linep++ = '\0';
  1927. if (atstart) {
  1928. c = rlookup(line);
  1929. if (c != 0) {
  1930. startl = 1;
  1931. return c;
  1932. }
  1933. }
  1934. yylval.cp = strsave(line, areanum);
  1935. return WORD;
  1936. }
  1937. static int collect(int c, int c1)
  1938. {
  1939. char s[2];
  1940. DBGPRINTF8(("COLLECT: enter, c=%d, c1=%d\n", c, c1));
  1941. *global_env.linep++ = c;
  1942. while ((c = my_getc(c1)) != c1) {
  1943. if (c == 0) {
  1944. unget(c);
  1945. s[0] = c1;
  1946. s[1] = 0;
  1947. prs("no closing ");
  1948. yyerror(s);
  1949. return YYERRCODE;
  1950. }
  1951. if (interactive && c == '\n' && global_env.iop <= iostack) {
  1952. #if ENABLE_FEATURE_EDITING
  1953. current_prompt = cprompt->value;
  1954. #else
  1955. prs(cprompt->value);
  1956. #endif
  1957. }
  1958. *global_env.linep++ = c;
  1959. }
  1960. *global_env.linep++ = c;
  1961. DBGPRINTF8(("COLLECT: return 0, line is %s\n", line));
  1962. return 0;
  1963. }
  1964. /* "multiline commands" helper func */
  1965. /* see if next 2 chars form a shell multiline */
  1966. static int dual(int c)
  1967. {
  1968. char s[3];
  1969. char *cp = s;
  1970. DBGPRINTF8(("DUAL: enter, c=%d\n", c));
  1971. *cp++ = c; /* c is the given "peek" char */
  1972. *cp++ = my_getc(0); /* get next char of input */
  1973. *cp = '\0'; /* add EOS marker */
  1974. c = rlookup(s); /* see if 2 chars form a shell multiline */
  1975. if (c == 0)
  1976. unget(*--cp); /* String is not a shell multiline, put peek char back */
  1977. return c; /* String is multiline, return numeric multiline (restab) code */
  1978. }
  1979. static void diag(int ec)
  1980. {
  1981. int c;
  1982. DBGPRINTF8(("DIAG: enter, ec=%d\n", ec));
  1983. c = my_getc(0);
  1984. if (c == '>' || c == '<') {
  1985. if (c != ec)
  1986. zzerr();
  1987. yylval.i = (ec == '>' ? IOWRITE | IOCAT : IOHERE);
  1988. c = my_getc(0);
  1989. } else
  1990. yylval.i = (ec == '>' ? IOWRITE : IOREAD);
  1991. if (c != '&' || yylval.i == IOHERE)
  1992. unget(c);
  1993. else
  1994. yylval.i |= IODUP;
  1995. }
  1996. static char *tree(unsigned size)
  1997. {
  1998. char *t;
  1999. t = getcell(size);
  2000. if (t == NULL) {
  2001. DBGPRINTF2(("TREE: getcell(%d) failed!\n", size));
  2002. prs("command line too complicated\n");
  2003. fail();
  2004. /* NOTREACHED */
  2005. }
  2006. return t;
  2007. }
  2008. /* VARARGS1 */
  2009. /* ARGSUSED */
  2010. /* -------- exec.c -------- */
  2011. static struct op **find1case(struct op *t, const char *w)
  2012. {
  2013. struct op *t1;
  2014. struct op **tp;
  2015. char **wp;
  2016. char *cp;
  2017. if (t == NULL) {
  2018. DBGPRINTF3(("FIND1CASE: enter, t==NULL, returning.\n"));
  2019. return NULL;
  2020. }
  2021. DBGPRINTF3(("FIND1CASE: enter, t->op_type=%d (%s)\n", t->op_type,
  2022. T_CMD_NAMES[t->op_type]));
  2023. if (t->op_type == TLIST) {
  2024. tp = find1case(t->left, w);
  2025. if (tp != NULL) {
  2026. DBGPRINTF3(("FIND1CASE: found one to the left, returning tp=%p\n", tp));
  2027. return tp;
  2028. }
  2029. t1 = t->right; /* TPAT */
  2030. } else
  2031. t1 = t;
  2032. for (wp = t1->op_words; *wp;) {
  2033. cp = evalstr(*wp++, DOSUB);
  2034. if (cp && gmatch(w, cp)) {
  2035. DBGPRINTF3(("FIND1CASE: returning &t1->left= %p.\n",
  2036. &t1->left));
  2037. return &t1->left;
  2038. }
  2039. }
  2040. DBGPRINTF(("FIND1CASE: returning NULL\n"));
  2041. return NULL;
  2042. }
  2043. static struct op *findcase(struct op *t, const char *w)
  2044. {
  2045. struct op **tp;
  2046. tp = find1case(t, w);
  2047. return tp != NULL ? *tp : NULL;
  2048. }
  2049. /*
  2050. * execute tree
  2051. */
  2052. static int execute(struct op *t, int *pin, int *pout, int no_fork)
  2053. {
  2054. struct op *t1;
  2055. volatile int i, rv, a;
  2056. const char *cp;
  2057. char **wp, **wp2;
  2058. struct var *vp;
  2059. struct op *outtree_save;
  2060. struct brkcon bc;
  2061. #if __GNUC__
  2062. /* Avoid longjmp clobbering */
  2063. (void) &wp;
  2064. #endif
  2065. if (t == NULL) {
  2066. DBGPRINTF4(("EXECUTE: enter, t==null, returning.\n"));
  2067. return 0;
  2068. }
  2069. DBGPRINTF(("EXECUTE: t=%p, t->op_type=%d (%s), t->op_words is %s\n", t,
  2070. t->op_type, T_CMD_NAMES[t->op_type],
  2071. ((t->op_words == NULL) ? "NULL" : t->op_words[0])));
  2072. rv = 0;
  2073. a = areanum++;
  2074. wp2 = t->op_words;
  2075. wp = (wp2 != NULL)
  2076. ? eval(wp2, t->op_type == TCOM ? DOALL : DOALL & ~DOKEY)
  2077. : NULL;
  2078. switch (t->op_type) {
  2079. case TDOT:
  2080. DBGPRINTF3(("EXECUTE: TDOT\n"));
  2081. outtree_save = outtree;
  2082. newfile(evalstr(t->op_words[0], DOALL));
  2083. t->left = dowholefile(TLIST /*, 0*/);
  2084. t->right = NULL;
  2085. outtree = outtree_save;
  2086. if (t->left)
  2087. rv = execute(t->left, pin, pout, /* no_fork: */ 0);
  2088. if (t->right)
  2089. rv = execute(t->right, pin, pout, /* no_fork: */ 0);
  2090. break;
  2091. case TPAREN:
  2092. rv = execute(t->left, pin, pout, /* no_fork: */ 0);
  2093. break;
  2094. case TCOM:
  2095. rv = forkexec(t, pin, pout, no_fork, wp);
  2096. break;
  2097. case TPIPE:
  2098. {
  2099. int pv[2];
  2100. rv = openpipe(pv);
  2101. if (rv < 0)
  2102. break;
  2103. pv[0] = remap(pv[0]);
  2104. pv[1] = remap(pv[1]);
  2105. (void) execute(t->left, pin, pv, /* no_fork: */ 0);
  2106. rv = execute(t->right, pv, pout, /* no_fork: */ 0);
  2107. }
  2108. break;
  2109. case TLIST:
  2110. (void) execute(t->left, pin, pout, /* no_fork: */ 0);
  2111. rv = execute(t->right, pin, pout, /* no_fork: */ 0);
  2112. break;
  2113. case TASYNC:
  2114. {
  2115. smallint hinteractive = interactive;
  2116. DBGPRINTF7(("EXECUTE: TASYNC clause, calling vfork()...\n"));
  2117. i = vfork();
  2118. if (i == 0) { /* child */
  2119. signal(SIGINT, SIG_IGN);
  2120. signal(SIGQUIT, SIG_IGN);
  2121. if (interactive)
  2122. signal(SIGTERM, SIG_DFL);
  2123. interactive = 0;
  2124. if (pin == NULL) {
  2125. close(0);
  2126. xopen(bb_dev_null, O_RDONLY);
  2127. }
  2128. _exit(execute(t->left, pin, pout, /* no_fork: */ 1));
  2129. }
  2130. interactive = hinteractive;
  2131. if (i != -1) {
  2132. setval(lookup("!"), putn(i));
  2133. closepipe(pin);
  2134. if (interactive) {
  2135. prs(putn(i));
  2136. prs("\n");
  2137. }
  2138. } else
  2139. rv = -1;
  2140. setstatus(rv);
  2141. }
  2142. break;
  2143. case TOR:
  2144. case TAND:
  2145. rv = execute(t->left, pin, pout, /* no_fork: */ 0);
  2146. t1 = t->right;
  2147. if (t1 != NULL && (rv == 0) == (t->op_type == TAND))
  2148. rv = execute(t1, pin, pout, /* no_fork: */ 0);
  2149. break;
  2150. case TFOR:
  2151. if (wp == NULL) {
  2152. wp = dolv + 1;
  2153. i = dolc;
  2154. if (i < 0)
  2155. i = 0;
  2156. } else {
  2157. i = -1;
  2158. while (*wp++ != NULL)
  2159. continue;
  2160. }
  2161. vp = lookup(t->str);
  2162. while (setjmp(bc.brkpt))
  2163. if (isbreak)
  2164. goto broken;
  2165. /* Restore areanum value. It may be incremented by execute()
  2166. * below, and then "continue" may jump back to setjmp above */
  2167. areanum = a + 1;
  2168. freearea(areanum + 1);
  2169. brkset(&bc);
  2170. for (t1 = t->left; i-- && *wp != NULL;) {
  2171. setval(vp, *wp++);
  2172. rv = execute(t1, pin, pout, /* no_fork: */ 0);
  2173. }
  2174. brklist = brklist->nextlev;
  2175. break;
  2176. case TWHILE:
  2177. case TUNTIL:
  2178. while (setjmp(bc.brkpt))
  2179. if (isbreak)
  2180. goto broken;
  2181. /* Restore areanum value. It may be incremented by execute()
  2182. * below, and then "continue" may jump back to setjmp above */
  2183. areanum = a + 1;
  2184. freearea(areanum + 1);
  2185. brkset(&bc);
  2186. t1 = t->left;
  2187. while ((execute(t1, pin, pout, /* no_fork: */ 0) == 0) == (t->op_type == TWHILE))
  2188. rv = execute(t->right, pin, pout, /* no_fork: */ 0);
  2189. brklist = brklist->nextlev;
  2190. break;
  2191. case TIF:
  2192. case TELIF:
  2193. if (t->right != NULL) {
  2194. rv = !execute(t->left, pin, pout, /* no_fork: */ 0) ?
  2195. execute(t->right->left, pin, pout, /* no_fork: */ 0) :
  2196. execute(t->right->right, pin, pout, /* no_fork: */ 0);
  2197. }
  2198. break;
  2199. case TCASE:
  2200. cp = evalstr(t->str, DOSUB | DOTRIM);
  2201. if (cp == NULL)
  2202. cp = "";
  2203. DBGPRINTF7(("EXECUTE: TCASE, t->str is %s, cp is %s\n",
  2204. ((t->str == NULL) ? "NULL" : t->str),
  2205. ((cp == NULL) ? "NULL" : cp)));
  2206. t1 = findcase(t->left, cp);
  2207. if (t1 != NULL) {
  2208. DBGPRINTF7(("EXECUTE: TCASE, calling execute(t=%p, t1=%p)...\n", t, t1));
  2209. rv = execute(t1, pin, pout, /* no_fork: */ 0);
  2210. DBGPRINTF7(("EXECUTE: TCASE, back from execute(t=%p, t1=%p)...\n", t, t1));
  2211. }
  2212. break;
  2213. case TBRACE:
  2214. /*
  2215. iopp = t->ioact;
  2216. if (i)
  2217. while (*iopp)
  2218. if (iosetup(*iopp++, pin!=NULL, pout!=NULL)) {
  2219. rv = -1;
  2220. break;
  2221. }
  2222. */
  2223. if (rv >= 0) {
  2224. t1 = t->left;
  2225. if (t1) {
  2226. rv = execute(t1, pin, pout, /* no_fork: */ 0);
  2227. }
  2228. }
  2229. break;
  2230. };
  2231. broken:
  2232. // Restoring op_words is most likely not needed now: see comment in forkexec()
  2233. // (also take a look at exec builtin (doexec) - it touches t->op_words)
  2234. t->op_words = wp2;
  2235. isbreak = 0;
  2236. freehere(areanum);
  2237. freearea(areanum);
  2238. areanum = a;
  2239. if (interactive && intr) {
  2240. closeall();
  2241. fail();
  2242. }
  2243. i = trapset;
  2244. if (i != 0) {
  2245. trapset = 0;
  2246. runtrap(i);
  2247. }
  2248. DBGPRINTF(("EXECUTE: returning from t=%p, rv=%d\n", t, rv));
  2249. return rv;
  2250. }
  2251. static builtin_func_ptr inbuilt(const char *s)
  2252. {
  2253. const struct builtincmd *bp;
  2254. for (bp = builtincmds; bp->name; bp++)
  2255. if (strcmp(bp->name, s) == 0)
  2256. return bp->builtinfunc;
  2257. return NULL;
  2258. }
  2259. static int forkexec(struct op *t, int *pin, int *pout, int no_fork, char **wp)
  2260. {
  2261. pid_t newpid;
  2262. int i;
  2263. builtin_func_ptr bltin = NULL;
  2264. const char *bltin_name = NULL;
  2265. const char *cp;
  2266. struct ioword **iopp;
  2267. int resetsig;
  2268. char **owp;
  2269. int forked;
  2270. int *hpin = pin;
  2271. int *hpout = pout;
  2272. char *hwp;
  2273. smallint hinteractive;
  2274. smallint hintr;
  2275. smallint hexecflg;
  2276. struct brkcon *hbrklist;
  2277. #if __GNUC__
  2278. /* Avoid longjmp clobbering */
  2279. (void) &pin;
  2280. (void) &pout;
  2281. (void) &wp;
  2282. (void) &bltin;
  2283. (void) &cp;
  2284. (void) &resetsig;
  2285. (void) &owp;
  2286. #endif
  2287. DBGPRINTF(("FORKEXEC: t=%p, pin %p, pout %p, no_fork %d\n", t, pin,
  2288. pout, no_fork));
  2289. DBGPRINTF7(("FORKEXEC: t->op_words is %s\n",
  2290. ((t->op_words == NULL) ? "NULL" : t->op_words[0])));
  2291. owp = wp;
  2292. resetsig = 0;
  2293. if (t->op_type == TCOM) {
  2294. while (*wp++ != NULL)
  2295. continue;
  2296. cp = *wp;
  2297. /* strip all initial assignments */
  2298. /* FIXME: not correct wrt PATH=yyy command etc */
  2299. if (FLAG['x']) {
  2300. DBGPRINTF9(("FORKEXEC: echo'ing, cp=%p, wp=%p, owp=%p\n",
  2301. cp, wp, owp));
  2302. echo(cp ? wp : owp);
  2303. }
  2304. if (cp == NULL) {
  2305. if (t->ioact == NULL) {
  2306. while ((cp = *owp++) != NULL && assign(cp, COPYV))
  2307. continue;
  2308. DBGPRINTF(("FORKEXEC: returning setstatus(0)\n"));
  2309. return setstatus(0);
  2310. }
  2311. } else { /* cp != NULL */
  2312. bltin_name = cp;
  2313. bltin = inbuilt(cp);
  2314. }
  2315. }
  2316. forked = 0;
  2317. // We were pointing t->op_words to temporary (expanded) arg list:
  2318. // t->op_words = wp;
  2319. // and restored it later (in execute()), but "break"
  2320. // longjmps away (at "Run builtin" below), leaving t->op_words clobbered!
  2321. // See http://bugs.busybox.net/view.php?id=846.
  2322. // Now we do not touch t->op_words, but separately pass wp as param list
  2323. // to builtins
  2324. DBGPRINTF(("FORKEXEC: bltin %p, no_fork %d, owp %p\n", bltin,
  2325. no_fork, owp));
  2326. /* Don't fork if it is a lone builtin (not in pipe)
  2327. * OR we are told to _not_ fork */
  2328. if ((!bltin || pin || pout) /* not lone bltin AND */
  2329. && !no_fork /* not told to avoid fork */
  2330. ) {
  2331. /* Save values in case child alters them after vfork */
  2332. hpin = pin;
  2333. hpout = pout;
  2334. hwp = *wp;
  2335. hinteractive = interactive;
  2336. hintr = intr;
  2337. hbrklist = brklist;
  2338. hexecflg = execflg;
  2339. DBGPRINTF3(("FORKEXEC: calling vfork()...\n"));
  2340. newpid = vfork();
  2341. if (newpid == -1) {
  2342. DBGPRINTF(("FORKEXEC: ERROR, can't vfork()!\n"));
  2343. return -1;
  2344. }
  2345. if (newpid > 0) { /* Parent */
  2346. /* Restore values */
  2347. pin = hpin;
  2348. pout = hpout;
  2349. *wp = hwp;
  2350. interactive = hinteractive;
  2351. intr = hintr;
  2352. brklist = hbrklist;
  2353. execflg = hexecflg;
  2354. closepipe(pin);
  2355. return (pout == NULL ? setstatus(waitfor(newpid, 0)) : 0);
  2356. }
  2357. /* Child */
  2358. DBGPRINTF(("FORKEXEC: child process, bltin=%p (%s)\n", bltin, bltin_name));
  2359. if (interactive) {
  2360. signal(SIGINT, SIG_IGN);
  2361. signal(SIGQUIT, SIG_IGN);
  2362. resetsig = 1;
  2363. }
  2364. interactive = 0;
  2365. intr = 0;
  2366. forked = 1;
  2367. brklist = 0;
  2368. execflg = 0;
  2369. }
  2370. if (owp)
  2371. while ((cp = *owp++) != NULL && assign(cp, COPYV))
  2372. if (!bltin)
  2373. export(lookup(cp));
  2374. if (pin) { /* NB: close _first_, then move fds! */
  2375. close(pin[1]);
  2376. xmove_fd(pin[0], 0);
  2377. }
  2378. if (pout) {
  2379. close(pout[0]);
  2380. xmove_fd(pout[1], 1);
  2381. }
  2382. iopp = t->ioact;
  2383. if (iopp) {
  2384. if (bltin && bltin != doexec) {
  2385. prs(bltin_name);
  2386. err(": can't redirect shell command");
  2387. if (forked)
  2388. _exit(-1);
  2389. return -1;
  2390. }
  2391. while (*iopp) {
  2392. if (iosetup(*iopp++, pin != NULL, pout != NULL)) {
  2393. /* system-detected error */
  2394. if (forked)
  2395. _exit(-1);
  2396. return -1;
  2397. }
  2398. }
  2399. }
  2400. if (bltin) {
  2401. if (forked || pin || pout) {
  2402. /* Builtin in pipe: disallowed */
  2403. /* TODO: allow "exec"? */
  2404. prs(bltin_name);
  2405. err(": can't run builtin as part of pipe");
  2406. if (forked)
  2407. _exit(-1);
  2408. return -1;
  2409. }
  2410. /* Run builtin */
  2411. i = setstatus(bltin(t, wp));
  2412. if (forked)
  2413. _exit(i);
  2414. DBGPRINTF(("FORKEXEC: returning i=%d\n", i));
  2415. return i;
  2416. }
  2417. /* should use FIOCEXCL */
  2418. for (i = FDBASE; i < NOFILE; i++)
  2419. close(i);
  2420. if (resetsig) {
  2421. signal(SIGINT, SIG_DFL);
  2422. signal(SIGQUIT, SIG_DFL);
  2423. }
  2424. if (t->op_type == TPAREN)
  2425. _exit(execute(t->left, NOPIPE, NOPIPE, /* no_fork: */ 1));
  2426. if (wp[0] == NULL)
  2427. _exit(EXIT_SUCCESS);
  2428. cp = rexecve(wp[0], wp, makenv(0, NULL));
  2429. prs(wp[0]);
  2430. prs(": ");
  2431. err(cp);
  2432. if (!execflg)
  2433. trap[0] = NULL;
  2434. DBGPRINTF(("FORKEXEC: calling leave(), pid=%d\n", getpid()));
  2435. leave();
  2436. /* NOTREACHED */
  2437. return 0;
  2438. }
  2439. /*
  2440. * 0< 1> are ignored as required
  2441. * within pipelines.
  2442. */
  2443. static int iosetup(struct ioword *iop, int pipein, int pipeout)
  2444. {
  2445. int u = -1;
  2446. char *cp = NULL;
  2447. const char *msg;
  2448. DBGPRINTF(("IOSETUP: iop %p, pipein %i, pipeout %i\n", iop,
  2449. pipein, pipeout));
  2450. if (iop->io_fd == IODEFAULT) /* take default */
  2451. iop->io_fd = iop->io_flag & (IOREAD | IOHERE) ? 0 : 1;
  2452. if (pipein && iop->io_fd == 0)
  2453. return 0;
  2454. if (pipeout && iop->io_fd == 1)
  2455. return 0;
  2456. msg = iop->io_flag & (IOREAD | IOHERE) ? "open" : "create";
  2457. if ((iop->io_flag & IOHERE) == 0) {
  2458. cp = iop->io_name; /* huh?? */
  2459. cp = evalstr(cp, DOSUB | DOTRIM);
  2460. if (cp == NULL)
  2461. return 1;
  2462. }
  2463. if (iop->io_flag & IODUP) {
  2464. if (cp[1] || (!isdigit(*cp) && *cp != '-')) {
  2465. prs(cp);
  2466. err(": illegal >& argument");
  2467. return 1;
  2468. }
  2469. if (*cp == '-')
  2470. iop->io_flag = IOCLOSE;
  2471. iop->io_flag &= ~(IOREAD | IOWRITE);
  2472. }
  2473. switch (iop->io_flag) {
  2474. case IOREAD:
  2475. u = open(cp, O_RDONLY);
  2476. break;
  2477. case IOHERE:
  2478. case IOHERE | IOXHERE:
  2479. u = herein(iop->io_name, iop->io_flag & IOXHERE);
  2480. cp = (char*)"here file";
  2481. break;
  2482. case IOWRITE | IOCAT:
  2483. u = open(cp, O_WRONLY);
  2484. if (u >= 0) {
  2485. lseek(u, (long) 0, SEEK_END);
  2486. break;
  2487. }
  2488. /* fall through to creation if >>file doesn't exist */
  2489. case IOWRITE:
  2490. u = creat(cp, 0666);
  2491. break;
  2492. case IODUP:
  2493. u = dup2(*cp - '0', iop->io_fd);
  2494. break;
  2495. case IOCLOSE:
  2496. close(iop->io_fd);
  2497. return 0;
  2498. }
  2499. if (u < 0) {
  2500. prs(cp);
  2501. prs(": can't ");
  2502. warn(msg);
  2503. return 1;
  2504. }
  2505. xmove_fd(u, iop->io_fd);
  2506. return 0;
  2507. }
  2508. /*
  2509. * Enter a new loop level (marked for break/continue).
  2510. */
  2511. static void brkset(struct brkcon *bc)
  2512. {
  2513. bc->nextlev = brklist;
  2514. brklist = bc;
  2515. }
  2516. /*
  2517. * Wait for the last process created.
  2518. * Print a message for each process found
  2519. * that was killed by a signal.
  2520. * Ignore interrupt signals while waiting
  2521. * unless `canintr' is true.
  2522. */
  2523. static int waitfor(int lastpid, int canintr)
  2524. {
  2525. int pid, rv;
  2526. int s;
  2527. smallint oheedint = heedint;
  2528. heedint = 0;
  2529. rv = 0;
  2530. do {
  2531. pid = wait(&s);
  2532. if (pid == -1) {
  2533. if (errno != EINTR || canintr)
  2534. break;
  2535. } else {
  2536. rv = WAITSIG(s);
  2537. if (rv != 0) {
  2538. if (rv < ARRAY_SIZE(signame)) {
  2539. if (signame[rv] != NULL) {
  2540. if (pid != lastpid) {
  2541. prn(pid);
  2542. prs(": ");
  2543. }
  2544. prs(signame[rv]);
  2545. }
  2546. } else {
  2547. if (pid != lastpid) {
  2548. prn(pid);
  2549. prs(": ");
  2550. }
  2551. prs("Signal ");
  2552. prn(rv);
  2553. prs(" ");
  2554. }
  2555. if (WAITCORE(s))
  2556. prs(" - core dumped");
  2557. if (rv >= ARRAY_SIZE(signame) || signame[rv])
  2558. prs("\n");
  2559. rv |= 0x80;
  2560. } else
  2561. rv = WAITVAL(s);
  2562. }
  2563. } while (pid != lastpid);
  2564. heedint = oheedint;
  2565. if (intr) {
  2566. if (interactive) {
  2567. if (canintr)
  2568. intr = 0;
  2569. } else {
  2570. if (exstat == 0)
  2571. exstat = rv;
  2572. onintr(0);
  2573. }
  2574. }
  2575. return rv;
  2576. }
  2577. static int setstatus(int s)
  2578. {
  2579. exstat = s;
  2580. setval(lookup("?"), putn(s));
  2581. return s;
  2582. }
  2583. /*
  2584. * PATH-searching interface to execve.
  2585. * If getenv("PATH") were kept up-to-date,
  2586. * execvp might be used.
  2587. */
  2588. static const char *rexecve(char *c, char **v, char **envp)
  2589. {
  2590. const char *sp;
  2591. char *tp;
  2592. int asis = 0;
  2593. char *name = c;
  2594. if (ENABLE_FEATURE_SH_STANDALONE) {
  2595. if (find_applet_by_name(name) >= 0) {
  2596. /* We have to exec here since we vforked. Running
  2597. * run_applet_and_exit() won't work and bad things
  2598. * will happen. */
  2599. execve(bb_busybox_exec_path, v, envp);
  2600. }
  2601. }
  2602. DBGPRINTF(("REXECVE: c=%p, v=%p, envp=%p\n", c, v, envp));
  2603. sp = any('/', c) ? "" : path->value;
  2604. asis = (*sp == '\0');
  2605. while (asis || *sp != '\0') {
  2606. asis = 0;
  2607. tp = global_env.linep;
  2608. for (; *sp != '\0'; tp++) {
  2609. *tp = *sp++;
  2610. if (*tp == ':') {
  2611. asis = (*sp == '\0');
  2612. break;
  2613. }
  2614. }
  2615. if (tp != global_env.linep)
  2616. *tp++ = '/';
  2617. strcpy(tp, c);
  2618. DBGPRINTF3(("REXECVE: global_env.linep is %s\n", global_env.linep));
  2619. execve(global_env.linep, v, envp);
  2620. switch (errno) {
  2621. case ENOEXEC:
  2622. /* File is executable but file format isnt recognized */
  2623. /* Run it as a shell script */
  2624. /* (execve above didnt do it itself, unlike execvp) */
  2625. *v = global_env.linep;
  2626. v--;
  2627. tp = *v;
  2628. *v = (char*)DEFAULT_SHELL;
  2629. execve(DEFAULT_SHELL, v, envp);
  2630. *v = tp;
  2631. return "no shell";
  2632. case ENOMEM:
  2633. return (char *) bb_msg_memory_exhausted;
  2634. case E2BIG:
  2635. return "argument list too long";
  2636. }
  2637. }
  2638. if (errno == ENOENT) {
  2639. exstat = 127; /* standards require this */
  2640. return "not found";
  2641. }
  2642. exstat = 126; /* mimic bash */
  2643. return "can't execute";
  2644. }
  2645. /*
  2646. * Run the command produced by generator `f'
  2647. * applied to stream `arg'.
  2648. */
  2649. static int run(struct ioarg *argp, int (*f) (struct ioarg *))
  2650. {
  2651. struct op *otree;
  2652. struct wdblock *swdlist;
  2653. struct wdblock *siolist;
  2654. jmp_buf ev, rt;
  2655. xint *ofail;
  2656. int rv;
  2657. #if __GNUC__
  2658. /* Avoid longjmp clobbering */
  2659. (void) &rv;
  2660. #endif
  2661. DBGPRINTF(("RUN: enter, areanum %d, outtree %p, failpt %p\n",
  2662. areanum, outtree, failpt));
  2663. areanum++;
  2664. swdlist = wdlist;
  2665. siolist = iolist;
  2666. otree = outtree;
  2667. ofail = failpt;
  2668. rv = -1;
  2669. errpt = ev;
  2670. if (newenv(setjmp(errpt)) == 0) {
  2671. wdlist = NULL;
  2672. iolist = NULL;
  2673. pushio(argp, f);
  2674. global_env.iobase = global_env.iop;
  2675. yynerrs = 0;
  2676. failpt = rt;
  2677. if (setjmp(failpt) == 0 && yyparse() == 0)
  2678. rv = execute(outtree, NOPIPE, NOPIPE, /* no_fork: */ 0);
  2679. quitenv();
  2680. } else {
  2681. DBGPRINTF(("RUN: error from newenv()!\n"));
  2682. }
  2683. wdlist = swdlist;
  2684. iolist = siolist;
  2685. failpt = ofail;
  2686. outtree = otree;
  2687. freearea(areanum--);
  2688. return rv;
  2689. }
  2690. /* -------- do.c -------- */
  2691. /*
  2692. * built-in commands: doX
  2693. */
  2694. static int dohelp(struct op *t UNUSED_PARAM, char **args UNUSED_PARAM)
  2695. {
  2696. int col;
  2697. const struct builtincmd *x;
  2698. printf(
  2699. "Built-in commands:\n"
  2700. "------------------\n");
  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, STDOUT_FILENO);
  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. xwrite_str(out, s);
  3104. xwrite(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. */