Source:Hack 1.0/hack.topl.c

Below is the full text to hack.topl.c from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/hack.topl.c#line123 ]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

1.   /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */ 2.     3.    #include "hack.h"  4.    #include   5.    extern char *eos; 6.   #define	TOPLSZ	(COLNO-8)	/* leave room for --More-- */ 7.   char toplines[BUFSZ]; 8.   xchar tlx, tly;			/* set by pline; used by addtopl */ 9.    10.   struct topl { 11.  	struct topl *next_topl; 12.  	char *topl_text; 13.  } *old_toplines, *last_redone_topl; 14.  #define	OTLMAX	20		/* max nr of old toplines remembered */ 15.   16.   doredotopl{ 17.  	if(last_redone_topl) 18.  		last_redone_topl = last_redone_topl->next_topl; 19.  	if(!last_redone_topl) 20.  		last_redone_topl = old_toplines; 21.  	if(last_redone_topl){ 22.  		(void) strcpy(toplines, last_redone_topl->topl_text); 23.  	}  24.   	redotoplin; 25.  	return(0); 26.  }  27.    28.   redotoplin { 29.  	home; 30.  	if(index(toplines, '\n')) cl_end; 31.  	putstr(toplines); 32.  	cl_end; 33.  	tlx = curx; 34.  	tly = cury; 35.  	flags.topl = 1; 36.  	if(tly > 1) 37.  		more; 38.  }  39.    40.   remember_topl { 41.  register struct topl *tl; 42.  register int cnt = OTLMAX; 43.  	if(last_redone_topl &&  44.   	   !strcmp(toplines, last_redone_topl->topl_text)) return; 45.  	if(old_toplines &&  46.   	   !strcmp(toplines, old_toplines->topl_text)) return; 47.  	last_redone_topl = 0; 48.  	tl = (struct topl *) 49.  		alloc((unsigned)(strlen(toplines) + sizeof(struct topl) + 1)); 50.  	tl->next_topl = old_toplines; 51.  	tl->topl_text = (char *)(tl + 1); 52.  	(void) strcpy(tl->topl_text, toplines); 53.  	old_toplines = tl; 54.  	while(cnt && tl){ 55.  		cnt--; 56.  		tl = tl->next_topl; 57.  	}  58.   	if(tl && tl->next_topl){ 59.  		free((char *) tl->next_topl); 60.  		tl->next_topl = 0; 61.  	}  62.   }  63.    64.   addtopl(s) char *s; { 65.  	curs(tlx,tly); 66.  	if(tlx + strlen(s) > COLNO) putsym('\n'); 67.  	putstr(s); 68.  	tlx = curx; 69.  	tly = cury; 70.  	flags.topl = 1; 71.  }  72.    73.   xmore(spaceflag) 74.  boolean spaceflag;	/* TRUE if space required */ 75.  {  76.   	if(flags.topl) { 77.  		curs(tlx, tly); 78.  		if(tlx + 8 > COLNO) putsym('\n'), tly++; 79.  	}  80.   	putstr("--More--"); 81.  	xwaitforspace(spaceflag); 82.  	if(flags.topl && tly > 1) { 83.  		home; 84.  		cl_end; 85.  		docorner(1, tly-1); 86.  	}  87.   	flags.topl = 0; 88.  }  89.    90.   more{ 91.  	xmore(TRUE); 92.  }  93.    94.   cmore{ 95.  	xmore(FALSE); 96.  }  97.    98.   clrlin{ 99.  	if(flags.topl) { 100. 		home; 101. 		cl_end; 102. 		if(tly > 1) docorner(1, tly-1); 103. 		remember_topl; 104. 	}  105.  	flags.topl = 0; 106. }  107.   108.  /*VARARGS1*/ 109. pline(line,arg1,arg2,arg3,arg4,arg5,arg6) 110. register char *line,*arg1,*arg2,*arg3,*arg4,*arg5,*arg6; 111. {  112.  	char pbuf[BUFSZ]; 113. 	register char *bp = pbuf, *tl; 114. 	register int n,n0; 115.  116.  	if(!line || !*line) return; 117. 	if(!index(line, '%')) (void) strcpy(pbuf,line); else 118. 	(void) sprintf(pbuf,line,arg1,arg2,arg3,arg4,arg5,arg6); 119. 	if(flags.topl == 1 && !strcmp(pbuf, toplines)) return; 120. 	nscr;		/* %% */ 121.  122.  	/* If there is room on the line, print message on same line */ 123. 	/* But messages like "You die..." deserve their own line */ 124. 	n0 = strlen(bp); 125. 	if(flags.topl == 1 && tly == 1 &&  126.  	    n0 + strlen(toplines) + 3 < TOPLSZ &&  127.  	    strncmp(bp, "You ", 4)) { 128. 		(void) strcat(toplines, "  "); 129. 		(void) strcat(toplines, bp); 130. 		tlx += 2; 131. 		addtopl(bp); 132. 		return; 133. 	}  134.  	if(flags.topl == 1) more; 135. 	remember_topl; 136. 	toplines[0] = 0; 137. 	while(n0){ 138. 		if(n0 >= COLNO){ 139. 			/* look for appropriate cut point */ 140. 			n0 = 0; 141. 			for(n = 0; n < COLNO; n++) if(bp[n] == ' ') 142. 				n0 = n;  143. if(!n0) for(n = 0; n < COLNO-1; n++) 144. 				if(!letter(bp[n])) n0 = n;  145. if(!n0) n0 = COLNO-2; 146. 		}  147.  		(void) strncpy((tl = eos(toplines)), bp, n0); 148. 		tl[n0] = 0; 149. 		bp += n0; 150.  151.  		/* remove trailing spaces, but leave one */ 152. 		while(n0 > 1 && tl[n0-1] == ' ' && tl[n0-2] == ' ') 153. 			tl[--n0] = 0; 154.  155.  		n0 = strlen(bp); 156. 		if(n0 && tl[0]) (void) strcat(tl, "\n"); 157. 	}  158.  	redotoplin; 159. }  160.   161.  putsym(c) char c; { 162. 	switch(c) { 163. 	case '\b': 164. 		backsp; 165. 		return; 166. 	case '\n': 167. 		curx = 1; 168. 		cury++; 169. 		if(cury > tly) tly = cury; 170. 		break; 171. 	default: 172. 		curx++; 173. 		if(curx == COLNO) putsym('\n'); 174. 	}  175.  	(void) putchar(c); 176. }  177.   178.  putstr(s) register char *s; { 179. 	while(*s) putsym(*s++); 180. }