summaryrefslogtreecommitdiff
path: root/payloads/libpayload/curses/PDCurses/pdcurses/insch.c
blob: 67b9f4784b9ca11294201d2ce9ac2c3c37f210a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* Public Domain Curses */

#include <curspriv.h>

RCSID("$Id: insch.c,v 1.44 2008/07/13 16:08:18 wmcbrine Exp $")

/*man-start**************************************************************

  Name:                                                         insch

  Synopsis:
        int insch(chtype ch);
        int winsch(WINDOW *win, chtype ch);
        int mvinsch(int y, int x, chtype ch);
        int mvwinsch(WINDOW *win, int y, int x, chtype ch);

        int insrawch(chtype ch);
        int winsrawch(WINDOW *win, chtype ch);
        int mvinsrawch(int y, int x, chtype ch);
        int mvwinsrawch(WINDOW *win, int y, int x, chtype ch);

        int ins_wch(const cchar_t *wch);
        int wins_wch(WINDOW *win, const cchar_t *wch);
        int mvins_wch(int y, int x, const cchar_t *wch);
        int mvwins_wch(WINDOW *win, int y, int x, const cchar_t *wch);

  Description:
        The insch() functions insert a chtype into the window at the
        current or specified cursor position. The cursor is NOT
        advanced. A newline is equivalent to clrtoeol(); tabs are
        expanded; other control characters are converted as with
        unctrl().

        The ins_wch() functions are the wide-character
        equivalents, taking cchar_t pointers rather than chtypes.

        Video attributes can be combined with a character by ORing
        them into the parameter. Text, including attributes, can be
        copied from one place to another using inch() and insch().

        insrawch() etc. are PDCurses-specific wrappers for insch() etc.
        that disable the translation of control characters.

  Return Value:
        All functions return OK on success and ERR on error.

  Portability                                X/Open    BSD    SYS V
        insch                                   Y       Y       Y
        winsch                                  Y       Y       Y
        mvinsch                                 Y       Y       Y
        mvwinsch                                Y       Y       Y
        insrawch                                -       -       -
        winsrawch                               -       -       -
        ins_wch                                 Y
        wins_wch                                Y
        mvins_wch                               Y
        mvwins_wch                              Y

**man-end****************************************************************/

#include <string.h>

int winsch(WINDOW *win, chtype ch)
{
    int x, y;
    chtype attr;
    bool xlat;

    PDC_LOG(("winsch() - called: win=%p ch=%x (text=%c attr=0x%x)\n",
             win, ch, ch & A_CHARTEXT, ch & A_ATTRIBUTES));

    if (!win)
        return ERR;

    x = win->_curx;
    y = win->_cury;

    if (y > win->_maxy || x > win->_maxx || y < 0 || x < 0)
        return ERR;

    xlat = !SP->raw_out && !(ch & A_ALTCHARSET);
    attr = ch & A_ATTRIBUTES;
    ch &= A_CHARTEXT;

    if (xlat && (ch < ' ' || ch == 0x7f))
    {
        int x2;

        switch (ch)
        {
        case '\t':
            for (x2 = ((x / TABSIZE) + 1) * TABSIZE; x < x2; x++)
            {
                if (winsch(win, attr | ' ') == ERR)
                    return ERR;
            }
            return OK;

        case '\n':
            wclrtoeol(win);
            break;

        case 0x7f:
            if (winsch(win, attr | '?') == ERR)
                return ERR;

            return winsch(win, attr | '^');

        default:
            /* handle control chars */

            if (winsch(win, attr | (ch + '@')) == ERR)
                return ERR;

            return winsch(win, attr | '^');
        }
    }
    else
    {
        int maxx;
        chtype *temp;

        /* If the incoming character doesn't have its own attribute,
           then use the current attributes for the window. If it has
           attributes but not a color component, OR the attributes to
           the current attributes for the window. If it has a color
           component, use the attributes solely from the incoming
           character. */

        if (!(attr & A_COLOR))
            attr |= win->_attrs;

        /* wrs (4/10/93): Apply the same sort of logic for the window
           background, in that it only takes precedence if other color
           attributes are not there and that the background character
           will only print if the printing character is blank. */

        if (!(attr & A_COLOR))
            attr |= win->_bkgd & A_ATTRIBUTES;
        else
            attr |= win->_bkgd & (A_ATTRIBUTES ^ A_COLOR);

        if (ch == ' ')
            ch = win->_bkgd & A_CHARTEXT;

        /* Add the attribute back into the character. */

        ch |= attr;

        maxx = win->_maxx;
        temp = &win->_y[y][x];

        memmove(temp + 1, temp, (maxx - x - 1) * sizeof(chtype));

        win->_lastch[y] = maxx - 1;

        if ((win->_firstch[y] == _NO_CHANGE) || (win->_firstch[y] > x))
            win->_firstch[y] = x;

        *temp = ch;
    }

    PDC_sync(win);

    return OK;
}

int insch(chtype ch)
{
    PDC_LOG(("insch() - called\n"));

    return winsch(stdscr, ch);
}

int mvinsch(int y, int x, chtype ch)
{
    PDC_LOG(("mvinsch() - called\n"));

    if (move(y, x) == ERR)
        return ERR;

    return winsch(stdscr, ch);
}

int mvwinsch(WINDOW *win, int y, int x, chtype ch)
{
    PDC_LOG(("mvwinsch() - called\n"));

    if (wmove(win, y, x) == ERR)
        return ERR;

    return winsch(win, ch);
}

int winsrawch(WINDOW *win, chtype ch)
{
    PDC_LOG(("winsrawch() - called: win=%p ch=%x "
             "(char=%c attr=0x%x)\n", win, ch,
             ch & A_CHARTEXT, ch & A_ATTRIBUTES));

    if ((ch & A_CHARTEXT) < ' ' || (ch & A_CHARTEXT) == 0x7f)
        ch |= A_ALTCHARSET;

    return winsch(win, ch);
}

int insrawch(chtype ch)
{
    PDC_LOG(("insrawch() - called\n"));

    return winsrawch(stdscr, ch);
}

int mvinsrawch(int y, int x, chtype ch)
{
    PDC_LOG(("mvinsrawch() - called\n"));

    if (move(y, x) == ERR)
        return ERR;

    return winsrawch(stdscr, ch);
}

int mvwinsrawch(WINDOW *win, int y, int x, chtype ch)
{
    PDC_LOG(("mvwinsrawch() - called\n"));

    if (wmove(win, y, x) == ERR)
        return ERR;

    return winsrawch(win, ch);
}

#ifdef PDC_WIDE
int wins_wch(WINDOW *win, const cchar_t *wch)
{
    PDC_LOG(("wins_wch() - called\n"));

    return wch ? winsch(win, *wch) : ERR;
}

int ins_wch(const cchar_t *wch)
{
    PDC_LOG(("ins_wch() - called\n"));

    return wins_wch(stdscr, wch);
}

int mvins_wch(int y, int x, const cchar_t *wch)
{
    PDC_LOG(("mvins_wch() - called\n"));

    if (move(y, x) == ERR)
        return ERR;

    return wins_wch(stdscr, wch);
}

int mvwins_wch(WINDOW *win, int y, int x, const cchar_t *wch)
{
    PDC_LOG(("mvwins_wch() - called\n"));

    if (wmove(win, y, x) == ERR)
        return ERR;

    return wins_wch(win, wch);
}
#endif