1*0a6a1f1dSLionel Sambuc /* $NetBSD: addnstr.c,v 1.14 2014/07/13 01:58:08 blymn Exp $ */
251ffecc1SBen Gras
351ffecc1SBen Gras /*
451ffecc1SBen Gras * Copyright (c) 1993, 1994
551ffecc1SBen Gras * The Regents of the University of California. All rights reserved.
651ffecc1SBen Gras *
751ffecc1SBen Gras * Redistribution and use in source and binary forms, with or without
851ffecc1SBen Gras * modification, are permitted provided that the following conditions
951ffecc1SBen Gras * are met:
1051ffecc1SBen Gras * 1. Redistributions of source code must retain the above copyright
1151ffecc1SBen Gras * notice, this list of conditions and the following disclaimer.
1251ffecc1SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
1351ffecc1SBen Gras * notice, this list of conditions and the following disclaimer in the
1451ffecc1SBen Gras * documentation and/or other materials provided with the distribution.
1551ffecc1SBen Gras * 3. Neither the name of the University nor the names of its contributors
1651ffecc1SBen Gras * may be used to endorse or promote products derived from this software
1751ffecc1SBen Gras * without specific prior written permission.
1851ffecc1SBen Gras *
1951ffecc1SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2051ffecc1SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2151ffecc1SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2251ffecc1SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2351ffecc1SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2451ffecc1SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2551ffecc1SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2651ffecc1SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2751ffecc1SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2851ffecc1SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2951ffecc1SBen Gras * SUCH DAMAGE.
3051ffecc1SBen Gras */
3151ffecc1SBen Gras
3251ffecc1SBen Gras #include <sys/cdefs.h>
3351ffecc1SBen Gras #ifndef lint
3451ffecc1SBen Gras #if 0
3551ffecc1SBen Gras static char sccsid[] = "@(#)addnstr.c 8.2 (Berkeley) 5/4/94";
3651ffecc1SBen Gras #else
37*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: addnstr.c,v 1.14 2014/07/13 01:58:08 blymn Exp $");
3851ffecc1SBen Gras #endif
3951ffecc1SBen Gras #endif /* not lint */
4051ffecc1SBen Gras
4151ffecc1SBen Gras #include <string.h>
4251ffecc1SBen Gras
4351ffecc1SBen Gras #include "curses.h"
4451ffecc1SBen Gras #include "curses_private.h"
4551ffecc1SBen Gras
4651ffecc1SBen Gras #ifndef _CURSES_USE_MACROS
4751ffecc1SBen Gras
4851ffecc1SBen Gras /*
4951ffecc1SBen Gras * addstr --
5051ffecc1SBen Gras * Add a string to stdscr starting at (_cury, _curx).
5151ffecc1SBen Gras */
5251ffecc1SBen Gras int
addstr(const char * s)5351ffecc1SBen Gras addstr(const char *s)
5451ffecc1SBen Gras {
5551ffecc1SBen Gras return waddnstr(stdscr, s, -1);
5651ffecc1SBen Gras }
5751ffecc1SBen Gras
5851ffecc1SBen Gras /*
5951ffecc1SBen Gras * waddstr --
6051ffecc1SBen Gras * Add a string to the given window starting at (_cury, _curx).
6151ffecc1SBen Gras */
6251ffecc1SBen Gras int
waddstr(WINDOW * win,const char * s)6351ffecc1SBen Gras waddstr(WINDOW *win, const char *s)
6451ffecc1SBen Gras {
6551ffecc1SBen Gras return waddnstr(win, s, -1);
6651ffecc1SBen Gras }
6751ffecc1SBen Gras
6851ffecc1SBen Gras /*
6951ffecc1SBen Gras * addnstr --
7051ffecc1SBen Gras * Add a string (at most n characters) to stdscr starting
7151ffecc1SBen Gras * at (_cury, _curx). If n is negative, add the entire string.
7251ffecc1SBen Gras */
7351ffecc1SBen Gras int
addnstr(const char * str,int n)7451ffecc1SBen Gras addnstr(const char *str, int n)
7551ffecc1SBen Gras {
7651ffecc1SBen Gras return waddnstr(stdscr, str, n);
7751ffecc1SBen Gras }
7851ffecc1SBen Gras
7951ffecc1SBen Gras /*
8051ffecc1SBen Gras * mvaddstr --
8151ffecc1SBen Gras * Add a string to stdscr starting at (y, x)
8251ffecc1SBen Gras */
8351ffecc1SBen Gras int
mvaddstr(int y,int x,const char * str)8451ffecc1SBen Gras mvaddstr(int y, int x, const char *str)
8551ffecc1SBen Gras {
8651ffecc1SBen Gras return mvwaddnstr(stdscr, y, x, str, -1);
8751ffecc1SBen Gras }
8851ffecc1SBen Gras
8951ffecc1SBen Gras /*
9051ffecc1SBen Gras * mvwaddstr --
9151ffecc1SBen Gras * Add a string to the given window starting at (y, x)
9251ffecc1SBen Gras */
9351ffecc1SBen Gras int
mvwaddstr(WINDOW * win,int y,int x,const char * str)9451ffecc1SBen Gras mvwaddstr(WINDOW *win, int y, int x, const char *str)
9551ffecc1SBen Gras {
9651ffecc1SBen Gras return mvwaddnstr(win, y, x, str, -1);
9751ffecc1SBen Gras }
9851ffecc1SBen Gras
9951ffecc1SBen Gras /*
10051ffecc1SBen Gras * mvaddnstr --
10151ffecc1SBen Gras * Add a string of at most n characters to stdscr
10251ffecc1SBen Gras * starting at (y, x).
10351ffecc1SBen Gras */
10451ffecc1SBen Gras int
mvaddnstr(int y,int x,const char * str,int count)10551ffecc1SBen Gras mvaddnstr(int y, int x, const char *str, int count)
10651ffecc1SBen Gras {
10751ffecc1SBen Gras return mvwaddnstr(stdscr, y, x, str, count);
10851ffecc1SBen Gras }
10951ffecc1SBen Gras
11051ffecc1SBen Gras /*
11151ffecc1SBen Gras * mvwaddnstr --
11251ffecc1SBen Gras * Add a string of at most n characters to the given window
11351ffecc1SBen Gras * starting at (y, x).
11451ffecc1SBen Gras */
11551ffecc1SBen Gras int
mvwaddnstr(WINDOW * win,int y,int x,const char * str,int count)11651ffecc1SBen Gras mvwaddnstr(WINDOW *win, int y, int x, const char *str, int count)
11751ffecc1SBen Gras {
11851ffecc1SBen Gras if (wmove(win, y, x) == ERR)
11951ffecc1SBen Gras return ERR;
12051ffecc1SBen Gras
12151ffecc1SBen Gras return waddnstr(win, str, count);
12251ffecc1SBen Gras }
12351ffecc1SBen Gras
12451ffecc1SBen Gras #endif
12551ffecc1SBen Gras
12651ffecc1SBen Gras /*
12751ffecc1SBen Gras * waddnstr --
12851ffecc1SBen Gras * Add a string (at most n characters) to the given window
12951ffecc1SBen Gras * starting at (_cury, _curx). If n is negative, add the
13051ffecc1SBen Gras * entire string.
13151ffecc1SBen Gras */
13251ffecc1SBen Gras int
waddnstr(WINDOW * win,const char * s,int n)13351ffecc1SBen Gras waddnstr(WINDOW *win, const char *s, int n)
13451ffecc1SBen Gras {
13551ffecc1SBen Gras size_t len;
13651ffecc1SBen Gras const char *p;
13751ffecc1SBen Gras
13851ffecc1SBen Gras #ifdef DEBUG
13951ffecc1SBen Gras __CTRACE(__CTRACE_INPUT, "ADDNSTR: win %p, length %d\n",
14051ffecc1SBen Gras win, n);
14151ffecc1SBen Gras #endif
14251ffecc1SBen Gras /*
14351ffecc1SBen Gras * behavior changed from traditional BSD curses, for better XCURSES
14451ffecc1SBen Gras * conformance.
14551ffecc1SBen Gras *
14651ffecc1SBen Gras * BSD curses: if (n > 0) then "at most n", else "len = strlen(s)"
14751ffecc1SBen Gras * ncurses: if (n >= 0) then "at most n", else "len = strlen(s)"
14851ffecc1SBen Gras * XCURSES: if (n != -1) then "at most n", else "len = strlen(s)"
1490c3ae37fSLionel Sambuc *
15051ffecc1SBen Gras */
15151ffecc1SBen Gras if (n >= 0)
15251ffecc1SBen Gras for (p = s, len = 0; n-- && *p++; ++len);
15351ffecc1SBen Gras else
15451ffecc1SBen Gras len = strlen(s);
1550c3ae37fSLionel Sambuc
15651ffecc1SBen Gras return(waddbytes(win, s, (int) len));
15751ffecc1SBen Gras }
158