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