3
0

vi.c 106 KB

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