Home | History | Annotate | Line # | Download | only in hack
hack.topl.c revision 1.2
      1 /*
      2  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
      3  */
      4 
      5 #ifndef lint
      6 static char rcsid[] = "$Id: hack.topl.c,v 1.2 1993/08/02 17:19:30 mycroft Exp $";
      7 #endif /* not lint */
      8 
      9 #include "hack.h"
     10 #include <stdio.h>
     11 extern char *eos();
     12 extern int CO;
     13 
     14 char toplines[BUFSZ];
     15 xchar tlx, tly;			/* set by pline; used by addtopl */
     16 
     17 struct topl {
     18 	struct topl *next_topl;
     19 	char *topl_text;
     20 } *old_toplines, *last_redone_topl;
     21 #define	OTLMAX	20		/* max nr of old toplines remembered */
     22 
     23 doredotopl(){
     24 	if(last_redone_topl)
     25 		last_redone_topl = last_redone_topl->next_topl;
     26 	if(!last_redone_topl)
     27 		last_redone_topl = old_toplines;
     28 	if(last_redone_topl){
     29 		(void) strcpy(toplines, last_redone_topl->topl_text);
     30 	}
     31 	redotoplin();
     32 	return(0);
     33 }
     34 
     35 redotoplin() {
     36 	home();
     37 	if(index(toplines, '\n')) cl_end();
     38 	putstr(toplines);
     39 	cl_end();
     40 	tlx = curx;
     41 	tly = cury;
     42 	flags.toplin = 1;
     43 	if(tly > 1)
     44 		more();
     45 }
     46 
     47 remember_topl() {
     48 register struct topl *tl;
     49 register int cnt = OTLMAX;
     50 	if(last_redone_topl &&
     51 	   !strcmp(toplines, last_redone_topl->topl_text)) return;
     52 	if(old_toplines &&
     53 	   !strcmp(toplines, old_toplines->topl_text)) return;
     54 	last_redone_topl = 0;
     55 	tl = (struct topl *)
     56 		alloc((unsigned)(strlen(toplines) + sizeof(struct topl) + 1));
     57 	tl->next_topl = old_toplines;
     58 	tl->topl_text = (char *)(tl + 1);
     59 	(void) strcpy(tl->topl_text, toplines);
     60 	old_toplines = tl;
     61 	while(cnt && tl){
     62 		cnt--;
     63 		tl = tl->next_topl;
     64 	}
     65 	if(tl && tl->next_topl){
     66 		free((char *) tl->next_topl);
     67 		tl->next_topl = 0;
     68 	}
     69 }
     70 
     71 addtopl(s) char *s; {
     72 	curs(tlx,tly);
     73 	if(tlx + strlen(s) > CO) putsym('\n');
     74 	putstr(s);
     75 	tlx = curx;
     76 	tly = cury;
     77 	flags.toplin = 1;
     78 }
     79 
     80 xmore(s)
     81 char *s;	/* allowed chars besides space/return */
     82 {
     83 	if(flags.toplin) {
     84 		curs(tlx, tly);
     85 		if(tlx + 8 > CO) putsym('\n'), tly++;
     86 	}
     87 
     88 	if(flags.standout)
     89 		standoutbeg();
     90 	putstr("--More--");
     91 	if(flags.standout)
     92 		standoutend();
     93 
     94 	xwaitforspace(s);
     95 	if(flags.toplin && tly > 1) {
     96 		home();
     97 		cl_end();
     98 		docorner(1, tly-1);
     99 	}
    100 	flags.toplin = 0;
    101 }
    102 
    103 more(){
    104 	xmore("");
    105 }
    106 
    107 cmore(s)
    108 register char *s;
    109 {
    110 	xmore(s);
    111 }
    112 
    113 clrlin(){
    114 	if(flags.toplin) {
    115 		home();
    116 		cl_end();
    117 		if(tly > 1) docorner(1, tly-1);
    118 		remember_topl();
    119 	}
    120 	flags.toplin = 0;
    121 }
    122 
    123 /*VARARGS1*/
    124 pline(line,arg1,arg2,arg3,arg4,arg5,arg6)
    125 register char *line,*arg1,*arg2,*arg3,*arg4,*arg5,*arg6;
    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);
    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 }
    198