vi.c 107 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * tiny vi.c: A small 'vi' clone
  4. * Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com>
  5. *
  6. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  7. */
  8. /*
  9. * Things To Do:
  10. * EXINIT
  11. * $HOME/.exrc and ./.exrc
  12. * add magic to search /foo.*bar
  13. * add :help command
  14. * :map macros
  15. * if mark[] values were line numbers rather than pointers
  16. * it would be easier to change the mark when add/delete lines
  17. * More intelligence in refresh()
  18. * ":r !cmd" and "!cmd" to filter text through an external command
  19. * A true "undo" facility
  20. * An "ex" line oriented mode- maybe using "cmdedit"
  21. */
  22. #include "libbb.h"
  23. #define ENABLE_FEATURE_VI_CRASHME 0
  24. #if ENABLE_LOCALE_SUPPORT
  25. #define Isprint(c) isprint((c))
  26. #else
  27. /* 0x9b is Meta-ESC */
  28. #define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
  29. #endif
  30. enum {
  31. MAX_LINELEN = CONFIG_FEATURE_VI_MAX_LEN,
  32. MAX_SCR_COLS = CONFIG_FEATURE_VI_MAX_LEN,
  33. };
  34. // Misc. non-Ascii keys that report an escape sequence
  35. #define VI_K_UP (char)128 // cursor key Up
  36. #define VI_K_DOWN (char)129 // cursor key Down
  37. #define VI_K_RIGHT (char)130 // Cursor Key Right
  38. #define VI_K_LEFT (char)131 // cursor key Left
  39. #define VI_K_HOME (char)132 // Cursor Key Home
  40. #define VI_K_END (char)133 // Cursor Key End
  41. #define VI_K_INSERT (char)134 // Cursor Key Insert
  42. #define VI_K_PAGEUP (char)135 // Cursor Key Page Up
  43. #define VI_K_PAGEDOWN (char)136 // Cursor Key Page Down
  44. #define VI_K_FUN1 (char)137 // Function Key F1
  45. #define VI_K_FUN2 (char)138 // Function Key F2
  46. #define VI_K_FUN3 (char)139 // Function Key F3
  47. #define VI_K_FUN4 (char)140 // Function Key F4
  48. #define VI_K_FUN5 (char)141 // Function Key F5
  49. #define VI_K_FUN6 (char)142 // Function Key F6
  50. #define VI_K_FUN7 (char)143 // Function Key F7
  51. #define VI_K_FUN8 (char)144 // Function Key F8
  52. #define VI_K_FUN9 (char)145 // Function Key F9
  53. #define VI_K_FUN10 (char)146 // Function Key F10
  54. #define VI_K_FUN11 (char)147 // Function Key F11
  55. #define VI_K_FUN12 (char)148 // Function Key F12
  56. /* vt102 typical ESC sequence */
  57. /* terminal standout start/normal ESC sequence */
  58. static const char SOs[] ALIGN1 = "\033[7m";
  59. static const char SOn[] ALIGN1 = "\033[0m";
  60. /* terminal bell sequence */
  61. static const char bell[] ALIGN1 = "\007";
  62. /* Clear-end-of-line and Clear-end-of-screen ESC sequence */
  63. static const char Ceol[] ALIGN1 = "\033[0K";
  64. static const char Ceos[] ALIGN1 = "\033[0J";
  65. /* Cursor motion arbitrary destination ESC sequence */
  66. static const char CMrc[] ALIGN1 = "\033[%d;%dH";
  67. /* Cursor motion up and down ESC sequence */
  68. static const char CMup[] ALIGN1 = "\033[A";
  69. static const char CMdown[] ALIGN1 = "\n";
  70. enum {
  71. YANKONLY = FALSE,
  72. YANKDEL = TRUE,
  73. FORWARD = 1, // code depends on "1" for array index
  74. BACK = -1, // code depends on "-1" for array index
  75. LIMITED = 0, // how much of text[] in char_search
  76. FULL = 1, // how much of text[] in char_search
  77. S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
  78. S_TO_WS = 2, // used in skip_thing() for moving "dot"
  79. S_OVER_WS = 3, // used in skip_thing() for moving "dot"
  80. S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
  81. S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
  82. };
  83. /* vi.c expects chars to be unsigned. */
  84. /* busybox build system provides that, but it's better */
  85. /* to audit and fix the source */
  86. static smallint vi_setops;
  87. #define VI_AUTOINDENT 1
  88. #define VI_SHOWMATCH 2
  89. #define VI_IGNORECASE 4
  90. #define VI_ERR_METHOD 8
  91. #define autoindent (vi_setops & VI_AUTOINDENT)
  92. #define showmatch (vi_setops & VI_SHOWMATCH )
  93. #define ignorecase (vi_setops & VI_IGNORECASE)
  94. /* indicate error with beep or flash */
  95. #define err_method (vi_setops & VI_ERR_METHOD)
  96. static smallint editing; // >0 while we are editing a file
  97. // [code audit says "can be 0 or 1 only"]
  98. static smallint cmd_mode; // 0=command 1=insert 2=replace
  99. static smallint file_modified; // buffer contents changed
  100. static smallint last_file_modified = -1;
  101. static int fn_start; // index of first cmd line file name
  102. static int save_argc; // how many file names on cmd line
  103. static int cmdcnt; // repetition count
  104. static int rows, columns; // the terminal screen is this size
  105. static int crow, ccol, offset; // cursor is on Crow x Ccol with Horz Ofset
  106. static char *status_buffer; // mesages to the user
  107. #define STATUS_BUFFER_LEN 200
  108. static int have_status_msg; // is default edit status needed?
  109. // [don't make smallint!]
  110. static int last_status_cksum; // hash of current status line
  111. static char *current_filename; // current file name
  112. //static char *text, *end; // pointers to the user data in memory
  113. static char *screen; // pointer to the virtual screen buffer
  114. static int screensize; // and its size
  115. static char *screenbegin; // index into text[], of top line on the screen
  116. //static char *dot; // where all the action takes place
  117. static int tabstop;
  118. static char erase_char; // the users erase character
  119. static char last_input_char; // last char read from user
  120. static char last_forward_char; // last char searched for with 'f'
  121. #if ENABLE_FEATURE_VI_READONLY
  122. //static smallint vi_readonly, readonly;
  123. static smallint readonly_mode = 0;
  124. #define SET_READONLY_FILE(flags) ((flags) |= 0x01)
  125. #define SET_READONLY_MODE(flags) ((flags) |= 0x02)
  126. #define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
  127. #else
  128. #define readonly_mode 0
  129. #define SET_READONLY_FILE(flags)
  130. #define SET_READONLY_MODE(flags)
  131. #define UNSET_READONLY_FILE(flags)
  132. #endif
  133. #if ENABLE_FEATURE_VI_DOT_CMD
  134. static smallint adding2q; // are we currently adding user input to q
  135. static char *last_modifying_cmd; // last modifying cmd for "."
  136. static char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
  137. #endif
  138. #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
  139. static int last_row; // where the cursor was last moved to
  140. #endif
  141. #if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
  142. static int my_pid;
  143. #endif
  144. #if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
  145. static char *modifying_cmds; // cmds that modify text[]
  146. #endif
  147. #if ENABLE_FEATURE_VI_SEARCH
  148. static char *last_search_pattern; // last pattern from a '/' or '?' search
  149. #endif
  150. /* Moving biggest data to malloced space... */
  151. struct globals {
  152. /* many references - keep near the top of globals */
  153. char *text, *end; // pointers to the user data in memory
  154. int text_size; // size of the allocated buffer
  155. char *dot; // where all the action takes place
  156. #if ENABLE_FEATURE_VI_YANKMARK
  157. char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
  158. int YDreg, Ureg; // default delete register and orig line for "U"
  159. char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
  160. char *context_start, *context_end;
  161. #endif
  162. /* a few references only */
  163. #if ENABLE_FEATURE_VI_USE_SIGNALS
  164. jmp_buf restart; // catch_sig()
  165. #endif
  166. struct termios term_orig, term_vi; // remember what the cooked mode was
  167. #if ENABLE_FEATURE_VI_COLON
  168. char *initial_cmds[3]; // currently 2 entries, NULL terminated
  169. #endif
  170. char readbuffer[MAX_LINELEN];
  171. };
  172. #define G (*ptr_to_globals)
  173. #define text (G.text )
  174. #define text_size (G.text_size )
  175. #define end (G.end )
  176. #define dot (G.dot )
  177. #define reg (G.reg )
  178. #define YDreg (G.YDreg )
  179. #define Ureg (G.Ureg )
  180. #define mark (G.mark )
  181. #define context_start (G.context_start )
  182. #define context_end (G.context_end )
  183. #define restart (G.restart )
  184. #define term_orig (G.term_orig )
  185. #define term_vi (G.term_vi )
  186. #define initial_cmds (G.initial_cmds )
  187. #define readbuffer (G.readbuffer )
  188. #define INIT_G() do { \
  189. PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
  190. } while (0)
  191. static int init_text_buffer(char *); // init from file or create new
  192. static void edit_file(char *); // edit one file
  193. static void do_cmd(char); // execute a command
  194. static int next_tabstop(int);
  195. static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
  196. static char *begin_line(char *); // return pointer to cur line B-o-l
  197. static char *end_line(char *); // return pointer to cur line E-o-l
  198. static char *prev_line(char *); // return pointer to prev line B-o-l
  199. static char *next_line(char *); // return pointer to next line B-o-l
  200. static char *end_screen(void); // get pointer to last char on screen
  201. static int count_lines(char *, char *); // count line from start to stop
  202. static char *find_line(int); // find begining of line #li
  203. static char *move_to_col(char *, int); // move "p" to column l
  204. static void dot_left(void); // move dot left- dont leave line
  205. static void dot_right(void); // move dot right- dont leave line
  206. static void dot_begin(void); // move dot to B-o-l
  207. static void dot_end(void); // move dot to E-o-l
  208. static void dot_next(void); // move dot to next line B-o-l
  209. static void dot_prev(void); // move dot to prev line B-o-l
  210. static void dot_scroll(int, int); // move the screen up or down
  211. static void dot_skip_over_ws(void); // move dot pat WS
  212. static void dot_delete(void); // delete the char at 'dot'
  213. static char *bound_dot(char *); // make sure text[0] <= P < "end"
  214. static char *new_screen(int, int); // malloc virtual screen memory
  215. static char *char_insert(char *, char); // insert the char c at 'p'
  216. static char *stupid_insert(char *, char); // stupidly insert the char c at 'p'
  217. static char find_range(char **, char **, char); // return pointers for an object
  218. static int st_test(char *, int, int, char *); // helper for skip_thing()
  219. static char *skip_thing(char *, int, int, int); // skip some object
  220. static char *find_pair(char *, char); // find matching pair () [] {}
  221. static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
  222. static char *text_hole_make(char *, int); // at "p", make a 'size' byte hole
  223. static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
  224. static void show_help(void); // display some help info
  225. static void rawmode(void); // set "raw" mode on tty
  226. static void cookmode(void); // return to "cooked" mode on tty
  227. // sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
  228. static int mysleep(int);
  229. static char readit(void); // read (maybe cursor) key from stdin
  230. static char get_one_char(void); // read 1 char from stdin
  231. static int file_size(const char *); // what is the byte size of "fn"
  232. #if ENABLE_FEATURE_VI_READONLY
  233. static int file_insert(const char *, char *, int);
  234. #else
  235. static int file_insert(const char *, char *);
  236. #endif
  237. static int file_write(char *, char *, char *);
  238. static void place_cursor(int, int, int);
  239. static void screen_erase(void);
  240. static void clear_to_eol(void);
  241. static void clear_to_eos(void);
  242. static void standout_start(void); // send "start reverse video" sequence
  243. static void standout_end(void); // send "end reverse video" sequence
  244. static void flash(int); // flash the terminal screen
  245. static void show_status_line(void); // put a message on the bottom line
  246. static void psb(const char *, ...); // Print Status Buf
  247. static void psbs(const char *, ...); // Print Status Buf in standout mode
  248. static void ni(const char *); // display messages
  249. static int format_edit_status(void); // format file status on status line
  250. static void redraw(int); // force a full screen refresh
  251. static void format_line(char*, char*, int);
  252. static void refresh(int); // update the terminal from screen[]
  253. static void Indicate_Error(void); // use flash or beep to indicate error
  254. #define indicate_error(c) Indicate_Error()
  255. static void Hit_Return(void);
  256. #if ENABLE_FEATURE_VI_SEARCH
  257. static char *char_search(char *, const char *, int, int); // search for pattern starting at p
  258. static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase"
  259. #endif
  260. #if ENABLE_FEATURE_VI_COLON
  261. static char *get_one_address(char *, int *); // get colon addr, if present
  262. static char *get_address(char *, int *, int *); // get two colon addrs, if present
  263. static void colon(char *); // execute the "colon" mode cmds
  264. #endif
  265. #if ENABLE_FEATURE_VI_USE_SIGNALS
  266. static void winch_sig(int); // catch window size changes
  267. static void suspend_sig(int); // catch ctrl-Z
  268. static void catch_sig(int); // catch ctrl-C and alarm time-outs
  269. #endif
  270. #if ENABLE_FEATURE_VI_DOT_CMD
  271. static void start_new_cmd_q(char); // new queue for command
  272. static void end_cmd_q(void); // stop saving input chars
  273. #else
  274. #define end_cmd_q() ((void)0)
  275. #endif
  276. #if ENABLE_FEATURE_VI_SETOPTS
  277. static void showmatching(char *); // show the matching pair () [] {}
  278. #endif
  279. #if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
  280. static char *string_insert(char *, char *); // insert the string at 'p'
  281. #endif
  282. #if ENABLE_FEATURE_VI_YANKMARK
  283. static char *text_yank(char *, char *, int); // save copy of "p" into a register
  284. static char what_reg(void); // what is letter of current YDreg
  285. static void check_context(char); // remember context for '' command
  286. #endif
  287. #if ENABLE_FEATURE_VI_CRASHME
  288. static void crash_dummy();
  289. static void crash_test();
  290. static int crashme = 0;
  291. #endif
  292. static void write1(const char *out)
  293. {
  294. fputs(out, stdout);
  295. }
  296. int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  297. int vi_main(int argc, char **argv)
  298. {
  299. int c;
  300. RESERVE_CONFIG_BUFFER(STATUS_BUFFER, STATUS_BUFFER_LEN);
  301. #if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
  302. my_pid = getpid();
  303. #endif
  304. INIT_G();
  305. #if ENABLE_FEATURE_VI_CRASHME
  306. srand((long) my_pid);
  307. #endif
  308. status_buffer = STATUS_BUFFER;
  309. last_status_cksum = 0;
  310. text = NULL;
  311. #ifdef NO_SUCH_APPLET_YET
  312. /* If we aren't "vi", we are "view" */
  313. if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
  314. SET_READONLY_MODE(readonly_mode);
  315. }
  316. #endif
  317. vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
  318. #if ENABLE_FEATURE_VI_YANKMARK
  319. memset(reg, 0, sizeof(reg)); // init the yank regs
  320. #endif
  321. #if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
  322. modifying_cmds = (char *) "aAcCdDiIJoOpPrRsxX<>~"; // cmds modifying text[]
  323. #endif
  324. // 1- process $HOME/.exrc file (not inplemented yet)
  325. // 2- process EXINIT variable from environment
  326. // 3- process command line args
  327. #if ENABLE_FEATURE_VI_COLON
  328. {
  329. char *p = getenv("EXINIT");
  330. if (p && *p)
  331. initial_cmds[0] = xstrdup(p);
  332. }
  333. #endif
  334. while ((c = getopt(argc, argv, "hCR" USE_FEATURE_VI_COLON("c:"))) != -1) {
  335. switch (c) {
  336. #if ENABLE_FEATURE_VI_CRASHME
  337. case 'C':
  338. crashme = 1;
  339. break;
  340. #endif
  341. #if ENABLE_FEATURE_VI_READONLY
  342. case 'R': // Read-only flag
  343. SET_READONLY_MODE(readonly_mode);
  344. break;
  345. #endif
  346. //case 'r': // recover flag- ignore- we don't use tmp file
  347. //case 'x': // encryption flag- ignore
  348. //case 'c': // execute command first
  349. #if ENABLE_FEATURE_VI_COLON
  350. case 'c': // cmd line vi command
  351. if (*optarg)
  352. initial_cmds[initial_cmds[0] != 0] = xstrdup(optarg);
  353. break;
  354. //case 'h': // help -- just use default
  355. #endif
  356. default:
  357. show_help();
  358. return 1;
  359. }
  360. }
  361. // The argv array can be used by the ":next" and ":rewind" commands
  362. // save optind.
  363. fn_start = optind; // remember first file name for :next and :rew
  364. save_argc = argc;
  365. //----- This is the main file handling loop --------------
  366. if (optind >= argc) {
  367. edit_file(0);
  368. } else {
  369. for (; optind < argc; optind++) {
  370. edit_file(argv[optind]);
  371. }
  372. }
  373. //-----------------------------------------------------------
  374. return 0;
  375. }
  376. /* read text from file or create an empty buf */
  377. /* will also update current_filename */
  378. static int init_text_buffer(char *fn)
  379. {
  380. int rc;
  381. int size = file_size(fn); // file size. -1 means does not exist.
  382. /* allocate/reallocate text buffer */
  383. free(text);
  384. text_size = size * 2;
  385. if (text_size < 10240)
  386. text_size = 10240; // have a minimum size for new files
  387. screenbegin = dot = end = text = xzalloc(text_size);
  388. if (fn != current_filename) {
  389. free(current_filename);
  390. current_filename = xstrdup(fn);
  391. }
  392. if (size < 0) {
  393. // file dont exist. Start empty buf with dummy line
  394. char_insert(text, '\n');
  395. rc = 0;
  396. } else {
  397. rc = file_insert(fn, text
  398. USE_FEATURE_VI_READONLY(, 1));
  399. }
  400. file_modified = 0;
  401. last_file_modified = -1;
  402. #if ENABLE_FEATURE_VI_YANKMARK
  403. /* init the marks. */
  404. memset(mark, 0, sizeof(mark));
  405. #endif
  406. return rc;
  407. }
  408. static void edit_file(char * fn)
  409. {
  410. char c;
  411. int size;
  412. #if ENABLE_FEATURE_VI_USE_SIGNALS
  413. int sig;
  414. #endif
  415. #if ENABLE_FEATURE_VI_YANKMARK
  416. static char *cur_line;
  417. #endif
  418. editing = 1; // 0= exit, 1= one file, 2= multiple files
  419. rawmode();
  420. rows = 24;
  421. columns = 80;
  422. size = 0;
  423. if (ENABLE_FEATURE_VI_WIN_RESIZE)
  424. get_terminal_width_height(0, &columns, &rows);
  425. new_screen(rows, columns); // get memory for virtual screen
  426. init_text_buffer(fn);
  427. #if ENABLE_FEATURE_VI_YANKMARK
  428. YDreg = 26; // default Yank/Delete reg
  429. Ureg = 27; // hold orig line for "U" cmd
  430. mark[26] = mark[27] = text; // init "previous context"
  431. #endif
  432. last_forward_char = last_input_char = '\0';
  433. crow = 0;
  434. ccol = 0;
  435. #if ENABLE_FEATURE_VI_USE_SIGNALS
  436. catch_sig(0);
  437. signal(SIGWINCH, winch_sig);
  438. signal(SIGTSTP, suspend_sig);
  439. sig = setjmp(restart);
  440. if (sig != 0) {
  441. screenbegin = dot = text;
  442. }
  443. #endif
  444. cmd_mode = 0; // 0=command 1=insert 2='R'eplace
  445. cmdcnt = 0;
  446. tabstop = 8;
  447. offset = 0; // no horizontal offset
  448. c = '\0';
  449. #if ENABLE_FEATURE_VI_DOT_CMD
  450. free(last_modifying_cmd);
  451. free(ioq_start);
  452. ioq = ioq_start = last_modifying_cmd = NULL;
  453. adding2q = 0;
  454. #endif
  455. redraw(FALSE); // dont force every col re-draw
  456. #if ENABLE_FEATURE_VI_COLON
  457. {
  458. char *p, *q;
  459. int n = 0;
  460. while ((p = initial_cmds[n])) {
  461. do {
  462. q = p;
  463. p = strchr(q,'\n');
  464. if (p)
  465. while (*p == '\n')
  466. *p++ = '\0';
  467. if (*q)
  468. colon(q);
  469. } while (p);
  470. free(initial_cmds[n]);
  471. initial_cmds[n] = NULL;
  472. n++;
  473. }
  474. }
  475. #endif
  476. //------This is the main Vi cmd handling loop -----------------------
  477. while (editing > 0) {
  478. #if ENABLE_FEATURE_VI_CRASHME
  479. if (crashme > 0) {
  480. if ((end - text) > 1) {
  481. crash_dummy(); // generate a random command
  482. } else {
  483. crashme = 0;
  484. dot = string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
  485. refresh(FALSE);
  486. }
  487. }
  488. #endif
  489. last_input_char = c = get_one_char(); // get a cmd from user
  490. #if ENABLE_FEATURE_VI_YANKMARK
  491. // save a copy of the current line- for the 'U" command
  492. if (begin_line(dot) != cur_line) {
  493. cur_line = begin_line(dot);
  494. text_yank(begin_line(dot), end_line(dot), Ureg);
  495. }
  496. #endif
  497. #if ENABLE_FEATURE_VI_DOT_CMD
  498. // These are commands that change text[].
  499. // Remember the input for the "." command
  500. if (!adding2q && ioq_start == 0
  501. && strchr(modifying_cmds, c)
  502. ) {
  503. start_new_cmd_q(c);
  504. }
  505. #endif
  506. do_cmd(c); // execute the user command
  507. //
  508. // poll to see if there is input already waiting. if we are
  509. // not able to display output fast enough to keep up, skip
  510. // the display update until we catch up with input.
  511. if (mysleep(0) == 0) {
  512. // no input pending- so update output
  513. refresh(FALSE);
  514. show_status_line();
  515. }
  516. #if ENABLE_FEATURE_VI_CRASHME
  517. if (crashme > 0)
  518. crash_test(); // test editor variables
  519. #endif
  520. }
  521. //-------------------------------------------------------------------
  522. place_cursor(rows, 0, FALSE); // go to bottom of screen
  523. clear_to_eol(); // Erase to end of line
  524. cookmode();
  525. }
  526. //----- The Colon commands -------------------------------------
  527. #if ENABLE_FEATURE_VI_COLON
  528. static char *get_one_address(char * p, int *addr) // get colon addr, if present
  529. {
  530. int st;
  531. char *q;
  532. #if ENABLE_FEATURE_VI_YANKMARK
  533. char c;
  534. #endif
  535. #if ENABLE_FEATURE_VI_SEARCH
  536. char *pat, buf[MAX_LINELEN];
  537. #endif
  538. *addr = -1; // assume no addr
  539. if (*p == '.') { // the current line
  540. p++;
  541. q = begin_line(dot);
  542. *addr = count_lines(text, q);
  543. #if ENABLE_FEATURE_VI_YANKMARK
  544. } else if (*p == '\'') { // is this a mark addr
  545. p++;
  546. c = tolower(*p);
  547. p++;
  548. if (c >= 'a' && c <= 'z') {
  549. // we have a mark
  550. c = c - 'a';
  551. q = mark[(unsigned char) c];
  552. if (q != NULL) { // is mark valid
  553. *addr = count_lines(text, q); // count lines
  554. }
  555. }
  556. #endif
  557. #if ENABLE_FEATURE_VI_SEARCH
  558. } else if (*p == '/') { // a search pattern
  559. q = buf;
  560. for (p++; *p; p++) {
  561. if (*p == '/')
  562. break;
  563. *q++ = *p;
  564. *q = '\0';
  565. }
  566. pat = xstrdup(buf); // save copy of pattern
  567. if (*p == '/')
  568. p++;
  569. q = char_search(dot, pat, FORWARD, FULL);
  570. if (q != NULL) {
  571. *addr = count_lines(text, q);
  572. }
  573. free(pat);
  574. #endif
  575. } else if (*p == '$') { // the last line in file
  576. p++;
  577. q = begin_line(end - 1);
  578. *addr = count_lines(text, q);
  579. } else if (isdigit(*p)) { // specific line number
  580. sscanf(p, "%d%n", addr, &st);
  581. p += st;
  582. } else { // I don't reconise this
  583. // unrecognised address- assume -1
  584. *addr = -1;
  585. }
  586. return p;
  587. }
  588. static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
  589. {
  590. //----- get the address' i.e., 1,3 'a,'b -----
  591. // get FIRST addr, if present
  592. while (isblank(*p))
  593. p++; // skip over leading spaces
  594. if (*p == '%') { // alias for 1,$
  595. p++;
  596. *b = 1;
  597. *e = count_lines(text, end-1);
  598. goto ga0;
  599. }
  600. p = get_one_address(p, b);
  601. while (isblank(*p))
  602. p++;
  603. if (*p == ',') { // is there a address separator
  604. p++;
  605. while (isblank(*p))
  606. p++;
  607. // get SECOND addr, if present
  608. p = get_one_address(p, e);
  609. }
  610. ga0:
  611. while (isblank(*p))
  612. p++; // skip over trailing spaces
  613. return p;
  614. }
  615. #if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
  616. static void setops(const char *args, const char *opname, int flg_no,
  617. const char *short_opname, int opt)
  618. {
  619. const char *a = args + flg_no;
  620. int l = strlen(opname) - 1; /* opname have + ' ' */
  621. if (strncasecmp(a, opname, l) == 0
  622. || strncasecmp(a, short_opname, 2) == 0
  623. ) {
  624. if (flg_no)
  625. vi_setops &= ~opt;
  626. else
  627. vi_setops |= opt;
  628. }
  629. }
  630. #endif
  631. static void colon(char * buf)
  632. {
  633. char c, *orig_buf, *buf1, *q, *r;
  634. char *fn, cmd[MAX_LINELEN], args[MAX_LINELEN];
  635. int i, l, li, ch, b, e;
  636. int useforce = FALSE, forced = FALSE;
  637. // :3154 // if (-e line 3154) goto it else stay put
  638. // :4,33w! foo // write a portion of buffer to file "foo"
  639. // :w // write all of buffer to current file
  640. // :q // quit
  641. // :q! // quit- dont care about modified file
  642. // :'a,'z!sort -u // filter block through sort
  643. // :'f // goto mark "f"
  644. // :'fl // list literal the mark "f" line
  645. // :.r bar // read file "bar" into buffer before dot
  646. // :/123/,/abc/d // delete lines from "123" line to "abc" line
  647. // :/xyz/ // goto the "xyz" line
  648. // :s/find/replace/ // substitute pattern "find" with "replace"
  649. // :!<cmd> // run <cmd> then return
  650. //
  651. if (!buf[0])
  652. goto vc1;
  653. if (*buf == ':')
  654. buf++; // move past the ':'
  655. li = ch = i = 0;
  656. b = e = -1;
  657. q = text; // assume 1,$ for the range
  658. r = end - 1;
  659. li = count_lines(text, end - 1);
  660. fn = current_filename; // default to current file
  661. memset(cmd, '\0', MAX_LINELEN); // clear cmd[]
  662. memset(args, '\0', MAX_LINELEN); // clear args[]
  663. // look for optional address(es) :. :1 :1,9 :'q,'a :%
  664. buf = get_address(buf, &b, &e);
  665. // remember orig command line
  666. orig_buf = buf;
  667. // get the COMMAND into cmd[]
  668. buf1 = cmd;
  669. while (*buf != '\0') {
  670. if (isspace(*buf))
  671. break;
  672. *buf1++ = *buf++;
  673. }
  674. // get any ARGuments
  675. while (isblank(*buf))
  676. buf++;
  677. strcpy(args, buf);
  678. buf1 = last_char_is(cmd, '!');
  679. if (buf1) {
  680. useforce = TRUE;
  681. *buf1 = '\0'; // get rid of !
  682. }
  683. if (b >= 0) {
  684. // if there is only one addr, then the addr
  685. // is the line number of the single line the
  686. // user wants. So, reset the end
  687. // pointer to point at end of the "b" line
  688. q = find_line(b); // what line is #b
  689. r = end_line(q);
  690. li = 1;
  691. }
  692. if (e >= 0) {
  693. // we were given two addrs. change the
  694. // end pointer to the addr given by user.
  695. r = find_line(e); // what line is #e
  696. r = end_line(r);
  697. li = e - b + 1;
  698. }
  699. // ------------ now look for the command ------------
  700. i = strlen(cmd);
  701. if (i == 0) { // :123CR goto line #123
  702. if (b >= 0) {
  703. dot = find_line(b); // what line is #b
  704. dot_skip_over_ws();
  705. }
  706. }
  707. #if ENABLE_FEATURE_ALLOW_EXEC
  708. else if (strncmp(cmd, "!", 1) == 0) { // run a cmd
  709. int retcode;
  710. // :!ls run the <cmd>
  711. alarm(0); // wait for input- no alarms
  712. place_cursor(rows - 1, 0, FALSE); // go to Status line
  713. clear_to_eol(); // clear the line
  714. cookmode();
  715. retcode = system(orig_buf + 1); // run the cmd
  716. if (retcode)
  717. printf("\nshell returned %i\n\n", retcode);
  718. rawmode();
  719. Hit_Return(); // let user see results
  720. alarm(3); // done waiting for input
  721. }
  722. #endif
  723. else if (strncmp(cmd, "=", i) == 0) { // where is the address
  724. if (b < 0) { // no addr given- use defaults
  725. b = e = count_lines(text, dot);
  726. }
  727. psb("%d", b);
  728. } else if (strncasecmp(cmd, "delete", i) == 0) { // delete lines
  729. if (b < 0) { // no addr given- use defaults
  730. q = begin_line(dot); // assume .,. for the range
  731. r = end_line(dot);
  732. }
  733. dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
  734. dot_skip_over_ws();
  735. } else if (strncasecmp(cmd, "edit", i) == 0) { // Edit a file
  736. // don't edit, if the current file has been modified
  737. if (file_modified && ! useforce) {
  738. psbs("No write since last change (:edit! overrides)");
  739. goto vc1;
  740. }
  741. if (args[0]) {
  742. // the user supplied a file name
  743. fn = args;
  744. } else if (current_filename && current_filename[0]) {
  745. // no user supplied name- use the current filename
  746. // fn = current_filename; was set by default
  747. } else {
  748. // no user file name, no current name- punt
  749. psbs("No current filename");
  750. goto vc1;
  751. }
  752. if (init_text_buffer(fn) < 0)
  753. goto vc1;
  754. #if ENABLE_FEATURE_VI_YANKMARK
  755. if (Ureg >= 0 && Ureg < 28 && reg[Ureg] != 0) {
  756. free(reg[Ureg]); // free orig line reg- for 'U'
  757. reg[Ureg]= 0;
  758. }
  759. if (YDreg >= 0 && YDreg < 28 && reg[YDreg] != 0) {
  760. free(reg[YDreg]); // free default yank/delete register
  761. reg[YDreg]= 0;
  762. }
  763. #endif
  764. // how many lines in text[]?
  765. li = count_lines(text, end - 1);
  766. psb("\"%s\"%s"
  767. USE_FEATURE_VI_READONLY("%s")
  768. " %dL, %dC", current_filename,
  769. (file_size(fn) < 0 ? " [New file]" : ""),
  770. USE_FEATURE_VI_READONLY(
  771. ((readonly_mode) ? " [Readonly]" : ""),
  772. )
  773. li, ch);
  774. } else if (strncasecmp(cmd, "file", i) == 0) { // what File is this
  775. if (b != -1 || e != -1) {
  776. ni("No address allowed on this command");
  777. goto vc1;
  778. }
  779. if (args[0]) {
  780. // user wants a new filename
  781. free(current_filename);
  782. current_filename = xstrdup(args);
  783. } else {
  784. // user wants file status info
  785. last_status_cksum = 0; // force status update
  786. }
  787. } else if (strncasecmp(cmd, "features", i) == 0) { // what features are available
  788. // print out values of all features
  789. place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
  790. clear_to_eol(); // clear the line
  791. cookmode();
  792. show_help();
  793. rawmode();
  794. Hit_Return();
  795. } else if (strncasecmp(cmd, "list", i) == 0) { // literal print line
  796. if (b < 0) { // no addr given- use defaults
  797. q = begin_line(dot); // assume .,. for the range
  798. r = end_line(dot);
  799. }
  800. place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
  801. clear_to_eol(); // clear the line
  802. puts("\r");
  803. for (; q <= r; q++) {
  804. int c_is_no_print;
  805. c = *q;
  806. c_is_no_print = (c & 0x80) && !Isprint(c);
  807. if (c_is_no_print) {
  808. c = '.';
  809. standout_start();
  810. }
  811. if (c == '\n') {
  812. write1("$\r");
  813. } else if (c < ' ' || c == 127) {
  814. bb_putchar('^');
  815. if (c == 127)
  816. c = '?';
  817. else
  818. c += '@';
  819. }
  820. bb_putchar(c);
  821. if (c_is_no_print)
  822. standout_end();
  823. }
  824. #if ENABLE_FEATURE_VI_SET
  825. vc2:
  826. #endif
  827. Hit_Return();
  828. } else if (strncasecmp(cmd, "quit", i) == 0 // Quit
  829. || strncasecmp(cmd, "next", i) == 0 // edit next file
  830. ) {
  831. if (useforce) {
  832. // force end of argv list
  833. if (*cmd == 'q') {
  834. optind = save_argc;
  835. }
  836. editing = 0;
  837. goto vc1;
  838. }
  839. // don't exit if the file been modified
  840. if (file_modified) {
  841. psbs("No write since last change (:%s! overrides)",
  842. (*cmd == 'q' ? "quit" : "next"));
  843. goto vc1;
  844. }
  845. // are there other file to edit
  846. if (*cmd == 'q' && optind < save_argc - 1) {
  847. psbs("%d more file to edit", (save_argc - optind - 1));
  848. goto vc1;
  849. }
  850. if (*cmd == 'n' && optind >= save_argc - 1) {
  851. psbs("No more files to edit");
  852. goto vc1;
  853. }
  854. editing = 0;
  855. } else if (strncasecmp(cmd, "read", i) == 0) { // read file into text[]
  856. fn = args;
  857. if (!fn[0]) {
  858. psbs("No filename given");
  859. goto vc1;
  860. }
  861. if (b < 0) { // no addr given- use defaults
  862. q = begin_line(dot); // assume "dot"
  863. }
  864. // read after current line- unless user said ":0r foo"
  865. if (b != 0)
  866. q = next_line(q);
  867. ch = file_insert(fn, q USE_FEATURE_VI_READONLY(, 0));
  868. if (ch < 0)
  869. goto vc1; // nothing was inserted
  870. // how many lines in text[]?
  871. li = count_lines(q, q + ch - 1);
  872. psb("\"%s\""
  873. USE_FEATURE_VI_READONLY("%s")
  874. " %dL, %dC", fn,
  875. USE_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
  876. li, ch);
  877. if (ch > 0) {
  878. // if the insert is before "dot" then we need to update
  879. if (q <= dot)
  880. dot += ch;
  881. file_modified++;
  882. }
  883. } else if (strncasecmp(cmd, "rewind", i) == 0) { // rewind cmd line args
  884. if (file_modified && ! useforce) {
  885. psbs("No write since last change (:rewind! overrides)");
  886. } else {
  887. // reset the filenames to edit
  888. optind = fn_start - 1;
  889. editing = 0;
  890. }
  891. #if ENABLE_FEATURE_VI_SET
  892. } else if (strncasecmp(cmd, "set", i) == 0) { // set or clear features
  893. #if ENABLE_FEATURE_VI_SETOPTS
  894. char *argp;
  895. #endif
  896. i = 0; // offset into args
  897. // only blank is regarded as args delmiter. What about tab '\t' ?
  898. if (!args[0] || strcasecmp(args, "all") == 0) {
  899. // print out values of all options
  900. place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
  901. clear_to_eol(); // clear the line
  902. printf("----------------------------------------\r\n");
  903. #if ENABLE_FEATURE_VI_SETOPTS
  904. if (!autoindent)
  905. printf("no");
  906. printf("autoindent ");
  907. if (!err_method)
  908. printf("no");
  909. printf("flash ");
  910. if (!ignorecase)
  911. printf("no");
  912. printf("ignorecase ");
  913. if (!showmatch)
  914. printf("no");
  915. printf("showmatch ");
  916. printf("tabstop=%d ", tabstop);
  917. #endif
  918. printf("\r\n");
  919. goto vc2;
  920. }
  921. #if ENABLE_FEATURE_VI_SETOPTS
  922. argp = args;
  923. while (*argp) {
  924. if (strncasecmp(argp, "no", 2) == 0)
  925. i = 2; // ":set noautoindent"
  926. setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
  927. setops(argp, "flash ", i, "fl", VI_ERR_METHOD);
  928. setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
  929. setops(argp, "showmatch ", i, "ic", VI_SHOWMATCH);
  930. /* tabstopXXXX */
  931. if (strncasecmp(argp + i, "tabstop=%d ", 7) == 0) {
  932. sscanf(strchr(argp + i, '='), "tabstop=%d" + 7, &ch);
  933. if (ch > 0 && ch < columns - 1)
  934. tabstop = ch;
  935. }
  936. while (*argp && *argp != ' ')
  937. argp++; // skip to arg delimiter (i.e. blank)
  938. while (*argp && *argp == ' ')
  939. argp++; // skip all delimiting blanks
  940. }
  941. #endif /* FEATURE_VI_SETOPTS */
  942. #endif /* FEATURE_VI_SET */
  943. #if ENABLE_FEATURE_VI_SEARCH
  944. } else if (strncasecmp(cmd, "s", 1) == 0) { // substitute a pattern with a replacement pattern
  945. char *ls, *F, *R;
  946. int gflag;
  947. // F points to the "find" pattern
  948. // R points to the "replace" pattern
  949. // replace the cmd line delimiters "/" with NULLs
  950. gflag = 0; // global replace flag
  951. c = orig_buf[1]; // what is the delimiter
  952. F = orig_buf + 2; // start of "find"
  953. R = strchr(F, c); // middle delimiter
  954. if (!R) goto colon_s_fail;
  955. *R++ = '\0'; // terminate "find"
  956. buf1 = strchr(R, c);
  957. if (!buf1) goto colon_s_fail;
  958. *buf1++ = '\0'; // terminate "replace"
  959. if (*buf1 == 'g') { // :s/foo/bar/g
  960. buf1++;
  961. gflag++; // turn on gflag
  962. }
  963. q = begin_line(q);
  964. if (b < 0) { // maybe :s/foo/bar/
  965. q = begin_line(dot); // start with cur line
  966. b = count_lines(text, q); // cur line number
  967. }
  968. if (e < 0)
  969. e = b; // maybe :.s/foo/bar/
  970. for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
  971. ls = q; // orig line start
  972. vc4:
  973. buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
  974. if (buf1) {
  975. // we found the "find" pattern - delete it
  976. text_hole_delete(buf1, buf1 + strlen(F) - 1);
  977. // inset the "replace" patern
  978. string_insert(buf1, R); // insert the string
  979. // check for "global" :s/foo/bar/g
  980. if (gflag == 1) {
  981. if ((buf1 + strlen(R)) < end_line(ls)) {
  982. q = buf1 + strlen(R);
  983. goto vc4; // don't let q move past cur line
  984. }
  985. }
  986. }
  987. q = next_line(ls);
  988. }
  989. #endif /* FEATURE_VI_SEARCH */
  990. } else if (strncasecmp(cmd, "version", i) == 0) { // show software version
  991. psb("%s", BB_VER " " BB_BT);
  992. } else if (strncasecmp(cmd, "write", i) == 0 // write text to file
  993. || strncasecmp(cmd, "wq", i) == 0
  994. || strncasecmp(cmd, "wn", i) == 0
  995. || strncasecmp(cmd, "x", i) == 0
  996. ) {
  997. // is there a file name to write to?
  998. if (args[0]) {
  999. fn = args;
  1000. }
  1001. #if ENABLE_FEATURE_VI_READONLY
  1002. if (readonly_mode && !useforce) {
  1003. psbs("\"%s\" File is read only", fn);
  1004. goto vc3;
  1005. }
  1006. #endif
  1007. // how many lines in text[]?
  1008. li = count_lines(q, r);
  1009. ch = r - q + 1;
  1010. // see if file exists- if not, its just a new file request
  1011. if (useforce) {
  1012. // if "fn" is not write-able, chmod u+w
  1013. // sprintf(syscmd, "chmod u+w %s", fn);
  1014. // system(syscmd);
  1015. forced = TRUE;
  1016. }
  1017. l = file_write(fn, q, r);
  1018. if (useforce && forced) {
  1019. // chmod u-w
  1020. // sprintf(syscmd, "chmod u-w %s", fn);
  1021. // system(syscmd);
  1022. forced = FALSE;
  1023. }
  1024. if (l < 0) {
  1025. if (l == -1)
  1026. psbs("\"%s\" %s", fn, strerror(errno));
  1027. } else {
  1028. psb("\"%s\" %dL, %dC", fn, li, l);
  1029. if (q == text && r == end - 1 && l == ch) {
  1030. file_modified = 0;
  1031. last_file_modified = -1;
  1032. }
  1033. if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' ||
  1034. cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N')
  1035. && l == ch) {
  1036. editing = 0;
  1037. }
  1038. }
  1039. #if ENABLE_FEATURE_VI_READONLY
  1040. vc3:;
  1041. #endif
  1042. #if ENABLE_FEATURE_VI_YANKMARK
  1043. } else if (strncasecmp(cmd, "yank", i) == 0) { // yank lines
  1044. if (b < 0) { // no addr given- use defaults
  1045. q = begin_line(dot); // assume .,. for the range
  1046. r = end_line(dot);
  1047. }
  1048. text_yank(q, r, YDreg);
  1049. li = count_lines(q, r);
  1050. psb("Yank %d lines (%d chars) into [%c]",
  1051. li, strlen(reg[YDreg]), what_reg());
  1052. #endif
  1053. } else {
  1054. // cmd unknown
  1055. ni(cmd);
  1056. }
  1057. vc1:
  1058. dot = bound_dot(dot); // make sure "dot" is valid
  1059. return;
  1060. #if ENABLE_FEATURE_VI_SEARCH
  1061. colon_s_fail:
  1062. psb(":s expression missing delimiters");
  1063. #endif
  1064. }
  1065. #endif /* FEATURE_VI_COLON */
  1066. static void Hit_Return(void)
  1067. {
  1068. char c;
  1069. standout_start(); // start reverse video
  1070. write1("[Hit return to continue]");
  1071. standout_end(); // end reverse video
  1072. while ((c = get_one_char()) != '\n' && c != '\r') /*do nothing */
  1073. ;
  1074. redraw(TRUE); // force redraw all
  1075. }
  1076. static int next_tabstop(int col)
  1077. {
  1078. return col + ((tabstop - 1) - (col % tabstop));
  1079. }
  1080. //----- Synchronize the cursor to Dot --------------------------
  1081. static void sync_cursor(char * d, int *row, int *col)
  1082. {
  1083. char *beg_cur; // begin and end of "d" line
  1084. char *end_scr; // begin and end of screen
  1085. char *tp;
  1086. int cnt, ro, co;
  1087. beg_cur = begin_line(d); // first char of cur line
  1088. end_scr = end_screen(); // last char of screen
  1089. if (beg_cur < screenbegin) {
  1090. // "d" is before top line on screen
  1091. // how many lines do we have to move
  1092. cnt = count_lines(beg_cur, screenbegin);
  1093. sc1:
  1094. screenbegin = beg_cur;
  1095. if (cnt > (rows - 1) / 2) {
  1096. // we moved too many lines. put "dot" in middle of screen
  1097. for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
  1098. screenbegin = prev_line(screenbegin);
  1099. }
  1100. }
  1101. } else if (beg_cur > end_scr) {
  1102. // "d" is after bottom line on screen
  1103. // how many lines do we have to move
  1104. cnt = count_lines(end_scr, beg_cur);
  1105. if (cnt > (rows - 1) / 2)
  1106. goto sc1; // too many lines
  1107. for (ro = 0; ro < cnt - 1; ro++) {
  1108. // move screen begin the same amount
  1109. screenbegin = next_line(screenbegin);
  1110. // now, move the end of screen
  1111. end_scr = next_line(end_scr);
  1112. end_scr = end_line(end_scr);
  1113. }
  1114. }
  1115. // "d" is on screen- find out which row
  1116. tp = screenbegin;
  1117. for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
  1118. if (tp == beg_cur)
  1119. break;
  1120. tp = next_line(tp);
  1121. }
  1122. // find out what col "d" is on
  1123. co = 0;
  1124. do { // drive "co" to correct column
  1125. if (*tp == '\n' || *tp == '\0')
  1126. break;
  1127. if (*tp == '\t') {
  1128. if (d == tp && cmd_mode) { /* handle tabs like real vi */
  1129. break;
  1130. } else {
  1131. co = next_tabstop(co);
  1132. }
  1133. } else if (*tp < ' ' || *tp == 127) {
  1134. co++; // display as ^X, use 2 columns
  1135. }
  1136. } while (tp++ < d && ++co);
  1137. // "co" is the column where "dot" is.
  1138. // The screen has "columns" columns.
  1139. // The currently displayed columns are 0+offset -- columns+ofset
  1140. // |-------------------------------------------------------------|
  1141. // ^ ^ ^
  1142. // offset | |------- columns ----------------|
  1143. //
  1144. // If "co" is already in this range then we do not have to adjust offset
  1145. // but, we do have to subtract the "offset" bias from "co".
  1146. // If "co" is outside this range then we have to change "offset".
  1147. // If the first char of a line is a tab the cursor will try to stay
  1148. // in column 7, but we have to set offset to 0.
  1149. if (co < 0 + offset) {
  1150. offset = co;
  1151. }
  1152. if (co >= columns + offset) {
  1153. offset = co - columns + 1;
  1154. }
  1155. // if the first char of the line is a tab, and "dot" is sitting on it
  1156. // force offset to 0.
  1157. if (d == beg_cur && *d == '\t') {
  1158. offset = 0;
  1159. }
  1160. co -= offset;
  1161. *row = ro;
  1162. *col = co;
  1163. }
  1164. //----- Text Movement Routines ---------------------------------
  1165. static char *begin_line(char * p) // return pointer to first char cur line
  1166. {
  1167. while (p > text && p[-1] != '\n')
  1168. p--; // go to cur line B-o-l
  1169. return p;
  1170. }
  1171. static char *end_line(char * p) // return pointer to NL of cur line line
  1172. {
  1173. while (p < end - 1 && *p != '\n')
  1174. p++; // go to cur line E-o-l
  1175. return p;
  1176. }
  1177. static inline char *dollar_line(char * p) // return pointer to just before NL line
  1178. {
  1179. while (p < end - 1 && *p != '\n')
  1180. p++; // go to cur line E-o-l
  1181. // Try to stay off of the Newline
  1182. if (*p == '\n' && (p - begin_line(p)) > 0)
  1183. p--;
  1184. return p;
  1185. }
  1186. static char *prev_line(char * p) // return pointer first char prev line
  1187. {
  1188. p = begin_line(p); // goto begining of cur line
  1189. if (p[-1] == '\n' && p > text)
  1190. p--; // step to prev line
  1191. p = begin_line(p); // goto begining of prev line
  1192. return p;
  1193. }
  1194. static char *next_line(char * p) // return pointer first char next line
  1195. {
  1196. p = end_line(p);
  1197. if (*p == '\n' && p < end - 1)
  1198. p++; // step to next line
  1199. return p;
  1200. }
  1201. //----- Text Information Routines ------------------------------
  1202. static char *end_screen(void)
  1203. {
  1204. char *q;
  1205. int cnt;
  1206. // find new bottom line
  1207. q = screenbegin;
  1208. for (cnt = 0; cnt < rows - 2; cnt++)
  1209. q = next_line(q);
  1210. q = end_line(q);
  1211. return q;
  1212. }
  1213. static int count_lines(char * start, char * stop) // count line from start to stop
  1214. {
  1215. char *q;
  1216. int cnt;
  1217. if (stop < start) { // start and stop are backwards- reverse them
  1218. q = start;
  1219. start = stop;
  1220. stop = q;
  1221. }
  1222. cnt = 0;
  1223. stop = end_line(stop); // get to end of this line
  1224. for (q = start; q <= stop && q <= end - 1; q++) {
  1225. if (*q == '\n')
  1226. cnt++;
  1227. }
  1228. return cnt;
  1229. }
  1230. static char *find_line(int li) // find begining of line #li
  1231. {
  1232. char *q;
  1233. for (q = text; li > 1; li--) {
  1234. q = next_line(q);
  1235. }
  1236. return q;
  1237. }
  1238. //----- Dot Movement Routines ----------------------------------
  1239. static void dot_left(void)
  1240. {
  1241. if (dot > text && dot[-1] != '\n')
  1242. dot--;
  1243. }
  1244. static void dot_right(void)
  1245. {
  1246. if (dot < end - 1 && *dot != '\n')
  1247. dot++;
  1248. }
  1249. static void dot_begin(void)
  1250. {
  1251. dot = begin_line(dot); // return pointer to first char cur line
  1252. }
  1253. static void dot_end(void)
  1254. {
  1255. dot = end_line(dot); // return pointer to last char cur line
  1256. }
  1257. static char *move_to_col(char * p, int l)
  1258. {
  1259. int co;
  1260. p = begin_line(p);
  1261. co = 0;
  1262. do {
  1263. if (*p == '\n' || *p == '\0')
  1264. break;
  1265. if (*p == '\t') {
  1266. co = next_tabstop(co);
  1267. } else if (*p < ' ' || *p == 127) {
  1268. co++; // display as ^X, use 2 columns
  1269. }
  1270. } while (++co <= l && p++ < end);
  1271. return p;
  1272. }
  1273. static void dot_next(void)
  1274. {
  1275. dot = next_line(dot);
  1276. }
  1277. static void dot_prev(void)
  1278. {
  1279. dot = prev_line(dot);
  1280. }
  1281. static void dot_scroll(int cnt, int dir)
  1282. {
  1283. char *q;
  1284. for (; cnt > 0; cnt--) {
  1285. if (dir < 0) {
  1286. // scroll Backwards
  1287. // ctrl-Y scroll up one line
  1288. screenbegin = prev_line(screenbegin);
  1289. } else {
  1290. // scroll Forwards
  1291. // ctrl-E scroll down one line
  1292. screenbegin = next_line(screenbegin);
  1293. }
  1294. }
  1295. // make sure "dot" stays on the screen so we dont scroll off
  1296. if (dot < screenbegin)
  1297. dot = screenbegin;
  1298. q = end_screen(); // find new bottom line
  1299. if (dot > q)
  1300. dot = begin_line(q); // is dot is below bottom line?
  1301. dot_skip_over_ws();
  1302. }
  1303. static void dot_skip_over_ws(void)
  1304. {
  1305. // skip WS
  1306. while (isspace(*dot) && *dot != '\n' && dot < end - 1)
  1307. dot++;
  1308. }
  1309. static void dot_delete(void) // delete the char at 'dot'
  1310. {
  1311. text_hole_delete(dot, dot);
  1312. }
  1313. static char *bound_dot(char * p) // make sure text[0] <= P < "end"
  1314. {
  1315. if (p >= end && end > text) {
  1316. p = end - 1;
  1317. indicate_error('1');
  1318. }
  1319. if (p < text) {
  1320. p = text;
  1321. indicate_error('2');
  1322. }
  1323. return p;
  1324. }
  1325. //----- Helper Utility Routines --------------------------------
  1326. //----------------------------------------------------------------
  1327. //----- Char Routines --------------------------------------------
  1328. /* Chars that are part of a word-
  1329. * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
  1330. * Chars that are Not part of a word (stoppers)
  1331. * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
  1332. * Chars that are WhiteSpace
  1333. * TAB NEWLINE VT FF RETURN SPACE
  1334. * DO NOT COUNT NEWLINE AS WHITESPACE
  1335. */
  1336. static char *new_screen(int ro, int co)
  1337. {
  1338. int li;
  1339. free(screen);
  1340. screensize = ro * co + 8;
  1341. screen = xmalloc(screensize);
  1342. // initialize the new screen. assume this will be a empty file.
  1343. screen_erase();
  1344. // non-existent text[] lines start with a tilde (~).
  1345. for (li = 1; li < ro - 1; li++) {
  1346. screen[(li * co) + 0] = '~';
  1347. }
  1348. return screen;
  1349. }
  1350. #if ENABLE_FEATURE_VI_SEARCH
  1351. static int mycmp(const char * s1, const char * s2, int len)
  1352. {
  1353. int i;
  1354. i = strncmp(s1, s2, len);
  1355. if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) {
  1356. i = strncasecmp(s1, s2, len);
  1357. }
  1358. return i;
  1359. }
  1360. // search for pattern starting at p
  1361. static char *char_search(char * p, const char * pat, int dir, int range)
  1362. {
  1363. #ifndef REGEX_SEARCH
  1364. char *start, *stop;
  1365. int len;
  1366. len = strlen(pat);
  1367. if (dir == FORWARD) {
  1368. stop = end - 1; // assume range is p - end-1
  1369. if (range == LIMITED)
  1370. stop = next_line(p); // range is to next line
  1371. for (start = p; start < stop; start++) {
  1372. if (mycmp(start, pat, len) == 0) {
  1373. return start;
  1374. }
  1375. }
  1376. } else if (dir == BACK) {
  1377. stop = text; // assume range is text - p
  1378. if (range == LIMITED)
  1379. stop = prev_line(p); // range is to prev line
  1380. for (start = p - len; start >= stop; start--) {
  1381. if (mycmp(start, pat, len) == 0) {
  1382. return start;
  1383. }
  1384. }
  1385. }
  1386. // pattern not found
  1387. return NULL;
  1388. #else /* REGEX_SEARCH */
  1389. char *q;
  1390. struct re_pattern_buffer preg;
  1391. int i;
  1392. int size, range;
  1393. re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
  1394. preg.translate = 0;
  1395. preg.fastmap = 0;
  1396. preg.buffer = 0;
  1397. preg.allocated = 0;
  1398. // assume a LIMITED forward search
  1399. q = next_line(p);
  1400. q = end_line(q);
  1401. q = end - 1;
  1402. if (dir == BACK) {
  1403. q = prev_line(p);
  1404. q = text;
  1405. }
  1406. // count the number of chars to search over, forward or backward
  1407. size = q - p;
  1408. if (size < 0)
  1409. size = p - q;
  1410. // RANGE could be negative if we are searching backwards
  1411. range = q - p;
  1412. q = re_compile_pattern(pat, strlen(pat), &preg);
  1413. if (q != 0) {
  1414. // The pattern was not compiled
  1415. psbs("bad search pattern: \"%s\": %s", pat, q);
  1416. i = 0; // return p if pattern not compiled
  1417. goto cs1;
  1418. }
  1419. q = p;
  1420. if (range < 0) {
  1421. q = p - size;
  1422. if (q < text)
  1423. q = text;
  1424. }
  1425. // search for the compiled pattern, preg, in p[]
  1426. // range < 0- search backward
  1427. // range > 0- search forward
  1428. // 0 < start < size
  1429. // re_search() < 0 not found or error
  1430. // re_search() > 0 index of found pattern
  1431. // struct pattern char int int int struct reg
  1432. // re_search (*pattern_buffer, *string, size, start, range, *regs)
  1433. i = re_search(&preg, q, size, 0, range, 0);
  1434. if (i == -1) {
  1435. p = 0;
  1436. i = 0; // return NULL if pattern not found
  1437. }
  1438. cs1:
  1439. if (dir == FORWARD) {
  1440. p = p + i;
  1441. } else {
  1442. p = p - i;
  1443. }
  1444. return p;
  1445. #endif /* REGEX_SEARCH */
  1446. }
  1447. #endif /* FEATURE_VI_SEARCH */
  1448. static char *char_insert(char * p, char c) // insert the char c at 'p'
  1449. {
  1450. if (c == 22) { // Is this an ctrl-V?
  1451. p = stupid_insert(p, '^'); // use ^ to indicate literal next
  1452. p--; // backup onto ^
  1453. refresh(FALSE); // show the ^
  1454. c = get_one_char();
  1455. *p = c;
  1456. p++;
  1457. file_modified++; // has the file been modified
  1458. } else if (c == 27) { // Is this an ESC?
  1459. cmd_mode = 0;
  1460. cmdcnt = 0;
  1461. end_cmd_q(); // stop adding to q
  1462. last_status_cksum = 0; // force status update
  1463. if ((p[-1] != '\n') && (dot > text)) {
  1464. p--;
  1465. }
  1466. } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
  1467. // 123456789
  1468. if ((p[-1] != '\n') && (dot>text)) {
  1469. p--;
  1470. p = text_hole_delete(p, p); // shrink buffer 1 char
  1471. }
  1472. } else {
  1473. // insert a char into text[]
  1474. char *sp; // "save p"
  1475. if (c == 13)
  1476. c = '\n'; // translate \r to \n
  1477. sp = p; // remember addr of insert
  1478. p = stupid_insert(p, c); // insert the char
  1479. #if ENABLE_FEATURE_VI_SETOPTS
  1480. if (showmatch && strchr(")]}", *sp) != NULL) {
  1481. showmatching(sp);
  1482. }
  1483. if (autoindent && c == '\n') { // auto indent the new line
  1484. char *q;
  1485. q = prev_line(p); // use prev line as templet
  1486. for (; isblank(*q); q++) {
  1487. p = stupid_insert(p, *q); // insert the char
  1488. }
  1489. }
  1490. #endif
  1491. }
  1492. return p;
  1493. }
  1494. static char *stupid_insert(char * p, char c) // stupidly insert the char c at 'p'
  1495. {
  1496. p = text_hole_make(p, 1);
  1497. if (p != 0) {
  1498. *p = c;
  1499. file_modified++; // has the file been modified
  1500. p++;
  1501. }
  1502. return p;
  1503. }
  1504. static char find_range(char ** start, char ** stop, char c)
  1505. {
  1506. char *save_dot, *p, *q;
  1507. int cnt;
  1508. save_dot = dot;
  1509. p = q = dot;
  1510. if (strchr("cdy><", c)) {
  1511. // these cmds operate on whole lines
  1512. p = q = begin_line(p);
  1513. for (cnt = 1; cnt < cmdcnt; cnt++) {
  1514. q = next_line(q);
  1515. }
  1516. q = end_line(q);
  1517. } else if (strchr("^%$0bBeEft", c)) {
  1518. // These cmds operate on char positions
  1519. do_cmd(c); // execute movement cmd
  1520. q = dot;
  1521. } else if (strchr("wW", c)) {
  1522. do_cmd(c); // execute movement cmd
  1523. // if we are at the next word's first char
  1524. // step back one char
  1525. // but check the possibilities when it is true
  1526. if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
  1527. || (ispunct(dot[-1]) && !ispunct(dot[0]))
  1528. || (isalnum(dot[-1]) && !isalnum(dot[0]))))
  1529. dot--; // move back off of next word
  1530. if (dot > text && *dot == '\n')
  1531. dot--; // stay off NL
  1532. q = dot;
  1533. } else if (strchr("H-k{", c)) {
  1534. // these operate on multi-lines backwards
  1535. q = end_line(dot); // find NL
  1536. do_cmd(c); // execute movement cmd
  1537. dot_begin();
  1538. p = dot;
  1539. } else if (strchr("L+j}\r\n", c)) {
  1540. // these operate on multi-lines forwards
  1541. p = begin_line(dot);
  1542. do_cmd(c); // execute movement cmd
  1543. dot_end(); // find NL
  1544. q = dot;
  1545. } else {
  1546. c = 27; // error- return an ESC char
  1547. //break;
  1548. }
  1549. *start = p;
  1550. *stop = q;
  1551. if (q < p) {
  1552. *start = q;
  1553. *stop = p;
  1554. }
  1555. dot = save_dot;
  1556. return c;
  1557. }
  1558. static int st_test(char * p, int type, int dir, char * tested)
  1559. {
  1560. char c, c0, ci;
  1561. int test, inc;
  1562. inc = dir;
  1563. c = c0 = p[0];
  1564. ci = p[inc];
  1565. test = 0;
  1566. if (type == S_BEFORE_WS) {
  1567. c = ci;
  1568. test = ((!isspace(c)) || c == '\n');
  1569. }
  1570. if (type == S_TO_WS) {
  1571. c = c0;
  1572. test = ((!isspace(c)) || c == '\n');
  1573. }
  1574. if (type == S_OVER_WS) {
  1575. c = c0;
  1576. test = ((isspace(c)));
  1577. }
  1578. if (type == S_END_PUNCT) {
  1579. c = ci;
  1580. test = ((ispunct(c)));
  1581. }
  1582. if (type == S_END_ALNUM) {
  1583. c = ci;
  1584. test = ((isalnum(c)) || c == '_');
  1585. }
  1586. *tested = c;
  1587. return test;
  1588. }
  1589. static char *skip_thing(char * p, int linecnt, int dir, int type)
  1590. {
  1591. char c;
  1592. while (st_test(p, type, dir, &c)) {
  1593. // make sure we limit search to correct number of lines
  1594. if (c == '\n' && --linecnt < 1)
  1595. break;
  1596. if (dir >= 0 && p >= end - 1)
  1597. break;
  1598. if (dir < 0 && p <= text)
  1599. break;
  1600. p += dir; // move to next char
  1601. }
  1602. return p;
  1603. }
  1604. // find matching char of pair () [] {}
  1605. static char *find_pair(char * p, const char c)
  1606. {
  1607. char match, *q;
  1608. int dir, level;
  1609. match = ')';
  1610. level = 1;
  1611. dir = 1; // assume forward
  1612. switch (c) {
  1613. case '(':
  1614. match = ')';
  1615. break;
  1616. case '[':
  1617. match = ']';
  1618. break;
  1619. case '{':
  1620. match = '}';
  1621. break;
  1622. case ')':
  1623. match = '(';
  1624. dir = -1;
  1625. break;
  1626. case ']':
  1627. match = '[';
  1628. dir = -1;
  1629. break;
  1630. case '}':
  1631. match = '{';
  1632. dir = -1;
  1633. break;
  1634. }
  1635. for (q = p + dir; text <= q && q < end; q += dir) {
  1636. // look for match, count levels of pairs (( ))
  1637. if (*q == c)
  1638. level++; // increase pair levels
  1639. if (*q == match)
  1640. level--; // reduce pair level
  1641. if (level == 0)
  1642. break; // found matching pair
  1643. }
  1644. if (level != 0)
  1645. q = NULL; // indicate no match
  1646. return q;
  1647. }
  1648. #if ENABLE_FEATURE_VI_SETOPTS
  1649. // show the matching char of a pair, () [] {}
  1650. static void showmatching(char * p)
  1651. {
  1652. char *q, *save_dot;
  1653. // we found half of a pair
  1654. q = find_pair(p, *p); // get loc of matching char
  1655. if (q == NULL) {
  1656. indicate_error('3'); // no matching char
  1657. } else {
  1658. // "q" now points to matching pair
  1659. save_dot = dot; // remember where we are
  1660. dot = q; // go to new loc
  1661. refresh(FALSE); // let the user see it
  1662. mysleep(40); // give user some time
  1663. dot = save_dot; // go back to old loc
  1664. refresh(FALSE);
  1665. }
  1666. }
  1667. #endif /* FEATURE_VI_SETOPTS */
  1668. // open a hole in text[]
  1669. static char *text_hole_make(char * p, int size) // at "p", make a 'size' byte hole
  1670. {
  1671. char *src, *dest;
  1672. int cnt;
  1673. if (size <= 0)
  1674. goto thm0;
  1675. src = p;
  1676. dest = p + size;
  1677. cnt = end - src; // the rest of buffer
  1678. if ( ((end + size) >= (text + text_size)) // TODO: realloc here
  1679. || memmove(dest, src, cnt) != dest) {
  1680. psbs("can't create room for new characters");
  1681. p = NULL;
  1682. goto thm0;
  1683. }
  1684. memset(p, ' ', size); // clear new hole
  1685. end += size; // adjust the new END
  1686. file_modified++; // has the file been modified
  1687. thm0:
  1688. return p;
  1689. }
  1690. // close a hole in text[]
  1691. static char *text_hole_delete(char * p, char * q) // delete "p" thru "q", inclusive
  1692. {
  1693. char *src, *dest;
  1694. int cnt, hole_size;
  1695. // move forwards, from beginning
  1696. // assume p <= q
  1697. src = q + 1;
  1698. dest = p;
  1699. if (q < p) { // they are backward- swap them
  1700. src = p + 1;
  1701. dest = q;
  1702. }
  1703. hole_size = q - p + 1;
  1704. cnt = end - src;
  1705. if (src < text || src > end)
  1706. goto thd0;
  1707. if (dest < text || dest >= end)
  1708. goto thd0;
  1709. if (src >= end)
  1710. goto thd_atend; // just delete the end of the buffer
  1711. if (memmove(dest, src, cnt) != dest) {
  1712. psbs("can't delete the character");
  1713. }
  1714. thd_atend:
  1715. end = end - hole_size; // adjust the new END
  1716. if (dest >= end)
  1717. dest = end - 1; // make sure dest in below end-1
  1718. if (end <= text)
  1719. dest = end = text; // keep pointers valid
  1720. file_modified++; // has the file been modified
  1721. thd0:
  1722. return dest;
  1723. }
  1724. // copy text into register, then delete text.
  1725. // if dist <= 0, do not include, or go past, a NewLine
  1726. //
  1727. static char *yank_delete(char * start, char * stop, int dist, int yf)
  1728. {
  1729. char *p;
  1730. // make sure start <= stop
  1731. if (start > stop) {
  1732. // they are backwards, reverse them
  1733. p = start;
  1734. start = stop;
  1735. stop = p;
  1736. }
  1737. if (dist <= 0) {
  1738. // we cannot cross NL boundaries
  1739. p = start;
  1740. if (*p == '\n')
  1741. return p;
  1742. // dont go past a NewLine
  1743. for (; p + 1 <= stop; p++) {
  1744. if (p[1] == '\n') {
  1745. stop = p; // "stop" just before NewLine
  1746. break;
  1747. }
  1748. }
  1749. }
  1750. p = start;
  1751. #if ENABLE_FEATURE_VI_YANKMARK
  1752. text_yank(start, stop, YDreg);
  1753. #endif
  1754. if (yf == YANKDEL) {
  1755. p = text_hole_delete(start, stop);
  1756. } // delete lines
  1757. return p;
  1758. }
  1759. static void show_help(void)
  1760. {
  1761. puts("These features are available:"
  1762. #if ENABLE_FEATURE_VI_SEARCH
  1763. "\n\tPattern searches with / and ?"
  1764. #endif
  1765. #if ENABLE_FEATURE_VI_DOT_CMD
  1766. "\n\tLast command repeat with \'.\'"
  1767. #endif
  1768. #if ENABLE_FEATURE_VI_YANKMARK
  1769. "\n\tLine marking with 'x"
  1770. "\n\tNamed buffers with \"x"
  1771. #endif
  1772. #if ENABLE_FEATURE_VI_READONLY
  1773. "\n\tReadonly if vi is called as \"view\""
  1774. "\n\tReadonly with -R command line arg"
  1775. #endif
  1776. #if ENABLE_FEATURE_VI_SET
  1777. "\n\tSome colon mode commands with \':\'"
  1778. #endif
  1779. #if ENABLE_FEATURE_VI_SETOPTS
  1780. "\n\tSettable options with \":set\""
  1781. #endif
  1782. #if ENABLE_FEATURE_VI_USE_SIGNALS
  1783. "\n\tSignal catching- ^C"
  1784. "\n\tJob suspend and resume with ^Z"
  1785. #endif
  1786. #if ENABLE_FEATURE_VI_WIN_RESIZE
  1787. "\n\tAdapt to window re-sizes"
  1788. #endif
  1789. );
  1790. }
  1791. static inline void print_literal(char * buf, const char * s) // copy s to buf, convert unprintable
  1792. {
  1793. unsigned char c;
  1794. char b[2];
  1795. b[1] = '\0';
  1796. buf[0] = '\0';
  1797. if (!s[0])
  1798. s = "(NULL)";
  1799. for (; *s; s++) {
  1800. int c_is_no_print;
  1801. c = *s;
  1802. c_is_no_print = (c & 0x80) && !Isprint(c);
  1803. if (c_is_no_print) {
  1804. strcat(buf, SOn);
  1805. c = '.';
  1806. }
  1807. if (c < ' ' || c == 127) {
  1808. strcat(buf, "^");
  1809. if (c == 127)
  1810. c = '?';
  1811. else
  1812. c += '@';
  1813. }
  1814. b[0] = c;
  1815. strcat(buf, b);
  1816. if (c_is_no_print)
  1817. strcat(buf, SOs);
  1818. if (*s == '\n')
  1819. strcat(buf, "$");
  1820. }
  1821. }
  1822. #if ENABLE_FEATURE_VI_DOT_CMD
  1823. static void start_new_cmd_q(char c)
  1824. {
  1825. // release old cmd
  1826. free(last_modifying_cmd);
  1827. // get buffer for new cmd
  1828. last_modifying_cmd = xzalloc(MAX_LINELEN);
  1829. // if there is a current cmd count put it in the buffer first
  1830. if (cmdcnt > 0)
  1831. sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
  1832. else // just save char c onto queue
  1833. last_modifying_cmd[0] = c;
  1834. adding2q = 1;
  1835. }
  1836. static void end_cmd_q(void)
  1837. {
  1838. #if ENABLE_FEATURE_VI_YANKMARK
  1839. YDreg = 26; // go back to default Yank/Delete reg
  1840. #endif
  1841. adding2q = 0;
  1842. }
  1843. #endif /* FEATURE_VI_DOT_CMD */
  1844. #if ENABLE_FEATURE_VI_YANKMARK \
  1845. || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
  1846. || ENABLE_FEATURE_VI_CRASHME
  1847. static char *string_insert(char * p, char * s) // insert the string at 'p'
  1848. {
  1849. int cnt, i;
  1850. i = strlen(s);
  1851. if (text_hole_make(p, i)) {
  1852. strncpy(p, s, i);
  1853. for (cnt = 0; *s != '\0'; s++) {
  1854. if (*s == '\n')
  1855. cnt++;
  1856. }
  1857. #if ENABLE_FEATURE_VI_YANKMARK
  1858. psb("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
  1859. #endif
  1860. }
  1861. return p;
  1862. }
  1863. #endif
  1864. #if ENABLE_FEATURE_VI_YANKMARK
  1865. static char *text_yank(char * p, char * q, int dest) // copy text into a register
  1866. {
  1867. char *t;
  1868. int cnt;
  1869. if (q < p) { // they are backwards- reverse them
  1870. t = q;
  1871. q = p;
  1872. p = t;
  1873. }
  1874. cnt = q - p + 1;
  1875. t = reg[dest];
  1876. free(t); // if already a yank register, free it
  1877. t = xmalloc(cnt + 1); // get a new register
  1878. memset(t, '\0', cnt + 1); // clear new text[]
  1879. strncpy(t, p, cnt); // copy text[] into bufer
  1880. reg[dest] = t;
  1881. return p;
  1882. }
  1883. static char what_reg(void)
  1884. {
  1885. char c;
  1886. c = 'D'; // default to D-reg
  1887. if (0 <= YDreg && YDreg <= 25)
  1888. c = 'a' + (char) YDreg;
  1889. if (YDreg == 26)
  1890. c = 'D';
  1891. if (YDreg == 27)
  1892. c = 'U';
  1893. return c;
  1894. }
  1895. static void check_context(char cmd)
  1896. {
  1897. // A context is defined to be "modifying text"
  1898. // Any modifying command establishes a new context.
  1899. if (dot < context_start || dot > context_end) {
  1900. if (strchr(modifying_cmds, cmd) != NULL) {
  1901. // we are trying to modify text[]- make this the current context
  1902. mark[27] = mark[26]; // move cur to prev
  1903. mark[26] = dot; // move local to cur
  1904. context_start = prev_line(prev_line(dot));
  1905. context_end = next_line(next_line(dot));
  1906. //loiter= start_loiter= now;
  1907. }
  1908. }
  1909. }
  1910. static inline char *swap_context(char * p) // goto new context for '' command make this the current context
  1911. {
  1912. char *tmp;
  1913. // the current context is in mark[26]
  1914. // the previous context is in mark[27]
  1915. // only swap context if other context is valid
  1916. if (text <= mark[27] && mark[27] <= end - 1) {
  1917. tmp = mark[27];
  1918. mark[27] = mark[26];
  1919. mark[26] = tmp;
  1920. p = mark[26]; // where we are going- previous context
  1921. context_start = prev_line(prev_line(prev_line(p)));
  1922. context_end = next_line(next_line(next_line(p)));
  1923. }
  1924. return p;
  1925. }
  1926. #endif /* FEATURE_VI_YANKMARK */
  1927. //----- Set terminal attributes --------------------------------
  1928. static void rawmode(void)
  1929. {
  1930. tcgetattr(0, &term_orig);
  1931. term_vi = term_orig;
  1932. term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
  1933. term_vi.c_iflag &= (~IXON & ~ICRNL);
  1934. term_vi.c_oflag &= (~ONLCR);
  1935. term_vi.c_cc[VMIN] = 1;
  1936. term_vi.c_cc[VTIME] = 0;
  1937. erase_char = term_vi.c_cc[VERASE];
  1938. tcsetattr(0, TCSANOW, &term_vi);
  1939. }
  1940. static void cookmode(void)
  1941. {
  1942. fflush(stdout);
  1943. tcsetattr(0, TCSANOW, &term_orig);
  1944. }
  1945. //----- Come here when we get a window resize signal ---------
  1946. #if ENABLE_FEATURE_VI_USE_SIGNALS
  1947. static void winch_sig(int sig ATTRIBUTE_UNUSED)
  1948. {
  1949. signal(SIGWINCH, winch_sig);
  1950. if (ENABLE_FEATURE_VI_WIN_RESIZE)
  1951. get_terminal_width_height(0, &columns, &rows);
  1952. new_screen(rows, columns); // get memory for virtual screen
  1953. redraw(TRUE); // re-draw the screen
  1954. }
  1955. //----- Come here when we get a continue signal -------------------
  1956. static void cont_sig(int sig ATTRIBUTE_UNUSED)
  1957. {
  1958. rawmode(); // terminal to "raw"
  1959. last_status_cksum = 0; // force status update
  1960. redraw(TRUE); // re-draw the screen
  1961. signal(SIGTSTP, suspend_sig);
  1962. signal(SIGCONT, SIG_DFL);
  1963. kill(my_pid, SIGCONT);
  1964. }
  1965. //----- Come here when we get a Suspend signal -------------------
  1966. static void suspend_sig(int sig ATTRIBUTE_UNUSED)
  1967. {
  1968. place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
  1969. clear_to_eol(); // Erase to end of line
  1970. cookmode(); // terminal to "cooked"
  1971. signal(SIGCONT, cont_sig);
  1972. signal(SIGTSTP, SIG_DFL);
  1973. kill(my_pid, SIGTSTP);
  1974. }
  1975. //----- Come here when we get a signal ---------------------------
  1976. static void catch_sig(int sig)
  1977. {
  1978. signal(SIGINT, catch_sig);
  1979. if (sig)
  1980. longjmp(restart, sig);
  1981. }
  1982. #endif /* FEATURE_VI_USE_SIGNALS */
  1983. static int mysleep(int hund) // sleep for 'h' 1/100 seconds
  1984. {
  1985. struct pollfd pfd[1];
  1986. pfd[0].fd = 0;
  1987. pfd[0].events = POLLIN;
  1988. return safe_poll(pfd, 1, hund*10) > 0;
  1989. }
  1990. static int readed_for_parse;
  1991. //----- IO Routines --------------------------------------------
  1992. static char readit(void) // read (maybe cursor) key from stdin
  1993. {
  1994. char c;
  1995. int n;
  1996. struct esc_cmds {
  1997. const char seq[4];
  1998. char val;
  1999. };
  2000. static const struct esc_cmds esccmds[] = {
  2001. {"OA", VI_K_UP}, // cursor key Up
  2002. {"OB", VI_K_DOWN}, // cursor key Down
  2003. {"OC", VI_K_RIGHT}, // Cursor Key Right
  2004. {"OD", VI_K_LEFT}, // cursor key Left
  2005. {"OH", VI_K_HOME}, // Cursor Key Home
  2006. {"OF", VI_K_END}, // Cursor Key End
  2007. {"[A", VI_K_UP}, // cursor key Up
  2008. {"[B", VI_K_DOWN}, // cursor key Down
  2009. {"[C", VI_K_RIGHT}, // Cursor Key Right
  2010. {"[D", VI_K_LEFT}, // cursor key Left
  2011. {"[H", VI_K_HOME}, // Cursor Key Home
  2012. {"[F", VI_K_END}, // Cursor Key End
  2013. {"[1~", VI_K_HOME}, // Cursor Key Home
  2014. {"[2~", VI_K_INSERT}, // Cursor Key Insert
  2015. {"[4~", VI_K_END}, // Cursor Key End
  2016. {"[5~", VI_K_PAGEUP}, // Cursor Key Page Up
  2017. {"[6~", VI_K_PAGEDOWN},// Cursor Key Page Down
  2018. {"OP", VI_K_FUN1}, // Function Key F1
  2019. {"OQ", VI_K_FUN2}, // Function Key F2
  2020. {"OR", VI_K_FUN3}, // Function Key F3
  2021. {"OS", VI_K_FUN4}, // Function Key F4
  2022. // careful: these have no terminating NUL!
  2023. {"[15~", VI_K_FUN5}, // Function Key F5
  2024. {"[17~", VI_K_FUN6}, // Function Key F6
  2025. {"[18~", VI_K_FUN7}, // Function Key F7
  2026. {"[19~", VI_K_FUN8}, // Function Key F8
  2027. {"[20~", VI_K_FUN9}, // Function Key F9
  2028. {"[21~", VI_K_FUN10}, // Function Key F10
  2029. {"[23~", VI_K_FUN11}, // Function Key F11
  2030. {"[24~", VI_K_FUN12}, // Function Key F12
  2031. {"[11~", VI_K_FUN1}, // Function Key F1
  2032. {"[12~", VI_K_FUN2}, // Function Key F2
  2033. {"[13~", VI_K_FUN3}, // Function Key F3
  2034. {"[14~", VI_K_FUN4}, // Function Key F4
  2035. };
  2036. enum { ESCCMDS_COUNT = ARRAY_SIZE(esccmds) };
  2037. alarm(0); // turn alarm OFF while we wait for input
  2038. fflush(stdout);
  2039. n = readed_for_parse;
  2040. // get input from User- are there already input chars in Q?
  2041. if (n <= 0) {
  2042. // the Q is empty, wait for a typed char
  2043. n = safe_read(0, readbuffer, MAX_LINELEN - 1);
  2044. if (n < 0) {
  2045. if (errno == EBADF || errno == EFAULT || errno == EINVAL
  2046. || errno == EIO)
  2047. editing = 0;
  2048. errno = 0;
  2049. }
  2050. if (n <= 0)
  2051. return 0; // error
  2052. if (readbuffer[0] == 27) {
  2053. // This is an ESC char. Is this Esc sequence?
  2054. // Could be bare Esc key. See if there are any
  2055. // more chars to read after the ESC. This would
  2056. // be a Function or Cursor Key sequence.
  2057. struct pollfd pfd[1];
  2058. pfd[0].fd = 0;
  2059. pfd[0].events = POLLIN;
  2060. // keep reading while there are input chars and room in buffer
  2061. while (safe_poll(pfd, 1, 0) > 0 && n <= (MAX_LINELEN - 5)) {
  2062. // read the rest of the ESC string
  2063. int r = safe_read(0, readbuffer + n, MAX_LINELEN - n);
  2064. if (r > 0)
  2065. n += r;
  2066. }
  2067. }
  2068. readed_for_parse = n;
  2069. }
  2070. c = readbuffer[0];
  2071. if (c == 27 && n > 1) {
  2072. // Maybe cursor or function key?
  2073. const struct esc_cmds *eindex;
  2074. for (eindex = esccmds; eindex < &esccmds[ESCCMDS_COUNT]; eindex++) {
  2075. int cnt = strnlen(eindex->seq, 4);
  2076. if (n <= cnt)
  2077. continue;
  2078. if (strncmp(eindex->seq, readbuffer + 1, cnt))
  2079. continue;
  2080. // is a Cursor key- put derived value back into Q
  2081. c = eindex->val;
  2082. // for squeeze out the ESC sequence
  2083. n = cnt + 1;
  2084. break;
  2085. }
  2086. if (eindex == &esccmds[ESCCMDS_COUNT]) {
  2087. /* defined ESC sequence not found, set only one ESC */
  2088. n = 1;
  2089. }
  2090. } else {
  2091. n = 1;
  2092. }
  2093. // remove key sequence from Q
  2094. readed_for_parse -= n;
  2095. memmove(readbuffer, readbuffer + n, MAX_LINELEN - n);
  2096. alarm(3); // we are done waiting for input, turn alarm ON
  2097. return c;
  2098. }
  2099. //----- IO Routines --------------------------------------------
  2100. static char get_one_char(void)
  2101. {
  2102. static char c;
  2103. #if ENABLE_FEATURE_VI_DOT_CMD
  2104. // ! adding2q && ioq == 0 read()
  2105. // ! adding2q && ioq != 0 *ioq
  2106. // adding2q *last_modifying_cmd= read()
  2107. if (!adding2q) {
  2108. // we are not adding to the q.
  2109. // but, we may be reading from a q
  2110. if (ioq == 0) {
  2111. // there is no current q, read from STDIN
  2112. c = readit(); // get the users input
  2113. } else {
  2114. // there is a queue to get chars from first
  2115. c = *ioq++;
  2116. if (c == '\0') {
  2117. // the end of the q, read from STDIN
  2118. free(ioq_start);
  2119. ioq_start = ioq = 0;
  2120. c = readit(); // get the users input
  2121. }
  2122. }
  2123. } else {
  2124. // adding STDIN chars to q
  2125. c = readit(); // get the users input
  2126. if (last_modifying_cmd != 0) {
  2127. int len = strlen(last_modifying_cmd);
  2128. if (len >= MAX_LINELEN - 1) {
  2129. psbs("last_modifying_cmd overrun");
  2130. } else {
  2131. // add new char to q
  2132. last_modifying_cmd[len] = c;
  2133. }
  2134. }
  2135. }
  2136. #else
  2137. c = readit(); // get the users input
  2138. #endif /* FEATURE_VI_DOT_CMD */
  2139. return c; // return the char, where ever it came from
  2140. }
  2141. static char *get_input_line(const char * prompt) // get input line- use "status line"
  2142. {
  2143. static char *obufp;
  2144. char buf[MAX_LINELEN];
  2145. char c;
  2146. int i;
  2147. strcpy(buf, prompt);
  2148. last_status_cksum = 0; // force status update
  2149. place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
  2150. clear_to_eol(); // clear the line
  2151. write1(prompt); // write out the :, /, or ? prompt
  2152. i = strlen(buf);
  2153. while (i < MAX_LINELEN) {
  2154. c = get_one_char(); // read user input
  2155. if (c == '\n' || c == '\r' || c == 27)
  2156. break; // is this end of input
  2157. if (c == erase_char || c == 8 || c == 127) {
  2158. // user wants to erase prev char
  2159. i--; // backup to prev char
  2160. buf[i] = '\0'; // erase the char
  2161. buf[i + 1] = '\0'; // null terminate buffer
  2162. write1("\b \b"); // erase char on screen
  2163. if (i <= 0) { // user backs up before b-o-l, exit
  2164. break;
  2165. }
  2166. } else {
  2167. buf[i] = c; // save char in buffer
  2168. buf[i + 1] = '\0'; // make sure buffer is null terminated
  2169. bb_putchar(c); // echo the char back to user
  2170. i++;
  2171. }
  2172. }
  2173. refresh(FALSE);
  2174. free(obufp);
  2175. obufp = xstrdup(buf);
  2176. return obufp;
  2177. }
  2178. static int file_size(const char *fn) // what is the byte size of "fn"
  2179. {
  2180. struct stat st_buf;
  2181. int cnt;
  2182. cnt = -1;
  2183. if (fn && fn[0] && stat(fn, &st_buf) == 0) // see if file exists
  2184. cnt = (int) st_buf.st_size;
  2185. return cnt;
  2186. }
  2187. static int file_insert(const char * fn, char *p
  2188. USE_FEATURE_VI_READONLY(, int update_ro_status))
  2189. {
  2190. int cnt = -1;
  2191. int fd, size;
  2192. struct stat statbuf;
  2193. /* Validate file */
  2194. if (stat(fn, &statbuf) < 0) {
  2195. psbs("\"%s\" %s", fn, strerror(errno));
  2196. goto fi0;
  2197. }
  2198. if ((statbuf.st_mode & S_IFREG) == 0) {
  2199. // This is not a regular file
  2200. psbs("\"%s\" Not a regular file", fn);
  2201. goto fi0;
  2202. }
  2203. /* // this check is done by open()
  2204. if ((statbuf.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == 0) {
  2205. // dont have any read permissions
  2206. psbs("\"%s\" Not readable", fn);
  2207. goto fi0;
  2208. }
  2209. */
  2210. if (p < text || p > end) {
  2211. psbs("Trying to insert file outside of memory");
  2212. goto fi0;
  2213. }
  2214. // read file to buffer
  2215. fd = open(fn, O_RDONLY);
  2216. if (fd < 0) {
  2217. psbs("\"%s\" %s", fn, strerror(errno));
  2218. goto fi0;
  2219. }
  2220. size = statbuf.st_size;
  2221. p = text_hole_make(p, size);
  2222. if (p == NULL)
  2223. goto fi0;
  2224. cnt = safe_read(fd, p, size);
  2225. if (cnt < 0) {
  2226. psbs("\"%s\" %s", fn, strerror(errno));
  2227. p = text_hole_delete(p, p + size - 1); // un-do buffer insert
  2228. } else if (cnt < size) {
  2229. // There was a partial read, shrink unused space text[]
  2230. p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
  2231. psbs("cannot read all of file \"%s\"", fn);
  2232. }
  2233. if (cnt >= size)
  2234. file_modified++;
  2235. close(fd);
  2236. fi0:
  2237. #if ENABLE_FEATURE_VI_READONLY
  2238. if (update_ro_status
  2239. && ((access(fn, W_OK) < 0) ||
  2240. /* root will always have access()
  2241. * so we check fileperms too */
  2242. !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
  2243. )
  2244. ) {
  2245. SET_READONLY_FILE(readonly_mode);
  2246. }
  2247. #endif
  2248. return cnt;
  2249. }
  2250. static int file_write(char * fn, char * first, char * last)
  2251. {
  2252. int fd, cnt, charcnt;
  2253. if (fn == 0) {
  2254. psbs("No current filename");
  2255. return -2;
  2256. }
  2257. charcnt = 0;
  2258. // FIXIT- use the correct umask()
  2259. fd = open(fn, (O_WRONLY | O_CREAT | O_TRUNC), 0664);
  2260. if (fd < 0)
  2261. return -1;
  2262. cnt = last - first + 1;
  2263. charcnt = write(fd, first, cnt);
  2264. if (charcnt == cnt) {
  2265. // good write
  2266. //file_modified = FALSE; // the file has not been modified
  2267. } else {
  2268. charcnt = 0;
  2269. }
  2270. close(fd);
  2271. return charcnt;
  2272. }
  2273. //----- Terminal Drawing ---------------------------------------
  2274. // The terminal is made up of 'rows' line of 'columns' columns.
  2275. // classically this would be 24 x 80.
  2276. // screen coordinates
  2277. // 0,0 ... 0,79
  2278. // 1,0 ... 1,79
  2279. // . ... .
  2280. // . ... .
  2281. // 22,0 ... 22,79
  2282. // 23,0 ... 23,79 status line
  2283. //
  2284. //----- Move the cursor to row x col (count from 0, not 1) -------
  2285. static void place_cursor(int row, int col, int opti)
  2286. {
  2287. char cm1[MAX_LINELEN];
  2288. char *cm;
  2289. #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
  2290. char cm2[MAX_LINELEN];
  2291. char *screenp;
  2292. // char cm3[MAX_LINELEN];
  2293. int Rrow = last_row;
  2294. #endif
  2295. memset(cm1, '\0', MAX_LINELEN); // clear the buffer
  2296. if (row < 0) row = 0;
  2297. if (row >= rows) row = rows - 1;
  2298. if (col < 0) col = 0;
  2299. if (col >= columns) col = columns - 1;
  2300. //----- 1. Try the standard terminal ESC sequence
  2301. sprintf(cm1, CMrc, row + 1, col + 1);
  2302. cm = cm1;
  2303. if (!opti)
  2304. goto pc0;
  2305. #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
  2306. //----- find the minimum # of chars to move cursor -------------
  2307. //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
  2308. memset(cm2, '\0', MAX_LINELEN); // clear the buffer
  2309. // move to the correct row
  2310. while (row < Rrow) {
  2311. // the cursor has to move up
  2312. strcat(cm2, CMup);
  2313. Rrow--;
  2314. }
  2315. while (row > Rrow) {
  2316. // the cursor has to move down
  2317. strcat(cm2, CMdown);
  2318. Rrow++;
  2319. }
  2320. // now move to the correct column
  2321. strcat(cm2, "\r"); // start at col 0
  2322. // just send out orignal source char to get to correct place
  2323. screenp = &screen[row * columns]; // start of screen line
  2324. strncat(cm2, screenp, col);
  2325. //----- 3. Try some other way of moving cursor
  2326. //---------------------------------------------
  2327. // pick the shortest cursor motion to send out
  2328. cm = cm1;
  2329. if (strlen(cm2) < strlen(cm)) {
  2330. cm = cm2;
  2331. } /* else if (strlen(cm3) < strlen(cm)) {
  2332. cm= cm3;
  2333. } */
  2334. #endif /* FEATURE_VI_OPTIMIZE_CURSOR */
  2335. pc0:
  2336. write1(cm); // move the cursor
  2337. }
  2338. //----- Erase from cursor to end of line -----------------------
  2339. static void clear_to_eol(void)
  2340. {
  2341. write1(Ceol); // Erase from cursor to end of line
  2342. }
  2343. //----- Erase from cursor to end of screen -----------------------
  2344. static void clear_to_eos(void)
  2345. {
  2346. write1(Ceos); // Erase from cursor to end of screen
  2347. }
  2348. //----- Start standout mode ------------------------------------
  2349. static void standout_start(void) // send "start reverse video" sequence
  2350. {
  2351. write1(SOs); // Start reverse video mode
  2352. }
  2353. //----- End standout mode --------------------------------------
  2354. static void standout_end(void) // send "end reverse video" sequence
  2355. {
  2356. write1(SOn); // End reverse video mode
  2357. }
  2358. //----- Flash the screen --------------------------------------
  2359. static void flash(int h)
  2360. {
  2361. standout_start(); // send "start reverse video" sequence
  2362. redraw(TRUE);
  2363. mysleep(h);
  2364. standout_end(); // send "end reverse video" sequence
  2365. redraw(TRUE);
  2366. }
  2367. static void Indicate_Error(void)
  2368. {
  2369. #if ENABLE_FEATURE_VI_CRASHME
  2370. if (crashme > 0)
  2371. return; // generate a random command
  2372. #endif
  2373. if (!err_method) {
  2374. write1(bell); // send out a bell character
  2375. } else {
  2376. flash(10);
  2377. }
  2378. }
  2379. //----- Screen[] Routines --------------------------------------
  2380. //----- Erase the Screen[] memory ------------------------------
  2381. static void screen_erase(void)
  2382. {
  2383. memset(screen, ' ', screensize); // clear new screen
  2384. }
  2385. static int bufsum(char *buf, int count)
  2386. {
  2387. int sum = 0;
  2388. char *e = buf + count;
  2389. while (buf < e)
  2390. sum += (unsigned char) *buf++;
  2391. return sum;
  2392. }
  2393. //----- Draw the status line at bottom of the screen -------------
  2394. static void show_status_line(void)
  2395. {
  2396. int cnt = 0, cksum = 0;
  2397. // either we already have an error or status message, or we
  2398. // create one.
  2399. if (!have_status_msg) {
  2400. cnt = format_edit_status();
  2401. cksum = bufsum(status_buffer, cnt);
  2402. }
  2403. if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
  2404. last_status_cksum= cksum; // remember if we have seen this line
  2405. place_cursor(rows - 1, 0, FALSE); // put cursor on status line
  2406. write1(status_buffer);
  2407. clear_to_eol();
  2408. if (have_status_msg) {
  2409. if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
  2410. (columns - 1) ) {
  2411. have_status_msg = 0;
  2412. Hit_Return();
  2413. }
  2414. have_status_msg = 0;
  2415. }
  2416. place_cursor(crow, ccol, FALSE); // put cursor back in correct place
  2417. }
  2418. fflush(stdout);
  2419. }
  2420. //----- format the status buffer, the bottom line of screen ------
  2421. // format status buffer, with STANDOUT mode
  2422. static void psbs(const char *format, ...)
  2423. {
  2424. va_list args;
  2425. va_start(args, format);
  2426. strcpy(status_buffer, SOs); // Terminal standout mode on
  2427. vsprintf(status_buffer + strlen(status_buffer), format, args);
  2428. strcat(status_buffer, SOn); // Terminal standout mode off
  2429. va_end(args);
  2430. have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
  2431. }
  2432. // format status buffer
  2433. static void psb(const char *format, ...)
  2434. {
  2435. va_list args;
  2436. va_start(args, format);
  2437. vsprintf(status_buffer, format, args);
  2438. va_end(args);
  2439. have_status_msg = 1;
  2440. }
  2441. static void ni(const char * s) // display messages
  2442. {
  2443. char buf[MAX_LINELEN];
  2444. print_literal(buf, s);
  2445. psbs("\'%s\' is not implemented", buf);
  2446. }
  2447. static int format_edit_status(void) // show file status on status line
  2448. {
  2449. static int tot;
  2450. static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
  2451. int cur, percent, ret, trunc_at;
  2452. // file_modified is now a counter rather than a flag. this
  2453. // helps reduce the amount of line counting we need to do.
  2454. // (this will cause a mis-reporting of modified status
  2455. // once every MAXINT editing operations.)
  2456. // it would be nice to do a similar optimization here -- if
  2457. // we haven't done a motion that could have changed which line
  2458. // we're on, then we shouldn't have to do this count_lines()
  2459. cur = count_lines(text, dot);
  2460. // reduce counting -- the total lines can't have
  2461. // changed if we haven't done any edits.
  2462. if (file_modified != last_file_modified) {
  2463. tot = cur + count_lines(dot, end - 1) - 1;
  2464. last_file_modified = file_modified;
  2465. }
  2466. // current line percent
  2467. // ------------- ~~ ----------
  2468. // total lines 100
  2469. if (tot > 0) {
  2470. percent = (100 * cur) / tot;
  2471. } else {
  2472. cur = tot = 0;
  2473. percent = 100;
  2474. }
  2475. trunc_at = columns < STATUS_BUFFER_LEN-1 ?
  2476. columns : STATUS_BUFFER_LEN-1;
  2477. ret = snprintf(status_buffer, trunc_at+1,
  2478. #if ENABLE_FEATURE_VI_READONLY
  2479. "%c %s%s%s %d/%d %d%%",
  2480. #else
  2481. "%c %s%s %d/%d %d%%",
  2482. #endif
  2483. cmd_mode_indicator[cmd_mode & 3],
  2484. (current_filename != NULL ? current_filename : "No file"),
  2485. #if ENABLE_FEATURE_VI_READONLY
  2486. (readonly_mode ? " [Readonly]" : ""),
  2487. #endif
  2488. (file_modified ? " [Modified]" : ""),
  2489. cur, tot, percent);
  2490. if (ret >= 0 && ret < trunc_at)
  2491. return ret; /* it all fit */
  2492. return trunc_at; /* had to truncate */
  2493. }
  2494. //----- Force refresh of all Lines -----------------------------
  2495. static void redraw(int full_screen)
  2496. {
  2497. place_cursor(0, 0, FALSE); // put cursor in correct place
  2498. clear_to_eos(); // tel terminal to erase display
  2499. screen_erase(); // erase the internal screen buffer
  2500. last_status_cksum = 0; // force status update
  2501. refresh(full_screen); // this will redraw the entire display
  2502. show_status_line();
  2503. }
  2504. //----- Format a text[] line into a buffer ---------------------
  2505. static void format_line(char *dest, char *src, int li)
  2506. {
  2507. int co;
  2508. char c;
  2509. for (co = 0; co < MAX_SCR_COLS; co++) {
  2510. c = ' '; // assume blank
  2511. if (li > 0 && co == 0) {
  2512. c = '~'; // not first line, assume Tilde
  2513. }
  2514. // are there chars in text[] and have we gone past the end
  2515. if (text < end && src < end) {
  2516. c = *src++;
  2517. }
  2518. if (c == '\n')
  2519. break;
  2520. if ((c & 0x80) && !Isprint(c)) {
  2521. c = '.';
  2522. }
  2523. if ((unsigned char)(c) < ' ' || c == 0x7f) {
  2524. if (c == '\t') {
  2525. c = ' ';
  2526. // co % 8 != 7
  2527. for (; (co % tabstop) != (tabstop - 1); co++) {
  2528. dest[co] = c;
  2529. }
  2530. } else {
  2531. dest[co++] = '^';
  2532. if (c == 0x7f)
  2533. c = '?';
  2534. else
  2535. c += '@'; // make it visible
  2536. }
  2537. }
  2538. // the co++ is done here so that the column will
  2539. // not be overwritten when we blank-out the rest of line
  2540. dest[co] = c;
  2541. if (src >= end)
  2542. break;
  2543. }
  2544. }
  2545. //----- Refresh the changed screen lines -----------------------
  2546. // Copy the source line from text[] into the buffer and note
  2547. // if the current screenline is different from the new buffer.
  2548. // If they differ then that line needs redrawing on the terminal.
  2549. //
  2550. static void refresh(int full_screen)
  2551. {
  2552. static int old_offset;
  2553. int li, changed;
  2554. char buf[MAX_SCR_COLS];
  2555. char *tp, *sp; // pointer into text[] and screen[]
  2556. #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
  2557. int last_li = -2; // last line that changed- for optimizing cursor movement
  2558. #endif
  2559. if (ENABLE_FEATURE_VI_WIN_RESIZE)
  2560. get_terminal_width_height(0, &columns, &rows);
  2561. sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
  2562. tp = screenbegin; // index into text[] of top line
  2563. // compare text[] to screen[] and mark screen[] lines that need updating
  2564. for (li = 0; li < rows - 1; li++) {
  2565. int cs, ce; // column start & end
  2566. memset(buf, ' ', MAX_SCR_COLS); // blank-out the buffer
  2567. buf[MAX_SCR_COLS-1] = 0; // NULL terminate the buffer
  2568. // format current text line into buf
  2569. format_line(buf, tp, li);
  2570. // skip to the end of the current text[] line
  2571. while (tp < end && *tp++ != '\n') /*no-op*/;
  2572. // see if there are any changes between vitual screen and buf
  2573. changed = FALSE; // assume no change
  2574. cs= 0;
  2575. ce= columns-1;
  2576. sp = &screen[li * columns]; // start of screen line
  2577. if (full_screen) {
  2578. // force re-draw of every single column from 0 - columns-1
  2579. goto re0;
  2580. }
  2581. // compare newly formatted buffer with virtual screen
  2582. // look forward for first difference between buf and screen
  2583. for (; cs <= ce; cs++) {
  2584. if (buf[cs + offset] != sp[cs]) {
  2585. changed = TRUE; // mark for redraw
  2586. break;
  2587. }
  2588. }
  2589. // look backward for last difference between buf and screen
  2590. for ( ; ce >= cs; ce--) {
  2591. if (buf[ce + offset] != sp[ce]) {
  2592. changed = TRUE; // mark for redraw
  2593. break;
  2594. }
  2595. }
  2596. // now, cs is index of first diff, and ce is index of last diff
  2597. // if horz offset has changed, force a redraw
  2598. if (offset != old_offset) {
  2599. re0:
  2600. changed = TRUE;
  2601. }
  2602. // make a sanity check of columns indexes
  2603. if (cs < 0) cs= 0;
  2604. if (ce > columns-1) ce= columns-1;
  2605. if (cs > ce) { cs= 0; ce= columns-1; }
  2606. // is there a change between vitual screen and buf
  2607. if (changed) {
  2608. // copy changed part of buffer to virtual screen
  2609. memmove(sp+cs, buf+(cs+offset), ce-cs+1);
  2610. // move cursor to column of first change
  2611. if (offset != old_offset) {
  2612. // opti_cur_move is still too stupid
  2613. // to handle offsets correctly
  2614. place_cursor(li, cs, FALSE);
  2615. } else {
  2616. #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
  2617. // if this just the next line
  2618. // try to optimize cursor movement
  2619. // otherwise, use standard ESC sequence
  2620. place_cursor(li, cs, li == (last_li+1) ? TRUE : FALSE);
  2621. last_li= li;
  2622. #else
  2623. place_cursor(li, cs, FALSE); // use standard ESC sequence
  2624. #endif /* FEATURE_VI_OPTIMIZE_CURSOR */
  2625. }
  2626. // write line out to terminal
  2627. {
  2628. int nic = ce - cs + 1;
  2629. char *out = sp + cs;
  2630. while (nic-- > 0) {
  2631. bb_putchar(*out);
  2632. out++;
  2633. }
  2634. }
  2635. #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
  2636. last_row = li;
  2637. #endif
  2638. }
  2639. }
  2640. #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
  2641. place_cursor(crow, ccol, (crow == last_row) ? TRUE : FALSE);
  2642. last_row = crow;
  2643. #else
  2644. place_cursor(crow, ccol, FALSE);
  2645. #endif
  2646. if (offset != old_offset)
  2647. old_offset = offset;
  2648. }
  2649. //---------------------------------------------------------------------
  2650. //----- the Ascii Chart -----------------------------------------------
  2651. //
  2652. // 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
  2653. // 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
  2654. // 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
  2655. // 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
  2656. // 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
  2657. // 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
  2658. // 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
  2659. // 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
  2660. // 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
  2661. // 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
  2662. // 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
  2663. // 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
  2664. // 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
  2665. // 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
  2666. // 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
  2667. // 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
  2668. //---------------------------------------------------------------------
  2669. //----- Execute a Vi Command -----------------------------------
  2670. static void do_cmd(char c)
  2671. {
  2672. const char *msg;
  2673. char c1, *p, *q, buf[9], *save_dot;
  2674. int cnt, i, j, dir, yf;
  2675. c1 = c; // quiet the compiler
  2676. cnt = yf = dir = 0; // quiet the compiler
  2677. msg = p = q = save_dot = buf; // quiet the compiler
  2678. memset(buf, '\0', 9); // clear buf
  2679. show_status_line();
  2680. /* if this is a cursor key, skip these checks */
  2681. switch (c) {
  2682. case VI_K_UP:
  2683. case VI_K_DOWN:
  2684. case VI_K_LEFT:
  2685. case VI_K_RIGHT:
  2686. case VI_K_HOME:
  2687. case VI_K_END:
  2688. case VI_K_PAGEUP:
  2689. case VI_K_PAGEDOWN:
  2690. goto key_cmd_mode;
  2691. }
  2692. if (cmd_mode == 2) {
  2693. // flip-flop Insert/Replace mode
  2694. if (c == VI_K_INSERT)
  2695. goto dc_i;
  2696. // we are 'R'eplacing the current *dot with new char
  2697. if (*dot == '\n') {
  2698. // don't Replace past E-o-l
  2699. cmd_mode = 1; // convert to insert
  2700. } else {
  2701. if (1 <= c || Isprint(c)) {
  2702. if (c != 27)
  2703. dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
  2704. dot = char_insert(dot, c); // insert new char
  2705. }
  2706. goto dc1;
  2707. }
  2708. }
  2709. if (cmd_mode == 1) {
  2710. // hitting "Insert" twice means "R" replace mode
  2711. if (c == VI_K_INSERT) goto dc5;
  2712. // insert the char c at "dot"
  2713. if (1 <= c || Isprint(c)) {
  2714. dot = char_insert(dot, c);
  2715. }
  2716. goto dc1;
  2717. }
  2718. key_cmd_mode:
  2719. switch (c) {
  2720. //case 0x01: // soh
  2721. //case 0x09: // ht
  2722. //case 0x0b: // vt
  2723. //case 0x0e: // so
  2724. //case 0x0f: // si
  2725. //case 0x10: // dle
  2726. //case 0x11: // dc1
  2727. //case 0x13: // dc3
  2728. #if ENABLE_FEATURE_VI_CRASHME
  2729. case 0x14: // dc4 ctrl-T
  2730. crashme = (crashme == 0) ? 1 : 0;
  2731. break;
  2732. #endif
  2733. //case 0x16: // syn
  2734. //case 0x17: // etb
  2735. //case 0x18: // can
  2736. //case 0x1c: // fs
  2737. //case 0x1d: // gs
  2738. //case 0x1e: // rs
  2739. //case 0x1f: // us
  2740. //case '!': // !-
  2741. //case '#': // #-
  2742. //case '&': // &-
  2743. //case '(': // (-
  2744. //case ')': // )-
  2745. //case '*': // *-
  2746. //case ',': // ,-
  2747. //case '=': // =-
  2748. //case '@': // @-
  2749. //case 'F': // F-
  2750. //case 'K': // K-
  2751. //case 'Q': // Q-
  2752. //case 'S': // S-
  2753. //case 'T': // T-
  2754. //case 'V': // V-
  2755. //case '[': // [-
  2756. //case '\\': // \-
  2757. //case ']': // ]-
  2758. //case '_': // _-
  2759. //case '`': // `-
  2760. //case 'g': // g-
  2761. //case 'u': // u- FIXME- there is no undo
  2762. //case 'v': // v-
  2763. default: // unrecognised command
  2764. buf[0] = c;
  2765. buf[1] = '\0';
  2766. if (c < ' ') {
  2767. buf[0] = '^';
  2768. buf[1] = c + '@';
  2769. buf[2] = '\0';
  2770. }
  2771. ni(buf);
  2772. end_cmd_q(); // stop adding to q
  2773. case 0x00: // nul- ignore
  2774. break;
  2775. case 2: // ctrl-B scroll up full screen
  2776. case VI_K_PAGEUP: // Cursor Key Page Up
  2777. dot_scroll(rows - 2, -1);
  2778. break;
  2779. #if ENABLE_FEATURE_VI_USE_SIGNALS
  2780. case 0x03: // ctrl-C interrupt
  2781. longjmp(restart, 1);
  2782. break;
  2783. case 26: // ctrl-Z suspend
  2784. suspend_sig(SIGTSTP);
  2785. break;
  2786. #endif
  2787. case 4: // ctrl-D scroll down half screen
  2788. dot_scroll((rows - 2) / 2, 1);
  2789. break;
  2790. case 5: // ctrl-E scroll down one line
  2791. dot_scroll(1, 1);
  2792. break;
  2793. case 6: // ctrl-F scroll down full screen
  2794. case VI_K_PAGEDOWN: // Cursor Key Page Down
  2795. dot_scroll(rows - 2, 1);
  2796. break;
  2797. case 7: // ctrl-G show current status
  2798. last_status_cksum = 0; // force status update
  2799. break;
  2800. case 'h': // h- move left
  2801. case VI_K_LEFT: // cursor key Left
  2802. case 8: // ctrl-H- move left (This may be ERASE char)
  2803. case 0x7f: // DEL- move left (This may be ERASE char)
  2804. if (cmdcnt-- > 1) {
  2805. do_cmd(c);
  2806. } // repeat cnt
  2807. dot_left();
  2808. break;
  2809. case 10: // Newline ^J
  2810. case 'j': // j- goto next line, same col
  2811. case VI_K_DOWN: // cursor key Down
  2812. if (cmdcnt-- > 1) {
  2813. do_cmd(c);
  2814. } // repeat cnt
  2815. dot_next(); // go to next B-o-l
  2816. dot = move_to_col(dot, ccol + offset); // try stay in same col
  2817. break;
  2818. case 12: // ctrl-L force redraw whole screen
  2819. case 18: // ctrl-R force redraw
  2820. place_cursor(0, 0, FALSE); // put cursor in correct place
  2821. clear_to_eos(); // tel terminal to erase display
  2822. mysleep(10);
  2823. screen_erase(); // erase the internal screen buffer
  2824. last_status_cksum = 0; // force status update
  2825. refresh(TRUE); // this will redraw the entire display
  2826. break;
  2827. case 13: // Carriage Return ^M
  2828. case '+': // +- goto next line
  2829. if (cmdcnt-- > 1) {
  2830. do_cmd(c);
  2831. } // repeat cnt
  2832. dot_next();
  2833. dot_skip_over_ws();
  2834. break;
  2835. case 21: // ctrl-U scroll up half screen
  2836. dot_scroll((rows - 2) / 2, -1);
  2837. break;
  2838. case 25: // ctrl-Y scroll up one line
  2839. dot_scroll(1, -1);
  2840. break;
  2841. case 27: // esc
  2842. if (cmd_mode == 0)
  2843. indicate_error(c);
  2844. cmd_mode = 0; // stop insrting
  2845. end_cmd_q();
  2846. last_status_cksum = 0; // force status update
  2847. break;
  2848. case ' ': // move right
  2849. case 'l': // move right
  2850. case VI_K_RIGHT: // Cursor Key Right
  2851. if (cmdcnt-- > 1) {
  2852. do_cmd(c);
  2853. } // repeat cnt
  2854. dot_right();
  2855. break;
  2856. #if ENABLE_FEATURE_VI_YANKMARK
  2857. case '"': // "- name a register to use for Delete/Yank
  2858. c1 = get_one_char();
  2859. c1 = tolower(c1);
  2860. if (islower(c1)) {
  2861. YDreg = c1 - 'a';
  2862. } else {
  2863. indicate_error(c);
  2864. }
  2865. break;
  2866. case '\'': // '- goto a specific mark
  2867. c1 = get_one_char();
  2868. c1 = tolower(c1);
  2869. if (islower(c1)) {
  2870. c1 = c1 - 'a';
  2871. // get the b-o-l
  2872. q = mark[(unsigned char) c1];
  2873. if (text <= q && q < end) {
  2874. dot = q;
  2875. dot_begin(); // go to B-o-l
  2876. dot_skip_over_ws();
  2877. }
  2878. } else if (c1 == '\'') { // goto previous context
  2879. dot = swap_context(dot); // swap current and previous context
  2880. dot_begin(); // go to B-o-l
  2881. dot_skip_over_ws();
  2882. } else {
  2883. indicate_error(c);
  2884. }
  2885. break;
  2886. case 'm': // m- Mark a line
  2887. // this is really stupid. If there are any inserts or deletes
  2888. // between text[0] and dot then this mark will not point to the
  2889. // correct location! It could be off by many lines!
  2890. // Well..., at least its quick and dirty.
  2891. c1 = get_one_char();
  2892. c1 = tolower(c1);
  2893. if (islower(c1)) {
  2894. c1 = c1 - 'a';
  2895. // remember the line
  2896. mark[(int) c1] = dot;
  2897. } else {
  2898. indicate_error(c);
  2899. }
  2900. break;
  2901. case 'P': // P- Put register before
  2902. case 'p': // p- put register after
  2903. p = reg[YDreg];
  2904. if (p == 0) {
  2905. psbs("Nothing in register %c", what_reg());
  2906. break;
  2907. }
  2908. // are we putting whole lines or strings
  2909. if (strchr(p, '\n') != NULL) {
  2910. if (c == 'P') {
  2911. dot_begin(); // putting lines- Put above
  2912. }
  2913. if (c == 'p') {
  2914. // are we putting after very last line?
  2915. if (end_line(dot) == (end - 1)) {
  2916. dot = end; // force dot to end of text[]
  2917. } else {
  2918. dot_next(); // next line, then put before
  2919. }
  2920. }
  2921. } else {
  2922. if (c == 'p')
  2923. dot_right(); // move to right, can move to NL
  2924. }
  2925. dot = string_insert(dot, p); // insert the string
  2926. end_cmd_q(); // stop adding to q
  2927. break;
  2928. case 'U': // U- Undo; replace current line with original version
  2929. if (reg[Ureg] != 0) {
  2930. p = begin_line(dot);
  2931. q = end_line(dot);
  2932. p = text_hole_delete(p, q); // delete cur line
  2933. p = string_insert(p, reg[Ureg]); // insert orig line
  2934. dot = p;
  2935. dot_skip_over_ws();
  2936. }
  2937. break;
  2938. #endif /* FEATURE_VI_YANKMARK */
  2939. case '$': // $- goto end of line
  2940. case VI_K_END: // Cursor Key End
  2941. if (cmdcnt-- > 1) {
  2942. do_cmd(c);
  2943. } // repeat cnt
  2944. dot = end_line(dot);
  2945. break;
  2946. case '%': // %- find matching char of pair () [] {}
  2947. for (q = dot; q < end && *q != '\n'; q++) {
  2948. if (strchr("()[]{}", *q) != NULL) {
  2949. // we found half of a pair
  2950. p = find_pair(q, *q);
  2951. if (p == NULL) {
  2952. indicate_error(c);
  2953. } else {
  2954. dot = p;
  2955. }
  2956. break;
  2957. }
  2958. }
  2959. if (*q == '\n')
  2960. indicate_error(c);
  2961. break;
  2962. case 'f': // f- forward to a user specified char
  2963. last_forward_char = get_one_char(); // get the search char
  2964. //
  2965. // dont separate these two commands. 'f' depends on ';'
  2966. //
  2967. //**** fall thru to ... ';'
  2968. case ';': // ;- look at rest of line for last forward char
  2969. if (cmdcnt-- > 1) {
  2970. do_cmd(';');
  2971. } // repeat cnt
  2972. if (last_forward_char == 0)
  2973. break;
  2974. q = dot + 1;
  2975. while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
  2976. q++;
  2977. }
  2978. if (*q == last_forward_char)
  2979. dot = q;
  2980. break;
  2981. case '-': // -- goto prev line
  2982. if (cmdcnt-- > 1) {
  2983. do_cmd(c);
  2984. } // repeat cnt
  2985. dot_prev();
  2986. dot_skip_over_ws();
  2987. break;
  2988. #if ENABLE_FEATURE_VI_DOT_CMD
  2989. case '.': // .- repeat the last modifying command
  2990. // Stuff the last_modifying_cmd back into stdin
  2991. // and let it be re-executed.
  2992. if (last_modifying_cmd != 0) {
  2993. ioq = ioq_start = xstrdup(last_modifying_cmd);
  2994. }
  2995. break;
  2996. #endif
  2997. #if ENABLE_FEATURE_VI_SEARCH
  2998. case '?': // /- search for a pattern
  2999. case '/': // /- search for a pattern
  3000. buf[0] = c;
  3001. buf[1] = '\0';
  3002. q = get_input_line(buf); // get input line- use "status line"
  3003. if (q[0] && !q[1])
  3004. goto dc3; // if no pat re-use old pat
  3005. if (q[0]) { // strlen(q) > 1: new pat- save it and find
  3006. // there is a new pat
  3007. free(last_search_pattern);
  3008. last_search_pattern = xstrdup(q);
  3009. goto dc3; // now find the pattern
  3010. }
  3011. // user changed mind and erased the "/"- do nothing
  3012. break;
  3013. case 'N': // N- backward search for last pattern
  3014. if (cmdcnt-- > 1) {
  3015. do_cmd(c);
  3016. } // repeat cnt
  3017. dir = BACK; // assume BACKWARD search
  3018. p = dot - 1;
  3019. if (last_search_pattern[0] == '?') {
  3020. dir = FORWARD;
  3021. p = dot + 1;
  3022. }
  3023. goto dc4; // now search for pattern
  3024. break;
  3025. case 'n': // n- repeat search for last pattern
  3026. // search rest of text[] starting at next char
  3027. // if search fails return orignal "p" not the "p+1" address
  3028. if (cmdcnt-- > 1) {
  3029. do_cmd(c);
  3030. } // repeat cnt
  3031. dc3:
  3032. if (last_search_pattern == 0) {
  3033. msg = "No previous regular expression";
  3034. goto dc2;
  3035. }
  3036. if (last_search_pattern[0] == '/') {
  3037. dir = FORWARD; // assume FORWARD search
  3038. p = dot + 1;
  3039. }
  3040. if (last_search_pattern[0] == '?') {
  3041. dir = BACK;
  3042. p = dot - 1;
  3043. }
  3044. dc4:
  3045. q = char_search(p, last_search_pattern + 1, dir, FULL);
  3046. if (q != NULL) {
  3047. dot = q; // good search, update "dot"
  3048. msg = "";
  3049. goto dc2;
  3050. }
  3051. // no pattern found between "dot" and "end"- continue at top
  3052. p = text;
  3053. if (dir == BACK) {
  3054. p = end - 1;
  3055. }
  3056. q = char_search(p, last_search_pattern + 1, dir, FULL);
  3057. if (q != NULL) { // found something
  3058. dot = q; // found new pattern- goto it
  3059. msg = "search hit BOTTOM, continuing at TOP";
  3060. if (dir == BACK) {
  3061. msg = "search hit TOP, continuing at BOTTOM";
  3062. }
  3063. } else {
  3064. msg = "Pattern not found";
  3065. }
  3066. dc2:
  3067. if (*msg)
  3068. psbs("%s", msg);
  3069. break;
  3070. case '{': // {- move backward paragraph
  3071. q = char_search(dot, "\n\n", BACK, FULL);
  3072. if (q != NULL) { // found blank line
  3073. dot = next_line(q); // move to next blank line
  3074. }
  3075. break;
  3076. case '}': // }- move forward paragraph
  3077. q = char_search(dot, "\n\n", FORWARD, FULL);
  3078. if (q != NULL) { // found blank line
  3079. dot = next_line(q); // move to next blank line
  3080. }
  3081. break;
  3082. #endif /* FEATURE_VI_SEARCH */
  3083. case '0': // 0- goto begining of line
  3084. case '1': // 1-
  3085. case '2': // 2-
  3086. case '3': // 3-
  3087. case '4': // 4-
  3088. case '5': // 5-
  3089. case '6': // 6-
  3090. case '7': // 7-
  3091. case '8': // 8-
  3092. case '9': // 9-
  3093. if (c == '0' && cmdcnt < 1) {
  3094. dot_begin(); // this was a standalone zero
  3095. } else {
  3096. cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
  3097. }
  3098. break;
  3099. case ':': // :- the colon mode commands
  3100. p = get_input_line(":"); // get input line- use "status line"
  3101. #if ENABLE_FEATURE_VI_COLON
  3102. colon(p); // execute the command
  3103. #else
  3104. if (*p == ':')
  3105. p++; // move past the ':'
  3106. cnt = strlen(p);
  3107. if (cnt <= 0)
  3108. break;
  3109. if (strncasecmp(p, "quit", cnt) == 0
  3110. || strncasecmp(p, "q!", cnt) == 0 // delete lines
  3111. ) {
  3112. if (file_modified && p[1] != '!') {
  3113. psbs("No write since last change (:quit! overrides)");
  3114. } else {
  3115. editing = 0;
  3116. }
  3117. } else if (strncasecmp(p, "write", cnt) == 0
  3118. || strncasecmp(p, "wq", cnt) == 0
  3119. || strncasecmp(p, "wn", cnt) == 0
  3120. || strncasecmp(p, "x", cnt) == 0
  3121. ) {
  3122. cnt = file_write(current_filename, text, end - 1);
  3123. if (cnt < 0) {
  3124. if (cnt == -1)
  3125. psbs("Write error: %s", strerror(errno));
  3126. } else {
  3127. file_modified = 0;
  3128. last_file_modified = -1;
  3129. psb("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
  3130. if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
  3131. || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
  3132. ) {
  3133. editing = 0;
  3134. }
  3135. }
  3136. } else if (strncasecmp(p, "file", cnt) == 0) {
  3137. last_status_cksum = 0; // force status update
  3138. } else if (sscanf(p, "%d", &j) > 0) {
  3139. dot = find_line(j); // go to line # j
  3140. dot_skip_over_ws();
  3141. } else { // unrecognised cmd
  3142. ni(p);
  3143. }
  3144. #endif /* !FEATURE_VI_COLON */
  3145. break;
  3146. case '<': // <- Left shift something
  3147. case '>': // >- Right shift something
  3148. cnt = count_lines(text, dot); // remember what line we are on
  3149. c1 = get_one_char(); // get the type of thing to delete
  3150. find_range(&p, &q, c1);
  3151. yank_delete(p, q, 1, YANKONLY); // save copy before change
  3152. p = begin_line(p);
  3153. q = end_line(q);
  3154. i = count_lines(p, q); // # of lines we are shifting
  3155. for ( ; i > 0; i--, p = next_line(p)) {
  3156. if (c == '<') {
  3157. // shift left- remove tab or 8 spaces
  3158. if (*p == '\t') {
  3159. // shrink buffer 1 char
  3160. text_hole_delete(p, p);
  3161. } else if (*p == ' ') {
  3162. // we should be calculating columns, not just SPACE
  3163. for (j = 0; *p == ' ' && j < tabstop; j++) {
  3164. text_hole_delete(p, p);
  3165. }
  3166. }
  3167. } else if (c == '>') {
  3168. // shift right -- add tab or 8 spaces
  3169. char_insert(p, '\t');
  3170. }
  3171. }
  3172. dot = find_line(cnt); // what line were we on
  3173. dot_skip_over_ws();
  3174. end_cmd_q(); // stop adding to q
  3175. break;
  3176. case 'A': // A- append at e-o-l
  3177. dot_end(); // go to e-o-l
  3178. //**** fall thru to ... 'a'
  3179. case 'a': // a- append after current char
  3180. if (*dot != '\n')
  3181. dot++;
  3182. goto dc_i;
  3183. break;
  3184. case 'B': // B- back a blank-delimited Word
  3185. case 'E': // E- end of a blank-delimited word
  3186. case 'W': // W- forward a blank-delimited word
  3187. if (cmdcnt-- > 1) {
  3188. do_cmd(c);
  3189. } // repeat cnt
  3190. dir = FORWARD;
  3191. if (c == 'B')
  3192. dir = BACK;
  3193. if (c == 'W' || isspace(dot[dir])) {
  3194. dot = skip_thing(dot, 1, dir, S_TO_WS);
  3195. dot = skip_thing(dot, 2, dir, S_OVER_WS);
  3196. }
  3197. if (c != 'W')
  3198. dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
  3199. break;
  3200. case 'C': // C- Change to e-o-l
  3201. case 'D': // D- delete to e-o-l
  3202. save_dot = dot;
  3203. dot = dollar_line(dot); // move to before NL
  3204. // copy text into a register and delete
  3205. dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
  3206. if (c == 'C')
  3207. goto dc_i; // start inserting
  3208. #if ENABLE_FEATURE_VI_DOT_CMD
  3209. if (c == 'D')
  3210. end_cmd_q(); // stop adding to q
  3211. #endif
  3212. break;
  3213. case 'G': // G- goto to a line number (default= E-O-F)
  3214. dot = end - 1; // assume E-O-F
  3215. if (cmdcnt > 0) {
  3216. dot = find_line(cmdcnt); // what line is #cmdcnt
  3217. }
  3218. dot_skip_over_ws();
  3219. break;
  3220. case 'H': // H- goto top line on screen
  3221. dot = screenbegin;
  3222. if (cmdcnt > (rows - 1)) {
  3223. cmdcnt = (rows - 1);
  3224. }
  3225. if (cmdcnt-- > 1) {
  3226. do_cmd('+');
  3227. } // repeat cnt
  3228. dot_skip_over_ws();
  3229. break;
  3230. case 'I': // I- insert before first non-blank
  3231. dot_begin(); // 0
  3232. dot_skip_over_ws();
  3233. //**** fall thru to ... 'i'
  3234. case 'i': // i- insert before current char
  3235. case VI_K_INSERT: // Cursor Key Insert
  3236. dc_i:
  3237. cmd_mode = 1; // start insrting
  3238. break;
  3239. case 'J': // J- join current and next lines together
  3240. if (cmdcnt-- > 2) {
  3241. do_cmd(c);
  3242. } // repeat cnt
  3243. dot_end(); // move to NL
  3244. if (dot < end - 1) { // make sure not last char in text[]
  3245. *dot++ = ' '; // replace NL with space
  3246. file_modified++;
  3247. while (isblank(*dot)) { // delete leading WS
  3248. dot_delete();
  3249. }
  3250. }
  3251. end_cmd_q(); // stop adding to q
  3252. break;
  3253. case 'L': // L- goto bottom line on screen
  3254. dot = end_screen();
  3255. if (cmdcnt > (rows - 1)) {
  3256. cmdcnt = (rows - 1);
  3257. }
  3258. if (cmdcnt-- > 1) {
  3259. do_cmd('-');
  3260. } // repeat cnt
  3261. dot_begin();
  3262. dot_skip_over_ws();
  3263. break;
  3264. case 'M': // M- goto middle line on screen
  3265. dot = screenbegin;
  3266. for (cnt = 0; cnt < (rows-1) / 2; cnt++)
  3267. dot = next_line(dot);
  3268. break;
  3269. case 'O': // O- open a empty line above
  3270. // 0i\n ESC -i
  3271. p = begin_line(dot);
  3272. if (p[-1] == '\n') {
  3273. dot_prev();
  3274. case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
  3275. dot_end();
  3276. dot = char_insert(dot, '\n');
  3277. } else {
  3278. dot_begin(); // 0
  3279. dot = char_insert(dot, '\n'); // i\n ESC
  3280. dot_prev(); // -
  3281. }
  3282. goto dc_i;
  3283. break;
  3284. case 'R': // R- continuous Replace char
  3285. dc5:
  3286. cmd_mode = 2;
  3287. break;
  3288. case 'X': // X- delete char before dot
  3289. case 'x': // x- delete the current char
  3290. case 's': // s- substitute the current char
  3291. if (cmdcnt-- > 1) {
  3292. do_cmd(c);
  3293. } // repeat cnt
  3294. dir = 0;
  3295. if (c == 'X')
  3296. dir = -1;
  3297. if (dot[dir] != '\n') {
  3298. if (c == 'X')
  3299. dot--; // delete prev char
  3300. dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
  3301. }
  3302. if (c == 's')
  3303. goto dc_i; // start insrting
  3304. end_cmd_q(); // stop adding to q
  3305. break;
  3306. case 'Z': // Z- if modified, {write}; exit
  3307. // ZZ means to save file (if necessary), then exit
  3308. c1 = get_one_char();
  3309. if (c1 != 'Z') {
  3310. indicate_error(c);
  3311. break;
  3312. }
  3313. if (file_modified) {
  3314. if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
  3315. psbs("\"%s\" File is read only", current_filename);
  3316. break;
  3317. }
  3318. cnt = file_write(current_filename, text, end - 1);
  3319. if (cnt < 0) {
  3320. if (cnt == -1)
  3321. psbs("Write error: %s", strerror(errno));
  3322. } else if (cnt == (end - 1 - text + 1)) {
  3323. editing = 0;
  3324. }
  3325. } else {
  3326. editing = 0;
  3327. }
  3328. break;
  3329. case '^': // ^- move to first non-blank on line
  3330. dot_begin();
  3331. dot_skip_over_ws();
  3332. break;
  3333. case 'b': // b- back a word
  3334. case 'e': // e- end of word
  3335. if (cmdcnt-- > 1) {
  3336. do_cmd(c);
  3337. } // repeat cnt
  3338. dir = FORWARD;
  3339. if (c == 'b')
  3340. dir = BACK;
  3341. if ((dot + dir) < text || (dot + dir) > end - 1)
  3342. break;
  3343. dot += dir;
  3344. if (isspace(*dot)) {
  3345. dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
  3346. }
  3347. if (isalnum(*dot) || *dot == '_') {
  3348. dot = skip_thing(dot, 1, dir, S_END_ALNUM);
  3349. } else if (ispunct(*dot)) {
  3350. dot = skip_thing(dot, 1, dir, S_END_PUNCT);
  3351. }
  3352. break;
  3353. case 'c': // c- change something
  3354. case 'd': // d- delete something
  3355. #if ENABLE_FEATURE_VI_YANKMARK
  3356. case 'y': // y- yank something
  3357. case 'Y': // Y- Yank a line
  3358. #endif
  3359. yf = YANKDEL; // assume either "c" or "d"
  3360. #if ENABLE_FEATURE_VI_YANKMARK
  3361. if (c == 'y' || c == 'Y')
  3362. yf = YANKONLY;
  3363. #endif
  3364. c1 = 'y';
  3365. if (c != 'Y')
  3366. c1 = get_one_char(); // get the type of thing to delete
  3367. find_range(&p, &q, c1);
  3368. if (c1 == 27) { // ESC- user changed mind and wants out
  3369. c = c1 = 27; // Escape- do nothing
  3370. } else if (strchr("wW", c1)) {
  3371. if (c == 'c') {
  3372. // don't include trailing WS as part of word
  3373. while (isblank(*q)) {
  3374. if (q <= text || q[-1] == '\n')
  3375. break;
  3376. q--;
  3377. }
  3378. }
  3379. dot = yank_delete(p, q, 0, yf); // delete word
  3380. } else if (strchr("^0bBeEft$", c1)) {
  3381. // single line copy text into a register and delete
  3382. dot = yank_delete(p, q, 0, yf); // delete word
  3383. } else if (strchr("cdykjHL%+-{}\r\n", c1)) {
  3384. // multiple line copy text into a register and delete
  3385. dot = yank_delete(p, q, 1, yf); // delete lines
  3386. if (c == 'c') {
  3387. dot = char_insert(dot, '\n');
  3388. // on the last line of file don't move to prev line
  3389. if (dot != (end-1)) {
  3390. dot_prev();
  3391. }
  3392. } else if (c == 'd') {
  3393. dot_begin();
  3394. dot_skip_over_ws();
  3395. }
  3396. } else {
  3397. // could not recognize object
  3398. c = c1 = 27; // error-
  3399. indicate_error(c);
  3400. }
  3401. if (c1 != 27) {
  3402. // if CHANGING, not deleting, start inserting after the delete
  3403. if (c == 'c') {
  3404. strcpy(buf, "Change");
  3405. goto dc_i; // start inserting
  3406. }
  3407. if (c == 'd') {
  3408. strcpy(buf, "Delete");
  3409. }
  3410. #if ENABLE_FEATURE_VI_YANKMARK
  3411. if (c == 'y' || c == 'Y') {
  3412. strcpy(buf, "Yank");
  3413. }
  3414. p = reg[YDreg];
  3415. q = p + strlen(p);
  3416. for (cnt = 0; p <= q; p++) {
  3417. if (*p == '\n')
  3418. cnt++;
  3419. }
  3420. psb("%s %d lines (%d chars) using [%c]",
  3421. buf, cnt, strlen(reg[YDreg]), what_reg());
  3422. #endif
  3423. end_cmd_q(); // stop adding to q
  3424. }
  3425. break;
  3426. case 'k': // k- goto prev line, same col
  3427. case VI_K_UP: // cursor key Up
  3428. if (cmdcnt-- > 1) {
  3429. do_cmd(c);
  3430. } // repeat cnt
  3431. dot_prev();
  3432. dot = move_to_col(dot, ccol + offset); // try stay in same col
  3433. break;
  3434. case 'r': // r- replace the current char with user input
  3435. c1 = get_one_char(); // get the replacement char
  3436. if (*dot != '\n') {
  3437. *dot = c1;
  3438. file_modified++; // has the file been modified
  3439. }
  3440. end_cmd_q(); // stop adding to q
  3441. break;
  3442. case 't': // t- move to char prior to next x
  3443. last_forward_char = get_one_char();
  3444. do_cmd(';');
  3445. if (*dot == last_forward_char)
  3446. dot_left();
  3447. last_forward_char= 0;
  3448. break;
  3449. case 'w': // w- forward a word
  3450. if (cmdcnt-- > 1) {
  3451. do_cmd(c);
  3452. } // repeat cnt
  3453. if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
  3454. dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
  3455. } else if (ispunct(*dot)) { // we are on PUNCT
  3456. dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
  3457. }
  3458. if (dot < end - 1)
  3459. dot++; // move over word
  3460. if (isspace(*dot)) {
  3461. dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
  3462. }
  3463. break;
  3464. case 'z': // z-
  3465. c1 = get_one_char(); // get the replacement char
  3466. cnt = 0;
  3467. if (c1 == '.')
  3468. cnt = (rows - 2) / 2; // put dot at center
  3469. if (c1 == '-')
  3470. cnt = rows - 2; // put dot at bottom
  3471. screenbegin = begin_line(dot); // start dot at top
  3472. dot_scroll(cnt, -1);
  3473. break;
  3474. case '|': // |- move to column "cmdcnt"
  3475. dot = move_to_col(dot, cmdcnt - 1); // try to move to column
  3476. break;
  3477. case '~': // ~- flip the case of letters a-z -> A-Z
  3478. if (cmdcnt-- > 1) {
  3479. do_cmd(c);
  3480. } // repeat cnt
  3481. if (islower(*dot)) {
  3482. *dot = toupper(*dot);
  3483. file_modified++; // has the file been modified
  3484. } else if (isupper(*dot)) {
  3485. *dot = tolower(*dot);
  3486. file_modified++; // has the file been modified
  3487. }
  3488. dot_right();
  3489. end_cmd_q(); // stop adding to q
  3490. break;
  3491. //----- The Cursor and Function Keys -----------------------------
  3492. case VI_K_HOME: // Cursor Key Home
  3493. dot_begin();
  3494. break;
  3495. // The Fn keys could point to do_macro which could translate them
  3496. case VI_K_FUN1: // Function Key F1
  3497. case VI_K_FUN2: // Function Key F2
  3498. case VI_K_FUN3: // Function Key F3
  3499. case VI_K_FUN4: // Function Key F4
  3500. case VI_K_FUN5: // Function Key F5
  3501. case VI_K_FUN6: // Function Key F6
  3502. case VI_K_FUN7: // Function Key F7
  3503. case VI_K_FUN8: // Function Key F8
  3504. case VI_K_FUN9: // Function Key F9
  3505. case VI_K_FUN10: // Function Key F10
  3506. case VI_K_FUN11: // Function Key F11
  3507. case VI_K_FUN12: // Function Key F12
  3508. break;
  3509. }
  3510. dc1:
  3511. // if text[] just became empty, add back an empty line
  3512. if (end == text) {
  3513. char_insert(text, '\n'); // start empty buf with dummy line
  3514. dot = text;
  3515. }
  3516. // it is OK for dot to exactly equal to end, otherwise check dot validity
  3517. if (dot != end) {
  3518. dot = bound_dot(dot); // make sure "dot" is valid
  3519. }
  3520. #if ENABLE_FEATURE_VI_YANKMARK
  3521. check_context(c); // update the current context
  3522. #endif
  3523. if (!isdigit(c))
  3524. cmdcnt = 0; // cmd was not a number, reset cmdcnt
  3525. cnt = dot - begin_line(dot);
  3526. // Try to stay off of the Newline
  3527. if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
  3528. dot--;
  3529. }
  3530. #if ENABLE_FEATURE_VI_CRASHME
  3531. static int totalcmds = 0;
  3532. static int Mp = 85; // Movement command Probability
  3533. static int Np = 90; // Non-movement command Probability
  3534. static int Dp = 96; // Delete command Probability
  3535. static int Ip = 97; // Insert command Probability
  3536. static int Yp = 98; // Yank command Probability
  3537. static int Pp = 99; // Put command Probability
  3538. static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
  3539. const char chars[20] = "\t012345 abcdABCD-=.$";
  3540. const char *const words[20] = {
  3541. "this", "is", "a", "test",
  3542. "broadcast", "the", "emergency", "of",
  3543. "system", "quick", "brown", "fox",
  3544. "jumped", "over", "lazy", "dogs",
  3545. "back", "January", "Febuary", "March"
  3546. };
  3547. const char *const lines[20] = {
  3548. "You should have received a copy of the GNU General Public License\n",
  3549. "char c, cm, *cmd, *cmd1;\n",
  3550. "generate a command by percentages\n",
  3551. "Numbers may be typed as a prefix to some commands.\n",
  3552. "Quit, discarding changes!\n",
  3553. "Forced write, if permission originally not valid.\n",
  3554. "In general, any ex or ed command (such as substitute or delete).\n",
  3555. "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
  3556. "Please get w/ me and I will go over it with you.\n",
  3557. "The following is a list of scheduled, committed changes.\n",
  3558. "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
  3559. "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
  3560. "Any question about transactions please contact Sterling Huxley.\n",
  3561. "I will try to get back to you by Friday, December 31.\n",
  3562. "This Change will be implemented on Friday.\n",
  3563. "Let me know if you have problems accessing this;\n",
  3564. "Sterling Huxley recently added you to the access list.\n",
  3565. "Would you like to go to lunch?\n",
  3566. "The last command will be automatically run.\n",
  3567. "This is too much english for a computer geek.\n",
  3568. };
  3569. char *multilines[20] = {
  3570. "You should have received a copy of the GNU General Public License\n",
  3571. "char c, cm, *cmd, *cmd1;\n",
  3572. "generate a command by percentages\n",
  3573. "Numbers may be typed as a prefix to some commands.\n",
  3574. "Quit, discarding changes!\n",
  3575. "Forced write, if permission originally not valid.\n",
  3576. "In general, any ex or ed command (such as substitute or delete).\n",
  3577. "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
  3578. "Please get w/ me and I will go over it with you.\n",
  3579. "The following is a list of scheduled, committed changes.\n",
  3580. "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
  3581. "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
  3582. "Any question about transactions please contact Sterling Huxley.\n",
  3583. "I will try to get back to you by Friday, December 31.\n",
  3584. "This Change will be implemented on Friday.\n",
  3585. "Let me know if you have problems accessing this;\n",
  3586. "Sterling Huxley recently added you to the access list.\n",
  3587. "Would you like to go to lunch?\n",
  3588. "The last command will be automatically run.\n",
  3589. "This is too much english for a computer geek.\n",
  3590. };
  3591. // create a random command to execute
  3592. static void crash_dummy()
  3593. {
  3594. static int sleeptime; // how long to pause between commands
  3595. char c, cm, *cmd, *cmd1;
  3596. int i, cnt, thing, rbi, startrbi, percent;
  3597. // "dot" movement commands
  3598. cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
  3599. // is there already a command running?
  3600. if (readed_for_parse > 0)
  3601. goto cd1;
  3602. cd0:
  3603. startrbi = rbi = 0;
  3604. sleeptime = 0; // how long to pause between commands
  3605. memset(readbuffer, '\0', MAX_LINELEN); // clear the read buffer
  3606. // generate a command by percentages
  3607. percent = (int) lrand48() % 100; // get a number from 0-99
  3608. if (percent < Mp) { // Movement commands
  3609. // available commands
  3610. cmd = cmd1;
  3611. M++;
  3612. } else if (percent < Np) { // non-movement commands
  3613. cmd = "mz<>\'\""; // available commands
  3614. N++;
  3615. } else if (percent < Dp) { // Delete commands
  3616. cmd = "dx"; // available commands
  3617. D++;
  3618. } else if (percent < Ip) { // Inset commands
  3619. cmd = "iIaAsrJ"; // available commands
  3620. I++;
  3621. } else if (percent < Yp) { // Yank commands
  3622. cmd = "yY"; // available commands
  3623. Y++;
  3624. } else if (percent < Pp) { // Put commands
  3625. cmd = "pP"; // available commands
  3626. P++;
  3627. } else {
  3628. // We do not know how to handle this command, try again
  3629. U++;
  3630. goto cd0;
  3631. }
  3632. // randomly pick one of the available cmds from "cmd[]"
  3633. i = (int) lrand48() % strlen(cmd);
  3634. cm = cmd[i];
  3635. if (strchr(":\024", cm))
  3636. goto cd0; // dont allow colon or ctrl-T commands
  3637. readbuffer[rbi++] = cm; // put cmd into input buffer
  3638. // now we have the command-
  3639. // there are 1, 2, and multi char commands
  3640. // find out which and generate the rest of command as necessary
  3641. if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
  3642. cmd1 = " \n\r0$^-+wWeEbBhjklHL";
  3643. if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
  3644. cmd1 = "abcdefghijklmnopqrstuvwxyz";
  3645. }
  3646. thing = (int) lrand48() % strlen(cmd1); // pick a movement command
  3647. c = cmd1[thing];
  3648. readbuffer[rbi++] = c; // add movement to input buffer
  3649. }
  3650. if (strchr("iIaAsc", cm)) { // multi-char commands
  3651. if (cm == 'c') {
  3652. // change some thing
  3653. thing = (int) lrand48() % strlen(cmd1); // pick a movement command
  3654. c = cmd1[thing];
  3655. readbuffer[rbi++] = c; // add movement to input buffer
  3656. }
  3657. thing = (int) lrand48() % 4; // what thing to insert
  3658. cnt = (int) lrand48() % 10; // how many to insert
  3659. for (i = 0; i < cnt; i++) {
  3660. if (thing == 0) { // insert chars
  3661. readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
  3662. } else if (thing == 1) { // insert words
  3663. strcat(readbuffer, words[(int) lrand48() % 20]);
  3664. strcat(readbuffer, " ");
  3665. sleeptime = 0; // how fast to type
  3666. } else if (thing == 2) { // insert lines
  3667. strcat(readbuffer, lines[(int) lrand48() % 20]);
  3668. sleeptime = 0; // how fast to type
  3669. } else { // insert multi-lines
  3670. strcat(readbuffer, multilines[(int) lrand48() % 20]);
  3671. sleeptime = 0; // how fast to type
  3672. }
  3673. }
  3674. strcat(readbuffer, "\033");
  3675. }
  3676. readed_for_parse = strlen(readbuffer);
  3677. cd1:
  3678. totalcmds++;
  3679. if (sleeptime > 0)
  3680. mysleep(sleeptime); // sleep 1/100 sec
  3681. }
  3682. // test to see if there are any errors
  3683. static void crash_test()
  3684. {
  3685. static time_t oldtim;
  3686. time_t tim;
  3687. char d[2], msg[MAX_LINELEN];
  3688. msg[0] = '\0';
  3689. if (end < text) {
  3690. strcat(msg, "end<text ");
  3691. }
  3692. if (end > textend) {
  3693. strcat(msg, "end>textend ");
  3694. }
  3695. if (dot < text) {
  3696. strcat(msg, "dot<text ");
  3697. }
  3698. if (dot > end) {
  3699. strcat(msg, "dot>end ");
  3700. }
  3701. if (screenbegin < text) {
  3702. strcat(msg, "screenbegin<text ");
  3703. }
  3704. if (screenbegin > end - 1) {
  3705. strcat(msg, "screenbegin>end-1 ");
  3706. }
  3707. if (msg[0]) {
  3708. alarm(0);
  3709. printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
  3710. totalcmds, last_input_char, msg, SOs, SOn);
  3711. fflush(stdout);
  3712. while (safe_read(0, d, 1) > 0) {
  3713. if (d[0] == '\n' || d[0] == '\r')
  3714. break;
  3715. }
  3716. alarm(3);
  3717. }
  3718. tim = (time_t) time((time_t *) 0);
  3719. if (tim >= (oldtim + 3)) {
  3720. sprintf(status_buffer,
  3721. "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
  3722. totalcmds, M, N, I, D, Y, P, U, end - text + 1);
  3723. oldtim = tim;
  3724. }
  3725. }
  3726. #endif