pcterm.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724
  1. /* pcterm.c -- How to handle the PC terminal for Info under MS-DOS/MS-Windows.
  2. $Id$
  3. Copyright 1998, 1999, 2003, 2004, 2007, 2008, 2012, 2013, 2014, 2015
  4. Free Software Foundation, Inc.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* WARNING WARNING WARNING!!! This probably won't work as is with
  16. anything but DJGPP and MinGW! However, Borland should come close,
  17. and other PC compilers will need minor modifications. */
  18. #ifdef __MSDOS__
  19. /* intl/libintl.h defines a macro `gettext' which
  20. conflicts with conio.h header. */
  21. #ifdef gettext
  22. # undef gettext
  23. # define gettext _gettext
  24. #endif
  25. #include <pc.h>
  26. #include <keys.h>
  27. #include <conio.h>
  28. #endif
  29. #ifdef _WIN32
  30. #include <io.h>
  31. #include <conio.h>
  32. #include <process.h>
  33. #include <malloc.h> /* for alloca */
  34. #define WIN32_LEAN_AND_MEAN
  35. #include <windows.h>
  36. #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
  37. #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 4
  38. #endif
  39. #ifndef COMMON_LVB_UNDERSCORE
  40. #define COMMON_LVB_UNDERSCORE 0x8000
  41. #endif
  42. struct text_info {
  43. WORD normattr;
  44. WORD attribute;
  45. SHORT winleft;
  46. SHORT wintop;
  47. SHORT winright;
  48. SHORT winbottom;
  49. SHORT screenheight;
  50. SHORT screenwidth;
  51. SHORT curx;
  52. SHORT cury;
  53. COORD bufsize;
  54. unsigned char currmode; /* unused and unsupported for Windows */
  55. };
  56. struct termios {
  57. int dummy;
  58. };
  59. enum text_modes { LASTMODE=-1 };
  60. #define cprintf _cprintf
  61. #define cputs _cputs
  62. #undef read
  63. #undef _read
  64. #include "display.h"
  65. void reset_info_window_sizes (void);
  66. void redisplay_after_signal (void);
  67. #endif
  68. #include "variables.h"
  69. #include "session.h"
  70. #include "terminal.h"
  71. extern int speech_friendly; /* defined in info.c */
  72. /* **************************************************************** */
  73. /* */
  74. /* PC Terminal Output Functions */
  75. /* */
  76. /* **************************************************************** */
  77. static struct text_info outside_info; /* holds screen params outside Info */
  78. #ifdef _WIN32
  79. static WORD norm_attr, inv_attr, xref_attr;
  80. static WORD current_attr;
  81. static HANDLE hstdin = INVALID_HANDLE_VALUE;
  82. static HANDLE hstdout = INVALID_HANDLE_VALUE;
  83. static HANDLE hinfo = INVALID_HANDLE_VALUE;
  84. static HANDLE hscreen = INVALID_HANDLE_VALUE;
  85. static DWORD old_inpmode;
  86. static DWORD old_outpmode;
  87. static UINT output_cp;
  88. #else
  89. static unsigned char norm_attr, inv_attr, xref_attr;
  90. #endif
  91. static unsigned const char * find_sequence (int);
  92. #ifdef _WIN32
  93. /* Windows-specific initialization and de-initialization. */
  94. void
  95. w32_info_prep (void)
  96. {
  97. if (hinfo != INVALID_HANDLE_VALUE)
  98. {
  99. DWORD new_mode;
  100. SetConsoleActiveScreenBuffer (hinfo);
  101. current_attr = norm_attr;
  102. hscreen = hinfo;
  103. SetConsoleMode (hstdin, ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
  104. GetConsoleMode (hscreen, &old_outpmode);
  105. new_mode = old_outpmode & ~ENABLE_WRAP_AT_EOL_OUTPUT;
  106. SetConsoleMode (hscreen, new_mode);
  107. /* Enable underline, if available. */
  108. SetConsoleMode (hscreen, new_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
  109. }
  110. }
  111. void
  112. w32_info_unprep (void)
  113. {
  114. if (hinfo != INVALID_HANDLE_VALUE)
  115. {
  116. SetConsoleActiveScreenBuffer (hstdout);
  117. current_attr = outside_info.normattr;
  118. hscreen = hstdout;
  119. SetConsoleMode (hstdin, old_inpmode);
  120. }
  121. }
  122. void
  123. w32_cleanup (void)
  124. {
  125. if (hinfo != INVALID_HANDLE_VALUE)
  126. {
  127. COORD cursor_pos;
  128. /* Restore the original position of the cursor. */
  129. cursor_pos.X = outside_info.curx;
  130. cursor_pos.Y = outside_info.cury;
  131. SetConsoleCursorPosition (hstdout, cursor_pos);
  132. /* Close the input handle we created. */
  133. CloseHandle (hinfo);
  134. }
  135. }
  136. static void w32_info_init (void) __attribute__((constructor));
  137. static void pc_initialize_terminal (char *);
  138. static void
  139. w32_info_init (void)
  140. {
  141. /* We need to set this single hook here; the rest
  142. will be set by pc_initialize_terminal when it is called. */
  143. terminal_initialize_terminal_hook = pc_initialize_terminal;
  144. }
  145. /* Emulate DJGPP conio functions for Windows. */
  146. static void
  147. gettextinfo (struct text_info *ti)
  148. {
  149. CONSOLE_SCREEN_BUFFER_INFO csbi;
  150. static TCHAR errbuf[500];
  151. DWORD ignored;
  152. hstdin = GetStdHandle (STD_INPUT_HANDLE);
  153. hstdout = GetStdHandle (STD_OUTPUT_HANDLE);
  154. if (!GetConsoleMode (hstdin, &ignored))
  155. hstdin = INVALID_HANDLE_VALUE;
  156. if (hstdout != INVALID_HANDLE_VALUE
  157. && GetConsoleMode (hstdout, &ignored))
  158. {
  159. hinfo = CreateConsoleScreenBuffer (GENERIC_READ | GENERIC_WRITE,
  160. FILE_SHARE_READ | FILE_SHARE_WRITE,
  161. NULL, CONSOLE_TEXTMODE_BUFFER, NULL);
  162. if (hinfo != INVALID_HANDLE_VALUE
  163. && GetConsoleScreenBufferInfo (hstdout, &csbi))
  164. {
  165. ti->normattr = csbi.wAttributes;
  166. ti->winleft = 1;
  167. ti->wintop = 1;
  168. ti->winright = csbi.srWindow.Right + 1;
  169. ti->winbottom = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  170. ti->attribute = csbi.wAttributes;
  171. ti->screenheight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  172. ti->screenwidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
  173. ti->curx = csbi.dwCursorPosition.X;
  174. ti->cury = csbi.dwCursorPosition.Y;
  175. ti->bufsize = csbi.dwSize;
  176. atexit (w32_cleanup);
  177. }
  178. else
  179. {
  180. DWORD error_no = GetLastError ();
  181. if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL,
  182. error_no,
  183. 0, /* choose most suitable language */
  184. errbuf, sizeof (errbuf), NULL))
  185. sprintf (errbuf, "w32 error %u", error_no);
  186. CloseHandle (hinfo);
  187. info_error (_("Terminal cannot be initialized: %s\n"), errbuf);
  188. exit (1);
  189. }
  190. }
  191. else
  192. {
  193. /* We were invoked non-interactively. Do the minimum we must. */
  194. ti->screenheight = 24;
  195. ti->screenwidth = 80;
  196. }
  197. }
  198. void
  199. textattr (int attr)
  200. {
  201. if (hscreen != INVALID_HANDLE_VALUE)
  202. SetConsoleTextAttribute (hscreen, attr);
  203. }
  204. void
  205. textmode (int mode)
  206. {
  207. /* Nothing. */
  208. }
  209. void
  210. highvideo (void)
  211. {
  212. int attr;
  213. CONSOLE_SCREEN_BUFFER_INFO csbi;
  214. GetConsoleScreenBufferInfo (hscreen, &csbi);
  215. attr = csbi.wAttributes | FOREGROUND_INTENSITY;
  216. attr ^= norm_attr & FOREGROUND_INTENSITY;
  217. textattr (attr);
  218. }
  219. void
  220. normvideo (void)
  221. {
  222. int attr;
  223. CONSOLE_SCREEN_BUFFER_INFO csbi;
  224. GetConsoleScreenBufferInfo (hscreen, &csbi);
  225. attr = csbi.wAttributes & ~(FOREGROUND_INTENSITY | BACKGROUND_INTENSITY
  226. | COMMON_LVB_UNDERSCORE);
  227. attr |= norm_attr & (FOREGROUND_INTENSITY | BACKGROUND_INTENSITY);
  228. textattr (attr);
  229. }
  230. void
  231. blinkvideo (void)
  232. {
  233. int attr;
  234. CONSOLE_SCREEN_BUFFER_INFO csbi;
  235. GetConsoleScreenBufferInfo (hscreen, &csbi);
  236. attr = csbi.wAttributes | BACKGROUND_INTENSITY;
  237. attr ^= norm_attr & BACKGROUND_INTENSITY;
  238. textattr (attr);
  239. }
  240. void
  241. underline (void)
  242. {
  243. int attr;
  244. CONSOLE_SCREEN_BUFFER_INFO csbi;
  245. GetConsoleScreenBufferInfo (hscreen, &csbi);
  246. attr = csbi.wAttributes | COMMON_LVB_UNDERSCORE;
  247. textattr (attr);
  248. }
  249. void
  250. textcolor (int color)
  251. {
  252. int attr;
  253. CONSOLE_SCREEN_BUFFER_INFO csbi;
  254. GetConsoleScreenBufferInfo (hscreen, &csbi);
  255. attr = (csbi.wAttributes & (COMMON_LVB_UNDERSCORE | 0xf0)) | (color & 0x0f);
  256. textattr (attr);
  257. }
  258. void
  259. textbackground (int color)
  260. {
  261. int attr;
  262. CONSOLE_SCREEN_BUFFER_INFO csbi;
  263. GetConsoleScreenBufferInfo (hscreen, &csbi);
  264. attr = (csbi.wAttributes & (COMMON_LVB_UNDERSCORE | 0x0f)) | ((color & 0x0f) << 4);
  265. textattr (attr);
  266. }
  267. void
  268. ScreenGetCursor (int *row, int *col)
  269. {
  270. CONSOLE_SCREEN_BUFFER_INFO csbi;
  271. if (hscreen == INVALID_HANDLE_VALUE)
  272. *row = *col = 0;
  273. else
  274. {
  275. GetConsoleScreenBufferInfo (hscreen, &csbi);
  276. *row = csbi.dwCursorPosition.Y;
  277. *col = csbi.dwCursorPosition.X;
  278. }
  279. }
  280. void
  281. ScreenSetCursor (int row, int col)
  282. {
  283. if (hscreen != INVALID_HANDLE_VALUE)
  284. {
  285. COORD cursor_pos;
  286. cursor_pos.X = col;
  287. cursor_pos.Y = row;
  288. SetConsoleCursorPosition (hscreen, cursor_pos);
  289. }
  290. }
  291. void
  292. ScreenClear (void)
  293. {
  294. if (hscreen != INVALID_HANDLE_VALUE)
  295. {
  296. DWORD nchars = screenwidth * screenheight;
  297. COORD start_pos;
  298. DWORD written;
  299. start_pos.X = start_pos.Y = 0;
  300. FillConsoleOutputAttribute (hscreen, norm_attr, nchars, start_pos,
  301. &written);
  302. FillConsoleOutputCharacter (hscreen, ' ', nchars, start_pos, &written);
  303. }
  304. }
  305. void
  306. clreol (void)
  307. {
  308. if (hscreen != INVALID_HANDLE_VALUE)
  309. {
  310. DWORD nchars;
  311. COORD start_pos;
  312. DWORD written;
  313. CONSOLE_SCREEN_BUFFER_INFO csbi;
  314. GetConsoleScreenBufferInfo (hscreen, &csbi);
  315. start_pos = csbi.dwCursorPosition;
  316. nchars = csbi.dwSize.X - start_pos.X;
  317. FillConsoleOutputAttribute (hscreen, current_attr, nchars, start_pos,
  318. &written);
  319. FillConsoleOutputCharacter (hscreen, ' ', nchars, start_pos, &written);
  320. }
  321. }
  322. void
  323. ScreenVisualBell (void)
  324. {
  325. if (hscreen != INVALID_HANDLE_VALUE)
  326. {
  327. DWORD nchars = screenwidth * screenheight;
  328. COORD start_pos;
  329. DWORD written;
  330. PWORD attr;
  331. DWORD i;
  332. start_pos.X = start_pos.Y = 0;
  333. attr = xmalloc (nchars * sizeof (WORD));
  334. ReadConsoleOutputAttribute (hscreen, attr, nchars, start_pos, &written);
  335. for (i = 0; i < nchars; ++i)
  336. attr[i] ^= norm_attr ^ inv_attr;
  337. WriteConsoleOutputAttribute (hscreen, attr, nchars, start_pos, &written);
  338. Sleep (50);
  339. for (i = 0; i < nchars; ++i)
  340. attr[i] ^= norm_attr ^ inv_attr;
  341. WriteConsoleOutputAttribute (hscreen, attr, nchars, start_pos, &written);
  342. free (attr);
  343. }
  344. else
  345. {
  346. printf ("%c", '\a');
  347. fflush (stdout);
  348. }
  349. }
  350. int
  351. movetext(int left, int top, int right, int bottom, int destleft, int desttop)
  352. {
  353. if (hscreen != INVALID_HANDLE_VALUE)
  354. {
  355. SMALL_RECT src;
  356. COORD dest;
  357. CHAR_INFO fill;
  358. src.Left = left - 1;
  359. src.Top = top - 1;
  360. src.Right = right - 1;
  361. src.Bottom = bottom - 1;
  362. dest.X = destleft - 1;
  363. dest.Y = desttop - 1;
  364. fill.Attributes = norm_attr;
  365. fill.Char.AsciiChar = (CHAR)' ';
  366. return ScrollConsoleScreenBuffer (hscreen, &src , NULL, dest, &fill) != 0;
  367. }
  368. else
  369. return 0;
  370. }
  371. int
  372. ScreenRows (void)
  373. {
  374. if (hscreen != INVALID_HANDLE_VALUE)
  375. {
  376. CONSOLE_SCREEN_BUFFER_INFO csbi;
  377. GetConsoleScreenBufferInfo (hscreen, &csbi);
  378. return csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  379. }
  380. else
  381. return 24;
  382. }
  383. int
  384. ScreenCols (void)
  385. {
  386. if (hscreen != INVALID_HANDLE_VALUE)
  387. {
  388. CONSOLE_SCREEN_BUFFER_INFO csbi;
  389. GetConsoleScreenBufferInfo (hscreen, &csbi);
  390. return csbi.srWindow.Right - csbi.srWindow.Left + 1;
  391. }
  392. else
  393. return 80;
  394. }
  395. void
  396. _set_screen_lines (int lines)
  397. {
  398. if (hscreen != INVALID_HANDLE_VALUE)
  399. {
  400. SMALL_RECT window_rectangle;
  401. CONSOLE_SCREEN_BUFFER_INFO csbi;
  402. COORD scrbufsize;
  403. GetConsoleScreenBufferInfo (hscreen, &csbi);
  404. window_rectangle = csbi.srWindow;
  405. window_rectangle.Bottom = window_rectangle.Top + lines - 1;
  406. SetConsoleWindowInfo (hscreen, TRUE, &window_rectangle);
  407. /* Set the screen buffer size to the same dimensions as the window,
  408. so that the dysfunctional scroll bar disappears. */
  409. scrbufsize.X = window_rectangle.Right - window_rectangle.Left + 1;
  410. scrbufsize.Y = window_rectangle.Bottom - window_rectangle.Top + 1;
  411. SetConsoleScreenBufferSize (hscreen, scrbufsize);
  412. }
  413. }
  414. void
  415. w32_set_screen_dimensions (int cols, int rows)
  416. {
  417. if (hscreen != INVALID_HANDLE_VALUE)
  418. {
  419. SMALL_RECT window_rectangle;
  420. CONSOLE_SCREEN_BUFFER_INFO csbi;
  421. GetConsoleScreenBufferInfo (hscreen, &csbi);
  422. window_rectangle = csbi.srWindow;
  423. window_rectangle.Bottom = window_rectangle.Top + rows - 1;
  424. window_rectangle.Right = window_rectangle.Left + cols - 1;
  425. SetConsoleWindowInfo (hscreen, TRUE, &window_rectangle);
  426. }
  427. }
  428. /* Emulate `sleep'. */
  429. unsigned
  430. sleep (unsigned sec)
  431. {
  432. Sleep (sec*1000);
  433. return 0;
  434. }
  435. /* Keyboard input support. */
  436. static int
  437. w32_our_tty (int fd)
  438. {
  439. /* Is this our tty? */
  440. return hstdin != INVALID_HANDLE_VALUE
  441. && hstdin == (HANDLE)_get_osfhandle (fd);
  442. }
  443. /* Translate a Windows key event into the equivalent sequence of bytes
  444. to be submitted to Info dispatcher. */
  445. #define define_seq(p,s1,s2) \
  446. do { \
  447. if ((ctl & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) != 0) \
  448. memcpy (p, s1, sizeof (s1)), p += sizeof (s1) - 1; \
  449. else \
  450. memcpy (p, s2, sizeof (s2)), p += sizeof (s2) - 1; \
  451. } while (0)
  452. static int
  453. w32keyseq (unsigned char ascii_ch, WORD vkey, DWORD ctl, unsigned char *seq)
  454. {
  455. unsigned char *p = seq;
  456. switch (ascii_ch)
  457. {
  458. case '\0':
  459. /* Keys with no ASCII code are extended keys, like arrows. */
  460. switch (vkey)
  461. {
  462. case VK_PRIOR:
  463. define_seq (p, "\033\061p", "\033v");
  464. break;
  465. case VK_NEXT:
  466. define_seq (p, "\033\061n", "\026");
  467. break;
  468. case VK_END:
  469. define_seq (p, "\033>", "\033>");
  470. break;
  471. case VK_HOME:
  472. define_seq (p, "\033<", "\033<");
  473. break;
  474. case VK_LEFT:
  475. define_seq (p, "\033b", "\033[D");
  476. break;
  477. case VK_UP:
  478. define_seq (p, "\033\061u", "\033[A");
  479. break;
  480. case VK_RIGHT:
  481. define_seq (p, "\033f", "\033[C");
  482. break;
  483. case VK_DOWN:
  484. define_seq (p, "\033\061m", "\033[B");
  485. break;
  486. case VK_INSERT:
  487. define_seq (p, "\033[L", "\033[L");
  488. break;
  489. case VK_DELETE: /* Delete => Ctrl-d, Alt-Delete => ESC d */
  490. if ((ctl & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) != 0)
  491. define_seq (p, "\033d", "\033d");
  492. else
  493. define_seq (p, "\033d", "\004");
  494. break;
  495. case VK_HELP: /* F1 => Ctrl-h */
  496. case VK_F1:
  497. *p++ = '\010';
  498. break;
  499. case 50: /* Ctrl-@ => '\0' */
  500. if ((ctl & SHIFT_PRESSED) != 0)
  501. *p++ = '\0';
  502. break;
  503. default:
  504. if (0x41 <= vkey && vkey <= 0x5a)
  505. {
  506. /* Alt-Ctrl-a, Alt-Ctrl-b, etc. */
  507. *p++ = '\033';
  508. *p++ = '\001' + vkey - 0x41;
  509. }
  510. }
  511. break;
  512. case ' ': /* Ctrl-SPC => '\0' */
  513. if ((ctl & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) != 0)
  514. ascii_ch = '\0';
  515. *p++ = ascii_ch;
  516. break;
  517. case '\t': /* Shift-TAB/Alt-TAB => Esc-TAB */
  518. if ((ctl & (SHIFT_PRESSED | LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) != 0)
  519. {
  520. memcpy (p, "\033\011", sizeof ("\033\011"));
  521. p += sizeof ("\033\011") - 1;
  522. }
  523. else
  524. *p++ = '\t';
  525. break;
  526. case '\b':
  527. /* Backspace => DEL. */
  528. ascii_ch = '\177';
  529. /* FALLTHROUGH */
  530. default:
  531. if ((ctl & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) != 0)
  532. *p++ = '\033';
  533. *p++ = ascii_ch;
  534. break;
  535. }
  536. return p - seq;
  537. }
  538. static unsigned char buffered_chars[512];
  539. static size_t buf_head;
  540. static size_t buf_tail;
  541. static ssize_t
  542. w32_kbd_read (unsigned char *inbuf, size_t n)
  543. {
  544. DWORD nevents, nread;
  545. INPUT_RECORD inrec;
  546. ssize_t nret = 0;
  547. do {
  548. /* Stuff any unread buffered characters. */
  549. while (buf_head < buf_tail && n > 0)
  550. {
  551. *inbuf++ = buffered_chars[buf_head++];
  552. nret++;
  553. n--;
  554. }
  555. if (n <= 0)
  556. break;
  557. /* Wait for input. */
  558. while (GetNumberOfConsoleInputEvents (hstdin, &nevents)
  559. && nevents < 1)
  560. Sleep (20);
  561. while (nevents-- && n > 0)
  562. {
  563. if (!ReadConsoleInput (hstdin, &inrec, 1, &nread))
  564. return -1;
  565. if (nread > 0)
  566. {
  567. switch (inrec.EventType)
  568. {
  569. case KEY_EVENT:
  570. if (inrec.Event.KeyEvent.bKeyDown == TRUE
  571. && !(inrec.Event.KeyEvent.wVirtualScanCode == 0
  572. || inrec.Event.KeyEvent.wVirtualKeyCode == VK_SHIFT
  573. || inrec.Event.KeyEvent.wVirtualKeyCode == VK_CONTROL
  574. || inrec.Event.KeyEvent.wVirtualKeyCode == VK_MENU))
  575. {
  576. unsigned char keyseq[10];
  577. int count = inrec.Event.KeyEvent.wRepeatCount;
  578. unsigned char ch = inrec.Event.KeyEvent.uChar.AsciiChar;
  579. WORD vkey = inrec.Event.KeyEvent.wVirtualKeyCode;
  580. DWORD ctl_state = inrec.Event.KeyEvent.dwControlKeyState;
  581. int nbytes = w32keyseq (ch, vkey, ctl_state, keyseq);
  582. /* Supply up to N characters to the caller. */
  583. while (count && n >= nbytes)
  584. {
  585. if (nbytes == 1 && keyseq[0] == '\032')
  586. {
  587. terminal_goto_xy (0, screenheight - 1);
  588. terminal_clear_to_eol ();
  589. fflush (stdout);
  590. terminal_unprep_terminal ();
  591. kill (getpid (), 0);
  592. terminal_prep_terminal ();
  593. reset_info_window_sizes ();
  594. }
  595. else
  596. {
  597. memcpy (&inbuf[nret], keyseq, nbytes);
  598. nret += nbytes;
  599. n -= nbytes;
  600. }
  601. count--;
  602. }
  603. /* Buffer the rest. */
  604. if (count > 0)
  605. {
  606. buf_head = buf_tail = 0;
  607. while (count--
  608. && buf_tail < sizeof(buffered_chars) - nbytes)
  609. {
  610. memcpy (&buffered_chars[buf_tail], keyseq, nbytes);
  611. buf_tail += nbytes;
  612. }
  613. }
  614. }
  615. break;
  616. case WINDOW_BUFFER_SIZE_EVENT:
  617. {
  618. int rows, cols;
  619. /* Note: this event is only sent when the console
  620. window's _screen_buffer_ size is changed via
  621. the Properties->Layout dialog. */
  622. cols = inrec.Event.WindowBufferSizeEvent.dwSize.X;
  623. rows = inrec.Event.WindowBufferSizeEvent.dwSize.Y;
  624. screenwidth = cols;
  625. screenheight = rows;
  626. w32_set_screen_dimensions (cols, rows);
  627. display_initialize_display (screenwidth, screenheight);
  628. window_new_screen_size (screenwidth, screenheight);
  629. redisplay_after_signal ();
  630. }
  631. break;
  632. case MOUSE_EVENT:
  633. {
  634. /* Only vertical wheel support for now. */
  635. int wheeled =
  636. (inrec.Event.MouseEvent.dwEventFlags & MOUSE_WHEELED) != 0;
  637. if (wheeled && mouse_protocol == MP_NORMAL_TRACKING)
  638. {
  639. extern void info_up_line (WINDOW *, int count);
  640. extern void info_down_line (WINDOW *, int count);
  641. extern WINDOW *active_window;
  642. int hiword =
  643. HIWORD (inrec.Event.MouseEvent.dwButtonState);
  644. if ((hiword & 0xFF00) == 0)
  645. info_up_line (active_window, 3);
  646. else
  647. info_down_line (active_window, 3);
  648. display_update_display ();
  649. }
  650. }
  651. break;
  652. default:
  653. break;
  654. }
  655. }
  656. }
  657. } while (n > 0);
  658. return nret;
  659. }
  660. long
  661. w32_chars_avail (int fd)
  662. {
  663. if (w32_our_tty (fd))
  664. return buf_tail - buf_head;
  665. else
  666. {
  667. struct stat st;
  668. if (fstat (fd, &st) < 0)
  669. return 1;
  670. else
  671. return st.st_size;
  672. }
  673. }
  674. ssize_t
  675. w32_read (int fd, void *buf, size_t n)
  676. {
  677. if (w32_our_tty (fd))
  678. return w32_kbd_read (buf, n);
  679. else
  680. return _read (fd, buf, n);
  681. }
  682. /* Write to the console a string of text encoded in UTF-8 or UTF-7. */
  683. static void
  684. write_utf (DWORD cp, const char *text, int nbytes)
  685. {
  686. /* MSDN says UTF-7 requires zero in flags. */
  687. DWORD flags = (cp == CP_UTF7) ? 0 : MB_ERR_INVALID_CHARS;
  688. /* How much space do we need for wide characters? */
  689. int wlen = MultiByteToWideChar (cp, flags, text, nbytes, NULL, 0);
  690. if (wlen)
  691. {
  692. WCHAR *text_w = alloca (wlen * sizeof (WCHAR));
  693. DWORD written;
  694. if (MultiByteToWideChar (cp, flags, text, nbytes, text_w, wlen) > 0)
  695. {
  696. WriteConsoleW (hscreen, text_w, (nbytes < 0) ? wlen - 1 : wlen,
  697. &written, NULL);
  698. return;
  699. }
  700. }
  701. /* Fall back on conio. */
  702. if (nbytes < 0)
  703. cputs (text);
  704. else
  705. cprintf ("%.*s", nbytes, text);
  706. }
  707. /* A replacement for nl_langinfo which does a more accurate job for
  708. the console output codeset. Windows can use 3 different encodings
  709. at the same time, and the Posix-compliant nl_langinfo simply
  710. doesn't know enough to decide which one is needed when CODESET is
  711. requested. */
  712. #undef nl_langinfo
  713. #include <langinfo.h>
  714. char *
  715. rpl_nl_langinfo (nl_item item)
  716. {
  717. if (item == CODESET)
  718. {
  719. static char buf[100];
  720. /* We need all the help we can get from GNU libiconv, so we
  721. request transliteration as well. */
  722. sprintf (buf, "CP%u//TRANSLIT", GetConsoleOutputCP ());
  723. return buf;
  724. }
  725. else
  726. return nl_langinfo (item);
  727. }
  728. #ifndef HAVE_WCWIDTH
  729. /* A replacement for wcwidth. The Gnulib version calls setlocale for
  730. every character Info is about to display, which makes display of
  731. large nodes annoyingly slow.
  732. Note that the Gnulib version is still compiled and put into
  733. libgnu.a, because the configure script doesn't know about this
  734. replacement. But the linker will not pull the Gnulib version into
  735. the binary, because it resolves the calls to this replacement
  736. function. */
  737. int
  738. wcwidth (wchar_t wc)
  739. {
  740. return wc == 0 ? 0 : iswprint (wc) ? 1 : -1;
  741. }
  742. #endif
  743. #endif /* _WIN32 */
  744. /* Turn on reverse video. */
  745. static void
  746. pc_begin_inverse (void)
  747. {
  748. textattr (inv_attr);
  749. }
  750. /* Turn off reverse video. */
  751. static void
  752. pc_end_inverse (void)
  753. {
  754. textattr (norm_attr);
  755. }
  756. /* The implementation of the underlined text. The DOS/Windows console
  757. doesn't support underlined text (until Win10), so we make it blue instead
  758. (blue, because this face is used for hyperlinks). */
  759. static void
  760. pc_begin_underline (void)
  761. {
  762. if (xref_attr != COMMON_LVB_UNDERSCORE)
  763. textattr (xref_attr);
  764. else
  765. underline ();
  766. }
  767. static void
  768. pc_end_underline (void)
  769. {
  770. if (xref_attr != COMMON_LVB_UNDERSCORE)
  771. textattr (norm_attr);
  772. else
  773. normvideo ();
  774. }
  775. /* Standout (a.k.a. "high video") text. */
  776. static void
  777. pc_begin_standout (void)
  778. {
  779. highvideo ();
  780. }
  781. static void
  782. pc_end_standout (void)
  783. {
  784. normvideo ();
  785. }
  786. static void
  787. pc_begin_blink (void)
  788. {
  789. blinkvideo ();
  790. }
  791. static void
  792. pc_default_color (void)
  793. {
  794. textattr (norm_attr);
  795. }
  796. /* Info definitions of 8 colors (see terminal.h) are in an order
  797. that's different from Windows/DOS console colors. This function
  798. unscrambles the order, and also maps 8 standard ANSI colors to the
  799. low-intensity shades of the 16 PC colors, so that "standout" works
  800. by turning the intensity bit. */
  801. static int
  802. convert_color (int terminal_color)
  803. {
  804. /* The terminal.h order is:
  805. black, red, green, yellow, blue, magenta, cyan, white. */
  806. static int pc_color_map[] = {
  807. 0, 4, 2, 6, 1, 5, 3, 7
  808. };
  809. int intensity = terminal_color & (FOREGROUND_INTENSITY | BACKGROUND_INTENSITY);
  810. terminal_color &= ~(FOREGROUND_INTENSITY | BACKGROUND_INTENSITY);
  811. if (terminal_color >= 0
  812. && terminal_color < sizeof(pc_color_map) / sizeof (pc_color_map[0]))
  813. return pc_color_map[terminal_color] | intensity;
  814. return 7; /* lightgray */
  815. }
  816. static void
  817. pc_set_fg_color (int color)
  818. {
  819. textcolor (convert_color (color) | (norm_attr & FOREGROUND_INTENSITY));
  820. }
  821. static void
  822. pc_set_bg_color (int color)
  823. {
  824. textbackground (convert_color (color) | (norm_attr & BACKGROUND_INTENSITY));
  825. }
  826. /* Move the cursor up one line. */
  827. static void
  828. pc_up_line (void)
  829. {
  830. int x, y;
  831. ScreenGetCursor (&y, &x);
  832. ScreenSetCursor (MAX (y-1, 0), x);
  833. }
  834. /* Move the cursor down one line. */
  835. static void
  836. pc_down_line (void)
  837. {
  838. int x, y;
  839. ScreenGetCursor (&y, &x);
  840. ScreenSetCursor (MIN (screenheight-1, y+1), x);
  841. }
  842. /* Clear the entire terminal screen. */
  843. static void
  844. pc_clear_screen (void)
  845. {
  846. ScreenClear ();
  847. }
  848. /* Clear from the current position of the cursor to the end of the line. */
  849. static void
  850. pc_clear_to_eol (void)
  851. {
  852. clreol (); /* perhaps to be replaced by a loop */
  853. }
  854. /* Set the global variables SCREENWIDTH and SCREENHEIGHT. */
  855. static void
  856. pc_get_screen_size(void)
  857. {
  858. /* Current screen dimensions are the default. */
  859. if (!outside_info.screenheight) /* paranoia */
  860. gettextinfo (&outside_info);
  861. screenwidth = outside_info.screenwidth;
  862. screenheight = outside_info.screenheight;
  863. /* Environment variable "LINES" overrides the default. */
  864. if (getenv ("LINES") != NULL)
  865. screenheight = atoi (getenv ("LINES"));
  866. /* Environment variable "INFO_LINES" overrides "LINES". */
  867. if (getenv ("INFO_LINES") != NULL)
  868. screenheight = atoi (getenv ("INFO_LINES"));
  869. }
  870. /* Move the cursor to the terminal location of X and Y. */
  871. static void
  872. pc_goto_xy (x, y)
  873. int x, y;
  874. {
  875. ScreenSetCursor (y, x); /* yes, pc.h says ScreenSetCursor (row, column) !! */
  876. }
  877. /* Print STRING to the terminal at the current position. */
  878. static void
  879. pc_put_text (string)
  880. char *string;
  881. {
  882. if (speech_friendly)
  883. fputs (string, stdout);
  884. #ifdef __MINGW32__
  885. else if (hscreen == INVALID_HANDLE_VALUE)
  886. fputs (string, stdout);
  887. else if (output_cp == CP_UTF8 || output_cp == CP_UTF7)
  888. write_utf (output_cp, string, -1);
  889. #endif
  890. else
  891. cputs (string);
  892. }
  893. /* Ring the terminal bell. The bell is rung visibly if the terminal is
  894. capable of doing that, and if terminal_use_visible_bell_p is non-zero. */
  895. static void
  896. pc_ring_bell(void)
  897. {
  898. if (terminal_has_visible_bell_p && terminal_use_visible_bell_p)
  899. ScreenVisualBell ();
  900. else
  901. {
  902. printf ("%c",'\a');
  903. fflush (stdout);
  904. }
  905. }
  906. /* Print NCHARS from STRING to the terminal at the current position. */
  907. static void
  908. pc_write_chars (string, nchars)
  909. char *string;
  910. int nchars;
  911. {
  912. if (!nchars)
  913. return;
  914. if (speech_friendly)
  915. printf ("%.*s", nchars, string);
  916. #ifdef __MINGW32__
  917. else if (hscreen == INVALID_HANDLE_VALUE)
  918. printf ("%.*s", nchars, string);
  919. else if (output_cp == CP_UTF8 || output_cp == CP_UTF7)
  920. write_utf (output_cp, string, nchars);
  921. #endif
  922. else
  923. cprintf ("%.*s", nchars, string);
  924. }
  925. /* Scroll an area of the terminal from START to (and excluding) END,
  926. AMOUNT lines. If AMOUNT is negative, the lines are scrolled
  927. towards the top of the screen, else they are scrolled towards the
  928. bottom of the screen. The lines of the old region which do not
  929. overlap the new region are cleared, to mimic terminal operation. */
  930. static void
  931. pc_scroll_terminal (start, end, amount)
  932. int start, end, amount;
  933. {
  934. int line_to_clear = amount > 0 ? start : end + amount;
  935. /* Move the text. Note that `movetext' expects 1-based coordinates. */
  936. movetext (1, start + 1, ScreenCols (), end, 1, start + amount + 1);
  937. /* Now clear the lines which were left unoccupied. */
  938. if (amount < 0)
  939. amount = -amount;
  940. while (amount--)
  941. {
  942. ScreenSetCursor (line_to_clear++, 0);
  943. clreol ();
  944. }
  945. }
  946. /* Put the screen in the video mode and colors which Info will use.
  947. Prepare to start using the terminal to read characters singly. */
  948. static void
  949. pc_prep_terminal (void)
  950. {
  951. int tty;
  952. #ifdef _WIN32
  953. w32_info_prep ();
  954. #endif
  955. /* Do not set screen height if we already have it, because
  956. doing so erases the screen. */
  957. if (screenheight != ScreenRows ())
  958. _set_screen_lines (screenheight);
  959. /* Don't fail if they asked for screen dimensions that their
  960. hardware cannot support. */
  961. screenheight = ScreenRows ();
  962. screenwidth = ScreenCols ();
  963. /* Try setting the colors user asked for. */
  964. textattr (norm_attr);
  965. ScreenClear ();
  966. /* Switch console reads to binary mode. */
  967. tty = fileno (stdin);
  968. #ifdef __DJGPP__
  969. setmode (tty, O_BINARY);
  970. __djgpp_set_ctrl_c (1); /* re-enable SIGINT generation by Ctrl-C */
  971. #endif
  972. }
  973. /* Restore the tty settings back to what they were before we started using
  974. this terminal. */
  975. static void
  976. pc_unprep_terminal (void)
  977. {
  978. int tty;
  979. #ifdef _WIN32
  980. w32_info_unprep ();
  981. #endif
  982. textattr (outside_info.normattr);
  983. /* Do not set screen height if we already have it, because
  984. doing so erases the screen. */
  985. if (outside_info.screenheight != ScreenRows ())
  986. {
  987. _set_screen_lines (outside_info.screenheight);
  988. textmode (LASTMODE);
  989. }
  990. #ifdef __MSDOS__
  991. else
  992. pc_clear_to_eol (); /* for text attributes to really take effect */
  993. #endif
  994. #ifdef _WIN32
  995. if (hscreen != INVALID_HANDLE_VALUE)
  996. SetConsoleScreenBufferSize (hstdout, outside_info.bufsize);
  997. #endif
  998. /* Switch back to text mode on stdin. */
  999. tty = fileno (stdin);
  1000. #ifdef __DJGPP__
  1001. setmode (tty, O_TEXT);
  1002. #endif
  1003. }
  1004. /* Initialize the terminal which is known as TERMINAL_NAME. If this
  1005. terminal doesn't have cursor addressability, `terminal_is_dumb_p'
  1006. becomes nonzero. The variables SCREENHEIGHT and SCREENWIDTH are set
  1007. to the dimensions that this terminal actually has. Finally, the
  1008. terminal screen is cleared. */
  1009. static void
  1010. pc_initialize_terminal (term_name)
  1011. char *term_name;
  1012. {
  1013. char *info_colors;
  1014. if (!term_name)
  1015. {
  1016. term_name = getenv ("TERM");
  1017. if (!term_name)
  1018. #ifdef __MSDOS__
  1019. term_name = "pc-dos"; /* ``what's in a name?'' */
  1020. #endif
  1021. #ifdef _WIN32
  1022. term_name = "w32console";
  1023. #endif
  1024. }
  1025. /* Get current video information, to be restored later. */
  1026. if (outside_info.screenwidth == 0)
  1027. gettextinfo (&outside_info);
  1028. /* Current screen colors are the default. */
  1029. norm_attr = outside_info.normattr;
  1030. inv_attr = (((outside_info.normattr & 7) << 4) |
  1031. ((outside_info.normattr & 0x7f) >> 4));
  1032. #ifdef __MSDOS__
  1033. xref_attr = CYAN;
  1034. #endif
  1035. #ifdef _WIN32
  1036. xref_attr = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
  1037. #endif
  1038. xref_attr |= outside_info.normattr & 0xf0;
  1039. /* Does the user want non-default colors? */
  1040. info_colors = getenv ("INFO_COLORS");
  1041. if ((info_colors != (char *)0) && !speech_friendly)
  1042. {
  1043. /* Decode a color from a string descriptor.
  1044. The descriptor string is a sequence of color specifiers separated
  1045. by a non-numeric character. Each color specifier should represent
  1046. a small integer which fits into an unsigned char, and can be given
  1047. in any base supported by strtoul. Examples of valid descriptors:
  1048. "10 31"
  1049. "0x13/0x45"
  1050. "007.077"
  1051. The separator between two color specifiers can be any character which
  1052. cannot be used in a printed representation of an integer number. */
  1053. char *endp;
  1054. unsigned long color_desc = strtoul (info_colors, &endp, 0);
  1055. if (color_desc <= UCHAR_MAX)
  1056. {
  1057. norm_attr = (unsigned char)color_desc;
  1058. xref_attr = (xref_attr & 0x0f) | (norm_attr & 0xf0);
  1059. color_desc = strtoul (endp + 1, &endp, 0);
  1060. if (color_desc <= UCHAR_MAX)
  1061. inv_attr = (unsigned char)color_desc;
  1062. #ifdef _WIN32
  1063. if (*endp == 'u')
  1064. xref_attr = COMMON_LVB_UNDERSCORE;
  1065. else
  1066. #endif
  1067. if (*endp != '\0')
  1068. {
  1069. color_desc = strtoul (endp + 1, &endp, 0);
  1070. if (color_desc <= UCHAR_MAX)
  1071. {
  1072. #ifdef _WIN32
  1073. if (*endp == 'u')
  1074. color_desc |= COMMON_LVB_UNDERSCORE;
  1075. xref_attr = (WORD)color_desc;
  1076. #else
  1077. xref_attr = (unsigned char)color_desc;
  1078. #endif
  1079. }
  1080. }
  1081. }
  1082. }
  1083. /* We can scroll. */
  1084. terminal_can_scroll = 1;
  1085. /* We know how to produce a visible bell, if somebody's looking... */
  1086. if (!speech_friendly)
  1087. terminal_has_visible_bell_p = 1;
  1088. /* We are *certainly* NOT dumb! */
  1089. terminal_is_dumb_p = 0;
  1090. pc_get_screen_size ();
  1091. #ifdef __MINGW32__
  1092. /* Record the screen output codepage. */
  1093. output_cp = GetConsoleOutputCP ();
  1094. #endif
  1095. #ifdef __MSDOS__
  1096. /* Store the arrow keys. */
  1097. term_ku = (char *)find_sequence (K_Up);
  1098. term_kd = (char *)find_sequence (K_Down);
  1099. term_kr = (char *)find_sequence (K_Right);
  1100. term_kl = (char *)find_sequence (K_Left);
  1101. term_kP = (char *)find_sequence (K_PageUp);
  1102. term_kN = (char *)find_sequence (K_PageDown);
  1103. term_kh = (char *)find_sequence (K_Home);
  1104. term_ke = (char *)find_sequence (K_End);
  1105. term_ki = (char *)find_sequence (K_Insert);
  1106. term_kD = (char *)find_sequence (K_Delete);
  1107. #elif defined _WIN32
  1108. term_kh = "\033<";
  1109. term_ke = "\033>";
  1110. term_ki = "\033[L";
  1111. #endif /* __MSDOS__ */
  1112. /* Set all the hooks to our PC-specific functions. */
  1113. terminal_begin_inverse_hook = pc_begin_inverse;
  1114. terminal_end_inverse_hook = pc_end_inverse;
  1115. terminal_begin_standout_hook = pc_begin_standout;
  1116. terminal_end_standout_hook = pc_end_standout;
  1117. terminal_begin_underline_hook = pc_begin_underline;
  1118. terminal_end_underline_hook = pc_end_underline;
  1119. terminal_begin_bold_hook = pc_begin_standout;
  1120. terminal_begin_blink_hook = pc_begin_blink;
  1121. terminal_end_all_modes_hook = pc_default_color;
  1122. terminal_default_colour_hook = pc_default_color;
  1123. terminal_set_colour_hook = pc_set_fg_color;
  1124. terminal_set_bgcolour_hook = pc_set_bg_color;
  1125. terminal_prep_terminal_hook = pc_prep_terminal;
  1126. terminal_unprep_terminal_hook = pc_unprep_terminal;
  1127. terminal_up_line_hook = pc_up_line;
  1128. terminal_down_line_hook = pc_down_line;
  1129. terminal_clear_screen_hook = pc_clear_screen;
  1130. terminal_clear_to_eol_hook = pc_clear_to_eol;
  1131. terminal_get_screen_size_hook = pc_get_screen_size;
  1132. terminal_goto_xy_hook = pc_goto_xy;
  1133. terminal_put_text_hook = pc_put_text;
  1134. terminal_ring_bell_hook = pc_ring_bell;
  1135. terminal_write_chars_hook = pc_write_chars;
  1136. terminal_scroll_terminal_hook = pc_scroll_terminal;
  1137. }
  1138. /* **************************************************************** */
  1139. /* */
  1140. /* How to Read Characters From the PC Terminal */
  1141. /* */
  1142. /* **************************************************************** */
  1143. /* This will most certainly work ONLY with DJGPP. */
  1144. #ifdef __DJGPP__
  1145. #include <errno.h>
  1146. #include <sys/fsext.h>
  1147. #include <dpmi.h>
  1148. /* Translation table for some special keys.
  1149. Arrow keys which are standard on other keyboards are translated into
  1150. standard ESC-sequences, in case somebody rebinds the simple keys
  1151. (like C-f, C-b, C-n, etc.).
  1152. The strange "\033\061" prefix in some keys is a numeric argument of
  1153. one, which means ``do the next command once''. It is here so that
  1154. when the according PC key is pressed in the middle of an incremental
  1155. search, Info doesn't see just an ASCII character like `n' or `B',
  1156. and doesn't add it to the search string; instead, it will exit the
  1157. incremental search and then perform the command. */
  1158. static struct
  1159. {
  1160. int inkey;
  1161. unsigned char const * const sequence;
  1162. } DJGPP_keytab[] = { /* these are for moving between nodes... */
  1163. {K_Control_PageDown, "\033\061n"},
  1164. {K_Control_PageUp, "\033\061p"},
  1165. {K_Control_Up, "\033\061u"},
  1166. {K_Control_Down, "\033\061m"},
  1167. {K_Control_Center, "\033\061l"},
  1168. {K_Home, "\033[H"}, /* ...and these are for moving IN a node */
  1169. {K_End, "\033[F"}, /* they're Numeric-Keypad-Keys, so */
  1170. {K_Left, "\033[D"}, /* NUMLOCK should be off !! */
  1171. {K_Right, "\033[C"},
  1172. {K_Down, "\033[B"},
  1173. {K_Up, "\033[A"},
  1174. {K_PageDown, "\033[G"},
  1175. {K_PageUp, "\033[I"},
  1176. {K_Control_Left, "\033b"},
  1177. {K_Control_Right, "\033f"},
  1178. {K_Control_Home, "\033<"},
  1179. {K_Control_End, "\033>"},
  1180. {K_EHome, "\033[H"}, /* these are also for moving IN a node */
  1181. {K_EEnd, "\033[F"}, /* they're the "extended" (Grey) keys */
  1182. {K_ELeft, "\033[D"},
  1183. {K_ERight, "\033[C"},
  1184. {K_EDown, "\033[B"},
  1185. {K_EUp, "\033[A"},
  1186. {K_EPageDown, "\033[G"},
  1187. {K_EPageUp, "\033[I"},
  1188. {K_Control_ELeft, "\033b"},
  1189. {K_Control_ERight, "\033f"},
  1190. {K_Control_EHome, "\033<"},
  1191. {K_Control_EEnd, "\033>"},
  1192. {K_BackTab, "\033\011"},
  1193. {K_F1, "\10"}, /* YEAH, gimme that good old F-one-thing */
  1194. {K_Delete, "\177"}, /* to make Kp-Del be DEL (0x7f) */
  1195. {K_EDelete, "\177"}, /* to make Delete be DEL (0x7f) */
  1196. {K_Insert, "\033[L"},
  1197. {K_EInsert, "\033[L"},
  1198. /* These are here to map more Alt-X keys to ESC X sequences. */
  1199. {K_Alt_Q, "\033q"},
  1200. {K_Alt_W, "\033w"},
  1201. {K_Alt_E, "\033e"},
  1202. {K_Alt_R, "\033r"},
  1203. {K_Alt_T, "\033t"},
  1204. {K_Alt_Y, "\033y"},
  1205. {K_Alt_U, "\033u"},
  1206. {K_Alt_I, "\033i"},
  1207. {K_Alt_O, "\033o"},
  1208. {K_Alt_P, "\033p"},
  1209. {K_Alt_LBracket, "\033["},
  1210. {K_Alt_RBracket, "\033]"},
  1211. {K_Alt_Return, "\033\015"},
  1212. {K_Alt_A, "\033a"},
  1213. {K_Alt_S, "\033s"},
  1214. {K_Alt_D, "\033d"},
  1215. {K_Alt_F, "\033f"},
  1216. {K_Alt_G, "\033g"},
  1217. {K_Alt_H, "\033h"},
  1218. {K_Alt_J, "\033j"},
  1219. {K_Alt_K, "\033k"},
  1220. {K_Alt_L, "\033l"},
  1221. {K_Alt_Semicolon, "\033;"},
  1222. {K_Alt_Quote, "\033'"},
  1223. {K_Alt_Backquote, "\033`"},
  1224. {K_Alt_Backslash, "\033\\"},
  1225. {K_Alt_Z, "\033z"},
  1226. {K_Alt_X, "\033x"},
  1227. {K_Alt_C, "\033c"},
  1228. {K_Alt_V, "\033v"},
  1229. {K_Alt_B, "\033b"},
  1230. {K_Alt_N, "\033n"},
  1231. {K_Alt_M, "\033m"},
  1232. {K_Alt_Comma, "\033<"}, /* our reader cannot distinguish between */
  1233. {K_Alt_Period, "\033>"}, /* Alt-. and Alt->, so we cheat a little */
  1234. {K_Alt_Slash, "\033?"}, /* ditto, to get Alt-? */
  1235. {K_Alt_Backspace, "\033\177"}, /* M-DEL, to delete word backwards */
  1236. {K_Alt_1, "\033\061"},
  1237. {K_Alt_2, "\033\062"},
  1238. {K_Alt_3, "\033\063"},
  1239. {K_Alt_4, "\033\064"},
  1240. {K_Alt_5, "\033\065"},
  1241. {K_Alt_6, "\033\066"},
  1242. {K_Alt_7, "\033\067"},
  1243. {K_Alt_8, "\033\070"},
  1244. {K_Alt_9, "\033\071"},
  1245. {K_Alt_0, "\033\060"},
  1246. {K_Alt_Dash, "\033\055"},
  1247. {K_Alt_EPageUp, "\033\033[I"},
  1248. {K_Alt_EPageDown, "\033\033[G"},
  1249. {K_Alt_Equals, "\033\075"},
  1250. {K_Alt_EDelete, "\033\177"},
  1251. {K_Alt_Tab, "\033\011"},
  1252. {0, 0}
  1253. };
  1254. /* Given a key, return the sequence of characters which
  1255. our keyboard driver generates. */
  1256. static unsigned const char *
  1257. find_sequence (int key)
  1258. {
  1259. int i;
  1260. for (i = 0; DJGPP_keytab[i].inkey; i++)
  1261. if (key == DJGPP_keytab[i].inkey)
  1262. return DJGPP_keytab[i].sequence;
  1263. return NULL;
  1264. }
  1265. /* Return zero if a key is pending in the
  1266. keyboard buffer, non-zero otherwise. */
  1267. static int
  1268. kbd_buffer_empty (void)
  1269. {
  1270. __dpmi_regs r;
  1271. int retval;
  1272. r.h.ah = 0x11; /* Get enhanced keyboard status */
  1273. __dpmi_int (0x16, &r);
  1274. /* If the keyboard buffer is empty, the Zero Flag will be set. */
  1275. return (r.x.flags & 0x40) == 0x40;
  1276. }
  1277. /* The buffered characters pending to be read.
  1278. Actually, Info usually reads a single character, but when we
  1279. translate a key into a sequence of characters, we keep them here. */
  1280. static unsigned char buffered[512];
  1281. /* Index of the next buffered character to be returned. */
  1282. static int buf_idx;
  1283. /* Return the number of characters waiting to be read. */
  1284. long
  1285. pc_term_chars_avail (void)
  1286. {
  1287. if (buf_idx >= sizeof (buffered)) /* paranoia */
  1288. {
  1289. buf_idx = 0;
  1290. buffered[buf_idx] = '\0';
  1291. return 0;
  1292. }
  1293. else
  1294. return strlen (buffered + buf_idx);
  1295. }
  1296. /* Our special terminal keyboard reader. It will be called by
  1297. low-level libc functions when the application calls `read' or
  1298. the ANSI-standard stream-oriented read functions. If the
  1299. caller wants to read the terminal, we redirect the call to
  1300. the BIOS keyboard functions, since that lets us recognize more
  1301. keys than DOS does. */
  1302. static int
  1303. keyboard_read (__FSEXT_Fnumber func, int *retval, va_list rest_args)
  1304. {
  1305. /* When we are called, REST_ARGS are: file_descriptor, buf, nbytes. */
  1306. unsigned char *buf;
  1307. size_t nbytes, nread = 0;
  1308. int fd = va_arg (rest_args, int);
  1309. /* Is this call for us? */
  1310. if (func != __FSEXT_read || !isatty (fd))
  1311. return 0; /* and the usual DOS call will be issued */
  1312. buf = va_arg (rest_args, unsigned char *);
  1313. nbytes = va_arg (rest_args, size_t);
  1314. if (!buf)
  1315. {
  1316. errno = EINVAL;
  1317. *retval = -1;
  1318. return 1;
  1319. }
  1320. if (!nbytes)
  1321. {
  1322. *retval = 0;
  1323. return 1;
  1324. }
  1325. /* Loop here until enough bytes has been read. */
  1326. do
  1327. {
  1328. int key;
  1329. /* If any ``buffered characters'' are left, return as much
  1330. of them as the caller wanted. */
  1331. while (buffered[buf_idx] && nbytes)
  1332. {
  1333. *buf++ = buffered[buf_idx++];
  1334. nread++;
  1335. nbytes--;
  1336. }
  1337. if (nbytes <= 0)
  1338. break;
  1339. /* Wait for another key.
  1340. We do that in a busy-waiting loop so we don't get parked
  1341. inside a BIOS call, which will effectively disable signals.
  1342. While we wait for them to type something, we repeatedly
  1343. release the rest of our time slice, so that other programs
  1344. in a multitasking environment, such as Windows, get more cycles. */
  1345. while (kbd_buffer_empty ())
  1346. __dpmi_yield ();
  1347. key = getxkey ();
  1348. /* Translate the key if necessary.
  1349. Untranslated non-ASCII keys are silently ignored. */
  1350. if ((key & 0x300) != 0)
  1351. {
  1352. unsigned char const * key_sequence = find_sequence (key);
  1353. if (key_sequence != NULL)
  1354. {
  1355. strcpy (buffered, key_sequence);
  1356. buf_idx = 0;
  1357. }
  1358. }
  1359. else if (key == K_Control_Z)
  1360. raise (SIGUSR1); /* we don't have SIGTSTP, so simulate it */
  1361. else if (key <= 0xff)
  1362. {
  1363. *buf++ = key;
  1364. nbytes--;
  1365. nread++;
  1366. }
  1367. }
  1368. while (nbytes > 0);
  1369. *retval = nread;
  1370. return 1; /* meaning that we handled the call */
  1371. }
  1372. /* Install our keyboard handler.
  1373. This is called by the startup code before `main'. */
  1374. static void __attribute__((constructor))
  1375. install_keyboard_handler (void)
  1376. {
  1377. __FSEXT_set_function (fileno (stdin), keyboard_read);
  1378. /* We need to set this single hook here; the rest
  1379. will be set by pc_initialize_terminal when it is called. */
  1380. terminal_initialize_terminal_hook = pc_initialize_terminal;
  1381. }
  1382. #endif /* __DJGPP__ */
  1383. /* **************************************************************** */
  1384. /* */
  1385. /* Emulation of SIGTSTP on Ctrl-Z */
  1386. /* */
  1387. /* **************************************************************** */
  1388. #include <limits.h>
  1389. #include "signals.h"
  1390. #ifndef PATH_MAX
  1391. # define PATH_MAX 512
  1392. #endif
  1393. /* Effectively disable signals which aren't defined
  1394. (assuming no signal can ever be zero).
  1395. SIGINT is ANSI, so we expect it to be always defined. */
  1396. #ifndef SIGUSR1
  1397. # define SIGUSR1 0
  1398. #endif
  1399. #ifndef SIGQUIT
  1400. # define SIGQUIT 0
  1401. #endif
  1402. int
  1403. kill (pid_t pid, int sig)
  1404. {
  1405. static char interrupted_msg[] = "Interrupted\r\n";
  1406. static char stopped_msg[] = "Stopped. Type 'exit RET' to return.\r\n";
  1407. char cwd[PATH_MAX + 1];
  1408. if (pid == getpid ()
  1409. || pid == 0
  1410. || pid == -1
  1411. || pid == -getpid ())
  1412. {
  1413. switch (sig)
  1414. {
  1415. RETSIGTYPE (*old_INT)(int), (*old_QUIT)(int);
  1416. case SIGINT:
  1417. #ifdef __DJGPP__
  1418. /* If SIGINT was generated by a readable key, we want to remove
  1419. it from the PC keyboard buffer, so that DOS and other
  1420. programs never see it. DJGPP signal-handling mechanism
  1421. doesn't remove the INT key from the keyboard buffer. */
  1422. if (!kbd_buffer_empty ())
  1423. getxkey ();
  1424. #endif
  1425. pc_write_chars (interrupted_msg, sizeof (interrupted_msg) - 1);
  1426. exit (EXIT_FAILURE);
  1427. case SIGUSR1:
  1428. /* Simulate SIGTSTP by invoking a subsidiary shell. */
  1429. #ifndef _WIN32
  1430. pc_goto_xy (0, outside_info.screenheight - 1);
  1431. pc_clear_to_eol ();
  1432. pc_write_chars (stopped_msg, sizeof (stopped_msg) - 1);
  1433. #endif
  1434. /* The child shell can change the working directory, so
  1435. we need to save and restore it, since it is global. */
  1436. if (!getcwd (cwd, PATH_MAX)) /* should never happen */
  1437. cwd[0] = '\0';
  1438. /* We don't want to get fatal signals while the subshell runs. */
  1439. old_INT = signal (SIGINT, SIG_IGN);
  1440. old_QUIT = signal (SIGQUIT, SIG_IGN);
  1441. #ifdef _WIN32
  1442. {
  1443. const char *argv[2];
  1444. const char *shell = NULL;
  1445. argv[0] = NULL;
  1446. shell = getenv ("SHELL");
  1447. if (!shell)
  1448. {
  1449. shell = getenv ("COMSPEC");
  1450. if (!shell)
  1451. return -1;
  1452. argv[0] = " /k";
  1453. }
  1454. argv[1] = NULL;
  1455. _spawnvp (_P_WAIT, shell, argv);
  1456. }
  1457. #else
  1458. system ("");
  1459. #endif
  1460. if (*cwd)
  1461. chdir (cwd);
  1462. signal (SIGINT, old_INT);
  1463. signal (SIGQUIT, old_QUIT);
  1464. break;
  1465. default:
  1466. if (sig)
  1467. raise (sig);
  1468. break;
  1469. }
  1470. return 0;
  1471. }
  1472. else
  1473. return -1;
  1474. }
  1475. /* These should never be called, but they make the linker happy. */
  1476. int tputs (const char *a, int b, int (*c)(int))
  1477. {
  1478. perror ("tputs"); return 0; /* here and below, added dummy retvals */
  1479. }
  1480. char* tgoto (const char *a, int b, int c)
  1481. {
  1482. perror ("tgoto"); return 0;
  1483. }
  1484. int tgetnum (char*a)
  1485. {
  1486. perror ("tgetnum"); return 0;
  1487. }
  1488. int tgetflag (char*a)
  1489. {
  1490. perror ("tgetflag"); return 0;
  1491. }
  1492. char* tgetstr (char *a, char **b)
  1493. {
  1494. perror ("tgetstr"); return 0;
  1495. }
  1496. int tgetent (char *a, const char *b)
  1497. {
  1498. perror ("tgetent"); return 0;
  1499. }
  1500. int tcgetattr(int fildes, struct termios *termios_p)
  1501. {
  1502. perror ("tcgetattr"); return 0;
  1503. }
  1504. int tcsetattr(int fd, int opt_actions, const struct termios *termios_p)
  1505. {
  1506. perror ("tcsetattr"); return 0;
  1507. }