ckucmd.c 219 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140
  1. #include "ckcsym.h"
  2. char *cmdv = "Command package 9.0.176, 18 September 2020";
  3. /* C K U C M D -- Interactive command package for Unix */
  4. /* (In reality, it's for all platforms, not just Unix) */
  5. /*
  6. Author: Frank da Cruz (fdc@columbia.edu),
  7. Formerly of Columbia University Academic Information Systems, New York City.
  8. Since 1 July 2011, Open Source Kermit Project.
  9. Copyright (C) 1985, 2020,
  10. Trustees of Columbia University in the City of New York.
  11. All rights reserved. See the C-Kermit COPYING.TXT file or the
  12. copyright text in the ckcmai.c module for disclaimer and permissions.
  13. */
  14. #define FUNCTIONTEST
  15. #define TOKPRECHECK
  16. #define DOCHKVAR
  17. /* Command-terminal-to-C-Kermit character mask */
  18. #ifdef OS2 /* K95 */
  19. int cmdmsk = 255; /* (always was 255) */
  20. #else /* All others... */
  21. int cmdmsk = 255; /* 31 Dec 2000 (was 127) */
  22. #endif /* OS2 */
  23. #ifdef BS_DIRSEP /* Directory separator is backslash */
  24. #undef BS_DIRSEP
  25. #endif /* BS_DIRSEP */
  26. #ifdef OS2
  27. #define BS_DIRSEP
  28. #endif /* BS_DIRSEP */
  29. #define CKUCMD_C
  30. #include "ckcdeb.h" /* Formats for debug(), etc. */
  31. #include "ckcker.h" /* Needed for BIGBUFOK definition */
  32. #include "ckcnet.h" /* Needed for server-side Telnet */
  33. #include "ckucmd.h" /* Needed for everything */
  34. #include "ckuusr.h" /* Needed for prompt length */
  35. #ifndef NOARROWKEYS
  36. #ifndef NOESCSEQ
  37. #ifdef VMSORUNIX
  38. #define USE_ARROWKEYS /* Use arrow keys for command recall */
  39. #endif /* VMSORUNIX */
  40. #endif /* NOESCSEQ */
  41. #endif /* NOARROWKEYS */
  42. #undef CKUCMD_C
  43. _PROTOTYP( int unhex, (char) );
  44. _PROTOTYP( static VOID cmdclrscn, (void) );
  45. #ifdef CKLEARN
  46. _PROTOTYP( VOID learncmd, (char *) );
  47. #endif /* CKLEARN */
  48. static char *moname[] = {
  49. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  50. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  51. };
  52. static char *fullmonthname[] = {
  53. "January", "February", "March", "April", "May", "June",
  54. "July", "August", "September", "October", "November", "December"
  55. };
  56. struct keytab cmonths[] = {
  57. { "april", 4, 0 },
  58. { "august", 8, 0 },
  59. { "december", 12, 0 },
  60. { "february", 2, 0 },
  61. { "january", 1, 0 },
  62. { "july", 7, 0 },
  63. { "june", 6, 0 },
  64. { "march", 3, 0 },
  65. { "may", 5, 0 },
  66. { "november", 11, 0 },
  67. { "october", 10, 0 },
  68. { "september", 9, 0 }
  69. };
  70. #ifndef NOICP /* The rest only if interactive command parsing selected */
  71. #ifndef NOSPL
  72. _PROTOTYP( int chkvar, (char *) );
  73. extern int askflag, echostars;
  74. #endif /* NOSPL */
  75. #ifdef CKROOT
  76. extern int ckrooterr;
  77. #endif /* CKROOT */
  78. #ifdef IKSD
  79. extern int inserver;
  80. #endif /* IKSD */
  81. int cmfldflgs = 0; /* Flags for cmfld() */
  82. int cmkwflgs = 0; /* Flags from last keyword parse */
  83. static int nomsg = 0;
  84. static int blocklvl = 0; /* Block nesting level */
  85. static int linebegin = 0; /* Flag for at start of a line */
  86. static int quoting = 1; /* Quoting is allowed */
  87. static int swarg = 0; /* Parsing a switch argument */
  88. static int xcmfdb = 0; /* Flag for parsing chained fdbs... */
  89. static int chsrc = 0; /* Source of character, 1 = tty */
  90. static int newcmd = 0; /* See addcmd() */
  91. #ifdef BS_DIRSEP
  92. static int dirnamflg = 0;
  93. #endif /* BS_DIRSEP */
  94. /*
  95. Modeled after the DECSYSTEM-20 command parser (the COMND JSYS), RIP. Features:
  96. . parses and verifies keywords, filenames, text strings, numbers, other data
  97. . displays appropriate menu or help message when user types "?"
  98. . does keyword and filename completion when user types ESC or TAB
  99. . does partial keyword and filename completion
  100. . accepts any unique abbreviation for a keyword
  101. . allows keywords to have attributes, like "invisible" and "abbreviation"
  102. . can supply defaults for fields omitted by user
  103. . provides command retry and recall
  104. . provides character, word, and line deletion (but only from the end)
  105. . accepts input from keyboard, command files, macros, or redirected stdin
  106. . allows for full or half duplex operation, character or line input
  107. . allows \-escapes for special characters
  108. . allows specification of a user exit to expand variables, etc.
  109. . settable prompt, protected from deletion, dynamically re-evaluated each time
  110. . allows chained parse functions.
  111. Functions:
  112. cmsetp - Set prompt (cmprom is prompt string)
  113. cmsavp - Save current prompt
  114. cmgetp = Get current prompt
  115. prompt - Issue prompt
  116. cmini - Clear the command buffer (before parsing a new command)
  117. cmres - Reset command buffer pointers (before reparsing)
  118. cmkey - Parse a keyword or token (also cmkey2)
  119. cmswi - Parse a switch
  120. cmnum - Parse a number
  121. cmifi - Parse an input file name
  122. cmofi - Parse an output file name (also cmifip, cmifi2, ...)
  123. cmdir - Parse a directory name (also cmdirp)
  124. cmfld - Parse an arbitrary field
  125. cmtxt - Parse a text string
  126. cmdate - Parse a date-time string
  127. cmcfm - Parse command confirmation (end of line)
  128. cmfdb - Parse any of a list of the foregoing (chained parse functions)
  129. Return codes:
  130. -9: like -2 except this module already printed the error message
  131. -3: no input provided when required
  132. -2: input was invalid (e.g. not a number when a number was required)
  133. -1: reparse required (user deleted into a preceding field)
  134. 0 or greater: success
  135. See individual functions for greater detail.
  136. Before using these routines, the caller should #include "ckucmd.h" and set the
  137. program's prompt by calling cmsetp(). If the file parsing functions cmifi,
  138. cmofi, or cmdir are to be used, this module must be linked with a ck?fio file
  139. system support module for the appropriate system, e.g. ckufio for Unix. If
  140. the caller puts the terminal in character wakeup ("cbreak") mode with no echo,
  141. then these functions will provide line editing -- character, word, and line
  142. deletion, as well as keyword and filename completion upon ESC and help
  143. strings, keyword, or file menus upon '?'. If the caller puts the terminal
  144. into character wakeup/noecho mode, care should be taken to restore it before
  145. exit from or interruption of the program. If the character wakeup mode is not
  146. set, the system's own line editor may be used.
  147. NOTE: Contrary to expectations, many #ifdef's have been added to this module.
  148. Any operation requiring an #ifdef (like clear screen, get character from
  149. keyboard, erase character from screen, etc) should eventually be turned into a
  150. call to a function that is defined in ck?tio.c, but then all the ck?tio.c
  151. modules would have to be changed...
  152. */
  153. /* Includes */
  154. #include "ckcker.h"
  155. #include "ckcasc.h" /* ASCII character symbols */
  156. #include "ckucmd.h" /* Command parsing definitions */
  157. #ifdef OSF13
  158. #ifdef CK_ANSIC
  159. #ifdef _NO_PROTO
  160. #undef _NO_PROTO
  161. #endif /* _NO_PROTO */
  162. #endif /* CK_ANSIC */
  163. #endif /* OSF13 */
  164. #ifndef HPUXPRE65
  165. #include <errno.h> /* Error number symbols */
  166. #else
  167. #ifndef ERRNO_INCLUDED
  168. #include <errno.h> /* Error number symbols */
  169. #endif /* ERRNO_INCLUDED */
  170. #endif /* HPUXPRE65 */
  171. #ifdef OS2
  172. #ifndef NT
  173. #define INCL_NOPM
  174. #define INCL_VIO /* Needed for ckocon.h */
  175. #include <os2.h>
  176. #undef COMMENT
  177. #else
  178. #define APIRET ULONG
  179. #include <windows.h>
  180. #endif /* NT */
  181. #include "ckocon.h"
  182. #include <io.h>
  183. #endif /* OS2 */
  184. #ifdef OSK
  185. #define cc ccount /* OS-9/68K compiler bug */
  186. #endif /* OSK */
  187. #ifdef GEMDOS /* Atari ST */
  188. #ifdef putchar
  189. #undef putchar
  190. #endif /* putchar */
  191. #define putchar(x) conoc(x)
  192. #endif /* GEMDOS */
  193. #ifdef CK_AUTODL
  194. extern int cmdadl, justone;
  195. #endif /* CK_AUTODL */
  196. extern int timelimit, nzxopts, nopush, nolocal, xcmdsrc, keepallchars;
  197. #ifdef CKSYSLOG
  198. #ifdef UNIX
  199. #ifdef CKXPRINTF /* Our printf macro conflicts with */
  200. #undef printf /* use of "printf" in syslog.h */
  201. #endif /* CKXPRINTF */
  202. #ifdef RTAIX
  203. #include <sys/syslog.h>
  204. #else /* RTAIX */
  205. #include <syslog.h>
  206. #endif /* RTAIX */
  207. #ifdef CKXPRINTF
  208. #define printf ckxprintf
  209. #endif /* CKXPRINTF */
  210. #endif /* UNIX */
  211. #endif /* CKSYSLOG */
  212. /* Local variables */
  213. static
  214. int psetf = 0, /* Flag that prompt has been set */
  215. cc = 0, /* Character count */
  216. dpx = 0, /* Duplex (0 = full) */
  217. inword = 0; /* In the middle of getting a word */
  218. char *dfprom = "Command? "; /* Default prompt */
  219. #ifndef NOLASTFILE
  220. char *lastfile = NULL; /* Last filespec */
  221. static char *tmplastfile = NULL; /* Last filespec candidate */
  222. #endif /* NOLASTFILE */
  223. int cmflgs; /* Command flags */
  224. int cmfsav; /* A saved version of them */
  225. static char pushc = NUL;
  226. static char brkchar = NUL;
  227. #define CMDEFAULT 1023
  228. static char cmdefault[CMDEFAULT+1];
  229. #ifdef DCMDBUF
  230. char *cmdbuf = NULL; /* Command buffer */
  231. char *savbuf = NULL; /* Buffer to save copy of command */
  232. char *atmbuf = NULL; /* Atom buffer - for current field */
  233. char *atxbuf = NULL; /* For expanding the atom buffer */
  234. char *prevcmd = NULL;
  235. static char *atybuf = NULL; /* For copying atom buffer */
  236. static char *filbuf = NULL; /* File name buffer */
  237. static char *cmprom = NULL; /* Program's prompt */
  238. static char *cmprxx = NULL; /* Program's prompt, unevaluated */
  239. #ifdef CK_RECALL
  240. /*
  241. Command recall is available only if we can make profligate use of malloc().
  242. */
  243. #define R_MAX 10 /* How many commands to save */
  244. int cm_recall = R_MAX; /* Size of command recall buffer */
  245. int on_recall = 1; /* Recall feature is ON */
  246. static int no_recall = 0; /* Recall OFF for this cmd only */
  247. static int force_add = 0; /* Force cmd into recall buffer */
  248. static int last_recall = -1; /* Last recall-related action */
  249. /*
  250. -1 = none
  251. 0 = CR (a command was entered)
  252. 1 = Up
  253. 2 = Down
  254. */
  255. int in_recall = 0; /* Recall buffers are init'd */
  256. static int
  257. current = -1, /* Pointer to current command */
  258. rlast = -1; /* Index of last command in buffer */
  259. static char **recall = NULL; /* Array of recall buffer pointers */
  260. #endif /* CK_RECALL */
  261. #else /* !DCMDBUF */
  262. char cmdbuf[CMDBL+4]; /* Command buffer */
  263. char savbuf[CMDBL+4]; /* Buffer to save copy of command */
  264. char atmbuf[ATMBL+4]; /* Atom buffer */
  265. char atxbuf[CMDBL+4]; /* For expanding the atom buffer */
  266. char prevcmd[CMDBL+4]; /* For displaying the last command */
  267. static char atybuf[ATMBL+4]; /* For copying atom buffer */
  268. static char filbuf[ATMBL+4]; /* File name buffer */
  269. static char cmprom[PROMPTL+1]; /* Program's prompt */
  270. static char cmprxx[PROMPTL+1]; /* Program's prompt, unevaluated */
  271. #endif /* DCMDBUF */
  272. /* Command buffer pointers */
  273. #define PPVLEN VNAML /* 20080305 Wolfram Sang (was 24) */
  274. char ppvnambuf[PPVLEN+1] = { NUL, NUL };
  275. char * cmbptr = NULL; /* Current position (for export) */
  276. static char *bp, /* Current command buffer position */
  277. *pp, /* Start of current field */
  278. *np; /* Start of next field */
  279. static int ungw, /* For ungetting words */
  280. atxn; /* Expansion buffer (atxbuf) length */
  281. #ifdef OS2
  282. extern int wideresult;
  283. #endif /* OS2 */
  284. extern int cmd_cols, cmd_rows, local, quiet;
  285. #ifdef TNCODE
  286. #ifdef IAC
  287. #undef IAC
  288. #endif /* IAC */
  289. #define IAC 255
  290. #endif /* TNCODE */
  291. _PROTOTYP( static int gtword, (int) );
  292. _PROTOTYP( static int addbuf, (char *) );
  293. _PROTOTYP( static int setatm, (char *, int) );
  294. _PROTOTYP( static VOID cmdnewl, (char) );
  295. _PROTOTYP( static VOID cmdchardel, (void) );
  296. _PROTOTYP( static VOID cmdecho, (char, int) );
  297. _PROTOTYP( static int test, (int, int) );
  298. #ifdef GEMDOS
  299. _PROTOTYP( extern char *strchr, (char *, int) );
  300. #endif /* GEMDOS */
  301. extern char * dftty;
  302. /* The following are for use with chained FDB's */
  303. static int crflag = 0; /* Carriage return was typed */
  304. static int qmflag = 0; /* Question mark was typed */
  305. static int esflag = 0; /* Escape was typed */
  306. /* Directory separator */
  307. #ifdef GEMDOS
  308. static char dirsep = '\\';
  309. #else
  310. #ifdef datageneral
  311. static char dirsep = ':';
  312. #else
  313. #ifdef MAC
  314. static char dirsep = ':';
  315. #else
  316. #ifdef VMS
  317. static char dirsep = '.';
  318. #else
  319. #ifdef STRATUS
  320. static char dirsep = '>';
  321. #else
  322. static char dirsep = '/'; /* UNIX, OS/2, OS-9, Amiga, etc. */
  323. #endif /* STRATUS */
  324. #endif /* VMS */
  325. #endif /* MAC */
  326. #endif /* datageneral */
  327. #endif /* GEMDOS */
  328. /* H A S N O P A T H */
  329. /* Returns 0 if filespec s includes any path segments; 1 if it doesn't. */
  330. int
  331. hasnopath(s) char * s; {
  332. char * p = NULL;
  333. if (!s) return(0);
  334. if (!*s) return(0);
  335. zstrip(s,&p);
  336. return(ckstrcmp(s,p,CKMAXPATH,filecase) == 0 ? 1 : 0);
  337. }
  338. /* C K S P R E A D -- Print string double-spaced */
  339. static char * sprptr = NULL;
  340. static char *
  341. ckspread(s) char * s; {
  342. int n = 0;
  343. char * p;
  344. n = strlen(s);
  345. if (sprptr)
  346. free(sprptr);
  347. sprptr = malloc(n + n + 3);
  348. if (sprptr) {
  349. p = sprptr;
  350. while (*s) {
  351. *p++ = *s++;
  352. *p++ = SP;
  353. }
  354. *p = NUL;
  355. }
  356. return(sprptr ? sprptr : "");
  357. }
  358. /* T E S T -- Bit test */
  359. static int
  360. test(x,m) int x, m; { /* Returns 1 if any bits from m are on in x, else 0 */
  361. return((x & m) ? 1 : 0);
  362. }
  363. /* K W D H E L P -- Given a keyword table, print keywords in columns. */
  364. /*
  365. Call with:
  366. s - keyword table
  367. n - number of entries
  368. pat - pattern (left substring) that must match for each keyword
  369. pre - prefix to add to each keyword
  370. post - suffix to add to each keyword
  371. off - offset on first screenful, allowing room for introductory text
  372. xhlp - 1 to print any CM_INV keywords that are not also abbreviations.
  373. 2 to print CM_INV keywords if CM_HLP also set
  374. 4 if it's a switch table (to show ':' if CM_ARG)
  375. 8 print any keywords that CONTAIN the pattern
  376. Arranges keywords in columns with width based on longest keyword.
  377. Does "more?" prompting at end of screen.
  378. Uses global cmd_rows and cmd_cols for screen size.
  379. */
  380. VOID
  381. kwdhelp(s,n,pat,pre,post,off,xhlp)
  382. struct keytab s[]; int n, off, xhlp; char *pat, *pre, *post;
  383. /* kwdhelp */ {
  384. int width = 0;
  385. int cc;
  386. int cols, height, i, j, k, lc, n2 = 0;
  387. char *b = NULL, *p, *q;
  388. char *pa, *px;
  389. char **s2 = NULL;
  390. char *tmpbuf = NULL;
  391. cc = strlen(pat);
  392. if (!s) return; /* Nothing to do */
  393. if (n < 1) return; /* Ditto */
  394. if (off < 0) off = 0; /* Offset for first page */
  395. if (!pre) pre = ""; /* Handle null string pointers */
  396. if (!post) post = "";
  397. lc = off; /* Screen-line counter */
  398. if (xhlp & 4) /* For switches */
  399. tmpbuf = (char *)malloc(TMPBUFSIZ+1);
  400. if ((s2 = (char **) malloc(n * sizeof(char *)))) {
  401. for (i = 0; i < n; i++) { /* Find longest keyword */
  402. s2[i] = NULL;
  403. if (xhlp & 8) {
  404. if (ckindex(pat,s[i].kwd,0,0,0) < 1) /* for SHOW FUNCTIONS */
  405. continue;
  406. } else if (ckstrcmp(s[i].kwd,pat,cc,0)) /* for regular keywords */
  407. continue;
  408. if (s[i].flgs & CM_PSH /* NOPUSH or nopush screening */
  409. #ifndef NOPUSH
  410. && nopush
  411. #endif /* NOPUSH */
  412. )
  413. continue;
  414. if (s[i].flgs & CM_LOC /* NOLOCAL or nolocal screening */
  415. #ifndef NOLOCAL
  416. && nolocal
  417. #endif /* NOLOCAL */
  418. )
  419. continue;
  420. if (s[i].flgs & CM_INV) {
  421. #ifdef COMMENT
  422. /* This code does not show invisible keywords at all except for "help ?" */
  423. /* and then only help topics (CM_HLP) in the top-level keyword list. */
  424. if ((xhlp & 2) == 0)
  425. continue;
  426. else if ((s[i].flgs & CM_HLP) == 0)
  427. continue;
  428. #else
  429. /* This code shows invisible keywords that are not also abbreviations when */
  430. /* ? was typed AFTER the beginning of the field so the user can find out */
  431. /* what they are and (for example) why completion doesn't work at this point */
  432. if (s[i].flgs & CM_ABR)
  433. continue;
  434. else if ((xhlp & 3) == 0)
  435. continue;
  436. else if ((xhlp & 2) && ((s[i].flgs & CM_HLP) == 0))
  437. continue;
  438. #endif /* COMMENT */
  439. }
  440. j = strlen(s[i].kwd);
  441. if (!(xhlp & 4) || !tmpbuf) { /* Regular keyword table */
  442. s2[n2++] = s[i].kwd; /* Copy pointers to visible ones */
  443. } else { /* Switches */
  444. ckmakmsg(tmpbuf, /* Make a copy that shows ":" if */
  445. TMPBUFSIZ, /* the switch takes an argument. */
  446. s[i].kwd,
  447. (s[i].flgs & CM_ARG) ? ":" : "",
  448. NULL,
  449. NULL
  450. );
  451. makestr(&(s2[n2]),tmpbuf);
  452. if (s[i].flgs & CM_ARG) j++;
  453. n2++;
  454. }
  455. if (j > width)
  456. width = j;
  457. }
  458. /* Column width */
  459. n = n2;
  460. }
  461. if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer */
  462. char * bx;
  463. bx = b + cmd_cols;
  464. width += (int)strlen(pre) + (int)strlen(post) + 2;
  465. cols = cmd_cols / width; /* How many columns? */
  466. if (cols < 1) cols = 1;
  467. height = n / cols; /* How long is each column? */
  468. if (n % cols) height++; /* Add one for remainder, if any */
  469. for (i = 0; i < height; i++) { /* Loop for each row */
  470. for (j = 0; j < cmd_cols; j++) /* First fill row with blanks */
  471. b[j] = SP;
  472. for (j = 0; j < cols; j++) { /* Loop for each column in row */
  473. k = i + (j * height); /* Index of next keyword */
  474. if (k < n) { /* In range? */
  475. pa = pre;
  476. px = post;
  477. p = s2[k]; /* Point to verb name */
  478. q = b + (j * width) + 1; /* Where to copy it to */
  479. while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */
  480. q--; /* Back up over NUL */
  481. while ((q < bx) && (*q++ = *p++)) ; /* Copy filename */
  482. q--; /* Back up over NUL */
  483. while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */
  484. if (j < cols - 1) {
  485. q--;
  486. *q = SP; /* Replace the space */
  487. }
  488. }
  489. }
  490. p = b + cmd_cols - 1; /* Last char in line */
  491. while (*p-- == SP) ; /* Trim */
  492. *(p+2) = NUL;
  493. printf("%s\n",b); /* Print the line */
  494. if (++lc > (cmd_rows - 2)) { /* Screen full? */
  495. if (!askmore()) /* Do more-prompting... */
  496. goto xkwdhelp;
  497. else
  498. lc = 0;
  499. }
  500. }
  501. /* printf("\n"); */ /* Blank line at end of report */
  502. } else { /* Malloc failure, no columns */
  503. for (i = 0; i < n; i++) {
  504. if (s[i].flgs & CM_INV) /* Use original keyword table */
  505. continue; /* skipping invisible entries */
  506. printf("%s%s%s\n",pre,s[i].kwd,post);
  507. if (++lc > (cmd_rows - 2)) { /* Screen full? */
  508. if (!askmore()) /* Do more-prompting... */
  509. goto xkwdhelp;
  510. else
  511. lc = 0;
  512. }
  513. }
  514. }
  515. xkwdhelp:
  516. if (xhlp & 4) {
  517. if (tmpbuf) free((char *)tmpbuf);
  518. for (i = 0; i < n; i++)
  519. if (s2[i]) free(s2[i]);
  520. }
  521. if (s2) free(s2); /* Free array copy */
  522. if (b) free(b); /* Free line buffer */
  523. return;
  524. }
  525. /* X F I L H E L P -- Given a file list, print names in columns. */
  526. /*
  527. Call with:
  528. n - number of entries
  529. pre - prefix to add to each filename
  530. post - suffix to add to each filename
  531. off - offset on first screenful, allowing room for introductory text
  532. cmdirflg - 1 if only directory names should be listed, 0 to list all files
  533. fs - call fileselect() to decide whether to include each file.
  534. The rest of the args are the same as for fileselect().
  535. Arranges filenames in columns with width based on longest filename.
  536. Does "more?" prompting at end of screen.
  537. Uses global cmd_rows and cmd_cols for screen size.
  538. */
  539. int
  540. #ifdef CK_ANSIC
  541. xfilhelp(
  542. int n, char *pre, char *post, int off, int cmdirflag,
  543. int fs, char *sa, char *sb, char *sna, char *snb,
  544. CK_OFF_T minsiz, CK_OFF_T maxsiz,
  545. int nbu, int nxlist,
  546. char ** xlist
  547. )
  548. #else
  549. xfilhelp(n,pre,post,off,cmdirflg,
  550. fs,sa,sb,sna,snb,minsiz,maxsiz,nbu,nxlist,xlist)
  551. int n, off; char *pre, *post; int cmdirflg;
  552. int fs; char *sa,*sb,*sna,*snb; CK_OFF_T minsiz,maxsiz;
  553. int nbu,nxlist; char ** xlist;
  554. #endif /* CK_ANSIC */
  555. {
  556. char filbuf[CKMAXPATH + 1]; /* Temp buffer for one filename */
  557. int width = 0;
  558. int cols, height, i, j, k, lc, n2 = 0, rc = 0, itsadir = 0;
  559. char *b = NULL, *p, *q;
  560. char *pa, *px;
  561. char **s2 = NULL;
  562. #ifdef VMS
  563. char * cdp = zgtdir();
  564. #endif /* VMS */
  565. if (n < 1) return(0);
  566. if (off < 0) off = 0; /* Offset for first page */
  567. if (!pre) pre = ""; /* Handle null string pointers */
  568. if (!post) post = "";
  569. lc = off; /* Screen-line counter */
  570. if ((s2 = (char **) malloc(n * sizeof(char *)))) {
  571. for (i = 0; i < n; i++) { /* Loop through filenames */
  572. itsadir = 0;
  573. s2[i] = NULL; /* Initialize each pointer to NULL */
  574. znext(filbuf); /* Get next filename */
  575. if (!filbuf[0]) /* Shouldn't happen */
  576. break;
  577. #ifdef COMMENT
  578. itsadir = isdir(filbuf); /* Is it a directory? */
  579. if (cmdirflg && !itsadir) /* No, listing directories only? */
  580. continue; /* So skip this one. */
  581. #endif /* COMMENT */
  582. if (fs) if (fileselect(filbuf,
  583. sa,sb,sna,snb,
  584. minsiz,maxsiz,nbu,nxlist,xlist) < 1) {
  585. continue;
  586. }
  587. #ifdef VMS
  588. ckstrncpy(filbuf,zrelname(filbuf,cdp),CKMAXPATH);
  589. #endif /* VMS */
  590. j = strlen(filbuf);
  591. #ifndef VMS
  592. if (itsadir && j < CKMAXPATH - 1 && j > 0) {
  593. if (filbuf[j-1] != dirsep) {
  594. filbuf[j++] = dirsep;
  595. filbuf[j] = NUL;
  596. }
  597. }
  598. #endif /* VMS */
  599. if (!(s2[n2] = malloc(j+1))) {
  600. printf("?Memory allocation failure\n");
  601. rc = -9;
  602. goto xfilhelp;
  603. }
  604. if (j <= CKMAXPATH) {
  605. strcpy(s2[n2],filbuf);
  606. n2++;
  607. } else {
  608. printf("?Name too long - %s\n", filbuf);
  609. rc = -9;
  610. goto xfilhelp;
  611. }
  612. if (j > width) /* Get width of widest one */
  613. width = j;
  614. }
  615. n = n2; /* How many we actually got */
  616. }
  617. sh_sort(s2,NULL,n,0,0,filecase); /* Alphabetize the list */
  618. rc = 1;
  619. if (s2 && (b = (char *) malloc(cmd_cols + 1))) { /* Make a line buffer */
  620. char * bx;
  621. bx = b + cmd_cols;
  622. width += (int)strlen(pre) + (int)strlen(post) + 2;
  623. cols = cmd_cols / width; /* How many columns? */
  624. if (cols < 1) cols = 1;
  625. height = n / cols; /* How long is each column? */
  626. if (n % cols) height++; /* Add one for remainder, if any */
  627. for (i = 0; i < height; i++) { /* Loop for each row */
  628. for (j = 0; j < cmd_cols; j++) /* First fill row with blanks */
  629. b[j] = SP;
  630. for (j = 0; j < cols; j++) { /* Loop for each column in row */
  631. k = i + (j * height); /* Index of next filename */
  632. if (k < n) { /* In range? */
  633. pa = pre;
  634. px = post;
  635. p = s2[k]; /* Point to filename */
  636. q = b + (j * width) + 1; /* and destination */
  637. while ((q < bx) && (*q++ = *pa++)) ; /* Copy prefix */
  638. q--; /* Back up over NUL */
  639. while ((q < bx) && (*q++ = *p++)) ; /* Copy filename */
  640. q--; /* Back up over NUL */
  641. while ((q < bx) && (*q++ = *px++)) ; /* Copy suffix */
  642. if (j < cols - 1) {
  643. q--;
  644. *q = SP; /* Replace the space */
  645. }
  646. }
  647. }
  648. p = b + cmd_cols - 1; /* Last char in line */
  649. while (*p-- == SP) ; /* Trim */
  650. *(p+2) = NUL;
  651. printf("%s\n",b); /* Print the line */
  652. if (++lc > (cmd_rows - 2)) { /* Screen full? */
  653. if (!askmore()) { /* Do more-prompting... */
  654. rc = 0;
  655. goto xfilhelp;
  656. } else
  657. lc = 0;
  658. }
  659. }
  660. printf("\n"); /* Blank line at end of report */
  661. goto xfilhelp;
  662. } else { /* Malloc failure, no columns */
  663. for (i = 0; i < n; i++) {
  664. znext(filbuf);
  665. if (!filbuf[0]) break;
  666. printf("%s%s%s\n",pre,filbuf,post);
  667. if (++lc > (cmd_rows - 2)) { /* Screen full? */
  668. if (!askmore()) { /* Do more-prompting... */
  669. rc = 0;
  670. goto xfilhelp;
  671. } else lc = 0;
  672. }
  673. }
  674. xfilhelp:
  675. if (b) free(b);
  676. for (i = 0; i < n2; i++)
  677. if (s2[i]) free(s2[i]);
  678. if (s2) free((char *)s2);
  679. return(rc);
  680. }
  681. }
  682. /*
  683. Simpler front end for xfilhelp() with shorter arg list when no
  684. file selection is needed.
  685. */
  686. int
  687. filhelp(n,pre,post,off,cmdirflg) int n, off; char *pre, *post; int cmdirflg; {
  688. return(xfilhelp(n,pre,post,off,cmdirflg,
  689. 0,NULL,NULL,NULL,NULL,
  690. (CK_OFF_T)0,(CK_OFF_T)0,0,0,(char **)NULL));
  691. }
  692. /* C M S E T U P -- Set up command buffers */
  693. #ifdef DCMDBUF
  694. int
  695. cmsetup() {
  696. if (!(cmdbuf = malloc(CMDBL + 4))) return(-1);
  697. if (!(savbuf = malloc(CMDBL + 4))) return(-1);
  698. savbuf[0] = '\0';
  699. if (!(prevcmd = malloc(CMDBL + 4))) return(-1);
  700. prevcmd[0] = '\0';
  701. if (!(atmbuf = malloc(ATMBL + 4))) return(-1);
  702. if (!(atxbuf = malloc(CMDBL + 4))) return(-1);
  703. if (!(atybuf = malloc(ATMBL + 4))) return(-1);
  704. if (!(filbuf = malloc(ATMBL + 4))) return(-1);
  705. if (!(cmprom = malloc(PROMPTL + 4))) return(-1);
  706. if (!(cmprxx = malloc(PROMPTL + 4))) return(-1);
  707. #ifdef CK_RECALL
  708. cmrini(cm_recall);
  709. #endif /* CK_RECALL */
  710. return(0);
  711. }
  712. #endif /* DCMDBUF */
  713. /* C M S E T P -- Set the program prompt. */
  714. VOID
  715. cmsetp(s) char *s; {
  716. if (!s) s = "";
  717. ckstrncpy(cmprxx,s,PROMPTL);
  718. psetf = 1; /* Flag that prompt has been set. */
  719. }
  720. /* C M S A V P -- Save a copy of the current prompt. */
  721. VOID
  722. #ifdef CK_ANSIC
  723. cmsavp(char s[], int n)
  724. #else
  725. cmsavp(s,n) char s[]; int n;
  726. #endif /* CK_ANSIC */
  727. /* cmsavp */ {
  728. if (psetf) /* But not if no prompt is set. */
  729. ckstrncpy(s,cmprxx,n);
  730. }
  731. char *
  732. cmgetp() {
  733. return(cmprxx);
  734. }
  735. int
  736. cmgbrk() {
  737. return(brkchar);
  738. }
  739. int
  740. cmgkwflgs() {
  741. return(cmkwflgs);
  742. }
  743. /* P R O M P T -- Issue the program prompt. */
  744. VOID
  745. prompt(f) xx_strp f; {
  746. char *sx, *sy; int n;
  747. #ifdef CK_SSL
  748. extern int ssl_active_flag, tls_active_flag;
  749. #endif /* CK_SSL */
  750. if (psetf == 0) /* If no prompt set, set default. */
  751. cmsetp(dfprom);
  752. sx = cmprxx; /* Unevaluated copy */
  753. if (f) { /* If conversion function given */
  754. sy = cmprom; /* Evaluate it */
  755. #ifdef COMMENT
  756. debug(F101,"prompt sx","",sx);
  757. debug(F101,"prompt sy","",sy);
  758. #endif /* COMMENT */
  759. n = PROMPTL;
  760. if ((*f)(sx,&sy,&n) < 0) /* If evaluation failed */
  761. sx = cmprxx; /* revert to unevaluated copy */
  762. else if (!*cmprom) /* ditto if it came up empty */
  763. sx = cmprxx;
  764. else
  765. sx = cmprom;
  766. } else
  767. ckstrncpy(cmprom,sx,PROMPTL);
  768. cmprom[PROMPTL-1] = NUL;
  769. if (!*sx) /* Don't print if empty */
  770. return;
  771. #ifdef OSK
  772. fputs(sx, stdout);
  773. #else
  774. #ifdef MAC
  775. printf("%s", sx);
  776. #else
  777. #ifdef IKSD
  778. if (inserver) { /* Print the prompt. */
  779. ttoc(CR); /* If TELNET Server */
  780. ttoc(NUL); /* must folloW CR by NUL */
  781. printf("%s",sx);
  782. } else
  783. #endif /* IKSD */
  784. printf("\r%s",sx);
  785. #ifdef CK_SSL
  786. if (!(ssl_active_flag || tls_active_flag))
  787. #endif /* CK_SSL */
  788. fflush(stdout); /* Now! */
  789. #endif /* MAC */
  790. #endif /* OSK */
  791. }
  792. #ifndef NOSPL
  793. VOID
  794. pushcmd(s) char * s; { /* For use with IF command. */
  795. if (!s) s = np;
  796. ckstrncpy(savbuf,s,CMDBL); /* Save the dependent clause, */
  797. cmres(); /* and clear the command buffer. */
  798. debug(F110, "pushcmd savbuf", savbuf, 0);
  799. }
  800. VOID
  801. pushqcmd(s) char * s; { /* For use with ELSE command. */
  802. char c, * p = savbuf; /* Dest */
  803. if (!s) s = np; /* Source */
  804. while (*s) { /* Get first nonwhitespace char */
  805. if (*s != SP)
  806. break;
  807. else
  808. s++;
  809. }
  810. if (*s != '{') { /* If it's not "{" */
  811. pushcmd(s); /* do regular pushcmd */
  812. return;
  813. }
  814. while ((c = *s++)) { /* Otherwise insert quotes */
  815. if (c == CMDQ)
  816. *p++ = CMDQ;
  817. *p++ = c;
  818. }
  819. cmres(); /* and clear the command buffer. */
  820. debug(F110, "pushqcmd savbuf", savbuf, 0);
  821. }
  822. #endif /* NOSPL */
  823. #ifdef COMMENT
  824. /* no longer used... */
  825. VOID
  826. popcmd() {
  827. ckstrncpy(cmdbuf,savbuf,CMDBL); /* Put back the saved material */
  828. *savbuf = '\0'; /* and clear the save buffer */
  829. cmres();
  830. }
  831. #endif /* COMMENT */
  832. /* C M R E S -- Reset pointers to beginning of command buffer. */
  833. VOID
  834. cmres() {
  835. inword = 0; /* We're not in a word */
  836. cc = 0; /* Character count is zero */
  837. /* Initialize pointers */
  838. pp = cmdbuf; /* Beginning of current field */
  839. bp = cmdbuf; /* Current position within buffer */
  840. np = cmdbuf; /* Where to start next field */
  841. cmfldflgs = 0;
  842. cmflgs = -5; /* Parse not yet started. */
  843. ungw = 0; /* Don't need to unget a word. */
  844. }
  845. /* C M I N I -- Clear the command and atom buffers, reset pointers. */
  846. /*
  847. The argument specifies who is to echo the user's typein --
  848. 1 means the cmd package echoes
  849. 0 somebody else (system, front end, terminal) echoes
  850. */
  851. VOID
  852. cmini(d) int d; {
  853. #ifdef DCMDBUF
  854. if (!atmbuf)
  855. if (cmsetup()<0)
  856. fatal("fatal error: unable to allocate command buffers");
  857. #endif /* DCMDBUF */
  858. #ifdef USE_MEMCPY
  859. memset(cmdbuf,0,CMDBL);
  860. memset(atmbuf,0,ATMBL);
  861. #else
  862. for (bp = cmdbuf; bp < cmdbuf+CMDBL; bp++) *bp = NUL;
  863. for (bp = atmbuf; bp < atmbuf+ATMBL; bp++) *bp = NUL;
  864. #endif /* USE_MEMCPY */
  865. *atmbuf = *savbuf = *atxbuf = *atybuf = *filbuf = NUL;
  866. blocklvl = 0; /* Block level is 0 */
  867. linebegin = 1; /* At the beginning of a line */
  868. dpx = d; /* Global copy of the echo flag */
  869. debug(F101,"cmini dpx","",dpx);
  870. crflag = 0; /* Reset flags */
  871. qmflag = 0;
  872. esflag = 0;
  873. #ifdef CK_RECALL
  874. no_recall = 0; /* Start out with recall enabled */
  875. #endif /* CK_RECALL */
  876. cmres(); /* Sets bp etc */
  877. newcmd = 1; /* See addcmd() */
  878. }
  879. #ifndef NOSPL
  880. /*
  881. The following bits are to allow the command package to call itself
  882. in the middle of a parse. To do this, begin by calling cmpush, and
  883. end by calling cmpop. As you can see, this is rather expensive.
  884. */
  885. #ifdef DCMDBUF
  886. struct cmp {
  887. int i[5]; /* stack for integers */
  888. char *c[3]; /* stack for pointers */
  889. char *b[8]; /* stack for buffer contents */
  890. };
  891. struct cmp *cmp = 0;
  892. #else
  893. int cmp_i[CMDDEP+1][5]; /* Stack for integers */
  894. char *cmp_c[CMDDEP+1][5]; /* for misc pointers */
  895. char *cmp_b[CMDDEP+1][7]; /* for buffer contents pointers */
  896. #endif /* DCMDBUF */
  897. int cmddep = -1; /* Current stack depth */
  898. int
  899. cmpush() { /* Save the command environment */
  900. char *cp; /* Character pointer */
  901. if (cmddep >= CMDDEP) /* Enter a new command depth */
  902. return(-1);
  903. cmddep++;
  904. debug(F101,"&cmpush to depth","",cmddep);
  905. #ifdef DCMDBUF
  906. /* allocate memory for cmp if not already done */
  907. if (!cmp && !(cmp = (struct cmp *) malloc(sizeof(struct cmp)*(CMDDEP+1))))
  908. fatal("cmpush: no memory for cmp");
  909. cmp[cmddep].i[0] = cmflgs; /* First do the global ints */
  910. cmp[cmddep].i[1] = cmfsav;
  911. cmp[cmddep].i[2] = atxn;
  912. cmp[cmddep].i[3] = ungw;
  913. cmp[cmddep].c[0] = bp; /* Then the global pointers */
  914. cmp[cmddep].c[1] = pp;
  915. cmp[cmddep].c[2] = np;
  916. #else
  917. cmp_i[cmddep][0] = cmflgs; /* First do the global ints */
  918. cmp_i[cmddep][1] = cmfsav;
  919. cmp_i[cmddep][2] = atxn;
  920. cmp_i[cmddep][3] = ungw;
  921. cmp_c[cmddep][0] = bp; /* Then the global pointers */
  922. cmp_c[cmddep][1] = pp;
  923. cmp_c[cmddep][2] = np;
  924. #endif /* DCMDBUF */
  925. /* Now the buffers themselves. A lot of repititious code... */
  926. #ifdef DCMDBUF
  927. cp = malloc((int)strlen(cmdbuf)+1); /* 0: Command buffer */
  928. if (cp) strcpy(cp,cmdbuf);
  929. cmp[cmddep].b[0] = cp;
  930. if (cp == NULL) return(-1);
  931. cp = malloc((int)strlen(savbuf)+1); /* 1: Save buffer */
  932. if (cp) strcpy(cp,savbuf);
  933. cmp[cmddep].b[1] = cp;
  934. if (cp == NULL) return(-1);
  935. cmp[cmddep].b[2] = NULL;
  936. cp = malloc((int)strlen(atmbuf)+1); /* 3: Atom buffer */
  937. if (cp) strcpy(cp,atmbuf);
  938. cmp[cmddep].b[3] = cp;
  939. if (cp == NULL) return(-1);
  940. cp = malloc((int)strlen(atxbuf)+1); /* 4: Expansion buffer */
  941. if (cp) strcpy(cp,atxbuf);
  942. cmp[cmddep].b[4] = cp;
  943. if (cp == NULL) return(-1);
  944. cp = malloc((int)strlen(atybuf)+1); /* 5: Atom buffer copy */
  945. if (cp) strcpy(cp,atybuf);
  946. cmp[cmddep].b[5] = cp;
  947. if (cp == NULL) return(-1);
  948. cp = malloc((int)strlen(filbuf)+1); /* 6: File name buffer */
  949. if (cp) strcpy(cp,filbuf);
  950. cmp[cmddep].b[6] = cp;
  951. if (cp == NULL) return(-1);
  952. #else
  953. cp = malloc((int)strlen(cmdbuf)+1); /* 0: Command buffer */
  954. if (cp) strcpy(cp,cmdbuf);
  955. cmp_b[cmddep][0] = cp;
  956. if (cp == NULL) return(-1);
  957. cp = malloc((int)strlen(savbuf)+1); /* 1: Save buffer */
  958. if (cp) strcpy(cp,savbuf);
  959. cmp_b[cmddep][1] = cp;
  960. if (cp == NULL) return(-1);
  961. cmp_b[cmddep][2] = NULL;
  962. cp = malloc((int)strlen(atmbuf)+1); /* 3: Atom buffer */
  963. if (cp) strcpy(cp,atmbuf);
  964. cmp_b[cmddep][3] = cp;
  965. if (cp == NULL) return(-1);
  966. cp = malloc((int)strlen(atxbuf)+1); /* 4: Expansion buffer */
  967. if (cp) strcpy(cp,atxbuf);
  968. cmp_b[cmddep][4] = cp;
  969. if (cp == NULL) return(-1);
  970. cp = malloc((int)strlen(atybuf)+1); /* 5: Atom buffer copy */
  971. if (cp) strcpy(cp,atybuf);
  972. cmp_b[cmddep][5] = cp;
  973. if (cp == NULL) return(-1);
  974. cp = malloc((int)strlen(filbuf)+1); /* 6: File name buffer */
  975. if (cp) strcpy(cp,filbuf);
  976. cmp_b[cmddep][6] = cp;
  977. if (cp == NULL) return(-1);
  978. #endif /* DCMDBUF */
  979. cmini(dpx); /* Initize the command parser */
  980. return(0);
  981. }
  982. int
  983. cmpop() { /* Restore the command environment */
  984. if (cmddep < 0) {
  985. debug(F100,"&cmpop called from top level","",0);
  986. return(-1); /* Don't pop too much! */
  987. }
  988. #ifdef DCMDBUF
  989. cmflgs = cmp[cmddep].i[0]; /* First do the global ints */
  990. cmfsav = cmp[cmddep].i[1];
  991. atxn = cmp[cmddep].i[2];
  992. ungw = cmp[cmddep].i[3];
  993. bp = cmp[cmddep].c[0]; /* Then the global pointers */
  994. pp = cmp[cmddep].c[1];
  995. np = cmp[cmddep].c[2];
  996. #else
  997. cmflgs = cmp_i[cmddep][0]; /* First do the global ints */
  998. cmfsav = cmp_i[cmddep][1];
  999. atxn = cmp_i[cmddep][2];
  1000. ungw = cmp_i[cmddep][3];
  1001. bp = cmp_c[cmddep][0]; /* Then the global pointers */
  1002. pp = cmp_c[cmddep][1];
  1003. np = cmp_c[cmddep][2];
  1004. #endif /* DCMDBUF */
  1005. /* Now the buffers themselves. */
  1006. /* Note: strncpy(), not ckstrncpy() -- Here we WANT the NUL padding... */
  1007. #ifdef DCMDBUF
  1008. if (cmp[cmddep].b[0]) {
  1009. strncpy(cmdbuf,cmp[cmddep].b[0],CMDBL); /* 0: Command buffer */
  1010. free(cmp[cmddep].b[0]);
  1011. cmp[cmddep].b[0] = NULL;
  1012. }
  1013. if (cmp[cmddep].b[1]) {
  1014. strncpy(savbuf,cmp[cmddep].b[1],CMDBL); /* 1: Save buffer */
  1015. free(cmp[cmddep].b[1]);
  1016. cmp[cmddep].b[1] = NULL;
  1017. }
  1018. if (cmp[cmddep].b[3]) {
  1019. strncpy(atmbuf,cmp[cmddep].b[3],ATMBL); /* 3: Atomic buffer! */
  1020. free(cmp[cmddep].b[3]);
  1021. cmp[cmddep].b[3] = NULL;
  1022. }
  1023. if (cmp[cmddep].b[4]) {
  1024. strncpy(atxbuf,cmp[cmddep].b[4],ATMBL); /* 4: eXpansion buffer */
  1025. free(cmp[cmddep].b[4]);
  1026. cmp[cmddep].b[4] = NULL;
  1027. }
  1028. if (cmp[cmddep].b[5]) {
  1029. strncpy(atybuf,cmp[cmddep].b[5],ATMBL); /* 5: Atom buffer copY */
  1030. free(cmp[cmddep].b[5]);
  1031. cmp[cmddep].b[5] = NULL;
  1032. }
  1033. if (cmp[cmddep].b[6]) {
  1034. strncpy(filbuf,cmp[cmddep].b[6],ATMBL); /* 6: Filename buffer */
  1035. free(cmp[cmddep].b[6]);
  1036. cmp[cmddep].b[6] = NULL;
  1037. }
  1038. #else
  1039. if (cmp_b[cmddep][0]) {
  1040. strncpy(cmdbuf,cmp_b[cmddep][0],CMDBL); /* 0: Command buffer */
  1041. free(cmp_b[cmddep][0]);
  1042. cmp_b[cmddep][0] = NULL;
  1043. }
  1044. if (cmp_b[cmddep][1]) {
  1045. strncpy(savbuf,cmp_b[cmddep][1],CMDBL); /* 1: Save buffer */
  1046. free(cmp_b[cmddep][1]);
  1047. cmp_b[cmddep][1] = NULL;
  1048. }
  1049. if (cmp_b[cmddep][3]) {
  1050. strncpy(atmbuf,cmp_b[cmddep][3],ATMBL); /* 3: Atomic buffer! */
  1051. free(cmp_b[cmddep][3]);
  1052. cmp_b[cmddep][3] = NULL;
  1053. }
  1054. if (cmp_b[cmddep][4]) {
  1055. strncpy(atxbuf,cmp_b[cmddep][4],ATMBL); /* 4: eXpansion buffer */
  1056. free(cmp_b[cmddep][4]);
  1057. cmp_b[cmddep][4] = NULL;
  1058. }
  1059. if (cmp_b[cmddep][5]) {
  1060. strncpy(atybuf,cmp_b[cmddep][5],ATMBL); /* 5: Atom buffer copY */
  1061. free(cmp_b[cmddep][5]);
  1062. cmp_b[cmddep][5] = NULL;
  1063. }
  1064. if (cmp_b[cmddep][6]) {
  1065. strncpy(filbuf,cmp_b[cmddep][6],ATMBL); /* 6: Filename buffer */
  1066. free(cmp_b[cmddep][6]);
  1067. cmp_b[cmddep][6] = NULL;
  1068. }
  1069. #endif /* DCMDBUF */
  1070. cmddep--; /* Rise, rise */
  1071. debug(F101,"&cmpop to depth","",cmddep);
  1072. return(cmddep);
  1073. }
  1074. #endif /* NOSPL */
  1075. #ifdef COMMENT
  1076. VOID /* Not used */
  1077. stripq(s) char *s; { /* Function to strip '\' quotes */
  1078. char *t;
  1079. while (*s) {
  1080. if (*s == CMDQ) {
  1081. for (t = s; *t != '\0'; t++) *t = *(t+1);
  1082. }
  1083. s++;
  1084. }
  1085. }
  1086. #endif /* COMMENT */
  1087. /* Convert tabs to spaces, one for one */
  1088. VOID
  1089. untab(s) char *s; {
  1090. while (*s) {
  1091. if (*s == HT) *s = SP;
  1092. s++;
  1093. }
  1094. }
  1095. /* C M N U M -- Parse a number in the indicated radix */
  1096. /*
  1097. The radix is specified in the arg list.
  1098. Parses unquoted numeric strings in the given radix.
  1099. Parses backslash-quoted numbers in the radix indicated by the quote:
  1100. \nnn = \dnnn = decimal, \onnn = octal, \xnn = Hexadecimal.
  1101. If these fail, then if a preprocessing function is supplied, that is applied
  1102. and then a second attempt is made to parse an unquoted decimal string.
  1103. And if that fails, the preprocessed string is passed to an arithmetic
  1104. expression evaluator.
  1105. Returns:
  1106. -3 if no input present when required,
  1107. -2 if user typed an illegal number,
  1108. -1 if reparse needed,
  1109. 0 otherwise, with argument n set to the number that was parsed
  1110. */
  1111. /* This is the traditional cmnum() that gets an int */
  1112. int
  1113. cmnum(xhlp,xdef,radix,n,f) char *xhlp, *xdef; int radix, *n; xx_strp f; {
  1114. CK_OFF_T z = (CK_OFF_T)0, check;
  1115. int x;
  1116. x = cmnumw(xhlp,xdef,radix,&z,f);
  1117. *n = z;
  1118. check = *n;
  1119. if (check != z) {
  1120. printf("?Magnitude of result too large for integer - %s\n",ckfstoa(z));
  1121. return(-9);
  1122. }
  1123. return(x);
  1124. }
  1125. /*
  1126. This is the new cmnum() that gets a "wide" result, whatever CK_OFF_T
  1127. is defined to be, normally 32 or 64 bits, depending on the platform.
  1128. fdc, 24 Dec 2005.
  1129. */
  1130. int
  1131. cmnumw(xhlp,xdef,radix,n,f)
  1132. char *xhlp, *xdef; int radix; CK_OFF_T *n; xx_strp f; {
  1133. int x; char *s, *zp, *zq;
  1134. #ifdef COMMENT
  1135. char lbrace, rbrace;
  1136. #endif /* COMMENT */
  1137. if (!xhlp) xhlp = "";
  1138. if (!xdef) xdef = "";
  1139. #ifdef COMMENT
  1140. if (cmfldflgs & 1) {
  1141. lbrace = '(';
  1142. rbrace = ')';
  1143. } else {
  1144. lbrace = '{';
  1145. rbrace = '}';
  1146. }
  1147. #endif /* COMMENT */
  1148. if (radix != 10 && radix != 8) { /* Just do bases 8 and 10 */
  1149. printf("cmnum: illegal radix - %d\n",radix);
  1150. return(-2);
  1151. } /* Easy to add others but there has never been a need for it. */
  1152. x = cmfld(xhlp,xdef,&s,(xx_strp)0);
  1153. debug(F101,"cmnum: cmfld","",x);
  1154. if (x < 0) return(x); /* Parse a field */
  1155. zp = atmbuf;
  1156. /*
  1157. Edit 192 - Allow any number field to be braced. This lets us include
  1158. spaces in expressions, but perhaps more important lets us have user-defined
  1159. functions in numeric fields.
  1160. */
  1161. zp = brstrip(zp); /* Strip braces */
  1162. if (cmfldflgs & 1 && *zp == '(') { /* Parens too.. */
  1163. x = (int) strlen(atmbuf);
  1164. if (x > 0) {
  1165. if (*(atmbuf+x-1) == ')') {
  1166. *(atmbuf+x-1) = NUL;
  1167. zp++;
  1168. }
  1169. }
  1170. }
  1171. if (chknum(zp)) { /* Check for number */
  1172. if (radix == 8) { /* If it's supposed to be octal */
  1173. zp = ckradix(zp,8,10); /* convert to decimal */
  1174. if (!zp) return(-2);
  1175. if (!strcmp(zp,"-1")) return(-2);
  1176. }
  1177. errno = 0; /* Got one, we're done. */
  1178. *n = ckatofs(zp);
  1179. if (errno) {
  1180. perror(zp);
  1181. return(-9);
  1182. }
  1183. debug(F101,"cmnum 1st chknum ok","",*n);
  1184. return(0);
  1185. } else if ((x = xxesc(&zp)) > -1) { /* Check for backslash escape */
  1186. #ifndef OS2
  1187. *n = x;
  1188. #else
  1189. *n = wideresult;
  1190. #endif /* OS2 */
  1191. debug(F101,"cmnum xxesc ok","",*n);
  1192. return(*zp ? -2 : 0);
  1193. } else if (f) { /* If conversion function given */
  1194. zq = atxbuf; /* Try that */
  1195. atxn = CMDBL;
  1196. if ((*f)(zp,&zq,&atxn) < 0) /* Convert */
  1197. return(-2);
  1198. zp = atxbuf;
  1199. }
  1200. debug(F110,"cmnum zp 1",zp,0);
  1201. if (!*zp) zp = xdef; /* Result empty, substitute default */
  1202. debug(F110,"cmnum zp 2",zp,0);
  1203. if (chknum(zp)) { /* Check again for decimal number */
  1204. if (radix == 8) { /* If it's supposed to be octal */
  1205. zp = ckradix(zp,8,10); /* convert to decimal */
  1206. if (!zp) return(-2);
  1207. if (!strcmp(zp,"-1")) return(-2);
  1208. }
  1209. errno = 0;
  1210. *n = ckatofs(zp);
  1211. if (errno) {
  1212. perror(zp);
  1213. return(-9);
  1214. }
  1215. debug(F101,"cmnum 2nd chknum ok","",*n);
  1216. return(0);
  1217. #ifndef NOSPL
  1218. } else if ((x = xxesc(&zp)) > -1) { /* Check for backslash escape */
  1219. #ifndef OS2
  1220. *n = x;
  1221. #else
  1222. *n = wideresult;
  1223. #endif /* OS2 */
  1224. debug(F101,"cmnum xxesc 2 ok","",*n);
  1225. return(*zp ? -2 : 0);
  1226. } else if (f) { /* Not numeric, maybe an expression */
  1227. char * p;
  1228. p = evala(zp);
  1229. if (chknum(p)) {
  1230. if (radix == 8) { /* If it's supposed to be octal */
  1231. zp = ckradix(zp,8,10); /* convert to decimal */
  1232. if (!zp) return(-2);
  1233. if (!strcmp(zp,"-1")) return(-2);
  1234. }
  1235. errno = 0;
  1236. *n = ckatofs(p);
  1237. if (errno) {
  1238. perror(p);
  1239. return(-9);
  1240. }
  1241. debug(F101,"cmnum exp eval ok","",*n);
  1242. return(0);
  1243. } else return(-2);
  1244. #endif /* NOSPL */
  1245. } else { /* Not numeric */
  1246. return(-2);
  1247. }
  1248. }
  1249. #ifdef CKCHANNELIO
  1250. extern int z_error;
  1251. #endif /* CKCHANNELIO */
  1252. /* C M O F I -- Parse the name of an output file */
  1253. /*
  1254. Depends on the external function zchko(); if zchko() not available, use
  1255. cmfld() to parse output file names.
  1256. Returns:
  1257. -9 like -2, except message already printed,
  1258. -3 if no input present when required,
  1259. -2 if permission would be denied to create the file,
  1260. -1 if reparse needed,
  1261. 0 or 1 if file can be created, with xp pointing to name.
  1262. 2 if given the name of an existing directory.
  1263. */
  1264. int
  1265. cmofi(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1266. int x; char *s, *zq;
  1267. #ifdef DOCHKVAR
  1268. int tries;
  1269. #endif /* DOCHKVAR */
  1270. #ifdef DTILDE
  1271. char *dirp;
  1272. #endif /* DTILDE */
  1273. cmfldflgs = 0;
  1274. if (!xhlp) xhlp = "";
  1275. if (!xdef) xdef = "";
  1276. if (*xhlp == NUL) xhlp = "Output file";
  1277. *xp = "";
  1278. debug(F110,"cmofi xdef",xdef,0);
  1279. x = cmfld(xhlp,xdef,&s,(xx_strp)0);
  1280. debug(F111,"cmofi cmfld returns",s,x);
  1281. if (x < 0)
  1282. return(x);
  1283. s = brstrip(s); /* Strip enclosing braces */
  1284. debug(F110,"cmofi 1.5",s,0);
  1285. #ifdef DOCHKVAR
  1286. tries = 0;
  1287. {
  1288. char *p = s;
  1289. /*
  1290. This is really ugly. If we skip conversion the first time through,
  1291. then variable names like \%a will be used as filenames (e.g. creating
  1292. a file called %A in the root directory). If we DON'T skip conversion
  1293. the first time through, then single backslashes used as directory
  1294. separators in filenames will be misinterpreted as variable lead-ins.
  1295. So we prescan to see if it has any variable references. But this
  1296. module is not supposed to know anything about variables, functions,
  1297. etc, so this code does not really belong here, but rather it should
  1298. be at the same level as zzstring().
  1299. */
  1300. /*
  1301. Hmmm, this looks a lot like chkvar() except it that includes \nnn number
  1302. escapes. But why? This makes commands like "mkdir c:\123" impossible.
  1303. And in fact, "mkdir c:\123" creates a directory called "c:{". What's worse,
  1304. rmdir(), which *does* call chkvar(), won't let us remove it. So let's at
  1305. least try making cmofi() symmetrical with cmifi()...
  1306. */
  1307. #ifdef COMMENT
  1308. char * q;
  1309. while ( (tries == 0) && (p = strchr(p,CMDQ)) ) {
  1310. q = *(p+1); /* Char after backslash */
  1311. if (!q) /* None, quit */
  1312. break;
  1313. if (isupper(q)) /* If letter, convert to lowercase */
  1314. q = tolower(q);
  1315. if (isdigit(q)) { /* If it's a digit, */
  1316. tries = 1; /* assume it's a backslash code */
  1317. break;
  1318. }
  1319. switch (q) {
  1320. case CMDQ: /* Double backslash */
  1321. tries = 1; /* so call the conversion function */
  1322. break;
  1323. case '%': /* Variable or array reference */
  1324. case '&': /* must be followed by letter */
  1325. if (isalpha(*(p+2)) || (*(p+2) >= '0' && *(p+2) <= '9'))
  1326. tries = 1;
  1327. break;
  1328. case 'm': case 'v': case '$': /* \m(), \v(), \$() */
  1329. if (*(p+2) == '(')
  1330. if (strchr(p+2,')'))
  1331. tries = 1;
  1332. break;
  1333. case 'f': /* \Fname() */
  1334. if (strchr(p+2,'('))
  1335. if (strchr(p+2,')'))
  1336. tries = 1;
  1337. break;
  1338. case '{': /* \{...} */
  1339. if (strchr(p+2,'}'))
  1340. tries = 1;
  1341. break;
  1342. case 'd': case 'o': /* Decimal or Octal number */
  1343. if (isdigit(*(p+2)))
  1344. tries = 1;
  1345. break;
  1346. case 'x': /* Hex number */
  1347. if (isdigit(*(p+2)) ||
  1348. ((*(p+2) >= 'a' && *(p+2) <= 'f') ||
  1349. ((*(p+2) >= 'A' && *(p+2) <= 'F'))))
  1350. tries = 1;
  1351. default:
  1352. break;
  1353. }
  1354. p++;
  1355. }
  1356. #else
  1357. #ifndef NOSPL
  1358. if (f) { /* If a conversion function is given */
  1359. char *s = p; /* See if there are any variables in */
  1360. while (*s) { /* the string and if so, expand them */
  1361. if (chkvar(s)) {
  1362. tries = 1;
  1363. break;
  1364. }
  1365. s++;
  1366. }
  1367. }
  1368. #endif /* NOSPL */
  1369. #endif /* COMMENT */
  1370. }
  1371. #ifdef OS2
  1372. o_again:
  1373. #endif /* OS2 */
  1374. if (tries == 1)
  1375. #endif /* DOCHKVAR */
  1376. if (f) { /* If a conversion function is given */
  1377. zq = atxbuf; /* do the conversion. */
  1378. atxn = CMDBL;
  1379. if ((x = (*f)(s,&zq,&atxn)) < 0)
  1380. return(-2);
  1381. s = atxbuf;
  1382. if (!*s) /* Result empty, substitute default */
  1383. s = xdef;
  1384. }
  1385. debug(F111,"cmofi 2",s,x);
  1386. #ifdef DTILDE
  1387. dirp = tilde_expand(s); /* Expand tilde, if any, */
  1388. if (*dirp != '\0') { /* right in the atom buffer. */
  1389. if (setatm(dirp,1) < 0) {
  1390. printf("?Name too long\n");
  1391. return(-9);
  1392. }
  1393. }
  1394. s = atmbuf;
  1395. debug(F110,"cmofi 3",s,0);
  1396. #endif /* DTILDE */
  1397. if (iswild(s)) {
  1398. printf("?Wildcards not allowed - %s\n",s);
  1399. return(-2);
  1400. }
  1401. debug(F110,"cmofi 4",s,0);
  1402. #ifdef CK_TMPDIR
  1403. /* isdir() function required for this! */
  1404. if (isdir(s)) {
  1405. debug(F110,"cmofi 5: is directory",s,0);
  1406. *xp = s;
  1407. return(2);
  1408. }
  1409. #endif /* CK_TMPDIR */
  1410. if (strcmp(s,CTTNAM) && (zchko(s) < 0)) { /* OK to write to console */
  1411. #ifdef COMMENT
  1412. #ifdef OS2
  1413. /*
  1414. We don't try again because we already prescanned the string to see if
  1415. if it contained anything that could be used by zzstring().
  1416. */
  1417. if (tries++ < 1)
  1418. goto o_again;
  1419. #endif /* OS2 */
  1420. #endif /* COMMENT */
  1421. /*
  1422. Note: there are certain circumstances where zchko() can give a false
  1423. positive, so don't rely on it to catch every conceivable situation in
  1424. which the given output file can't be created. In other words, we print
  1425. a message and fail here if we KNOW the file can't be created. If we
  1426. succeed but the file can't be opened, the code that tries to open the file
  1427. has to print a message.
  1428. */
  1429. debug(F110,"cmofi 6: failure",s,0);
  1430. #ifdef CKROOT
  1431. if (ckrooterr)
  1432. printf("?Off Limits: %s\n",s);
  1433. else
  1434. #endif /* CKROOT */
  1435. printf("?Write permission denied - %s\n",s);
  1436. #ifdef CKCHANNELIO
  1437. z_error = FX_ACC;
  1438. #endif /* CKCHANNELIO */
  1439. return(-9);
  1440. } else {
  1441. debug(F110,"cmofi 7: ok",s,0);
  1442. *xp = s;
  1443. return(x);
  1444. }
  1445. }
  1446. /* C M I F I -- Parse the name of an existing file */
  1447. /*
  1448. This function depends on the external functions:
  1449. zchki() - Check if input file exists and is readable.
  1450. zxpand() - Expand a wild file specification into a list.
  1451. znext() - Return next file name from list.
  1452. If these functions aren't available, then use cmfld() to parse filenames.
  1453. */
  1454. /*
  1455. Returns
  1456. -4 EOF
  1457. -3 if no input present when required,
  1458. -2 if file does not exist or is not readable,
  1459. -1 if reparse needed,
  1460. 0 or 1 otherwise, with:
  1461. xp pointing to name,
  1462. wild = 1 if name contains '*' or '?', 0 otherwise.
  1463. */
  1464. #ifdef COMMENT /* This horrible hack has been replaced - see further down */
  1465. /*
  1466. C M I O F I -- Parse an input file OR the name of a nonexistent file.
  1467. Use this when an existing file is wanted (so we get help, completion, etc),
  1468. but if a file of the given name does not exist, the name of a new file is
  1469. accepted. For example, with the EDIT command (edit an existing file, or
  1470. create a new file). Returns -9 if file does not exist. It is up to the
  1471. caller to check creatability.
  1472. */
  1473. static int nomsg = 0;
  1474. int
  1475. cmiofi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  1476. int msgsave, x;
  1477. msgsave = nomsg;
  1478. nomsg = 1;
  1479. x = cmifi2(xhlp,xdef,xp,wild,0,NULL,f,0);
  1480. nomsg = msgsave;
  1481. return(x);
  1482. }
  1483. #endif /* COMMENT */
  1484. int
  1485. cmifi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  1486. return(cmifi2(xhlp,xdef,xp,wild,0,NULL,f,0));
  1487. }
  1488. /*
  1489. cmifip() is called when we want to supply a path or path list to search
  1490. in case the filename that the user gives is (a) not absolute, and (b) can't
  1491. be found as given. The path string can be the name of a single directory,
  1492. or a list of directories separated by the PATHSEP character, defined in
  1493. ckucmd.h. Look in ckuusr.c and ckuus3.c for examples of usage.
  1494. */
  1495. int
  1496. cmifip(xhlp,xdef,xp,wild,d,path,f)
  1497. char *xhlp,*xdef,**xp; int *wild, d; char * path; xx_strp f; {
  1498. return(cmifi2(xhlp,xdef,xp,wild,0,path,f,0));
  1499. }
  1500. /* C M D I R -- Parse a directory name */
  1501. /*
  1502. This function depends on the external functions:
  1503. isdir(s) - Check if string s is the name of a directory
  1504. zchki(s) - Check if input file s exists and what type it is.
  1505. If these functions aren't available, then use cmfld() to parse dir names.
  1506. Returns
  1507. -9 For all sorts of reasons, after printing appropriate error message.
  1508. -4 EOF
  1509. -3 if no input present when required,
  1510. -2 if out of space or other internal error,
  1511. -1 if reparse needed,
  1512. 0 or 1, with xp pointing to name, if directory specified,
  1513. */
  1514. int
  1515. cmdir(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  1516. int wild;
  1517. return(cmifi2(xhlp,xdef,xp,&wild,0,NULL,f,1));
  1518. }
  1519. /* Like CMDIR but includes PATH search */
  1520. int
  1521. cmdirp(xhlp,xdef,xp,path,f) char *xhlp, *xdef, **xp; char * path; xx_strp f; {
  1522. int wild;
  1523. return(cmifi2(xhlp,xdef,xp,&wild,0,path,f,1));
  1524. }
  1525. /*
  1526. cmifi2() is the base filename parser called by cmifi, cmifip, cmdir, etc.
  1527. Use it directly when you also want to parse a directory or device
  1528. name as an input file, as in the DIRECTORY command. Call with:
  1529. xhlp -- help message on ?
  1530. xdef -- default response
  1531. xp -- pointer to result (in our space, must be copied from here)
  1532. wild -- flag set upon return to indicate if filespec was wild
  1533. d -- 0 to parse files, 1 to parse files or directories
  1534. Add 2 to inhibit following of symlinks.
  1535. path -- search path for files
  1536. f -- pointer to string processing function (e.g. to evaluate variables)
  1537. dirflg -- 1 to parse *only* directories, 0 otherwise
  1538. */
  1539. int
  1540. cmifi2(xhlp,xdef,xp,wild,d,path,f,dirflg)
  1541. char *xhlp,*xdef,**xp; int *wild, d; char * path; xx_strp f; int dirflg; {
  1542. extern int recursive, diractive, cdactive, dblquo;
  1543. int i, x, itsadir, xc, expanded = 0, nfiles = 0, children = -1;
  1544. int qflag = 0;
  1545. long y;
  1546. CK_OFF_T filesize;
  1547. char *sp = NULL, *zq, *np = NULL;
  1548. char *sv = NULL, *p = NULL;
  1549. #ifdef DTILDE
  1550. char *dirp;
  1551. #endif /* DTILDE */
  1552. #ifndef NOPARTIAL
  1553. #ifndef OS2
  1554. #ifdef OSK
  1555. /* This large array is dynamic for OS-9 -- should do for others too... */
  1556. extern char **mtchs;
  1557. #else
  1558. #ifdef UNIX
  1559. /* OK, for UNIX too */
  1560. extern char **mtchs;
  1561. #else
  1562. #ifdef VMS
  1563. extern char **mtchs;
  1564. #else
  1565. extern char *mtchs[];
  1566. #endif /* VMS */
  1567. #endif /* UNIX */
  1568. #endif /* OSK */
  1569. #endif /* OS2 */
  1570. #endif /* NOPARTIAL */
  1571. if (!xhlp) xhlp = "";
  1572. if (!xdef) xdef = "";
  1573. #ifndef NOLASTFILE
  1574. makestr(&tmplastfile,NULL);
  1575. #endif /* NOLASTFILE */
  1576. nzxopts = 0; /* zxpand() options */
  1577. debug(F101,"cmifi d","",d);
  1578. if (d & 2) { /* d & 2 means don't follow symlinks */
  1579. d ^= 2;
  1580. nzxopts = ZX_NOLINKS;
  1581. }
  1582. debug(F101,"cmifi nzxopts","",nzxopts);
  1583. cmfldflgs = 0;
  1584. if (path)
  1585. if (!*path)
  1586. path = NULL;
  1587. if (path) { /* Make a copy we can poke */
  1588. x = strlen(path);
  1589. np = (char *) malloc(x + 1);
  1590. if (np) {
  1591. strcpy(np, path);
  1592. path = sp = np;
  1593. }
  1594. }
  1595. debug(F110,"cmifi2 path",path,0);
  1596. ckstrncpy(cmdefault,xdef,CMDEFAULT); /* Copy default */
  1597. xdef = cmdefault;
  1598. inword = 0; /* Initialize counts & pointers */
  1599. cc = 0;
  1600. xc = 0;
  1601. *xp = ""; /* Pointer to result string */
  1602. if ((x = cmflgs) != 1) { /* Already confirmed? */
  1603. #ifdef BS_DIRSEP
  1604. dirnamflg = 1;
  1605. x = gtword(0); /* No, get a word */
  1606. dirnamflg = 0;
  1607. #else
  1608. x = gtword(0); /* No, get a word */
  1609. #endif /* BS_DIRSEP */
  1610. } else { /* If so, use default, if any. */
  1611. if (setatm(xdef,1) < 0) {
  1612. printf("?Default name too long\n");
  1613. if (np) free(np);
  1614. return(-9);
  1615. }
  1616. }
  1617. i_path:
  1618. *xp = atmbuf; /* Point to result. */
  1619. while (1) {
  1620. xc += cc; /* Count this character. */
  1621. debug(F111,"cmifi gtword",atmbuf,xc);
  1622. debug(F101,"cmifi switch x","",x);
  1623. switch (x) { /* x = gtword() return code */
  1624. case -10:
  1625. if (gtimer() > timelimit) {
  1626. #ifdef IKSD
  1627. if (inserver) {
  1628. printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  1629. doexit(GOOD_EXIT,0);
  1630. }
  1631. #endif /* IKSD */
  1632. /* if (!quiet) printf("?Timed out\n"); */
  1633. return(-10);
  1634. } else {
  1635. x = gtword(0);
  1636. continue;
  1637. }
  1638. case -9:
  1639. printf("Command or field too long\n");
  1640. case -4: /* EOF */
  1641. case -2: /* Out of space. */
  1642. case -1: /* Reparse needed */
  1643. if (np) free(np);
  1644. return(x);
  1645. case 1: /* CR */
  1646. case 0: /* SP */
  1647. if (xc == 0) /* If no input... */
  1648. *xp = xdef; /* substitute the default */
  1649. #ifndef NOLASTFILE
  1650. makestr(&tmplastfile,*xp); /* Make a copy before bstripping */
  1651. #endif /* #ifndef NOLASTFILE */
  1652. *xp = brstrip(*xp); /* Strip braces */
  1653. if (**xp == NUL) { /* 12 mar 2001 */
  1654. if (np) free(np);
  1655. return(-3);
  1656. }
  1657. debug(F110,"cmifi brstrip",*xp,0);
  1658. #ifndef NOSPL
  1659. if (f) { /* If a conversion function is given */
  1660. #ifdef DOCHKVAR
  1661. char *s = *xp; /* See if there are any variables in */
  1662. int x;
  1663. while (*s) { /* the string and if so, expand them */
  1664. x = chkvar(s);
  1665. /* debug(F111,"cmifi chkvar",*xp,x); */
  1666. if (x) {
  1667. #endif /* DOCHKVAR */
  1668. zq = atxbuf;
  1669. atxn = CMDBL;
  1670. if ((*f)(*xp,&zq,&atxn) < 0) {
  1671. if (np) free(np);
  1672. return(-2);
  1673. }
  1674. *xp = atxbuf;
  1675. if (!atxbuf[0])
  1676. *xp = xdef;
  1677. #ifdef DOCHKVAR
  1678. break;
  1679. }
  1680. s++;
  1681. }
  1682. #endif /* DOCHKVAR */
  1683. }
  1684. #endif /* NOSPL */
  1685. if (**xp == NUL) { /* 12 mar 2001 */
  1686. if (np) free(np);
  1687. return(-3);
  1688. }
  1689. #ifdef DTILDE
  1690. if (dirflg) {
  1691. dirp = tilde_expand(*xp); /* Expand tilde, if any, */
  1692. if (*dirp != '\0') { /* in the atom buffer. */
  1693. if (setatm(dirp,1) < 0) {
  1694. printf("Expanded name too long\n");
  1695. if (np) free(np);
  1696. return(-9);
  1697. }
  1698. }
  1699. *xp = atmbuf;
  1700. debug(F110,"cmifi tilde_expand",*xp,0);
  1701. }
  1702. #endif /* DTILDE */
  1703. if (!sv) { /* Only do this once */
  1704. sv = malloc((int)strlen(*xp)+1); /* Make a safe copy */
  1705. if (!sv) {
  1706. printf("?cmifi: malloc error\n");
  1707. if (np) free(np);
  1708. return(-9);
  1709. }
  1710. strcpy(sv,*xp);
  1711. debug(F110,"cmifi sv",sv,0);
  1712. }
  1713. /* This is to get around "cd /" failing because "too many directories match" */
  1714. expanded = 0; /* Didn't call zxpand */
  1715. #ifdef datageneral
  1716. debug(F110,"cmifi isdir 1",*xp,0);
  1717. {
  1718. int y; char *s;
  1719. s = *xp;
  1720. y = strlen(s);
  1721. if (y > 1 &&
  1722. (s[y-1] == ':' ||
  1723. s[y-1] == '^' ||
  1724. s[y-1] == '=')
  1725. )
  1726. s[y-1] = NUL;
  1727. }
  1728. debug(F110,"cmifi isdir 2",*xp,0);
  1729. #endif /* datageneral */
  1730. #ifdef VMS
  1731. if (dirflg) {
  1732. if (!strcmp(*xp,"..")) { /* For UNIXers... */
  1733. setatm("-",0);
  1734. *xp = atmbuf;
  1735. } else if (!strcmp(*xp,".")) {
  1736. setatm("[]",0);
  1737. *xp = atmbuf;
  1738. }
  1739. }
  1740. #endif /* VMS */
  1741. itsadir = isdir(*xp); /* Is it a directory? */
  1742. debug(F111,"cmifi itsadir",*xp,itsadir);
  1743. #ifdef VMS
  1744. /* If they said "blah" where "blah.dir" is a directory... */
  1745. /* change it to [.blah]. */
  1746. if (!itsadir) {
  1747. char tmpbuf[600];
  1748. int flag = 0; char c, * p;
  1749. p = *xp;
  1750. while ((c = *p++) && !flag)
  1751. if (ckstrchr(".[]:*?<>",c))
  1752. flag = 1;
  1753. debug(F111,"cmifi VMS dirname flag",*xp,flag);
  1754. if (!flag) {
  1755. ckmakmsg(tmpbuf,TMPBUFSIZ,"[.",*xp,"]",NULL);
  1756. itsadir = isdir(tmpbuf);
  1757. if (itsadir) {
  1758. setatm(tmpbuf,0);
  1759. *xp = atmbuf;
  1760. }
  1761. debug(F111,"cmifi VMS dirname flag itsadir",*xp,itsadir);
  1762. }
  1763. } else if (itsadir == 1 && *(xp[0]) == '.' && *(xp[1])) {
  1764. char *p;
  1765. if (p = malloc(cc + 4)) {
  1766. ckmakmsg(p,cc+4,"[",*xp,"]",NULL);
  1767. setatm(p,0);
  1768. *xp = atmbuf;
  1769. debug(F110,"cmdir .foo",*xp,0);
  1770. free(p);
  1771. }
  1772. } else if (itsadir == 2 && !diractive) {
  1773. int x; /* [FOO]BAR.DIR instead of [FOO.BAR] */
  1774. char *p;
  1775. p = malloc(cc + 4);
  1776. if (p) {
  1777. x = cvtdir(*xp,p,ATMBL); /* Convert to [FOO.BAR] */
  1778. if (x > 0) {
  1779. setatm(p,0);
  1780. *xp = atmbuf;
  1781. debug(F110,"cmdir cvtdir",*xp,0);
  1782. }
  1783. free(p);
  1784. }
  1785. }
  1786. #endif /* VMS */
  1787. debug(F101,"cmifi dirflg","",dirflg);
  1788. debug(F101,"cmifi diractive","",diractive);
  1789. if (dirflg) { /* Parsing a directory name? */
  1790. /* Yes, does it contain wildcards? */
  1791. if (iswild(*xp) ||
  1792. (diractive && (!strcmp(*xp,".") || !strcmp(*xp,"..")))
  1793. ) {
  1794. nzxopts |= ZX_DIRONLY; /* Match only directory names */
  1795. if (matchdot) nzxopts |= ZX_MATCHDOT;
  1796. if (recursive) nzxopts |= ZX_RECURSE;
  1797. debug(F111,"cmifi nzxopts 2",*xp,nzxopts);
  1798. y = nzxpand(*xp,nzxopts);
  1799. debug(F111,"cmifi nzxpand 2",*xp,y);
  1800. nfiles = y;
  1801. expanded = 1;
  1802. } else {
  1803. #ifdef VMS
  1804. /*
  1805. This is to allow (e.g.) "cd foo", where FOO.DIR;1 is in the
  1806. current directory.
  1807. */
  1808. debug(F111,"cmdir itsadir",*xp,itsadir);
  1809. if (!itsadir) {
  1810. char *s;
  1811. int n;
  1812. s = *xp;
  1813. n = strlen(s);
  1814. if (n > 0 &&
  1815. #ifdef COMMENT
  1816. *s != '[' && s[n-1] != ']' &&
  1817. *s != '<' && s[n-1] != '>' &&
  1818. #else
  1819. ckindex("[",s,0,0,1) == 0 &&
  1820. ckindex("<",s,0,0,1) == 0 &&
  1821. #endif /* COMMENT */
  1822. s[n-1] != ':') {
  1823. char * dirbuf = NULL;
  1824. dirbuf = (char *)malloc(n+4);
  1825. if (dirbuf) {
  1826. if (*s == '.')
  1827. ckmakmsg(dirbuf,n+4,"[",s,"]",NULL);
  1828. else
  1829. ckmakmsg(dirbuf,n+4,"[.",s,"]",NULL);
  1830. itsadir = isdir(dirbuf);
  1831. debug(F111,"cmdir dirbuf",dirbuf,itsadir);
  1832. if (itsadir) {
  1833. setatm(dirbuf,0);
  1834. *xp = atmbuf;
  1835. debug(F110,"cmdir new *xp",*xp,0);
  1836. }
  1837. free(dirbuf);
  1838. }
  1839. /* This is to allow CDPATH to work in VMS... */
  1840. } else if (n > 0) {
  1841. char * p; int i, j, k, d;
  1842. char rb[2] = "]";
  1843. if (p = malloc(x + 8)) {
  1844. ckstrncpy(p,*xp,x+8);
  1845. i = ckindex(".",p,-1,1,1);
  1846. d = ckindex(".dir",p,0,0,0);
  1847. j = ckindex("]",p,-1,1,1);
  1848. if (j == 0) {
  1849. j = ckindex(">",p,-1,1,1);
  1850. rb[0] = '>';
  1851. }
  1852. k = ckindex(":",p,-1,1,1);
  1853. if (i < j || i < k) i = 0;
  1854. if (d < j || d < k) d = 0;
  1855. /* Change [FOO]BAR or [FOO]BAR.DIR */
  1856. /* to [FOO.BAR] */
  1857. if (j > 0 && j < n) {
  1858. p[j-1] = '.';
  1859. if (d > 0) p[d-1] = NUL;
  1860. ckstrncat(p,rb,x+8);
  1861. debug(F110,"cmdir xxx",p,0);
  1862. }
  1863. itsadir = isdir(p);
  1864. debug(F111,"cmdir p",p,itsadir);
  1865. if (itsadir) {
  1866. setatm(p,0);
  1867. *xp = atmbuf;
  1868. debug(F110,"cmdir new *xp",*xp,0);
  1869. }
  1870. free(p);
  1871. }
  1872. }
  1873. }
  1874. #endif /* VMS */
  1875. y = (!itsadir) ? 0 : 1;
  1876. debug(F111,"cmifi y itsadir",*xp,y);
  1877. }
  1878. } else { /* Parsing a filename. */
  1879. debug(F110,"cmifi *xp pre-zxpand",*xp,0);
  1880. #ifndef COMMENT
  1881. nzxopts |= (d == 0) ? ZX_FILONLY : 0; /* So always expand. */
  1882. if (matchdot) nzxopts |= ZX_MATCHDOT;
  1883. if (recursive) nzxopts |= ZX_RECURSE;
  1884. y = nzxpand(*xp,nzxopts);
  1885. #else
  1886. /* Here we're trying to fix a problem in which a directory name is accepted */
  1887. /* as a filename, but this breaks too many other things. */
  1888. /* nzxopts = 0; */
  1889. if (!d) {
  1890. if (itsadir & !iswild(*xp)) {
  1891. debug(F100,"cmifi dir when filonly","",0);
  1892. printf("?Not a regular file: \"%s\"\n",*xp);
  1893. if (sv) free(sv);
  1894. if (np) free(np);
  1895. return(-9);
  1896. } else {
  1897. nzxopts |= ZX_FILONLY;
  1898. if (matchdot) nzxopts |= ZX_MATCHDOT;
  1899. if (recursive) nzxopts |= ZX_RECURSE;
  1900. y = nzxpand(*xp,nzxopts);
  1901. }
  1902. }
  1903. #endif /* COMMENT */
  1904. nfiles = y;
  1905. debug(F111,"cmifi y nzxpand",*xp,y);
  1906. debug(F111,"cmifi y atmbuf",atmbuf,itsadir);
  1907. expanded = 1;
  1908. }
  1909. /* domydir() calls zxrewind() so we MUST call nzxpand() here */
  1910. if (!expanded && diractive) {
  1911. debug(F110,"cmifi diractive catch-all zxpand",*xp,0);
  1912. nzxopts |= (d == 0) ? ZX_FILONLY : (dirflg ? ZX_DIRONLY : 0);
  1913. if (matchdot) nzxopts |= ZX_MATCHDOT;
  1914. if (recursive) nzxopts |= ZX_RECURSE;
  1915. y = nzxpand(*xp,nzxopts);
  1916. debug(F111,"cmifi diractive nzxpand",*xp,y);
  1917. nfiles = y;
  1918. expanded = 1;
  1919. }
  1920. *wild = (iswild(sv) || (y > 1)) && (itsadir == 0);
  1921. #ifdef RECURSIVE
  1922. if (!*wild) *wild = recursive;
  1923. #endif /* RECURSIVE */
  1924. debug(F111,"cmifi sv wild",sv,*wild);
  1925. debug(F101,"cmifi y","",y);
  1926. if (dirflg && *wild && cdactive) {
  1927. if (y > 1) {
  1928. printf("?Wildcard matches more than one directory\n");
  1929. if (sv) free(sv);
  1930. if (np) free(np);
  1931. return(-9);
  1932. } else {
  1933. znext(*xp);
  1934. }
  1935. }
  1936. if (itsadir && d && !dirflg) { /* It's a directory and not wild */
  1937. if (sv) free(sv); /* and it's ok to parse directories */
  1938. if (np) free(np);
  1939. #ifndef NOLASTFILE
  1940. makestr(&lastfile,tmplastfile);
  1941. #endif /* NOLASTFILE */
  1942. return(x);
  1943. }
  1944. if (y == 0) { /* File was not found */
  1945. int dosearch = 0;
  1946. dosearch = (path != NULL); /* A search path was given */
  1947. if (dosearch) {
  1948. dosearch = hasnopath(sv); /* Filename includes no path */
  1949. debug(F111,"cmifip hasnopath",sv,dosearch);
  1950. }
  1951. if (dosearch) { /* Search the path... */
  1952. char * ptr = path;
  1953. char c;
  1954. while (1) {
  1955. c = *ptr;
  1956. if (c == PATHSEP || c == NUL) {
  1957. if (!*path) {
  1958. path = NULL;
  1959. break;
  1960. }
  1961. *ptr = NUL;
  1962. #ifdef UNIX
  1963. /* By definition of CDPATH, an empty member denotes the current directory */
  1964. if (!*path)
  1965. ckstrncpy(atmbuf,".",ATMBL);
  1966. else
  1967. #endif /* UNIX */
  1968. ckstrncpy(atmbuf,path,ATMBL);
  1969. #ifdef VMS
  1970. atmbuf[ATMBL] = NUL;
  1971. /* If we have a logical name, evaluate it recursively */
  1972. if (*(ptr-1) == ':') { /* Logical name ends in : */
  1973. char *p; int n;
  1974. while (((n = strlen(atmbuf)) > 0) &&
  1975. atmbuf[n-1] == ':') {
  1976. atmbuf[n-1] = NUL;
  1977. for (p = atmbuf; *p; p++)
  1978. if (islower(*p)) *p = toupper(*p);
  1979. debug(F111,"cmdir CDPATH LN 1",atmbuf,n);
  1980. p = getenv(atmbuf);
  1981. debug(F110,"cmdir CDPATH LN 2",p,0);
  1982. if (!p)
  1983. break;
  1984. strncpy(atmbuf,p,ATMBL);
  1985. atmbuf[ATMBL] = NUL;
  1986. }
  1987. }
  1988. #else
  1989. #ifdef OS2
  1990. if (*(ptr-1) != '\\' && *(ptr-1) != '/')
  1991. ckstrncat(atmbuf,"\\",ATMBL);
  1992. #else
  1993. #ifdef UNIX
  1994. if (*(ptr-1) != '/')
  1995. ckstrncat(atmbuf,"/",ATMBL);
  1996. #else
  1997. #ifdef datageneral
  1998. if (*(ptr-1) != ':')
  1999. ckstrncat(atmbuf,":",ATMBL);
  2000. #endif /* datageneral */
  2001. #endif /* UNIX */
  2002. #endif /* OS2 */
  2003. #endif /* VMS */
  2004. ckstrncat(atmbuf,sv,ATMBL);
  2005. debug(F110,"cmifip add path",atmbuf,0);
  2006. if (c == PATHSEP) ptr++;
  2007. path = ptr;
  2008. break;
  2009. }
  2010. ptr++;
  2011. }
  2012. x = 1;
  2013. inword = 0;
  2014. cc = 0;
  2015. xc = (int) strlen(atmbuf);
  2016. *xp = "";
  2017. goto i_path;
  2018. }
  2019. if (d) {
  2020. if (sv) free(sv);
  2021. if (np) free(np);
  2022. return(-2);
  2023. } else {
  2024. if (!nomsg) {
  2025. #ifdef CKROOT
  2026. if (ckrooterr)
  2027. printf("?Off Limits: %s\n",sv);
  2028. else
  2029. #endif /* CKROOT */
  2030. if (!quiet)
  2031. printf("?No %s match - %s\n",
  2032. dirflg ? "directories" : "files", sv);
  2033. }
  2034. if (sv) free(sv);
  2035. if (np) free(np);
  2036. return(-9);
  2037. }
  2038. } else if (y < 0) {
  2039. #ifdef CKROOT
  2040. if (ckrooterr)
  2041. printf("?Off Limits: %s\n",sv);
  2042. else
  2043. #endif /* CKROOT */
  2044. printf("?Too many %s match - %s\n",
  2045. dirflg ? "directories" : "files", sv);
  2046. if (sv) free(sv);
  2047. if (np) free(np);
  2048. return(-9);
  2049. } else if (*wild || y > 1) {
  2050. if (sv) free(sv);
  2051. if (np) free(np);
  2052. #ifndef NOLASTFILE
  2053. makestr(&lastfile,tmplastfile);
  2054. #endif /* NOLASTFILE */
  2055. return(x);
  2056. }
  2057. /* If not wild, see if it exists and is readable. */
  2058. debug(F111,"cmifi sv not wild",sv,*wild);
  2059. if (expanded)
  2060. znext(*xp); /* Get first (only?) matching file */
  2061. if (dirflg) /* Maybe wild and expanded */
  2062. itsadir = isdir(*xp); /* so do this again. */
  2063. filesize = dirflg ? itsadir : zchki(*xp); /* Check accessibility */
  2064. if (expanded) {
  2065. #ifdef ZXREWIND
  2066. nfiles = zxrewind(); /* Rewind so next znext() gets 1st */
  2067. #else
  2068. nzxopts |= dirflg ? ZX_DIRONLY : 0;
  2069. if (matchdot) nzxopts |= ZX_MATCHDOT;
  2070. if (recursive) nzxopts |= ZX_RECURSE;
  2071. nfiles = nzxpand(*xp,nzxopts);
  2072. #endif /* ZXREWIND */
  2073. }
  2074. debug(F111,"cmifi nfiles",*xp,nfiles);
  2075. debug(F101,"cmifi filesize","",filesize);
  2076. free(sv); /* done with this */
  2077. sv = NULL;
  2078. if (dirflg && !filesize) {
  2079. printf("?Not a directory - %s\n",*xp);
  2080. #ifdef CKCHANNELIO
  2081. z_error = FX_ACC;
  2082. #endif /* CKCHANNELIO */
  2083. return(-9);
  2084. } else if (filesize == (CK_OFF_T)-3) {
  2085. if (!xcmfdb) {
  2086. if (diractive)
  2087. /* Don't show filename if we're not allowed to see it */
  2088. printf("?Read permission denied\n");
  2089. else
  2090. printf("?Read permission denied - %s\n",*xp);
  2091. }
  2092. if (np) free(np);
  2093. #ifdef CKCHANNELIO
  2094. z_error = FX_ACC;
  2095. #endif /* CKCHANNELIO */
  2096. return(xcmfdb ? -6 : -9);
  2097. } else if (filesize == (CK_OFF_T)-2) {
  2098. if (!recursive) {
  2099. if (np) free(np);
  2100. if (d) {
  2101. #ifndef NOLASTFILE
  2102. makestr(&lastfile,tmplastfile);
  2103. #endif /* NOLASTFILE */
  2104. return(0);
  2105. }
  2106. if (!xcmfdb)
  2107. printf("?File not readable - %s\n",*xp);
  2108. #ifdef CKCHANNELIO
  2109. z_error = FX_ACC;
  2110. #endif /* CKCHANNELIO */
  2111. return(xcmfdb ? -6 : -9);
  2112. }
  2113. } else if (filesize < (CK_OFF_T)0) {
  2114. if (np) free(np);
  2115. if (!nomsg && !xcmfdb)
  2116. printf("?File not found - %s\n",*xp);
  2117. #ifdef CKCHANNELIO
  2118. z_error = FX_FNF;
  2119. #endif /* CKCHANNELIO */
  2120. return(xcmfdb ? -6 : -9);
  2121. }
  2122. if (np) free(np);
  2123. #ifndef NOLASTFILE
  2124. makestr(&lastfile,tmplastfile);
  2125. #endif /* NOLASTFILE */
  2126. return(x);
  2127. #ifndef MAC
  2128. case 2: /* ESC */
  2129. debug(F101,"cmifi esc, xc","",xc);
  2130. if (xc == 0) {
  2131. if (*xdef) {
  2132. printf("%s ",xdef); /* If at beginning of field */
  2133. #ifdef GEMDOS
  2134. fflush(stdout);
  2135. #endif /* GEMDOS */
  2136. inword = cmflgs = 0;
  2137. addbuf(xdef); /* Supply default. */
  2138. if (setatm(xdef,0) < 0) {
  2139. printf("Default name too long\n");
  2140. if (np) free(np);
  2141. return(-9);
  2142. }
  2143. } else { /* No default */
  2144. bleep(BP_WARN);
  2145. }
  2146. break;
  2147. }
  2148. if (**xp == '{') { /* Did user type opening brace... */
  2149. *xp = *xp + 1;
  2150. xc--;
  2151. cc--;
  2152. qflag = '}';
  2153. } else if (dblquo && **xp == '"') { /* or doublequote? */
  2154. *xp = *xp + 1; /* If so ignore it and space past it */
  2155. xc--;
  2156. cc--;
  2157. qflag = '"';
  2158. }
  2159. #ifndef NOSPL
  2160. if (f) { /* If a conversion function is given */
  2161. #ifdef DOCHKVAR
  2162. char *s = *xp; /* See if there are any variables in */
  2163. while (*s) { /* the string and if so, expand it. */
  2164. if (chkvar(s)) {
  2165. #endif /* DOCHKVAR */
  2166. zq = atxbuf;
  2167. atxn = CMDBL;
  2168. if ((x = (*f)(*xp,&zq,&atxn)) < 0) {
  2169. if (np) free(np);
  2170. return(-2);
  2171. }
  2172. #ifdef DOCHKVAR
  2173. /* reduce cc by number of \\ consumed by conversion */
  2174. /* function (needed for OS/2, where \ is path separator) */
  2175. cc -= (strlen(*xp) - strlen(atxbuf));
  2176. #endif /* DOCHKVAR */
  2177. *xp = atxbuf;
  2178. if (!atxbuf[0]) { /* Result empty, use default */
  2179. *xp = xdef;
  2180. cc = strlen(xdef);
  2181. }
  2182. #ifdef DOCHKVAR
  2183. break;
  2184. }
  2185. s++;
  2186. }
  2187. #endif /* DOCHKVAR */
  2188. }
  2189. #endif /* NOSPL */
  2190. #ifdef DTILDE
  2191. if (dirflg && *(*xp) == '~') {
  2192. debug(F111,"cmifi tilde_expand A",*xp,cc);
  2193. dirp = tilde_expand(*xp); /* Expand tilde, if any... */
  2194. if (!dirp) dirp = "";
  2195. if (*dirp) {
  2196. int i, xx;
  2197. char * sp;
  2198. xc = cc; /* Length of ~thing */
  2199. xx = setatm(dirp,0); /* Copy expansion to atom buffer */
  2200. debug(F111,"cmifi tilde_expand B",atmbuf,cc);
  2201. if (xx < 0) {
  2202. printf("Expanded name too long\n");
  2203. if (np) free(np);
  2204. return(-9);
  2205. }
  2206. debug(F111,"cmifi tilde_expand xc","",xc);
  2207. for (i = 0; i < xc; i++) {
  2208. cmdchardel(); /* Back up over ~thing */
  2209. bp--;
  2210. }
  2211. xc = cc; /* How many new ones we just got */
  2212. sp = atmbuf;
  2213. printf("%s",sp); /* Print them */
  2214. while ((*bp++ = *sp++)) ; /* Copy to command buffer */
  2215. bp--; /* Back up over NUL */
  2216. }
  2217. *xp = atmbuf;
  2218. }
  2219. #endif /* DTILDE */
  2220. sp = *xp + cc;
  2221. #ifdef UNIXOROSK
  2222. if (!strcmp(atmbuf,"..")) {
  2223. printf(" ");
  2224. ckstrncat(cmdbuf," ",CMDBL);
  2225. cc++;
  2226. bp++;
  2227. *wild = 0;
  2228. *xp = atmbuf;
  2229. break;
  2230. } else if (!strcmp(atmbuf,".")) {
  2231. bleep(BP_WARN);
  2232. if (np) free(np);
  2233. return(-1);
  2234. } else {
  2235. /* This patches a glitch when user types "./foo<ESC>" */
  2236. /* in which the next two chars are omitted from the */
  2237. /* expansion. There should be a better fix, however, */
  2238. /* since there is no problem with "../foo<ESC>". */
  2239. char *p = *xp;
  2240. if (*p == '.' && *(p+1) == '/')
  2241. cc -= 2;
  2242. }
  2243. #endif /* UNIXOROSK */
  2244. #ifdef datageneral
  2245. *sp++ = '+'; /* Data General AOS wildcard */
  2246. #else
  2247. *sp++ = '*'; /* Others */
  2248. #endif /* datageneral */
  2249. *sp-- = '\0';
  2250. #ifdef GEMDOS
  2251. if (!strchr(*xp, '.')) /* abde.e -> abcde.e* */
  2252. strcat(*xp, ".*"); /* abc -> abc*.* */
  2253. #endif /* GEMDOS */
  2254. /* Add wildcard and expand list. */
  2255. #ifdef COMMENT
  2256. /* This kills partial completion when ESC given in path segment */
  2257. nzxopts |= dirflg ? ZX_DIRONLY : (d ? 0 : ZX_FILONLY);
  2258. #else
  2259. /* nzxopts = 0; */
  2260. #endif /* COMMENT */
  2261. if (matchdot) nzxopts |= ZX_MATCHDOT;
  2262. if (recursive) nzxopts |= ZX_RECURSE;
  2263. y = nzxpand(*xp,nzxopts);
  2264. nfiles = y;
  2265. debug(F111,"cmifi nzxpand",*xp,y);
  2266. if (y > 0) {
  2267. #ifdef OS2
  2268. znext(filbuf); /* Get first */
  2269. #ifdef ZXREWIND
  2270. zxrewind(); /* Must "rewind" */
  2271. #else
  2272. nzxpand(*xp,nxzopts);
  2273. #endif /* ZXREWIND */
  2274. #else /* Not OS2 */
  2275. ckstrncpy(filbuf,mtchs[0],CKMAXPATH);
  2276. #endif /* OS2 */
  2277. } else
  2278. *filbuf = '\0';
  2279. filbuf[CKMAXPATH] = NUL;
  2280. *sp = '\0'; /* Remove wildcard. */
  2281. debug(F111,"cmifi filbuf",filbuf,y);
  2282. debug(F111,"cmifi *xp",*xp,cc);
  2283. *wild = (y > 1);
  2284. if (y == 0) {
  2285. if (!nomsg) {
  2286. #ifdef CKROOT
  2287. if (ckrooterr)
  2288. printf("?Off Limits: %s\n",atmbuf);
  2289. else
  2290. #endif /* CKROOT */
  2291. printf("?No %s match - %s\n",
  2292. dirflg ? "directories" : "files", atmbuf);
  2293. if (np) free(np);
  2294. return(-9);
  2295. } else {
  2296. bleep(BP_WARN);
  2297. if (np) free(np);
  2298. return(-1);
  2299. }
  2300. } else if (y < 0) {
  2301. #ifdef CKROOT
  2302. if (ckrooterr)
  2303. printf("?Off Limits: %s\n",atmbuf);
  2304. else
  2305. #endif /* CKROOT */
  2306. printf("?Too many %s match - %s\n",
  2307. dirflg ? "directories" : "files", atmbuf);
  2308. if (np) free(np);
  2309. return(-9);
  2310. } else if (y > 1 /* Not unique */
  2311. #ifndef VMS
  2312. || (y == 1 && isdir(filbuf)) /* Unique directory */
  2313. #endif /* VMS */
  2314. ) {
  2315. #ifndef NOPARTIAL
  2316. /* Partial filename completion */
  2317. int j, k; char c;
  2318. k = 0;
  2319. debug(F111,"cmifi partial",filbuf,cc);
  2320. #ifdef OS2
  2321. {
  2322. int cur = 0,
  2323. len = 0,
  2324. len2 = 0,
  2325. min = strlen(filbuf),
  2326. found = 0;
  2327. char localfn[CKMAXPATH+1];
  2328. len = min;
  2329. for (j = 1; j <= y; j++) {
  2330. znext(localfn);
  2331. if (dirflg && !isdir(localfn))
  2332. continue;
  2333. found = 1;
  2334. len2 = strlen(localfn);
  2335. for (cur = cc;
  2336. cur < len && cur < len2 && cur <= min;
  2337. cur++
  2338. ) {
  2339. /* OS/2 or Windows, case doesn't matter */
  2340. if (tolower(filbuf[cur]) != tolower(localfn[cur]))
  2341. break;
  2342. }
  2343. if (cur < min)
  2344. min = cur;
  2345. }
  2346. if (!found)
  2347. min = cc;
  2348. filbuf[min] = NUL;
  2349. if (min > cc)
  2350. k++;
  2351. }
  2352. #else /* OS2 */
  2353. for (i = cc; (c = filbuf[i]); i++) {
  2354. for (j = 1; j < y; j++)
  2355. if (mtchs[j][i] != c) break;
  2356. if (j == y) k++;
  2357. else filbuf[i] = filbuf[i+1] = NUL;
  2358. }
  2359. #endif /* OS2 */
  2360. #ifndef VMS
  2361. /* isdir() function required for this! */
  2362. if (y == 1 && isdir(filbuf)) { /* Dont we already know this? */
  2363. int len;
  2364. len = strlen(filbuf);
  2365. if (len > 0 && len < ATMBL - 1) {
  2366. if (filbuf[len-1] != dirsep) {
  2367. filbuf[len] = dirsep;
  2368. filbuf[len+1] = NUL;
  2369. }
  2370. }
  2371. /*
  2372. At this point, before just doing partial completion, we should look first to
  2373. see if the given directory does indeed have any subdirectories (dirflg) or
  2374. files (!dirflg); if it doesn't we should do full completion. Otherwise, the
  2375. result looks funny to the user and "?" blows up the command for no good
  2376. reason.
  2377. */
  2378. {
  2379. int flags = 0;
  2380. filbuf[len+1] = '*';
  2381. filbuf[len+2] = NUL;
  2382. if (dirflg) flags = ZX_DIRONLY;
  2383. children = nzxpand(filbuf,flags);
  2384. debug(F111,"cmifi children",filbuf,children);
  2385. filbuf[len+1] = NUL;
  2386. nzxpand(filbuf,flags); /* Restore previous list */
  2387. if (children == 0)
  2388. goto NOSUBDIRS;
  2389. }
  2390. if (len + 1 > cc)
  2391. k++;
  2392. }
  2393. /* Add doublequotes if there are spaces in the name */
  2394. {
  2395. int x;
  2396. if (qflag) {
  2397. x = (qflag == '}'); /* (or braces) */
  2398. } else {
  2399. x = !dblquo;
  2400. }
  2401. if (filbuf[0] != '"' && filbuf[0] != '{')
  2402. k = dquote(filbuf,ATMBL,x);
  2403. }
  2404. #endif /* VMS */
  2405. debug(F111,"cmifi REPAINT filbuf",filbuf,k);
  2406. if (k > 0) { /* Got more characters */
  2407. debug(F101,"cmifi REPAINT cc","",cc);
  2408. debug(F101,"cmifi REPAINT xc","",xc);
  2409. debug(F110,"cmifi REPAINT bp-cc",bp-cc,0);
  2410. debug(F110,"cmifi REPAINT bp-xc",bp-xc,0);
  2411. sp = filbuf + cc; /* Point to new ones */
  2412. if (qflag || strncmp(filbuf,bp-cc,cc)) { /* Repaint? */
  2413. int x;
  2414. x = cc;
  2415. if (qflag) x++;
  2416. for (i = 0; i < x; i++) {
  2417. cmdchardel(); /* Back up over old partial spec */
  2418. bp--;
  2419. }
  2420. sp = filbuf; /* Point to new word start */
  2421. debug(F110,"cmifi erase ok",sp,0);
  2422. }
  2423. cc = k; /* How many new ones we just got */
  2424. printf("%s",sp); /* Print them */
  2425. while ((*bp++ = *sp++)) ; /* Copy to command buffer */
  2426. bp--; /* Back up over NUL */
  2427. debug(F110,"cmifi partial cmdbuf",cmdbuf,0);
  2428. if (setatm(filbuf,0) < 0) {
  2429. printf("?Partial name too long\n");
  2430. if (np) free(np);
  2431. return(-9);
  2432. }
  2433. debug(F111,"cmifi partial atmbuf",atmbuf,cc);
  2434. *xp = atmbuf;
  2435. }
  2436. #endif /* NOPARTIAL */
  2437. bleep(BP_WARN);
  2438. } else { /* Unique, complete it. */
  2439. #ifndef VMS
  2440. #ifdef CK_TMPDIR
  2441. /* isdir() function required for this! */
  2442. NOSUBDIRS:
  2443. debug(F111,"cmifi unique",filbuf,children);
  2444. if (isdir(filbuf) && children > 0) {
  2445. int len;
  2446. len = strlen(filbuf);
  2447. if (len > 0 && len < ATMBL - 1) {
  2448. if (filbuf[len-1] != dirsep) {
  2449. filbuf[len] = dirsep;
  2450. filbuf[len+1] = NUL;
  2451. }
  2452. }
  2453. sp = filbuf + cc;
  2454. bleep(BP_WARN);
  2455. printf("%s",sp);
  2456. cc++;
  2457. while ((*bp++ = *sp++)) ;
  2458. bp--;
  2459. if (setatm(filbuf,0) < 0) {
  2460. printf("?Directory name too long\n");
  2461. if (np) free(np);
  2462. return(-9);
  2463. }
  2464. debug(F111,"cmifi directory atmbuf",atmbuf,cc);
  2465. *xp = atmbuf;
  2466. } else { /* Not a directory or dirflg */
  2467. #endif /* CK_TMPDIR */
  2468. #endif /* VMS */
  2469. #ifndef VMS /* VMS dir names are special */
  2470. #ifndef datageneral /* VS dirnames must not end in ":" */
  2471. if (dirflg) {
  2472. int len;
  2473. len = strlen(filbuf);
  2474. if (len > 0 && len < ATMBL - 1) {
  2475. if (filbuf[len-1] != dirsep) {
  2476. filbuf[len] = dirsep;
  2477. filbuf[len+1] = NUL;
  2478. }
  2479. }
  2480. }
  2481. #endif /* datageneral */
  2482. #endif /* VMS */
  2483. sp = filbuf + cc; /* Point past what user typed. */
  2484. {
  2485. int x;
  2486. if (qflag) {
  2487. x = (qflag == '}');
  2488. } else {
  2489. x = !dblquo;
  2490. }
  2491. if (filbuf[0] != '"' && filbuf[0] != '{')
  2492. dquote(filbuf,ATMBL,x);
  2493. }
  2494. if (qflag || strncmp(filbuf,bp-cc,cc)) { /* Repaint? */
  2495. int x;
  2496. x = cc;
  2497. if (qflag) x++;
  2498. for (i = 0; i < x; i++) {
  2499. cmdchardel(); /* Back up over old partial spec */
  2500. bp--;
  2501. }
  2502. sp = filbuf; /* Point to new word start */
  2503. debug(F111,"cmifi after erase sp=",sp,cc);
  2504. }
  2505. printf("%s ",sp); /* Print the completed name. */
  2506. #ifdef GEMDOS
  2507. fflush(stdout);
  2508. #endif /* GEMDOS */
  2509. addbuf(sp); /* Add the characters to cmdbuf. */
  2510. if (setatm(filbuf,0) < 0) { /* And to atmbuf. */
  2511. printf("?Completed name too long\n");
  2512. if (np) free(np);
  2513. return(-9);
  2514. }
  2515. inword = cmflgs = 0;
  2516. *xp = brstrip(atmbuf); /* Return pointer to atmbuf. */
  2517. if (dirflg && !isdir(*xp)) {
  2518. printf("?Not a directory - %s\n", filbuf);
  2519. if (np) free(np);
  2520. return(-9);
  2521. }
  2522. if (np) free(np);
  2523. #ifndef NOLASTFILE
  2524. makestr(&lastfile,tmplastfile);
  2525. #endif /* NOLASTFILE */
  2526. return(0);
  2527. #ifndef VMS
  2528. #ifdef CK_TMPDIR
  2529. }
  2530. #endif /* CK_TMPDIR */
  2531. #endif /* VMS */
  2532. }
  2533. break;
  2534. case 3: /* Question mark - file menu wanted */
  2535. if (*xhlp == NUL)
  2536. printf(dirflg ? " Directory name" : " Input file specification");
  2537. else
  2538. printf(" %s",xhlp);
  2539. #ifdef GEMDOS
  2540. fflush(stdout);
  2541. #endif /* GEMDOS */
  2542. /* If user typed an opening quote or brace, just skip past it */
  2543. if (**xp == '"' || **xp == '{') {
  2544. *xp = *xp + 1;
  2545. xc--;
  2546. cc--;
  2547. }
  2548. #ifndef NOSPL
  2549. if (f) { /* If a conversion function is given */
  2550. #ifdef DOCHKVAR
  2551. char *s = *xp; /* See if there are any variables in */
  2552. while (*s) { /* the string and if so, expand them */
  2553. if (chkvar(s)) {
  2554. #endif /* DOCHKVAR */
  2555. zq = atxbuf;
  2556. atxn = CMDBL;
  2557. if ((x = (*f)(*xp,&zq,&atxn)) < 0) {
  2558. if (np) free(np);
  2559. return(-2);
  2560. }
  2561. #ifdef DOCHKVAR
  2562. /* reduce cc by number of \\ consumed by conversion */
  2563. /* function (needed for OS/2, where \ is path separator) */
  2564. cc -= (strlen(*xp) - strlen(atxbuf));
  2565. #endif /* DOCHKVAR */
  2566. *xp = atxbuf;
  2567. #ifdef DOCHKVAR
  2568. break;
  2569. }
  2570. s++;
  2571. }
  2572. #endif /* DOCHKVAR */
  2573. }
  2574. #endif /* NOSPL */
  2575. debug(F111,"cmifi ? *xp, cc",*xp,cc);
  2576. sp = *xp + cc; /* Insert "*" at end */
  2577. #ifdef datageneral
  2578. *sp++ = '+'; /* Insert +, the DG wild card */
  2579. #else
  2580. *sp++ = '*';
  2581. #endif /* datageneral */
  2582. *sp-- = '\0';
  2583. #ifdef GEMDOS
  2584. if (! strchr(*xp, '.')) /* abde.e -> abcde.e* */
  2585. strcat(*xp, ".*"); /* abc -> abc*.* */
  2586. #endif /* GEMDOS */
  2587. debug(F110,"cmifi ? wild",*xp,0);
  2588. nzxopts |= dirflg ? ZX_DIRONLY : (d ? 0 : ZX_FILONLY);
  2589. debug(F101,"cmifi matchdot","",matchdot);
  2590. if (matchdot) nzxopts |= ZX_MATCHDOT;
  2591. if (recursive) nzxopts |= ZX_RECURSE;
  2592. y = nzxpand(*xp,nzxopts);
  2593. nfiles = y;
  2594. *sp = '\0';
  2595. if (y == 0) {
  2596. if (nomsg) {
  2597. printf(": %s\n",atmbuf);
  2598. printf("%s%s",cmprom,cmdbuf);
  2599. fflush(stdout);
  2600. if (np) free(np);
  2601. return(-1);
  2602. } else {
  2603. #ifdef CKROOT
  2604. if (ckrooterr)
  2605. printf("?Off Limits: %s\n",atmbuf);
  2606. else
  2607. #endif /* CKROOT */
  2608. printf("?No %s match - %s\n",
  2609. dirflg ? "directories" : "files", atmbuf);
  2610. if (np) free(np);
  2611. return(-9);
  2612. }
  2613. } else if (y < 0) {
  2614. #ifdef CKROOT
  2615. if (ckrooterr)
  2616. printf("?Off Limits: %s\n",atmbuf);
  2617. else
  2618. #endif /* CKROOT */
  2619. printf("?Too many %s match - %s\n",
  2620. dirflg ? "directories" : "files", atmbuf);
  2621. if (np) free(np);
  2622. return(-9);
  2623. } else {
  2624. printf(", one of the following:\n");
  2625. if (filhelp((int)y,"","",1,dirflg) < 0) {
  2626. if (np) free(np);
  2627. return(-9);
  2628. }
  2629. }
  2630. printf("%s%s",cmprom,cmdbuf);
  2631. fflush(stdout);
  2632. break;
  2633. #endif /* MAC */
  2634. }
  2635. #ifdef BS_DIRSEP
  2636. dirnamflg = 1;
  2637. x = gtword(0); /* No, get a word */
  2638. dirnamflg = 0;
  2639. #else
  2640. x = gtword(0); /* No, get a word */
  2641. #endif /* BS_DIRSEP */
  2642. *xp = atmbuf;
  2643. }
  2644. }
  2645. /* C M F L D -- Parse an arbitrary field */
  2646. /*
  2647. Returns:
  2648. -3 if no input present when required,
  2649. -2 if field too big for buffer,
  2650. -1 if reparse needed,
  2651. 0 otherwise, xp pointing to string result.
  2652. NOTE: Global flag keepallchars says whether this routine should break on CR
  2653. or LF: needed for MINPUT targets and DECLARE initializers, where we want to
  2654. keep control characters if the user specifies them (March 2003). It might
  2655. have been better to change the calling sequence but that was not practical.
  2656. */
  2657. int
  2658. cmfld(xhlp,xdef,xp,f) char *xhlp, *xdef, **xp; xx_strp f; {
  2659. int x, xc, isavar = 0;
  2660. char *zq;
  2661. inword = 0; /* Initialize counts & pointers */
  2662. cc = 0;
  2663. xc = 0;
  2664. *xp = "";
  2665. debug(F110,"cmfld xdef 1",xdef,0);
  2666. if (!xhlp) xhlp = "";
  2667. if (!xdef) xdef = "";
  2668. ckstrncpy(cmdefault,xdef,CMDEFAULT); /* Copy default */
  2669. xdef = cmdefault;
  2670. debug(F111,"cmfld xdef 2",xdef,cmflgs);
  2671. debug(F111,"cmfld atmbuf 1",atmbuf,xc);
  2672. if ((x = cmflgs) != 1) { /* Already confirmed? */
  2673. x = gtword(0); /* No, get a word */
  2674. } else {
  2675. if (setatm(xdef,0) < 0) { /* If so, use default, if any. */
  2676. printf("?Default too long\n");
  2677. return(-9);
  2678. }
  2679. }
  2680. *xp = atmbuf; /* Point to result. */
  2681. debug(F111,"cmfld atmbuf 2",atmbuf,cmflgs);
  2682. while (1) {
  2683. xc += cc; /* Count the characters. */
  2684. debug(F111,"cmfld gtword",atmbuf,xc);
  2685. debug(F101,"cmfld x","",x);
  2686. switch (x) {
  2687. case -9:
  2688. printf("Command or field too long\n");
  2689. case -4: /* EOF */
  2690. case -3: /* Empty. */
  2691. case -2: /* Out of space. */
  2692. case -1: /* Reparse needed */
  2693. return(x);
  2694. case 1: /* CR */
  2695. case 0: /* SP */
  2696. debug(F111,"cmfld 1",atmbuf,xc);
  2697. if (xc == 0) { /* If no input, return default. */
  2698. if (setatm(xdef,0) < 0) {
  2699. printf("?Default too long\n");
  2700. return(-9);
  2701. }
  2702. }
  2703. *xp = atmbuf; /* Point to what we got. */
  2704. debug(F111,"cmfld 2",atmbuf,((f) ? 1 : 0));
  2705. if (f) { /* If a conversion function is given */
  2706. zq = atxbuf; /* employ it now. */
  2707. atxn = CMDBL;
  2708. if ((*f)(*xp,&zq,&atxn) < 0)
  2709. return(-2);
  2710. debug(F111,"cmfld 3",atxbuf,xc);
  2711. /*
  2712. fdc 2013/12/06 - allow a field to be empty if it is
  2713. the name of a variable that has no value.
  2714. */
  2715. isavar = (atmbuf[0] == '\\'); /* Remember if it was a var */
  2716. /* Replace by new value -- for MINPUT only keep all chars */
  2717. if (setatm(atxbuf,keepallchars ? 3:1) < 0) { /* 16 Mar 2003 */
  2718. printf("Value too long\n");
  2719. return(-9);
  2720. }
  2721. *xp = atmbuf;
  2722. }
  2723. debug(F111,"cmfld 4",atmbuf,xc);
  2724. if (**xp == NUL) { /* If variable evaluates to null */
  2725. if (setatm(xdef,0) < 0) {
  2726. printf("?Default too long\n");
  2727. return(-9);
  2728. }
  2729. /* If still empty, return -3 unless it was a variable */
  2730. if (**xp == NUL) x = (isavar ? 0 : -3); /* fdc 2013/12/06 */
  2731. }
  2732. debug(F111,"cmfld returns",*xp,x);
  2733. return(x);
  2734. case 2: /* ESC */
  2735. if (xc == 0 && *xdef) {
  2736. printf("%s ",xdef); /* If at beginning of field, */
  2737. #ifdef GEMDOS
  2738. fflush(stdout);
  2739. #endif /* GEMDOS */
  2740. addbuf(xdef); /* Supply default. */
  2741. inword = cmflgs = 0;
  2742. if (setatm(xdef,0) < 0) {
  2743. printf("?Default too long\n");
  2744. return(-9);
  2745. } else /* Return as if whole field */
  2746. return(0); /* typed, followed by space. */
  2747. } else {
  2748. bleep(BP_WARN);
  2749. }
  2750. break;
  2751. case 3: /* Question mark */
  2752. debug(F110,"cmfld QUESTIONMARK",cmdbuf,0);
  2753. if (*xhlp == NUL)
  2754. printf(" Please complete this field");
  2755. else
  2756. printf(" %s",xhlp);
  2757. printf("\n%s%s",cmprom,cmdbuf);
  2758. fflush(stdout);
  2759. break;
  2760. }
  2761. debug(F111,"cmfld gtword A x",cmdbuf,x);
  2762. x = gtword(0);
  2763. debug(F111,"cmfld gtword B x",cmdbuf,x);
  2764. }
  2765. }
  2766. /* C M T X T -- Get a text string, including confirmation */
  2767. /*
  2768. Print help message 'xhlp' if ? typed, supply default 'xdef' if null
  2769. string typed. Returns:
  2770. -1 if reparse needed or buffer overflows.
  2771. 1 otherwise.
  2772. with cmflgs set to return code, and xp pointing to result string.
  2773. */
  2774. int
  2775. cmtxt(xhlp,xdef,xp,f) char *xhlp; char *xdef; char **xp; xx_strp f; {
  2776. int x, i;
  2777. char *xx, *zq;
  2778. static int xc;
  2779. if (!xhlp) xhlp = "";
  2780. if (!xdef) xdef = "";
  2781. cmfldflgs = 0;
  2782. cmdefault[0] = NUL;
  2783. if (*xdef)
  2784. ckstrncpy(cmdefault,xdef,CMDEFAULT); /* Copy default */
  2785. xdef = cmdefault;
  2786. debug(F101,"cmtxt cmflgs","",cmflgs);
  2787. inword = 0; /* Start atmbuf counter off at 0 */
  2788. cc = 0;
  2789. if (cmflgs == -1) { /* If reparsing, */
  2790. *xp = pp;
  2791. xc = (int)strlen(*xp); /* get back the total text length, */
  2792. bp = *xp; /* and back up the pointers. */
  2793. np = *xp;
  2794. pp = *xp;
  2795. } else { /* otherwise, */
  2796. /* debug(F100,"cmtxt: fresh start","",0); */
  2797. *xp = ""; /* start fresh. */
  2798. xc = 0;
  2799. }
  2800. *atmbuf = NUL; /* And empty the atom buffer. */
  2801. rtimer(); /* Reset timer */
  2802. if ((x = cmflgs) != 1) {
  2803. int done = 0;
  2804. while (!done) {
  2805. x = gtword(0); /* Get first word. */
  2806. *xp = pp; /* Save pointer to it. */
  2807. /* debug(F111,"cmtxt:",*xp,cc); */
  2808. if (x == -10) {
  2809. if (gtimer() > timelimit) {
  2810. /* if (!quiet) printf("?Timed out\n"); */
  2811. return(x);
  2812. }
  2813. } else
  2814. done = 1;
  2815. }
  2816. }
  2817. while (1) { /* Loop for each word in text. */
  2818. xc += cc; /* Char count for all words. */
  2819. /* debug(F111,"cmtxt gtword",atmbuf,xc); */
  2820. /* debug(F101,"cmtxt x","",x); */
  2821. switch (x) {
  2822. case -10:
  2823. if (gtimer() > timelimit) {
  2824. #ifdef IKSD
  2825. extern int inserver;
  2826. if (inserver) {
  2827. printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  2828. doexit(GOOD_EXIT,0);
  2829. }
  2830. #endif /* IKSD */
  2831. /* if (!quiet) printf("?Timed out\n"); */
  2832. return(-10);
  2833. } else {
  2834. x = gtword(0);
  2835. continue;
  2836. }
  2837. case -9: /* Buffer overflow */
  2838. printf("Command or field too long\n");
  2839. case -4: /* EOF */
  2840. #ifdef MAC
  2841. case -3: /* Quit/Timeout */
  2842. #endif /* MAC */
  2843. case -2: /* Overflow */
  2844. case -1: /* Deletion */
  2845. return(x);
  2846. case 0: /* Space */
  2847. xc++; /* Just count it */
  2848. break;
  2849. case 1: /* CR or LF */
  2850. if (xc == 0) *xp = xdef;
  2851. if (f) { /* If a conversion function is given */
  2852. char * sx = atxbuf;
  2853. zq = atxbuf; /* Point to the expansion buffer */
  2854. atxn = CMDBL; /* specify its length */
  2855. /* debug(F111,"cmtxt calling (*f)",*xp,atxbuf); */
  2856. if ((x = (*f)(*xp,&zq,&atxn)) < 0) return(-2);
  2857. sx = atxbuf;
  2858. #ifndef COMMENT
  2859. cc = 0;
  2860. while (*sx++) cc++; /* (faster than calling strlen) */
  2861. #else
  2862. cc = (int)strlen(atxbuf);
  2863. #endif /* COMMENT */
  2864. /* Should be equal to (CMDBL - atxn) but isn't always. */
  2865. /* Why not? */
  2866. if (cc < 1) { /* Nothing in expansion buffer? */
  2867. *xp = xdef; /* Point to default string instead. */
  2868. #ifndef COMMENT
  2869. sx = xdef;
  2870. while (*sx++) cc++; /* (faster than calling strlen) */
  2871. #else
  2872. cc = strlen(xdef);
  2873. #endif /* COMMENT */
  2874. } else { /* Expansion function got something */
  2875. *xp = atxbuf; /* return pointer to it. */
  2876. }
  2877. debug(F111,"cmtxt (*f)",*xp,cc);
  2878. } else { /* No expansion function */
  2879. #ifndef COMMENT
  2880. /* Avoid a strlen() call */
  2881. xx = *xp;
  2882. cc = 0;
  2883. while (*xx++) cc++;
  2884. #else
  2885. /* NO! xc is apparently not always set appropriately */
  2886. cc = xc;
  2887. #endif /* COMMENT */
  2888. }
  2889. xx = *xp;
  2890. #ifdef COMMENT
  2891. /* strlen() no longer needed */
  2892. for (i = (int)strlen(xx) - 1; i > 0; i--)
  2893. #else
  2894. for (i = cc - 1; i > 0; i--)
  2895. #endif /* COMMENT */
  2896. if (xx[i] != SP) /* Trim trailing blanks */
  2897. break;
  2898. else
  2899. xx[i] = NUL;
  2900. return(x);
  2901. case 2: /* ESC */
  2902. if (xc == 0) { /* Nothing typed yet */
  2903. if (*xdef) { /* Have a default for this field? */
  2904. printf("%s ",xdef); /* Yes, supply it */
  2905. inword = cmflgs = 0;
  2906. #ifdef GEMDOS
  2907. fflush(stdout);
  2908. #endif /* GEMDOS */
  2909. cc = addbuf(xdef);
  2910. } else bleep(BP_WARN); /* No default */
  2911. } else { /* Already in field */
  2912. int x; char *p;
  2913. x = strlen(atmbuf);
  2914. if (ckstrcmp(atmbuf,xdef,x,0)) { /* Matches default? */
  2915. bleep(BP_WARN); /* No */
  2916. } else if ((int)strlen(xdef) > x) { /* Yes */
  2917. p = xdef + x;
  2918. printf("%s ", p);
  2919. #ifdef GEMDOS
  2920. fflush(stdout);
  2921. #endif /* GEMDOS */
  2922. addbuf(p);
  2923. inword = cmflgs = 0;
  2924. debug(F110,"cmtxt: addbuf",cmdbuf,0);
  2925. } else {
  2926. bleep(BP_WARN);
  2927. }
  2928. }
  2929. break;
  2930. case 3: /* Question Mark */
  2931. if (*xhlp == NUL)
  2932. printf(" Text string");
  2933. else
  2934. printf(" %s",xhlp);
  2935. printf("\n%s%s",cmprom,cmdbuf);
  2936. fflush(stdout);
  2937. break;
  2938. default:
  2939. printf("?Unexpected return code from gtword() - %d\n",x);
  2940. return(-2);
  2941. }
  2942. x = gtword(0);
  2943. }
  2944. }
  2945. /* C M K E Y -- Parse a keyword */
  2946. /*
  2947. Call with:
  2948. table -- keyword table, in 'struct keytab' format;
  2949. n -- number of entries in table;
  2950. xhlp -- pointer to help string;
  2951. xdef -- pointer to default keyword;
  2952. f -- string preprocessing function (e.g. to evaluate variables)
  2953. pmsg -- 0 = don't print error messages
  2954. 1 = print error messages
  2955. 2 = include CM_HLP keywords even if invisible
  2956. 3 = 1+2
  2957. 4 = parse a switch (keyword possibly ending in : or =)
  2958. 8 = don't strip comments (used, e.g., for "help #")
  2959. Returns:
  2960. -3 -- no input supplied and no default available
  2961. -2 -- input doesn't uniquely match a keyword in the table
  2962. -1 -- user deleted too much, command reparse required
  2963. n >= 0 -- value associated with keyword
  2964. */
  2965. /*
  2966. Front ends for cmkey2():
  2967. cmkey() - The normal keyword parser
  2968. cmkeyx() - Like cmkey() but suppresses error messages
  2969. cmswi() - Switch parser
  2970. */
  2971. int
  2972. cmkey(table,n,xhlp,xdef,f)
  2973. /* cmkey */ struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  2974. return(cmkey2(table,n,xhlp,xdef,"",f,1));
  2975. }
  2976. int
  2977. cmkeyx(table,n,xhlp,xdef,f)
  2978. /* cmkeyx */ struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  2979. return(cmkey2(table,n,xhlp,xdef,"",f,0));
  2980. }
  2981. int
  2982. cmswi(table,n,xhlp,xdef,f)
  2983. /* cmswi */ struct keytab table[]; int n; char *xhlp, *xdef; xx_strp f; {
  2984. return(cmkey2(table,n,xhlp,xdef,"",f,4));
  2985. }
  2986. int
  2987. cmkey2(table,n,xhlp,xdef,tok,f,pmsg)
  2988. struct keytab table[];
  2989. int n;
  2990. char *xhlp, *xdef;
  2991. char *tok;
  2992. xx_strp f;
  2993. int pmsg;
  2994. { /* cmkey2 */
  2995. extern int havetoken;
  2996. int i, tl, y, z = 0, zz, xc, wordlen = 0, cmswitch;
  2997. char *xp, *zq;
  2998. if (!xhlp) xhlp = "";
  2999. if (!xdef) xdef = "";
  3000. cmfldflgs = 0;
  3001. if (!table) {
  3002. printf("?Keyword table missing\n");
  3003. return(-9);
  3004. }
  3005. tl = (int)strlen(tok);
  3006. inword = xc = cc = 0; /* Clear character counters. */
  3007. cmswitch = pmsg & 4; /* Flag for parsing a switch */
  3008. debug(F101,"cmkey: pmsg","",pmsg);
  3009. debug(F101,"cmkey: cmflgs","",cmflgs);
  3010. debug(F101,"cmkey: cmswitch","",cmswitch);
  3011. /* debug(F101,"cmkey: cmdbuf","",cmdbuf);*/
  3012. ppvnambuf[0] = NUL;
  3013. #if 0
  3014. for(i = 0; i < n; i++) {
  3015. printf("");
  3016. printf("TABLE[%d] kwd=[%s] kwval=%d\n",i,table[i].kwd,table[i].kwval);
  3017. }
  3018. #endif
  3019. if ((zz = cmflgs) == 1) { /* Command already entered? */
  3020. if (setatm(xdef,0) < 0) { /* Yes, copy default into atom buf */
  3021. printf("?Default too long\n");
  3022. return(-9);
  3023. }
  3024. rtimer(); /* Reset timer */
  3025. } else { /* Otherwise get a command word */
  3026. rtimer(); /* Reset timer */
  3027. if (pmsg & 8) /* 8 is for parsing HELP tokens */
  3028. zz = gtword(4);
  3029. else
  3030. zz = gtword((pmsg == 4) ? 1 : 0);
  3031. }
  3032. debug(F101,"cmkey table length","",n);
  3033. debug(F101,"cmkey cmflgs","",cmflgs);
  3034. debug(F101,"cmkey cc","",cc);
  3035. while (1) {
  3036. xc += cc;
  3037. debug(F111,"cmkey gtword xc",atmbuf,xc);
  3038. debug(F101,"cmkey gtword zz","",zz);
  3039. switch (zz) {
  3040. case -10: /* Timeout */
  3041. if (gtimer() < timelimit) {
  3042. if (pmsg & 8) /* 8 is for parsing HELP tokens */
  3043. zz = gtword(4);
  3044. else
  3045. zz = gtword((pmsg == 4) ? 1 : 0);
  3046. continue;
  3047. } else {
  3048. #ifdef IKSD
  3049. extern int inserver;
  3050. if (inserver) {
  3051. printf("\r\nIKSD IDLE TIMEOUT: %d sec\r\n", timelimit);
  3052. doexit(GOOD_EXIT,0);
  3053. }
  3054. #endif /* IKSD */
  3055. return(-10);
  3056. }
  3057. case -5:
  3058. return(cmflgs = 0);
  3059. case -9:
  3060. printf("Command or field too long\n");
  3061. case -4: /* EOF */
  3062. case -3: /* Null Command/Quit/Timeout */
  3063. case -2: /* Buffer overflow */
  3064. case -1: /* Or user did some deleting. */
  3065. return(cmflgs = zz);
  3066. case 1: /* CR */
  3067. case 0: /* User terminated word with space */
  3068. case 4: /* or switch ending in : or = */
  3069. wordlen = cc; /* Length if no conversion */
  3070. if (cc == 0) { /* Supply default if we got nothing */
  3071. if ((wordlen = setatm(xdef,(zz == 4) ? 2 : 0)) < 0) {
  3072. printf("?Default too long\n");
  3073. return(-9);
  3074. }
  3075. }
  3076. if (zz == 1 && cc == 0) /* Required field missing */
  3077. return(-3);
  3078. if (f) { /* If a conversion function is given */
  3079. char * p2;
  3080. zq = atxbuf; /* apply it */
  3081. p2 = atxbuf;
  3082. atxn = CMDBL;
  3083. if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  3084. debug(F110,"cmkey atxbuf after *f",atxbuf,0);
  3085. if (!*p2) /* Supply default if we got nothing */
  3086. p2 = xdef;
  3087. ckstrncpy(ppvnambuf,atmbuf,PPVLEN);
  3088. if ((wordlen = setatm(p2,(zz == 4) ? 2 : 0)) < 0) {
  3089. printf("Evaluated keyword too long\n");
  3090. return(-9);
  3091. }
  3092. #ifdef M_UNGW
  3093. /*
  3094. This bit lets us save more than one "word".
  3095. For example, "define \%x echo one two three", "\%x".
  3096. It works too, but it breaks labels, and therefore
  3097. WHILE and FOR loops, etc.
  3098. */
  3099. if (p2[wordlen] >= SP) {
  3100. p2 += wordlen;
  3101. while (*p2 == SP) p2++;
  3102. if (*p2) {
  3103. ungword();
  3104. pp = p2;
  3105. }
  3106. }
  3107. #endif /* M_UNGW */
  3108. }
  3109. #ifdef COMMENT /* ^^^ */
  3110. if (cmswitch && *atmbuf != '/') {
  3111. if (pmsg & 1) {
  3112. bleep(BP_FAIL);
  3113. printf("?Not a switch - %s\n",atmbuf);
  3114. }
  3115. cmflgs = -2;
  3116. return(-6);
  3117. }
  3118. #endif /* COMMENT */
  3119. if (cmswitch) {
  3120. int i;
  3121. for (i = 0; i < wordlen; i++) {
  3122. if (atmbuf[i] == ':' || atmbuf[i] == '=') {
  3123. brkchar = atmbuf[i];
  3124. atmbuf[i] = NUL;
  3125. break;
  3126. }
  3127. }
  3128. }
  3129. #ifdef TOKPRECHECK
  3130. /* This was an effective optimization but it breaks sometimes on labels. */
  3131. if (tl && !isalpha(atmbuf[0])) { /* Precheck for token */
  3132. for (i = 0; i < tl; i++) { /* Save function call to ckstrchr */
  3133. if (tok[i] == atmbuf[0]) {
  3134. debug(F000,"cmkey token:",atmbuf,*atmbuf);
  3135. ungword(); /* Put back the following word */
  3136. return(-5); /* Special return code for token */
  3137. }
  3138. }
  3139. }
  3140. #endif /* TOKPRECHECK */
  3141. y = lookup(table,atmbuf,n,&z); /* Look up word in the table */
  3142. //by linwei
  3143. #if 0
  3144. if(atmbuf != NULL)
  3145. printf("atmbuf=%s,nz=%d\n",atmbuf,z);
  3146. for(i = 0; i < n; i++) {
  3147. printf("TABLE[%d] kwd=[%s] kwval=%d\n",i,table[i].kwd,table[i].kwval);
  3148. }
  3149. #endif
  3150. debug(F111,"cmkey lookup",atmbuf,y);
  3151. debug(F101,"cmkey zz","",zz);
  3152. debug(F101,"cmkey cmflgs","",cmflgs);
  3153. debug(F101,"cmkey crflag","",crflag);
  3154. switch (y) {
  3155. case -3: /* Nothing to look up */
  3156. break;
  3157. case -2: /* Ambiguous */
  3158. cmflgs = -2;
  3159. if (pmsg & 1) {
  3160. bleep(BP_FAIL);
  3161. printf("?Ambiguous - %s\n",atmbuf);
  3162. return(-9);
  3163. }
  3164. return(-2);
  3165. case -1: /* Not found at all */
  3166. #ifndef TOKPRECHECK
  3167. if (tl) {
  3168. for (i = 0; i < tl; i++) /* Check for token */
  3169. if (tok[i] == *atmbuf) { /* Got one */
  3170. debug(F000,"cmkey token:",atmbuf,*atmbuf);
  3171. ungword(); /* Put back the following word */
  3172. return(-5); /* Special return code for token */
  3173. }
  3174. }
  3175. #endif /* TOKPRECHECK */
  3176. if (tl == 0) { /* No tokens were included */
  3177. #ifdef OS2
  3178. /* In OS/2 and Windows, allow for a disk letter like DOS */
  3179. if (isalpha(*atmbuf) && *(atmbuf+1) == ':')
  3180. return(-7);
  3181. #endif /* OS2 */
  3182. if ((pmsg & 1) && !quiet) {
  3183. bleep(BP_FAIL);
  3184. printf("?No keywords match - %s\n",atmbuf); /* cmkey */
  3185. }
  3186. return(cmflgs = -9);
  3187. } else {
  3188. if (cmflgs == 1 || cmswitch) /* cmkey2 or cmswi */
  3189. return(cmflgs = -6);
  3190. else
  3191. return(cmflgs = -2);
  3192. /* The -6 code is to let caller try another table */
  3193. }
  3194. break;
  3195. default:
  3196. #ifdef CK_RECALL
  3197. if (test(table[z].flgs,CM_NOR)) no_recall = 1;
  3198. #endif /* CK_RECALL */
  3199. if (zz == 4)
  3200. swarg = 1;
  3201. cmkwflgs = table[z].flgs;
  3202. break;
  3203. }
  3204. return(y);
  3205. case 2: /* User terminated word with ESC */
  3206. debug(F101,"cmkey Esc cc","",cc);
  3207. if (cc == 0) {
  3208. if (*xdef != NUL) { /* Nothing in atmbuf */
  3209. printf("%s ",xdef); /* Supply default if any */
  3210. #ifdef GEMDOS
  3211. fflush(stdout);
  3212. #endif /* GEMDOS */
  3213. addbuf(xdef);
  3214. if (setatm(xdef,0) < 0) {
  3215. printf("?Default too long\n");
  3216. return(-9);
  3217. }
  3218. inword = cmflgs = 0;
  3219. debug(F111,"cmkey: default",atmbuf,cc);
  3220. } else {
  3221. debug(F101,"cmkey Esc pmsg","",0);
  3222. #ifdef COMMENT
  3223. /*
  3224. Chained FDBs... The idea is that this function might not have a default,
  3225. but the next one might. But if it doesn't, there is no way to come back to
  3226. this one. To be revisited later...
  3227. */
  3228. if (xcmfdb) /* Chained fdb -- try next one */
  3229. return(-3);
  3230. #endif /* COMMENT */
  3231. if (pmsg & (1|4)) { /* So for now just beep */
  3232. bleep(BP_WARN);
  3233. }
  3234. break;
  3235. }
  3236. }
  3237. if (f) { /* If a conversion function is given */
  3238. char * pp;
  3239. zq = atxbuf; /* apply it */
  3240. pp = atxbuf;
  3241. atxn = CMDBL;
  3242. if ((*f)(atmbuf,&zq,&atxn) < 0)
  3243. return(-2);
  3244. if (!*pp)
  3245. pp = xdef;
  3246. if (setatm(pp,0) < 0) {
  3247. printf("Evaluated keyword too long\n");
  3248. return(-9);
  3249. }
  3250. }
  3251. y = lookup(table,atmbuf,n,&z); /* Something in atmbuf */
  3252. debug(F111,"cmkey lookup y",atmbuf,y);
  3253. debug(F111,"cmkey lookup z",atmbuf,z);
  3254. if (y == -2 && z >= 0 && z < n) { /* Ambiguous */
  3255. #ifndef NOPARTIAL
  3256. int j, k, len = 9999; /* Do partial completion */
  3257. /* Skip past any abbreviations in the table */
  3258. for ( ; z < n; z++) {
  3259. if ((table[z].flgs & CM_ABR) == 0)
  3260. break;
  3261. if (!(table[z].flgs & CM_HLP) || (pmsg & 2))
  3262. break;
  3263. }
  3264. debug(F111,"cmkey partial z",atmbuf,z);
  3265. debug(F111,"cmkey partial n",atmbuf,n);
  3266. for (j = z+1; j < n; j++) {
  3267. debug(F111,"cmkey partial j",table[j].kwd,j);
  3268. if (ckstrcmp(atmbuf,table[j].kwd,cc,0))
  3269. break;
  3270. if (table[j].flgs & CM_ABR)
  3271. continue;
  3272. if ((table[j].flgs & CM_HLP) && !(pmsg & 2))
  3273. continue;
  3274. k = ckstrpre(table[z].kwd,table[j].kwd);
  3275. debug(F111,"cmkey partial k",table[z].kwd,k);
  3276. if (k < len)
  3277. len = k; /* Length of longest common prefix */
  3278. }
  3279. debug(F111,"cmkey partial len",table[z].kwd,len);
  3280. if (len != 9999 && len > cc) {
  3281. ckstrncat(atmbuf,table[z].kwd+cc,ATMBL);
  3282. atmbuf[len] = NUL;
  3283. printf("%s",atmbuf+cc);
  3284. ckstrncat(cmdbuf,atmbuf+cc,CMDBL);
  3285. xc += (len - cc);
  3286. cc = len;
  3287. }
  3288. #endif /* NOPARTIAL */
  3289. bleep(BP_WARN);
  3290. break;
  3291. } else if (y == -3) {
  3292. bleep(BP_WARN);
  3293. break;
  3294. } else if (y == -1) { /* Not found */
  3295. if ((pmsg & 1) && !quiet) {
  3296. bleep(BP_FAIL);
  3297. printf("?No keywords match - \"%s\"\n",atmbuf);
  3298. }
  3299. cmflgs = -2;
  3300. return(-9);
  3301. }
  3302. /*
  3303. If we found it, but it's a help-only keyword and the "help" bit is not
  3304. set in pmsg, then not found.
  3305. */
  3306. debug(F101,"cmkey flgs","",table[z].flgs);
  3307. if (test(table[z].flgs,CM_HLP) && ((pmsg & 2) == 0)) {
  3308. if ((pmsg & 1) && !quiet) {
  3309. bleep(BP_FAIL);
  3310. printf("?No keywords match - %s\n",atmbuf);
  3311. }
  3312. cmflgs = -2;
  3313. return(-9);
  3314. }
  3315. /*
  3316. See if the keyword just found has the CM_ABR bit set in its flgs field, and
  3317. if so, search forwards in the table for a keyword that has the same kwval
  3318. but does not have CM_ABR (or CM_INV?) set, and then expand using the full
  3319. keyword. WARNING: This assumes that (a) keywords are in alphabetical order,
  3320. and (b) the CM_ABR bit is set only if the the abbreviated keyword is a true
  3321. abbreviation (left substring) of the full keyword.
  3322. */
  3323. if (test(table[z].flgs,CM_ABR)) {
  3324. int zz;
  3325. for (zz = z+1; zz < n; zz++)
  3326. if ((table[zz].kwval == table[z].kwval) &&
  3327. (!test(table[zz].flgs,CM_ABR)) &&
  3328. (!test(table[zz].flgs,CM_INV))) {
  3329. z = zz;
  3330. break;
  3331. }
  3332. }
  3333. xp = table[z].kwd + cc;
  3334. if (cmswitch && test(table[z].flgs,CM_ARG)) {
  3335. #ifdef VMS
  3336. printf("%s=",xp);
  3337. brkchar = '=';
  3338. #else
  3339. printf("%s:",xp);
  3340. brkchar = ':';
  3341. #endif /* VMS */
  3342. } else {
  3343. printf("%s ",xp);
  3344. brkchar = SP;
  3345. }
  3346. #ifdef CK_RECALL
  3347. if (test(table[z].flgs,CM_NOR)) no_recall = 1;
  3348. #endif /* CK_RECALL */
  3349. cmkwflgs = table[z].flgs;
  3350. #ifdef GEMDOS
  3351. fflush(stdout);
  3352. #endif /* GEMDOS */
  3353. addbuf(xp);
  3354. if (cmswitch && test(table[z].flgs,CM_ARG)) {
  3355. bp--; /* Replace trailing space with : */
  3356. #ifdef VMS
  3357. *bp++ = '=';
  3358. #else
  3359. *bp++ = ':';
  3360. #endif /* VMS */
  3361. *bp = NUL;
  3362. np = bp;
  3363. swarg = 1;
  3364. }
  3365. inword = 0;
  3366. cmflgs = 0;
  3367. debug(F110,"cmkey: addbuf",cmdbuf,0);
  3368. return(y);
  3369. case 3: /* User typed "?" */
  3370. if (f) { /* If a conversion function is given */
  3371. char * pp;
  3372. zq = atxbuf; /* do the conversion now. */
  3373. pp = atxbuf;
  3374. atxn = CMDBL;
  3375. if ((*f)(atmbuf,&zq,&atxn) < 0) return(-2);
  3376. if (setatm(pp,0) < 0) {
  3377. printf("?Evaluated keyword too long\n");
  3378. return(-9);
  3379. }
  3380. }
  3381. y = lookup(table,atmbuf,n,&z); /* Look up what we have so far. */
  3382. if (y == -1) {
  3383. /*
  3384. Strictly speaking if the main keyword table search fails,
  3385. then we should look in the token table if one is given.
  3386. But in practice, tokens are also included in the main
  3387. keyword table.
  3388. */
  3389. cmflgs = -2;
  3390. if ((pmsg & 1) && !quiet) {
  3391. bleep(BP_FAIL);
  3392. printf(" No keywords match\n");
  3393. return(-9);
  3394. }
  3395. return(-2);
  3396. }
  3397. #ifndef COMMENT
  3398. /* This is to allow ?-help to work immediately after a token */
  3399. /* without having to type an intermediate space */
  3400. if (tl) {
  3401. for (i = 0; i < tl; i++) /* Check for token */
  3402. if (tok[i] == *atmbuf) { /* Got one */
  3403. debug(F000,"cmkey token:",atmbuf,*atmbuf);
  3404. ungword(); /* Put back the following word */
  3405. cmflgs = 3; /* Force help next time around */
  3406. return(-5); /* Special return code for token */
  3407. }
  3408. }
  3409. #endif /* COMMENT */
  3410. if (*xhlp == NUL)
  3411. printf(" One of the following:\n");
  3412. else
  3413. printf(" %s, one of the following:\n",xhlp);
  3414. {
  3415. int x;
  3416. x = pmsg & (2|4); /* See kwdhelp() comments */
  3417. if (atmbuf[0]) /* If not at beginning of field */
  3418. x |= 1; /* also show invisibles */
  3419. kwdhelp(table,n,atmbuf,"","",1,x);
  3420. }
  3421. #ifndef NOSPL
  3422. if (!havetoken) {
  3423. extern int topcmd;
  3424. if (tl > 0 && topcmd != XXHLP) /* This is bad... */
  3425. printf("or a macro name (\"do ?\" for a list) ");
  3426. }
  3427. #endif /* NOSPL */
  3428. if (*atmbuf == NUL && !havetoken) {
  3429. if (tl == 1)
  3430. printf("or the token %c\n",*tok);
  3431. else if (tl > 1)
  3432. printf("or one of the tokens: %s\n",ckspread(tok));
  3433. }
  3434. printf("%s%s", cmprom, cmdbuf);
  3435. fflush(stdout);
  3436. break;
  3437. default:
  3438. printf("\n%d - Unexpected return code from gtword\n",zz);
  3439. return(cmflgs = -2);
  3440. }
  3441. zz = (pmsg & 8) ? gtword(4) : gtword((pmsg == 4) ? 1 : 0);
  3442. debug(F111,"cmkey gtword zz",atmbuf,zz);
  3443. }
  3444. }
  3445. int
  3446. chktok(tlist) char *tlist; {
  3447. char *p;
  3448. p = tlist;
  3449. while (*p != NUL && *p != *atmbuf) p++;
  3450. return((*p) ? (int) *p : 0);
  3451. }
  3452. /* Routines for parsing and converting dates and times */
  3453. #define isdatesep(c) (ckstrchr(" -/._",c))
  3454. #define CMDATEBUF 1024
  3455. char cmdatebuf[CMDATEBUF+4] = { NUL, NUL };
  3456. static char * cmdatebp = cmdatebuf;
  3457. char * cmdatemsg = NULL;
  3458. char * cmdatestr = NULL;
  3459. static struct keytab timeunits[] = {
  3460. { "days", TU_DAYS, 0 },
  3461. { "months", TU_MONTHS, 0 },
  3462. { "weeks", TU_WEEKS, 0 },
  3463. { "wks", TU_WEEKS, 0 },
  3464. { "years", TU_YEARS, 0 },
  3465. { "yrs", TU_YEARS, 0 }
  3466. };
  3467. static int nunits = (sizeof(timeunits) / sizeof(struct keytab));
  3468. #define SYM_NOW 0
  3469. #define SYM_TODA 1
  3470. #define SYM_TOMO 2
  3471. #define SYM_YEST 3
  3472. static struct keytab symdaytab[] = {
  3473. { "now", SYM_NOW, 0 },
  3474. { "today", SYM_TODA, 0 },
  3475. { "tomorrow", SYM_TOMO, 0 },
  3476. { "yesterday", SYM_YEST, 0 }
  3477. };
  3478. static int nsymdays = (sizeof(symdaytab) / sizeof(struct keytab));
  3479. static struct keytab daysofweek[] = {
  3480. { "Friday", 5, 0 },
  3481. { "Monday", 1, 0 },
  3482. { "Saturday", 6, 0 },
  3483. { "Sunday", 0, 0 },
  3484. { "Thursday", 4, 0 },
  3485. { "Tuesday", 2, 0 },
  3486. { "Wednesday", 3, 0 }
  3487. };
  3488. static struct keytab usatz[] = { /* RFC 822 timezones */
  3489. { "cdt", 5, 0 }, /* Values are GMT offsets */
  3490. { "cst", 6, 0 },
  3491. { "edt", 4, 0 },
  3492. { "est", 5, 0 },
  3493. { "gmt", 0, 0 },
  3494. { "mdt", 6, 0 },
  3495. { "mst", 7, 0 },
  3496. { "pdt", 7, 0 },
  3497. { "pst", 8, 0 },
  3498. { "utc", 0, 0 },
  3499. { "zulu", 0, 0 }
  3500. };
  3501. static int nusatz = (sizeof(usatz) / sizeof(struct keytab));
  3502. /* C M C V T D A T E -- Converts free-form date to standard form. */
  3503. /*
  3504. Call with
  3505. s = pointer to free-format date, time, or date and time.
  3506. t = 0: return time only if time was given in s.
  3507. t = 1: always return time (00:00:00 if no time given in s).
  3508. t = 2: allow time to be > 24:00:00.
  3509. Returns:
  3510. NULL on failure;
  3511. Pointer to "yyyymmdd hh:mm:ss" (local date-time) on success.
  3512. */
  3513. /*
  3514. Before final release the following long lines should be wrapped.
  3515. Until then we leave them long since wrapping them wrecks EMACS's
  3516. C indentation.
  3517. */
  3518. /* asctime pattern */
  3519. static char * atp1 = "[A-Z][a-z][a-z] [A-Z][a-z][a-z] [ 0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]";
  3520. /* asctime pattern with timezone */
  3521. static char * atp2 = "[A-Z][a-z][a-z] [A-Z][a-z][a-z] [ 0-9][0-9] [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [A-Z][A-Z][A-Z] [0-9][0-9][0-9][0-9]";
  3522. #define DATEBUFLEN 127
  3523. #define YYYYMMDD 24 /* Year-month-day buffer */
  3524. #define isleap(y) (((y) % 4 == 0 && (y) % 100 != 0) || (y) % 400 == 0)
  3525. static int mdays[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  3526. #define NEED_DAYS 1
  3527. #define NEED_HRS 2
  3528. #define NEED_MINS 3
  3529. #define NEED_SECS 4
  3530. #define NEED_FRAC 5
  3531. #define DELTABUF 256
  3532. static char deltabuf[DELTABUF];
  3533. static char * deltabp = deltabuf;
  3534. char *
  3535. cmdelta(yy, mo, dd, hh, mm, ss, sign, dyy, dmo, ddd, dhh, dmm, dss)
  3536. int yy, mo, dd, hh, mm, ss, sign, dyy, dmo, ddd, dhh, dmm, dss;
  3537. /* cmdelta */ {
  3538. int zyy, zmo, zdd, zhh, zmm, zss;
  3539. long t1, t2, t3, t4;
  3540. long d1 = 0, d2, d3;
  3541. char datebuf[DATEBUFLEN+1];
  3542. #ifdef DEBUG
  3543. if (deblog) {
  3544. debug(F101,"cmdelta yy","",yy);
  3545. debug(F101,"cmdelta mo","",mo);
  3546. debug(F101,"cmdelta dd","",dd);
  3547. debug(F101,"cmdelta hh","",hh);
  3548. debug(F101,"cmdelta mm","",mm);
  3549. debug(F101,"cmdelta ss","",ss);
  3550. debug(F101,"cmdelta sign","",sign);
  3551. debug(F101,"cmdelta dyy","",dyy);
  3552. debug(F101,"cmdelta dmo","",dmo);
  3553. debug(F101,"cmdelta ddd","",ddd);
  3554. debug(F101,"cmdelta dhh","",dhh);
  3555. debug(F101,"cmdelta dmm","",dmm);
  3556. debug(F101,"cmdelta dss","",dss);
  3557. }
  3558. #endif /* DEBLOG */
  3559. if (yy < 0 || yy > 9999) {
  3560. makestr(&cmdatemsg,"Base year out of range");
  3561. debug(F111,"cmdelta",cmdatemsg,-1);
  3562. return(NULL);
  3563. }
  3564. if (mo < 1 || mo > 12) {
  3565. makestr(&cmdatemsg,"Base month out of range");
  3566. debug(F111,"cmdelta",cmdatemsg,-1);
  3567. return(NULL);
  3568. }
  3569. {
  3570. int i = mdays[mo];
  3571. if (mo == 2) if (isleap(yy)) i++;
  3572. if (dd < 1 || dd > i) {
  3573. makestr(&cmdatemsg,"Base day out of range");
  3574. debug(F111,"cmdelta",cmdatemsg,-1);
  3575. return(NULL);
  3576. }
  3577. }
  3578. if (hh < 0 || hh > 23) {
  3579. makestr(&cmdatemsg,"Base hour out of range");
  3580. debug(F111,"cmdelta",cmdatemsg,-1);
  3581. return(NULL);
  3582. }
  3583. if (mm < 0 || mm > 59) {
  3584. makestr(&cmdatemsg,"Base minute out of range");
  3585. debug(F111,"cmdelta",cmdatemsg,-1);
  3586. return(NULL);
  3587. }
  3588. if (ss < 0 || ss > 60) {
  3589. makestr(&cmdatemsg,"Base second out of range");
  3590. debug(F111,"cmdelta",cmdatemsg,-1);
  3591. return(NULL);
  3592. }
  3593. sign = (sign < 0) ? -1 : 1;
  3594. if (dmo != 0) {
  3595. if (sign > 0) {
  3596. mo += (sign * dmo);
  3597. if (mo > 12) {
  3598. yy += mo / 12;
  3599. mo = mo % 12;
  3600. }
  3601. } else if (sign < 0) {
  3602. while (dmo > 12) {
  3603. yy--;
  3604. dmo -= 12;
  3605. }
  3606. if (dmo < mo) {
  3607. mo -= dmo;
  3608. } else {
  3609. yy--;
  3610. mo = 12 - (dmo - mo);
  3611. }
  3612. }
  3613. }
  3614. if (dyy != 0) {
  3615. yy += (sign * dyy);
  3616. if (yy > 9999 || yy < 0) {
  3617. makestr(&cmdatemsg,"Result year out of range");
  3618. debug(F111,"cmdelta",cmdatemsg,-1);
  3619. return(NULL);
  3620. }
  3621. }
  3622. sprintf(datebuf,"%04d%02d%02d %02d:%02d:%02d",yy,mo,dd,hh,mm,ss);
  3623. d1 = mjd(datebuf);
  3624. debug(F111,"cmdelta mjd",datebuf,d1);
  3625. t1 = hh * 3600 + mm * 60 + ss; /* Base time to secs since midnight */
  3626. t2 = dhh * 3600 + dmm * 60 + dss; /* Delta time, ditto */
  3627. t3 = t1 + (sign * t2); /* Get sum (or difference) */
  3628. d2 = (sign * ddd); /* Delta days */
  3629. d2 += t3 / 86400L;
  3630. t4 = t3 % 86400L; /* Fractional part of day */
  3631. if (t4 < 0) { /* If negative */
  3632. d2--; /* one less delta day */
  3633. t4 += 86400L; /* get positive seconds */
  3634. }
  3635. hh = (int) (t4 / 3600L);
  3636. mm = (int) (t4 % 3600L) / 60;
  3637. ss = (int) (t4 % 3600L) % 60;
  3638. sprintf(datebuf,"%s %02d:%02d:%02d", mjd2date(d1+d2),hh,mm,ss);
  3639. {
  3640. int len, k, n;
  3641. char * p;
  3642. len = strlen(datebuf);
  3643. k = deltabp - (char *)deltabuf; /* Space used */
  3644. n = DELTABUF - k - 1; /* Space left */
  3645. if (n < len) { /* Not enough? */
  3646. deltabp = deltabuf; /* Wrap around */
  3647. n = DELTABUF;
  3648. }
  3649. ckstrncpy(deltabp,datebuf,n);
  3650. p = deltabp;
  3651. deltabp += len + 1;
  3652. return(p);
  3653. }
  3654. }
  3655. /* Convert Delta Time to Seconds */
  3656. int
  3657. delta2sec(s,result) char * s; long * result; {
  3658. long ddays = 0L, zz;
  3659. int dsign = 1, dhours = 0, dmins = 0, dsecs = 0, units;
  3660. int state = NEED_DAYS;
  3661. char *p, *p2, *p3, c = 0;
  3662. char buf[64];
  3663. if (!s) s = "";
  3664. if (!*s)
  3665. return(-1);
  3666. if ((int)strlen(s) > 63)
  3667. return(-1);
  3668. ckstrncpy(buf,s,64);
  3669. p = buf;
  3670. if (*p != '+' && *p != '-')
  3671. return(-1);
  3672. if (*p++ == '-')
  3673. dsign = -1;
  3674. while (*p == SP) /* Skip intervening spaces */
  3675. p++;
  3676. while (state) { /* FSA to parse delta time */
  3677. if (state < 0 || !isdigit(*p))
  3678. return(-1);
  3679. p2 = p; /* Get next numeric field */
  3680. while (isdigit(*p2))
  3681. p2++;
  3682. c = *p2; /* And break character */
  3683. *p2 = NUL; /* Terminate the number */
  3684. switch (state) { /* Interpret according to state */
  3685. case NEED_DAYS: /* Initial */
  3686. if ((c == '-') || /* VMS format */
  3687. ((c == 'd' || c == 'D')
  3688. && !isalpha(*(p2+1)))) { /* Days */
  3689. ddays = atol(p);
  3690. if (!*(p2+1))
  3691. state = 0;
  3692. else /* if anything is left */
  3693. state = NEED_HRS; /* now we want hours. */
  3694. } else if (c == ':') { /* delimiter is colon */
  3695. dhours = atoi(p); /* so it's hours */
  3696. state = NEED_MINS; /* now we want minutes */
  3697. } else if (!c) { /* end of string */
  3698. dhours = atoi(p); /* it's still hours */
  3699. state = 0; /* and we're done */
  3700. } else if (isalpha(c) || c == SP) {
  3701. if (c == SP) { /* It's a keyword? */
  3702. p2++; /* Skip spaces */
  3703. while (*p2 == SP)
  3704. p2++;
  3705. } else { /* or replace first letter */
  3706. *p2 = c;
  3707. }
  3708. p3 = p2; /* p2 points to beginning of keyword */
  3709. while (isalpha(*p3)) /* Find end of keyword */
  3710. p3++;
  3711. c = *p3; /* NUL it out so we can look it up */
  3712. if (*p3) /* p3 points to keyword terminator */
  3713. *p3 = NUL;
  3714. if ((units = lookup(timeunits,p2,nunits,NULL)) < 0)
  3715. return(-1);
  3716. *p2 = NUL; /* Re-terminate the number */
  3717. *p3 = c;
  3718. while (*p3 == SP) /* Point at field after units */
  3719. p3++;
  3720. p2 = p3;
  3721. switch (units) {
  3722. case TU_DAYS:
  3723. ddays = atol(p);
  3724. break;
  3725. default:
  3726. return(-1);
  3727. }
  3728. if (*p2) {
  3729. state = NEED_HRS;
  3730. p2--;
  3731. } else
  3732. state = 0;
  3733. } else { /* Anything else */
  3734. state = -1; /* is an error */
  3735. }
  3736. break;
  3737. case NEED_HRS: /* Looking for hours */
  3738. if (c == ':') {
  3739. dhours = atoi(p);
  3740. state = NEED_MINS;
  3741. } else if (!c) {
  3742. dhours = atoi(p);
  3743. state = 0;
  3744. } else {
  3745. state = -1;
  3746. }
  3747. break;
  3748. case NEED_MINS: /* Looking for minutes */
  3749. if (c == ':') {
  3750. dmins = atoi(p);
  3751. state = NEED_SECS;
  3752. } else if (!c) {
  3753. dmins = atoi(p);
  3754. state = 0;
  3755. } else {
  3756. state = -1;
  3757. }
  3758. break;
  3759. case NEED_SECS: /* Looking for seconds */
  3760. if (c == '.') {
  3761. dsecs = atoi(p);
  3762. state = NEED_FRAC;
  3763. } else if (!c) {
  3764. dsecs = atoi(p);
  3765. state = 0;
  3766. } else {
  3767. state = -1;
  3768. }
  3769. break;
  3770. case NEED_FRAC: /* Fraction of second */
  3771. if (!c && rdigits(p)) {
  3772. if (*p > '4')
  3773. dsecs++;
  3774. state = 0;
  3775. } else {
  3776. state = -1;
  3777. }
  3778. break;
  3779. }
  3780. if (c) /* next field if any */
  3781. p = p2 + 1;
  3782. }
  3783. if (state < 0)
  3784. return(-1);
  3785. /* if days > 24854 and sizeof(long) == 32 we overflow */
  3786. zz = ddays * 86400L;
  3787. if (zz < 0L) /* This catches it */
  3788. return(-2);
  3789. zz += dhours * 3600L + dmins * 60L + dsecs;
  3790. zz *= dsign;
  3791. *result = zz;
  3792. return(0);
  3793. }
  3794. char *
  3795. cmcvtdate(s,t) char * s; int t; {
  3796. int x, i, j, k, hh, mm, ss, ff, pmflag = 0, nodate = 0, len, dow;
  3797. int units, isgmt = 0, gmtsign = 0, d = 0, state = 0, nday;
  3798. int kn = 0, ft[8], isletter = 0, f2len = 0;
  3799. int zhh = 0; /* Timezone adjustments */
  3800. int zmm = 0;
  3801. int zdd = 0;
  3802. int dsign = 1; /* Delta-time adjustments */
  3803. int ddays = 0;
  3804. int dmonths = 0;
  3805. int dyears = 0;
  3806. int dhours = 0;
  3807. int dmins = 0;
  3808. int dsecs = 0;
  3809. int havedelta = 0;
  3810. char * fld[8], * p = "", * p2, * p3; /* Assorted buffers and pointers */
  3811. char * s2, * s3;
  3812. char * year = NULL, * month = NULL, * day = NULL;
  3813. char * hour = "00", * min = "00", * sec = "00";
  3814. char datesep = 0;
  3815. char tmpbuf[16];
  3816. char xbuf[DATEBUFLEN+1];
  3817. char ybuf[DATEBUFLEN+1];
  3818. char zbuf[DATEBUFLEN+1];
  3819. char yyyymmdd[YYYYMMDD];
  3820. char dbuf[26];
  3821. char daybuf[3];
  3822. char monbuf[3];
  3823. char yearbuf[5];
  3824. char timbuf[16], *tb, cc;
  3825. char * dp = NULL; /* Result pointer */
  3826. char * newdate = NULL;
  3827. char * datepat = "[12][0-9][0-9][0-9]:[0-9][0-9]:[0-9][0-9]";
  3828. if (!s) s = "";
  3829. tmpbuf[0] = NUL;
  3830. len = strlen(s);
  3831. while (*s == SP) s++; /* Gobble any leading blanks */
  3832. if (ckmatch(datepat,s,0,4)) { /* Check for Apache web log format */
  3833. int i, j = 0;
  3834. newdate = (char *)malloc((len + 1) * sizeof(char *));
  3835. for (i = 0; i <= len; i++) { /* Loop to remove colons */
  3836. if (s[i] != ':') newdate[j++] = s[i];
  3837. if (s[i] == NUL) break;
  3838. }
  3839. s = newdate; /* Replace arg with result */
  3840. }
  3841. if (isalpha(*s)) /* Remember if 1st char is a letter */
  3842. isletter = 1;
  3843. debug(F110,"cmcvtdate",s,len);
  3844. if (len == 0) { /* No arg - return current date-time */
  3845. dp = ckdate();
  3846. goto xcvtdate;
  3847. }
  3848. if (len > DATEBUFLEN) { /* Check length of arg */
  3849. makestr(&cmdatemsg,"Date-time string too long");
  3850. debug(F111,"cmcvtdate",cmdatemsg,-1);
  3851. return(NULL);
  3852. }
  3853. hh = 0; /* Init time to 00:00:00.0 */
  3854. mm = 0;
  3855. ss = 0;
  3856. ff = 0;
  3857. ztime(&p);
  3858. if (!p)
  3859. p = "";
  3860. if (*p) { /* Init time to current time */
  3861. x = ckstrncpy(dbuf,p,26);
  3862. if (x > 17) {
  3863. hh = atoi(&dbuf[11]);
  3864. mm = atoi(&dbuf[14]);
  3865. ss = atoi(&dbuf[17]);
  3866. }
  3867. }
  3868. ckstrncpy(yyyymmdd,zzndate(),YYYYMMDD); /* Init date to current date */
  3869. ckstrncpy(yearbuf,yyyymmdd,5);
  3870. ckstrncpy(monbuf,&yyyymmdd[4],3);
  3871. ckstrncpy(daybuf,&yyyymmdd[6],3);
  3872. year = yearbuf;
  3873. month = monbuf;
  3874. day = daybuf;
  3875. nday = atoi(daybuf);
  3876. ckstrncpy(xbuf,s,DATEBUFLEN); /* Make a local copy we can poke */
  3877. s = xbuf; /* Point to it */
  3878. s[len] = NUL;
  3879. if (s[0] == ':') {
  3880. p = s;
  3881. goto dotime;
  3882. }
  3883. /* Special preset formats... */
  3884. if (len >= 14) { /* FTP MDTM all-numeric date */
  3885. char c;
  3886. c = s[14]; /* e.g. 19980615100045.014 */
  3887. s[14] = NUL;
  3888. x = rdigits(s);
  3889. s[14] = c;
  3890. if (x) {
  3891. ckstrncpy(yyyymmdd,s,8+1);
  3892. year = NULL;
  3893. p = &s[8];
  3894. goto dotime;
  3895. }
  3896. }
  3897. x = 0; /* Becomes > 0 for asctime format */
  3898. if (isalpha(s[0])) {
  3899. if (len == 24) { /* Asctime format? */
  3900. /* Sat Jul 14 15:57:32 2001 */
  3901. x = ckmatch(atp1,s,0,0);
  3902. debug(F111,"cmcvtdate asctime",s,x);
  3903. } else if (len == 28) { /* Or Asctime plus timezone? */
  3904. /* Sat Jul 14 15:15:39 EDT 2001 */
  3905. x = ckmatch(atp2,s,0,0);
  3906. debug(F111,"cmcvtdate asctime+timezone",s,x);
  3907. }
  3908. }
  3909. if (x > 0) { /* Asctime format */
  3910. int xx;
  3911. strncpy(yearbuf,s + len - 4,4);
  3912. yearbuf[4] = NUL;
  3913. for (i = 0; i < 3; i++)
  3914. tmpbuf[i] = s[i+4];
  3915. tmpbuf[3] = NUL;
  3916. if ((xx = lookup(cmonths,tmpbuf,12,NULL)) < 0) {
  3917. makestr(&cmdatemsg,"Invalid month");
  3918. debug(F111,"cmcvtdate",cmdatemsg,-1);
  3919. return(NULL);
  3920. }
  3921. debug(F101,"cmcvtdate asctime month","",xx);
  3922. monbuf[0] = (xx / 10) + '0';
  3923. monbuf[1] = (xx % 10) + '0';
  3924. monbuf[2] = NUL;
  3925. daybuf[0] = (s[8] == ' ' ? '0' : s[8]);
  3926. daybuf[1] = s[9];
  3927. daybuf[2] = NUL;
  3928. xbuf[0] = SP;
  3929. for (i = 11; i < 19; i++)
  3930. xbuf[i-10] = s[i];
  3931. xbuf[9] = NUL;
  3932. ckmakmsg(zbuf,18,yearbuf,monbuf,daybuf,xbuf);
  3933. debug(F110,"cmcvtdate asctime ok",zbuf,0);
  3934. if (len == 24) {
  3935. dp = zbuf;
  3936. goto xcvtdate;
  3937. } else {
  3938. int n;
  3939. n = ckmakmsg(ybuf,DATEBUFLEN-4,zbuf," ",NULL,NULL);
  3940. ybuf[n++] = s[20];
  3941. ybuf[n++] = s[21];
  3942. ybuf[n++] = s[22];
  3943. ybuf[n++] = NUL;
  3944. ckstrncpy(xbuf,ybuf,DATEBUFLEN);
  3945. s = xbuf;
  3946. isletter = 0;
  3947. }
  3948. }
  3949. /* Check for day of week */
  3950. p = s;
  3951. while (*p == SP) p++;
  3952. dow = -1;
  3953. if (*p) {
  3954. p2 = p;
  3955. cc = NUL;
  3956. while (1) {
  3957. if (*p2 == ',' || *p2 == SP || !*p2) {
  3958. cc = *p2; /* Save break char */
  3959. *p2 = NUL; /* NUL it out */
  3960. p3 = p2; /* Remember this spot */
  3961. if ((dow = lookup(daysofweek,p,7,NULL)) > -1) {
  3962. debug(F111,"cmcvtdate dow",p,dow);
  3963. s = p2;
  3964. if (cc == ',' || cc == SP) { /* Point to next field */
  3965. s++;
  3966. while (*s == SP) s++;
  3967. }
  3968. p = s;
  3969. debug(F111,"cmcvtdate dow new p",p,dow);
  3970. break;
  3971. } else if (isalpha(*p) && cc == ',') {
  3972. makestr(&cmdatemsg,"Unrecognized day of week");
  3973. debug(F111,"cmcvtdate",cmdatemsg,-1);
  3974. return(NULL);
  3975. } else {
  3976. *p3 = cc;
  3977. break;
  3978. }
  3979. }
  3980. p2++;
  3981. }
  3982. }
  3983. len = strlen(s); /* Update length */
  3984. debug(F111,"cmcvtdate s",s,len);
  3985. debug(F111,"cmcvtdate dow",s,dow);
  3986. if (dow > -1) { /* Have a day-of-week number */
  3987. long zz; int n, j;
  3988. zz = mjd(zzndate()); /* Get today's MJD */
  3989. debug(F111,"cmcvtdate zz","",zz);
  3990. j = (((int)(zz % 7L)) + 3) % 7; /* Today's day-of-week number */
  3991. debug(F111,"cmcvtdate j","",j);
  3992. hh = 0; /* Init time to midnight */
  3993. mm = 0;
  3994. ss = 0;
  3995. if (j == dow) {
  3996. ckstrncpy(yyyymmdd,zzndate(),YYYYMMDD);
  3997. year = NULL;
  3998. } else {
  3999. n = dow - j; /* Days from now */
  4000. if (dow < j)
  4001. n += 7;
  4002. if (n < 0) n += 7; /* Add to MJD */
  4003. zz += n;
  4004. ckstrncpy(yyyymmdd,mjd2date(zz),YYYYMMDD); /* New date */
  4005. year = NULL;
  4006. }
  4007. debug(F111,"cmcvtdate A",yyyymmdd,len);
  4008. if (len == 0) { /* No more fields after this */
  4009. ckmakmsg(zbuf,18,yyyymmdd," 00:00:00",NULL,NULL);
  4010. dp = zbuf;
  4011. goto xcvtdate;
  4012. }
  4013. isletter = 0;
  4014. if (rdigits(p) && len < 8) /* Next field is time? */
  4015. goto dotime; /* If so go straight to time section */
  4016. if (isdigit(*p)) {
  4017. if (*(p+1) == ':')
  4018. goto dotime;
  4019. else if (isdigit(*(p+1)) && (*(p+2) == ':'))
  4020. goto dotime;
  4021. }
  4022. }
  4023. debug(F111,"cmcvtdate B s",s,dow);
  4024. debug(F111,"cmcvtdate B p",p,dow);
  4025. if (*s == '+' || *s == '-') { /* Delta time only - skip ahead. */
  4026. p = s;
  4027. goto delta;
  4028. }
  4029. #ifdef COMMENT
  4030. /*
  4031. What is the purpose of this? It breaks parsing of email dates like
  4032. "Wed, 13 Feb 2002 17:43:02 -0800 (PST)". Removing this code fixes the
  4033. problem and Kermit still passes the 'dates' script.
  4034. - fdc, Sat Nov 26 10:52:45 2005.
  4035. */
  4036. if (dow > -1) {
  4037. /* Day of week given followed by something that is not a time */
  4038. /* or a delta so it can't be valid */
  4039. makestr(&cmdatemsg,"Invalid tokens after day of week");
  4040. debug(F111,"cmcvtdate fail",cmdatemsg,-1);
  4041. return(NULL);
  4042. }
  4043. #endif /* COMMENT */
  4044. /* Handle "today", "yesterday", "tomorrow", and +/- n units */
  4045. if (ckstrchr("TtYyNn",s[0])) {
  4046. int i, k, n, minus = 0;
  4047. char c;
  4048. long jd;
  4049. jd = mjd(ckdate());
  4050. debug(F111,"cmcvtdate mjd",s,jd);
  4051. /* Symbolic date: TODAY, TOMORROW, etc...? */
  4052. s2 = s; /* Find end of keyword */
  4053. i = 0;
  4054. while (isalpha(*s2)) { /* and get its length */
  4055. i++;
  4056. s2++;
  4057. }
  4058. c = *s2; /* Zap but save delimiter */
  4059. *s2 = NUL;
  4060. k = lookup(symdaytab,s,nsymdays,NULL); /* Look up keyword */
  4061. *s2 = c; /* Replace delimiter */
  4062. if (k < 0) /* Keyword not found */
  4063. goto normal;
  4064. s3 = &s[i];
  4065. while (*s3 == SP) /* Skip whitespace */
  4066. s3++;
  4067. if (*s3 == '_' || *s3 == ':')
  4068. s3++;
  4069. switch (k) { /* Have keyword */
  4070. case SYM_NOW: /* NOW */
  4071. ckstrncpy(ybuf,ckdate(),DATEBUFLEN);
  4072. ckstrncpy(yyyymmdd,ybuf,YYYYMMDD);
  4073. year = NULL;
  4074. if (*s3) { /* No overwriting current time. */
  4075. ckstrncat(ybuf," ",DATEBUFLEN);
  4076. ckstrncat(ybuf,s3,DATEBUFLEN);
  4077. }
  4078. break;
  4079. default: /* Yesterday, Today, and Tomorrow */
  4080. if (k == SYM_TOMO) { /* TOMORROW */
  4081. strncpy(ybuf,mjd2date(jd+1),8);
  4082. } else if (k == SYM_YEST) { /* YESTERDAY */
  4083. strncpy(ybuf,mjd2date(jd-1),8);
  4084. } else { /* TODAY */
  4085. strncpy(ybuf,ckdate(),8);
  4086. }
  4087. strncpy(ybuf+8," 00:00:00",DATEBUFLEN-8); /* Default time is 0 */
  4088. ckstrncpy(yyyymmdd,ybuf,YYYYMMDD);
  4089. year = NULL;
  4090. if (*s3) { /* If something follows keyword... */
  4091. if (isdigit(*s3)) { /* Time - overwrite default time */
  4092. strncpy(ybuf+8,s+i,DATEBUFLEN-8);
  4093. } else { /* Something else, keep default time */
  4094. ckstrncat(ybuf," ",DATEBUFLEN); /* and append */
  4095. ckstrncat(ybuf,s3,DATEBUFLEN); /* whatever we have */
  4096. }
  4097. }
  4098. }
  4099. s = ybuf; /* Point to rewritten date-time */
  4100. len = strlen(s); /* Update length */
  4101. isletter = 0; /* Cancel this */
  4102. }
  4103. /* Regular free-format non-symbolic date */
  4104. normal:
  4105. debug(F111,"cmcvtdate NORMAL",s,len);
  4106. debug(F111,"cmcvtdate dow",s,dow);
  4107. if (yyyymmdd[0] && !year) {
  4108. ckstrncpy(yearbuf,yyyymmdd,5);
  4109. ckstrncpy(monbuf,&yyyymmdd[4],3);
  4110. ckstrncpy(daybuf,&yyyymmdd[6],3);
  4111. year = yearbuf;
  4112. month = monbuf;
  4113. day = daybuf;
  4114. nday = atoi(daybuf);
  4115. }
  4116. if (isdigit(s[0])) { /* Time without date? */
  4117. p = s;
  4118. if (s[1] == ':') {
  4119. debug(F111,"cmcvtdate NORMAL X1",s,len);
  4120. goto dotime;
  4121. } else if (len > 1 && isdigit(s[1]) && s[2] == ':') {
  4122. debug(F111,"cmcvtdate NORMAL X2",s,len);
  4123. goto dotime;
  4124. } else if (rdigits(s) && len < 8) {
  4125. debug(F111,"cmcvtdate NORMAL X3",s,len);
  4126. goto dotime;
  4127. }
  4128. }
  4129. if (len >= 8 && isdigit(*s)) { /* Check first for yyyymmdd* */
  4130. debug(F111,"cmcvtdate NORMAL A",s,len);
  4131. cc = s[8];
  4132. s[8] = NUL; /* Isolate first 8 characters */
  4133. if (rdigits(s)) {
  4134. /* Have valid time separator? */
  4135. p2 = cc ? ckstrchr(" Tt_-:",cc) : NULL;
  4136. if (!cc || p2) {
  4137. ckstrncpy(yyyymmdd,s,YYYYMMDD); /* Valid separator */
  4138. year = NULL;
  4139. s += 8; /* or time not given */
  4140. if (cc) s++; /* Keep date */
  4141. p = s; /* and go handle time */
  4142. goto dotime;
  4143. } else if (!p2) {
  4144. if (isdigit(cc))
  4145. makestr(&cmdatemsg,"Numeric date too long");
  4146. else
  4147. makestr(&cmdatemsg,"Invalid date-time separator");
  4148. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4149. return(NULL);
  4150. }
  4151. }
  4152. s[8] = cc; /* Put this back! */
  4153. }
  4154. debug(F111,"cmcvtdate NORMAL non-yyyymmdd",s,len);
  4155. /* Free-format date -- figure it out */
  4156. #ifdef COMMENT
  4157. if (*s && !isdigit(*s)) {
  4158. makestr(&cmdatemsg,"Unrecognized word in date");
  4159. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4160. return(NULL);
  4161. }
  4162. #endif /* COMMENT */
  4163. for (i = 0; i < 8; i++) /* Field types */
  4164. ft[i] = -1;
  4165. fld[i = 0] = (p = s); /* First field */
  4166. while (*p) { /* Get next two fields */
  4167. if (isdatesep(*p)) { /* Have a date separator */
  4168. if (i == 0) {
  4169. datesep = *p;
  4170. } else if (i == 1 && *p != datesep) {
  4171. makestr(&cmdatemsg,"Inconsistent date separators");
  4172. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4173. return(NULL);
  4174. }
  4175. *p++ = NUL; /* Replace by NUL */
  4176. if (*p) { /* Now we're at the next field */
  4177. while (*p == SP) p++; /* Skip leading spaces */
  4178. if (!*p) break; /* Make sure we still have something */
  4179. if (i == 2) /* Last one? */
  4180. break;
  4181. fld[++i] = p; /* No, record pointer to this one */
  4182. } else {
  4183. break;
  4184. }
  4185. } else if ((*p == 'T' || *p == 't') && isdigit(*(p+1))) { /* Time */
  4186. *p++ = NUL;
  4187. break;
  4188. } else if (*p == ':') {
  4189. if (i == 0 && p == s) {
  4190. nodate = 1;
  4191. break;
  4192. } else if (i != 0) { /* After a date */
  4193. if (i == 2) { /* OK as date-time separator (VMS) */
  4194. *p++ = NUL;
  4195. break;
  4196. }
  4197. if (i < 2)
  4198. makestr(&cmdatemsg,"Too few fields in date");
  4199. else
  4200. makestr(&cmdatemsg,"Misplaced time separator");
  4201. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4202. return(NULL);
  4203. }
  4204. nodate = 1; /* Or without a date */
  4205. break;
  4206. }
  4207. p++;
  4208. }
  4209. if (p > s && i == 0) /* Make sure we have a date */
  4210. nodate = 1; /* No date. */
  4211. if (nodate && dow > -1) { /* Have implied date from DOW? */
  4212. goto dotime; /* Use, use that, go do time. */
  4213. } else if (nodate) { /* No date and no implied date */
  4214. char *tmp = NULL; /* Substitute today's date */
  4215. ztime(&tmp);
  4216. if (!tmp)
  4217. tmp = "";
  4218. if (!*tmp) {
  4219. makestr(&cmdatemsg,"Problem supplying current date");
  4220. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4221. return(NULL);
  4222. }
  4223. ckstrncpy(dbuf,tmp,26); /* Reformat */
  4224. if (dbuf[8] == SP) dbuf[8] = '0';
  4225. fld[0] = dbuf+8; /* dd */
  4226. dbuf[10] = NUL;
  4227. fld[1] = dbuf+4; /* mmm */
  4228. dbuf[7] = NUL;
  4229. fld[2] = dbuf+20; /* yyyy */
  4230. dbuf[24] = NUL;
  4231. hh = atoi(&dbuf[11]);
  4232. mm = atoi(&dbuf[14]);
  4233. ss = atoi(&dbuf[17]);
  4234. p = s; /* Back up source pointer to reparse */
  4235. } else if (i < 2) {
  4236. makestr(&cmdatemsg,"Too few fields in date");
  4237. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4238. return(NULL);
  4239. }
  4240. /* Have three date fields - see what they are */
  4241. for (k = 0, j = 0; j < 3; j++) { /* Get number of non-numeric fields */
  4242. ft[j] = rdigits(fld[j]);
  4243. debug(F111,"cmcvtdate fld",fld[j],j);
  4244. if (ft[j] == 0)
  4245. k++;
  4246. }
  4247. kn = k; /* How many numeric fields */
  4248. month = NULL; /* Strike out default values */
  4249. year = NULL;
  4250. day = NULL;
  4251. if (k == 2 && ft[2] > 0) { /* Jul 20, 2001 */
  4252. int xx;
  4253. xx = strlen(fld[1]);
  4254. p3 = fld[1];
  4255. if (xx > 0) if (p3[xx-1] == ',') {
  4256. p3[xx-1] = NUL;
  4257. if (rdigits(p3)) {
  4258. k = 1;
  4259. ft[1] = 1;
  4260. } else p3[xx-1] = ',';
  4261. }
  4262. }
  4263. if (k > 1) { /* We can have only one non-numeric */
  4264. if (nodate)
  4265. makestr(&cmdatemsg,"Unrecognized word in date");
  4266. else if (!ft[2] && isdigit(*(fld[2])))
  4267. makestr(&cmdatemsg,"Invalid date-time separator");
  4268. else
  4269. makestr(&cmdatemsg,"Too many non-numeric fields in date");
  4270. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4271. return(NULL);
  4272. }
  4273. if (!ft[0]) {
  4274. k = 0;
  4275. } else if (!ft[1]) {
  4276. k = 1;
  4277. } else if (!ft[2]) {
  4278. makestr(&cmdatemsg,"Non-digit in third date field");
  4279. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4280. return(NULL);
  4281. } else
  4282. k = -1;
  4283. if (k > -1) {
  4284. if ((x = lookup(cmonths,fld[k],12,NULL)) < 0) {
  4285. makestr(&cmdatemsg,"Unknown month");
  4286. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4287. return(NULL);
  4288. }
  4289. sprintf(tmpbuf,"%02d",x);
  4290. month = tmpbuf;
  4291. }
  4292. f2len = strlen(fld[2]); /* Length of 3rd field */
  4293. if (k == 0) { /* monthname dd, yyyy */
  4294. day = fld[1];
  4295. year = fld[2];
  4296. } else if (((int)strlen(fld[0]) == 4)) { /* yyyy-xx-dd */
  4297. year = fld[0];
  4298. day = fld[2];
  4299. if (!month)
  4300. month = fld[1]; /* yyyy-mm-dd */
  4301. } else if (f2len == 4) { /* xx-xx-yyyy */
  4302. year = fld[2];
  4303. if (month) { /* dd-name-yyyy */
  4304. day = fld[0];
  4305. } else { /* xx-xx-yyyy */
  4306. int f0, f1;
  4307. f0 = atoi(fld[0]);
  4308. f1 = atoi(fld[1]);
  4309. if (((f0 > 12) && (f1 <= 12)) || (f1 <= 12 && f0 == f1)) {
  4310. day = fld[0]; /* mm-dd-yyyy */
  4311. month = fld[1];
  4312. } else if ((f0 <= 12) && (f1 > 12)) {
  4313. if (!rdigits(fld[1])) {
  4314. makestr(&cmdatemsg,"Day not numeric");
  4315. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4316. return(NULL);
  4317. } else {
  4318. day = fld[1]; /* dd-mm-yyyy */
  4319. }
  4320. month = fld[0];
  4321. } else {
  4322. if (!f0 || !f1)
  4323. makestr(&cmdatemsg,"Day or month out of range");
  4324. else
  4325. makestr(&cmdatemsg,"Day and month are ambiguous");
  4326. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4327. return(NULL);
  4328. }
  4329. }
  4330. } else if ((f2len < 4) && /* dd mmm yy (RFC822) */
  4331. !rdigits(fld[1]) && /* middle field is monthname */
  4332. rdigits(fld[2])) {
  4333. int tmpyear;
  4334. day = fld[0];
  4335. if (!fld[2][1]) {
  4336. makestr(&cmdatemsg,"Too few digits in year");
  4337. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4338. return(NULL);
  4339. }
  4340. tmpyear = atoi(fld[2]);
  4341. if (tmpyear < 50) /* RFC 2822 windowing */
  4342. tmpyear += 2000;
  4343. else /* This includes 3-digit years. */
  4344. tmpyear += 1900;
  4345. year = ckitoa(tmpyear);
  4346. } else if ((f2len < 4) && (k < 0) && ((int)strlen(fld[0]) < 4)) {
  4347. makestr(&cmdatemsg,"Ambiguous numeric date");
  4348. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4349. return(NULL);
  4350. } else if ((f2len > 4) && ft[2]) {
  4351. makestr(&cmdatemsg,"Too many digits in year");
  4352. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4353. return(NULL);
  4354. } else {
  4355. makestr(&cmdatemsg,"Unexpected date format");
  4356. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4357. return(NULL);
  4358. }
  4359. x = atoi(month);
  4360. sprintf(tmpbuf,"%02d",x); /* 2-digit numeric month */
  4361. /*
  4362. state = 1 = hours
  4363. state = 2 = minutes
  4364. state = 3 = seconds
  4365. state = 4 = fractions of seconds
  4366. */
  4367. dotime:
  4368. if (isletter && (s == p)) {
  4369. makestr(&cmdatemsg,"Unknown date-time word");
  4370. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4371. return(NULL);
  4372. }
  4373. if (!year && yyyymmdd[0]) {
  4374. debug(F110,"cmcvtdate dotime yyyymmdd",yyyymmdd,0);
  4375. for (i = 0; i < 4; i++)
  4376. yearbuf[i] = yyyymmdd[i];
  4377. yearbuf[4] = NUL;
  4378. monbuf[0] = yyyymmdd[4];
  4379. monbuf[1] = yyyymmdd[5];
  4380. monbuf[2] = NUL;
  4381. daybuf[0] = yyyymmdd[6];
  4382. daybuf[1] = yyyymmdd[7];
  4383. daybuf[2] = NUL;
  4384. day = daybuf;
  4385. nday = atoi(daybuf);
  4386. month = monbuf;
  4387. year = yearbuf;
  4388. }
  4389. if (!year) {
  4390. makestr(&cmdatemsg,"Internal error - date not defaulted");
  4391. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4392. return(NULL);
  4393. }
  4394. /* Get here with day, month, and year set */
  4395. debug(F110,"cmcvtdate dotime day",day,0);
  4396. debug(F110,"cmcvtdate dotime month",month,0);
  4397. debug(F110,"cmcvtdate dotime year",year,0);
  4398. debug(F110,"cmcvtdate dotime s",s,0);
  4399. debug(F110,"cmcvtdate dotime p",p,0);
  4400. x = atoi(month);
  4401. if (x > 12 || x < 1) {
  4402. makestr(&cmdatemsg,"Month out of range");
  4403. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4404. return(NULL);
  4405. }
  4406. nday = atoi(day);
  4407. i = mdays[x];
  4408. if (x == 2) if (isleap(atoi(year))) i++;
  4409. if (nday > i || nday < 1) {
  4410. makestr(&cmdatemsg,"Day out of range");
  4411. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4412. return(NULL);
  4413. }
  4414. if (!*p && t == 0) {
  4415. sprintf(zbuf,"%04d%02d%02d",atoi(year),atoi(month),nday);
  4416. dp = zbuf;
  4417. goto xcvtdate;
  4418. }
  4419. if (*p == '+' || *p == '-') { /* GMT offset without a time */
  4420. hh = 0; /* so default time to 00:00:00 */
  4421. mm = 0;
  4422. ss = 0;
  4423. goto cmtimezone; /* and go do timezone */
  4424. }
  4425. if (*p && !isdigit(*p) && *p != ':') {
  4426. makestr(&cmdatemsg,"Invalid time");
  4427. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4428. return(NULL);
  4429. }
  4430. sprintf(yyyymmdd,"%s%s%02d",year,month,nday); /* for tz calculations... */
  4431. state = 1; /* Initialize time-parsing FSA */
  4432. hh = 0; /* hours */
  4433. mm = 0; /* minutes */
  4434. ss = 0; /* seconds */
  4435. ff = -1; /* fraction */
  4436. d = 0; /* Digit counter */
  4437. p2 = p; /* Preliminary digit count... */
  4438. while (isdigit(*p2)) {
  4439. d++;
  4440. p2++;
  4441. }
  4442. if (d > 6) {
  4443. makestr(&cmdatemsg,"Too many time digits");
  4444. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4445. return(NULL);
  4446. }
  4447. d = (d & 1 && *p2 != ':') ? 1 : 0; /* Odd implies leading '0' */
  4448. while (*p) { /* Get the time, if any */
  4449. if (isdigit(*p)) { /* digit */
  4450. if (d++ > 1) {
  4451. state++;
  4452. d = 1;
  4453. }
  4454. switch (state) {
  4455. case 1: /* Hours */
  4456. hh = hh * 10 + (*p - '0');
  4457. break;
  4458. case 2: /* Minutes */
  4459. mm = mm * 10 + (*p - '0');
  4460. break;
  4461. case 3: /* Seconds */
  4462. ss = ss * 10 + (*p - '0');
  4463. break;
  4464. case 4: /* Fraction of second */
  4465. if (ff < 0)
  4466. ff = (*p > '4') ? 1 : 0;
  4467. break;
  4468. }
  4469. } else if (*p == ':') { /* Colon */
  4470. state++;
  4471. d = 0;
  4472. if (state > 3) {
  4473. makestr(&cmdatemsg,"Too many time fields");
  4474. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4475. return(NULL);
  4476. }
  4477. } else if (*p == '.') {
  4478. if (state == 3) {
  4479. state = 4;
  4480. d = 0;
  4481. } else {
  4482. makestr(&cmdatemsg,"Improper fraction");
  4483. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4484. return(NULL);
  4485. }
  4486. } else if (*p == SP) { /* Space */
  4487. while (*p && (*p == SP)) /* position to first nonspace */
  4488. p++;
  4489. break;
  4490. } else if (isalpha(*p)) { /* AM/PM/Z or timezone */
  4491. break;
  4492. } else if (*p == '+' || *p == '-') { /* GMT offset */
  4493. break;
  4494. } else {
  4495. makestr(&cmdatemsg,"Invalid time characters");
  4496. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4497. return(NULL);
  4498. }
  4499. p++;
  4500. }
  4501. if (!*p) /* If nothing left */
  4502. goto xcmdate; /* go finish up */
  4503. /* At this point we have HH, MM, SS, and FF */
  4504. /* Now handle the rest: AM, PM, and/or timezone info */
  4505. if (!ckstrcmp(p,"am",2,0)) { /* AM/PM... */
  4506. pmflag = 0;
  4507. p += 2;
  4508. } else if (!ckstrcmp(p,"a.m.",4,0)) {
  4509. pmflag = 0;
  4510. p += 4;
  4511. } else if (!ckstrcmp(p,"pm",2,0)) {
  4512. pmflag = 1;
  4513. p += 2;
  4514. } else if (!ckstrcmp(p,"p.m.",4,0)) {
  4515. pmflag = 1;
  4516. p += 4;
  4517. }
  4518. if (pmflag && hh < 12) /* If PM was given */
  4519. hh += 12; /* add 12 to the hour */
  4520. /* Now handle timezone */
  4521. cmtimezone:
  4522. debug(F110,"cmcvtdate timezone",p,0);
  4523. zhh = 0; /* GMT offset HH */
  4524. zmm = 0; /* GMT offset MM */
  4525. gmtsign = 0; /* Sign of GMT offset */
  4526. isgmt = 0; /* 1 if time is GMT */
  4527. while (*p && *p == SP) /* Gobble spaces */
  4528. p++;
  4529. if (!*p) /* If nothing left */
  4530. goto xcmdate; /* we're done */
  4531. if (isalpha(*p)) { /* Something left */
  4532. int zone = 0; /* Alphabetic must be timezone */
  4533. p2 = p; /* Isolate timezone */
  4534. p++;
  4535. while (isalpha(*p))
  4536. p++;
  4537. p3 = p;
  4538. cc = *p;
  4539. *p = NUL;
  4540. p = p2; /* Have timezone, look it up */
  4541. zone = lookup(usatz,p,nusatz,NULL);
  4542. debug(F111,"cmcvtdate timezone alpha",p,zone);
  4543. if (zone < 0) { /* Not found */
  4544. makestr(&cmdatemsg,"Unknown timezone");
  4545. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4546. return(NULL);
  4547. }
  4548. isgmt++; /* All dates are GMT from here down */
  4549. if (zone != 0) { /* But not this one so make it GMT */
  4550. hh += zone; /* RFC 822 timezone: EST etc */
  4551. debug(F101,"cmcvtdate hh + zone","",hh);
  4552. if (hh > 23) { /* Offset crosses date boundary */
  4553. int i;
  4554. long jd;
  4555. jd = mjd(yyyymmdd); /* Get MJD */
  4556. jd += hh / 24; /* Add new day(s) */
  4557. hh = hh % 24; /* and convert back to yyyymmdd */
  4558. ckstrncpy(yyyymmdd,mjd2date(jd),YYYYMMDD);
  4559. debug(F111,"cmcvtdate zone-adjusted date",yyyymmdd,hh);
  4560. for (i = 0; i < 4; i++)
  4561. yearbuf[i] = yyyymmdd[i];
  4562. yearbuf[4] = NUL;
  4563. monbuf[0] = yyyymmdd[4];
  4564. monbuf[1] = yyyymmdd[5];
  4565. monbuf[2] = NUL;
  4566. daybuf[0] = yyyymmdd[6];
  4567. daybuf[1] = yyyymmdd[7];
  4568. daybuf[2] = NUL;
  4569. day = daybuf;
  4570. nday = atoi(daybuf);
  4571. month = monbuf;
  4572. year = yearbuf;
  4573. }
  4574. }
  4575. p = p3; /* Put back whatever we poked above */
  4576. *p = cc;
  4577. } else if (*p == '+' || *p == '-') { /* GMT/UTC offset */
  4578. p3 = p;
  4579. debug(F110,"cmcvtdate timezone GMT offset",p,0);
  4580. gmtsign = (*p == '+') ? -1 : 1;
  4581. isgmt++;
  4582. p++;
  4583. while (*p == SP) p++;
  4584. d = 0;
  4585. p2 = p;
  4586. while (isdigit(*p)) { /* Count digits */
  4587. d++;
  4588. p++;
  4589. }
  4590. if (d != 4) { /* Strict RFC [2]822 */
  4591. isgmt = 0; /* If not exactly 4 digits */
  4592. p = p3; /* it's not a GMT offset. */
  4593. goto delta; /* So treat it as a delta time. */
  4594. }
  4595. d = (d & 1 && *p != ':') ? 1 : 0; /* Odd implies leading '0' */
  4596. p = p2;
  4597. debug(F111,"cmcvtdate GMT offset sign",p,gmtsign);
  4598. debug(F101,"cmcvtdate GMT offset d","",d);
  4599. state = 1;
  4600. while (*p) {
  4601. if (isdigit(*p)) { /* digit */
  4602. if (d++ > 1) {
  4603. state++;
  4604. d = 1;
  4605. }
  4606. switch (state) {
  4607. case 1:
  4608. zhh = zhh * 10 + (*p - '0');
  4609. break;
  4610. case 2:
  4611. zmm = zmm * 10 + (*p - '0');
  4612. break;
  4613. default: /* Ignore seconds or fractions */
  4614. break;
  4615. }
  4616. } else if (*p == ':') { /* Colon */
  4617. state++;
  4618. d = 0;
  4619. } else if (*p == SP || *p == '(') {
  4620. break;
  4621. } else {
  4622. p = p3; /* Maybe it's not a GMT offset. */
  4623. goto delta; /* So treat it as a delta time. */
  4624. }
  4625. p++;
  4626. }
  4627. }
  4628. debug(F110,"cmcvtdate source string after timezone",p,0);
  4629. if (*p) { /* Anything left? */
  4630. p2 = p;
  4631. while (*p2 == SP) /* Skip past spaces */
  4632. p2++;
  4633. if (*p2 == '(') { /* RFC-822 comment? */
  4634. int pc = 1; /* paren counter */
  4635. p2++;
  4636. while (*p2) {
  4637. if (*p2 == ')') {
  4638. if (--pc == 0) {
  4639. p2++;
  4640. break;
  4641. }
  4642. } else if (*p2 == ')') {
  4643. pc++;
  4644. }
  4645. p2++;
  4646. }
  4647. while (*p2 == SP) /* Skip past spaces */
  4648. p2++;
  4649. if (!*p2) /* Anything left? */
  4650. *p = NUL; /* No, erase comment */
  4651. }
  4652. if (!*p2) /* Anything left? */
  4653. goto xcmdate; /* No, done. */
  4654. p = p2;
  4655. delta:
  4656. debug(F110,"cmcvtdate delta yyyymmdd",yyyymmdd,0);
  4657. debug(F110,"cmcvtdate delta year",year,0);
  4658. debug(F110,"cmcvtdate delta p",p,0);
  4659. if (*p == '+' || *p == '-') { /* Delta time */
  4660. int state = NEED_DAYS; /* Start off looking for days */
  4661. char c = 0;
  4662. dsign = 1; /* Get sign */
  4663. if (*p++ == '-')
  4664. dsign = -1;
  4665. while (*p == SP) /* Skip intervening spaces */
  4666. p++;
  4667. while (state) { /* FSA to parse delta time */
  4668. if (state < 0 || !isdigit(*p)) {
  4669. makestr(&cmdatemsg,"Invalid delta time");
  4670. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4671. return(NULL);
  4672. }
  4673. p2 = p; /* Get next numeric field */
  4674. while (isdigit(*p2))
  4675. p2++;
  4676. c = *p2; /* And break character */
  4677. *p2 = NUL; /* Terminate the number */
  4678. switch (state) { /* Interpret according to state */
  4679. case NEED_DAYS: /* Initial */
  4680. if ((c == '-') || /* VMS format */
  4681. ((c == 'd' || c == 'D')
  4682. && !isalpha(*(p2+1)))) { /* Days */
  4683. ddays = atoi(p);
  4684. if (!*(p2+1))
  4685. state = 0;
  4686. else /* if anything is left */
  4687. state = NEED_HRS; /* now we want hours. */
  4688. } else if ((c == 'W' || c == 'w') && !isalpha(*(p2+1))) {
  4689. ddays = atoi(p) * 7; /* weeks... */
  4690. if (!*(p2+1))
  4691. state = 0;
  4692. else
  4693. state = NEED_HRS;
  4694. } else if ((c == 'M' || c == 'm') && !isalpha(*(p2+1))) {
  4695. dmonths = atoi(p); /* months... */
  4696. if (!*(p2+1))
  4697. state = 0;
  4698. else
  4699. state = NEED_HRS;
  4700. } else if ((c == 'Y' || c == 'y') && !isalpha(*(p2+1))) {
  4701. dyears = atoi(p); /* years... */
  4702. if (!*(p2+1))
  4703. state = 0;
  4704. else
  4705. state = NEED_HRS;
  4706. } else if (c == ':') { /* delimiter is colon */
  4707. dhours = atoi(p); /* so it's hours */
  4708. state = NEED_MINS; /* now we want minutes */
  4709. } else if (!c) { /* end of string */
  4710. dhours = atoi(p); /* it's still hours */
  4711. state = 0; /* and we're done */
  4712. } else if (isalpha(c) || c == SP) {
  4713. if (c == SP) { /* It's a keyword? */
  4714. p2++; /* Skip spaces */
  4715. while (*p2 == SP)
  4716. p2++;
  4717. } else { /* or replace first letter */
  4718. *p2 = c;
  4719. }
  4720. p3 = p2; /* p2 points to beginning of keyword */
  4721. while (isalpha(*p3)) /* Find end of keyword */
  4722. p3++;
  4723. c = *p3; /* NUL it out so we can look it up */
  4724. if (*p3) /* p3 points to keyword terminator */
  4725. *p3 = NUL;
  4726. units = lookup(timeunits,p2,nunits,NULL);
  4727. if (units < 0) {
  4728. makestr(&cmdatemsg,"Invalid units in delta time");
  4729. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4730. return(NULL);
  4731. }
  4732. *p2 = NUL; /* Re-terminate the number */
  4733. *p3 = c;
  4734. while (*p3 == SP) /* Point at field after units */
  4735. p3++;
  4736. p2 = p3;
  4737. switch (units) {
  4738. case TU_DAYS:
  4739. ddays = atoi(p);
  4740. break;
  4741. case TU_WEEKS:
  4742. ddays = atoi(p) * 7;
  4743. break;
  4744. case TU_MONTHS:
  4745. dmonths = atoi(p);
  4746. break;
  4747. case TU_YEARS:
  4748. dyears = atoi(p);
  4749. break;
  4750. }
  4751. if (*p2) {
  4752. state = NEED_HRS;
  4753. p2--;
  4754. } else
  4755. state = 0;
  4756. } else { /* Anything else */
  4757. state = -1; /* is an error */
  4758. }
  4759. break;
  4760. case NEED_HRS: /* Looking for hours */
  4761. debug(F000,"cmcvtdate NEED_HRS",p,c);
  4762. if (c == ':') {
  4763. dhours = atoi(p);
  4764. state = NEED_MINS;
  4765. } else if (!c) {
  4766. dhours = atoi(p);
  4767. state = 0;
  4768. } else {
  4769. state = -1;
  4770. }
  4771. break;
  4772. case NEED_MINS: /* Looking for minutes */
  4773. if (c == ':') {
  4774. dmins = atoi(p);
  4775. state = NEED_SECS;
  4776. } else if (!c) {
  4777. dmins = atoi(p);
  4778. state = 0;
  4779. } else {
  4780. state = -1;
  4781. }
  4782. break;
  4783. case NEED_SECS: /* Looking for seconds */
  4784. if (c == '.') {
  4785. dsecs = atoi(p);
  4786. state = NEED_FRAC;
  4787. } else if (!c) {
  4788. dsecs = atoi(p);
  4789. state = 0;
  4790. } else {
  4791. state = -1;
  4792. }
  4793. break;
  4794. case NEED_FRAC: /* Fraction of second */
  4795. if (!c && rdigits(p)) {
  4796. if (*p > '4')
  4797. dsecs++;
  4798. state = 0;
  4799. } else {
  4800. state = -1;
  4801. }
  4802. break;
  4803. }
  4804. if (c) /* next field if any */
  4805. p = p2 + 1;
  4806. }
  4807. havedelta = 1;
  4808. } else {
  4809. makestr(&cmdatemsg,"Extraneous material at end");
  4810. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4811. return(NULL);
  4812. }
  4813. }
  4814. xcmdate:
  4815. if ((t != 2 && hh > 24) || hh < 0) { /* Hour range check */
  4816. makestr(&cmdatemsg,"Invalid hours");
  4817. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4818. return(NULL);
  4819. }
  4820. if (mm > 59) { /* Minute range check */
  4821. makestr(&cmdatemsg,"Invalid minutes");
  4822. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4823. return(NULL);
  4824. }
  4825. if (ff > 0) { /* Fraction of second? */
  4826. if (ss < 59) {
  4827. ss++;
  4828. ff = 0;
  4829. } else if (mm < 59) {
  4830. ss = 0;
  4831. mm++;
  4832. ff = 0;
  4833. } else if (hh < 24) {
  4834. ss = 0;
  4835. mm = 0;
  4836. hh++;
  4837. ff = 0;
  4838. }
  4839. /* Must add a day -- leave ff at 1... */
  4840. /* (DO SOMETHING ABOUT THIS LATER) */
  4841. }
  4842. if (ss > 60) { /* Seconds range check */
  4843. makestr(&cmdatemsg,"Invalid seconds"); /* 60 is ok because of */
  4844. debug(F111,"cmcvtdate",cmdatemsg,-1); /* Leap Second. */
  4845. return(NULL);
  4846. }
  4847. if ((mm < 0 || ss < 0) ||
  4848. (t != 2 && (ss > 0 || mm > 0) && hh > 23)) {
  4849. makestr(&cmdatemsg,"Invalid minutes or seconds");
  4850. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4851. return(NULL);
  4852. }
  4853. debug(F110,"cmcvtdate year",year,0);
  4854. debug(F110,"cmcvtdate month",month,0);
  4855. debug(F101,"cmcvtdate nday","",nday);
  4856. debug(F101,"cmcvtdate hh","",hh);
  4857. debug(F101,"cmcvtdate mm","",mm);
  4858. debug(F101,"cmcvtdate ss","",ss);
  4859. debug(F101,"cmcvtdate gmtsign","",gmtsign);
  4860. debug(F101,"cmcvtdate zhh","",zhh);
  4861. debug(F101,"cmcvtdate zmm","",zmm);
  4862. debug(F101,"cmcvtdate isgmt","",isgmt);
  4863. #ifdef ZLOCALTIME
  4864. /* Handle timezone -- first convert to GMT */
  4865. zdd = 0; /* Days changed */
  4866. if (isgmt && (zmm || zhh)) { /* If GMT offset given */
  4867. long sec1, sec2, zz;
  4868. sec1 = ss + 60 * mm + 3600 * hh;
  4869. sec2 = gmtsign * (60 * zmm + 3600 * zhh);
  4870. sec1 += sec2;
  4871. if (sec1 < 0) {
  4872. sec1 = 0 - sec1;
  4873. zdd = 0L - (sec1 / 86400L);
  4874. sec1 = sec1 % 86400L;
  4875. } else if (sec1 > 86400L) {
  4876. zdd = sec1 / 86400L;
  4877. sec1 = sec1 % 86400L;
  4878. }
  4879. ss = sec1 % 60;
  4880. zz = sec1 / 60;
  4881. mm = zz % 60;
  4882. hh = zz / 60;
  4883. debug(F101,"cmcvtdate NEW hh","",hh);
  4884. debug(F101,"cmcvtdate NEW mm","",mm);
  4885. debug(F101,"cmcvtdate NEW dd","",zdd);
  4886. /* At this point hh:mm:ss is in GMT and zdd is the calendar adjustment */
  4887. }
  4888. #endif /* ZLOCALTIME */
  4889. if (yyyymmdd[0] && !year) {
  4890. ckstrncpy(yearbuf,yyyymmdd,5);
  4891. ckstrncpy(monbuf,&yyyymmdd[4],3);
  4892. ckstrncpy(daybuf,&yyyymmdd[6],3);
  4893. year = yearbuf;
  4894. month = monbuf;
  4895. day = daybuf;
  4896. nday = atoi(daybuf);
  4897. }
  4898. sprintf(zbuf,"%04d%02d%02d %02d:%02d:%02d", /* SAFE */
  4899. atoi(year),atoi(month),nday,hh,mm,ss
  4900. );
  4901. dp = zbuf;
  4902. #ifdef ZLOCALTIME
  4903. /* Now convert from GMT to local time */
  4904. if (isgmt) { /* If GMT convert to local time */
  4905. debug(F110,"cmcvtdate GMT 1",dp,0);
  4906. if (zdd) { /* Apply any calendar adjustment */
  4907. long zz;
  4908. zz = mjd(dp) + zdd;
  4909. sprintf(zbuf,"%s %02d:%02d:%02d",mjd2date(zz),hh,mm,ss);
  4910. }
  4911. debug(F110,"cmcvtdate GMT 2",dp,0);
  4912. if ((p = zlocaltime(dp))) {
  4913. debug(F110,"cmcvtdate asctime zlocaltime",p,0);
  4914. if (p) ckstrncpy(zbuf,p,18);
  4915. }
  4916. debug(F110,"cmcvtdate GMT 3",dp,0);
  4917. for (i = 0; i < 4; i++)
  4918. yearbuf[i] = dp[i];
  4919. yearbuf[4] = NUL;
  4920. monbuf[0] = dp[4];
  4921. monbuf[1] = dp[5];
  4922. monbuf[2] = NUL;
  4923. daybuf[0] = dp[6];
  4924. daybuf[1] = dp[7];
  4925. daybuf[2] = NUL;
  4926. day = daybuf;
  4927. nday = atoi(daybuf);
  4928. month = monbuf;
  4929. year = yearbuf;
  4930. hh = atoi(&dp[9]);
  4931. mm = atoi(&dp[12]);
  4932. ss = atoi(&dp[15]);
  4933. }
  4934. #endif /* ZLOCALTIME */
  4935. #ifdef DEBUG
  4936. if (deblog) {
  4937. debug(F101,"cmcvtdate hour","",hh);
  4938. debug(F101,"cmcvtdate minute","",mm);
  4939. debug(F101,"cmcvtdate second","",ss);
  4940. }
  4941. #endif /* DEBLOG */
  4942. makestr(&cmdatemsg,NULL);
  4943. if (havedelta) {
  4944. #ifdef DEBUG
  4945. if (deblog) {
  4946. debug(F110,"cmcvtdate base ",dp,0);
  4947. debug(F101,"cmcvtdate delta sign","",dsign);
  4948. debug(F101,"cmcvtdate delta yrs ","",dyears);
  4949. debug(F101,"cmcvtdate delta mos ","",dmonths);
  4950. debug(F101,"cmcvtdate delta days","",ddays);
  4951. debug(F101,"cmcvtdate delta hrs ","",dhours);
  4952. debug(F101,"cmcvtdate delta mins","",dmins);
  4953. debug(F101,"cmcvtdate delta secs","",dsecs);
  4954. }
  4955. #endif /* DEBLOG */
  4956. if (!(dp = cmdelta(atoi(year),
  4957. atoi(month),
  4958. nday, hh, mm, ss,
  4959. dsign, dyears, dmonths, ddays, dhours, dmins, dsecs))) {
  4960. debug(F111,"cmcvtdate",cmdatemsg,-1);
  4961. return(NULL);
  4962. }
  4963. }
  4964. xcvtdate: /* Exit point for success */
  4965. {
  4966. int len, k, n;
  4967. char * p;
  4968. debug(F110,"cmcvtdate xcvtdate dp",dp,0);
  4969. if (!dp) dp = ""; /* Shouldn't happen */
  4970. if (!*dp) return(NULL); /* ... */
  4971. len = strlen(dp);
  4972. debug(F111,"cmcvtdate result",dp,len);
  4973. k = cmdatebp - (char *)cmdatebuf; /* Space used */
  4974. n = CMDATEBUF - k - 1; /* Space left */
  4975. if (n < len) { /* Not enough? */
  4976. cmdatebp = cmdatebuf; /* Wrap around */
  4977. n = CMDATEBUF;
  4978. }
  4979. ckstrncpy(cmdatebp,dp,n);
  4980. p = cmdatebp;
  4981. cmdatebp += len + 1;
  4982. return(p);
  4983. }
  4984. }
  4985. int
  4986. cmvdate(d) char * d; { /* Verify date-time */
  4987. int i;
  4988. if (!d) return(0);
  4989. if ((int)strlen(d) != 17) return(0);
  4990. for (i = 0; i < 8; i++) { if (!isdigit(d[i])) return(0); }
  4991. if (!isdigit(d[9]) || !isdigit(d[10]) ||
  4992. !isdigit(d[12]) || !isdigit(d[13]) ||
  4993. !isdigit(d[15]) || !isdigit(d[16]))
  4994. return(0);
  4995. if (!ckstrchr(" Tt_-:",d[8])) return(0);
  4996. if (d[11] != ':' && d[14] != ':') return(0);
  4997. return(1);
  4998. }
  4999. /* c m d i f f d a t e -- Get difference between two date-times */
  5000. char *
  5001. cmdiffdate(d1,d2) char * d1, * d2; {
  5002. char d1buf[9], d2buf[9];
  5003. char x1buf[18], x2buf[18];
  5004. char * p;
  5005. int hh1 = 0, mm1 = 0, ss1 = 0;
  5006. int hh2 = 0, mm2 = 0, ss2 = 0;
  5007. int hh, mm, ss;
  5008. int sign;
  5009. long jd1, jd2, jd, f1, f2, fx;
  5010. static char result[24], *rp;
  5011. debug(F110,"cmdiffdate d1 A",d1,0);
  5012. debug(F110,"cmdiffdate d2 A",d2,0);
  5013. if (!(p = cmcvtdate(d1,1))) /* Convert dates to standard format */
  5014. return(NULL);
  5015. ckstrncpy(x1buf,p,18);
  5016. d1 = x1buf;
  5017. if (!(p = cmcvtdate(d2,1)))
  5018. return(NULL);
  5019. ckstrncpy(x2buf,p,18);
  5020. d2 = x2buf;
  5021. debug(F110,"cmdiffdate d1 B",d1,0);
  5022. debug(F110,"cmdiffdate d2 B",d2,0);
  5023. if (!cmvdate(d1) || !cmvdate(d2))
  5024. return(NULL);
  5025. hh1 = atoi(&d1[9]); /* Get hours, minutes, and seconds */
  5026. mm1 = atoi(&d1[12]); /* for first date */
  5027. ss1 = atoi(&d1[15]);
  5028. ckstrncpy(d1buf,d1,9);
  5029. hh2 = atoi(&d2[9]); /* ditto for second date */
  5030. mm2 = atoi(&d2[12]);
  5031. ss2 = atoi(&d2[15]);
  5032. ckstrncpy(d2buf,d2,9);
  5033. jd1 = mjd(d1buf); /* Get the two Julian dates */
  5034. jd2 = mjd(d2buf);
  5035. f1 = ss1 + 60 * mm1 + 3600 * hh1; /* Convert first time to seconds */
  5036. f2 = ss2 + 60 * mm2 + 3600 * hh2; /* Ditto for second time */
  5037. debug(F101,"cmdiffdate jd1","",jd1);
  5038. debug(F101,"cmdiffdate f1","",f1);
  5039. debug(F101,"cmdiffdate jd2","",jd2);
  5040. debug(F101,"cmdiffdate f2","",f2);
  5041. if (jd2 > jd1 || (jd1 == jd2 && f2 > f1)) {
  5042. sign = -1;
  5043. if (f1 > f2) {jd2--; f2 += 86400L;}
  5044. jd = jd2 - jd1;
  5045. fx = f2 - f1;
  5046. } else {
  5047. sign = 1;
  5048. if (f2 > f1) {jd1--; f1 += 86400L;}
  5049. jd = jd1 - jd2;
  5050. fx = f1 - f2;
  5051. }
  5052. debug(F111,"cmdiffdate sign jd",sign<0?"-":"+",jd);
  5053. debug(F101,"cmdiffdate fx","",fx);
  5054. hh = (int) (fx / 3600L); /* Convert seconds to hh:mm:ss */
  5055. mm = (int) (fx % 3600L) / 60L;
  5056. ss = (int) (fx % 3600L) % 60L;
  5057. rp = result; /* Format the result */
  5058. *rp++ = (sign < 0) ? '-' : '+';
  5059. if (jd != 0 && hh+mm+ss == 0) {
  5060. sprintf(rp,"%ldd",jd);
  5061. } else if (jd == 0) {
  5062. if (ss == 0)
  5063. sprintf(rp,"%d:%02d",hh,mm);
  5064. else
  5065. sprintf(rp,"%d:%02d:%02d",hh,mm,ss);
  5066. } else {
  5067. if (ss == 0)
  5068. sprintf(rp,"%ldd%d:%02d",jd,hh,mm);
  5069. else
  5070. sprintf(rp,"%ldd%d:%02d:%02d",jd,hh,mm,ss);
  5071. }
  5072. debug(F110,"cmdiffdate result",result,0);
  5073. return((char *)result);
  5074. }
  5075. #ifndef NOSPL
  5076. /* s h u f f l e d a t e -- Rearrange date string */
  5077. /*
  5078. Call with:
  5079. A date string in standard format: yyyymmdd hh:mm:ss (time optional).
  5080. Options:
  5081. 1: Reformat date to yyyy-mmm-dd (mmm = English month abbreviation).
  5082. 2: Reformat date to dd-mmm-yyyy (mmm = English month abbreviation).
  5083. 3: Reformat as numeric yyyymmddhhmmss.
  5084. 4: Reformat in asctime() format Sat Nov 26 11:10:34 2005
  5085. 5: Reformat as delimited numeric yyyy:mm:dd:hh:mm:ss.
  5086. Returns:
  5087. Pointer to result if args valid, otherwise original arg pointer.
  5088. */
  5089. char *
  5090. shuffledate(p,opt) char * p; int opt; {
  5091. extern char * wkdays[];
  5092. int len;
  5093. char ibuf[32];
  5094. static char obuf[128];
  5095. char c;
  5096. int yy, dd, mm;
  5097. #define MONTHBUFLEN 32
  5098. char monthbuf[MONTHBUFLEN];
  5099. char * monthstring = NULL;
  5100. #ifdef HAVE_LOCALE
  5101. _PROTOTYP( char * locale_monthname, (int, int) );
  5102. extern int nolocale;
  5103. #endif /* HAVE_LOCALE */
  5104. if (!p) p = "";
  5105. if (!*p) p = ckdate();
  5106. if (opt < 1 || opt > 6)
  5107. return(p);
  5108. len = strlen(p);
  5109. if (len < 8 || len > 31) return(p);
  5110. if (opt == 4) { /* Asctime format (26 Nov 2005) */
  5111. char c, * s;
  5112. long z; int k;
  5113. ckstrncpy(ibuf,p,31);
  5114. k = len;
  5115. while (k >= 0 && ibuf[k] == CR || ibuf[k] == LF)
  5116. ibuf[k--] = NUL;
  5117. while (k >= 0 && ibuf[k] == SP || ibuf[k] == HT)
  5118. ibuf[k--] = NUL;
  5119. if (k < 9) ckstrncpy(&ibuf[8]," 00:00:00",9);
  5120. p = ibuf;
  5121. z = mjd(p); /* Convert to modified Julian date */
  5122. z = z % 7L;
  5123. if (z < 0) {
  5124. z = 0 - z;
  5125. k = 6 - ((int)z + 3) % 7;
  5126. } else {
  5127. k = ((int)z + 3) % 7; /* Day of week */
  5128. }
  5129. s = wkdays[k];
  5130. obuf[0] = s[0]; /* Day of week */
  5131. obuf[1] = s[1];
  5132. obuf[2] = s[2];
  5133. obuf[3] = SP; /* Space */
  5134. c = p[6];
  5135. p[6] = NUL;
  5136. mm = atoi(&ibuf[4]); /* Month */
  5137. s = moname[mm-1]; /* Name of month */
  5138. p[6] = c;
  5139. obuf[4] = s[0]; /* Month */
  5140. obuf[5] = s[1];
  5141. obuf[6] = s[2];
  5142. obuf[7] = SP; /* Space */
  5143. if (p[6] == '0') /* Date of month */
  5144. obuf[8] = SP;
  5145. else
  5146. obuf[8] = p[6];
  5147. obuf[9] = p[7];
  5148. ckstrncpy(&obuf[10],&p[8],10); /* Time */
  5149. obuf[19] = SP; /* Space */
  5150. obuf[20] = p[0]; /* Year */
  5151. obuf[21] = p[1];
  5152. obuf[22] = p[2];
  5153. obuf[23] = p[3];
  5154. obuf[24] = NUL;
  5155. return((char *)obuf);
  5156. }
  5157. if (opt == 5) { /* 20130722 All fields delimited */
  5158. /* yyyymmdd hh:mm:ss */
  5159. /* 0123456789012345678 */
  5160. /* yyyy:mm:dd:hh:mm:ss */
  5161. char sep = ':';
  5162. int i = 0;
  5163. obuf[i++] = p[0]; /* y */
  5164. obuf[i++] = p[1]; /* y */
  5165. obuf[i++] = p[2]; /* y */
  5166. obuf[i++] = p[3]; /* y */
  5167. obuf[i++] = sep; /* */
  5168. obuf[i++] = p[4]; /* m */
  5169. obuf[i++] = p[5]; /* m */
  5170. obuf[i++] = sep; /* */
  5171. obuf[i++] = p[6]; /* d */
  5172. obuf[i++] = p[7]; /* d */
  5173. obuf[i++] = sep; /* */
  5174. obuf[i++] = p[9]; /* h */
  5175. obuf[i++] = p[10]; /* h */
  5176. obuf[i++] = sep; /* */
  5177. obuf[i++] = p[12]; /* m */
  5178. obuf[i++] = p[13]; /* m */
  5179. obuf[i++] = sep; /* */
  5180. obuf[i++] = p[15]; /* s */
  5181. obuf[i++] = p[16]; /* s */
  5182. obuf[i++] = NUL; /* end */
  5183. return((char *)obuf);
  5184. }
  5185. if (opt == 3) {
  5186. ckstrncpy(obuf,p,48);
  5187. /* yyyymmdd hh:mm:ss */
  5188. /* 01234567890123456 */
  5189. /* yyyymmddhhmmss */
  5190. obuf[8] = obuf[9];
  5191. obuf[9] = obuf[10];
  5192. obuf[10] = obuf[12];
  5193. obuf[11] = obuf[13];
  5194. obuf[12] = obuf[15];
  5195. obuf[13] = obuf[16];
  5196. obuf[14] = NUL;
  5197. return((char *)obuf);
  5198. }
  5199. ckstrncpy(ibuf,p,32);
  5200. c = ibuf[4]; /* Warning: not "Y10K compliant" */
  5201. ibuf[4] = NUL;
  5202. if (!rdigits(ibuf))
  5203. return(p);
  5204. yy = atoi(ibuf);
  5205. if (yy < 1 || yy > 9999)
  5206. return(p);
  5207. ibuf[4] = c;
  5208. c = ibuf[6];
  5209. ibuf[6] = NUL;
  5210. if (!rdigits(&ibuf[4]))
  5211. return(p);
  5212. mm = atoi(&ibuf[4]);
  5213. if (mm < 1 || mm > 12)
  5214. return(p);
  5215. ibuf[6] = c;
  5216. c = ibuf[8];
  5217. ibuf[8] = NUL;
  5218. if (!rdigits(&ibuf[6]))
  5219. return(p);
  5220. dd = atoi(&ibuf[6]);
  5221. ibuf[8] = c;
  5222. if (dd < 1 || mm > 31)
  5223. return(p);
  5224. #ifdef HAVE_LOCALE
  5225. /*
  5226. We truncate the month name to 3 characters even though some
  5227. some locales use longer "short month names". Fixing this will
  5228. require some redesign which can be done if ever anybody complains.
  5229. */
  5230. if (!nolocale) { /* If user didn't do --nolocale */
  5231. char *s = NULL;
  5232. if (opt == 1 || opt == 2) { /* Short month name */
  5233. s = locale_monthname(mm-1,1); /* Get short month name */
  5234. if (!s) s = moname[mm-1]; /* Allow for error */
  5235. ckstrncpy(monthbuf,s,MONTHBUFLEN); /* Copy it to this buffer */
  5236. monthbuf[3] = NUL; /* Truncate it at 3 */
  5237. } else {
  5238. s = locale_monthname(mm-1,0); /* Get full month name */
  5239. if (!s) s = fullmonthname[mm-1]; /* Allow for error */
  5240. ckstrncpy(monthbuf,s,MONTHBUFLEN);
  5241. }
  5242. monthstring = monthbuf; /* Point to it */
  5243. } else
  5244. #endif /* HAVE_LOCALE */
  5245. /* Otherwise use old month name table */
  5246. monthstring = (opt == 6) ? fullmonthname[mm-1] : moname[mm-1];
  5247. switch (opt) {
  5248. case 1:
  5249. sprintf(obuf,"%04d-%s-%02d%s",yy,monthstring,dd,&ibuf[8]);
  5250. break;
  5251. case 2:
  5252. sprintf(obuf,"%02d-%s-%04d%s",dd,monthstring,yy,&ibuf[8]);
  5253. break;
  5254. case 6:
  5255. sprintf(obuf,"%d %s %d%s", dd, monthstring, yy, &ibuf[8]);
  5256. break;
  5257. default:
  5258. return(p);
  5259. }
  5260. return((char *)obuf);
  5261. }
  5262. #endif /* NOSPL */
  5263. /* C K C V T D A T E -- Like cmcvtdate(), but returns string. */
  5264. /* For use by date-related functions */
  5265. /* See calling conventions for cmcvtdate() above. */
  5266. char *
  5267. ckcvtdate(p,t) char * p; int t; {
  5268. char * s;
  5269. if (!(s = cmcvtdate(p,t)))
  5270. return("<BAD_DATE_OR_TIME>"); /* \fblah() error message */
  5271. else
  5272. return(s);
  5273. }
  5274. /* C M D A T E -- Parse a date and/or time */
  5275. /*
  5276. Accepts date in various formats. If the date is recognized,
  5277. this routine returns 0 or greater with the result string pointer
  5278. pointing to a buffer containing the date as "yyyymmdd hh:mm:ss".
  5279. */
  5280. int
  5281. cmdate(xhlp,xdef,xp,quiet,f) char *xhlp, *xdef, **xp; int quiet; xx_strp f; {
  5282. int x, rc;
  5283. char *o, *s, *zq, *dp;
  5284. cmfldflgs = 0;
  5285. if (!xhlp) xhlp = "";
  5286. if (!xdef) xdef = "";
  5287. if (!*xhlp) xhlp = "Date and/or time";
  5288. *xp = "";
  5289. rc = cmfld(xhlp,xdef,&s,(xx_strp)0);
  5290. debug(F101,"cmdate cmfld rc","",rc);
  5291. if (rc < 0)
  5292. return(rc);
  5293. debug(F110,"cmdate 1",s,0);
  5294. o = s; /* Remember what they typed. */
  5295. s = brstrip(s);
  5296. debug(F110,"cmdate 2",s,0);
  5297. x = 0;
  5298. if (f) { /* If a conversion function is given */
  5299. char * pp;
  5300. zq = atxbuf; /* do the conversion. */
  5301. pp = atxbuf;
  5302. atxn = CMDBL;
  5303. if ((x = (*f)(s,&zq,&atxn)) < 0) return(-2);
  5304. if (!*pp)
  5305. pp = xdef;
  5306. if (setatm(pp,0) < 0) {
  5307. if (!quiet) printf("?Evaluated date too long\n");
  5308. return(-9);
  5309. }
  5310. s = atxbuf;
  5311. }
  5312. dp = cmcvtdate(s,1);
  5313. if (!dp) {
  5314. if (!quiet) printf("?%s\n",cmdatemsg);
  5315. return(-9);
  5316. }
  5317. *xp = dp;
  5318. return(0);
  5319. }
  5320. #ifdef CK_RECALL /* Command-recall functions */
  5321. /* C M R I N I -- Initialize or change size of command recall buffer */
  5322. int
  5323. cmrini(n) int n; {
  5324. int i;
  5325. if (recall && in_recall) { /* Free old storage, if any */
  5326. for (i = 0; i < cm_recall; i++) {
  5327. if (recall[i]) {
  5328. free(recall[i]);
  5329. recall[i] = NULL;
  5330. }
  5331. }
  5332. free(recall);
  5333. recall = NULL;
  5334. }
  5335. cm_recall = n; /* Set new size */
  5336. rlast = current = -1; /* Initialize pointers */
  5337. if (n > 0) {
  5338. recall = (char **)malloc((cm_recall + 1) * sizeof(char *));
  5339. if (!recall)
  5340. return(1);
  5341. for (i = 0; i < cm_recall; i++) {
  5342. recall[i] = NULL;
  5343. }
  5344. in_recall = 1; /* Recall buffers init'd */
  5345. }
  5346. return(0);
  5347. }
  5348. /* C M A D D N E X T -- Force addition of next command */
  5349. VOID
  5350. cmaddnext() {
  5351. if (on_recall && in_recall) { /* Even if it doesn't come */
  5352. force_add = 1; /* from the keyboard */
  5353. newcmd = 1;
  5354. no_recall = 0;
  5355. }
  5356. }
  5357. /* C M G E T C M D -- Find most recent matching command */
  5358. char *
  5359. cmgetcmd(s) char * s; {
  5360. int i;
  5361. for (i = current; i >= 0; i--) { /* Search backward thru history list */
  5362. if (!recall[i]) continue; /* This one's null, skip it */
  5363. if (ckmatch(s,recall[i],0,1)) /* Match? */
  5364. return(recall[i]); /* Yes, return pointer */
  5365. }
  5366. return(NULL); /* No match, return NULL pointer */
  5367. }
  5368. #endif /* CK_RECALL */
  5369. /* A D D C M D -- Add a command to the recall buffer */
  5370. VOID
  5371. addcmd(s) char * s; {
  5372. int len = 0, nq = 0;
  5373. char * p;
  5374. #ifdef CKLEARN
  5375. extern int learning;
  5376. #endif /* CKLEARN */
  5377. if (xcmdsrc) /* Only for interactive commands */
  5378. return;
  5379. if (!newcmd) /* The command has been here already */
  5380. return; /* so ignore it. */
  5381. newcmd = 0; /* It's new but do this only once. */
  5382. if (!s) s = cmdbuf;
  5383. if (s[0])
  5384. len = strlen(s);
  5385. if (len < 1) /* Don't save empty commands */
  5386. return;
  5387. p = s;
  5388. while (*p) { if (*p++ == '?') nq++; } /* Count question marks */
  5389. #ifdef CKLEARN
  5390. if (learning) /* If a learned script is active */
  5391. learncmd(s); /* record this command. */
  5392. #endif /* CKLEARN */
  5393. debug(F010,"CMD(P)",s,0); /* Maybe record it in the debug log */
  5394. #ifdef CKSYSLOG
  5395. if (ckxlogging) { /* Maybe record it in syslog */
  5396. if (ckxsyslog >= SYSLG_CX || ckxsyslog >= SYSLG_CM)
  5397. cksyslog(SYSLG_CX, 1, "command", s, NULL);
  5398. }
  5399. #endif /* CKSYSLOG */
  5400. #ifdef CK_RECALL
  5401. last_recall = 0;
  5402. if (on_recall && /* Command recall is on? */
  5403. cm_recall > 0 && /* Recall buffer size is > 0? */
  5404. !no_recall) { /* Not not saving this command? */
  5405. if (!force_add && rlast > -1) /* If previous command was identical */
  5406. if (!strcmp(s,recall[rlast])) /* don't add another copy */
  5407. return;
  5408. force_add = 0; /* Reset now in case it was set */
  5409. if (rlast >= cm_recall - 1) { /* Recall buffer full? */
  5410. int i;
  5411. if (recall[0]) { /* Discard oldest command */
  5412. free(recall[0]);
  5413. recall[0] = NULL;
  5414. }
  5415. for (i = 0; i < rlast; i++) { /* The rest */
  5416. recall[i] = recall[i+1]; /* move back */
  5417. }
  5418. rlast--; /* Now we have one less */
  5419. }
  5420. rlast++; /* Index of last command in buffer */
  5421. current = rlast; /* Also now the current command */
  5422. if (current >= cm_recall) { /* Shouldn't happen */
  5423. printf("?Command history error\n"); /* but if it does */
  5424. on_recall = 0; /* turn off command saving */
  5425. #ifdef COMMENT
  5426. } else if (nq > 0) { /* Have at least one question mark */
  5427. recall[current] = malloc(len+nq+1);
  5428. if (recall[current]) {
  5429. p = recall[current];
  5430. while (*s) {
  5431. if (*s == '?')
  5432. *p++ = '\\';
  5433. *p++ = *s++;
  5434. }
  5435. *p = NUL;
  5436. }
  5437. #endif /* COMMENT */
  5438. } else { /* Normal case, just copy */
  5439. recall[current] = malloc(len+1);
  5440. if (recall[current])
  5441. ckstrncpy(recall[current],s,len+1);
  5442. }
  5443. }
  5444. #endif /* CK_RECALL */
  5445. }
  5446. #ifdef CK_RECALL
  5447. /* C M H I S T O R Y */
  5448. VOID
  5449. cmhistory() {
  5450. int i, lc = 1;
  5451. for (i = 0; i <= current; i++) {
  5452. printf(" %s\n", recall[i]);
  5453. if (++lc > (cmd_rows - 2)) { /* Screen full? */
  5454. if (!askmore()) /* Do more-prompting... */
  5455. break;
  5456. else
  5457. lc = 0;
  5458. }
  5459. }
  5460. }
  5461. int
  5462. savhistory(s,disp) char *s; int disp; {
  5463. FILE * fp;
  5464. int i;
  5465. fp = fopen(s, disp ? "a" : "w");
  5466. if (!fp) {
  5467. perror(s);
  5468. return(0);
  5469. }
  5470. for (i = 0; i <= current; i++)
  5471. fprintf(fp,"%s\n", recall[i]);
  5472. fclose(fp);
  5473. return(1);
  5474. }
  5475. #endif /* CK_RECALL */
  5476. #ifdef COMMENT
  5477. /* apparently not used */
  5478. int
  5479. cmgetlc(s) char * s; { /* Get leading char */
  5480. char c;
  5481. while ((c = *s++) <= SP) {
  5482. if (!c)
  5483. break;
  5484. }
  5485. return(c);
  5486. }
  5487. #endif /* COMMENT */
  5488. /* C M C F M -- Parse command confirmation (end of line) */
  5489. /*
  5490. Returns
  5491. -2: User typed anything but whitespace or newline
  5492. -1: Reparse needed
  5493. 0: Confirmation was received
  5494. */
  5495. int
  5496. cmcfm() {
  5497. int x, xc;
  5498. debug(F101,"cmcfm: cmflgs","",cmflgs);
  5499. debug(F110,"cmcfm: atmbuf",atmbuf,0);
  5500. inword = xc = cc = 0;
  5501. setatm("",0); /* (Probably unnecessary) */
  5502. while (cmflgs != 1) {
  5503. x = gtword(0);
  5504. xc += cc;
  5505. switch (x) {
  5506. case -9:
  5507. printf("Command or field too long\n");
  5508. case -4: /* EOF */
  5509. case -2:
  5510. case -1:
  5511. return(x);
  5512. case 1: /* End of line */
  5513. if (xc > 0) {
  5514. if (xcmfdb) {
  5515. return(-6);
  5516. } else {
  5517. printf("?Not confirmed - %s\n",atmbuf);
  5518. return(-9);
  5519. }
  5520. } else
  5521. break; /* Finish up below */
  5522. case 2: /* ESC */
  5523. if (xc == 0) {
  5524. bleep(BP_WARN);
  5525. continue; /* or fall thru. */
  5526. }
  5527. case 0: /* Space */
  5528. if (xc == 0) /* If no chars typed, continue, */
  5529. continue; /* else fall thru. */
  5530. /* else fall thru... */
  5531. case 3: /* Question mark */
  5532. if (xc > 0) {
  5533. if (xcmfdb) {
  5534. return(-6);
  5535. } else {
  5536. printf("?Not confirmed - %s\n",atmbuf);
  5537. return(-9);
  5538. }
  5539. }
  5540. printf(
  5541. "\n Press the Return or Enter key to confirm the command\n");
  5542. printf("%s%s",cmprom,cmdbuf);
  5543. fflush(stdout);
  5544. continue;
  5545. }
  5546. }
  5547. debok = 1;
  5548. return(0);
  5549. }
  5550. /* The following material supports chained parsing functions. */
  5551. /* See ckucmd.h for FDB and OFDB definitions. */
  5552. struct OFDB cmresult = { /* Universal cmfdb result holder */
  5553. NULL, /* Address of succeeding FDB struct */
  5554. 0, /* Function code */
  5555. NULL, /* String result */
  5556. 0, /* Integer result */
  5557. (CK_OFF_T)0 /* Wide result */
  5558. };
  5559. VOID
  5560. cmfdbi(p,fc,s1,s2,s3,n1,n2,f,k,nxt) /* Initialize an FDB */
  5561. struct FDB * p;
  5562. int fc;
  5563. char * s1, * s2, * s3;
  5564. int n1, n2;
  5565. xx_strp f;
  5566. struct keytab * k;
  5567. struct FDB * nxt; {
  5568. p->fcode = fc;
  5569. p->hlpmsg = s1;
  5570. p->dflt = s2;
  5571. p->sdata = s3;
  5572. p->ndata1 = n1;
  5573. p->ndata2 = n2;
  5574. p->spf = f;
  5575. p->kwdtbl = k;
  5576. p->nxtfdb = nxt;
  5577. }
  5578. /* C M F D B -- Parse a field with several possible functions */
  5579. int
  5580. cmfdb(fdbin) struct FDB * fdbin; {
  5581. #ifndef NOSPL
  5582. extern int x_ifnum; /* IF NUMERIC - disables warnings */
  5583. #endif /* NOSPL */
  5584. struct FDB * in = fdbin;
  5585. struct OFDB * out = &cmresult;
  5586. int x = 0, n, r;
  5587. CK_OFF_T w = (CK_OFF_T)0;
  5588. char *s, *xp, *m = NULL;
  5589. int errbits = 0;
  5590. xp = bp;
  5591. out->fcode = -1; /* Initialize output struct */
  5592. out->fdbaddr = NULL;
  5593. out->sresult = NULL;
  5594. out->nresult = 0;
  5595. /*
  5596. Currently we make one trip through the FDBs. So if the user types Esc or
  5597. Tab at the beginning of a field, only the first FDB is examined for a
  5598. default. If the user types ?, help is given only for one FDB. We should
  5599. search through the FDBs for all matching possibilities -- and in particular
  5600. display the pertinent context-sensitive help for each function, rather than
  5601. the only the first one that works, and then rewind the FDB pointer so we
  5602. are not locked out of the earlier ones.
  5603. */
  5604. cmfldflgs = 0;
  5605. while (1) { /* Loop through the chain of FDBs */
  5606. nomsg = 1;
  5607. xcmfdb = 1;
  5608. s = NULL;
  5609. n = 0;
  5610. debug(F101,"cmfdb in->fcode","",in->fcode);
  5611. switch (in->fcode) { /* Current parsing function code */
  5612. case _CMNUM:
  5613. r = in->ndata1;
  5614. if (r != 10 && r != 8) r = 10;
  5615. #ifndef NOSPL
  5616. x_ifnum = 1; /* Disables warning messages */
  5617. #endif /* NOSPL */
  5618. x = cmnum(in->hlpmsg,in->dflt,r,&n,in->spf);
  5619. #ifndef NOSPL
  5620. x_ifnum = 0;
  5621. #endif /* NOSPL */
  5622. debug(F101,"cmfdb cmnum","",x);
  5623. if (x < 0) errbits |= 1;
  5624. break;
  5625. case _CMNUW: /* Wide cmnum - 24 Dec 2005 */
  5626. r = in->ndata1;
  5627. if (r != 10 && r != 8) r = 10;
  5628. #ifndef NOSPL
  5629. x_ifnum = 1; /* Disables warning messages */
  5630. #endif /* NOSPL */
  5631. x = cmnumw(in->hlpmsg,in->dflt,r,&w,in->spf);
  5632. #ifndef NOSPL
  5633. x_ifnum = 0;
  5634. #endif /* NOSPL */
  5635. debug(F101,"cmfdb cmnumw","",w);
  5636. if (x < 0) errbits |= 1;
  5637. break;
  5638. case _CMOFI:
  5639. x = cmofi(in->hlpmsg,in->dflt,&s,in->spf);
  5640. debug(F101,"cmfdb cmofi","",x);
  5641. if (x < 0) errbits |= 2;
  5642. break;
  5643. case _CMIFI:
  5644. x = cmifi2(in->hlpmsg,
  5645. in->dflt,
  5646. &s,
  5647. &n,
  5648. in->ndata1,
  5649. in->sdata,
  5650. in->spf,
  5651. in->ndata2
  5652. );
  5653. debug(F101,"cmfdb cmifi2 x","",x);
  5654. debug(F101,"cmfdb cmifi2 n","",n);
  5655. if (x < 0) errbits |= 4;
  5656. break;
  5657. case _CMFLD:
  5658. cmfldflgs = in->ndata1;
  5659. x = cmfld(in->hlpmsg,in->dflt,&s,in->spf);
  5660. debug(F101,"cmfdb cmfld","",x);
  5661. if (x < 0) errbits |= 8;
  5662. break;
  5663. case _CMTXT:
  5664. x = cmtxt(in->hlpmsg,in->dflt,&s,in->spf);
  5665. debug(F101,"cmfdb cmtxt","",x);
  5666. if (x < 0) errbits |= 16;
  5667. break;
  5668. case _CMKEY:
  5669. x = cmkey2(in->kwdtbl,
  5670. in->ndata1,
  5671. in->hlpmsg,in->dflt,in->sdata,in->spf,in->ndata2);
  5672. debug(F101,"cmfdb cmkey","",x);
  5673. if (x < 0) errbits |= ((in->ndata2 & 4) ? 32 : 64);
  5674. break;
  5675. case _CMCFM:
  5676. x = cmcfm();
  5677. debug(F101,"cmfdb cmcfm","",x);
  5678. if (x < 0) errbits |= 128;
  5679. break;
  5680. default:
  5681. debug(F101,"cmfdb - unexpected function code","",in->fcode);
  5682. printf("?cmfdb - unexpected function code: %d\n",in->fcode);
  5683. }
  5684. debug(F101,"cmfdb x","",x);
  5685. debug(F101,"cmfdb cmflgs","",cmflgs);
  5686. debug(F101,"cmfdb crflag","",crflag);
  5687. debug(F101,"cmfdb qmflag","",qmflag);
  5688. debug(F101,"cmfdb esflag","",esflag);
  5689. if (x > -1) { /* Success */
  5690. out->fcode = in->fcode; /* Fill in output struct */
  5691. out->fdbaddr = in;
  5692. out->sresult = s;
  5693. out->nresult = (in->fcode == _CMKEY) ? x : n;
  5694. out->wresult = w;
  5695. out->kflags = (in->fcode == _CMKEY) ? cmkwflgs : 0;
  5696. debug(F111,"cmfdb out->nresult",out->sresult,out->nresult);
  5697. debug(F111,"cmfdb out->wresult",out->sresult,out->wresult);
  5698. nomsg = 0;
  5699. xcmfdb = 0;
  5700. /* debug(F111,"cmfdb cmdbuf & crflag",cmdbuf,crflag); */
  5701. if (crflag) {
  5702. cmflgs = 1;
  5703. }
  5704. return(x); /* and return */
  5705. }
  5706. in = in->nxtfdb; /* Failed, get next parsing function */
  5707. nomsg = 0;
  5708. xcmfdb = 0;
  5709. if (!in) { /* No more */
  5710. debug(F101,"cmfdb failure x","",x);
  5711. debug(F101,"cmfdb failure errbits","",errbits);
  5712. if (x == -6)
  5713. x = -9;
  5714. if (x == -9) {
  5715. #ifdef CKROOT
  5716. if (ckrooterr)
  5717. m = "Off Limits";
  5718. else
  5719. #endif /* CKROOT */
  5720. /* Make informative messages for a few common cases */
  5721. switch (errbits) {
  5722. case 4+32: m = "Does not match filename or switch"; break;
  5723. case 4+64: m = "Does not match filename or keyword"; break;
  5724. case 1+32: m = "Not a number or valid keyword"; break;
  5725. case 1+64: m = "Not a number or valid switch"; break;
  5726. default: m = "Not valid in this position";
  5727. }
  5728. printf("?%s: \"%s\"\n",m, atmbuf);
  5729. }
  5730. return(x);
  5731. }
  5732. if (x != -2 && x != -6 && x != -9 && x != -3) /* Editing or somesuch */
  5733. return(x); /* Go back and reparse */
  5734. pp = np = bp = xp; /* Back up pointers */
  5735. cmflgs = -1; /* Force a reparse */
  5736. #ifndef NOSPL
  5737. if (!askflag) { /* If not executing ASK-class cmd... */
  5738. #endif /* NOSPL */
  5739. if (crflag) { /* If CR was typed, put it back */
  5740. pushc = LF; /* But as a linefeed */
  5741. } else if (qmflag) { /* Ditto for Question mark */
  5742. pushc = '?';
  5743. } else if (esflag) { /* and Escape or Tab */
  5744. pushc = ESC;
  5745. }
  5746. #ifndef NOSPL
  5747. }
  5748. #endif /* NOSPL */
  5749. }
  5750. }
  5751. /*
  5752. C M I O F I -- Parse an input file OR the name of a nonexistent file.
  5753. Replaces the commented-out version above. This one actually works and
  5754. has the expected straightforward interface.
  5755. */
  5756. int
  5757. cmiofi(xhlp,xdef,xp,wild,f) char *xhlp, *xdef, **xp; int *wild; xx_strp f; {
  5758. int x;
  5759. struct FDB f1, f2;
  5760. cmfdbi(&f1,_CMIFI,xhlp,xdef,"",0,0,f,NULL,&f2);
  5761. cmfdbi(&f2,_CMOFI,"","","",0,0,f,NULL,NULL);
  5762. x = cmfdb(&f1);
  5763. if (x < 0) {
  5764. if (x == -3) {
  5765. x = -9;
  5766. printf("?Filename required\n");
  5767. }
  5768. }
  5769. *wild = cmresult.nresult;
  5770. *xp = cmresult.sresult;
  5771. return(x);
  5772. }
  5773. /* G T W O R D -- Gets a "word" from the command input stream */
  5774. /*
  5775. Usage: retcode = gtword(brk);
  5776. brk = 0 for normal word breaks (space, CR, Esc, ?)
  5777. brk = 1 to add ':' and '=' (for parsing switches). These characters
  5778. act as break characters only if the first character of the field
  5779. is slash ('/'), i.e. switch introducer.
  5780. brk = 4 to not strip comments (used only for "help #" and "help ;").
  5781. Returns:
  5782. -10 Timelimit set and timed out
  5783. -9 if input was too long
  5784. -4 if end of file (e.g. pipe broken)
  5785. -3 if null field
  5786. -2 if command buffer overflows
  5787. -1 if user did some deleting
  5788. 0 if word terminates with SP or tab
  5789. 1 if ... CR
  5790. 2 if ... ESC
  5791. 3 if ... ? (question mark)
  5792. 4 if ... : or = and called with brk != 0
  5793. With:
  5794. pp pointing to beginning of word in buffer
  5795. bp pointing to after current position
  5796. atmbuf containing a copy of the word
  5797. cc containing the number of characters in the word copied to atmbuf
  5798. */
  5799. int
  5800. ungword() { /* Unget a word */
  5801. debug(F101,"ungword cmflgs","",cmflgs);
  5802. if (ungw) return(0);
  5803. cmfsav = cmflgs;
  5804. ungw = 1;
  5805. cmflgs = 0;
  5806. return(0);
  5807. }
  5808. /* Un-un-get word. Undo ungword() if it has been done. */
  5809. VOID
  5810. unungw() {
  5811. debug(F010,"unungw atmbuf",atmbuf,0);
  5812. if (ungw) {
  5813. ungw = 0;
  5814. cmflgs = cmfsav;
  5815. atmbuf[0] = NUL;
  5816. }
  5817. }
  5818. static int
  5819. gtword(brk) int brk; {
  5820. int c; /* Current char */
  5821. int cq = 0; /* Next char */
  5822. int quote = 0; /* Flag for quote character */
  5823. int echof = 0; /* Flag for whether to echo */
  5824. int comment = 0; /* Flag for in comment */
  5825. char *cp = NULL; /* Comment pointer */
  5826. int eintr = 0; /* Flag for syscall interrupted */
  5827. int bracelvl = 0; /* nested brace counter [jrs] */
  5828. int iscontd = 0; /* Flag for continuation */
  5829. int realtty = 0; /* Stdin is really a tty */
  5830. char firstnb = NUL;
  5831. char lastchar = NUL;
  5832. char prevchar = NUL;
  5833. char lbrace, rbrace;
  5834. int dq = 0; /* Doublequote flag */
  5835. int dqn = 0; /* and count */
  5836. int isesc = 0;
  5837. #ifdef FUNCTIONTEST
  5838. /*
  5839. September 2018 - Code to prevent spaces in function argument
  5840. list to cause a word break during command parsing. Matching
  5841. code also added to setatm().
  5842. */
  5843. int fndebug = 0;
  5844. int fnstate = 0; /* Function-parsing state */
  5845. int fnparens = 0; /* Parens counter */
  5846. #endif /* FUNCTIONTEST */
  5847. #ifdef RTU
  5848. extern int rtu_bug;
  5849. #endif /* RTU */
  5850. #ifdef IKSD
  5851. extern int inserver;
  5852. #endif /* IKSD */
  5853. extern int kstartactive;
  5854. #ifdef datageneral
  5855. extern int termtype; /* DG terminal type flag */
  5856. extern int con_reads_mt; /* Console read asynch is active */
  5857. if (con_reads_mt) connoi_mt(); /* Task would interfere w/cons read */
  5858. #endif /* datageneral */
  5859. #ifdef COMMENT
  5860. #ifdef DEBUG
  5861. if (deblog) {
  5862. debug(F101,"gtword brk","",brk);
  5863. debug(F101,"gtword cmfldflgs","",cmfldflgs);
  5864. debug(F101,"gtword swarg","",swarg);
  5865. debug(F101,"gtword dpx","",dpx);
  5866. debug(F101,"gtword echof","",echof);
  5867. #ifndef NOSPL
  5868. debug(F101,"gtword askflag","",askflag);
  5869. debug(F101,"gtword timelimit","",timelimit);
  5870. #ifndef NOLOCAL
  5871. #ifndef NOXFER
  5872. #ifdef CK_AUTODL
  5873. debug(F101,"gtword cmdadl","",cmdadl);
  5874. #endif /* CK_AUTODL */
  5875. #endif /* NOXFER */
  5876. #endif /* NOLOCAL */
  5877. #endif /* NOSPL */
  5878. }
  5879. #endif /* DEBUG */
  5880. #endif /* COMMENT */
  5881. realtty = is_a_tty(0); /* Stdin is really a tty? */
  5882. if (cmfldflgs & 1) {
  5883. lbrace = '(';
  5884. rbrace = ')';
  5885. } else {
  5886. lbrace = '{';
  5887. rbrace = '}';
  5888. }
  5889. crflag = 0;
  5890. qmflag = 0;
  5891. esflag = 0;
  5892. if (swarg) { /* No leading space for switch args */
  5893. inword = 1;
  5894. swarg = 0;
  5895. }
  5896. if (ungw) { /* Have a word saved? */
  5897. #ifdef M_UNGW
  5898. /* Experimental code to allow ungetting multiple words. */
  5899. /* See comments in ckmkey2() above. */
  5900. int x;
  5901. if (np > pp) pp = np;
  5902. while (*pp == SP) pp++;
  5903. if (!*pp) {
  5904. ungw = 0;
  5905. cmflgs = cmfsav;
  5906. } else {
  5907. if ((x = setatm(pp,2)) < 0) {
  5908. printf("?Saved word too long\n");
  5909. return(-9);
  5910. }
  5911. if (pp[x] >= SP) {
  5912. char *p2;
  5913. p2 = pp;
  5914. p2 += x;
  5915. while (*p2 == SP) p2++;
  5916. if (*p2) {
  5917. np = p2;
  5918. ungword();
  5919. }
  5920. } else {
  5921. ungw = 0;
  5922. cmflgs = cmfsav;
  5923. debug(F010,"gtword ungw return atmbuf",atmbuf,0);
  5924. }
  5925. }
  5926. return(cmflgs);
  5927. #else
  5928. /*
  5929. You would think the following should be:
  5930. while (*pp == SP) pp++;
  5931. but you would be wrong -- making this change breaks GOTO.
  5932. */
  5933. while (*pp++ == SP) ;
  5934. if (setatm(pp,2) < 0) {
  5935. printf("?Saved word too long\n");
  5936. return(-9);
  5937. }
  5938. ungw = 0;
  5939. cmflgs = cmfsav;
  5940. debug(F010,"gtword ungw return atmbuf",atmbuf,0);
  5941. return(cmflgs);
  5942. #endif /* M_UNGW */
  5943. }
  5944. pp = np; /* Start of current field */
  5945. #ifdef FUNCTIONTEST
  5946. #ifdef DEBUG
  5947. if (deblog) {
  5948. debug(F110,"gtword cmdbuf",cmdbuf,0);
  5949. debug(F110,"gtword bp",bp,0);
  5950. debug(F110,"gtword pp",pp,0);
  5951. }
  5952. #endif /* DEBUG */
  5953. #endif /* FUNCTIONTEST */
  5954. {
  5955. /* If we are reparsing we have to recount any braces or doublequotes */
  5956. char * p = pp;
  5957. char c;
  5958. if (*p == '"')
  5959. dq++;
  5960. while ((c = *p++))
  5961. if (c == lbrace)
  5962. bracelvl++;
  5963. else if (c == rbrace)
  5964. bracelvl--;
  5965. else if (dq && c == '"')
  5966. dqn++;
  5967. }
  5968. while (bp < cmdbuf+CMDBL) { /* Big get-a-character loop */
  5969. echof = 0; /* Assume we don't echo because */
  5970. chsrc = 0; /* character came from reparse buf. */
  5971. #ifdef BS_DIRSEP
  5972. CMDIRPARSE:
  5973. #endif /* BS_DIRSEP */
  5974. c = *bp;
  5975. cq = *(bp+1);
  5976. debug(F000,"CHAR C","",c); /* FUNCTIONTEST */
  5977. if (!c) { /* If no char waiting in reparse buf */
  5978. if ((dpx
  5979. #ifndef NOSPL
  5980. || echostars
  5981. #endif /* NOSPL */
  5982. ) && (!pushc
  5983. #ifndef NOSPL
  5984. || askflag
  5985. #endif /* NOSPL */
  5986. )) /* Get from tty, set echo flag */
  5987. echof = 1;
  5988. c = cmdgetc(timelimit); /* Read a command character. */
  5989. #ifdef DEBUG
  5990. debug(F101,"gtword c","",c);
  5991. #endif /* DEBUG */
  5992. if (timelimit && c < -1) { /* Timed out */
  5993. return(-10);
  5994. }
  5995. #ifndef NOXFER
  5996. /*
  5997. The following allows packet recognition in the command parser.
  5998. Presently it works only for Kermit packets, and if our current protocol
  5999. happens to be anything besides Kermit, we simply force it to Kermit.
  6000. We don't use the APC mechanism here for mechanical reasons, and also
  6001. because this way, it works even with minimally configured interactive
  6002. versions. Add Zmodem later...
  6003. */
  6004. #ifdef CK_AUTODL
  6005. if ((!local && cmdadl) /* Autodownload enabled? */
  6006. #ifdef IKS_OPTION
  6007. || TELOPT_SB(TELOPT_KERMIT).kermit.me_start
  6008. #endif /* IKS_OPTION */
  6009. ) {
  6010. int k;
  6011. k = kstart((CHAR)c); /* Kermit S or I packet? */
  6012. if (k) {
  6013. int ksign = 0;
  6014. if (k < 0) { /* Minus-Protocol? */
  6015. #ifdef NOSERVER
  6016. goto noserver; /* Need server mode for this */
  6017. #else
  6018. ksign = 1; /* Remember */
  6019. k = 0 - k; /* Convert to actual protocol */
  6020. justone = 1; /* Flag for protocol module */
  6021. #endif /* NOSERVER */
  6022. } else
  6023. justone = 0;
  6024. k--; /* Adjust kstart's return value */
  6025. if (k == PROTO_K) {
  6026. extern int protocol, g_proto;
  6027. extern CHAR sstate;
  6028. g_proto = protocol;
  6029. protocol = PROTO_K; /* Crude... */
  6030. sstate = ksign ? 'x' : 'v';
  6031. cmdbuf[0] = NUL;
  6032. return(-3);
  6033. }
  6034. }
  6035. }
  6036. #ifdef NOSERVER
  6037. noserver:
  6038. #endif /* NOSERVER */
  6039. #endif /* CK_AUTODL */
  6040. #endif /* NOXFER */
  6041. chsrc = 1; /* Remember character source is tty. */
  6042. brkchar = c;
  6043. #ifdef IKSD
  6044. if (inserver && c < 0) { /* End of session? */
  6045. debug(F111,"gtword c < 0","exiting",c);
  6046. return(-4); /* Cleanup and terminate */
  6047. }
  6048. #endif /* IKSD */
  6049. #ifdef OS2
  6050. if (c < 0) { /* Error */
  6051. if (c == -3) { /* Empty word? */
  6052. if (blocklvl > 0) /* In a block */
  6053. continue; /* so keep looking for block end */
  6054. else
  6055. return(-3); /* Otherwise say we got nothing */
  6056. } else { /* Not empty word */
  6057. return(-4); /* So some kind of i/o error */
  6058. }
  6059. }
  6060. #else
  6061. #ifdef MAC
  6062. if (c == -3) /* Empty word... */
  6063. if (blocklvl > 0)
  6064. continue;
  6065. else
  6066. return(-3);
  6067. #endif /* MAC */
  6068. #endif /* OS2 */
  6069. if (c == EOF) { /* This can happen if stdin not tty. */
  6070. #ifdef EINTR
  6071. /*
  6072. Some operating and/or C runtime systems return EINTR for no good reason,
  6073. when the end of the standard input "file" is encountered. In cases like
  6074. this, we get into an infinite loop; hence the eintr counter, which is reset
  6075. to 0 upon each call to this routine.
  6076. */
  6077. debug(F101,"gtword EOF","",errno);
  6078. if (errno == EINTR && ++eintr < 4) /* When bg'd process is */
  6079. continue; /* fg'd again. */
  6080. #endif /* EINTR */
  6081. return(-4);
  6082. }
  6083. c &= cmdmsk; /* Strip any parity bit */
  6084. } /* if desired. */
  6085. /* Now we have the next character */
  6086. isesc = (c == ESC); /* A real ESC? */
  6087. if (!firstnb && c > SP) { /* First nonblank */
  6088. firstnb = c;
  6089. if (c == '"') /* Starts with doublequote */
  6090. dq = 1;
  6091. }
  6092. if (c == '"') /* Count doublequotes */
  6093. dqn++;
  6094. #ifdef FUNCTIONTEST /* gtword() */
  6095. if (fnstate == 0 && c == '\\') {
  6096. fnstate = 1;
  6097. if (fndebug) printf("g%d%c/",fnstate,c);
  6098. } else if (fnstate == 1 && c == '\\') {
  6099. /* because gtword doubles the backslash */
  6100. fnstate = 1;
  6101. if (fndebug) printf("g%d%c/",fnstate,c);
  6102. } else if (fnstate == 1) {
  6103. fnstate = (c == 'f' || c == 'F') ? 2 : 0;
  6104. if (fndebug) printf("g%d%c/",fnstate,c);
  6105. } else if (fnstate == 2 && isalpha(c)) {
  6106. fnstate = 3;
  6107. if (fndebug) printf("g%d%c/",fnstate,c);
  6108. } else if (fnstate == 3 && isalpha(c)) {
  6109. fnstate = 4;
  6110. if (fndebug) printf("g%d%c/",fnstate,c);
  6111. } else if (fnstate == 4 && c == '(') {
  6112. fnstate = 5;
  6113. fnparens++;
  6114. if (fndebug) printf("g%d%c/",fnstate,c);
  6115. } else if (fnstate == 5 && c == ')') {
  6116. fnparens--;
  6117. if (fnparens == 0) {
  6118. fnstate = 0;
  6119. }
  6120. if (fndebug) printf("g%d%c/",fnstate,c);
  6121. }
  6122. #endif /* FUNCTIONTEST */
  6123. if (quote && (c == CR || c == LF)) { /* Enter key following quote */
  6124. *bp++ = CMDQ; /* Double it */
  6125. *bp = NUL;
  6126. quote = 0;
  6127. }
  6128. if (quote == 0) { /* If this is not a quoted character */
  6129. switch (c) {
  6130. case CMDQ: /* Got the quote character itself */
  6131. if (!comment && quoting)
  6132. quote = 1; /* Flag it if not in a comment */
  6133. break;
  6134. case FF: /* Formfeed. */
  6135. c = NL; /* Replace with newline */
  6136. cmdclrscn(); /* Clear the screen */
  6137. break;
  6138. case HT: /* Horizontal Tab */
  6139. if (comment) /* If in comment, */
  6140. c = SP; /* substitute space */
  6141. else /* otherwise */
  6142. c = ESC; /* substitute ESC (for completion) */
  6143. break;
  6144. case ';': /* Trailing comment */
  6145. case '#':
  6146. if (! (brk & 4) ) { /* If not keeping comments */
  6147. if (inword == 0 && quoting) { /* If not in a word */
  6148. comment = 1; /* start a comment. */
  6149. cp = bp; /* remember where it starts. */
  6150. }
  6151. }
  6152. break;
  6153. }
  6154. if (!kstartactive && /* Not in possible Kermit packet */
  6155. !comment && c == SP) { /* Space not in comment */
  6156. *bp++ = (char) c; /* deposit in buffer if not already */
  6157. debug(F101,"gtword SPACE fnstate","",fnstate);
  6158. debug(F101,"gtword SPACE inword","",inword);
  6159. #ifdef BEBOX
  6160. if (echof) {
  6161. cmdecho((char) c, 0); /* Echo what was typed. */
  6162. fflush(stdout);
  6163. fflush(stderr);
  6164. }
  6165. #else
  6166. if (echof) {
  6167. cmdecho((char) c, 0); /* Echo what was typed. */
  6168. if (timelimit)
  6169. fflush(stdout);
  6170. }
  6171. #endif /* BEBOX */
  6172. if (inword == 0
  6173. #ifdef FUNCTIONTEST
  6174. && !fnstate
  6175. #endif /* FUNCTIONTEST */
  6176. ) { /* If leading, gobble it. */
  6177. pp++;
  6178. continue;
  6179. } else {
  6180. #ifdef FUNCTIONTEST
  6181. if (fnstate == 5) { /* Space inside function arg list */
  6182. debug(F101,"SP in fn arglist fnstate","",fnstate);
  6183. continue;
  6184. }
  6185. #endif /* FUNCTIONTEST */
  6186. if ((!dq && ((*pp != lbrace) || (bracelvl == 0))) ||
  6187. (dq && dqn > 1 && *(bp-2) == '"')) {
  6188. np = bp; /* If field-terminating space, return. */
  6189. cmbptr = np;
  6190. if (setatm(pp,0) < 0) {
  6191. printf("?Field too long error 1\n");
  6192. debug(F111,"gtword too long #1",pp,strlen(pp));
  6193. return(-9);
  6194. }
  6195. brkchar = c;
  6196. #ifdef FUNCTIONTEST
  6197. debug(F110,"XXX atmbuf",atmbuf,0);
  6198. debug(F110,"XXX pp",pp,0);
  6199. debug(F101,"XXX brkchar","",c);
  6200. #endif /* FUNCTIONTEST */
  6201. inword = cmflgs = 0;
  6202. return(0);
  6203. }
  6204. continue;
  6205. }
  6206. }
  6207. if (c == lbrace) {
  6208. bracelvl++;
  6209. /* debug(F101,"gtword bracelvl++","",bracelvl); */
  6210. }
  6211. if (c == rbrace && bracelvl > 0) {
  6212. bracelvl--;
  6213. /* debug(F101,"gtword bracelvl--","",bracelvl); */
  6214. if (linebegin)
  6215. blocklvl--;
  6216. }
  6217. if ((c == '=' || c == ':') &&
  6218. /* ^^^ */
  6219. !kstartactive && !comment && brk /* && (firstnb == '/') */
  6220. ) {
  6221. *bp++ = (char) c; /* Switch argument separator */
  6222. /* debug(F111,"gtword switch argsep",cmdbuf,brk); */
  6223. #ifdef BEBOX
  6224. if (echof) {
  6225. cmdecho((char) c, 0); /* Echo what was typed. */
  6226. fflush(stdout);
  6227. fflush(stderr);
  6228. }
  6229. #else
  6230. if (echof) {
  6231. cmdecho((char) c, 0); /* Echo what was typed. */
  6232. if (timelimit)
  6233. fflush(stdout);
  6234. }
  6235. #endif /* BEBOX */
  6236. if ((*pp != lbrace) || (bracelvl == 0)) {
  6237. np = bp;
  6238. cmbptr = np;
  6239. if (setatm(pp,2) < 0) { /* ^^^ */
  6240. printf("?Field too long error 1\n");
  6241. debug(F111,"gtword too long #1",pp,strlen(pp));
  6242. return(-9);
  6243. }
  6244. inword = cmflgs = 0;
  6245. brkchar = c;
  6246. return(4);
  6247. }
  6248. }
  6249. if (c == LF || c == CR) { /* CR or LF. */
  6250. if (echof) {
  6251. cmdnewl((char)c); /* echo it. */
  6252. #ifdef BEBOX
  6253. fflush(stdout);
  6254. fflush(stderr);
  6255. #endif /* BEBOX */
  6256. }
  6257. {
  6258. /* Trim trailing comment and whitespace */
  6259. char *qq;
  6260. if (comment) { /* Erase comment */
  6261. while (bp >= cp) /* Back to comment pointer */
  6262. *bp-- = NUL;
  6263. bp++;
  6264. pp = bp; /* Adjust other pointers */
  6265. inword = 0; /* and flags */
  6266. comment = 0;
  6267. cp = NULL;
  6268. }
  6269. qq = inword ? pp : (char *)cmdbuf;
  6270. /* Erase trailing whitespace */
  6271. while (bp > qq && (*(bp-1) == SP || *(bp-1) == HT)) {
  6272. bp--;
  6273. /* debug(F000,"erasing","",*bp); */
  6274. *bp = NUL;
  6275. }
  6276. lastchar = (bp > qq) ? *(bp-1) : NUL;
  6277. prevchar = (bp > qq+1) ? *(bp-2) : NUL;
  6278. }
  6279. if (linebegin && blocklvl > 0) /* Blank line in {...} block */
  6280. continue;
  6281. linebegin = 1; /* At beginning of next line */
  6282. iscontd = prevchar != CMDQ &&
  6283. (lastchar == '-' || lastchar == lbrace);
  6284. debug(F101,"gtword iscontd","",iscontd);
  6285. if (iscontd) { /* If line is continued... */
  6286. if (chsrc) { /* If reading from tty, */
  6287. if (*(bp-1) == lbrace) { /* Check for "begin block" */
  6288. *bp++ = SP; /* Insert a space for neatness */
  6289. blocklvl++; /* Count block nesting level */
  6290. } else { /* Or hyphen */
  6291. bp--; /* Overwrite the hyphen */
  6292. }
  6293. *bp = NUL; /* erase the dash, */
  6294. continue; /* and go back for next char now. */
  6295. }
  6296. } else if (blocklvl > 0) { /* No continuation character */
  6297. if (chsrc) { /* But we're in a "block" */
  6298. *bp++ = ','; /* Add comma */
  6299. *bp = NUL;
  6300. continue;
  6301. }
  6302. } else { /* No continuation, end of command. */
  6303. *bp = NUL; /* Terminate the command string. */
  6304. if (comment) { /* If we're in a comment, */
  6305. comment = 0; /* Say we're not any more, */
  6306. *cp = NUL; /* cut it off. */
  6307. }
  6308. np = bp; /* Where to start next field. */
  6309. cmbptr = np;
  6310. if (setatm(pp,0) < 0) { /* Copy field to atom buffer */
  6311. debug(F111,"gtword too long #2",pp,strlen(pp));
  6312. printf("?Field too long error 2\n");
  6313. return(-9);
  6314. }
  6315. inword = 0; /* Not in a word any more. */
  6316. crflag = 1;
  6317. /* debug(F110,"gtword","crflag is set",0); */
  6318. #ifdef CK_RECALL
  6319. current = rlast;
  6320. #endif /* CK_RECALL */
  6321. cmflgs = 1;
  6322. if (!xcmdsrc
  6323. #ifdef CK_RECALL
  6324. || force_add
  6325. #endif /* CK_RECALL */
  6326. )
  6327. addcmd(cmdbuf);
  6328. return(cmflgs);
  6329. }
  6330. }
  6331. /*
  6332. This section handles interactive help, completion, editing, and history.
  6333. Rearranged as a switch statement executed only if we're at top level since
  6334. there is no need for any of this within command files and macros: Aug 2000.
  6335. Jun 2001: Even if at top level, skip this if the character was fetched from
  6336. the reparse or recall buffer, or if stdin is redirected.
  6337. */
  6338. if ((xcmdsrc == 0 /* Only at top level */
  6339. #ifndef NOSPL
  6340. || askflag /* or user is typing ASK response */
  6341. #endif /* NOSPL */
  6342. ) && chsrc != 0 && realtty) { /* from the real keyboard */
  6343. /* Use ANSI / VT100 up and down arrow keys for command recall. */
  6344. if (isesc && (
  6345. #ifdef IKSD
  6346. inserver
  6347. #else
  6348. 0
  6349. #endif /* IKSD */
  6350. #ifdef USE_ARROWKEYS
  6351. || 1
  6352. #endif /* USE_ARROWKEYS */
  6353. )
  6354. ) { /* A real ESC was typed */
  6355. int x;
  6356. msleep(200); /* Wait 1/5 sec */
  6357. x = cmdconchk(); /* Was it followed by anything? */
  6358. debug(F101,"Arrowkey ESC cmdconchk","",x);
  6359. if (x > 1) { /* If followed by at least 2 chars */
  6360. int c2;
  6361. c2 = cmdgetc(0); /* Get the first one */
  6362. debug(F101,"Arrowkey ESC c2","",c2);
  6363. if (c2 != '[' && c2 != 'O') { /* If not [ or O */
  6364. pushc = c2; /* Push it and take the ESC solo */
  6365. } else {
  6366. c2 = cmdgetc(0); /* Get the second one */
  6367. debug(F101,"Arrowkey ESC c3","",c2);
  6368. switch (c2) {
  6369. #ifndef NORECALL
  6370. case 'A': /* Up */
  6371. c = BEL;
  6372. c = C_UP;
  6373. break;
  6374. case 'B': /* Down */
  6375. c = BEL;
  6376. c = C_DN;
  6377. break;
  6378. case 'C': /* Right */
  6379. case 'D': /* Left */
  6380. #else
  6381. default:
  6382. #endif /* NORECALL */
  6383. c = BEL; /* We don't use these yet */
  6384. break;
  6385. }
  6386. }
  6387. }
  6388. }
  6389. switch (c) {
  6390. case '?': /* ?-Help */
  6391. #ifndef NOSPL
  6392. if (askflag) /* No help in ASK response */
  6393. break;
  6394. #endif /* NOSPL */
  6395. if (quoting
  6396. && !kstartactive
  6397. && !comment
  6398. ) {
  6399. cmdecho((char) c, 0);
  6400. *bp = NUL;
  6401. if (setatm(pp,0) < 0) {
  6402. debug(F111,"gtword too long ?",pp,strlen(pp));
  6403. printf("?Too long\n");
  6404. return(-9);
  6405. }
  6406. qmflag = 1;
  6407. return(cmflgs = 3);
  6408. }
  6409. case ESC: /* Esc or Tab completion */
  6410. if (!comment) {
  6411. *bp = NUL;
  6412. if (setatm(pp,0) < 0) {
  6413. debug(F111,"gtword too long Esc",pp,strlen(pp));
  6414. printf("?Too long\n");
  6415. return(-9);
  6416. }
  6417. esflag = 1;
  6418. return(cmflgs = 2);
  6419. } else {
  6420. bleep(BP_WARN);
  6421. continue;
  6422. }
  6423. case BS: /* Character deletion */
  6424. case RUB:
  6425. if (bp > cmdbuf) { /* If still in buffer... */
  6426. cmdchardel(); /* erase it. */
  6427. bp--; /* point behind it, */
  6428. if (*bp == lbrace) bracelvl--; /* Adjust brace count */
  6429. if (*bp == rbrace) bracelvl++;
  6430. if ((*bp == SP) && /* Flag if current field gone */
  6431. (*pp != lbrace || bracelvl == 0))
  6432. inword = 0;
  6433. *bp = NUL; /* Erase character from buffer. */
  6434. } else { /* Otherwise, */
  6435. bleep(BP_WARN);
  6436. cmres(); /* and start parsing a new command. */
  6437. *bp = *atmbuf = NUL;
  6438. }
  6439. if (pp < bp)
  6440. continue;
  6441. else
  6442. return(cmflgs = -1);
  6443. case LDEL: /* ^U, line deletion */
  6444. while ((bp--) > cmdbuf) {
  6445. cmdchardel();
  6446. *bp = NUL;
  6447. }
  6448. cmres(); /* Restart the command. */
  6449. *bp = *atmbuf = NUL;
  6450. inword = 0;
  6451. return(cmflgs = -1);
  6452. case WDEL: /* ^W, word deletion */
  6453. if (bp <= cmdbuf) { /* Beep if nothing to delete */
  6454. bleep(BP_WARN);
  6455. cmres();
  6456. *bp = *atmbuf = NUL;
  6457. return(cmflgs = -1);
  6458. }
  6459. bp--;
  6460. /* Back up over any trailing nonalphanums */
  6461. /* This is dependent on ASCII collating sequence */
  6462. /* but isalphanum() is not available everywhere. */
  6463. for ( ;
  6464. (bp >= cmdbuf) &&
  6465. ((*bp < '0') ||
  6466. ((*bp > '9') && (*bp < '@')) ||
  6467. ((*bp > 'Z') && (*bp < 'a')) ||
  6468. (*bp > 'z'));
  6469. bp--
  6470. ) {
  6471. cmdchardel();
  6472. *bp = NUL;
  6473. }
  6474. /* Now delete back to rightmost remaining nonalphanum */
  6475. for ( ; (bp >= cmdbuf) && (*bp) ; bp--) {
  6476. if ((*bp < '0') ||
  6477. (*bp > '9' && *bp < '@') ||
  6478. (*bp > 'Z' && *bp < 'a') ||
  6479. (*bp > 'z'))
  6480. break;
  6481. cmdchardel();
  6482. *bp = NUL;
  6483. }
  6484. bp++;
  6485. inword = 0;
  6486. return(cmflgs = -1);
  6487. case RDIS: { /* ^R, redisplay */
  6488. char *cpx; char cx;
  6489. *bp = NUL;
  6490. printf("\n%s",cmprom);
  6491. cpx = cmdbuf;
  6492. while ((cx = *cpx++)) {
  6493. cmdecho(cx,0);
  6494. }
  6495. fflush(stdout);
  6496. continue;
  6497. }
  6498. #ifndef NOLASTFILE
  6499. case VT:
  6500. if (lastfile) {
  6501. printf("%s ",lastfile);
  6502. #ifdef GEMDOS
  6503. fflush(stdout);
  6504. #endif /* GEMDOS */
  6505. inword = cmflgs = 0;
  6506. addbuf(lastfile); /* Supply default. */
  6507. if (setatm(lastfile,0) < 0) {
  6508. printf("Last name too long\n");
  6509. if (np) free(np);
  6510. return(-9);
  6511. }
  6512. } else { /* No default */
  6513. bleep(BP_WARN);
  6514. }
  6515. return(0);
  6516. #endif /* NOLASTFILE */
  6517. }
  6518. #ifdef CK_RECALL
  6519. if (on_recall && /* Reading commands from keyboard? */
  6520. (cm_recall > 0) && /* Saving commands? */
  6521. (c == C_UP || c == C_UP2)) { /* Go up one */
  6522. if (last_recall == 2 && current > 0)
  6523. current--;
  6524. if (current < 0) { /* Nowhere to go, */
  6525. bleep(BP_WARN);
  6526. continue;
  6527. }
  6528. if (recall[current]) { /* We have a previous command */
  6529. while ((bp--) > cmdbuf) { /* Erase current line */
  6530. cmdchardel();
  6531. *bp = NUL;
  6532. }
  6533. ckstrncpy(cmdbuf,recall[current],CMDBL);
  6534. #ifdef OSK
  6535. fflush(stdout);
  6536. write(fileno(stdout), "\r", 1);
  6537. printf("%s%s",cmprom,cmdbuf);
  6538. #else
  6539. printf("\r%s%s",cmprom,cmdbuf);
  6540. #endif /* OSK */
  6541. current--;
  6542. }
  6543. last_recall = 1;
  6544. return(cmflgs = -1); /* Force a reparse */
  6545. }
  6546. if (on_recall && /* Reading commands from keyboard? */
  6547. (cm_recall > 0) && /* Saving commands? */
  6548. (c == C_DN)) { /* Down one */
  6549. int x = 1;
  6550. if (last_recall == 1)
  6551. x++;
  6552. if (current + x > rlast) { /* Already at bottom, beep */
  6553. bleep(BP_WARN);
  6554. continue;
  6555. }
  6556. current += x; /* OK to go down */
  6557. if (recall[current]) {
  6558. while ((bp--) > cmdbuf) { /* Erase current line */
  6559. cmdchardel();
  6560. *bp = NUL;
  6561. }
  6562. ckstrncpy(cmdbuf,recall[current],CMDBL);
  6563. #ifdef OSK
  6564. fflush(stdout);
  6565. write(fileno(stdout), "\r", 1);
  6566. printf("%s%s",cmprom,cmdbuf);
  6567. #else
  6568. printf("\r%s%s",cmprom,cmdbuf);
  6569. #endif /* OSK */
  6570. last_recall = 2;
  6571. return(cmflgs = -1); /* Force reparse */
  6572. }
  6573. }
  6574. #endif /* CK_RECALL */
  6575. }
  6576. if (c < SP && quote == 0) { /* Any other unquoted control char */
  6577. if (!chsrc) { /* If cmd file, point past it */
  6578. bp++;
  6579. } else {
  6580. bleep(BP_WARN);
  6581. }
  6582. continue; /* continue, don't put in buffer */
  6583. }
  6584. linebegin = 0; /* Not at beginning of line */
  6585. #ifdef BEBOX
  6586. if (echof) {
  6587. cmdecho((char) c, 0); /* Echo what was typed. */
  6588. fflush (stdout);
  6589. fflush(stderr);
  6590. }
  6591. #else
  6592. #ifdef NOSPL
  6593. if (echof || chsrc)
  6594. #else
  6595. if (echof || (echostars && chsrc))
  6596. #endif /* NOSPL */
  6597. cmdecho((char) c, 0); /* Echo what was typed. */
  6598. #endif /* BEBOX */
  6599. } else { /* This character was quoted. */
  6600. int qf = 1;
  6601. quote = 0; /* Unset the quote flag. */
  6602. /* debug(F000,"gtword quote 0","",c); */
  6603. /* Quote character at this level is only for SP, ?, and controls */
  6604. /* If anything else was quoted, leave quote in, and let */
  6605. /* the command-specific parsing routines handle it, e.g. \007 */
  6606. if (c > 32 && c != '?' && c != RUB && chsrc != 0) {
  6607. /* debug(F000,"gtword quote 1","",c); */
  6608. *bp++ = CMDQ; /* Deposit \ if it came from tty */
  6609. qf = 0; /* and don't erase it from screen */
  6610. linebegin = 0; /* Not at beginning of line */
  6611. #ifdef BS_DIRSEP
  6612. /*
  6613. This is a hack to handle "cd \" or "cd foo\" on OS/2 and similar systems.
  6614. If we were called from cmdir() and the previous character was the quote
  6615. character, i.e. backslash, and this character is the command terminator,
  6616. then we stuff an extra backslash into the buffer without echoing, then
  6617. we stuff the carriage return back in again, and go back and process it,
  6618. this time with the quote flag off.
  6619. */
  6620. } else if (dirnamflg && (c == CR || c == LF || c == SP)) {
  6621. /* debug(F000,"gtword quote 2","",c); */
  6622. *bp++ = CMDQ;
  6623. linebegin = 0; /* Not at beginning of line */
  6624. *bp = (c == SP ? SP : CR);
  6625. goto CMDIRPARSE;
  6626. #endif /* BS_DIRSEP */
  6627. }
  6628. #ifdef BEBOX
  6629. if (echof) {
  6630. cmdecho((char) c, qf); /* Echo what was typed. */
  6631. fflush (stdout);
  6632. fflush(stderr);
  6633. }
  6634. #else
  6635. if (echof) cmdecho((char) c, qf); /* Now echo quoted character */
  6636. #endif /* BEBOX */
  6637. /* debug(F111,"gtword quote",cmdbuf,c); */
  6638. }
  6639. #ifdef COMMENT
  6640. if (echof) cmdecho((char) c,quote); /* Echo what was typed. */
  6641. #endif /* COMMENT */
  6642. if (!comment) inword = 1; /* Flag we're in a word. */
  6643. if (quote) continue; /* Don't deposit quote character. */
  6644. if (c != NL) { /* Deposit command character. */
  6645. *bp++ = (char) c; /* and make sure there is a NUL */
  6646. #ifdef COMMENT
  6647. *bp = NUL; /* after it */
  6648. #endif /* COMMENT */
  6649. }
  6650. } /* End of big while */
  6651. bleep(BP_WARN);
  6652. printf("?Command too long, maximum length: %d.\n",CMDBL);
  6653. cmflgs = -2;
  6654. return(-9);
  6655. }
  6656. /* Utility functions */
  6657. /* A D D B U F -- Add the string pointed to by cp to the command buffer */
  6658. static int
  6659. addbuf(cp) char *cp; {
  6660. int len = 0;
  6661. while ((*cp != NUL) && (bp < cmdbuf+CMDBL)) {
  6662. *bp++ = *cp++; /* Copy and */
  6663. len++; /* count the characters. */
  6664. }
  6665. *bp++ = SP; /* Put a space at the end */
  6666. *bp = NUL; /* Terminate with a null */
  6667. np = bp; /* Update the next-field pointer */
  6668. cmbptr = np;
  6669. return(len); /* Return the length */
  6670. }
  6671. /* S E T A T M -- Deposit a token in the atom buffer. */
  6672. /*
  6673. Break on space, newline, carriage return, or NUL.
  6674. Call with:
  6675. cp = Pointer to string to copy to atom buffer.
  6676. fcode = 0 means break on whitespace or EOL.
  6677. fcode = 1 means don't break on space.
  6678. fcode = 2 means break on space, ':', or '='.
  6679. fcode = 3 means copy the whole string.
  6680. Null-terminate the result.
  6681. Return length of token, and also set global "cc" to this length.
  6682. Return -1 if token was too long.
  6683. */
  6684. static int
  6685. setatm(cp,fcode) char *cp; int fcode; {
  6686. char *ap, *xp, *dqp = NULL, lbrace, rbrace;
  6687. int bracelvl = 0, dq = 0;
  6688. int c; /* current char */
  6689. #ifdef FUNCTIONTEST
  6690. /*
  6691. September 2018 - Code to prevent spaces in function argument
  6692. list to cause a word break during command parsing. Matching
  6693. code also added to setatm().
  6694. */
  6695. int fnstate = 0; /* Function-parsing state */
  6696. int fnparens = 0; /* Parens counter */
  6697. int fndebug = 0;
  6698. #endif /* FUNCTIONTEST */
  6699. register char * s;
  6700. register int n = 0;
  6701. #ifdef FUNCTIONTEST
  6702. /*
  6703. printf("---------------------------------\n");
  6704. printf("SETATM...\n");
  6705. printf("CP=[%s]\n",cp);
  6706. */
  6707. #endif /* FUNCTIONTEST */
  6708. if (cmfldflgs & 1) { /* Handle grouping */
  6709. lbrace = '(';
  6710. rbrace = ')';
  6711. } else {
  6712. lbrace = '{';
  6713. rbrace = '}';
  6714. }
  6715. cc = 0; /* Character counter */
  6716. ap = atmbuf; /* Address of atom buffer */
  6717. s = cp;
  6718. while (*s++) n++; /* Save a call to strlen */
  6719. if (n > ATMBL) {
  6720. printf("?Command buffer overflow\n");
  6721. return(-1);
  6722. }
  6723. /* debug(F111,"setatm",cp,n); */
  6724. if (cp == ap) { /* In case source is atom buffer */
  6725. xp = atybuf; /* make a copy */
  6726. #ifdef COMMENT
  6727. strncpy(xp,ap,ATMBL); /* so we can copy it back, edited. */
  6728. cp = xp;
  6729. #else
  6730. s = ap;
  6731. while ((*xp++ = *s++)) ; /* We already know it's big enough */
  6732. cp = xp = atybuf;
  6733. #endif /* COMMENT */
  6734. }
  6735. *ap = NUL; /* Zero the atom buffer */
  6736. if (fcode == 1) { /* Trim trailing blanks */
  6737. while (--n >= 0 && cp[n] == SP)
  6738. ;
  6739. cp[n+1] = NUL;
  6740. }
  6741. while (*cp == SP) { /* Trim leading spaces */
  6742. cp++;
  6743. n--;
  6744. }
  6745. if (*cp == '"') { /* Starts with doublequote? */
  6746. dq = 1;
  6747. dqp = cp;
  6748. }
  6749. while (*cp) {
  6750. c = *cp;
  6751. #ifdef FUNCTIONTEST /* setatm() */
  6752. if (fnstate == 0 && c == '\\') {
  6753. fnstate = 1;
  6754. if (fndebug) printf("s%d%c/",fnstate,c);
  6755. } else if (fnstate == 1) {
  6756. fnstate = (c == 'f' || c == 'F') ? 2 : 0;
  6757. if (fndebug) printf("s%d%c/",fnstate,c);
  6758. } else if (fnstate == 2 && isalpha(c)) {
  6759. fnstate = 3;
  6760. if (fndebug) printf("s%d%c/",fnstate,c);
  6761. } else if (fnstate == 3 && isalpha(c)) {
  6762. fnstate = 4;
  6763. if (fndebug) printf("s%d%c/",fnstate,c);
  6764. } else if (fnstate == 4 && c == '(') {
  6765. fnstate = 5;
  6766. fnparens++;
  6767. if (fndebug) printf("s%d%c/",fnstate,c);
  6768. } else if (fnstate == 5 && c == ')') {
  6769. fnparens--;
  6770. if (fnparens == 0) {
  6771. fnstate = 0;
  6772. }
  6773. if (fndebug) printf("s%d%c/",fnstate,c);
  6774. }
  6775. #endif /* FUNCTIONTEST */
  6776. if (*cp == lbrace)
  6777. bracelvl++;
  6778. else if (*cp == rbrace)
  6779. bracelvl--;
  6780. if (bracelvl < 0)
  6781. bracelvl = 0;
  6782. if (bracelvl == 0) {
  6783. if (dq) {
  6784. if (*cp == SP || *cp == HT) {
  6785. if (cp > dqp+1) {
  6786. if (*(cp-1) == '"' && *(cp-2) != CMDQ) {
  6787. break;
  6788. }
  6789. }
  6790. }
  6791. } else if ((*cp == SP || *cp == HT) && fcode != 1 && fcode != 3) {
  6792. #ifdef FUNCTIONTEST
  6793. if (fnstate == 0)
  6794. #endif /* FUNCTIONTEST */
  6795. break;
  6796. }
  6797. if ((fcode == 2) && (*cp == '=' || *cp == ':')) break;
  6798. if ((fcode != 3) && (*cp == LF || *cp == CR)) break;
  6799. }
  6800. *ap++ = *cp++;
  6801. cc++;
  6802. }
  6803. *ap = NUL; /* Terminate the string. */
  6804. #ifdef FUNCTIONTEST
  6805. /* printf("ATMBUF=[%s]\n", atmbuf); */
  6806. #endif /* FUNCTIONTEST */
  6807. /* debug(F111,"setatm result",atmbuf,cc); */
  6808. return(cc); /* Return length. */
  6809. }
  6810. /*
  6811. These functions attempt to hide system dependencies from the mainline
  6812. code in gtword(). Dummy arg for cmdgetc() needed for compatibility with
  6813. coninc(), ttinc(), etc, since a pointer to this routine can be passed in
  6814. place of those to tn_doop().
  6815. No longer static. Used by askmore(). Fri Aug 20 15:03:34 1999.
  6816. */
  6817. #define CMD_CONINC /* How we get keyboard chars */
  6818. int
  6819. cmdgetc(timelimit) int timelimit; { /* Get a character from the tty. */
  6820. int c;
  6821. #ifdef IKSD
  6822. extern int inserver;
  6823. #endif /* IKSD */
  6824. #ifdef CK_LOGIN
  6825. extern int x_logged;
  6826. #endif /* CK_LOGIN */
  6827. #ifdef TNCODE
  6828. static int got_cr = 0;
  6829. extern int ckxech;
  6830. int tx = 0, is_tn = 0;
  6831. #endif /* TNCODE */
  6832. if (pushc
  6833. #ifndef NOSPL
  6834. && !askflag
  6835. #endif /* NOSPL */
  6836. ) {
  6837. debug(F111,"cmdgetc()","pushc",pushc);
  6838. c = pushc;
  6839. pushc = NUL;
  6840. if (xcmfdb && c == '?') /* Don't echo ? twice if chaining. */
  6841. cmdchardel();
  6842. return(c);
  6843. }
  6844. #ifdef datageneral
  6845. {
  6846. char ch;
  6847. c = dgncinb(0,&ch,1); /* -1 is EOF, -2 TO,
  6848. * -c is AOS/VS error */
  6849. if (c == -2) { /* timeout was enabled? */
  6850. resto(channel(0)); /* reset timeouts */
  6851. c = dgncinb(0,&ch,1); /* retry this now! */
  6852. }
  6853. if (c < 0) return(-4); /* EOF or some error */
  6854. else c = (int) ch & 0177; /* Get char without parity */
  6855. /* echof = 1; */
  6856. }
  6857. #else /* Not datageneral */
  6858. #ifndef MINIX2
  6859. if (
  6860. #ifdef IKSD
  6861. (!local && inserver) ||
  6862. #endif /* IKSD */
  6863. timelimit > 0) {
  6864. #ifdef TNCODE
  6865. GETNEXTCH:
  6866. is_tn = !pushc && !local && sstelnet;
  6867. #endif /* TNCODE */
  6868. #ifdef COMMENT
  6869. c = coninc(timelimit > 0 ? 1 : 0);
  6870. #else /* COMMENT */
  6871. /* This is likely to break the asktimeout... */
  6872. c = coninc(timelimit);
  6873. #endif /* COMMENT */
  6874. /* debug(F101,"cmdgetc coninc","",c); */
  6875. #ifdef TNCODE
  6876. if (c >= 0 && is_tn) { /* Server-side Telnet */
  6877. switch (c) {
  6878. case IAC:
  6879. /* debug(F111,"gtword IAC","c",c); */
  6880. got_cr = 0;
  6881. if ((tx = tn_doop((CHAR)(c & 0xff),ckxech,coninc)) == 0) {
  6882. goto GETNEXTCH;
  6883. } else if (tx <= -1) { /* I/O error */
  6884. /* If there was a fatal I/O error then ttclos() */
  6885. /* has been called and the next GETNEXTCH attempt */
  6886. /* will be !is_tn since ttclos() sets sstelnet = 0 */
  6887. doexit(BAD_EXIT,-1); /* (or return(-4)? */
  6888. } else if (tx == 1) { /* ECHO change */
  6889. ckxech = dpx = 1; /* Get next char */
  6890. goto GETNEXTCH;
  6891. } else if (tx == 2) { /* ECHO change */
  6892. ckxech = dpx = 0; /* Get next char */
  6893. goto GETNEXTCH;
  6894. } else if (tx == 3) { /* Quoted IAC */
  6895. c = 255; /* proceeed with it. */
  6896. }
  6897. #ifdef IKS_OPTION
  6898. else if (tx == 4) { /* IKS State Change */
  6899. goto GETNEXTCH;
  6900. }
  6901. #endif /* IKS_OPTION */
  6902. else if (tx == 6) { /* Remote Logout */
  6903. doexit(GOOD_EXIT,0);
  6904. } else {
  6905. goto GETNEXTCH; /* Unknown, get next char */
  6906. }
  6907. break;
  6908. #ifdef COMMENT
  6909. case CR:
  6910. if (!TELOPT_U(TELOPT_BINARY)) {
  6911. if (got_cr) {
  6912. /* This means the sender is violating Telnet */
  6913. /* protocol because we received two CRs in a */
  6914. /* row without getting either LF or NUL. */
  6915. /* This will not solve the problem but it */
  6916. /* will at least allow two CRs to do something */
  6917. /* whereas before the user would have to guess */
  6918. /* to send LF or NUL after the CR. */
  6919. debug(F100,"gtword CR telnet error","",0);
  6920. c = LF;
  6921. } else {
  6922. debug(F100,"gtword skipping CR","",0);
  6923. got_cr = 1; /* Remember a CR was received */
  6924. goto GETNEXTCH;
  6925. }
  6926. } else {
  6927. debug(F100,"gtword CR to LF","",0);
  6928. c = LF;
  6929. }
  6930. break;
  6931. case LF:
  6932. if (!TELOPT_U(TELOPT_BINARY)) {
  6933. got_cr = 0;
  6934. debug(F100,"gtword LF","",0);
  6935. } else {
  6936. if (got_cr) {
  6937. got_cr = 0;
  6938. debug(F100,"gtword skipping LF","",0);
  6939. goto GETNEXTCH;
  6940. }
  6941. }
  6942. break;
  6943. case NUL:
  6944. if (!TELOPT_U(TELOPT_BINARY) && got_cr) {
  6945. c = LF;
  6946. debug(F100,"gtword NUL to LF","",0);
  6947. } else {
  6948. debug(F100,"gtword NUL","",0);
  6949. }
  6950. got_cr = 0;
  6951. break;
  6952. #else /* COMMENT */
  6953. case CR:
  6954. if ( !TELOPT_U(TELOPT_BINARY) && got_cr ) {
  6955. /* This means the sender is violating Telnet */
  6956. /* protocol because we received two CRs in a */
  6957. /* row without getting either LF or NUL. */
  6958. /* This will not solve the problem but it */
  6959. /* will at least allow two CRs to do something */
  6960. /* whereas before the user would have to guess */
  6961. /* to send LF or NUL after the CR. */
  6962. debug(F100,"gtword CR telnet error","",0);
  6963. } else {
  6964. got_cr = 1; /* Remember a CR was received */
  6965. }
  6966. /* debug(F100,"gtword CR to LF","",0); */
  6967. c = LF;
  6968. break;
  6969. case LF:
  6970. if (got_cr) {
  6971. got_cr = 0;
  6972. /* debug(F100,"gtword skipping LF","",0); */
  6973. goto GETNEXTCH;
  6974. }
  6975. break;
  6976. case NUL:
  6977. if (got_cr) {
  6978. got_cr = 0;
  6979. /* debug(F100,"gtword skipping NUL","",0); */
  6980. goto GETNEXTCH;
  6981. #ifdef COMMENT
  6982. } else {
  6983. debug(F100,"gtword NUL","",0);
  6984. #endif /* COMMENT */
  6985. }
  6986. break;
  6987. #endif /* COMMENT */
  6988. #ifdef IKSD
  6989. case ETX: /* Ctrl-C... */
  6990. case EOT: /* EOT = EOF */
  6991. if (inserver
  6992. #ifdef CK_LOGIN
  6993. && !x_logged
  6994. #endif /* CK_LOGIN */
  6995. )
  6996. return(-4);
  6997. break;
  6998. #endif /* IKSD */
  6999. default:
  7000. got_cr = 0;
  7001. }
  7002. }
  7003. #endif /* TNCODE */
  7004. } else {
  7005. #ifdef OS2
  7006. c = coninc(0);
  7007. #else /* OS2 */
  7008. #ifdef CMD_CONINC
  7009. #undef CMD_CONINC
  7010. #endif /* CMD_CONINC */
  7011. c = getchar();
  7012. #endif /* OS2 */
  7013. }
  7014. #else /* MINIX2 */
  7015. #undef getc
  7016. #ifdef CMD_CONINC
  7017. #undef CMD_CONINC
  7018. #endif /* CMD_CONINC */
  7019. c = getc(stdin);
  7020. /* debug(F101,"cmdgetc getc","",c); */
  7021. #endif /* MINIX2 */
  7022. #ifdef RTU
  7023. if (rtu_bug) {
  7024. #ifdef CMD_CONINC
  7025. #undef CMD_CONINC
  7026. #endif /* CMD_CONINC */
  7027. c = getchar(); /* RTU doesn't discard the ^Z */
  7028. rtu_bug = 0;
  7029. }
  7030. #endif /* RTU */
  7031. #endif /* datageneral */
  7032. return(c); /* Return what we got */
  7033. }
  7034. /* #ifdef USE_ARROWKEYS */
  7035. /* Mechanism to use for peeking into stdin buffer */
  7036. #ifndef USE_FILE_CNT /* stdin->__cnt */
  7037. #ifndef USE_FILE__CNT /* Note: two underscores */
  7038. #ifdef HPUX /* HPUX 7-11 */
  7039. #ifndef HPUX5
  7040. #ifndef HPUX6
  7041. #define USE_FILE__CNT
  7042. #endif /* HPUX6 */
  7043. #endif /* HPUX5 */
  7044. #else
  7045. #ifdef ANYSCO /* SCO UNIX, OSR5, Unixware, etc */
  7046. #ifndef OLD_UNIXWARE /* But not Unixware 1.x or 2.0 */
  7047. #ifndef UNIXWARE2 /* or 2.1.0 */
  7048. #define USE_FILE__CNT
  7049. #endif /* UNIXWARE2 */
  7050. #endif /* OLD_UNIXWARE */
  7051. #endif /* ANYSCO */
  7052. #endif /* HPUX */
  7053. #endif /* USE_FILE__CNT */
  7054. #endif /* USE_FILE_CNT */
  7055. #ifndef USE_FILE_R /* stdin->_r */
  7056. #ifndef USE_FILE_CNT
  7057. #ifndef USE_FILE__CNT
  7058. #ifdef BSD44 /* {Free,Open,Net}BSD, BSDI */
  7059. #define USE_FILE_R
  7060. #endif /* BSD44 */
  7061. #endif /* USE_FILE__CNT */
  7062. #endif /* USE_FILE_CNT */
  7063. #endif /* USE_FILE_R */
  7064. #ifndef USE_FILE_R /* stdin->_cnt */
  7065. #ifndef USE_FILE_CNT
  7066. #ifndef USE_FILE__CNT
  7067. /* #define USE_FILE_CNT */ /* Everybody else (but Linux) */
  7068. #endif /* USE_FILE__CNT */
  7069. #endif /* USE_FILE_CNT */
  7070. #endif /* USE_FILE_R */
  7071. /*
  7072. c m d c o n c h k
  7073. How many characters are waiting to be read at the console? Normally
  7074. conchk() would tell us, but in Unix and VMS cmdgetc() uses stdio getchar(),
  7075. thus bypassing coninc()/conchk(), so we have to peek into the stdin buffer,
  7076. which is totally nonportable. Which is why this routine is, at least for
  7077. now, used only for checking for arrow-key sequences from the keyboard after
  7078. an ESC was read. Wouldn't it be nice if the stdio package had a function
  7079. that returned the number of bytes waiting to be read from its buffer?
  7080. Returns 0 or greater always.
  7081. */
  7082. int
  7083. cmdconchk() {
  7084. int x = 0, y;
  7085. y = pushc ? 1 : 0; /* Have command character pushed? */
  7086. #ifdef OS2
  7087. x = conchk(); /* Check device-driver buffer */
  7088. if (x < 0) x = 0;
  7089. #else /* OS2 */
  7090. #ifdef CMD_CONINC /* See cmdgetc() */
  7091. x = conchk(); /* Check device-driver buffer */
  7092. if (x < 0) x = 0;
  7093. #else /* CMD_CONINC */
  7094. /* Here we must look inside the stdin buffer - highly platform dependent */
  7095. #ifdef __FILE_defined /* glibc 2.28 1 Aug 2018 */
  7096. x = (int) ((stdin->_IO_read_end) - (stdin->_IO_read_ptr));
  7097. debug(F101,"cmdconchk __FILE_defined","",x);
  7098. #else /* __FILE_defined */
  7099. #ifdef _IO_file_flags /* Linux (glibc 2.28 removed this symbol */
  7100. x = (int) ((stdin->_IO_read_end) - (stdin->_IO_read_ptr));
  7101. debug(F101,"cmdconchk _IO_file_flags","",x);
  7102. #else /* _IO_file_flags */
  7103. #ifdef USE_FILE_CNT /* Traditional */
  7104. #ifdef VMS
  7105. debug(F101,"cmdconchk (*stdin)->_cnt","",(*stdin)->_cnt);
  7106. x = (*stdin)->_cnt;
  7107. #else
  7108. #ifdef NOARROWKEYS
  7109. debug(F101,"cmdconchk NOARROWKEYS x","",0);
  7110. #else
  7111. debug(F101,"cmdconchk stdin->_cnt","",stdin->_cnt);
  7112. x = stdin->_cnt;
  7113. #endif /* NOARROWKEYS */
  7114. #endif /* VMS */
  7115. if (x == 0) x = conchk();
  7116. if (x < 0) x = 0;
  7117. #else /* USE_FILE_CNT */
  7118. #ifdef USE_FILE__CNT /* HP-UX */
  7119. debug(F101,"cmdconchk stdin->__cnt","",stdin->__cnt);
  7120. x = stdin->__cnt;
  7121. if (x == 0) x = conchk();
  7122. if (x < 0) x = 0;
  7123. #else /* USE_FILE_CNT */
  7124. #ifdef USE_FILE_R /* FreeBSD, OpenBSD, etc */
  7125. debug(F101,"cmdconchk stdin->_r","",stdin->_r);
  7126. x = stdin->_r;
  7127. if (x == 0) x = conchk();
  7128. if (x < 0) x = 0;
  7129. /* Fill in any others here... */
  7130. #endif /* USE_FILE_R */
  7131. #endif /* USE_FILE__CNT */
  7132. #endif /* USE_FILE_CNT */
  7133. #endif /* _IO_file_flags */
  7134. #endif /* __FILE_defined */
  7135. #endif /* CMD_CONINC */
  7136. #endif /* OS2 */
  7137. return(x + y);
  7138. }
  7139. /* #endif */ /* USE_ARROWKEYS */
  7140. static VOID
  7141. cmdclrscn() { /* Clear the screen */
  7142. ck_cls();
  7143. }
  7144. static VOID /* What to echo at end of command */
  7145. #ifdef CK_ANSIC
  7146. cmdnewl(char c)
  7147. #else
  7148. cmdnewl(c) char c;
  7149. #endif /* CK_ANSIC */
  7150. /* cmdnewl */ {
  7151. #ifdef OS2
  7152. #ifdef IKSD
  7153. extern int inserver;
  7154. if (inserver && c == LF)
  7155. putchar(CR);
  7156. #endif /* IKSD */
  7157. #endif /* OS2 */
  7158. putchar(c); /* c is the terminating character */
  7159. #ifdef WINTCP /* what is this doing here? */
  7160. if (c == CR) putchar(NL);
  7161. #endif /* WINTCP */
  7162. /*
  7163. A.A. Chernov, who sent in changes for FreeBSD, said we also needed this
  7164. for SVORPOSIX because "setup terminal by termios and curses does
  7165. not convert \r to \n, so additional \n needed in newline function." But
  7166. it is also very likely to result in unwanted blank lines.
  7167. */
  7168. #ifdef BSD44
  7169. if (c == CR) putchar(NL);
  7170. #endif /* BSD44 */
  7171. #ifdef COMMENT
  7172. /* OS2 no longer needs this as all CR are converted to NL in coninc() */
  7173. /* This eliminates the ugly extra blank lines discussed above. */
  7174. #ifdef OS2
  7175. if (c == CR) putchar(NL);
  7176. #endif /* OS2 */
  7177. #endif /* COMMENT */
  7178. #ifdef aegis
  7179. if (c == CR) putchar(NL);
  7180. #endif /* aegis */
  7181. #ifdef AMIGA
  7182. if (c == CR) putchar(NL);
  7183. #endif /* AMIGA */
  7184. #ifdef datageneral
  7185. if (c == CR) putchar(NL);
  7186. #endif /* datageneral */
  7187. #ifdef GEMDOS
  7188. if (c == CR) putchar(NL);
  7189. #endif /* GEMDOS */
  7190. #ifdef STRATUS
  7191. if (c == CR) putchar(NL);
  7192. #endif /* STRATUS */
  7193. }
  7194. static VOID
  7195. cmdchardel() { /* Erase a character from the screen */
  7196. #ifndef NOSPL
  7197. if (!echostars)
  7198. #endif /* NOSPL */
  7199. if (!dpx) return;
  7200. #ifdef datageneral
  7201. /* DG '\b' is EM (^y or \031) */
  7202. if (termtype == 1)
  7203. /* Erase a character from non-DG screen, */
  7204. dgncoub(1,"\010 \010",3);
  7205. else
  7206. #endif /* datageneral */
  7207. printf("\b \b");
  7208. #ifdef GEMDOS
  7209. fflush(stdout);
  7210. #else
  7211. #ifdef BEBOX
  7212. fflush(stdout);
  7213. #endif /* BEBOX */
  7214. #endif /* GEMDOS */
  7215. }
  7216. static VOID
  7217. #ifdef CK_ANSIC
  7218. cmdecho(char c, int quote)
  7219. #else
  7220. cmdecho(c,quote) char c; int quote;
  7221. #endif /* CK_ANSIC */
  7222. { /* cmdecho */
  7223. #ifdef NOSPL
  7224. if (!dpx) return;
  7225. #else
  7226. if (!echostars) {
  7227. if (!dpx) return;
  7228. } else {
  7229. c = (char)echostars;
  7230. }
  7231. #endif /* NOSPL */
  7232. /* Echo tty input character c */
  7233. if (quote) {
  7234. putchar(BS);
  7235. putchar(SP);
  7236. putchar(BS);
  7237. #ifdef isprint
  7238. putchar((CHAR) (isprint(c) ? c : '^' ));
  7239. #else
  7240. putchar((CHAR) ((c >= SP && c < DEL) ? c : '^'));
  7241. #endif /* isprint */
  7242. } else {
  7243. putchar(c);
  7244. }
  7245. #ifdef OS2
  7246. if (quote==1 && c==CR) putchar((CHAR) NL);
  7247. #endif /* OS2 */
  7248. if (timelimit)
  7249. fflush(stdout);
  7250. }
  7251. /* Return pointer to current position in command buffer. */
  7252. char *
  7253. cmpeek() {
  7254. return(np);
  7255. }
  7256. #endif /* NOICP */
  7257. #ifdef NOICP
  7258. #include "ckcdeb.h"
  7259. #include "ckucmd.h"
  7260. #include "ckcasc.h"
  7261. #endif /* NOICP */
  7262. /* X X E S C -- Interprets backslash codes */
  7263. /* Returns the int value of the backslash code if it is > -1 and < 256 */
  7264. /* and updates the string pointer to first character after backslash code. */
  7265. /* If the argument is invalid, leaves pointer unchanged and returns -1. */
  7266. int
  7267. xxesc(s) char **s; { /* Expand backslash escapes */
  7268. int x, y, brace, radix; /* Returns the int value */
  7269. char hd = '9'; /* Highest digit in radix */
  7270. char *p;
  7271. p = *s; /* pointer to beginning */
  7272. if (!p) return(-1); /* watch out for null pointer */
  7273. x = *p++; /* character at beginning */
  7274. if (x != CMDQ) return(-1); /* make sure it's a backslash code */
  7275. x = *p; /* it is, get the next character */
  7276. if (x == '{') { /* bracketed quantity? */
  7277. p++; /* begin past bracket */
  7278. x = *p;
  7279. brace = 1;
  7280. } else brace = 0;
  7281. switch (x) { /* Start interpreting */
  7282. case 'd': /* Decimal radix indicator */
  7283. case 'D':
  7284. p++; /* Just point past it and fall thru */
  7285. case '0': /* Starts with digit */
  7286. case '1':
  7287. case '2': case '3': case '4': case '5':
  7288. case '6': case '7': case '8': case '9':
  7289. radix = 10; /* Decimal */
  7290. hd = '9'; /* highest valid digit */
  7291. break;
  7292. case 'o': /* Starts with o or O */
  7293. case 'O':
  7294. radix = 8; /* Octal */
  7295. hd = '7'; /* highest valid digit */
  7296. p++; /* point past radix indicator */
  7297. break;
  7298. case 'x': /* Starts with x or X */
  7299. case 'X':
  7300. radix = 16; /* Hexadecimal */
  7301. p++; /* point past radix indicator */
  7302. break;
  7303. default: /* All others */
  7304. #ifdef COMMENT
  7305. *s = p+1; /* Treat as quote of next char */
  7306. return(*p);
  7307. #else
  7308. return(-1);
  7309. #endif /* COMMENT */
  7310. }
  7311. /* For OS/2, there are "wide" characters required for the keyboard
  7312. * binding, i.e \644 and similar codes larger than 255 (byte).
  7313. * For this purpose, give up checking for < 256. If someone means
  7314. * \266 should result in \26 followed by a "6" character, he should
  7315. * always write \{26}6 anyway. Now, return only the lower byte of
  7316. * the result, i.e. 10, but eat up the whole \266 sequence and
  7317. * put the wide result 266 into a global variable. Yes, that's not
  7318. * the most beautiful programming style but requires the least
  7319. * amount of changes to other routines.
  7320. */
  7321. if (*p == '{') { /* Sun May 11 20:00:40 2003 */
  7322. brace = 1; /* Allow {} after radix indicator */
  7323. p++;
  7324. }
  7325. if (radix <= 10) { /* Number in radix 8 or 10 */
  7326. for ( x = y = 0;
  7327. (*p) && (*p >= '0') && (*p <= hd)
  7328. #ifdef OS2
  7329. && (y < 5) && (x*radix < KMSIZE);
  7330. /* the maximum needed value \8196 is 4 digits long */
  7331. /* while as octal it requires \1377, i.e. 5 digits */
  7332. #else
  7333. && (y < 3) && (x*radix < 256);
  7334. #endif /* OS2 */
  7335. p++,y++) {
  7336. x = x * radix + (int) *p - 48;
  7337. }
  7338. #ifdef OS2
  7339. wideresult = x; /* Remember wide result */
  7340. x &= 255;
  7341. #endif /* OS2 */
  7342. if (y == 0 || x > 255) { /* No valid digits? */
  7343. *s = p; /* point after it */
  7344. return(-1); /* return failure. */
  7345. }
  7346. } else if (radix == 16) { /* Special case for hex */
  7347. if ((x = unhex(*p++)) < 0) { *s = p - 1; return(-1); }
  7348. if ((y = unhex(*p++)) < 0) { *s = p - 2; return(-1); }
  7349. x = ((x << 4) & 0xF0) | (y & 0x0F);
  7350. #ifdef OS2
  7351. wideresult = x;
  7352. if ((y = unhex(*p)) >= 0) {
  7353. p++;
  7354. wideresult = ((x << 4) & 0xFF0) | (y & 0x0F);
  7355. x = wideresult & 255;
  7356. }
  7357. #endif /* OS2 */
  7358. } else x = -1;
  7359. if (brace && *p == '}' && x > -1) /* Point past closing brace, if any */
  7360. p++;
  7361. *s = p; /* Point to next char after sequence */
  7362. return(x); /* Return value of sequence */
  7363. }
  7364. int /* Convert hex string to int */
  7365. #ifdef CK_ANSIC
  7366. unhex(char x)
  7367. #else
  7368. unhex(x) char x;
  7369. #endif /* CK_ANSIC */
  7370. /* unhex */ {
  7371. if (x >= '0' && x <= '9') /* 0-9 is offset by hex 30 */
  7372. return(x - 0x30);
  7373. else if (x >= 'A' && x <= 'F') /* A-F offset by hex 37 */
  7374. return(x - 0x37);
  7375. else if (x >= 'a' && x <= 'f') /* a-f offset by hex 57 */
  7376. return(x - 0x57); /* (obviously ASCII dependent) */
  7377. else return(-1);
  7378. }
  7379. /* L O O K U P -- Lookup the string in the given array of strings */
  7380. /*
  7381. Call this way: v = lookup(table,word,n,&x);
  7382. table - a 'struct keytab' table.
  7383. word - the target string to look up in the table.
  7384. n - the number of elements in the table.
  7385. x - address of an integer for returning the table array index,
  7386. or NULL if you don't need a table index.
  7387. The keyword table must be arranged in ascending alphabetical order;
  7388. alphabetic case doesn't matter but letters are treated as lowercase
  7389. for purposes of ordering; thus "^" and "_" come *before* the letters,
  7390. not after them.
  7391. Returns the keyword's associated value (zero or greater) if found,
  7392. with the variable x set to the keyword-table index. If is lookup()
  7393. is not successful, it returns:
  7394. -3 if nothing to look up (target was null),
  7395. -2 if ambiguous,
  7396. -1 if not found.
  7397. A match is successful if the target matches a keyword exactly, or if
  7398. the target is a prefix of exactly one keyword. It is ambiguous if the
  7399. target matches two or more keywords from the table.
  7400. Lookup() is the critical routine in scripts and so is optimized with a
  7401. simple static cache plus some other tricks. Maybe it could be improved
  7402. further with binary search or hash techniques but I doubt it since most
  7403. keyword tables are fairly short.
  7404. */
  7405. #ifdef USE_LUCACHE /* Lookup cache */
  7406. extern int lusize; /* (initialized in ckuus5.c) */
  7407. extern char * lucmd[];
  7408. extern int luval[];
  7409. extern int luidx[];
  7410. extern struct keytab * lutab[];
  7411. long luhits = 0L;
  7412. long lucalls = 0L;
  7413. long xxhits = 0L;
  7414. long luloop = 0L;
  7415. #endif /* USE_LUCACHE */
  7416. int
  7417. lookup(table,cmd,n,x) char *cmd; struct keytab table[]; int n, *x; {
  7418. register int i, m;
  7419. int v, len, cmdlen = 0;
  7420. char c = NUL, c1, *s;
  7421. /* Get 1st char of search object, if it's null return -3. */
  7422. if (!cmd || n < 1) /* Defense de nullarg */
  7423. return(-3);
  7424. c1 = *cmd; /* First character */
  7425. if (!c1) /* Make sure there is one */
  7426. return(-3);
  7427. if (isupper(c1)) /* If letter make it lowercase */
  7428. c1 = tolower(c1);
  7429. #ifdef USE_LUCACHE /* lookup() cache */
  7430. m = lusize;
  7431. lucalls++; /* Count this lookup() call */
  7432. for (i = 0; i < m; i++) { /* Loop thru cache */
  7433. if (*(lucmd[i]) == c1) { /* Same as 1st char of search item? */
  7434. if (lutab[i] == table) { /* Yes - same table too? */
  7435. if (!strcmp(cmd,lucmd[i])) { /* Yes - compare */
  7436. if (x) *x = luidx[i]; /* Match - return index */
  7437. luhits++; /* Count cache hit */
  7438. return(luval[i]); /* Return associated value */
  7439. }
  7440. }
  7441. }
  7442. }
  7443. #endif /* USE_LUCACHE */
  7444. /* Not null, not in cache, look it up */
  7445. s = cmd;
  7446. while (*s++) cmdlen++; /* Length of target */
  7447. /*
  7448. Quick binary search to find last table entry whose first character is
  7449. lexically less than the first character of the search object. This is
  7450. the starting point of the next loop, which must go in sequence since it
  7451. compares adjacent table entries.
  7452. */
  7453. if (n < 5) { /* Not worth it for small tables */
  7454. i = 0;
  7455. } else {
  7456. int lo = 0;
  7457. int hi = n;
  7458. int count = 0;
  7459. while (lo+2 < hi && ++count < 12) {
  7460. i = lo + ((hi - lo) / 2);
  7461. c = *(table[i].kwd);
  7462. if (isupper(c)) c = tolower(c);
  7463. if (c < c1) {
  7464. lo = i;
  7465. } else {
  7466. hi = i;
  7467. }
  7468. }
  7469. i = (c < c1) ? lo+1 : lo;
  7470. #ifdef USE_LUCACHE
  7471. if (i > 0) xxhits++;
  7472. #endif /* USE_LUCACHE */
  7473. }
  7474. for ( ; i < n-1; i++) {
  7475. #ifdef USE_LUCACHE
  7476. luloop++;
  7477. #endif /* USE_LUCACHE */
  7478. v = 0;
  7479. c = *(table[i].kwd);
  7480. if (c) {
  7481. if (isupper(c)) c = tolower(c);
  7482. /* The following is a big performance booster but makes it */
  7483. /* absolutely essential that all lookup() tables are in order. */
  7484. if (c > c1) /* Leave early if past our mark */
  7485. return(-1);
  7486. #ifdef DEBUG
  7487. /* Use LOG DEBUG to check */
  7488. if (deblog) {
  7489. if (ckstrcmp(table[i].kwd,table[i+1].kwd,0,0) > 0) {
  7490. printf("TABLE OUT OF ORDER [%s] [%s]\n",
  7491. table[i].kwd,table[i+1].kwd);
  7492. }
  7493. }
  7494. #endif /* DEBUG */
  7495. if (c == c1) {
  7496. len = 0;
  7497. s = table[i].kwd;
  7498. while (*s++) len++;
  7499. if ((len == cmdlen && !ckstrcmp(table[i].kwd,cmd,len,0)) ||
  7500. ((v = !ckstrcmp(table[i].kwd,cmd,cmdlen,0)) &&
  7501. ckstrcmp(table[i+1].kwd,cmd,cmdlen,0))) {
  7502. if (x) *x = i;
  7503. return(table[i].kwval);
  7504. }
  7505. } else v = 0;
  7506. }
  7507. if (v) { /* Ambiguous */
  7508. if (x) *x = i; /* Set index of first match */
  7509. return(-2);
  7510. }
  7511. }
  7512. /* Last (or only) element */
  7513. if (!ckstrcmp(table[n-1].kwd,cmd,cmdlen,0)) {
  7514. if (x) *x = n-1;
  7515. /* debug(F111,"lookup",table[i].kwd,table); */
  7516. return(table[n-1].kwval);
  7517. } else return(-1);
  7518. }
  7519. /*
  7520. x l o o k u p
  7521. Like lookup, but requires a full (but case-independent) match
  7522. and does NOT require the table to be in order.
  7523. */
  7524. int
  7525. xlookup(table,cmd,n,x) struct keytab table[]; char *cmd; int n, *x; {
  7526. register int i;
  7527. int len, cmdlen, one = 0;
  7528. register char c, c2, * s, * s2;
  7529. if (!cmd) cmd = ""; /* Check args */
  7530. if (!*cmd || n < 1) return(-3);
  7531. c = *cmd; /* First char of string to look up */
  7532. if (!*(cmd+1)) { /* Special handling for 1-char names */
  7533. cmdlen = 1;
  7534. if (isupper(c))
  7535. c = tolower(c);
  7536. one = 1;
  7537. } else {
  7538. cmdlen = 0;
  7539. s = cmd;
  7540. while (*s++) cmdlen++;
  7541. c = *cmd;
  7542. if (isupper(c))
  7543. c = tolower(c);
  7544. }
  7545. if (cmdlen < 1)
  7546. return(-3);
  7547. for (i = 0; i < n; i++) {
  7548. s = table[i].kwd; /* This entry */
  7549. if (!s) s = "";
  7550. if (!*s) continue; /* Empty table entry */
  7551. c2 = *s;
  7552. if (isupper(c2)) c2 = tolower(c2);
  7553. if (c != c2) continue; /* First char doesn't match */
  7554. if (one) { /* Name is one char long */
  7555. if (!*(s+1)) {
  7556. if (x) *x = i;
  7557. *cmd = c;
  7558. return(table[i].kwval); /* So is table entry */
  7559. }
  7560. } else { /* Otherwise do string comparison */
  7561. s2 = s;
  7562. len = 0;
  7563. while (*s2++) len++;
  7564. if (len == cmdlen && !ckstrcmp(s,cmd,-1,0)) {
  7565. if (x) *x = i;
  7566. return(table[i].kwval);
  7567. }
  7568. }
  7569. }
  7570. return(-1);
  7571. }
  7572. /* Reverse lookup */
  7573. char *
  7574. rlookup(table,n,x) struct keytab table[]; int n, x; {
  7575. int i;
  7576. for (i = 0; i < n; i++) {
  7577. if (table[i].kwval == x)
  7578. return(table[i].kwd);
  7579. }
  7580. return(NULL);
  7581. }
  7582. #ifndef NOICP
  7583. int
  7584. cmdsquo(x) int x; {
  7585. quoting = x;
  7586. return(1);
  7587. }
  7588. int
  7589. cmdgquo() {
  7590. return(quoting);
  7591. }
  7592. #endif /* NOICP */