1*84d9c625SLionel Sambuc /* $NetBSD: addchnstr.c,v 1.6 2013/11/09 11:16:59 blymn Exp $ */
251ffecc1SBen Gras
351ffecc1SBen Gras /*
451ffecc1SBen Gras * Copyright (c) 2003 The NetBSD Foundation, Inc.
551ffecc1SBen Gras * All rights reserved.
651ffecc1SBen Gras *
751ffecc1SBen Gras * This code is derived from software contributed to The NetBSD Foundation
851ffecc1SBen Gras * by Douwe Kiela (virtus@wanadoo.nl).
951ffecc1SBen Gras *
1051ffecc1SBen Gras * Redistribution and use in source and binary forms, with or without
1151ffecc1SBen Gras * modification, are permitted provided that the following conditions
1251ffecc1SBen Gras * are met:
1351ffecc1SBen Gras * 1. Redistributions of source code must retain the above copyright
1451ffecc1SBen Gras * notice, this list of conditions and the following disclaimer.
1551ffecc1SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
1651ffecc1SBen Gras * notice, this list of conditions and the following disclaimer in the
1751ffecc1SBen Gras * documentation and/or other materials provided with the distribution.
1851ffecc1SBen Gras *
1951ffecc1SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2051ffecc1SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2151ffecc1SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2251ffecc1SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2351ffecc1SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2451ffecc1SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2551ffecc1SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2651ffecc1SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2751ffecc1SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2851ffecc1SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2951ffecc1SBen Gras * POSSIBILITY OF SUCH DAMAGE.
3051ffecc1SBen Gras */
3151ffecc1SBen Gras
3251ffecc1SBen Gras #include <sys/cdefs.h>
3351ffecc1SBen Gras #ifndef lint
34*84d9c625SLionel Sambuc __RCSID("$NetBSD: addchnstr.c,v 1.6 2013/11/09 11:16:59 blymn Exp $");
3551ffecc1SBen Gras #endif /* not lint */
3651ffecc1SBen Gras
3751ffecc1SBen Gras #include <stdlib.h>
3851ffecc1SBen Gras
3951ffecc1SBen Gras #include "curses.h"
4051ffecc1SBen Gras #include "curses_private.h"
4151ffecc1SBen Gras
4251ffecc1SBen Gras #ifndef _CURSES_USE_MACROS
4351ffecc1SBen Gras
4451ffecc1SBen Gras /*
4551ffecc1SBen Gras * addchstr --
4651ffecc1SBen Gras * Add a string to stdscr starting at (_cury, _curx).
4751ffecc1SBen Gras */
4851ffecc1SBen Gras int
addchstr(const chtype * chstr)4951ffecc1SBen Gras addchstr(const chtype *chstr)
5051ffecc1SBen Gras {
5151ffecc1SBen Gras return waddchnstr(stdscr, chstr, -1);
5251ffecc1SBen Gras }
5351ffecc1SBen Gras
5451ffecc1SBen Gras /*
5551ffecc1SBen Gras * waddchstr --
5651ffecc1SBen Gras * Add a string to the given window starting at (_cury, _curx).
5751ffecc1SBen Gras */
5851ffecc1SBen Gras int
waddchstr(WINDOW * win,const chtype * chstr)5951ffecc1SBen Gras waddchstr(WINDOW *win, const chtype *chstr)
6051ffecc1SBen Gras {
6151ffecc1SBen Gras return waddchnstr(win, chstr, -1);
6251ffecc1SBen Gras }
6351ffecc1SBen Gras
6451ffecc1SBen Gras /*
6551ffecc1SBen Gras * addchnstr --
6651ffecc1SBen Gras * Add a string (at most n characters) to stdscr starting
6751ffecc1SBen Gras * at (_cury, _curx). If n is negative, add the entire string.
6851ffecc1SBen Gras */
6951ffecc1SBen Gras int
addchnstr(const chtype * chstr,int n)7051ffecc1SBen Gras addchnstr(const chtype *chstr, int n)
7151ffecc1SBen Gras {
7251ffecc1SBen Gras return waddchnstr(stdscr, chstr, n);
7351ffecc1SBen Gras }
7451ffecc1SBen Gras
7551ffecc1SBen Gras /*
7651ffecc1SBen Gras * mvaddchstr --
7751ffecc1SBen Gras * Add a string to stdscr starting at (y, x)
7851ffecc1SBen Gras */
7951ffecc1SBen Gras int
mvaddchstr(int y,int x,const chtype * chstr)8051ffecc1SBen Gras mvaddchstr(int y, int x, const chtype *chstr)
8151ffecc1SBen Gras {
8251ffecc1SBen Gras return mvwaddchnstr(stdscr, y, x, chstr, -1);
8351ffecc1SBen Gras }
8451ffecc1SBen Gras
8551ffecc1SBen Gras /*
8651ffecc1SBen Gras * mvwaddchstr --
8751ffecc1SBen Gras * Add a string to the given window starting at (y, x)
8851ffecc1SBen Gras */
8951ffecc1SBen Gras int
mvwaddchstr(WINDOW * win,int y,int x,const chtype * chstr)9051ffecc1SBen Gras mvwaddchstr(WINDOW *win, int y, int x, const chtype *chstr)
9151ffecc1SBen Gras {
9251ffecc1SBen Gras return mvwaddchnstr(win, y, x, chstr, -1);
9351ffecc1SBen Gras }
9451ffecc1SBen Gras
9551ffecc1SBen Gras /*
9651ffecc1SBen Gras * mvaddchnstr --
9751ffecc1SBen Gras * Add a string of at most n characters to stdscr
9851ffecc1SBen Gras * starting at (y, x).
9951ffecc1SBen Gras */
10051ffecc1SBen Gras int
mvaddchnstr(int y,int x,const chtype * chstr,int n)10151ffecc1SBen Gras mvaddchnstr(int y, int x, const chtype *chstr, int n)
10251ffecc1SBen Gras {
10351ffecc1SBen Gras return mvwaddchnstr(stdscr, y, x, chstr, n);
10451ffecc1SBen Gras }
10551ffecc1SBen Gras
10651ffecc1SBen Gras /*
10751ffecc1SBen Gras * mvwaddchnstr --
10851ffecc1SBen Gras * Add a string of at most n characters to the given window
10951ffecc1SBen Gras * starting at (y, x).
11051ffecc1SBen Gras */
11151ffecc1SBen Gras int
mvwaddchnstr(WINDOW * win,int y,int x,const chtype * chstr,int n)11251ffecc1SBen Gras mvwaddchnstr(WINDOW *win, int y, int x, const chtype *chstr, int n)
11351ffecc1SBen Gras {
11451ffecc1SBen Gras if (wmove(win, y, x) == ERR)
11551ffecc1SBen Gras return ERR;
11651ffecc1SBen Gras
11751ffecc1SBen Gras return waddchnstr(win, chstr, n);
11851ffecc1SBen Gras }
11951ffecc1SBen Gras
12051ffecc1SBen Gras #endif
12151ffecc1SBen Gras
12251ffecc1SBen Gras /*
12351ffecc1SBen Gras * waddchnstr --
12451ffecc1SBen Gras * Add a string (at most n characters) to the given window
1250c3ae37fSLionel Sambuc * starting at (_cury, _curx) until the end of line is reached or
1260c3ae37fSLionel Sambuc * n characters have been added. If n is negative, add as much
1270c3ae37fSLionel Sambuc * of the string that will fit on the current line. SUSv2 says
1280c3ae37fSLionel Sambuc * that the addchnstr family does not wrap and strings are truncated
1290c3ae37fSLionel Sambuc * to the RHS of the window.
13051ffecc1SBen Gras */
13151ffecc1SBen Gras int
waddchnstr(WINDOW * win,const chtype * chstr,int n)13251ffecc1SBen Gras waddchnstr(WINDOW *win, const chtype *chstr, int n)
13351ffecc1SBen Gras {
13451ffecc1SBen Gras size_t len;
13551ffecc1SBen Gras const chtype *chp;
13651ffecc1SBen Gras attr_t attr;
13751ffecc1SBen Gras char *ocp, *cp, *start;
138*84d9c625SLionel Sambuc int i, ret, ox, oy;
13951ffecc1SBen Gras
14051ffecc1SBen Gras #ifdef DEBUG
14151ffecc1SBen Gras __CTRACE(__CTRACE_INPUT, "waddchnstr: win = %p, chstr = %p, n = %d\n",
14251ffecc1SBen Gras win, chstr, n);
14351ffecc1SBen Gras #endif
14451ffecc1SBen Gras
14551ffecc1SBen Gras if (n >= 0)
14651ffecc1SBen Gras for (chp = chstr, len = 0; n-- && *chp++; ++len);
14751ffecc1SBen Gras else
14851ffecc1SBen Gras for (chp = chstr, len = 0; *chp++; ++len);
14951ffecc1SBen Gras
1500c3ae37fSLionel Sambuc /* check if string is too long for current location */
1510c3ae37fSLionel Sambuc if (len > (win->maxx - win->curx))
1520c3ae37fSLionel Sambuc len = win->maxx - win->curx;
1530c3ae37fSLionel Sambuc
15451ffecc1SBen Gras if ((ocp = malloc(len + 1)) == NULL)
15551ffecc1SBen Gras return ERR;
15651ffecc1SBen Gras chp = chstr;
15751ffecc1SBen Gras cp = ocp;
15851ffecc1SBen Gras start = ocp;
15951ffecc1SBen Gras i = 0;
16051ffecc1SBen Gras attr = (*chp) & __ATTRIBUTES;
161*84d9c625SLionel Sambuc ox = win->curx;
162*84d9c625SLionel Sambuc oy = win->cury;
16351ffecc1SBen Gras while (len) {
16451ffecc1SBen Gras *cp = (*chp) & __CHARTEXT;
16551ffecc1SBen Gras cp++;
16651ffecc1SBen Gras chp++;
16751ffecc1SBen Gras i++;
16851ffecc1SBen Gras len--;
16951ffecc1SBen Gras if (((*chp) & __ATTRIBUTES) != attr) {
17051ffecc1SBen Gras *cp = '\0';
171*84d9c625SLionel Sambuc if (_cursesi_waddbytes(win, start, i, attr, 0) == ERR) {
17251ffecc1SBen Gras free(ocp);
17351ffecc1SBen Gras return ERR;
17451ffecc1SBen Gras }
17551ffecc1SBen Gras attr = (*chp) & __ATTRIBUTES;
17651ffecc1SBen Gras start = cp;
17751ffecc1SBen Gras i = 0;
17851ffecc1SBen Gras }
17951ffecc1SBen Gras }
18051ffecc1SBen Gras *cp = '\0';
181*84d9c625SLionel Sambuc ret = _cursesi_waddbytes(win, start, i, attr, 0);
18251ffecc1SBen Gras free(ocp);
183*84d9c625SLionel Sambuc wmove(win, oy, ox);
18451ffecc1SBen Gras return ret;
18551ffecc1SBen Gras }
186