/contrib/tcsh/ed.init.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 708 lines · 509 code · 98 blank · 101 comment · 118 complexity · e9daea368ac84c74dbc1de106351d4bd MD5 · raw file

  1. /* $Header: /p/tcsh/cvsroot/tcsh/ed.init.c,v 3.60 2006/08/24 20:56:31 christos Exp $ */
  2. /*
  3. * ed.init.c: Editor initializations
  4. */
  5. /*-
  6. * Copyright (c) 1980, 1991 The Regents of the University of California.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTS_ION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "sh.h"
  34. RCSID("$tcsh: ed.init.c,v 3.60 2006/08/24 20:56:31 christos Exp $")
  35. #include "ed.h"
  36. #include "tc.h"
  37. #include "ed.defns.h"
  38. /* ed.init.c -- init routines for the line editor */
  39. /* #define DEBUG_TTY */
  40. int Tty_raw_mode = 0; /* Last tty change was to raw mode */
  41. int MacroLvl = -1; /* pointer to current macro nesting level; */
  42. /* (-1 == none) */
  43. static int Tty_quote_mode = 0; /* Last tty change was to quote mode */
  44. static unsigned char vdisable; /* The value of _POSIX_VDISABLE from
  45. * pathconf(2) */
  46. int Tty_eight_bit = -1; /* does the tty handle eight bits */
  47. extern int GotTermCaps;
  48. static ttydata_t extty, edtty, tstty;
  49. #define qutty tstty
  50. #define SHTTY (insource ? OLDSTD : SHIN)
  51. #define uc unsigned char
  52. static unsigned char ttychars[NN_IO][C_NCC] = {
  53. {
  54. (uc)CINTR, (uc)CQUIT, (uc)CERASE, (uc)CKILL,
  55. (uc)CEOF, (uc)CEOL, (uc)CEOL2, (uc)CSWTCH,
  56. (uc)CDSWTCH, (uc)CERASE2, (uc)CSTART, (uc)CSTOP,
  57. (uc)CWERASE, (uc)CSUSP, (uc)CDSUSP, (uc)CREPRINT,
  58. (uc)CDISCARD, (uc)CLNEXT, (uc)CSTATUS, (uc)CPAGE,
  59. (uc)CPGOFF, (uc)CKILL2, (uc)CBRK, (uc)CMIN,
  60. (uc)CTIME
  61. },
  62. {
  63. CINTR, CQUIT, CERASE, CKILL,
  64. _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE,
  65. _POSIX_VDISABLE, CERASE2, CSTART, CSTOP,
  66. _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE,
  67. CDISCARD, _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE,
  68. _POSIX_VDISABLE, _POSIX_VDISABLE, _POSIX_VDISABLE, 1,
  69. 0
  70. },
  71. {
  72. 0, 0, 0, 0,
  73. 0, 0, 0, 0,
  74. 0, 0, 0, 0,
  75. 0, 0, 0, 0,
  76. 0, 0, 0, 0,
  77. 0, 0, 0, 0,
  78. 0
  79. }
  80. };
  81. #ifdef SIG_WINDOW
  82. void
  83. check_window_size(int force)
  84. {
  85. int lins, cols;
  86. /* don't want to confuse things here */
  87. pintr_disabled++;
  88. cleanup_push(&pintr_disabled, disabled_cleanup);
  89. /*
  90. * From: bret@shark.agps.lanl.gov (Bret Thaeler) Avoid sunview bug, where a
  91. * partially hidden window gets a SIG_WINDOW every time the text is
  92. * scrolled
  93. */
  94. if (GetSize(&lins, &cols) || force) {
  95. if (GettingInput) {
  96. ClearLines();
  97. ClearDisp();
  98. MoveToLine(0);
  99. MoveToChar(0);
  100. ChangeSize(lins, cols);
  101. Refresh();
  102. }
  103. else
  104. ChangeSize(lins, cols);
  105. }
  106. windowchg = 0;
  107. cleanup_until(&pintr_disabled); /* can change it again */
  108. }
  109. void
  110. /*ARGSUSED*/
  111. window_change(int snum)
  112. {
  113. USE(snum);
  114. windowchg = 1;
  115. }
  116. #endif /* SIG_WINDOW */
  117. void
  118. ed_set_tty_eight_bit(void)
  119. {
  120. if (tty_getty(SHTTY, &extty) == -1) {
  121. #ifdef DEBUG_TTY
  122. xprintf("ed_set_tty_eight_bit: tty_getty: %s\n", strerror(errno));
  123. #endif /* DEBUG_TTY */
  124. return;
  125. }
  126. Tty_eight_bit = tty_geteightbit(&extty);
  127. }
  128. int
  129. ed_Setup(int rst)
  130. {
  131. static int havesetup = 0;
  132. struct varent *imode;
  133. if (havesetup) /* if we have never been called */
  134. return(0);
  135. #if defined(POSIX) && defined(_PC_VDISABLE) && !defined(BSD4_4) && \
  136. !defined(WINNT_NATIVE)
  137. {
  138. long pcret;
  139. if ((pcret = fpathconf(SHTTY, _PC_VDISABLE)) == -1L)
  140. vdisable = (unsigned char) _POSIX_VDISABLE;
  141. else
  142. vdisable = (unsigned char) pcret;
  143. if (vdisable != (unsigned char) _POSIX_VDISABLE && rst != 0)
  144. for (rst = 0; rst < C_NCC; rst++) {
  145. if (ttychars[ED_IO][rst] == (unsigned char) _POSIX_VDISABLE)
  146. ttychars[ED_IO][rst] = vdisable;
  147. if (ttychars[EX_IO][rst] == (unsigned char) _POSIX_VDISABLE)
  148. ttychars[EX_IO][rst] = vdisable;
  149. }
  150. }
  151. #else /* ! POSIX || !_PC_VDISABLE || BSD4_4 || WINNT_NATIVE */
  152. vdisable = (unsigned char) _POSIX_VDISABLE;
  153. #endif /* POSIX && _PC_VDISABLE && !BSD4_4 && !WINNT_NATIVE */
  154. if ((imode = adrof(STRinputmode)) != NULL && imode->vec != NULL) {
  155. if (!Strcmp(*(imode->vec), STRinsert))
  156. inputmode = MODE_INSERT;
  157. else if (!Strcmp(*(imode->vec), STRoverwrite))
  158. inputmode = MODE_REPLACE;
  159. }
  160. else
  161. inputmode = MODE_INSERT;
  162. ed_InitMaps();
  163. Hist_num = 0;
  164. Expand = 0;
  165. SetKillRing(getn(varval(STRkillring)));
  166. #ifndef WINNT_NATIVE
  167. if (tty_getty(SHTTY, &extty) == -1) {
  168. # ifdef DEBUG_TTY
  169. xprintf("ed_Setup: tty_getty: %s\n", strerror(errno));
  170. # endif /* DEBUG_TTY */
  171. return(-1);
  172. }
  173. tstty = edtty = extty;
  174. T_Speed = tty_getspeed(&extty);
  175. T_Tabs = tty_gettabs(&extty);
  176. Tty_eight_bit = tty_geteightbit(&extty);
  177. # if defined(POSIX) || defined(TERMIO)
  178. extty.d_t.c_iflag &= ~ttylist[EX_IO][M_INPUT].t_clrmask;
  179. extty.d_t.c_iflag |= ttylist[EX_IO][M_INPUT].t_setmask;
  180. extty.d_t.c_oflag &= ~ttylist[EX_IO][M_OUTPUT].t_clrmask;
  181. extty.d_t.c_oflag |= ttylist[EX_IO][M_OUTPUT].t_setmask;
  182. extty.d_t.c_cflag &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  183. extty.d_t.c_cflag |= ttylist[EX_IO][M_CONTROL].t_setmask;
  184. extty.d_t.c_lflag &= ~ttylist[EX_IO][M_LINED].t_clrmask;
  185. extty.d_t.c_lflag |= ttylist[EX_IO][M_LINED].t_setmask;
  186. # if defined(IRIX3_3) && SYSVREL < 4
  187. extty.d_t.c_line = NTTYDISC;
  188. # endif /* IRIX3_3 && SYSVREL < 4 */
  189. # else /* GSTTY */ /* V7, Berkeley style tty */
  190. if (T_Tabs) { /* order of &= and |= is important to XTABS */
  191. extty.d_t.sg_flags &= ~(ttylist[EX_IO][M_CONTROL].t_clrmask|XTABS);
  192. extty.d_t.sg_flags |= ttylist[EX_IO][M_CONTROL].t_setmask;
  193. }
  194. else {
  195. extty.d_t.sg_flags &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  196. extty.d_t.sg_flags |= (ttylist[EX_IO][M_CONTROL].t_setmask|XTABS);
  197. }
  198. extty.d_lb &= ~ttylist[EX_IO][M_LOCAL].t_clrmask;
  199. extty.d_lb |= ttylist[EX_IO][M_LOCAL].t_setmask;
  200. # endif /* GSTTY */
  201. /*
  202. * Reset the tty chars to reasonable defaults
  203. * If they are disabled, then enable them.
  204. */
  205. if (rst) {
  206. if (tty_cooked_mode(&tstty)) {
  207. tty_getchar(&tstty, ttychars[TS_IO]);
  208. /*
  209. * Don't affect CMIN and CTIME for the editor mode
  210. */
  211. for (rst = 0; rst < C_NCC - 2; rst++)
  212. if (ttychars[TS_IO][rst] != vdisable &&
  213. ttychars[ED_IO][rst] != vdisable)
  214. ttychars[ED_IO][rst] = ttychars[TS_IO][rst];
  215. for (rst = 0; rst < C_NCC; rst++)
  216. if (ttychars[TS_IO][rst] != vdisable &&
  217. ttychars[EX_IO][rst] != vdisable)
  218. ttychars[EX_IO][rst] = ttychars[TS_IO][rst];
  219. }
  220. tty_setchar(&extty, ttychars[EX_IO]);
  221. if (tty_setty(SHTTY, &extty) == -1) {
  222. # ifdef DEBUG_TTY
  223. xprintf("ed_Setup: tty_setty: %s\n", strerror(errno));
  224. # endif /* DEBUG_TTY */
  225. return(-1);
  226. }
  227. }
  228. else
  229. tty_setchar(&extty, ttychars[EX_IO]);
  230. # ifdef SIG_WINDOW
  231. {
  232. sigset_t set;
  233. (void)signal(SIG_WINDOW, window_change); /* for window systems */
  234. sigemptyset(&set);
  235. sigaddset(&set, SIG_WINDOW);
  236. (void)sigprocmask(SIG_UNBLOCK, &set, NULL);
  237. }
  238. # endif
  239. #else /* WINNT_NATIVE */
  240. # ifdef DEBUG
  241. if (rst)
  242. xprintf("rst received in ed_Setup() %d\n", rst);
  243. # endif
  244. #endif /* WINNT_NATIVE */
  245. havesetup = 1;
  246. return(0);
  247. }
  248. void
  249. ed_Init(void)
  250. {
  251. ResetInLine(1); /* reset the input pointers */
  252. GettingInput = 0; /* just in case */
  253. #ifdef notdef
  254. /* XXX This code was here before the kill ring:
  255. LastKill = KillBuf; / * no kill buffer * /
  256. If there was any reason for that other than to make sure LastKill
  257. was initialized, the code below should go in here instead - but
  258. it doesn't seem reasonable to lose the entire kill ring (which is
  259. "self-initializing") just because you set $term or whatever, so
  260. presumably this whole '#ifdef notdef' should just be taken out. */
  261. { /* no kill ring - why? */
  262. int i;
  263. for (i = 0; i < KillRingMax; i++) {
  264. xfree(KillRing[i].buf);
  265. KillRing[i].buf = NULL;
  266. KillRing[i].len = 0;
  267. }
  268. YankPos = KillPos = 0;
  269. KillRingLen = 0;
  270. }
  271. #endif
  272. #ifdef DEBUG_EDIT
  273. CheckMaps(); /* do a little error checking on key maps */
  274. #endif
  275. if (ed_Setup(0) == -1)
  276. return;
  277. /*
  278. * if we have been called before but GotTermCaps isn't set, our TERM has
  279. * changed, so get new termcaps and try again
  280. */
  281. if (!GotTermCaps)
  282. GetTermCaps(); /* does the obvious, but gets term type each
  283. * time */
  284. #ifndef WINNT_NATIVE
  285. # if defined(TERMIO) || defined(POSIX)
  286. edtty.d_t.c_iflag &= ~ttylist[ED_IO][M_INPUT].t_clrmask;
  287. edtty.d_t.c_iflag |= ttylist[ED_IO][M_INPUT].t_setmask;
  288. edtty.d_t.c_oflag &= ~ttylist[ED_IO][M_OUTPUT].t_clrmask;
  289. edtty.d_t.c_oflag |= ttylist[ED_IO][M_OUTPUT].t_setmask;
  290. edtty.d_t.c_cflag &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  291. edtty.d_t.c_cflag |= ttylist[ED_IO][M_CONTROL].t_setmask;
  292. edtty.d_t.c_lflag &= ~ttylist[ED_IO][M_LINED].t_clrmask;
  293. edtty.d_t.c_lflag |= ttylist[ED_IO][M_LINED].t_setmask;
  294. # if defined(IRIX3_3) && SYSVREL < 4
  295. edtty.d_t.c_line = NTTYDISC;
  296. # endif /* IRIX3_3 && SYSVREL < 4 */
  297. # else /* GSTTY */
  298. if (T_Tabs) { /* order of &= and |= is important to XTABS */
  299. edtty.d_t.sg_flags &= ~(ttylist[ED_IO][M_CONTROL].t_clrmask | XTABS);
  300. edtty.d_t.sg_flags |= ttylist[ED_IO][M_CONTROL].t_setmask;
  301. }
  302. else {
  303. edtty.d_t.sg_flags &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  304. edtty.d_t.sg_flags |= (ttylist[ED_IO][M_CONTROL].t_setmask | XTABS);
  305. }
  306. edtty.d_lb &= ~ttylist[ED_IO][M_LOCAL].t_clrmask;
  307. edtty.d_lb |= ttylist[ED_IO][M_LOCAL].t_setmask;
  308. # endif /* POSIX || TERMIO */
  309. tty_setchar(&edtty, ttychars[ED_IO]);
  310. #endif /* WINNT_NATIVE */
  311. }
  312. /*
  313. * Check and re-init the line. set the terminal into 1 char at a time mode.
  314. */
  315. int
  316. Rawmode(void)
  317. {
  318. if (Tty_raw_mode)
  319. return (0);
  320. #ifdef WINNT_NATIVE
  321. do_nt_raw_mode();
  322. #else /* !WINNT_NATIVE */
  323. # ifdef _IBMR2
  324. tty_setdisc(SHTTY, ED_IO);
  325. # endif /* _IBMR2 */
  326. if (tty_getty(SHTTY, &tstty) == -1) {
  327. # ifdef DEBUG_TTY
  328. xprintf("Rawmode: tty_getty: %s\n", strerror(errno));
  329. # endif /* DEBUG_TTY */
  330. return(-1);
  331. }
  332. /*
  333. * We always keep up with the eight bit setting and the speed of the
  334. * tty. But only we only believe changes that are made to cooked mode!
  335. */
  336. # if defined(POSIX) || defined(TERMIO)
  337. Tty_eight_bit = tty_geteightbit(&tstty);
  338. T_Speed = tty_getspeed(&tstty);
  339. # ifdef POSIX
  340. /*
  341. * Fix from: Steven (Steve) B. Green <xrsbg@charney.gsfc.nasa.gov>
  342. * Speed was not being set up correctly under POSIX.
  343. */
  344. if (tty_getspeed(&extty) != T_Speed || tty_getspeed(&edtty) != T_Speed) {
  345. (void) cfsetispeed(&extty.d_t, T_Speed);
  346. (void) cfsetospeed(&extty.d_t, T_Speed);
  347. (void) cfsetispeed(&edtty.d_t, T_Speed);
  348. (void) cfsetospeed(&edtty.d_t, T_Speed);
  349. }
  350. # endif /* POSIX */
  351. # else /* GSTTY */
  352. T_Speed = tty_getspeed(&tstty);
  353. Tty_eight_bit = tty_geteightbit(&tstty);
  354. if (extty.d_t.sg_ispeed != tstty.d_t.sg_ispeed) {
  355. extty.d_t.sg_ispeed = tstty.d_t.sg_ispeed;
  356. edtty.d_t.sg_ispeed = tstty.d_t.sg_ispeed;
  357. }
  358. if (extty.d_t.sg_ospeed != tstty.d_t.sg_ospeed) {
  359. extty.d_t.sg_ospeed = tstty.d_t.sg_ospeed;
  360. edtty.d_t.sg_ospeed = tstty.d_t.sg_ospeed;
  361. }
  362. # endif /* POSIX || TERMIO */
  363. if (tty_cooked_mode(&tstty)) {
  364. /*
  365. * re-test for some things here (like maybe the user typed
  366. * "stty -tabs"
  367. */
  368. if (tty_gettabs(&tstty) == 0)
  369. T_Tabs = 0;
  370. else
  371. T_Tabs = CanWeTab();
  372. # if defined(POSIX) || defined(TERMIO)
  373. extty.d_t.c_cflag = tstty.d_t.c_cflag;
  374. extty.d_t.c_cflag &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  375. extty.d_t.c_cflag |= ttylist[EX_IO][M_CONTROL].t_setmask;
  376. edtty.d_t.c_cflag = tstty.d_t.c_cflag;
  377. edtty.d_t.c_cflag &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  378. edtty.d_t.c_cflag |= ttylist[ED_IO][M_CONTROL].t_setmask;
  379. extty.d_t.c_lflag = tstty.d_t.c_lflag;
  380. extty.d_t.c_lflag &= ~ttylist[EX_IO][M_LINED].t_clrmask;
  381. extty.d_t.c_lflag |= ttylist[EX_IO][M_LINED].t_setmask;
  382. edtty.d_t.c_lflag = tstty.d_t.c_lflag;
  383. edtty.d_t.c_lflag &= ~ttylist[ED_IO][M_LINED].t_clrmask;
  384. edtty.d_t.c_lflag |= ttylist[ED_IO][M_LINED].t_setmask;
  385. extty.d_t.c_iflag = tstty.d_t.c_iflag;
  386. extty.d_t.c_iflag &= ~ttylist[EX_IO][M_INPUT].t_clrmask;
  387. extty.d_t.c_iflag |= ttylist[EX_IO][M_INPUT].t_setmask;
  388. edtty.d_t.c_iflag = tstty.d_t.c_iflag;
  389. edtty.d_t.c_iflag &= ~ttylist[ED_IO][M_INPUT].t_clrmask;
  390. edtty.d_t.c_iflag |= ttylist[ED_IO][M_INPUT].t_setmask;
  391. extty.d_t.c_oflag = tstty.d_t.c_oflag;
  392. extty.d_t.c_oflag &= ~ttylist[EX_IO][M_OUTPUT].t_clrmask;
  393. extty.d_t.c_oflag |= ttylist[EX_IO][M_OUTPUT].t_setmask;
  394. edtty.d_t.c_oflag = tstty.d_t.c_oflag;
  395. edtty.d_t.c_oflag &= ~ttylist[ED_IO][M_OUTPUT].t_clrmask;
  396. edtty.d_t.c_oflag |= ttylist[ED_IO][M_OUTPUT].t_setmask;
  397. # else /* GSTTY */
  398. extty.d_t.sg_flags = tstty.d_t.sg_flags;
  399. extty.d_t.sg_flags &= ~ttylist[EX_IO][M_CONTROL].t_clrmask;
  400. extty.d_t.sg_flags |= ttylist[EX_IO][M_CONTROL].t_setmask;
  401. if (T_Tabs) /* order of &= and |= is important to XTABS */
  402. extty.d_t.sg_flags &= ~XTABS;
  403. else
  404. extty.d_t.sg_flags |= XTABS;
  405. extty.d_lb = tstty.d_lb;
  406. extty.d_lb &= ~ttylist[EX_IO][M_LOCAL].t_clrmask;
  407. extty.d_lb |= ttylist[EX_IO][M_LOCAL].t_setmask;
  408. edtty.d_t.sg_flags = extty.d_t.sg_flags;
  409. if (T_Tabs) { /* order of &= and |= is important to XTABS */
  410. edtty.d_t.sg_flags &=
  411. ~(ttylist[ED_IO][M_CONTROL].t_clrmask|XTABS);
  412. edtty.d_t.sg_flags |= ttylist[ED_IO][M_CONTROL].t_setmask;
  413. }
  414. else {
  415. edtty.d_t.sg_flags &= ~ttylist[ED_IO][M_CONTROL].t_clrmask;
  416. edtty.d_t.sg_flags |=
  417. (ttylist[ED_IO][M_CONTROL].t_setmask|XTABS);
  418. }
  419. edtty.d_lb = tstty.d_lb;
  420. edtty.d_lb &= ~ttylist[ED_IO][M_LOCAL].t_clrmask;
  421. edtty.d_lb |= ttylist[ED_IO][M_LOCAL].t_setmask;
  422. # endif /* TERMIO || POSIX */
  423. {
  424. int i;
  425. tty_getchar(&tstty, ttychars[TS_IO]);
  426. /*
  427. * Check if the user made any changes.
  428. * If he did, then propagate the changes to the
  429. * edit and execute data structures.
  430. */
  431. for (i = 0; i < C_NCC; i++)
  432. if (ttychars[TS_IO][i] != ttychars[EX_IO][i])
  433. break;
  434. if (i != C_NCC || didsetty) {
  435. didsetty = 0;
  436. /*
  437. * Propagate changes only to the unprotected chars
  438. * that have been modified just now.
  439. */
  440. for (i = 0; i < C_NCC; i++) {
  441. if (!((ttylist[ED_IO][M_CHAR].t_setmask & C_SH(i))) &&
  442. (ttychars[TS_IO][i] != ttychars[EX_IO][i]))
  443. ttychars[ED_IO][i] = ttychars[TS_IO][i];
  444. if (ttylist[ED_IO][M_CHAR].t_clrmask & C_SH(i))
  445. ttychars[ED_IO][i] = vdisable;
  446. }
  447. tty_setchar(&edtty, ttychars[ED_IO]);
  448. for (i = 0; i < C_NCC; i++) {
  449. if (!((ttylist[EX_IO][M_CHAR].t_setmask & C_SH(i))) &&
  450. (ttychars[TS_IO][i] != ttychars[EX_IO][i]))
  451. ttychars[EX_IO][i] = ttychars[TS_IO][i];
  452. if (ttylist[EX_IO][M_CHAR].t_clrmask & C_SH(i))
  453. ttychars[EX_IO][i] = vdisable;
  454. }
  455. tty_setchar(&extty, ttychars[EX_IO]);
  456. }
  457. }
  458. }
  459. if (tty_setty(SHTTY, &edtty) == -1) {
  460. # ifdef DEBUG_TTY
  461. xprintf("Rawmode: tty_setty: %s\n", strerror(errno));
  462. # endif /* DEBUG_TTY */
  463. return(-1);
  464. }
  465. #endif /* WINNT_NATIVE */
  466. Tty_raw_mode = 1;
  467. flush(); /* flush any buffered output */
  468. return (0);
  469. }
  470. int
  471. Cookedmode(void)
  472. { /* set tty in normal setup */
  473. #ifdef WINNT_NATIVE
  474. do_nt_cooked_mode();
  475. #else
  476. sigset_t set, oset;
  477. int res;
  478. # ifdef _IBMR2
  479. tty_setdisc(SHTTY, EX_IO);
  480. # endif /* _IBMR2 */
  481. if (!Tty_raw_mode)
  482. return (0);
  483. /* hold this for reseting tty */
  484. sigemptyset(&set);
  485. sigaddset(&set, SIGINT);
  486. (void)sigprocmask(SIG_BLOCK, &set, &oset);
  487. cleanup_push(&oset, sigprocmask_cleanup);
  488. res = tty_setty(SHTTY, &extty);
  489. cleanup_until(&oset);
  490. if (res == -1) {
  491. # ifdef DEBUG_TTY
  492. xprintf("Cookedmode: tty_setty: %s\n", strerror(errno));
  493. # endif /* DEBUG_TTY */
  494. return -1;
  495. }
  496. #endif /* WINNT_NATIVE */
  497. Tty_raw_mode = 0;
  498. return (0);
  499. }
  500. void
  501. ResetInLine(int macro)
  502. {
  503. Cursor = InputBuf; /* reset cursor */
  504. LastChar = InputBuf;
  505. InputLim = &InputBuf[INBUFSIZE - 2];/*FIXBUF*/
  506. Mark = InputBuf;
  507. MarkIsSet = 0;
  508. MetaNext = 0;
  509. CurrentKeyMap = CcKeyMap;
  510. AltKeyMap = 0;
  511. Hist_num = 0;
  512. DoingArg = 0;
  513. Argument = 1;
  514. LastCmd = F_UNASSIGNED; /* previous command executed */
  515. IncMatchLen = 0;
  516. if (macro)
  517. MacroLvl = -1; /* no currently active macros */
  518. }
  519. int
  520. Load_input_line(void)
  521. {
  522. static Char *Input_Line = NULL;
  523. #ifdef SUNOS4
  524. long chrs = 0;
  525. #else /* !SUNOS4 */
  526. /*
  527. * *Everyone* else has an int, but SunOS wants long!
  528. * This breaks where int != long (alpha)
  529. */
  530. int chrs = 0;
  531. #endif /* SUNOS4 */
  532. if (Input_Line)
  533. xfree(Input_Line);
  534. Input_Line = NULL;
  535. if (Tty_raw_mode)
  536. return 0;
  537. #if defined(FIONREAD) && !defined(OREO)
  538. (void) ioctl(SHIN, FIONREAD, (ioctl_t) &chrs);
  539. if (chrs > 0) {
  540. char buf[BUFSIZE];
  541. chrs = xread(SHIN, buf, min(chrs, BUFSIZE - 1));
  542. if (chrs > 0) {
  543. buf[chrs] = '\0';
  544. Input_Line = Strsave(str2short(buf));
  545. PushMacro(Input_Line);
  546. }
  547. #ifdef convex
  548. /* need to print errno message in case file is migrated */
  549. if (chrs < 0)
  550. stderror(ERR_SYSTEM, progname, strerror(errno));
  551. #endif
  552. }
  553. #endif /* FIONREAD && !OREO */
  554. return chrs > 0;
  555. }
  556. /*
  557. * Bugfix (in Swedish) by:
  558. * Johan Widen
  559. * SICS, PO Box 1263, S-163 13 SPANGA, SWEDEN
  560. * {mcvax,munnari,cernvax,diku,inria,prlb2,penet,ukc,unido}!enea!sics.se!jw
  561. * Internet: jw@sics.se
  562. *
  563. * (via Hans J Albertsson (thanks))
  564. */
  565. void
  566. QuoteModeOn(void)
  567. {
  568. if (MacroLvl >= 0)
  569. return;
  570. #ifndef WINNT_NATIVE
  571. qutty = edtty;
  572. #if defined(TERMIO) || defined(POSIX)
  573. qutty.d_t.c_iflag &= ~ttylist[QU_IO][M_INPUT].t_clrmask;
  574. qutty.d_t.c_iflag |= ttylist[QU_IO][M_INPUT].t_setmask;
  575. qutty.d_t.c_oflag &= ~ttylist[QU_IO][M_OUTPUT].t_clrmask;
  576. qutty.d_t.c_oflag |= ttylist[QU_IO][M_OUTPUT].t_setmask;
  577. qutty.d_t.c_cflag &= ~ttylist[QU_IO][M_CONTROL].t_clrmask;
  578. qutty.d_t.c_cflag |= ttylist[QU_IO][M_CONTROL].t_setmask;
  579. qutty.d_t.c_lflag &= ~ttylist[QU_IO][M_LINED].t_clrmask;
  580. qutty.d_t.c_lflag |= ttylist[QU_IO][M_LINED].t_setmask;
  581. #else /* GSTTY */
  582. qutty.d_t.sg_flags &= ~ttylist[QU_IO][M_CONTROL].t_clrmask;
  583. qutty.d_t.sg_flags |= ttylist[QU_IO][M_CONTROL].t_setmask;
  584. qutty.d_lb &= ~ttylist[QU_IO][M_LOCAL].t_clrmask;
  585. qutty.d_lb |= ttylist[QU_IO][M_LOCAL].t_setmask;
  586. #endif /* TERMIO || POSIX */
  587. if (tty_setty(SHTTY, &qutty) == -1) {
  588. #ifdef DEBUG_TTY
  589. xprintf("QuoteModeOn: tty_setty: %s\n", strerror(errno));
  590. #endif /* DEBUG_TTY */
  591. return;
  592. }
  593. #endif /* !WINNT_NATIVE */
  594. Tty_quote_mode = 1;
  595. return;
  596. }
  597. void
  598. QuoteModeOff(void)
  599. {
  600. if (!Tty_quote_mode)
  601. return;
  602. Tty_quote_mode = 0;
  603. if (tty_setty(SHTTY, &edtty) == -1) {
  604. #ifdef DEBUG_TTY
  605. xprintf("QuoteModeOff: tty_setty: %s\n", strerror(errno));
  606. #endif /* DEBUG_TTY */
  607. return;
  608. }
  609. return;
  610. }