libpayload: Add PDCurses and ncurses' libform/libmenu
[coreboot.git] / payloads / libpayload / curses / PDCurses-3.4 / pdcurses / insch.c
1 /* Public Domain Curses */
2
3 #include <curspriv.h>
4
5 RCSID("$Id: insch.c,v 1.44 2008/07/13 16:08:18 wmcbrine Exp $")
6
7 /*man-start**************************************************************
8
9   Name:                                                         insch
10
11   Synopsis:
12         int insch(chtype ch);
13         int winsch(WINDOW *win, chtype ch);
14         int mvinsch(int y, int x, chtype ch);
15         int mvwinsch(WINDOW *win, int y, int x, chtype ch);
16
17         int insrawch(chtype ch);
18         int winsrawch(WINDOW *win, chtype ch);
19         int mvinsrawch(int y, int x, chtype ch);
20         int mvwinsrawch(WINDOW *win, int y, int x, chtype ch);
21
22         int ins_wch(const cchar_t *wch);
23         int wins_wch(WINDOW *win, const cchar_t *wch);
24         int mvins_wch(int y, int x, const cchar_t *wch);
25         int mvwins_wch(WINDOW *win, int y, int x, const cchar_t *wch);
26
27   Description:
28         The insch() functions insert a chtype into the window at the 
29         current or specified cursor position. The cursor is NOT 
30         advanced. A newline is equivalent to clrtoeol(); tabs are 
31         expanded; other control characters are converted as with 
32         unctrl().
33
34         The ins_wch() functions are the wide-character
35         equivalents, taking cchar_t pointers rather than chtypes.
36
37         Video attributes can be combined with a character by ORing
38         them into the parameter. Text, including attributes, can be 
39         copied from one place to another using inch() and insch().
40
41         insrawch() etc. are PDCurses-specific wrappers for insch() etc. 
42         that disable the translation of control characters.
43
44   Return Value:
45         All functions return OK on success and ERR on error.
46
47   Portability                                X/Open    BSD    SYS V
48         insch                                   Y       Y       Y
49         winsch                                  Y       Y       Y
50         mvinsch                                 Y       Y       Y
51         mvwinsch                                Y       Y       Y
52         insrawch                                -       -       -
53         winsrawch                               -       -       -
54         ins_wch                                 Y
55         wins_wch                                Y
56         mvins_wch                               Y
57         mvwins_wch                              Y
58
59 **man-end****************************************************************/
60
61 #include <string.h>
62
63 int winsch(WINDOW *win, chtype ch)
64 {
65     int x, y;
66     chtype attr;
67     bool xlat;
68
69     PDC_LOG(("winsch() - called: win=%p ch=%x (text=%c attr=0x%x)\n",
70              win, ch, ch & A_CHARTEXT, ch & A_ATTRIBUTES));
71
72     if (!win)
73         return ERR;
74
75     x = win->_curx;
76     y = win->_cury;
77
78     if (y > win->_maxy || x > win->_maxx || y < 0 || x < 0)
79         return ERR;
80
81     xlat = !SP->raw_out && !(ch & A_ALTCHARSET);
82     attr = ch & A_ATTRIBUTES;
83     ch &= A_CHARTEXT;
84
85     if (xlat && (ch < ' ' || ch == 0x7f))
86     {
87         int x2;
88
89         switch (ch)
90         {
91         case '\t':
92             for (x2 = ((x / TABSIZE) + 1) * TABSIZE; x < x2; x++)
93             {
94                 if (winsch(win, attr | ' ') == ERR)
95                     return ERR;
96             }
97             return OK;
98
99         case '\n':
100             wclrtoeol(win);
101             break;
102
103         case 0x7f:
104             if (winsch(win, attr | '?') == ERR)
105                 return ERR;
106
107             return winsch(win, attr | '^');
108
109         default:
110             /* handle control chars */
111
112             if (winsch(win, attr | (ch + '@')) == ERR)
113                 return ERR;
114
115             return winsch(win, attr | '^');
116         }
117     }
118     else
119     {
120         int maxx;
121         chtype *temp;
122
123         /* If the incoming character doesn't have its own attribute,
124            then use the current attributes for the window. If it has
125            attributes but not a color component, OR the attributes to
126            the current attributes for the window. If it has a color
127            component, use the attributes solely from the incoming
128            character. */
129
130         if (!(attr & A_COLOR))
131             attr |= win->_attrs;
132
133         /* wrs (4/10/93): Apply the same sort of logic for the window
134            background, in that it only takes precedence if other color
135            attributes are not there and that the background character
136            will only print if the printing character is blank. */
137
138         if (!(attr & A_COLOR))
139             attr |= win->_bkgd & A_ATTRIBUTES;
140         else
141             attr |= win->_bkgd & (A_ATTRIBUTES ^ A_COLOR);
142
143         if (ch == ' ')
144             ch = win->_bkgd & A_CHARTEXT;
145
146         /* Add the attribute back into the character. */
147
148         ch |= attr;
149
150         maxx = win->_maxx;
151         temp = &win->_y[y][x];
152
153         memmove(temp + 1, temp, (maxx - x - 1) * sizeof(chtype));
154
155         win->_lastch[y] = maxx - 1;
156
157         if ((win->_firstch[y] == _NO_CHANGE) || (win->_firstch[y] > x))
158             win->_firstch[y] = x;
159
160         *temp = ch;
161     }
162
163     PDC_sync(win);
164
165     return OK;
166 }
167
168 int insch(chtype ch)
169 {
170     PDC_LOG(("insch() - called\n"));
171
172     return winsch(stdscr, ch);
173 }
174
175 int mvinsch(int y, int x, chtype ch)
176 {
177     PDC_LOG(("mvinsch() - called\n"));
178
179     if (move(y, x) == ERR)
180         return ERR;
181
182     return winsch(stdscr, ch);
183 }
184
185 int mvwinsch(WINDOW *win, int y, int x, chtype ch)
186 {
187     PDC_LOG(("mvwinsch() - called\n"));
188
189     if (wmove(win, y, x) == ERR)
190         return ERR;
191
192     return winsch(win, ch);
193 }
194
195 int winsrawch(WINDOW *win, chtype ch)
196 {
197     PDC_LOG(("winsrawch() - called: win=%p ch=%x "
198              "(char=%c attr=0x%x)\n", win, ch,
199              ch & A_CHARTEXT, ch & A_ATTRIBUTES));
200
201     if ((ch & A_CHARTEXT) < ' ' || (ch & A_CHARTEXT) == 0x7f)
202         ch |= A_ALTCHARSET;
203
204     return winsch(win, ch);
205 }
206
207 int insrawch(chtype ch)
208 {
209     PDC_LOG(("insrawch() - called\n"));
210
211     return winsrawch(stdscr, ch);
212 }
213
214 int mvinsrawch(int y, int x, chtype ch)
215 {
216     PDC_LOG(("mvinsrawch() - called\n"));
217
218     if (move(y, x) == ERR)
219         return ERR;
220
221     return winsrawch(stdscr, ch);
222 }
223
224 int mvwinsrawch(WINDOW *win, int y, int x, chtype ch)
225 {
226     PDC_LOG(("mvwinsrawch() - called\n"));
227
228     if (wmove(win, y, x) == ERR)
229         return ERR;
230
231     return winsrawch(win, ch);
232 }
233
234 #ifdef PDC_WIDE
235 int wins_wch(WINDOW *win, const cchar_t *wch)
236 {
237     PDC_LOG(("wins_wch() - called\n"));
238
239     return wch ? winsch(win, *wch) : ERR;
240 }
241
242 int ins_wch(const cchar_t *wch)
243 {
244     PDC_LOG(("ins_wch() - called\n"));
245
246     return wins_wch(stdscr, wch);
247 }
248
249 int mvins_wch(int y, int x, const cchar_t *wch)
250 {
251     PDC_LOG(("mvins_wch() - called\n"));
252
253     if (move(y, x) == ERR)
254         return ERR;
255
256     return wins_wch(stdscr, wch);
257 }
258
259 int mvwins_wch(WINDOW *win, int y, int x, const cchar_t *wch)
260 {
261     PDC_LOG(("mvwins_wch() - called\n"));
262
263     if (wmove(win, y, x) == ERR)
264         return ERR;
265
266     return wins_wch(win, wch);
267 }
268 #endif