Source:NetHack 2.2a/topl.c
Jump to navigation
Jump to search
Below is the full text to topl.c from the source code of NetHack 2.2a.
Warning! This is the source code from an old release. For new releases, see Source code
Screenshots and source code from Hack are used under the CWI license.
1. /* SCCS Id: @(#)topl.c 2.0 87/09/15 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. 4. #include <stdio.h> 5. #include "hack.h" 6. #ifdef GENIX 7. #define void int /* jhn - mod to prevent compiler from bombing */ 8. #endif 9. 10. extern char *eos(); 11. extern int CO; 12. 13. char toplines[BUFSIZ]; 14. xchar tlx, tly; /* set by pline; used by addtopl */ 15. 16. struct topl { 17. struct topl *next_topl; 18. char *topl_text; 19. } *old_toplines, *last_redone_topl; 20. #define OTLMAX 20 /* max nr of old toplines remembered */ 21. 22. doredotopl(){ 23. if(last_redone_topl) 24. last_redone_topl = last_redone_topl->next_topl; 25. if(!last_redone_topl) 26. last_redone_topl = old_toplines; 27. if(last_redone_topl){ 28. (void) strcpy(toplines, last_redone_topl->topl_text); 29. } 30. redotoplin(); 31. return(0); 32. } 33. 34. redotoplin() { 35. home(); 36. if(index(toplines, '\n')) cl_end(); 37. putstr(toplines); 38. cl_end(); 39. tlx = curx; 40. tly = cury; 41. flags.toplin = 1; 42. if(tly > 1) 43. more(); 44. } 45. 46. remember_topl() { 47. register struct topl *tl; 48. register int cnt = OTLMAX; 49. if(last_redone_topl && 50. !strcmp(toplines, last_redone_topl->topl_text)) return; 51. if(old_toplines && 52. !strcmp(toplines, old_toplines->topl_text)) return; 53. last_redone_topl = 0; 54. tl = (struct topl *) 55. alloc((unsigned)(strlen(toplines) + sizeof(struct topl) + 1)); 56. tl->next_topl = old_toplines; 57. tl->topl_text = (char *)(tl + 1); 58. (void) strcpy(tl->topl_text, toplines); 59. old_toplines = tl; 60. while(cnt && tl){ 61. cnt--; 62. tl = tl->next_topl; 63. } 64. if(tl && tl->next_topl){ 65. free((char *) tl->next_topl); 66. tl->next_topl = 0; 67. } 68. } 69. 70. addtopl(s) char *s; { 71. curs(tlx,tly); 72. if(tlx + strlen(s) > CO) putsym('\n'); 73. putstr(s); 74. tlx = curx; 75. tly = cury; 76. flags.toplin = 1; 77. } 78. 79. xmore(s) 80. char *s; /* allowed chars besides space/return */ 81. { 82. if(flags.toplin) { 83. curs(tlx, tly); 84. if(tlx + 8 > CO) putsym('\n'), tly++; 85. } 86. 87. if(flags.standout) 88. standoutbeg(); 89. putstr("--More--"); 90. if(flags.standout) 91. standoutend(); 92. 93. xwaitforspace(s); 94. if(flags.toplin && tly > 1) { 95. home(); 96. cl_end(); 97. docorner(1, tly-1); 98. } 99. flags.toplin = 0; 100. } 101. 102. more(){ 103. xmore(""); 104. } 105. 106. cmore(s) 107. register char *s; 108. { 109. xmore(s); 110. } 111. 112. clrlin(){ 113. if(flags.toplin) { 114. home(); 115. cl_end(); 116. if(tly > 1) docorner(1, tly-1); 117. remember_topl(); 118. } 119. flags.toplin = 0; 120. } 121. 122. /*VARARGS1*/ 123. /* Because the modified mstatusline has 9 arguments KAA */ 124. pline(line,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9) 125. register char *line,*arg1,*arg2,*arg3,*arg4,*arg5,*arg6,*arg7,*arg8,*arg9; 126. { 127. char pbuf[BUFSZ]; 128. register char *bp = pbuf, *tl; 129. register int n,n0; 130. 131. if(!line || !*line) return; 132. if(!index(line, '%')) (void) strcpy(pbuf,line); else 133. (void) sprintf(pbuf,line,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9); 134. if(flags.toplin == 1 && !strcmp(pbuf, toplines)) return; 135. nscr(); /* %% */ 136. 137. /* If there is room on the line, print message on same line */ 138. /* But messages like "You die..." deserve their own line */ 139. n0 = strlen(bp); 140. if(flags.toplin == 1 && tly == 1 && 141. n0 + strlen(toplines) + 3 < CO-8 && /* leave room for --More-- */ 142. strncmp(bp, "You ", 4)) { 143. (void) strcat(toplines, " "); 144. (void) strcat(toplines, bp); 145. tlx += 2; 146. addtopl(bp); 147. return; 148. } 149. if(flags.toplin == 1) more(); 150. remember_topl(); 151. toplines[0] = 0; 152. while(n0){ 153. if(n0 >= CO){ 154. /* look for appropriate cut point */ 155. n0 = 0; 156. for(n = 0; n < CO; n++) if(bp[n] == ' ') 157. n0 = n; 158. if(!n0) for(n = 0; n < CO-1; n++) 159. if(!letter(bp[n])) n0 = n; 160. if(!n0) n0 = CO-2; 161. } 162. (void) strncpy((tl = eos(toplines)), bp, n0); 163. tl[n0] = 0; 164. bp += n0; 165. 166. /* remove trailing spaces, but leave one */ 167. while(n0 > 1 && tl[n0-1] == ' ' && tl[n0-2] == ' ') 168. tl[--n0] = 0; 169. 170. n0 = strlen(bp); 171. if(n0 && tl[0]) (void) strcat(tl, "\n"); 172. } 173. redotoplin(); 174. } 175. 176. putsym(c) char c; { 177. switch(c) { 178. case '\b': 179. backsp(); 180. return; 181. case '\n': 182. curx = 1; 183. cury++; 184. if(cury > tly) tly = cury; 185. break; 186. default: 187. if(curx == CO) 188. putsym('\n'); /* 1 <= curx <= CO; avoid CO */ 189. else 190. curx++; 191. } 192. (void) putchar(c); 193. } 194. 195. putstr(s) register char *s; { 196. while(*s) putsym(*s++); 197. }