Home | History | Annotate | Line # | Download | only in hack
hack.topl.c revision 1.5
      1  1.5       jsm /*	$NetBSD: hack.topl.c,v 1.5 2001/03/25 20:44:03 jsm Exp $	*/
      2  1.4  christos 
      3  1.2   mycroft /*
      4  1.2   mycroft  * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
      5  1.2   mycroft  */
      6  1.2   mycroft 
      7  1.4  christos #include <sys/cdefs.h>
      8  1.2   mycroft #ifndef lint
      9  1.5       jsm __RCSID("$NetBSD: hack.topl.c,v 1.5 2001/03/25 20:44:03 jsm Exp $");
     10  1.4  christos #endif				/* not lint */
     11  1.1       cgd 
     12  1.4  christos #include <stdlib.h>
     13  1.1       cgd #include "hack.h"
     14  1.4  christos #include "extern.h"
     15  1.1       cgd 
     16  1.4  christos char            toplines[BUFSZ];
     17  1.4  christos xchar           tlx, tly;	/* set by pline; used by addtopl */
     18  1.1       cgd 
     19  1.1       cgd struct topl {
     20  1.4  christos 	struct topl    *next_topl;
     21  1.4  christos 	char           *topl_text;
     22  1.4  christos }              *old_toplines, *last_redone_topl;
     23  1.1       cgd #define	OTLMAX	20		/* max nr of old toplines remembered */
     24  1.1       cgd 
     25  1.4  christos int
     26  1.4  christos doredotopl()
     27  1.4  christos {
     28  1.4  christos 	if (last_redone_topl)
     29  1.1       cgd 		last_redone_topl = last_redone_topl->next_topl;
     30  1.4  christos 	if (!last_redone_topl)
     31  1.1       cgd 		last_redone_topl = old_toplines;
     32  1.4  christos 	if (last_redone_topl) {
     33  1.1       cgd 		(void) strcpy(toplines, last_redone_topl->topl_text);
     34  1.1       cgd 	}
     35  1.1       cgd 	redotoplin();
     36  1.4  christos 	return (0);
     37  1.1       cgd }
     38  1.1       cgd 
     39  1.4  christos void
     40  1.4  christos redotoplin()
     41  1.4  christos {
     42  1.1       cgd 	home();
     43  1.4  christos 	if (strchr(toplines, '\n'))
     44  1.4  christos 		cl_end();
     45  1.1       cgd 	putstr(toplines);
     46  1.1       cgd 	cl_end();
     47  1.1       cgd 	tlx = curx;
     48  1.1       cgd 	tly = cury;
     49  1.1       cgd 	flags.toplin = 1;
     50  1.4  christos 	if (tly > 1)
     51  1.1       cgd 		more();
     52  1.1       cgd }
     53  1.1       cgd 
     54  1.4  christos void
     55  1.4  christos remember_topl()
     56  1.4  christos {
     57  1.4  christos 	struct topl    *tl;
     58  1.4  christos 	int             cnt = OTLMAX;
     59  1.4  christos 	if (last_redone_topl &&
     60  1.4  christos 	    !strcmp(toplines, last_redone_topl->topl_text))
     61  1.4  christos 		return;
     62  1.4  christos 	if (old_toplines &&
     63  1.4  christos 	    !strcmp(toplines, old_toplines->topl_text))
     64  1.4  christos 		return;
     65  1.1       cgd 	last_redone_topl = 0;
     66  1.1       cgd 	tl = (struct topl *)
     67  1.4  christos 		alloc((unsigned) (strlen(toplines) + sizeof(struct topl) + 1));
     68  1.1       cgd 	tl->next_topl = old_toplines;
     69  1.4  christos 	tl->topl_text = (char *) (tl + 1);
     70  1.1       cgd 	(void) strcpy(tl->topl_text, toplines);
     71  1.1       cgd 	old_toplines = tl;
     72  1.4  christos 	while (cnt && tl) {
     73  1.1       cgd 		cnt--;
     74  1.1       cgd 		tl = tl->next_topl;
     75  1.1       cgd 	}
     76  1.4  christos 	if (tl && tl->next_topl) {
     77  1.1       cgd 		free((char *) tl->next_topl);
     78  1.1       cgd 		tl->next_topl = 0;
     79  1.1       cgd 	}
     80  1.1       cgd }
     81  1.1       cgd 
     82  1.4  christos void
     83  1.4  christos addtopl(s)
     84  1.5       jsm 	const char           *s;
     85  1.4  christos {
     86  1.4  christos 	curs(tlx, tly);
     87  1.4  christos 	if (tlx + strlen(s) > CO)
     88  1.4  christos 		putsym('\n');
     89  1.1       cgd 	putstr(s);
     90  1.1       cgd 	tlx = curx;
     91  1.1       cgd 	tly = cury;
     92  1.1       cgd 	flags.toplin = 1;
     93  1.1       cgd }
     94  1.1       cgd 
     95  1.4  christos void
     96  1.1       cgd xmore(s)
     97  1.5       jsm 	const char *s;	/* allowed chars besides space/return */
     98  1.1       cgd {
     99  1.4  christos 	if (flags.toplin) {
    100  1.1       cgd 		curs(tlx, tly);
    101  1.4  christos 		if (tlx + 8 > CO)
    102  1.4  christos 			putsym('\n'), tly++;
    103  1.1       cgd 	}
    104  1.4  christos 	if (flags.standout)
    105  1.1       cgd 		standoutbeg();
    106  1.1       cgd 	putstr("--More--");
    107  1.4  christos 	if (flags.standout)
    108  1.1       cgd 		standoutend();
    109  1.1       cgd 
    110  1.1       cgd 	xwaitforspace(s);
    111  1.4  christos 	if (flags.toplin && tly > 1) {
    112  1.1       cgd 		home();
    113  1.1       cgd 		cl_end();
    114  1.4  christos 		docorner(1, tly - 1);
    115  1.1       cgd 	}
    116  1.1       cgd 	flags.toplin = 0;
    117  1.1       cgd }
    118  1.1       cgd 
    119  1.4  christos void
    120  1.4  christos more()
    121  1.4  christos {
    122  1.1       cgd 	xmore("");
    123  1.1       cgd }
    124  1.1       cgd 
    125  1.4  christos void
    126  1.1       cgd cmore(s)
    127  1.5       jsm 	const char           *s;
    128  1.1       cgd {
    129  1.1       cgd 	xmore(s);
    130  1.1       cgd }
    131  1.1       cgd 
    132  1.4  christos void
    133  1.4  christos clrlin()
    134  1.4  christos {
    135  1.4  christos 	if (flags.toplin) {
    136  1.1       cgd 		home();
    137  1.1       cgd 		cl_end();
    138  1.4  christos 		if (tly > 1)
    139  1.4  christos 			docorner(1, tly - 1);
    140  1.1       cgd 		remember_topl();
    141  1.1       cgd 	}
    142  1.1       cgd 	flags.toplin = 0;
    143  1.1       cgd }
    144  1.1       cgd 
    145  1.4  christos void
    146  1.4  christos #ifdef __STDC__
    147  1.4  christos pline(const char *fmt, ...)
    148  1.4  christos #else
    149  1.4  christos pline(va_alist)
    150  1.4  christos 	va_dcl
    151  1.4  christos #endif
    152  1.4  christos {
    153  1.4  christos 	va_list ap;
    154  1.4  christos #ifndef __STDC__
    155  1.4  christos 	const char *fmt;
    156  1.4  christos 	va_start(ap);
    157  1.4  christos 	fmt = va_arg(ap, const char *);
    158  1.4  christos #else
    159  1.4  christos 	va_start(ap, fmt);
    160  1.4  christos #endif
    161  1.4  christos 	vpline(fmt, ap);
    162  1.4  christos 	va_end(ap);
    163  1.4  christos }
    164  1.4  christos 
    165  1.4  christos void
    166  1.4  christos vpline(line, ap)
    167  1.4  christos 	const char *line;
    168  1.4  christos 	va_list ap;
    169  1.4  christos {
    170  1.4  christos 	char            pbuf[BUFSZ];
    171  1.4  christos 	char           *bp = pbuf, *tl;
    172  1.4  christos 	int             n, n0;
    173  1.4  christos 
    174  1.4  christos 	if (!line || !*line)
    175  1.4  christos 		return;
    176  1.4  christos 	if (!strchr(line, '%'))
    177  1.4  christos 		(void) strcpy(pbuf, line);
    178  1.4  christos 	else
    179  1.4  christos 		(void) vsprintf(pbuf, line, ap);
    180  1.4  christos 	if (flags.toplin == 1 && !strcmp(pbuf, toplines))
    181  1.4  christos 		return;
    182  1.4  christos 	nscr();			/* %% */
    183  1.1       cgd 
    184  1.1       cgd 	/* If there is room on the line, print message on same line */
    185  1.1       cgd 	/* But messages like "You die..." deserve their own line */
    186  1.1       cgd 	n0 = strlen(bp);
    187  1.4  christos 	if (flags.toplin == 1 && tly == 1 &&
    188  1.4  christos 	    n0 + strlen(toplines) + 3 < CO - 8 &&	/* leave room for
    189  1.4  christos 							 * --More-- */
    190  1.1       cgd 	    strncmp(bp, "You ", 4)) {
    191  1.1       cgd 		(void) strcat(toplines, "  ");
    192  1.1       cgd 		(void) strcat(toplines, bp);
    193  1.1       cgd 		tlx += 2;
    194  1.1       cgd 		addtopl(bp);
    195  1.1       cgd 		return;
    196  1.1       cgd 	}
    197  1.4  christos 	if (flags.toplin == 1)
    198  1.4  christos 		more();
    199  1.1       cgd 	remember_topl();
    200  1.1       cgd 	toplines[0] = 0;
    201  1.4  christos 	while (n0) {
    202  1.4  christos 		if (n0 >= CO) {
    203  1.1       cgd 			/* look for appropriate cut point */
    204  1.1       cgd 			n0 = 0;
    205  1.4  christos 			for (n = 0; n < CO; n++)
    206  1.4  christos 				if (bp[n] == ' ')
    207  1.4  christos 					n0 = n;
    208  1.4  christos 			if (!n0)
    209  1.4  christos 				for (n = 0; n < CO - 1; n++)
    210  1.4  christos 					if (!letter(bp[n]))
    211  1.4  christos 						n0 = n;
    212  1.4  christos 			if (!n0)
    213  1.4  christos 				n0 = CO - 2;
    214  1.1       cgd 		}
    215  1.1       cgd 		(void) strncpy((tl = eos(toplines)), bp, n0);
    216  1.1       cgd 		tl[n0] = 0;
    217  1.1       cgd 		bp += n0;
    218  1.1       cgd 
    219  1.1       cgd 		/* remove trailing spaces, but leave one */
    220  1.4  christos 		while (n0 > 1 && tl[n0 - 1] == ' ' && tl[n0 - 2] == ' ')
    221  1.1       cgd 			tl[--n0] = 0;
    222  1.1       cgd 
    223  1.1       cgd 		n0 = strlen(bp);
    224  1.4  christos 		if (n0 && tl[0])
    225  1.4  christos 			(void) strcat(tl, "\n");
    226  1.1       cgd 	}
    227  1.1       cgd 	redotoplin();
    228  1.1       cgd }
    229  1.1       cgd 
    230  1.4  christos void
    231  1.4  christos putsym(c)
    232  1.4  christos 	char            c;
    233  1.4  christos {
    234  1.4  christos 	switch (c) {
    235  1.1       cgd 	case '\b':
    236  1.1       cgd 		backsp();
    237  1.1       cgd 		return;
    238  1.1       cgd 	case '\n':
    239  1.1       cgd 		curx = 1;
    240  1.1       cgd 		cury++;
    241  1.4  christos 		if (cury > tly)
    242  1.4  christos 			tly = cury;
    243  1.1       cgd 		break;
    244  1.1       cgd 	default:
    245  1.4  christos 		if (curx == CO)
    246  1.1       cgd 			putsym('\n');	/* 1 <= curx <= CO; avoid CO */
    247  1.1       cgd 		else
    248  1.1       cgd 			curx++;
    249  1.1       cgd 	}
    250  1.1       cgd 	(void) putchar(c);
    251  1.1       cgd }
    252  1.1       cgd 
    253  1.4  christos void
    254  1.4  christos putstr(s)
    255  1.5       jsm 	const char           *s;
    256  1.4  christos {
    257  1.4  christos 	while (*s)
    258  1.4  christos 		putsym(*s++);
    259  1.1       cgd }
    260