terminal.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "putty.h"
  5. #define CL_ANSIMIN 0x0001 /* Everybody has these even MSDOS */
  6. #define CL_VT100 0x0002 /* VT100 */
  7. #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
  8. #define CL_VT102 0x0008 /* VT102 */
  9. #define CL_VT220 0x0010 /* VT220 */
  10. #define CL_VT320 0x0020 /* VT320 */
  11. #define CL_VT420 0x0040 /* VT420 */
  12. #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
  13. #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
  14. #define CL_ANSI 0x1000 /* ANSI ECMA-48 not in the VT100..VT420 */
  15. #define CL_OTHER 0x2000 /* Others, Xterm, linux, putty, dunno, etc */
  16. #define TM_ANSIMIN (CL_ANSIMIN)
  17. #define TM_VT100 (CL_ANSIMIN+CL_VT100)
  18. #define TM_VT100AVO (TM_VT100+CL_VT100AVO)
  19. #define TM_VT102 (TM_VT100AVO+CL_VT102)
  20. #define TM_PUTTY (-1)
  21. #define compatibility(x) \
  22. if ( ((CL_##x)&compatibility_level) == 0 ) { \
  23. termstate=TOPLEVEL; \
  24. break; \
  25. }
  26. #define has_compat(x) ( ((CL_##x)&compatibility_level) != 0 )
  27. static int compatibility_level = TM_PUTTY;
  28. static unsigned long *text; /* buffer of text on terminal screen */
  29. static unsigned long *scrtop; /* top of working screen */
  30. static unsigned long *disptop; /* top of displayed screen */
  31. static unsigned long *sbtop; /* top of scrollback */
  32. static unsigned long *cpos; /* cursor position (convenience) */
  33. static unsigned long *disptext; /* buffer of text on real screen */
  34. static unsigned long *wanttext; /* buffer of text we want on screen */
  35. static unsigned long *alttext; /* buffer of text on alt. screen */
  36. static unsigned char *selspace; /* buffer for building selections in */
  37. #define TSIZE (sizeof(*text))
  38. #define fix_cpos do { cpos = scrtop + curs_y * (cols+1) + curs_x; } while(0)
  39. static unsigned long curr_attr, save_attr;
  40. static unsigned long erase_char = ERASE_CHAR;
  41. static int curs_x, curs_y; /* cursor */
  42. static int save_x, save_y; /* saved cursor position */
  43. static int marg_t, marg_b; /* scroll margins */
  44. static int dec_om; /* DEC origin mode flag */
  45. static int wrap, wrapnext; /* wrap flags */
  46. static int insert; /* insert-mode flag */
  47. static int cset; /* 0 or 1: which char set */
  48. static int save_cset, save_csattr; /* saved with cursor position */
  49. static int rvideo; /* global reverse video flag */
  50. static int cursor_on; /* cursor enabled flag */
  51. static int reset_132; /* Flag ESC c resets to 80 cols */
  52. static int use_bce; /* Use Background coloured erase */
  53. static int blinker; /* When blinking is the cursor on ? */
  54. static int vt52_mode; /* Is VT100+ in vt52 mode ? */
  55. static unsigned long cset_attr[2];
  56. /*
  57. * Saved settings on the alternate screen.
  58. */
  59. static int alt_x, alt_y, alt_om, alt_wrap, alt_wnext, alt_ins, alt_cset;
  60. static int alt_t, alt_b;
  61. static int alt_which;
  62. #define ARGS_MAX 32 /* max # of esc sequence arguments */
  63. #define ARG_DEFAULT 0 /* if an arg isn't specified */
  64. #define def(a,d) ( (a) == ARG_DEFAULT ? (d) : (a) )
  65. static int esc_args[ARGS_MAX];
  66. static int esc_nargs;
  67. static int esc_query;
  68. #define ANSI(x,y) ((x)+((y)<<8))
  69. #define ANSI_QUE(x) ANSI(x,TRUE)
  70. #define OSC_STR_MAX 2048
  71. static int osc_strlen;
  72. static char osc_string[OSC_STR_MAX+1];
  73. static int osc_w;
  74. static unsigned char *tabs;
  75. static enum {
  76. TOPLEVEL,
  77. SEEN_ESC,
  78. SEEN_CSI,
  79. SEEN_OSC,
  80. SEEN_OSC_W,
  81. DO_CTRLS,
  82. IGNORE_NEXT,
  83. SET_GL, SET_GR,
  84. SEEN_OSC_P,
  85. OSC_STRING, OSC_MAYBE_ST,
  86. SEEN_ESCHASH,
  87. VT52_ESC,
  88. VT52_Y1,
  89. VT52_Y2
  90. } termstate;
  91. static enum {
  92. NO_SELECTION, ABOUT_TO, DRAGGING, SELECTED
  93. } selstate;
  94. static enum {
  95. SM_CHAR, SM_WORD, SM_LINE
  96. } selmode;
  97. static unsigned long *selstart, *selend, *selanchor;
  98. static short wordness[256] = {
  99. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 01 */
  100. 0,1,2,1,1,1,1,1,1,1,1,1,1,2,2,2, 2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1, /* 23 */
  101. 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,2, /* 45 */
  102. 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1, /* 67 */
  103. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 89 */
  104. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* AB */
  105. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* CD */
  106. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,1,2,2,2,2,2,2,2,2, /* EF */
  107. };
  108. static unsigned char sel_nl[] = SEL_NL;
  109. /*
  110. * Internal prototypes.
  111. */
  112. static void do_paint (Context, int);
  113. static void erase_lots (int, int, int);
  114. static void swap_screen (int);
  115. static void update_sbar (void);
  116. static void deselect (void);
  117. /*
  118. * Set up power-on settings for the terminal.
  119. */
  120. static void power_on(void) {
  121. curs_x = curs_y = alt_x = alt_y = save_x = save_y = 0;
  122. alt_t = marg_t = 0;
  123. if (rows != -1)
  124. alt_b = marg_b = rows - 1;
  125. else
  126. alt_b = marg_b = 0;
  127. if (cols != -1) {
  128. int i;
  129. for (i = 0; i < cols; i++)
  130. tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
  131. }
  132. alt_om = dec_om = cfg.dec_om;
  133. alt_wnext = wrapnext = alt_ins = insert = FALSE;
  134. alt_wrap = wrap = cfg.wrap_mode;
  135. alt_cset = cset = 0;
  136. cset_attr[0] = cset_attr[1] = ATTR_ASCII;
  137. rvideo = 0;
  138. cursor_on = 1;
  139. save_attr = curr_attr = ATTR_DEFAULT;
  140. app_cursor_keys = cfg.app_cursor;
  141. app_keypad_keys = cfg.app_keypad;
  142. use_bce = 0;
  143. erase_char = ERASE_CHAR;
  144. alt_which = 0;
  145. {
  146. int i;
  147. for (i = 0; i < 256; i++)
  148. wordness[i] = cfg.wordness[i];
  149. }
  150. if (text) {
  151. swap_screen (1);
  152. erase_lots (FALSE, TRUE, TRUE);
  153. swap_screen (0);
  154. erase_lots (FALSE, TRUE, TRUE);
  155. }
  156. }
  157. /*
  158. * Force a screen update.
  159. */
  160. void term_update(void) {
  161. Context ctx;
  162. ctx = get_ctx();
  163. if (ctx) {
  164. if ( (seen_key_event && (unscroll_event & US_KEY)) ||
  165. (seen_disp_event && (unscroll_event & US_DISP)) ) {
  166. disptop = scrtop;
  167. seen_disp_event = seen_key_event = 0;
  168. }
  169. do_paint (ctx, TRUE);
  170. free_ctx (ctx);
  171. }
  172. }
  173. /*
  174. * Same as power_on(), but an external function.
  175. */
  176. void term_pwron(void) {
  177. power_on();
  178. fix_cpos;
  179. disptop = scrtop;
  180. deselect();
  181. term_update();
  182. }
  183. /*
  184. * Clear the scrollback.
  185. */
  186. void term_clrsb(void) {
  187. disptop = sbtop = scrtop;
  188. update_sbar();
  189. }
  190. /*
  191. * Initialise the terminal.
  192. */
  193. void term_init(void) {
  194. text = sbtop = scrtop = disptop = cpos = NULL;
  195. disptext = wanttext = NULL;
  196. tabs = NULL;
  197. selspace = NULL;
  198. deselect();
  199. rows = cols = -1;
  200. power_on();
  201. }
  202. /*
  203. * Set up the terminal for a given size.
  204. */
  205. void term_size(int newrows, int newcols, int newsavelines) {
  206. unsigned long *newtext, *newdisp, *newwant, *newalt;
  207. int i, j, crows, ccols;
  208. int save_alt_which = alt_which;
  209. if (newrows == rows && newcols == cols && newsavelines == savelines)
  210. return; /* nothing to do */
  211. deselect();
  212. swap_screen(0);
  213. alt_t = marg_t = 0;
  214. alt_b = marg_b = newrows - 1;
  215. newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
  216. disptop = newtext + newsavelines*(newcols+1);
  217. for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
  218. newtext[i] = ERASE_CHAR;
  219. if (rows != -1) {
  220. crows = rows + (scrtop - sbtop) / (cols+1);
  221. if (crows > newrows+newsavelines)
  222. crows = newrows+newsavelines;
  223. ccols = (cols < newcols ? cols : newcols);
  224. for (i=0; i<crows; i++) {
  225. int oldidx = (rows + savelines - crows + i) * (cols+1);
  226. int newidx = (newrows + newsavelines - crows + i) * (newcols+1);
  227. for (j=0; j<ccols; j++)
  228. newtext[newidx+j] = text[oldidx+j];
  229. newtext[newidx+newcols] =
  230. (cols == newcols ? text[oldidx+cols] : 0);
  231. }
  232. sbtop = disptop - (crows - newrows) * (newcols+1);
  233. if (sbtop > disptop)
  234. sbtop = disptop;
  235. } else
  236. sbtop = disptop;
  237. scrtop = disptop;
  238. sfree (text);
  239. text = newtext;
  240. newdisp = smalloc (newrows*(newcols+1)*TSIZE);
  241. for (i=0; i<newrows*(newcols+1); i++)
  242. newdisp[i] = ATTR_INVALID;
  243. sfree (disptext);
  244. disptext = newdisp;
  245. newwant = smalloc (newrows*(newcols+1)*TSIZE);
  246. for (i=0; i<newrows*(newcols+1); i++)
  247. newwant[i] = ATTR_INVALID;
  248. sfree (wanttext);
  249. wanttext = newwant;
  250. newalt = smalloc (newrows*(newcols+1)*TSIZE);
  251. for (i=0; i<newrows*(newcols+1); i++)
  252. newalt[i] = erase_char;
  253. sfree (alttext);
  254. alttext = newalt;
  255. sfree (selspace);
  256. selspace = smalloc ( (newrows+newsavelines) * (newcols+sizeof(sel_nl)) );
  257. tabs = srealloc (tabs, newcols*sizeof(*tabs));
  258. {
  259. int i;
  260. for (i = (cols > 0 ? cols : 0); i < newcols; i++)
  261. tabs[i] = (i % 8 == 0 ? TRUE : FALSE);
  262. }
  263. if (rows > 0)
  264. curs_y += newrows - rows;
  265. if (curs_y < 0)
  266. curs_y = 0;
  267. if (curs_y >= newrows)
  268. curs_y = newrows-1;
  269. if (curs_x >= newcols)
  270. curs_x = newcols-1;
  271. alt_x = alt_y = 0;
  272. wrapnext = alt_wnext = FALSE;
  273. rows = newrows;
  274. cols = newcols;
  275. savelines = newsavelines;
  276. fix_cpos;
  277. swap_screen(save_alt_which);
  278. update_sbar();
  279. term_update();
  280. }
  281. /*
  282. * Swap screens.
  283. */
  284. static void swap_screen (int which) {
  285. int t;
  286. unsigned long tt;
  287. if (which == alt_which)
  288. return;
  289. alt_which = which;
  290. for (t=0; t<rows*(cols+1); t++) {
  291. tt = scrtop[t]; scrtop[t] = alttext[t]; alttext[t] = tt;
  292. }
  293. t = curs_x; curs_x = alt_x; alt_x = t;
  294. t = curs_y; curs_y = alt_y; alt_y = t;
  295. t = marg_t; marg_t = alt_t; alt_t = t;
  296. t = marg_b; marg_b = alt_b; alt_b = t;
  297. t = dec_om; dec_om = alt_om; alt_om = t;
  298. t = wrap; wrap = alt_wrap; alt_wrap = t;
  299. t = wrapnext; wrapnext = alt_wnext; alt_wnext = t;
  300. t = insert; insert = alt_ins; alt_ins = t;
  301. t = cset; cset = alt_cset; alt_cset = t;
  302. fix_cpos;
  303. }
  304. /*
  305. * Retrieve a character from `inbuf'.
  306. */
  307. static int inbuf_getc(void) {
  308. if (inbuf_head == inbuf_reap)
  309. return -1; /* EOF */
  310. else {
  311. int n = inbuf_reap;
  312. inbuf_reap = (inbuf_reap+1) & INBUF_MASK;
  313. return inbuf[n];
  314. }
  315. }
  316. /*
  317. * Update the scroll bar.
  318. */
  319. static void update_sbar(void) {
  320. int min;
  321. min = (sbtop - text) / (cols+1);
  322. set_sbar ((scrtop - text) / (cols+1) + rows - min,
  323. (disptop - text) / (cols+1) - min,
  324. rows);
  325. }
  326. /*
  327. * Check whether the region bounded by the two pointers intersects
  328. * the scroll region, and de-select the on-screen selection if so.
  329. */
  330. static void check_selection (unsigned long *from, unsigned long *to) {
  331. if (from < selend && selstart < to)
  332. deselect();
  333. }
  334. /*
  335. * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
  336. * for backward.) `sb' is TRUE if the scrolling is permitted to
  337. * affect the scrollback buffer.
  338. */
  339. static void scroll (int topline, int botline, int lines, int sb) {
  340. unsigned long *scroll_top;
  341. int scroll_size, size, i;
  342. scroll_top = scrtop + topline*(cols+1);
  343. size = (lines < 0 ? -lines : lines) * (cols+1);
  344. scroll_size = (botline - topline + 1) * (cols+1) - size;
  345. if (lines > 0 && topline == 0 && alt_which == 0 && sb) {
  346. /*
  347. * Since we're going to scroll the top line and we're on the
  348. * scrolling screen let's also affect the scrollback buffer.
  349. */
  350. sbtop -= lines * (cols+1);
  351. if (sbtop < text)
  352. sbtop = text;
  353. scroll_size += scroll_top - sbtop;
  354. scroll_top = sbtop;
  355. update_sbar();
  356. }
  357. if (scroll_size < 0) {
  358. size += scroll_size;
  359. scroll_size = 0;
  360. }
  361. if (lines > 0) {
  362. if (scroll_size)
  363. memmove (scroll_top, scroll_top + size, scroll_size*TSIZE);
  364. for (i = 0; i < size; i++)
  365. scroll_top[i+scroll_size] = erase_char;
  366. if (selstart > scroll_top &&
  367. selstart < scroll_top + size + scroll_size) {
  368. selstart -= size;
  369. if (selstart < scroll_top)
  370. selstart = scroll_top;
  371. }
  372. if (selend > scroll_top &&
  373. selend < scroll_top + size + scroll_size) {
  374. selend -= size;
  375. if (selend < scroll_top)
  376. selend = scroll_top;
  377. }
  378. } else {
  379. if (scroll_size)
  380. memmove (scroll_top + size, scroll_top, scroll_size*TSIZE);
  381. for (i = 0; i < size; i++)
  382. scroll_top[i] = erase_char;
  383. if (selstart > scroll_top &&
  384. selstart < scroll_top + size + scroll_size) {
  385. selstart += size;
  386. if (selstart > scroll_top + size + scroll_size)
  387. selstart = scroll_top + size + scroll_size;
  388. }
  389. if (selend > scroll_top &&
  390. selend < scroll_top + size + scroll_size) {
  391. selend += size;
  392. if (selend > scroll_top + size + scroll_size)
  393. selend = scroll_top + size + scroll_size;
  394. }
  395. }
  396. }
  397. /*
  398. * Move the cursor to a given position, clipping at boundaries. We
  399. * may or may not want to clip at the scroll margin: marg_clip is 0
  400. * not to, 1 to disallow _passing_ the margins, and 2 to disallow
  401. * even _being_ outside the margins.
  402. */
  403. static void move (int x, int y, int marg_clip) {
  404. if (x < 0)
  405. x = 0;
  406. if (x >= cols)
  407. x = cols-1;
  408. if (marg_clip) {
  409. if ((curs_y >= marg_t || marg_clip == 2) && y < marg_t)
  410. y = marg_t;
  411. if ((curs_y <= marg_b || marg_clip == 2) && y > marg_b)
  412. y = marg_b;
  413. }
  414. if (y < 0)
  415. y = 0;
  416. if (y >= rows)
  417. y = rows-1;
  418. curs_x = x;
  419. curs_y = y;
  420. fix_cpos;
  421. wrapnext = FALSE;
  422. }
  423. /*
  424. * Save or restore the cursor and SGR mode.
  425. */
  426. static void save_cursor(int save) {
  427. if (save) {
  428. save_x = curs_x;
  429. save_y = curs_y;
  430. save_attr = curr_attr;
  431. save_cset = cset;
  432. save_csattr = cset_attr[cset];
  433. } else {
  434. curs_x = save_x;
  435. curs_y = save_y;
  436. /* Make sure the window hasn't shrunk since the save */
  437. if (curs_x >= cols) curs_x = cols-1;
  438. if (curs_y >= rows) curs_y = rows-1;
  439. curr_attr = save_attr;
  440. cset = save_cset;
  441. cset_attr[cset] = save_csattr;
  442. fix_cpos;
  443. if (use_bce) erase_char = (' ' |(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
  444. }
  445. }
  446. /*
  447. * Erase a large portion of the screen: the whole screen, or the
  448. * whole line, or parts thereof.
  449. */
  450. static void erase_lots (int line_only, int from_begin, int to_end) {
  451. unsigned long *startpos, *endpos;
  452. if (line_only) {
  453. startpos = cpos - curs_x;
  454. endpos = startpos + cols+1;
  455. } else {
  456. startpos = scrtop;
  457. endpos = startpos + rows * (cols+1);
  458. }
  459. if (!from_begin)
  460. startpos = cpos;
  461. if (!to_end)
  462. endpos = cpos+1;
  463. check_selection (startpos, endpos);
  464. /* Clear screen also forces a full window redraw, just in case. */
  465. if (startpos == scrtop && endpos == scrtop + rows * (cols+1))
  466. term_invalidate();
  467. while (startpos < endpos)
  468. *startpos++ = erase_char;
  469. }
  470. /*
  471. * Insert or delete characters within the current line. n is +ve if
  472. * insertion is desired, and -ve for deletion.
  473. */
  474. static void insch (int n) {
  475. int dir = (n < 0 ? -1 : +1);
  476. int m;
  477. n = (n < 0 ? -n : n);
  478. if (n > cols - curs_x)
  479. n = cols - curs_x;
  480. m = cols - curs_x - n;
  481. check_selection (cpos, cpos+n);
  482. if (dir < 0) {
  483. memmove (cpos, cpos+n, m*TSIZE);
  484. while (n--)
  485. cpos[m++] = erase_char;
  486. } else {
  487. memmove (cpos+n, cpos, m*TSIZE);
  488. while (n--)
  489. cpos[n] = erase_char;
  490. }
  491. }
  492. /*
  493. * Toggle terminal mode `mode' to state `state'. (`query' indicates
  494. * whether the mode is a DEC private one or a normal one.)
  495. */
  496. static void toggle_mode (int mode, int query, int state) {
  497. if (query) switch (mode) {
  498. case 1: /* application cursor keys */
  499. app_cursor_keys = state;
  500. break;
  501. case 2: /* VT52 mode */
  502. vt52_mode = !state;
  503. break;
  504. case 3: /* 80/132 columns */
  505. deselect();
  506. request_resize (state ? 132 : 80, rows, 1);
  507. reset_132 = state;
  508. break;
  509. case 5: /* reverse video */
  510. rvideo = state;
  511. seen_disp_event = TRUE;
  512. if (state) term_update();
  513. break;
  514. case 6: /* DEC origin mode */
  515. dec_om = state;
  516. break;
  517. case 7: /* auto wrap */
  518. wrap = state;
  519. break;
  520. case 25: /* enable/disable cursor */
  521. compatibility(VT220);
  522. cursor_on = state;
  523. seen_disp_event = TRUE;
  524. break;
  525. case 47: /* alternate screen */
  526. compatibility(OTHER);
  527. deselect();
  528. swap_screen (state);
  529. disptop = scrtop;
  530. break;
  531. } else switch (mode) {
  532. case 4: /* set insert mode */
  533. compatibility(VT102);
  534. insert = state;
  535. break;
  536. case 12: /* set echo mode */
  537. /*
  538. * This may be very good in smcup and rmcup (or smkx & rmkx) if you
  539. * have a long RTT and the telnet client/daemon doesn't understand
  540. * linemode.
  541. *
  542. * DONT send TS_RECHO/TS_LECHO; the telnet daemon tries to fix the
  543. * tty and _really_ confuses some programs.
  544. */
  545. compatibility(VT220);
  546. ldisc = (state? &ldisc_simple : &ldisc_term);
  547. break;
  548. }
  549. }
  550. /*
  551. * Process an OSC sequence: set window title or icon name.
  552. */
  553. static void do_osc(void) {
  554. if (osc_w) {
  555. while (osc_strlen--)
  556. wordness[(unsigned char)osc_string[osc_strlen]] = esc_args[0];
  557. } else {
  558. osc_string[osc_strlen] = '\0';
  559. switch (esc_args[0]) {
  560. case 0:
  561. case 1:
  562. set_icon (osc_string);
  563. if (esc_args[0] == 1)
  564. break;
  565. /* fall through: parameter 0 means set both */
  566. case 2:
  567. case 21:
  568. set_title (osc_string);
  569. break;
  570. }
  571. }
  572. }
  573. /*
  574. * Remove everything currently in `inbuf' and stick it up on the
  575. * in-memory display. There's a big state machine in here to
  576. * process escape sequences...
  577. */
  578. void term_out(void) {
  579. int c;
  580. while ( (c = inbuf_getc()) != -1) {
  581. /*
  582. * Optionally log the session traffic to a file. Useful for
  583. * debugging and possibly also useful for actual logging.
  584. */
  585. if (logfile) {
  586. static FILE *fp = NULL;
  587. if (!fp) fp = fopen(logfile, "wb");
  588. if (fp) fputc (c, fp);
  589. }
  590. /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
  591. * be able to display 8-bit characters, but I'll let that go 'cause
  592. * of i18n.
  593. */
  594. if( (c&0x60) == 0 && termstate < DO_CTRLS &&
  595. ( (c&0x80) == 0 || has_compat(VT220))) {
  596. switch (c) {
  597. case '\005': /* terminal type query */
  598. /* Strictly speaking this is VT100 but a VT100 defaults to
  599. * no response. Other terminals respond at their option.
  600. *
  601. * Don't put a CR in the default string as this tends to
  602. * upset some weird software.
  603. *
  604. * An xterm returns "xterm" (5 characters)
  605. */
  606. compatibility(OTHER);
  607. ldisc->send ("PuTTY", 5);
  608. break;
  609. case '\007':
  610. if(cfg.beep) beep();
  611. disptop = scrtop;
  612. break;
  613. case '\b':
  614. if (curs_x == 0 && curs_y > 0)
  615. curs_x = cols-1, curs_y--;
  616. else if (wrapnext)
  617. wrapnext = FALSE;
  618. else
  619. curs_x--;
  620. fix_cpos;
  621. seen_disp_event = TRUE;
  622. break;
  623. case '\016':
  624. compatibility(VT100);
  625. cset = 1;
  626. break;
  627. case '\017':
  628. compatibility(VT100);
  629. cset = 0;
  630. break;
  631. case '\033':
  632. if (vt52_mode)
  633. termstate = VT52_ESC;
  634. else {
  635. compatibility(ANSIMIN);
  636. termstate = SEEN_ESC;
  637. }
  638. break;
  639. case 0233:
  640. compatibility(VT220);
  641. termstate = SEEN_CSI;
  642. esc_nargs = 1;
  643. esc_args[0] = ARG_DEFAULT;
  644. esc_query = FALSE;
  645. break;
  646. case 0235:
  647. compatibility(VT220);
  648. termstate = SEEN_OSC;
  649. esc_args[0] = 0;
  650. break;
  651. case '\r':
  652. curs_x = 0;
  653. wrapnext = FALSE;
  654. fix_cpos;
  655. seen_disp_event = TRUE;
  656. break;
  657. case '\013':
  658. case '\014':
  659. compatibility(VT100);
  660. case '\n':
  661. if (curs_y == marg_b)
  662. scroll (marg_t, marg_b, 1, TRUE);
  663. else if (curs_y < rows-1)
  664. curs_y++;
  665. if (cfg.lfhascr)
  666. curs_x = 0;
  667. fix_cpos;
  668. wrapnext = FALSE;
  669. seen_disp_event = 1;
  670. break;
  671. case '\t':
  672. do {
  673. curs_x++;
  674. } while (curs_x < cols-1 && !tabs[curs_x]);
  675. if (curs_x >= cols)
  676. curs_x = cols-1;
  677. {
  678. unsigned long *old_cpos = cpos;
  679. fix_cpos;
  680. check_selection (old_cpos, cpos);
  681. }
  682. seen_disp_event = TRUE;
  683. break;
  684. }
  685. }
  686. else switch (termstate) {
  687. case TOPLEVEL:
  688. /* Only graphic characters get this far, ctrls are stripped above */
  689. if (wrapnext) {
  690. cpos[1] = ATTR_WRAPPED;
  691. if (curs_y == marg_b)
  692. scroll (marg_t, marg_b, 1, TRUE);
  693. else if (curs_y < rows-1)
  694. curs_y++;
  695. curs_x = 0;
  696. fix_cpos;
  697. wrapnext = FALSE;
  698. }
  699. if (insert)
  700. insch (1);
  701. if (selstate != NO_SELECTION)
  702. check_selection (cpos, cpos+1);
  703. switch (cset_attr[cset]) {
  704. /* Linedraw characters are different from 'ESC ( B' only
  705. * for a small range, for ones outside that range make sure
  706. * we use the same font as well as the same encoding.
  707. */
  708. case ATTR_LINEDRW:
  709. if (c<0x60 || c>0x7F)
  710. *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
  711. ATTR_ASCII;
  712. else
  713. *cpos++ = ((unsigned char)c) | curr_attr | ATTR_LINEDRW;
  714. break;
  715. default:
  716. *cpos++ = xlat_tty2scr((unsigned char)c) | curr_attr |
  717. (c <= 0x7F ? cset_attr[cset] : ATTR_ASCII);
  718. break;
  719. }
  720. curs_x++;
  721. if (curs_x == cols) {
  722. cpos--;
  723. curs_x--;
  724. wrapnext = wrap;
  725. }
  726. seen_disp_event = 1;
  727. break;
  728. case IGNORE_NEXT:
  729. termstate = TOPLEVEL;
  730. break;
  731. case OSC_MAYBE_ST:
  732. /*
  733. * This state is virtually identical to SEEN_ESC, with the
  734. * exception that we have an OSC sequence in the pipeline,
  735. * and _if_ we see a backslash, we process it.
  736. */
  737. if (c == '\\') {
  738. do_osc();
  739. termstate = TOPLEVEL;
  740. break;
  741. }
  742. /* else fall through */
  743. case SEEN_ESC:
  744. termstate = TOPLEVEL;
  745. switch (c) {
  746. case ' ': /* some weird sequence? */
  747. compatibility(VT220);
  748. termstate = IGNORE_NEXT;
  749. break;
  750. case '[': /* enter CSI mode */
  751. termstate = SEEN_CSI;
  752. esc_nargs = 1;
  753. esc_args[0] = ARG_DEFAULT;
  754. esc_query = FALSE;
  755. break;
  756. case ']': /* xterm escape sequences */
  757. /* Compatibility is nasty here, xterm, linux, decterm yuk! */
  758. compatibility(OTHER);
  759. termstate = SEEN_OSC;
  760. esc_args[0] = 0;
  761. break;
  762. case '(': /* should set GL */
  763. compatibility(VT100);
  764. termstate = SET_GL;
  765. break;
  766. case ')': /* should set GR */
  767. compatibility(VT100);
  768. termstate = SET_GR;
  769. break;
  770. case '7': /* save cursor */
  771. compatibility(VT100);
  772. save_cursor (TRUE);
  773. break;
  774. case '8': /* restore cursor */
  775. compatibility(VT100);
  776. save_cursor (FALSE);
  777. seen_disp_event = TRUE;
  778. break;
  779. case '=':
  780. compatibility(VT100);
  781. app_keypad_keys = TRUE;
  782. break;
  783. case '>':
  784. compatibility(VT100);
  785. app_keypad_keys = FALSE;
  786. break;
  787. case 'D': /* exactly equivalent to LF */
  788. compatibility(VT100);
  789. if (curs_y == marg_b)
  790. scroll (marg_t, marg_b, 1, TRUE);
  791. else if (curs_y < rows-1)
  792. curs_y++;
  793. fix_cpos;
  794. wrapnext = FALSE;
  795. seen_disp_event = TRUE;
  796. break;
  797. case 'E': /* exactly equivalent to CR-LF */
  798. compatibility(VT100);
  799. curs_x = 0;
  800. wrapnext = FALSE;
  801. if (curs_y == marg_b)
  802. scroll (marg_t, marg_b, 1, TRUE);
  803. else if (curs_y < rows-1)
  804. curs_y++;
  805. fix_cpos;
  806. wrapnext = FALSE;
  807. seen_disp_event = TRUE;
  808. break;
  809. case 'M': /* reverse index - backwards LF */
  810. compatibility(VT100);
  811. if (curs_y == marg_t)
  812. scroll (marg_t, marg_b, -1, TRUE);
  813. else if (curs_y > 0)
  814. curs_y--;
  815. fix_cpos;
  816. wrapnext = FALSE;
  817. seen_disp_event = TRUE;
  818. break;
  819. case 'Z': /* terminal type query */
  820. compatibility(VT100);
  821. ldisc->send ("\033[?6c", 5);
  822. break;
  823. case 'c': /* restore power-on settings */
  824. compatibility(VT100);
  825. power_on();
  826. if (reset_132) {
  827. request_resize (80, rows, 1);
  828. reset_132 = 0;
  829. }
  830. fix_cpos;
  831. disptop = scrtop;
  832. seen_disp_event = TRUE;
  833. break;
  834. case '#': /* ESC # 8 fills screen with Es :-) */
  835. compatibility(VT100);
  836. termstate = SEEN_ESCHASH;
  837. break;
  838. case 'H': /* set a tab */
  839. compatibility(VT100);
  840. tabs[curs_x] = TRUE;
  841. break;
  842. }
  843. break;
  844. case SEEN_CSI:
  845. termstate = TOPLEVEL; /* default */
  846. if( isdigit(c) )
  847. {
  848. if (esc_nargs <= ARGS_MAX) {
  849. if (esc_args[esc_nargs-1] == ARG_DEFAULT)
  850. esc_args[esc_nargs-1] = 0;
  851. esc_args[esc_nargs-1] =
  852. 10 * esc_args[esc_nargs-1] + c - '0';
  853. }
  854. termstate = SEEN_CSI;
  855. }
  856. else if( c == ';' )
  857. {
  858. if (++esc_nargs <= ARGS_MAX)
  859. esc_args[esc_nargs-1] = ARG_DEFAULT;
  860. termstate = SEEN_CSI;
  861. }
  862. else if( c < '@' )
  863. {
  864. if( esc_query ) esc_query = -1;
  865. else if( c == '?' ) esc_query = TRUE;
  866. else esc_query = c;
  867. termstate = SEEN_CSI;
  868. }
  869. else switch (ANSI(c,esc_query)) {
  870. case 'A': /* move up N lines */
  871. move (curs_x, curs_y - def(esc_args[0], 1), 1);
  872. seen_disp_event = TRUE;
  873. break;
  874. case 'e': /* move down N lines */
  875. compatibility(ANSI);
  876. case 'B':
  877. move (curs_x, curs_y + def(esc_args[0], 1), 1);
  878. seen_disp_event = TRUE;
  879. break;
  880. case 'a': /* move right N cols */
  881. compatibility(ANSI);
  882. case 'C':
  883. move (curs_x + def(esc_args[0], 1), curs_y, 1);
  884. seen_disp_event = TRUE;
  885. break;
  886. case 'D': /* move left N cols */
  887. move (curs_x - def(esc_args[0], 1), curs_y, 1);
  888. seen_disp_event = TRUE;
  889. break;
  890. case 'E': /* move down N lines and CR */
  891. compatibility(ANSI);
  892. move (0, curs_y + def(esc_args[0], 1), 1);
  893. seen_disp_event = TRUE;
  894. break;
  895. case 'F': /* move up N lines and CR */
  896. compatibility(ANSI);
  897. move (0, curs_y - def(esc_args[0], 1), 1);
  898. seen_disp_event = TRUE;
  899. break;
  900. case 'G': case '`': /* set horizontal posn */
  901. compatibility(ANSI);
  902. move (def(esc_args[0], 1) - 1, curs_y, 0);
  903. seen_disp_event = TRUE;
  904. break;
  905. case 'd': /* set vertical posn */
  906. compatibility(ANSI);
  907. move (curs_x, (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
  908. (dec_om ? 2 : 0));
  909. seen_disp_event = TRUE;
  910. break;
  911. case 'H': case 'f': /* set horz and vert posns at once */
  912. if (esc_nargs < 2)
  913. esc_args[1] = ARG_DEFAULT;
  914. move (def(esc_args[1], 1) - 1,
  915. (dec_om ? marg_t : 0) + def(esc_args[0], 1) - 1,
  916. (dec_om ? 2 : 0));
  917. seen_disp_event = TRUE;
  918. break;
  919. case 'J': /* erase screen or parts of it */
  920. {
  921. unsigned int i = def(esc_args[0], 0) + 1;
  922. if (i > 3)
  923. i = 0;
  924. erase_lots(FALSE, !!(i & 2), !!(i & 1));
  925. }
  926. disptop = scrtop;
  927. seen_disp_event = TRUE;
  928. break;
  929. case 'K': /* erase line or parts of it */
  930. {
  931. unsigned int i = def(esc_args[0], 0) + 1;
  932. if (i > 3)
  933. i = 0;
  934. erase_lots(TRUE, !!(i & 2), !!(i & 1));
  935. }
  936. seen_disp_event = TRUE;
  937. break;
  938. case 'L': /* insert lines */
  939. compatibility(VT102);
  940. if (curs_y <= marg_b)
  941. scroll (curs_y, marg_b, -def(esc_args[0], 1), FALSE);
  942. seen_disp_event = TRUE;
  943. break;
  944. case 'M': /* delete lines */
  945. compatibility(VT102);
  946. if (curs_y <= marg_b)
  947. scroll (curs_y, marg_b, def(esc_args[0], 1), TRUE);
  948. seen_disp_event = TRUE;
  949. break;
  950. case '@': /* insert chars */
  951. /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
  952. compatibility(VT102);
  953. insch (def(esc_args[0], 1));
  954. seen_disp_event = TRUE;
  955. break;
  956. case 'P': /* delete chars */
  957. compatibility(VT102);
  958. insch (-def(esc_args[0], 1));
  959. seen_disp_event = TRUE;
  960. break;
  961. case 'c': /* terminal type query */
  962. compatibility(VT100);
  963. /* This is the response for a VT102 */
  964. ldisc->send ("\033[?6c", 5);
  965. break;
  966. case 'n': /* cursor position query */
  967. if (esc_args[0] == 6) {
  968. /* Wonder of wonders ANSI.SYS has this!! */
  969. char buf[32];
  970. sprintf (buf, "\033[%d;%dR", curs_y + 1, curs_x + 1);
  971. ldisc->send (buf, strlen(buf));
  972. }
  973. else if (esc_args[0] == 5) {
  974. /* Are we working ... well I suppose so :-) */
  975. /* But ANSI.SYS doesn't say it's working :-) :-) */
  976. compatibility(VT100);
  977. ldisc->send ("\033[0n", 4);
  978. }
  979. break;
  980. case 'h': /* toggle modes to high */
  981. case ANSI_QUE('h'):
  982. compatibility(VT100);
  983. {
  984. int i;
  985. for (i=0; i<esc_nargs; i++)
  986. toggle_mode (esc_args[i], esc_query, TRUE);
  987. }
  988. break;
  989. case 'l': /* toggle modes to low */
  990. case ANSI_QUE('l'):
  991. compatibility(VT100);
  992. {
  993. int i;
  994. for (i=0; i<esc_nargs; i++)
  995. toggle_mode (esc_args[i], esc_query, FALSE);
  996. }
  997. break;
  998. case 'g': /* clear tabs */
  999. compatibility(VT100);
  1000. if (esc_nargs == 1) {
  1001. if (esc_args[0] == 0) {
  1002. tabs[curs_x] = FALSE;
  1003. } else if (esc_args[0] == 3) {
  1004. int i;
  1005. for (i = 0; i < cols; i++)
  1006. tabs[i] = FALSE;
  1007. }
  1008. }
  1009. break;
  1010. case 'r': /* set scroll margins */
  1011. compatibility(VT100);
  1012. if (esc_nargs <= 2) {
  1013. int top, bot;
  1014. /* VTTEST Bug 9 if the first arg is default _or_ zero
  1015. * this is a full screen region irrespective of 2nd arg.
  1016. */
  1017. if (esc_args[0] <= 0) {
  1018. top=0;
  1019. bot=rows-1;
  1020. } else {
  1021. top = def(esc_args[0], 1) - 1;
  1022. bot = (esc_nargs <= 1 || esc_args[1] == 0 ? rows :
  1023. def(esc_args[1], rows)) - 1;
  1024. }
  1025. if (bot >= rows)
  1026. bot = rows-1;
  1027. if (top <= bot) {
  1028. marg_t = top;
  1029. marg_b = bot;
  1030. curs_x = 0;
  1031. /*
  1032. * I used to think the cursor should be
  1033. * placed at the top of the newly marginned
  1034. * area. Apparently not: VMS TPU falls over
  1035. * if so.
  1036. *
  1037. * Well actually it should for Origin mode - RDB
  1038. */
  1039. curs_y = (dec_om ? marg_t : 0);
  1040. fix_cpos;
  1041. seen_disp_event = TRUE;
  1042. }
  1043. }
  1044. break;
  1045. case 'm': /* set graphics rendition */
  1046. {
  1047. /*
  1048. * A VT100 without the AVO only had one attribute, either
  1049. * underline or reverse video depending on the cursor type,
  1050. * this was selected by CSI 7m.
  1051. *
  1052. * case 2:
  1053. * This is DIM on the VT100-AVO and VT102
  1054. * case 5:
  1055. * This is BLINK on the VT100-AVO and VT102+
  1056. * case 8:
  1057. * This is INVIS on the VT100-AVO and VT102
  1058. * case 21:
  1059. * This like 22 disables BOLD, DIM and INVIS
  1060. *
  1061. * The ANSI colours appear on any terminal that has colour
  1062. * (obviously) but the interaction between sgr0 and the
  1063. * colours varies but is usually related to the background
  1064. * colour erase item.
  1065. * The interaction between colour attributes and the mono
  1066. * ones is also very implementation dependent.
  1067. *
  1068. * The 39 and 49 attributes are likely to be unimplemented.
  1069. */
  1070. int i;
  1071. for (i=0; i<esc_nargs; i++) {
  1072. switch (def(esc_args[i], 0)) {
  1073. case 0: /* restore defaults */
  1074. curr_attr = ATTR_DEFAULT; break;
  1075. case 1: /* enable bold */
  1076. compatibility(VT100AVO);
  1077. curr_attr |= ATTR_BOLD; break;
  1078. case 21: /* (enable double underline) */
  1079. compatibility(OTHER);
  1080. case 4: /* enable underline */
  1081. compatibility(VT100AVO);
  1082. curr_attr |= ATTR_UNDER; break;
  1083. case 5: /* enable blink */
  1084. compatibility(VT100AVO);
  1085. curr_attr |= ATTR_BLINK; break;
  1086. case 7: /* enable reverse video */
  1087. curr_attr |= ATTR_REVERSE; break;
  1088. case 22: /* disable bold */
  1089. compatibility(VT220);
  1090. curr_attr &= ~ATTR_BOLD; break;
  1091. case 24: /* disable underline */
  1092. compatibility(VT220);
  1093. curr_attr &= ~ATTR_UNDER; break;
  1094. case 25: /* disable blink */
  1095. compatibility(VT220);
  1096. curr_attr &= ~ATTR_BLINK; break;
  1097. case 27: /* disable reverse video */
  1098. compatibility(VT220);
  1099. curr_attr &= ~ATTR_REVERSE; break;
  1100. case 30: case 31: case 32: case 33:
  1101. case 34: case 35: case 36: case 37:
  1102. /* foreground */
  1103. curr_attr &= ~ATTR_FGMASK;
  1104. curr_attr |= (esc_args[i] - 30) << ATTR_FGSHIFT;
  1105. break;
  1106. case 39: /* default-foreground */
  1107. curr_attr &= ~ATTR_FGMASK;
  1108. curr_attr |= ATTR_DEFFG;
  1109. break;
  1110. case 40: case 41: case 42: case 43:
  1111. case 44: case 45: case 46: case 47:
  1112. /* background */
  1113. curr_attr &= ~ATTR_BGMASK;
  1114. curr_attr |= (esc_args[i] - 40) << ATTR_BGSHIFT;
  1115. break;
  1116. case 49: /* default-background */
  1117. curr_attr &= ~ATTR_BGMASK;
  1118. curr_attr |= ATTR_DEFBG;
  1119. break;
  1120. }
  1121. }
  1122. if (use_bce)
  1123. erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
  1124. }
  1125. break;
  1126. case 's': /* save cursor */
  1127. save_cursor (TRUE);
  1128. break;
  1129. case 'u': /* restore cursor */
  1130. save_cursor (FALSE);
  1131. seen_disp_event = TRUE;
  1132. break;
  1133. case 't': /* set page size - ie window height */
  1134. /*
  1135. * VT340/VT420 sequence DECSLPP, DEC only allows values
  1136. * 24/25/36/48/72/144 other emulators (eg dtterm) use
  1137. * illegal values (eg first arg 1..9) for window changing
  1138. * and reports.
  1139. */
  1140. compatibility(VT340TEXT);
  1141. if (esc_nargs<=1 && (esc_args[0]<1 || esc_args[0]>=24)) {
  1142. unsigned int newrows = def(esc_args[0], 24);
  1143. /* Hack: prevent big-resize DoS attack. */
  1144. if (newrows > max(512, cfg.height))
  1145. newrows = max(512, cfg.height);
  1146. request_resize (cols, newrows, 0);
  1147. deselect();
  1148. }
  1149. break;
  1150. case ANSI('|', '*'):
  1151. /* VT420 sequence DECSNLS
  1152. * Set number of lines on screen
  1153. * VT420 uses VGA like hardware and can support any size in
  1154. * reasonable range (24..49 AIUI) with no default specified.
  1155. */
  1156. compatibility(VT420);
  1157. if (esc_nargs==1 && esc_args[0]>=24) {
  1158. unsigned int newrows = def(esc_args[0], cfg.height);
  1159. /* Hack: prevent big-resize DoS attack. */
  1160. if (newrows > max(512, cfg.height))
  1161. newrows = max(512, cfg.height);
  1162. request_resize (cols, newrows, 0);
  1163. deselect();
  1164. }
  1165. break;
  1166. case ANSI('|', '$'):
  1167. /* VT340/VT420 sequence DECSCPP
  1168. * Set number of columns per page
  1169. * Docs imply range is only 80 or 132, but I'll allow any.
  1170. */
  1171. compatibility(VT340TEXT);
  1172. if (esc_nargs<=1) {
  1173. unsigned int newcols = def(esc_args[0], cfg.width);
  1174. /* Hack: prevent big-resize DoS attack. */
  1175. if (newcols > max(512, cfg.width))
  1176. newcols = max(512, cfg.width);
  1177. request_resize (newcols, rows, 0);
  1178. deselect();
  1179. }
  1180. break;
  1181. case 'X': /* write N spaces w/o moving cursor */
  1182. /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
  1183. compatibility(VT100);
  1184. {
  1185. int n = def(esc_args[0], 1);
  1186. unsigned long *p = cpos;
  1187. if (n > cols - curs_x)
  1188. n = cols - curs_x;
  1189. check_selection (cpos, cpos+n);
  1190. while (n--)
  1191. *p++ = erase_char;
  1192. seen_disp_event = TRUE;
  1193. }
  1194. break;
  1195. case 'x': /* report terminal characteristics */
  1196. compatibility(VT100);
  1197. {
  1198. char buf[32];
  1199. int i = def(esc_args[0], 0);
  1200. if (i == 0 || i == 1) {
  1201. strcpy (buf, "\033[2;1;1;112;112;1;0x");
  1202. buf[2] += i;
  1203. ldisc->send (buf, 20);
  1204. }
  1205. }
  1206. break;
  1207. case ANSI('L','='):
  1208. compatibility(OTHER);
  1209. use_bce = (esc_args[0]<=0);
  1210. erase_char = ERASE_CHAR;
  1211. if (use_bce)
  1212. erase_char = (' '|(curr_attr&(ATTR_FGMASK|ATTR_BGMASK)));
  1213. break;
  1214. case ANSI('p','"'):
  1215. /* Allow the host to make this emulator a 'perfect' VT102.
  1216. * This first appeared in the VT220, but we do need to get
  1217. * back to PuTTY mode so I won't check it.
  1218. *
  1219. * The arg == 60 is a PuTTY extension.
  1220. * The 2nd arg, 8bit vs 7bit is not obeyed.
  1221. *
  1222. * Setting VT102 mode should also change the Fkeys to
  1223. * generate PF* codes as a real VT102 has no Fkeys.
  1224. * The VT220 does this, F11..F13 become ESC,BS,LF other Fkeys
  1225. * send nothing.
  1226. *
  1227. * Note ESC c will NOT change this!
  1228. */
  1229. if (esc_args[0] == 61) compatibility_level = TM_VT102;
  1230. else if (esc_args[0] == 60) compatibility_level = TM_ANSIMIN;
  1231. else compatibility_level = TM_PUTTY;
  1232. break;
  1233. }
  1234. break;
  1235. case SET_GL:
  1236. case SET_GR:
  1237. /* VT100 only here, checked above */
  1238. switch (c) {
  1239. case 'A':
  1240. cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_GBCHR;
  1241. break;
  1242. case '0':
  1243. cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_LINEDRW;
  1244. break;
  1245. case 'B':
  1246. default: /* specifically, 'B' */
  1247. cset_attr[termstate == SET_GL ? 0 : 1] = ATTR_ASCII;
  1248. break;
  1249. }
  1250. if( !has_compat(VT220) || c != '%' )
  1251. termstate = TOPLEVEL;
  1252. break;
  1253. case SEEN_OSC:
  1254. osc_w = FALSE;
  1255. switch (c) {
  1256. case 'P': /* Linux palette sequence */
  1257. termstate = SEEN_OSC_P;
  1258. osc_strlen = 0;
  1259. break;
  1260. case 'R': /* Linux palette reset */
  1261. palette_reset();
  1262. term_invalidate();
  1263. termstate = TOPLEVEL;
  1264. break;
  1265. case 'W': /* word-set */
  1266. termstate = SEEN_OSC_W;
  1267. osc_w = TRUE;
  1268. break;
  1269. case '0': case '1': case '2': case '3': case '4':
  1270. case '5': case '6': case '7': case '8': case '9':
  1271. esc_args[0] = 10 * esc_args[0] + c - '0';
  1272. break;
  1273. case 'L':
  1274. /*
  1275. * Grotty hack to support xterm and DECterm title
  1276. * sequences concurrently.
  1277. */
  1278. if (esc_args[0] == 2) {
  1279. esc_args[0] = 1;
  1280. break;
  1281. }
  1282. /* else fall through */
  1283. default:
  1284. termstate = OSC_STRING;
  1285. osc_strlen = 0;
  1286. }
  1287. break;
  1288. case OSC_STRING:
  1289. /*
  1290. * This OSC stuff is EVIL. It takes just one character to get into
  1291. * sysline mode and it's not initially obvious how to get out.
  1292. * So I've added CR and LF as string aborts.
  1293. * This shouldn't effect compatibility as I believe embedded
  1294. * control characters are supposed to be interpreted (maybe?)
  1295. * and they don't display anything useful anyway.
  1296. *
  1297. * -- RDB
  1298. */
  1299. if (c == '\n' || c == '\r') {
  1300. termstate = TOPLEVEL;
  1301. } else if (c == 0234 || c == '\007' ) {
  1302. /*
  1303. * These characters terminate the string; ST and BEL
  1304. * terminate the sequence and trigger instant
  1305. * processing of it, whereas ESC goes back to SEEN_ESC
  1306. * mode unless it is followed by \, in which case it is
  1307. * synonymous with ST in the first place.
  1308. */
  1309. do_osc();
  1310. termstate = TOPLEVEL;
  1311. } else if (c == '\033')
  1312. termstate = OSC_MAYBE_ST;
  1313. else if (osc_strlen < OSC_STR_MAX)
  1314. osc_string[osc_strlen++] = c;
  1315. break;
  1316. case SEEN_OSC_P:
  1317. {
  1318. int max = (osc_strlen == 0 ? 21 : 16);
  1319. int val;
  1320. if (c >= '0' && c <= '9')
  1321. val = c - '0';
  1322. else if (c >= 'A' && c <= 'A'+max-10)
  1323. val = c - 'A' + 10;
  1324. else if (c >= 'a' && c <= 'a'+max-10)
  1325. val = c - 'a' + 10;
  1326. else
  1327. termstate = TOPLEVEL;
  1328. osc_string[osc_strlen++] = val;
  1329. if (osc_strlen >= 7) {
  1330. palette_set (osc_string[0],
  1331. osc_string[1] * 16 + osc_string[2],
  1332. osc_string[3] * 16 + osc_string[4],
  1333. osc_string[5] * 16 + osc_string[6]);
  1334. term_invalidate();
  1335. termstate = TOPLEVEL;
  1336. }
  1337. }
  1338. break;
  1339. case SEEN_OSC_W:
  1340. switch (c) {
  1341. case '0': case '1': case '2': case '3': case '4':
  1342. case '5': case '6': case '7': case '8': case '9':
  1343. esc_args[0] = 10 * esc_args[0] + c - '0';
  1344. break;
  1345. default:
  1346. termstate = OSC_STRING;
  1347. osc_strlen = 0;
  1348. }
  1349. break;
  1350. case SEEN_ESCHASH:
  1351. if (c == '8') {
  1352. unsigned long *p = scrtop;
  1353. int n = rows * (cols+1);
  1354. while (n--)
  1355. *p++ = ATTR_DEFAULT | 'E';
  1356. disptop = scrtop;
  1357. seen_disp_event = TRUE;
  1358. check_selection (scrtop, scrtop + rows * (cols+1));
  1359. }
  1360. termstate = TOPLEVEL;
  1361. break;
  1362. case VT52_ESC:
  1363. termstate = TOPLEVEL;
  1364. seen_disp_event = TRUE;
  1365. switch (c) {
  1366. case 'A':
  1367. move (curs_x, curs_y - 1, 1);
  1368. break;
  1369. case 'B':
  1370. move (curs_x, curs_y + 1, 1);
  1371. break;
  1372. case 'C':
  1373. move (curs_x + 1, curs_y, 1);
  1374. break;
  1375. case 'D':
  1376. move (curs_x - 1, curs_y, 1);
  1377. break;
  1378. case 'F':
  1379. cset_attr[cset=0] = ATTR_LINEDRW;
  1380. break;
  1381. case 'G':
  1382. cset_attr[cset=0] = ATTR_ASCII;
  1383. break;
  1384. case 'H':
  1385. move (0, 0, 0);
  1386. break;
  1387. case 'I':
  1388. if (curs_y == 0)
  1389. scroll (0, rows-1, -1, TRUE);
  1390. else if (curs_y > 0)
  1391. curs_y--;
  1392. fix_cpos;
  1393. wrapnext = FALSE;
  1394. break;
  1395. case 'J':
  1396. erase_lots(FALSE, FALSE, TRUE);
  1397. disptop = scrtop;
  1398. break;
  1399. case 'K':
  1400. erase_lots(TRUE, FALSE, TRUE);
  1401. break;
  1402. case 'V':
  1403. /* XXX Print cursor line */
  1404. break;
  1405. case 'W':
  1406. /* XXX Start controller mode */
  1407. break;
  1408. case 'X':
  1409. /* XXX Stop controller mode */
  1410. break;
  1411. case 'Y':
  1412. termstate = VT52_Y1;
  1413. break;
  1414. case 'Z':
  1415. ldisc->send ("\033/Z", 3);
  1416. break;
  1417. case '=':
  1418. app_cursor_keys = TRUE;
  1419. break;
  1420. case '>':
  1421. app_cursor_keys = FALSE;
  1422. break;
  1423. case '<':
  1424. /* XXX This should switch to VT100 mode not current or default
  1425. * VT mode. But this will only have effect in a VT220+
  1426. * emulation.
  1427. */
  1428. vt52_mode = FALSE;
  1429. break;
  1430. case '^':
  1431. /* XXX Enter auto print mode */
  1432. break;
  1433. case '_':
  1434. /* XXX Exit auto print mode */
  1435. break;
  1436. case ']':
  1437. /* XXX Print screen */
  1438. break;
  1439. }
  1440. break;
  1441. case VT52_Y1:
  1442. termstate = VT52_Y2;
  1443. move(curs_x, c-' ', 0);
  1444. break;
  1445. case VT52_Y2:
  1446. termstate = TOPLEVEL;
  1447. move(c-' ', curs_y, 0);
  1448. break;
  1449. }
  1450. if (selstate != NO_SELECTION)
  1451. check_selection (cpos, cpos+1);
  1452. }
  1453. }
  1454. /*
  1455. * Compare two lines to determine whether they are sufficiently
  1456. * alike to scroll-optimise one to the other. Return the degree of
  1457. * similarity.
  1458. */
  1459. static int linecmp (unsigned long *a, unsigned long *b) {
  1460. int i, n;
  1461. for (i=n=0; i < cols; i++)
  1462. n += (*a++ == *b++);
  1463. return n;
  1464. }
  1465. /*
  1466. * Given a context, update the window. Out of paranoia, we don't
  1467. * allow WM_PAINT responses to do scrolling optimisations.
  1468. */
  1469. static void do_paint (Context ctx, int may_optimise){
  1470. int i, j, start, our_curs_y;
  1471. unsigned long attr, rv, cursor;
  1472. char ch[1024];
  1473. if (cursor_on) {
  1474. if (has_focus) {
  1475. if (blinker || !cfg.blink_cur)
  1476. cursor = ATTR_ACTCURS;
  1477. else
  1478. cursor = 0;
  1479. }
  1480. else
  1481. cursor = ATTR_PASCURS;
  1482. }
  1483. else cursor = 0;
  1484. rv = (rvideo ? ATTR_REVERSE : 0);
  1485. our_curs_y = curs_y + (scrtop - disptop) / (cols+1);
  1486. for (i=0; i<rows; i++) {
  1487. int idx = i*(cols+1);
  1488. for (j=0; j<=cols; j++,idx++) {
  1489. unsigned long *d = disptop+idx;
  1490. wanttext[idx] = ((*d ^ rv
  1491. ^ (selstart <= d && d < selend ?
  1492. ATTR_REVERSE : 0)) |
  1493. (i==our_curs_y && j==curs_x ? cursor : 0));
  1494. }
  1495. }
  1496. /*
  1497. * We would perform scrolling optimisations in here, if they
  1498. * didn't have a nasty tendency to cause the whole sodding
  1499. * program to hang for a second at speed-critical moments.
  1500. * We'll leave it well alone...
  1501. */
  1502. for (i=0; i<rows; i++) {
  1503. int idx = i*(cols+1);
  1504. start = -1;
  1505. for (j=0; j<=cols; j++,idx++) {
  1506. unsigned long t = wanttext[idx];
  1507. int needs_update = (j < cols && t != disptext[idx]);
  1508. int keep_going = (start != -1 && needs_update &&
  1509. (t & ATTR_MASK) == attr &&
  1510. j-start < sizeof(ch));
  1511. if (start != -1 && !keep_going) {
  1512. do_text (ctx, start, i, ch, j-start, attr);
  1513. start = -1;
  1514. }
  1515. if (needs_update) {
  1516. if (start == -1) {
  1517. start = j;
  1518. attr = t & ATTR_MASK;
  1519. }
  1520. ch[j-start] = (char) (t & CHAR_MASK);
  1521. }
  1522. disptext[idx] = t;
  1523. }
  1524. }
  1525. }
  1526. /*
  1527. * Flick the switch that says if blinking things should be shown or hidden.
  1528. */
  1529. void term_blink(int flg) {
  1530. static long last_blink = 0;
  1531. long now, blink_diff;
  1532. if (flg) {
  1533. blinker = 1;
  1534. last_blink = GetTickCount();
  1535. return;
  1536. }
  1537. now = GetTickCount();
  1538. blink_diff = now-last_blink;
  1539. /* Make sure the cursor blinks no more than 2Hz */
  1540. if (blink_diff>=0 && blink_diff<450)
  1541. return;
  1542. last_blink = now;
  1543. blinker = !blinker;
  1544. }
  1545. /*
  1546. * Invalidate the whole screen so it will be repainted in full.
  1547. */
  1548. void term_invalidate(void) {
  1549. int i;
  1550. for (i=0; i<rows*(cols+1); i++)
  1551. disptext[i] = ATTR_INVALID;
  1552. }
  1553. /*
  1554. * Paint the window in response to a WM_PAINT message.
  1555. */
  1556. void term_paint (Context ctx, int l, int t, int r, int b) {
  1557. int i, j, left, top, right, bottom;
  1558. left = l / font_width;
  1559. right = (r - 1) / font_width;
  1560. top = t / font_height;
  1561. bottom = (b - 1) / font_height;
  1562. for (i = top; i <= bottom && i < rows ; i++)
  1563. for (j = left; j <= right && j < cols ; j++)
  1564. disptext[i*(cols+1)+j] = ATTR_INVALID;
  1565. /* This should happen soon enough, also for some reason it sometimes
  1566. * fails to actually do anything when re-sizing ... painting the wrong
  1567. * window perhaps ?
  1568. do_paint (ctx, FALSE);
  1569. */
  1570. }
  1571. /*
  1572. * Attempt to scroll the scrollback. The second parameter gives the
  1573. * position we want to scroll to; the first is +1 to denote that
  1574. * this position is relative to the beginning of the scrollback, -1
  1575. * to denote it is relative to the end, and 0 to denote that it is
  1576. * relative to the current position.
  1577. */
  1578. void term_scroll (int rel, int where) {
  1579. int n = where * (cols+1);
  1580. disptop = (rel < 0 ? scrtop :
  1581. rel > 0 ? sbtop : disptop) + n;
  1582. if (disptop < sbtop)
  1583. disptop = sbtop;
  1584. if (disptop > scrtop)
  1585. disptop = scrtop;
  1586. update_sbar();
  1587. term_update();
  1588. }
  1589. /*
  1590. * Spread the selection outwards according to the selection mode.
  1591. */
  1592. static unsigned long *sel_spread_half (unsigned long *p, int dir) {
  1593. unsigned long *linestart, *lineend;
  1594. int x;
  1595. short wvalue;
  1596. x = (p - text) % (cols+1);
  1597. linestart = p - x;
  1598. lineend = linestart + cols;
  1599. switch (selmode) {
  1600. case SM_CHAR:
  1601. /*
  1602. * In this mode, every character is a separate unit, except
  1603. * for runs of spaces at the end of a non-wrapping line.
  1604. */
  1605. if (!(linestart[cols] & ATTR_WRAPPED)) {
  1606. unsigned long *q = lineend;
  1607. while (q > linestart && (q[-1] & CHAR_MASK) == 0x20)
  1608. q--;
  1609. if (q == lineend)
  1610. q--;
  1611. if (p >= q)
  1612. p = (dir == -1 ? q : lineend - 1);
  1613. }
  1614. break;
  1615. case SM_WORD:
  1616. /*
  1617. * In this mode, the units are maximal runs of characters
  1618. * whose `wordness' has the same value.
  1619. */
  1620. wvalue = wordness[*p & CHAR_MASK];
  1621. if (dir == +1) {
  1622. while (p < lineend && wordness[p[1] & CHAR_MASK] == wvalue)
  1623. p++;
  1624. } else {
  1625. while (p > linestart && wordness[p[-1] & CHAR_MASK] == wvalue)
  1626. p--;
  1627. }
  1628. break;
  1629. case SM_LINE:
  1630. /*
  1631. * In this mode, every line is a unit.
  1632. */
  1633. p = (dir == -1 ? linestart : lineend - 1);
  1634. break;
  1635. }
  1636. return p;
  1637. }
  1638. static void sel_spread (void) {
  1639. selstart = sel_spread_half (selstart, -1);
  1640. selend = sel_spread_half (selend - 1, +1) + 1;
  1641. }
  1642. void term_mouse (Mouse_Button b, Mouse_Action a, int x, int y) {
  1643. unsigned long *selpoint;
  1644. if (y<0) y = 0;
  1645. if (y>=rows) y = rows-1;
  1646. if (x<0) {
  1647. if (y > 0) {
  1648. x = cols-1;
  1649. y--;
  1650. } else
  1651. x = 0;
  1652. }
  1653. if (x>=cols) x = cols-1;
  1654. selpoint = disptop + y * (cols+1) + x;
  1655. if (b == MB_SELECT && a == MA_CLICK) {
  1656. deselect();
  1657. selstate = ABOUT_TO;
  1658. selanchor = selpoint;
  1659. selmode = SM_CHAR;
  1660. } else if (b == MB_SELECT && (a == MA_2CLK || a == MA_3CLK)) {
  1661. deselect();
  1662. selmode = (a == MA_2CLK ? SM_WORD : SM_LINE);
  1663. selstate = DRAGGING;
  1664. selstart = selanchor = selpoint;
  1665. selend = selstart + 1;
  1666. sel_spread();
  1667. } else if ((b == MB_SELECT && a == MA_DRAG) ||
  1668. (b == MB_EXTEND && a != MA_RELEASE)) {
  1669. if (selstate == ABOUT_TO && selanchor == selpoint)
  1670. return;
  1671. if (b == MB_EXTEND && a != MA_DRAG && selstate == SELECTED) {
  1672. if (selpoint-selstart < (selend-selstart)/2)
  1673. selanchor = selend - 1;
  1674. else
  1675. selanchor = selstart;
  1676. selstate = DRAGGING;
  1677. }
  1678. if (selstate != ABOUT_TO && selstate != DRAGGING)
  1679. selanchor = selpoint;
  1680. selstate = DRAGGING;
  1681. if (selpoint < selanchor) {
  1682. selstart = selpoint;
  1683. selend = selanchor + 1;
  1684. } else {
  1685. selstart = selanchor;
  1686. selend = selpoint + 1;
  1687. }
  1688. sel_spread();
  1689. } else if ((b == MB_SELECT || b == MB_EXTEND) && a == MA_RELEASE) {
  1690. if (selstate == DRAGGING) {
  1691. /*
  1692. * We've completed a selection. We now transfer the
  1693. * data to the clipboard.
  1694. */
  1695. unsigned char *p = selspace;
  1696. unsigned long *q = selstart;
  1697. while (q < selend) {
  1698. int nl = FALSE;
  1699. unsigned long *lineend = q - (q-text) % (cols+1) + cols;
  1700. unsigned long *nlpos = lineend;
  1701. if (!(*nlpos & ATTR_WRAPPED)) {
  1702. while ((nlpos[-1] & CHAR_MASK) == 0x20 && nlpos > q)
  1703. nlpos--;
  1704. if (nlpos < selend)
  1705. nl = TRUE;
  1706. }
  1707. while (q < nlpos && q < selend)
  1708. *p++ = (unsigned char) (*q++ & CHAR_MASK);
  1709. if (nl) {
  1710. int i;
  1711. for (i=0; i<sizeof(sel_nl); i++)
  1712. *p++ = sel_nl[i];
  1713. }
  1714. q = lineend + 1; /* start of next line */
  1715. }
  1716. write_clip (selspace, p - selspace);
  1717. selstate = SELECTED;
  1718. } else
  1719. selstate = NO_SELECTION;
  1720. } else if (b == MB_PASTE && (a==MA_CLICK || a==MA_2CLK || a==MA_3CLK)) {
  1721. char *data;
  1722. int len;
  1723. get_clip((void **) &data, &len);
  1724. if (data) {
  1725. char *p, *q;
  1726. p = q = data;
  1727. while (p < data+len) {
  1728. while (p < data+len &&
  1729. !(p <= data+len-sizeof(sel_nl) &&
  1730. !memcmp(p, sel_nl, sizeof(sel_nl))))
  1731. p++;
  1732. {
  1733. int i;
  1734. unsigned char c;
  1735. for(i=0;i<p-q;i++)
  1736. {
  1737. c=xlat_kbd2tty(q[i]);
  1738. ldisc->send(&c,1);
  1739. }
  1740. }
  1741. if (p <= data+len-sizeof(sel_nl) &&
  1742. !memcmp(p, sel_nl, sizeof(sel_nl))) {
  1743. ldisc->send ("\r", 1);
  1744. p += sizeof(sel_nl);
  1745. }
  1746. q = p;
  1747. }
  1748. }
  1749. get_clip(NULL, NULL);
  1750. }
  1751. term_update();
  1752. }
  1753. static void deselect (void) {
  1754. selstate = NO_SELECTION;
  1755. selstart = selend = scrtop;
  1756. }
  1757. void term_deselect (void) {
  1758. deselect();
  1759. term_update();
  1760. }