hack.termcap.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* $NetBSD: hack.termcap.c,v 1.12 2003/04/02 18:36:40 jsm Exp $ */
  2. /* For Linux: still using old termcap interface from version 1.9. */
  3. /*
  4. * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
  5. * Amsterdam
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are
  10. * met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * - Neither the name of the Stichting Centrum voor Wiskunde en
  20. * Informatica, nor the names of its contributors may be used to endorse or
  21. * promote products derived from this software without specific prior
  22. * written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  25. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  26. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  27. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  28. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  29. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  30. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /*
  37. * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
  38. * All rights reserved.
  39. *
  40. * Redistribution and use in source and binary forms, with or without
  41. * modification, are permitted provided that the following conditions
  42. * are met:
  43. * 1. Redistributions of source code must retain the above copyright
  44. * notice, this list of conditions and the following disclaimer.
  45. * 2. Redistributions in binary form must reproduce the above copyright
  46. * notice, this list of conditions and the following disclaimer in the
  47. * documentation and/or other materials provided with the distribution.
  48. * 3. The name of the author may not be used to endorse or promote products
  49. * derived from this software without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  52. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  53. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  54. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  55. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  56. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  57. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  58. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  59. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  60. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. */
  62. #include <sys/cdefs.h>
  63. #ifndef lint
  64. __RCSID("$NetBSD: hack.termcap.c,v 1.12 2003/04/02 18:36:40 jsm Exp $");
  65. #endif /* not lint */
  66. #include <string.h>
  67. #include <termios.h>
  68. #include <termcap.h>
  69. #include <stdlib.h>
  70. #include <unistd.h>
  71. #include "hack.h"
  72. #include "extern.h"
  73. #include "def.flag.h" /* for flags.nonull */
  74. static char tbuf[512];
  75. char *HO, *CL, *CE, *UP, *CM, *ND, *XD, *BC, *SO, *SE, *TI, *TE;
  76. static char *VS, *VE;
  77. static int SG;
  78. char PC = '\0';
  79. char *CD; /* tested in pri.c: docorner() */
  80. int CO, LI; /* used in pri.c and whatis.c */
  81. void
  82. startup()
  83. {
  84. char *term;
  85. char *tptr;
  86. char *tbufptr, *pc;
  87. tptr = (char *) alloc(1024);
  88. tbufptr = tbuf;
  89. if (!(term = getenv("TERM")))
  90. error("Can't get TERM.");
  91. if (!strncmp(term, "5620", 4))
  92. flags.nonull = 1; /* this should be a termcap flag */
  93. if (tgetent(tptr, term) < 1)
  94. error("Unknown terminal type: %s.", term);
  95. if ((pc = tgetstr("pc", &tbufptr)) != NULL)
  96. PC = *pc;
  97. if (!(BC = tgetstr("bc", &tbufptr))) {
  98. if (!tgetflag("bs"))
  99. error("Terminal must backspace.");
  100. BC = tbufptr;
  101. tbufptr += 2;
  102. *BC = '\b';
  103. }
  104. HO = tgetstr("ho", &tbufptr);
  105. CO = tgetnum("co");
  106. LI = tgetnum("li");
  107. if (CO < COLNO || LI < ROWNO + 2)
  108. setclipped();
  109. if (!(CL = tgetstr("cl", &tbufptr)))
  110. error("Hack needs CL.");
  111. ND = tgetstr("nd", &tbufptr);
  112. if (tgetflag("os"))
  113. error("Hack can't have OS.");
  114. CE = tgetstr("ce", &tbufptr);
  115. UP = tgetstr("up", &tbufptr);
  116. /*
  117. * It seems that xd is no longer supported, and we should use a
  118. * linefeed instead; unfortunately this requires resetting CRMOD, and
  119. * many output routines will have to be modified slightly. Let's
  120. * leave that till the next release.
  121. */
  122. XD = tgetstr("xd", &tbufptr);
  123. /* not: XD = tgetstr("do", &tbufptr); */
  124. if (!(CM = tgetstr("cm", &tbufptr))) {
  125. if (!UP && !HO)
  126. error("Hack needs CM or UP or HO.");
  127. printf("Playing hack on terminals without cm is suspect...\n");
  128. getret();
  129. }
  130. SO = tgetstr("so", &tbufptr);
  131. SE = tgetstr("se", &tbufptr);
  132. SG = tgetnum("sg"); /* -1: not fnd; else # of spaces left by so */
  133. if (!SO || !SE || (SG > 0))
  134. SO = SE = 0;
  135. CD = tgetstr("cd", &tbufptr);
  136. set_whole_screen(); /* uses LI and CD */
  137. if (tbufptr - tbuf > (int)sizeof(tbuf))
  138. error("TERMCAP entry too big...\n");
  139. free(tptr);
  140. }
  141. void
  142. start_screen()
  143. {
  144. xputs(TI);
  145. xputs(VS);
  146. }
  147. void
  148. end_screen()
  149. {
  150. xputs(VE);
  151. xputs(TE);
  152. }
  153. /* Cursor movements */
  154. void
  155. curs(x, y)
  156. int x, y; /* not xchar: perhaps xchar is unsigned and
  157. * curx-x would be unsigned as well */
  158. {
  159. if (y == cury && x == curx)
  160. return;
  161. if (!ND && (curx != x || x <= 3)) { /* Extremely primitive */
  162. cmov(x, y); /* bunker!wtm */
  163. return;
  164. }
  165. if (abs(cury - y) <= 3 && abs(curx - x) <= 3)
  166. nocmov(x, y);
  167. else if ((x <= 3 && abs(cury - y) <= 3) || (!CM && x < abs(curx - x))) {
  168. (void) putchar('\r');
  169. curx = 1;
  170. nocmov(x, y);
  171. } else if (!CM) {
  172. nocmov(x, y);
  173. } else
  174. cmov(x, y);
  175. }
  176. void
  177. nocmov(x, y)
  178. int x, y;
  179. {
  180. if (cury > y) {
  181. if (UP) {
  182. while (cury > y) { /* Go up. */
  183. xputs(UP);
  184. cury--;
  185. }
  186. } else if (CM) {
  187. cmov(x, y);
  188. } else if (HO) {
  189. home();
  190. curs(x, y);
  191. } /* else impossible("..."); */
  192. } else if (cury < y) {
  193. if (XD) {
  194. while (cury < y) {
  195. xputs(XD);
  196. cury++;
  197. }
  198. } else if (CM) {
  199. cmov(x, y);
  200. } else {
  201. while (cury < y) {
  202. xputc('\n');
  203. curx = 1;
  204. cury++;
  205. }
  206. }
  207. }
  208. if (curx < x) { /* Go to the right. */
  209. if (!ND)
  210. cmov(x, y);
  211. else /* bah */
  212. /* should instead print what is there already */
  213. while (curx < x) {
  214. xputs(ND);
  215. curx++;
  216. }
  217. } else if (curx > x) {
  218. while (curx > x) { /* Go to the left. */
  219. xputs(BC);
  220. curx--;
  221. }
  222. }
  223. }
  224. void
  225. cmov(x, y)
  226. int x, y;
  227. {
  228. xputs(tgoto(CM, x - 1, y - 1));
  229. cury = y;
  230. curx = x;
  231. }
  232. int
  233. xputc(c)
  234. char c;
  235. {
  236. return (fputc(c, stdout));
  237. }
  238. void
  239. xputs(s)
  240. char *s;
  241. {
  242. tputs(s, 1, xputc);
  243. }
  244. void
  245. cl_end()
  246. {
  247. if (CE)
  248. xputs(CE);
  249. else { /* no-CE fix - free after Harold Rynes */
  250. /*
  251. * this looks terrible, especially on a slow terminal but is
  252. * better than nothing
  253. */
  254. int cx = curx, cy = cury;
  255. while (curx < COLNO) {
  256. xputc(' ');
  257. curx++;
  258. }
  259. curs(cx, cy);
  260. }
  261. }
  262. void
  263. clear_screen()
  264. {
  265. xputs(CL);
  266. curx = cury = 1;
  267. }
  268. void
  269. home()
  270. {
  271. if (HO)
  272. xputs(HO);
  273. else if (CM)
  274. xputs(tgoto(CM, 0, 0));
  275. else
  276. curs(1, 1); /* using UP ... */
  277. curx = cury = 1;
  278. }
  279. void
  280. standoutbeg()
  281. {
  282. if (SO)
  283. xputs(SO);
  284. }
  285. void
  286. standoutend()
  287. {
  288. if (SE)
  289. xputs(SE);
  290. }
  291. void
  292. backsp()
  293. {
  294. xputs(BC);
  295. curx--;
  296. }
  297. void
  298. bell()
  299. {
  300. (void) putchar('\007'); /* curx does not change */
  301. (void) fflush(stdout);
  302. }
  303. void
  304. delay_output()
  305. {
  306. /* delay 50 ms - could also use a 'nap'-system call */
  307. /* or the usleep call like this :-) */
  308. usleep(50000);
  309. }
  310. void
  311. cl_eos()
  312. { /* free after Robert Viduya *//* must only be
  313. * called with curx = 1 */
  314. if (CD)
  315. xputs(CD);
  316. else {
  317. int cx = curx, cy = cury;
  318. while (cury <= LI - 2) {
  319. cl_end();
  320. xputc('\n');
  321. curx = 1;
  322. cury++;
  323. }
  324. cl_end();
  325. curs(cx, cy);
  326. }
  327. }