terminal.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. /* terminal.c -- how to handle the physical terminal for Info.
  2. $Id$
  3. Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1996, 1997, 1998,
  4. 1999, 2001, 2002, 2004, 2007, 2008, 2012, 2013, 2014, 2015
  5. Free Software Foundation, Inc.
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. Originally written by Brian Fox. */
  17. #include "info.h"
  18. #include "terminal.h"
  19. #include "termdep.h"
  20. #include "doc.h"
  21. #include "variables.h"
  22. #include <sys/types.h>
  23. #include <signal.h>
  24. /* The Unix termcap interface code. */
  25. /* With MinGW, if the user has ncurses installed, including termcap.h
  26. or ncurses/termcap.h will cause the Info binary depend on the ncurses
  27. DLL, just because BC and PC are declared there, although they are
  28. never used in the MinGW build. Avoid that useless dependency. */
  29. #if defined (HAVE_NCURSES_TERMCAP_H) && !defined (__MINGW32__)
  30. #include <ncurses/termcap.h>
  31. #elif defined (HAVE_TERMCAP_H) && !defined (__MINGW32__)
  32. #include <termcap.h>
  33. #else /* (!HAVE_NCURSES_TERMCAP_H || __MINGW32__) && !HAVE_TERMCAP_H */
  34. /* On Solaris2, sys/types.h #includes sys/reg.h, which #defines PC.
  35. Unfortunately, PC is a global variable used by the termcap library. */
  36. #undef PC
  37. /* Termcap requires these variables, whether we access them or not. */
  38. char *BC, *UP;
  39. char PC; /* Pad character */
  40. short ospeed; /* Terminal output baud rate */
  41. extern int tgetnum (), tgetflag (), tgetent ();
  42. extern char *tgetstr (), *tgoto ();
  43. extern int tputs ();
  44. #endif /* not HAVE_NCURSES_TERMCAP_H */
  45. /* Function "hooks". If you make one of these point to a function, that
  46. function is called when appropriate instead of its namesake. Your
  47. function is called with exactly the same arguments that were passed
  48. to the namesake function. */
  49. VFunction *terminal_begin_inverse_hook = NULL;
  50. VFunction *terminal_end_inverse_hook = NULL;
  51. VFunction *terminal_begin_standout_hook = NULL;
  52. VFunction *terminal_end_standout_hook = NULL;
  53. VFunction *terminal_begin_underline_hook = NULL;
  54. VFunction *terminal_end_underline_hook = NULL;
  55. VFunction *terminal_begin_bold_hook = NULL;
  56. VFunction *terminal_begin_blink_hook = NULL;
  57. VFunction *terminal_end_all_modes_hook = NULL;
  58. VFunction *terminal_default_colour_hook = NULL;
  59. VFunction *terminal_set_colour_hook = NULL;
  60. VFunction *terminal_set_bgcolour_hook = NULL;
  61. VFunction *terminal_prep_terminal_hook = NULL;
  62. VFunction *terminal_unprep_terminal_hook = NULL;
  63. VFunction *terminal_up_line_hook = NULL;
  64. VFunction *terminal_down_line_hook = NULL;
  65. VFunction *terminal_clear_screen_hook = NULL;
  66. VFunction *terminal_clear_to_eol_hook = NULL;
  67. VFunction *terminal_get_screen_size_hook = NULL;
  68. VFunction *terminal_goto_xy_hook = NULL;
  69. VFunction *terminal_initialize_terminal_hook = NULL;
  70. VFunction *terminal_new_terminal_hook = NULL;
  71. VFunction *terminal_put_text_hook = NULL;
  72. VFunction *terminal_ring_bell_hook = NULL;
  73. VFunction *terminal_write_chars_hook = NULL;
  74. VFunction *terminal_scroll_terminal_hook = NULL;
  75. /* User variable 'mouse'. Values can be MP_* constants in terminal.h. */
  76. int mouse_protocol = MP_NONE;
  77. /* **************************************************************** */
  78. /* */
  79. /* Terminal and Termcap */
  80. /* */
  81. /* **************************************************************** */
  82. /* A buffer which holds onto the current terminal description, and a pointer
  83. used to float within it. And the name of the terminal. */
  84. static char *term_buffer = NULL;
  85. static char *term_string_buffer = NULL;
  86. static char *term_name;
  87. /* Some strings to control terminal actions. These are output by tputs (). */
  88. static char *term_goto, *term_clreol, *term_cr, *term_clrpag;
  89. static char *term_begin_use, *term_end_use;
  90. static char *term_AL, *term_DL, *term_al, *term_dl;
  91. static char *term_cs; /* Set scrolling region. */
  92. static char *term_SF, *term_SR; /* Scroll forward and in reverse. */
  93. static char *term_keypad_on, *term_keypad_off;
  94. /* How to go up a line. */
  95. static char *term_up;
  96. /* How to go down a line. */
  97. static char *term_dn;
  98. /* An audible bell, if the terminal can be made to make noise. */
  99. static char *audible_bell;
  100. /* A visible bell, if the terminal can be made to flash the screen. */
  101. static char *visible_bell;
  102. /* The string to turn on inverse mode, if this term has one. */
  103. static char *term_invbeg;
  104. /* The string to turn off inverse mode, if this term has one. */
  105. static char *term_invend;
  106. /* String introducing a mouse event. */
  107. static char *term_Km;
  108. /* Strings entering and leaving standout mode. */
  109. char *term_so, *term_se;
  110. /* Strings entering and leaving underline mode. */
  111. char *term_us, *term_ue;
  112. /* Set foreground and background colours (terminfo setaf and setab) */
  113. char *term_AF, *term_AB;
  114. /* Restore original colours, both foreground and background.
  115. ("original pair") */
  116. char *term_op;
  117. /* Turn on bold mode. */
  118. char *term_md;
  119. /* Turn on blink mode. */
  120. char *term_mb;
  121. /* Exit all attribute modes. */
  122. char *term_me;
  123. /* Although I can't find any documentation that says this is supposed to
  124. return its argument, all the code I've looked at (termutils, less)
  125. does so, so fine. */
  126. static int
  127. output_character_function (int c)
  128. {
  129. putc (c, stdout);
  130. return c;
  131. }
  132. /* Macro to send STRING to the terminal. */
  133. #define send_to_terminal(string) \
  134. do { \
  135. if (string) \
  136. tputs (string, 1, output_character_function); \
  137. } while (0)
  138. /* Tell the terminal that we will be doing cursor addressable motion. */
  139. static void
  140. terminal_begin_using_terminal (void)
  141. {
  142. RETSIGTYPE (*sigsave) (int signum);
  143. /* Turn on mouse reporting. This is "normal tracking mode" supported by
  144. xterm. The presence of the Km capability may not be a reliable way to
  145. tell whether this mode exists, but sending the following sequence is
  146. probably harmless if it doesn't. */
  147. if (mouse_protocol == MP_NORMAL_TRACKING
  148. && term_Km && !strcmp (term_Km, "\033[M"))
  149. send_to_terminal ("\033[?1000h");
  150. else
  151. term_Km = 0;
  152. if (term_keypad_on)
  153. send_to_terminal (term_keypad_on);
  154. if (!term_begin_use || !*term_begin_use)
  155. return;
  156. #ifdef SIGWINCH
  157. sigsave = signal (SIGWINCH, SIG_IGN);
  158. #endif
  159. send_to_terminal (term_begin_use);
  160. fflush (stdout);
  161. if (STREQ (term_name, "sun-cmd"))
  162. /* Without this fflush and sleep, running info in a shelltool or
  163. cmdtool (TERM=sun-cmd) with scrollbars loses -- the scrollbars are
  164. not restored properly.
  165. From: strube@physik3.gwdg.de (Hans Werner Strube). */
  166. sleep (1);
  167. #ifdef SIGWINCH
  168. signal (SIGWINCH, sigsave);
  169. #endif
  170. }
  171. /* Tell the terminal that we will not be doing any more cursor
  172. addressable motion. */
  173. static void
  174. terminal_end_using_terminal (void)
  175. {
  176. RETSIGTYPE (*sigsave) (int signum);
  177. /* Turn off mouse reporting ("normal tracking mode"). */
  178. if (term_Km)
  179. send_to_terminal ("\033[?1000l");
  180. if (term_keypad_off)
  181. send_to_terminal (term_keypad_off);
  182. if (!term_end_use || !*term_end_use)
  183. return;
  184. #ifdef SIGWINCH
  185. sigsave = signal (SIGWINCH, SIG_IGN);
  186. #endif
  187. send_to_terminal (term_end_use);
  188. fflush (stdout);
  189. if (STREQ (term_name, "sun-cmd"))
  190. /* See comments at other sleep. */
  191. sleep (1);
  192. #ifdef SIGWINCH
  193. signal (SIGWINCH, sigsave);
  194. #endif
  195. }
  196. /* **************************************************************** */
  197. /* */
  198. /* Necessary Terminal Functions */
  199. /* */
  200. /* **************************************************************** */
  201. /* The functions and variables on this page implement the user visible
  202. portion of the terminal interface. */
  203. /* The width and height of the terminal. */
  204. int screenwidth, screenheight;
  205. /* Non-zero means this terminal can't really do anything. */
  206. int terminal_is_dumb_p = 0;
  207. /* Non-zero means that this terminal can produce a visible bell. */
  208. int terminal_has_visible_bell_p = 0;
  209. /* Non-zero means to use that visible bell if at all possible. */
  210. int terminal_use_visible_bell_p = 0;
  211. /* Non-zero means that the terminal can do scrolling. */
  212. int terminal_can_scroll = 0;
  213. /* Non-zero means that the terminal scroll within a restricted region
  214. of lines. */
  215. int terminal_can_scroll_region = 0;
  216. /* The key sequences output by the arrow keys, if this terminal has any. */
  217. char *term_ku = NULL;
  218. char *term_kd = NULL;
  219. char *term_kr = NULL;
  220. char *term_kl = NULL;
  221. char *term_kP = NULL; /* page-up */
  222. char *term_kN = NULL; /* page-down */
  223. char *term_kh = NULL; /* home */
  224. char *term_ke = NULL; /* end */
  225. char *term_kD = NULL; /* delete */
  226. char *term_ki = NULL; /* ins */
  227. char *term_kB = NULL; /* back tab */
  228. /* Move the cursor to the terminal location of X and Y. */
  229. void
  230. terminal_goto_xy (int x, int y)
  231. {
  232. if (terminal_goto_xy_hook)
  233. (*terminal_goto_xy_hook) (x, y);
  234. else
  235. {
  236. if (term_goto)
  237. tputs (tgoto (term_goto, x, y), 1, output_character_function);
  238. }
  239. }
  240. /* Print STRING to the terminal at the current position. */
  241. void
  242. terminal_put_text (char *string)
  243. {
  244. if (terminal_put_text_hook)
  245. (*terminal_put_text_hook) (string);
  246. else
  247. {
  248. printf ("%s", string);
  249. }
  250. }
  251. /* Print NCHARS from STRING to the terminal at the current position. */
  252. void
  253. terminal_write_chars (char *string, int nchars)
  254. {
  255. if (terminal_write_chars_hook)
  256. (*terminal_write_chars_hook) (string, nchars);
  257. else
  258. {
  259. if (nchars)
  260. fwrite (string, 1, nchars, stdout);
  261. }
  262. }
  263. /* Clear from the current position of the cursor to the end of the line. */
  264. void
  265. terminal_clear_to_eol (void)
  266. {
  267. if (terminal_clear_to_eol_hook)
  268. (*terminal_clear_to_eol_hook) ();
  269. else
  270. {
  271. send_to_terminal (term_clreol);
  272. }
  273. }
  274. /* Clear the entire terminal screen. */
  275. void
  276. terminal_clear_screen (void)
  277. {
  278. if (terminal_clear_screen_hook)
  279. (*terminal_clear_screen_hook) ();
  280. else
  281. {
  282. send_to_terminal (term_clrpag);
  283. }
  284. }
  285. /* Move the cursor up one line. */
  286. void
  287. terminal_up_line (void)
  288. {
  289. if (terminal_up_line_hook)
  290. (*terminal_up_line_hook) ();
  291. else
  292. {
  293. send_to_terminal (term_up);
  294. }
  295. }
  296. /* Move the cursor down one line. */
  297. void
  298. terminal_down_line (void)
  299. {
  300. if (terminal_down_line_hook)
  301. (*terminal_down_line_hook) ();
  302. else
  303. {
  304. send_to_terminal (term_dn);
  305. }
  306. }
  307. /* Turn on reverse video if possible. */
  308. void
  309. terminal_begin_inverse (void)
  310. {
  311. if (terminal_begin_inverse_hook)
  312. (*terminal_begin_inverse_hook) ();
  313. else
  314. {
  315. send_to_terminal (term_invbeg);
  316. }
  317. }
  318. /* Turn off reverse video if possible. */
  319. void
  320. terminal_end_inverse (void)
  321. {
  322. if (terminal_end_inverse_hook)
  323. (*terminal_end_inverse_hook) ();
  324. else
  325. {
  326. send_to_terminal (term_invend);
  327. }
  328. }
  329. /* Turn on "standout mode" if possible. Likely the same
  330. as reverse video. */
  331. void
  332. terminal_begin_standout (void)
  333. {
  334. if (terminal_begin_standout_hook)
  335. (*terminal_begin_standout_hook) ();
  336. else
  337. {
  338. send_to_terminal (term_so);
  339. }
  340. }
  341. /* Turn off "standout mode" if possible. */
  342. void
  343. terminal_end_standout (void)
  344. {
  345. if (terminal_end_standout_hook)
  346. (*terminal_end_standout_hook) ();
  347. else
  348. {
  349. send_to_terminal (term_se);
  350. }
  351. }
  352. void
  353. terminal_begin_underline (void)
  354. {
  355. if (terminal_begin_underline_hook)
  356. (*terminal_begin_underline_hook) ();
  357. else
  358. {
  359. send_to_terminal (term_us);
  360. }
  361. }
  362. void
  363. terminal_end_underline (void)
  364. {
  365. if (terminal_end_underline_hook)
  366. (*terminal_end_underline_hook) ();
  367. else
  368. {
  369. send_to_terminal (term_ue);
  370. }
  371. }
  372. void
  373. terminal_begin_bold (void)
  374. {
  375. if (terminal_begin_bold_hook)
  376. (*terminal_begin_bold_hook) ();
  377. else
  378. {
  379. send_to_terminal (term_md);
  380. }
  381. }
  382. void
  383. terminal_begin_blink (void)
  384. {
  385. if (terminal_begin_blink_hook)
  386. (*terminal_begin_blink_hook) ();
  387. else
  388. {
  389. send_to_terminal (term_mb);
  390. }
  391. }
  392. void
  393. terminal_end_all_modes (void)
  394. {
  395. if (terminal_end_all_modes_hook)
  396. (*terminal_end_all_modes_hook) ();
  397. else
  398. {
  399. send_to_terminal (term_me);
  400. }
  401. }
  402. /* Ring the terminal bell. The bell is run visibly if it both has one and
  403. terminal_use_visible_bell_p is non-zero. */
  404. void
  405. terminal_ring_bell (void)
  406. {
  407. if (terminal_ring_bell_hook)
  408. (*terminal_ring_bell_hook) ();
  409. else
  410. {
  411. if (terminal_has_visible_bell_p && terminal_use_visible_bell_p)
  412. send_to_terminal (visible_bell);
  413. else
  414. send_to_terminal (audible_bell);
  415. }
  416. }
  417. /* At the line START, delete COUNT lines from the terminal display. */
  418. static void
  419. terminal_delete_lines (int start, int count)
  420. {
  421. int lines;
  422. /* Normalize arguments. */
  423. if (start < 0)
  424. start = 0;
  425. lines = screenheight - start;
  426. terminal_goto_xy (0, start);
  427. if (term_DL)
  428. tputs (tgoto (term_DL, 0, count), lines, output_character_function);
  429. else
  430. {
  431. while (count--)
  432. tputs (term_dl, lines, output_character_function);
  433. }
  434. fflush (stdout);
  435. }
  436. /* At the line START, insert COUNT lines in the terminal display. */
  437. static void
  438. terminal_insert_lines (int start, int count)
  439. {
  440. int lines;
  441. /* Normalize arguments. */
  442. if (start < 0)
  443. start = 0;
  444. lines = screenheight - start;
  445. terminal_goto_xy (0, start);
  446. if (term_AL)
  447. tputs (tgoto (term_AL, 0, count), lines, output_character_function);
  448. else
  449. {
  450. while (count--)
  451. tputs (term_al, lines, output_character_function);
  452. }
  453. fflush (stdout);
  454. }
  455. void
  456. terminal_scroll_region (int start, int end, int amount)
  457. {
  458. /* Any scrolling at all? */
  459. if (amount == 0)
  460. return;
  461. if (terminal_scroll_terminal_hook)
  462. {
  463. (*terminal_scroll_terminal_hook) (start, end, amount);
  464. return;
  465. }
  466. if (terminal_can_scroll_region)
  467. {
  468. /* Set scrolling region. */
  469. tputs (tgoto (term_cs, end - 1, start), 0, output_character_function);
  470. /* Scroll. */
  471. if (amount > 0)
  472. tputs (tgoto (term_SR, 0, amount), 0, output_character_function);
  473. else
  474. tputs (tgoto (term_SF, 0, -amount), 0, output_character_function);
  475. /* Reset scrolling region. */
  476. tputs (tgoto (term_cs, screenheight - 1, 0), 0, output_character_function);
  477. return;
  478. }
  479. }
  480. /* Scroll an area of the terminal, starting with the region from START
  481. to END, AMOUNT lines. If AMOUNT is negative, the lines are scrolled
  482. towards the top of the screen, else they are scrolled towards the
  483. bottom of the screen. */
  484. void
  485. terminal_scroll_terminal (int start, int end, int amount)
  486. {
  487. if (!terminal_can_scroll)
  488. return;
  489. /* Any scrolling at all? */
  490. if (amount == 0)
  491. return;
  492. if (terminal_scroll_terminal_hook)
  493. (*terminal_scroll_terminal_hook) (start, end, amount);
  494. else if (amount > 0)
  495. {
  496. /* If we are scrolling down, delete AMOUNT lines at END. Then insert
  497. AMOUNT lines at START. */
  498. terminal_delete_lines (end, amount);
  499. terminal_insert_lines (start, amount);
  500. }
  501. else
  502. {
  503. /* If we are scrolling up, delete AMOUNT lines before START. This
  504. actually does the upwards scroll. Then, insert AMOUNT lines
  505. after the already scrolled region (i.e., END - AMOUNT). */
  506. int abs_amount = -amount;
  507. terminal_delete_lines (start - abs_amount, abs_amount);
  508. terminal_insert_lines (end - abs_amount, abs_amount);
  509. }
  510. }
  511. /* Revert to the default foreground and background colours. */
  512. static void
  513. terminal_default_colour (void)
  514. {
  515. if (terminal_default_colour_hook)
  516. (*terminal_default_colour_hook) ();
  517. else
  518. tputs (term_op, 0, output_character_function);
  519. }
  520. static void
  521. terminal_set_colour (int colour)
  522. {
  523. if (terminal_set_colour_hook)
  524. (*terminal_set_colour_hook) (colour);
  525. else
  526. tputs (tgoto (term_AF, 0, colour), 0, output_character_function);
  527. }
  528. static void
  529. terminal_set_bgcolour (int colour)
  530. {
  531. if (terminal_set_bgcolour_hook)
  532. (*terminal_set_bgcolour_hook) (colour);
  533. else
  534. tputs (tgoto (term_AB, 0, colour), 0, output_character_function);
  535. }
  536. /* Information about what styles like colour, underlining, boldface are
  537. currently output for text on the screen. All zero represents the default
  538. rendition. */
  539. static unsigned long terminal_rendition;
  540. /* Modes for which there aren't termcap entries for turning them off. */
  541. #define COMBINED_MODES (BOLD_MASK | BLINK_MASK)
  542. void
  543. terminal_switch_rendition (unsigned long new)
  544. {
  545. unsigned long old = terminal_rendition;
  546. if ((old & new & COMBINED_MODES) != (old & COMBINED_MODES))
  547. {
  548. /* Some modes we can't turn off by themselves, so if we need to turn
  549. one of them off, turn back on all the ones that should be on
  550. afterwards. */
  551. terminal_end_all_modes ();
  552. old = 0;
  553. }
  554. else if (!(new & COLOUR_MASK) && (old & COLOUR_MASK)
  555. || !(new & BGCOLOUR_MASK) && (old & BGCOLOUR_MASK))
  556. {
  557. terminal_default_colour ();
  558. old &= ~(COLOUR_MASK|BGCOLOUR_MASK);
  559. }
  560. if ((new & COLOUR_MASK) != (old & COLOUR_MASK))
  561. {
  562. if ((new & COLOUR_MASK) >= 8)
  563. {
  564. terminal_set_colour ((new & COLOUR_MASK) - 8);
  565. }
  566. /* Colour values from 1 to 7 don't do anything right now. */
  567. }
  568. if ((new & BGCOLOUR_MASK) != (old & BGCOLOUR_MASK))
  569. {
  570. /* Switch colour. */
  571. if ((new & BGCOLOUR_MASK) >> 9 >= 8)
  572. {
  573. terminal_set_bgcolour (((new & BGCOLOUR_MASK) >> 9) - 8);
  574. }
  575. /* Colour values from 1 to 7 don't do anything right now. */
  576. }
  577. if ((new & UNDERLINE_MASK) != (old & UNDERLINE_MASK))
  578. {
  579. if ((new & UNDERLINE_MASK))
  580. terminal_begin_underline ();
  581. else
  582. terminal_end_underline ();
  583. }
  584. if ((new & STANDOUT_MASK) != (old & STANDOUT_MASK))
  585. {
  586. if ((new & STANDOUT_MASK))
  587. terminal_begin_standout ();
  588. else
  589. terminal_end_standout ();
  590. }
  591. if ((new & BOLD_MASK) != (old & BOLD_MASK))
  592. {
  593. if ((new & BOLD_MASK))
  594. terminal_begin_bold ();
  595. }
  596. if ((new & BLINK_MASK) != (old & BLINK_MASK))
  597. {
  598. if ((new & BLINK_MASK))
  599. terminal_begin_blink ();
  600. }
  601. terminal_rendition = new;
  602. }
  603. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  604. has changed. */
  605. void
  606. terminal_new_terminal (char *terminal_name)
  607. {
  608. if (terminal_new_terminal_hook)
  609. (*terminal_new_terminal_hook) (terminal_name);
  610. else
  611. {
  612. terminal_initialize_terminal (terminal_name);
  613. }
  614. }
  615. /* Saved values of the LINES and COLUMNS environmental variables. */
  616. static char *env_lines, *env_columns;
  617. /* Set the global variables SCREENWIDTH and SCREENHEIGHT. */
  618. void
  619. terminal_get_screen_size (void)
  620. {
  621. if (terminal_get_screen_size_hook)
  622. (*terminal_get_screen_size_hook) ();
  623. else
  624. {
  625. screenwidth = screenheight = 0;
  626. #if defined (TIOCGWINSZ)
  627. {
  628. struct winsize window_size;
  629. if (ioctl (fileno (stdout), TIOCGWINSZ, &window_size) == 0)
  630. {
  631. screenwidth = (int) window_size.ws_col;
  632. screenheight = (int) window_size.ws_row;
  633. }
  634. }
  635. #endif /* TIOCGWINSZ */
  636. /* Environment variable COLUMNS overrides setting of "co". */
  637. if (screenwidth <= 0)
  638. {
  639. if (env_columns)
  640. screenwidth = atoi (env_columns);
  641. if (screenwidth <= 0)
  642. screenwidth = tgetnum ("co");
  643. }
  644. /* Environment variable LINES overrides setting of "li". */
  645. if (screenheight <= 0)
  646. {
  647. if (env_lines)
  648. screenheight = atoi (env_lines);
  649. if (screenheight <= 0)
  650. screenheight = tgetnum ("li");
  651. }
  652. /* If all else fails, default to 80x24 terminal. */
  653. if (screenwidth <= 0)
  654. screenwidth = 80;
  655. if (screenheight <= 0)
  656. screenheight = 24;
  657. }
  658. }
  659. /* Root of structure representing a mapping from sequences of bytes to named
  660. keys. */
  661. BYTEMAP_ENTRY *byte_seq_to_key;
  662. static void
  663. add_seq_to_byte_map (int key_id, char *seq)
  664. {
  665. BYTEMAP_ENTRY *b = byte_seq_to_key;
  666. /* Must consider bytes as unsigned because we use them as array indices. */
  667. unsigned char *c = (unsigned char *) seq;
  668. for (; *c; c++)
  669. {
  670. if (c[1] == '\0') /* Last character. */
  671. {
  672. b[*c].type = BYTEMAP_KEY;
  673. b[*c].key = key_id;
  674. }
  675. else
  676. {
  677. b[*c].type = BYTEMAP_MAP;
  678. b[*c].key = 0;
  679. if (!b[*c].next)
  680. b[*c].next = xzalloc (256 * sizeof (BYTEMAP_ENTRY));
  681. b = b[*c].next;
  682. }
  683. }
  684. }
  685. /* When non-zero, various display and input functions handle extended
  686. character sets such as ISO Latin or UTF-8 correctly. */
  687. int ISO_Latin_p = 1;
  688. /* Initialize byte map read in get_input_key. */
  689. static void
  690. initialize_byte_map (void)
  691. {
  692. int i;
  693. static struct special_keys {
  694. int key_id;
  695. char **byte_seq;
  696. } keys[] = {
  697. KEY_RIGHT_ARROW, &term_kr,
  698. KEY_LEFT_ARROW, &term_kl,
  699. KEY_UP_ARROW, &term_ku,
  700. KEY_DOWN_ARROW, &term_kd,
  701. KEY_PAGE_UP, &term_kP,
  702. KEY_PAGE_DOWN, &term_kN,
  703. KEY_HOME, &term_kh,
  704. KEY_END, &term_ke,
  705. KEY_DELETE, &term_kD,
  706. KEY_INSERT, &term_ki,
  707. KEY_BACK_TAB, &term_kB
  708. };
  709. /* Recognize arrow key sequences with both of the usual prefixes in case they
  710. are missing in the termcap entry. */
  711. static struct special_keys2 {
  712. int key_id;
  713. char *byte_seq;
  714. } keys2[] = {
  715. KEY_RIGHT_ARROW, "\033[C",
  716. KEY_RIGHT_ARROW, "\033OC",
  717. KEY_LEFT_ARROW, "\033[D",
  718. KEY_LEFT_ARROW, "\033OD",
  719. KEY_UP_ARROW, "\033[A",
  720. KEY_UP_ARROW, "\033OA",
  721. KEY_DOWN_ARROW, "\033[B",
  722. KEY_DOWN_ARROW, "\033OB"
  723. };
  724. byte_seq_to_key = xmalloc (256 * sizeof (BYTEMAP_ENTRY));
  725. /* Make each byte represent itself by default. */
  726. for (i = 0; i < 128; i++)
  727. {
  728. byte_seq_to_key[i].type = BYTEMAP_KEY;
  729. byte_seq_to_key[i].key = i;
  730. byte_seq_to_key[i].next = 0;
  731. }
  732. /* Use 'ISO-Latin' variable to decide whether bytes with the 8th bit set
  733. represent the Meta key being pressed. Maybe we should have another
  734. variable to enable 8-bit input. If 'ISO-Latin' is set this allows input
  735. of non-ASCII characters in the echo area. */
  736. if (!ISO_Latin_p)
  737. for (i = 128; i < 256; i++)
  738. {
  739. byte_seq_to_key[i].type = BYTEMAP_KEY;
  740. byte_seq_to_key[i].key = (i - 128) + KEYMAP_META_BASE;
  741. byte_seq_to_key[i].next = 0;
  742. }
  743. /* Hard-code octal 177 = delete. Either 177 or the term_kD sequence will
  744. result in a delete key being registered. */
  745. byte_seq_to_key['\177'].type = BYTEMAP_KEY;
  746. byte_seq_to_key['\177'].key = KEY_DELETE;
  747. byte_seq_to_key['\177'].next = 0;
  748. /* For each special key, record its byte sequence. */
  749. for (i = 0; i < sizeof (keys) / sizeof (*keys); i++)
  750. {
  751. if (!*keys[i].byte_seq)
  752. continue; /* No byte sequence known for this key. */
  753. add_seq_to_byte_map (keys[i].key_id, *keys[i].byte_seq);
  754. }
  755. /* Hard-coded byte sequences. */
  756. for (i = 0; i < sizeof (keys2) / sizeof (*keys2); i++)
  757. {
  758. add_seq_to_byte_map (keys2[i].key_id, keys2[i].byte_seq);
  759. }
  760. if (term_Km)
  761. add_seq_to_byte_map (KEY_MOUSE, term_Km);
  762. /* Special case for ESC: Can introduce special key sequences, represent the
  763. Meta key being pressed, or be a key on its own. */
  764. byte_seq_to_key['\033'].type = BYTEMAP_ESC;
  765. }
  766. /* Initialize the terminal which is known as TERMINAL_NAME. If this
  767. terminal doesn't have cursor addressability, `terminal_is_dumb_p'
  768. becomes nonzero. The variables SCREENHEIGHT and SCREENWIDTH are set
  769. to the dimensions that this terminal actually has. Get and save various
  770. termcap strings. */
  771. void
  772. terminal_initialize_terminal (char *terminal_name)
  773. {
  774. char *buffer;
  775. terminal_is_dumb_p = 0;
  776. if (terminal_initialize_terminal_hook)
  777. {
  778. (*terminal_initialize_terminal_hook) (terminal_name);
  779. initialize_byte_map ();
  780. return;
  781. }
  782. term_name = terminal_name ? terminal_name : getenv ("TERM");
  783. if (!term_name)
  784. term_name = "dumb";
  785. env_lines = getenv ("LINES");
  786. env_columns = getenv ("COLUMNS");
  787. /* We save LINES and COLUMNS before the call to tgetent below, because
  788. on some openSUSE systems, including openSUSE 12.3, the call to tgetent
  789. changes the values returned by getenv for these. */
  790. if (!term_string_buffer)
  791. term_string_buffer = xmalloc (2048);
  792. if (!term_buffer)
  793. term_buffer = xmalloc (2048);
  794. buffer = term_string_buffer;
  795. term_clrpag = term_cr = term_clreol = NULL;
  796. /* HP-UX 11.x returns 0 for OK --jeff.hull@state.co.us. */
  797. if (tgetent (term_buffer, term_name) < 0)
  798. {
  799. terminal_is_dumb_p = 1;
  800. screenwidth = 80;
  801. screenheight = 24;
  802. term_cr = "\r";
  803. term_up = term_dn = audible_bell = visible_bell = NULL;
  804. term_ku = term_kd = term_kl = term_kr = NULL;
  805. term_kP = term_kN = NULL;
  806. term_kh = term_ke = NULL;
  807. term_kD = NULL;
  808. return;
  809. }
  810. BC = tgetstr ("pc", &buffer);
  811. PC = BC ? *BC : 0;
  812. #if defined (HAVE_TERMIOS_H)
  813. {
  814. struct termios ti;
  815. if (tcgetattr (fileno(stdout), &ti) != -1)
  816. ospeed = cfgetospeed (&ti);
  817. else
  818. ospeed = B9600;
  819. }
  820. #else
  821. # if defined (TIOCGETP)
  822. {
  823. struct sgttyb sg;
  824. if (ioctl (fileno (stdout), TIOCGETP, &sg) != -1)
  825. ospeed = sg.sg_ospeed;
  826. else
  827. ospeed = B9600;
  828. }
  829. # else
  830. #ifndef __MINGW32__
  831. ospeed = B9600;
  832. #endif
  833. # endif /* !TIOCGETP */
  834. #endif
  835. term_cr = tgetstr ("cr", &buffer);
  836. term_clreol = tgetstr ("ce", &buffer);
  837. term_clrpag = tgetstr ("cl", &buffer);
  838. term_goto = tgetstr ("cm", &buffer);
  839. /* Find out about this terminal's scrolling capability. */
  840. term_AL = tgetstr ("AL", &buffer);
  841. term_DL = tgetstr ("DL", &buffer);
  842. term_al = tgetstr ("al", &buffer);
  843. term_dl = tgetstr ("dl", &buffer);
  844. term_cs = tgetstr ("cs", &buffer);
  845. term_SF = tgetstr ("SF", &buffer);
  846. term_SR = tgetstr ("SR", &buffer);
  847. terminal_can_scroll = ((term_AL || term_al) && (term_DL || term_dl));
  848. terminal_can_scroll_region = term_cs && term_SF && term_SR;
  849. term_invbeg = tgetstr ("mr", &buffer);
  850. if (term_invbeg)
  851. term_invend = tgetstr ("me", &buffer);
  852. else
  853. term_invend = NULL;
  854. term_so = tgetstr ("so", &buffer);
  855. if (term_so)
  856. term_se = tgetstr ("se", &buffer);
  857. else
  858. term_se = NULL;
  859. term_us = tgetstr ("us", &buffer);
  860. if (term_us)
  861. term_ue = tgetstr ("ue", &buffer);
  862. else
  863. term_ue = NULL;
  864. term_AF = tgetstr ("AF", &buffer);
  865. if (term_AF)
  866. term_AB = tgetstr ("AB", &buffer);
  867. else
  868. term_AB = NULL;
  869. term_op = tgetstr ("op", &buffer);
  870. term_md = tgetstr ("md", &buffer);
  871. term_mb = tgetstr ("mb", &buffer);
  872. term_me = tgetstr ("me", &buffer);
  873. if (!term_me)
  874. term_md = 0; /* Don't use modes if we can't turn them off. */
  875. if (!term_cr)
  876. term_cr = "\r";
  877. terminal_get_screen_size ();
  878. term_up = tgetstr ("up", &buffer);
  879. term_dn = tgetstr ("dn", &buffer);
  880. visible_bell = tgetstr ("vb", &buffer);
  881. terminal_has_visible_bell_p = (visible_bell != NULL);
  882. audible_bell = tgetstr ("bl", &buffer);
  883. if (!audible_bell)
  884. audible_bell = "\007";
  885. term_begin_use = tgetstr ("ti", &buffer);
  886. term_end_use = tgetstr ("te", &buffer);
  887. term_keypad_on = tgetstr ("ks", &buffer);
  888. term_keypad_off = tgetstr ("ke", &buffer);
  889. /* Attempt to find the arrow keys. */
  890. term_ku = tgetstr ("ku", &buffer);
  891. term_kd = tgetstr ("kd", &buffer);
  892. term_kr = tgetstr ("kr", &buffer);
  893. term_kl = tgetstr ("kl", &buffer);
  894. term_kP = tgetstr ("kP", &buffer);
  895. term_kN = tgetstr ("kN", &buffer);
  896. term_kh = tgetstr ("kh", &buffer);
  897. term_ke = tgetstr ("@7", &buffer);
  898. term_ki = tgetstr ("kI", &buffer);
  899. term_kD = tgetstr ("kD", &buffer);
  900. term_kB = tgetstr ("kB", &buffer);
  901. /* String introducing a mouse event. */
  902. term_Km = tgetstr ("Km", &buffer);
  903. initialize_byte_map ();
  904. /* If this terminal is not cursor addressable, then it is really dumb. */
  905. if (!term_goto)
  906. terminal_is_dumb_p = 1;
  907. }
  908. /* How to read characters from the terminal. */
  909. #if defined (HAVE_TERMIOS_H)
  910. struct termios original_termios, ttybuff;
  911. #else
  912. # if defined (HAVE_TERMIO_H)
  913. /* A buffer containing the terminal mode flags upon entry to info. */
  914. struct termio original_termio, ttybuff;
  915. # else /* !HAVE_TERMIO_H */
  916. /* Buffers containing the terminal mode flags upon entry to info. */
  917. int original_tty_flags = 0;
  918. int original_lmode;
  919. #ifndef __MINGW32__
  920. struct sgttyb ttybuff;
  921. #endif
  922. # if defined(TIOCGETC) && defined(M_XENIX)
  923. /* SCO 3.2v5.0.2 defines but does not support TIOCGETC. Gak. Maybe
  924. better fix would be to use Posix termios in preference. --gildea,
  925. 1jul99. */
  926. # undef TIOCGETC
  927. # endif
  928. # if defined (TIOCGETC)
  929. /* A buffer containing the terminal interrupt characters upon entry
  930. to Info. */
  931. struct tchars original_tchars;
  932. # endif
  933. # if defined (TIOCGLTC)
  934. /* A buffer containing the local terminal mode characters upon entry
  935. to Info. */
  936. struct ltchars original_ltchars;
  937. # endif
  938. # endif /* !HAVE_TERMIO_H */
  939. #endif /* !HAVE_TERMIOS_H */
  940. /* Prepare to start using the terminal to read characters singly. Return
  941. 0 if terminal is too dumb to run Info interactively. */
  942. int
  943. terminal_prep_terminal (void)
  944. {
  945. int tty;
  946. if (terminal_is_dumb_p)
  947. return 0;
  948. if (terminal_prep_terminal_hook)
  949. {
  950. (*terminal_prep_terminal_hook) ();
  951. return 1;
  952. }
  953. terminal_begin_using_terminal ();
  954. tty = fileno (stdin);
  955. #if defined (HAVE_TERMIOS_H)
  956. tcgetattr (tty, &original_termios);
  957. tcgetattr (tty, &ttybuff);
  958. #else
  959. # if defined (HAVE_TERMIO_H)
  960. ioctl (tty, TCGETA, &original_termio);
  961. ioctl (tty, TCGETA, &ttybuff);
  962. # endif
  963. #endif
  964. #if defined (HAVE_TERMIOS_H) || defined (HAVE_TERMIO_H)
  965. ttybuff.c_iflag &= (~ISTRIP & ~INLCR & ~IGNCR & ~ICRNL & ~IXON);
  966. /* These output flags are not part of POSIX, so only use them if they
  967. are defined. */
  968. #ifdef ONLCR
  969. ttybuff.c_oflag &= ~ONLCR ;
  970. #endif
  971. #ifdef OCRNL
  972. ttybuff.c_oflag &= ~OCRNL;
  973. #endif
  974. ttybuff.c_lflag &= (~ICANON & ~ECHO);
  975. ttybuff.c_cc[VMIN] = 1;
  976. ttybuff.c_cc[VTIME] = 0;
  977. if (ttybuff.c_cc[VINTR] == '\177')
  978. ttybuff.c_cc[VINTR] = -1;
  979. if (ttybuff.c_cc[VQUIT] == '\177')
  980. ttybuff.c_cc[VQUIT] = -1;
  981. #ifdef VLNEXT
  982. if (ttybuff.c_cc[VLNEXT] == '\026')
  983. ttybuff.c_cc[VLNEXT] = -1;
  984. #endif /* VLNEXT */
  985. #endif /* TERMIOS or TERMIO */
  986. /* cf. emacs/src/sysdep.c for being sure output is on. */
  987. #if defined (HAVE_TERMIOS_H)
  988. /* linux kernel 2.2.x needs a TCOFF followed by a TCOON to turn output
  989. back on if the user presses ^S at the very beginning; just a TCOON
  990. doesn't work. --Kevin Ryde <user42@zip.com.au>, 16jun2000. */
  991. tcsetattr (tty, TCSANOW, &ttybuff);
  992. # ifdef TCOON
  993. tcflow (tty, TCOOFF);
  994. tcflow (tty, TCOON);
  995. # endif
  996. #else
  997. # if defined (HAVE_TERMIO_H)
  998. ioctl (tty, TCSETA, &ttybuff);
  999. # ifdef TCXONC
  1000. ioctl (tty, TCXONC, 1);
  1001. # endif
  1002. # endif
  1003. #endif
  1004. #if !defined (HAVE_TERMIOS_H) && !defined (HAVE_TERMIO_H) && !defined(__MINGW32__)
  1005. ioctl (tty, TIOCGETP, &ttybuff);
  1006. if (!original_tty_flags)
  1007. original_tty_flags = ttybuff.sg_flags;
  1008. /* Make this terminal pass 8 bits around while we are using it. */
  1009. # if defined (PASS8)
  1010. ttybuff.sg_flags |= PASS8;
  1011. # endif /* PASS8 */
  1012. # if defined (TIOCLGET) && defined (LPASS8)
  1013. {
  1014. int flags;
  1015. ioctl (tty, TIOCLGET, &flags);
  1016. original_lmode = flags;
  1017. flags |= LPASS8;
  1018. ioctl (tty, TIOCLSET, &flags);
  1019. }
  1020. # endif /* TIOCLGET && LPASS8 */
  1021. # if defined (TIOCGETC)
  1022. {
  1023. struct tchars temp;
  1024. ioctl (tty, TIOCGETC, &original_tchars);
  1025. temp = original_tchars;
  1026. /* C-s and C-q. */
  1027. temp.t_startc = temp.t_stopc = -1;
  1028. /* Often set to C-d. */
  1029. temp.t_eofc = -1;
  1030. /* If the a quit or interrupt character conflicts with one of our
  1031. commands, then make it go away. */
  1032. if (temp.t_intrc == '\177')
  1033. temp.t_intrc = -1;
  1034. if (temp.t_quitc == '\177')
  1035. temp.t_quitc = -1;
  1036. ioctl (tty, TIOCSETC, &temp);
  1037. }
  1038. # endif /* TIOCGETC */
  1039. # if defined (TIOCGLTC)
  1040. {
  1041. struct ltchars temp;
  1042. ioctl (tty, TIOCGLTC, &original_ltchars);
  1043. temp = original_ltchars;
  1044. /* Make the interrupt keys go away. Just enough to make people happy. */
  1045. temp.t_lnextc = -1; /* C-v. */
  1046. temp.t_dsuspc = -1; /* C-y. */
  1047. temp.t_flushc = -1; /* C-o. */
  1048. ioctl (tty, TIOCSLTC, &temp);
  1049. }
  1050. # endif /* TIOCGLTC */
  1051. # ifndef __MINGW32__
  1052. ttybuff.sg_flags &= ~ECHO;
  1053. ttybuff.sg_flags |= CBREAK;
  1054. ioctl (tty, TIOCSETN, &ttybuff);
  1055. # endif
  1056. #endif /* !HAVE_TERMIOS_H && !HAVE_TERMIO_H */
  1057. return 1;
  1058. }
  1059. /* Restore the tty settings back to what they were before we started using
  1060. this terminal. */
  1061. void
  1062. terminal_unprep_terminal (void)
  1063. {
  1064. int tty;
  1065. if (terminal_unprep_terminal_hook)
  1066. {
  1067. (*terminal_unprep_terminal_hook) ();
  1068. return;
  1069. }
  1070. tty = fileno (stdin);
  1071. #if defined (HAVE_TERMIOS_H)
  1072. tcsetattr (tty, TCSANOW, &original_termios);
  1073. #else
  1074. # if defined (HAVE_TERMIO_H)
  1075. ioctl (tty, TCSETA, &original_termio);
  1076. # else /* !HAVE_TERMIO_H */
  1077. # ifndef __MINGW32__
  1078. ioctl (tty, TIOCGETP, &ttybuff);
  1079. ttybuff.sg_flags = original_tty_flags;
  1080. ioctl (tty, TIOCSETN, &ttybuff);
  1081. # endif
  1082. # if defined (TIOCGETC)
  1083. ioctl (tty, TIOCSETC, &original_tchars);
  1084. # endif /* TIOCGETC */
  1085. # if defined (TIOCGLTC)
  1086. ioctl (tty, TIOCSLTC, &original_ltchars);
  1087. # endif /* TIOCGLTC */
  1088. # if defined (TIOCLGET) && defined (LPASS8)
  1089. ioctl (tty, TIOCLSET, &original_lmode);
  1090. # endif /* TIOCLGET && LPASS8 */
  1091. # endif /* !HAVE_TERMIO_H */
  1092. #endif /* !HAVE_TERMIOS_H */
  1093. terminal_end_using_terminal ();
  1094. }
  1095. #if defined(__MSDOS__) || defined(__MINGW32__)
  1096. # include "pcterm.c"
  1097. #endif