hack.topl.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /* $NetBSD: hack.topl.c,v 1.7 2003/04/02 18:36:41 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
  4. * Amsterdam
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. *
  14. * - 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. *
  18. * - Neither the name of the Stichting Centrum voor Wiskunde en
  19. * Informatica, nor the names of its contributors may be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  24. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  25. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  26. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  27. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  29. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  30. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. /*
  36. * Copyright (c) 1982 Jay Fenlason <hack@gnu.org>
  37. * All rights reserved.
  38. *
  39. * Redistribution and use in source and binary forms, with or without
  40. * modification, are permitted provided that the following conditions
  41. * are met:
  42. * 1. Redistributions of source code must retain the above copyright
  43. * notice, this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright
  45. * notice, this list of conditions and the following disclaimer in the
  46. * documentation and/or other materials provided with the distribution.
  47. * 3. The name of the author may not be used to endorse or promote products
  48. * derived from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  51. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  52. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  53. * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  54. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  55. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  56. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  57. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  58. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  59. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. */
  61. #include <sys/cdefs.h>
  62. #ifndef lint
  63. __RCSID("$NetBSD: hack.topl.c,v 1.7 2003/04/02 18:36:41 jsm Exp $");
  64. #endif /* not lint */
  65. #include <stdlib.h>
  66. #include "hack.h"
  67. #include "extern.h"
  68. char toplines[BUFSZ];
  69. xchar tlx, tly; /* set by pline; used by addtopl */
  70. struct topl {
  71. struct topl *next_topl;
  72. char *topl_text;
  73. } *old_toplines, *last_redone_topl;
  74. #define OTLMAX 20 /* max nr of old toplines remembered */
  75. int
  76. doredotopl()
  77. {
  78. if (last_redone_topl)
  79. last_redone_topl = last_redone_topl->next_topl;
  80. if (!last_redone_topl)
  81. last_redone_topl = old_toplines;
  82. if (last_redone_topl) {
  83. (void) strcpy(toplines, last_redone_topl->topl_text);
  84. }
  85. redotoplin();
  86. return (0);
  87. }
  88. void
  89. redotoplin()
  90. {
  91. home();
  92. if (strchr(toplines, '\n'))
  93. cl_end();
  94. putstr(toplines);
  95. cl_end();
  96. tlx = curx;
  97. tly = cury;
  98. flags.toplin = 1;
  99. if (tly > 1)
  100. more();
  101. }
  102. void
  103. remember_topl()
  104. {
  105. struct topl *tl;
  106. int cnt = OTLMAX;
  107. if (last_redone_topl &&
  108. !strcmp(toplines, last_redone_topl->topl_text))
  109. return;
  110. if (old_toplines &&
  111. !strcmp(toplines, old_toplines->topl_text))
  112. return;
  113. last_redone_topl = 0;
  114. tl = (struct topl *)
  115. alloc((unsigned) (strlen(toplines) + sizeof(struct topl) + 1));
  116. tl->next_topl = old_toplines;
  117. tl->topl_text = (char *) (tl + 1);
  118. (void) strcpy(tl->topl_text, toplines);
  119. old_toplines = tl;
  120. while (cnt && tl) {
  121. cnt--;
  122. tl = tl->next_topl;
  123. }
  124. if (tl && tl->next_topl) {
  125. free((char *) tl->next_topl);
  126. tl->next_topl = 0;
  127. }
  128. }
  129. void
  130. addtopl(s)
  131. const char *s;
  132. {
  133. curs(tlx, tly);
  134. if (tlx + (int)strlen(s) > CO)
  135. putsym('\n');
  136. putstr(s);
  137. tlx = curx;
  138. tly = cury;
  139. flags.toplin = 1;
  140. }
  141. void
  142. xmore(s)
  143. const char *s; /* allowed chars besides space/return */
  144. {
  145. if (flags.toplin) {
  146. curs(tlx, tly);
  147. if (tlx + 8 > CO)
  148. putsym('\n'), tly++;
  149. }
  150. if (flags.standout)
  151. standoutbeg();
  152. putstr("--More--");
  153. if (flags.standout)
  154. standoutend();
  155. xwaitforspace(s);
  156. if (flags.toplin && tly > 1) {
  157. home();
  158. cl_end();
  159. docorner(1, tly - 1);
  160. }
  161. flags.toplin = 0;
  162. }
  163. void
  164. more()
  165. {
  166. xmore("");
  167. }
  168. void
  169. cmore(s)
  170. const char *s;
  171. {
  172. xmore(s);
  173. }
  174. void
  175. clrlin()
  176. {
  177. if (flags.toplin) {
  178. home();
  179. cl_end();
  180. if (tly > 1)
  181. docorner(1, tly - 1);
  182. remember_topl();
  183. }
  184. flags.toplin = 0;
  185. }
  186. void
  187. pline(const char *fmt, ...)
  188. {
  189. va_list ap;
  190. va_start(ap, fmt);
  191. vpline(fmt, ap);
  192. va_end(ap);
  193. }
  194. void
  195. vpline(line, ap)
  196. const char *line;
  197. va_list ap;
  198. {
  199. char pbuf[BUFSZ];
  200. char *bp = pbuf, *tl;
  201. int n, n0;
  202. if (!line || !*line)
  203. return;
  204. if (!strchr(line, '%'))
  205. (void) strcpy(pbuf, line);
  206. else
  207. (void) vsprintf(pbuf, line, ap);
  208. if (flags.toplin == 1 && !strcmp(pbuf, toplines))
  209. return;
  210. nscr(); /* %% */
  211. /* If there is room on the line, print message on same line */
  212. /* But messages like "You die..." deserve their own line */
  213. n0 = strlen(bp);
  214. if (flags.toplin == 1 && tly == 1 &&
  215. n0 + (int)strlen(toplines) + 3 < CO - 8 && /* leave room for
  216. * --More-- */
  217. strncmp(bp, "You ", 4)) {
  218. (void) strcat(toplines, " ");
  219. (void) strcat(toplines, bp);
  220. tlx += 2;
  221. addtopl(bp);
  222. return;
  223. }
  224. if (flags.toplin == 1)
  225. more();
  226. remember_topl();
  227. toplines[0] = 0;
  228. while (n0) {
  229. if (n0 >= CO) {
  230. /* look for appropriate cut point */
  231. n0 = 0;
  232. for (n = 0; n < CO; n++)
  233. if (bp[n] == ' ')
  234. n0 = n;
  235. if (!n0)
  236. for (n = 0; n < CO - 1; n++)
  237. if (!letter(bp[n]))
  238. n0 = n;
  239. if (!n0)
  240. n0 = CO - 2;
  241. }
  242. (void) strncpy((tl = eos(toplines)), bp, n0);
  243. tl[n0] = 0;
  244. bp += n0;
  245. /* remove trailing spaces, but leave one */
  246. while (n0 > 1 && tl[n0 - 1] == ' ' && tl[n0 - 2] == ' ')
  247. tl[--n0] = 0;
  248. n0 = strlen(bp);
  249. if (n0 && tl[0])
  250. (void) strcat(tl, "\n");
  251. }
  252. redotoplin();
  253. }
  254. void
  255. putsym(c)
  256. char c;
  257. {
  258. switch (c) {
  259. case '\b':
  260. backsp();
  261. return;
  262. case '\n':
  263. curx = 1;
  264. cury++;
  265. if (cury > tly)
  266. tly = cury;
  267. break;
  268. default:
  269. if (curx == CO)
  270. putsym('\n'); /* 1 <= curx <= CO; avoid CO */
  271. else
  272. curx++;
  273. }
  274. (void) putchar(c);
  275. }
  276. void
  277. putstr(s)
  278. const char *s;
  279. {
  280. while (*s)
  281. putsym(*s++);
  282. }