X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/4506ee6421a65ebf465cb3e1124f0ba604dbcb20..cd13042d23065cae90a02dae4f7602a2b4eb7ce0:/hack/hack.topl.c?ds=inline diff --git a/hack/hack.topl.c b/hack/hack.topl.c index ff32e307..d99b76f8 100644 --- a/hack/hack.topl.c +++ b/hack/hack.topl.c @@ -1,29 +1,89 @@ -/* $NetBSD: hack.topl.c,v 1.6 2002/05/26 00:12:12 wiz Exp $ */ +/* $NetBSD: hack.topl.c,v 1.14 2011/08/06 20:29:37 dholland Exp $ */ /* - * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. + * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica, + * Amsterdam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of the Stichting Centrum voor Wiskunde en + * Informatica, nor the names of its contributors may be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER + * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Copyright (c) 1982 Jay Fenlason + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #ifndef lint -__RCSID("$NetBSD: hack.topl.c,v 1.6 2002/05/26 00:12:12 wiz Exp $"); +__RCSID("$NetBSD: hack.topl.c,v 1.14 2011/08/06 20:29:37 dholland Exp $"); #endif /* not lint */ #include #include "hack.h" #include "extern.h" -char toplines[BUFSZ]; -xchar tlx, tly; /* set by pline; used by addtopl */ +static char toplines[BUFSZ]; +static xchar tlx, tly; /* set by pline; used by addtopl */ -struct topl { +static struct topl { struct topl *next_topl; char *topl_text; } *old_toplines, *last_redone_topl; #define OTLMAX 20 /* max nr of old toplines remembered */ +static void redotoplin(void); +static void xmore(const char *); + int -doredotopl() +doredotopl(void) { if (last_redone_topl) last_redone_topl = last_redone_topl->next_topl; @@ -36,8 +96,8 @@ doredotopl() return (0); } -void -redotoplin() +static void +redotoplin(void) { home(); if (strchr(toplines, '\n')) @@ -52,7 +112,7 @@ redotoplin() } void -remember_topl() +remember_topl(void) { struct topl *tl; int cnt = OTLMAX; @@ -63,8 +123,7 @@ remember_topl() !strcmp(toplines, old_toplines->topl_text)) return; last_redone_topl = 0; - tl = (struct topl *) - alloc((unsigned) (strlen(toplines) + sizeof(struct topl) + 1)); + tl = alloc(strlen(toplines) + sizeof(*tl) + 1); tl->next_topl = old_toplines; tl->topl_text = (char *) (tl + 1); (void) strcpy(tl->topl_text, toplines); @@ -74,17 +133,16 @@ remember_topl() tl = tl->next_topl; } if (tl && tl->next_topl) { - free((char *) tl->next_topl); + free(tl->next_topl); tl->next_topl = 0; } } void -addtopl(s) - const char *s; +addtopl(const char *s) { curs(tlx, tly); - if (tlx + strlen(s) > CO) + if (tlx + (int)strlen(s) > CO) putsym('\n'); putstr(s); tlx = curx; @@ -92,9 +150,9 @@ addtopl(s) flags.toplin = 1; } -void -xmore(s) - const char *s; /* allowed chars besides space/return */ +/* s = allowed chars besides space/return */ +static void +xmore(const char *s) { if (flags.toplin) { curs(tlx, tly); @@ -117,20 +175,19 @@ xmore(s) } void -more() +more(void) { xmore(""); } void -cmore(s) - const char *s; +cmore(const char *s) { xmore(s); } void -clrlin() +clrlin(void) { if (flags.toplin) { home(); @@ -153,20 +210,18 @@ pline(const char *fmt, ...) } void -vpline(line, ap) - const char *line; - va_list ap; +vpline(const char *line, va_list ap) { char pbuf[BUFSZ]; char *bp = pbuf, *tl; - int n, n0; + int n, n0, tlpos, dead; if (!line || !*line) return; if (!strchr(line, '%')) - (void) strcpy(pbuf, line); + (void) strlcpy(pbuf, line, sizeof(pbuf)); else - (void) vsprintf(pbuf, line, ap); + (void) vsnprintf(pbuf, sizeof(pbuf), line, ap); if (flags.toplin == 1 && !strcmp(pbuf, toplines)) return; nscr(); /* %% */ @@ -175,7 +230,7 @@ vpline(line, ap) /* But messages like "You die..." deserve their own line */ n0 = strlen(bp); if (flags.toplin == 1 && tly == 1 && - n0 + strlen(toplines) + 3 < CO - 8 && /* leave room for + n0 + (int)strlen(toplines) + 3 < CO - 8 && /* leave room for * --More-- */ strncmp(bp, "You ", 4)) { (void) strcat(toplines, " "); @@ -187,8 +242,9 @@ vpline(line, ap) if (flags.toplin == 1) more(); remember_topl(); + dead = 0; toplines[0] = 0; - while (n0) { + while (n0 && !dead) { if (n0 >= CO) { /* look for appropriate cut point */ n0 = 0; @@ -202,7 +258,14 @@ vpline(line, ap) if (!n0) n0 = CO - 2; } - (void) strncpy((tl = eos(toplines)), bp, n0); + tlpos = strlen(toplines); + tl = toplines + tlpos; + /* avoid overflow */ + if (tlpos + n0 > (int)sizeof(toplines) - 1) { + n0 = sizeof(toplines) - 1 - tlpos; + dead = 1; + } + (void) memcpy(tl, bp, n0); tl[n0] = 0; bp += n0; @@ -212,15 +275,16 @@ vpline(line, ap) n0 = strlen(bp); if (n0 && tl[0]) - (void) strcat(tl, "\n"); + (void) strlcat(toplines, "\n", sizeof(toplines)); } redotoplin(); } void -putsym(c) - char c; +putsym(int c1) { + char c = c1; /* XXX this hack prevents .o diffs -- remove later */ + switch (c) { case '\b': backsp(); @@ -241,8 +305,7 @@ putsym(c) } void -putstr(s) - const char *s; +putstr(const char *s) { while (*s) putsym(*s++);