Home | History | Annotate | Line # | Download | only in hack
hack.topl.c revision 1.10
      1  1.10  dholland /*	$NetBSD: hack.topl.c,v 1.10 2009/06/07 20:13:18 dholland Exp $	*/
      2   1.4  christos 
      3   1.2   mycroft /*
      4   1.7       jsm  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
      5   1.7       jsm  * Amsterdam
      6   1.7       jsm  * All rights reserved.
      7   1.7       jsm  *
      8   1.7       jsm  * Redistribution and use in source and binary forms, with or without
      9   1.7       jsm  * modification, are permitted provided that the following conditions are
     10   1.7       jsm  * met:
     11   1.7       jsm  *
     12   1.7       jsm  * - Redistributions of source code must retain the above copyright notice,
     13   1.7       jsm  * this list of conditions and the following disclaimer.
     14   1.7       jsm  *
     15   1.7       jsm  * - Redistributions in binary form must reproduce the above copyright
     16   1.7       jsm  * notice, this list of conditions and the following disclaimer in the
     17   1.7       jsm  * documentation and/or other materials provided with the distribution.
     18   1.7       jsm  *
     19   1.7       jsm  * - Neither the name of the Stichting Centrum voor Wiskunde en
     20   1.7       jsm  * Informatica, nor the names of its contributors may be used to endorse or
     21   1.7       jsm  * promote products derived from this software without specific prior
     22   1.7       jsm  * written permission.
     23   1.7       jsm  *
     24   1.7       jsm  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     25   1.7       jsm  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26   1.7       jsm  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     27   1.7       jsm  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     28   1.7       jsm  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29   1.7       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30   1.7       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     31   1.7       jsm  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     32   1.7       jsm  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33   1.7       jsm  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34   1.7       jsm  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35   1.7       jsm  */
     36   1.7       jsm 
     37   1.7       jsm /*
     38   1.7       jsm  * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
     39   1.7       jsm  * All rights reserved.
     40   1.7       jsm  *
     41   1.7       jsm  * Redistribution and use in source and binary forms, with or without
     42   1.7       jsm  * modification, are permitted provided that the following conditions
     43   1.7       jsm  * are met:
     44   1.7       jsm  * 1. Redistributions of source code must retain the above copyright
     45   1.7       jsm  *    notice, this list of conditions and the following disclaimer.
     46   1.7       jsm  * 2. Redistributions in binary form must reproduce the above copyright
     47   1.7       jsm  *    notice, this list of conditions and the following disclaimer in the
     48   1.7       jsm  *    documentation and/or other materials provided with the distribution.
     49   1.7       jsm  * 3. The name of the author may not be used to endorse or promote products
     50   1.7       jsm  *    derived from this software without specific prior written permission.
     51   1.7       jsm  *
     52   1.7       jsm  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     53   1.7       jsm  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     54   1.7       jsm  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     55   1.7       jsm  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     56   1.7       jsm  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     57   1.7       jsm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     58   1.7       jsm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     59   1.7       jsm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     60   1.7       jsm  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     61   1.7       jsm  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62   1.2   mycroft  */
     63   1.2   mycroft 
     64   1.4  christos #include <sys/cdefs.h>
     65   1.2   mycroft #ifndef lint
     66  1.10  dholland __RCSID("$NetBSD: hack.topl.c,v 1.10 2009/06/07 20:13:18 dholland Exp $");
     67   1.4  christos #endif				/* not lint */
     68   1.1       cgd 
     69   1.4  christos #include <stdlib.h>
     70   1.1       cgd #include "hack.h"
     71   1.4  christos #include "extern.h"
     72   1.1       cgd 
     73   1.4  christos char            toplines[BUFSZ];
     74   1.4  christos xchar           tlx, tly;	/* set by pline; used by addtopl */
     75   1.1       cgd 
     76   1.1       cgd struct topl {
     77   1.4  christos 	struct topl    *next_topl;
     78   1.4  christos 	char           *topl_text;
     79   1.4  christos }              *old_toplines, *last_redone_topl;
     80   1.1       cgd #define	OTLMAX	20		/* max nr of old toplines remembered */
     81   1.1       cgd 
     82   1.4  christos int
     83   1.9  dholland doredotopl(void)
     84   1.4  christos {
     85   1.4  christos 	if (last_redone_topl)
     86   1.1       cgd 		last_redone_topl = last_redone_topl->next_topl;
     87   1.4  christos 	if (!last_redone_topl)
     88   1.1       cgd 		last_redone_topl = old_toplines;
     89   1.4  christos 	if (last_redone_topl) {
     90   1.1       cgd 		(void) strcpy(toplines, last_redone_topl->topl_text);
     91   1.1       cgd 	}
     92   1.1       cgd 	redotoplin();
     93   1.4  christos 	return (0);
     94   1.1       cgd }
     95   1.1       cgd 
     96   1.4  christos void
     97   1.9  dholland redotoplin(void)
     98   1.4  christos {
     99   1.1       cgd 	home();
    100   1.4  christos 	if (strchr(toplines, '\n'))
    101   1.4  christos 		cl_end();
    102   1.1       cgd 	putstr(toplines);
    103   1.1       cgd 	cl_end();
    104   1.1       cgd 	tlx = curx;
    105   1.1       cgd 	tly = cury;
    106   1.1       cgd 	flags.toplin = 1;
    107   1.4  christos 	if (tly > 1)
    108   1.1       cgd 		more();
    109   1.1       cgd }
    110   1.1       cgd 
    111   1.4  christos void
    112   1.9  dholland remember_topl(void)
    113   1.4  christos {
    114   1.4  christos 	struct topl    *tl;
    115   1.4  christos 	int             cnt = OTLMAX;
    116   1.4  christos 	if (last_redone_topl &&
    117   1.4  christos 	    !strcmp(toplines, last_redone_topl->topl_text))
    118   1.4  christos 		return;
    119   1.4  christos 	if (old_toplines &&
    120   1.4  christos 	    !strcmp(toplines, old_toplines->topl_text))
    121   1.4  christos 		return;
    122   1.1       cgd 	last_redone_topl = 0;
    123   1.1       cgd 	tl = (struct topl *)
    124   1.4  christos 		alloc((unsigned) (strlen(toplines) + sizeof(struct topl) + 1));
    125   1.1       cgd 	tl->next_topl = old_toplines;
    126   1.4  christos 	tl->topl_text = (char *) (tl + 1);
    127   1.1       cgd 	(void) strcpy(tl->topl_text, toplines);
    128   1.1       cgd 	old_toplines = tl;
    129   1.4  christos 	while (cnt && tl) {
    130   1.1       cgd 		cnt--;
    131   1.1       cgd 		tl = tl->next_topl;
    132   1.1       cgd 	}
    133   1.4  christos 	if (tl && tl->next_topl) {
    134   1.1       cgd 		free((char *) tl->next_topl);
    135   1.1       cgd 		tl->next_topl = 0;
    136   1.1       cgd 	}
    137   1.1       cgd }
    138   1.1       cgd 
    139   1.4  christos void
    140   1.9  dholland addtopl(const char *s)
    141   1.4  christos {
    142   1.4  christos 	curs(tlx, tly);
    143   1.8  dholland 	if (tlx + (int)strlen(s) > CO)
    144   1.4  christos 		putsym('\n');
    145   1.1       cgd 	putstr(s);
    146   1.1       cgd 	tlx = curx;
    147   1.1       cgd 	tly = cury;
    148   1.1       cgd 	flags.toplin = 1;
    149   1.1       cgd }
    150   1.1       cgd 
    151   1.9  dholland /* s = allowed chars besides space/return */
    152   1.4  christos void
    153   1.9  dholland xmore(const char *s)
    154   1.1       cgd {
    155   1.4  christos 	if (flags.toplin) {
    156   1.1       cgd 		curs(tlx, tly);
    157   1.4  christos 		if (tlx + 8 > CO)
    158   1.4  christos 			putsym('\n'), tly++;
    159   1.1       cgd 	}
    160   1.4  christos 	if (flags.standout)
    161   1.1       cgd 		standoutbeg();
    162   1.1       cgd 	putstr("--More--");
    163   1.4  christos 	if (flags.standout)
    164   1.1       cgd 		standoutend();
    165   1.1       cgd 
    166   1.1       cgd 	xwaitforspace(s);
    167   1.4  christos 	if (flags.toplin && tly > 1) {
    168   1.1       cgd 		home();
    169   1.1       cgd 		cl_end();
    170   1.4  christos 		docorner(1, tly - 1);
    171   1.1       cgd 	}
    172   1.1       cgd 	flags.toplin = 0;
    173   1.1       cgd }
    174   1.1       cgd 
    175   1.4  christos void
    176   1.9  dholland more(void)
    177   1.4  christos {
    178   1.1       cgd 	xmore("");
    179   1.1       cgd }
    180   1.1       cgd 
    181   1.4  christos void
    182   1.9  dholland cmore(const char *s)
    183   1.1       cgd {
    184   1.1       cgd 	xmore(s);
    185   1.1       cgd }
    186   1.1       cgd 
    187   1.4  christos void
    188   1.9  dholland clrlin(void)
    189   1.4  christos {
    190   1.4  christos 	if (flags.toplin) {
    191   1.1       cgd 		home();
    192   1.1       cgd 		cl_end();
    193   1.4  christos 		if (tly > 1)
    194   1.4  christos 			docorner(1, tly - 1);
    195   1.1       cgd 		remember_topl();
    196   1.1       cgd 	}
    197   1.1       cgd 	flags.toplin = 0;
    198   1.1       cgd }
    199   1.1       cgd 
    200   1.4  christos void
    201   1.4  christos pline(const char *fmt, ...)
    202   1.4  christos {
    203   1.4  christos 	va_list ap;
    204   1.6       wiz 
    205   1.4  christos 	va_start(ap, fmt);
    206   1.4  christos 	vpline(fmt, ap);
    207   1.4  christos 	va_end(ap);
    208   1.4  christos }
    209   1.4  christos 
    210   1.4  christos void
    211   1.9  dholland vpline(const char *line, va_list ap)
    212   1.4  christos {
    213   1.4  christos 	char            pbuf[BUFSZ];
    214   1.4  christos 	char           *bp = pbuf, *tl;
    215   1.4  christos 	int             n, n0;
    216   1.4  christos 
    217   1.4  christos 	if (!line || !*line)
    218   1.4  christos 		return;
    219   1.4  christos 	if (!strchr(line, '%'))
    220  1.10  dholland 		(void) strlcpy(pbuf, line, sizeof(pbuf));
    221   1.4  christos 	else
    222  1.10  dholland 		(void) vsnprintf(pbuf, sizeof(pbuf), line, ap);
    223   1.4  christos 	if (flags.toplin == 1 && !strcmp(pbuf, toplines))
    224   1.4  christos 		return;
    225   1.4  christos 	nscr();			/* %% */
    226   1.1       cgd 
    227   1.1       cgd 	/* If there is room on the line, print message on same line */
    228   1.1       cgd 	/* But messages like "You die..." deserve their own line */
    229   1.1       cgd 	n0 = strlen(bp);
    230   1.4  christos 	if (flags.toplin == 1 && tly == 1 &&
    231   1.8  dholland 	    n0 + (int)strlen(toplines) + 3 < CO - 8 &&	/* leave room for
    232   1.4  christos 							 * --More-- */
    233   1.1       cgd 	    strncmp(bp, "You ", 4)) {
    234   1.1       cgd 		(void) strcat(toplines, "  ");
    235   1.1       cgd 		(void) strcat(toplines, bp);
    236   1.1       cgd 		tlx += 2;
    237   1.1       cgd 		addtopl(bp);
    238   1.1       cgd 		return;
    239   1.1       cgd 	}
    240   1.4  christos 	if (flags.toplin == 1)
    241   1.4  christos 		more();
    242   1.1       cgd 	remember_topl();
    243   1.1       cgd 	toplines[0] = 0;
    244   1.4  christos 	while (n0) {
    245   1.4  christos 		if (n0 >= CO) {
    246   1.1       cgd 			/* look for appropriate cut point */
    247   1.1       cgd 			n0 = 0;
    248   1.4  christos 			for (n = 0; n < CO; n++)
    249   1.4  christos 				if (bp[n] == ' ')
    250   1.4  christos 					n0 = n;
    251   1.4  christos 			if (!n0)
    252   1.4  christos 				for (n = 0; n < CO - 1; n++)
    253   1.4  christos 					if (!letter(bp[n]))
    254   1.4  christos 						n0 = n;
    255   1.4  christos 			if (!n0)
    256   1.4  christos 				n0 = CO - 2;
    257   1.1       cgd 		}
    258   1.1       cgd 		(void) strncpy((tl = eos(toplines)), bp, n0);
    259   1.1       cgd 		tl[n0] = 0;
    260   1.1       cgd 		bp += n0;
    261   1.1       cgd 
    262   1.1       cgd 		/* remove trailing spaces, but leave one */
    263   1.4  christos 		while (n0 > 1 && tl[n0 - 1] == ' ' && tl[n0 - 2] == ' ')
    264   1.1       cgd 			tl[--n0] = 0;
    265   1.1       cgd 
    266   1.1       cgd 		n0 = strlen(bp);
    267   1.4  christos 		if (n0 && tl[0])
    268   1.4  christos 			(void) strcat(tl, "\n");
    269   1.1       cgd 	}
    270   1.1       cgd 	redotoplin();
    271   1.1       cgd }
    272   1.1       cgd 
    273   1.4  christos void
    274   1.9  dholland putsym(int c1)
    275   1.4  christos {
    276   1.9  dholland 	char c = c1; /* XXX this hack prevents .o diffs -- remove later */
    277   1.9  dholland 
    278   1.4  christos 	switch (c) {
    279   1.1       cgd 	case '\b':
    280   1.1       cgd 		backsp();
    281   1.1       cgd 		return;
    282   1.1       cgd 	case '\n':
    283   1.1       cgd 		curx = 1;
    284   1.1       cgd 		cury++;
    285   1.4  christos 		if (cury > tly)
    286   1.4  christos 			tly = cury;
    287   1.1       cgd 		break;
    288   1.1       cgd 	default:
    289   1.4  christos 		if (curx == CO)
    290   1.1       cgd 			putsym('\n');	/* 1 <= curx <= CO; avoid CO */
    291   1.1       cgd 		else
    292   1.1       cgd 			curx++;
    293   1.1       cgd 	}
    294   1.1       cgd 	(void) putchar(c);
    295   1.1       cgd }
    296   1.1       cgd 
    297   1.4  christos void
    298   1.9  dholland putstr(const char *s)
    299   1.4  christos {
    300   1.4  christos 	while (*s)
    301   1.4  christos 		putsym(*s++);
    302   1.1       cgd }
    303